Skip to content

Commit

Permalink
TurboAdapter_NoBatt: Return dummy bundle
Browse files Browse the repository at this point in the history
* Check whether charing is enabled from fw and report it as charging is enabled/disabled

Change-Id: Ic60b8319c4a3f16ab6e88f0fdef8bbf0decce954
  • Loading branch information
someone5678 committed Jul 21, 2024
1 parent dc76906 commit 0965c19
Showing 1 changed file with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.google.android.turboadapter;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.os.ServiceSpecificException;
import android.util.Log;

import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

public class GoogleBatteryService extends Service {
private GoogleBatteryProxyDummy mBinder;
ExecutorService mSingleThreadExecutor = Executors.newSingleThreadExecutor();

IBinder bindToGoogleBatteryProxyDummy() {
if (mBinder == null) {
Expand All @@ -31,11 +40,15 @@ public void onDestroy() {
super.onDestroy();
}

public void runOnBackgroundThread(Runnable runnable) {
mSingleThreadExecutor.execute(runnable);
}

public class GoogleBatteryProxyDummy extends IGoogleBatteryService.Stub {
final GoogleBatteryService googleBatteryService;
final GoogleBatteryService mGoogleBatteryService;

GoogleBatteryProxyDummy(GoogleBatteryService googleBatteryService) {
this.googleBatteryService = googleBatteryService;
mGoogleBatteryService = googleBatteryService;
}

@Override
Expand All @@ -55,6 +68,7 @@ public boolean setChargingDeadline(int i) {

@Override
public void getChargingStageAndDeadline(ResultReceiver resultReceiver) {
runOnBackgroundThread(new GetChargingStageAndDeadlineRunnable(resultReceiver));
}

@Override
Expand All @@ -64,6 +78,7 @@ public boolean setProperty(int i, int i2, int i3) {

@Override
public void getProperty(int i, int i2, ResultReceiver resultReceiver) {
runOnBackgroundThread(new GetBatteryPropertyRunnable(i, i2, resultReceiver));
}

@Override
Expand All @@ -76,4 +91,60 @@ public String getStringProperty(int i, int i2) {
return null; // TODO: Correctly report dummy value
}
}

final class GetBatteryPropertyRunnable implements Runnable {
final int mFeature;
final int mProperty;
final ResultReceiver mResultReceiver;

GetBatteryPropertyRunnable(int i, int i2, ResultReceiver resultReceiver) {
mFeature = i;
mProperty = i2;
mResultReceiver = resultReceiver;
}

@Override
public void run() {
try {
Bundle bundle = new Bundle();
bundle.putInt("feature_property", 0);
mResultReceiver.send(0, bundle);
} catch (ServiceSpecificException | IllegalArgumentException e) {
Log.e("GoogleBatteryService", "getProperty failed", e);
}
}
}

final class GetChargingStageAndDeadlineRunnable implements Runnable {
final ResultReceiver mResultReceiver;

GetChargingStageAndDeadlineRunnable(ResultReceiver resultReceiver) {
mResultReceiver = resultReceiver;
}

@Override
public void run() {
try {
Bundle bundle = new Bundle();
String charing = "Disabled";
if (isCharging()) {
charing = "Enabled";
}
bundle.putString("charging_stage", charing); // Inactive, Active, Enabled, ...
bundle.putInt("deadline_seconds", -3);
mResultReceiver.send(0, bundle);
} catch (ServiceSpecificException | IllegalArgumentException e) {
Log.e("GoogleBatteryService", "getChargingStageAndDeadline failed", e);
}
}
}

private boolean isCharging() {
BatteryManager batteryManager =
(BatteryManager) this.getSystemService(Context.BATTERY_SERVICE);
if (batteryManager != null) {
return batteryManager.isCharging();
}
return false;
}
}

0 comments on commit 0965c19

Please sign in to comment.