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