Merge "Import translations. DO NOT MERGE" into jb-mr2-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index f4b4bc6..143a394 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -1602,11 +1602,5 @@
             </intent-filter>
         </receiver>
 
-        <receiver android:name=".users.RestrictionsReceiver">
-            <intent-filter>
-                <action android:name="android.intent.action.GET_RESTRICTION_ENTRIES"/>
-            </intent-filter>
-        </receiver>
-
     </application>
 </manifest>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 11827be..573f4a0 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4422,6 +4422,27 @@
     <string name="user_restrictions_title">Allow apps and content</string>
     <!-- User limits screen, user name rename text [CHAR LIMIT=15] -->
     <string name="user_rename">RENAME</string>
+    <!-- Preference label for custom restrictions [CHAR LIMIT=35] -->
+    <string name="app_restrictions_custom_label">Set application limits</string>
+
+    <!-- Restrictions title for configuring wifi and mobile [CHAR LIMIT=35] -->
+    <string name="restriction_wifi_config_title">Wi\u2011Fi and Mobile</string>
+    <!-- Restrictions summary for configuring wifi and mobile [CHAR LIMIT=100] -->
+    <string name="restriction_wifi_config_summary">Allow modification of Wi\u2011Fi and Mobile settings</string>
+    <!-- Restrictions title for changing bluetooth configuration [CHAR LIMIT=35] -->
+    <string name="restriction_bluetooth_config_title">Bluetooth</string>
+    <!-- Restrictions summary for changing bluetooth configuration [CHAR LIMIT=100] -->
+    <string name="restriction_bluetooth_config_summary">Allow modification of Bluetooth pairings and settings</string>
+    <!-- Restrictions title for allowing NFC transfers [CHAR LIMIT=35] -->
+    <string name="restriction_nfc_enable_title">NFC</string>
+    <!-- Restrictions summary for allowing NFC transfers (tablet) [CHAR LIMIT=100] -->
+    <string name="restriction_nfc_enable_summary" product="tablet">Allow data exchange when the tablet touches another device</string>
+    <!-- Restrictions summary for allowing NFC transfers (phone) [CHAR LIMIT=100] -->
+    <string name="restriction_nfc_enable_summary" product="default">Allow data exchange when the phone touches another device</string>
+    <!-- Restrictions title for allowing location sharing [CHAR LIMIT=35] -->
+    <string name="restriction_location_enable_title">Location access</string>
+    <!-- Restrictions summary for allowing location sharing [CHAR LIMIT=100] -->
+    <string name="restriction_location_enable_summary" >Let apps use your location information</string>
 
     <!-- Wizard back button label [CHAR LIMIT=25] -->
     <string name="wizard_back">Back</string>
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index 64a7d7f..e29874b 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -57,7 +57,6 @@
 import com.android.settings.accounts.ManageAccountsSettings;
 import com.android.settings.bluetooth.BluetoothEnabler;
 import com.android.settings.bluetooth.BluetoothSettings;
-import com.android.settings.users.RestrictionsReceiver;
 import com.android.settings.wfd.WifiDisplaySettings;
 import com.android.settings.wifi.WifiEnabler;
 import com.android.settings.wifi.WifiSettings;
@@ -457,15 +456,6 @@
                 if (!showDev) {
                     target.remove(i);
                 }
-            } else if (id == R.id.application_settings) {
-                if (mAppRestrictions != null) {
-                    for (RestrictionEntry entry : mAppRestrictions) {
-                        if (entry.key.equals(RestrictionsReceiver.KEY_ENABLE_APPS)
-                                && !entry.getBooleanValue()) {
-                            target.remove(i);
-                        }
-                    }
-                }
             } else if (id == R.id.account_add) {
                 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
                     target.remove(i);
diff --git a/src/com/android/settings/deviceinfo/Memory.java b/src/com/android/settings/deviceinfo/Memory.java
index b669bac..d03f6dc 100644
--- a/src/com/android/settings/deviceinfo/Memory.java
+++ b/src/com/android/settings/deviceinfo/Memory.java
@@ -33,6 +33,7 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.UserManager;
 import android.os.storage.IMountService;
 import android.os.storage.StorageEventListener;
 import android.os.storage.StorageManager;
@@ -174,7 +175,10 @@
     @Override
     public void onPrepareOptionsMenu(Menu menu) {
         final MenuItem usb = menu.findItem(R.id.storage_usb);
-        usb.setVisible(!isMassStorageEnabled());
+        UserManager um = (UserManager)getActivity().getSystemService(Context.USER_SERVICE);
+        boolean usbItemVisible = !isMassStorageEnabled()
+                && !um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
+        usb.setVisible(usbItemVisible);
     }
 
     @Override
diff --git a/src/com/android/settings/deviceinfo/UsbSettings.java b/src/com/android/settings/deviceinfo/UsbSettings.java
index a836fb3..42de2fd 100644
--- a/src/com/android/settings/deviceinfo/UsbSettings.java
+++ b/src/com/android/settings/deviceinfo/UsbSettings.java
@@ -16,20 +16,16 @@
 
 package com.android.settings.deviceinfo;
 
-import android.app.AlertDialog;
-import android.app.Dialog;
 import android.content.BroadcastReceiver;
-import android.content.ContentQueryMap;
-import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.hardware.usb.UsbManager;
 import android.os.Bundle;
+import android.os.UserManager;
 import android.preference.CheckBoxPreference;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
-import android.provider.Settings;
 import android.util.Log;
 
 import com.android.settings.R;
@@ -73,6 +69,12 @@
         mMtp = (CheckBoxPreference)root.findPreference(KEY_MTP);
         mPtp = (CheckBoxPreference)root.findPreference(KEY_PTP);
 
+        UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
+        if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
+            mMtp.setEnabled(false);
+            mPtp.setEnabled(false);
+        }
+
         return root;
     }
 
@@ -112,8 +114,12 @@
             mMtp.setChecked(false);
             mPtp.setChecked(false);
         }
-
-        if (!mUsbAccessoryMode) {
+        UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
+        if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
+            Log.e(TAG, "USB is locked down");
+            mMtp.setEnabled(false);
+            mPtp.setEnabled(false);
+        } else if (!mUsbAccessoryMode) {
             //Enable MTP and PTP switch while USB is not in Accessory Mode, otherwise disable it
             Log.e(TAG, "USB Normal Mode");
             mMtp.setEnabled(true);
@@ -133,6 +139,12 @@
         if (Utils.isMonkeyRunning()) {
             return true;
         }
+        // If this user is disallowed from using USB, don't handle their attempts to change the
+        // setting.
+        UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
+        if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
+            return true;
+        }
 
         String function = "none";
         if (preference == mMtp && mMtp.isChecked()) {
diff --git a/src/com/android/settings/users/AppRestrictionsFragment.java b/src/com/android/settings/users/AppRestrictionsFragment.java
index b610025..6154bdf 100644
--- a/src/com/android/settings/users/AppRestrictionsFragment.java
+++ b/src/com/android/settings/users/AppRestrictionsFragment.java
@@ -16,10 +16,12 @@
 
 package com.android.settings.users;
 
+import android.appwidget.AppWidgetManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.RestrictionEntry;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
@@ -31,6 +33,7 @@
 import android.graphics.drawable.ColorDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
+import android.os.Parcelable;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.preference.CheckBoxPreference;
@@ -85,11 +88,14 @@
     private static final int MAX_APP_RESTRICTIONS = 100;
 
     private static final String DELIMITER = ";";
-    private List<ResolveInfo> mApps;
     HashMap<String,Boolean> mSelectedPackages = new HashMap<String,Boolean>();
     private boolean mFirstTime = true;
     private boolean mNewUser;
 
+    private int mCustomRequestCode;
+    private HashMap<Integer, AppRestrictionsPreference> mCustomRequestMap =
+            new HashMap<Integer,AppRestrictionsPreference>();
+
     public static class Activity extends PreferenceActivity {
         @Override
         public Intent getIntent() {
@@ -133,7 +139,7 @@
         RestrictionEntry getRestriction(String key) {
             if (restrictions == null) return null;
             for (RestrictionEntry entry : restrictions) {
-                if (entry.key.equals(key)) {
+                if (entry.getKey().equals(key)) {
                     return entry;
                 }
             }
@@ -207,33 +213,70 @@
         mUserPreference.setText(info.name);
     }
 
+    private void addSystemApps(List<ApplicationInfo> visibleApps, Intent intent) {
+        final PackageManager pm = getActivity().getPackageManager();
+        List<ResolveInfo> launchableApps = pm.queryIntentActivities(intent, 0);
+        for (ResolveInfo app : launchableApps) {
+            if (app.activityInfo != null && app.activityInfo.applicationInfo != null) {
+                int flags = app.activityInfo.applicationInfo.flags;
+                if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0
+                        || (flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
+                    // System app
+                    visibleApps.add(app.activityInfo.applicationInfo);
+                }
+            }
+        }
+    }
+
     private void populateApps() {
         mAppList.setOrderingAsAdded(false);
-
+        List<ApplicationInfo> visibleApps = new ArrayList<ApplicationInfo>();
         // TODO: Do this asynchronously since it can be a long operation
         final Context context = getActivity();
         PackageManager pm = context.getPackageManager();
+
+        // Add launchers
         Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
         launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
-        mApps = pm.queryIntentActivities(launcherIntent, 0);
-        Collections.sort(mApps, new AppLabelComparator(pm));
+        addSystemApps(visibleApps, launcherIntent);
 
+        // Add widgets
+        Intent widgetIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
+        addSystemApps(visibleApps, widgetIntent);
+
+        List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
+        for (ApplicationInfo app : installedApps) {
+            if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0
+                    && (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0) {
+                // Downloaded app
+                visibleApps.add(app);
+            }
+        }
+        Collections.sort(visibleApps, new AppLabelComparator(pm));
+
+        for (int i = visibleApps.size() - 1; i > 1; i--) {
+            ApplicationInfo appInfo = visibleApps.get(i);
+            if (appInfo.packageName.equals(visibleApps.get(i-1).packageName)) {
+                visibleApps.remove(i);
+            }
+        }
         Intent restrictionsIntent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
         final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(restrictionsIntent, 0);
         final List<ResolveInfo> existingApps = pm.queryIntentActivitiesAsUser(launcherIntent,
                 0, mUser.getIdentifier());
         int i = 0;
-        if (receivers != null && receivers.size() > 0) {
-            for (ResolveInfo app : mApps) {
-                if (app.activityInfo == null || app.activityInfo.packageName == null) continue;
-                String packageName = app.activityInfo.packageName;
+        if (visibleApps.size() > 0) {
+            for (ApplicationInfo app : visibleApps) {
+                if (app.packageName == null) continue;
+                String packageName = app.packageName;
                 Drawable icon = app.loadIcon(pm);
                 CharSequence label = app.loadLabel(pm);
                 AppRestrictionsPreference p = new AppRestrictionsPreference(context, this);
                 p.setIcon(icon);
                 p.setTitle(label);
                 p.setKey(PKG_PREFIX + packageName);
-                p.setSettingsEnabled(hasPackage(receivers, packageName));
+                p.setSettingsEnabled(hasPackage(receivers, packageName)
+                        || packageName.equals(getActivity().getPackageName()));
                 p.setPersistent(false);
                 p.setOnPreferenceChangeListener(this);
                 p.setOnPreferenceClickListener(this);
@@ -261,7 +304,7 @@
         }
     }
 
-    private class AppLabelComparator implements Comparator<ResolveInfo> {
+    private class AppLabelComparator implements Comparator<ApplicationInfo> {
 
         PackageManager pm;
 
@@ -269,13 +312,13 @@
             this.pm = pm;
         }
 
-        private CharSequence getLabel(ResolveInfo info) {
+        private CharSequence getLabel(ApplicationInfo info) {
             // TODO: Optimize this with a cache
-            return info.activityInfo.loadLabel(pm);
+            return info.loadLabel(pm);
         }
 
         @Override
-        public int compare(ResolveInfo lhs, ResolveInfo rhs) {
+        public int compare(ApplicationInfo lhs, ApplicationInfo rhs) {
             String lhsLabel = getLabel(lhs).toString();
             String rhsLabel = getLabel(rhs).toString();
             return lhsLabel.compareTo(rhsLabel);
@@ -296,7 +339,7 @@
         if (v.getTag() instanceof AppRestrictionsPreference) {
             AppRestrictionsPreference pref = (AppRestrictionsPreference) v.getTag();
             if (v.getId() == R.id.app_restrictions_settings) {
-                handleSettingsClick(pref);
+                toggleAppPanel(pref);
             } else if (!pref.isRequired()) {
                 pref.setChecked(!pref.isChecked());
                 mSelectedPackages.put(pref.getKey().substring(PKG_PREFIX.length()),
@@ -317,21 +360,18 @@
             ArrayList<RestrictionEntry> restrictions = appPref.getRestrictions();
             if (restrictions != null) {
                 for (RestrictionEntry entry : restrictions) {
-                    if (entry.key.equals(restrictionKey)) {
-                        switch (entry.type) {
+                    if (entry.getKey().equals(restrictionKey)) {
+                        switch (entry.getType()) {
                         case RestrictionEntry.TYPE_BOOLEAN:
-                            entry.setValue((Boolean) newValue);
+                            entry.setSelectedState((Boolean) newValue);
                             break;
                         case RestrictionEntry.TYPE_CHOICE:
                         case RestrictionEntry.TYPE_CHOICE_LEVEL:
                             ListPreference listPref = (ListPreference) preference;
-                            entry.setValue((String) newValue);
-                            for (int i = 0; i < listPref.getEntryValues().length; i++) {
-                                if (entry.values[i].equals(newValue)) {
-                                    listPref.setSummary(entry.choices[i]);
-                                    break;
-                                }
-                            }
+                            entry.setSelectedString((String) newValue);
+                            String readable = findInArray(entry.getChoiceEntries(),
+                                    entry.getChoiceValues(), (String) newValue);
+                            listPref.setSummary(readable);
                             break;
                         case RestrictionEntry.TYPE_MULTI_SELECT:
                             MultiSelectListPreference msListPref =
@@ -339,13 +379,17 @@
                             Set<String> set = (Set<String>) newValue;
                             String [] selectedValues = new String[set.size()];
                             set.toArray(selectedValues);
-                            entry.setMultipleValues(selectedValues);
+                            entry.setAllSelectedStrings(selectedValues);
                             break;
                         default:
                             continue;
                         }
-                        mUserManager.setApplicationRestrictions(packageName, restrictions,
-                                mUser);
+                        if (packageName.equals(getActivity().getPackageName())) {
+                            RestrictionUtils.setRestrictions(getActivity(), restrictions, mUser);
+                        } else {
+                            mUserManager.setApplicationRestrictions(packageName, restrictions,
+                                    mUser);
+                        }
                         break;
                     }
                 }
@@ -360,7 +404,7 @@
         return true;
     }
 
-    private void handleSettingsClick(AppRestrictionsPreference preference) {
+    private void toggleAppPanel(AppRestrictionsPreference preference) {
         if (preference.getKey().startsWith(PKG_PREFIX)) {
             if (preference.panelOpen) {
                 for (Preference p : preference.childPreferences) {
@@ -369,14 +413,22 @@
                 preference.childPreferences.clear();
             } else {
                 String packageName = preference.getKey().substring(PKG_PREFIX.length());
-                List<RestrictionEntry> oldEntries =
-                        mUserManager.getApplicationRestrictions(packageName, mUser);
-                Intent intent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
-                intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS,
-                        new ArrayList<RestrictionEntry>(oldEntries));
-                getActivity().sendOrderedBroadcast(intent, null,
-                        new RestrictionsResultReceiver(packageName, preference),
-                        null, Activity.RESULT_OK, null, null);
+                if (packageName.equals(getActivity().getPackageName())) {
+                    // Settings, fake it by using user restrictions
+                    ArrayList<RestrictionEntry> restrictions = RestrictionUtils.getRestrictions(
+                            getActivity(), mUser);
+                    onRestrictionsReceived(preference, packageName, restrictions);
+                } else {
+                    List<RestrictionEntry> oldEntries =
+                            mUserManager.getApplicationRestrictions(packageName, mUser);
+                    Intent intent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
+                    intent.setPackage(packageName);
+                    intent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS,
+                            new ArrayList<RestrictionEntry>(oldEntries));
+                    getActivity().sendOrderedBroadcast(intent, null,
+                            new RestrictionsResultReceiver(packageName, preference),
+                            null, Activity.RESULT_OK, null, null);
+                }
             }
             preference.panelOpen = !preference.panelOpen;
         }
@@ -384,6 +436,7 @@
 
     class RestrictionsResultReceiver extends BroadcastReceiver {
 
+        private static final String CUSTOM_RESTRICTIONS_INTENT = Intent.EXTRA_RESTRICTIONS_INTENT;
         String packageName;
         AppRestrictionsPreference preference;
 
@@ -395,64 +448,144 @@
 
         @Override
         public void onReceive(Context context, Intent intent) {
-            ArrayList<RestrictionEntry> restrictions = getResultExtras(true).getParcelableArrayList(
+            Bundle results = getResultExtras(true);
+            final ArrayList<RestrictionEntry> restrictions = results.getParcelableArrayList(
                     Intent.EXTRA_RESTRICTIONS);
-            if (restrictions != null) {
-                int count = 1;
-                for (RestrictionEntry entry : restrictions) {
-                    Preference p = null;
-                    switch (entry.type) {
-                    case RestrictionEntry.TYPE_BOOLEAN:
-                        p = new CheckBoxPreference(context);
-                        p.setTitle(entry.title);
-                        p.setSummary(entry.description);
-                        ((CheckBoxPreference)p).setChecked(entry.getBooleanValue());
-                        break;
-                    case RestrictionEntry.TYPE_CHOICE:
-                    case RestrictionEntry.TYPE_CHOICE_LEVEL:
-                        p = new ListPreference(context);
-                        p.setTitle(entry.title);
-                        String value = entry.getStringValue();
-                        if (value == null) {
-                            value = entry.description;
-                        }
-                        p.setSummary(value);
-                        ((ListPreference)p).setEntryValues(entry.values);
-                        ((ListPreference)p).setEntries(entry.choices);
-                        ((ListPreference)p).setValue(entry.getStringValue());
-                        break;
-                    case RestrictionEntry.TYPE_MULTI_SELECT:
-                        p = new MultiSelectListPreference(context);
-                        p.setTitle(entry.title);
-                        ((MultiSelectListPreference)p).setEntryValues(entry.values);
-                        ((MultiSelectListPreference)p).setEntries(entry.choices);
-                        HashSet<String> set = new HashSet<String>();
-                        for (String s : entry.getMultipleValues()) {
-                            set.add(s);
-                        }
-                        ((MultiSelectListPreference)p).setValues(set);
-                        break;
-                    case RestrictionEntry.TYPE_NULL:
-                    default:
+            Intent restrictionsIntent = (Intent) results.getParcelable(CUSTOM_RESTRICTIONS_INTENT);
+            if (restrictions != null && restrictionsIntent == null) {
+                onRestrictionsReceived(preference, packageName, restrictions);
+                mUserManager.setApplicationRestrictions(packageName, restrictions, mUser);
+            } else if (restrictionsIntent != null) {
+                final Intent customIntent = restrictionsIntent;
+                customIntent.putParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS, restrictions);
+                Preference p = new Preference(context);
+                p.setTitle(R.string.app_restrictions_custom_label);
+                p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
+                    @Override
+                    public boolean onPreferenceClick(Preference preference) {
+                        int requestCode = generateCustomActivityRequestCode(
+                                RestrictionsResultReceiver.this.preference);
+                        AppRestrictionsFragment.this.startActivityForResult(
+                                customIntent, requestCode);
+                        return false;
                     }
-                    if (p != null) {
-                        p.setPersistent(false);
-                        p.setOrder(preference.getOrder() + count);
-                        // Store the restrictions key string as a key for the preference
-                        p.setKey(preference.getKey().substring(PKG_PREFIX.length()) + DELIMITER
-                                + entry.key);
-                        mAppList.addPreference(p);
-                        p.setOnPreferenceChangeListener(AppRestrictionsFragment.this);
-                        preference.childPreferences.add(p);
-                        count++;
-                    }
-                }
+                });
+                p.setPersistent(false);
+                p.setOrder(preference.getOrder() + 1);
+                preference.childPreferences.add(p);
+                mAppList.addPreference(p);
                 preference.setRestrictions(restrictions);
             }
-            mUserManager.setApplicationRestrictions(packageName, restrictions, mUser);
         }
     }
 
+    private void onRestrictionsReceived(AppRestrictionsPreference preference, String packageName,
+            ArrayList<RestrictionEntry> restrictions) {
+        // Non-custom-activity case - expand the restrictions in-place
+        final Context context = preference.getContext();
+        int count = 1;
+        for (RestrictionEntry entry : restrictions) {
+            Preference p = null;
+            switch (entry.getType()) {
+            case RestrictionEntry.TYPE_BOOLEAN:
+                p = new CheckBoxPreference(context);
+                p.setTitle(entry.getTitle());
+                p.setSummary(entry.getDescription());
+                ((CheckBoxPreference)p).setChecked(entry.getSelectedState());
+                break;
+            case RestrictionEntry.TYPE_CHOICE:
+            case RestrictionEntry.TYPE_CHOICE_LEVEL:
+                p = new ListPreference(context);
+                p.setTitle(entry.getTitle());
+                String value = entry.getSelectedString();
+                if (value == null) {
+                    value = entry.getDescription();
+                }
+                p.setSummary(findInArray(entry.getChoiceEntries(), entry.getChoiceValues(),
+                        value));
+                ((ListPreference)p).setEntryValues(entry.getChoiceValues());
+                ((ListPreference)p).setEntries(entry.getChoiceEntries());
+                ((ListPreference)p).setValue(value);
+                break;
+            case RestrictionEntry.TYPE_MULTI_SELECT:
+                p = new MultiSelectListPreference(context);
+                p.setTitle(entry.getTitle());
+                ((MultiSelectListPreference)p).setEntryValues(entry.getChoiceValues());
+                ((MultiSelectListPreference)p).setEntries(entry.getChoiceEntries());
+                HashSet<String> set = new HashSet<String>();
+                for (String s : entry.getAllSelectedStrings()) {
+                    set.add(s);
+                }
+                ((MultiSelectListPreference)p).setValues(set);
+                break;
+            case RestrictionEntry.TYPE_NULL:
+            default:
+            }
+            if (p != null) {
+                p.setPersistent(false);
+                p.setOrder(preference.getOrder() + count);
+                // Store the restrictions key string as a key for the preference
+                p.setKey(preference.getKey().substring(PKG_PREFIX.length()) + DELIMITER
+                        + entry.getKey());
+                mAppList.addPreference(p);
+                p.setOnPreferenceChangeListener(AppRestrictionsFragment.this);
+                preference.childPreferences.add(p);
+                count++;
+            }
+        }
+        preference.setRestrictions(restrictions);
+    }
+
+    /**
+     * Generates a request code that is stored in a map to retrieve the associated
+     * AppRestrictionsPreference.
+     * @param preference
+     * @return
+     */
+    private int generateCustomActivityRequestCode(AppRestrictionsPreference preference) {
+        mCustomRequestCode++;
+        mCustomRequestMap.put(mCustomRequestCode, preference);
+        return mCustomRequestCode;
+    }
+
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+
+        Log.i(TAG, "Got activity resultCode=" + resultCode + ", requestCode="
+                + requestCode + ", data=" + data);
+
+        AppRestrictionsPreference pref = mCustomRequestMap.get(requestCode);
+        if (pref == null) {
+            Log.w(TAG, "Unknown requestCode " + requestCode);
+            return;
+        }
+
+        if (resultCode == Activity.RESULT_OK) {
+            ArrayList<RestrictionEntry> list =
+                    data.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS);
+            if (list != null) {
+                // If there's a valid result, persist it to the user manager.
+                String packageName = pref.getKey().substring(PKG_PREFIX.length());
+                pref.setRestrictions(list);
+                mUserManager.setApplicationRestrictions(packageName, list, mUser);
+            }
+            toggleAppPanel(pref);
+        }
+        // Remove request from the map
+        mCustomRequestMap.remove(requestCode);
+    }
+
+    private String findInArray(String[] choiceEntries, String[] choiceValues,
+            String selectedString) {
+        for (int i = 0; i < choiceValues.length; i++) {
+            if (choiceValues[i].equals(selectedString)) {
+                return choiceEntries[i];
+            }
+        }
+        return selectedString;
+    }
+
     @Override
     public boolean onPreferenceClick(Preference preference) {
         if (preference.getKey().startsWith(PKG_PREFIX)) {
diff --git a/src/com/android/settings/users/RestrictionUtils.java b/src/com/android/settings/users/RestrictionUtils.java
new file mode 100644
index 0000000..8b675a5
--- /dev/null
+++ b/src/com/android/settings/users/RestrictionUtils.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2013 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.users;
+
+import android.content.Context;
+import android.content.RestrictionEntry;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.provider.Settings.Secure;
+
+import com.android.settings.R;
+
+import java.util.ArrayList;
+
+public class RestrictionUtils {
+
+    public static final String [] sRestrictionKeys = {
+        UserManager.DISALLOW_CONFIG_WIFI,
+        UserManager.DISALLOW_CONFIG_BLUETOOTH,
+        UserManager.DISALLOW_SHARE_LOCATION,
+        UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES
+    };
+
+    public static final int [] sRestrictionTitles = {
+        R.string.restriction_wifi_config_title,
+        R.string.restriction_bluetooth_config_title,
+        R.string.restriction_location_enable_title,
+        R.string.install_applications
+    };
+
+    public static final int [] sRestrictionDescriptions = {
+        R.string.restriction_wifi_config_summary,
+        R.string.restriction_bluetooth_config_summary,
+        R.string.restriction_location_enable_summary,
+        R.string.install_unknown_applications
+    };
+
+    /**
+     * Returns the current user restrictions in the form of application
+     * restriction entries.
+     * @return list of RestrictionEntry objects with user-visible text.
+     */
+    public static ArrayList<RestrictionEntry> getRestrictions(Context context, UserHandle user) {
+        Resources res = context.getResources();
+        ArrayList<RestrictionEntry> entries = new ArrayList<RestrictionEntry>();
+        UserManager um = UserManager.get(context);
+        Bundle userRestrictions = um.getUserRestrictions(user);
+
+        for (int i = 0; i < sRestrictionKeys.length; i++) {
+            RestrictionEntry entry = new RestrictionEntry(
+                    sRestrictionKeys[i],
+                    !userRestrictions.getBoolean(sRestrictionKeys[i], false));
+            entry.setTitle(res.getString(sRestrictionTitles[i]));
+            entry.setDescription(res.getString(sRestrictionDescriptions[i]));
+            entry.setType(RestrictionEntry.TYPE_BOOLEAN);
+            entries.add(entry);
+        }
+
+        return entries;
+    }
+
+    public static void setRestrictions(Context context, ArrayList<RestrictionEntry> entries,
+            UserHandle user) {
+        UserManager um = UserManager.get(context);
+        Bundle userRestrictions = um.getUserRestrictions(user);
+
+        for (RestrictionEntry entry : entries) {
+            userRestrictions.putBoolean(entry.getKey(), !entry.getSelectedState());
+            if (entry.getKey().equals(UserManager.DISALLOW_SHARE_LOCATION)
+                    && !entry.getSelectedState()) {
+                Secure.putStringForUser(context.getContentResolver(),
+                        Secure.LOCATION_PROVIDERS_ALLOWED, "", user.getIdentifier());
+            }
+        }
+        um.setUserRestrictions(userRestrictions, user);
+    }
+}
diff --git a/src/com/android/settings/users/RestrictionsReceiver.java b/src/com/android/settings/users/RestrictionsReceiver.java
deleted file mode 100644
index 0eeb7cd..0000000
--- a/src/com/android/settings/users/RestrictionsReceiver.java
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * Copyright (C) 2013 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.users;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.RestrictionEntry;
-import android.os.Bundle;
-import android.util.Log;
-
-import com.android.settings.R;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/** Test class, to demonstrate the features. TODO: Remove or modify with real restrictions */
-public class RestrictionsReceiver extends BroadcastReceiver {
-
-    private static final String TAG = RestrictionsReceiver.class.getSimpleName();
-
-    public static final String KEY_VERSION = "version";
-    public static final String KEY_ENABLE_APPS = "enable_apps";
-    public static final String KEY_SECTIONS_TO_SHOW = "enable_sections";
-    public static final String KEY_CONTENT_RATING = "content_rating";
-
-    private static final int[] SECTION_IDS = {
-        R.id.wifi_settings,
-        R.id.bluetooth_settings,
-        R.id.data_usage_settings,
-        R.id.app_settings,
-        R.id.date_time_settings,
-        R.id.about_settings
-    };
-
-    private static final int[] SECTION_TITLE_IDS = {
-        R.string.wifi_settings,
-        R.string.bluetooth_settings,
-        R.string.data_usage_summary_title,
-        R.string.manageapplications_settings_title,
-        R.string.date_and_time,
-        R.string.about_settings
-    };
-
-    @Override
-    public void onReceive(final Context context, Intent intent) {
-        final PendingResult result = goAsync();
-        final ArrayList<RestrictionEntry> oldRestrictions =
-                intent.getParcelableArrayListExtra(Intent.EXTRA_RESTRICTIONS);
-        Log.i(TAG, "oldRestrictions = " + oldRestrictions);
-        new Thread() {
-            public void run() {
-                createRestrictions(context, result, oldRestrictions);
-            }
-        }.start();
-    }
-
-    private void createRestrictions(Context context,
-            PendingResult result, List<RestrictionEntry> old) {
-        ArrayList<RestrictionEntry> newRestrictions = new ArrayList<RestrictionEntry>();
-        boolean oldEnableApps = false;
-        String oldContentRating = "";
-        String[] oldEnabledSections = new String[0];
-        if (old != null) {
-            for (RestrictionEntry r : old) {
-                if (r.key.equals(KEY_ENABLE_APPS)) {
-                    oldEnableApps = r.getBooleanValue();
-                } else if (r.key.equals(KEY_CONTENT_RATING)) {
-                    oldContentRating = r.getStringValue();
-                } else if (r.key.equals(KEY_SECTIONS_TO_SHOW)) {
-                    oldEnabledSections = r.getMultipleValues();
-                }
-            }
-        }
-
-        RestrictionEntry r0 = new RestrictionEntry(KEY_VERSION, "1");
-        newRestrictions.add(r0);
-
-        RestrictionEntry r1 = new RestrictionEntry(KEY_ENABLE_APPS,
-                Boolean.toString(oldEnableApps));
-        r1.title = "Enable apps";
-        r1.description = "Show the Apps section in Settings";
-        r1.type = RestrictionEntry.TYPE_BOOLEAN;
-        newRestrictions.add(r1);
-
-        RestrictionEntry r2 = new RestrictionEntry(KEY_CONTENT_RATING, oldContentRating);
-        r2.title = "Test: Content rating";
-        r2.description = "Limit content to chosen rating and lower";
-        r2.type = RestrictionEntry.TYPE_CHOICE_LEVEL;
-        r2.values = new String[] { "G", "PG", "PG13", "R", "NR" };
-        r2.choices = new String[] { "G", "PG", "PG-13", "Restricted", "Not Rated" };
-        newRestrictions.add(r2);
-
-        String [] values = new String[SECTION_IDS.length];
-        String [] choices = new String[SECTION_IDS.length];
-        int i = 0;
-        for (int sectionId : SECTION_IDS) {
-            values[i] = Integer.toString(sectionId);
-            choices[i] = context.getString(SECTION_TITLE_IDS[i]);
-            i++;
-        }
-        RestrictionEntry r3 = new RestrictionEntry(KEY_SECTIONS_TO_SHOW, oldEnabledSections);
-        r3.type = RestrictionEntry.TYPE_MULTI_SELECT;
-        r3.choices = choices;
-        r3.values = values;
-        r3.title = "Test: Sections to show";
-        newRestrictions.add(r3);
-
-        Bundle extras = new Bundle();
-        extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS, newRestrictions);
-        result.setResult(0, null, extras);
-        result.finish();
-    }
-}
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index 872d144..5830184 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -501,6 +501,12 @@
         boolean moreUsers = mUserManager.getMaxSupportedUsers() > users.size();
         mAddRestrictedUser.setEnabled(moreUsers);
         mAddTrustedUser.setEnabled(moreUsers);
+        // Remove the limited users category if there aren't other limited users.
+        if (!mIsOwner) {
+            if (mLimitedUserListCategory.getPreferenceCount() == 0) {
+                removePreference(KEY_LIMITED_USER_LIST);
+            }
+        }
     }
 
     private void loadIconsAsync(List<Integer> missingIcons) {