diff --git a/core/java/android/app/ActivityManager.java b/core/java/android/app/ActivityManager.java index b4f6d6fe770d0aa0226f50b9239fe8f38a063ebb..4f4569134c1959acc83c2a0d6ba277c65e9c2a4d 100644 --- a/core/java/android/app/ActivityManager.java +++ b/core/java/android/app/ActivityManager.java @@ -5192,11 +5192,21 @@ public class ActivityManager { * @hide */ public static void broadcastStickyIntent(Intent intent, int appOp, Bundle options, int userId) { + broadcastStickyIntent(intent, null, appOp, options, userId); + } + + /** + * Convenience for sending a sticky broadcast. For internal use only. + * + * @hide + */ + public static void broadcastStickyIntent(Intent intent, String[] excludedPackages, + int appOp, Bundle options, int userId) { try { getService().broadcastIntentWithFeature( null, null, intent, null, null, Activity.RESULT_OK, null, null, null /*requiredPermissions*/, null /*excludedPermissions*/, - null /*excludedPackages*/, appOp, options, false, true, userId); + excludedPackages, appOp, options, false, true, userId); } catch (RemoteException ex) { } } diff --git a/services/core/java/com/android/server/BatteryService.java b/services/core/java/com/android/server/BatteryService.java index d94f4f22f2c94f4b6735517448320a916b00cf54..556eba6ced768e96780ca8b6e7d539092ce85c5b 100644 --- a/services/core/java/com/android/server/BatteryService.java +++ b/services/core/java/com/android/server/BatteryService.java @@ -167,6 +167,8 @@ public final class BatteryService extends SystemService { private int mBatteryNearlyFullLevel; private int mShutdownBatteryTemperature; + private static String sSystemUiPackage; + private int mPlugType; private int mLastPlugType = -1; // Extra state so we can detect first run @@ -228,6 +230,8 @@ public final class BatteryService extends SystemService { com.android.internal.R.integer.config_lowBatteryCloseWarningBump); mShutdownBatteryTemperature = mContext.getResources().getInteger( com.android.internal.R.integer.config_shutdownBatteryTemperature); + sSystemUiPackage = mContext.getResources().getString( + com.android.internal.R.string.config_systemUi); mBatteryLevelsEventQueue = new ArrayDeque<>(); mMetricsLogger = new MetricsLogger(); @@ -750,8 +754,21 @@ public final class BatteryService extends SystemService { + ", info:" + mHealthInfo.toString()); } - mHandler.post(() -> ActivityManager.broadcastStickyIntent(intent, AppOpsManager.OP_NONE, - mBatteryChangedOptions, UserHandle.USER_ALL)); + mHandler.post(() -> broadcastBatteryChangedIntent(intent, mBatteryChangedOptions)); + } + + private static void broadcastBatteryChangedIntent(Intent intent, Bundle options) { + // TODO (293959093): It is important that SystemUI receives this broadcast as soon as + // possible. Ideally, it should be using binder callbacks but until then, dispatch this + // as a foreground broadcast to SystemUI. + final Intent fgIntent = new Intent(intent); + fgIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); + fgIntent.setPackage(sSystemUiPackage); + ActivityManager.broadcastStickyIntent(fgIntent, AppOpsManager.OP_NONE, + options, UserHandle.USER_ALL); + + ActivityManager.broadcastStickyIntent(intent, new String[] {sSystemUiPackage}, + AppOpsManager.OP_NONE, options, UserHandle.USER_ALL); } private void sendBatteryLevelChangedIntentLocked() {