Respect user restrictions about adding/removing accounts, sideloading

Hide or disable options in the settings app when the user is restricted from
making changes.

Remove "Add account" button from Settings menu, and "remove account" option from menu in AccountSyncSettings.
Remove sideloading checkbox in SecuritySettings.

Also handle replacement of UserManager.isShareLocationToggleAllowed() with hasUserRestriction, which takes a restriction key string.

Change-Id: I34c74fd5aed8956ba00f92e3d3c657b608454dfe
diff --git a/src/com/android/settings/LocationSettings.java b/src/com/android/settings/LocationSettings.java
index b73dccd..2edefdf 100644
--- a/src/com/android/settings/LocationSettings.java
+++ b/src/com/android/settings/LocationSettings.java
@@ -96,7 +96,7 @@
         // Only enable these controls if this user is allowed to change location
         // sharing settings.
         final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
-        boolean isToggleAllowed = um.isLocationSharingToggleAllowed();
+        boolean isToggleAllowed = !um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION);
         if (mLocationAccess != null) mLocationAccess.setEnabled(isToggleAllowed);
         if (mNetwork != null) mNetwork.setEnabled(isToggleAllowed);
         if (mGps != null) mGps.setEnabled(isToggleAllowed);
@@ -117,6 +117,7 @@
 
         if (mSettingsObserver == null) {
             mSettingsObserver = new Observer() {
+                @Override
                 public void update(Observable o, Object arg) {
                     updateLocationToggles();
                 }
@@ -131,13 +132,13 @@
         final ContentResolver cr = getContentResolver();
         final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
         if (preference == mNetwork) {
-            if (um.isLocationSharingToggleAllowed()) {
+            if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
                 Settings.Secure.setLocationProviderEnabled(cr,
                         LocationManager.NETWORK_PROVIDER, mNetwork.isChecked());
             }
         } else if (preference == mGps) {
             boolean enabled = mGps.isChecked();
-            if (um.isLocationSharingToggleAllowed()) {
+            if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
                 Settings.Secure.setLocationProviderEnabled(cr,
                         LocationManager.GPS_PROVIDER, enabled);
                 if (mAssistedGps != null) {
@@ -186,7 +187,7 @@
     /** Enable or disable all providers when the master toggle is changed. */
     private void onToggleLocationAccess(boolean checked) {
         final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
-        if (! um.isLocationSharingToggleAllowed()) {
+        if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
             return;
         }
         final ContentResolver cr = getContentResolver();
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index b51f2f3..00a0e09 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -236,10 +236,26 @@
             removePreference(KEY_CREDENTIALS_MANAGER);
         }
 
+        PreferenceGroup deviceAdminCategory= (PreferenceGroup)
+                root.findPreference(KEY_DEVICE_ADMIN_CATEGORY);
         mToggleAppInstallation = (CheckBoxPreference) findPreference(
                 KEY_TOGGLE_INSTALL_APPLICATIONS);
         mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
 
+        final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
+        boolean isSideloadingAllowed =
+                !um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
+        // Side loading of apps.
+        if (isSideloadingAllowed) {
+            mToggleAppInstallation.setEnabled(isSideloadingAllowed);
+        } else {
+            if (deviceAdminCategory != null) {
+                deviceAdminCategory.removePreference(mToggleAppInstallation);
+            } else {
+                mToggleAppInstallation.setEnabled(false);
+            }
+        }
+
         // Package verification, only visible to primary user and if enabled
         mToggleVerifyApps = (CheckBoxPreference) findPreference(KEY_TOGGLE_VERIFY_APPLICATIONS);
         if (mIsPrimary && showVerifierSetting()) {
@@ -250,8 +266,6 @@
                 mToggleVerifyApps.setEnabled(false);
             }
         } else {
-            PreferenceGroup deviceAdminCategory= (PreferenceGroup)
-                    root.findPreference(KEY_DEVICE_ADMIN_CATEGORY);
             if (deviceAdminCategory != null) {
                 deviceAdminCategory.removePreference(mToggleVerifyApps);
             } else {
@@ -268,6 +282,10 @@
     }
 
     private void setNonMarketAppsAllowed(boolean enabled) {
+        final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
+        if (um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
+            return;
+        }
         // Change the system setting
         Settings.Global.putInt(getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS,
                                 enabled ? 1 : 0);
@@ -303,6 +321,7 @@
                 .show();
     }
 
+    @Override
     public void onClick(DialogInterface dialog, int which) {
         if (dialog == mWarnInstallApps && which == DialogInterface.BUTTON_POSITIVE) {
             setNonMarketAppsAllowed(true);
@@ -505,6 +524,7 @@
         createPreferenceHierarchy();
     }
 
+    @Override
     public boolean onPreferenceChange(Preference preference, Object value) {
         if (preference == mLockAfter) {
             int timeout = Integer.parseInt((String) value);
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index be8e1cc..64a7d7f 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -178,6 +178,7 @@
 
         if (mParentHeader != null) {
             setParentTitle(mParentHeader.title, null, new OnClickListener() {
+                @Override
                 public void onClick(View v) {
                     switchToParent(mParentHeader.fragment);
                 }
@@ -405,7 +406,6 @@
     @Override
     public void onBuildHeaders(List<Header> headers) {
         loadHeadersFromResource(R.xml.settings_headers, headers);
-
         updateHeaderList(headers);
     }
 
@@ -414,6 +414,8 @@
                 DevelopmentSettings.PREF_SHOW,
                 android.os.Build.TYPE.equals("eng"));
         int i = 0;
+
+        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
         mHeaderIndexMap.clear();
         while (i < target.size()) {
             Header header = target.get(i);
@@ -464,6 +466,10 @@
                         }
                     }
                 }
+            } else if (id == R.id.account_add) {
+                if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
+                    target.remove(i);
+                }
             }
 
             if (i < target.size() && target.get(i) == header
@@ -778,6 +784,7 @@
         return true;
     }
 
+    @Override
     public boolean shouldUpRecreateTask(Intent targetIntent) {
         return super.shouldUpRecreateTask(new Intent(this, Settings.class));
     }
diff --git a/src/com/android/settings/accounts/AccountSyncSettings.java b/src/com/android/settings/accounts/AccountSyncSettings.java
index c346017..64c4d4a 100644
--- a/src/com/android/settings/accounts/AccountSyncSettings.java
+++ b/src/com/android/settings/accounts/AccountSyncSettings.java
@@ -28,17 +28,16 @@
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
-import android.content.Intent;
 import android.content.SyncAdapterType;
 import android.content.SyncInfo;
 import android.content.SyncStatusInfo;
 import android.content.pm.ProviderInfo;
 import android.net.ConnectivityManager;
 import android.os.Bundle;
+import android.os.UserManager;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
 import android.text.TextUtils;
-import android.text.format.DateFormat;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.Menu;
@@ -93,10 +92,12 @@
                 .setNegativeButton(android.R.string.cancel, null)
                 .setPositiveButton(R.string.remove_account_label,
                         new DialogInterface.OnClickListener() {
+                    @Override
                     public void onClick(DialogInterface dialog, int which) {
                         AccountManager.get(AccountSyncSettings.this.getActivity())
                                 .removeAccount(mAccount,
                                 new AccountManagerCallback<Boolean>() {
+                            @Override
                             public void run(AccountManagerFuture<Boolean> future) {
                                 // If already out of this screen, don't proceed.
                                 if (!AccountSyncSettings.this.isResumed()) {
@@ -233,12 +234,15 @@
         MenuItem syncCancel = menu.add(0, MENU_SYNC_CANCEL_ID, 0,
                 getString(R.string.sync_menu_sync_cancel))
                 .setIcon(com.android.internal.R.drawable.ic_menu_close_clear_cancel);
-        MenuItem removeAccount = menu.add(0, MENU_REMOVE_ACCOUNT_ID, 0,
-                getString(R.string.remove_account_label))
-                .setIcon(R.drawable.ic_menu_delete_holo_dark);
 
-        removeAccount.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER |
-                MenuItem.SHOW_AS_ACTION_WITH_TEXT);
+        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
+        if (!um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
+            MenuItem removeAccount = menu.add(0, MENU_REMOVE_ACCOUNT_ID, 0,
+                    getString(R.string.remove_account_label))
+                    .setIcon(R.drawable.ic_menu_delete_holo_dark);
+            removeAccount.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER |
+                    MenuItem.SHOW_AS_ACTION_WITH_TEXT);
+        }
         syncNow.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER |
                 MenuItem.SHOW_AS_ACTION_WITH_TEXT);
         syncCancel.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER |
diff --git a/src/com/android/settings/accounts/AddAccountSettings.java b/src/com/android/settings/accounts/AddAccountSettings.java
index 6f7e29a..f1b7752 100644
--- a/src/com/android/settings/accounts/AddAccountSettings.java
+++ b/src/com/android/settings/accounts/AddAccountSettings.java
@@ -23,8 +23,10 @@
 import android.accounts.OperationCanceledException;
 import android.app.Activity;
 import android.app.PendingIntent;
+import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.UserManager;
 import android.util.Log;
 
 import com.android.settings.Utils;
@@ -71,7 +73,8 @@
 
     private PendingIntent mPendingIntent;
 
-    private AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {
+    private final AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {
+        @Override
         public void run(AccountManagerFuture<Bundle> future) {
             boolean done = true;
             try {
@@ -120,8 +123,10 @@
             if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "restored");
         }
 
-        if (mAddAccountCalled) {
+        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
+        if (mAddAccountCalled || um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
             // We already called add account - maybe the callback was lost.
+            // Or we aren't allowed to add an account.
             finish();
             return;
         }
@@ -162,6 +167,7 @@
         }
     }
 
+    @Override
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putBoolean(KEY_ADD_CALLED, mAddAccountCalled);
diff --git a/src/com/android/settings/widget/SettingsAppWidgetProvider.java b/src/com/android/settings/widget/SettingsAppWidgetProvider.java
index ac9df9f..5d581d8 100644
--- a/src/com/android/settings/widget/SettingsAppWidgetProvider.java
+++ b/src/com/android/settings/widget/SettingsAppWidgetProvider.java
@@ -541,7 +541,7 @@
                 protected Boolean doInBackground(Void... args) {
                     final UserManager um =
                             (UserManager) context.getSystemService(Context.USER_SERVICE);
-                    if (um.isLocationSharingToggleAllowed()) {
+                    if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
                         Settings.Secure.setLocationProviderEnabled(
                             resolver,
                             LocationManager.GPS_PROVIDER,