Add SafetyCenter entry to Settings.
Test: atest SettingsUnitTests
Bug: 204978295
Change-Id: I9bedb58ad740b4ee76620e04675a0aefa659566a
diff --git a/res/drawable/ic_settings_safety_center.xml b/res/drawable/ic_settings_safety_center.xml
new file mode 100644
index 0000000..f43359f
--- /dev/null
+++ b/res/drawable/ic_settings_safety_center.xml
@@ -0,0 +1,29 @@
+<!--
+ ~ Copyright (C) 2021 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.
+ -->
+<!-- TODO(b/208624929): Update to an UX approved icon. -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="24dp"
+ android:height="24dp"
+ android:viewportHeight="24"
+ android:viewportWidth="24"
+ android:tint="?android:attr/colorControlNormal">
+ <path
+ android:fillColor="#FFFFFFFF"
+ android:pathData="M12,15m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0"/>
+ <path
+ android:fillColor="#FFFFFFFF"
+ android:pathData="M18.5,1C16.01,1 14,3.01 14,5.5V8H6c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2V10c0,-1.1 -0.9,-2 -2,-2h-2V5.5C16,4.12 17.12,3 18.5,3C19.88,3 21,4.12 21,5.5V6h2V5.5C23,3.01 20.99,1 18.5,1zM18,10v10H6V10H18z"/>
+</vector>
\ No newline at end of file
diff --git a/res/values/menu_keys.xml b/res/values/menu_keys.xml
index e69664a..2841b69 100755
--- a/res/values/menu_keys.xml
+++ b/res/values/menu_keys.xml
@@ -26,6 +26,7 @@
<string name="menu_key_display" translatable="false">top_level_display</string>
<string name="menu_key_wallpaper" translatable="false">top_level_wallpaper</string>
<string name="menu_key_accessibility" translatable="false">top_level_accessibility</string>
+ <string name="menu_key_safety_center" translatable="false">top_level_safety_center</string>
<string name="menu_key_security" translatable="false">top_level_security</string>
<string name="menu_key_privacy" translatable="false">top_level_privacy</string>
<string name="menu_key_location" translatable="false">top_level_location</string>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index bf9fc2a..995742d 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -722,6 +722,12 @@
<string name="security_status_title">Security status</string>
<!-- Summary for Security settings, explaining a few important settings under it [CHAR LIMIT=NONE] -->
<string name="security_dashboard_summary">Screen lock, Find My Device, app security</string>
+ <!-- TODO(b/208624929): Update to an UX approved title and char limit. -->
+ <!-- Main Settings screen setting title for the item that takes you to the safety center [CHAR LIMIT=60] -->
+ <string name="safety_center_title">Security & privacy</string>
+ <!-- TODO(b/208624929): Update to an UX approved summary and char limit. -->
+ <!-- Main Settings screen setting summary for the item that takes you to the safety center [CHAR LIMIT=60] -->
+ <string name="safety_center_summary">Permissions, screen lock, app security</string>
<!-- Face enrollment and settings --><skip />
<!-- Note: Update FaceEnrollParentalConsent.CONSENT_STRING_RESOURCES when any _consent_ strings are added or removed. -->
diff --git a/res/xml/top_level_settings.xml b/res/xml/top_level_settings.xml
index 042ce43..6005581 100644
--- a/res/xml/top_level_settings.xml
+++ b/res/xml/top_level_settings.xml
@@ -117,6 +117,15 @@
settings:controller="com.android.settings.accessibility.TopLevelAccessibilityPreferenceController"/>
<com.android.settings.widget.HomepagePreference
+ android:icon="@drawable/ic_settings_safety_center"
+ android:key="top_level_safety_center"
+ android:order="-55"
+ android:title="@string/safety_center_title"
+ android:summary="@string/safety_center_summary"
+ settings:highlightableMenuKey="@string/menu_key_safety_center"
+ settings:controller="com.android.settings.safetycenter.TopLevelSafetyCenterEntryPreferenceController"/>
+
+ <com.android.settings.widget.HomepagePreference
android:fragment="com.android.settings.security.SecuritySettings"
android:icon="@drawable/ic_settings_security_white"
android:key="top_level_security"
diff --git a/src/com/android/settings/safetycenter/SafetyCenterStatus.java b/src/com/android/settings/safetycenter/SafetyCenterStatus.java
new file mode 100644
index 0000000..491ee4d
--- /dev/null
+++ b/src/com/android/settings/safetycenter/SafetyCenterStatus.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2021 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.safetycenter;
+
+import android.provider.DeviceConfig;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+/** Knows whether safety center is enabled or disabled. */
+public class SafetyCenterStatus {
+
+ /** Whether SafetyCenter page is enabled. */
+ @VisibleForTesting
+ static final String SAFETY_CENTER_IS_ENABLED = "safety_center_is_enabled";
+
+ /** Returns true is SafetyCenter page is enabled, false otherwise. */
+ public static boolean isEnabled() {
+ // TODO(b/208625216): use SafetyManager API instead
+ return DeviceConfig.getBoolean(
+ DeviceConfig.NAMESPACE_PRIVACY, SAFETY_CENTER_IS_ENABLED, false);
+ }
+}
diff --git a/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java b/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java
new file mode 100644
index 0000000..b3b49b0
--- /dev/null
+++ b/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2021 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.safetycenter;
+
+import android.content.ActivityNotFoundException;
+import android.content.Context;
+import android.content.Intent;
+import android.text.TextUtils;
+import android.util.Log;
+
+import androidx.preference.Preference;
+
+import com.android.settings.core.BasePreferenceController;
+
+/** Controller for the SafetyCenter entry in top level Settings. */
+public class TopLevelSafetyCenterEntryPreferenceController extends BasePreferenceController {
+
+ private static final String TAG = "TopLevelSafetyCenterEntryPreferenceController";
+
+ public TopLevelSafetyCenterEntryPreferenceController(Context context, String preferenceKey) {
+ super(context, preferenceKey);
+ }
+
+ @Override
+ public int getAvailabilityStatus() {
+ if (SafetyCenterStatus.isEnabled()) {
+ return AVAILABLE;
+ }
+ return CONDITIONALLY_UNAVAILABLE;
+ }
+
+ @Override
+ public boolean handlePreferenceTreeClick(Preference preference) {
+ if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
+ return super.handlePreferenceTreeClick(preference);
+ }
+
+ try {
+ mContext.startActivity(new Intent(Intent.ACTION_SAFETY_CENTER));
+ } catch (ActivityNotFoundException e) {
+ Log.e(TAG, "Unable to open safety center", e);
+ return false;
+ }
+ return true;
+ }
+}
diff --git a/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusTest.java b/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusTest.java
new file mode 100644
index 0000000..f36fbe1
--- /dev/null
+++ b/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2021 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.safetycenter;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.provider.DeviceConfig;
+import android.provider.Settings;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class SafetyCenterStatusTest {
+
+ @Before
+ public void setUp() {
+ DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS,
+ DeviceConfig.NAMESPACE_PRIVACY);
+ }
+
+ @After
+ public void tearDown() {
+ DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS,
+ DeviceConfig.NAMESPACE_PRIVACY);
+ }
+
+ @Test
+ public void isEnabled_whenFlagTrue_returnsTrue() {
+ DeviceConfig.setProperty(
+ DeviceConfig.NAMESPACE_PRIVACY,
+ SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
+ /* value = */ Boolean.toString(true),
+ /* makeDefault = */ false);
+
+ assertThat(SafetyCenterStatus.isEnabled()).isTrue();
+ }
+
+ @Test
+ public void isEnabled_whenFlagFalse_returnsFalse() {
+ DeviceConfig.setProperty(
+ DeviceConfig.NAMESPACE_PRIVACY,
+ SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
+ /* value = */ Boolean.toString(false),
+ /* makeDefault = */ false);
+
+ assertThat(SafetyCenterStatus.isEnabled()).isFalse();
+ }
+}
diff --git a/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java b/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java
new file mode 100644
index 0000000..907fb99
--- /dev/null
+++ b/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2021 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.safetycenter;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.verify;
+
+import android.content.ActivityNotFoundException;
+import android.content.Context;
+import android.content.Intent;
+import android.provider.DeviceConfig;
+import android.provider.Settings;
+
+import androidx.preference.Preference;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(AndroidJUnit4.class)
+public class TopLevelSafetyCenterEntryPreferenceControllerTest {
+
+ private static final String PREFERENCE_KEY = "top_level_safety_center";
+
+ private TopLevelSafetyCenterEntryPreferenceController
+ mTopLevelSafetyCenterEntryPreferenceController;
+ private Preference mPreference;
+
+ @Mock
+ private Context mContext;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ mPreference = new Preference(ApplicationProvider.getApplicationContext());
+ mPreference.setKey(PREFERENCE_KEY);
+
+ doNothing().when(mContext).startActivity(any(Intent.class));
+ mTopLevelSafetyCenterEntryPreferenceController =
+ new TopLevelSafetyCenterEntryPreferenceController(mContext, PREFERENCE_KEY);
+ DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS,
+ DeviceConfig.NAMESPACE_PRIVACY);
+ }
+
+ @After
+ public void tearDown() {
+ DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS,
+ DeviceConfig.NAMESPACE_PRIVACY);
+ }
+
+ @Test
+ public void handlePreferenceTreeClick_forDifferentPreferenceKey_isNotHandled() {
+ Preference preference = new Preference(ApplicationProvider.getApplicationContext());
+ preference.setKey("some_other_preference");
+
+ boolean preferenceHandled =
+ mTopLevelSafetyCenterEntryPreferenceController.handlePreferenceTreeClick(
+ preference);
+
+ assertThat(preferenceHandled).isFalse();
+ }
+
+ @Test
+ public void handlePreferenceTreeClick_launchesIntendedIntent() {
+ final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
+
+ boolean preferenceHandled = mTopLevelSafetyCenterEntryPreferenceController
+ .handlePreferenceTreeClick(mPreference);
+
+ assertThat(preferenceHandled).isTrue();
+ verify(mContext).startActivity(intentCaptor.capture());
+ assertThat(intentCaptor.getValue().getAction()).isEqualTo(Intent.ACTION_SAFETY_CENTER);
+ }
+
+ @Test
+ public void handlePreferenceTreeClick_onStartActivityThrows_returnsFalse() {
+ doThrow(ActivityNotFoundException.class)
+ .when(mContext).startActivity(any(Intent.class));
+
+ boolean preferenceHandled = mTopLevelSafetyCenterEntryPreferenceController
+ .handlePreferenceTreeClick(mPreference);
+
+ assertThat(preferenceHandled).isFalse();
+ }
+
+ @Test
+ public void getAvailabilityStatus_whenSafetyCenterDisabled_returnsUnavailable() {
+ DeviceConfig.setProperty(
+ DeviceConfig.NAMESPACE_PRIVACY,
+ SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
+ /* value = */ Boolean.toString(false),
+ /* makeDefault = */ false);
+
+ assertThat(mTopLevelSafetyCenterEntryPreferenceController.getAvailabilityStatus())
+ .isEqualTo(TopLevelSafetyCenterEntryPreferenceController.CONDITIONALLY_UNAVAILABLE);
+ }
+
+ @Test
+ public void getAvailabilityStatus_whenSafetyCenterEnabled_returnsAvailable() {
+ DeviceConfig.setProperty(
+ DeviceConfig.NAMESPACE_PRIVACY,
+ SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
+ /* value = */ Boolean.toString(true),
+ /* makeDefault = */ false);
+
+ assertThat(mTopLevelSafetyCenterEntryPreferenceController.getAvailabilityStatus())
+ .isEqualTo(TopLevelSafetyCenterEntryPreferenceController.AVAILABLE);
+ }
+}