Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.systemui.statusbar.policy; |
| 18 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 19 | import android.app.ActivityManager; |
| 20 | import android.app.ActivityManagerNative; |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 21 | import android.app.Dialog; |
Fyodor Kupolov | f4d6ad2 | 2015-04-13 11:52:18 -0700 | [diff] [blame] | 22 | import android.app.Notification; |
| 23 | import android.app.NotificationManager; |
| 24 | import android.app.PendingIntent; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.Context; |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 27 | import android.content.DialogInterface; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 28 | import android.content.Intent; |
| 29 | import android.content.IntentFilter; |
| 30 | import android.content.pm.UserInfo; |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 31 | import android.database.ContentObserver; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 32 | import android.graphics.Bitmap; |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 33 | import android.graphics.drawable.Drawable; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 34 | import android.os.AsyncTask; |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 35 | import android.os.Handler; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 36 | import android.os.RemoteException; |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 37 | import android.os.UserHandle; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 38 | import android.os.UserManager; |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 39 | import android.provider.Settings; |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 40 | import android.telephony.PhoneStateListener; |
| 41 | import android.telephony.TelephonyManager; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 42 | import android.util.Log; |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 43 | import android.util.SparseArray; |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 44 | import android.util.SparseBooleanArray; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 45 | import android.view.View; |
| 46 | import android.view.ViewGroup; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 47 | import android.widget.BaseAdapter; |
| 48 | |
Tamas Berghammer | cbd3f0c | 2016-06-22 15:21:38 +0100 | [diff] [blame^] | 49 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Alexandra Gherghina | 64d4dca | 2014-08-28 18:26:56 +0100 | [diff] [blame] | 50 | import com.android.internal.util.UserIcons; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 51 | import com.android.settingslib.RestrictedLockUtils; |
Alexandra Gherghina | 64d4dca | 2014-08-28 18:26:56 +0100 | [diff] [blame] | 52 | import com.android.systemui.GuestResumeSessionReceiver; |
| 53 | import com.android.systemui.R; |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 54 | import com.android.systemui.SystemUI; |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 55 | import com.android.systemui.SystemUISecondaryUserService; |
Alexandra Gherghina | 64d4dca | 2014-08-28 18:26:56 +0100 | [diff] [blame] | 56 | import com.android.systemui.qs.QSTile; |
| 57 | import com.android.systemui.qs.tiles.UserDetailView; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 58 | import com.android.systemui.statusbar.phone.ActivityStarter; |
Alexandra Gherghina | 64d4dca | 2014-08-28 18:26:56 +0100 | [diff] [blame] | 59 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
| 60 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 61 | import java.io.FileDescriptor; |
| 62 | import java.io.PrintWriter; |
| 63 | import java.lang.ref.WeakReference; |
| 64 | import java.util.ArrayList; |
| 65 | import java.util.List; |
| 66 | |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 67 | import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
| 68 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 69 | /** |
| 70 | * Keeps a list of all users on the device for user switching. |
| 71 | */ |
| 72 | public class UserSwitcherController { |
| 73 | |
| 74 | private static final String TAG = "UserSwitcherController"; |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 75 | private static final boolean DEBUG = false; |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 76 | private static final String SIMPLE_USER_SWITCHER_GLOBAL_SETTING = |
| 77 | "lockscreenSimpleUserSwitcher"; |
Fyodor Kupolov | f4d6ad2 | 2015-04-13 11:52:18 -0700 | [diff] [blame] | 78 | private static final String ACTION_REMOVE_GUEST = "com.android.systemui.REMOVE_GUEST"; |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 79 | private static final String ACTION_LOGOUT_USER = "com.android.systemui.LOGOUT_USER"; |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 80 | private static final int PAUSE_REFRESH_USERS_TIMEOUT_MS = 3000; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 81 | |
Amith Yamasani | d81f827 | 2015-07-23 17:15:45 -0700 | [diff] [blame] | 82 | private static final int ID_REMOVE_GUEST = 1010; |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 83 | private static final int ID_LOGOUT_USER = 1011; |
Amith Yamasani | d81f827 | 2015-07-23 17:15:45 -0700 | [diff] [blame] | 84 | private static final String TAG_REMOVE_GUEST = "remove_guest"; |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 85 | private static final String TAG_LOGOUT_USER = "logout_user"; |
| 86 | |
Amith Yamasani | d81f827 | 2015-07-23 17:15:45 -0700 | [diff] [blame] | 87 | private static final String PERMISSION_SELF = "com.android.systemui.permission.SELF"; |
| 88 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 89 | private final Context mContext; |
| 90 | private final UserManager mUserManager; |
| 91 | private final ArrayList<WeakReference<BaseUserAdapter>> mAdapters = new ArrayList<>(); |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 92 | private final GuestResumeSessionReceiver mGuestResumeSessionReceiver |
| 93 | = new GuestResumeSessionReceiver(); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 94 | private final KeyguardMonitor mKeyguardMonitor; |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 95 | private final Handler mHandler; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 96 | private final ActivityStarter mActivityStarter; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 97 | |
| 98 | private ArrayList<UserRecord> mUsers = new ArrayList<>(); |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 99 | private Dialog mExitGuestDialog; |
Adrian Roos | 0c6763a | 2014-09-08 19:11:00 +0200 | [diff] [blame] | 100 | private Dialog mAddUserDialog; |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 101 | private int mLastNonGuestUser = UserHandle.USER_SYSTEM; |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 102 | private boolean mSimpleUserSwitcher; |
| 103 | private boolean mAddUsersWhenLocked; |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 104 | private boolean mPauseRefreshUsers; |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 105 | private int mSecondaryUser = UserHandle.USER_NULL; |
| 106 | private Intent mSecondaryUserServiceIntent; |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 107 | private SparseBooleanArray mForcePictureLoadForUserId = new SparseBooleanArray(2); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 108 | |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 109 | public UserSwitcherController(Context context, KeyguardMonitor keyguardMonitor, |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 110 | Handler handler, ActivityStarter activityStarter) { |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 111 | mContext = context; |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 112 | mGuestResumeSessionReceiver.register(context); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 113 | mKeyguardMonitor = keyguardMonitor; |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 114 | mHandler = handler; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 115 | mActivityStarter = activityStarter; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 116 | mUserManager = UserManager.get(context); |
| 117 | IntentFilter filter = new IntentFilter(); |
| 118 | filter.addAction(Intent.ACTION_USER_ADDED); |
| 119 | filter.addAction(Intent.ACTION_USER_REMOVED); |
| 120 | filter.addAction(Intent.ACTION_USER_INFO_CHANGED); |
| 121 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 122 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 123 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 124 | mContext.registerReceiverAsUser(mReceiver, UserHandle.SYSTEM, filter, |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 125 | null /* permission */, null /* scheduler */); |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 126 | |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 127 | mSecondaryUserServiceIntent = new Intent(context, SystemUISecondaryUserService.class); |
| 128 | |
Amith Yamasani | d81f827 | 2015-07-23 17:15:45 -0700 | [diff] [blame] | 129 | filter = new IntentFilter(); |
| 130 | filter.addAction(ACTION_REMOVE_GUEST); |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 131 | filter.addAction(ACTION_LOGOUT_USER); |
Amith Yamasani | 5891a34 | 2015-07-24 13:50:58 -0700 | [diff] [blame] | 132 | mContext.registerReceiverAsUser(mReceiver, UserHandle.SYSTEM, filter, |
Amith Yamasani | d81f827 | 2015-07-23 17:15:45 -0700 | [diff] [blame] | 133 | PERMISSION_SELF, null /* scheduler */); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 134 | |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 135 | mContext.getContentResolver().registerContentObserver( |
| 136 | Settings.Global.getUriFor(SIMPLE_USER_SWITCHER_GLOBAL_SETTING), true, |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 137 | mSettingsObserver); |
| 138 | mContext.getContentResolver().registerContentObserver( |
| 139 | Settings.Global.getUriFor(Settings.Global.ADD_USERS_WHEN_LOCKED), true, |
| 140 | mSettingsObserver); |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 141 | mContext.getContentResolver().registerContentObserver( |
| 142 | Settings.Global.getUriFor( |
| 143 | Settings.Global.ALLOW_USER_SWITCHING_WHEN_SYSTEM_USER_LOCKED), |
| 144 | true, mSettingsObserver); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 145 | // Fetch initial values. |
| 146 | mSettingsObserver.onChange(false); |
| 147 | |
| 148 | keyguardMonitor.addCallback(mCallback); |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 149 | listenForCallState(); |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 150 | |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 151 | refreshUsers(UserHandle.USER_NULL); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 154 | /** |
| 155 | * Refreshes users from UserManager. |
| 156 | * |
| 157 | * The pictures are only loaded if they have not been loaded yet. |
| 158 | * |
| 159 | * @param forcePictureLoadForId forces the picture of the given user to be reloaded. |
| 160 | */ |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 161 | @SuppressWarnings("unchecked") |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 162 | private void refreshUsers(int forcePictureLoadForId) { |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 163 | if (DEBUG) Log.d(TAG, "refreshUsers(forcePictureLoadForId=" + forcePictureLoadForId+")"); |
| 164 | if (forcePictureLoadForId != UserHandle.USER_NULL) { |
| 165 | mForcePictureLoadForUserId.put(forcePictureLoadForId, true); |
| 166 | } |
| 167 | |
| 168 | if (mPauseRefreshUsers) { |
| 169 | return; |
| 170 | } |
Adrian Roos | c5db390 | 2014-11-21 13:54:04 +0000 | [diff] [blame] | 171 | |
Adrian Roos | d390b89 | 2016-05-05 10:50:49 -0400 | [diff] [blame] | 172 | boolean forceAllUsers = mForcePictureLoadForUserId.get(UserHandle.USER_ALL); |
Adrian Roos | c5db390 | 2014-11-21 13:54:04 +0000 | [diff] [blame] | 173 | SparseArray<Bitmap> bitmaps = new SparseArray<>(mUsers.size()); |
| 174 | final int N = mUsers.size(); |
| 175 | for (int i = 0; i < N; i++) { |
| 176 | UserRecord r = mUsers.get(i); |
Adrian Roos | d390b89 | 2016-05-05 10:50:49 -0400 | [diff] [blame] | 177 | if (r == null || r.picture == null || r.info == null || forceAllUsers |
| 178 | || mForcePictureLoadForUserId.get(r.info.id)) { |
Adrian Roos | c5db390 | 2014-11-21 13:54:04 +0000 | [diff] [blame] | 179 | continue; |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 180 | } |
Adrian Roos | c5db390 | 2014-11-21 13:54:04 +0000 | [diff] [blame] | 181 | bitmaps.put(r.info.id, r.picture); |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 182 | } |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 183 | mForcePictureLoadForUserId.clear(); |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 184 | |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 185 | final boolean addUsersWhenLocked = mAddUsersWhenLocked; |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 186 | new AsyncTask<SparseArray<Bitmap>, Void, ArrayList<UserRecord>>() { |
| 187 | @SuppressWarnings("unchecked") |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 188 | @Override |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 189 | protected ArrayList<UserRecord> doInBackground(SparseArray<Bitmap>... params) { |
| 190 | final SparseArray<Bitmap> bitmaps = params[0]; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 191 | List<UserInfo> infos = mUserManager.getUsers(true); |
| 192 | if (infos == null) { |
| 193 | return null; |
| 194 | } |
| 195 | ArrayList<UserRecord> records = new ArrayList<>(infos.size()); |
| 196 | int currentId = ActivityManager.getCurrentUser(); |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 197 | boolean canSwitchUsers = mUserManager.canSwitchUsers(); |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 198 | UserInfo currentUserInfo = null; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 199 | UserRecord guestRecord = null; |
| 200 | |
| 201 | for (UserInfo info : infos) { |
| 202 | boolean isCurrent = currentId == info.id; |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 203 | if (isCurrent) { |
| 204 | currentUserInfo = info; |
| 205 | } |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 206 | boolean switchToEnabled = canSwitchUsers || isCurrent; |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 207 | if (info.isEnabled()) { |
| 208 | if (info.isGuest()) { |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 209 | // Tapping guest icon triggers remove and a user switch therefore |
| 210 | // the icon shouldn't be enabled even if the user is current |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 211 | guestRecord = new UserRecord(info, null /* picture */, |
| 212 | true /* isGuest */, isCurrent, false /* isAddUser */, |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 213 | false /* isRestricted */, canSwitchUsers); |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 214 | } else if (info.supportsSwitchToByUser()) { |
| 215 | Bitmap picture = bitmaps.get(info.id); |
| 216 | if (picture == null) { |
| 217 | picture = mUserManager.getUserIcon(info.id); |
Adrian Roos | cba0faa | 2014-11-17 17:41:28 +0100 | [diff] [blame] | 218 | |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 219 | if (picture != null) { |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 220 | int avatarSize = mContext.getResources() |
| 221 | .getDimensionPixelSize(R.dimen.max_avatar_size); |
| 222 | picture = Bitmap.createScaledBitmap( |
| 223 | picture, avatarSize, avatarSize, true); |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 224 | } |
Adrian Roos | cba0faa | 2014-11-17 17:41:28 +0100 | [diff] [blame] | 225 | } |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 226 | int index = isCurrent ? 0 : records.size(); |
| 227 | records.add(index, new UserRecord(info, picture, false /* isGuest */, |
| 228 | isCurrent, false /* isAddUser */, false /* isRestricted */, |
| 229 | switchToEnabled)); |
Dan Sandler | 4d75c07 | 2014-07-17 16:01:28 -0400 | [diff] [blame] | 230 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 234 | boolean systemCanCreateUsers = !mUserManager.hasBaseUserRestriction( |
| 235 | UserManager.DISALLOW_ADD_USER, UserHandle.SYSTEM); |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 236 | boolean currentUserCanCreateUsers = currentUserInfo != null |
| 237 | && (currentUserInfo.isAdmin() |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 238 | || currentUserInfo.id == UserHandle.USER_SYSTEM) |
| 239 | && systemCanCreateUsers; |
| 240 | boolean anyoneCanCreateUsers = systemCanCreateUsers && addUsersWhenLocked; |
| 241 | boolean canCreateGuest = (currentUserCanCreateUsers || anyoneCanCreateUsers) |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 242 | && guestRecord == null; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 243 | boolean canCreateUser = (currentUserCanCreateUsers || anyoneCanCreateUsers) |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 244 | && mUserManager.canAddMoreUsers(); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 245 | boolean createIsRestricted = !addUsersWhenLocked; |
| 246 | |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 247 | if (!mSimpleUserSwitcher) { |
| 248 | if (guestRecord == null) { |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 249 | if (canCreateGuest) { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 250 | guestRecord = new UserRecord(null /* info */, null /* picture */, |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 251 | true /* isGuest */, false /* isCurrent */, |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 252 | false /* isAddUser */, createIsRestricted, canSwitchUsers); |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 253 | checkIfAddUserDisallowedByAdminOnly(guestRecord); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 254 | records.add(guestRecord); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 255 | } |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 256 | } else { |
Adrian Roos | bed6e3b | 2014-08-21 20:31:41 +0200 | [diff] [blame] | 257 | int index = guestRecord.isCurrent ? 0 : records.size(); |
| 258 | records.add(index, guestRecord); |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 259 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Jason Monk | 092be7d | 2014-09-02 15:26:13 -0400 | [diff] [blame] | 262 | if (!mSimpleUserSwitcher && canCreateUser) { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 263 | UserRecord addUserRecord = new UserRecord(null /* info */, null /* picture */, |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 264 | false /* isGuest */, false /* isCurrent */, true /* isAddUser */, |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 265 | createIsRestricted, canSwitchUsers); |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 266 | checkIfAddUserDisallowedByAdminOnly(addUserRecord); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 267 | records.add(addUserRecord); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 270 | return records; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | protected void onPostExecute(ArrayList<UserRecord> userRecords) { |
| 275 | if (userRecords != null) { |
| 276 | mUsers = userRecords; |
| 277 | notifyAdapters(); |
| 278 | } |
| 279 | } |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 280 | }.execute((SparseArray) bitmaps); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 283 | private void pauseRefreshUsers() { |
| 284 | if (!mPauseRefreshUsers) { |
| 285 | mHandler.postDelayed(mUnpauseRefreshUsers, PAUSE_REFRESH_USERS_TIMEOUT_MS); |
| 286 | mPauseRefreshUsers = true; |
| 287 | } |
| 288 | } |
| 289 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 290 | private void notifyAdapters() { |
| 291 | for (int i = mAdapters.size() - 1; i >= 0; i--) { |
| 292 | BaseUserAdapter adapter = mAdapters.get(i).get(); |
| 293 | if (adapter != null) { |
| 294 | adapter.notifyDataSetChanged(); |
| 295 | } else { |
| 296 | mAdapters.remove(i); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 301 | public boolean isSimpleUserSwitcher() { |
| 302 | return mSimpleUserSwitcher; |
| 303 | } |
| 304 | |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 305 | public boolean useFullscreenUserSwitcher() { |
| 306 | // Use adb to override: |
| 307 | // adb shell settings put system enable_fullscreen_user_switcher 0 # Turn it off. |
| 308 | // adb shell settings put system enable_fullscreen_user_switcher 1 # Turn it on. |
| 309 | // Restart SystemUI or adb reboot. |
| 310 | final int DEFAULT = -1; |
| 311 | final int overrideUseFullscreenUserSwitcher = |
| 312 | Settings.System.getInt(mContext.getContentResolver(), |
| 313 | "enable_fullscreen_user_switcher", DEFAULT); |
| 314 | if (overrideUseFullscreenUserSwitcher != DEFAULT) { |
| 315 | return overrideUseFullscreenUserSwitcher != 0; |
| 316 | } |
| 317 | // Otherwise default to the build setting. |
Xiyuan Xia | 40f9dab | 2015-08-17 13:19:30 -0700 | [diff] [blame] | 318 | return mContext.getResources().getBoolean(R.bool.config_enableFullscreenUserSwitcher); |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Xiyuan Xia | 1b30f79 | 2016-01-06 08:50:30 -0800 | [diff] [blame] | 321 | public void logoutCurrentUser() { |
| 322 | int currentUser = ActivityManager.getCurrentUser(); |
| 323 | if (currentUser != UserHandle.USER_SYSTEM) { |
Fyodor Kupolov | b5690bc3 | 2016-03-02 17:50:58 -0800 | [diff] [blame] | 324 | pauseRefreshUsers(); |
| 325 | ActivityManager.logoutCurrentUser(); |
Xiyuan Xia | 1b30f79 | 2016-01-06 08:50:30 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 329 | public void removeUserId(int userId) { |
| 330 | if (userId == UserHandle.USER_SYSTEM) { |
| 331 | Log.w(TAG, "User " + userId + " could not removed."); |
| 332 | return; |
| 333 | } |
| 334 | if (ActivityManager.getCurrentUser() == userId) { |
| 335 | switchToUserId(UserHandle.USER_SYSTEM); |
| 336 | } |
| 337 | if (mUserManager.removeUser(userId)) { |
| 338 | refreshUsers(UserHandle.USER_NULL); |
| 339 | } |
| 340 | } |
| 341 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 342 | public void switchTo(UserRecord record) { |
| 343 | int id; |
| 344 | if (record.isGuest && record.info == null) { |
| 345 | // No guest user. Create one. |
Adrian Roos | f99727c | 2014-09-17 16:15:07 +0200 | [diff] [blame] | 346 | UserInfo guest = mUserManager.createGuest( |
| 347 | mContext, mContext.getString(R.string.guest_nickname)); |
| 348 | if (guest == null) { |
| 349 | // Couldn't create guest, most likely because there already exists one, we just |
| 350 | // haven't reloaded the user list yet. |
| 351 | return; |
| 352 | } |
| 353 | id = guest.id; |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 354 | } else if (record.isAddUser) { |
Adrian Roos | 0c6763a | 2014-09-08 19:11:00 +0200 | [diff] [blame] | 355 | showAddUserDialog(); |
| 356 | return; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 357 | } else { |
| 358 | id = record.info.id; |
| 359 | } |
| 360 | |
| 361 | if (ActivityManager.getCurrentUser() == id) { |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 362 | if (record.isGuest) { |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 363 | showExitGuestDialog(id); |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 364 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 365 | return; |
| 366 | } |
| 367 | |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 368 | switchToUserId(id); |
| 369 | } |
| 370 | |
Fyodor Kupolov | caab266 | 2016-03-01 18:39:27 -0800 | [diff] [blame] | 371 | public void switchTo(int userId) { |
| 372 | final int count = mUsers.size(); |
| 373 | for (int i = 0; i < count; ++i) { |
| 374 | UserRecord record = mUsers.get(i); |
| 375 | if (record.info != null && record.info.id == userId) { |
| 376 | switchTo(record); |
| 377 | return; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | Log.e(TAG, "Couldn't switch to user, id=" + userId); |
| 382 | } |
| 383 | |
Daniel Nishi | 8ba446c | 2016-06-20 18:56:35 -0700 | [diff] [blame] | 384 | public int getSwitchableUserCount() { |
| 385 | int count = 0; |
| 386 | final int N = mUsers.size(); |
| 387 | for (int i = 0; i < N; ++i) { |
| 388 | UserRecord record = mUsers.get(i); |
| 389 | if (record.info != null && record.info.supportsSwitchTo()) { |
| 390 | count++; |
| 391 | } |
| 392 | } |
| 393 | return count; |
| 394 | } |
| 395 | |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 396 | private void switchToUserId(int id) { |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 397 | try { |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 398 | pauseRefreshUsers(); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 399 | ActivityManagerNative.getDefault().switchUser(id); |
| 400 | } catch (RemoteException e) { |
| 401 | Log.e(TAG, "Couldn't switch user.", e); |
| 402 | } |
| 403 | } |
| 404 | |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 405 | private void showExitGuestDialog(int id) { |
| 406 | if (mExitGuestDialog != null && mExitGuestDialog.isShowing()) { |
| 407 | mExitGuestDialog.cancel(); |
| 408 | } |
| 409 | mExitGuestDialog = new ExitGuestDialog(mContext, id); |
| 410 | mExitGuestDialog.show(); |
| 411 | } |
| 412 | |
Adrian Roos | 0c6763a | 2014-09-08 19:11:00 +0200 | [diff] [blame] | 413 | private void showAddUserDialog() { |
| 414 | if (mAddUserDialog != null && mAddUserDialog.isShowing()) { |
| 415 | mAddUserDialog.cancel(); |
| 416 | } |
| 417 | mAddUserDialog = new AddUserDialog(mContext); |
| 418 | mAddUserDialog.show(); |
| 419 | } |
| 420 | |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 421 | private void exitGuest(int id) { |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 422 | int newId = UserHandle.USER_SYSTEM; |
| 423 | if (mLastNonGuestUser != UserHandle.USER_SYSTEM) { |
Adrian Roos | 7044146 | 2014-07-22 16:03:12 +0200 | [diff] [blame] | 424 | UserInfo info = mUserManager.getUserInfo(mLastNonGuestUser); |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 425 | if (info != null && info.isEnabled() && info.supportsSwitchToByUser()) { |
Adrian Roos | 7044146 | 2014-07-22 16:03:12 +0200 | [diff] [blame] | 426 | newId = info.id; |
| 427 | } |
| 428 | } |
| 429 | switchToUserId(newId); |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 430 | mUserManager.removeUser(id); |
| 431 | } |
| 432 | |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 433 | private void listenForCallState() { |
Jason Monk | 705a4bc | 2016-06-30 15:04:11 -0400 | [diff] [blame] | 434 | TelephonyManager.from(mContext).listen(mPhoneStateListener, |
| 435 | PhoneStateListener.LISTEN_CALL_STATE); |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Jason Monk | 705a4bc | 2016-06-30 15:04:11 -0400 | [diff] [blame] | 438 | private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() { |
| 439 | private int mCallState; |
| 440 | |
| 441 | @Override |
| 442 | public void onCallStateChanged(int state, String incomingNumber) { |
| 443 | if (mCallState == state) return; |
| 444 | if (DEBUG) Log.v(TAG, "Call state changed: " + state); |
| 445 | mCallState = state; |
| 446 | int currentUserId = ActivityManager.getCurrentUser(); |
| 447 | UserInfo userInfo = mUserManager.getUserInfo(currentUserId); |
| 448 | if (userInfo != null && userInfo.isGuest()) { |
| 449 | showGuestNotification(currentUserId); |
| 450 | } |
| 451 | refreshUsers(UserHandle.USER_NULL); |
| 452 | } |
| 453 | }; |
| 454 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 455 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 456 | @Override |
| 457 | public void onReceive(Context context, Intent intent) { |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 458 | if (DEBUG) { |
| 459 | Log.v(TAG, "Broadcast: a=" + intent.getAction() |
| 460 | + " user=" + intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 461 | } |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 462 | |
| 463 | boolean unpauseRefreshUsers = false; |
| 464 | int forcePictureLoadForId = UserHandle.USER_NULL; |
| 465 | |
Fyodor Kupolov | f4d6ad2 | 2015-04-13 11:52:18 -0700 | [diff] [blame] | 466 | if (ACTION_REMOVE_GUEST.equals(intent.getAction())) { |
| 467 | int currentUser = ActivityManager.getCurrentUser(); |
| 468 | UserInfo userInfo = mUserManager.getUserInfo(currentUser); |
| 469 | if (userInfo != null && userInfo.isGuest()) { |
| 470 | showExitGuestDialog(currentUser); |
| 471 | } |
| 472 | return; |
Adrian Roos | 80996bb | 2015-07-24 16:38:58 -0700 | [diff] [blame] | 473 | } else if (ACTION_LOGOUT_USER.equals(intent.getAction())) { |
Xiyuan Xia | 1b30f79 | 2016-01-06 08:50:30 -0800 | [diff] [blame] | 474 | logoutCurrentUser(); |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 475 | } else if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) { |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 476 | if (mExitGuestDialog != null && mExitGuestDialog.isShowing()) { |
| 477 | mExitGuestDialog.cancel(); |
| 478 | mExitGuestDialog = null; |
| 479 | } |
| 480 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 481 | final int currentId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 482 | final UserInfo userInfo = mUserManager.getUserInfo(currentId); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 483 | final int N = mUsers.size(); |
| 484 | for (int i = 0; i < N; i++) { |
| 485 | UserRecord record = mUsers.get(i); |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 486 | if (record.info == null) continue; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 487 | boolean shouldBeCurrent = record.info.id == currentId; |
| 488 | if (record.isCurrent != shouldBeCurrent) { |
| 489 | mUsers.set(i, record.copyWithIsCurrent(shouldBeCurrent)); |
| 490 | } |
Adrian Roos | 7044146 | 2014-07-22 16:03:12 +0200 | [diff] [blame] | 491 | if (shouldBeCurrent && !record.isGuest) { |
| 492 | mLastNonGuestUser = record.info.id; |
| 493 | } |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 494 | if ((userInfo == null || !userInfo.isAdmin()) && record.isRestricted) { |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 495 | // Immediately remove restricted records in case the AsyncTask is too slow. |
| 496 | mUsers.remove(i); |
| 497 | i--; |
| 498 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 499 | } |
| 500 | notifyAdapters(); |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 501 | |
Winson | 3c2c34b | 2016-04-04 17:47:41 -0700 | [diff] [blame] | 502 | // Disconnect from the old secondary user's service |
| 503 | if (mSecondaryUser != UserHandle.USER_NULL) { |
| 504 | context.stopServiceAsUser(mSecondaryUserServiceIntent, |
| 505 | UserHandle.of(mSecondaryUser)); |
| 506 | mSecondaryUser = UserHandle.USER_NULL; |
| 507 | } |
| 508 | // Connect to the new secondary user's service (purely to ensure that a persistent |
| 509 | // SystemUI application is created for that user) |
| 510 | if (userInfo != null && !userInfo.isPrimary()) { |
| 511 | context.startServiceAsUser(mSecondaryUserServiceIntent, |
| 512 | UserHandle.of(userInfo.id)); |
| 513 | mSecondaryUser = userInfo.id; |
| 514 | } |
| 515 | |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 516 | if (UserManager.isSplitSystemUser() && userInfo != null && !userInfo.isGuest() |
| 517 | && userInfo.id != UserHandle.USER_SYSTEM) { |
| 518 | showLogoutNotification(currentId); |
| 519 | } |
Fyodor Kupolov | ce16186 | 2015-08-18 12:41:52 -0700 | [diff] [blame] | 520 | if (userInfo != null && userInfo.isGuest()) { |
| 521 | showGuestNotification(currentId); |
| 522 | } |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 523 | unpauseRefreshUsers = true; |
| 524 | } else if (Intent.ACTION_USER_INFO_CHANGED.equals(intent.getAction())) { |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 525 | forcePictureLoadForId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 526 | UserHandle.USER_NULL); |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 527 | } else if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) { |
| 528 | // Unlocking the system user may require a refresh |
| 529 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 530 | if (userId != UserHandle.USER_SYSTEM) { |
| 531 | return; |
| 532 | } |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 533 | } |
| 534 | refreshUsers(forcePictureLoadForId); |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 535 | if (unpauseRefreshUsers) { |
| 536 | mUnpauseRefreshUsers.run(); |
| 537 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 538 | } |
Fyodor Kupolov | f4d6ad2 | 2015-04-13 11:52:18 -0700 | [diff] [blame] | 539 | |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 540 | private void showLogoutNotification(int userId) { |
| 541 | PendingIntent logoutPI = PendingIntent.getBroadcastAsUser(mContext, |
| 542 | 0, new Intent(ACTION_LOGOUT_USER), 0, UserHandle.SYSTEM); |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 543 | Notification.Builder builder = new Notification.Builder(mContext) |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 544 | .setVisibility(Notification.VISIBILITY_SECRET) |
| 545 | .setPriority(Notification.PRIORITY_MIN) |
| 546 | .setSmallIcon(R.drawable.ic_person) |
| 547 | .setContentTitle(mContext.getString(R.string.user_logout_notification_title)) |
| 548 | .setContentText(mContext.getString(R.string.user_logout_notification_text)) |
Fyodor Kupolov | 52408f4 | 2015-08-17 16:34:07 -0700 | [diff] [blame] | 549 | .setContentIntent(logoutPI) |
Fyodor Kupolov | ce16186 | 2015-08-18 12:41:52 -0700 | [diff] [blame] | 550 | .setOngoing(true) |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 551 | .setShowWhen(false) |
| 552 | .addAction(R.drawable.ic_delete, |
| 553 | mContext.getString(R.string.user_logout_notification_action), |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 554 | logoutPI); |
| 555 | SystemUI.overrideNotificationAppName(mContext, builder); |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 556 | NotificationManager.from(mContext).notifyAsUser(TAG_LOGOUT_USER, ID_LOGOUT_USER, |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 557 | builder.build(), new UserHandle(userId)); |
Xiaohui Chen | 860397f | 2015-07-22 15:03:48 -0700 | [diff] [blame] | 558 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 559 | }; |
| 560 | |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 561 | private void showGuestNotification(int guestUserId) { |
| 562 | boolean canSwitchUsers = mUserManager.canSwitchUsers(); |
| 563 | // Disable 'Remove guest' action if cannot switch users right now |
| 564 | PendingIntent removeGuestPI = canSwitchUsers ? PendingIntent.getBroadcastAsUser(mContext, |
| 565 | 0, new Intent(ACTION_REMOVE_GUEST), 0, UserHandle.SYSTEM) : null; |
| 566 | |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 567 | Notification.Builder builder = new Notification.Builder(mContext) |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 568 | .setVisibility(Notification.VISIBILITY_SECRET) |
| 569 | .setPriority(Notification.PRIORITY_MIN) |
| 570 | .setSmallIcon(R.drawable.ic_person) |
| 571 | .setContentTitle(mContext.getString(R.string.guest_notification_title)) |
| 572 | .setContentText(mContext.getString(R.string.guest_notification_text)) |
| 573 | .setContentIntent(removeGuestPI) |
| 574 | .setShowWhen(false) |
| 575 | .addAction(R.drawable.ic_delete, |
| 576 | mContext.getString(R.string.guest_notification_remove_action), |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 577 | removeGuestPI); |
| 578 | SystemUI.overrideNotificationAppName(mContext, builder); |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 579 | NotificationManager.from(mContext).notifyAsUser(TAG_REMOVE_GUEST, ID_REMOVE_GUEST, |
Adrian Roos | e25c18d | 2016-06-17 15:59:49 -0700 | [diff] [blame] | 580 | builder.build(), new UserHandle(guestUserId)); |
Fyodor Kupolov | 523c404 | 2016-02-24 15:03:13 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Adrian Roos | 88b1193 | 2015-07-22 14:59:48 -0700 | [diff] [blame] | 583 | private final Runnable mUnpauseRefreshUsers = new Runnable() { |
| 584 | @Override |
| 585 | public void run() { |
| 586 | mHandler.removeCallbacks(this); |
| 587 | mPauseRefreshUsers = false; |
| 588 | refreshUsers(UserHandle.USER_NULL); |
| 589 | } |
| 590 | }; |
| 591 | |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 592 | private final ContentObserver mSettingsObserver = new ContentObserver(new Handler()) { |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 593 | public void onChange(boolean selfChange) { |
| 594 | mSimpleUserSwitcher = Settings.Global.getInt(mContext.getContentResolver(), |
| 595 | SIMPLE_USER_SWITCHER_GLOBAL_SETTING, 0) != 0; |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 596 | mAddUsersWhenLocked = Settings.Global.getInt(mContext.getContentResolver(), |
| 597 | Settings.Global.ADD_USERS_WHEN_LOCKED, 0) != 0; |
Jason Monk | 2daf62c | 2014-07-31 14:35:06 -0400 | [diff] [blame] | 598 | refreshUsers(UserHandle.USER_NULL); |
| 599 | }; |
| 600 | }; |
| 601 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 602 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 603 | pw.println("UserSwitcherController state:"); |
Adrian Roos | 7044146 | 2014-07-22 16:03:12 +0200 | [diff] [blame] | 604 | pw.println(" mLastNonGuestUser=" + mLastNonGuestUser); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 605 | pw.print(" mUsers.size="); pw.println(mUsers.size()); |
| 606 | for (int i = 0; i < mUsers.size(); i++) { |
| 607 | final UserRecord u = mUsers.get(i); |
| 608 | pw.print(" "); pw.println(u.toString()); |
| 609 | } |
| 610 | } |
| 611 | |
Adrian Roos | 57cf570 | 2014-09-03 15:56:30 +0200 | [diff] [blame] | 612 | public String getCurrentUserName(Context context) { |
| 613 | if (mUsers.isEmpty()) return null; |
| 614 | UserRecord item = mUsers.get(0); |
| 615 | if (item == null || item.info == null) return null; |
| 616 | if (item.isGuest) return context.getString(R.string.guest_nickname); |
| 617 | return item.info.name; |
| 618 | } |
| 619 | |
Adrian Roos | d390b89 | 2016-05-05 10:50:49 -0400 | [diff] [blame] | 620 | public void onDensityOrFontScaleChanged() { |
| 621 | refreshUsers(UserHandle.USER_ALL); |
| 622 | } |
| 623 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 624 | public static abstract class BaseUserAdapter extends BaseAdapter { |
| 625 | |
| 626 | final UserSwitcherController mController; |
| 627 | |
| 628 | protected BaseUserAdapter(UserSwitcherController controller) { |
| 629 | mController = controller; |
| 630 | controller.mAdapters.add(new WeakReference<>(this)); |
| 631 | } |
| 632 | |
| 633 | @Override |
| 634 | public int getCount() { |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 635 | boolean secureKeyguardShowing = mController.mKeyguardMonitor.isShowing() |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 636 | && mController.mKeyguardMonitor.isSecure() |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 637 | && !mController.mKeyguardMonitor.canSkipBouncer(); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 638 | if (!secureKeyguardShowing) { |
| 639 | return mController.mUsers.size(); |
| 640 | } |
| 641 | // The lock screen is secure and showing. Filter out restricted records. |
| 642 | final int N = mController.mUsers.size(); |
| 643 | int count = 0; |
| 644 | for (int i = 0; i < N; i++) { |
| 645 | if (mController.mUsers.get(i).isRestricted) { |
| 646 | break; |
| 647 | } else { |
| 648 | count++; |
| 649 | } |
| 650 | } |
| 651 | return count; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | @Override |
| 655 | public UserRecord getItem(int position) { |
| 656 | return mController.mUsers.get(position); |
| 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public long getItemId(int position) { |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 661 | return position; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | public void switchTo(UserRecord record) { |
| 665 | mController.switchTo(record); |
| 666 | } |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 667 | |
| 668 | public String getName(Context context, UserRecord item) { |
| 669 | if (item.isGuest) { |
| 670 | if (item.isCurrent) { |
| 671 | return context.getString(R.string.guest_exit_guest); |
| 672 | } else { |
| 673 | return context.getString( |
| 674 | item.info == null ? R.string.guest_new_guest : R.string.guest_nickname); |
| 675 | } |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 676 | } else if (item.isAddUser) { |
| 677 | return context.getString(R.string.user_add_user); |
Adrian Roos | e9c7d43 | 2014-07-17 18:27:38 +0200 | [diff] [blame] | 678 | } else { |
| 679 | return item.info.name; |
| 680 | } |
| 681 | } |
Adrian Roos | 723632e | 2014-07-23 21:13:21 +0200 | [diff] [blame] | 682 | |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 683 | public Drawable getDrawable(Context context, UserRecord item) { |
| 684 | if (item.isAddUser) { |
| 685 | return context.getDrawable(R.drawable.ic_add_circle_qs); |
| 686 | } |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 687 | return UserIcons.getDefaultUserIcon(item.resolveId(), /* light= */ true); |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 688 | } |
Adrian Roos | 844c92b | 2014-12-01 14:19:05 +0100 | [diff] [blame] | 689 | |
| 690 | public void refresh() { |
| 691 | mController.refreshUsers(UserHandle.USER_NULL); |
| 692 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 695 | private void checkIfAddUserDisallowedByAdminOnly(UserRecord record) { |
Sudheer Shanka | ad143c0 | 2016-03-31 00:24:05 +0000 | [diff] [blame] | 696 | EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext, |
| 697 | UserManager.DISALLOW_ADD_USER, ActivityManager.getCurrentUser()); |
| 698 | if (admin != null && !RestrictedLockUtils.hasBaseUserRestriction(mContext, |
| 699 | UserManager.DISALLOW_ADD_USER, ActivityManager.getCurrentUser())) { |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 700 | record.isDisabledByAdmin = true; |
Sudheer Shanka | ad143c0 | 2016-03-31 00:24:05 +0000 | [diff] [blame] | 701 | record.enforcedAdmin = admin; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 702 | } else { |
| 703 | record.isDisabledByAdmin = false; |
| 704 | record.enforcedAdmin = null; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | public void startActivity(Intent intent) { |
| 709 | mActivityStarter.startActivity(intent, true); |
| 710 | } |
| 711 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 712 | public static final class UserRecord { |
| 713 | public final UserInfo info; |
| 714 | public final Bitmap picture; |
| 715 | public final boolean isGuest; |
| 716 | public final boolean isCurrent; |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 717 | public final boolean isAddUser; |
| 718 | /** If true, the record is only visible to the owner and only when unlocked. */ |
| 719 | public final boolean isRestricted; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 720 | public boolean isDisabledByAdmin; |
| 721 | public EnforcedAdmin enforcedAdmin; |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 722 | public boolean isSwitchToEnabled; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 723 | |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 724 | public UserRecord(UserInfo info, Bitmap picture, boolean isGuest, boolean isCurrent, |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 725 | boolean isAddUser, boolean isRestricted, boolean isSwitchToEnabled) { |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 726 | this.info = info; |
| 727 | this.picture = picture; |
| 728 | this.isGuest = isGuest; |
| 729 | this.isCurrent = isCurrent; |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 730 | this.isAddUser = isAddUser; |
| 731 | this.isRestricted = isRestricted; |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 732 | this.isSwitchToEnabled = isSwitchToEnabled; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | public UserRecord copyWithIsCurrent(boolean _isCurrent) { |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 736 | return new UserRecord(info, picture, isGuest, _isCurrent, isAddUser, isRestricted, |
| 737 | isSwitchToEnabled); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 738 | } |
| 739 | |
Evan Rosky | aa7f51f | 2016-03-16 13:15:53 -0700 | [diff] [blame] | 740 | public int resolveId() { |
| 741 | if (isGuest || info == null) { |
| 742 | return UserHandle.USER_NULL; |
| 743 | } |
| 744 | return info.id; |
| 745 | } |
| 746 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 747 | public String toString() { |
| 748 | StringBuilder sb = new StringBuilder(); |
| 749 | sb.append("UserRecord("); |
| 750 | if (info != null) { |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 751 | sb.append("name=\"").append(info.name).append("\" id=").append(info.id); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 752 | } else { |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 753 | if (isGuest) { |
| 754 | sb.append("<add guest placeholder>"); |
| 755 | } else if (isAddUser) { |
| 756 | sb.append("<add user placeholder>"); |
| 757 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 758 | } |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 759 | if (isGuest) sb.append(" <isGuest>"); |
| 760 | if (isAddUser) sb.append(" <isAddUser>"); |
| 761 | if (isCurrent) sb.append(" <isCurrent>"); |
| 762 | if (picture != null) sb.append(" <hasPicture>"); |
| 763 | if (isRestricted) sb.append(" <isRestricted>"); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 764 | if (isDisabledByAdmin) { |
| 765 | sb.append(" <isDisabledByAdmin>"); |
Fyodor Kupolov | 07140f7 | 2016-02-17 10:46:11 -0800 | [diff] [blame] | 766 | sb.append(" enforcedAdmin=").append(enforcedAdmin); |
| 767 | } |
| 768 | if (isSwitchToEnabled) { |
| 769 | sb.append(" <isSwitchToEnabled>"); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 770 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 771 | sb.append(')'); |
| 772 | return sb.toString(); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | public final QSTile.DetailAdapter userDetailAdapter = new QSTile.DetailAdapter() { |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 777 | private final Intent USER_SETTINGS_INTENT = new Intent(Settings.ACTION_USER_SETTINGS); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 778 | |
| 779 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 780 | public CharSequence getTitle() { |
| 781 | return mContext.getString(R.string.quick_settings_user_title); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | @Override |
| 785 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
Adrian Roos | 1940892 | 2014-08-07 20:54:12 +0200 | [diff] [blame] | 786 | UserDetailView v; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 787 | if (!(convertView instanceof UserDetailView)) { |
Adrian Roos | 1940892 | 2014-08-07 20:54:12 +0200 | [diff] [blame] | 788 | v = UserDetailView.inflate(context, parent, false); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 789 | v.createAndSetAdapter(UserSwitcherController.this); |
Adrian Roos | 1940892 | 2014-08-07 20:54:12 +0200 | [diff] [blame] | 790 | } else { |
| 791 | v = (UserDetailView) convertView; |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 792 | } |
Adrian Roos | 844c92b | 2014-12-01 14:19:05 +0100 | [diff] [blame] | 793 | v.refreshAdapter(); |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 794 | return v; |
| 795 | } |
| 796 | |
| 797 | @Override |
| 798 | public Intent getSettingsIntent() { |
| 799 | return USER_SETTINGS_INTENT; |
| 800 | } |
| 801 | |
| 802 | @Override |
| 803 | public Boolean getToggleState() { |
| 804 | return null; |
| 805 | } |
| 806 | |
| 807 | @Override |
| 808 | public void setToggleState(boolean state) { |
| 809 | } |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 810 | |
| 811 | @Override |
| 812 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 813 | return MetricsEvent.QS_USERDETAIL; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 814 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 815 | }; |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 816 | |
Adrian Roos | ccdff62 | 2014-08-06 00:07:18 +0200 | [diff] [blame] | 817 | private final KeyguardMonitor.Callback mCallback = new KeyguardMonitor.Callback() { |
| 818 | @Override |
| 819 | public void onKeyguardChanged() { |
| 820 | notifyAdapters(); |
| 821 | } |
| 822 | }; |
| 823 | |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 824 | private final class ExitGuestDialog extends SystemUIDialog implements |
| 825 | DialogInterface.OnClickListener { |
| 826 | |
| 827 | private final int mGuestId; |
| 828 | |
| 829 | public ExitGuestDialog(Context context, int guestId) { |
| 830 | super(context); |
| 831 | setTitle(R.string.guest_exit_guest_dialog_title); |
| 832 | setMessage(context.getString(R.string.guest_exit_guest_dialog_message)); |
| 833 | setButton(DialogInterface.BUTTON_NEGATIVE, |
Amith Yamasani | e5b274a | 2014-08-18 07:57:56 -0700 | [diff] [blame] | 834 | context.getString(android.R.string.cancel), this); |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 835 | setButton(DialogInterface.BUTTON_POSITIVE, |
Amith Yamasani | e5b274a | 2014-08-18 07:57:56 -0700 | [diff] [blame] | 836 | context.getString(R.string.guest_exit_guest_dialog_remove), this); |
Adrian Roos | 5005244 | 2014-07-17 23:35:18 +0200 | [diff] [blame] | 837 | setCanceledOnTouchOutside(false); |
| 838 | mGuestId = guestId; |
| 839 | } |
| 840 | |
| 841 | @Override |
| 842 | public void onClick(DialogInterface dialog, int which) { |
| 843 | if (which == BUTTON_NEGATIVE) { |
| 844 | cancel(); |
| 845 | } else { |
| 846 | dismiss(); |
| 847 | exitGuest(mGuestId); |
| 848 | } |
| 849 | } |
| 850 | } |
Adrian Roos | 0c6763a | 2014-09-08 19:11:00 +0200 | [diff] [blame] | 851 | |
| 852 | private final class AddUserDialog extends SystemUIDialog implements |
| 853 | DialogInterface.OnClickListener { |
| 854 | |
| 855 | public AddUserDialog(Context context) { |
| 856 | super(context); |
| 857 | setTitle(R.string.user_add_user_title); |
| 858 | setMessage(context.getString(R.string.user_add_user_message_short)); |
| 859 | setButton(DialogInterface.BUTTON_NEGATIVE, |
| 860 | context.getString(android.R.string.cancel), this); |
| 861 | setButton(DialogInterface.BUTTON_POSITIVE, |
| 862 | context.getString(android.R.string.ok), this); |
| 863 | } |
| 864 | |
| 865 | @Override |
| 866 | public void onClick(DialogInterface dialog, int which) { |
| 867 | if (which == BUTTON_NEGATIVE) { |
| 868 | cancel(); |
| 869 | } else { |
| 870 | dismiss(); |
Guang Zhu | ccbeb61 | 2014-10-21 14:33:50 -0700 | [diff] [blame] | 871 | if (ActivityManager.isUserAMonkey()) { |
| 872 | return; |
| 873 | } |
Xiaohui Chen | cfe64c8 | 2015-07-16 14:30:50 -0700 | [diff] [blame] | 874 | UserInfo user = mUserManager.createUser( |
Adrian Roos | f99727c | 2014-09-17 16:15:07 +0200 | [diff] [blame] | 875 | mContext.getString(R.string.user_new_user_name), 0 /* flags */); |
| 876 | if (user == null) { |
| 877 | // Couldn't create user, most likely because there are too many, but we haven't |
| 878 | // been able to reload the list yet. |
| 879 | return; |
| 880 | } |
| 881 | int id = user.id; |
Alexandra Gherghina | 64d4dca | 2014-08-28 18:26:56 +0100 | [diff] [blame] | 882 | Bitmap icon = UserIcons.convertToBitmap(UserIcons.getDefaultUserIcon( |
| 883 | id, /* light= */ false)); |
| 884 | mUserManager.setUserIcon(id, icon); |
Adrian Roos | 0c6763a | 2014-09-08 19:11:00 +0200 | [diff] [blame] | 885 | switchToUserId(id); |
| 886 | } |
| 887 | } |
| 888 | } |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 889 | } |