Defers initialization of preferences.

In Screen Timeout Settings, preferences should not be initialized in
onAttach() because the setting style hasn't been loaded yet. This change
defers the initialiaztion of those preferences so that they appear
correctly.

Test: manually tested.
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.display
Bug: 183909540

Change-Id: I86cfe196549d709ed763faa004fff7b631365b1e
This commit is contained in:
Yi Jiang
2021-05-19 11:44:36 -07:00
parent 3616520985
commit 8aaa9f5549
3 changed files with 33 additions and 30 deletions

View File

@@ -32,23 +32,20 @@ import com.android.settingslib.widget.BannerMessagePreference;
public class AdaptiveSleepBatterySaverPreferenceController { public class AdaptiveSleepBatterySaverPreferenceController {
@VisibleForTesting @VisibleForTesting
final BannerMessagePreference mPreference; BannerMessagePreference mPreference;
private final PowerManager mPowerManager; private final PowerManager mPowerManager;
private final Context mContext;
public AdaptiveSleepBatterySaverPreferenceController(Context context) { public AdaptiveSleepBatterySaverPreferenceController(Context context) {
mPreference = new BannerMessagePreference(context);
mPreference.setTitle(R.string.ambient_camera_summary_battery_saver_on);
mPreference.setPositiveButtonText(R.string.disable_text);
mPowerManager = context.getSystemService(PowerManager.class); mPowerManager = context.getSystemService(PowerManager.class);
mPreference.setPositiveButtonOnClickListener(p -> { mContext = context;
mPowerManager.setPowerSaveModeEnabled(false);
});
} }
/** /**
* Adds the controlled preference to the provided preference screen. * Adds the controlled preference to the provided preference screen.
*/ */
public void addToScreen(PreferenceScreen screen) { public void addToScreen(PreferenceScreen screen) {
initializePreference();
screen.addPreference(mPreference); screen.addPreference(mPreference);
updateVisibility(); updateVisibility();
} }
@@ -66,6 +63,17 @@ public class AdaptiveSleepBatterySaverPreferenceController {
* Refreshes the visibility of the preference. * Refreshes the visibility of the preference.
*/ */
public void updateVisibility() { public void updateVisibility() {
initializePreference();
mPreference.setVisible(isPowerSaveMode()); mPreference.setVisible(isPowerSaveMode());
} }
private void initializePreference() {
if (mPreference == null) {
mPreference = new BannerMessagePreference(mContext);
mPreference.setTitle(R.string.ambient_camera_summary_battery_saver_on);
mPreference.setPositiveButtonText(R.string.disable_text);
mPreference.setPositiveButtonOnClickListener(
p -> mPowerManager.setPowerSaveModeEnabled(false));
}
}
} }

View File

@@ -33,26 +33,22 @@ import com.android.settingslib.widget.BannerMessagePreference;
*/ */
public class AdaptiveSleepCameraStatePreferenceController { public class AdaptiveSleepCameraStatePreferenceController {
@VisibleForTesting @VisibleForTesting
final BannerMessagePreference mPreference; BannerMessagePreference mPreference;
private final SensorPrivacyManager mPrivacyManager; private final SensorPrivacyManager mPrivacyManager;
private final Context mContext;
public AdaptiveSleepCameraStatePreferenceController(Context context) { public AdaptiveSleepCameraStatePreferenceController(Context context) {
mPreference = new BannerMessagePreference(context);
mPreference.setTitle(R.string.auto_rotate_camera_lock_title);
mPreference.setSummary(R.string.adaptive_sleep_camera_lock_summary);
mPreference.setPositiveButtonText(R.string.allow);
mPrivacyManager = SensorPrivacyManager.getInstance(context); mPrivacyManager = SensorPrivacyManager.getInstance(context);
mPrivacyManager.addSensorPrivacyListener(CAMERA, mPrivacyManager.addSensorPrivacyListener(CAMERA,
(sensor, enabled) -> updateVisibility()); (sensor, enabled) -> updateVisibility());
mPreference.setPositiveButtonOnClickListener(p -> { mContext = context;
mPrivacyManager.setSensorPrivacy(CAMERA, false);
});
} }
/** /**
* Adds the controlled preference to the provided preference screen. * Adds the controlled preference to the provided preference screen.
*/ */
public void addToScreen(PreferenceScreen screen) { public void addToScreen(PreferenceScreen screen) {
initializePreference();
screen.addPreference(mPreference); screen.addPreference(mPreference);
updateVisibility(); updateVisibility();
} }
@@ -70,6 +66,18 @@ public class AdaptiveSleepCameraStatePreferenceController {
* Refreshes the visibility of the preference. * Refreshes the visibility of the preference.
*/ */
public void updateVisibility() { public void updateVisibility() {
initializePreference();
mPreference.setVisible(isCameraLocked()); mPreference.setVisible(isCameraLocked());
} }
private void initializePreference() {
if (mPreference == null) {
mPreference = new BannerMessagePreference(mContext);
mPreference.setTitle(R.string.auto_rotate_camera_lock_title);
mPreference.setSummary(R.string.adaptive_sleep_camera_lock_summary);
mPreference.setPositiveButtonText(R.string.allow);
mPreference.setPositiveButtonOnClickListener(
p -> mPrivacyManager.setSensorPrivacy(CAMERA, false));
}
}
} }

View File

@@ -65,19 +65,6 @@ public class AdaptiveSleepPreferenceController {
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
mPrivacyManager = SensorPrivacyManager.getInstance(context); mPrivacyManager = SensorPrivacyManager.getInstance(context);
mPowerManager = context.getSystemService(PowerManager.class); mPowerManager = context.getSystemService(PowerManager.class);
mPreference = new RestrictedSwitchPreference(context);
mPreference.setTitle(R.string.adaptive_sleep_title);
mPreference.setSummary(R.string.adaptive_sleep_description);
mPreference.setChecked(isChecked());
mPreference.setKey(PREFERENCE_KEY);
mPreference.setOnPreferenceClickListener(preference -> {
final boolean isChecked = ((RestrictedSwitchPreference) preference).isChecked();
mMetricsFeatureProvider.action(context, SettingsEnums.ACTION_SCREEN_ATTENTION_CHANGED,
isChecked);
Settings.Secure.putInt(context.getContentResolver(),
Settings.Secure.ADAPTIVE_SLEEP, isChecked ? 1 : DEFAULT_VALUE);
return true;
});
mPackageManager = context.getPackageManager(); mPackageManager = context.getPackageManager();
} }
@@ -116,8 +103,8 @@ public class AdaptiveSleepPreferenceController {
mPreference.setSummary(R.string.adaptive_sleep_description); mPreference.setSummary(R.string.adaptive_sleep_description);
mPreference.setChecked(isChecked()); mPreference.setChecked(isChecked());
mPreference.setKey(PREFERENCE_KEY); mPreference.setKey(PREFERENCE_KEY);
mPreference.setOnPreferenceChangeListener((preference, value) -> { mPreference.setOnPreferenceClickListener(preference -> {
final boolean isChecked = (Boolean) value; final boolean isChecked = ((RestrictedSwitchPreference) preference).isChecked();
mMetricsFeatureProvider.action(mContext, mMetricsFeatureProvider.action(mContext,
SettingsEnums.ACTION_SCREEN_ATTENTION_CHANGED, SettingsEnums.ACTION_SCREEN_ATTENTION_CHANGED,
isChecked); isChecked);