Modified the PrivacySettings and add PreferenceControllers

- Changed the PrivacySettings to DashboardFragment.
- Added the new five related PreferenceControllers and related codes
- Modified the privacy_settings.xml
- Removed the PrivacySettingsTest test by design changed
- Moved the PrivacySettings.java from root directory to backup

Test: manual
Test: make RunSettingsRoboTests
Change-Id: Ic88e2e58a11d024d2394f75c3db5b46fe2d86dba
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index e87951b..8d1307f 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -1390,7 +1390,7 @@
                 <category android:name="android.intent.category.VOICE_LAUNCH" />
             </intent-filter>
             <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
-                android:value="com.android.settings.PrivacySettings" />
+                android:value="com.android.settings.backup.PrivacySettings" />
             <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
                 android:value="true" />
         </activity>
diff --git a/res/xml/privacy_settings.xml b/res/xml/privacy_settings.xml
index a25933b..8754a1f 100644
--- a/res/xml/privacy_settings.xml
+++ b/res/xml/privacy_settings.xml
@@ -14,8 +14,10 @@
      limitations under the License.
 -->
 
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+<PreferenceScreen
+    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:settings="http://schemas.android.com/apk/res-auto"
+    android:key="privacy_settings_screen"
     android:title="@string/privacy_settings_title">
 
     <!-- Backup settings -->
@@ -23,31 +25,36 @@
         android:key="backup_data"
         android:title="@string/backup_data_title"
         android:persistent="false"
-        android:fragment="com.android.settings.backup.ToggleBackupSettingFragment"/>
+        android:fragment="com.android.settings.backup.ToggleBackupSettingFragment"
+        settings:controller="com.android.settings.backup.BackupDataPreferenceController" />
 
     <Preference
         android:key="configure_account"
         android:title="@string/backup_configure_account_title"
-        android:persistent="false">
+        android:persistent="false"
+        settings:controller="com.android.settings.backup.ConfigureAccountPreferenceController">
         <!-- the Intent declared here is always overwritten by a real one -->
         <intent android:action="dummy" />
     </Preference>
 
     <Preference
         android:key="data_management"
-        android:persistent="false">
+        android:persistent="false"
+        settings:controller="com.android.settings.backup.DataManagementPreferenceController">
     </Preference>
 
     <SwitchPreference
         android:key="auto_restore"
         android:title="@string/auto_restore_title"
-        android:summary="@string/auto_restore_summary" />
+        android:summary="@string/auto_restore_summary"
+        settings:controller="com.android.settings.backup.AutoRestorePreferenceController" />
 
     <Preference
         android:key="backup_inactive"
         android:title="@string/backup_inactive_title"
         android:persistent="false"
         android:enabled="false"
-        android:selectable="false" />
+        android:selectable="false"
+        settings:controller="com.android.settings.backup.BackupInactivePreferenceController" />
 
 </PreferenceScreen>
diff --git a/src/com/android/settings/PrivacySettings.java b/src/com/android/settings/PrivacySettings.java
deleted file mode 100644
index 24774e0..0000000
--- a/src/com/android/settings/PrivacySettings.java
+++ /dev/null
@@ -1,248 +0,0 @@
-/*
- * Copyright (C) 2009 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;
-
-import android.app.backup.IBackupManager;
-import android.content.ContentResolver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
-import android.os.Bundle;
-import android.os.RemoteException;
-import android.os.ServiceManager;
-import android.os.UserHandle;
-import android.os.UserManager;
-import android.provider.SearchIndexableResource;
-import android.provider.Settings;
-import android.support.annotation.VisibleForTesting;
-import android.support.v14.preference.SwitchPreference;
-import android.support.v7.preference.Preference;
-import android.support.v7.preference.Preference.OnPreferenceChangeListener;
-import android.support.v7.preference.PreferenceScreen;
-import android.util.Log;
-
-import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
-import com.android.settings.search.BaseSearchIndexProvider;
-import com.android.settings.search.Indexable;
-import com.android.settingslib.RestrictedLockUtils;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-/**
- * Gesture lock pattern settings.
- */
-public class PrivacySettings extends SettingsPreferenceFragment {
-
-    // Vendor specific
-    private static final String GSETTINGS_PROVIDER = "com.google.settings";
-    @VisibleForTesting
-    static final String BACKUP_DATA = "backup_data";
-    @VisibleForTesting
-    static final String AUTO_RESTORE = "auto_restore";
-    @VisibleForTesting
-    static final String CONFIGURE_ACCOUNT = "configure_account";
-    @VisibleForTesting
-    static final String DATA_MANAGEMENT = "data_management";
-    private static final String BACKUP_INACTIVE = "backup_inactive";
-    private static final String TAG = "PrivacySettings";
-    private IBackupManager mBackupManager;
-    private Preference mBackup;
-    private SwitchPreference mAutoRestore;
-    private Preference mConfigure;
-    private Preference mManageData;
-    private boolean mEnabled;
-
-    @Override
-    public int getMetricsCategory() {
-        return MetricsEvent.PRIVACY;
-    }
-
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        // Don't allow any access if this is not an admin user.
-        // TODO: backup/restore currently only works with owner user b/22760572
-        mEnabled = UserManager.get(getActivity()).isAdminUser();
-        if (!mEnabled) {
-            return;
-        }
-
-        addPreferencesFromResource(R.xml.privacy_settings);
-        final PreferenceScreen screen = getPreferenceScreen();
-        mBackupManager = IBackupManager.Stub.asInterface(
-                ServiceManager.getService(Context.BACKUP_SERVICE));
-
-        setPreferenceReferences(screen);
-
-        Set<String> keysToRemove = new HashSet<>();
-        getNonVisibleKeys(getActivity(), keysToRemove);
-        final int screenPreferenceCount = screen.getPreferenceCount();
-        for (int i = screenPreferenceCount - 1; i >= 0; --i) {
-            Preference preference = screen.getPreference(i);
-            if (keysToRemove.contains(preference.getKey())) {
-                screen.removePreference(preference);
-            }
-        }
-
-        updateToggles();
-    }
-
-    @Override
-    public void onResume() {
-        super.onResume();
-
-        // Refresh UI
-        if (mEnabled) {
-            updateToggles();
-        }
-    }
-
-    @VisibleForTesting
-    void setPreferenceReferences(PreferenceScreen screen) {
-        mBackup = screen.findPreference(BACKUP_DATA);
-
-        mAutoRestore = (SwitchPreference) screen.findPreference(AUTO_RESTORE);
-        mAutoRestore.setOnPreferenceChangeListener(preferenceChangeListener);
-
-        mConfigure = screen.findPreference(CONFIGURE_ACCOUNT);
-        mManageData = screen.findPreference(DATA_MANAGEMENT);
-    }
-
-    private OnPreferenceChangeListener preferenceChangeListener = new OnPreferenceChangeListener() {
-        @Override
-        public boolean onPreferenceChange(Preference preference, Object newValue) {
-            if (!(preference instanceof SwitchPreference)) {
-                return true;
-            }
-            boolean nextValue = (Boolean) newValue;
-            boolean result = false;
-            if (preference == mAutoRestore) {
-                try {
-                    mBackupManager.setAutoRestore(nextValue);
-                    result = true;
-                } catch (RemoteException e) {
-                    mAutoRestore.setChecked(!nextValue);
-                }
-            }
-            return result;
-        }
-    };
-
-
-    /*
-     * Creates toggles for each backup/reset preference.
-     */
-    private void updateToggles() {
-        ContentResolver res = getContentResolver();
-
-        boolean backupEnabled = false;
-        Intent configIntent = null;
-        String configSummary = null;
-        Intent manageIntent = null;
-        String manageLabel = null;
-        try {
-            backupEnabled = mBackupManager.isBackupEnabled();
-            String transport = mBackupManager.getCurrentTransport();
-            configIntent = validatedActivityIntent(
-                    mBackupManager.getConfigurationIntent(transport), "config");
-            configSummary = mBackupManager.getDestinationString(transport);
-            manageIntent = validatedActivityIntent(
-                    mBackupManager.getDataManagementIntent(transport), "management");
-            manageLabel = mBackupManager.getDataManagementLabel(transport);
-
-            mBackup.setSummary(backupEnabled
-                    ? R.string.accessibility_feature_state_on
-                    : R.string.accessibility_feature_state_off);
-        } catch (RemoteException e) {
-            // leave it 'false' and disable the UI; there's no backup manager
-            mBackup.setEnabled(false);
-        }
-
-        mAutoRestore.setChecked(Settings.Secure.getInt(res,
-                Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1);
-        mAutoRestore.setEnabled(backupEnabled);
-
-        final boolean configureEnabled = (configIntent != null) && backupEnabled;
-        mConfigure.setEnabled(configureEnabled);
-        mConfigure.setIntent(configIntent);
-        setConfigureSummary(configSummary);
-
-        final boolean manageEnabled = (manageIntent != null) && backupEnabled;
-        if (manageEnabled) {
-            mManageData.setIntent(manageIntent);
-            if (manageLabel != null) {
-                mManageData.setTitle(manageLabel);
-            }
-        } else {
-            // Hide the item if data management intent is not supported by transport.
-            getPreferenceScreen().removePreference(mManageData);
-        }
-    }
-
-    private Intent validatedActivityIntent(Intent intent, String logLabel) {
-        if (intent != null) {
-            PackageManager pm = getPackageManager();
-            List<ResolveInfo> resolved = pm.queryIntentActivities(intent, 0);
-            if (resolved == null || resolved.isEmpty()) {
-                intent = null;
-                Log.e(TAG, "Backup " + logLabel + " intent " + intent
-                        + " fails to resolve; ignoring");
-            }
-        }
-        return intent;
-    }
-
-    private void setConfigureSummary(String summary) {
-        if (summary != null) {
-            mConfigure.setSummary(summary);
-        } else {
-            mConfigure.setSummary(R.string.backup_configure_account_default_summary);
-        }
-    }
-
-    @Override
-    public int getHelpResource() {
-        return R.string.help_url_backup_reset;
-    }
-
-    private static void getNonVisibleKeys(Context context, Collection<String> nonVisibleKeys) {
-        final IBackupManager backupManager = IBackupManager.Stub.asInterface(
-                ServiceManager.getService(Context.BACKUP_SERVICE));
-        boolean isServiceActive = false;
-        try {
-            isServiceActive = backupManager.isBackupServiceActive(UserHandle.myUserId());
-        } catch (RemoteException e) {
-            Log.w(TAG, "Failed querying backup manager service activity status. " +
-                    "Assuming it is inactive.");
-        }
-        boolean vendorSpecific = context.getPackageManager().
-                resolveContentProvider(GSETTINGS_PROVIDER, 0) == null;
-        if (vendorSpecific || isServiceActive) {
-            nonVisibleKeys.add(BACKUP_INACTIVE);
-        }
-        if (vendorSpecific || !isServiceActive) {
-            nonVisibleKeys.add(BACKUP_DATA);
-            nonVisibleKeys.add(AUTO_RESTORE);
-            nonVisibleKeys.add(CONFIGURE_ACCOUNT);
-        }
-    }
-}
diff --git a/src/com/android/settings/backup/AutoRestorePreferenceController.java b/src/com/android/settings/backup/AutoRestorePreferenceController.java
new file mode 100644
index 0000000..7bcf350
--- /dev/null
+++ b/src/com/android/settings/backup/AutoRestorePreferenceController.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import android.app.backup.IBackupManager;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.provider.Settings;
+import android.support.v14.preference.SwitchPreference;
+import android.support.v7.preference.Preference;
+import android.util.Log;
+
+import com.android.settings.core.TogglePreferenceController;
+
+public class AutoRestorePreferenceController extends TogglePreferenceController {
+    private static final String TAG = "AutoRestorePrefCtrler";
+
+    private PrivacySettingsConfigData mPSCD;
+    private Preference mPreference;
+
+    public AutoRestorePreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    public void setPrivacySettingsConfigData(final PrivacySettingsConfigData pData) {
+        mPSCD = pData;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (!PrivacySettingsUtils.isAdminUser(mContext)) {
+            return DISABLED_FOR_USER;
+        }
+        if (PrivacySettingsUtils.isInvisibleKey(mContext, PrivacySettingsUtils.AUTO_RESTORE)) {
+            return DISABLED_UNSUPPORTED;
+        }
+        return AVAILABLE;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        super.updateState(preference);
+        mPreference = preference;
+        preference.setEnabled(mPSCD.isBackupEnabled());
+    }
+
+    @Override
+    public boolean isChecked() {
+        final ContentResolver res = mContext.getContentResolver();
+
+        return Settings.Secure.getInt(res,
+                Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1;
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        final boolean nextValue = isChecked;
+        boolean result = false;
+
+        final IBackupManager backupManager = IBackupManager.Stub.asInterface(
+                ServiceManager.getService(Context.BACKUP_SERVICE));
+
+        try {
+            backupManager.setAutoRestore(nextValue);
+            result = true;
+        } catch (RemoteException e) {
+            ((SwitchPreference) mPreference).setChecked(!nextValue);
+            Log.e(TAG, "Error can't set setAutoRestore", e);
+        }
+
+        return result;
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/backup/BackupDataPreferenceController.java b/src/com/android/settings/backup/BackupDataPreferenceController.java
new file mode 100644
index 0000000..0a0f581
--- /dev/null
+++ b/src/com/android/settings/backup/BackupDataPreferenceController.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import android.content.Context;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.R;
+
+public class BackupDataPreferenceController extends BasePreferenceController {
+    private PrivacySettingsConfigData mPSCD;
+
+    public BackupDataPreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    public void setPrivacySettingsConfigData(final PrivacySettingsConfigData pData) {
+        mPSCD = pData;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (!PrivacySettingsUtils.isAdminUser(mContext)) {
+            return DISABLED_FOR_USER;
+        }
+        if (PrivacySettingsUtils.isInvisibleKey(mContext, PrivacySettingsUtils.BACKUP_DATA)) {
+            return DISABLED_UNSUPPORTED;
+        }
+        return AVAILABLE;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        super.updateState(preference);
+        if (mPSCD.isBackupGray()) {
+            preference.setEnabled(false);
+        }
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        if (!mPSCD.isBackupGray()) {
+            return mPSCD.isBackupEnabled()
+                    ? mContext.getText(R.string.accessibility_feature_state_on)
+                    : mContext.getText(R.string.accessibility_feature_state_off);
+        }
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/backup/BackupInactivePreferenceController.java b/src/com/android/settings/backup/BackupInactivePreferenceController.java
new file mode 100644
index 0000000..d44801e
--- /dev/null
+++ b/src/com/android/settings/backup/BackupInactivePreferenceController.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import android.content.Context;
+
+import com.android.settings.core.BasePreferenceController;
+
+public class BackupInactivePreferenceController extends BasePreferenceController {
+
+    public BackupInactivePreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (!PrivacySettingsUtils.isAdminUser(mContext)) {
+            return DISABLED_FOR_USER;
+        }
+        if (PrivacySettingsUtils.isInvisibleKey(mContext, PrivacySettingsUtils.BACKUP_INACTIVE)) {
+            return DISABLED_UNSUPPORTED;
+        }
+        return AVAILABLE;
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/backup/ConfigureAccountPreferenceController.java b/src/com/android/settings/backup/ConfigureAccountPreferenceController.java
new file mode 100644
index 0000000..f1bcee9
--- /dev/null
+++ b/src/com/android/settings/backup/ConfigureAccountPreferenceController.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import android.content.Context;
+import android.content.Intent;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.R;
+
+public class ConfigureAccountPreferenceController extends BasePreferenceController {
+    private PrivacySettingsConfigData mPSCD;
+
+    public ConfigureAccountPreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    public void setPrivacySettingsConfigData(final PrivacySettingsConfigData pData) {
+        mPSCD = pData;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (!PrivacySettingsUtils.isAdminUser(mContext)) {
+            return DISABLED_FOR_USER;
+        }
+        if (PrivacySettingsUtils.isInvisibleKey(mContext, PrivacySettingsUtils.CONFIGURE_ACCOUNT)) {
+            return DISABLED_UNSUPPORTED;
+        }
+        return AVAILABLE;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        super.updateState(preference);
+        final Intent configIntent = mPSCD.getConfigIntent();
+        final boolean configureEnabled = (configIntent != null) && mPSCD.isBackupEnabled();
+        preference.setEnabled(configureEnabled);
+        preference.setIntent(configIntent);
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        final String configSummary = mPSCD.getConfigSummary();
+        return configSummary != null
+                ? configSummary
+                : mContext.getText(R.string.backup_configure_account_default_summary);
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/backup/DataManagementPreferenceController.java b/src/com/android/settings/backup/DataManagementPreferenceController.java
new file mode 100644
index 0000000..de987ab
--- /dev/null
+++ b/src/com/android/settings/backup/DataManagementPreferenceController.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import android.content.Context;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.core.BasePreferenceController;
+
+public class DataManagementPreferenceController extends BasePreferenceController {
+    private PrivacySettingsConfigData mPSCD;
+    private boolean mManageEnabled;
+
+    public DataManagementPreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    public void setPrivacySettingsConfigData(final PrivacySettingsConfigData pData) {
+        mPSCD = pData;
+        mManageEnabled = (mPSCD.getManageIntent() != null) && mPSCD.isBackupEnabled();
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (!PrivacySettingsUtils.isAdminUser(mContext)) {
+            return DISABLED_FOR_USER;
+        }
+        if (!mManageEnabled) {
+            return DISABLED_UNSUPPORTED;
+        }
+        return AVAILABLE;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        if (mManageEnabled) {
+            preference.setIntent(mPSCD.getManageIntent());
+            final String manageLabel = mPSCD.getManageLabel();
+            if (manageLabel != null) {
+                preference.setTitle(manageLabel);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/backup/PrivacySettings.java b/src/com/android/settings/backup/PrivacySettings.java
new file mode 100644
index 0000000..9b342e7
--- /dev/null
+++ b/src/com/android/settings/backup/PrivacySettings.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2009 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.backup;
+
+import android.content.Context;
+import android.provider.SearchIndexableResource;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settingslib.search.SearchIndexable;
+
+import java.util.Arrays;
+import java.util.List;
+
+@SearchIndexable
+public class PrivacySettings extends DashboardFragment {
+    private static final String TAG = "PrivacySettings";
+
+    @Override
+    public int getMetricsCategory() {
+        return MetricsEvent.PRIVACY;
+    }
+
+    @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.privacy_settings;
+    }
+
+    @Override
+    public int getHelpResource() {
+        return R.string.help_url_backup_reset;
+    }
+
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        updatePrivacySettingsConfigData(context);
+    }
+
+    @Override
+    protected void updatePreferenceStates() {
+        updatePrivacySettingsConfigData(getContext());
+        super.updatePreferenceStates();
+    }
+
+    private void updatePrivacySettingsConfigData(final Context context) {
+        final PrivacySettingsConfigData pData = new PrivacySettingsConfigData();
+        if (PrivacySettingsUtils.isAdminUser(context)) {
+            PrivacySettingsUtils.updatePrivacyBuffer(context, pData);
+        }
+
+        use(BackupDataPreferenceController.class).setPrivacySettingsConfigData(pData);
+        use(ConfigureAccountPreferenceController.class).setPrivacySettingsConfigData(pData);
+        use(DataManagementPreferenceController.class).setPrivacySettingsConfigData(pData);
+        use(AutoRestorePreferenceController.class).setPrivacySettingsConfigData(pData);
+    }
+
+    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider() {
+                @Override
+                public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
+                        boolean enabled) {
+                    final SearchIndexableResource sir = new SearchIndexableResource(context);
+                    sir.xmlResId = R.xml.privacy_settings;
+                    return Arrays.asList(sir);
+                }
+
+                @Override
+                protected boolean isPageSearchEnabled(Context context) {
+                    final BackupSettingsHelper backupHelper = new BackupSettingsHelper(context);
+                    return !backupHelper.isBackupProvidedByManufacturer() &&
+                            !backupHelper.isIntentProvidedByTransport();
+                }
+            };
+}
diff --git a/src/com/android/settings/backup/PrivacySettingsConfigData.java b/src/com/android/settings/backup/PrivacySettingsConfigData.java
new file mode 100644
index 0000000..9b72a82
--- /dev/null
+++ b/src/com/android/settings/backup/PrivacySettingsConfigData.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import android.content.Intent;
+
+public class PrivacySettingsConfigData {
+    private boolean mBackupEnabled;
+    private boolean mBackupGray;
+    private Intent mConfigIntent;
+    private String mConfigSummary;
+    private Intent mManageIntent;
+    private String mManageLabel;
+
+    public PrivacySettingsConfigData() {
+        mBackupEnabled = false;
+        mBackupGray = false;
+        mConfigIntent = null;
+        mConfigSummary = null;
+        mManageIntent = null;
+        mManageLabel = null;
+    }
+
+    public boolean isBackupEnabled() {
+        return mBackupEnabled;
+    }
+
+    public void setBackupEnabled(final boolean backupEnabled) {
+        mBackupEnabled = backupEnabled;
+    }
+
+    public boolean isBackupGray() {
+        return mBackupGray;
+    }
+
+    public void setBackupGray(final boolean backupGray) {
+        mBackupGray = backupGray;
+    }
+
+    public Intent getConfigIntent() {
+        return mConfigIntent;
+    }
+
+    public void setConfigIntent(final Intent configIntent) {
+        mConfigIntent = configIntent;
+    }
+
+    public String getConfigSummary() {
+        return mConfigSummary;
+    }
+
+    public void setConfigSummary(final String configSummary) {
+        mConfigSummary = configSummary;
+    }
+
+    public Intent getManageIntent() {
+        return mManageIntent;
+    }
+
+    public void setManageIntent(final Intent manageIntent) {
+        mManageIntent = manageIntent;
+    }
+
+    public String getManageLabel() {
+        return mManageLabel;
+    }
+
+    public void setManageLabel(final String manageLabel) {
+        mManageLabel = manageLabel;
+    }
+}
diff --git a/src/com/android/settings/backup/PrivacySettingsUtils.java b/src/com/android/settings/backup/PrivacySettingsUtils.java
new file mode 100644
index 0000000..f8f21dd
--- /dev/null
+++ b/src/com/android/settings/backup/PrivacySettingsUtils.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import android.app.backup.IBackupManager;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.util.Log;
+
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+public class PrivacySettingsUtils {
+    private static final String TAG = "PrivacySettingsUtils";
+    private static final String GSETTINGS_PROVIDER = "com.google.settings";
+
+    static final String BACKUP_DATA = "backup_data";
+    static final String AUTO_RESTORE = "auto_restore";
+    static final String CONFIGURE_ACCOUNT = "configure_account";
+    static final String BACKUP_INACTIVE = "backup_inactive";
+
+    // Don't allow any access if this is not an admin user.
+    // TODO: backup/restore currently only works with owner user b/22760572
+    static boolean isAdminUser(final Context context) {
+        return UserManager.get(context).isAdminUser();
+    }
+
+    /**
+     * Send a {@param key} to check its preference will display in PrivacySettings or not.
+     */
+    static boolean isInvisibleKey(final Context context, final String key) {
+        final Set<String> keysToRemove = getInvisibleKey(context);
+        if (Log.isLoggable(TAG, Log.DEBUG)) {
+            Log.d(TAG,
+                    "keysToRemove size=" + keysToRemove.size() + " keysToRemove=" + keysToRemove);
+        }
+        if (keysToRemove.contains(key)) {
+            return true;
+        }
+        return false;
+    }
+
+    private static Set<String> getInvisibleKey(final Context context) {
+        final IBackupManager backupManager = IBackupManager.Stub.asInterface(
+                ServiceManager.getService(Context.BACKUP_SERVICE));
+        boolean isServiceActive = false;
+        try {
+            isServiceActive = backupManager.isBackupServiceActive(UserHandle.myUserId());
+        } catch (RemoteException e) {
+            Log.w(TAG, "Failed querying backup manager service activity status. " +
+                    "Assuming it is inactive.");
+        }
+        boolean vendorSpecific = context.getPackageManager().
+                resolveContentProvider(GSETTINGS_PROVIDER, 0) == null;
+        final Set<String> inVisibleKeys = new TreeSet<>();
+        if (vendorSpecific || isServiceActive) {
+            inVisibleKeys.add(BACKUP_INACTIVE);
+        }
+        if (vendorSpecific || !isServiceActive) {
+            inVisibleKeys.add(BACKUP_DATA);
+            inVisibleKeys.add(AUTO_RESTORE);
+            inVisibleKeys.add(CONFIGURE_ACCOUNT);
+        }
+        return inVisibleKeys;
+    }
+
+    public static void updatePrivacyBuffer(final Context context, PrivacySettingsConfigData data) {
+        final IBackupManager backupManager = IBackupManager.Stub.asInterface(
+                ServiceManager.getService(Context.BACKUP_SERVICE));
+
+        try {
+            data.setBackupEnabled(backupManager.isBackupEnabled());
+            String transport = backupManager.getCurrentTransport();
+            data.setConfigIntent(validatedActivityIntent(context,
+                    backupManager.getConfigurationIntent(transport), "config"));
+            data.setConfigSummary(backupManager.getDestinationString(transport));
+            data.setManageIntent(validatedActivityIntent(context,
+                    backupManager.getDataManagementIntent(transport), "management"));
+            data.setManageLabel(backupManager.getDataManagementLabel(transport));
+            data.setBackupGray(false);
+        } catch (RemoteException e) {
+            // leave it 'false' and disable the UI; there's no backup manager
+            //  mBackup.setEnabled(false);
+            data.setBackupGray(true);
+        }
+    }
+
+    private static Intent validatedActivityIntent(final Context context, Intent intent,
+            String logLabel) {
+        if (intent != null) {
+            PackageManager pm = context.getPackageManager();
+            List<ResolveInfo> resolved = pm.queryIntentActivities(intent, 0);
+            if (resolved == null || resolved.isEmpty()) {
+                intent = null;
+                Log.e(TAG, "Backup " + logLabel + " intent " + intent
+                        + " fails to resolve; ignoring");
+            }
+        }
+        return intent;
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index aa9d7b5..be9b722 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -21,7 +21,6 @@
 import com.android.settings.DisplaySettings;
 import com.android.settings.IccLockSettings;
 import com.android.settings.MasterClear;
-import com.android.settings.PrivacySettings;
 import com.android.settings.Settings;
 import com.android.settings.TestingSettings;
 import com.android.settings.TetherSettings;
@@ -51,6 +50,7 @@
 import com.android.settings.applications.appops.BackgroundCheckSummary;
 import com.android.settings.applications.assist.ManageAssist;
 import com.android.settings.applications.manageapplications.ManageApplications;
+import com.android.settings.backup.PrivacySettings;
 import com.android.settings.backup.ToggleBackupSettingFragment;
 import com.android.settings.bluetooth.BluetoothDeviceDetailsFragment;
 import com.android.settings.connecteddevice.AdvancedConnectedDeviceDashboardFragment;
diff --git a/tests/robotests/assets/grandfather_not_implementing_indexable b/tests/robotests/assets/grandfather_not_implementing_indexable
index 045841a..ad56a12 100644
--- a/tests/robotests/assets/grandfather_not_implementing_indexable
+++ b/tests/robotests/assets/grandfather_not_implementing_indexable
@@ -56,7 +56,6 @@
 com.android.settings.applications.appinfo.PictureInPictureSettings
 com.android.settings.applications.appinfo.PictureInPictureDetails
 com.android.settings.network.ApnSettings
-com.android.settings.PrivacySettings
 com.android.settings.wifi.calling.WifiCallingSettingsForSub
 com.android.settings.password.SetupChooseLockGeneric$SetupChooseLockGenericFragment
 com.android.settings.SetupRedactionInterstitial$SetupRedactionInterstitialFragment
diff --git a/tests/robotests/src/com/android/settings/PrivacySettingsTest.java b/tests/robotests/src/com/android/settings/PrivacySettingsTest.java
deleted file mode 100644
index 24defa1..0000000
--- a/tests/robotests/src/com/android/settings/PrivacySettingsTest.java
+++ /dev/null
@@ -1,59 +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;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import android.support.v14.preference.SwitchPreference;
-import android.support.v7.preference.Preference;
-import android.support.v7.preference.PreferenceScreen;
-
-import com.android.settings.testutils.SettingsRobolectricTestRunner;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-
-@RunWith(SettingsRobolectricTestRunner.class)
-public class PrivacySettingsTest {
-
-    @Mock
-    private PreferenceScreen mScreen;
-    private PrivacySettings mSettings;
-
-    @Before
-    public void setUp() {
-        MockitoAnnotations.initMocks(this);
-        when(mScreen.findPreference(PrivacySettings.BACKUP_DATA))
-                .thenReturn(mock(Preference.class));
-        when(mScreen.findPreference(PrivacySettings.CONFIGURE_ACCOUNT))
-                .thenReturn(mock(Preference.class));
-        when(mScreen.findPreference(PrivacySettings.DATA_MANAGEMENT))
-                .thenReturn(mock(Preference.class));
-        when(mScreen.findPreference(PrivacySettings.AUTO_RESTORE))
-                .thenReturn(mock(SwitchPreference.class));
-        mSettings = new PrivacySettings();
-    }
-
-    @Test
-    public void testSetPreference_noCrash() {
-        mSettings.setPreferenceReferences(mScreen);
-    }
-}
diff --git a/tests/robotests/src/com/android/settings/backup/AutoRestorePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/backup/AutoRestorePreferenceControllerTest.java
new file mode 100644
index 0000000..0fc0826
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/backup/AutoRestorePreferenceControllerTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.support.v14.preference.SwitchPreference;
+
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {ShadowPrivacySettingsUtils.class})
+public class AutoRestorePreferenceControllerTest {
+    private Context mContext;
+    private AutoRestorePreferenceController mController;
+    private PrivacySettingsConfigData mPSCD;
+    private SwitchPreference mPreference;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = RuntimeEnvironment.application;
+        mPSCD = new PrivacySettingsConfigData();
+        mController = new AutoRestorePreferenceController(mContext,
+                PrivacySettingsUtils.AUTO_RESTORE);
+        mPreference = new SwitchPreference(mContext);
+    }
+
+    @After
+    public void tearDown() {
+        ShadowPrivacySettingsUtils.reset();
+    }
+
+    @Test
+    public void updateState_backupEnabled_prefShouldBeEnabled() {
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        mController.updateState(mPreference);
+        assertThat(mPreference.isEnabled()).isTrue();
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_isnotInvisibleKey_shouldBeAvailable() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        ShadowPrivacySettingsUtils.setIsInvisibleKey(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_isInvisibleKey_shouldBeDisabledUnsupported() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        ShadowPrivacySettingsUtils.setIsInvisibleKey(true);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/backup/BackupDataPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/backup/BackupDataPreferenceControllerTest.java
new file mode 100644
index 0000000..fa25473
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/backup/BackupDataPreferenceControllerTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.os.UserManager;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {ShadowPrivacySettingsUtils.class})
+public class BackupDataPreferenceControllerTest {
+    private Context mContext;
+    private BackupDataPreferenceController mController;
+    private PrivacySettingsConfigData mPSCD;
+    private Preference mPreference;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = RuntimeEnvironment.application;
+        mPSCD = new PrivacySettingsConfigData();
+        mController = new BackupDataPreferenceController(mContext,
+                PrivacySettingsUtils.BACKUP_DATA);
+        mPreference = new Preference(mContext);
+    }
+
+    @After
+    public void tearDown() {
+        ShadowPrivacySettingsUtils.reset();
+    }
+
+    @Test
+    public void updateState_backupEnabled_prefShouldBeEnabled() {
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        mController.updateState(mPreference);
+        assertThat(mPreference.isEnabled()).isTrue();
+    }
+
+    @Test
+    public void updateState_backupEnabled_prefShouldDisplayOnSummary() {
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        mController.updateState(mPreference);
+        assertThat(mPreference.getSummary())
+                .isEqualTo(mContext.getString(R.string.accessibility_feature_state_on));
+    }
+
+    @Test
+    public void updateState_backupDisabled_prefShouldDisplayOffSummary() {
+        mPSCD.setBackupEnabled(false);
+        mPSCD.setBackupGray(false);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        mController.updateState(mPreference);
+        assertThat(mPreference.getSummary())
+                .isEqualTo(mContext.getString(R.string.accessibility_feature_state_off));
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_isnotInvisibleKey_shouldBeAvailable() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        ShadowPrivacySettingsUtils.setIsInvisibleKey(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_isInvisibleKey_shouldBeDisabledUnsupported() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        ShadowPrivacySettingsUtils.setIsInvisibleKey(true);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/backup/BackupInactivePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/backup/BackupInactivePreferenceControllerTest.java
new file mode 100644
index 0000000..c17cc67
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/backup/BackupInactivePreferenceControllerTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {ShadowPrivacySettingsUtils.class})
+public class BackupInactivePreferenceControllerTest {
+    private Context mContext;
+    private BackupInactivePreferenceController mController;
+    private Preference mPreference;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = RuntimeEnvironment.application;
+        mController = new BackupInactivePreferenceController(mContext,
+                PrivacySettingsUtils.BACKUP_INACTIVE);
+    }
+
+    @After
+    public void tearDown() {
+        ShadowPrivacySettingsUtils.reset();
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_isnotInvisibleKey_shouldBeAvailable() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        ShadowPrivacySettingsUtils.setIsInvisibleKey(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_isInvisibleKey_shouldBeDisabledUnsupported() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        ShadowPrivacySettingsUtils.setIsInvisibleKey(true);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/backup/ConfigureAccountPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/backup/ConfigureAccountPreferenceControllerTest.java
new file mode 100644
index 0000000..57cb4d5
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/backup/ConfigureAccountPreferenceControllerTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.content.Intent;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+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;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {ShadowPrivacySettingsUtils.class})
+public class ConfigureAccountPreferenceControllerTest {
+    private Context mContext;
+    private ConfigureAccountPreferenceController mController;
+    private PrivacySettingsConfigData mPSCD;
+    private Preference mPreference;
+    private String mTestSummary;
+
+    @Mock
+    private Intent mIntent;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = RuntimeEnvironment.application;
+        mPSCD = new PrivacySettingsConfigData();
+        mController = new ConfigureAccountPreferenceController(mContext,
+                PrivacySettingsUtils.CONFIGURE_ACCOUNT);
+        mPreference = new Preference(mContext);
+        mTestSummary = "Summary";
+    }
+
+    @After
+    public void tearDown() {
+        ShadowPrivacySettingsUtils.reset();
+    }
+
+    @Test
+    public void updateState_backupEnabled_hadConfigIntent_prefShouldBeEnabled() {
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mPSCD.setConfigIntent(mIntent);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        mController.updateState(mPreference);
+        assertThat(mPreference.isEnabled()).isTrue();
+    }
+
+    @Test
+    public void
+    updateState_backupEnabled_hadConfigIntent_nullConfigSummary_prefDisplayDefaultSummary() {
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mPSCD.setConfigIntent(mIntent);
+        mPSCD.setConfigSummary(null);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        mController.updateState(mPreference);
+        assertThat(mPreference.getSummary())
+                .isEqualTo(mContext.getString(R.string.backup_configure_account_default_summary));
+    }
+
+    @Test
+    public void
+    updateState_backupEnabled_hadConfigIntent_hasConfigSummary_prefDisplayConfigSummary() {
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mPSCD.setConfigIntent(mIntent);
+        mPSCD.setConfigSummary(mTestSummary);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        mController.updateState(mPreference);
+        assertThat(mPreference.getSummary()).isEqualTo(mTestSummary);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_isnotInvisibleKey_shouldBeAvailable() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        ShadowPrivacySettingsUtils.setIsInvisibleKey(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_isInvisibleKey_shouldBeDisabledUnsupported() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        ShadowPrivacySettingsUtils.setIsInvisibleKey(true);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/backup/DataManagementPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/backup/DataManagementPreferenceControllerTest.java
new file mode 100644
index 0000000..bd5bef4
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/backup/DataManagementPreferenceControllerTest.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.content.Intent;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+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;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {ShadowPrivacySettingsUtils.class})
+public class DataManagementPreferenceControllerTest {
+    private final String KEY = "data_management";
+    private Context mContext;
+    private DataManagementPreferenceController mController;
+    private PrivacySettingsConfigData mPSCD;
+    private Preference mPreference;
+    private String mTitle;
+
+    @Mock
+    private Intent mIntent;
+
+    @After
+    public void tearDown() {
+        ShadowPrivacySettingsUtils.reset();
+    }
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = RuntimeEnvironment.application;
+        mPSCD = new PrivacySettingsConfigData();
+        mController = new DataManagementPreferenceController(mContext, KEY);
+        mPreference = new Preference(mContext);
+        mTitle = "Title";
+    }
+
+    @Test
+    public void updateState_backupEnabled_hadManageIntent_hasManageLable_prefShouldBeHasTitle() {
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mPSCD.setManageIntent(mIntent);
+        mPSCD.setManageLabel(mTitle);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        mController.updateState(mPreference);
+        assertThat(mPreference.getTitle())
+                .isEqualTo(mTitle);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isAdmiUser_backupEnabled_hadManageIntent_shouldBeAvailable() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mPSCD.setManageIntent(mIntent);
+        mPSCD.setManageLabel(mTitle);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(false);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void
+    getAvailabilityStatus_isAdmiUser_backupEnabled_nullManageIntent_shouldBeDisabledUnsupported() {
+        ShadowPrivacySettingsUtils.setIsAdminUser(true);
+        mPSCD.setBackupEnabled(true);
+        mPSCD.setBackupGray(false);
+        mPSCD.setManageIntent(null);
+        mPSCD.setManageLabel(mTitle);
+        mController.setPrivacySettingsConfigData(mPSCD);
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/backup/ShadowPrivacySettingsUtils.java b/tests/robotests/src/com/android/settings/backup/ShadowPrivacySettingsUtils.java
new file mode 100644
index 0000000..74bf534
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/backup/ShadowPrivacySettingsUtils.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2018 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.backup;
+
+import android.content.Context;
+
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
+import org.robolectric.annotation.Resetter;
+
+@Implements(PrivacySettingsUtils.class)
+public class ShadowPrivacySettingsUtils {
+    private static boolean sIsAdminUser;
+    private static boolean sIsInvisibleKey;
+
+    @Resetter
+    static void reset() {
+        sIsAdminUser = true;
+        sIsInvisibleKey = false;
+    }
+
+    @Implementation
+    public static boolean isAdminUser(final Context context) {
+        return sIsAdminUser;
+    }
+
+    @Implementation
+    public static boolean isInvisibleKey(final Context context, final String key) {
+        return sIsInvisibleKey;
+    }
+
+    public static void setIsAdminUser(boolean isAdminUser) {
+        sIsAdminUser = isAdminUser;
+    }
+
+    public static void setIsInvisibleKey(boolean isInvisibleKey) {
+        sIsInvisibleKey = isInvisibleKey;
+    }
+}