Make SensorToggleControllers lifecycle aware

We need to watch the lifecycle so that we can unregister callbacks and
not cause leaks.

This change also rewrites the SensorPrivacyManagerHelper. The previous
implmementation was using deprecated apis. It also had an issue where
if a callback was added it would not necessarily register alistener with
the callback with the service since that was only done when the value is
checked. Now we register a listener when the class is instantiated and
with the new API there will only be the 1.

Finally we impove the tests to have more coverage and test both
SensorToggleControllers and the SensorPRivacyManagerHelper class.

Test: Use profiler to verify no more leaks
      SensorToggleControllerTest, SensorPrivacyManagerHelperTest
Bug: 244280065
Change-Id: Ibf0bcee455444a104ca6800302907c3dc0de8f1f
This commit is contained in:
Evan Severson
2022-09-01 14:08:38 -07:00
parent 1051fb16d8
commit 5ba4065703
8 changed files with 921 additions and 508 deletions

View File

@@ -18,29 +18,37 @@ package com.android.settings.privacy;
import static android.os.UserManager.DISALLOW_CAMERA_TOGGLE;
import static com.android.settings.utils.SensorPrivacyManagerHelper.CAMERA;
import static com.android.settings.utils.SensorPrivacyManagerHelper.SENSOR_CAMERA;
import android.content.Context;
import android.provider.DeviceConfig;
import androidx.annotation.VisibleForTesting;
import com.android.settings.utils.SensorPrivacyManagerHelper;
/**
* Controller for microphone toggle
*/
public class CameraToggleController extends SensorToggleController {
public CameraToggleController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getSensor() {
return CAMERA;
@VisibleForTesting
public CameraToggleController(Context context, String preferenceKey,
SensorPrivacyManagerHelper sensorPrivacyManagerHelper) {
super(context, preferenceKey, sensorPrivacyManagerHelper, /* ignoreDeviceConfig */ true);
}
@Override
public int getAvailabilityStatus() {
return mSensorPrivacyManagerHelper.supportsSensorToggle(getSensor())
&& DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, "camera_toggle_enabled",
true) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
public int getSensor() {
return SENSOR_CAMERA;
}
@Override
public String getDeviceConfigKey() {
return "camera_toggle_enabled";
}
@Override