blob: dd24ea546776cd66f406e79a054bf6716ef3accb [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
Udam Saini679f7ad2016-03-25 10:47:00 -0700227 if (mUserCaps.mCanAddUser && Utils.isDeviceProvisioned(getActivity())) {
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("")) {
Sudheer Shanka71caf692016-04-14 16:50:14 -0700334 // Assign profile photo.
335 Utils.copyMeProfilePhoto(getActivity(), user);
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700336 }
Amith Yamasani2d578ce2014-07-25 09:37:33 -0700337 return user.name;
Amith Yamasani32dccbc2012-09-13 18:06:49 -0700338 }
339 }.execute();
340 }
341
342 private void finishLoadProfile(String profileName) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700343 if (getActivity() == null) return;
344 mMePreference.setTitle(getString(R.string.user_you, profileName));
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700345 int myUserId = UserHandle.myUserId();
346 Bitmap b = mUserManager.getUserIcon(myUserId);
347 if (b != null) {
Amith Yamasani864e3b72013-03-27 12:27:01 -0700348 mMePreference.setIcon(encircle(b));
349 mUserIcons.put(myUserId, b);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700350 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700351 }
352
Amith Yamasanif162efe2013-04-29 10:43:05 -0700353 private boolean hasLockscreenSecurity() {
354 LockPatternUtils lpu = new LockPatternUtils(getActivity());
Adrian Roos54375882015-04-16 17:11:22 -0700355 return lpu.isSecure(UserHandle.myUserId());
Amith Yamasanif162efe2013-04-29 10:43:05 -0700356 }
357
358 private void launchChooseLockscreen() {
359 Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
360 chooseLockIntent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY,
361 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
362 startActivityForResult(chooseLockIntent, REQUEST_CHOOSE_LOCK);
363 }
364
365 @Override
366 public void onActivityResult(int requestCode, int resultCode, Intent data) {
367 super.onActivityResult(requestCode, resultCode, data);
368
369 if (requestCode == REQUEST_CHOOSE_LOCK) {
370 if (resultCode != Activity.RESULT_CANCELED && hasLockscreenSecurity()) {
371 addUserNow(USER_TYPE_RESTRICTED_PROFILE);
Amith Yamasanif162efe2013-04-29 10:43:05 -0700372 }
Amith Yamasaniee398742014-07-24 14:13:36 -0700373 } else {
374 mEditUserInfoController.onActivityResult(requestCode, resultCode, data);
Amith Yamasanif162efe2013-04-29 10:43:05 -0700375 }
376 }
377
Amith Yamasanicc395692013-04-11 18:21:07 -0700378 private void onAddUserClicked(int userType) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700379 synchronized (mUserLock) {
380 if (mRemovingUserId == -1 && !mAddingUser) {
Amith Yamasanicc395692013-04-11 18:21:07 -0700381 switch (userType) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700382 case USER_TYPE_USER:
383 showDialog(DIALOG_ADD_USER);
Amith Yamasanicc395692013-04-11 18:21:07 -0700384 break;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700385 case USER_TYPE_RESTRICTED_PROFILE:
Amith Yamasanif162efe2013-04-29 10:43:05 -0700386 if (hasLockscreenSecurity()) {
387 addUserNow(USER_TYPE_RESTRICTED_PROFILE);
388 } else {
389 showDialog(DIALOG_NEED_LOCKSCREEN);
390 }
Amith Yamasanicc395692013-04-11 18:21:07 -0700391 break;
392 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700393 }
394 }
395 }
396
397 private void onRemoveUserClicked(int userId) {
398 synchronized (mUserLock) {
399 if (mRemovingUserId == -1 && !mAddingUser) {
400 mRemovingUserId = userId;
401 showDialog(DIALOG_CONFIRM_REMOVE);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700402 }
403 }
404 }
405
Fyodor Kupolov9938f6f2015-08-26 17:24:09 -0700406 private UserInfo createRestrictedProfile() {
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700407 UserInfo newUserInfo = mUserManager.createRestrictedProfile(mAddingUserName);
Sudheer Shanka71caf692016-04-14 16:50:14 -0700408 Utils.assignDefaultPhoto(getActivity(), newUserInfo.id);
Amith Yamasanicc395692013-04-11 18:21:07 -0700409 return newUserInfo;
410 }
411
412 private UserInfo createTrustedUser() {
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700413 UserInfo newUserInfo = mUserManager.createUser(mAddingUserName, 0);
Amith Yamasanicc395692013-04-11 18:21:07 -0700414 if (newUserInfo != null) {
Sudheer Shanka71caf692016-04-14 16:50:14 -0700415 Utils.assignDefaultPhoto(getActivity(), newUserInfo.id);
Amith Yamasanicc395692013-04-11 18:21:07 -0700416 }
417 return newUserInfo;
418 }
419
420 private void onManageUserClicked(int userId, boolean newUser) {
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700421 mAddingUser = false;
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530422 if (userId == UserPreference.USERID_GUEST_DEFAULTS) {
423 Bundle extras = new Bundle();
424 extras.putBoolean(UserDetailsSettings.EXTRA_USER_GUEST, true);
425 ((SettingsActivity) getActivity()).startPreferencePanel(
426 UserDetailsSettings.class.getName(),
427 extras, R.string.user_guest, null, null, 0);
428 return;
429 }
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700430 UserInfo info = mUserManager.getUserInfo(userId);
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700431 if (info.isRestricted() && mUserCaps.mIsAdmin) {
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700432 Bundle extras = new Bundle();
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700433 extras.putInt(RestrictedProfileSettings.EXTRA_USER_ID, userId);
434 extras.putBoolean(RestrictedProfileSettings.EXTRA_NEW_USER, newUser);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800435 ((SettingsActivity) getActivity()).startPreferencePanel(
Amith Yamasani0ca80fe2013-06-03 16:41:03 -0700436 RestrictedProfileSettings.class.getName(),
Amith Yamasani7c4dfcc2013-05-22 12:51:30 -0700437 extras, R.string.user_restrictions_title, null,
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700438 null, 0);
439 } else if (info.id == UserHandle.myUserId()) {
440 // Jump to owner info panel
Jason Monk3bcd76c2015-04-21 11:20:20 -0400441 OwnerInfoSettings.show(this);
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700442 } else if (mUserCaps.mIsAdmin) {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530443 Bundle extras = new Bundle();
444 extras.putInt(UserDetailsSettings.EXTRA_USER_ID, userId);
445 ((SettingsActivity) getActivity()).startPreferencePanel(
446 UserDetailsSettings.class.getName(),
Amith Yamasani51c6dac2014-07-02 00:06:37 +0530447 extras,
448 -1, /* No title res id */
449 info.name, /* title */
450 null, /* resultTo */
451 0 /* resultRequestCode */);
Amith Yamasanicc395692013-04-11 18:21:07 -0700452 }
Amith Yamasani233592b2013-03-01 17:05:23 -0800453 }
454
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700455 private void onUserCreated(int userId) {
456 mAddedUserId = userId;
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700457 mAddingUser = false;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700458 if (mUserManager.getUserInfo(userId).isRestricted()) {
459 showDialog(DIALOG_SETUP_PROFILE);
460 } else {
461 showDialog(DIALOG_SETUP_USER);
462 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700463 }
464
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700465 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700466 public void onDialogShowing() {
467 super.onDialogShowing();
468
469 setOnDismissListener(this);
470 }
471
472 @Override
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700473 public Dialog onCreateDialog(int dialogId) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700474 Context context = getActivity();
475 if (context == null) return null;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700476 switch (dialogId) {
Amith Yamasanic861cf82012-10-02 14:51:46 -0700477 case DIALOG_CONFIRM_REMOVE: {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530478 Dialog dlg =
Fyodor Kupolovdbbd4fc2014-12-29 13:40:27 -0800479 UserDialogs.createRemoveDialog(getActivity(), mRemovingUserId,
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530480 new DialogInterface.OnClickListener() {
481 public void onClick(DialogInterface dialog, int which) {
482 removeUserNow();
483 }
484 }
485 );
Amith Yamasanic861cf82012-10-02 14:51:46 -0700486 return dlg;
487 }
Amith Yamasani1a634d32012-09-27 23:01:35 -0700488 case DIALOG_USER_CANNOT_MANAGE:
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700489 return new AlertDialog.Builder(context)
Amith Yamasani1a634d32012-09-27 23:01:35 -0700490 .setMessage(R.string.user_cannot_manage_message)
491 .setPositiveButton(android.R.string.ok, null)
492 .create();
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700493 case DIALOG_ADD_USER: {
Svetoslav Ganov263f7442012-09-25 16:11:16 -0700494 final SharedPreferences preferences = getActivity().getPreferences(
495 Context.MODE_PRIVATE);
496 final boolean longMessageDisplayed = preferences.getBoolean(
497 KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, false);
498 final int messageResId = longMessageDisplayed
499 ? R.string.user_add_user_message_short
500 : R.string.user_add_user_message_long;
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700501 final int userType = dialogId == DIALOG_ADD_USER
502 ? USER_TYPE_USER : USER_TYPE_RESTRICTED_PROFILE;
503 Dialog dlg = new AlertDialog.Builder(context)
504 .setTitle(R.string.user_add_user_title)
505 .setMessage(messageResId)
506 .setPositiveButton(android.R.string.ok,
507 new DialogInterface.OnClickListener() {
508 public void onClick(DialogInterface dialog, int which) {
509 addUserNow(userType);
510 if (!longMessageDisplayed) {
511 preferences.edit().putBoolean(
512 KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, true).apply();
513 }
Svetoslav Ganov263f7442012-09-25 16:11:16 -0700514 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700515 })
516 .setNegativeButton(android.R.string.cancel, null)
517 .create();
Amith Yamasanic861cf82012-10-02 14:51:46 -0700518 return dlg;
519 }
520 case DIALOG_SETUP_USER: {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700521 Dialog dlg = new AlertDialog.Builder(context)
522 .setTitle(R.string.user_setup_dialog_title)
523 .setMessage(R.string.user_setup_dialog_message)
524 .setPositiveButton(R.string.user_setup_button_setup_now,
525 new DialogInterface.OnClickListener() {
526 public void onClick(DialogInterface dialog, int which) {
527 switchUserNow(mAddedUserId);
528 }
529 })
530 .setNegativeButton(R.string.user_setup_button_setup_later, null)
531 .create();
532 return dlg;
533 }
534 case DIALOG_SETUP_PROFILE: {
535 Dialog dlg = new AlertDialog.Builder(context)
536 .setMessage(R.string.user_setup_profile_dialog_message)
537 .setPositiveButton(android.R.string.ok,
538 new DialogInterface.OnClickListener() {
539 public void onClick(DialogInterface dialog, int which) {
540 switchUserNow(mAddedUserId);
541 }
542 })
543 .setNegativeButton(android.R.string.cancel, null)
544 .create();
545 return dlg;
546 }
547 case DIALOG_CHOOSE_USER_TYPE: {
548 List<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
549 HashMap<String,String> addUserItem = new HashMap<String,String>();
550 addUserItem.put(KEY_TITLE, getString(R.string.user_add_user_item_title));
551 addUserItem.put(KEY_SUMMARY, getString(R.string.user_add_user_item_summary));
552 HashMap<String,String> addProfileItem = new HashMap<String,String>();
553 addProfileItem.put(KEY_TITLE, getString(R.string.user_add_profile_item_title));
554 addProfileItem.put(KEY_SUMMARY, getString(R.string.user_add_profile_item_summary));
555 data.add(addUserItem);
556 data.add(addProfileItem);
Amith Yamasanif3db0212014-12-02 17:21:23 -0800557 AlertDialog.Builder builder = new AlertDialog.Builder(context);
558 SimpleAdapter adapter = new SimpleAdapter(builder.getContext(),
559 data, R.layout.two_line_list_item,
560 new String[] {KEY_TITLE, KEY_SUMMARY},
561 new int[] {R.id.title, R.id.summary});
562 builder.setTitle(R.string.user_add_user_type_title);
563 builder.setAdapter(adapter,
564 new DialogInterface.OnClickListener() {
565 @Override
566 public void onClick(DialogInterface dialog, int which) {
567 onAddUserClicked(which == 0
568 ? USER_TYPE_USER
569 : USER_TYPE_RESTRICTED_PROFILE);
570 }
571 });
572 return builder.create();
Amith Yamasanic861cf82012-10-02 14:51:46 -0700573 }
Amith Yamasanif162efe2013-04-29 10:43:05 -0700574 case DIALOG_NEED_LOCKSCREEN: {
575 Dialog dlg = new AlertDialog.Builder(context)
576 .setMessage(R.string.user_need_lock_message)
577 .setPositiveButton(R.string.user_set_lock_button,
578 new DialogInterface.OnClickListener() {
579 @Override
580 public void onClick(DialogInterface dialog, int which) {
581 launchChooseLockscreen();
582 }
583 })
584 .setNegativeButton(android.R.string.cancel, null)
585 .create();
586 return dlg;
587 }
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700588 case DIALOG_CONFIRM_EXIT_GUEST: {
589 Dialog dlg = new AlertDialog.Builder(context)
590 .setTitle(R.string.user_exit_guest_confirm_title)
591 .setMessage(R.string.user_exit_guest_confirm_message)
Amith Yamasani94c01812014-08-18 16:42:43 -0700592 .setPositiveButton(R.string.user_exit_guest_dialog_remove,
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700593 new DialogInterface.OnClickListener() {
594 @Override
595 public void onClick(DialogInterface dialog, int which) {
596 exitGuest();
597 }
598 })
Amith Yamasani94c01812014-08-18 16:42:43 -0700599 .setNegativeButton(android.R.string.cancel, null)
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700600 .create();
601 return dlg;
602 }
Amith Yamasaniee398742014-07-24 14:13:36 -0700603 case DIALOG_USER_PROFILE_EDITOR: {
604 Dialog dlg = mEditUserInfoController.createDialog(
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700605 this,
Amith Yamasaniee398742014-07-24 14:13:36 -0700606 mMePreference.getIcon(),
607 mMePreference.getTitle(),
608 R.string.profile_info_settings_title,
609 this /* callback */,
610 android.os.Process.myUserHandle());
611 return dlg;
612 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700613 default:
614 return null;
615 }
616 }
617
mariagpuyol60435fc2016-01-28 14:58:03 -0800618 private boolean emergencyInfoActivityPresent() {
619 Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO).setPackage("com.android.emergency");
620 List<ResolveInfo> infos = getContext().getPackageManager().queryIntentActivities(intent, 0);
621 if (infos == null || infos.isEmpty()) {
622 return false;
623 }
624 return true;
625 }
626
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700627 private void removeUserNow() {
628 if (mRemovingUserId == UserHandle.myUserId()) {
629 removeThisUser();
630 } else {
631 new Thread() {
632 public void run() {
633 synchronized (mUserLock) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700634 mUserManager.removeUser(mRemovingUserId);
635 mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700636 }
637 }
638 }.start();
639 }
640 }
641
642 private void removeThisUser() {
Fyodor Kupolov52ae0532016-02-24 15:54:10 -0800643 if (!mUserManager.canSwitchUsers()) {
644 Log.w(TAG, "Cannot remove current user when switching is disabled");
645 return;
646 }
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700647 try {
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700648 ActivityManagerNative.getDefault().switchUser(UserHandle.USER_SYSTEM);
Fyodor Kupolov52ae0532016-02-24 15:54:10 -0800649 getContext().getSystemService(UserManager.class).removeUser(UserHandle.myUserId());
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700650 } catch (RemoteException re) {
651 Log.e(TAG, "Unable to remove self user");
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700652 }
653 }
654
Amith Yamasanicc395692013-04-11 18:21:07 -0700655 private void addUserNow(final int userType) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700656 synchronized (mUserLock) {
657 mAddingUser = true;
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700658 mAddingUserName = userType == USER_TYPE_USER ? getString(R.string.user_new_user_name)
659 : getString(R.string.user_new_profile_name);
Amith Yamasania98c4022013-04-19 15:27:52 -0700660 //updateUserList();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700661 new Thread() {
662 public void run() {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700663 UserInfo user;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700664 // Could take a few seconds
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700665 if (userType == USER_TYPE_USER) {
Amith Yamasanicc395692013-04-11 18:21:07 -0700666 user = createTrustedUser();
667 } else {
Fyodor Kupolov9938f6f2015-08-26 17:24:09 -0700668 user = createRestrictedProfile();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700669 }
Sudheer Shankaaa524ae2016-03-14 17:23:13 -0700670 if (user == null) {
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700671 mAddingUser = false;
Sudheer Shankaaa524ae2016-03-14 17:23:13 -0700672 return;
673 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700674 synchronized (mUserLock) {
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700675 if (userType == USER_TYPE_USER) {
Amith Yamasania98c4022013-04-19 15:27:52 -0700676 mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
Amith Yamasanicc395692013-04-11 18:21:07 -0700677 mHandler.sendMessage(mHandler.obtainMessage(
678 MESSAGE_SETUP_USER, user.id, user.serialNumber));
679 } else {
680 mHandler.sendMessage(mHandler.obtainMessage(
681 MESSAGE_CONFIG_USER, user.id, user.serialNumber));
682 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700683 }
684 }
685 }.start();
686 }
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700687 }
688
Amith Yamasaniae47ef42012-09-16 17:53:35 -0700689 private void switchUserNow(int userId) {
690 try {
691 ActivityManagerNative.getDefault().switchUser(userId);
692 } catch (RemoteException re) {
693 // Nothing to do
694 }
695 }
696
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700697 /**
698 * Erase the current user (guest) and switch to another user.
699 */
700 private void exitGuest() {
701 // Just to be safe
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700702 if (!mUserCaps.mIsGuest) {
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700703 return;
704 }
705 removeThisUser();
706 }
707
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700708 private void updateUserList() {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700709 if (getActivity() == null) return;
Amith Yamasanie278eda2012-09-20 22:44:48 -0700710 List<UserInfo> users = mUserManager.getUsers(true);
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530711 final Context context = getActivity();
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700712
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530713 final boolean voiceCapable = Utils.isVoiceCapable(context);
Fyodor Kupolov55145092014-12-08 14:15:36 -0800714 final ArrayList<Integer> missingIcons = new ArrayList<>();
715 final ArrayList<UserPreference> userPreferences = new ArrayList<>();
716 userPreferences.add(mMePreference);
717
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700718 for (UserInfo user : users) {
Xiaohui Chen148ef772015-07-16 10:06:03 -0700719 if (!user.supportsSwitchToByUser()) {
720 // Only users that can be switched to should show up here.
721 // e.g. Managed profiles appear under Accounts Settings instead
Alexandra Gherghina83adb562014-07-31 19:30:41 +0100722 continue;
723 }
Fyodor Kupolov55145092014-12-08 14:15:36 -0800724 UserPreference pref;
Amith Yamasanic861cf82012-10-02 14:51:46 -0700725 if (user.id == UserHandle.myUserId()) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700726 pref = mMePreference;
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530727 } else if (user.isGuest()) {
728 // Skip over Guest. We add generic Guest settings after this loop
729 continue;
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700730 } else {
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530731 // With Telephony:
732 // Secondary user: Settings
733 // Guest: Settings
734 // Restricted Profile: There is no Restricted Profile
735 // Without Telephony:
736 // Secondary user: Delete
737 // Guest: Nothing
738 // Restricted Profile: Settings
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700739 final boolean showSettings = mUserCaps.mIsAdmin
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700740 && (voiceCapable || user.isRestricted());
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700741 final boolean showDelete = mUserCaps.mIsAdmin
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530742 && (!voiceCapable && !user.isRestricted() && !user.isGuest());
Jason Monk39b46742015-09-10 15:52:51 -0400743 pref = new UserPreference(getPrefContext(), null, user.id,
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530744 showSettings ? this : null,
745 showDelete ? this : null);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700746 pref.setOnPreferenceClickListener(this);
747 pref.setKey("id=" + user.id);
Fyodor Kupolov55145092014-12-08 14:15:36 -0800748 userPreferences.add(pref);
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700749 if (user.isAdmin()) {
750 pref.setSummary(R.string.user_admin);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700751 }
752 pref.setTitle(user.name);
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700753 }
Julia Reynolds8bda21a2016-04-29 08:42:01 -0400754 if (pref == null) {
755 continue;
756 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700757 if (!isInitialized(user)) {
Alexandra Gherghina423bafc2014-04-23 10:56:37 +0000758 if (user.isRestricted()) {
759 pref.setSummary(R.string.user_summary_restricted_not_set_up);
Alexandra Gherghina423bafc2014-04-23 10:56:37 +0000760 } else {
761 pref.setSummary(R.string.user_summary_not_set_up);
762 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700763 } else if (user.isRestricted()) {
764 pref.setSummary(R.string.user_summary_restricted_profile);
Amith Yamasani48fa8572012-04-17 17:12:01 -0700765 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700766 if (user.iconPath != null) {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700767 if (mUserIcons.get(user.id) == null) {
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100768 // Icon not loaded yet, print a placeholder
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700769 missingIcons.add(user.id);
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100770 pref.setIcon(getEncircledDefaultIcon());
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700771 } else {
772 setPhotoId(pref, user);
773 }
Amith Yamasanif2663ab2014-08-11 17:17:29 -0700774 } else {
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100775 // Icon not available yet, print a placeholder
776 pref.setIcon(getEncircledDefaultIcon());
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700777 }
778 }
Amith Yamasani247520c2014-07-25 12:52:29 -0700779
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700780 // Add a temporary entry for the user being created
781 if (mAddingUser) {
Jason Monk39b46742015-09-10 15:52:51 -0400782 UserPreference pref = new UserPreference(getPrefContext(), null,
Fyodor Kupolov55145092014-12-08 14:15:36 -0800783 UserPreference.USERID_UNKNOWN, null, null);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700784 pref.setEnabled(false);
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700785 pref.setTitle(mAddingUserName);
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100786 pref.setIcon(getEncircledDefaultIcon());
Fyodor Kupolov55145092014-12-08 14:15:36 -0800787 userPreferences.add(pref);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700788 }
Amith Yamasani9e6ac3d2014-06-30 15:31:37 +0530789
Fyodor Kupolov889bc3d2016-01-29 17:29:54 -0800790 // Check if Guest tile should be added.
Sudheer Shanka29737ad2016-02-11 17:17:21 +0000791 if (!mUserCaps.mIsGuest && (mUserCaps.mCanAddGuest ||
792 mUserCaps.mDisallowAddUserSetByAdmin)) {
793 // Add a virtual Guest user for guest defaults
Jason Monk39b46742015-09-10 15:52:51 -0400794 UserPreference pref = new UserPreference(getPrefContext(), null,
Fyodor Kupolov55145092014-12-08 14:15:36 -0800795 UserPreference.USERID_GUEST_DEFAULTS,
Xiaohui Chen64f530c2015-06-24 12:35:07 -0700796 mUserCaps.mIsAdmin && voiceCapable? this : null /* settings icon handler */,
Fyodor Kupolov55145092014-12-08 14:15:36 -0800797 null /* delete icon handler */);
798 pref.setTitle(R.string.user_guest);
799 pref.setIcon(getEncircledDefaultIcon());
Fyodor Kupolov55145092014-12-08 14:15:36 -0800800 userPreferences.add(pref);
Sudheer Shanka682a9162016-01-13 22:10:18 +0000801 pref.setDisabledByAdmin(
802 mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null);
Fyodor Kupolov55145092014-12-08 14:15:36 -0800803 }
804
805 // Sort list of users by serialNum
806 Collections.sort(userPreferences, UserPreference.SERIAL_NUMBER_COMPARATOR);
807
808 getActivity().invalidateOptionsMenu();
809
810 // Load the icons
811 if (missingIcons.size() > 0) {
812 loadIconsAsync(missingIcons);
813 }
814
815 PreferenceScreen preferenceScreen = getPreferenceScreen();
816 preferenceScreen.removeAll();
817
818 // If profiles are supported, userPreferences will be added to the category labeled
819 // "User & Profiles", otherwise the category is skipped and elements are added directly
820 // to preferenceScreen
821 PreferenceGroup groupToAddUsers;
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700822 if (mUserCaps.mCanAddRestrictedProfile) {
Fyodor Kupolov55145092014-12-08 14:15:36 -0800823 mUserListCategory.removeAll();
824 mUserListCategory.setOrder(Preference.DEFAULT_ORDER);
825 preferenceScreen.addPreference(mUserListCategory);
826 groupToAddUsers = mUserListCategory;
827 } else {
828 groupToAddUsers = preferenceScreen;
829 }
830 for (UserPreference userPreference : userPreferences) {
831 userPreference.setOrder(Preference.DEFAULT_ORDER);
832 groupToAddUsers.addPreference(userPreference);
833 }
834
835 // Append Add user to the end of the list
Udam Saini679f7ad2016-03-25 10:47:00 -0700836 if ((mUserCaps.mCanAddUser || mUserCaps.mDisallowAddUserSetByAdmin) &&
837 Utils.isDeviceProvisioned(getActivity())) {
Fyodor Kupolov55145092014-12-08 14:15:36 -0800838 boolean moreUsers = mUserManager.canAddMoreUsers();
Fyodor Kupolov55145092014-12-08 14:15:36 -0800839 mAddUser.setOrder(Preference.DEFAULT_ORDER);
840 preferenceScreen.addPreference(mAddUser);
Fyodor Kupolovfe6cb6d2016-03-28 15:10:19 -0700841 mAddUser.setEnabled(moreUsers && !mAddingUser);
Fyodor Kupolova7a7bfe2015-05-08 15:54:38 -0700842 if (!moreUsers) {
843 mAddUser.setSummary(getString(R.string.user_add_max_count, getMaxRealUsers()));
844 } else {
845 mAddUser.setSummary(null);
846 }
Sudheer Shanka682a9162016-01-13 22:10:18 +0000847 if (mAddUser.isEnabled()) {
848 mAddUser.setDisabledByAdmin(
849 mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null);
850 }
Fyodor Kupolov55145092014-12-08 14:15:36 -0800851 }
Sudheer Shanka29737ad2016-02-11 17:17:21 +0000852 if (mUserCaps.mIsAdmin &&
853 (!mUserCaps.mDisallowAddUser || mUserCaps.mDisallowAddUserSetByAdmin)) {
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -0800854 mLockScreenSettings.setOrder(Preference.DEFAULT_ORDER);
855 preferenceScreen.addPreference(mLockScreenSettings);
856 mAddUserWhenLocked.setChecked(Settings.Global.getInt(getContentResolver(),
857 Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1);
858 mAddUserWhenLocked.setOnPreferenceChangeListener(this);
Sudheer Shanka682a9162016-01-13 22:10:18 +0000859 mAddUserWhenLocked.setDisabledByAdmin(
860 mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null);
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -0800861 }
mariagpuyolfe19bc32016-02-17 19:23:24 -0800862
863 if (emergencyInfoActivityPresent()) {
864 mEmergencyInfoPreference.setOnPreferenceClickListener(this);
865 mEmergencyInfoPreference.setOrder(Preference.DEFAULT_ORDER);
866 preferenceScreen.addPreference(mEmergencyInfoPreference);
867 }
Fyodor Kupolov55145092014-12-08 14:15:36 -0800868 }
869
Fyodor Kupolova7a7bfe2015-05-08 15:54:38 -0700870 private int getMaxRealUsers() {
871 // guest is not counted against getMaxSupportedUsers() number
872 final int maxUsersAndGuest = UserManager.getMaxSupportedUsers() + 1;
873 final List<UserInfo> users = mUserManager.getUsers();
874 // managed profiles are counted against getMaxSupportedUsers()
875 int managedProfiles = 0;
876 for (UserInfo user : users) {
877 if (user.isManagedProfile()) {
878 managedProfiles++;
879 }
880 }
881 return maxUsersAndGuest - managedProfiles;
882 }
883
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700884 private void loadIconsAsync(List<Integer> missingIcons) {
885 new AsyncTask<List<Integer>, Void, Void>() {
886 @Override
887 protected void onPostExecute(Void result) {
888 updateUserList();
889 }
890
891 @Override
892 protected Void doInBackground(List<Integer>... values) {
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700893 for (int userId : values[0]) {
894 Bitmap bitmap = mUserManager.getUserIcon(userId);
Amith Yamasanif2663ab2014-08-11 17:17:29 -0700895 if (bitmap == null) {
Zoltan Szatmary-Bane5814ff2014-12-19 16:27:45 +0000896 bitmap = Utils.getDefaultUserIconAsBitmap(userId);
Amith Yamasanif2663ab2014-08-11 17:17:29 -0700897 }
Amith Yamasani864e3b72013-03-27 12:27:01 -0700898 mUserIcons.append(userId, bitmap);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700899 }
900 return null;
901 }
902 }.execute(missingIcons);
Amith Yamasanif34a85d2012-09-17 18:31:45 -0700903 }
Amith Yamasani5cdc9372012-11-14 16:23:40 -0800904
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100905 private Drawable getEncircledDefaultIcon() {
906 if (mDefaultIconDrawable == null) {
Zoltan Szatmary-Bane5814ff2014-12-19 16:27:45 +0000907 mDefaultIconDrawable = encircle(Utils.getDefaultUserIconAsBitmap(UserHandle.USER_NULL));
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700908 }
Alexandra Gherghinabc6e78f2014-09-03 10:22:09 +0100909 return mDefaultIconDrawable;
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700910 }
911
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700912 private void setPhotoId(Preference pref, UserInfo user) {
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700913 Bitmap bitmap = mUserIcons.get(user.id);
Amith Yamasani864e3b72013-03-27 12:27:01 -0700914 if (bitmap != null) {
915 pref.setIcon(encircle(bitmap));
Amith Yamasanie940dac2012-09-17 10:21:27 -0700916 }
Amith Yamasani48fa8572012-04-17 17:12:01 -0700917 }
918
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700919 @Override
920 public boolean onPreferenceClick(Preference pref) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700921 if (pref == mMePreference) {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700922 if (mUserCaps.mIsGuest) {
Amith Yamasanic16b73e2014-07-17 19:13:51 -0700923 showDialog(DIALOG_CONFIRM_EXIT_GUEST);
924 return true;
925 }
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700926 // If this is a limited user, launch the user info settings instead of profile editor
927 if (mUserManager.isLinkedUser()) {
928 onManageUserClicked(UserHandle.myUserId(), false);
929 } else {
Amith Yamasaniee398742014-07-24 14:13:36 -0700930 showDialog(DIALOG_USER_PROFILE_EDITOR);
Amith Yamasanid4abdd22013-04-17 18:27:01 -0700931 }
Amith Yamasani1a634d32012-09-27 23:01:35 -0700932 } else if (pref instanceof UserPreference) {
933 int userId = ((UserPreference) pref).getUserId();
Fyodor Kupolov889bc3d2016-01-29 17:29:54 -0800934 // Get the latest status of the user
935 UserInfo user = mUserManager.getUserInfo(userId);
936 if (!isInitialized(user)) {
937 mHandler.sendMessage(mHandler.obtainMessage(
938 MESSAGE_SETUP_USER, user.id, user.serialNumber));
Amith Yamasani1a634d32012-09-27 23:01:35 -0700939 }
Amith Yamasanifc8a3832013-04-23 11:44:59 -0700940 } else if (pref == mAddUser) {
Amith Yamasani0e43d322014-05-07 10:19:34 -0700941 // If we allow both types, show a picker, otherwise directly go to
942 // flow for full user.
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -0700943 if (mUserCaps.mCanAddRestrictedProfile) {
Amith Yamasani0e43d322014-05-07 10:19:34 -0700944 showDialog(DIALOG_CHOOSE_USER_TYPE);
945 } else {
946 onAddUserClicked(USER_TYPE_USER);
947 }
mariagpuyol60435fc2016-01-28 14:58:03 -0800948 } else if (pref == mEmergencyInfoPreference) {
Maria Garcia Puyolda497f22016-03-17 22:52:44 +0000949 Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO);
mariagpuyol6ca89402016-03-30 18:04:54 -0700950 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Maria Garcia Puyolda497f22016-03-17 22:52:44 +0000951 startActivity(intent);
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700952 }
953 return false;
954 }
955
Amith Yamasani1a634d32012-09-27 23:01:35 -0700956 private boolean isInitialized(UserInfo user) {
957 return (user.flags & UserInfo.FLAG_INITIALIZED) != 0;
958 }
959
Amith Yamasani864e3b72013-03-27 12:27:01 -0700960 private Drawable encircle(Bitmap icon) {
961 Drawable circled = CircleFramedDrawable.getInstance(getActivity(), icon);
962 return circled;
963 }
964
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700965 @Override
966 public void onClick(View v) {
967 if (v.getTag() instanceof UserPreference) {
968 int userId = ((UserPreference) v.getTag()).getUserId();
Amith Yamasani233592b2013-03-01 17:05:23 -0800969 switch (v.getId()) {
970 case UserPreference.DELETE_ID:
Sudheer Shankab5c92142016-03-22 17:24:07 -0700971 final EnforcedAdmin removeDisallowedAdmin =
972 RestrictedLockUtils.checkIfRestrictionEnforced(getContext(),
973 UserManager.DISALLOW_REMOVE_USER, UserHandle.myUserId());
974 if (removeDisallowedAdmin != null) {
975 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
976 removeDisallowedAdmin);
977 } else {
978 onRemoveUserClicked(userId);
979 }
Amith Yamasani233592b2013-03-01 17:05:23 -0800980 break;
981 case UserPreference.SETTINGS_ID:
Amith Yamasanicc395692013-04-11 18:21:07 -0700982 onManageUserClicked(userId, false);
Amith Yamasani233592b2013-03-01 17:05:23 -0800983 break;
984 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700985 }
986 }
987
988 @Override
989 public void onDismiss(DialogInterface dialog) {
990 synchronized (mUserLock) {
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700991 mRemovingUserId = -1;
Amith Yamasanic861cf82012-10-02 14:51:46 -0700992 updateUserList();
Amith Yamasanidc6bfa62012-09-06 18:04:06 -0700993 }
994 }
995
996 @Override
997 public boolean onPreferenceChange(Preference preference, Object newValue) {
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -0800998 if (preference == mAddUserWhenLocked) {
999 Boolean value = (Boolean) newValue;
1000 Settings.Global.putInt(getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED,
1001 value != null && value ? 1 : 0);
Amith Yamasanib810a0d2012-03-25 10:12:26 -07001002 return true;
1003 }
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -08001004
Amith Yamasanib810a0d2012-03-25 10:12:26 -07001005 return false;
1006 }
Amith Yamasanidc6bfa62012-09-06 18:04:06 -07001007
Amith Yamasani38223dc2012-09-18 09:51:07 -07001008 @Override
1009 public int getHelpResource() {
1010 return R.string.help_url_users;
1011 }
Amith Yamasaniee398742014-07-24 14:13:36 -07001012
1013 @Override
1014 public void onPhotoChanged(Drawable photo) {
1015 mMePreference.setIcon(photo);
1016 }
1017
1018 @Override
1019 public void onLabelChanged(CharSequence label) {
1020 mMePreference.setTitle(label);
1021 }
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001022
1023 private static class UserCapabilities {
1024 boolean mEnabled = true;
1025 boolean mCanAddUser = true;
1026 boolean mCanAddRestrictedProfile = true;
Xiaohui Chen64f530c2015-06-24 12:35:07 -07001027 boolean mIsAdmin;
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001028 boolean mIsGuest;
Fyodor Kupolov097936a2015-05-21 18:31:45 -07001029 boolean mCanAddGuest;
Fyodor Kupolov8ba52d62016-01-05 14:30:03 -08001030 boolean mDisallowAddUser;
Sudheer Shanka29737ad2016-02-11 17:17:21 +00001031 boolean mDisallowAddUserSetByAdmin;
Sudheer Shanka682a9162016-01-13 22:10:18 +00001032 EnforcedAdmin mEnforcedAdmin;
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001033
Xiaohui Chen64f530c2015-06-24 12:35:07 -07001034 private UserCapabilities() {}
1035
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001036 public static UserCapabilities create(Context context) {
1037 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
1038 UserCapabilities caps = new UserCapabilities();
1039 if (!UserManager.supportsMultipleUsers() || Utils.isMonkeyRunning()) {
1040 caps.mEnabled = false;
1041 return caps;
1042 }
1043
Xiaohui Chen64f530c2015-06-24 12:35:07 -07001044 final UserInfo myUserInfo = userManager.getUserInfo(UserHandle.myUserId());
1045 caps.mIsGuest = myUserInfo.isGuest();
1046 caps.mIsAdmin = myUserInfo.isAdmin();
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001047 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
1048 Context.DEVICE_POLICY_SERVICE);
1049 // No restricted profiles for tablets with a device owner, or phones.
Makoto Onuki4cfe39f2015-11-19 13:47:55 -08001050 if (dpm.isDeviceManaged() || Utils.isVoiceCapable(context)) {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001051 caps.mCanAddRestrictedProfile = false;
1052 }
Sudheer Shankaaa524ae2016-03-14 17:23:13 -07001053 caps.updateAddUserCapabilities(context);
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001054 return caps;
1055 }
1056
Sudheer Shankaaa524ae2016-03-14 17:23:13 -07001057 public void updateAddUserCapabilities(Context context) {
1058 mEnforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
1059 UserManager.DISALLOW_ADD_USER, UserHandle.myUserId());
1060 final boolean hasBaseUserRestriction = RestrictedLockUtils.hasBaseUserRestriction(
1061 context, UserManager.DISALLOW_ADD_USER, UserHandle.myUserId());
1062 mDisallowAddUserSetByAdmin =
1063 mEnforcedAdmin != null && !hasBaseUserRestriction;
1064 mDisallowAddUser =
1065 (mEnforcedAdmin != null || hasBaseUserRestriction);
1066 mCanAddUser = true;
1067 if (!mIsAdmin || UserManager.getMaxSupportedUsers() < 2
1068 || !UserManager.supportsMultipleUsers()
1069 || mDisallowAddUser) {
1070 mCanAddUser = false;
1071 }
1072
1073 final boolean canAddUsersWhenLocked = mIsAdmin || Settings.Global.getInt(
1074 context.getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1;
1075 mCanAddGuest = !mIsGuest && !mDisallowAddUser && canAddUsersWhenLocked;
1076 }
1077
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001078 @Override
1079 public String toString() {
1080 return "UserCapabilities{" +
1081 "mEnabled=" + mEnabled +
1082 ", mCanAddUser=" + mCanAddUser +
1083 ", mCanAddRestrictedProfile=" + mCanAddRestrictedProfile +
Xiaohui Chen64f530c2015-06-24 12:35:07 -07001084 ", mIsAdmin=" + mIsAdmin +
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001085 ", mIsGuest=" + mIsGuest +
Sudheer Shanka682a9162016-01-13 22:10:18 +00001086 ", mCanAddGuest=" + mCanAddGuest +
1087 ", mDisallowAddUser=" + mDisallowAddUser +
1088 ", mEnforcedAdmin=" + mEnforcedAdmin +
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001089 '}';
1090 }
1091 }
1092
Jason Monk9d80eea2015-11-20 14:27:51 -05001093 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
1094
1095 private final Context mContext;
1096 private final SummaryLoader mSummaryLoader;
1097
1098 public SummaryProvider(Context context, SummaryLoader summaryLoader) {
1099 mContext = context;
1100 mSummaryLoader = summaryLoader;
1101 }
1102
1103 @Override
1104 public void setListening(boolean listening) {
1105 if (listening) {
1106 UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo(
1107 UserHandle.myUserId());
1108 mSummaryLoader.setSummary(this, mContext.getString(R.string.user_summary,
1109 info.name));
1110 }
1111 }
1112 }
1113
1114 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
1115 = new SummaryLoader.SummaryProviderFactory() {
1116 @Override
1117 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
1118 SummaryLoader summaryLoader) {
1119 return new SummaryProvider(activity, summaryLoader);
1120 }
1121 };
1122
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001123 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
1124 new BaseSearchIndexProvider() {
1125 @Override
1126 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
1127 boolean enabled) {
1128 final List<SearchIndexableRaw> result = new ArrayList<>();
1129 final UserCapabilities userCaps = UserCapabilities.create(context);
1130 if (!userCaps.mEnabled) {
1131 return result;
1132 }
1133 final Resources res = context.getResources();
1134 SearchIndexableRaw data = new SearchIndexableRaw(context);
1135 data.title = res.getString(R.string.user_settings_title);
1136 data.screenTitle = res.getString(R.string.user_settings_title);
1137 result.add(data);
1138
Sudheer Shanka29737ad2016-02-11 17:17:21 +00001139 if (userCaps.mCanAddUser || userCaps.mDisallowAddUserSetByAdmin) {
Fyodor Kupolovc9a95ef2015-05-15 17:16:15 -07001140 data = new SearchIndexableRaw(context);
1141 data.title = res.getString(userCaps.mCanAddRestrictedProfile ?
1142 R.string.user_add_user_or_profile_menu
1143 : R.string.user_add_user_menu);
1144 data.screenTitle = res.getString(R.string.user_settings_title);
1145 result.add(data);
1146 }
1147 return result;
1148 }
1149 };
1150
Amith Yamasanib810a0d2012-03-25 10:12:26 -07001151}