Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.server.am; |
| 18 | |
| 19 | import static android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 20 | import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 21 | import static android.app.ActivityManager.USER_OP_ERROR_IS_SYSTEM; |
| 22 | import static android.app.ActivityManager.USER_OP_ERROR_RELATED_USERS_CANNOT_STOP; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 23 | import static android.app.ActivityManager.USER_OP_IS_CURRENT; |
| 24 | import static android.app.ActivityManager.USER_OP_SUCCESS; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 25 | import static android.os.Process.SYSTEM_UID; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 26 | import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_MU; |
| 27 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 28 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 29 | import static com.android.server.am.ActivityManagerService.ALLOW_FULL_ONLY; |
| 30 | import static com.android.server.am.ActivityManagerService.ALLOW_NON_FULL; |
| 31 | import static com.android.server.am.ActivityManagerService.ALLOW_NON_FULL_IN_PROFILE; |
| 32 | import static com.android.server.am.ActivityManagerService.MY_PID; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 33 | import static com.android.server.am.ActivityManagerService.REPORT_USER_SWITCH_COMPLETE_MSG; |
| 34 | import static com.android.server.am.ActivityManagerService.REPORT_USER_SWITCH_MSG; |
| 35 | import static com.android.server.am.ActivityManagerService.SYSTEM_USER_CURRENT_MSG; |
| 36 | import static com.android.server.am.ActivityManagerService.SYSTEM_USER_START_MSG; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 37 | import static com.android.server.am.ActivityManagerService.SYSTEM_USER_UNLOCK_MSG; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 38 | import static com.android.server.am.ActivityManagerService.USER_SWITCH_TIMEOUT_MSG; |
| 39 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 40 | import android.annotation.NonNull; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 41 | import android.app.ActivityManager; |
| 42 | import android.app.AppOpsManager; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 43 | import android.app.Dialog; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 44 | import android.app.IStopUserCallback; |
| 45 | import android.app.IUserSwitchObserver; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 46 | import android.content.Context; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 47 | import android.content.IIntentReceiver; |
| 48 | import android.content.Intent; |
| 49 | import android.content.pm.PackageManager; |
| 50 | import android.content.pm.UserInfo; |
| 51 | import android.os.BatteryStats; |
| 52 | import android.os.Binder; |
| 53 | import android.os.Bundle; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 54 | import android.os.Debug; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 55 | import android.os.Handler; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 56 | import android.os.IBinder; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 57 | import android.os.IRemoteCallback; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 58 | import android.os.IUserManager; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 59 | import android.os.Process; |
| 60 | import android.os.RemoteCallbackList; |
| 61 | import android.os.RemoteException; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 62 | import android.os.ServiceManager; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 63 | import android.os.UserHandle; |
| 64 | import android.os.UserManager; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 65 | import android.os.storage.IMountService; |
| 66 | import android.os.storage.StorageManager; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 67 | import android.util.IntArray; |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 68 | import android.util.Pair; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 69 | import android.util.Slog; |
| 70 | import android.util.SparseArray; |
| 71 | import android.util.SparseIntArray; |
| 72 | |
| 73 | import com.android.internal.R; |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 74 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 75 | import com.android.internal.util.ArrayUtils; |
| 76 | import com.android.server.pm.UserManagerService; |
| 77 | |
| 78 | import java.io.PrintWriter; |
| 79 | import java.util.ArrayList; |
| 80 | import java.util.Arrays; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 81 | import java.util.HashSet; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 82 | import java.util.List; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 83 | import java.util.Set; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Helper class for {@link ActivityManagerService} responsible for multi-user functionality. |
| 87 | */ |
| 88 | final class UserController { |
| 89 | private static final String TAG = TAG_WITH_CLASS_NAME ? "UserController" : TAG_AM; |
| 90 | // Maximum number of users we allow to be running at a time. |
| 91 | static final int MAX_RUNNING_USERS = 3; |
| 92 | |
| 93 | // Amount of time we wait for observers to handle a user switch before |
| 94 | // giving up on them and unfreezing the screen. |
| 95 | static final int USER_SWITCH_TIMEOUT = 2 * 1000; |
| 96 | |
| 97 | private final ActivityManagerService mService; |
| 98 | private final Handler mHandler; |
| 99 | |
| 100 | // Holds the current foreground user's id |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 101 | private int mCurrentUserId = UserHandle.USER_SYSTEM; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 102 | // Holds the target user's id during a user switch |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 103 | private int mTargetUserId = UserHandle.USER_NULL; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Which users have been started, so are allowed to run code. |
| 107 | */ |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 108 | @GuardedBy("mService") |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 109 | private final SparseArray<UserState> mStartedUsers = new SparseArray<>(); |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 110 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 111 | /** |
| 112 | * LRU list of history of current users. Most recently current is at the end. |
| 113 | */ |
| 114 | private final ArrayList<Integer> mUserLru = new ArrayList<>(); |
| 115 | |
| 116 | /** |
| 117 | * Constant array of the users that are currently started. |
| 118 | */ |
| 119 | private int[] mStartedUserArray = new int[] { 0 }; |
| 120 | |
| 121 | // If there are multiple profiles for the current user, their ids are here |
| 122 | // Currently only the primary user can have managed profiles |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 123 | private int[] mCurrentProfileIds = new int[] {}; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Mapping from each known user ID to the profile group ID it is associated with. |
| 127 | */ |
| 128 | private final SparseIntArray mUserProfileGroupIdsSelfLocked = new SparseIntArray(); |
| 129 | |
| 130 | /** |
| 131 | * Registered observers of the user switching mechanics. |
| 132 | */ |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 133 | private final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 134 | = new RemoteCallbackList<>(); |
| 135 | |
| 136 | /** |
| 137 | * Currently active user switch. |
| 138 | */ |
| 139 | Object mCurUserSwitchCallback; |
| 140 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 141 | private volatile UserManagerService mUserManager; |
| 142 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 143 | UserController(ActivityManagerService service) { |
| 144 | mService = service; |
| 145 | mHandler = mService.mHandler; |
| 146 | // User 0 is the first and only user that runs at boot. |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 147 | final UserState uss = new UserState(UserHandle.SYSTEM); |
| 148 | mStartedUsers.put(UserHandle.USER_SYSTEM, uss); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 149 | mUserLru.add(UserHandle.USER_SYSTEM); |
| 150 | updateStartedUserArrayLocked(); |
| 151 | } |
| 152 | |
| 153 | void finishUserSwitch(UserState uss) { |
| 154 | synchronized (mService) { |
| 155 | finishUserBoot(uss); |
| 156 | |
| 157 | startProfilesLocked(); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 158 | stopRunningUsersLocked(MAX_RUNNING_USERS); |
| 159 | } |
| 160 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 161 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 162 | void stopRunningUsersLocked(int maxRunningUsers) { |
| 163 | int num = mUserLru.size(); |
| 164 | int i = 0; |
| 165 | while (num > maxRunningUsers && i < mUserLru.size()) { |
| 166 | Integer oldUserId = mUserLru.get(i); |
| 167 | UserState oldUss = mStartedUsers.get(oldUserId); |
| 168 | if (oldUss == null) { |
| 169 | // Shouldn't happen, but be sane if it does. |
| 170 | mUserLru.remove(i); |
| 171 | num--; |
| 172 | continue; |
| 173 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 174 | if (oldUss.state == UserState.STATE_STOPPING |
| 175 | || oldUss.state == UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 176 | // This user is already stopping, doesn't count. |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 177 | num--; |
| 178 | i++; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 179 | continue; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 180 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 181 | if (oldUserId == UserHandle.USER_SYSTEM || oldUserId == mCurrentUserId) { |
| 182 | // Owner/System user and current user can't be stopped. We count it as running |
| 183 | // when it is not a pure system user. |
| 184 | if (UserInfo.isSystemOnly(oldUserId)) { |
| 185 | num--; |
| 186 | } |
| 187 | i++; |
| 188 | continue; |
| 189 | } |
| 190 | // This is a user to be stopped. |
| 191 | if (stopUsersLocked(oldUserId, false, null) != USER_OP_SUCCESS) { |
| 192 | num--; |
| 193 | } |
| 194 | num--; |
| 195 | i++; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | void finishUserBoot(UserState uss) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 200 | finishUserBoot(uss, null); |
| 201 | } |
| 202 | |
| 203 | void finishUserBoot(UserState uss, IIntentReceiver resultTo) { |
| 204 | final int userId = uss.mHandle.getIdentifier(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 205 | synchronized (mService) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 206 | // Bail if we ended up with a stale user |
| 207 | if (mStartedUsers.get(userId) != uss) return; |
| 208 | |
| 209 | // We always walk through all the user lifecycle states to send |
| 210 | // consistent developer events. We step into RUNNING_LOCKED here, |
| 211 | // but we might immediately step into RUNNING below if the user |
| 212 | // storage is already unlocked. |
| 213 | if (uss.state == UserState.STATE_BOOTING) { |
| 214 | uss.setState(UserState.STATE_RUNNING_LOCKED); |
| 215 | |
| 216 | Intent intent = new Intent(Intent.ACTION_LOCKED_BOOT_COMPLETED, null); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 217 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Jeff Sharkey | a242f82 | 2015-12-17 15:38:20 -0700 | [diff] [blame^] | 218 | intent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT |
| 219 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 220 | mService.broadcastIntentLocked(null, null, intent, null, resultTo, 0, null, null, |
| 221 | new String[] { android.Manifest.permission.RECEIVE_BOOT_COMPLETED }, |
| 222 | AppOpsManager.OP_NONE, null, true, false, MY_PID, SYSTEM_UID, userId); |
| 223 | } |
| 224 | |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 225 | maybeUnlockUser(userId); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Consider stepping from {@link UserState#STATE_RUNNING_LOCKED} into |
| 231 | * {@link UserState#STATE_RUNNING}, which only occurs if the user storage is |
| 232 | * actually unlocked. |
| 233 | */ |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 234 | void finishUserUnlock(UserState uss) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 235 | final int userId = uss.mHandle.getIdentifier(); |
| 236 | synchronized (mService) { |
| 237 | // Bail if we ended up with a stale user |
| 238 | if (mStartedUsers.get(uss.mHandle.getIdentifier()) != uss) return; |
| 239 | |
| 240 | // Only keep marching forward if user is actually unlocked |
| 241 | if (!isUserKeyUnlocked(userId)) return; |
| 242 | |
| 243 | if (uss.state == UserState.STATE_RUNNING_LOCKED) { |
| 244 | uss.setState(UserState.STATE_RUNNING); |
| 245 | |
| 246 | mHandler.sendMessage(mHandler.obtainMessage(SYSTEM_USER_UNLOCK_MSG, userId, 0)); |
| 247 | |
| 248 | final Intent unlockedIntent = new Intent(Intent.ACTION_USER_UNLOCKED); |
Jeff Sharkey | af6ec29 | 2015-12-17 11:19:00 -0700 | [diff] [blame] | 249 | unlockedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 250 | unlockedIntent.addFlags( |
| 251 | Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND); |
| 252 | mService.broadcastIntentLocked(null, null, unlockedIntent, null, null, 0, null, |
| 253 | null, null, AppOpsManager.OP_NONE, null, false, false, MY_PID, SYSTEM_UID, |
| 254 | userId); |
| 255 | |
| 256 | final Intent bootIntent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 257 | bootIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Dianne Hackborn | 6ac42ae | 2015-12-08 17:22:10 -0800 | [diff] [blame] | 258 | bootIntent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT |
| 259 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 260 | mService.broadcastIntentLocked(null, null, bootIntent, null, null, 0, null, null, |
| 261 | new String[] { android.Manifest.permission.RECEIVE_BOOT_COMPLETED }, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 262 | AppOpsManager.OP_NONE, null, true, false, MY_PID, SYSTEM_UID, userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 267 | int stopUser(final int userId, final boolean force, final IStopUserCallback callback) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 268 | if (mService.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
| 269 | != PackageManager.PERMISSION_GRANTED) { |
| 270 | String msg = "Permission Denial: switchUser() from pid=" |
| 271 | + Binder.getCallingPid() |
| 272 | + ", uid=" + Binder.getCallingUid() |
| 273 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 274 | Slog.w(TAG, msg); |
| 275 | throw new SecurityException(msg); |
| 276 | } |
| 277 | if (userId < 0 || userId == UserHandle.USER_SYSTEM) { |
| 278 | throw new IllegalArgumentException("Can't stop system user " + userId); |
| 279 | } |
| 280 | mService.enforceShellRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, |
| 281 | userId); |
| 282 | synchronized (mService) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 283 | return stopUsersLocked(userId, force, callback); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Stops the user along with its related users. The method calls |
| 289 | * {@link #getUsersToStopLocked(int)} to determine the list of users that should be stopped. |
| 290 | */ |
| 291 | private int stopUsersLocked(final int userId, boolean force, final IStopUserCallback callback) { |
| 292 | if (userId == UserHandle.USER_SYSTEM) { |
| 293 | return USER_OP_ERROR_IS_SYSTEM; |
| 294 | } |
| 295 | if (isCurrentUserLocked(userId)) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 296 | return USER_OP_IS_CURRENT; |
| 297 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 298 | int[] usersToStop = getUsersToStopLocked(userId); |
| 299 | // If one of related users is system or current, no related users should be stopped |
| 300 | for (int i = 0; i < usersToStop.length; i++) { |
| 301 | int relatedUserId = usersToStop[i]; |
| 302 | if ((UserHandle.USER_SYSTEM == relatedUserId) || isCurrentUserLocked(relatedUserId)) { |
| 303 | if (DEBUG_MU) Slog.i(TAG, "stopUsersLocked cannot stop related user " |
| 304 | + relatedUserId); |
| 305 | // We still need to stop the requested user if it's a force stop. |
| 306 | if (force) { |
| 307 | stopSingleUserLocked(userId, callback); |
| 308 | } |
| 309 | return USER_OP_ERROR_RELATED_USERS_CANNOT_STOP; |
| 310 | } |
| 311 | } |
| 312 | if (DEBUG_MU) Slog.i(TAG, "stopUsersLocked usersToStop=" + Arrays.toString(usersToStop)); |
| 313 | for (int userIdToStop : usersToStop) { |
| 314 | stopSingleUserLocked(userIdToStop, userIdToStop == userId ? callback : null); |
| 315 | } |
| 316 | return USER_OP_SUCCESS; |
| 317 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 318 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 319 | private void stopSingleUserLocked(final int userId, final IStopUserCallback callback) { |
| 320 | if (DEBUG_MU) Slog.i(TAG, "stopSingleUserLocked userId=" + userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 321 | final UserState uss = mStartedUsers.get(userId); |
| 322 | if (uss == null) { |
| 323 | // User is not started, nothing to do... but we do need to |
| 324 | // callback if requested. |
| 325 | if (callback != null) { |
| 326 | mHandler.post(new Runnable() { |
| 327 | @Override |
| 328 | public void run() { |
| 329 | try { |
| 330 | callback.userStopped(userId); |
| 331 | } catch (RemoteException e) { |
| 332 | } |
| 333 | } |
| 334 | }); |
| 335 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 336 | return; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (callback != null) { |
| 340 | uss.mStopCallbacks.add(callback); |
| 341 | } |
| 342 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 343 | if (uss.state != UserState.STATE_STOPPING |
| 344 | && uss.state != UserState.STATE_SHUTDOWN) { |
| 345 | uss.setState(UserState.STATE_STOPPING); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 346 | updateStartedUserArrayLocked(); |
| 347 | |
| 348 | long ident = Binder.clearCallingIdentity(); |
| 349 | try { |
| 350 | // We are going to broadcast ACTION_USER_STOPPING and then |
| 351 | // once that is done send a final ACTION_SHUTDOWN and then |
| 352 | // stop the user. |
| 353 | final Intent stoppingIntent = new Intent(Intent.ACTION_USER_STOPPING); |
| 354 | stoppingIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 355 | stoppingIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 356 | stoppingIntent.putExtra(Intent.EXTRA_SHUTDOWN_USERSPACE_ONLY, true); |
| 357 | final Intent shutdownIntent = new Intent(Intent.ACTION_SHUTDOWN); |
| 358 | // This is the result receiver for the final shutdown broadcast. |
| 359 | final IIntentReceiver shutdownReceiver = new IIntentReceiver.Stub() { |
| 360 | @Override |
| 361 | public void performReceive(Intent intent, int resultCode, String data, |
| 362 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 363 | finishUserStop(uss); |
| 364 | } |
| 365 | }; |
| 366 | // This is the result receiver for the initial stopping broadcast. |
| 367 | final IIntentReceiver stoppingReceiver = new IIntentReceiver.Stub() { |
| 368 | @Override |
| 369 | public void performReceive(Intent intent, int resultCode, String data, |
| 370 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 371 | // On to the next. |
| 372 | synchronized (mService) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 373 | if (uss.state != UserState.STATE_STOPPING) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 374 | // Whoops, we are being started back up. Abort, abort! |
| 375 | return; |
| 376 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 377 | uss.setState(UserState.STATE_SHUTDOWN); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 378 | } |
| 379 | mService.mBatteryStatsService.noteEvent( |
| 380 | BatteryStats.HistoryItem.EVENT_USER_RUNNING_FINISH, |
| 381 | Integer.toString(userId), userId); |
| 382 | mService.mSystemServiceManager.stopUser(userId); |
| 383 | mService.broadcastIntentLocked(null, null, shutdownIntent, |
| 384 | null, shutdownReceiver, 0, null, null, null, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 385 | null, true, false, MY_PID, SYSTEM_UID, userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 386 | } |
| 387 | }; |
| 388 | // Kick things off. |
| 389 | mService.broadcastIntentLocked(null, null, stoppingIntent, |
| 390 | null, stoppingReceiver, 0, null, null, |
| 391 | new String[]{INTERACT_ACROSS_USERS}, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 392 | null, true, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 393 | } finally { |
| 394 | Binder.restoreCallingIdentity(ident); |
| 395 | } |
| 396 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void finishUserStop(UserState uss) { |
| 400 | final int userId = uss.mHandle.getIdentifier(); |
| 401 | boolean stopped; |
| 402 | ArrayList<IStopUserCallback> callbacks; |
| 403 | synchronized (mService) { |
| 404 | callbacks = new ArrayList<>(uss.mStopCallbacks); |
| 405 | if (mStartedUsers.get(userId) != uss) { |
| 406 | stopped = false; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 407 | } else if (uss.state != UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 408 | stopped = false; |
| 409 | } else { |
| 410 | stopped = true; |
| 411 | // User can no longer run. |
| 412 | mStartedUsers.remove(userId); |
| 413 | mUserLru.remove(Integer.valueOf(userId)); |
| 414 | updateStartedUserArrayLocked(); |
| 415 | |
| 416 | // Clean up all state and processes associated with the user. |
| 417 | // Kill all the processes for the user. |
| 418 | forceStopUserLocked(userId, "finish user"); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | for (int i = 0; i < callbacks.size(); i++) { |
| 423 | try { |
| 424 | if (stopped) callbacks.get(i).userStopped(userId); |
| 425 | else callbacks.get(i).userStopAborted(userId); |
| 426 | } catch (RemoteException e) { |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | if (stopped) { |
| 431 | mService.mSystemServiceManager.cleanupUser(userId); |
| 432 | synchronized (mService) { |
| 433 | mService.mStackSupervisor.removeUserLocked(userId); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 438 | /** |
| 439 | * Determines the list of users that should be stopped together with the specified |
| 440 | * {@code userId}. The returned list includes {@code userId}. |
| 441 | */ |
| 442 | private @NonNull int[] getUsersToStopLocked(int userId) { |
| 443 | int startedUsersSize = mStartedUsers.size(); |
| 444 | IntArray userIds = new IntArray(); |
| 445 | userIds.add(userId); |
| 446 | synchronized (mUserProfileGroupIdsSelfLocked) { |
| 447 | int userGroupId = mUserProfileGroupIdsSelfLocked.get(userId, |
| 448 | UserInfo.NO_PROFILE_GROUP_ID); |
| 449 | for (int i = 0; i < startedUsersSize; i++) { |
| 450 | UserState uss = mStartedUsers.valueAt(i); |
| 451 | int startedUserId = uss.mHandle.getIdentifier(); |
| 452 | // Skip unrelated users (profileGroupId mismatch) |
| 453 | int startedUserGroupId = mUserProfileGroupIdsSelfLocked.get(startedUserId, |
| 454 | UserInfo.NO_PROFILE_GROUP_ID); |
| 455 | boolean sameGroup = (userGroupId != UserInfo.NO_PROFILE_GROUP_ID) |
| 456 | && (userGroupId == startedUserGroupId); |
| 457 | // userId has already been added |
| 458 | boolean sameUserId = startedUserId == userId; |
| 459 | if (!sameGroup || sameUserId) { |
| 460 | continue; |
| 461 | } |
| 462 | userIds.add(startedUserId); |
| 463 | } |
| 464 | } |
| 465 | return userIds.toArray(); |
| 466 | } |
| 467 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 468 | private void forceStopUserLocked(int userId, String reason) { |
| 469 | mService.forceStopPackageLocked(null, -1, false, false, true, false, false, |
| 470 | userId, reason); |
| 471 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
| 472 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 473 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 474 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 475 | mService.broadcastIntentLocked(null, null, intent, |
| 476 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 477 | null, false, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 480 | /** |
| 481 | * Stops the guest user if it has gone to the background. |
| 482 | */ |
| 483 | private void stopGuestUserIfBackground() { |
| 484 | synchronized (mService) { |
| 485 | final int num = mUserLru.size(); |
| 486 | for (int i = 0; i < num; i++) { |
| 487 | Integer oldUserId = mUserLru.get(i); |
| 488 | UserState oldUss = mStartedUsers.get(oldUserId); |
| 489 | if (oldUserId == UserHandle.USER_SYSTEM || oldUserId == mCurrentUserId |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 490 | || oldUss.state == UserState.STATE_STOPPING |
| 491 | || oldUss.state == UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 492 | continue; |
| 493 | } |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 494 | UserInfo userInfo = getUserInfo(oldUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 495 | if (userInfo.isGuest()) { |
| 496 | // This is a user to be stopped. |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 497 | stopUsersLocked(oldUserId, true, null); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void startProfilesLocked() { |
| 505 | if (DEBUG_MU) Slog.i(TAG, "startProfilesLocked"); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 506 | List<UserInfo> profiles = getUserManager().getProfiles( |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 507 | mCurrentUserId, false /* enabledOnly */); |
| 508 | List<UserInfo> profilesToStart = new ArrayList<>(profiles.size()); |
| 509 | for (UserInfo user : profiles) { |
| 510 | if ((user.flags & UserInfo.FLAG_INITIALIZED) == UserInfo.FLAG_INITIALIZED |
| 511 | && user.id != mCurrentUserId) { |
| 512 | profilesToStart.add(user); |
| 513 | } |
| 514 | } |
| 515 | final int profilesToStartSize = profilesToStart.size(); |
| 516 | int i = 0; |
| 517 | for (; i < profilesToStartSize && i < (MAX_RUNNING_USERS - 1); ++i) { |
| 518 | startUser(profilesToStart.get(i).id, /* foreground= */ false); |
| 519 | } |
| 520 | if (i < profilesToStartSize) { |
| 521 | Slog.w(TAG, "More profiles than MAX_RUNNING_USERS"); |
| 522 | } |
| 523 | } |
| 524 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 525 | private UserManagerService getUserManager() { |
| 526 | UserManagerService userManager = mUserManager; |
| 527 | if (userManager == null) { |
| 528 | IBinder b = ServiceManager.getService(Context.USER_SERVICE); |
| 529 | userManager = mUserManager = (UserManagerService) IUserManager.Stub.asInterface(b); |
| 530 | } |
| 531 | return userManager; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 534 | private IMountService getMountService() { |
| 535 | return IMountService.Stub.asInterface(ServiceManager.getService("mount")); |
| 536 | } |
| 537 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 538 | private boolean isUserKeyUnlocked(int userId) { |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 539 | final IMountService mountService = getMountService(); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 540 | if (mountService != null) { |
| 541 | try { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 542 | return mountService.isUserKeyUnlocked(userId); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 543 | } catch (RemoteException e) { |
| 544 | throw e.rethrowAsRuntimeException(); |
| 545 | } |
| 546 | } else { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 547 | Slog.w(TAG, "Mount service not published; guessing locked state based on property"); |
| 548 | return !StorageManager.isFileBasedEncryptionEnabled(); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 552 | boolean startUser(final int userId, final boolean foreground) { |
| 553 | if (mService.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
| 554 | != PackageManager.PERMISSION_GRANTED) { |
| 555 | String msg = "Permission Denial: switchUser() from pid=" |
| 556 | + Binder.getCallingPid() |
| 557 | + ", uid=" + Binder.getCallingUid() |
| 558 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 559 | Slog.w(TAG, msg); |
| 560 | throw new SecurityException(msg); |
| 561 | } |
| 562 | |
| 563 | if (DEBUG_MU) Slog.i(TAG, "starting userid:" + userId + " fore:" + foreground); |
| 564 | |
| 565 | final long ident = Binder.clearCallingIdentity(); |
| 566 | try { |
| 567 | synchronized (mService) { |
| 568 | final int oldUserId = mCurrentUserId; |
| 569 | if (oldUserId == userId) { |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | mService.mStackSupervisor.setLockTaskModeLocked(null, |
| 574 | ActivityManager.LOCK_TASK_MODE_NONE, "startUser", false); |
| 575 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 576 | final UserInfo userInfo = getUserInfo(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 577 | if (userInfo == null) { |
| 578 | Slog.w(TAG, "No user info for user #" + userId); |
| 579 | return false; |
| 580 | } |
| 581 | if (foreground && userInfo.isManagedProfile()) { |
| 582 | Slog.w(TAG, "Cannot switch to User #" + userId + ": not a full user"); |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | if (foreground) { |
| 587 | mService.mWindowManager.startFreezingScreen( |
| 588 | R.anim.screen_user_exit, R.anim.screen_user_enter); |
| 589 | } |
| 590 | |
| 591 | boolean needStart = false; |
| 592 | |
| 593 | // If the user we are switching to is not currently started, then |
| 594 | // we need to start it now. |
| 595 | if (mStartedUsers.get(userId) == null) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 596 | mStartedUsers.put(userId, new UserState(UserHandle.of(userId))); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 597 | updateStartedUserArrayLocked(); |
| 598 | needStart = true; |
| 599 | } |
| 600 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 601 | final UserState uss = mStartedUsers.get(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 602 | final Integer userIdInt = userId; |
| 603 | mUserLru.remove(userIdInt); |
| 604 | mUserLru.add(userIdInt); |
| 605 | |
| 606 | if (foreground) { |
| 607 | mCurrentUserId = userId; |
| 608 | mService.updateUserConfigurationLocked(); |
| 609 | mTargetUserId = UserHandle.USER_NULL; // reset, mCurrentUserId has caught up |
| 610 | updateCurrentProfileIdsLocked(); |
| 611 | mService.mWindowManager.setCurrentUser(userId, mCurrentProfileIds); |
| 612 | // Once the internal notion of the active user has switched, we lock the device |
| 613 | // with the option to show the user switcher on the keyguard. |
| 614 | mService.mWindowManager.lockNow(null); |
| 615 | } else { |
| 616 | final Integer currentUserIdInt = mCurrentUserId; |
| 617 | updateCurrentProfileIdsLocked(); |
| 618 | mService.mWindowManager.setCurrentProfileIds(mCurrentProfileIds); |
| 619 | mUserLru.remove(currentUserIdInt); |
| 620 | mUserLru.add(currentUserIdInt); |
| 621 | } |
| 622 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 623 | // Make sure user is in the started state. If it is currently |
| 624 | // stopping, we need to knock that off. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 625 | if (uss.state == UserState.STATE_STOPPING) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 626 | // If we are stopping, we haven't sent ACTION_SHUTDOWN, |
| 627 | // so we can just fairly silently bring the user back from |
| 628 | // the almost-dead. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 629 | uss.setState(uss.lastState); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 630 | updateStartedUserArrayLocked(); |
| 631 | needStart = true; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 632 | } else if (uss.state == UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 633 | // This means ACTION_SHUTDOWN has been sent, so we will |
| 634 | // need to treat this as a new boot of the user. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 635 | uss.setState(UserState.STATE_BOOTING); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 636 | updateStartedUserArrayLocked(); |
| 637 | needStart = true; |
| 638 | } |
| 639 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 640 | if (uss.state == UserState.STATE_BOOTING) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 641 | // Let user manager propagate user restrictions to other services. |
| 642 | getUserManager().onBeforeStartUser(userId); |
| 643 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 644 | // Booting up a new user, need to tell system services about it. |
| 645 | // Note that this is on the same handler as scheduling of broadcasts, |
| 646 | // which is important because it needs to go first. |
| 647 | mHandler.sendMessage(mHandler.obtainMessage(SYSTEM_USER_START_MSG, userId, 0)); |
| 648 | } |
| 649 | |
| 650 | if (foreground) { |
| 651 | mHandler.sendMessage(mHandler.obtainMessage(SYSTEM_USER_CURRENT_MSG, userId, |
| 652 | oldUserId)); |
| 653 | mHandler.removeMessages(REPORT_USER_SWITCH_MSG); |
| 654 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 655 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG, |
| 656 | oldUserId, userId, uss)); |
| 657 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG, |
| 658 | oldUserId, userId, uss), USER_SWITCH_TIMEOUT); |
| 659 | } |
| 660 | |
| 661 | if (needStart) { |
| 662 | // Send USER_STARTED broadcast |
| 663 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
| 664 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 665 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 666 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 667 | mService.broadcastIntentLocked(null, null, intent, |
| 668 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 669 | null, false, false, MY_PID, SYSTEM_UID, userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if ((userInfo.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
| 673 | if (userId != UserHandle.USER_SYSTEM) { |
| 674 | Intent intent = new Intent(Intent.ACTION_USER_INITIALIZE); |
| 675 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 676 | mService.broadcastIntentLocked(null, null, intent, null, |
| 677 | new IIntentReceiver.Stub() { |
| 678 | public void performReceive(Intent intent, int resultCode, |
| 679 | String data, Bundle extras, boolean ordered, |
| 680 | boolean sticky, int sendingUser) { |
| 681 | onUserInitialized(uss, foreground, oldUserId, userId); |
| 682 | } |
| 683 | }, 0, null, null, null, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 684 | null, true, false, MY_PID, SYSTEM_UID, userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 685 | uss.initializing = true; |
| 686 | } else { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 687 | getUserManager().makeInitialized(userInfo.id); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | if (foreground) { |
| 692 | if (!uss.initializing) { |
| 693 | moveUserToForegroundLocked(uss, oldUserId, userId); |
| 694 | } |
| 695 | } else { |
| 696 | mService.mStackSupervisor.startBackgroundUserLocked(userId, uss); |
| 697 | } |
| 698 | |
| 699 | if (needStart) { |
| 700 | Intent intent = new Intent(Intent.ACTION_USER_STARTING); |
| 701 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 702 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 703 | mService.broadcastIntentLocked(null, null, intent, |
| 704 | null, new IIntentReceiver.Stub() { |
| 705 | @Override |
| 706 | public void performReceive(Intent intent, int resultCode, |
| 707 | String data, Bundle extras, boolean ordered, boolean sticky, |
| 708 | int sendingUser) throws RemoteException { |
| 709 | } |
| 710 | }, 0, null, null, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 711 | new String[] {INTERACT_ACROSS_USERS}, AppOpsManager.OP_NONE, |
| 712 | null, true, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | } finally { |
| 716 | Binder.restoreCallingIdentity(ident); |
| 717 | } |
| 718 | |
| 719 | return true; |
| 720 | } |
| 721 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 722 | /** |
| 723 | * Start user, if its not already running, and bring it to foreground. |
| 724 | */ |
| 725 | boolean startUserInForeground(final int userId, Dialog dlg) { |
| 726 | boolean result = startUser(userId, /* foreground */ true); |
| 727 | dlg.dismiss(); |
| 728 | return result; |
| 729 | } |
| 730 | |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 731 | boolean unlockUser(final int userId, byte[] token) { |
| 732 | if (mService.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
| 733 | != PackageManager.PERMISSION_GRANTED) { |
| 734 | String msg = "Permission Denial: unlockUser() from pid=" |
| 735 | + Binder.getCallingPid() |
| 736 | + ", uid=" + Binder.getCallingUid() |
| 737 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 738 | Slog.w(TAG, msg); |
| 739 | throw new SecurityException(msg); |
| 740 | } |
| 741 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 742 | final long binderToken = Binder.clearCallingIdentity(); |
| 743 | try { |
| 744 | return unlockUserCleared(userId, token); |
| 745 | } finally { |
| 746 | Binder.restoreCallingIdentity(binderToken); |
| 747 | } |
| 748 | } |
| 749 | |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 750 | /** |
| 751 | * Attempt to unlock user without a credential token. This typically |
| 752 | * succeeds when the device doesn't have credential-encrypted storage, or |
| 753 | * when the the credential-encrypted storage isn't tied to a user-provided |
| 754 | * PIN or pattern. |
| 755 | */ |
| 756 | boolean maybeUnlockUser(final int userId) { |
| 757 | // Try unlocking storage using empty token |
| 758 | return unlockUserCleared(userId, null); |
| 759 | } |
| 760 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 761 | boolean unlockUserCleared(final int userId, byte[] token) { |
| 762 | synchronized (mService) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 763 | // Bail if already running unlocked |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 764 | final UserState uss = mStartedUsers.get(userId); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 765 | if (uss.state == UserState.STATE_RUNNING) return true; |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 768 | if (!isUserKeyUnlocked(userId)) { |
| 769 | final UserInfo userInfo = getUserInfo(userId); |
| 770 | final IMountService mountService = getMountService(); |
| 771 | try { |
| 772 | mountService.unlockUserKey(userId, userInfo.serialNumber, token); |
| 773 | } catch (RemoteException | RuntimeException e) { |
| 774 | Slog.w(TAG, "Failed to unlock: " + e.getMessage()); |
| 775 | return false; |
| 776 | } |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | synchronized (mService) { |
| 780 | final UserState uss = mStartedUsers.get(userId); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 781 | finishUserUnlock(uss); |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | return true; |
| 785 | } |
| 786 | |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 787 | void showUserSwitchDialog(Pair<UserInfo, UserInfo> fromToUserPair) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 788 | // The dialog will show and then initiate the user switch by calling startUserInForeground |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 789 | Dialog d = new UserSwitchingDialog(mService, mService.mContext, fromToUserPair.first, |
| 790 | fromToUserPair.second, true /* above system */); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 791 | d.show(); |
| 792 | } |
| 793 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 794 | void dispatchForegroundProfileChanged(int userId) { |
| 795 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 796 | for (int i = 0; i < observerCount; i++) { |
| 797 | try { |
| 798 | mUserSwitchObservers.getBroadcastItem(i).onForegroundProfileSwitch(userId); |
| 799 | } catch (RemoteException e) { |
| 800 | // Ignore |
| 801 | } |
| 802 | } |
| 803 | mUserSwitchObservers.finishBroadcast(); |
| 804 | } |
| 805 | |
| 806 | /** Called on handler thread */ |
| 807 | void dispatchUserSwitchComplete(int userId) { |
| 808 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 809 | for (int i = 0; i < observerCount; i++) { |
| 810 | try { |
| 811 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(userId); |
| 812 | } catch (RemoteException e) { |
| 813 | } |
| 814 | } |
| 815 | mUserSwitchObservers.finishBroadcast(); |
| 816 | } |
| 817 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 818 | private void stopBackgroundUsersIfEnforced(int oldUserId) { |
| 819 | // Never stop system user |
| 820 | if (oldUserId == UserHandle.USER_SYSTEM) { |
| 821 | return; |
| 822 | } |
| 823 | // For now, only check for user restriction. Additional checks can be added here |
| 824 | boolean disallowRunInBg = hasUserRestriction(UserManager.DISALLOW_RUN_IN_BACKGROUND, |
| 825 | oldUserId); |
| 826 | if (!disallowRunInBg) { |
| 827 | return; |
| 828 | } |
| 829 | synchronized (mService) { |
| 830 | if (DEBUG_MU) Slog.i(TAG, "stopBackgroundUsersIfEnforced stopping " + oldUserId |
| 831 | + " and related users"); |
| 832 | stopUsersLocked(oldUserId, false, null); |
| 833 | } |
| 834 | } |
| 835 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 836 | void timeoutUserSwitch(UserState uss, int oldUserId, int newUserId) { |
| 837 | synchronized (mService) { |
Amith Yamasani | ca0ac5c | 2015-11-20 09:44:08 -0800 | [diff] [blame] | 838 | Slog.wtf(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 839 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | void dispatchUserSwitch(final UserState uss, final int oldUserId, |
| 844 | final int newUserId) { |
| 845 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 846 | if (observerCount > 0) { |
| 847 | final IRemoteCallback callback = new IRemoteCallback.Stub() { |
| 848 | int mCount = 0; |
| 849 | @Override |
| 850 | public void sendResult(Bundle data) throws RemoteException { |
| 851 | synchronized (mService) { |
| 852 | if (mCurUserSwitchCallback == this) { |
| 853 | mCount++; |
| 854 | if (mCount == observerCount) { |
| 855 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | }; |
| 861 | synchronized (mService) { |
| 862 | uss.switching = true; |
| 863 | mCurUserSwitchCallback = callback; |
| 864 | } |
| 865 | for (int i = 0; i < observerCount; i++) { |
| 866 | try { |
| 867 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitching( |
| 868 | newUserId, callback); |
| 869 | } catch (RemoteException e) { |
| 870 | } |
| 871 | } |
| 872 | } else { |
| 873 | synchronized (mService) { |
| 874 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 875 | } |
| 876 | } |
| 877 | mUserSwitchObservers.finishBroadcast(); |
| 878 | } |
| 879 | |
| 880 | void sendContinueUserSwitchLocked(UserState uss, int oldUserId, int newUserId) { |
| 881 | mCurUserSwitchCallback = null; |
| 882 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 883 | mHandler.sendMessage(mHandler.obtainMessage(ActivityManagerService.CONTINUE_USER_SWITCH_MSG, |
| 884 | oldUserId, newUserId, uss)); |
| 885 | } |
| 886 | |
| 887 | void continueUserSwitch(UserState uss, int oldUserId, int newUserId) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 888 | completeSwitchAndInitialize(uss, oldUserId, newUserId, false, true); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | void onUserInitialized(UserState uss, boolean foreground, int oldUserId, int newUserId) { |
| 892 | synchronized (mService) { |
| 893 | if (foreground) { |
| 894 | moveUserToForegroundLocked(uss, oldUserId, newUserId); |
| 895 | } |
| 896 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 897 | completeSwitchAndInitialize(uss, oldUserId, newUserId, true, false); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 900 | void completeSwitchAndInitialize(UserState uss, int oldUserId, int newUserId, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 901 | boolean clearInitializing, boolean clearSwitching) { |
| 902 | boolean unfrozen = false; |
| 903 | synchronized (mService) { |
| 904 | if (clearInitializing) { |
| 905 | uss.initializing = false; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 906 | getUserManager().makeInitialized(uss.mHandle.getIdentifier()); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 907 | } |
| 908 | if (clearSwitching) { |
| 909 | uss.switching = false; |
| 910 | } |
| 911 | if (!uss.switching && !uss.initializing) { |
| 912 | mService.mWindowManager.stopFreezingScreen(); |
| 913 | unfrozen = true; |
| 914 | } |
| 915 | } |
| 916 | if (unfrozen) { |
| 917 | mHandler.removeMessages(REPORT_USER_SWITCH_COMPLETE_MSG); |
| 918 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_COMPLETE_MSG, |
| 919 | newUserId, 0)); |
| 920 | } |
| 921 | stopGuestUserIfBackground(); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 922 | stopBackgroundUsersIfEnforced(oldUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | void moveUserToForegroundLocked(UserState uss, int oldUserId, int newUserId) { |
| 926 | boolean homeInFront = mService.mStackSupervisor.switchUserLocked(newUserId, uss); |
| 927 | if (homeInFront) { |
| 928 | mService.startHomeActivityLocked(newUserId, "moveUserToForeground"); |
| 929 | } else { |
| 930 | mService.mStackSupervisor.resumeTopActivitiesLocked(); |
| 931 | } |
| 932 | EventLogTags.writeAmSwitchUser(newUserId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 933 | getUserManager().onUserForeground(newUserId); |
| 934 | sendUserSwitchBroadcastsLocked(oldUserId, newUserId); |
| 935 | } |
| 936 | |
| 937 | void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) { |
| 938 | long ident = Binder.clearCallingIdentity(); |
| 939 | try { |
| 940 | Intent intent; |
| 941 | if (oldUserId >= 0) { |
| 942 | // Send USER_BACKGROUND broadcast to all profiles of the outgoing user |
| 943 | List<UserInfo> profiles = getUserManager().getProfiles(oldUserId, false); |
| 944 | int count = profiles.size(); |
| 945 | for (int i = 0; i < count; i++) { |
| 946 | int profileUserId = profiles.get(i).id; |
| 947 | intent = new Intent(Intent.ACTION_USER_BACKGROUND); |
| 948 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 949 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 950 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileUserId); |
| 951 | mService.broadcastIntentLocked(null, null, intent, |
| 952 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 953 | null, false, false, MY_PID, SYSTEM_UID, profileUserId); |
| 954 | } |
| 955 | } |
| 956 | if (newUserId >= 0) { |
| 957 | // Send USER_FOREGROUND broadcast to all profiles of the incoming user |
| 958 | List<UserInfo> profiles = getUserManager().getProfiles(newUserId, false); |
| 959 | int count = profiles.size(); |
| 960 | for (int i = 0; i < count; i++) { |
| 961 | int profileUserId = profiles.get(i).id; |
| 962 | intent = new Intent(Intent.ACTION_USER_FOREGROUND); |
| 963 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 964 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 965 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileUserId); |
| 966 | mService.broadcastIntentLocked(null, null, intent, |
| 967 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 968 | null, false, false, MY_PID, SYSTEM_UID, profileUserId); |
| 969 | } |
| 970 | intent = new Intent(Intent.ACTION_USER_SWITCHED); |
| 971 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 972 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 973 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 974 | mService.broadcastIntentLocked(null, null, intent, |
| 975 | null, null, 0, null, null, |
| 976 | new String[] {android.Manifest.permission.MANAGE_USERS}, |
| 977 | AppOpsManager.OP_NONE, null, false, false, MY_PID, SYSTEM_UID, |
| 978 | UserHandle.USER_ALL); |
| 979 | } |
| 980 | } finally { |
| 981 | Binder.restoreCallingIdentity(ident); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | |
| 986 | int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 987 | int allowMode, String name, String callerPackage) { |
| 988 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 989 | if (callingUserId == userId) { |
| 990 | return userId; |
| 991 | } |
| 992 | |
| 993 | // Note that we may be accessing mCurrentUserId outside of a lock... |
| 994 | // shouldn't be a big deal, if this is being called outside |
| 995 | // of a locked context there is intrinsically a race with |
| 996 | // the value the caller will receive and someone else changing it. |
| 997 | // We assume that USER_CURRENT_OR_SELF will use the current user; later |
| 998 | // we will switch to the calling user if access to the current user fails. |
| 999 | int targetUserId = unsafeConvertIncomingUserLocked(userId); |
| 1000 | |
| 1001 | if (callingUid != 0 && callingUid != SYSTEM_UID) { |
| 1002 | final boolean allow; |
| 1003 | if (mService.checkComponentPermission(INTERACT_ACROSS_USERS_FULL, callingPid, |
| 1004 | callingUid, -1, true) == PackageManager.PERMISSION_GRANTED) { |
| 1005 | // If the caller has this permission, they always pass go. And collect $200. |
| 1006 | allow = true; |
| 1007 | } else if (allowMode == ALLOW_FULL_ONLY) { |
| 1008 | // We require full access, sucks to be you. |
| 1009 | allow = false; |
| 1010 | } else if (mService.checkComponentPermission(INTERACT_ACROSS_USERS, callingPid, |
| 1011 | callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 1012 | // If the caller does not have either permission, they are always doomed. |
| 1013 | allow = false; |
| 1014 | } else if (allowMode == ALLOW_NON_FULL) { |
| 1015 | // We are blanket allowing non-full access, you lucky caller! |
| 1016 | allow = true; |
| 1017 | } else if (allowMode == ALLOW_NON_FULL_IN_PROFILE) { |
| 1018 | // We may or may not allow this depending on whether the two users are |
| 1019 | // in the same profile. |
| 1020 | allow = isSameProfileGroup(callingUserId, targetUserId); |
| 1021 | } else { |
| 1022 | throw new IllegalArgumentException("Unknown mode: " + allowMode); |
| 1023 | } |
| 1024 | if (!allow) { |
| 1025 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 1026 | // In this case, they would like to just execute as their |
| 1027 | // owner user instead of failing. |
| 1028 | targetUserId = callingUserId; |
| 1029 | } else { |
| 1030 | StringBuilder builder = new StringBuilder(128); |
| 1031 | builder.append("Permission Denial: "); |
| 1032 | builder.append(name); |
| 1033 | if (callerPackage != null) { |
| 1034 | builder.append(" from "); |
| 1035 | builder.append(callerPackage); |
| 1036 | } |
| 1037 | builder.append(" asks to run as user "); |
| 1038 | builder.append(userId); |
| 1039 | builder.append(" but is calling from user "); |
| 1040 | builder.append(UserHandle.getUserId(callingUid)); |
| 1041 | builder.append("; this requires "); |
| 1042 | builder.append(INTERACT_ACROSS_USERS_FULL); |
| 1043 | if (allowMode != ALLOW_FULL_ONLY) { |
| 1044 | builder.append(" or "); |
| 1045 | builder.append(INTERACT_ACROSS_USERS); |
| 1046 | } |
| 1047 | String msg = builder.toString(); |
| 1048 | Slog.w(TAG, msg); |
| 1049 | throw new SecurityException(msg); |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | if (!allowAll && targetUserId < 0) { |
| 1054 | throw new IllegalArgumentException( |
| 1055 | "Call does not support special user #" + targetUserId); |
| 1056 | } |
| 1057 | // Check shell permission |
| 1058 | if (callingUid == Process.SHELL_UID && targetUserId >= UserHandle.USER_SYSTEM) { |
| 1059 | if (hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, targetUserId)) { |
| 1060 | throw new SecurityException("Shell does not have permission to access user " |
| 1061 | + targetUserId + "\n " + Debug.getCallers(3)); |
| 1062 | } |
| 1063 | } |
| 1064 | return targetUserId; |
| 1065 | } |
| 1066 | |
| 1067 | int unsafeConvertIncomingUserLocked(int userId) { |
| 1068 | return (userId == UserHandle.USER_CURRENT || userId == UserHandle.USER_CURRENT_OR_SELF) |
| 1069 | ? getCurrentUserIdLocked(): userId; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | void registerUserSwitchObserver(IUserSwitchObserver observer) { |
| 1073 | if (mService.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
| 1074 | != PackageManager.PERMISSION_GRANTED) { |
| 1075 | final String msg = "Permission Denial: registerUserSwitchObserver() from pid=" |
| 1076 | + Binder.getCallingPid() |
| 1077 | + ", uid=" + Binder.getCallingUid() |
| 1078 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 1079 | Slog.w(TAG, msg); |
| 1080 | throw new SecurityException(msg); |
| 1081 | } |
| 1082 | |
| 1083 | mUserSwitchObservers.register(observer); |
| 1084 | } |
| 1085 | |
| 1086 | void unregisterUserSwitchObserver(IUserSwitchObserver observer) { |
| 1087 | mUserSwitchObservers.unregister(observer); |
| 1088 | } |
| 1089 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1090 | UserState getStartedUserStateLocked(int userId) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1091 | return mStartedUsers.get(userId); |
| 1092 | } |
| 1093 | |
| 1094 | boolean hasStartedUserState(int userId) { |
| 1095 | return mStartedUsers.get(userId) != null; |
| 1096 | } |
| 1097 | |
| 1098 | private void updateStartedUserArrayLocked() { |
| 1099 | int num = 0; |
| 1100 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1101 | UserState uss = mStartedUsers.valueAt(i); |
| 1102 | // This list does not include stopping users. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1103 | if (uss.state != UserState.STATE_STOPPING |
| 1104 | && uss.state != UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1105 | num++; |
| 1106 | } |
| 1107 | } |
| 1108 | mStartedUserArray = new int[num]; |
| 1109 | num = 0; |
| 1110 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1111 | UserState uss = mStartedUsers.valueAt(i); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1112 | if (uss.state != UserState.STATE_STOPPING |
| 1113 | && uss.state != UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1114 | mStartedUserArray[num] = mStartedUsers.keyAt(i); |
| 1115 | num++; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | void sendBootCompletedLocked(IIntentReceiver resultTo) { |
| 1121 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1122 | UserState uss = mStartedUsers.valueAt(i); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1123 | finishUserBoot(uss, resultTo); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * Refreshes the list of users related to the current user when either a |
| 1129 | * user switch happens or when a new related user is started in the |
| 1130 | * background. |
| 1131 | */ |
| 1132 | void updateCurrentProfileIdsLocked() { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1133 | final List<UserInfo> profiles = getUserManager().getProfiles(mCurrentUserId, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1134 | false /* enabledOnly */); |
| 1135 | int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null |
| 1136 | for (int i = 0; i < currentProfileIds.length; i++) { |
| 1137 | currentProfileIds[i] = profiles.get(i).id; |
| 1138 | } |
| 1139 | mCurrentProfileIds = currentProfileIds; |
| 1140 | |
| 1141 | synchronized (mUserProfileGroupIdsSelfLocked) { |
| 1142 | mUserProfileGroupIdsSelfLocked.clear(); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1143 | final List<UserInfo> users = getUserManager().getUsers(false); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1144 | for (int i = 0; i < users.size(); i++) { |
| 1145 | UserInfo user = users.get(i); |
| 1146 | if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 1147 | mUserProfileGroupIdsSelfLocked.put(user.id, user.profileGroupId); |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | int[] getStartedUserArrayLocked() { |
| 1154 | return mStartedUserArray; |
| 1155 | } |
| 1156 | |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 1157 | boolean isUserRunningLocked(int userId, int flags) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1158 | UserState state = getStartedUserStateLocked(userId); |
| 1159 | if (state == null) { |
| 1160 | return false; |
| 1161 | } |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 1162 | if ((flags & ActivityManager.FLAG_OR_STOPPED) != 0) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1163 | return true; |
| 1164 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1165 | |
| 1166 | final boolean unlocked; |
| 1167 | switch (state.state) { |
| 1168 | case UserState.STATE_STOPPING: |
| 1169 | case UserState.STATE_SHUTDOWN: |
| 1170 | default: |
| 1171 | return false; |
| 1172 | |
| 1173 | case UserState.STATE_BOOTING: |
| 1174 | case UserState.STATE_RUNNING_LOCKED: |
| 1175 | unlocked = false; |
| 1176 | break; |
| 1177 | |
| 1178 | case UserState.STATE_RUNNING: |
| 1179 | unlocked = true; |
| 1180 | break; |
Jeff Sharkey | 0825ab2 | 2015-12-02 13:04:49 -0700 | [diff] [blame] | 1181 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1182 | |
| 1183 | if ((flags & ActivityManager.FLAG_AND_LOCKED) != 0) { |
| 1184 | return !unlocked; |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 1185 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1186 | if ((flags & ActivityManager.FLAG_AND_UNLOCKED) != 0) { |
| 1187 | return unlocked; |
| 1188 | } |
| 1189 | |
| 1190 | // One way or another, we're running! |
| 1191 | return true; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1194 | UserInfo getCurrentUser() { |
| 1195 | if ((mService.checkCallingPermission(INTERACT_ACROSS_USERS) |
| 1196 | != PackageManager.PERMISSION_GRANTED) && ( |
| 1197 | mService.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
| 1198 | != PackageManager.PERMISSION_GRANTED)) { |
| 1199 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 1200 | + Binder.getCallingPid() |
| 1201 | + ", uid=" + Binder.getCallingUid() |
| 1202 | + " requires " + INTERACT_ACROSS_USERS; |
| 1203 | Slog.w(TAG, msg); |
| 1204 | throw new SecurityException(msg); |
| 1205 | } |
| 1206 | synchronized (mService) { |
| 1207 | return getCurrentUserLocked(); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | UserInfo getCurrentUserLocked() { |
| 1212 | int userId = mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1213 | return getUserInfo(userId); |
| 1214 | } |
| 1215 | |
| 1216 | int getCurrentOrTargetUserIdLocked() { |
| 1217 | return mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | int getCurrentUserIdLocked() { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1221 | return mCurrentUserId; |
| 1222 | } |
| 1223 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1224 | private boolean isCurrentUserLocked(int userId) { |
| 1225 | return mCurrentUserId == userId || mTargetUserId == userId; |
| 1226 | } |
| 1227 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1228 | int setTargetUserIdLocked(int targetUserId) { |
| 1229 | return mTargetUserId = targetUserId; |
| 1230 | } |
| 1231 | |
| 1232 | int[] getUsers() { |
| 1233 | UserManagerService ums = getUserManager(); |
| 1234 | return ums != null ? ums.getUserIds() : new int[] { 0 }; |
| 1235 | } |
| 1236 | |
| 1237 | UserInfo getUserInfo(int userId) { |
| 1238 | return getUserManager().getUserInfo(userId); |
| 1239 | } |
| 1240 | |
| 1241 | int[] getUserIds() { |
| 1242 | return getUserManager().getUserIds(); |
| 1243 | } |
| 1244 | |
| 1245 | boolean exists(int userId) { |
| 1246 | return getUserManager().exists(userId); |
| 1247 | } |
| 1248 | |
| 1249 | boolean hasUserRestriction(String restriction, int userId) { |
| 1250 | return getUserManager().hasUserRestriction(restriction, userId); |
| 1251 | } |
| 1252 | |
| 1253 | Set<Integer> getProfileIds(int userId) { |
| 1254 | Set<Integer> userIds = new HashSet<>(); |
| 1255 | final List<UserInfo> profiles = getUserManager().getProfiles(userId, |
| 1256 | false /* enabledOnly */); |
| 1257 | for (UserInfo user : profiles) { |
| 1258 | userIds.add(user.id); |
| 1259 | } |
| 1260 | return userIds; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | boolean isSameProfileGroup(int callingUserId, int targetUserId) { |
| 1264 | synchronized (mUserProfileGroupIdsSelfLocked) { |
| 1265 | int callingProfile = mUserProfileGroupIdsSelfLocked.get(callingUserId, |
| 1266 | UserInfo.NO_PROFILE_GROUP_ID); |
| 1267 | int targetProfile = mUserProfileGroupIdsSelfLocked.get(targetUserId, |
| 1268 | UserInfo.NO_PROFILE_GROUP_ID); |
| 1269 | return callingProfile != UserInfo.NO_PROFILE_GROUP_ID |
| 1270 | && callingProfile == targetProfile; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | boolean isCurrentProfileLocked(int userId) { |
| 1275 | return ArrayUtils.contains(mCurrentProfileIds, userId); |
| 1276 | } |
| 1277 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1278 | int[] getCurrentProfileIdsLocked() { |
| 1279 | return mCurrentProfileIds; |
| 1280 | } |
| 1281 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1282 | void dump(PrintWriter pw, boolean dumpAll) { |
| 1283 | pw.println(" mStartedUsers:"); |
| 1284 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1285 | UserState uss = mStartedUsers.valueAt(i); |
| 1286 | pw.print(" User #"); pw.print(uss.mHandle.getIdentifier()); |
| 1287 | pw.print(": "); uss.dump("", pw); |
| 1288 | } |
| 1289 | pw.print(" mStartedUserArray: ["); |
| 1290 | for (int i = 0; i < mStartedUserArray.length; i++) { |
| 1291 | if (i > 0) pw.print(", "); |
| 1292 | pw.print(mStartedUserArray[i]); |
| 1293 | } |
| 1294 | pw.println("]"); |
| 1295 | pw.print(" mUserLru: ["); |
| 1296 | for (int i = 0; i < mUserLru.size(); i++) { |
| 1297 | if (i > 0) pw.print(", "); |
| 1298 | pw.print(mUserLru.get(i)); |
| 1299 | } |
| 1300 | pw.println("]"); |
| 1301 | if (dumpAll) { |
| 1302 | pw.print(" mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray)); |
| 1303 | } |
| 1304 | synchronized (mUserProfileGroupIdsSelfLocked) { |
| 1305 | if (mUserProfileGroupIdsSelfLocked.size() > 0) { |
| 1306 | pw.println(" mUserProfileGroupIds:"); |
| 1307 | for (int i=0; i<mUserProfileGroupIdsSelfLocked.size(); i++) { |
| 1308 | pw.print(" User #"); |
| 1309 | pw.print(mUserProfileGroupIdsSelfLocked.keyAt(i)); |
| 1310 | pw.print(" -> profile #"); |
| 1311 | pw.println(mUserProfileGroupIdsSelfLocked.valueAt(i)); |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1316 | } |