Skip to content

Commit

Permalink
SystemUIGoogle: BatteryDefenderNotification: Convert to aidl
Browse files Browse the repository at this point in the history
Change-Id: I352e233dca2da8c97c12a47d976b94153cc95de6
Signed-off-by: spezi77 <spezi7713@gmx.net>
Signed-off-by: aswin7469 <aswinas@pixysos.com>
Signed-off-by: Anushek Prasal <anushekprasal@gmail.com>
  • Loading branch information
spezi77 authored and basamaryan committed Aug 8, 2023
1 parent d7ce06a commit b5950a8
Showing 1 changed file with 31 additions and 24 deletions.
Expand Up @@ -21,8 +21,10 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.IHwBinder;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.util.Log;

Expand All @@ -37,9 +39,12 @@
import java.time.Clock;
import java.util.NoSuchElementException;

import vendor.google.google_battery.V1_1.IGoogleBattery;
import vendor.google.google_battery.IGoogleBattery;

class BatteryDefenderNotification {

private static final String TAG = "BatteryDefenderNotification";

private final Context mContext;
private final NotificationManager mNotificationManager;
private final UiEventLogger mUiEventLogger;
Expand Down Expand Up @@ -81,7 +86,7 @@ private void resolveBatteryChangedIntent(Intent intent) {
z = true;
}
boolean isFullyCharged = PowerUtils.isFullyCharged(intent);
if (DEBUG) Log.d("BatteryDefenderNotification", "isPlugged: " + z2 + " | isOverheated: " + z + " | defenderEnabled: " + mDefenderEnabled + " | isCharged: " + isFullyCharged);
Log.d(TAG, "isPlugged: " + z2 + " | isOverheated: " + z + " | defenderEnabled: " + mDefenderEnabled + " | isCharged: " + isFullyCharged);
if (isFullyCharged && mPostNotificationVisible) {
cancelPostNotification();
}
Expand Down Expand Up @@ -137,7 +142,7 @@ private void sendPostNotification() {
mNotificationManager.notifyAsUser("battery_defender", PowerUtils.POST_NOTIFICATION_ID, addAction.build(), UserHandle.ALL);
mPostNotificationVisible = true;
} else {
Log.w("BatteryDefenderNotification", "error getting trigger time");
Log.w(TAG, "error getting trigger time");
}
clearDefenderStartRecord();
}
Expand All @@ -146,7 +151,7 @@ private void resumeCharging(BatteryDefenderEvent batteryDefenderEvent) {
if (mUiEventLogger != null) {
mUiEventLogger.logWithPosition(batteryDefenderEvent, 0, (String) null, mBatteryLevel);
}
Log.d("BatteryDefenderNotification", "resume charging: " + batteryDefenderEvent.mId);
Log.d(TAG, "resume charging: " + batteryDefenderEvent.mId);
executeBypassActionWithAsync();
mNotificationManager.cancelAsUser("battery_defender", PowerUtils.NOTIFICATION_ID, UserHandle.ALL);
clearDefenderStartRecord();
Expand All @@ -157,50 +162,52 @@ private void executeBypassActionWithAsync() {
return;
}
AsyncTask.execute(() -> {
IHwBinder.DeathRecipient cbRecipient = new IHwBinder.DeathRecipient() {
IBinder.DeathRecipient cbRecipient = new IBinder.DeathRecipient() {
@Override
public void serviceDied(long j) {
Log.d("BatteryDefenderNotification", "IHwBinder serviceDied");
public final void binderDied() {
Log.d(TAG, "serviceDied");
}
};
IGoogleBattery initHalInterface = initHalInterface(cbRecipient);
if (initHalInterface == null) {
Log.d("BatteryDefenderNotification", "Can not init hal interface");
Log.d(TAG, "Can not init hal interface");
}
try {
try {
initHalInterface.setProperty(2, 17, 1);
initHalInterface.setProperty(3, 17, 1);
initHalInterface.setProperty(1, 17, 1);
} catch (RemoteException e) {
Log.e("BatteryDefenderNotification", "setProperty error: " + e);
Log.e(TAG, "setProperty error: " + e);
}
} finally {
destroyHalInterface(initHalInterface, cbRecipient);
}
});
}

private IGoogleBattery initHalInterface(IHwBinder.DeathRecipient deathRecipient) {
private static IGoogleBattery initHalInterface(IBinder.DeathRecipient deathReceiver) {
Log.d(TAG, "initHalInterface");
try {
IGoogleBattery service = IGoogleBattery.getService();
if (service != null && deathRecipient != null) {
service.linkToDeath(deathRecipient, 0L);
IBinder binder = Binder.allowBlocking(ServiceManager.waitForDeclaredService("vendor.google.google_battery.IGoogleBattery/default"));
IGoogleBattery batteryInterface = null;
if (binder != null) {
batteryInterface = IGoogleBattery.Stub.asInterface(binder);
if (batteryInterface != null && deathReceiver != null) {
binder.linkToDeath(deathReceiver, 0);
}
}
return service;
} catch (RemoteException | NoSuchElementException e) {
Log.e("BatteryDefenderNotification", "failed to get Google Battery HAL: ", e);
return batteryInterface;
} catch (RemoteException | NoSuchElementException | SecurityException e) {
Log.e(TAG, "failed to get Google Battery HAL: ", e);
return null;
}
}

private void destroyHalInterface(IGoogleBattery iGoogleBattery, IHwBinder.DeathRecipient deathRecipient) {
if (deathRecipient != null) {
try {
iGoogleBattery.unlinkToDeath(deathRecipient);
} catch (RemoteException e) {
Log.e("BatteryDefenderNotification", "unlinkToDeath failed: ", e);
}
private static void destroyHalInterface(IGoogleBattery iGoogleBattery, IBinder.DeathRecipient deathRecipient) {
Log.d(TAG, "destroyHalInterface");
if (deathRecipient != null && iGoogleBattery != null) {
iGoogleBattery.asBinder().unlinkToDeath(deathRecipient, 0);
}
}

Expand Down

0 comments on commit b5950a8

Please sign in to comment.