blob: 2d583274ad2a9a3a4cc6241b33c60f6167df3c04 [file] [log] [blame]
Amith Yamasanib810a0d2012-03-25 10:12:26 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.users;
18
Geoffrey Borggaardfe21d9a2013-08-02 18:16:27 -040019import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.List;
22
Amith Yamasani233592b2013-03-01 17:05:23 -080023import android.accounts.Account;
24import android.accounts.AccountManager;
Amith Yamasanif162efe2013-04-29 10:43:05 -070025import android.app.Activity;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070026import android.app.ActivityManagerNative;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070027import android.app.AlertDialog;
28import android.app.Dialog;
Amith Yamasanif162efe2013-04-29 10:43:05 -070029import android.app.admin.DevicePolicyManager;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070030import android.content.BroadcastReceiver;
Amith Yamasani32630392012-08-10 19:31:39 -070031import android.content.Context;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070032import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Svetoslav Ganov263f7442012-09-25 16:11:16 -070035import android.content.SharedPreferences;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070036import android.content.pm.UserInfo;
Amith Yamasani5cdc9372012-11-14 16:23:40 -080037import android.content.res.Resources;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070038import android.graphics.Bitmap;
39import android.graphics.BitmapFactory;
Amith Yamasani48fa8572012-04-17 17:12:01 -070040import android.graphics.drawable.Drawable;
Amith Yamasani32dccbc2012-09-13 18:06:49 -070041import android.os.AsyncTask;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070042import android.os.Bundle;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070043import android.os.Handler;
44import android.os.Message;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070045import android.os.RemoteException;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070046import android.os.UserHandle;
Amith Yamasani32630392012-08-10 19:31:39 -070047import android.os.UserManager;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070048import android.preference.Preference;
49import android.preference.Preference.OnPreferenceClickListener;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070050import android.preference.PreferenceGroup;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070051import android.provider.ContactsContract;
52import android.provider.ContactsContract.Contacts;
Amith Yamasani1bad7142013-04-22 13:37:58 -070053import android.provider.Settings.Secure;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070054import android.util.Log;
Amith Yamasanif34a85d2012-09-17 18:31:45 -070055import android.util.SparseArray;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070056import android.view.Menu;
57import android.view.MenuInflater;
58import android.view.MenuItem;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070059import android.view.View;
60import android.view.View.OnClickListener;
Amith Yamasanifc8a3832013-04-23 11:44:59 -070061import android.widget.SimpleAdapter;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070062
Amith Yamasanif162efe2013-04-29 10:43:05 -070063import com.android.internal.widget.LockPatternUtils;
64import com.android.settings.ChooseLockGeneric;
Amith Yamasanid4abdd22013-04-17 18:27:01 -070065import com.android.settings.OwnerInfoSettings;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070066import com.android.settings.R;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070067import com.android.settings.SelectableEditTextPreference;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080068import com.android.settings.SettingsActivity;
Julia Reynoldsb9195d22014-06-11 16:18:16 -040069import com.android.settings.SettingsPreferenceFragment;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070070import com.android.settings.Utils;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070071
Julia Reynoldsb9195d22014-06-11 16:18:16 -040072public class UserSettings extends SettingsPreferenceFragment
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070073 implements OnPreferenceClickListener, OnClickListener, DialogInterface.OnDismissListener,
74 Preference.OnPreferenceChangeListener {
Amith Yamasanib810a0d2012-03-25 10:12:26 -070075
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070076 private static final String TAG = "UserSettings";
77
Amith Yamasanic861cf82012-10-02 14:51:46 -070078 /** UserId of the user being removed */
79 private static final String SAVE_REMOVING_USER = "removing_user";
80 /** UserId of the user that was just added */
81 private static final String SAVE_ADDING_USER = "adding_user";
82
Amith Yamasanifc8a3832013-04-23 11:44:59 -070083 private static final String KEY_USER_LIST = "user_list";
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070084 private static final String KEY_USER_ME = "user_me";
Amith Yamasanifc8a3832013-04-23 11:44:59 -070085 private static final String KEY_ADD_USER = "user_add";
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070086
Amith Yamasanicc395692013-04-11 18:21:07 -070087 private static final int MENU_REMOVE_USER = Menu.FIRST;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070088
89 private static final int DIALOG_CONFIRM_REMOVE = 1;
Amith Yamasanifc8a3832013-04-23 11:44:59 -070090 private static final int DIALOG_ADD_USER = 2;
91 private static final int DIALOG_SETUP_USER = 3;
92 private static final int DIALOG_SETUP_PROFILE = 4;
Amith Yamasanicc395692013-04-11 18:21:07 -070093 private static final int DIALOG_USER_CANNOT_MANAGE = 5;
Amith Yamasanifc8a3832013-04-23 11:44:59 -070094 private static final int DIALOG_CHOOSE_USER_TYPE = 6;
Amith Yamasanif162efe2013-04-29 10:43:05 -070095 private static final int DIALOG_NEED_LOCKSCREEN = 7;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070096
97 private static final int MESSAGE_UPDATE_LIST = 1;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070098 private static final int MESSAGE_SETUP_USER = 2;
Amith Yamasanicc395692013-04-11 18:21:07 -070099 private static final int MESSAGE_CONFIG_USER = 3;
100
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700101 private static final int USER_TYPE_USER = 1;
102 private static final int USER_TYPE_RESTRICTED_PROFILE = 2;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700103
Amith Yamasanif162efe2013-04-29 10:43:05 -0700104 private static final int REQUEST_CHOOSE_LOCK = 10;
105
Svetoslav Ganov263f7442012-09-25 16:11:16 -0700106 private static final String KEY_ADD_USER_LONG_MESSAGE_DISPLAYED =
107 "key_add_user_long_message_displayed";
108
Amith Yamasani233592b2013-03-01 17:05:23 -0800109 static final int[] USER_DRAWABLES = {
Amith Yamasani95dc4732012-10-02 21:10:48 -0700110 R.drawable.avatar_default_1,
111 R.drawable.avatar_default_2,
112 R.drawable.avatar_default_3,
113 R.drawable.avatar_default_4,
114 R.drawable.avatar_default_5,
115 R.drawable.avatar_default_6,
116 R.drawable.avatar_default_7,
117 R.drawable.avatar_default_8
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700118 };
119
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700120 private static final String KEY_TITLE = "title";
121 private static final String KEY_SUMMARY = "summary";
122
123 private PreferenceGroup mUserListCategory;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700124 private Preference mMePreference;
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700125 private SelectableEditTextPreference mNicknamePreference;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700126 private Preference mAddUser;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700127 private int mRemovingUserId = -1;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700128 private int mAddedUserId = 0;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700129 private boolean mAddingUser;
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700130 private boolean mProfileExists;
Amith Yamasanicfb8d7f2014-03-26 14:46:47 -0700131 private boolean mEnabled = true;
Amith Yamasani0e43d322014-05-07 10:19:34 -0700132 private boolean mCanAddRestrictedProfile = true;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700133
134 private final Object mUserLock = new Object();
135 private UserManager mUserManager;
Amith Yamasani864e3b72013-03-27 12:27:01 -0700136 private SparseArray<Bitmap> mUserIcons = new SparseArray<Bitmap>();
Amith Yamasani8666b9e2012-09-27 14:50:13 -0700137 private boolean mIsOwner = UserHandle.myUserId() == UserHandle.USER_OWNER;
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530138 private boolean mIsGuest;
Amith Yamasani8666b9e2012-09-27 14:50:13 -0700139
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700140 private Handler mHandler = new Handler() {
141 @Override
142 public void handleMessage(Message msg) {
143 switch (msg.what) {
144 case MESSAGE_UPDATE_LIST:
145 updateUserList();
146 break;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700147 case MESSAGE_SETUP_USER:
148 onUserCreated(msg.arg1);
149 break;
Amith Yamasanicc395692013-04-11 18:21:07 -0700150 case MESSAGE_CONFIG_USER:
151 onManageUserClicked(msg.arg1, true);
152 break;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700153 }
154 }
155 };
156
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700157 private BroadcastReceiver mUserChangeReceiver = new BroadcastReceiver() {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700158 @Override
159 public void onReceive(Context context, Intent intent) {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700160 if (intent.getAction().equals(Intent.ACTION_USER_REMOVED)) {
161 mRemovingUserId = -1;
162 } else if (intent.getAction().equals(Intent.ACTION_USER_INFO_CHANGED)) {
163 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
164 if (userHandle != -1) {
165 mUserIcons.remove(userHandle);
166 }
167 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700168 mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
169 }
170 };
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700171
172 @Override
173 public void onCreate(Bundle icicle) {
174 super.onCreate(icicle);
Amith Yamasani8666b9e2012-09-27 14:50:13 -0700175
Amith Yamasanic861cf82012-10-02 14:51:46 -0700176 if (icicle != null) {
177 if (icicle.containsKey(SAVE_ADDING_USER)) {
178 mAddedUserId = icicle.getInt(SAVE_ADDING_USER);
179 }
180 if (icicle.containsKey(SAVE_REMOVING_USER)) {
181 mRemovingUserId = icicle.getInt(SAVE_REMOVING_USER);
182 }
183 }
Amith Yamasani0e43d322014-05-07 10:19:34 -0700184 final Context context = getActivity();
185 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
Amith Yamasani4093e402014-06-06 14:31:37 -0700186 boolean hasMultipleUsers = mUserManager.getUserCount() > 1;
187 if ((!UserManager.supportsMultipleUsers() && !hasMultipleUsers)
188 || Utils.isMonkeyRunning()) {
Amith Yamasanicfb8d7f2014-03-26 14:46:47 -0700189 mEnabled = false;
190 return;
191 }
Amith Yamasani0e43d322014-05-07 10:19:34 -0700192
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530193 final int myUserId = UserHandle.myUserId();
194 mIsGuest = mUserManager.getUserInfo(myUserId).isGuest();
195
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700196 addPreferencesFromResource(R.xml.user_settings);
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700197 mUserListCategory = (PreferenceGroup) findPreference(KEY_USER_LIST);
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530198 mMePreference = new UserPreference(context, null, myUserId,
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700199 mUserManager.isLinkedUser() ? null : this, null);
200 mMePreference.setKey(KEY_USER_ME);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700201 mMePreference.setOnPreferenceClickListener(this);
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700202 if (mIsOwner) {
203 mMePreference.setSummary(R.string.user_owner);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700204 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700205 mAddUser = findPreference(KEY_ADD_USER);
Amith Yamasani4093e402014-06-06 14:31:37 -0700206 if (!mIsOwner || UserManager.getMaxSupportedUsers() < 2
Julia Reynoldsb9195d22014-06-11 16:18:16 -0400207 || !UserManager.supportsMultipleUsers()
208 || mUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER)) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700209 removePreference(KEY_ADD_USER);
Amith Yamasani0e43d322014-05-07 10:19:34 -0700210 } else {
211 mAddUser.setOnPreferenceClickListener(this);
212 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
213 Context.DEVICE_POLICY_SERVICE);
Amith Yamasani51c6dac2014-07-02 00:06:37 +0530214 // No restricted profiles for tablets with a device owner, or phones.
215 if (dpm.getDeviceOwner() != null || Utils.isVoiceCapable(context)) {
Amith Yamasani0e43d322014-05-07 10:19:34 -0700216 mCanAddRestrictedProfile = false;
217 mAddUser.setTitle(R.string.user_add_user_menu);
218 }
Amith Yamasani233592b2013-03-01 17:05:23 -0800219 }
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700220 loadProfile();
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700221 setHasOptionsMenu(true);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700222 IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED);
223 filter.addAction(Intent.ACTION_USER_INFO_CHANGED);
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530224 context.registerReceiverAsUser(mUserChangeReceiver, UserHandle.ALL, filter, null,
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700225 mHandler);
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700226 }
227
228 @Override
229 public void onResume() {
230 super.onResume();
Amith Yamasanicfb8d7f2014-03-26 14:46:47 -0700231
232 if (!mEnabled) return;
233
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700234 loadProfile();
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700235 updateUserList();
236 }
237
238 @Override
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700239 public void onDestroy() {
240 super.onDestroy();
Amith Yamasanicfb8d7f2014-03-26 14:46:47 -0700241
242 if (!mEnabled) return;
243
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700244 getActivity().unregisterReceiver(mUserChangeReceiver);
245 }
246
247 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700248 public void onSaveInstanceState(Bundle outState) {
249 super.onSaveInstanceState(outState);
250
251 outState.putInt(SAVE_ADDING_USER, mAddedUserId);
252 outState.putInt(SAVE_REMOVING_USER, mRemovingUserId);
253 }
254
255 @Override
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700256 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Emily Bernier11bd33a2013-04-11 16:24:56 -0400257 UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
258 if (!mIsOwner && !um.hasUserRestriction(UserManager.DISALLOW_REMOVE_USER)) {
Amith Yamasani1a634d32012-09-27 23:01:35 -0700259 String nickname = mUserManager.getUserName();
260 MenuItem removeThisUser = menu.add(0, MENU_REMOVE_USER, 0,
261 getResources().getString(R.string.user_remove_user_menu, nickname));
262 removeThisUser.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700263 }
Amith Yamasani38223dc2012-09-18 09:51:07 -0700264 super.onCreateOptionsMenu(menu, inflater);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700265 }
266
267 @Override
268 public boolean onOptionsItemSelected(MenuItem item) {
269 final int itemId = item.getItemId();
Amith Yamasanicc395692013-04-11 18:21:07 -0700270 if (itemId == MENU_REMOVE_USER) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700271 onRemoveUserClicked(UserHandle.myUserId());
272 return true;
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700273 } else {
274 return super.onOptionsItemSelected(item);
275 }
276 }
277
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700278 private void loadProfile() {
279 mProfileExists = false;
280 new AsyncTask<Void, Void, String>() {
281 @Override
282 protected void onPostExecute(String result) {
283 finishLoadProfile(result);
284 }
285
286 @Override
287 protected String doInBackground(Void... values) {
288 UserInfo user = mUserManager.getUserInfo(UserHandle.myUserId());
289 if (user.iconPath == null || user.iconPath.equals("")) {
290 assignProfilePhoto(user);
291 }
292 String profileName = getProfileName();
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700293 if (profileName == null) {
294 profileName = user.name;
295 }
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700296 return profileName;
297 }
298 }.execute();
299 }
300
301 private void finishLoadProfile(String profileName) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700302 if (getActivity() == null) return;
303 mMePreference.setTitle(getString(R.string.user_you, profileName));
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700304 int myUserId = UserHandle.myUserId();
305 Bitmap b = mUserManager.getUserIcon(myUserId);
306 if (b != null) {
Amith Yamasani864e3b72013-03-27 12:27:01 -0700307 mMePreference.setIcon(encircle(b));
308 mUserIcons.put(myUserId, b);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700309 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700310 }
311
Amith Yamasanif162efe2013-04-29 10:43:05 -0700312 private boolean hasLockscreenSecurity() {
313 LockPatternUtils lpu = new LockPatternUtils(getActivity());
314 return lpu.isLockPasswordEnabled() || lpu.isLockPatternEnabled();
315 }
316
317 private void launchChooseLockscreen() {
318 Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
319 chooseLockIntent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY,
320 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
321 startActivityForResult(chooseLockIntent, REQUEST_CHOOSE_LOCK);
322 }
323
324 @Override
325 public void onActivityResult(int requestCode, int resultCode, Intent data) {
326 super.onActivityResult(requestCode, resultCode, data);
327
328 if (requestCode == REQUEST_CHOOSE_LOCK) {
329 if (resultCode != Activity.RESULT_CANCELED && hasLockscreenSecurity()) {
330 addUserNow(USER_TYPE_RESTRICTED_PROFILE);
Amith Yamasanif162efe2013-04-29 10:43:05 -0700331 }
332 }
333 }
334
Amith Yamasanicc395692013-04-11 18:21:07 -0700335 private void onAddUserClicked(int userType) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700336 synchronized (mUserLock) {
337 if (mRemovingUserId == -1 && !mAddingUser) {
Amith Yamasanicc395692013-04-11 18:21:07 -0700338 switch (userType) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700339 case USER_TYPE_USER:
340 showDialog(DIALOG_ADD_USER);
Amith Yamasanicc395692013-04-11 18:21:07 -0700341 break;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700342 case USER_TYPE_RESTRICTED_PROFILE:
Amith Yamasanif162efe2013-04-29 10:43:05 -0700343 if (hasLockscreenSecurity()) {
344 addUserNow(USER_TYPE_RESTRICTED_PROFILE);
345 } else {
346 showDialog(DIALOG_NEED_LOCKSCREEN);
347 }
Amith Yamasanicc395692013-04-11 18:21:07 -0700348 break;
349 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700350 }
351 }
352 }
353
354 private void onRemoveUserClicked(int userId) {
355 synchronized (mUserLock) {
356 if (mRemovingUserId == -1 && !mAddingUser) {
357 mRemovingUserId = userId;
358 showDialog(DIALOG_CONFIRM_REMOVE);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700359 }
360 }
361 }
362
Amith Yamasanicc395692013-04-11 18:21:07 -0700363 private UserInfo createLimitedUser() {
364 UserInfo newUserInfo = mUserManager.createUser(
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700365 getResources().getString(R.string.user_new_profile_name),
Amith Yamasanicc395692013-04-11 18:21:07 -0700366 UserInfo.FLAG_RESTRICTED);
367 int userId = newUserInfo.id;
368 UserHandle user = new UserHandle(userId);
369 mUserManager.setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user);
Amith Yamasani1bad7142013-04-22 13:37:58 -0700370 mUserManager.setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true, user);
Tom O'Neilldad115e2014-03-04 10:59:29 -0800371 Secure.putIntForUser(getContentResolver(),
372 Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF, userId);
Amith Yamasanicc395692013-04-11 18:21:07 -0700373 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
374 UserSettings.USER_DRAWABLES[
375 userId % UserSettings.USER_DRAWABLES.length]);
376 mUserManager.setUserIcon(userId, bitmap);
377 // Add shared accounts
378 AccountManager am = AccountManager.get(getActivity());
379 Account [] accounts = am.getAccounts();
380 if (accounts != null) {
381 for (Account account : accounts) {
382 am.addSharedAccount(account, user);
383 }
384 }
385 return newUserInfo;
386 }
387
388 private UserInfo createTrustedUser() {
389 UserInfo newUserInfo = mUserManager.createUser(
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700390 getResources().getString(R.string.user_new_user_name), 0);
Amith Yamasanicc395692013-04-11 18:21:07 -0700391 if (newUserInfo != null) {
392 assignDefaultPhoto(newUserInfo);
393 }
394 return newUserInfo;
395 }
396
397 private void onManageUserClicked(int userId, boolean newUser) {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530398 if (userId == UserPreference.USERID_GUEST_DEFAULTS) {
399 Bundle extras = new Bundle();
400 extras.putBoolean(UserDetailsSettings.EXTRA_USER_GUEST, true);
401 ((SettingsActivity) getActivity()).startPreferencePanel(
402 UserDetailsSettings.class.getName(),
403 extras, R.string.user_guest, null, null, 0);
404 return;
405 }
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700406 UserInfo info = mUserManager.getUserInfo(userId);
407 if (info.isRestricted() && mIsOwner) {
408 Bundle extras = new Bundle();
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700409 extras.putInt(RestrictedProfileSettings.EXTRA_USER_ID, userId);
410 extras.putBoolean(RestrictedProfileSettings.EXTRA_NEW_USER, newUser);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800411 ((SettingsActivity) getActivity()).startPreferencePanel(
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700412 RestrictedProfileSettings.class.getName(),
Amith Yamasani7c4dfcc2013-05-22 12:51:30 -0700413 extras, R.string.user_restrictions_title, null,
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700414 null, 0);
415 } else if (info.id == UserHandle.myUserId()) {
416 // Jump to owner info panel
417 Bundle extras = new Bundle();
Amith Yamasanib0171712013-04-28 22:13:22 -0700418 if (!info.isRestricted()) {
419 extras.putBoolean(OwnerInfoSettings.EXTRA_SHOW_NICKNAME, true);
420 }
421 int titleResId = info.id == UserHandle.USER_OWNER ? R.string.owner_info_settings_title
422 : (info.isRestricted() ? R.string.profile_info_settings_title
423 : R.string.user_info_settings_title);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800424 ((SettingsActivity) getActivity()).startPreferencePanel(
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700425 OwnerInfoSettings.class.getName(),
Amith Yamasanib0171712013-04-28 22:13:22 -0700426 extras, titleResId, null, null, 0);
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530427 } else if (mIsOwner) {
428 Bundle extras = new Bundle();
429 extras.putInt(UserDetailsSettings.EXTRA_USER_ID, userId);
430 ((SettingsActivity) getActivity()).startPreferencePanel(
431 UserDetailsSettings.class.getName(),
Amith Yamasani51c6dac2014-07-02 00:06:37 +0530432 extras,
433 -1, /* No title res id */
434 info.name, /* title */
435 null, /* resultTo */
436 0 /* resultRequestCode */);
Amith Yamasanicc395692013-04-11 18:21:07 -0700437 }
Amith Yamasani233592b2013-03-01 17:05:23 -0800438 }
439
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700440 private void onUserCreated(int userId) {
441 mAddedUserId = userId;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700442 if (mUserManager.getUserInfo(userId).isRestricted()) {
443 showDialog(DIALOG_SETUP_PROFILE);
444 } else {
445 showDialog(DIALOG_SETUP_USER);
446 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700447 }
448
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700449 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700450 public void onDialogShowing() {
451 super.onDialogShowing();
452
453 setOnDismissListener(this);
454 }
455
456 @Override
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700457 public Dialog onCreateDialog(int dialogId) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700458 Context context = getActivity();
459 if (context == null) return null;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700460 switch (dialogId) {
Amith Yamasanic861cf82012-10-02 14:51:46 -0700461 case DIALOG_CONFIRM_REMOVE: {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530462 Dialog dlg =
Amith Yamasani51c6dac2014-07-02 00:06:37 +0530463 Utils.createRemoveConfirmationDialog(getActivity(), mRemovingUserId,
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530464 new DialogInterface.OnClickListener() {
465 public void onClick(DialogInterface dialog, int which) {
466 removeUserNow();
467 }
468 }
469 );
Amith Yamasanic861cf82012-10-02 14:51:46 -0700470 return dlg;
471 }
Amith Yamasani1a634d32012-09-27 23:01:35 -0700472 case DIALOG_USER_CANNOT_MANAGE:
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700473 return new AlertDialog.Builder(context)
Amith Yamasani1a634d32012-09-27 23:01:35 -0700474 .setMessage(R.string.user_cannot_manage_message)
475 .setPositiveButton(android.R.string.ok, null)
476 .create();
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700477 case DIALOG_ADD_USER: {
Svetoslav Ganov263f7442012-09-25 16:11:16 -0700478 final SharedPreferences preferences = getActivity().getPreferences(
479 Context.MODE_PRIVATE);
480 final boolean longMessageDisplayed = preferences.getBoolean(
481 KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, false);
482 final int messageResId = longMessageDisplayed
483 ? R.string.user_add_user_message_short
484 : R.string.user_add_user_message_long;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700485 final int userType = dialogId == DIALOG_ADD_USER
486 ? USER_TYPE_USER : USER_TYPE_RESTRICTED_PROFILE;
487 Dialog dlg = new AlertDialog.Builder(context)
488 .setTitle(R.string.user_add_user_title)
489 .setMessage(messageResId)
490 .setPositiveButton(android.R.string.ok,
491 new DialogInterface.OnClickListener() {
492 public void onClick(DialogInterface dialog, int which) {
493 addUserNow(userType);
494 if (!longMessageDisplayed) {
495 preferences.edit().putBoolean(
496 KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, true).apply();
497 }
Svetoslav Ganov263f7442012-09-25 16:11:16 -0700498 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700499 })
500 .setNegativeButton(android.R.string.cancel, null)
501 .create();
Amith Yamasanic861cf82012-10-02 14:51:46 -0700502 return dlg;
503 }
504 case DIALOG_SETUP_USER: {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700505 Dialog dlg = new AlertDialog.Builder(context)
506 .setTitle(R.string.user_setup_dialog_title)
507 .setMessage(R.string.user_setup_dialog_message)
508 .setPositiveButton(R.string.user_setup_button_setup_now,
509 new DialogInterface.OnClickListener() {
510 public void onClick(DialogInterface dialog, int which) {
511 switchUserNow(mAddedUserId);
512 }
513 })
514 .setNegativeButton(R.string.user_setup_button_setup_later, null)
515 .create();
516 return dlg;
517 }
518 case DIALOG_SETUP_PROFILE: {
519 Dialog dlg = new AlertDialog.Builder(context)
520 .setMessage(R.string.user_setup_profile_dialog_message)
521 .setPositiveButton(android.R.string.ok,
522 new DialogInterface.OnClickListener() {
523 public void onClick(DialogInterface dialog, int which) {
524 switchUserNow(mAddedUserId);
525 }
526 })
527 .setNegativeButton(android.R.string.cancel, null)
528 .create();
529 return dlg;
530 }
531 case DIALOG_CHOOSE_USER_TYPE: {
532 List<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
533 HashMap<String,String> addUserItem = new HashMap<String,String>();
534 addUserItem.put(KEY_TITLE, getString(R.string.user_add_user_item_title));
535 addUserItem.put(KEY_SUMMARY, getString(R.string.user_add_user_item_summary));
536 HashMap<String,String> addProfileItem = new HashMap<String,String>();
537 addProfileItem.put(KEY_TITLE, getString(R.string.user_add_profile_item_title));
538 addProfileItem.put(KEY_SUMMARY, getString(R.string.user_add_profile_item_summary));
539 data.add(addUserItem);
540 data.add(addProfileItem);
541 Dialog dlg = new AlertDialog.Builder(context)
542 .setTitle(R.string.user_add_user_type_title)
543 .setAdapter(new SimpleAdapter(context, data, R.layout.two_line_list_item,
544 new String[] {KEY_TITLE, KEY_SUMMARY},
545 new int[] {R.id.title, R.id.summary}),
546 new DialogInterface.OnClickListener() {
547 public void onClick(DialogInterface dialog, int which) {
548 onAddUserClicked(which == 0
549 ? USER_TYPE_USER
550 : USER_TYPE_RESTRICTED_PROFILE);
551 }
552 })
553 .create();
Amith Yamasanic861cf82012-10-02 14:51:46 -0700554 return dlg;
555 }
Amith Yamasanif162efe2013-04-29 10:43:05 -0700556 case DIALOG_NEED_LOCKSCREEN: {
557 Dialog dlg = new AlertDialog.Builder(context)
558 .setMessage(R.string.user_need_lock_message)
559 .setPositiveButton(R.string.user_set_lock_button,
560 new DialogInterface.OnClickListener() {
561 @Override
562 public void onClick(DialogInterface dialog, int which) {
563 launchChooseLockscreen();
564 }
565 })
566 .setNegativeButton(android.R.string.cancel, null)
567 .create();
568 return dlg;
569 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700570 default:
571 return null;
572 }
573 }
574
575 private void removeUserNow() {
576 if (mRemovingUserId == UserHandle.myUserId()) {
577 removeThisUser();
578 } else {
579 new Thread() {
580 public void run() {
581 synchronized (mUserLock) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700582 mUserManager.removeUser(mRemovingUserId);
583 mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700584 }
585 }
586 }.start();
587 }
588 }
589
590 private void removeThisUser() {
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700591 try {
592 ActivityManagerNative.getDefault().switchUser(UserHandle.USER_OWNER);
593 ((UserManager) getActivity().getSystemService(Context.USER_SERVICE))
594 .removeUser(UserHandle.myUserId());
595 } catch (RemoteException re) {
596 Log.e(TAG, "Unable to remove self user");
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700597 }
598 }
599
Amith Yamasanicc395692013-04-11 18:21:07 -0700600 private void addUserNow(final int userType) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700601 synchronized (mUserLock) {
602 mAddingUser = true;
Amith Yamasania98c4022013-04-19 15:27:52 -0700603 //updateUserList();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700604 new Thread() {
605 public void run() {
Amith Yamasanicc395692013-04-11 18:21:07 -0700606 UserInfo user = null;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700607 // Could take a few seconds
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700608 if (userType == USER_TYPE_USER) {
Amith Yamasanicc395692013-04-11 18:21:07 -0700609 user = createTrustedUser();
610 } else {
611 user = createLimitedUser();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700612 }
613 synchronized (mUserLock) {
614 mAddingUser = false;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700615 if (userType == USER_TYPE_USER) {
Amith Yamasania98c4022013-04-19 15:27:52 -0700616 mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
Amith Yamasanicc395692013-04-11 18:21:07 -0700617 mHandler.sendMessage(mHandler.obtainMessage(
618 MESSAGE_SETUP_USER, user.id, user.serialNumber));
619 } else {
620 mHandler.sendMessage(mHandler.obtainMessage(
621 MESSAGE_CONFIG_USER, user.id, user.serialNumber));
622 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700623 }
624 }
625 }.start();
626 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700627 }
628
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700629 private void switchUserNow(int userId) {
630 try {
631 ActivityManagerNative.getDefault().switchUser(userId);
632 } catch (RemoteException re) {
633 // Nothing to do
634 }
635 }
636
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700637 private void updateUserList() {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700638 if (getActivity() == null) return;
Amith Yamasanie278eda2012-09-20 22:44:48 -0700639 List<UserInfo> users = mUserManager.getUsers(true);
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530640 final Context context = getActivity();
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700641
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700642 mUserListCategory.removeAll();
643 mUserListCategory.setOrderingAsAdded(false);
644 mUserListCategory.addPreference(mMePreference);
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700645
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530646 final boolean voiceCapable = Utils.isVoiceCapable(context);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700647 final ArrayList<Integer> missingIcons = new ArrayList<Integer>();
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700648 for (UserInfo user : users) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700649 Preference pref;
Amith Yamasanic861cf82012-10-02 14:51:46 -0700650 if (user.id == UserHandle.myUserId()) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700651 pref = mMePreference;
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530652 } else if (user.isGuest()) {
653 // Skip over Guest. We add generic Guest settings after this loop
654 continue;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700655 } else {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530656 // With Telephony:
657 // Secondary user: Settings
658 // Guest: Settings
659 // Restricted Profile: There is no Restricted Profile
660 // Without Telephony:
661 // Secondary user: Delete
662 // Guest: Nothing
663 // Restricted Profile: Settings
664 final boolean showSettings = mIsOwner && (voiceCapable || user.isRestricted());
665 final boolean showDelete = mIsOwner
666 && (!voiceCapable && !user.isRestricted() && !user.isGuest());
667 pref = new UserPreference(context, null, user.id,
668 showSettings ? this : null,
669 showDelete ? this : null);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700670 pref.setOnPreferenceClickListener(this);
671 pref.setKey("id=" + user.id);
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700672 mUserListCategory.addPreference(pref);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700673 if (user.id == UserHandle.USER_OWNER) {
674 pref.setSummary(R.string.user_owner);
675 }
676 pref.setTitle(user.name);
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700677 }
678 if (!isInitialized(user)) {
Alexandra Gherghina423bafc2014-04-23 10:56:37 +0000679 if (user.isRestricted()) {
680 pref.setSummary(R.string.user_summary_restricted_not_set_up);
681 } else if (user.isManagedProfile()) {
682 pref.setSummary(R.string.user_summary_managed_profile_not_set_up);
683 } else {
684 pref.setSummary(R.string.user_summary_not_set_up);
685 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700686 } else if (user.isRestricted()) {
687 pref.setSummary(R.string.user_summary_restricted_profile);
Alexandra Gherghina423bafc2014-04-23 10:56:37 +0000688 } else if (user.isManagedProfile()) {
Alexandra Gherghina312c4572014-04-28 13:59:39 +0100689 if (user.isEnabled()) {
Alexandra Gherghina423bafc2014-04-23 10:56:37 +0000690 pref.setSummary(R.string.user_summary_managed_profile);
691 } else {
692 pref.setSummary(R.string.user_summary_managed_profile_not_enabled);
693 }
Amith Yamasani48fa8572012-04-17 17:12:01 -0700694 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700695 if (user.iconPath != null) {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700696 if (mUserIcons.get(user.id) == null) {
697 missingIcons.add(user.id);
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700698 pref.setIcon(encircle(R.drawable.avatar_default_1));
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700699 } else {
700 setPhotoId(pref, user);
701 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700702 }
703 }
704 // Add a temporary entry for the user being created
705 if (mAddingUser) {
706 Preference pref = new UserPreference(getActivity(), null, UserPreference.USERID_UNKNOWN,
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700707 null, null);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700708 pref.setEnabled(false);
709 pref.setTitle(R.string.user_new_user_name);
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700710 pref.setIcon(encircle(R.drawable.avatar_default_1));
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700711 mUserListCategory.addPreference(pref);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700712 }
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530713
714 if (!mIsGuest) {
715 // Add a virtual Guest user for guest defaults
716 Preference pref = new UserPreference(getActivity(), null,
717 UserPreference.USERID_GUEST_DEFAULTS, mIsOwner ? this : null, null);
718 pref.setTitle(R.string.user_guest);
719 pref.setIcon(encircle(R.drawable.ic_settings_accounts));
720 pref.setOnPreferenceClickListener(this);
721 mUserListCategory.addPreference(pref);
722 }
723
Amith Yamasani6be075d2012-09-11 11:01:35 -0700724 getActivity().invalidateOptionsMenu();
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700725
726 // Load the icons
727 if (missingIcons.size() > 0) {
728 loadIconsAsync(missingIcons);
729 }
Amith Yamasani864e3b72013-03-27 12:27:01 -0700730 boolean moreUsers = mUserManager.getMaxSupportedUsers() > users.size();
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700731 mAddUser.setEnabled(moreUsers);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700732 }
733
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700734 private void loadIconsAsync(List<Integer> missingIcons) {
Amith Yamasani5cdc9372012-11-14 16:23:40 -0800735 final Resources resources = getResources();
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700736 new AsyncTask<List<Integer>, Void, Void>() {
737 @Override
738 protected void onPostExecute(Void result) {
739 updateUserList();
740 }
741
742 @Override
743 protected Void doInBackground(List<Integer>... values) {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700744 for (int userId : values[0]) {
745 Bitmap bitmap = mUserManager.getUserIcon(userId);
Amith Yamasani864e3b72013-03-27 12:27:01 -0700746 mUserIcons.append(userId, bitmap);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700747 }
748 return null;
749 }
750 }.execute(missingIcons);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700751 }
Amith Yamasani5cdc9372012-11-14 16:23:40 -0800752
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700753 private void assignProfilePhoto(final UserInfo user) {
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700754 if (!Utils.copyMeProfilePhoto(getActivity(), user)) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700755 assignDefaultPhoto(user);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700756 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700757 }
758
759 private String getProfileName() {
Amith Yamasani8d40fac2012-10-23 15:36:16 -0700760 String name = Utils.getMeProfileName(getActivity(), true);
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700761 if (name != null) {
762 mProfileExists = true;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700763 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700764 return name;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700765 }
766
767 private void assignDefaultPhoto(UserInfo user) {
768 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
769 USER_DRAWABLES[user.id % USER_DRAWABLES.length]);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700770 mUserManager.setUserIcon(user.id, bitmap);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700771 }
772
773 private void setPhotoId(Preference pref, UserInfo user) {
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700774 Bitmap bitmap = mUserIcons.get(user.id);
Amith Yamasani864e3b72013-03-27 12:27:01 -0700775 if (bitmap != null) {
776 pref.setIcon(encircle(bitmap));
Amith Yamasanie940dac2012-09-17 10:21:27 -0700777 }
Amith Yamasani48fa8572012-04-17 17:12:01 -0700778 }
779
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700780 private void setUserName(String name) {
781 mUserManager.setUserName(UserHandle.myUserId(), name);
782 mNicknamePreference.setSummary(name);
Amith Yamasani1a634d32012-09-27 23:01:35 -0700783 getActivity().invalidateOptionsMenu();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700784 }
785
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700786 @Override
787 public boolean onPreferenceClick(Preference pref) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700788 if (pref == mMePreference) {
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700789 Intent editProfile;
790 if (!mProfileExists) {
791 editProfile = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
792 // TODO: Make this a proper API
793 editProfile.putExtra("newLocalProfile", true);
794 } else {
795 editProfile = new Intent(Intent.ACTION_EDIT, ContactsContract.Profile.CONTENT_URI);
796 }
Amith Yamasani43db5262012-09-11 17:04:10 -0700797 // To make sure that it returns back here when done
798 // TODO: Make this a proper API
799 editProfile.putExtra("finishActivityOnSaveCompleted", true);
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700800
801 // If this is a limited user, launch the user info settings instead of profile editor
802 if (mUserManager.isLinkedUser()) {
803 onManageUserClicked(UserHandle.myUserId(), false);
804 } else {
805 startActivity(editProfile);
806 }
Amith Yamasani1a634d32012-09-27 23:01:35 -0700807 } else if (pref instanceof UserPreference) {
808 int userId = ((UserPreference) pref).getUserId();
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530809 if (userId == UserPreference.USERID_GUEST_DEFAULTS) {
810 createAndSwitchToGuestUser();
Amith Yamasani1a634d32012-09-27 23:01:35 -0700811 } else {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530812 // Get the latest status of the user
813 UserInfo user = mUserManager.getUserInfo(userId);
Amith Yamasani1a634d32012-09-27 23:01:35 -0700814 if (!isInitialized(user)) {
815 mHandler.sendMessage(mHandler.obtainMessage(
816 MESSAGE_SETUP_USER, user.id, user.serialNumber));
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530817 } else if (!user.isManagedProfile()) {
818 switchUserNow(userId);
Amith Yamasani1a634d32012-09-27 23:01:35 -0700819 }
820 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700821 } else if (pref == mAddUser) {
Amith Yamasani0e43d322014-05-07 10:19:34 -0700822 // If we allow both types, show a picker, otherwise directly go to
823 // flow for full user.
824 if (mCanAddRestrictedProfile) {
825 showDialog(DIALOG_CHOOSE_USER_TYPE);
826 } else {
827 onAddUserClicked(USER_TYPE_USER);
828 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700829 }
830 return false;
831 }
832
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530833 private void createAndSwitchToGuestUser() {
834 List<UserInfo> users = mUserManager.getUsers();
835 for (UserInfo user : users) {
836 if (user.isGuest()) {
837 switchUserNow(user.id);
838 return;
839 }
840 }
841 // No guest user. Create one.
842 UserInfo guestUser = mUserManager.createGuest(getActivity(),
843 getResources().getString(R.string.user_guest));
844 if (guestUser != null) {
845 switchUserNow(guestUser.id);
846 }
847 }
848
Amith Yamasani1a634d32012-09-27 23:01:35 -0700849 private boolean isInitialized(UserInfo user) {
850 return (user.flags & UserInfo.FLAG_INITIALIZED) != 0;
851 }
852
Amith Yamasani864e3b72013-03-27 12:27:01 -0700853 private Drawable encircle(int iconResId) {
854 Bitmap icon = BitmapFactory.decodeResource(getResources(), iconResId);
855 return encircle(icon);
856 }
857
858 private Drawable encircle(Bitmap icon) {
859 Drawable circled = CircleFramedDrawable.getInstance(getActivity(), icon);
860 return circled;
861 }
862
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700863 @Override
864 public void onClick(View v) {
865 if (v.getTag() instanceof UserPreference) {
866 int userId = ((UserPreference) v.getTag()).getUserId();
Amith Yamasani233592b2013-03-01 17:05:23 -0800867 switch (v.getId()) {
868 case UserPreference.DELETE_ID:
Maggie Benthall44534962013-08-29 10:49:13 -0400869 onRemoveUserClicked(userId);
Amith Yamasani233592b2013-03-01 17:05:23 -0800870 break;
871 case UserPreference.SETTINGS_ID:
Amith Yamasanicc395692013-04-11 18:21:07 -0700872 onManageUserClicked(userId, false);
Amith Yamasani233592b2013-03-01 17:05:23 -0800873 break;
874 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700875 }
876 }
877
878 @Override
879 public void onDismiss(DialogInterface dialog) {
880 synchronized (mUserLock) {
881 mAddingUser = false;
882 mRemovingUserId = -1;
Amith Yamasanic861cf82012-10-02 14:51:46 -0700883 updateUserList();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700884 }
885 }
886
887 @Override
888 public boolean onPreferenceChange(Preference preference, Object newValue) {
889 if (preference == mNicknamePreference) {
890 String value = (String) newValue;
891 if (preference == mNicknamePreference && value != null
892 && value.length() > 0) {
893 setUserName(value);
894 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700895 return true;
896 }
897 return false;
898 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700899
Amith Yamasani38223dc2012-09-18 09:51:07 -0700900 @Override
901 public int getHelpResource() {
902 return R.string.help_url_users;
903 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700904}