Merge "Fix repeating apps on pause/resume" into jb-mr2-dev
diff --git a/res/layout/user_info_header.xml b/res/layout/user_info_header.xml
new file mode 100644
index 0000000..f2b0c59
--- /dev/null
+++ b/res/layout/user_info_header.xml
@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:gravity="center_vertical"
+    android:orientation="vertical"
+    android:focusable="true"
+    android:clickable="true"
+    android:paddingStart="@*android:dimen/preference_fragment_padding_side"
+    android:paddingEnd="@*android:dimen/preference_fragment_padding_side" >
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:layout_weight="1"
+        android:id="@+id/app_restrictions_pref"
+        android:gravity="center_vertical"
+        android:paddingStart="@*android:dimen/preference_item_padding_side"
+        android:paddingEnd="?android:attr/scrollbarSize"
+        android:background="?android:attr/selectableItemBackground" >
+        <LinearLayout
+            android:layout_width="wrap_content"
+            android:layout_height="match_parent"
+            android:gravity="center"
+            android:minWidth="@*android:dimen/preference_icon_minWidth"
+            android:orientation="horizontal">
+            <ImageView
+                android:id="@+android:id/icon"
+                android:layout_width="48dp"
+                android:layout_height="48dp"
+                android:layout_gravity="center"
+                android:minWidth="48dp"
+                android:scaleType="centerInside"
+                android:layout_marginEnd="@*android:dimen/preference_item_padding_inner"
+                 />
+        </LinearLayout>
+        <RelativeLayout
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginEnd="6dip"
+            android:layout_marginTop="6dip"
+            android:layout_marginBottom="6dip"
+            android:layout_weight="1">
+            <TextView
+                android:id="@+android:id/title"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:singleLine="true"
+                android:textAppearance="?android:attr/textAppearanceMedium"
+                android:ellipsize="marquee"
+                android:fadingEdge="horizontal"/>
+            <TextView
+                android:id="@android:id/summary"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_below="@android:id/title"
+                android:layout_alignStart="@android:id/title"
+                android:paddingBottom="3dip"
+                android:visibility="gone"
+                android:textAppearance="?android:attr/textAppearanceSmall"
+                android:textSize="13sp"
+                android:textColor="?android:attr/textColorSecondary"
+                android:focusable="false"
+                android:maxLines="4" />
+        </RelativeLayout>
+    </LinearLayout>
+    <View android:layout_width="match_parent"
+          android:layout_height="2dp"
+          android:background="@color/divider_color" />
+</LinearLayout>
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index e97c73f..a64d731 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -257,10 +257,8 @@
                 KEY_TOGGLE_INSTALL_APPLICATIONS);
         mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
 
-        boolean isSideloadingAllowed =
-                !um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
         // Side loading of apps.
-        mToggleAppInstallation.setEnabled(isSideloadingAllowed);
+        mToggleAppInstallation.setEnabled(mIsPrimary);
 
         // Package verification, only visible to primary user and if enabled
         mToggleVerifyApps = (CheckBoxPreference) findPreference(KEY_TOGGLE_VERIFY_APPLICATIONS);
diff --git a/src/com/android/settings/users/AppRestrictionsFragment.java b/src/com/android/settings/users/AppRestrictionsFragment.java
index a7540cc..9f64b71 100644
--- a/src/com/android/settings/users/AppRestrictionsFragment.java
+++ b/src/com/android/settings/users/AppRestrictionsFragment.java
@@ -74,6 +74,7 @@
 import android.widget.ListAdapter;
 import android.widget.ListPopupWindow;
 import android.widget.Switch;
+import android.widget.TextView;
 
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
@@ -104,7 +105,6 @@
     private UserManager mUserManager;
     private UserHandle mUser;
 
-    private Preference mUserPreference;
     private PreferenceGroup mAppList;
 
     private static final int MAX_APP_RESTRICTIONS = 100;
@@ -125,6 +125,9 @@
     private int mCustomRequestCode;
     private HashMap<Integer, AppRestrictionsPreference> mCustomRequestMap =
             new HashMap<Integer,AppRestrictionsPreference>();
+    private View mHeaderView;
+    private ImageView mUserIconView;
+    private TextView mUserNameView;
 
     private List<SelectableAppInfo> mVisibleApps;
     private List<ApplicationInfo> mUserApps;
@@ -262,14 +265,25 @@
         mUserManager = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
         addPreferencesFromResource(R.xml.app_restrictions);
         mAppList = getPreferenceScreen();
-        mUserPreference = findPreference(KEY_USER_INFO);
-
-        mUserPreference.setOnPreferenceClickListener(this);
-
         setHasOptionsMenu(true);
     }
 
     @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        if (mHeaderView == null) {
+            mHeaderView = LayoutInflater.from(getActivity()).inflate(
+                    R.layout.user_info_header, null);
+            ((ViewGroup) getListView().getParent()).addView(mHeaderView, 0);
+            mHeaderView.setOnClickListener(this);
+            mUserIconView = (ImageView) mHeaderView.findViewById(android.R.id.icon);
+            mUserNameView = (TextView) mHeaderView.findViewById(android.R.id.title);
+            getListView().setFastScrollEnabled(true);
+        }
+        // This is going to bind the preferences.
+        super.onActivityCreated(savedInstanceState);
+    }
+
+    @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putBoolean(EXTRA_NEW_USER, mNewUser);
@@ -282,12 +296,11 @@
         new AppLoadingTask().execute((Void[]) null);
 
         UserInfo info = mUserManager.getUserInfo(mUser.getIdentifier());
-        mUserPreference.setTitle(info.name);
         Bitmap userIcon = mUserManager.getUserIcon(mUser.getIdentifier());
         CircleFramedDrawable circularIcon =
                 CircleFramedDrawable.getInstance(this.getActivity(), userIcon);
-        mUserPreference.setIcon(circularIcon);
-        mUserPreference.setTitle(info.name);
+        ((TextView) mHeaderView.findViewById(android.R.id.title)).setText(info.name);
+        ((ImageView) mHeaderView.findViewById(android.R.id.icon)).setImageDrawable(circularIcon);
     }
 
     public void onPause() {
@@ -335,6 +348,7 @@
     }
 
     private void addSystemApps(List<SelectableAppInfo> visibleApps, Intent intent) {
+        if (getActivity() == null) return;
         final PackageManager pm = getActivity().getPackageManager();
         List<ResolveInfo> launchableApps = pm.queryIntentActivities(intent, 0);
         for (ResolveInfo app : launchableApps) {
@@ -377,6 +391,7 @@
         mAppList.setOrderingAsAdded(false);
         mVisibleApps = new ArrayList<SelectableAppInfo>();
         final Context context = getActivity();
+        if (context == null) return;
         PackageManager pm = context.getPackageManager();
         IPackageManager ipm = AppGlobals.getPackageManager();
 
@@ -450,13 +465,14 @@
                 packageMap.put(info.packageName, info);
             }
         }
-
     }
 
     private void populateApps() {
         final Context context = getActivity();
+        if (context == null) return;
         PackageManager pm = context.getPackageManager();
         IPackageManager ipm = AppGlobals.getPackageManager();
+        mAppList.removeAll();
         Intent restrictionsIntent = new Intent(Intent.ACTION_GET_RESTRICTION_ENTRIES);
         final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(restrictionsIntent, 0);
         int i = 0;
@@ -464,14 +480,14 @@
             for (SelectableAppInfo app : mVisibleApps) {
                 String packageName = app.packageName;
                 if (packageName == null) continue;
-                final boolean isSettingsApp = packageName.equals(getActivity().getPackageName());
+                final boolean isSettingsApp = packageName.equals(context.getPackageName());
                 AppRestrictionsPreference p = new AppRestrictionsPreference(context, this);
                 final boolean hasSettings = resolveInfoListHasPackage(receivers, packageName);
                 p.setIcon(app.icon);
                 p.setChecked(false);
                 p.setTitle(app.activityName);
                 if (app.masterEntry != null) {
-                    p.setSummary(getActivity().getString(R.string.user_restrictions_controlled_by,
+                    p.setSummary(context.getString(R.string.user_restrictions_controlled_by,
                             app.masterEntry.activityName));
                 }
                 p.setKey(PKG_PREFIX + packageName);
@@ -563,7 +579,9 @@
 
     @Override
     public void onClick(View v) {
-        if (v.getTag() instanceof AppRestrictionsPreference) {
+        if (v == mHeaderView) {
+            showDialog(DIALOG_ID_EDIT_USER_INFO);
+        } else if (v.getTag() instanceof AppRestrictionsPreference) {
             AppRestrictionsPreference pref = (AppRestrictionsPreference) v.getTag();
             if (v.getId() == R.id.app_restrictions_settings) {
                 toggleAppPanel(pref);
@@ -624,12 +642,6 @@
                     }
                 }
             }
-        } else if (preference == mUserPreference) {
-            String userName = ((CharSequence) newValue).toString();
-            if (!TextUtils.isEmpty(userName)) {
-                mUserManager.setUserName(mUser.getIdentifier(), userName);
-                mUserPreference.setTitle(userName);
-            }
         }
         return true;
     }
@@ -840,8 +852,6 @@
                 mAppListChanged = true;
             }
             return true;
-        } else if (preference == mUserPreference) {
-            showDialog(DIALOG_ID_EDIT_USER_INFO);
         }
         return false;
     }
@@ -862,7 +872,7 @@
             userNameView.setText(info.name);
 
             final ImageView userPhotoView = (ImageView) content.findViewById(R.id.user_photo);
-            userPhotoView.setImageDrawable(mUserPreference.getIcon());
+            userPhotoView.setImageDrawable(mUserIconView.getDrawable());
 
             mEditUserPhotoController = new EditUserPhotoController(this, userPhotoView);
 
@@ -878,10 +888,11 @@
                             // Update the name if changed.
                             CharSequence userName = userNameView.getText();
                             if (!TextUtils.isEmpty(userName)) {
-                                CharSequence oldUserName = mUserPreference.getTitle();
+                                CharSequence oldUserName = mUserNameView.getText();
                                 if (oldUserName == null
                                         || !userName.toString().equals(oldUserName.toString())) {
-                                    mUserPreference.setTitle(userName);
+                                    ((TextView) mHeaderView.findViewById(android.R.id.title))
+                                            .setText(userName.toString());
                                     mUserManager.setUserName(mUser.getIdentifier(),
                                             userName.toString());
                                 }
@@ -889,8 +900,8 @@
                             // Update the photo if changed.
                             Drawable userPhoto = mEditUserPhotoController.getNewUserPhotoDrawable();
                             if (userPhoto != null
-                                    && !userPhoto.equals(mUserPreference.getIcon())) {
-                                mUserPreference.setIcon(userPhoto);
+                                    && !userPhoto.equals(mUserIconView.getDrawable())) {
+                                mUserIconView.setImageDrawable(userPhoto);
                                 new AsyncTask<Void, Void, Void>() {
                                     @Override
                                     protected Void doInBackground(Void... params) {
diff --git a/src/com/android/settings/users/RestrictionUtils.java b/src/com/android/settings/users/RestrictionUtils.java
index be62b3b..3ee6d51 100644
--- a/src/com/android/settings/users/RestrictionUtils.java
+++ b/src/com/android/settings/users/RestrictionUtils.java
@@ -34,21 +34,21 @@
 //        UserManager.DISALLOW_CONFIG_WIFI,
 //        UserManager.DISALLOW_CONFIG_BLUETOOTH,
         UserManager.DISALLOW_SHARE_LOCATION,
-        UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES
+//        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
+//        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
+//        R.string.install_unknown_applications
     };
 
     /**
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index 676951e..605daa5 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -45,6 +45,7 @@
 import android.preference.PreferenceGroup;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.Contacts;
+import android.provider.Settings.Secure;
 import android.util.Log;
 import android.util.SparseArray;
 import android.view.Menu;
@@ -305,7 +306,9 @@
         int userId = newUserInfo.id;
         UserHandle user = new UserHandle(userId);
         mUserManager.setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user);
-
+        mUserManager.setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true, user);
+        Secure.putStringForUser(getContentResolver(),
+                Secure.LOCATION_PROVIDERS_ALLOWED, "", userId);
         Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                 UserSettings.USER_DRAWABLES[
                         userId % UserSettings.USER_DRAWABLES.length]);