Merge "Update Data Usage Settings for Enterprise" into lmp-dev
diff --git a/res/layout/spinner_view.xml b/res/layout/spinner_view.xml
new file mode 100644
index 0000000..72c9673
--- /dev/null
+++ b/res/layout/spinner_view.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 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.
+-->
+
+<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/profile_spinner"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content" />
diff --git a/res/layout/user_preference.xml b/res/layout/user_preference.xml
new file mode 100644
index 0000000..c0a68e5
--- /dev/null
+++ b/res/layout/user_preference.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 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:id="@android:id/widget_frame"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal"
+        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
+
+    <ImageView
+            android:id="@+android:id/icon"
+            android:layout_width="@dimen/user_icon_diameter"
+            android:layout_height="@dimen/user_icon_diameter"
+            android:layout_gravity="center"
+            android:scaleType="fitCenter" />
+
+    <TextView
+            android:id="@+android:id/title"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:singleLine="true"
+            android:layout_gravity="center"
+            android:labelFor="@+android:id/icon"
+            android:ellipsize="marquee"
+            android:fadingEdge="horizontal"
+            style="@style/TextAppearance.Medium"/>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 4d06c1c..5db1671 100755
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -201,4 +201,7 @@
     <!-- Sim Card Name length -->
     <integer name="sim_name_length">32</integer>
 
+    <!-- Diameter of a round user icon -->
+    <dimen name="user_icon_diameter">56dp</dimen>
+
 </resources>
diff --git a/src/com/android/settings/UserSpinnerAdapter.java b/src/com/android/settings/UserSpinnerAdapter.java
new file mode 100644
index 0000000..c267f6f
--- /dev/null
+++ b/src/com/android/settings/UserSpinnerAdapter.java
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2014 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.content.Context;
+import android.content.pm.UserInfo;
+import android.database.DataSetObserver;
+import android.graphics.Bitmap;
+import android.graphics.drawable.Drawable;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.SpinnerAdapter;
+import android.widget.TextView;
+
+import com.android.settings.drawable.CircleFramedDrawable;
+
+import java.util.ArrayList;
+
+/**
+ * Adapter for a spinner that shows a list of users.
+ */
+public class UserSpinnerAdapter implements SpinnerAdapter {
+    // TODO: Update UI. See: http://b/16518801
+    /** Holder for user details */
+    public static class UserDetails {
+        private final UserHandle mUserHandle;
+        private final String name;
+        private final Drawable icon;
+
+        public UserDetails(UserHandle userHandle, UserManager um, Context context) {
+            mUserHandle = userHandle;
+            UserInfo userInfo = um.getUserInfo(mUserHandle.getIdentifier());
+            name = userInfo.name;
+            Bitmap bitmap = um.getUserIcon(userHandle.getIdentifier());
+            if (bitmap != null) {
+                icon = CircleFramedDrawable.getInstance(context, bitmap);
+            } else {
+                icon = null;
+            }
+        }
+    }
+    private ArrayList<UserDetails> data;
+    private final LayoutInflater mInflater;
+
+    public UserSpinnerAdapter(Context context, ArrayList<UserDetails> users) {
+        if (users == null) {
+            throw new IllegalArgumentException("A list of user details must be provided");
+        }
+        this.data = users;
+        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+    }
+
+    public UserHandle getUserHandle(int position) {
+        if (position < 0 || position >= data.size()) {
+            return null;
+        }
+        return data.get(position).mUserHandle;
+    }
+
+    @Override
+    public View getDropDownView(int position, View convertView, ViewGroup parent) {
+        final View row = convertView != null ? convertView : createUser(parent);
+
+        UserDetails user = data.get(position);
+        ((ImageView) row.findViewById(android.R.id.icon)).setImageDrawable(user.icon);
+        ((TextView) row.findViewById(android.R.id.title)).setText(user.name);
+        return row;
+    }
+
+    private View createUser(ViewGroup parent) {
+        return mInflater.inflate(R.layout.user_preference, parent, false);
+    }
+
+    @Override
+    public void registerDataSetObserver(DataSetObserver observer) {
+        // We don't support observers
+    }
+
+    @Override
+    public void unregisterDataSetObserver(DataSetObserver observer) {
+        // We don't support observers
+    }
+
+    @Override
+    public int getCount() {
+        return data.size();
+    }
+
+    @Override
+    public UserDetails getItem(int position) {
+        return data.get(position);
+    }
+
+    @Override
+    public long getItemId(int position) {
+        return data.get(position).mUserHandle.getIdentifier();
+    }
+
+    @Override
+    public boolean hasStableIds() {
+        return false;
+    }
+
+    @Override
+    public View getView(int position, View convertView, ViewGroup parent) {
+        return getDropDownView(position, convertView, parent);
+    }
+
+    @Override
+    public int getItemViewType(int position) {
+        return 0;
+    }
+
+    @Override
+    public int getViewTypeCount() {
+        return 1;
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return data.isEmpty();
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 558adde..feee90e 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -68,7 +68,7 @@
 
 import com.android.settings.dashboard.DashboardCategory;
 import com.android.settings.dashboard.DashboardTile;
-import com.android.settings.users.CircleFramedDrawable;
+import com.android.settings.drawable.CircleFramedDrawable;
 
 import java.io.IOException;
 import java.io.InputStream;
diff --git a/src/com/android/settings/accounts/AccountSettings.java b/src/com/android/settings/accounts/AccountSettings.java
index 88f47bc..47eb063 100644
--- a/src/com/android/settings/accounts/AccountSettings.java
+++ b/src/com/android/settings/accounts/AccountSettings.java
@@ -38,6 +38,7 @@
 import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceGroup;
+import android.preference.PreferenceScreen;
 
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
@@ -70,9 +71,12 @@
 
     private static final String ADD_ACCOUNT_ACTION = "android.settings.ADD_ACCOUNT_SETTINGS";
 
+    private static final ArrayList<String> EMPTY_LIST = new ArrayList<String>();
+
     private UserManager mUm;
     private SparseArray<ProfileData> mProfiles;
-    private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver;
+    private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver
+                = new ManagedProfileBroadcastReceiver();
     private boolean mIsSingleProfileUi = true;
 
     /**
@@ -102,7 +106,6 @@
         super.onCreate(savedInstanceState);
         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
         mProfiles = new SparseArray<ProfileData>(2);
-        updateUi();
         setHasOptionsMenu(true);
     }
 
@@ -149,6 +152,48 @@
             }
     }
 
+    @Override
+    public void onResume() {
+        super.onResume();
+        updateUi();
+        mManagedProfileBroadcastReceiver.register(getActivity());
+        listenToAccountUpdates();
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+        stopListeningToAccountUpdates();
+        mManagedProfileBroadcastReceiver.unregister(getActivity());
+        cleanUpPreferences();
+    }
+
+    @Override
+    public void onAccountsUpdate(UserHandle userHandle) {
+        final ProfileData profileData = mProfiles.get(userHandle.getIdentifier());
+        if (profileData != null) {
+            updateAccountTypes(profileData);
+        } else {
+            Log.w(TAG, "Missing Settings screen for: " + userHandle.getIdentifier());
+        }
+    }
+
+    @Override
+    public boolean onPreferenceClick(Preference preference) {
+        // Check the preference
+        final int count = mProfiles.size();
+        for (int i = 0; i < count; i++) {
+            ProfileData profileData = mProfiles.valueAt(i);
+            if (preference == profileData.addAccountPreference) {
+                Intent intent = new Intent(ADD_ACCOUNT_ACTION);
+                intent.putExtra(EXTRA_USER, profileData.userHandle);
+                startActivity(intent);
+                return true;
+            }
+        }
+        return false;
+    }
+
     void updateUi() {
         // Load the preferences from an XML resource
         addPreferencesFromResource(R.xml.account_settings);
@@ -169,13 +214,11 @@
             } else {
                 mIsSingleProfileUi = false;
                 updateProfileUi(currentProfile, KEY_CATEGORY_PERSONAL, KEY_ADD_ACCOUNT_PERSONAL,
-                        new ArrayList<String>());
+                        EMPTY_LIST);
                 final ArrayList<String> unusedPreferences = new ArrayList<String>(2);
                 unusedPreferences.add(KEY_ADD_ACCOUNT);
                 updateProfileUi(managedProfile, KEY_CATEGORY_WORK, KEY_ADD_ACCOUNT_WORK,
                         unusedPreferences);
-                mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver();
-                mManagedProfileBroadcastReceiver.register(getActivity());
             }
         }
         final int count = mProfiles.size();
@@ -210,36 +253,29 @@
         profileData.authenticatorHelper = new AuthenticatorHelper(
                 getActivity(), userHandle, mUm, this);
         mProfiles.put(userHandle.getIdentifier(), profileData);
-
-        profileData.authenticatorHelper.listenToAccountUpdates();
     }
 
-    @Override
-    public void onDestroy() {
-        super.onDestroy();
-        cleanUp();
-    }
-
-    void cleanUp() {
-        if (mManagedProfileBroadcastReceiver != null) {
-            mManagedProfileBroadcastReceiver.unregister(getActivity());
+    private void cleanUpPreferences() {
+        PreferenceScreen preferenceScreen = getPreferenceScreen();
+        if (preferenceScreen != null) {
+            preferenceScreen.removeAll();
         }
+    }
+
+    private void listenToAccountUpdates() {
+        final int count = mProfiles.size();
+        for (int i = 0; i < count; i++) {
+            mProfiles.valueAt(i).authenticatorHelper.listenToAccountUpdates();
+        }
+    }
+
+    private void stopListeningToAccountUpdates() {
         final int count = mProfiles.size();
         for (int i = 0; i < count; i++) {
             mProfiles.valueAt(i).authenticatorHelper.stopListeningToAccountUpdates();
         }
     }
 
-    @Override
-    public void onAccountsUpdate(UserHandle userHandle) {
-        final ProfileData profileData = mProfiles.get(userHandle.getIdentifier());
-        if (profileData != null) {
-            updateAccountTypes(profileData);
-        } else {
-            Log.w(TAG, "Missing Settings screen for: " + userHandle.getIdentifier());
-        }
-    }
-
     private void updateAccountTypes(ProfileData profileData) {
         profileData.preferenceGroup.removeAll();
         final ArrayList<AccountPreference> preferences = getAccountTypePreferences(
@@ -303,22 +339,6 @@
         return accountTypePreferences;
     }
 
-    @Override
-    public boolean onPreferenceClick(Preference preference) {
-        // Check the preference
-        final int count = mProfiles.size();
-        for (int i = 0; i < count; i++) {
-            ProfileData profileData = mProfiles.valueAt(i);
-            if (preference == profileData.addAccountPreference) {
-                Intent intent = new Intent(ADD_ACCOUNT_ACTION);
-                intent.putExtra(EXTRA_USER, profileData.userHandle);
-                startActivity(intent);
-                return true;
-            }
-        }
-        return false;
-    }
-
     private class AccountPreference extends Preference implements OnPreferenceClickListener {
         /**
          * Title of the tile that is shown to the user.
@@ -372,8 +392,12 @@
             if (intent.getAction().equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)
                     || intent.getAction().equals(Intent.ACTION_MANAGED_PROFILE_ADDED)) {
                 Log.v(TAG, "Received broadcast: " + intent.getAction());
-                cleanUp();
+                // Clean old state
+                stopListeningToAccountUpdates();
+                cleanUpPreferences();
+                // Build new state
                 updateUi();
+                listenToAccountUpdates();
                 return;
             }
             Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
diff --git a/src/com/android/settings/users/CircleFramedDrawable.java b/src/com/android/settings/drawable/CircleFramedDrawable.java
similarity index 99%
rename from src/com/android/settings/users/CircleFramedDrawable.java
rename to src/com/android/settings/drawable/CircleFramedDrawable.java
index 6423078..f68dace 100644
--- a/src/com/android/settings/users/CircleFramedDrawable.java
+++ b/src/com/android/settings/drawable/CircleFramedDrawable.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.settings.users;
+package com.android.settings.drawable;
 
 import android.content.Context;
 import android.content.res.Resources;
diff --git a/src/com/android/settings/print/PrintSettingsFragment.java b/src/com/android/settings/print/PrintSettingsFragment.java
index f883c7f..9fb42c5 100644
--- a/src/com/android/settings/print/PrintSettingsFragment.java
+++ b/src/com/android/settings/print/PrintSettingsFragment.java
@@ -16,6 +16,7 @@
 
 package com.android.settings.print;
 
+import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.LoaderManager.LoaderCallbacks;
 import android.content.AsyncTaskLoader;
@@ -31,6 +32,9 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.os.Process;
 import android.preference.Preference;
 import android.preference.PreferenceCategory;
 import android.preference.PreferenceScreen;
@@ -50,9 +54,12 @@
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AdapterView;
 import android.widget.TextView;
 
 import com.android.internal.content.PackageMonitor;
+import com.android.settings.UserSpinnerAdapter;
+import com.android.settings.UserSpinnerAdapter.UserDetails;
 import com.android.settings.DialogCreatable;
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
@@ -64,11 +71,14 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import android.widget.AdapterView.OnItemSelectedListener;
+import android.widget.Spinner;
+
 /**
  * Fragment with the top level print settings.
  */
 public class PrintSettingsFragment extends SettingsPreferenceFragment
-        implements DialogCreatable, Indexable {
+        implements DialogCreatable, Indexable, OnItemSelectedListener {
 
     private static final int LOADER_ID_PRINT_JOBS_LOADER = 1;
 
@@ -113,6 +123,14 @@
     private PreferenceCategory mPrintServicesCategory;
 
     private PrintJobsController mPrintJobsController;
+    private Context mContext;
+    private UserSpinnerAdapter mProfileSpinnerAdapter;
+
+    @Override
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+        mContext = activity;
+    }
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -169,6 +187,27 @@
         textView.setText(R.string.print_no_services_installed);
         contentRoot.addView(emptyView);
         getListView().setEmptyView(emptyView);
+
+        final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
+        List<UserHandle> userProfiles = um.getUserProfiles();
+        if (userProfiles.size() >= 2) {
+            Spinner spinner = (Spinner) getActivity().getLayoutInflater().inflate(
+                    R.layout.spinner_view, null);
+
+            UserHandle myUserHandle = Process.myUserHandle();
+            userProfiles.remove(myUserHandle);
+            userProfiles.add(0, myUserHandle);
+            ArrayList<UserDetails> userDetails = new ArrayList<UserDetails>(userProfiles.size());
+            final int count = userProfiles.size();
+            for (int i = 0; i < count; i++) {
+                userDetails.add(new UserDetails(userProfiles.get(i), um, mContext));
+            }
+
+            mProfileSpinnerAdapter = new UserSpinnerAdapter(mContext, userDetails);
+            spinner.setAdapter(mProfileSpinnerAdapter);
+            spinner.setOnItemSelectedListener(this);
+            setPinnedHeaderView(spinner);
+        }
     }
 
     private void updateServicesPreferences() {
@@ -271,6 +310,22 @@
         }
     }
 
+    @Override
+    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
+        UserHandle selectedUser = mProfileSpinnerAdapter.getUserHandle(position);
+        if (selectedUser.getIdentifier() != UserHandle.myUserId()) {
+            Intent intent = new Intent(Settings.ACTION_PRINT_SETTINGS);
+            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            mContext.startActivityAsUser(intent, selectedUser);
+            getActivity().finish();
+        }
+    }
+
+    @Override
+    public void onNothingSelected(AdapterView<?> parent) {
+        // Nothing to do
+    }
+
     private class SettingsPackageMonitor extends PackageMonitor {
         @Override
         public void onPackageAdded(String packageName, int uid) {
@@ -565,4 +620,4 @@
             return indexables;
         }
     };
-}
\ No newline at end of file
+}
diff --git a/src/com/android/settings/users/AppRestrictionsFragment.java b/src/com/android/settings/users/AppRestrictionsFragment.java
index 70b6ce9..0518f56 100644
--- a/src/com/android/settings/users/AppRestrictionsFragment.java
+++ b/src/com/android/settings/users/AppRestrictionsFragment.java
@@ -17,7 +17,6 @@
 package com.android.settings.users;
 
 import android.app.Activity;
-import android.app.AppGlobals;
 import android.appwidget.AppWidgetManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -57,13 +56,13 @@
 import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodManager;
 import android.view.ViewGroup;
-import android.widget.CheckBox;
 import android.widget.CompoundButton;
 import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.Switch;
 
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.drawable.CircleFramedDrawable;
 
 import java.util.ArrayList;
 import java.util.Collections;
diff --git a/src/com/android/settings/users/EditUserInfoController.java b/src/com/android/settings/users/EditUserInfoController.java
index 53aaf27..0f844a7 100644
--- a/src/com/android/settings/users/EditUserInfoController.java
+++ b/src/com/android/settings/users/EditUserInfoController.java
@@ -37,6 +37,7 @@
 import android.widget.ImageView;
 
 import com.android.settings.R;
+import com.android.settings.drawable.CircleFramedDrawable;
 
 /**
  * This class encapsulates a Dialog for editing the user nickname and photo.
diff --git a/src/com/android/settings/users/EditUserPhotoController.java b/src/com/android/settings/users/EditUserPhotoController.java
index 5ed3560..538f332 100644
--- a/src/com/android/settings/users/EditUserPhotoController.java
+++ b/src/com/android/settings/users/EditUserPhotoController.java
@@ -44,6 +44,7 @@
 import android.widget.ListPopupWindow;
 
 import com.android.settings.R;
+import com.android.settings.drawable.CircleFramedDrawable;
 
 import java.io.File;
 import java.io.FileNotFoundException;
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index 6f5a669..7dc83ef 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -37,7 +37,6 @@
 import android.content.pm.UserInfo;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.drawable.Drawable;
 import android.os.AsyncTask;
@@ -69,6 +68,7 @@
 import com.android.settings.SettingsActivity;
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.Utils;
+import com.android.settings.drawable.CircleFramedDrawable;
 
 /**
  * Screen that manages the list of users on the device.