Merge "Fix a bug with the screen incorrectly turning off on power button press." into main
diff --git a/services/core/Android.bp b/services/core/Android.bp
index 300b147..8ee560b 100644
--- a/services/core/Android.bp
+++ b/services/core/Android.bp
@@ -256,6 +256,7 @@
         "stats_flags_lib",
         "core_os_flags_lib",
         "connectivity_flags_lib",
+        "dreams_flags_lib",
     ],
     javac_shard_size: 50,
     javacflags: [
diff --git a/services/core/java/com/android/server/dreams/Android.bp b/services/core/java/com/android/server/dreams/Android.bp
new file mode 100644
index 0000000..4078a42
--- /dev/null
+++ b/services/core/java/com/android/server/dreams/Android.bp
@@ -0,0 +1,11 @@
+aconfig_declarations {
+    name: "dreams_flags",
+    package: "com.android.server.dreams",
+    container: "system",
+    srcs: ["*.aconfig"],
+}
+
+java_aconfig_library {
+    name: "dreams_flags_lib",
+    aconfig_declarations: "dreams_flags",
+}
diff --git a/services/core/java/com/android/server/dreams/DreamManagerService.java b/services/core/java/com/android/server/dreams/DreamManagerService.java
index fc63494..2def5ae 100644
--- a/services/core/java/com/android/server/dreams/DreamManagerService.java
+++ b/services/core/java/com/android/server/dreams/DreamManagerService.java
@@ -45,6 +45,7 @@
 import android.hardware.display.AmbientDisplayConfiguration;
 import android.net.Uri;
 import android.os.BatteryManager;
+import android.os.BatteryManagerInternal;
 import android.os.Binder;
 import android.os.Build;
 import android.os.Handler;
@@ -118,6 +119,7 @@
     private final DreamController mController;
     private final PowerManager mPowerManager;
     private final PowerManagerInternal mPowerManagerInternal;
+    private final BatteryManagerInternal mBatteryManagerInternal;
     private final PowerManager.WakeLock mDozeWakeLock;
     private final ActivityTaskManagerInternal mAtmInternal;
     private final PackageManagerInternal mPmInternal;
@@ -186,7 +188,11 @@
     private final BroadcastReceiver mChargingReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
-            mIsCharging = (BatteryManager.ACTION_CHARGING.equals(intent.getAction()));
+            if (Flags.useBatteryChangedBroadcast()) {
+                mIsCharging = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY);
+            } else {
+                mIsCharging = (BatteryManager.ACTION_CHARGING.equals(intent.getAction()));
+            }
         }
     };
 
@@ -251,6 +257,12 @@
                 com.android.internal.R.bool.config_keepDreamingWhenUnplugging);
         mDreamsDisabledByAmbientModeSuppressionConfig = mContext.getResources().getBoolean(
                 com.android.internal.R.bool.config_dreamsDisabledByAmbientModeSuppressionConfig);
+
+        if (Flags.useBatteryChangedBroadcast()) {
+            mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class);
+        } else {
+            mBatteryManagerInternal = null;
+        }
     }
 
     @Override
@@ -279,9 +291,15 @@
 
             mContext.registerReceiver(
                     mDockStateReceiver, new IntentFilter(Intent.ACTION_DOCK_EVENT));
+
             IntentFilter chargingIntentFilter = new IntentFilter();
-            chargingIntentFilter.addAction(BatteryManager.ACTION_CHARGING);
-            chargingIntentFilter.addAction(BatteryManager.ACTION_DISCHARGING);
+            if (Flags.useBatteryChangedBroadcast()) {
+                chargingIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
+                chargingIntentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
+            } else {
+                chargingIntentFilter.addAction(BatteryManager.ACTION_CHARGING);
+                chargingIntentFilter.addAction(BatteryManager.ACTION_DISCHARGING);
+            }
             mContext.registerReceiver(mChargingReceiver, chargingIntentFilter);
 
             mSettingsObserver = new SettingsObserver(mHandler);
diff --git a/services/core/java/com/android/server/dreams/flags.aconfig b/services/core/java/com/android/server/dreams/flags.aconfig
new file mode 100644
index 0000000..5d35ebd
--- /dev/null
+++ b/services/core/java/com/android/server/dreams/flags.aconfig
@@ -0,0 +1,12 @@
+package: "com.android.server.dreams"
+container: "system"
+
+flag {
+    name: "use_battery_changed_broadcast"
+    namespace: "communal"
+    description: "Use ACTION_BATTERY_CHANGED broadcast to track charging state"
+    bug: "329125239"
+    metadata {
+        purpose: PURPOSE_BUGFIX
+    }
+}
\ No newline at end of file