AodPolicy: Fix service leak
Fixes an issue where the AlwaysOnDisplayPolicy leaked a service
context, and with it the entirety of DozeMachine, preventing
wakelocks from being GCed and preventing them from being
fallback-released.
Bug: 78402666
Test: Toggle screen on and off a few times, verify that DozeService is not being leaked.
Change-Id: Ie3ad67c2d0c83760f4d04a53394fab4a3a35d6bc
diff --git a/packages/SystemUI/src/com/android/systemui/doze/AlwaysOnDisplayPolicy.java b/packages/SystemUI/src/com/android/systemui/doze/AlwaysOnDisplayPolicy.java
index 8515bf2..4fea45c3 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/AlwaysOnDisplayPolicy.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/AlwaysOnDisplayPolicy.java
@@ -116,6 +116,7 @@
private SettingsObserver mSettingsObserver;
public AlwaysOnDisplayPolicy(Context context) {
+ context = context.getApplicationContext();
mContext = context;
mParser = new KeyValueListParser(',');
mSettingsObserver = new SettingsObserver(context.getMainThreadHandler());
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeFactory.java b/packages/SystemUI/src/com/android/systemui/doze/DozeFactory.java
index 5bf62f6..e9d78d9 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeFactory.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeFactory.java
@@ -61,13 +61,14 @@
DozeMachine machine = new DozeMachine(wrappedService, config, wakeLock);
machine.setParts(new DozeMachine.Part[]{
- new DozePauser(handler, machine, alarmManager, new AlwaysOnDisplayPolicy(context)),
+ new DozePauser(handler, machine, alarmManager, params.getPolicy()),
new DozeFalsingManagerAdapter(FalsingManager.getInstance(context)),
createDozeTriggers(context, sensorManager, host, alarmManager, config, params,
handler, wakeLock, machine),
createDozeUi(context, host, wakeLock, machine, handler, alarmManager, params),
new DozeScreenState(wrappedService, handler, params, wakeLock),
- createDozeScreenBrightness(context, wrappedService, sensorManager, host, handler),
+ createDozeScreenBrightness(context, wrappedService, sensorManager, host, params,
+ handler),
new DozeWallpaperState(context, params)
});
@@ -76,11 +77,11 @@
private DozeMachine.Part createDozeScreenBrightness(Context context,
DozeMachine.Service service, SensorManager sensorManager, DozeHost host,
- Handler handler) {
+ DozeParameters params, Handler handler) {
Sensor sensor = DozeSensors.findSensorWithType(sensorManager,
context.getString(R.string.doze_brightness_sensor_type));
return new DozeScreenBrightness(context, service, sensorManager, sensor, host, handler,
- new AlwaysOnDisplayPolicy(context));
+ params.getPolicy());
}
private DozeTriggers createDozeTriggers(Context context, SensorManager sensorManager,
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
index aa26419..7339304 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
@@ -62,6 +62,7 @@
public void onDestroy() {
Dependency.get(PluginManager.class).removePluginListener(this);
super.onDestroy();
+ mDozeMachine = null;
}
@Override
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java b/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
index f7a258a..be3e322 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeTriggers.java
@@ -85,7 +85,7 @@
mAllowPulseTriggers = allowPulseTriggers;
mDozeSensors = new DozeSensors(context, alarmManager, mSensorManager, dozeParameters,
config, wakeLock, this::onSensor, this::onProximityFar,
- new AlwaysOnDisplayPolicy(context));
+ dozeParameters.getPolicy());
mUiModeManager = mContext.getSystemService(UiModeManager.class);
}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java
index 07b79a2..d2d9c4c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java
@@ -59,9 +59,9 @@
@VisibleForTesting
protected DozeParameters(Context context) {
- mContext = context;
+ mContext = context.getApplicationContext();
mAmbientDisplayConfiguration = new AmbientDisplayConfiguration(mContext);
- mAlwaysOnPolicy = new AlwaysOnDisplayPolicy(context);
+ mAlwaysOnPolicy = new AlwaysOnDisplayPolicy(mContext);
mControlScreenOffAnimation = !getDisplayNeedsBlanking();
mPowerManager = mContext.getSystemService(PowerManager.class);
@@ -243,6 +243,10 @@
mDozeAlwaysOn = mAmbientDisplayConfiguration.alwaysOnEnabled(UserHandle.USER_CURRENT);
}
+ public AlwaysOnDisplayPolicy getPolicy() {
+ return mAlwaysOnPolicy;
+ }
+
/**
* Parses a spec of the form `1,2,3,!5,*`. The resulting object will match numbers that are
* listed, will not match numbers that are listed with a ! prefix, and will match / not match