Fixes 'no ripple effect' issue for screen attention setting

Preferences shouldn't be initialized at onAttach() because the settings
style hasn't been loaded yet. This change defers the preferences
initialization so that they comply with the settings style.

The change ag/14114425 is reverted due to b/186904496. This change
includes a fix for that issue.

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

Change-Id: I317ce251f4d11e62c6592ee587c2919da4d45db3
This commit is contained in:
Yi Jiang
2021-05-03 17:56:15 -07:00
parent 08b7833120
commit 18c97b595f
5 changed files with 61 additions and 38 deletions

View File

@@ -37,26 +37,18 @@ public class AdaptiveSleepPermissionPreferenceController {
@VisibleForTesting
BannerMessagePreference mPreference;
private final PackageManager mPackageManager;
private final Context mContext;
public AdaptiveSleepPermissionPreferenceController(Context context) {
final String packageName = context.getPackageManager().getAttentionServicePackageName();
mPackageManager = context.getPackageManager();
final Intent intent = new Intent(
android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + packageName));
mPreference = new BannerMessagePreference(context);
mPreference.setTitle(R.string.adaptive_sleep_title_no_permission);
mPreference.setSummary(R.string.adaptive_sleep_summary_no_permission);
mPreference.setPositiveButtonText(R.string.adaptive_sleep_manage_permission_button);
mPreference.setPositiveButtonOnClickListener(p -> {
context.startActivity(intent);
});
mContext = context;
}
/**
* Adds the controlled preference to the provided preference screen.
*/
public void addToScreen(PreferenceScreen screen) {
initializePreference();
if (!hasSufficientPermission(mPackageManager)) {
screen.addPreference(mPreference);
}
@@ -66,6 +58,25 @@ public class AdaptiveSleepPermissionPreferenceController {
* Refreshes the visibility of the preference.
*/
public void updateVisibility() {
initializePreference();
mPreference.setVisible(!hasSufficientPermission(mPackageManager));
}
private void initializePreference() {
if (mPreference == null) {
final String packageName =
mContext.getPackageManager().getAttentionServicePackageName();
final Intent intent = new Intent(
android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + packageName));
mPreference = new BannerMessagePreference(mContext);
mPreference.setTitle(R.string.adaptive_sleep_title_no_permission);
mPreference.setSummary(R.string.adaptive_sleep_summary_no_permission);
mPreference.setPositiveButtonText(R.string.adaptive_sleep_manage_permission_button);
mPreference.setPositiveButtonOnClickListener(p -> {
mContext.startActivity(intent);
});
}
}
}