Skip to content

Commit

Permalink
Reduce calls to context.inputDevice.getBatteryState()
Browse files Browse the repository at this point in the history
  • Loading branch information
ClassicOldSong committed Jul 14, 2024
1 parent 421cad4 commit eed71b1
Showing 1 changed file with 56 additions and 47 deletions.
103 changes: 56 additions & 47 deletions app/src/main/java/com/limelight/binding/input/ControllerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1093,67 +1093,76 @@ private static boolean areBatteryCapacitiesEqual(float first, float second) {

// This must not be called on the main thread due to risk of ANRs!
private void sendControllerBatteryPacket(InputDeviceContext context) {
int currentBatteryStatus;
float currentBatteryCapacity;
int currentBatteryStatus = BatteryState.STATUS_FULL;
float currentBatteryCapacity = 0;

boolean batteryPresent = false;

// Use the BatteryState object introduced in Android S, if it's available and present.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && context.inputDevice.getBatteryState().isPresent()) {
currentBatteryStatus = context.inputDevice.getBatteryState().getStatus();
currentBatteryCapacity = context.inputDevice.getBatteryState().getCapacity();
}
else if (sceManager.isRecognizedDevice(context.inputDevice)) {
// On the SHIELD Android TV, we can use a proprietary API to access battery/charge state.
// We will convert it to the same form used by BatteryState to share code.
int batteryPercentage = sceManager.getBatteryPercentage(context.inputDevice);
if (batteryPercentage < 0) {
currentBatteryCapacity = Float.NaN;
}
else {
currentBatteryCapacity = batteryPercentage / 100.f;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
BatteryState batteryState = context.inputDevice.getBatteryState();
batteryPresent = batteryState.isPresent();
if (batteryPresent) {
currentBatteryStatus = batteryState.getStatus();
currentBatteryCapacity = batteryState.getCapacity();
}
}

SceConnectionType connectionType = sceManager.getConnectionType(context.inputDevice);
SceChargingState chargingState = sceManager.getChargingState(context.inputDevice);

// We can make some assumptions about charge state based on the connection type
if (connectionType == SceConnectionType.WIRED || connectionType == SceConnectionType.BOTH) {
if (batteryPercentage == 100) {
currentBatteryStatus = BatteryState.STATUS_FULL;
}
else if (chargingState == SceChargingState.NOT_CHARGING) {
currentBatteryStatus = BatteryState.STATUS_NOT_CHARGING;
if (!batteryPresent) {
if (sceManager.isRecognizedDevice(context.inputDevice)) {
// On the SHIELD Android TV, we can use a proprietary API to access battery/charge state.
// We will convert it to the same form used by BatteryState to share code.
int batteryPercentage = sceManager.getBatteryPercentage(context.inputDevice);
if (batteryPercentage < 0) {
currentBatteryCapacity = Float.NaN;
}
else {
currentBatteryStatus = BatteryState.STATUS_CHARGING;
currentBatteryCapacity = batteryPercentage / 100.f;
}
}
else if (connectionType == SceConnectionType.WIRELESS) {
if (chargingState == SceChargingState.CHARGING) {
currentBatteryStatus = BatteryState.STATUS_CHARGING;

SceConnectionType connectionType = sceManager.getConnectionType(context.inputDevice);
SceChargingState chargingState = sceManager.getChargingState(context.inputDevice);

// We can make some assumptions about charge state based on the connection type
if (connectionType == SceConnectionType.WIRED || connectionType == SceConnectionType.BOTH) {
if (batteryPercentage == 100) {
currentBatteryStatus = BatteryState.STATUS_FULL;
}
else if (chargingState == SceChargingState.NOT_CHARGING) {
currentBatteryStatus = BatteryState.STATUS_NOT_CHARGING;
}
else {
currentBatteryStatus = BatteryState.STATUS_CHARGING;
}
}
else if (connectionType == SceConnectionType.WIRELESS) {
if (chargingState == SceChargingState.CHARGING) {
currentBatteryStatus = BatteryState.STATUS_CHARGING;
}
else {
currentBatteryStatus = BatteryState.STATUS_DISCHARGING;
}
}
else {
currentBatteryStatus = BatteryState.STATUS_DISCHARGING;
// If connection type is unknown, just use the charge state
if (batteryPercentage == 100) {
currentBatteryStatus = BatteryState.STATUS_FULL;
}
else if (chargingState == SceChargingState.NOT_CHARGING) {
currentBatteryStatus = BatteryState.STATUS_DISCHARGING;
}
else if (chargingState == SceChargingState.CHARGING) {
currentBatteryStatus = BatteryState.STATUS_CHARGING;
}
else {
currentBatteryStatus = BatteryState.STATUS_UNKNOWN;
}
}
}
else {
// If connection type is unknown, just use the charge state
if (batteryPercentage == 100) {
currentBatteryStatus = BatteryState.STATUS_FULL;
}
else if (chargingState == SceChargingState.NOT_CHARGING) {
currentBatteryStatus = BatteryState.STATUS_DISCHARGING;
}
else if (chargingState == SceChargingState.CHARGING) {
currentBatteryStatus = BatteryState.STATUS_CHARGING;
}
else {
currentBatteryStatus = BatteryState.STATUS_UNKNOWN;
}
return;
}
}
else {
return;
}

if (currentBatteryStatus != context.lastReportedBatteryStatus ||
!areBatteryCapacitiesEqual(currentBatteryCapacity, context.lastReportedBatteryCapacity)) {
Expand Down

0 comments on commit eed71b1

Please sign in to comment.