diff --git a/src/com/android/settings/system/FactoryResetPreferenceController.java b/src/com/android/settings/system/FactoryResetPreferenceController.java new file mode 100644 index 00000000000..95a9e5494cc --- /dev/null +++ b/src/com/android/settings/system/FactoryResetPreferenceController.java @@ -0,0 +1,46 @@ +/* + * 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.system; + +import android.content.Context; + +import android.os.UserManager; + +import com.android.settings.R; +import com.android.settings.core.PreferenceController; + +public class FactoryResetPreferenceController extends PreferenceController { + /** Key of the "Factory reset" preference in {@link R.xml.system_dashboard_fragment}.*/ + private static final String KEY_FACTORY_RESET = "factory_reset"; + + private final UserManager mUm; + + public FactoryResetPreferenceController(Context context, UserManager um) { + super(context); + mUm = um; + } + + /** Hide "Factory reset" settings for secondary users. */ + @Override + public boolean isAvailable() { + return mUm.isAdminUser(); + } + + @Override + public String getPreferenceKey() { + return KEY_FACTORY_RESET; + } +} diff --git a/src/com/android/settings/system/SystemDashboardFragment.java b/src/com/android/settings/system/SystemDashboardFragment.java index 76ac4a6dab0..d4c63c2ee32 100644 --- a/src/com/android/settings/system/SystemDashboardFragment.java +++ b/src/com/android/settings/system/SystemDashboardFragment.java @@ -60,6 +60,7 @@ public class SystemDashboardFragment extends DashboardFragment { final List controllers = new ArrayList<>(); controllers.add(new SystemUpdatePreferenceController(context, UserManager.get(context))); controllers.add(new AdditionalSystemUpdatePreferenceController(context)); + controllers.add(new FactoryResetPreferenceController(context, UserManager.get(context))); return controllers; } diff --git a/tests/robotests/src/com/android/settings/system/FactoryResetPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/system/FactoryResetPreferenceControllerTest.java new file mode 100644 index 00000000000..f41870e79ab --- /dev/null +++ b/tests/robotests/src/com/android/settings/system/FactoryResetPreferenceControllerTest.java @@ -0,0 +1,74 @@ +/* + * 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.system; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Answers.RETURNS_DEEP_STUBS; + +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.os.UserManager; + +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; + + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class FactoryResetPreferenceControllerTest { + + private static final String FACTORY_RESET_KEY = "factory_reset"; + + @Mock(answer = RETURNS_DEEP_STUBS) + private Context mContext; + @Mock + private UserManager mUserManager; + + private FactoryResetPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mController = new FactoryResetPreferenceController(mContext, mUserManager); + } + + @Test + public void isAvailable_systemUser() { + when(mUserManager.isAdminUser()).thenReturn(true); + + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + public void isAvailable_nonSystemUser() { + when(mUserManager.isAdminUser()).thenReturn(false); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test public void getPreferenceKey() { + assertThat(mController.getPreferenceKey()).isEqualTo(FACTORY_RESET_KEY); + } +}