From fa3766a22033cf90630d14b9b5a4ced46bf30cbd Mon Sep 17 00:00:00 2001 From: Daniel Nishi Date: Tue, 18 Apr 2017 14:12:54 -0700 Subject: [PATCH] Don't show warning dialog on opening ASM settings. On older devices, the activation warning dialog would show upon the switchbar changing to On. This was occurring when the page opens because the status was changing onResume. By shuffling the switchbar changes into the ASMSwitchBarController, we can register our listener after the initialization of the switch bar's first status. Change-Id: I3610d07345684d1e66444981a8059d1c2965e955 Fixes: 37472724 Test: Settings robotests --- .../AutomaticStorageManagerSettings.java | 3 -- ...aticStorageManagerSwitchBarController.java | 11 +++++ ...StorageManagerSwitchBarControllerTest.java | 40 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/com/android/settings/deletionhelper/AutomaticStorageManagerSettings.java b/src/com/android/settings/deletionhelper/AutomaticStorageManagerSettings.java index adf293832c5..a2fe07d4e68 100644 --- a/src/com/android/settings/deletionhelper/AutomaticStorageManagerSettings.java +++ b/src/com/android/settings/deletionhelper/AutomaticStorageManagerSettings.java @@ -123,9 +123,6 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment boolean isStorageManagerChecked = Settings.Secure.getInt(getContentResolver(), Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0; - // Using the setCheckedInternal means the checked status won't propagate through the - // listeners -- this will prevent us from accidentally causing a metrics event on resume. - mSwitchBar.setCheckedInternal(isStorageManagerChecked); mDaysToRetain.setEnabled(isStorageManagerChecked); } diff --git a/src/com/android/settings/deletionhelper/AutomaticStorageManagerSwitchBarController.java b/src/com/android/settings/deletionhelper/AutomaticStorageManagerSwitchBarController.java index a6481022a64..8ab1a07fb13 100644 --- a/src/com/android/settings/deletionhelper/AutomaticStorageManagerSwitchBarController.java +++ b/src/com/android/settings/deletionhelper/AutomaticStorageManagerSwitchBarController.java @@ -52,6 +52,17 @@ public class AutomaticStorageManagerSwitchBarController mDaysToRetainPreference = Preconditions.checkNotNull(daysToRetainPreference); mFragmentManager = Preconditions.checkNotNull(fragmentManager); + initializeCheckedStatus(); + } + + private void initializeCheckedStatus() { + boolean isStorageManagerChecked = + Settings.Secure.getInt( + mContext.getContentResolver(), + Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, + 0) + != 0; + mSwitchBar.setChecked(isStorageManagerChecked); mSwitchBar.addOnSwitchChangeListener(this); } diff --git a/tests/robotests/src/com/android/settings/deletionhelper/AutomaticStorageManagerSwitchBarControllerTest.java b/tests/robotests/src/com/android/settings/deletionhelper/AutomaticStorageManagerSwitchBarControllerTest.java index b4f5f3d778c..be04eecefc8 100644 --- a/tests/robotests/src/com/android/settings/deletionhelper/AutomaticStorageManagerSwitchBarControllerTest.java +++ b/tests/robotests/src/com/android/settings/deletionhelper/AutomaticStorageManagerSwitchBarControllerTest.java @@ -16,6 +16,8 @@ package com.android.settings.deletionhelper; +import static com.google.common.truth.Truth.assertThat; + import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; @@ -27,6 +29,7 @@ import static org.mockito.Mockito.verify; import android.app.Fragment; import android.app.FragmentManager; import android.content.Context; +import android.provider.Settings; import android.support.v7.preference.Preference; import com.android.internal.logging.nano.MetricsProto; @@ -121,4 +124,41 @@ public class AutomaticStorageManagerSwitchBarControllerTest { verify(mFragmentManager.beginTransaction(), never()) .add(any(Fragment.class), eq(ActivationWarningFragment.TAG)); } + + @Test + public void initializeSwitchOnConstruction() { + Settings.Secure.putInt( + mContext.getContentResolver(), + Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, + 1); + + mController = + new AutomaticStorageManagerSwitchBarController( + mContext, + mSwitchBar, + mMetricsFeatureProvider, + mPreference, + mFragmentManager); + + assertThat(mSwitchBar.isChecked()).isTrue(); + } + + @Test + public void initializingSwitchDoesNotTriggerView() { + Settings.Secure.putInt( + mContext.getContentResolver(), + Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, + 1); + + mController = + new AutomaticStorageManagerSwitchBarController( + mContext, + mSwitchBar, + mMetricsFeatureProvider, + mPreference, + mFragmentManager); + + verify(mFragmentManager.beginTransaction(), never()) + .add(any(Fragment.class), eq(ActivationWarningFragment.TAG)); + } }