Disable factory reset for secondary users

Search in Settings makes it possible to find the factory reset screen.
Disable the search index in PrivacySettings so that backup and reset
keywords don't get indexed for secondary users.
Also add additional safeguards for other entry points such as public
intents, so that the backup/reset screen does not show any options.

Bug: 18076086
Change-Id: Ie5135fbf4084038c99947a1a107ab4758f0c15a9
diff --git a/src/com/android/settings/MasterClear.java b/src/com/android/settings/MasterClear.java
index bbd98fb..f789b93 100644
--- a/src/com/android/settings/MasterClear.java
+++ b/src/com/android/settings/MasterClear.java
@@ -28,6 +28,7 @@
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Environment;
+import android.os.Process;
 import android.os.SystemProperties;
 import android.os.UserManager;
 import android.preference.Preference;
@@ -234,7 +235,8 @@
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
-        if (UserManager.get(getActivity()).hasUserRestriction(
+        if (!Process.myUserHandle().isOwner()
+                || UserManager.get(getActivity()).hasUserRestriction(
                 UserManager.DISALLOW_FACTORY_RESET)) {
             return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
         }
diff --git a/src/com/android/settings/PrivacySettings.java b/src/com/android/settings/PrivacySettings.java
index 1236c48..0a9f086 100644
--- a/src/com/android/settings/PrivacySettings.java
+++ b/src/com/android/settings/PrivacySettings.java
@@ -24,15 +24,24 @@
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.Process;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.UserHandle;
 import android.os.UserManager;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.PreferenceScreen;
 import android.preference.SwitchPreference;
+import android.provider.SearchIndexableResource;
 import android.provider.Settings;
 
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.search.Indexable.SearchIndexProvider;
+
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Gesture lock pattern settings.
  */
@@ -51,6 +60,7 @@
     private SwitchPreference mAutoRestore;
     private Dialog mConfirmDialog;
     private PreferenceScreen mConfigure;
+    private boolean mEnabled;
 
     private static final int DIALOG_ERASE_BACKUP = 2;
     private int mDialogType;
@@ -58,9 +68,14 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        // Don't allow any access if this is a secondary user
+        mEnabled = Process.myUserHandle().isOwner();
+        if (!mEnabled) {
+            return;
+        }
+
         addPreferencesFromResource(R.xml.privacy_settings);
         final PreferenceScreen screen = getPreferenceScreen();
-
         mBackupManager = IBackupManager.Stub.asInterface(
                 ServiceManager.getService(Context.BACKUP_SERVICE));
 
@@ -90,7 +105,9 @@
         super.onResume();
 
         // Refresh UI
-        updateToggles();
+        if (mEnabled) {
+            updateToggles();
+        }
     }
 
     @Override
@@ -233,4 +250,40 @@
     protected int getHelpResource() {
         return R.string.help_url_backup_reset;
     }
+
+    /**
+     * For Search.
+     */
+    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new PrivacySearchIndexProvider();
+
+    private static class PrivacySearchIndexProvider extends BaseSearchIndexProvider {
+
+        boolean mIsPrimary;
+
+        public PrivacySearchIndexProvider() {
+            super();
+
+            mIsPrimary = UserHandle.myUserId() == UserHandle.USER_OWNER;
+        }
+
+        @Override
+        public List<SearchIndexableResource> getXmlResourcesToIndex(
+                Context context, boolean enabled) {
+
+            List<SearchIndexableResource> result = new ArrayList<SearchIndexableResource>();
+
+            // For non-primary user, no backup or reset is available
+            if (!mIsPrimary) {
+                return result;
+            }
+
+            SearchIndexableResource sir = new SearchIndexableResource(context);
+            sir.xmlResId = R.xml.privacy_settings;
+            result.add(sir);
+
+            return result;
+        }
+    }
+
 }
\ No newline at end of file
diff --git a/src/com/android/settings/search/SearchIndexableResources.java b/src/com/android/settings/search/SearchIndexableResources.java
index 105ce7e..7b3fa77 100644
--- a/src/com/android/settings/search/SearchIndexableResources.java
+++ b/src/com/android/settings/search/SearchIndexableResources.java
@@ -224,7 +224,7 @@
         sResMap.put(PrivacySettings.class.getName(),
                 new SearchIndexableResource(
                         Ranking.getRankForClassName(PrivacySettings.class.getName()),
-                        R.xml.privacy_settings,
+                        NO_DATA_RES_ID,
                         PrivacySettings.class.getName(),
                         R.drawable.ic_settings_backup));