Call system service API instead of checking the phenotype flag directly.

Test: atest SettingsUnitTests
Bug: 208625216
Change-Id: Ib551cbaf0f09fe49bd81ef657037401263705521
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 66e4442..9582126 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -114,6 +114,7 @@
     <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
     <uses-permission android:name="android.permission.READ_APP_SPECIFIC_LOCALES" />
     <uses-permission android:name="android.permission.QUERY_ADMIN_POLICY" />
+    <uses-permission android:name="android.permission.READ_SAFETY_CENTER_STATUS" />
 
     <application
             android:name=".SettingsApplication"
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index 61d1180..2c213b4 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -33,7 +33,7 @@
 import com.android.settings.network.SubscriptionUtil;
 import com.android.settings.network.telephony.MobileNetworkUtils;
 import com.android.settings.overlay.FeatureFactory;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 import com.android.settings.security.SecuritySettingsFeatureProvider;
 
 import com.google.android.setupdesign.util.ThemeHelper;
@@ -152,7 +152,7 @@
         /** Redirects to SafetyCenter if enabled. */
         @VisibleForTesting
         public void handleSafetyCenterRedirection() {
-            if (SafetyCenterStatus.isEnabled()) {
+            if (SafetyCenterStatusHolder.get().isEnabled(this)) {
                 try {
                     startActivity(new Intent(Intent.ACTION_SAFETY_CENTER));
                     finish();
@@ -213,7 +213,7 @@
         /** Redirects to SafetyCenter if enabled. */
         @VisibleForTesting
         public void handleSafetyCenterRedirection() {
-            if (SafetyCenterStatus.isEnabled()) {
+            if (SafetyCenterStatusHolder.get().isEnabled(this)) {
                 try {
                     startActivity(new Intent(Intent.ACTION_SAFETY_CENTER));
                     finish();
diff --git a/src/com/android/settings/privacy/PrivacyDashboardFragment.java b/src/com/android/settings/privacy/PrivacyDashboardFragment.java
index c2aeeaf..7aa65b7 100644
--- a/src/com/android/settings/privacy/PrivacyDashboardFragment.java
+++ b/src/com/android/settings/privacy/PrivacyDashboardFragment.java
@@ -22,7 +22,7 @@
 import com.android.settings.R;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.notification.LockScreenNotificationPreferenceController;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -93,7 +93,7 @@
 
                 @Override
                 protected boolean isPageSearchEnabled(Context context) {
-                    return !SafetyCenterStatus.isEnabled();
+                    return !SafetyCenterStatusHolder.get().isEnabled(context);
                 }
             };
 }
diff --git a/src/com/android/settings/privacy/TopLevelPrivacyEntryPreferenceController.java b/src/com/android/settings/privacy/TopLevelPrivacyEntryPreferenceController.java
index dcc68ff..87daf7f 100644
--- a/src/com/android/settings/privacy/TopLevelPrivacyEntryPreferenceController.java
+++ b/src/com/android/settings/privacy/TopLevelPrivacyEntryPreferenceController.java
@@ -20,7 +20,7 @@
 import android.content.Context;
 
 import com.android.settings.core.BasePreferenceController;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 
 /** The preference controller for the top level privacy tile. */
 public class TopLevelPrivacyEntryPreferenceController  extends BasePreferenceController {
@@ -31,7 +31,7 @@
 
     @Override
     public int getAvailabilityStatus() {
-        if (!SafetyCenterStatus.isEnabled()) {
+        if (!SafetyCenterStatusHolder.get().isEnabled(mContext)) {
             return AVAILABLE;
         }
         return CONDITIONALLY_UNAVAILABLE;
diff --git a/src/com/android/settings/safetycenter/SafetyCenterStatus.java b/src/com/android/settings/safetycenter/SafetyCenterStatus.java
deleted file mode 100644
index d96bb32..0000000
--- a/src/com/android/settings/safetycenter/SafetyCenterStatus.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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
-    public 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/SafetyCenterStatusHolder.java b/src/com/android/settings/safetycenter/SafetyCenterStatusHolder.java
new file mode 100644
index 0000000..014c600
--- /dev/null
+++ b/src/com/android/settings/safetycenter/SafetyCenterStatusHolder.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2022 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.Context;
+import android.safetycenter.SafetyCenterManager;
+import android.util.Log;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+/** Knows whether safety center is enabled or disabled. */
+public class SafetyCenterStatusHolder {
+
+    private static final String TAG = "SafetyCenterStatusHolder";
+
+    @VisibleForTesting
+    public static SafetyCenterStatusHolder sInstance;
+
+    private SafetyCenterStatusHolder() {}
+
+    /** Returns an instance of {@link SafetyCenterStatusHolder}. */
+    public static SafetyCenterStatusHolder get() {
+        if (sInstance == null) {
+            sInstance = new SafetyCenterStatusHolder();
+        }
+        return sInstance;
+    }
+
+    /** Returns true is SafetyCenter page is enabled, false otherwise. */
+    public boolean isEnabled(Context context) {
+        if (context == null) {
+            Log.e(TAG, "Context is null at SafetyCenterStatusHolder#isEnabled");
+            return false;
+        }
+        SafetyCenterManager safetyCenterManager =
+                context.getSystemService(SafetyCenterManager.class);
+        if (safetyCenterManager == null) {
+            Log.w(TAG, "System service SAFETY_CENTER_SERVICE (SafetyCenterManager) is null");
+            return false;
+        }
+        try {
+            return safetyCenterManager.isSafetyCenterEnabled();
+        } catch (RuntimeException e) {
+            Log.e(TAG, "Calling isSafetyCenterEnabled failed.", e);
+            return false;
+        }
+    }
+}
diff --git a/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java b/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java
index b3b49b0..b00c084 100644
--- a/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java
+++ b/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java
@@ -37,7 +37,7 @@
 
     @Override
     public int getAvailabilityStatus() {
-        if (SafetyCenterStatus.isEnabled()) {
+        if (SafetyCenterStatusHolder.get().isEnabled(mContext)) {
             return AVAILABLE;
         }
         return CONDITIONALLY_UNAVAILABLE;
diff --git a/src/com/android/settings/security/SecurityAdvancedSettings.java b/src/com/android/settings/security/SecurityAdvancedSettings.java
index 4a73eb7..ce4a59c 100644
--- a/src/com/android/settings/security/SecurityAdvancedSettings.java
+++ b/src/com/android/settings/security/SecurityAdvancedSettings.java
@@ -26,7 +26,7 @@
 import com.android.settings.biometrics.fingerprint.FingerprintProfileStatusPreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.overlay.FeatureFactory;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.security.trustagent.TrustAgentListPreferenceController;
 import com.android.settings.widget.PreferenceCategoryController;
@@ -60,11 +60,14 @@
 
     @Override
     public String getCategoryKey() {
-        if (SafetyCenterStatus.isEnabled()) {
+        final Context context = getContext();
+        if (context == null) {
+            return CATEGORY_SECURITY_LEGACY_ADVANCED_SETTINGS;
+        } else if (SafetyCenterStatusHolder.get().isEnabled(context)) {
             return CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS;
         } else {
             final SecuritySettingsFeatureProvider securitySettingsFeatureProvider =
-                    FeatureFactory.getFactory(getContext())
+                    FeatureFactory.getFactory(context)
                             .getSecuritySettingsFeatureProvider();
 
             if (securitySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) {
diff --git a/src/com/android/settings/security/SecuritySettings.java b/src/com/android/settings/security/SecuritySettings.java
index 0d31ca2..6bfbf6c 100644
--- a/src/com/android/settings/security/SecuritySettings.java
+++ b/src/com/android/settings/security/SecuritySettings.java
@@ -25,7 +25,7 @@
 import com.android.settings.biometrics.fingerprint.FingerprintStatusPreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.overlay.FeatureFactory;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.security.trustagent.TrustAgentListPreferenceController;
 import com.android.settings.widget.PreferenceCategoryController;
@@ -129,7 +129,7 @@
                 protected boolean isPageSearchEnabled(Context context) {
                     return !FeatureFactory.getFactory(context).getSecuritySettingsFeatureProvider()
                             .hasAlternativeSecuritySettingsFragment()
-                            && !SafetyCenterStatus.isEnabled();
+                            && !SafetyCenterStatusHolder.get().isEnabled(context);
                 }
             };
 }
diff --git a/src/com/android/settings/security/TopLevelSecurityEntryPreferenceController.java b/src/com/android/settings/security/TopLevelSecurityEntryPreferenceController.java
index 2d98606..11d1dc3 100644
--- a/src/com/android/settings/security/TopLevelSecurityEntryPreferenceController.java
+++ b/src/com/android/settings/security/TopLevelSecurityEntryPreferenceController.java
@@ -24,7 +24,7 @@
 import com.android.settings.core.BasePreferenceController;
 import com.android.settings.core.SubSettingLauncher;
 import com.android.settings.overlay.FeatureFactory;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 
 public class TopLevelSecurityEntryPreferenceController extends BasePreferenceController {
 
@@ -38,7 +38,7 @@
 
     @Override
     public int getAvailabilityStatus() {
-        if (!SafetyCenterStatus.isEnabled()) {
+        if (!SafetyCenterStatusHolder.get().isEnabled(mContext)) {
             return AVAILABLE;
         }
         return CONDITIONALLY_UNAVAILABLE;
diff --git a/tests/unit/src/com/android/settings/privacy/PrivacyDashboardActivityTest.java b/tests/unit/src/com/android/settings/privacy/PrivacyDashboardActivityTest.java
index 2365553..d4df97f 100644
--- a/tests/unit/src/com/android/settings/privacy/PrivacyDashboardActivityTest.java
+++ b/tests/unit/src/com/android/settings/privacy/PrivacyDashboardActivityTest.java
@@ -23,34 +23,39 @@
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import android.content.Context;
 import android.content.Intent;
-import android.provider.DeviceConfig;
 
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 import androidx.test.platform.app.InstrumentationRegistry;
 
 import com.android.settings.Settings;
 import com.android.settings.SettingsActivity;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 
-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 PrivacyDashboardActivityTest {
 
     private static final String DEFAULT_FRAGMENT_CLASSNAME = "DefaultFragmentClassname";
 
+    @Mock
+    private SafetyCenterStatusHolder mSafetyCenterStatusHolder;
     private Settings.PrivacyDashboardActivity mActivity;
 
     @Before
-    public void setUp() {
-        DeviceConfig.resetToDefaults(android.provider.Settings.RESET_MODE_PACKAGE_DEFAULTS,
-                DeviceConfig.NAMESPACE_PRIVACY);
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        SafetyCenterStatusHolder.sInstance = mSafetyCenterStatusHolder;
         final Intent intent = new Intent();
         intent.setAction(android.provider.Settings.ACTION_PRIVACY_SETTINGS);
         intent.setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(),
@@ -71,19 +76,9 @@
         doNothing().when(mActivity).startActivity(any(Intent.class));
     }
 
-    @After
-    public void tearDown() {
-        DeviceConfig.resetToDefaults(android.provider.Settings.RESET_MODE_PACKAGE_DEFAULTS,
-                DeviceConfig.NAMESPACE_PRIVACY);
-    }
-
     @Test
     public void onCreate_whenSafetyCenterEnabled_redirectsToSafetyCenter() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(true),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(true);
         final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
 
         mActivity.handleSafetyCenterRedirection();
@@ -94,12 +89,7 @@
 
     @Test
     public void onCreate_whenSafetyCenterDisabled_doesntRedirectToSafetyCenter() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(false),
-                /* makeDefault = */ false);
-
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(false);
         mActivity.handleSafetyCenterRedirection();
 
         verify(mActivity, times(0)).startActivity(any());
diff --git a/tests/unit/src/com/android/settings/privacy/TopLevelPrivacyEntryPreferenceControllerTest.java b/tests/unit/src/com/android/settings/privacy/TopLevelPrivacyEntryPreferenceControllerTest.java
index 570df73..495700a 100644
--- a/tests/unit/src/com/android/settings/privacy/TopLevelPrivacyEntryPreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/privacy/TopLevelPrivacyEntryPreferenceControllerTest.java
@@ -18,20 +18,22 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import android.content.Context;
-import android.provider.DeviceConfig;
-import android.provider.Settings;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
 
+import android.content.Context;
+
+import androidx.test.core.app.ApplicationProvider;
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 import com.android.settings.security.TopLevelSecurityEntryPreferenceController;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 
 @RunWith(AndroidJUnit4.class)
 public class TopLevelPrivacyEntryPreferenceControllerTest {
@@ -41,30 +43,21 @@
     private TopLevelPrivacyEntryPreferenceController mTopLevelPrivacyEntryPreferenceController;
 
     @Mock
-    private Context mContext;
+    private SafetyCenterStatusHolder mSafetyCenterStatusHolder;
 
     @Before
     public void setUp() {
-        DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS,
-                DeviceConfig.NAMESPACE_PRIVACY);
+        MockitoAnnotations.initMocks(this);
+        SafetyCenterStatusHolder.sInstance = mSafetyCenterStatusHolder;
 
         mTopLevelPrivacyEntryPreferenceController =
-                new TopLevelPrivacyEntryPreferenceController(mContext, PREFERENCE_KEY);
-    }
-
-    @After
-    public void tearDown() {
-        DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS,
-                DeviceConfig.NAMESPACE_PRIVACY);
+                new TopLevelPrivacyEntryPreferenceController(
+                        ApplicationProvider.getApplicationContext(), PREFERENCE_KEY);
     }
 
     @Test
     public void getAvailabilityStatus_whenSafetyCenterEnabled_returnsUnavailable() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(true),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(true);
 
         assertThat(mTopLevelPrivacyEntryPreferenceController.getAvailabilityStatus())
                 .isEqualTo(TopLevelSecurityEntryPreferenceController.CONDITIONALLY_UNAVAILABLE);
@@ -72,11 +65,7 @@
 
     @Test
     public void getAvailabilityStatus_whenSafetyCenterDisabled_returnsAvailable() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(false),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(false);
 
         assertThat(mTopLevelPrivacyEntryPreferenceController.getAvailabilityStatus())
                 .isEqualTo(TopLevelSecurityEntryPreferenceController.AVAILABLE);
diff --git a/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusHolderTest.java b/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusHolderTest.java
new file mode 100644
index 0000000..2a31eb8
--- /dev/null
+++ b/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusHolderTest.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2022 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.Mockito.when;
+
+import android.content.Context;
+import android.safetycenter.SafetyCenterManager;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(AndroidJUnit4.class)
+public class SafetyCenterStatusHolderTest {
+
+    @Mock
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test
+    public void isEnabled_whenContextNull_returnsFalse() {
+        assertThat(SafetyCenterStatusHolder.get().isEnabled(null)).isFalse();
+    }
+
+    @Test
+    public void isEnabled_whenSystemServiceNull_returnsFalse() {
+        when(mContext.getSystemService(SafetyCenterManager.class)).thenReturn(null);
+
+        assertThat(SafetyCenterStatusHolder.get().isEnabled(mContext)).isFalse();
+    }
+}
diff --git a/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusTest.java b/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusTest.java
deleted file mode 100644
index f36fbe1..0000000
--- a/tests/unit/src/com/android/settings/safetycenter/SafetyCenterStatusTest.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * 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
index 907fb99..e9c451e 100644
--- a/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java
@@ -22,18 +22,16 @@
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 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;
@@ -51,11 +49,15 @@
     private Preference mPreference;
 
     @Mock
+    private SafetyCenterStatusHolder mSafetyCenterStatusHolder;
+
+    @Mock
     private Context mContext;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
+        SafetyCenterStatusHolder.sInstance = mSafetyCenterStatusHolder;
 
         mPreference = new Preference(ApplicationProvider.getApplicationContext());
         mPreference.setKey(PREFERENCE_KEY);
@@ -63,14 +65,6 @@
         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
@@ -110,11 +104,7 @@
 
     @Test
     public void getAvailabilityStatus_whenSafetyCenterDisabled_returnsUnavailable() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(false),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(false);
 
         assertThat(mTopLevelSafetyCenterEntryPreferenceController.getAvailabilityStatus())
                 .isEqualTo(TopLevelSafetyCenterEntryPreferenceController.CONDITIONALLY_UNAVAILABLE);
@@ -122,11 +112,7 @@
 
     @Test
     public void getAvailabilityStatus_whenSafetyCenterEnabled_returnsAvailable() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(true),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(true);
 
         assertThat(mTopLevelSafetyCenterEntryPreferenceController.getAvailabilityStatus())
                 .isEqualTo(TopLevelSafetyCenterEntryPreferenceController.AVAILABLE);
diff --git a/tests/unit/src/com/android/settings/security/SecurityAdvancedSettingsTest.java b/tests/unit/src/com/android/settings/security/SecurityAdvancedSettingsTest.java
index e18b258..ac91abb 100644
--- a/tests/unit/src/com/android/settings/security/SecurityAdvancedSettingsTest.java
+++ b/tests/unit/src/com/android/settings/security/SecurityAdvancedSettingsTest.java
@@ -18,17 +18,18 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
 import android.content.Context;
 import android.os.Looper;
-import android.provider.DeviceConfig;
 
 import androidx.test.annotation.UiThreadTest;
 import androidx.test.core.app.ApplicationProvider;
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.ResourcesUtils;
 import com.android.settingslib.drawer.CategoryKey;
@@ -36,6 +37,8 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 
 @RunWith(AndroidJUnit4.class)
 public class SecurityAdvancedSettingsTest {
@@ -47,16 +50,22 @@
     private Context mContext;
     private SecurityAdvancedSettings mSecurityAdvancedSettings;
 
+    @Mock
+    private SafetyCenterStatusHolder mSafetyCenterStatusHolder;
+
     @Before
     @UiThreadTest
     public void setUp() {
+        MockitoAnnotations.initMocks(this);
         if (Looper.myLooper() == null) {
             Looper.prepare();
         }
 
+        SafetyCenterStatusHolder.sInstance = mSafetyCenterStatusHolder;
         mContext = ApplicationProvider.getApplicationContext();
 
-        mSecurityAdvancedSettings = new SecurityAdvancedSettings();
+        mSecurityAdvancedSettings = spy(new SecurityAdvancedSettings());
+        when(mSecurityAdvancedSettings.getContext()).thenReturn(mContext);
     }
 
     @Test
@@ -67,7 +76,7 @@
 
     @Test
     public void getCategoryKey_whenSafetyCenterIsEnabled_returnsSecurity() {
-        setSafetyCenterEnabled(true);
+        when(mSafetyCenterStatusHolder.isEnabled(any())).thenReturn(true);
 
         assertThat(mSecurityAdvancedSettings.getCategoryKey())
                 .isEqualTo(CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS);
@@ -75,7 +84,7 @@
 
     @Test
     public void getCategoryKey_whenAlternativeFragmentPresented_returnsAlternative() {
-        setSafetyCenterEnabled(false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(false);
         setupAlternativeFragment(true, ALTERNATIVE_CATEGORY_KEY);
 
         assertThat(mSecurityAdvancedSettings.getCategoryKey())
@@ -84,7 +93,7 @@
 
     @Test
     public void getCategoryKey_whenNoAlternativeFragmentPresented_returnsLegacy() {
-        setSafetyCenterEnabled(false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(false);
         setupAlternativeFragment(false, null);
 
         assertThat(mSecurityAdvancedSettings.getCategoryKey())
@@ -95,14 +104,6 @@
         return ResourcesUtils.getResourcesId(mContext, "xml", resName);
     }
 
-    private void setSafetyCenterEnabled(boolean isEnabled) {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(isEnabled),
-                /* makeDefault = */ false);
-    }
-
     private void setupAlternativeFragment(boolean hasAlternativeFragment,
             String alternativeCategoryKey) {
         final FakeFeatureFactory fakeFeatureFactory = FakeFeatureFactory.setupForTest();
diff --git a/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java b/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java
index 0406616..3348663 100644
--- a/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java
+++ b/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java
@@ -25,22 +25,22 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import android.content.Context;
 import android.content.Intent;
-import android.provider.DeviceConfig;
 
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 import androidx.test.platform.app.InstrumentationRegistry;
 
 import com.android.settings.Settings;
 import com.android.settings.SettingsActivity;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 import com.android.settings.testutils.FakeFeatureFactory;
 
-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)
@@ -52,13 +52,16 @@
     private Settings.SecurityDashboardActivity mActivity;
     private Intent mDefaultIntent;
 
+    @Mock
+    private SafetyCenterStatusHolder mSafetyCenterStatusHolder;
+
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
         FakeFeatureFactory mFeatureFactory = FakeFeatureFactory.setupForTest();
         mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider();
-        DeviceConfig.resetToDefaults(android.provider.Settings.RESET_MODE_PACKAGE_DEFAULTS,
-                DeviceConfig.NAMESPACE_PRIVACY);
+        SafetyCenterStatusHolder.sInstance = mSafetyCenterStatusHolder;
+
         mDefaultIntent = new Intent();
         mDefaultIntent.setAction(android.provider.Settings.ACTION_SECURITY_SETTINGS);
         mDefaultIntent.setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(),
@@ -79,12 +82,6 @@
         doNothing().when(mActivity).startActivity(any(Intent.class));
     }
 
-    @After
-    public void tearDown() {
-        DeviceConfig.resetToDefaults(android.provider.Settings.RESET_MODE_PACKAGE_DEFAULTS,
-                DeviceConfig.NAMESPACE_PRIVACY);
-    }
-
     @Test
     public void noAvailableAlternativeFragmentAvailable_defaultFragmentSet() {
         when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
@@ -125,11 +122,7 @@
 
     @Test
     public void onCreate_whenSafetyCenterEnabled_redirectsToSafetyCenter() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(true),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(true);
         final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
 
         mActivity.handleSafetyCenterRedirection();
@@ -140,11 +133,7 @@
 
     @Test
     public void onCreate_whenSafetyCenterDisabled_doesntRedirectToSafetyCenter() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(false),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(false);
 
         mActivity.handleSafetyCenterRedirection();
 
diff --git a/tests/unit/src/com/android/settings/security/TopLevelSecurityEntryPreferenceControllerTest.java b/tests/unit/src/com/android/settings/security/TopLevelSecurityEntryPreferenceControllerTest.java
index 7e83ca7..81ebda6 100644
--- a/tests/unit/src/com/android/settings/security/TopLevelSecurityEntryPreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/security/TopLevelSecurityEntryPreferenceControllerTest.java
@@ -25,18 +25,15 @@
 
 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 com.android.settings.SettingsActivity;
-import com.android.settings.safetycenter.SafetyCenterStatus;
+import com.android.settings.safetycenter.SafetyCenterStatusHolder;
 import com.android.settings.testutils.FakeFeatureFactory;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -57,15 +54,15 @@
 
     @Mock
     private Context mContext;
+    @Mock
+    private SafetyCenterStatusHolder mSafetyCenterStatusHolder;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
         mFeatureFactory = FakeFeatureFactory.setupForTest();
         mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider();
-
-        DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS,
-                DeviceConfig.NAMESPACE_PRIVACY);
+        SafetyCenterStatusHolder.sInstance = mSafetyCenterStatusHolder;
 
         mPreference = new Preference(ApplicationProvider.getApplicationContext());
         mPreference.setKey(PREFERENCE_KEY);
@@ -75,12 +72,6 @@
                 new TopLevelSecurityEntryPreferenceController(mContext, PREFERENCE_KEY);
     }
 
-    @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());
@@ -137,11 +128,7 @@
 
     @Test
     public void getAvailabilityStatus_whenSafetyCenterEnabled_returnsUnavailable() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(true),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(true);
 
         assertThat(mTopLevelSecurityEntryPreferenceController.getAvailabilityStatus())
                 .isEqualTo(TopLevelSecurityEntryPreferenceController.CONDITIONALLY_UNAVAILABLE);
@@ -149,11 +136,7 @@
 
     @Test
     public void getAvailabilityStatus_whenSafetyCenterDisabled_returnsAvailable() {
-        DeviceConfig.setProperty(
-                DeviceConfig.NAMESPACE_PRIVACY,
-                SafetyCenterStatus.SAFETY_CENTER_IS_ENABLED,
-                /* value = */ Boolean.toString(false),
-                /* makeDefault = */ false);
+        when(mSafetyCenterStatusHolder.isEnabled(any(Context.class))).thenReturn(false);
 
         assertThat(mTopLevelSecurityEntryPreferenceController.getAvailabilityStatus())
                 .isEqualTo(TopLevelSecurityEntryPreferenceController.AVAILABLE);