blob: d009da9cc058acc32da2f0e82b2db5b736d9d732 [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
Amith Yamasanif162efe2013-04-29 10:43:05 -070019import android.app.Activity;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070020import android.app.ActivityManagerNative;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070021import android.app.AlertDialog;
22import android.app.Dialog;
Amith Yamasanif162efe2013-04-29 10:43:05 -070023import android.app.admin.DevicePolicyManager;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070024import android.content.BroadcastReceiver;
Amith Yamasani32630392012-08-10 19:31:39 -070025import android.content.Context;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070026import android.content.DialogInterface;
27import android.content.Intent;
28import android.content.IntentFilter;
Svetoslav Ganov263f7442012-09-25 16:11:16 -070029import android.content.SharedPreferences;
mariagpuyol60435fc2016-01-28 14:58:03 -080030import android.content.pm.ResolveInfo;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070031import android.content.pm.UserInfo;
Amith Yamasani5cdc9372012-11-14 16:23:40 -080032import android.content.res.Resources;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070033import android.graphics.Bitmap;
Amith Yamasani48fa8572012-04-17 17:12:01 -070034import android.graphics.drawable.Drawable;
Amith Yamasani32dccbc2012-09-13 18:06:49 -070035import android.os.AsyncTask;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070036import android.os.Bundle;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070037import android.os.Handler;
38import android.os.Message;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070039import android.os.RemoteException;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070040import android.os.UserHandle;
Amith Yamasani32630392012-08-10 19:31:39 -070041import android.os.UserManager;
Amith Yamasani247520c2014-07-25 12:52:29 -070042import android.provider.Settings;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v7.preference.Preference;
44import android.support.v7.preference.Preference.OnPreferenceClickListener;
45import android.support.v7.preference.PreferenceGroup;
46import android.support.v7.preference.PreferenceScreen;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070047import android.util.Log;
Amith Yamasanif34a85d2012-09-17 18:31:45 -070048import android.util.SparseArray;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070049import android.view.Menu;
50import android.view.MenuInflater;
51import android.view.MenuItem;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070052import android.view.View;
53import android.view.View.OnClickListener;
Amith Yamasanifc8a3832013-04-23 11:44:59 -070054import android.widget.SimpleAdapter;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070055
Chris Wren9d1bfd12016-01-26 18:04:01 -050056import com.android.internal.logging.MetricsProto.MetricsEvent;
Amith Yamasanif162efe2013-04-29 10:43:05 -070057import com.android.internal.widget.LockPatternUtils;
58import com.android.settings.ChooseLockGeneric;
Sudheer Shanka682a9162016-01-13 22:10:18 +000059import com.android.settings.DimmableIconPreference;
Amith Yamasanid4abdd22013-04-17 18:27:01 -070060import com.android.settings.OwnerInfoSettings;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070061import com.android.settings.R;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080062import com.android.settings.SettingsActivity;
Julia Reynoldsb9195d22014-06-11 16:18:16 -040063import com.android.settings.SettingsPreferenceFragment;
Amith Yamasaniae47ef42012-09-16 17:53:35 -070064import com.android.settings.Utils;
Jason Monk9d80eea2015-11-20 14:27:51 -050065import com.android.settings.dashboard.SummaryLoader;
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -070066import com.android.settings.search.BaseSearchIndexProvider;
67import com.android.settings.search.Indexable;
68import com.android.settings.search.SearchIndexableRaw;
Sudheer Shanka682a9162016-01-13 22:10:18 +000069import com.android.settingslib.RestrictedLockUtils;
70import com.android.settingslib.RestrictedSwitchPreference;
Wei Liu6e58d512015-08-24 17:29:04 -070071import com.android.settingslib.drawable.CircleFramedDrawable;
Amith Yamasanib810a0d2012-03-25 10:12:26 -070072
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +010073import java.util.ArrayList;
Fyodor Kupolov55145092014-12-08 14:15:36 -080074import java.util.Collections;
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +010075import java.util.HashMap;
76import java.util.List;
77
Sudheer Shanka682a9162016-01-13 22:10:18 +000078import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
79
Amith Yamasaniee398742014-07-24 14:13:36 -070080/**
81 * Screen that manages the list of users on the device.
82 * Guest user is an always visible entry, even if the guest is not currently
83 * active/created. It is meant for controlling properties of a guest user.
84 *
85 * The first one is always the current user.
86 * Owner is the primary user.
87 */
Julia Reynoldsb9195d22014-06-11 16:18:16 -040088public class UserSettings extends SettingsPreferenceFragment
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070089 implements OnPreferenceClickListener, OnClickListener, DialogInterface.OnDismissListener,
Amith Yamasaniee398742014-07-24 14:13:36 -070090 Preference.OnPreferenceChangeListener,
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -070091 EditUserInfoController.OnContentChangedCallback, Indexable {
Amith Yamasanib810a0d2012-03-25 10:12:26 -070092
Amith Yamasanidc6bfa62012-09-06 18:04:06 -070093 private static final String TAG = "UserSettings";
94
Amith Yamasanic861cf82012-10-02 14:51:46 -070095 /** UserId of the user being removed */
96 private static final String SAVE_REMOVING_USER = "removing_user";
97 /** UserId of the user that was just added */
98 private static final String SAVE_ADDING_USER = "adding_user";
99
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700100 private static final String KEY_USER_LIST = "user_list";
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700101 private static final String KEY_USER_ME = "user_me";
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700102 private static final String KEY_ADD_USER = "user_add";
mariagpuyol60435fc2016-01-28 14:58:03 -0800103 private static final String KEY_EMERGENCY_INFO = "emergency_info";
mariagpuyol60435fc2016-01-28 14:58:03 -0800104
105 private static final String ACTION_EDIT_EMERGENCY_INFO = "android.settings.EDIT_EMERGENGY_INFO";
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700106
Amith Yamasanicc395692013-04-11 18:21:07 -0700107 private static final int MENU_REMOVE_USER = Menu.FIRST;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700108
109 private static final int DIALOG_CONFIRM_REMOVE = 1;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700110 private static final int DIALOG_ADD_USER = 2;
111 private static final int DIALOG_SETUP_USER = 3;
112 private static final int DIALOG_SETUP_PROFILE = 4;
Amith Yamasanicc395692013-04-11 18:21:07 -0700113 private static final int DIALOG_USER_CANNOT_MANAGE = 5;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700114 private static final int DIALOG_CHOOSE_USER_TYPE = 6;
Amith Yamasanif162efe2013-04-29 10:43:05 -0700115 private static final int DIALOG_NEED_LOCKSCREEN = 7;
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700116 private static final int DIALOG_CONFIRM_EXIT_GUEST = 8;
Amith Yamasaniee398742014-07-24 14:13:36 -0700117 private static final int DIALOG_USER_PROFILE_EDITOR = 9;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700118
119 private static final int MESSAGE_UPDATE_LIST = 1;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700120 private static final int MESSAGE_SETUP_USER = 2;
Amith Yamasanicc395692013-04-11 18:21:07 -0700121 private static final int MESSAGE_CONFIG_USER = 3;
122
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700123 private static final int USER_TYPE_USER = 1;
124 private static final int USER_TYPE_RESTRICTED_PROFILE = 2;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700125
Amith Yamasanif162efe2013-04-29 10:43:05 -0700126 private static final int REQUEST_CHOOSE_LOCK = 10;
127
Svetoslav Ganov263f7442012-09-25 16:11:16 -0700128 private static final String KEY_ADD_USER_LONG_MESSAGE_DISPLAYED =
129 "key_add_user_long_message_displayed";
130
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700131 private static final String KEY_TITLE = "title";
132 private static final String KEY_SUMMARY = "summary";
133
134 private PreferenceGroup mUserListCategory;
Fyodor Kupolov55145092014-12-08 14:15:36 -0800135 private UserPreference mMePreference;
Sudheer Shanka682a9162016-01-13 22:10:18 +0000136 private DimmableIconPreference mAddUser;
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -0800137 private PreferenceGroup mLockScreenSettings;
Sudheer Shanka682a9162016-01-13 22:10:18 +0000138 private RestrictedSwitchPreference mAddUserWhenLocked;
mariagpuyol60435fc2016-01-28 14:58:03 -0800139 private Preference mEmergencyInfoPreference;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700140 private int mRemovingUserId = -1;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700141 private int mAddedUserId = 0;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700142 private boolean mAddingUser;
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700143 private String mAddingUserName;
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700144 private UserCapabilities mUserCaps;
Fyodor Kupolov8c9120b2016-03-07 11:49:35 -0800145 private boolean mShouldUpdateUserList = true;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700146 private final Object mUserLock = new Object();
147 private UserManager mUserManager;
Amith Yamasani864e3b72013-03-27 12:27:01 -0700148 private SparseArray<Bitmap> mUserIcons = new SparseArray<Bitmap>();
Amith Yamasani8666b9e2012-09-27 14:50:13 -0700149
Amith Yamasaniee398742014-07-24 14:13:36 -0700150 private EditUserInfoController mEditUserInfoController =
151 new EditUserInfoController();
152
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700153 // A place to cache the generated default avatar
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100154 private Drawable mDefaultIconDrawable;
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700155
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700156 private Handler mHandler = new Handler() {
157 @Override
158 public void handleMessage(Message msg) {
159 switch (msg.what) {
160 case MESSAGE_UPDATE_LIST:
161 updateUserList();
162 break;
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700163 case MESSAGE_SETUP_USER:
164 onUserCreated(msg.arg1);
165 break;
Amith Yamasanicc395692013-04-11 18:21:07 -0700166 case MESSAGE_CONFIG_USER:
167 onManageUserClicked(msg.arg1, true);
168 break;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700169 }
170 }
171 };
172
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700173 private BroadcastReceiver mUserChangeReceiver = new BroadcastReceiver() {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700174 @Override
175 public void onReceive(Context context, Intent intent) {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700176 if (intent.getAction().equals(Intent.ACTION_USER_REMOVED)) {
177 mRemovingUserId = -1;
178 } else if (intent.getAction().equals(Intent.ACTION_USER_INFO_CHANGED)) {
179 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
180 if (userHandle != -1) {
181 mUserIcons.remove(userHandle);
182 }
183 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700184 mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
185 }
186 };
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700187
188 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400189 protected int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500190 return MetricsEvent.USER;
Chris Wren8a963ba2015-03-20 10:29:14 -0400191 }
192
193 @Override
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700194 public void onCreate(Bundle icicle) {
195 super.onCreate(icicle);
Amith Yamasani8666b9e2012-09-27 14:50:13 -0700196
Amith Yamasanic861cf82012-10-02 14:51:46 -0700197 if (icicle != null) {
198 if (icicle.containsKey(SAVE_ADDING_USER)) {
199 mAddedUserId = icicle.getInt(SAVE_ADDING_USER);
200 }
201 if (icicle.containsKey(SAVE_REMOVING_USER)) {
202 mRemovingUserId = icicle.getInt(SAVE_REMOVING_USER);
203 }
Amith Yamasaniee398742014-07-24 14:13:36 -0700204 mEditUserInfoController.onRestoreInstanceState(icicle);
Amith Yamasanic861cf82012-10-02 14:51:46 -0700205 }
Amith Yamasani0e43d322014-05-07 10:19:34 -0700206 final Context context = getActivity();
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700207 mUserCaps = UserCapabilities.create(context);
Amith Yamasani0e43d322014-05-07 10:19:34 -0700208 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700209 if (!mUserCaps.mEnabled) {
Amith Yamasanicfb8d7f2014-03-26 14:46:47 -0700210 return;
211 }
Amith Yamasani0e43d322014-05-07 10:19:34 -0700212
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530213 final int myUserId = UserHandle.myUserId();
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530214
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700215 addPreferencesFromResource(R.xml.user_settings);
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700216 mUserListCategory = (PreferenceGroup) findPreference(KEY_USER_LIST);
Jason Monk39b46742015-09-10 15:52:51 -0400217 mMePreference = new UserPreference(getPrefContext(), null /* attrs */, myUserId,
Amith Yamasaniee398742014-07-24 14:13:36 -0700218 null /* settings icon handler */,
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700219 null /* delete icon handler */);
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700220 mMePreference.setKey(KEY_USER_ME);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700221 mMePreference.setOnPreferenceClickListener(this);
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700222 if (mUserCaps.mIsAdmin) {
223 mMePreference.setSummary(R.string.user_admin);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700224 }
Sudheer Shanka682a9162016-01-13 22:10:18 +0000225 mAddUser = (DimmableIconPreference) findPreference(KEY_ADD_USER);
Fyodor Kupolov55145092014-12-08 14:15:36 -0800226 // Determine if add user/profile button should be visible
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700227 if (mUserCaps.mCanAddUser) {
Amith Yamasani0e43d322014-05-07 10:19:34 -0700228 mAddUser.setOnPreferenceClickListener(this);
Fyodor Kupolov55145092014-12-08 14:15:36 -0800229 // change label to only mention user, if restricted profiles are not supported
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700230 if (!mUserCaps.mCanAddRestrictedProfile) {
Amith Yamasani0e43d322014-05-07 10:19:34 -0700231 mAddUser.setTitle(R.string.user_add_user_menu);
232 }
Amith Yamasani233592b2013-03-01 17:05:23 -0800233 }
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -0800234 mLockScreenSettings = (PreferenceGroup) findPreference("lock_screen_settings");
Sudheer Shanka682a9162016-01-13 22:10:18 +0000235 mAddUserWhenLocked = (RestrictedSwitchPreference) findPreference("add_users_when_locked");
mariagpuyol60435fc2016-01-28 14:58:03 -0800236 mEmergencyInfoPreference = findPreference(KEY_EMERGENCY_INFO);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700237 setHasOptionsMenu(true);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700238 IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED);
239 filter.addAction(Intent.ACTION_USER_INFO_CHANGED);
Fyodor Kupolov8c9120b2016-03-07 11:49:35 -0800240 context.registerReceiverAsUser(mUserChangeReceiver, UserHandle.ALL, filter, null, mHandler);
241 loadProfile();
242 updateUserList();
243 mShouldUpdateUserList = false;
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700244 }
245
246 @Override
247 public void onResume() {
248 super.onResume();
Amith Yamasanicfb8d7f2014-03-26 14:46:47 -0700249
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700250 if (!mUserCaps.mEnabled) return;
Fyodor Kupolov8c9120b2016-03-07 11:49:35 -0800251 if (mShouldUpdateUserList) {
Sudheer Shankaaa524ae2016-03-14 17:23:13 -0700252 mUserCaps.updateAddUserCapabilities(getActivity());
Fyodor Kupolov8c9120b2016-03-07 11:49:35 -0800253 loadProfile();
254 updateUserList();
255 }
256 }
257
258 @Override
259 public void onPause() {
260 mShouldUpdateUserList = true;
261 super.onPause();
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700262 }
263
264 @Override
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700265 public void onDestroy() {
266 super.onDestroy();
Amith Yamasanicfb8d7f2014-03-26 14:46:47 -0700267
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700268 if (!mUserCaps.mEnabled) return;
Amith Yamasanicfb8d7f2014-03-26 14:46:47 -0700269
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700270 getActivity().unregisterReceiver(mUserChangeReceiver);
271 }
272
273 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700274 public void onSaveInstanceState(Bundle outState) {
275 super.onSaveInstanceState(outState);
Amith Yamasaniee398742014-07-24 14:13:36 -0700276 mEditUserInfoController.onSaveInstanceState(outState);
Amith Yamasanic861cf82012-10-02 14:51:46 -0700277 outState.putInt(SAVE_ADDING_USER, mAddedUserId);
278 outState.putInt(SAVE_REMOVING_USER, mRemovingUserId);
279 }
280
281 @Override
Amith Yamasaniee398742014-07-24 14:13:36 -0700282 public void startActivityForResult(Intent intent, int requestCode) {
283 mEditUserInfoController.startingActivityForResult();
284 super.startActivityForResult(intent, requestCode);
285 }
286
287 @Override
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700288 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Amith Yamasani247520c2014-07-25 12:52:29 -0700289 int pos = 0;
Fyodor Kupolov52ae0532016-02-24 15:54:10 -0800290 UserManager um = getContext().getSystemService(UserManager.class);
291 boolean allowRemoveUser = !um.hasUserRestriction(UserManager.DISALLOW_REMOVE_USER);
292 boolean canSwitchUsers = um.canSwitchUsers();
293 if (!mUserCaps.mIsAdmin && allowRemoveUser && canSwitchUsers) {
Amith Yamasani1a634d32012-09-27 23:01:35 -0700294 String nickname = mUserManager.getUserName();
Amith Yamasani247520c2014-07-25 12:52:29 -0700295 MenuItem removeThisUser = menu.add(0, MENU_REMOVE_USER, pos++,
Amith Yamasani1a634d32012-09-27 23:01:35 -0700296 getResources().getString(R.string.user_remove_user_menu, nickname));
297 removeThisUser.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700298 }
Amith Yamasani38223dc2012-09-18 09:51:07 -0700299 super.onCreateOptionsMenu(menu, inflater);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700300 }
301
302 @Override
303 public boolean onOptionsItemSelected(MenuItem item) {
304 final int itemId = item.getItemId();
Amith Yamasanicc395692013-04-11 18:21:07 -0700305 if (itemId == MENU_REMOVE_USER) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700306 onRemoveUserClicked(UserHandle.myUserId());
307 return true;
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700308 } else {
309 return super.onOptionsItemSelected(item);
310 }
311 }
312
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700313 /**
314 * Loads profile information for the current user.
315 */
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700316 private void loadProfile() {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700317 if (mUserCaps.mIsGuest) {
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700318 // No need to load profile information
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100319 mMePreference.setIcon(getEncircledDefaultIcon());
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700320 mMePreference.setTitle(R.string.user_exit_guest_title);
321 return;
322 }
323
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700324 new AsyncTask<Void, Void, String>() {
325 @Override
326 protected void onPostExecute(String result) {
327 finishLoadProfile(result);
328 }
329
330 @Override
331 protected String doInBackground(Void... values) {
332 UserInfo user = mUserManager.getUserInfo(UserHandle.myUserId());
333 if (user.iconPath == null || user.iconPath.equals("")) {
334 assignProfilePhoto(user);
335 }
Amith Yamasani2d578ce2014-07-25 09:37:33 -0700336 return user.name;
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700337 }
338 }.execute();
339 }
340
341 private void finishLoadProfile(String profileName) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700342 if (getActivity() == null) return;
343 mMePreference.setTitle(getString(R.string.user_you, profileName));
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700344 int myUserId = UserHandle.myUserId();
345 Bitmap b = mUserManager.getUserIcon(myUserId);
346 if (b != null) {
Amith Yamasani864e3b72013-03-27 12:27:01 -0700347 mMePreference.setIcon(encircle(b));
348 mUserIcons.put(myUserId, b);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700349 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700350 }
351
Amith Yamasanif162efe2013-04-29 10:43:05 -0700352 private boolean hasLockscreenSecurity() {
353 LockPatternUtils lpu = new LockPatternUtils(getActivity());
Adrian Roos54375882015-04-16 17:11:22 -0700354 return lpu.isSecure(UserHandle.myUserId());
Amith Yamasanif162efe2013-04-29 10:43:05 -0700355 }
356
357 private void launchChooseLockscreen() {
358 Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
359 chooseLockIntent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY,
360 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
361 startActivityForResult(chooseLockIntent, REQUEST_CHOOSE_LOCK);
362 }
363
364 @Override
365 public void onActivityResult(int requestCode, int resultCode, Intent data) {
366 super.onActivityResult(requestCode, resultCode, data);
367
368 if (requestCode == REQUEST_CHOOSE_LOCK) {
369 if (resultCode != Activity.RESULT_CANCELED && hasLockscreenSecurity()) {
370 addUserNow(USER_TYPE_RESTRICTED_PROFILE);
Amith Yamasanif162efe2013-04-29 10:43:05 -0700371 }
Amith Yamasaniee398742014-07-24 14:13:36 -0700372 } else {
373 mEditUserInfoController.onActivityResult(requestCode, resultCode, data);
Amith Yamasanif162efe2013-04-29 10:43:05 -0700374 }
375 }
376
Amith Yamasanicc395692013-04-11 18:21:07 -0700377 private void onAddUserClicked(int userType) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700378 synchronized (mUserLock) {
379 if (mRemovingUserId == -1 && !mAddingUser) {
Amith Yamasanicc395692013-04-11 18:21:07 -0700380 switch (userType) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700381 case USER_TYPE_USER:
382 showDialog(DIALOG_ADD_USER);
Amith Yamasanicc395692013-04-11 18:21:07 -0700383 break;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700384 case USER_TYPE_RESTRICTED_PROFILE:
Amith Yamasanif162efe2013-04-29 10:43:05 -0700385 if (hasLockscreenSecurity()) {
386 addUserNow(USER_TYPE_RESTRICTED_PROFILE);
387 } else {
388 showDialog(DIALOG_NEED_LOCKSCREEN);
389 }
Amith Yamasanicc395692013-04-11 18:21:07 -0700390 break;
391 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700392 }
393 }
394 }
395
396 private void onRemoveUserClicked(int userId) {
397 synchronized (mUserLock) {
398 if (mRemovingUserId == -1 && !mAddingUser) {
399 mRemovingUserId = userId;
400 showDialog(DIALOG_CONFIRM_REMOVE);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700401 }
402 }
403 }
404
Fyodor Kupolov9938f6f2015-08-26 17:24:09 -0700405 private UserInfo createRestrictedProfile() {
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700406 UserInfo newUserInfo = mUserManager.createRestrictedProfile(mAddingUserName);
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100407 assignDefaultPhoto(newUserInfo);
Amith Yamasanicc395692013-04-11 18:21:07 -0700408 return newUserInfo;
409 }
410
411 private UserInfo createTrustedUser() {
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700412 UserInfo newUserInfo = mUserManager.createUser(mAddingUserName, 0);
Amith Yamasanicc395692013-04-11 18:21:07 -0700413 if (newUserInfo != null) {
414 assignDefaultPhoto(newUserInfo);
415 }
416 return newUserInfo;
417 }
418
419 private void onManageUserClicked(int userId, boolean newUser) {
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700420 mAddingUser = false;
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530421 if (userId == UserPreference.USERID_GUEST_DEFAULTS) {
422 Bundle extras = new Bundle();
423 extras.putBoolean(UserDetailsSettings.EXTRA_USER_GUEST, true);
424 ((SettingsActivity) getActivity()).startPreferencePanel(
425 UserDetailsSettings.class.getName(),
426 extras, R.string.user_guest, null, null, 0);
427 return;
428 }
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700429 UserInfo info = mUserManager.getUserInfo(userId);
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700430 if (info.isRestricted() && mUserCaps.mIsAdmin) {
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700431 Bundle extras = new Bundle();
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700432 extras.putInt(RestrictedProfileSettings.EXTRA_USER_ID, userId);
433 extras.putBoolean(RestrictedProfileSettings.EXTRA_NEW_USER, newUser);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800434 ((SettingsActivity) getActivity()).startPreferencePanel(
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700435 RestrictedProfileSettings.class.getName(),
Amith Yamasani7c4dfcc2013-05-22 12:51:30 -0700436 extras, R.string.user_restrictions_title, null,
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700437 null, 0);
438 } else if (info.id == UserHandle.myUserId()) {
439 // Jump to owner info panel
Jason Monk3bcd76c2015-04-21 11:20:20 -0400440 OwnerInfoSettings.show(this);
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700441 } else if (mUserCaps.mIsAdmin) {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530442 Bundle extras = new Bundle();
443 extras.putInt(UserDetailsSettings.EXTRA_USER_ID, userId);
444 ((SettingsActivity) getActivity()).startPreferencePanel(
445 UserDetailsSettings.class.getName(),
Amith Yamasani51c6dac2014-07-02 00:06:37 +0530446 extras,
447 -1, /* No title res id */
448 info.name, /* title */
449 null, /* resultTo */
450 0 /* resultRequestCode */);
Amith Yamasanicc395692013-04-11 18:21:07 -0700451 }
Amith Yamasani233592b2013-03-01 17:05:23 -0800452 }
453
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700454 private void onUserCreated(int userId) {
455 mAddedUserId = userId;
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700456 mAddingUser = false;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700457 if (mUserManager.getUserInfo(userId).isRestricted()) {
458 showDialog(DIALOG_SETUP_PROFILE);
459 } else {
460 showDialog(DIALOG_SETUP_USER);
461 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700462 }
463
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700464 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700465 public void onDialogShowing() {
466 super.onDialogShowing();
467
468 setOnDismissListener(this);
469 }
470
471 @Override
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700472 public Dialog onCreateDialog(int dialogId) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700473 Context context = getActivity();
474 if (context == null) return null;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700475 switch (dialogId) {
Amith Yamasanic861cf82012-10-02 14:51:46 -0700476 case DIALOG_CONFIRM_REMOVE: {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530477 Dialog dlg =
Fyodor Kupolovdbbd4fc2014-12-29 13:40:27 -0800478 UserDialogs.createRemoveDialog(getActivity(), mRemovingUserId,
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530479 new DialogInterface.OnClickListener() {
480 public void onClick(DialogInterface dialog, int which) {
481 removeUserNow();
482 }
483 }
484 );
Amith Yamasanic861cf82012-10-02 14:51:46 -0700485 return dlg;
486 }
Amith Yamasani1a634d32012-09-27 23:01:35 -0700487 case DIALOG_USER_CANNOT_MANAGE:
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700488 return new AlertDialog.Builder(context)
Amith Yamasani1a634d32012-09-27 23:01:35 -0700489 .setMessage(R.string.user_cannot_manage_message)
490 .setPositiveButton(android.R.string.ok, null)
491 .create();
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700492 case DIALOG_ADD_USER: {
Svetoslav Ganov263f7442012-09-25 16:11:16 -0700493 final SharedPreferences preferences = getActivity().getPreferences(
494 Context.MODE_PRIVATE);
495 final boolean longMessageDisplayed = preferences.getBoolean(
496 KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, false);
497 final int messageResId = longMessageDisplayed
498 ? R.string.user_add_user_message_short
499 : R.string.user_add_user_message_long;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700500 final int userType = dialogId == DIALOG_ADD_USER
501 ? USER_TYPE_USER : USER_TYPE_RESTRICTED_PROFILE;
502 Dialog dlg = new AlertDialog.Builder(context)
503 .setTitle(R.string.user_add_user_title)
504 .setMessage(messageResId)
505 .setPositiveButton(android.R.string.ok,
506 new DialogInterface.OnClickListener() {
507 public void onClick(DialogInterface dialog, int which) {
508 addUserNow(userType);
509 if (!longMessageDisplayed) {
510 preferences.edit().putBoolean(
511 KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, true).apply();
512 }
Svetoslav Ganov263f7442012-09-25 16:11:16 -0700513 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700514 })
515 .setNegativeButton(android.R.string.cancel, null)
516 .create();
Amith Yamasanic861cf82012-10-02 14:51:46 -0700517 return dlg;
518 }
519 case DIALOG_SETUP_USER: {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700520 Dialog dlg = new AlertDialog.Builder(context)
521 .setTitle(R.string.user_setup_dialog_title)
522 .setMessage(R.string.user_setup_dialog_message)
523 .setPositiveButton(R.string.user_setup_button_setup_now,
524 new DialogInterface.OnClickListener() {
525 public void onClick(DialogInterface dialog, int which) {
526 switchUserNow(mAddedUserId);
527 }
528 })
529 .setNegativeButton(R.string.user_setup_button_setup_later, null)
530 .create();
531 return dlg;
532 }
533 case DIALOG_SETUP_PROFILE: {
534 Dialog dlg = new AlertDialog.Builder(context)
535 .setMessage(R.string.user_setup_profile_dialog_message)
536 .setPositiveButton(android.R.string.ok,
537 new DialogInterface.OnClickListener() {
538 public void onClick(DialogInterface dialog, int which) {
539 switchUserNow(mAddedUserId);
540 }
541 })
542 .setNegativeButton(android.R.string.cancel, null)
543 .create();
544 return dlg;
545 }
546 case DIALOG_CHOOSE_USER_TYPE: {
547 List<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
548 HashMap<String,String> addUserItem = new HashMap<String,String>();
549 addUserItem.put(KEY_TITLE, getString(R.string.user_add_user_item_title));
550 addUserItem.put(KEY_SUMMARY, getString(R.string.user_add_user_item_summary));
551 HashMap<String,String> addProfileItem = new HashMap<String,String>();
552 addProfileItem.put(KEY_TITLE, getString(R.string.user_add_profile_item_title));
553 addProfileItem.put(KEY_SUMMARY, getString(R.string.user_add_profile_item_summary));
554 data.add(addUserItem);
555 data.add(addProfileItem);
Amith Yamasanif3db0212014-12-02 17:21:23 -0800556 AlertDialog.Builder builder = new AlertDialog.Builder(context);
557 SimpleAdapter adapter = new SimpleAdapter(builder.getContext(),
558 data, R.layout.two_line_list_item,
559 new String[] {KEY_TITLE, KEY_SUMMARY},
560 new int[] {R.id.title, R.id.summary});
561 builder.setTitle(R.string.user_add_user_type_title);
562 builder.setAdapter(adapter,
563 new DialogInterface.OnClickListener() {
564 @Override
565 public void onClick(DialogInterface dialog, int which) {
566 onAddUserClicked(which == 0
567 ? USER_TYPE_USER
568 : USER_TYPE_RESTRICTED_PROFILE);
569 }
570 });
571 return builder.create();
Amith Yamasanic861cf82012-10-02 14:51:46 -0700572 }
Amith Yamasanif162efe2013-04-29 10:43:05 -0700573 case DIALOG_NEED_LOCKSCREEN: {
574 Dialog dlg = new AlertDialog.Builder(context)
575 .setMessage(R.string.user_need_lock_message)
576 .setPositiveButton(R.string.user_set_lock_button,
577 new DialogInterface.OnClickListener() {
578 @Override
579 public void onClick(DialogInterface dialog, int which) {
580 launchChooseLockscreen();
581 }
582 })
583 .setNegativeButton(android.R.string.cancel, null)
584 .create();
585 return dlg;
586 }
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700587 case DIALOG_CONFIRM_EXIT_GUEST: {
588 Dialog dlg = new AlertDialog.Builder(context)
589 .setTitle(R.string.user_exit_guest_confirm_title)
590 .setMessage(R.string.user_exit_guest_confirm_message)
Amith Yamasani94c01812014-08-18 16:42:43 -0700591 .setPositiveButton(R.string.user_exit_guest_dialog_remove,
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700592 new DialogInterface.OnClickListener() {
593 @Override
594 public void onClick(DialogInterface dialog, int which) {
595 exitGuest();
596 }
597 })
Amith Yamasani94c01812014-08-18 16:42:43 -0700598 .setNegativeButton(android.R.string.cancel, null)
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700599 .create();
600 return dlg;
601 }
Amith Yamasaniee398742014-07-24 14:13:36 -0700602 case DIALOG_USER_PROFILE_EDITOR: {
603 Dialog dlg = mEditUserInfoController.createDialog(
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700604 this,
Amith Yamasaniee398742014-07-24 14:13:36 -0700605 mMePreference.getIcon(),
606 mMePreference.getTitle(),
607 R.string.profile_info_settings_title,
608 this /* callback */,
609 android.os.Process.myUserHandle());
610 return dlg;
611 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700612 default:
613 return null;
614 }
615 }
616
mariagpuyol60435fc2016-01-28 14:58:03 -0800617 private boolean emergencyInfoActivityPresent() {
618 Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO).setPackage("com.android.emergency");
619 List<ResolveInfo> infos = getContext().getPackageManager().queryIntentActivities(intent, 0);
620 if (infos == null || infos.isEmpty()) {
621 return false;
622 }
623 return true;
624 }
625
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700626 private void removeUserNow() {
627 if (mRemovingUserId == UserHandle.myUserId()) {
628 removeThisUser();
629 } else {
630 new Thread() {
631 public void run() {
632 synchronized (mUserLock) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700633 mUserManager.removeUser(mRemovingUserId);
634 mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700635 }
636 }
637 }.start();
638 }
639 }
640
641 private void removeThisUser() {
Fyodor Kupolov52ae0532016-02-24 15:54:10 -0800642 if (!mUserManager.canSwitchUsers()) {
643 Log.w(TAG, "Cannot remove current user when switching is disabled");
644 return;
645 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700646 try {
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700647 ActivityManagerNative.getDefault().switchUser(UserHandle.USER_SYSTEM);
Fyodor Kupolov52ae0532016-02-24 15:54:10 -0800648 getContext().getSystemService(UserManager.class).removeUser(UserHandle.myUserId());
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700649 } catch (RemoteException re) {
650 Log.e(TAG, "Unable to remove self user");
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700651 }
652 }
653
Amith Yamasanicc395692013-04-11 18:21:07 -0700654 private void addUserNow(final int userType) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700655 synchronized (mUserLock) {
656 mAddingUser = true;
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700657 mAddingUserName = userType == USER_TYPE_USER ? getString(R.string.user_new_user_name)
658 : getString(R.string.user_new_profile_name);
Amith Yamasania98c4022013-04-19 15:27:52 -0700659 //updateUserList();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700660 new Thread() {
661 public void run() {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700662 UserInfo user;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700663 // Could take a few seconds
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700664 if (userType == USER_TYPE_USER) {
Amith Yamasanicc395692013-04-11 18:21:07 -0700665 user = createTrustedUser();
666 } else {
Fyodor Kupolov9938f6f2015-08-26 17:24:09 -0700667 user = createRestrictedProfile();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700668 }
Sudheer Shankaaa524ae2016-03-14 17:23:13 -0700669 if (user == null) {
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700670 mAddingUser = false;
Sudheer Shankaaa524ae2016-03-14 17:23:13 -0700671 return;
672 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700673 synchronized (mUserLock) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700674 if (userType == USER_TYPE_USER) {
Amith Yamasania98c4022013-04-19 15:27:52 -0700675 mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
Amith Yamasanicc395692013-04-11 18:21:07 -0700676 mHandler.sendMessage(mHandler.obtainMessage(
677 MESSAGE_SETUP_USER, user.id, user.serialNumber));
678 } else {
679 mHandler.sendMessage(mHandler.obtainMessage(
680 MESSAGE_CONFIG_USER, user.id, user.serialNumber));
681 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700682 }
683 }
684 }.start();
685 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700686 }
687
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700688 private void switchUserNow(int userId) {
689 try {
690 ActivityManagerNative.getDefault().switchUser(userId);
691 } catch (RemoteException re) {
692 // Nothing to do
693 }
694 }
695
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700696 /**
697 * Erase the current user (guest) and switch to another user.
698 */
699 private void exitGuest() {
700 // Just to be safe
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700701 if (!mUserCaps.mIsGuest) {
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700702 return;
703 }
704 removeThisUser();
705 }
706
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700707 private void updateUserList() {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700708 if (getActivity() == null) return;
Amith Yamasanie278eda2012-09-20 22:44:48 -0700709 List<UserInfo> users = mUserManager.getUsers(true);
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530710 final Context context = getActivity();
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700711
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530712 final boolean voiceCapable = Utils.isVoiceCapable(context);
Fyodor Kupolov55145092014-12-08 14:15:36 -0800713 final ArrayList<Integer> missingIcons = new ArrayList<>();
714 final ArrayList<UserPreference> userPreferences = new ArrayList<>();
715 userPreferences.add(mMePreference);
716
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700717 for (UserInfo user : users) {
Xiaohui Chen148ef772015-07-16 10:06:03 -0700718 if (!user.supportsSwitchToByUser()) {
719 // Only users that can be switched to should show up here.
720 // e.g. Managed profiles appear under Accounts Settings instead
Alexandra Gherghina83adb562014-07-31 19:30:41 +0100721 continue;
722 }
Fyodor Kupolov55145092014-12-08 14:15:36 -0800723 UserPreference pref;
Amith Yamasanic861cf82012-10-02 14:51:46 -0700724 if (user.id == UserHandle.myUserId()) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700725 pref = mMePreference;
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530726 } else if (user.isGuest()) {
727 // Skip over Guest. We add generic Guest settings after this loop
728 continue;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700729 } else {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530730 // With Telephony:
731 // Secondary user: Settings
732 // Guest: Settings
733 // Restricted Profile: There is no Restricted Profile
734 // Without Telephony:
735 // Secondary user: Delete
736 // Guest: Nothing
737 // Restricted Profile: Settings
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700738 final boolean showSettings = mUserCaps.mIsAdmin
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700739 && (voiceCapable || user.isRestricted());
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700740 final boolean showDelete = mUserCaps.mIsAdmin
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530741 && (!voiceCapable && !user.isRestricted() && !user.isGuest());
Jason Monk39b46742015-09-10 15:52:51 -0400742 pref = new UserPreference(getPrefContext(), null, user.id,
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530743 showSettings ? this : null,
744 showDelete ? this : null);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700745 pref.setOnPreferenceClickListener(this);
746 pref.setKey("id=" + user.id);
Fyodor Kupolov55145092014-12-08 14:15:36 -0800747 userPreferences.add(pref);
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700748 if (user.isAdmin()) {
749 pref.setSummary(R.string.user_admin);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700750 }
751 pref.setTitle(user.name);
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700752 }
753 if (!isInitialized(user)) {
Alexandra Gherghina423bafc2014-04-23 10:56:37 +0000754 if (user.isRestricted()) {
755 pref.setSummary(R.string.user_summary_restricted_not_set_up);
Alexandra Gherghina423bafc2014-04-23 10:56:37 +0000756 } else {
757 pref.setSummary(R.string.user_summary_not_set_up);
758 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700759 } else if (user.isRestricted()) {
760 pref.setSummary(R.string.user_summary_restricted_profile);
Amith Yamasani48fa8572012-04-17 17:12:01 -0700761 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700762 if (user.iconPath != null) {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700763 if (mUserIcons.get(user.id) == null) {
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100764 // Icon not loaded yet, print a placeholder
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700765 missingIcons.add(user.id);
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100766 pref.setIcon(getEncircledDefaultIcon());
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700767 } else {
768 setPhotoId(pref, user);
769 }
Amith Yamasanif2663ab2014-08-11 17:17:29 -0700770 } else {
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100771 // Icon not available yet, print a placeholder
772 pref.setIcon(getEncircledDefaultIcon());
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700773 }
774 }
Amith Yamasani247520c2014-07-25 12:52:29 -0700775
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700776 // Add a temporary entry for the user being created
777 if (mAddingUser) {
Jason Monk39b46742015-09-10 15:52:51 -0400778 UserPreference pref = new UserPreference(getPrefContext(), null,
Fyodor Kupolov55145092014-12-08 14:15:36 -0800779 UserPreference.USERID_UNKNOWN, null, null);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700780 pref.setEnabled(false);
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700781 pref.setTitle(mAddingUserName);
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100782 pref.setIcon(getEncircledDefaultIcon());
Fyodor Kupolov55145092014-12-08 14:15:36 -0800783 userPreferences.add(pref);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700784 }
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530785
Fyodor Kupolov889bc3d2016-01-29 17:29:54 -0800786 // Check if Guest tile should be added.
Sudheer Shanka29737ad2016-02-11 17:17:21 +0000787 if (!mUserCaps.mIsGuest && (mUserCaps.mCanAddGuest ||
788 mUserCaps.mDisallowAddUserSetByAdmin)) {
789 // Add a virtual Guest user for guest defaults
Jason Monk39b46742015-09-10 15:52:51 -0400790 UserPreference pref = new UserPreference(getPrefContext(), null,
Fyodor Kupolov55145092014-12-08 14:15:36 -0800791 UserPreference.USERID_GUEST_DEFAULTS,
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700792 mUserCaps.mIsAdmin && voiceCapable? this : null /* settings icon handler */,
Fyodor Kupolov55145092014-12-08 14:15:36 -0800793 null /* delete icon handler */);
794 pref.setTitle(R.string.user_guest);
795 pref.setIcon(getEncircledDefaultIcon());
Fyodor Kupolov55145092014-12-08 14:15:36 -0800796 userPreferences.add(pref);
Sudheer Shanka682a9162016-01-13 22:10:18 +0000797 pref.setDisabledByAdmin(
798 mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null);
Fyodor Kupolov55145092014-12-08 14:15:36 -0800799 }
800
801 // Sort list of users by serialNum
802 Collections.sort(userPreferences, UserPreference.SERIAL_NUMBER_COMPARATOR);
803
804 getActivity().invalidateOptionsMenu();
805
806 // Load the icons
807 if (missingIcons.size() > 0) {
808 loadIconsAsync(missingIcons);
809 }
810
811 PreferenceScreen preferenceScreen = getPreferenceScreen();
812 preferenceScreen.removeAll();
813
814 // If profiles are supported, userPreferences will be added to the category labeled
815 // "User & Profiles", otherwise the category is skipped and elements are added directly
816 // to preferenceScreen
817 PreferenceGroup groupToAddUsers;
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700818 if (mUserCaps.mCanAddRestrictedProfile) {
Fyodor Kupolov55145092014-12-08 14:15:36 -0800819 mUserListCategory.removeAll();
820 mUserListCategory.setOrder(Preference.DEFAULT_ORDER);
821 preferenceScreen.addPreference(mUserListCategory);
822 groupToAddUsers = mUserListCategory;
823 } else {
824 groupToAddUsers = preferenceScreen;
825 }
826 for (UserPreference userPreference : userPreferences) {
827 userPreference.setOrder(Preference.DEFAULT_ORDER);
828 groupToAddUsers.addPreference(userPreference);
829 }
830
831 // Append Add user to the end of the list
Sudheer Shanka29737ad2016-02-11 17:17:21 +0000832 if (mUserCaps.mCanAddUser || mUserCaps.mDisallowAddUserSetByAdmin) {
Fyodor Kupolov55145092014-12-08 14:15:36 -0800833 boolean moreUsers = mUserManager.canAddMoreUsers();
Fyodor Kupolov55145092014-12-08 14:15:36 -0800834 mAddUser.setOrder(Preference.DEFAULT_ORDER);
835 preferenceScreen.addPreference(mAddUser);
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700836 mAddUser.setEnabled(moreUsers && !mAddingUser);
Fyodor Kupolova7a7bfe2015-05-08 15:54:38 -0700837 if (!moreUsers) {
838 mAddUser.setSummary(getString(R.string.user_add_max_count, getMaxRealUsers()));
839 } else {
840 mAddUser.setSummary(null);
841 }
Sudheer Shanka682a9162016-01-13 22:10:18 +0000842 if (mAddUser.isEnabled()) {
843 mAddUser.setDisabledByAdmin(
844 mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null);
845 }
Fyodor Kupolov55145092014-12-08 14:15:36 -0800846 }
Sudheer Shanka29737ad2016-02-11 17:17:21 +0000847 if (mUserCaps.mIsAdmin &&
848 (!mUserCaps.mDisallowAddUser || mUserCaps.mDisallowAddUserSetByAdmin)) {
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -0800849 mLockScreenSettings.setOrder(Preference.DEFAULT_ORDER);
850 preferenceScreen.addPreference(mLockScreenSettings);
851 mAddUserWhenLocked.setChecked(Settings.Global.getInt(getContentResolver(),
852 Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1);
853 mAddUserWhenLocked.setOnPreferenceChangeListener(this);
Sudheer Shanka682a9162016-01-13 22:10:18 +0000854 mAddUserWhenLocked.setDisabledByAdmin(
855 mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null);
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -0800856 }
mariagpuyolfe19bc32016-02-17 19:23:24 -0800857
858 if (emergencyInfoActivityPresent()) {
859 mEmergencyInfoPreference.setOnPreferenceClickListener(this);
860 mEmergencyInfoPreference.setOrder(Preference.DEFAULT_ORDER);
861 preferenceScreen.addPreference(mEmergencyInfoPreference);
862 }
Fyodor Kupolov55145092014-12-08 14:15:36 -0800863 }
864
Fyodor Kupolova7a7bfe2015-05-08 15:54:38 -0700865 private int getMaxRealUsers() {
866 // guest is not counted against getMaxSupportedUsers() number
867 final int maxUsersAndGuest = UserManager.getMaxSupportedUsers() + 1;
868 final List<UserInfo> users = mUserManager.getUsers();
869 // managed profiles are counted against getMaxSupportedUsers()
870 int managedProfiles = 0;
871 for (UserInfo user : users) {
872 if (user.isManagedProfile()) {
873 managedProfiles++;
874 }
875 }
876 return maxUsersAndGuest - managedProfiles;
877 }
878
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700879 private void loadIconsAsync(List<Integer> missingIcons) {
880 new AsyncTask<List<Integer>, Void, Void>() {
881 @Override
882 protected void onPostExecute(Void result) {
883 updateUserList();
884 }
885
886 @Override
887 protected Void doInBackground(List<Integer>... values) {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700888 for (int userId : values[0]) {
889 Bitmap bitmap = mUserManager.getUserIcon(userId);
Amith Yamasanif2663ab2014-08-11 17:17:29 -0700890 if (bitmap == null) {
Zoltan Szatmary-Bane5814ff2014-12-19 16:27:45 +0000891 bitmap = Utils.getDefaultUserIconAsBitmap(userId);
Amith Yamasanif2663ab2014-08-11 17:17:29 -0700892 }
Amith Yamasani864e3b72013-03-27 12:27:01 -0700893 mUserIcons.append(userId, bitmap);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700894 }
895 return null;
896 }
897 }.execute(missingIcons);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700898 }
Amith Yamasani5cdc9372012-11-14 16:23:40 -0800899
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700900 private void assignProfilePhoto(final UserInfo user) {
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700901 if (!Utils.copyMeProfilePhoto(getActivity(), user)) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700902 assignDefaultPhoto(user);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700903 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700904 }
905
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700906 private void assignDefaultPhoto(UserInfo user) {
Zoltan Szatmary-Bane5814ff2014-12-19 16:27:45 +0000907 Bitmap bitmap = Utils.getDefaultUserIconAsBitmap(user.id);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700908 mUserManager.setUserIcon(user.id, bitmap);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700909 }
910
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100911 private Drawable getEncircledDefaultIcon() {
912 if (mDefaultIconDrawable == null) {
Zoltan Szatmary-Bane5814ff2014-12-19 16:27:45 +0000913 mDefaultIconDrawable = encircle(Utils.getDefaultUserIconAsBitmap(UserHandle.USER_NULL));
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700914 }
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100915 return mDefaultIconDrawable;
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700916 }
917
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700918 private void setPhotoId(Preference pref, UserInfo user) {
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700919 Bitmap bitmap = mUserIcons.get(user.id);
Amith Yamasani864e3b72013-03-27 12:27:01 -0700920 if (bitmap != null) {
921 pref.setIcon(encircle(bitmap));
Amith Yamasanie940dac2012-09-17 10:21:27 -0700922 }
Amith Yamasani48fa8572012-04-17 17:12:01 -0700923 }
924
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700925 @Override
926 public boolean onPreferenceClick(Preference pref) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700927 if (pref == mMePreference) {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700928 if (mUserCaps.mIsGuest) {
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700929 showDialog(DIALOG_CONFIRM_EXIT_GUEST);
930 return true;
931 }
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700932 // If this is a limited user, launch the user info settings instead of profile editor
933 if (mUserManager.isLinkedUser()) {
934 onManageUserClicked(UserHandle.myUserId(), false);
935 } else {
Amith Yamasaniee398742014-07-24 14:13:36 -0700936 showDialog(DIALOG_USER_PROFILE_EDITOR);
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700937 }
Amith Yamasani1a634d32012-09-27 23:01:35 -0700938 } else if (pref instanceof UserPreference) {
939 int userId = ((UserPreference) pref).getUserId();
Fyodor Kupolov889bc3d2016-01-29 17:29:54 -0800940 // Get the latest status of the user
941 UserInfo user = mUserManager.getUserInfo(userId);
942 if (!isInitialized(user)) {
943 mHandler.sendMessage(mHandler.obtainMessage(
944 MESSAGE_SETUP_USER, user.id, user.serialNumber));
Amith Yamasani1a634d32012-09-27 23:01:35 -0700945 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700946 } else if (pref == mAddUser) {
Amith Yamasani0e43d322014-05-07 10:19:34 -0700947 // If we allow both types, show a picker, otherwise directly go to
948 // flow for full user.
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700949 if (mUserCaps.mCanAddRestrictedProfile) {
Amith Yamasani0e43d322014-05-07 10:19:34 -0700950 showDialog(DIALOG_CHOOSE_USER_TYPE);
951 } else {
952 onAddUserClicked(USER_TYPE_USER);
953 }
mariagpuyol60435fc2016-01-28 14:58:03 -0800954 } else if (pref == mEmergencyInfoPreference) {
Maria Garcia Puyolda497f22016-03-17 22:52:44 +0000955 Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO);
956 startActivity(intent);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700957 }
958 return false;
959 }
960
Amith Yamasani1a634d32012-09-27 23:01:35 -0700961 private boolean isInitialized(UserInfo user) {
962 return (user.flags & UserInfo.FLAG_INITIALIZED) != 0;
963 }
964
Amith Yamasani864e3b72013-03-27 12:27:01 -0700965 private Drawable encircle(Bitmap icon) {
966 Drawable circled = CircleFramedDrawable.getInstance(getActivity(), icon);
967 return circled;
968 }
969
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700970 @Override
971 public void onClick(View v) {
972 if (v.getTag() instanceof UserPreference) {
973 int userId = ((UserPreference) v.getTag()).getUserId();
Amith Yamasani233592b2013-03-01 17:05:23 -0800974 switch (v.getId()) {
975 case UserPreference.DELETE_ID:
Sudheer Shankab5c92142016-03-22 17:24:07 -0700976 final EnforcedAdmin removeDisallowedAdmin =
977 RestrictedLockUtils.checkIfRestrictionEnforced(getContext(),
978 UserManager.DISALLOW_REMOVE_USER, UserHandle.myUserId());
979 if (removeDisallowedAdmin != null) {
980 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
981 removeDisallowedAdmin);
982 } else {
983 onRemoveUserClicked(userId);
984 }
Amith Yamasani233592b2013-03-01 17:05:23 -0800985 break;
986 case UserPreference.SETTINGS_ID:
Amith Yamasanicc395692013-04-11 18:21:07 -0700987 onManageUserClicked(userId, false);
Amith Yamasani233592b2013-03-01 17:05:23 -0800988 break;
989 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700990 }
991 }
992
993 @Override
994 public void onDismiss(DialogInterface dialog) {
995 synchronized (mUserLock) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700996 mRemovingUserId = -1;
Amith Yamasanic861cf82012-10-02 14:51:46 -0700997 updateUserList();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700998 }
999 }
1000
1001 @Override
1002 public boolean onPreferenceChange(Preference preference, Object newValue) {
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -08001003 if (preference == mAddUserWhenLocked) {
1004 Boolean value = (Boolean) newValue;
1005 Settings.Global.putInt(getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED,
1006 value != null && value ? 1 : 0);
Amith Yamasanib810a0d2012-03-25 10:12:26 -07001007 return true;
1008 }
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -08001009
Amith Yamasanib810a0d2012-03-25 10:12:26 -07001010 return false;
1011 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -07001012
Amith Yamasani38223dc2012-09-18 09:51:07 -07001013 @Override
1014 public int getHelpResource() {
1015 return R.string.help_url_users;
1016 }
Amith Yamasaniee398742014-07-24 14:13:36 -07001017
1018 @Override
1019 public void onPhotoChanged(Drawable photo) {
1020 mMePreference.setIcon(photo);
1021 }
1022
1023 @Override
1024 public void onLabelChanged(CharSequence label) {
1025 mMePreference.setTitle(label);
1026 }
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001027
1028 private static class UserCapabilities {
1029 boolean mEnabled = true;
1030 boolean mCanAddUser = true;
1031 boolean mCanAddRestrictedProfile = true;
Xiaohui Chen64f530c2015-06-24 12:35:07 -07001032 boolean mIsAdmin;
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001033 boolean mIsGuest;
Fyodor Kupolov097936a2015-05-21 18:31:45 -07001034 boolean mCanAddGuest;
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -08001035 boolean mDisallowAddUser;
Sudheer Shanka29737ad2016-02-11 17:17:21 +00001036 boolean mDisallowAddUserSetByAdmin;
Sudheer Shanka682a9162016-01-13 22:10:18 +00001037 EnforcedAdmin mEnforcedAdmin;
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001038
Xiaohui Chen64f530c2015-06-24 12:35:07 -07001039 private UserCapabilities() {}
1040
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001041 public static UserCapabilities create(Context context) {
1042 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
1043 UserCapabilities caps = new UserCapabilities();
1044 if (!UserManager.supportsMultipleUsers() || Utils.isMonkeyRunning()) {
1045 caps.mEnabled = false;
1046 return caps;
1047 }
1048
Xiaohui Chen64f530c2015-06-24 12:35:07 -07001049 final UserInfo myUserInfo = userManager.getUserInfo(UserHandle.myUserId());
1050 caps.mIsGuest = myUserInfo.isGuest();
1051 caps.mIsAdmin = myUserInfo.isAdmin();
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001052 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
1053 Context.DEVICE_POLICY_SERVICE);
1054 // No restricted profiles for tablets with a device owner, or phones.
Makoto Onuki4cfe39f2015-11-19 13:47:55 -08001055 if (dpm.isDeviceManaged() || Utils.isVoiceCapable(context)) {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001056 caps.mCanAddRestrictedProfile = false;
1057 }
Sudheer Shankaaa524ae2016-03-14 17:23:13 -07001058 caps.updateAddUserCapabilities(context);
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001059 return caps;
1060 }
1061
Sudheer Shankaaa524ae2016-03-14 17:23:13 -07001062 public void updateAddUserCapabilities(Context context) {
1063 mEnforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
1064 UserManager.DISALLOW_ADD_USER, UserHandle.myUserId());
1065 final boolean hasBaseUserRestriction = RestrictedLockUtils.hasBaseUserRestriction(
1066 context, UserManager.DISALLOW_ADD_USER, UserHandle.myUserId());
1067 mDisallowAddUserSetByAdmin =
1068 mEnforcedAdmin != null && !hasBaseUserRestriction;
1069 mDisallowAddUser =
1070 (mEnforcedAdmin != null || hasBaseUserRestriction);
1071 mCanAddUser = true;
1072 if (!mIsAdmin || UserManager.getMaxSupportedUsers() < 2
1073 || !UserManager.supportsMultipleUsers()
1074 || mDisallowAddUser) {
1075 mCanAddUser = false;
1076 }
1077
1078 final boolean canAddUsersWhenLocked = mIsAdmin || Settings.Global.getInt(
1079 context.getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1;
1080 mCanAddGuest = !mIsGuest && !mDisallowAddUser && canAddUsersWhenLocked;
1081 }
1082
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001083 @Override
1084 public String toString() {
1085 return "UserCapabilities{" +
1086 "mEnabled=" + mEnabled +
1087 ", mCanAddUser=" + mCanAddUser +
1088 ", mCanAddRestrictedProfile=" + mCanAddRestrictedProfile +
Xiaohui Chen64f530c2015-06-24 12:35:07 -07001089 ", mIsAdmin=" + mIsAdmin +
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001090 ", mIsGuest=" + mIsGuest +
Sudheer Shanka682a9162016-01-13 22:10:18 +00001091 ", mCanAddGuest=" + mCanAddGuest +
1092 ", mDisallowAddUser=" + mDisallowAddUser +
1093 ", mEnforcedAdmin=" + mEnforcedAdmin +
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001094 '}';
1095 }
1096 }
1097
Jason Monk9d80eea2015-11-20 14:27:51 -05001098 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
1099
1100 private final Context mContext;
1101 private final SummaryLoader mSummaryLoader;
1102
1103 public SummaryProvider(Context context, SummaryLoader summaryLoader) {
1104 mContext = context;
1105 mSummaryLoader = summaryLoader;
1106 }
1107
1108 @Override
1109 public void setListening(boolean listening) {
1110 if (listening) {
1111 UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo(
1112 UserHandle.myUserId());
1113 mSummaryLoader.setSummary(this, mContext.getString(R.string.user_summary,
1114 info.name));
1115 }
1116 }
1117 }
1118
1119 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
1120 = new SummaryLoader.SummaryProviderFactory() {
1121 @Override
1122 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
1123 SummaryLoader summaryLoader) {
1124 return new SummaryProvider(activity, summaryLoader);
1125 }
1126 };
1127
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001128 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
1129 new BaseSearchIndexProvider() {
1130 @Override
1131 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
1132 boolean enabled) {
1133 final List<SearchIndexableRaw> result = new ArrayList<>();
1134 final UserCapabilities userCaps = UserCapabilities.create(context);
1135 if (!userCaps.mEnabled) {
1136 return result;
1137 }
1138 final Resources res = context.getResources();
1139 SearchIndexableRaw data = new SearchIndexableRaw(context);
1140 data.title = res.getString(R.string.user_settings_title);
1141 data.screenTitle = res.getString(R.string.user_settings_title);
1142 result.add(data);
1143
Sudheer Shanka29737ad2016-02-11 17:17:21 +00001144 if (userCaps.mCanAddUser || userCaps.mDisallowAddUserSetByAdmin) {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001145 data = new SearchIndexableRaw(context);
1146 data.title = res.getString(userCaps.mCanAddRestrictedProfile ?
1147 R.string.user_add_user_or_profile_menu
1148 : R.string.user_add_user_menu);
1149 data.screenTitle = res.getString(R.string.user_settings_title);
1150 result.add(data);
1151 }
1152 return result;
1153 }
1154 };
1155
Amith Yamasanib810a0d2012-03-25 10:12:26 -07001156}