Merge "Update the Manage Storage preference."

This commit is contained in:
TreeHugger Robot
2017-03-10 22:01:57 +00:00
committed by Android (Google) Code Review
12 changed files with 303 additions and 222 deletions

View File

@@ -56,15 +56,6 @@
android:textAlignment="viewStart"
android:textAppearance="@android:style/TextAppearance.Material.Body1" />
<TextView
android:id="@+id/storage_manager_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:textAlignment="viewStart"
android:textAppearance="@android:style/TextAppearance.Material.Body1"/>
<Button
android:id="@+id/deletion_helper_button"
android:layout_width="wrap_content"

View File

@@ -8251,18 +8251,6 @@
<!-- The percent of storage used by a storage volume. Exposed inside of a donut graph. [CHAR LIMIT=4]-->
<string name="storage_percent_used"><xliff:g id="percent" example="50%">%1$s</xliff:g>%%</string>
<!-- Indicates if the automatic storage manager is enabled or not. [CHAR_LIMIT=40] -->
<string name="storage_manager_indicator">Storage Manager: <xliff:g id="status" example="on">^1</xliff:g></string>
<!-- Off status for the automatic storage manager. [CHAR_LIMIT=10] -->
<string name="storage_manager_indicator_off">Off</string>
<!-- On status for the automatic storage manager. [CHAR_LIMIT=10] -->
<string name="storage_manager_indicator_on">On</string>
<!-- Added as the value of a header field indicating this is an instant app (as opposed to installed normally) -->
<string name="install_type_instant">Instant app</string>
<!-- Title of games app storage screen [CHAR LIMIT=30] -->
<string name="game_storage_settings">Games</string>
@@ -8291,4 +8279,16 @@
<!-- Temporary reboot string, will be removed -->
<string name="change_theme_reboot" translatable="false">Changing the theme requires a restart.</string>
<!-- Indicates if the automatic storage manager is enabled or not. [CHAR_LIMIT=40] -->
<string name="storage_manager_indicator">Storage Manager: <xliff:g id="status" example="on">^1</xliff:g></string>
<!-- Off status for the automatic storage manager. [CHAR_LIMIT=10] -->
<string name="storage_manager_indicator_off">Off</string>
<!-- On status for the automatic storage manager. [CHAR_LIMIT=10] -->
<string name="storage_manager_indicator_on">On</string>
<!-- Added as the value of a header field indicating this is an instant app (as opposed to installed normally) -->
<string name="install_type_instant">Instant app</string>
</resources>

View File

@@ -21,45 +21,38 @@
<com.android.settings.deviceinfo.storage.StorageSummaryDonutPreference
android:key="pref_summary"
android:order="0" />
<com.android.settings.widget.MasterSwitchPreference
android:fragment="com.android.settings.deletionhelper.AutomaticStorageManagerSettings"
android:key="toggle_asm"
android:title="@string/automatic_storage_manager_preference_title"
android:icon="@drawable/ic_settings_storage"
android:order="1" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
android:key="pref_photos_videos"
android:title="@string/storage_photos_videos"
android:order="1" >
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
android:order="2" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
android:key="pref_music_audio"
android:title="@string/storage_music_audio"
android:order="2" >
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
android:order="3" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
android:key="pref_games"
android:title="@string/storage_games"
android:order="3" >
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
android:order="4" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
android:key="pref_other_apps"
android:title="@string/storage_other_apps"
android:order="4" >
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
android:order="5" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
android:key="pref_files"
android:title="@string/storage_files"
android:order="5" >
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
android:order="6" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate
android:key="pref_system"
android:title="@string/storage_detail_system"
android:order="100" >
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate>
android:order="100" />
<PreferenceCategory
android:key="pref_secondary_users"
android:title="@string/storage_other_users"
android:order="200" />
<Preference
android:key="manage_storage"
android:title="@string/storage_menu_manage"
android:icon="@drawable/ic_settings_storage"
android:fragment="com.android.settings.deletionhelper.AutomaticStorageManagerSettings"
android:order="300" >
</Preference>
</PreferenceScreen>

View File

@@ -18,6 +18,7 @@ package com.android.settings.deletionhelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;

View File

@@ -1,41 +0,0 @@
/*
* Copyright (C) 2016 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.deviceinfo;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
public class ManageStoragePreferenceController extends PreferenceController {
public static final String KEY_MANAGE_STORAGE = "manage_storage";
public ManageStoragePreferenceController(Context context) {
super(context);
}
@Override
public String getPreferenceKey() {
return KEY_MANAGE_STORAGE;
}
@Override
public boolean isAvailable() {
return mContext.getResources().getBoolean(R.bool.config_storage_manager_settings_enabled);
}
}

View File

@@ -35,6 +35,7 @@ import com.android.settings.applications.UserManagerWrapper;
import com.android.settings.applications.UserManagerWrapperImpl;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.deviceinfo.storage.AutomaticStorageManagementSwitchPreferenceController;
import com.android.settings.deviceinfo.storage.SecondaryUserController;
import com.android.settings.deviceinfo.storage.StorageAsyncLoader;
import com.android.settings.deviceinfo.storage.StorageItemPreferenceController;
@@ -146,7 +147,11 @@ public class StorageDashboardFragment extends DashboardFragment
mSecondaryUsers = SecondaryUserController.getSecondaryUserControllers(context, userManager);
controllers.addAll(mSecondaryUsers);
controllers.add(new ManageStoragePreferenceController(context));
final AutomaticStorageManagementSwitchPreferenceController asmController =
new AutomaticStorageManagementSwitchPreferenceController(
context, mMetricsFeatureProvider, getFragmentManager());
getLifecycle().addObserver(asmController);
controllers.add(asmController);
return controllers;
}
@@ -189,7 +194,6 @@ public class StorageDashboardFragment extends DashboardFragment
null /* volume */, new StorageManagerVolumeProvider(sm)));
controllers.addAll(SecondaryUserController.getSecondaryUserControllers(
context, userManager));
controllers.add(new ManageStoragePreferenceController(context));
return controllers;
}

View File

@@ -0,0 +1,100 @@
/**
* 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.deviceinfo.storage;
import android.app.FragmentManager;
import android.content.Context;
import android.os.SystemProperties;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.core.PreferenceController;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.core.lifecycle.LifecycleObserver;
import com.android.settings.core.lifecycle.events.OnResume;
import com.android.settings.deletionhelper.ActivationWarningFragment;
import com.android.settings.widget.MasterSwitchController;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settings.widget.SwitchWidgetController;
public class AutomaticStorageManagementSwitchPreferenceController extends PreferenceController
implements LifecycleObserver, OnResume, SwitchWidgetController.OnSwitchChangeListener {
private static final String KEY_TOGGLE_ASM = "toggle_asm";
@VisibleForTesting
static final String STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY = "ro.storage_manager.enabled";
private MasterSwitchPreference mSwitch;
private MasterSwitchController mSwitchController;
private final MetricsFeatureProvider mMetricsFeatureProvider;
private final FragmentManager mFragmentManager;
public AutomaticStorageManagementSwitchPreferenceController(Context context,
MetricsFeatureProvider metricsFeatureProvider, FragmentManager fragmentManager) {
super(context);
mMetricsFeatureProvider = metricsFeatureProvider;
mFragmentManager = fragmentManager;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mSwitch = (MasterSwitchPreference) screen.findPreference(KEY_TOGGLE_ASM);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_TOGGLE_ASM;
}
@Override
public void onResume() {
boolean isStorageManagerEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0;
mSwitch.setChecked(isStorageManagerEnabled);
if (mSwitch != null) {
mSwitchController = new MasterSwitchController(mSwitch);
mSwitchController.setListener(this);
mSwitchController.startListening();
}
}
@Override
public boolean onSwitchToggled(boolean isChecked) {
mMetricsFeatureProvider.action(mContext,
MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER, isChecked);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED,
isChecked ? 1 : 0);
boolean storageManagerEnabledByDefault = SystemProperties.getBoolean(
STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, false);
if (isChecked && !storageManagerEnabledByDefault) {
ActivationWarningFragment fragment = ActivationWarningFragment.newInstance();
fragment.show(mFragmentManager, ActivationWarningFragment.TAG);
}
return true;
}
}

View File

@@ -80,23 +80,6 @@ public class StorageSummaryDonutPreference extends Preference implements View.On
if (deletionHelperButton != null) {
deletionHelperButton.setOnClickListener(this);
}
final TextView storageManagerText =
(TextView) view.findViewById(R.id.storage_manager_indicator);
if (storageManagerText != null) {
Context context = getContext();
final SpannableString templateSs = new SpannableString(
context.getString(R.string.storage_manager_indicator));
boolean isStorageManagerEnabled = Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0;
String value = isStorageManagerEnabled ?
context.getString(R.string.storage_manager_indicator_on) :
context.getString(R.string.storage_manager_indicator_off);
Locale locale = storageManagerText.getTextLocale();
final SpannableString ss = new SpannableString(value.toUpperCase(locale));
ss.setSpan(new BoldLinkSpan(), 0, value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
storageManagerText.setText(TextUtils.expandTemplate(templateSs, ss));
}
}
@Override

View File

@@ -17,7 +17,6 @@
package com.android.settings.deviceinfo.storage;
import android.content.Context;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
@@ -26,7 +25,6 @@ import android.text.format.Formatter;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
import com.android.settingslib.deviceinfo.StorageVolumeProvider;
/**

View File

@@ -1,102 +0,0 @@
/*
* Copyright (C) 2016 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.deviceinfo;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import java.util.ArrayList;
import java.util.List;
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.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ManageStoragePreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
private ManageStoragePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new ManageStoragePreferenceController(mContext);
}
@Test
public void updateNonIndexableKey_prefUnavaiable_shouldUpdate() {
final List<String> keys = new ArrayList<>();
mController.updateNonIndexableKeys(keys);
assertThat(keys).isNotEmpty();
}
@Test
public void updateNonIndexableKey_prefAvaiable_shouldNotUpdate() {
final List<String> keys = new ArrayList<>();
when(mContext.getResources().getBoolean(
com.android.settings.R.bool.config_storage_manager_settings_enabled))
.thenReturn(true);
mController.updateNonIndexableKeys(keys);
assertThat(keys).isEmpty();
}
@Test
public void displayPref_prefAvaiable_shouldDisplay() {
when(mContext.getResources().getBoolean(
com.android.settings.R.bool.config_storage_manager_settings_enabled))
.thenReturn(true);
mController.displayPreference(mScreen);
verify(mScreen, never()).removePreference(any(Preference.class));
}
@Test
public void displayPref_prefNotAvaiable_shouldNotDisplay() {
final Preference preference = mock(Preference.class);
when(mScreen.getPreferenceCount()).thenReturn(1);
when(mScreen.getPreference(0)).thenReturn(preference);
when(preference.getKey()).thenReturn(mController.getPreferenceKey());
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
}
}

View File

@@ -0,0 +1,172 @@
/*
* 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.deviceinfo.storage;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.deletionhelper.ActivationWarningFragment;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settings.overlay.FeatureFactory;
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 AutomaticStorageManagementSwitchPreferenceControllerTest {
@Mock
private PreferenceScreen mScreen;
@Mock
private MasterSwitchPreference mPreference;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mMockContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private FragmentManager mFragmentManager;
private Context mContext;
private AutomaticStorageManagementSwitchPreferenceController mController;
private MetricsFeatureProvider mMetricsFeature;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application.getApplicationContext();
FeatureFactory factory = FeatureFactory.getFactory(mContext);
mMetricsFeature = factory.getMetricsFeatureProvider();
mController = new AutomaticStorageManagementSwitchPreferenceController(
mContext, mMetricsFeature, mFragmentManager);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@Test
public void isAvailable_shouldAlwaysReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void onResume_shouldReflectEnabledStatus() {
mController.displayPreference(mScreen);
ContentResolver resolver = mContext.getContentResolver();
Settings.Secure.putInt(resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 1);
mController.onResume();
verify(mPreference).setChecked(eq(true));
}
@Test
public void onResume_shouldRegisterCallback() {
mController.displayPreference(mScreen);
mController.onResume();
verify(mPreference).setOnPreferenceChangeListener(
any(Preference.OnPreferenceChangeListener.class));
}
@Test
public void togglingShouldCauseMetricsEvent() {
// FakeFeatureFactory uses mock contexts, so this test scaffolds itself rather than using
// the instance variables.
FakeFeatureFactory.setupForTest(mMockContext);
FakeFeatureFactory factory =
(FakeFeatureFactory) FakeFeatureFactory.getFactory(mMockContext);
AutomaticStorageManagementSwitchPreferenceController controller =
new AutomaticStorageManagementSwitchPreferenceController(
mMockContext, factory.metricsFeatureProvider, mFragmentManager);
controller.onSwitchToggled(true);
verify(factory.metricsFeatureProvider, times(1)).action(
any(Context.class), eq(MetricsEvent.ACTION_TOGGLE_STORAGE_MANAGER), eq(true));
}
@Test
public void togglingShouldUpdateSettingsSecure() {
mController.onSwitchToggled(true);
ContentResolver resolver = mContext.getContentResolver();
assertThat(Settings.Secure.getInt(
resolver, Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0)).isNotEqualTo(0);
}
@Test
public void togglingOnShouldTriggerWarningFragment() {
FragmentTransaction transaction = mock(FragmentTransaction.class);
when (mFragmentManager.beginTransaction()).thenReturn(transaction);
mController.onSwitchToggled(true);
verify(transaction).add(any(), eq(ActivationWarningFragment.TAG));
}
@Test
public void togglingOffShouldTriggerWarningFragment() {
FragmentTransaction transaction = mock(FragmentTransaction.class);
when (mFragmentManager.beginTransaction()).thenReturn(transaction);
mController.onSwitchToggled(false);
verify(transaction, never()).add(any(), eq(ActivationWarningFragment.TAG));
}
@Config(shadows = {SettingsShadowSystemProperties.class})
@Test
public void togglingOnShouldNotTriggerWarningFragmentIfEnabledByDefault() {
FragmentTransaction transaction = mock(FragmentTransaction.class);
when (mFragmentManager.beginTransaction()).thenReturn(transaction);
SettingsShadowSystemProperties.set(
AutomaticStorageManagementSwitchPreferenceController
.STORAGE_MANAGER_ENABLED_BY_DEFAULT_PROPERTY, "true");
mController.onSwitchToggled(true);
verify(transaction, never()).add(any(), eq(ActivationWarningFragment.TAG));
}
}

View File

@@ -113,13 +113,6 @@ public class StorageSummaryDonutPreferenceControllerTest {
assertThat(mPreference.getSummary().toString()).isEqualTo("1.00KB free");
}
@Test
public void testAutomaticStorageManagerLabelOff() throws Exception {
mPreference.onBindViewHolder(mHolder);
TextView asmTextView = (TextView) mHolder.findViewById(R.id.storage_manager_indicator);
assertThat(asmTextView.getText().toString()).isEqualTo("Storage Manager: OFF");
}
@Test
public void testFreeUpSpaceMetricIsTriggered() throws Exception {
mPreference.onBindViewHolder(mHolder);
@@ -130,15 +123,4 @@ public class StorageSummaryDonutPreferenceControllerTest {
verify(mMetricsFeatureProvider, times(1)).action(
any(Context.class), eq(MetricsEvent.STORAGE_FREE_UP_SPACE_NOW));
}
@Test
public void testAutomaticStorageManagerLabelOn() throws Exception {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 1);
mPreference.onBindViewHolder(mHolder);
TextView asmTextView = (TextView) mHolder.findViewById(R.id.storage_manager_indicator);
assertThat(asmTextView.getText().toString()).isEqualTo("Storage Manager: ON");
}
}