Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings.users; |
| 18 | |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 21 | import android.app.AlertDialog; |
| 22 | import android.app.Dialog; |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 23 | import android.app.admin.DevicePolicyManager; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Amith Yamasani | 3263039 | 2012-08-10 19:31:39 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 26 | import android.content.DialogInterface; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
Svetoslav Ganov | 263f744 | 2012-09-25 16:11:16 -0700 | [diff] [blame] | 29 | import android.content.SharedPreferences; |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 30 | import android.content.pm.UserInfo; |
Amith Yamasani | 5cdc937 | 2012-11-14 16:23:40 -0800 | [diff] [blame] | 31 | import android.content.res.Resources; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 32 | import android.graphics.Bitmap; |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 33 | import android.graphics.BitmapFactory; |
Amith Yamasani | 48fa857 | 2012-04-17 17:12:01 -0700 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 35 | import android.net.Uri; |
Amith Yamasani | 32dccbc | 2012-09-13 18:06:49 -0700 | [diff] [blame] | 36 | import android.os.AsyncTask; |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 37 | import android.os.Bundle; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 38 | import android.os.Handler; |
| 39 | import android.os.Message; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 40 | import android.os.RemoteException; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 41 | import android.os.UserHandle; |
Amith Yamasani | 3263039 | 2012-08-10 19:31:39 -0700 | [diff] [blame] | 42 | import android.os.UserManager; |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 43 | import android.provider.ContactsContract; |
Fan Zhang | 66b573a | 2016-10-06 16:33:13 -0700 | [diff] [blame] | 44 | import android.provider.Settings.Global; |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 45 | import android.support.annotation.VisibleForTesting; |
| 46 | import android.support.annotation.WorkerThread; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 47 | import android.support.v7.preference.Preference; |
| 48 | import android.support.v7.preference.Preference.OnPreferenceClickListener; |
| 49 | import android.support.v7.preference.PreferenceGroup; |
| 50 | import android.support.v7.preference.PreferenceScreen; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 51 | import android.util.Log; |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 52 | import android.util.SparseArray; |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 53 | import android.view.Menu; |
| 54 | import android.view.MenuInflater; |
| 55 | import android.view.MenuItem; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 56 | import android.view.View; |
| 57 | import android.view.View.OnClickListener; |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 58 | import android.widget.SimpleAdapter; |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 59 | |
Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame] | 60 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 61 | import com.android.internal.util.UserIcons; |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 62 | import com.android.internal.widget.LockPatternUtils; |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 63 | import com.android.settings.R; |
Fabrice Di Meglio | 263bcc8 | 2014-01-17 19:17:58 -0800 | [diff] [blame] | 64 | import com.android.settings.SettingsActivity; |
Julia Reynolds | b9195d2 | 2014-06-11 16:18:16 -0400 | [diff] [blame] | 65 | import com.android.settings.SettingsPreferenceFragment; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 66 | import com.android.settings.Utils; |
Doris Ling | 81f22e8 | 2017-03-23 11:10:14 -0700 | [diff] [blame] | 67 | import com.android.settings.dashboard.SummaryLoader; |
Maurice Lam | 2eb170c | 2017-04-28 16:18:47 -0700 | [diff] [blame] | 68 | import com.android.settings.password.ChooseLockGeneric; |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 69 | import com.android.settings.search.BaseSearchIndexProvider; |
| 70 | import com.android.settings.search.Indexable; |
| 71 | import com.android.settings.search.SearchIndexableRaw; |
Sudheer Shanka | 682a916 | 2016-01-13 22:10:18 +0000 | [diff] [blame] | 72 | import com.android.settingslib.RestrictedLockUtils; |
Maurice Lam | 2eb170c | 2017-04-28 16:18:47 -0700 | [diff] [blame] | 73 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
Fan Zhang | 12b03b2 | 2017-10-29 13:57:29 -0700 | [diff] [blame] | 74 | import com.android.settingslib.RestrictedPreference; |
Wei Liu | 6e58d51 | 2015-08-24 17:29:04 -0700 | [diff] [blame] | 75 | import com.android.settingslib.drawable.CircleFramedDrawable; |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 76 | |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 77 | import java.io.IOException; |
| 78 | import java.io.InputStream; |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 79 | import java.util.ArrayList; |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 80 | import java.util.Collections; |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 81 | import java.util.HashMap; |
| 82 | import java.util.List; |
| 83 | |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Screen that manages the list of users on the device. |
| 86 | * Guest user is an always visible entry, even if the guest is not currently |
| 87 | * active/created. It is meant for controlling properties of a guest user. |
| 88 | * |
| 89 | * The first one is always the current user. |
| 90 | * Owner is the primary user. |
| 91 | */ |
Julia Reynolds | b9195d2 | 2014-06-11 16:18:16 -0400 | [diff] [blame] | 92 | public class UserSettings extends SettingsPreferenceFragment |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 93 | implements OnPreferenceClickListener, OnClickListener, DialogInterface.OnDismissListener, |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 94 | EditUserInfoController.OnContentChangedCallback, Indexable { |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 95 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 96 | private static final String TAG = "UserSettings"; |
| 97 | |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 98 | /** UserId of the user being removed */ |
| 99 | private static final String SAVE_REMOVING_USER = "removing_user"; |
| 100 | /** UserId of the user that was just added */ |
| 101 | private static final String SAVE_ADDING_USER = "adding_user"; |
| 102 | |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 103 | private static final String KEY_USER_LIST = "user_list"; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 104 | private static final String KEY_USER_ME = "user_me"; |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 105 | private static final String KEY_ADD_USER = "user_add"; |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 106 | private static final String KEY_ADD_USER_WHEN_LOCKED = "user_settings_add_users_when_locked"; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 107 | |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 108 | private static final int MENU_REMOVE_USER = Menu.FIRST; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 109 | |
| 110 | private static final int DIALOG_CONFIRM_REMOVE = 1; |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 111 | private static final int DIALOG_ADD_USER = 2; |
| 112 | private static final int DIALOG_SETUP_USER = 3; |
| 113 | private static final int DIALOG_SETUP_PROFILE = 4; |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 114 | private static final int DIALOG_USER_CANNOT_MANAGE = 5; |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 115 | private static final int DIALOG_CHOOSE_USER_TYPE = 6; |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 116 | private static final int DIALOG_NEED_LOCKSCREEN = 7; |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 117 | private static final int DIALOG_CONFIRM_EXIT_GUEST = 8; |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 118 | private static final int DIALOG_USER_PROFILE_EDITOR = 9; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 119 | |
| 120 | private static final int MESSAGE_UPDATE_LIST = 1; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 121 | private static final int MESSAGE_SETUP_USER = 2; |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 122 | private static final int MESSAGE_CONFIG_USER = 3; |
| 123 | |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 124 | private static final int USER_TYPE_USER = 1; |
| 125 | private static final int USER_TYPE_RESTRICTED_PROFILE = 2; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 126 | |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 127 | private static final int REQUEST_CHOOSE_LOCK = 10; |
| 128 | |
Svetoslav Ganov | 263f744 | 2012-09-25 16:11:16 -0700 | [diff] [blame] | 129 | private static final String KEY_ADD_USER_LONG_MESSAGE_DISPLAYED = |
| 130 | "key_add_user_long_message_displayed"; |
| 131 | |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 132 | private static final String KEY_TITLE = "title"; |
| 133 | private static final String KEY_SUMMARY = "summary"; |
| 134 | |
| 135 | private PreferenceGroup mUserListCategory; |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 136 | private UserPreference mMePreference; |
Fan Zhang | 12b03b2 | 2017-10-29 13:57:29 -0700 | [diff] [blame] | 137 | private RestrictedPreference mAddUser; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 138 | private int mRemovingUserId = -1; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 139 | private int mAddedUserId = 0; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 140 | private boolean mAddingUser; |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 141 | private String mAddingUserName; |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 142 | private UserCapabilities mUserCaps; |
Fyodor Kupolov | 8c9120b | 2016-03-07 11:49:35 -0800 | [diff] [blame] | 143 | private boolean mShouldUpdateUserList = true; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 144 | private final Object mUserLock = new Object(); |
| 145 | private UserManager mUserManager; |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 146 | private SparseArray<Bitmap> mUserIcons = new SparseArray<>(); |
| 147 | private static SparseArray<Bitmap> sDarkDefaultUserBitmapCache = new SparseArray<>(); |
Amith Yamasani | 8666b9e | 2012-09-27 14:50:13 -0700 | [diff] [blame] | 148 | |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 149 | private EditUserInfoController mEditUserInfoController = new EditUserInfoController(); |
| 150 | private AddUserWhenLockedPreferenceController mAddUserWhenLockedPreferenceController; |
| 151 | private AutoSyncDataPreferenceController mAutoSyncDataPreferenceController; |
| 152 | private AutoSyncPersonalDataPreferenceController mAutoSyncPersonalDataPreferenceController; |
| 153 | private AutoSyncWorkDataPreferenceController mAutoSyncWorkDataPreferenceController; |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 154 | |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 155 | // A place to cache the generated default avatar |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 156 | private Drawable mDefaultIconDrawable; |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 157 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 158 | private Handler mHandler = new Handler() { |
| 159 | @Override |
| 160 | public void handleMessage(Message msg) { |
| 161 | switch (msg.what) { |
| 162 | case MESSAGE_UPDATE_LIST: |
| 163 | updateUserList(); |
| 164 | break; |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 165 | case MESSAGE_SETUP_USER: |
| 166 | onUserCreated(msg.arg1); |
| 167 | break; |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 168 | case MESSAGE_CONFIG_USER: |
| 169 | onManageUserClicked(msg.arg1, true); |
| 170 | break; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | }; |
| 174 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 175 | private BroadcastReceiver mUserChangeReceiver = new BroadcastReceiver() { |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 178 | if (intent.getAction().equals(Intent.ACTION_USER_REMOVED)) { |
| 179 | mRemovingUserId = -1; |
| 180 | } else if (intent.getAction().equals(Intent.ACTION_USER_INFO_CHANGED)) { |
| 181 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 182 | if (userHandle != -1) { |
| 183 | mUserIcons.remove(userHandle); |
| 184 | } |
| 185 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 186 | mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST); |
| 187 | } |
| 188 | }; |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 189 | |
| 190 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 191 | public int getMetricsCategory() { |
Chris Wren | 9d1bfd1 | 2016-01-26 18:04:01 -0500 | [diff] [blame] | 192 | return MetricsEvent.USER; |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 196 | public void onCreate(Bundle icicle) { |
| 197 | super.onCreate(icicle); |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 198 | addPreferencesFromResource(R.xml.user_settings); |
| 199 | if (Global.getInt(getContext().getContentResolver(), Global.DEVICE_PROVISIONED, 0) == 0) { |
| 200 | getActivity().finish(); |
| 201 | return; |
| 202 | } |
| 203 | final Context context = getActivity(); |
| 204 | mAddUserWhenLockedPreferenceController = new AddUserWhenLockedPreferenceController( |
| 205 | context, KEY_ADD_USER_WHEN_LOCKED, getLifecycle()); |
| 206 | |
| 207 | mAutoSyncDataPreferenceController = new AutoSyncDataPreferenceController(context, this); |
| 208 | mAutoSyncPersonalDataPreferenceController = |
| 209 | new AutoSyncPersonalDataPreferenceController(context, this); |
| 210 | mAutoSyncWorkDataPreferenceController = |
| 211 | new AutoSyncWorkDataPreferenceController(context, this); |
| 212 | |
| 213 | final PreferenceScreen screen = getPreferenceScreen(); |
| 214 | mAddUserWhenLockedPreferenceController.displayPreference(screen); |
| 215 | mAutoSyncDataPreferenceController.displayPreference(screen); |
| 216 | mAutoSyncPersonalDataPreferenceController.displayPreference(screen); |
| 217 | mAutoSyncWorkDataPreferenceController.displayPreference(screen); |
| 218 | screen.findPreference(mAddUserWhenLockedPreferenceController.getPreferenceKey()) |
| 219 | .setOnPreferenceChangeListener(mAddUserWhenLockedPreferenceController); |
Amith Yamasani | 8666b9e | 2012-09-27 14:50:13 -0700 | [diff] [blame] | 220 | |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 221 | if (icicle != null) { |
| 222 | if (icicle.containsKey(SAVE_ADDING_USER)) { |
| 223 | mAddedUserId = icicle.getInt(SAVE_ADDING_USER); |
| 224 | } |
| 225 | if (icicle.containsKey(SAVE_REMOVING_USER)) { |
| 226 | mRemovingUserId = icicle.getInt(SAVE_REMOVING_USER); |
| 227 | } |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 228 | mEditUserInfoController.onRestoreInstanceState(icicle); |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 229 | } |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 230 | |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 231 | mUserCaps = UserCapabilities.create(context); |
Amith Yamasani | 0e43d32 | 2014-05-07 10:19:34 -0700 | [diff] [blame] | 232 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 233 | if (!mUserCaps.mEnabled) { |
Amith Yamasani | cfb8d7f | 2014-03-26 14:46:47 -0700 | [diff] [blame] | 234 | return; |
| 235 | } |
Amith Yamasani | 0e43d32 | 2014-05-07 10:19:34 -0700 | [diff] [blame] | 236 | |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 237 | final int myUserId = UserHandle.myUserId(); |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 238 | |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 239 | mUserListCategory = (PreferenceGroup) findPreference(KEY_USER_LIST); |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 240 | mMePreference = new UserPreference(getPrefContext(), null /* attrs */, myUserId, |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 241 | null /* settings icon handler */, |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 242 | null /* delete icon handler */); |
Amith Yamasani | d4abdd2 | 2013-04-17 18:27:01 -0700 | [diff] [blame] | 243 | mMePreference.setKey(KEY_USER_ME); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 244 | mMePreference.setOnPreferenceClickListener(this); |
Xiaohui Chen | 64f530c | 2015-06-24 12:35:07 -0700 | [diff] [blame] | 245 | if (mUserCaps.mIsAdmin) { |
| 246 | mMePreference.setSummary(R.string.user_admin); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 247 | } |
Fan Zhang | 12b03b2 | 2017-10-29 13:57:29 -0700 | [diff] [blame] | 248 | mAddUser = (RestrictedPreference) findPreference(KEY_ADD_USER); |
Yoshinori Hirano | 2ce843b | 2016-03-02 10:29:41 +0900 | [diff] [blame] | 249 | mAddUser.useAdminDisabledSummary(false); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 250 | // Determine if add user/profile button should be visible |
Udam Saini | 679f7ad | 2016-03-25 10:47:00 -0700 | [diff] [blame] | 251 | if (mUserCaps.mCanAddUser && Utils.isDeviceProvisioned(getActivity())) { |
Amith Yamasani | 0e43d32 | 2014-05-07 10:19:34 -0700 | [diff] [blame] | 252 | mAddUser.setOnPreferenceClickListener(this); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 253 | // change label to only mention user, if restricted profiles are not supported |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 254 | if (!mUserCaps.mCanAddRestrictedProfile) { |
Amith Yamasani | 0e43d32 | 2014-05-07 10:19:34 -0700 | [diff] [blame] | 255 | mAddUser.setTitle(R.string.user_add_user_menu); |
| 256 | } |
Amith Yamasani | 233592b | 2013-03-01 17:05:23 -0800 | [diff] [blame] | 257 | } |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 258 | final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 259 | filter.addAction(Intent.ACTION_USER_INFO_CHANGED); |
Fyodor Kupolov | 8c9120b | 2016-03-07 11:49:35 -0800 | [diff] [blame] | 260 | context.registerReceiverAsUser(mUserChangeReceiver, UserHandle.ALL, filter, null, mHandler); |
| 261 | loadProfile(); |
| 262 | updateUserList(); |
| 263 | mShouldUpdateUserList = false; |
Amith Yamasani | 0ca80fe | 2013-06-03 16:41:03 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void onResume() { |
| 268 | super.onResume(); |
Amith Yamasani | cfb8d7f | 2014-03-26 14:46:47 -0700 | [diff] [blame] | 269 | |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 270 | if (!mUserCaps.mEnabled) { |
| 271 | return; |
| 272 | } |
| 273 | final PreferenceScreen screen = getPreferenceScreen(); |
| 274 | |
| 275 | if (mAutoSyncDataPreferenceController.isAvailable()) { |
| 276 | mAutoSyncDataPreferenceController.updateState(screen.findPreference( |
| 277 | mAutoSyncDataPreferenceController.getPreferenceKey())); |
| 278 | } |
| 279 | if (mAddUserWhenLockedPreferenceController.isAvailable()) { |
| 280 | mAddUserWhenLockedPreferenceController.updateState(screen.findPreference( |
| 281 | mAddUserWhenLockedPreferenceController.getPreferenceKey())); |
| 282 | } |
| 283 | if (mAutoSyncPersonalDataPreferenceController.isAvailable()) { |
| 284 | mAutoSyncPersonalDataPreferenceController.updateState(screen.findPreference( |
| 285 | mAutoSyncPersonalDataPreferenceController.getPreferenceKey())); |
| 286 | } |
| 287 | if (mAutoSyncWorkDataPreferenceController.isAvailable()) { |
| 288 | mAutoSyncWorkDataPreferenceController.updateState(screen.findPreference( |
| 289 | mAutoSyncWorkDataPreferenceController.getPreferenceKey())); |
| 290 | } |
| 291 | |
Fyodor Kupolov | 8c9120b | 2016-03-07 11:49:35 -0800 | [diff] [blame] | 292 | if (mShouldUpdateUserList) { |
Sudheer Shanka | aa524ae | 2016-03-14 17:23:13 -0700 | [diff] [blame] | 293 | mUserCaps.updateAddUserCapabilities(getActivity()); |
Fyodor Kupolov | 8c9120b | 2016-03-07 11:49:35 -0800 | [diff] [blame] | 294 | loadProfile(); |
| 295 | updateUserList(); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void onPause() { |
| 301 | mShouldUpdateUserList = true; |
| 302 | super.onPause(); |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Override |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 306 | public void onDestroy() { |
| 307 | super.onDestroy(); |
Amith Yamasani | cfb8d7f | 2014-03-26 14:46:47 -0700 | [diff] [blame] | 308 | |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 309 | if (!mUserCaps.mEnabled) { |
| 310 | return; |
| 311 | } |
Amith Yamasani | cfb8d7f | 2014-03-26 14:46:47 -0700 | [diff] [blame] | 312 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 313 | getActivity().unregisterReceiver(mUserChangeReceiver); |
| 314 | } |
| 315 | |
| 316 | @Override |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 317 | public void onSaveInstanceState(Bundle outState) { |
| 318 | super.onSaveInstanceState(outState); |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 319 | mEditUserInfoController.onSaveInstanceState(outState); |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 320 | outState.putInt(SAVE_ADDING_USER, mAddedUserId); |
| 321 | outState.putInt(SAVE_REMOVING_USER, mRemovingUserId); |
| 322 | } |
| 323 | |
| 324 | @Override |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 325 | public void startActivityForResult(Intent intent, int requestCode) { |
| 326 | mEditUserInfoController.startingActivityForResult(); |
| 327 | super.startActivityForResult(intent, requestCode); |
| 328 | } |
| 329 | |
| 330 | @Override |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 331 | public boolean onPreferenceTreeClick(Preference preference) { |
| 332 | if (mAutoSyncDataPreferenceController.handlePreferenceTreeClick(preference)) { |
| 333 | return true; |
| 334 | } |
| 335 | if (mAutoSyncPersonalDataPreferenceController.handlePreferenceTreeClick(preference)) { |
| 336 | return true; |
| 337 | } |
| 338 | if (mAutoSyncWorkDataPreferenceController.handlePreferenceTreeClick(preference)) { |
| 339 | return true; |
| 340 | } |
| 341 | return super.onPreferenceTreeClick(preference); |
| 342 | } |
| 343 | |
| 344 | @Override |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 345 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
Amith Yamasani | 247520c | 2014-07-25 12:52:29 -0700 | [diff] [blame] | 346 | int pos = 0; |
Fyodor Kupolov | 52ae053 | 2016-02-24 15:54:10 -0800 | [diff] [blame] | 347 | UserManager um = getContext().getSystemService(UserManager.class); |
| 348 | boolean allowRemoveUser = !um.hasUserRestriction(UserManager.DISALLOW_REMOVE_USER); |
| 349 | boolean canSwitchUsers = um.canSwitchUsers(); |
| 350 | if (!mUserCaps.mIsAdmin && allowRemoveUser && canSwitchUsers) { |
Amith Yamasani | 1a634d3 | 2012-09-27 23:01:35 -0700 | [diff] [blame] | 351 | String nickname = mUserManager.getUserName(); |
Amith Yamasani | 247520c | 2014-07-25 12:52:29 -0700 | [diff] [blame] | 352 | MenuItem removeThisUser = menu.add(0, MENU_REMOVE_USER, pos++, |
Amith Yamasani | 1a634d3 | 2012-09-27 23:01:35 -0700 | [diff] [blame] | 353 | getResources().getString(R.string.user_remove_user_menu, nickname)); |
| 354 | removeThisUser.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 355 | } |
Amith Yamasani | 38223dc | 2012-09-18 09:51:07 -0700 | [diff] [blame] | 356 | super.onCreateOptionsMenu(menu, inflater); |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public boolean onOptionsItemSelected(MenuItem item) { |
| 361 | final int itemId = item.getItemId(); |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 362 | if (itemId == MENU_REMOVE_USER) { |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 363 | onRemoveUserClicked(UserHandle.myUserId()); |
| 364 | return true; |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 365 | } else { |
| 366 | return super.onOptionsItemSelected(item); |
| 367 | } |
| 368 | } |
| 369 | |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 370 | /** |
| 371 | * Loads profile information for the current user. |
| 372 | */ |
Amith Yamasani | 32dccbc | 2012-09-13 18:06:49 -0700 | [diff] [blame] | 373 | private void loadProfile() { |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 374 | if (mUserCaps.mIsGuest) { |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 375 | // No need to load profile information |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 376 | mMePreference.setIcon(getEncircledDefaultIcon()); |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 377 | mMePreference.setTitle(R.string.user_exit_guest_title); |
| 378 | return; |
| 379 | } |
| 380 | |
Amith Yamasani | 32dccbc | 2012-09-13 18:06:49 -0700 | [diff] [blame] | 381 | new AsyncTask<Void, Void, String>() { |
| 382 | @Override |
| 383 | protected void onPostExecute(String result) { |
| 384 | finishLoadProfile(result); |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | protected String doInBackground(Void... values) { |
| 389 | UserInfo user = mUserManager.getUserInfo(UserHandle.myUserId()); |
| 390 | if (user.iconPath == null || user.iconPath.equals("")) { |
Sudheer Shanka | 71caf69 | 2016-04-14 16:50:14 -0700 | [diff] [blame] | 391 | // Assign profile photo. |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 392 | copyMeProfilePhoto(getActivity(), user); |
Amith Yamasani | 32dccbc | 2012-09-13 18:06:49 -0700 | [diff] [blame] | 393 | } |
Amith Yamasani | 2d578ce | 2014-07-25 09:37:33 -0700 | [diff] [blame] | 394 | return user.name; |
Amith Yamasani | 32dccbc | 2012-09-13 18:06:49 -0700 | [diff] [blame] | 395 | } |
| 396 | }.execute(); |
| 397 | } |
| 398 | |
| 399 | private void finishLoadProfile(String profileName) { |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 400 | if (getActivity() == null) return; |
| 401 | mMePreference.setTitle(getString(R.string.user_you, profileName)); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 402 | int myUserId = UserHandle.myUserId(); |
| 403 | Bitmap b = mUserManager.getUserIcon(myUserId); |
| 404 | if (b != null) { |
Amith Yamasani | 864e3b7 | 2013-03-27 12:27:01 -0700 | [diff] [blame] | 405 | mMePreference.setIcon(encircle(b)); |
| 406 | mUserIcons.put(myUserId, b); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 407 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 410 | private boolean hasLockscreenSecurity() { |
| 411 | LockPatternUtils lpu = new LockPatternUtils(getActivity()); |
Adrian Roos | 5437588 | 2015-04-16 17:11:22 -0700 | [diff] [blame] | 412 | return lpu.isSecure(UserHandle.myUserId()); |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | private void launchChooseLockscreen() { |
| 416 | Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD); |
| 417 | chooseLockIntent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY, |
| 418 | DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); |
| 419 | startActivityForResult(chooseLockIntent, REQUEST_CHOOSE_LOCK); |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 424 | super.onActivityResult(requestCode, resultCode, data); |
| 425 | |
| 426 | if (requestCode == REQUEST_CHOOSE_LOCK) { |
| 427 | if (resultCode != Activity.RESULT_CANCELED && hasLockscreenSecurity()) { |
| 428 | addUserNow(USER_TYPE_RESTRICTED_PROFILE); |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 429 | } |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 430 | } else { |
| 431 | mEditUserInfoController.onActivityResult(requestCode, resultCode, data); |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 435 | private void onAddUserClicked(int userType) { |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 436 | synchronized (mUserLock) { |
| 437 | if (mRemovingUserId == -1 && !mAddingUser) { |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 438 | switch (userType) { |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 439 | case USER_TYPE_USER: |
| 440 | showDialog(DIALOG_ADD_USER); |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 441 | break; |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 442 | case USER_TYPE_RESTRICTED_PROFILE: |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 443 | if (hasLockscreenSecurity()) { |
| 444 | addUserNow(USER_TYPE_RESTRICTED_PROFILE); |
| 445 | } else { |
| 446 | showDialog(DIALOG_NEED_LOCKSCREEN); |
| 447 | } |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 448 | break; |
| 449 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | private void onRemoveUserClicked(int userId) { |
| 455 | synchronized (mUserLock) { |
| 456 | if (mRemovingUserId == -1 && !mAddingUser) { |
| 457 | mRemovingUserId = userId; |
| 458 | showDialog(DIALOG_CONFIRM_REMOVE); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
Fyodor Kupolov | 9938f6f | 2015-08-26 17:24:09 -0700 | [diff] [blame] | 463 | private UserInfo createRestrictedProfile() { |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 464 | UserInfo newUserInfo = mUserManager.createRestrictedProfile(mAddingUserName); |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 465 | if (newUserInfo != null && !assignDefaultPhoto(getActivity(), newUserInfo.id)) { |
jackqdyulei | 8164c64 | 2016-12-27 10:15:27 -0800 | [diff] [blame] | 466 | return null; |
| 467 | } |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 468 | return newUserInfo; |
| 469 | } |
| 470 | |
| 471 | private UserInfo createTrustedUser() { |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 472 | UserInfo newUserInfo = mUserManager.createUser(mAddingUserName, 0); |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 473 | if (newUserInfo != null && !assignDefaultPhoto(getActivity(), newUserInfo.id)) { |
jackqdyulei | 8164c64 | 2016-12-27 10:15:27 -0800 | [diff] [blame] | 474 | return null; |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 475 | } |
| 476 | return newUserInfo; |
| 477 | } |
| 478 | |
| 479 | private void onManageUserClicked(int userId, boolean newUser) { |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 480 | mAddingUser = false; |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 481 | if (userId == UserPreference.USERID_GUEST_DEFAULTS) { |
| 482 | Bundle extras = new Bundle(); |
| 483 | extras.putBoolean(UserDetailsSettings.EXTRA_USER_GUEST, true); |
Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 484 | ((SettingsActivity) getActivity()).startPreferencePanel(this, |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 485 | UserDetailsSettings.class.getName(), |
| 486 | extras, R.string.user_guest, null, null, 0); |
| 487 | return; |
| 488 | } |
Amith Yamasani | d4abdd2 | 2013-04-17 18:27:01 -0700 | [diff] [blame] | 489 | UserInfo info = mUserManager.getUserInfo(userId); |
Xiaohui Chen | 64f530c | 2015-06-24 12:35:07 -0700 | [diff] [blame] | 490 | if (info.isRestricted() && mUserCaps.mIsAdmin) { |
Amith Yamasani | d4abdd2 | 2013-04-17 18:27:01 -0700 | [diff] [blame] | 491 | Bundle extras = new Bundle(); |
Amith Yamasani | 0ca80fe | 2013-06-03 16:41:03 -0700 | [diff] [blame] | 492 | extras.putInt(RestrictedProfileSettings.EXTRA_USER_ID, userId); |
| 493 | extras.putBoolean(RestrictedProfileSettings.EXTRA_NEW_USER, newUser); |
Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 494 | ((SettingsActivity) getActivity()).startPreferencePanel(this, |
Amith Yamasani | 0ca80fe | 2013-06-03 16:41:03 -0700 | [diff] [blame] | 495 | RestrictedProfileSettings.class.getName(), |
Amith Yamasani | 7c4dfcc | 2013-05-22 12:51:30 -0700 | [diff] [blame] | 496 | extras, R.string.user_restrictions_title, null, |
Amith Yamasani | d4abdd2 | 2013-04-17 18:27:01 -0700 | [diff] [blame] | 497 | null, 0); |
| 498 | } else if (info.id == UserHandle.myUserId()) { |
| 499 | // Jump to owner info panel |
Jason Monk | 3bcd76c | 2015-04-21 11:20:20 -0400 | [diff] [blame] | 500 | OwnerInfoSettings.show(this); |
Xiaohui Chen | 64f530c | 2015-06-24 12:35:07 -0700 | [diff] [blame] | 501 | } else if (mUserCaps.mIsAdmin) { |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 502 | Bundle extras = new Bundle(); |
| 503 | extras.putInt(UserDetailsSettings.EXTRA_USER_ID, userId); |
Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 504 | ((SettingsActivity) getActivity()).startPreferencePanel(this, |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 505 | UserDetailsSettings.class.getName(), |
Amith Yamasani | 51c6dac | 2014-07-02 00:06:37 +0530 | [diff] [blame] | 506 | extras, |
| 507 | -1, /* No title res id */ |
| 508 | info.name, /* title */ |
| 509 | null, /* resultTo */ |
| 510 | 0 /* resultRequestCode */); |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 511 | } |
Amith Yamasani | 233592b | 2013-03-01 17:05:23 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 514 | private void onUserCreated(int userId) { |
| 515 | mAddedUserId = userId; |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 516 | mAddingUser = false; |
xiyuan.wang | b6fa43d | 2017-10-20 17:59:58 +0800 | [diff] [blame] | 517 | if (!isResumed()) { |
| 518 | Log.w(TAG, "Cannot show dialog after onPause"); |
| 519 | return; |
| 520 | } |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 521 | if (mUserManager.getUserInfo(userId).isRestricted()) { |
| 522 | showDialog(DIALOG_SETUP_PROFILE); |
| 523 | } else { |
| 524 | showDialog(DIALOG_SETUP_USER); |
| 525 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 528 | @Override |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 529 | public void onDialogShowing() { |
| 530 | super.onDialogShowing(); |
| 531 | |
| 532 | setOnDismissListener(this); |
| 533 | } |
| 534 | |
| 535 | @Override |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 536 | public Dialog onCreateDialog(int dialogId) { |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 537 | Context context = getActivity(); |
| 538 | if (context == null) return null; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 539 | switch (dialogId) { |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 540 | case DIALOG_CONFIRM_REMOVE: { |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 541 | Dialog dlg = |
Fyodor Kupolov | dbbd4fc | 2014-12-29 13:40:27 -0800 | [diff] [blame] | 542 | UserDialogs.createRemoveDialog(getActivity(), mRemovingUserId, |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 543 | new DialogInterface.OnClickListener() { |
| 544 | public void onClick(DialogInterface dialog, int which) { |
| 545 | removeUserNow(); |
| 546 | } |
| 547 | } |
| 548 | ); |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 549 | return dlg; |
| 550 | } |
Amith Yamasani | 1a634d3 | 2012-09-27 23:01:35 -0700 | [diff] [blame] | 551 | case DIALOG_USER_CANNOT_MANAGE: |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 552 | return new AlertDialog.Builder(context) |
Amith Yamasani | 1a634d3 | 2012-09-27 23:01:35 -0700 | [diff] [blame] | 553 | .setMessage(R.string.user_cannot_manage_message) |
| 554 | .setPositiveButton(android.R.string.ok, null) |
| 555 | .create(); |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 556 | case DIALOG_ADD_USER: { |
Svetoslav Ganov | 263f744 | 2012-09-25 16:11:16 -0700 | [diff] [blame] | 557 | final SharedPreferences preferences = getActivity().getPreferences( |
| 558 | Context.MODE_PRIVATE); |
| 559 | final boolean longMessageDisplayed = preferences.getBoolean( |
| 560 | KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, false); |
| 561 | final int messageResId = longMessageDisplayed |
| 562 | ? R.string.user_add_user_message_short |
| 563 | : R.string.user_add_user_message_long; |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 564 | final int userType = dialogId == DIALOG_ADD_USER |
| 565 | ? USER_TYPE_USER : USER_TYPE_RESTRICTED_PROFILE; |
| 566 | Dialog dlg = new AlertDialog.Builder(context) |
| 567 | .setTitle(R.string.user_add_user_title) |
| 568 | .setMessage(messageResId) |
| 569 | .setPositiveButton(android.R.string.ok, |
| 570 | new DialogInterface.OnClickListener() { |
| 571 | public void onClick(DialogInterface dialog, int which) { |
| 572 | addUserNow(userType); |
| 573 | if (!longMessageDisplayed) { |
| 574 | preferences.edit().putBoolean( |
| 575 | KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, true).apply(); |
| 576 | } |
Svetoslav Ganov | 263f744 | 2012-09-25 16:11:16 -0700 | [diff] [blame] | 577 | } |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 578 | }) |
| 579 | .setNegativeButton(android.R.string.cancel, null) |
| 580 | .create(); |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 581 | return dlg; |
| 582 | } |
| 583 | case DIALOG_SETUP_USER: { |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 584 | Dialog dlg = new AlertDialog.Builder(context) |
| 585 | .setTitle(R.string.user_setup_dialog_title) |
| 586 | .setMessage(R.string.user_setup_dialog_message) |
| 587 | .setPositiveButton(R.string.user_setup_button_setup_now, |
| 588 | new DialogInterface.OnClickListener() { |
| 589 | public void onClick(DialogInterface dialog, int which) { |
| 590 | switchUserNow(mAddedUserId); |
| 591 | } |
| 592 | }) |
| 593 | .setNegativeButton(R.string.user_setup_button_setup_later, null) |
| 594 | .create(); |
| 595 | return dlg; |
| 596 | } |
| 597 | case DIALOG_SETUP_PROFILE: { |
| 598 | Dialog dlg = new AlertDialog.Builder(context) |
| 599 | .setMessage(R.string.user_setup_profile_dialog_message) |
| 600 | .setPositiveButton(android.R.string.ok, |
| 601 | new DialogInterface.OnClickListener() { |
| 602 | public void onClick(DialogInterface dialog, int which) { |
| 603 | switchUserNow(mAddedUserId); |
| 604 | } |
| 605 | }) |
| 606 | .setNegativeButton(android.R.string.cancel, null) |
| 607 | .create(); |
| 608 | return dlg; |
| 609 | } |
| 610 | case DIALOG_CHOOSE_USER_TYPE: { |
| 611 | List<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>(); |
| 612 | HashMap<String,String> addUserItem = new HashMap<String,String>(); |
| 613 | addUserItem.put(KEY_TITLE, getString(R.string.user_add_user_item_title)); |
| 614 | addUserItem.put(KEY_SUMMARY, getString(R.string.user_add_user_item_summary)); |
| 615 | HashMap<String,String> addProfileItem = new HashMap<String,String>(); |
| 616 | addProfileItem.put(KEY_TITLE, getString(R.string.user_add_profile_item_title)); |
| 617 | addProfileItem.put(KEY_SUMMARY, getString(R.string.user_add_profile_item_summary)); |
| 618 | data.add(addUserItem); |
| 619 | data.add(addProfileItem); |
Amith Yamasani | f3db021 | 2014-12-02 17:21:23 -0800 | [diff] [blame] | 620 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 621 | SimpleAdapter adapter = new SimpleAdapter(builder.getContext(), |
| 622 | data, R.layout.two_line_list_item, |
| 623 | new String[] {KEY_TITLE, KEY_SUMMARY}, |
| 624 | new int[] {R.id.title, R.id.summary}); |
| 625 | builder.setTitle(R.string.user_add_user_type_title); |
| 626 | builder.setAdapter(adapter, |
| 627 | new DialogInterface.OnClickListener() { |
| 628 | @Override |
| 629 | public void onClick(DialogInterface dialog, int which) { |
| 630 | onAddUserClicked(which == 0 |
| 631 | ? USER_TYPE_USER |
| 632 | : USER_TYPE_RESTRICTED_PROFILE); |
| 633 | } |
| 634 | }); |
| 635 | return builder.create(); |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 636 | } |
Amith Yamasani | f162efe | 2013-04-29 10:43:05 -0700 | [diff] [blame] | 637 | case DIALOG_NEED_LOCKSCREEN: { |
| 638 | Dialog dlg = new AlertDialog.Builder(context) |
| 639 | .setMessage(R.string.user_need_lock_message) |
| 640 | .setPositiveButton(R.string.user_set_lock_button, |
| 641 | new DialogInterface.OnClickListener() { |
| 642 | @Override |
| 643 | public void onClick(DialogInterface dialog, int which) { |
| 644 | launchChooseLockscreen(); |
| 645 | } |
| 646 | }) |
| 647 | .setNegativeButton(android.R.string.cancel, null) |
| 648 | .create(); |
| 649 | return dlg; |
| 650 | } |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 651 | case DIALOG_CONFIRM_EXIT_GUEST: { |
| 652 | Dialog dlg = new AlertDialog.Builder(context) |
| 653 | .setTitle(R.string.user_exit_guest_confirm_title) |
| 654 | .setMessage(R.string.user_exit_guest_confirm_message) |
Amith Yamasani | 94c0181 | 2014-08-18 16:42:43 -0700 | [diff] [blame] | 655 | .setPositiveButton(R.string.user_exit_guest_dialog_remove, |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 656 | new DialogInterface.OnClickListener() { |
| 657 | @Override |
| 658 | public void onClick(DialogInterface dialog, int which) { |
| 659 | exitGuest(); |
| 660 | } |
| 661 | }) |
Amith Yamasani | 94c0181 | 2014-08-18 16:42:43 -0700 | [diff] [blame] | 662 | .setNegativeButton(android.R.string.cancel, null) |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 663 | .create(); |
| 664 | return dlg; |
| 665 | } |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 666 | case DIALOG_USER_PROFILE_EDITOR: { |
| 667 | Dialog dlg = mEditUserInfoController.createDialog( |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 668 | this, |
Stephen Chen | 94d0b16 | 2016-10-05 18:06:03 -0700 | [diff] [blame] | 669 | null, |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 670 | mMePreference.getTitle(), |
| 671 | R.string.profile_info_settings_title, |
| 672 | this /* callback */, |
| 673 | android.os.Process.myUserHandle()); |
| 674 | return dlg; |
| 675 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 676 | default: |
| 677 | return null; |
| 678 | } |
| 679 | } |
| 680 | |
Fan Zhang | d65184f | 2016-09-19 17:45:24 -0700 | [diff] [blame] | 681 | @Override |
| 682 | public int getDialogMetricsCategory(int dialogId) { |
| 683 | switch (dialogId) { |
| 684 | case DIALOG_CONFIRM_REMOVE: |
| 685 | return MetricsEvent.DIALOG_USER_REMOVE; |
| 686 | case DIALOG_USER_CANNOT_MANAGE: |
| 687 | return MetricsEvent.DIALOG_USER_CANNOT_MANAGE; |
| 688 | case DIALOG_ADD_USER: |
| 689 | return MetricsEvent.DIALOG_USER_ADD; |
| 690 | case DIALOG_SETUP_USER: |
| 691 | return MetricsEvent.DIALOG_USER_SETUP; |
| 692 | case DIALOG_SETUP_PROFILE: |
| 693 | return MetricsEvent.DIALOG_USER_SETUP_PROFILE; |
| 694 | case DIALOG_CHOOSE_USER_TYPE: |
| 695 | return MetricsEvent.DIALOG_USER_CHOOSE_TYPE; |
| 696 | case DIALOG_NEED_LOCKSCREEN: |
| 697 | return MetricsEvent.DIALOG_USER_NEED_LOCKSCREEN; |
| 698 | case DIALOG_CONFIRM_EXIT_GUEST: |
| 699 | return MetricsEvent.DIALOG_USER_CONFIRM_EXIT_GUEST; |
| 700 | case DIALOG_USER_PROFILE_EDITOR: |
| 701 | return MetricsEvent.DIALOG_USER_EDIT_PROFILE; |
| 702 | default: |
| 703 | return 0; |
| 704 | } |
| 705 | } |
| 706 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 707 | private void removeUserNow() { |
| 708 | if (mRemovingUserId == UserHandle.myUserId()) { |
| 709 | removeThisUser(); |
| 710 | } else { |
| 711 | new Thread() { |
| 712 | public void run() { |
| 713 | synchronized (mUserLock) { |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 714 | mUserManager.removeUser(mRemovingUserId); |
| 715 | mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | }.start(); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | private void removeThisUser() { |
Fyodor Kupolov | 52ae053 | 2016-02-24 15:54:10 -0800 | [diff] [blame] | 723 | if (!mUserManager.canSwitchUsers()) { |
| 724 | Log.w(TAG, "Cannot remove current user when switching is disabled"); |
| 725 | return; |
| 726 | } |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 727 | try { |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 728 | ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM); |
Fyodor Kupolov | 52ae053 | 2016-02-24 15:54:10 -0800 | [diff] [blame] | 729 | getContext().getSystemService(UserManager.class).removeUser(UserHandle.myUserId()); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 730 | } catch (RemoteException re) { |
| 731 | Log.e(TAG, "Unable to remove self user"); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 735 | private void addUserNow(final int userType) { |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 736 | synchronized (mUserLock) { |
| 737 | mAddingUser = true; |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 738 | mAddingUserName = userType == USER_TYPE_USER ? getString(R.string.user_new_user_name) |
| 739 | : getString(R.string.user_new_profile_name); |
Amith Yamasani | a98c402 | 2013-04-19 15:27:52 -0700 | [diff] [blame] | 740 | //updateUserList(); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 741 | new Thread() { |
| 742 | public void run() { |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 743 | UserInfo user; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 744 | // Could take a few seconds |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 745 | if (userType == USER_TYPE_USER) { |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 746 | user = createTrustedUser(); |
| 747 | } else { |
Fyodor Kupolov | 9938f6f | 2015-08-26 17:24:09 -0700 | [diff] [blame] | 748 | user = createRestrictedProfile(); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 749 | } |
Sudheer Shanka | aa524ae | 2016-03-14 17:23:13 -0700 | [diff] [blame] | 750 | if (user == null) { |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 751 | mAddingUser = false; |
Sudheer Shanka | aa524ae | 2016-03-14 17:23:13 -0700 | [diff] [blame] | 752 | return; |
| 753 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 754 | synchronized (mUserLock) { |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 755 | if (userType == USER_TYPE_USER) { |
Amith Yamasani | a98c402 | 2013-04-19 15:27:52 -0700 | [diff] [blame] | 756 | mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST); |
Alex Chau | ffae4c1 | 2018-01-30 14:22:50 +0000 | [diff] [blame^] | 757 | // Skip setting up user which results in user switching when the |
| 758 | // restriction is set. |
| 759 | if (!mUserCaps.mDisallowSwitchUser) { |
| 760 | mHandler.sendMessage(mHandler.obtainMessage( |
| 761 | MESSAGE_SETUP_USER, user.id, user.serialNumber)); |
| 762 | } |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 763 | } else { |
| 764 | mHandler.sendMessage(mHandler.obtainMessage( |
| 765 | MESSAGE_CONFIG_USER, user.id, user.serialNumber)); |
| 766 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | }.start(); |
| 770 | } |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 773 | private void switchUserNow(int userId) { |
| 774 | try { |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 775 | ActivityManager.getService().switchUser(userId); |
Amith Yamasani | ae47ef4 | 2012-09-16 17:53:35 -0700 | [diff] [blame] | 776 | } catch (RemoteException re) { |
| 777 | // Nothing to do |
| 778 | } |
| 779 | } |
| 780 | |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 781 | /** |
| 782 | * Erase the current user (guest) and switch to another user. |
| 783 | */ |
| 784 | private void exitGuest() { |
| 785 | // Just to be safe |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 786 | if (!mUserCaps.mIsGuest) { |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 787 | return; |
| 788 | } |
| 789 | removeThisUser(); |
| 790 | } |
| 791 | |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 792 | private void updateUserList() { |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 793 | if (getActivity() == null) return; |
Amith Yamasani | e278eda | 2012-09-20 22:44:48 -0700 | [diff] [blame] | 794 | List<UserInfo> users = mUserManager.getUsers(true); |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 795 | final Context context = getActivity(); |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 796 | |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 797 | final boolean voiceCapable = Utils.isVoiceCapable(context); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 798 | final ArrayList<Integer> missingIcons = new ArrayList<>(); |
| 799 | final ArrayList<UserPreference> userPreferences = new ArrayList<>(); |
Jason Monk | c1cc9fe | 2017-06-30 11:29:40 -0400 | [diff] [blame] | 800 | int guestId = UserPreference.USERID_GUEST_DEFAULTS; |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 801 | userPreferences.add(mMePreference); |
| 802 | |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 803 | for (UserInfo user : users) { |
Xiaohui Chen | 148ef77 | 2015-07-16 10:06:03 -0700 | [diff] [blame] | 804 | if (!user.supportsSwitchToByUser()) { |
| 805 | // Only users that can be switched to should show up here. |
| 806 | // e.g. Managed profiles appear under Accounts Settings instead |
Alexandra Gherghina | 83adb56 | 2014-07-31 19:30:41 +0100 | [diff] [blame] | 807 | continue; |
| 808 | } |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 809 | UserPreference pref; |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 810 | if (user.id == UserHandle.myUserId()) { |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 811 | pref = mMePreference; |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 812 | } else if (user.isGuest()) { |
| 813 | // Skip over Guest. We add generic Guest settings after this loop |
Jason Monk | c1cc9fe | 2017-06-30 11:29:40 -0400 | [diff] [blame] | 814 | guestId = user.id; |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 815 | continue; |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 816 | } else { |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 817 | // With Telephony: |
| 818 | // Secondary user: Settings |
| 819 | // Guest: Settings |
| 820 | // Restricted Profile: There is no Restricted Profile |
| 821 | // Without Telephony: |
| 822 | // Secondary user: Delete |
| 823 | // Guest: Nothing |
| 824 | // Restricted Profile: Settings |
Xiaohui Chen | 64f530c | 2015-06-24 12:35:07 -0700 | [diff] [blame] | 825 | final boolean showSettings = mUserCaps.mIsAdmin |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 826 | && (voiceCapable || user.isRestricted()); |
Xiaohui Chen | 64f530c | 2015-06-24 12:35:07 -0700 | [diff] [blame] | 827 | final boolean showDelete = mUserCaps.mIsAdmin |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 828 | && (!voiceCapable && !user.isRestricted() && !user.isGuest()); |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 829 | pref = new UserPreference(getPrefContext(), null, user.id, |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 830 | showSettings ? this : null, |
| 831 | showDelete ? this : null); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 832 | pref.setKey("id=" + user.id); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 833 | userPreferences.add(pref); |
Xiaohui Chen | 64f530c | 2015-06-24 12:35:07 -0700 | [diff] [blame] | 834 | if (user.isAdmin()) { |
| 835 | pref.setSummary(R.string.user_admin); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 836 | } |
| 837 | pref.setTitle(user.name); |
Fyodor Kupolov | 77865ec | 2016-06-02 10:39:33 -0700 | [diff] [blame] | 838 | pref.setSelectable(false); |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 839 | } |
Julia Reynolds | 8bda21a | 2016-04-29 08:42:01 -0400 | [diff] [blame] | 840 | if (pref == null) { |
| 841 | continue; |
| 842 | } |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 843 | if (!isInitialized(user)) { |
Alexandra Gherghina | 423bafc | 2014-04-23 10:56:37 +0000 | [diff] [blame] | 844 | if (user.isRestricted()) { |
| 845 | pref.setSummary(R.string.user_summary_restricted_not_set_up); |
Alexandra Gherghina | 423bafc | 2014-04-23 10:56:37 +0000 | [diff] [blame] | 846 | } else { |
| 847 | pref.setSummary(R.string.user_summary_not_set_up); |
| 848 | } |
Alex Chau | ffae4c1 | 2018-01-30 14:22:50 +0000 | [diff] [blame^] | 849 | // Disallow setting up user which results in user switching when the restriction is |
| 850 | // set. |
| 851 | if (!mUserCaps.mDisallowSwitchUser) { |
| 852 | pref.setOnPreferenceClickListener(this); |
| 853 | pref.setSelectable(true); |
| 854 | } |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 855 | } else if (user.isRestricted()) { |
| 856 | pref.setSummary(R.string.user_summary_restricted_profile); |
Amith Yamasani | 48fa857 | 2012-04-17 17:12:01 -0700 | [diff] [blame] | 857 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 858 | if (user.iconPath != null) { |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 859 | if (mUserIcons.get(user.id) == null) { |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 860 | // Icon not loaded yet, print a placeholder |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 861 | missingIcons.add(user.id); |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 862 | pref.setIcon(getEncircledDefaultIcon()); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 863 | } else { |
| 864 | setPhotoId(pref, user); |
| 865 | } |
Amith Yamasani | f2663ab | 2014-08-11 17:17:29 -0700 | [diff] [blame] | 866 | } else { |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 867 | // Icon not available yet, print a placeholder |
| 868 | pref.setIcon(getEncircledDefaultIcon()); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
Amith Yamasani | 247520c | 2014-07-25 12:52:29 -0700 | [diff] [blame] | 871 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 872 | // Add a temporary entry for the user being created |
| 873 | if (mAddingUser) { |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 874 | UserPreference pref = new UserPreference(getPrefContext(), null, |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 875 | UserPreference.USERID_UNKNOWN, null, null); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 876 | pref.setEnabled(false); |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 877 | pref.setTitle(mAddingUserName); |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 878 | pref.setIcon(getEncircledDefaultIcon()); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 879 | userPreferences.add(pref); |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 880 | } |
Amith Yamasani | 9e6ac3d | 2014-06-30 15:31:37 +0530 | [diff] [blame] | 881 | |
Fyodor Kupolov | 889bc3d | 2016-01-29 17:29:54 -0800 | [diff] [blame] | 882 | // Check if Guest tile should be added. |
Sudheer Shanka | 29737ad | 2016-02-11 17:17:21 +0000 | [diff] [blame] | 883 | if (!mUserCaps.mIsGuest && (mUserCaps.mCanAddGuest || |
| 884 | mUserCaps.mDisallowAddUserSetByAdmin)) { |
| 885 | // Add a virtual Guest user for guest defaults |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 886 | UserPreference pref = new UserPreference(getPrefContext(), null, |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 887 | UserPreference.USERID_GUEST_DEFAULTS, |
Xiaohui Chen | 64f530c | 2015-06-24 12:35:07 -0700 | [diff] [blame] | 888 | mUserCaps.mIsAdmin && voiceCapable? this : null /* settings icon handler */, |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 889 | null /* delete icon handler */); |
| 890 | pref.setTitle(R.string.user_guest); |
| 891 | pref.setIcon(getEncircledDefaultIcon()); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 892 | userPreferences.add(pref); |
Alex Chau | ffae4c1 | 2018-01-30 14:22:50 +0000 | [diff] [blame^] | 893 | if (mUserCaps.mDisallowAddUser) { |
| 894 | pref.setDisabledByAdmin(mUserCaps.mEnforcedAdmin); |
| 895 | } else if (mUserCaps.mDisallowSwitchUser) { |
| 896 | pref.setDisabledByAdmin(RestrictedLockUtils.getDeviceOwner(context)); |
| 897 | } else { |
| 898 | pref.setDisabledByAdmin(null); |
| 899 | } |
Jason Monk | c1cc9fe | 2017-06-30 11:29:40 -0400 | [diff] [blame] | 900 | int finalGuestId = guestId; |
| 901 | pref.setOnPreferenceClickListener(preference -> { |
| 902 | int id = finalGuestId; |
| 903 | if (id == UserPreference.USERID_GUEST_DEFAULTS) { |
| 904 | UserInfo guest = mUserManager.createGuest( |
| 905 | getContext(), preference.getTitle().toString()); |
| 906 | if (guest != null) { |
| 907 | id = guest.id; |
| 908 | } |
| 909 | } |
| 910 | try { |
| 911 | ActivityManager.getService().switchUser(id); |
| 912 | } catch (RemoteException e) { |
| 913 | e.rethrowFromSystemServer(); |
| 914 | } |
| 915 | return true; |
| 916 | }); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | // Sort list of users by serialNum |
| 920 | Collections.sort(userPreferences, UserPreference.SERIAL_NUMBER_COMPARATOR); |
| 921 | |
| 922 | getActivity().invalidateOptionsMenu(); |
| 923 | |
| 924 | // Load the icons |
| 925 | if (missingIcons.size() > 0) { |
| 926 | loadIconsAsync(missingIcons); |
| 927 | } |
| 928 | |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 929 | // Remove everything from mUserListCategory and add new users. |
| 930 | mUserListCategory.removeAll(); |
| 931 | // If profiles are supported, mUserListCategory will have a special title |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 932 | if (mUserCaps.mCanAddRestrictedProfile) { |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 933 | mUserListCategory.setTitle(R.string.user_list_title); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 934 | } else { |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 935 | mUserListCategory.setTitle(null); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 936 | } |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 937 | |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 938 | for (UserPreference userPreference : userPreferences) { |
| 939 | userPreference.setOrder(Preference.DEFAULT_ORDER); |
Fan Zhang | 881d579 | 2018-01-26 10:15:56 -0800 | [diff] [blame] | 940 | mUserListCategory.addPreference(userPreference); |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | // Append Add user to the end of the list |
Udam Saini | 679f7ad | 2016-03-25 10:47:00 -0700 | [diff] [blame] | 944 | if ((mUserCaps.mCanAddUser || mUserCaps.mDisallowAddUserSetByAdmin) && |
| 945 | Utils.isDeviceProvisioned(getActivity())) { |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 946 | boolean moreUsers = mUserManager.canAddMoreUsers(); |
Fyodor Kupolov | fe6cb6d | 2016-03-28 15:10:19 -0700 | [diff] [blame] | 947 | mAddUser.setEnabled(moreUsers && !mAddingUser); |
Fyodor Kupolov | a7a7bfe | 2015-05-08 15:54:38 -0700 | [diff] [blame] | 948 | if (!moreUsers) { |
| 949 | mAddUser.setSummary(getString(R.string.user_add_max_count, getMaxRealUsers())); |
| 950 | } else { |
| 951 | mAddUser.setSummary(null); |
| 952 | } |
Sudheer Shanka | 682a916 | 2016-01-13 22:10:18 +0000 | [diff] [blame] | 953 | if (mAddUser.isEnabled()) { |
| 954 | mAddUser.setDisabledByAdmin( |
| 955 | mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null); |
| 956 | } |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 957 | } |
Doris Ling | 86e1df9 | 2016-10-31 17:08:04 -0700 | [diff] [blame] | 958 | |
Fyodor Kupolov | 5514509 | 2014-12-08 14:15:36 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Fyodor Kupolov | a7a7bfe | 2015-05-08 15:54:38 -0700 | [diff] [blame] | 961 | private int getMaxRealUsers() { |
| 962 | // guest is not counted against getMaxSupportedUsers() number |
| 963 | final int maxUsersAndGuest = UserManager.getMaxSupportedUsers() + 1; |
| 964 | final List<UserInfo> users = mUserManager.getUsers(); |
| 965 | // managed profiles are counted against getMaxSupportedUsers() |
| 966 | int managedProfiles = 0; |
| 967 | for (UserInfo user : users) { |
| 968 | if (user.isManagedProfile()) { |
| 969 | managedProfiles++; |
| 970 | } |
| 971 | } |
| 972 | return maxUsersAndGuest - managedProfiles; |
| 973 | } |
| 974 | |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 975 | private void loadIconsAsync(List<Integer> missingIcons) { |
| 976 | new AsyncTask<List<Integer>, Void, Void>() { |
| 977 | @Override |
| 978 | protected void onPostExecute(Void result) { |
| 979 | updateUserList(); |
| 980 | } |
| 981 | |
| 982 | @Override |
| 983 | protected Void doInBackground(List<Integer>... values) { |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 984 | for (int userId : values[0]) { |
| 985 | Bitmap bitmap = mUserManager.getUserIcon(userId); |
Amith Yamasani | f2663ab | 2014-08-11 17:17:29 -0700 | [diff] [blame] | 986 | if (bitmap == null) { |
Tony Mak | 0ba8f51 | 2017-11-23 16:58:20 +0800 | [diff] [blame] | 987 | bitmap = getDefaultUserIconAsBitmap(getContext().getResources(), userId); |
Amith Yamasani | f2663ab | 2014-08-11 17:17:29 -0700 | [diff] [blame] | 988 | } |
Amith Yamasani | 864e3b7 | 2013-03-27 12:27:01 -0700 | [diff] [blame] | 989 | mUserIcons.append(userId, bitmap); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 990 | } |
| 991 | return null; |
| 992 | } |
| 993 | }.execute(missingIcons); |
Amith Yamasani | f34a85d | 2012-09-17 18:31:45 -0700 | [diff] [blame] | 994 | } |
Amith Yamasani | 5cdc937 | 2012-11-14 16:23:40 -0800 | [diff] [blame] | 995 | |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 996 | private Drawable getEncircledDefaultIcon() { |
| 997 | if (mDefaultIconDrawable == null) { |
Tony Mak | 0ba8f51 | 2017-11-23 16:58:20 +0800 | [diff] [blame] | 998 | mDefaultIconDrawable = encircle( |
| 999 | getDefaultUserIconAsBitmap(getContext().getResources(), UserHandle.USER_NULL)); |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 1000 | } |
Alexandra Gherghina | bc6e78f | 2014-09-03 10:22:09 +0100 | [diff] [blame] | 1001 | return mDefaultIconDrawable; |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 1004 | private void setPhotoId(Preference pref, UserInfo user) { |
Amith Yamasani | d4abdd2 | 2013-04-17 18:27:01 -0700 | [diff] [blame] | 1005 | Bitmap bitmap = mUserIcons.get(user.id); |
Amith Yamasani | 864e3b7 | 2013-03-27 12:27:01 -0700 | [diff] [blame] | 1006 | if (bitmap != null) { |
| 1007 | pref.setIcon(encircle(bitmap)); |
Amith Yamasani | e940dac | 2012-09-17 10:21:27 -0700 | [diff] [blame] | 1008 | } |
Amith Yamasani | 48fa857 | 2012-04-17 17:12:01 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 1011 | @Override |
| 1012 | public boolean onPreferenceClick(Preference pref) { |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 1013 | if (pref == mMePreference) { |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 1014 | if (mUserCaps.mIsGuest) { |
Amith Yamasani | c16b73e | 2014-07-17 19:13:51 -0700 | [diff] [blame] | 1015 | showDialog(DIALOG_CONFIRM_EXIT_GUEST); |
| 1016 | return true; |
| 1017 | } |
Amith Yamasani | d4abdd2 | 2013-04-17 18:27:01 -0700 | [diff] [blame] | 1018 | // If this is a limited user, launch the user info settings instead of profile editor |
| 1019 | if (mUserManager.isLinkedUser()) { |
| 1020 | onManageUserClicked(UserHandle.myUserId(), false); |
| 1021 | } else { |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 1022 | showDialog(DIALOG_USER_PROFILE_EDITOR); |
Amith Yamasani | d4abdd2 | 2013-04-17 18:27:01 -0700 | [diff] [blame] | 1023 | } |
Amith Yamasani | 1a634d3 | 2012-09-27 23:01:35 -0700 | [diff] [blame] | 1024 | } else if (pref instanceof UserPreference) { |
| 1025 | int userId = ((UserPreference) pref).getUserId(); |
Fyodor Kupolov | 889bc3d | 2016-01-29 17:29:54 -0800 | [diff] [blame] | 1026 | // Get the latest status of the user |
| 1027 | UserInfo user = mUserManager.getUserInfo(userId); |
| 1028 | if (!isInitialized(user)) { |
| 1029 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1030 | MESSAGE_SETUP_USER, user.id, user.serialNumber)); |
Amith Yamasani | 1a634d3 | 2012-09-27 23:01:35 -0700 | [diff] [blame] | 1031 | } |
Amith Yamasani | fc8a383 | 2013-04-23 11:44:59 -0700 | [diff] [blame] | 1032 | } else if (pref == mAddUser) { |
Amith Yamasani | 0e43d32 | 2014-05-07 10:19:34 -0700 | [diff] [blame] | 1033 | // If we allow both types, show a picker, otherwise directly go to |
| 1034 | // flow for full user. |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 1035 | if (mUserCaps.mCanAddRestrictedProfile) { |
Amith Yamasani | 0e43d32 | 2014-05-07 10:19:34 -0700 | [diff] [blame] | 1036 | showDialog(DIALOG_CHOOSE_USER_TYPE); |
| 1037 | } else { |
| 1038 | onAddUserClicked(USER_TYPE_USER); |
| 1039 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 1040 | } |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
Amith Yamasani | 1a634d3 | 2012-09-27 23:01:35 -0700 | [diff] [blame] | 1044 | private boolean isInitialized(UserInfo user) { |
| 1045 | return (user.flags & UserInfo.FLAG_INITIALIZED) != 0; |
| 1046 | } |
| 1047 | |
Amith Yamasani | 864e3b7 | 2013-03-27 12:27:01 -0700 | [diff] [blame] | 1048 | private Drawable encircle(Bitmap icon) { |
| 1049 | Drawable circled = CircleFramedDrawable.getInstance(getActivity(), icon); |
| 1050 | return circled; |
| 1051 | } |
| 1052 | |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 1053 | @Override |
| 1054 | public void onClick(View v) { |
| 1055 | if (v.getTag() instanceof UserPreference) { |
| 1056 | int userId = ((UserPreference) v.getTag()).getUserId(); |
Amith Yamasani | 233592b | 2013-03-01 17:05:23 -0800 | [diff] [blame] | 1057 | switch (v.getId()) { |
| 1058 | case UserPreference.DELETE_ID: |
Sudheer Shanka | b5c9214 | 2016-03-22 17:24:07 -0700 | [diff] [blame] | 1059 | final EnforcedAdmin removeDisallowedAdmin = |
| 1060 | RestrictedLockUtils.checkIfRestrictionEnforced(getContext(), |
| 1061 | UserManager.DISALLOW_REMOVE_USER, UserHandle.myUserId()); |
| 1062 | if (removeDisallowedAdmin != null) { |
| 1063 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), |
| 1064 | removeDisallowedAdmin); |
| 1065 | } else { |
| 1066 | onRemoveUserClicked(userId); |
| 1067 | } |
Amith Yamasani | 233592b | 2013-03-01 17:05:23 -0800 | [diff] [blame] | 1068 | break; |
| 1069 | case UserPreference.SETTINGS_ID: |
Amith Yamasani | cc39569 | 2013-04-11 18:21:07 -0700 | [diff] [blame] | 1070 | onManageUserClicked(userId, false); |
Amith Yamasani | 233592b | 2013-03-01 17:05:23 -0800 | [diff] [blame] | 1071 | break; |
| 1072 | } |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | @Override |
| 1077 | public void onDismiss(DialogInterface dialog) { |
| 1078 | synchronized (mUserLock) { |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 1079 | mRemovingUserId = -1; |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 1080 | updateUserList(); |
Amith Yamasani | dc6bfa6 | 2012-09-06 18:04:06 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | @Override |
Amith Yamasani | 38223dc | 2012-09-18 09:51:07 -0700 | [diff] [blame] | 1085 | public int getHelpResource() { |
| 1086 | return R.string.help_url_users; |
| 1087 | } |
Amith Yamasani | ee39874 | 2014-07-24 14:13:36 -0700 | [diff] [blame] | 1088 | |
| 1089 | @Override |
| 1090 | public void onPhotoChanged(Drawable photo) { |
| 1091 | mMePreference.setIcon(photo); |
| 1092 | } |
| 1093 | |
| 1094 | @Override |
| 1095 | public void onLabelChanged(CharSequence label) { |
| 1096 | mMePreference.setTitle(label); |
| 1097 | } |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 1098 | |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 1099 | /** |
| 1100 | * Returns a default user icon (as a {@link Bitmap}) for the given user. |
| 1101 | * |
| 1102 | * Note that for guest users, you should pass in {@code UserHandle.USER_NULL}. |
Tony Mak | 0ba8f51 | 2017-11-23 16:58:20 +0800 | [diff] [blame] | 1103 | * @param resources resources object to fetch the user icon. |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 1104 | * @param userId the user id or {@code UserHandle.USER_NULL} for a non-user specific icon |
| 1105 | */ |
Tony Mak | 0ba8f51 | 2017-11-23 16:58:20 +0800 | [diff] [blame] | 1106 | private static Bitmap getDefaultUserIconAsBitmap(Resources resources, int userId) { |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 1107 | Bitmap bitmap = null; |
| 1108 | // Try finding the corresponding bitmap in the dark bitmap cache |
| 1109 | bitmap = sDarkDefaultUserBitmapCache.get(userId); |
| 1110 | if (bitmap == null) { |
Tony Mak | 0ba8f51 | 2017-11-23 16:58:20 +0800 | [diff] [blame] | 1111 | bitmap = UserIcons.convertToBitmap( |
| 1112 | UserIcons.getDefaultUserIcon(resources, userId, false)); |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 1113 | // Save it to cache |
| 1114 | sDarkDefaultUserBitmapCache.put(userId, bitmap); |
| 1115 | } |
| 1116 | return bitmap; |
| 1117 | } |
| 1118 | |
| 1119 | /** |
| 1120 | * Assign the default photo to user with {@paramref userId} |
| 1121 | * @param context used to get the {@link UserManager} |
| 1122 | * @param userId used to get the icon bitmap |
| 1123 | * @return true if assign photo successfully, false if failed |
| 1124 | */ |
| 1125 | @VisibleForTesting |
| 1126 | static boolean assignDefaultPhoto(Context context, int userId) { |
| 1127 | if (context == null) { |
| 1128 | return false; |
| 1129 | } |
| 1130 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Tony Mak | 0ba8f51 | 2017-11-23 16:58:20 +0800 | [diff] [blame] | 1131 | Bitmap bitmap = getDefaultUserIconAsBitmap(context.getResources(), userId); |
Fan Zhang | 4e540db | 2017-11-08 13:06:38 -0800 | [diff] [blame] | 1132 | um.setUserIcon(userId, bitmap); |
| 1133 | |
| 1134 | return true; |
| 1135 | } |
| 1136 | |
| 1137 | @WorkerThread |
| 1138 | static void copyMeProfilePhoto(Context context, UserInfo user) { |
| 1139 | Uri contactUri = ContactsContract.Profile.CONTENT_URI; |
| 1140 | |
| 1141 | int userId = user != null ? user.id : UserHandle.myUserId(); |
| 1142 | |
| 1143 | InputStream avatarDataStream = ContactsContract.Contacts.openContactPhotoInputStream( |
| 1144 | context.getContentResolver(), |
| 1145 | contactUri, true); |
| 1146 | // If there's no profile photo, assign a default avatar |
| 1147 | if (avatarDataStream == null) { |
| 1148 | assignDefaultPhoto(context, userId); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 1153 | Bitmap icon = BitmapFactory.decodeStream(avatarDataStream); |
| 1154 | um.setUserIcon(userId, icon); |
| 1155 | try { |
| 1156 | avatarDataStream.close(); |
| 1157 | } catch (IOException ioe) { } |
| 1158 | } |
| 1159 | |
Doris Ling | 81f22e8 | 2017-03-23 11:10:14 -0700 | [diff] [blame] | 1160 | private static class SummaryProvider implements SummaryLoader.SummaryProvider { |
| 1161 | |
| 1162 | private final Context mContext; |
| 1163 | private final SummaryLoader mSummaryLoader; |
| 1164 | |
| 1165 | public SummaryProvider(Context context, SummaryLoader summaryLoader) { |
| 1166 | mContext = context; |
| 1167 | mSummaryLoader = summaryLoader; |
| 1168 | } |
| 1169 | |
| 1170 | @Override |
| 1171 | public void setListening(boolean listening) { |
| 1172 | if (listening) { |
| 1173 | UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo( |
| 1174 | UserHandle.myUserId()); |
| 1175 | mSummaryLoader.setSummary(this, mContext.getString(R.string.users_summary, |
| 1176 | info.name)); |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY = |
| 1182 | new SummaryLoader.SummaryProviderFactory() { |
| 1183 | @Override |
| 1184 | public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity, |
| 1185 | SummaryLoader summaryLoader) { |
| 1186 | return new SummaryProvider(activity, summaryLoader); |
| 1187 | } |
| 1188 | }; |
| 1189 | |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 1190 | public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = |
| 1191 | new BaseSearchIndexProvider() { |
| 1192 | @Override |
| 1193 | public List<SearchIndexableRaw> getRawDataToIndex(Context context, |
| 1194 | boolean enabled) { |
| 1195 | final List<SearchIndexableRaw> result = new ArrayList<>(); |
| 1196 | final UserCapabilities userCaps = UserCapabilities.create(context); |
| 1197 | if (!userCaps.mEnabled) { |
| 1198 | return result; |
| 1199 | } |
| 1200 | final Resources res = context.getResources(); |
| 1201 | SearchIndexableRaw data = new SearchIndexableRaw(context); |
| 1202 | data.title = res.getString(R.string.user_settings_title); |
Fan Zhang | 7d5c9ac | 2017-11-16 09:56:03 -0800 | [diff] [blame] | 1203 | data.key = "users_settings"; |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 1204 | data.screenTitle = res.getString(R.string.user_settings_title); |
| 1205 | result.add(data); |
| 1206 | |
Sudheer Shanka | 29737ad | 2016-02-11 17:17:21 +0000 | [diff] [blame] | 1207 | if (userCaps.mCanAddUser || userCaps.mDisallowAddUserSetByAdmin) { |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 1208 | data = new SearchIndexableRaw(context); |
| 1209 | data.title = res.getString(userCaps.mCanAddRestrictedProfile ? |
| 1210 | R.string.user_add_user_or_profile_menu |
| 1211 | : R.string.user_add_user_menu); |
| 1212 | data.screenTitle = res.getString(R.string.user_settings_title); |
Fan Zhang | 7d5c9ac | 2017-11-16 09:56:03 -0800 | [diff] [blame] | 1213 | data.key = "user_settings_add_users"; |
Fyodor Kupolov | c9a95ef | 2015-05-15 17:16:15 -0700 | [diff] [blame] | 1214 | result.add(data); |
| 1215 | } |
| 1216 | return result; |
| 1217 | } |
| 1218 | }; |
| 1219 | |
Amith Yamasani | b810a0d | 2012-03-25 10:12:26 -0700 | [diff] [blame] | 1220 | } |