Update the automatic storage management preferences.

This updates this page to match the newer mocks. The changes
include:

* Changing the toggle from a switch to a switch bar.
* Removing the Deletion Helper flow from the screen.
* Unit testing the previously untested functionality.

Change-Id: I35eb1e065c9acfbf32a64d659d35e18034025472
Fixes: 36486021
Test: Robotest
This commit is contained in:
Daniel Nishi
2017-03-21 14:13:39 -07:00
parent 3c0d5ff3d1
commit 1ee13fd386
4 changed files with 275 additions and 94 deletions

View File

@@ -18,38 +18,18 @@
xmlns:settings="http://schemas.android.com/apk/res-auto" xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/automatic_storage_manager_settings" > android:title="@string/automatic_storage_manager_settings" >
<PreferenceCategory
android:key="automatic"
android:title="@string/deletion_helper_automatic_title">
<SwitchPreference
android:key="storage_manager_active"
android:title="@string/automatic_storage_manager_preference_title"
android:summary="@string/automatic_storage_manager_text"/>
<Preference
android:key="freed_bytes"
android:persistent="false"
android:selectable="false" />
<DropDownPreference <DropDownPreference
android:key="days" android:key="days"
android:summary="%s" android:summary="%s"
android:title="@string/automatic_storage_manager_days_title" android:title="@string/automatic_storage_manager_days_title"
android:entries="@array/automatic_storage_management_days" android:entries="@array/automatic_storage_management_days"
android:entryValues="@array/automatic_storage_management_days_values" android:entryValues="@array/automatic_storage_management_days_values"
settings:allowDividerAbove="true" /> settings:allowDividerBelow="true" />
</PreferenceCategory>
<PreferenceCategory
android:key="manual"
android:title="@string/deletion_helper_manual_title">
<Preference <Preference
android:key="deletion_helper" android:key="freed_bytes"
android:title="@string/deletion_helper_preference_title"/> android:persistent="false"
android:selectable="false"
</PreferenceCategory> settings:allowDividerAbove="true" />
</PreferenceScreen> </PreferenceScreen>

View File

@@ -17,44 +17,39 @@
package com.android.settings.deletionhelper; package com.android.settings.deletionhelper;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.SystemProperties;
import android.os.storage.StorageManager;
import android.provider.Settings; import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.DropDownPreference; import android.support.v7.preference.DropDownPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.Preference.OnPreferenceChangeListener; import android.support.v7.preference.Preference.OnPreferenceChangeListener;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.text.format.Formatter; import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.widget.SwitchBar;
/** /**
* AutomaticStorageManagerSettings is the Settings screen for configuration and management of the * AutomaticStorageManagerSettings is the Settings screen for configuration and management of the
* automatic storage manager. * automatic storage manager.
*/ */
public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment implements public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
OnPreferenceChangeListener, Preference.OnPreferenceClickListener { implements OnPreferenceChangeListener {
public static final int DEFAULT_DAYS_TO_RETAIN = 90;
private static final String KEY_DAYS = "days"; private static final String KEY_DAYS = "days";
private static final String KEY_DELETION_HELPER = "deletion_helper";
private static final String KEY_FREED = "freed_bytes"; private static final String KEY_FREED = "freed_bytes";
private static final String KEY_STORAGE_MANAGER_SWITCH = "storage_manager_active";
private static final String STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY = private static final String STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY =
"ro.storage_manager.enabled"; "ro.storage_manager.enabled";
private AutomaticStorageManagerSwitchBarController mSwitchController;
private DropDownPreference mDaysToRetain; private DropDownPreference mDaysToRetain;
private Preference mFreedBytes; private Preference mFreedBytes;
private Preference mDeletionHelper; private SwitchBar mSwitchBar;
private SwitchPreference mStorageManagerSwitch;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@@ -63,19 +58,21 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
} }
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public View onCreateView(
super.onActivityCreated(savedInstanceState); LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
initializeDaysToRetainPreference();
initializeFreedBytesPreference();
initializeSwitchBar();
return view;
}
private void initializeDaysToRetainPreference() {
mDaysToRetain = (DropDownPreference) findPreference(KEY_DAYS); mDaysToRetain = (DropDownPreference) findPreference(KEY_DAYS);
mDaysToRetain.setOnPreferenceChangeListener(this); mDaysToRetain.setOnPreferenceChangeListener(this);
mFreedBytes = findPreference(KEY_FREED);
mDeletionHelper = findPreference(KEY_DELETION_HELPER);
mDeletionHelper.setOnPreferenceClickListener(this);
mStorageManagerSwitch = (SwitchPreference) findPreference(KEY_STORAGE_MANAGER_SWITCH);
mStorageManagerSwitch.setOnPreferenceChangeListener(this);
ContentResolver cr = getContentResolver(); ContentResolver cr = getContentResolver();
int photosDaysToRetain = Settings.Secure.getInt(cr, int photosDaysToRetain = Settings.Secure.getInt(cr,
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN,
@@ -83,7 +80,24 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
String[] stringValues = String[] stringValues =
getResources().getStringArray(R.array.automatic_storage_management_days_values); getResources().getStringArray(R.array.automatic_storage_management_days_values);
mDaysToRetain.setValue(stringValues[daysValueToIndex(photosDaysToRetain, stringValues)]); mDaysToRetain.setValue(stringValues[daysValueToIndex(photosDaysToRetain, stringValues)]);
}
private void initializeSwitchBar() {
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
mSwitchBar.show();
mSwitchController =
new AutomaticStorageManagerSwitchBarController(
getContext(),
mSwitchBar,
mMetricsFeatureProvider,
mDaysToRetain,
getFragmentManager());
}
private void initializeFreedBytesPreference() {
ContentResolver cr = getContentResolver();
mFreedBytes = findPreference(KEY_FREED);
long freedBytes = Settings.Secure.getLong(cr, long freedBytes = Settings.Secure.getLong(cr,
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_BYTES_CLEARED, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_BYTES_CLEARED,
0); 0);
@@ -93,11 +107,13 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
if (freedBytes == 0 || lastRunMillis == 0) { if (freedBytes == 0 || lastRunMillis == 0) {
mFreedBytes.setVisible(false); mFreedBytes.setVisible(false);
} else { } else {
Activity activity = getActivity(); final Activity activity = getActivity();
mFreedBytes.setSummary(activity.getString( mFreedBytes.setSummary(
R.string.automatic_storage_manager_freed_bytes, activity.getString(
Formatter.formatFileSize(activity, freedBytes), R.string.automatic_storage_manager_freed_bytes,
DateUtils.formatDateTime(activity, lastRunMillis, DateUtils.FORMAT_SHOW_DATE))); Formatter.formatFileSize(activity, freedBytes),
DateUtils.formatDateTime(
activity, lastRunMillis, DateUtils.FORMAT_SHOW_DATE)));
} }
} }
@@ -107,31 +123,27 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
boolean isStorageManagerChecked = boolean isStorageManagerChecked =
Settings.Secure.getInt(getContentResolver(), Settings.Secure.getInt(getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0; Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0;
mStorageManagerSwitch.setChecked(isStorageManagerChecked); // 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); mDaysToRetain.setEnabled(isStorageManagerChecked);
} }
@Override
public void onDestroyView() {
super.onDestroyView();
mSwitchBar.hide();
mSwitchController.tearDown();
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
switch (preference.getKey()) { if (KEY_DAYS.equals(preference.getKey())) {
case KEY_STORAGE_MANAGER_SWITCH: Settings.Secure.putInt(
boolean storageManagerChecked = (boolean) newValue; getContentResolver(),
mMetricsFeatureProvider.action(getContext(), Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN,
MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER, storageManagerChecked); Integer.parseInt((String) newValue));
mDaysToRetain.setEnabled(storageManagerChecked);
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED,
storageManagerChecked ? 1 : 0);
// Only show a warning if enabling.
if (storageManagerChecked) {
maybeShowWarning();
}
break;
case KEY_DAYS:
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN,
Integer.parseInt((String) newValue));
break;
} }
return true; return true;
} }
@@ -141,15 +153,6 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
return MetricsEvent.STORAGE_MANAGER_SETTINGS; return MetricsEvent.STORAGE_MANAGER_SETTINGS;
} }
@Override
public boolean onPreferenceClick(Preference preference) {
if (KEY_DELETION_HELPER.equals(preference.getKey())) {
Intent intent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
getContext().startActivity(intent);
}
return true;
}
@Override @Override
protected int getHelpResource() { protected int getHelpResource() {
return R.string.help_uri_storage; return R.string.help_uri_storage;
@@ -164,15 +167,4 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
} }
return indices.length - 1; return indices.length - 1;
} }
private void maybeShowWarning() {
// If the storage manager is on by default, we can use the normal message.
boolean warningUnneeded = SystemProperties.getBoolean(
STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, false);
if (warningUnneeded) {
return;
}
ActivationWarningFragment fragment = ActivationWarningFragment.newInstance();
fragment.show(getFragmentManager(), ActivationWarningFragment.TAG);
}
} }

View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deletionhelper;
import android.app.FragmentManager;
import android.content.Context;
import android.os.SystemProperties;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.widget.Switch;
import com.android.internal.util.Preconditions;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.widget.SwitchBar;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
/** Handles the logic for flipping the storage management toggle on a {@link SwitchBar}. */
public class AutomaticStorageManagerSwitchBarController
implements SwitchBar.OnSwitchChangeListener {
private static final String STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY =
"ro.storage_manager.enabled";
private Context mContext;
private SwitchBar mSwitchBar;
private MetricsFeatureProvider mMetrics;
private Preference mDaysToRetainPreference;
private FragmentManager mFragmentManager;
public AutomaticStorageManagerSwitchBarController(
Context context,
SwitchBar switchBar,
MetricsFeatureProvider metrics,
Preference daysToRetainPreference,
FragmentManager fragmentManager) {
mContext = Preconditions.checkNotNull(context);
mSwitchBar = Preconditions.checkNotNull(switchBar);
mMetrics = Preconditions.checkNotNull(metrics);
mDaysToRetainPreference = Preconditions.checkNotNull(daysToRetainPreference);
mFragmentManager = Preconditions.checkNotNull(fragmentManager);
mSwitchBar.addOnSwitchChangeListener(this);
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
mMetrics.action(mContext, MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER, isChecked);
mDaysToRetainPreference.setEnabled(isChecked);
Settings.Secure.putInt(
mContext.getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED,
isChecked ? 1 : 0);
// Only show a warning if enabling.
if (isChecked) {
maybeShowWarning();
}
}
/** Unregisters the controller from listening to further events. */
public void tearDown() {
mSwitchBar.removeOnSwitchChangeListener(this);
}
private void maybeShowWarning() {
// If the storage manager is on by default, we don't need to show the additional dialog.
if (SystemProperties.getBoolean(STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, false)) {
return;
}
ActivationWarningFragment fragment = ActivationWarningFragment.newInstance();
fragment.show(mFragmentManager, ActivationWarningFragment.TAG);
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.deletionhelper;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v7.preference.Preference;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
import com.android.settings.widget.SwitchBar;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class AutomaticStorageManagerSwitchBarControllerTest {
private Context mContext;
private SwitchBar mSwitchBar;
private MetricsFeatureProvider mMetricsFeatureProvider;
private Preference mPreference;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private FragmentManager mFragmentManager;
private AutomaticStorageManagerSwitchBarController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mSwitchBar = new SwitchBar(mContext);
Context fakeContextForFakeProvider = mock(Context.class, RETURNS_DEEP_STUBS);
FeatureFactory featureFactory = FakeFeatureFactory.getFactory(fakeContextForFakeProvider);
mMetricsFeatureProvider = featureFactory.getMetricsFeatureProvider();
mPreference = new Preference(mContext);
mController =
new AutomaticStorageManagerSwitchBarController(
mContext,
mSwitchBar,
mMetricsFeatureProvider,
mPreference,
mFragmentManager);
}
@Test
public void onSwitchChanged_false_recordsAMetric() {
mController.onSwitchChanged(null, false);
verify(mMetricsFeatureProvider)
.action(
eq(mContext),
eq(MetricsProto.MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER),
eq(false));
}
@Test
public void onSwitchChanged_true_recordsAMetric() {
mController.onSwitchChanged(null, true);
verify(mMetricsFeatureProvider)
.action(
eq(mContext),
eq(MetricsProto.MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER),
eq(true));
}
@Test
public void onSwitchChanged_showWarningFragmentIfNotEnabledByDefault() {
mController.onSwitchChanged(null, true);
verify(mFragmentManager.beginTransaction())
.add(any(Fragment.class), eq(ActivationWarningFragment.TAG));
}
@Config(shadows = {SettingsShadowSystemProperties.class})
@Test
public void onSwitchChange_doNotShowWarningFragmentIfEnabledByDefault() {
SettingsShadowSystemProperties.set("ro.storage_manager.enabled", "true");
mController.onSwitchChanged(null, true);
verify(mFragmentManager.beginTransaction(), never())
.add(any(Fragment.class), eq(ActivationWarningFragment.TAG));
}
}