diff --git a/SystemUIGoogle/src/com/google/android/systemui/power/BatteryDefenderNotification.java b/SystemUIGoogle/src/com/google/android/systemui/power/BatteryDefenderNotification.java index 3a456f0..3faa9ce 100644 --- a/SystemUIGoogle/src/com/google/android/systemui/power/BatteryDefenderNotification.java +++ b/SystemUIGoogle/src/com/google/android/systemui/power/BatteryDefenderNotification.java @@ -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; @@ -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; @@ -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(); } @@ -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(); } @@ -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(); @@ -157,15 +162,15 @@ 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 { @@ -173,7 +178,7 @@ public void serviceDied(long j) { 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); @@ -181,26 +186,28 @@ public void serviceDied(long j) { }); } - 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); } }