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 | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 25 | import static android.os.Process.SHELL_UID; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 26 | import static android.os.Process.SYSTEM_UID; |
Tony Mak | 64fd8c0 | 2017-12-01 19:11:59 +0000 | [diff] [blame] | 27 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 28 | import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_MU; |
| 29 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 30 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 31 | import static com.android.server.am.ActivityManagerService.ALLOW_FULL_ONLY; |
| 32 | import static com.android.server.am.ActivityManagerService.ALLOW_NON_FULL; |
| 33 | import static com.android.server.am.ActivityManagerService.ALLOW_NON_FULL_IN_PROFILE; |
| 34 | import static com.android.server.am.ActivityManagerService.MY_PID; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 35 | import static com.android.server.am.UserState.STATE_BOOTING; |
| 36 | import static com.android.server.am.UserState.STATE_RUNNING_LOCKED; |
| 37 | import static com.android.server.am.UserState.STATE_RUNNING_UNLOCKED; |
| 38 | import static com.android.server.am.UserState.STATE_RUNNING_UNLOCKING; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 39 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 40 | import android.annotation.NonNull; |
Tony Mak | 64fd8c0 | 2017-12-01 19:11:59 +0000 | [diff] [blame] | 41 | import android.annotation.Nullable; |
Tony Mak | 8c536f9 | 2016-03-21 12:20:41 +0000 | [diff] [blame] | 42 | import android.annotation.UserIdInt; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 43 | import android.app.ActivityManager; |
Fyodor Kupolov | 4ba91b9 | 2017-01-20 18:12:35 -0800 | [diff] [blame] | 44 | import android.app.AppGlobals; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 45 | import android.app.AppOpsManager; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 46 | import android.app.Dialog; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 47 | import android.app.IStopUserCallback; |
| 48 | import android.app.IUserSwitchObserver; |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 49 | import android.app.KeyguardManager; |
Dianne Hackborn | ced5439 | 2018-02-26 13:07:42 -0800 | [diff] [blame] | 50 | import android.app.usage.UsageEvents; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 51 | import android.content.Context; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 52 | import android.content.IIntentReceiver; |
| 53 | import android.content.Intent; |
Fyodor Kupolov | 4ba91b9 | 2017-01-20 18:12:35 -0800 | [diff] [blame] | 54 | import android.content.pm.IPackageManager; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 55 | import android.content.pm.PackageManager; |
| 56 | import android.content.pm.UserInfo; |
| 57 | import android.os.BatteryStats; |
| 58 | import android.os.Binder; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 59 | import android.os.Build; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 60 | import android.os.Bundle; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 61 | import android.os.Debug; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 62 | import android.os.Handler; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 63 | import android.os.IBinder; |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 64 | import android.os.IProgressListener; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 65 | import android.os.IRemoteCallback; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 66 | import android.os.IUserManager; |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 67 | import android.os.Message; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 68 | import android.os.Process; |
| 69 | import android.os.RemoteCallbackList; |
| 70 | import android.os.RemoteException; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 71 | import android.os.ServiceManager; |
James Hawkins | 899608a | 2016-05-27 11:15:06 -0700 | [diff] [blame] | 72 | import android.os.SystemClock; |
Fyodor Kupolov | 0d77c6d | 2017-10-11 17:53:23 -0700 | [diff] [blame] | 73 | import android.os.Trace; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 74 | import android.os.UserHandle; |
| 75 | import android.os.UserManager; |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 76 | import android.os.UserManagerInternal; |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 77 | import android.os.storage.IStorageManager; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 78 | import android.os.storage.StorageManager; |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 79 | import android.text.format.DateUtils; |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 80 | import android.util.ArraySet; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 81 | import android.util.IntArray; |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 82 | import android.util.Log; |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 83 | import android.util.Pair; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 84 | import android.util.Slog; |
| 85 | import android.util.SparseArray; |
| 86 | import android.util.SparseIntArray; |
Fyodor Kupolov | 0d77c6d | 2017-10-11 17:53:23 -0700 | [diff] [blame] | 87 | import android.util.TimingsTraceLog; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 88 | import android.util.proto.ProtoOutputStream; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 89 | |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 90 | import android.view.Window; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 91 | import com.android.internal.R; |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 92 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 93 | import com.android.internal.annotations.VisibleForTesting; |
James Hawkins | 899608a | 2016-05-27 11:15:06 -0700 | [diff] [blame] | 94 | import com.android.internal.logging.MetricsLogger; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 95 | import com.android.internal.util.ArrayUtils; |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 96 | import com.android.internal.util.Preconditions; |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 97 | import com.android.internal.widget.LockPatternUtils; |
Fyodor Kupolov | 4c72df0 | 2017-11-14 11:43:40 -0800 | [diff] [blame] | 98 | import com.android.server.FgThread; |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 99 | import com.android.server.LocalServices; |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 100 | import com.android.server.SystemServiceManager; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 101 | import com.android.server.pm.UserManagerService; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 102 | import com.android.server.wm.WindowManagerService; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 103 | |
| 104 | import java.io.PrintWriter; |
| 105 | import java.util.ArrayList; |
| 106 | import java.util.Arrays; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 107 | import java.util.HashSet; |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 108 | import java.util.Iterator; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 109 | import java.util.List; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 110 | import java.util.Objects; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 111 | import java.util.Set; |
Fyodor Kupolov | 3864183 | 2016-06-28 17:37:09 -0700 | [diff] [blame] | 112 | import java.util.concurrent.atomic.AtomicInteger; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Helper class for {@link ActivityManagerService} responsible for multi-user functionality. |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 116 | * |
| 117 | * <p>This class use {@link #mLock} to synchronize access to internal state. Methods that require |
| 118 | * {@link #mLock} to be held should have "LU" suffix in the name. |
| 119 | * |
| 120 | * <p><strong>Important:</strong> Synchronized code, i.e. one executed inside a synchronized(mLock) |
| 121 | * block or inside LU method, should only access internal state of this class or make calls to |
| 122 | * other LU methods. Non-LU method calls or calls to external classes are discouraged as they |
| 123 | * may cause lock inversion. |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 124 | */ |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 125 | class UserController implements Handler.Callback { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 126 | private static final String TAG = TAG_WITH_CLASS_NAME ? "UserController" : TAG_AM; |
Jeff Sharkey | fd24108 | 2016-04-19 15:58:24 -0600 | [diff] [blame] | 127 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 128 | // Amount of time we wait for observers to handle a user switch before |
| 129 | // giving up on them and unfreezing the screen. |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 130 | static final int USER_SWITCH_TIMEOUT_MS = 3 * 1000; |
| 131 | |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 132 | // ActivityManager thread message constants |
| 133 | static final int REPORT_USER_SWITCH_MSG = 10; |
| 134 | static final int CONTINUE_USER_SWITCH_MSG = 20; |
| 135 | static final int USER_SWITCH_TIMEOUT_MSG = 30; |
| 136 | static final int START_PROFILES_MSG = 40; |
| 137 | static final int SYSTEM_USER_START_MSG = 50; |
| 138 | static final int SYSTEM_USER_CURRENT_MSG = 60; |
| 139 | static final int FOREGROUND_PROFILE_CHANGED_MSG = 70; |
| 140 | static final int REPORT_USER_SWITCH_COMPLETE_MSG = 80; |
| 141 | static final int USER_SWITCH_CALLBACKS_TIMEOUT_MSG = 90; |
| 142 | static final int SYSTEM_USER_UNLOCK_MSG = 100; |
| 143 | static final int REPORT_LOCKED_BOOT_COMPLETE_MSG = 110; |
| 144 | static final int START_USER_SWITCH_FG_MSG = 120; |
| 145 | |
| 146 | // UI thread message constants |
| 147 | static final int START_USER_SWITCH_UI_MSG = 1000; |
| 148 | |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 149 | // If a callback wasn't called within USER_SWITCH_CALLBACKS_TIMEOUT_MS after |
| 150 | // USER_SWITCH_TIMEOUT_MS, an error is reported. Usually it indicates a problem in the observer |
| 151 | // when it never calls back. |
| 152 | private static final int USER_SWITCH_CALLBACKS_TIMEOUT_MS = 5 * 1000; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 153 | |
Fyodor Kupolov | 53cba30 | 2017-10-25 18:11:48 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Maximum number of users we allow to be running at a time, including system user. |
| 156 | * |
| 157 | * <p>This parameter only affects how many background users will be stopped when switching to a |
| 158 | * new user. It has no impact on {@link #startUser(int, boolean)} behavior. |
| 159 | * |
| 160 | * <p>Note: Current and system user (and their related profiles) are never stopped when |
| 161 | * switching users. Due to that, the actual number of running users can exceed mMaxRunningUsers |
| 162 | */ |
| 163 | int mMaxRunningUsers; |
| 164 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 165 | // Lock for internal state. |
| 166 | private final Object mLock = new Object(); |
| 167 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 168 | private final Injector mInjector; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 169 | private final Handler mHandler; |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 170 | private final Handler mUiHandler; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 171 | |
Fyodor Kupolov | dcb26bb | 2017-05-09 17:06:52 -0700 | [diff] [blame] | 172 | // Holds the current foreground user's id. Use mLock when updating |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 173 | @GuardedBy("mLock") |
Fyodor Kupolov | dcb26bb | 2017-05-09 17:06:52 -0700 | [diff] [blame] | 174 | private volatile int mCurrentUserId = UserHandle.USER_SYSTEM; |
| 175 | // Holds the target user's id during a user switch. The value of mCurrentUserId will be updated |
| 176 | // once target user goes into the foreground. Use mLock when updating |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 177 | @GuardedBy("mLock") |
Fyodor Kupolov | dcb26bb | 2017-05-09 17:06:52 -0700 | [diff] [blame] | 178 | private volatile int mTargetUserId = UserHandle.USER_NULL; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * Which users have been started, so are allowed to run code. |
| 182 | */ |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 183 | @GuardedBy("mLock") |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 184 | private final SparseArray<UserState> mStartedUsers = new SparseArray<>(); |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 185 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 186 | /** |
| 187 | * LRU list of history of current users. Most recently current is at the end. |
| 188 | */ |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 189 | @GuardedBy("mLock") |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 190 | private final ArrayList<Integer> mUserLru = new ArrayList<>(); |
| 191 | |
| 192 | /** |
| 193 | * Constant array of the users that are currently started. |
| 194 | */ |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 195 | @GuardedBy("mLock") |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 196 | private int[] mStartedUserArray = new int[] { 0 }; |
| 197 | |
| 198 | // If there are multiple profiles for the current user, their ids are here |
| 199 | // Currently only the primary user can have managed profiles |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 200 | @GuardedBy("mLock") |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 201 | private int[] mCurrentProfileIds = new int[] {}; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Mapping from each known user ID to the profile group ID it is associated with. |
| 205 | */ |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 206 | @GuardedBy("mLock") |
| 207 | private final SparseIntArray mUserProfileGroupIds = new SparseIntArray(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Registered observers of the user switching mechanics. |
| 211 | */ |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 212 | private final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 213 | = new RemoteCallbackList<>(); |
| 214 | |
Evan Rosky | 3e0c25b | 2016-08-09 12:43:33 -0700 | [diff] [blame] | 215 | boolean mUserSwitchUiEnabled = true; |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 216 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 217 | /** |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 218 | * Currently active user switch callbacks. |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 219 | */ |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 220 | @GuardedBy("mLock") |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 221 | private volatile ArraySet<String> mCurWaitingUserSwitchCallbacks; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 222 | |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 223 | /** |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 224 | * Messages for for switching from {@link android.os.UserHandle#SYSTEM}. |
| 225 | */ |
| 226 | @GuardedBy("mLock") |
| 227 | private String mSwitchingFromSystemUserMessage; |
| 228 | |
| 229 | /** |
| 230 | * Messages for for switching to {@link android.os.UserHandle#SYSTEM}. |
| 231 | */ |
| 232 | @GuardedBy("mLock") |
| 233 | private String mSwitchingToSystemUserMessage; |
| 234 | |
| 235 | /** |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 236 | * Callbacks that are still active after {@link #USER_SWITCH_TIMEOUT_MS} |
| 237 | */ |
| 238 | @GuardedBy("mLock") |
| 239 | private ArraySet<String> mTimeoutUserSwitchCallbacks; |
| 240 | |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 241 | private final LockPatternUtils mLockPatternUtils; |
| 242 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 243 | UserController(ActivityManagerService service) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 244 | this(new Injector(service)); |
| 245 | } |
| 246 | |
| 247 | @VisibleForTesting |
| 248 | UserController(Injector injector) { |
| 249 | mInjector = injector; |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 250 | mHandler = mInjector.getHandler(this); |
| 251 | mUiHandler = mInjector.getUiHandler(this); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 252 | // User 0 is the first and only user that runs at boot. |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 253 | final UserState uss = new UserState(UserHandle.SYSTEM); |
Fyodor Kupolov | 0d77c6d | 2017-10-11 17:53:23 -0700 | [diff] [blame] | 254 | uss.mUnlockProgress.addListener(new UserProgressListener()); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 255 | mStartedUsers.put(UserHandle.USER_SYSTEM, uss); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 256 | mUserLru.add(UserHandle.USER_SYSTEM); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 257 | mLockPatternUtils = mInjector.getLockPatternUtils(); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 258 | updateStartedUserArrayLU(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void finishUserSwitch(UserState uss) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 262 | finishUserBoot(uss); |
| 263 | startProfiles(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 264 | synchronized (mLock) { |
Fyodor Kupolov | 53cba30 | 2017-10-25 18:11:48 -0700 | [diff] [blame] | 265 | stopRunningUsersLU(mMaxRunningUsers); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 268 | |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 269 | List<Integer> getRunningUsersLU() { |
| 270 | ArrayList<Integer> runningUsers = new ArrayList<>(); |
| 271 | for (Integer userId : mUserLru) { |
| 272 | UserState uss = mStartedUsers.get(userId); |
| 273 | if (uss == null) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 274 | // Shouldn't happen, but be sane if it does. |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 275 | continue; |
| 276 | } |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 277 | if (uss.state == UserState.STATE_STOPPING |
| 278 | || uss.state == UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 279 | // This user is already stopping, doesn't count. |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 280 | continue; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 281 | } |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 282 | if (userId == UserHandle.USER_SYSTEM) { |
| 283 | // We only count system user as running when it is not a pure system user. |
| 284 | if (UserInfo.isSystemOnly(userId)) { |
| 285 | continue; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 286 | } |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 287 | } |
| 288 | runningUsers.add(userId); |
| 289 | } |
| 290 | return runningUsers; |
| 291 | } |
| 292 | |
| 293 | void stopRunningUsersLU(int maxRunningUsers) { |
| 294 | List<Integer> currentlyRunning = getRunningUsersLU(); |
| 295 | Iterator<Integer> iterator = currentlyRunning.iterator(); |
| 296 | while (currentlyRunning.size() > maxRunningUsers && iterator.hasNext()) { |
| 297 | Integer userId = iterator.next(); |
| 298 | if (userId == UserHandle.USER_SYSTEM || userId == mCurrentUserId) { |
| 299 | // Owner/System user and current user can't be stopped |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 300 | continue; |
| 301 | } |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 302 | if (stopUsersLU(userId, false, null) == USER_OP_SUCCESS) { |
| 303 | iterator.remove(); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 304 | } |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Returns if more users can be started without stopping currently running users. |
| 310 | */ |
| 311 | boolean canStartMoreUsers() { |
| 312 | synchronized (mLock) { |
| 313 | return getRunningUsersLU().size() < mMaxRunningUsers; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Fyodor Kupolov | 2e6acce | 2016-01-28 15:26:52 -0800 | [diff] [blame] | 317 | private void finishUserBoot(UserState uss) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 318 | finishUserBoot(uss, null); |
| 319 | } |
| 320 | |
Fyodor Kupolov | 2e6acce | 2016-01-28 15:26:52 -0800 | [diff] [blame] | 321 | private void finishUserBoot(UserState uss, IIntentReceiver resultTo) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 322 | final int userId = uss.mHandle.getIdentifier(); |
Suprabh Shukla | cc30b0e7 | 2016-05-20 14:41:22 -0700 | [diff] [blame] | 323 | |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 324 | Slog.d(TAG, "Finishing user boot " + userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 325 | synchronized (mLock) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 326 | // Bail if we ended up with a stale user |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 327 | if (mStartedUsers.get(userId) != uss) { |
| 328 | return; |
| 329 | } |
| 330 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 331 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 332 | // We always walk through all the user lifecycle states to send |
| 333 | // consistent developer events. We step into RUNNING_LOCKED here, |
| 334 | // but we might immediately step into RUNNING below if the user |
| 335 | // storage is already unlocked. |
| 336 | if (uss.setState(STATE_BOOTING, STATE_RUNNING_LOCKED)) { |
| 337 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
| 338 | // Do not report secondary users, runtime restarts or first boot/upgrade |
| 339 | if (userId == UserHandle.USER_SYSTEM |
| 340 | && !mInjector.isRuntimeRestarted() && !mInjector.isFirstBootOrUpgrade()) { |
| 341 | int uptimeSeconds = (int)(SystemClock.elapsedRealtime() / 1000); |
| 342 | MetricsLogger.histogram(mInjector.getContext(), |
| 343 | "framework_locked_boot_completed", uptimeSeconds); |
| 344 | final int MAX_UPTIME_SECONDS = 120; |
| 345 | if (uptimeSeconds > MAX_UPTIME_SECONDS) { |
| 346 | Slog.wtf("SystemServerTiming", |
| 347 | "finishUserBoot took too long. uptimeSeconds=" + uptimeSeconds); |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 348 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 351 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_LOCKED_BOOT_COMPLETE_MSG, |
| 352 | userId, 0)); |
| 353 | Intent intent = new Intent(Intent.ACTION_LOCKED_BOOT_COMPLETED, null); |
| 354 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 355 | intent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT |
| 356 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 357 | mInjector.broadcastIntent(intent, null, resultTo, 0, null, null, |
| 358 | new String[]{android.Manifest.permission.RECEIVE_BOOT_COMPLETED}, |
| 359 | AppOpsManager.OP_NONE, null, true, false, MY_PID, SYSTEM_UID, userId); |
| 360 | } |
| 361 | |
| 362 | // We need to delay unlocking managed profiles until the parent user |
| 363 | // is also unlocked. |
| 364 | if (mInjector.getUserManager().isManagedProfile(userId)) { |
| 365 | final UserInfo parent = mInjector.getUserManager().getProfileParent(userId); |
| 366 | if (parent != null |
| 367 | && isUserRunning(parent.id, ActivityManager.FLAG_AND_UNLOCKED)) { |
| 368 | Slog.d(TAG, "User " + userId + " (parent " + parent.id |
| 369 | + "): attempting unlock because parent is unlocked"); |
Jeff Sharkey | 5dab713 | 2016-04-07 01:20:58 -0600 | [diff] [blame] | 370 | maybeUnlockUser(userId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 371 | } else { |
| 372 | String parentId = (parent == null) ? "<null>" : String.valueOf(parent.id); |
| 373 | Slog.d(TAG, "User " + userId + " (parent " + parentId |
| 374 | + "): delaying unlock because parent is locked"); |
Jeff Sharkey | 5dab713 | 2016-04-07 01:20:58 -0600 | [diff] [blame] | 375 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 376 | } else { |
| 377 | maybeUnlockUser(userId); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 382 | * Step from {@link UserState#STATE_RUNNING_LOCKED} to |
| 383 | * {@link UserState#STATE_RUNNING_UNLOCKING}. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 384 | */ |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 385 | private void finishUserUnlocking(final UserState uss) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 386 | final int userId = uss.mHandle.getIdentifier(); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 387 | // Only keep marching forward if user is actually unlocked |
| 388 | if (!StorageManager.isUserKeyUnlocked(userId)) return; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 389 | synchronized (mLock) { |
Fyodor Kupolov | 4c72df0 | 2017-11-14 11:43:40 -0800 | [diff] [blame] | 390 | // Do not proceed if unexpected state or a stale user |
| 391 | if (mStartedUsers.get(userId) != uss || uss.state != STATE_RUNNING_LOCKED) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 392 | return; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 393 | } |
| 394 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 395 | uss.mUnlockProgress.start(); |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 396 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 397 | // Prepare app storage before we go any further |
| 398 | uss.mUnlockProgress.setProgress(5, |
| 399 | mInjector.getContext().getString(R.string.android_start_title)); |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 400 | |
Fyodor Kupolov | 4c72df0 | 2017-11-14 11:43:40 -0800 | [diff] [blame] | 401 | // Call onBeforeUnlockUser on a worker thread that allows disk I/O |
| 402 | FgThread.getHandler().post(() -> { |
| 403 | mInjector.getUserManager().onBeforeUnlockUser(userId); |
| 404 | synchronized (mLock) { |
| 405 | // Do not proceed if unexpected state |
| 406 | if (!uss.setState(STATE_RUNNING_LOCKED, STATE_RUNNING_UNLOCKING)) { |
| 407 | return; |
| 408 | } |
| 409 | } |
| 410 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
| 411 | |
| 412 | uss.mUnlockProgress.setProgress(20); |
| 413 | |
| 414 | // Dispatch unlocked to system services; when fully dispatched, |
| 415 | // that calls through to the next "unlocked" phase |
| 416 | mHandler.obtainMessage(SYSTEM_USER_UNLOCK_MSG, userId, 0, uss) |
| 417 | .sendToTarget(); |
| 418 | }); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Step from {@link UserState#STATE_RUNNING_UNLOCKING} to |
| 423 | * {@link UserState#STATE_RUNNING_UNLOCKED}. |
| 424 | */ |
| 425 | void finishUserUnlocked(final UserState uss) { |
| 426 | final int userId = uss.mHandle.getIdentifier(); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 427 | // Only keep marching forward if user is actually unlocked |
| 428 | if (!StorageManager.isUserKeyUnlocked(userId)) return; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 429 | synchronized (mLock) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 430 | // Bail if we ended up with a stale user |
| 431 | if (mStartedUsers.get(uss.mHandle.getIdentifier()) != uss) return; |
| 432 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 433 | // Do not proceed if unexpected state |
| 434 | if (!uss.setState(STATE_RUNNING_UNLOCKING, STATE_RUNNING_UNLOCKED)) { |
| 435 | return; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 436 | } |
| 437 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 438 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
| 439 | uss.mUnlockProgress.finish(); |
Jeff Sharkey | 26b0f35 | 2018-02-19 16:30:17 -0700 | [diff] [blame] | 440 | |
| 441 | // Get unaware persistent apps running and start any unaware providers |
| 442 | // in already-running apps that are partially aware |
| 443 | if (userId == UserHandle.USER_SYSTEM) { |
| 444 | mInjector.startPersistentApps(PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
| 445 | } |
| 446 | mInjector.installEncryptionUnawareProviders(userId); |
| 447 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 448 | // Dispatch unlocked to external apps |
| 449 | final Intent unlockedIntent = new Intent(Intent.ACTION_USER_UNLOCKED); |
| 450 | unlockedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 451 | unlockedIntent.addFlags( |
| 452 | Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND); |
| 453 | mInjector.broadcastIntent(unlockedIntent, null, null, 0, null, |
| 454 | null, null, AppOpsManager.OP_NONE, null, false, false, MY_PID, SYSTEM_UID, |
| 455 | userId); |
| 456 | |
| 457 | if (getUserInfo(userId).isManagedProfile()) { |
| 458 | UserInfo parent = mInjector.getUserManager().getProfileParent(userId); |
| 459 | if (parent != null) { |
| 460 | final Intent profileUnlockedIntent = new Intent( |
| 461 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED); |
| 462 | profileUnlockedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(userId)); |
| 463 | profileUnlockedIntent.addFlags( |
| 464 | Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 465 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 466 | mInjector.broadcastIntent(profileUnlockedIntent, |
| 467 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 468 | null, false, false, MY_PID, SYSTEM_UID, |
| 469 | parent.id); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // Send PRE_BOOT broadcasts if user fingerprint changed; we |
| 474 | // purposefully block sending BOOT_COMPLETED until after all |
| 475 | // PRE_BOOT receivers are finished to avoid ANR'ing apps |
| 476 | final UserInfo info = getUserInfo(userId); |
| 477 | if (!Objects.equals(info.lastLoggedInFingerprint, Build.FINGERPRINT)) { |
| 478 | // Suppress double notifications for managed profiles that |
| 479 | // were unlocked automatically as part of their parent user |
| 480 | // being unlocked. |
| 481 | final boolean quiet; |
| 482 | if (info.isManagedProfile()) { |
| 483 | quiet = !uss.tokenProvided |
| 484 | || !mLockPatternUtils.isSeparateProfileChallengeEnabled(userId); |
| 485 | } else { |
| 486 | quiet = false; |
| 487 | } |
| 488 | mInjector.sendPreBootBroadcast(userId, quiet, |
| 489 | () -> finishUserUnlockedCompleted(uss)); |
| 490 | } else { |
| 491 | finishUserUnlockedCompleted(uss); |
| 492 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 493 | } |
| 494 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 495 | private void finishUserUnlockedCompleted(UserState uss) { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 496 | final int userId = uss.mHandle.getIdentifier(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 497 | synchronized (mLock) { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 498 | // Bail if we ended up with a stale user |
| 499 | if (mStartedUsers.get(uss.mHandle.getIdentifier()) != uss) return; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 500 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 501 | UserInfo userInfo = getUserInfo(userId); |
| 502 | if (userInfo == null) { |
| 503 | return; |
| 504 | } |
| 505 | // Only keep marching forward if user is actually unlocked |
| 506 | if (!StorageManager.isUserKeyUnlocked(userId)) return; |
| 507 | |
| 508 | // Remember that we logged in |
| 509 | mInjector.getUserManager().onUserLoggedIn(userId); |
| 510 | |
| 511 | if (!userInfo.isInitialized()) { |
| 512 | if (userId != UserHandle.USER_SYSTEM) { |
| 513 | Slog.d(TAG, "Initializing user #" + userId); |
| 514 | Intent intent = new Intent(Intent.ACTION_USER_INITIALIZE); |
| 515 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND |
| 516 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 517 | mInjector.broadcastIntent(intent, null, |
| 518 | new IIntentReceiver.Stub() { |
| 519 | @Override |
| 520 | public void performReceive(Intent intent, int resultCode, |
| 521 | String data, Bundle extras, boolean ordered, |
| 522 | boolean sticky, int sendingUser) { |
| 523 | // Note: performReceive is called with mService lock held |
| 524 | mInjector.getUserManager().makeInitialized(userInfo.id); |
| 525 | } |
| 526 | }, 0, null, null, null, AppOpsManager.OP_NONE, |
| 527 | null, true, false, MY_PID, SYSTEM_UID, userId); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | Slog.i(TAG, "Sending BOOT_COMPLETE user #" + userId); |
| 532 | // Do not report secondary users, runtime restarts or first boot/upgrade |
| 533 | if (userId == UserHandle.USER_SYSTEM |
| 534 | && !mInjector.isRuntimeRestarted() && !mInjector.isFirstBootOrUpgrade()) { |
| 535 | int uptimeSeconds = (int) (SystemClock.elapsedRealtime() / 1000); |
| 536 | MetricsLogger.histogram(mInjector.getContext(), "framework_boot_completed", |
| 537 | uptimeSeconds); |
| 538 | } |
| 539 | final Intent bootIntent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 540 | bootIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 541 | bootIntent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT |
| 542 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 543 | mInjector.broadcastIntent(bootIntent, null, new IIntentReceiver.Stub() { |
| 544 | @Override |
| 545 | public void performReceive(Intent intent, int resultCode, String data, |
| 546 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) |
| 547 | throws RemoteException { |
| 548 | Slog.i(UserController.TAG, "Finished processing BOOT_COMPLETED for u" + userId); |
| 549 | } |
| 550 | }, 0, null, null, |
| 551 | new String[]{android.Manifest.permission.RECEIVE_BOOT_COMPLETED}, |
| 552 | AppOpsManager.OP_NONE, null, true, false, MY_PID, SYSTEM_UID, userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 555 | int restartUser(final int userId, final boolean foreground) { |
| 556 | return stopUser(userId, /* force */ true, new IStopUserCallback.Stub() { |
| 557 | @Override |
| 558 | public void userStopped(final int userId) { |
| 559 | // Post to the same handler that this callback is called from to ensure the user |
| 560 | // cleanup is complete before restarting. |
| 561 | mHandler.post(() -> startUser(userId, foreground)); |
| 562 | } |
| 563 | @Override |
| 564 | public void userStopAborted(final int userId) {} |
| 565 | }); |
| 566 | } |
| 567 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 568 | int stopUser(final int userId, final boolean force, final IStopUserCallback callback) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 569 | if (mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 570 | != PackageManager.PERMISSION_GRANTED) { |
| 571 | String msg = "Permission Denial: switchUser() from pid=" |
| 572 | + Binder.getCallingPid() |
| 573 | + ", uid=" + Binder.getCallingUid() |
| 574 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 575 | Slog.w(TAG, msg); |
| 576 | throw new SecurityException(msg); |
| 577 | } |
| 578 | if (userId < 0 || userId == UserHandle.USER_SYSTEM) { |
| 579 | throw new IllegalArgumentException("Can't stop system user " + userId); |
| 580 | } |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 581 | enforceShellRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 582 | synchronized (mLock) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 583 | return stopUsersLU(userId, force, callback); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 587 | /** |
| 588 | * Stops the user along with its related users. The method calls |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 589 | * {@link #getUsersToStopLU(int)} to determine the list of users that should be stopped. |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 590 | */ |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 591 | private int stopUsersLU(final int userId, boolean force, final IStopUserCallback callback) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 592 | if (userId == UserHandle.USER_SYSTEM) { |
| 593 | return USER_OP_ERROR_IS_SYSTEM; |
| 594 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 595 | if (isCurrentUserLU(userId)) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 596 | return USER_OP_IS_CURRENT; |
| 597 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 598 | int[] usersToStop = getUsersToStopLU(userId); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 599 | // If one of related users is system or current, no related users should be stopped |
| 600 | for (int i = 0; i < usersToStop.length; i++) { |
| 601 | int relatedUserId = usersToStop[i]; |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 602 | if ((UserHandle.USER_SYSTEM == relatedUserId) || isCurrentUserLU(relatedUserId)) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 603 | if (DEBUG_MU) Slog.i(TAG, "stopUsersLocked cannot stop related user " |
| 604 | + relatedUserId); |
| 605 | // We still need to stop the requested user if it's a force stop. |
| 606 | if (force) { |
Fyodor Kupolov | 7b4a8a4 | 2016-01-04 12:47:22 -0800 | [diff] [blame] | 607 | Slog.i(TAG, |
| 608 | "Force stop user " + userId + ". Related users will not be stopped"); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 609 | stopSingleUserLU(userId, callback); |
Fyodor Kupolov | 7b4a8a4 | 2016-01-04 12:47:22 -0800 | [diff] [blame] | 610 | return USER_OP_SUCCESS; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 611 | } |
| 612 | return USER_OP_ERROR_RELATED_USERS_CANNOT_STOP; |
| 613 | } |
| 614 | } |
| 615 | if (DEBUG_MU) Slog.i(TAG, "stopUsersLocked usersToStop=" + Arrays.toString(usersToStop)); |
| 616 | for (int userIdToStop : usersToStop) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 617 | stopSingleUserLU(userIdToStop, userIdToStop == userId ? callback : null); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 618 | } |
| 619 | return USER_OP_SUCCESS; |
| 620 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 621 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 622 | private void stopSingleUserLU(final int userId, final IStopUserCallback callback) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 623 | if (DEBUG_MU) Slog.i(TAG, "stopSingleUserLocked userId=" + userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 624 | final UserState uss = mStartedUsers.get(userId); |
| 625 | if (uss == null) { |
| 626 | // User is not started, nothing to do... but we do need to |
| 627 | // callback if requested. |
| 628 | if (callback != null) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 629 | mHandler.post(() -> { |
| 630 | try { |
| 631 | callback.userStopped(userId); |
| 632 | } catch (RemoteException e) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 633 | } |
| 634 | }); |
| 635 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 636 | return; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | if (callback != null) { |
| 640 | uss.mStopCallbacks.add(callback); |
| 641 | } |
| 642 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 643 | if (uss.state != UserState.STATE_STOPPING |
| 644 | && uss.state != UserState.STATE_SHUTDOWN) { |
| 645 | uss.setState(UserState.STATE_STOPPING); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 646 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 647 | updateStartedUserArrayLU(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 648 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 649 | // Post to handler to obtain amLock |
| 650 | mHandler.post(() -> { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 651 | // We are going to broadcast ACTION_USER_STOPPING and then |
| 652 | // once that is done send a final ACTION_SHUTDOWN and then |
| 653 | // stop the user. |
| 654 | final Intent stoppingIntent = new Intent(Intent.ACTION_USER_STOPPING); |
| 655 | stoppingIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 656 | stoppingIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 657 | stoppingIntent.putExtra(Intent.EXTRA_SHUTDOWN_USERSPACE_ONLY, true); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 658 | // This is the result receiver for the initial stopping broadcast. |
| 659 | final IIntentReceiver stoppingReceiver = new IIntentReceiver.Stub() { |
| 660 | @Override |
| 661 | public void performReceive(Intent intent, int resultCode, String data, |
| 662 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 663 | mHandler.post(() -> finishUserStopping(userId, uss)); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 664 | } |
| 665 | }; |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 666 | |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 667 | // Clear broadcast queue for the user to avoid delivering stale broadcasts |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 668 | mInjector.clearBroadcastQueueForUser(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 669 | // Kick things off. |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 670 | mInjector.broadcastIntent(stoppingIntent, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 671 | null, stoppingReceiver, 0, null, null, |
| 672 | new String[]{INTERACT_ACROSS_USERS}, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 673 | null, true, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 674 | }); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 675 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 678 | void finishUserStopping(final int userId, final UserState uss) { |
| 679 | // On to the next. |
| 680 | final Intent shutdownIntent = new Intent(Intent.ACTION_SHUTDOWN); |
| 681 | // This is the result receiver for the final shutdown broadcast. |
| 682 | final IIntentReceiver shutdownReceiver = new IIntentReceiver.Stub() { |
| 683 | @Override |
| 684 | public void performReceive(Intent intent, int resultCode, String data, |
| 685 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 686 | mHandler.post(new Runnable() { |
| 687 | @Override |
| 688 | public void run() { |
| 689 | finishUserStopped(uss); |
| 690 | } |
| 691 | }); |
| 692 | } |
| 693 | }; |
| 694 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 695 | synchronized (mLock) { |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 696 | if (uss.state != UserState.STATE_STOPPING) { |
| 697 | // Whoops, we are being started back up. Abort, abort! |
| 698 | return; |
| 699 | } |
| 700 | uss.setState(UserState.STATE_SHUTDOWN); |
| 701 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 702 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 703 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 704 | mInjector.batteryStatsServiceNoteEvent( |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 705 | BatteryStats.HistoryItem.EVENT_USER_RUNNING_FINISH, |
| 706 | Integer.toString(userId), userId); |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 707 | mInjector.getSystemServiceManager().stopUser(userId); |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 708 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 709 | mInjector.broadcastIntent(shutdownIntent, |
| 710 | null, shutdownReceiver, 0, null, null, null, |
| 711 | AppOpsManager.OP_NONE, |
| 712 | null, true, false, MY_PID, SYSTEM_UID, userId); |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void finishUserStopped(UserState uss) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 716 | final int userId = uss.mHandle.getIdentifier(); |
| 717 | boolean stopped; |
| 718 | ArrayList<IStopUserCallback> callbacks; |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 719 | boolean forceStopUser = false; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 720 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 721 | callbacks = new ArrayList<>(uss.mStopCallbacks); |
| 722 | if (mStartedUsers.get(userId) != uss) { |
| 723 | stopped = false; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 724 | } else if (uss.state != UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 725 | stopped = false; |
| 726 | } else { |
| 727 | stopped = true; |
| 728 | // User can no longer run. |
| 729 | mStartedUsers.remove(userId); |
| 730 | mUserLru.remove(Integer.valueOf(userId)); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 731 | updateStartedUserArrayLU(); |
| 732 | forceStopUser = true; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 735 | if (forceStopUser) { |
| 736 | mInjector.getUserManagerInternal().removeUserState(userId); |
| 737 | mInjector.activityManagerOnUserStopped(userId); |
| 738 | // Clean up all state and processes associated with the user. |
| 739 | // Kill all the processes for the user. |
| 740 | forceStopUser(userId, "finish user"); |
| 741 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 742 | |
| 743 | for (int i = 0; i < callbacks.size(); i++) { |
| 744 | try { |
| 745 | if (stopped) callbacks.get(i).userStopped(userId); |
| 746 | else callbacks.get(i).userStopAborted(userId); |
| 747 | } catch (RemoteException e) { |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | if (stopped) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 752 | mInjector.systemServiceManagerCleanupUser(userId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 753 | mInjector.stackSupervisorRemoveUser(userId); |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 754 | // Remove the user if it is ephemeral. |
| 755 | if (getUserInfo(userId).isEphemeral()) { |
Alex Chau | 1df8981 | 2018-02-06 14:41:47 +0000 | [diff] [blame] | 756 | mInjector.getUserManager().removeUserEvenWhenDisallowed(userId); |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 757 | } |
Pavel Grafov | 0502a4c | 2017-07-27 17:27:35 +0100 | [diff] [blame] | 758 | // Evict the user's credential encryption key. |
| 759 | try { |
| 760 | getStorageManager().lockUserKey(userId); |
| 761 | } catch (RemoteException re) { |
| 762 | throw re.rethrowAsRuntimeException(); |
| 763 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 767 | /** |
| 768 | * Determines the list of users that should be stopped together with the specified |
| 769 | * {@code userId}. The returned list includes {@code userId}. |
| 770 | */ |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 771 | private @NonNull int[] getUsersToStopLU(int userId) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 772 | int startedUsersSize = mStartedUsers.size(); |
| 773 | IntArray userIds = new IntArray(); |
| 774 | userIds.add(userId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 775 | int userGroupId = mUserProfileGroupIds.get(userId, UserInfo.NO_PROFILE_GROUP_ID); |
| 776 | for (int i = 0; i < startedUsersSize; i++) { |
| 777 | UserState uss = mStartedUsers.valueAt(i); |
| 778 | int startedUserId = uss.mHandle.getIdentifier(); |
| 779 | // Skip unrelated users (profileGroupId mismatch) |
| 780 | int startedUserGroupId = mUserProfileGroupIds.get(startedUserId, |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 781 | UserInfo.NO_PROFILE_GROUP_ID); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 782 | boolean sameGroup = (userGroupId != UserInfo.NO_PROFILE_GROUP_ID) |
| 783 | && (userGroupId == startedUserGroupId); |
| 784 | // userId has already been added |
| 785 | boolean sameUserId = startedUserId == userId; |
| 786 | if (!sameGroup || sameUserId) { |
| 787 | continue; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 788 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 789 | userIds.add(startedUserId); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 790 | } |
| 791 | return userIds.toArray(); |
| 792 | } |
| 793 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 794 | private void forceStopUser(int userId, String reason) { |
| 795 | mInjector.activityManagerForceStopPackage(userId, reason); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 796 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
| 797 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 798 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 799 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 800 | mInjector.broadcastIntent(intent, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 801 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 802 | null, false, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 805 | /** |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 806 | * Stops the guest or ephemeral user if it has gone to the background. |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 807 | */ |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 808 | private void stopGuestOrEphemeralUserIfBackground(int oldUserId) { |
| 809 | if (DEBUG_MU) Slog.i(TAG, "Stop guest or ephemeral user if background: " + oldUserId); |
Alex Chau | fbc7717 | 2018-01-18 18:43:35 +0000 | [diff] [blame] | 810 | synchronized(mLock) { |
| 811 | UserState oldUss = mStartedUsers.get(oldUserId); |
| 812 | if (oldUserId == UserHandle.USER_SYSTEM || oldUserId == mCurrentUserId || oldUss == null |
| 813 | || oldUss.state == UserState.STATE_STOPPING |
| 814 | || oldUss.state == UserState.STATE_SHUTDOWN) { |
| 815 | return; |
| 816 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 817 | } |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 818 | |
| 819 | UserInfo userInfo = getUserInfo(oldUserId); |
| 820 | if (userInfo.isEphemeral()) { |
| 821 | LocalServices.getService(UserManagerInternal.class).onEphemeralUserStop(oldUserId); |
| 822 | } |
| 823 | if (userInfo.isGuest() || userInfo.isEphemeral()) { |
| 824 | // This is a user to be stopped. |
| 825 | synchronized (mLock) { |
| 826 | stopUsersLU(oldUserId, true, null); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 831 | void scheduleStartProfiles() { |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 832 | if (!mHandler.hasMessages(START_PROFILES_MSG)) { |
| 833 | mHandler.sendMessageDelayed(mHandler.obtainMessage(START_PROFILES_MSG), |
| 834 | DateUtils.SECOND_IN_MILLIS); |
| 835 | } |
| 836 | } |
| 837 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 838 | void startProfiles() { |
| 839 | int currentUserId = getCurrentUserId(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 840 | if (DEBUG_MU) Slog.i(TAG, "startProfilesLocked"); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 841 | List<UserInfo> profiles = mInjector.getUserManager().getProfiles( |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 842 | currentUserId, false /* enabledOnly */); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 843 | List<UserInfo> profilesToStart = new ArrayList<>(profiles.size()); |
| 844 | for (UserInfo user : profiles) { |
| 845 | if ((user.flags & UserInfo.FLAG_INITIALIZED) == UserInfo.FLAG_INITIALIZED |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 846 | && user.id != currentUserId && !user.isQuietModeEnabled()) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 847 | profilesToStart.add(user); |
| 848 | } |
| 849 | } |
| 850 | final int profilesToStartSize = profilesToStart.size(); |
| 851 | int i = 0; |
Fyodor Kupolov | 53cba30 | 2017-10-25 18:11:48 -0700 | [diff] [blame] | 852 | for (; i < profilesToStartSize && i < (mMaxRunningUsers - 1); ++i) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 853 | startUser(profilesToStart.get(i).id, /* foreground= */ false); |
| 854 | } |
| 855 | if (i < profilesToStartSize) { |
| 856 | Slog.w(TAG, "More profiles than MAX_RUNNING_USERS"); |
| 857 | } |
| 858 | } |
| 859 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 860 | private IStorageManager getStorageManager() { |
| 861 | return IStorageManager.Stub.asInterface(ServiceManager.getService("mount")); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 862 | } |
Tony Mak | 64fd8c0 | 2017-12-01 19:11:59 +0000 | [diff] [blame] | 863 | boolean startUser(final int userId, final boolean foreground) { |
| 864 | return startUser(userId, foreground, null); |
| 865 | } |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 866 | |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 867 | /** |
| 868 | * Start user, if its not already running. |
| 869 | * <p>The user will be brought to the foreground, if {@code foreground} parameter is set. |
| 870 | * When starting the user, multiple intents will be broadcast in the following order:</p> |
| 871 | * <ul> |
| 872 | * <li>{@link Intent#ACTION_USER_STARTED} - sent to registered receivers of the new user |
| 873 | * <li>{@link Intent#ACTION_USER_BACKGROUND} - sent to registered receivers of the outgoing |
| 874 | * user and all profiles of this user. Sent only if {@code foreground} parameter is true |
| 875 | * <li>{@link Intent#ACTION_USER_FOREGROUND} - sent to registered receivers of the new |
| 876 | * user and all profiles of this user. Sent only if {@code foreground} parameter is true |
| 877 | * <li>{@link Intent#ACTION_USER_SWITCHED} - sent to registered receivers of the new user. |
| 878 | * Sent only if {@code foreground} parameter is true |
| 879 | * <li>{@link Intent#ACTION_USER_STARTING} - ordered broadcast sent to registered receivers |
| 880 | * of the new fg user |
| 881 | * <li>{@link Intent#ACTION_LOCKED_BOOT_COMPLETED} - ordered broadcast sent to receivers of |
| 882 | * the new user |
| 883 | * <li>{@link Intent#ACTION_USER_UNLOCKED} - sent to registered receivers of the new user |
| 884 | * <li>{@link Intent#ACTION_PRE_BOOT_COMPLETED} - ordered broadcast sent to receivers of the |
| 885 | * new user. Sent only when the user is booting after a system update. |
| 886 | * <li>{@link Intent#ACTION_USER_INITIALIZE} - ordered broadcast sent to receivers of the |
| 887 | * new user. Sent only the first time a user is starting. |
| 888 | * <li>{@link Intent#ACTION_BOOT_COMPLETED} - ordered broadcast sent to receivers of the new |
| 889 | * user. Indicates that the user has finished booting. |
| 890 | * </ul> |
| 891 | * |
| 892 | * @param userId ID of the user to start |
| 893 | * @param foreground true if user should be brought to the foreground |
| 894 | * @return true if the user has been successfully started |
| 895 | */ |
Tony Mak | 64fd8c0 | 2017-12-01 19:11:59 +0000 | [diff] [blame] | 896 | boolean startUser( |
| 897 | final int userId, |
| 898 | final boolean foreground, |
| 899 | @Nullable IProgressListener unlockListener) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 900 | if (mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 901 | != PackageManager.PERMISSION_GRANTED) { |
| 902 | String msg = "Permission Denial: switchUser() from pid=" |
| 903 | + Binder.getCallingPid() |
| 904 | + ", uid=" + Binder.getCallingUid() |
| 905 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 906 | Slog.w(TAG, msg); |
| 907 | throw new SecurityException(msg); |
| 908 | } |
| 909 | |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 910 | Slog.i(TAG, "Starting userid:" + userId + " fg:" + foreground); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 911 | |
| 912 | final long ident = Binder.clearCallingIdentity(); |
| 913 | try { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 914 | final int oldUserId = getCurrentUserId(); |
| 915 | if (oldUserId == userId) { |
| 916 | return true; |
| 917 | } |
| 918 | |
| 919 | if (foreground) { |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 920 | mInjector.clearAllLockedTasks("startUser"); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | final UserInfo userInfo = getUserInfo(userId); |
| 924 | if (userInfo == null) { |
| 925 | Slog.w(TAG, "No user info for user #" + userId); |
| 926 | return false; |
| 927 | } |
| 928 | if (foreground && userInfo.isManagedProfile()) { |
| 929 | Slog.w(TAG, "Cannot switch to User #" + userId + ": not a full user"); |
| 930 | return false; |
| 931 | } |
| 932 | |
| 933 | if (foreground && mUserSwitchUiEnabled) { |
| 934 | mInjector.getWindowManager().startFreezingScreen( |
| 935 | R.anim.screen_user_exit, R.anim.screen_user_enter); |
| 936 | } |
| 937 | |
| 938 | boolean needStart = false; |
| 939 | boolean updateUmState = false; |
| 940 | UserState uss; |
| 941 | |
| 942 | // If the user we are switching to is not currently started, then |
| 943 | // we need to start it now. |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 944 | synchronized (mLock) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 945 | uss = mStartedUsers.get(userId); |
| 946 | if (uss == null) { |
| 947 | uss = new UserState(UserHandle.of(userId)); |
Fyodor Kupolov | 0d77c6d | 2017-10-11 17:53:23 -0700 | [diff] [blame] | 948 | uss.mUnlockProgress.addListener(new UserProgressListener()); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 949 | mStartedUsers.put(userId, uss); |
| 950 | updateStartedUserArrayLU(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 951 | needStart = true; |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 952 | updateUmState = true; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 953 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 954 | final Integer userIdInt = userId; |
| 955 | mUserLru.remove(userIdInt); |
| 956 | mUserLru.add(userIdInt); |
Tony Mak | 553b997 | 2017-12-12 19:50:57 +0000 | [diff] [blame] | 957 | } |
| 958 | if (unlockListener != null) { |
| 959 | uss.mUnlockProgress.addListener(unlockListener); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 960 | } |
| 961 | if (updateUmState) { |
| 962 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
| 963 | } |
| 964 | if (foreground) { |
Dianne Hackborn | ced5439 | 2018-02-26 13:07:42 -0800 | [diff] [blame] | 965 | // Make sure the old user is no longer considering the display to be on. |
| 966 | mInjector.reportGlobalUsageEventLocked(UsageEvents.Event.SCREEN_NON_INTERACTIVE); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 967 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 968 | mCurrentUserId = userId; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 969 | mTargetUserId = UserHandle.USER_NULL; // reset, mCurrentUserId has caught up |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 970 | } |
| 971 | mInjector.updateUserConfiguration(); |
| 972 | updateCurrentProfileIds(); |
| 973 | mInjector.getWindowManager().setCurrentUser(userId, getCurrentProfileIds()); |
Dianne Hackborn | ced5439 | 2018-02-26 13:07:42 -0800 | [diff] [blame] | 974 | mInjector.reportCurWakefulnessUsageEvent(); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 975 | // Once the internal notion of the active user has switched, we lock the device |
| 976 | // with the option to show the user switcher on the keyguard. |
| 977 | if (mUserSwitchUiEnabled) { |
| 978 | mInjector.getWindowManager().setSwitchingUser(true); |
| 979 | mInjector.getWindowManager().lockNow(null); |
| 980 | } |
| 981 | } else { |
| 982 | final Integer currentUserIdInt = mCurrentUserId; |
| 983 | updateCurrentProfileIds(); |
| 984 | mInjector.getWindowManager().setCurrentProfileIds(getCurrentProfileIds()); |
| 985 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 986 | mUserLru.remove(currentUserIdInt); |
| 987 | mUserLru.add(currentUserIdInt); |
| 988 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 989 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 990 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 991 | // Make sure user is in the started state. If it is currently |
| 992 | // stopping, we need to knock that off. |
| 993 | if (uss.state == UserState.STATE_STOPPING) { |
| 994 | // If we are stopping, we haven't sent ACTION_SHUTDOWN, |
| 995 | // so we can just fairly silently bring the user back from |
| 996 | // the almost-dead. |
| 997 | uss.setState(uss.lastState); |
| 998 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
| 999 | synchronized (mLock) { |
| 1000 | updateStartedUserArrayLU(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1001 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1002 | needStart = true; |
| 1003 | } else if (uss.state == UserState.STATE_SHUTDOWN) { |
| 1004 | // This means ACTION_SHUTDOWN has been sent, so we will |
| 1005 | // need to treat this as a new boot of the user. |
| 1006 | uss.setState(UserState.STATE_BOOTING); |
| 1007 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
| 1008 | synchronized (mLock) { |
| 1009 | updateStartedUserArrayLU(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1010 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1011 | needStart = true; |
| 1012 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1013 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1014 | if (uss.state == UserState.STATE_BOOTING) { |
| 1015 | // Give user manager a chance to propagate user restrictions |
| 1016 | // to other services and prepare app storage |
| 1017 | mInjector.getUserManager().onBeforeStartUser(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1018 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1019 | // Booting up a new user, need to tell system services about it. |
| 1020 | // Note that this is on the same handler as scheduling of broadcasts, |
| 1021 | // which is important because it needs to go first. |
| 1022 | mHandler.sendMessage( |
| 1023 | mHandler.obtainMessage(SYSTEM_USER_START_MSG, userId, 0)); |
| 1024 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1025 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1026 | if (foreground) { |
| 1027 | mHandler.sendMessage(mHandler.obtainMessage(SYSTEM_USER_CURRENT_MSG, userId, |
| 1028 | oldUserId)); |
| 1029 | mHandler.removeMessages(REPORT_USER_SWITCH_MSG); |
| 1030 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 1031 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG, |
| 1032 | oldUserId, userId, uss)); |
| 1033 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG, |
| 1034 | oldUserId, userId, uss), USER_SWITCH_TIMEOUT_MS); |
| 1035 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1036 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1037 | if (needStart) { |
| 1038 | // Send USER_STARTED broadcast |
| 1039 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
| 1040 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1041 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 1042 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1043 | mInjector.broadcastIntent(intent, |
| 1044 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 1045 | null, false, false, MY_PID, SYSTEM_UID, userId); |
| 1046 | } |
| 1047 | |
| 1048 | if (foreground) { |
| 1049 | moveUserToForeground(uss, oldUserId, userId); |
| 1050 | } else { |
| 1051 | finishUserBoot(uss); |
| 1052 | } |
| 1053 | |
| 1054 | if (needStart) { |
| 1055 | Intent intent = new Intent(Intent.ACTION_USER_STARTING); |
| 1056 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1057 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1058 | mInjector.broadcastIntent(intent, |
| 1059 | null, new IIntentReceiver.Stub() { |
| 1060 | @Override |
| 1061 | public void performReceive(Intent intent, int resultCode, |
| 1062 | String data, Bundle extras, boolean ordered, |
| 1063 | boolean sticky, |
| 1064 | int sendingUser) throws RemoteException { |
| 1065 | } |
| 1066 | }, 0, null, null, |
| 1067 | new String[]{INTERACT_ACROSS_USERS}, AppOpsManager.OP_NONE, |
| 1068 | null, true, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1069 | } |
| 1070 | } finally { |
| 1071 | Binder.restoreCallingIdentity(ident); |
| 1072 | } |
| 1073 | |
| 1074 | return true; |
| 1075 | } |
| 1076 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1077 | /** |
| 1078 | * Start user, if its not already running, and bring it to foreground. |
| 1079 | */ |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 1080 | void startUserInForeground(final int targetUserId) { |
| 1081 | boolean success = startUser(targetUserId, /* foreground */ true); |
| 1082 | if (!success) { |
| 1083 | mInjector.getWindowManager().setSwitchingUser(false); |
| 1084 | } |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1087 | boolean unlockUser(final int userId, byte[] token, byte[] secret, IProgressListener listener) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1088 | if (mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 1089 | != PackageManager.PERMISSION_GRANTED) { |
| 1090 | String msg = "Permission Denial: unlockUser() from pid=" |
| 1091 | + Binder.getCallingPid() |
| 1092 | + ", uid=" + Binder.getCallingUid() |
| 1093 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 1094 | Slog.w(TAG, msg); |
| 1095 | throw new SecurityException(msg); |
| 1096 | } |
| 1097 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 1098 | final long binderToken = Binder.clearCallingIdentity(); |
| 1099 | try { |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1100 | return unlockUserCleared(userId, token, secret, listener); |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 1101 | } finally { |
| 1102 | Binder.restoreCallingIdentity(binderToken); |
| 1103 | } |
| 1104 | } |
| 1105 | |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1106 | /** |
| 1107 | * Attempt to unlock user without a credential token. This typically |
| 1108 | * succeeds when the device doesn't have credential-encrypted storage, or |
| 1109 | * when the the credential-encrypted storage isn't tied to a user-provided |
| 1110 | * PIN or pattern. |
| 1111 | */ |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1112 | private boolean maybeUnlockUser(final int userId) { |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1113 | // Try unlocking storage using empty token |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1114 | return unlockUserCleared(userId, null, null, null); |
| 1115 | } |
| 1116 | |
| 1117 | private static void notifyFinished(int userId, IProgressListener listener) { |
| 1118 | if (listener == null) return; |
| 1119 | try { |
| 1120 | listener.onFinished(userId, null); |
| 1121 | } catch (RemoteException ignored) { |
| 1122 | } |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1125 | private boolean unlockUserCleared(final int userId, byte[] token, byte[] secret, |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1126 | IProgressListener listener) { |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1127 | UserState uss; |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1128 | if (!StorageManager.isUserKeyUnlocked(userId)) { |
| 1129 | final UserInfo userInfo = getUserInfo(userId); |
| 1130 | final IStorageManager storageManager = getStorageManager(); |
| 1131 | try { |
| 1132 | // We always want to unlock user storage, even user is not started yet |
| 1133 | storageManager.unlockUserKey(userId, userInfo.serialNumber, token, secret); |
| 1134 | } catch (RemoteException | RuntimeException e) { |
| 1135 | Slog.w(TAG, "Failed to unlock: " + e.getMessage()); |
Ricky Wai | 4266fee | 2016-05-23 15:33:04 +0100 | [diff] [blame] | 1136 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1137 | } |
| 1138 | synchronized (mLock) { |
| 1139 | // Register the given listener to watch for unlock progress |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1140 | uss = mStartedUsers.get(userId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1141 | if (uss != null) { |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1142 | uss.mUnlockProgress.addListener(listener); |
Jeff Sharkey | 24d9491 | 2016-07-07 12:33:48 -0600 | [diff] [blame] | 1143 | uss.tokenProvided = (token != null); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1144 | } |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1145 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1146 | // Bail if user isn't actually running |
| 1147 | if (uss == null) { |
| 1148 | notifyFinished(userId, listener); |
| 1149 | return false; |
| 1150 | } |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 1151 | |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1152 | finishUserUnlocking(uss); |
| 1153 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1154 | // We just unlocked a user, so let's now attempt to unlock any |
| 1155 | // managed profiles under that user. |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 1156 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1157 | // First, get list of userIds. Requires mLock, so we cannot make external calls, e.g. to UMS |
| 1158 | int[] userIds; |
| 1159 | synchronized (mLock) { |
| 1160 | userIds = new int[mStartedUsers.size()]; |
| 1161 | for (int i = 0; i < userIds.length; i++) { |
| 1162 | userIds[i] = mStartedUsers.keyAt(i); |
Jeff Sharkey | 5dab713 | 2016-04-07 01:20:58 -0600 | [diff] [blame] | 1163 | } |
| 1164 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1165 | for (int testUserId : userIds) { |
| 1166 | final UserInfo parent = mInjector.getUserManager().getProfileParent(testUserId); |
| 1167 | if (parent != null && parent.id == userId && testUserId != userId) { |
| 1168 | Slog.d(TAG, "User " + testUserId + " (parent " + parent.id |
| 1169 | + "): attempting unlock because parent was just unlocked"); |
| 1170 | maybeUnlockUser(testUserId); |
| 1171 | } |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 1174 | return true; |
| 1175 | } |
| 1176 | |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1177 | boolean switchUser(final int targetUserId) { |
| 1178 | enforceShellRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, targetUserId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1179 | int currentUserId = getCurrentUserId(); |
| 1180 | UserInfo targetUserInfo = getUserInfo(targetUserId); |
| 1181 | if (targetUserId == currentUserId) { |
| 1182 | Slog.i(TAG, "user #" + targetUserId + " is already the current user"); |
| 1183 | return true; |
| 1184 | } |
| 1185 | if (targetUserInfo == null) { |
| 1186 | Slog.w(TAG, "No user info for user #" + targetUserId); |
| 1187 | return false; |
| 1188 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1189 | if (!targetUserInfo.supportsSwitchTo()) { |
| 1190 | Slog.w(TAG, "Cannot switch to User #" + targetUserId + ": not supported"); |
| 1191 | return false; |
| 1192 | } |
| 1193 | if (targetUserInfo.isManagedProfile()) { |
| 1194 | Slog.w(TAG, "Cannot switch to User #" + targetUserId + ": not a full user"); |
| 1195 | return false; |
| 1196 | } |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1197 | synchronized (mLock) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1198 | mTargetUserId = targetUserId; |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1199 | } |
| 1200 | if (mUserSwitchUiEnabled) { |
| 1201 | UserInfo currentUserInfo = getUserInfo(currentUserId); |
| 1202 | Pair<UserInfo, UserInfo> userNames = new Pair<>(currentUserInfo, targetUserInfo); |
| 1203 | mUiHandler.removeMessages(START_USER_SWITCH_UI_MSG); |
| 1204 | mUiHandler.sendMessage(mHandler.obtainMessage( |
| 1205 | START_USER_SWITCH_UI_MSG, userNames)); |
| 1206 | } else { |
| 1207 | mHandler.removeMessages(START_USER_SWITCH_FG_MSG); |
| 1208 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1209 | START_USER_SWITCH_FG_MSG, targetUserId, 0)); |
| 1210 | } |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1214 | private void showUserSwitchDialog(Pair<UserInfo, UserInfo> fromToUserPair) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1215 | // The dialog will show and then initiate the user switch by calling startUserInForeground |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 1216 | mInjector.showUserSwitchingDialog(fromToUserPair.first, fromToUserPair.second, |
| 1217 | getSwitchingFromSystemUserMessage(), getSwitchingToSystemUserMessage()); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1220 | private void dispatchForegroundProfileChanged(int userId) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1221 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 1222 | for (int i = 0; i < observerCount; i++) { |
| 1223 | try { |
| 1224 | mUserSwitchObservers.getBroadcastItem(i).onForegroundProfileSwitch(userId); |
| 1225 | } catch (RemoteException e) { |
| 1226 | // Ignore |
| 1227 | } |
| 1228 | } |
| 1229 | mUserSwitchObservers.finishBroadcast(); |
| 1230 | } |
| 1231 | |
| 1232 | /** Called on handler thread */ |
| 1233 | void dispatchUserSwitchComplete(int userId) { |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 1234 | mInjector.getWindowManager().setSwitchingUser(false); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1235 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 1236 | for (int i = 0; i < observerCount; i++) { |
| 1237 | try { |
| 1238 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(userId); |
| 1239 | } catch (RemoteException e) { |
| 1240 | } |
| 1241 | } |
| 1242 | mUserSwitchObservers.finishBroadcast(); |
| 1243 | } |
| 1244 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1245 | private void dispatchLockedBootComplete(int userId) { |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 1246 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 1247 | for (int i = 0; i < observerCount; i++) { |
| 1248 | try { |
| 1249 | mUserSwitchObservers.getBroadcastItem(i).onLockedBootComplete(userId); |
| 1250 | } catch (RemoteException e) { |
| 1251 | // Ignore |
| 1252 | } |
| 1253 | } |
| 1254 | mUserSwitchObservers.finishBroadcast(); |
| 1255 | } |
| 1256 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1257 | private void stopBackgroundUsersIfEnforced(int oldUserId) { |
| 1258 | // Never stop system user |
| 1259 | if (oldUserId == UserHandle.USER_SYSTEM) { |
| 1260 | return; |
| 1261 | } |
| 1262 | // For now, only check for user restriction. Additional checks can be added here |
| 1263 | boolean disallowRunInBg = hasUserRestriction(UserManager.DISALLOW_RUN_IN_BACKGROUND, |
| 1264 | oldUserId); |
| 1265 | if (!disallowRunInBg) { |
| 1266 | return; |
| 1267 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1268 | synchronized (mLock) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1269 | if (DEBUG_MU) Slog.i(TAG, "stopBackgroundUsersIfEnforced stopping " + oldUserId |
| 1270 | + " and related users"); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1271 | stopUsersLU(oldUserId, false, null); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1275 | private void timeoutUserSwitch(UserState uss, int oldUserId, int newUserId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1276 | synchronized (mLock) { |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 1277 | Slog.e(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId); |
| 1278 | mTimeoutUserSwitchCallbacks = mCurWaitingUserSwitchCallbacks; |
| 1279 | mHandler.removeMessages(USER_SWITCH_CALLBACKS_TIMEOUT_MSG); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1280 | sendContinueUserSwitchLU(uss, oldUserId, newUserId); |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 1281 | // Report observers that never called back (USER_SWITCH_CALLBACKS_TIMEOUT) |
| 1282 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_CALLBACKS_TIMEOUT_MSG, |
| 1283 | oldUserId, newUserId), USER_SWITCH_CALLBACKS_TIMEOUT_MS); |
| 1284 | } |
| 1285 | } |
| 1286 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1287 | private void timeoutUserSwitchCallbacks(int oldUserId, int newUserId) { |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 1288 | synchronized (mLock) { |
| 1289 | if (mTimeoutUserSwitchCallbacks != null && !mTimeoutUserSwitchCallbacks.isEmpty()) { |
| 1290 | Slog.wtf(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId |
| 1291 | + ". Observers that didn't respond: " + mTimeoutUserSwitchCallbacks); |
| 1292 | mTimeoutUserSwitchCallbacks = null; |
| 1293 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 1297 | void dispatchUserSwitch(final UserState uss, final int oldUserId, final int newUserId) { |
| 1298 | Slog.d(TAG, "Dispatch onUserSwitching oldUser #" + oldUserId + " newUser #" + newUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1299 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 1300 | if (observerCount > 0) { |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1301 | final ArraySet<String> curWaitingUserSwitchCallbacks = new ArraySet<>(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1302 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1303 | uss.switching = true; |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1304 | mCurWaitingUserSwitchCallbacks = curWaitingUserSwitchCallbacks; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1305 | } |
Fyodor Kupolov | 3864183 | 2016-06-28 17:37:09 -0700 | [diff] [blame] | 1306 | final AtomicInteger waitingCallbacksCount = new AtomicInteger(observerCount); |
Fyodor Kupolov | 0e0986e | 2016-09-22 18:01:36 -0700 | [diff] [blame] | 1307 | final long dispatchStartedTime = SystemClock.elapsedRealtime(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1308 | for (int i = 0; i < observerCount; i++) { |
| 1309 | try { |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1310 | // Prepend with unique prefix to guarantee that keys are unique |
| 1311 | final String name = "#" + i + " " + mUserSwitchObservers.getBroadcastCookie(i); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1312 | synchronized (mLock) { |
Fyodor Kupolov | 599d43b | 2016-06-24 13:46:18 -0700 | [diff] [blame] | 1313 | curWaitingUserSwitchCallbacks.add(name); |
| 1314 | } |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1315 | final IRemoteCallback callback = new IRemoteCallback.Stub() { |
| 1316 | @Override |
| 1317 | public void sendResult(Bundle data) throws RemoteException { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1318 | synchronized (mLock) { |
Fyodor Kupolov | 0e0986e | 2016-09-22 18:01:36 -0700 | [diff] [blame] | 1319 | long delay = SystemClock.elapsedRealtime() - dispatchStartedTime; |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 1320 | if (delay > USER_SWITCH_TIMEOUT_MS) { |
| 1321 | Slog.e(TAG, "User switch timeout: observer " + name |
Fyodor Kupolov | 0e0986e | 2016-09-22 18:01:36 -0700 | [diff] [blame] | 1322 | + " sent result after " + delay + " ms"); |
| 1323 | } |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1324 | curWaitingUserSwitchCallbacks.remove(name); |
Fyodor Kupolov | 807e650 | 2017-08-21 16:07:14 -0700 | [diff] [blame] | 1325 | // Continue switching if all callbacks have been notified and |
| 1326 | // user switching session is still valid |
| 1327 | if (waitingCallbacksCount.decrementAndGet() == 0 |
| 1328 | && (curWaitingUserSwitchCallbacks |
| 1329 | == mCurWaitingUserSwitchCallbacks)) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1330 | sendContinueUserSwitchLU(uss, oldUserId, newUserId); |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | }; |
| 1335 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitching(newUserId, callback); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1336 | } catch (RemoteException e) { |
| 1337 | } |
| 1338 | } |
| 1339 | } else { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1340 | synchronized (mLock) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1341 | sendContinueUserSwitchLU(uss, oldUserId, newUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | mUserSwitchObservers.finishBroadcast(); |
| 1345 | } |
| 1346 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1347 | void sendContinueUserSwitchLU(UserState uss, int oldUserId, int newUserId) { |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1348 | mCurWaitingUserSwitchCallbacks = null; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1349 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1350 | mHandler.sendMessage(mHandler.obtainMessage(CONTINUE_USER_SWITCH_MSG, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1351 | oldUserId, newUserId, uss)); |
| 1352 | } |
| 1353 | |
| 1354 | void continueUserSwitch(UserState uss, int oldUserId, int newUserId) { |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 1355 | Slog.d(TAG, "Continue user switch oldUser #" + oldUserId + ", newUser #" + newUserId); |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 1356 | if (mUserSwitchUiEnabled) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1357 | mInjector.getWindowManager().stopFreezingScreen(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1358 | } |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 1359 | uss.switching = false; |
| 1360 | mHandler.removeMessages(REPORT_USER_SWITCH_COMPLETE_MSG); |
| 1361 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_COMPLETE_MSG, |
| 1362 | newUserId, 0)); |
Alex Chau | b6ef869 | 2018-01-09 14:16:36 +0000 | [diff] [blame] | 1363 | stopGuestOrEphemeralUserIfBackground(oldUserId); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1364 | stopBackgroundUsersIfEnforced(oldUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1367 | private void moveUserToForeground(UserState uss, int oldUserId, int newUserId) { |
| 1368 | boolean homeInFront = mInjector.stackSupervisorSwitchUser(newUserId, uss); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1369 | if (homeInFront) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1370 | mInjector.startHomeActivity(newUserId, "moveUserToForeground"); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1371 | } else { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1372 | mInjector.stackSupervisorResumeFocusedStackTopActivity(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1373 | } |
| 1374 | EventLogTags.writeAmSwitchUser(newUserId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1375 | sendUserSwitchBroadcasts(oldUserId, newUserId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1378 | void sendUserSwitchBroadcasts(int oldUserId, int newUserId) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1379 | long ident = Binder.clearCallingIdentity(); |
| 1380 | try { |
| 1381 | Intent intent; |
| 1382 | if (oldUserId >= 0) { |
| 1383 | // Send USER_BACKGROUND broadcast to all profiles of the outgoing user |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1384 | List<UserInfo> profiles = mInjector.getUserManager().getProfiles(oldUserId, false); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1385 | int count = profiles.size(); |
| 1386 | for (int i = 0; i < count; i++) { |
| 1387 | int profileUserId = profiles.get(i).id; |
| 1388 | intent = new Intent(Intent.ACTION_USER_BACKGROUND); |
| 1389 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1390 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 1391 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileUserId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1392 | mInjector.broadcastIntent(intent, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1393 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 1394 | null, false, false, MY_PID, SYSTEM_UID, profileUserId); |
| 1395 | } |
| 1396 | } |
| 1397 | if (newUserId >= 0) { |
| 1398 | // Send USER_FOREGROUND broadcast to all profiles of the incoming user |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1399 | List<UserInfo> profiles = mInjector.getUserManager().getProfiles(newUserId, false); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1400 | int count = profiles.size(); |
| 1401 | for (int i = 0; i < count; i++) { |
| 1402 | int profileUserId = profiles.get(i).id; |
| 1403 | intent = new Intent(Intent.ACTION_USER_FOREGROUND); |
| 1404 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1405 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 1406 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileUserId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1407 | mInjector.broadcastIntent(intent, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1408 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 1409 | null, false, false, MY_PID, SYSTEM_UID, profileUserId); |
| 1410 | } |
| 1411 | intent = new Intent(Intent.ACTION_USER_SWITCHED); |
| 1412 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1413 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 1414 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1415 | mInjector.broadcastIntent(intent, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1416 | null, null, 0, null, null, |
| 1417 | new String[] {android.Manifest.permission.MANAGE_USERS}, |
| 1418 | AppOpsManager.OP_NONE, null, false, false, MY_PID, SYSTEM_UID, |
| 1419 | UserHandle.USER_ALL); |
| 1420 | } |
| 1421 | } finally { |
| 1422 | Binder.restoreCallingIdentity(ident); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 1428 | int allowMode, String name, String callerPackage) { |
| 1429 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 1430 | if (callingUserId == userId) { |
| 1431 | return userId; |
| 1432 | } |
| 1433 | |
| 1434 | // Note that we may be accessing mCurrentUserId outside of a lock... |
| 1435 | // shouldn't be a big deal, if this is being called outside |
| 1436 | // of a locked context there is intrinsically a race with |
| 1437 | // the value the caller will receive and someone else changing it. |
| 1438 | // We assume that USER_CURRENT_OR_SELF will use the current user; later |
| 1439 | // we will switch to the calling user if access to the current user fails. |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1440 | int targetUserId = unsafeConvertIncomingUser(userId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1441 | |
| 1442 | if (callingUid != 0 && callingUid != SYSTEM_UID) { |
| 1443 | final boolean allow; |
Tony Mak | e839d70 | 2018-01-22 15:34:46 +0000 | [diff] [blame] | 1444 | if (mInjector.isCallerRecents(callingUid) |
| 1445 | && callingUserId == getCurrentUserId() |
| 1446 | && isSameProfileGroup(callingUserId, targetUserId)) { |
| 1447 | // If the caller is Recents and it is running in the current user, we then allow it |
| 1448 | // to access its profiles. |
| 1449 | allow = true; |
| 1450 | } else if (mInjector.checkComponentPermission(INTERACT_ACROSS_USERS_FULL, callingPid, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1451 | callingUid, -1, true) == PackageManager.PERMISSION_GRANTED) { |
| 1452 | // If the caller has this permission, they always pass go. And collect $200. |
| 1453 | allow = true; |
| 1454 | } else if (allowMode == ALLOW_FULL_ONLY) { |
| 1455 | // We require full access, sucks to be you. |
| 1456 | allow = false; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1457 | } else if (mInjector.checkComponentPermission(INTERACT_ACROSS_USERS, callingPid, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1458 | callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 1459 | // If the caller does not have either permission, they are always doomed. |
| 1460 | allow = false; |
| 1461 | } else if (allowMode == ALLOW_NON_FULL) { |
| 1462 | // We are blanket allowing non-full access, you lucky caller! |
| 1463 | allow = true; |
| 1464 | } else if (allowMode == ALLOW_NON_FULL_IN_PROFILE) { |
| 1465 | // We may or may not allow this depending on whether the two users are |
| 1466 | // in the same profile. |
| 1467 | allow = isSameProfileGroup(callingUserId, targetUserId); |
| 1468 | } else { |
| 1469 | throw new IllegalArgumentException("Unknown mode: " + allowMode); |
| 1470 | } |
| 1471 | if (!allow) { |
| 1472 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 1473 | // In this case, they would like to just execute as their |
| 1474 | // owner user instead of failing. |
| 1475 | targetUserId = callingUserId; |
| 1476 | } else { |
| 1477 | StringBuilder builder = new StringBuilder(128); |
| 1478 | builder.append("Permission Denial: "); |
| 1479 | builder.append(name); |
| 1480 | if (callerPackage != null) { |
| 1481 | builder.append(" from "); |
| 1482 | builder.append(callerPackage); |
| 1483 | } |
| 1484 | builder.append(" asks to run as user "); |
| 1485 | builder.append(userId); |
| 1486 | builder.append(" but is calling from user "); |
| 1487 | builder.append(UserHandle.getUserId(callingUid)); |
| 1488 | builder.append("; this requires "); |
| 1489 | builder.append(INTERACT_ACROSS_USERS_FULL); |
| 1490 | if (allowMode != ALLOW_FULL_ONLY) { |
| 1491 | builder.append(" or "); |
| 1492 | builder.append(INTERACT_ACROSS_USERS); |
| 1493 | } |
| 1494 | String msg = builder.toString(); |
| 1495 | Slog.w(TAG, msg); |
| 1496 | throw new SecurityException(msg); |
| 1497 | } |
| 1498 | } |
| 1499 | } |
Makoto Onuki | 7041c4b | 2018-02-06 13:36:34 -0800 | [diff] [blame] | 1500 | if (!allowAll) { |
| 1501 | ensureNotSpecialUser(targetUserId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1502 | } |
| 1503 | // Check shell permission |
| 1504 | if (callingUid == Process.SHELL_UID && targetUserId >= UserHandle.USER_SYSTEM) { |
| 1505 | if (hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, targetUserId)) { |
| 1506 | throw new SecurityException("Shell does not have permission to access user " |
| 1507 | + targetUserId + "\n " + Debug.getCallers(3)); |
| 1508 | } |
| 1509 | } |
| 1510 | return targetUserId; |
| 1511 | } |
| 1512 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1513 | int unsafeConvertIncomingUser(int userId) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1514 | return (userId == UserHandle.USER_CURRENT || userId == UserHandle.USER_CURRENT_OR_SELF) |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1515 | ? getCurrentUserId(): userId; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Makoto Onuki | 7041c4b | 2018-02-06 13:36:34 -0800 | [diff] [blame] | 1518 | void ensureNotSpecialUser(int userId) { |
| 1519 | if (userId >= 0) { |
| 1520 | return; |
| 1521 | } |
| 1522 | throw new IllegalArgumentException("Call does not support special user #" + userId); |
| 1523 | } |
| 1524 | |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1525 | void registerUserSwitchObserver(IUserSwitchObserver observer, String name) { |
| 1526 | Preconditions.checkNotNull(name, "Observer name cannot be null"); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1527 | if (mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1528 | != PackageManager.PERMISSION_GRANTED) { |
| 1529 | final String msg = "Permission Denial: registerUserSwitchObserver() from pid=" |
| 1530 | + Binder.getCallingPid() |
| 1531 | + ", uid=" + Binder.getCallingUid() |
| 1532 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 1533 | Slog.w(TAG, msg); |
| 1534 | throw new SecurityException(msg); |
| 1535 | } |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1536 | mUserSwitchObservers.register(observer, name); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1539 | void sendForegroundProfileChanged(int userId) { |
| 1540 | mHandler.removeMessages(FOREGROUND_PROFILE_CHANGED_MSG); |
| 1541 | mHandler.obtainMessage(FOREGROUND_PROFILE_CHANGED_MSG, userId, 0).sendToTarget(); |
| 1542 | } |
| 1543 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1544 | void unregisterUserSwitchObserver(IUserSwitchObserver observer) { |
| 1545 | mUserSwitchObservers.unregister(observer); |
| 1546 | } |
| 1547 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1548 | UserState getStartedUserState(int userId) { |
| 1549 | synchronized (mLock) { |
| 1550 | return mStartedUsers.get(userId); |
| 1551 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | boolean hasStartedUserState(int userId) { |
| 1555 | return mStartedUsers.get(userId) != null; |
| 1556 | } |
| 1557 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1558 | private void updateStartedUserArrayLU() { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1559 | int num = 0; |
| 1560 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1561 | UserState uss = mStartedUsers.valueAt(i); |
| 1562 | // This list does not include stopping users. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1563 | if (uss.state != UserState.STATE_STOPPING |
| 1564 | && uss.state != UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1565 | num++; |
| 1566 | } |
| 1567 | } |
| 1568 | mStartedUserArray = new int[num]; |
| 1569 | num = 0; |
| 1570 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1571 | UserState uss = mStartedUsers.valueAt(i); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1572 | if (uss.state != UserState.STATE_STOPPING |
| 1573 | && uss.state != UserState.STATE_SHUTDOWN) { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 1574 | mStartedUserArray[num++] = mStartedUsers.keyAt(i); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1579 | void sendBootCompleted(IIntentReceiver resultTo) { |
| 1580 | // Get a copy of mStartedUsers to use outside of lock |
| 1581 | SparseArray<UserState> startedUsers; |
| 1582 | synchronized (mLock) { |
| 1583 | startedUsers = mStartedUsers.clone(); |
| 1584 | } |
| 1585 | for (int i = 0; i < startedUsers.size(); i++) { |
| 1586 | UserState uss = startedUsers.valueAt(i); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1587 | finishUserBoot(uss, resultTo); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | |
Wale Ogunwale | f80170f | 2016-02-04 15:12:29 -0800 | [diff] [blame] | 1591 | void onSystemReady() { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1592 | updateCurrentProfileIds(); |
Dianne Hackborn | 3378aa9 | 2018-03-30 17:43:49 -0700 | [diff] [blame] | 1593 | mInjector.reportCurWakefulnessUsageEvent(); |
Wale Ogunwale | f80170f | 2016-02-04 15:12:29 -0800 | [diff] [blame] | 1594 | } |
| 1595 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1596 | /** |
| 1597 | * Refreshes the list of users related to the current user when either a |
| 1598 | * user switch happens or when a new related user is started in the |
| 1599 | * background. |
| 1600 | */ |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1601 | private void updateCurrentProfileIds() { |
| 1602 | final List<UserInfo> profiles = mInjector.getUserManager().getProfiles(getCurrentUserId(), |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1603 | false /* enabledOnly */); |
| 1604 | int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null |
| 1605 | for (int i = 0; i < currentProfileIds.length; i++) { |
| 1606 | currentProfileIds[i] = profiles.get(i).id; |
| 1607 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1608 | final List<UserInfo> users = mInjector.getUserManager().getUsers(false); |
| 1609 | synchronized (mLock) { |
| 1610 | mCurrentProfileIds = currentProfileIds; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1611 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1612 | mUserProfileGroupIds.clear(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1613 | for (int i = 0; i < users.size(); i++) { |
| 1614 | UserInfo user = users.get(i); |
| 1615 | if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1616 | mUserProfileGroupIds.put(user.id, user.profileGroupId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1617 | } |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1622 | int[] getStartedUserArray() { |
| 1623 | synchronized (mLock) { |
| 1624 | return mStartedUserArray; |
| 1625 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1628 | boolean isUserRunning(int userId, int flags) { |
| 1629 | UserState state = getStartedUserState(userId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1630 | if (state == null) { |
| 1631 | return false; |
| 1632 | } |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 1633 | if ((flags & ActivityManager.FLAG_OR_STOPPED) != 0) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1634 | return true; |
| 1635 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1636 | if ((flags & ActivityManager.FLAG_AND_LOCKED) != 0) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1637 | switch (state.state) { |
| 1638 | case UserState.STATE_BOOTING: |
| 1639 | case UserState.STATE_RUNNING_LOCKED: |
| 1640 | return true; |
| 1641 | default: |
| 1642 | return false; |
| 1643 | } |
| 1644 | } |
| 1645 | if ((flags & ActivityManager.FLAG_AND_UNLOCKING_OR_UNLOCKED) != 0) { |
| 1646 | switch (state.state) { |
| 1647 | case UserState.STATE_RUNNING_UNLOCKING: |
| 1648 | case UserState.STATE_RUNNING_UNLOCKED: |
| 1649 | return true; |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 1650 | // In the stopping/shutdown state return unlock state of the user key |
| 1651 | case UserState.STATE_STOPPING: |
| 1652 | case UserState.STATE_SHUTDOWN: |
| 1653 | return StorageManager.isUserKeyUnlocked(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1654 | default: |
| 1655 | return false; |
| 1656 | } |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 1657 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1658 | if ((flags & ActivityManager.FLAG_AND_UNLOCKED) != 0) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1659 | switch (state.state) { |
| 1660 | case UserState.STATE_RUNNING_UNLOCKED: |
| 1661 | return true; |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 1662 | // In the stopping/shutdown state return unlock state of the user key |
| 1663 | case UserState.STATE_STOPPING: |
| 1664 | case UserState.STATE_SHUTDOWN: |
| 1665 | return StorageManager.isUserKeyUnlocked(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1666 | default: |
| 1667 | return false; |
| 1668 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Fyodor Kupolov | a24e81219f | 2017-06-23 12:28:39 -0700 | [diff] [blame] | 1671 | return state.state != UserState.STATE_STOPPING && state.state != UserState.STATE_SHUTDOWN; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1674 | UserInfo getCurrentUser() { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1675 | if ((mInjector.checkCallingPermission(INTERACT_ACROSS_USERS) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1676 | != PackageManager.PERMISSION_GRANTED) && ( |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1677 | mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1678 | != PackageManager.PERMISSION_GRANTED)) { |
| 1679 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 1680 | + Binder.getCallingPid() |
| 1681 | + ", uid=" + Binder.getCallingUid() |
| 1682 | + " requires " + INTERACT_ACROSS_USERS; |
| 1683 | Slog.w(TAG, msg); |
| 1684 | throw new SecurityException(msg); |
| 1685 | } |
Fyodor Kupolov | dcb26bb | 2017-05-09 17:06:52 -0700 | [diff] [blame] | 1686 | |
| 1687 | // Optimization - if there is no pending user switch, return current id |
| 1688 | if (mTargetUserId == UserHandle.USER_NULL) { |
| 1689 | return getUserInfo(mCurrentUserId); |
| 1690 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1691 | synchronized (mLock) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1692 | return getCurrentUserLU(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1696 | UserInfo getCurrentUserLU() { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1697 | int userId = mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1698 | return getUserInfo(userId); |
| 1699 | } |
| 1700 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1701 | int getCurrentOrTargetUserId() { |
| 1702 | synchronized (mLock) { |
| 1703 | return mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | int getCurrentOrTargetUserIdLU() { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1708 | return mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1711 | |
| 1712 | int getCurrentUserIdLU() { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1713 | return mCurrentUserId; |
| 1714 | } |
| 1715 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1716 | int getCurrentUserId() { |
| 1717 | synchronized (mLock) { |
| 1718 | return mCurrentUserId; |
| 1719 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1722 | private boolean isCurrentUserLU(int userId) { |
| 1723 | return userId == getCurrentOrTargetUserIdLU(); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | int[] getUsers() { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1727 | UserManagerService ums = mInjector.getUserManager(); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1728 | return ums != null ? ums.getUserIds() : new int[] { 0 }; |
| 1729 | } |
| 1730 | |
| 1731 | UserInfo getUserInfo(int userId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1732 | return mInjector.getUserManager().getUserInfo(userId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | int[] getUserIds() { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1736 | return mInjector.getUserManager().getUserIds(); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
Makoto Onuki | 6569c36 | 2018-02-27 15:52:01 -0800 | [diff] [blame] | 1739 | /** |
| 1740 | * If {@code userId} is {@link UserHandle#USER_ALL}, then return an array with all running user |
| 1741 | * IDs. Otherwise return an array whose only element is the given user id. |
| 1742 | * |
| 1743 | * It doesn't handle other special user IDs such as {@link UserHandle#USER_CURRENT}. |
| 1744 | */ |
| 1745 | int[] expandUserId(int userId) { |
| 1746 | if (userId != UserHandle.USER_ALL) { |
| 1747 | return new int[] {userId}; |
| 1748 | } else { |
| 1749 | return getUsers(); |
| 1750 | } |
| 1751 | } |
| 1752 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1753 | boolean exists(int userId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1754 | return mInjector.getUserManager().exists(userId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1755 | } |
| 1756 | |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1757 | void enforceShellRestriction(String restriction, int userHandle) { |
| 1758 | if (Binder.getCallingUid() == SHELL_UID) { |
| 1759 | if (userHandle < 0 || hasUserRestriction(restriction, userHandle)) { |
| 1760 | throw new SecurityException("Shell does not have permission to access user " |
| 1761 | + userHandle); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1766 | boolean hasUserRestriction(String restriction, int userId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1767 | return mInjector.getUserManager().hasUserRestriction(restriction, userId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | Set<Integer> getProfileIds(int userId) { |
| 1771 | Set<Integer> userIds = new HashSet<>(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1772 | final List<UserInfo> profiles = mInjector.getUserManager().getProfiles(userId, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1773 | false /* enabledOnly */); |
| 1774 | for (UserInfo user : profiles) { |
| 1775 | userIds.add(user.id); |
| 1776 | } |
| 1777 | return userIds; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | boolean isSameProfileGroup(int callingUserId, int targetUserId) { |
Makoto Onuki | 8198dea | 2016-07-28 13:10:42 -0700 | [diff] [blame] | 1781 | if (callingUserId == targetUserId) { |
| 1782 | return true; |
| 1783 | } |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1784 | synchronized (mLock) { |
| 1785 | int callingProfile = mUserProfileGroupIds.get(callingUserId, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1786 | UserInfo.NO_PROFILE_GROUP_ID); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1787 | int targetProfile = mUserProfileGroupIds.get(targetUserId, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1788 | UserInfo.NO_PROFILE_GROUP_ID); |
| 1789 | return callingProfile != UserInfo.NO_PROFILE_GROUP_ID |
| 1790 | && callingProfile == targetProfile; |
| 1791 | } |
| 1792 | } |
| 1793 | |
Tony Mak | 60f53e6 | 2017-12-21 20:03:29 +0000 | [diff] [blame] | 1794 | boolean isUserOrItsParentRunning(int userId) { |
| 1795 | synchronized (mLock) { |
| 1796 | if (isUserRunning(userId, 0)) { |
| 1797 | return true; |
| 1798 | } |
| 1799 | final int parentUserId = mUserProfileGroupIds.get(userId, UserInfo.NO_PROFILE_GROUP_ID); |
| 1800 | if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1801 | return false; |
| 1802 | } |
| 1803 | return isUserRunning(parentUserId, 0); |
| 1804 | } |
| 1805 | } |
| 1806 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1807 | boolean isCurrentProfile(int userId) { |
| 1808 | synchronized (mLock) { |
| 1809 | return ArrayUtils.contains(mCurrentProfileIds, userId); |
| 1810 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1813 | int[] getCurrentProfileIds() { |
| 1814 | synchronized (mLock) { |
| 1815 | return mCurrentProfileIds; |
| 1816 | } |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Fyodor Kupolov | 8d2faf0 | 2018-01-09 18:51:59 -0800 | [diff] [blame] | 1819 | void onUserRemoved(int userId) { |
| 1820 | synchronized (mLock) { |
| 1821 | int size = mUserProfileGroupIds.size(); |
| 1822 | for (int i = size - 1; i >= 0; i--) { |
| 1823 | if (mUserProfileGroupIds.keyAt(i) == userId |
| 1824 | || mUserProfileGroupIds.valueAt(i) == userId) { |
| 1825 | mUserProfileGroupIds.removeAt(i); |
| 1826 | |
| 1827 | } |
| 1828 | } |
| 1829 | mCurrentProfileIds = ArrayUtils.removeInt(mCurrentProfileIds, userId); |
| 1830 | } |
| 1831 | } |
| 1832 | |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 1833 | /** |
| 1834 | * Returns whether the given user requires credential entry at this time. This is used to |
| 1835 | * intercept activity launches for work apps when the Work Challenge is present. |
| 1836 | */ |
Benjamin Franz | 563707b | 2017-06-29 15:06:13 +0100 | [diff] [blame] | 1837 | protected boolean shouldConfirmCredentials(int userId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1838 | synchronized (mLock) { |
Rubin Xu | b93522a | 2016-02-23 18:21:48 +0000 | [diff] [blame] | 1839 | if (mStartedUsers.get(userId) == null) { |
| 1840 | return false; |
| 1841 | } |
| 1842 | } |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 1843 | if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 1844 | return false; |
| 1845 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1846 | final KeyguardManager km = mInjector.getKeyguardManager(); |
Tony Mak | ae13e18 | 2016-05-17 16:36:14 +0100 | [diff] [blame] | 1847 | return km.isDeviceLocked(userId) && km.isDeviceSecure(userId); |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Tony Mak | 8c536f9 | 2016-03-21 12:20:41 +0000 | [diff] [blame] | 1850 | boolean isLockScreenDisabled(@UserIdInt int userId) { |
| 1851 | return mLockPatternUtils.isLockScreenDisabled(userId); |
| 1852 | } |
| 1853 | |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 1854 | void setSwitchingFromSystemUserMessage(String switchingFromSystemUserMessage) { |
| 1855 | synchronized (mLock) { |
| 1856 | mSwitchingFromSystemUserMessage = switchingFromSystemUserMessage; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | void setSwitchingToSystemUserMessage(String switchingToSystemUserMessage) { |
| 1861 | synchronized (mLock) { |
| 1862 | mSwitchingToSystemUserMessage = switchingToSystemUserMessage; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | private String getSwitchingFromSystemUserMessage() { |
| 1867 | synchronized (mLock) { |
| 1868 | return mSwitchingFromSystemUserMessage; |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | private String getSwitchingToSystemUserMessage() { |
| 1873 | synchronized (mLock) { |
| 1874 | return mSwitchingToSystemUserMessage; |
| 1875 | } |
| 1876 | } |
| 1877 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 1878 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 1879 | synchronized (mLock) { |
| 1880 | long token = proto.start(fieldId); |
| 1881 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1882 | UserState uss = mStartedUsers.valueAt(i); |
| 1883 | final long uToken = proto.start(UserControllerProto.STARTED_USERS); |
| 1884 | proto.write(UserControllerProto.User.ID, uss.mHandle.getIdentifier()); |
| 1885 | uss.writeToProto(proto, UserControllerProto.User.STATE); |
| 1886 | proto.end(uToken); |
| 1887 | } |
| 1888 | for (int i = 0; i < mStartedUserArray.length; i++) { |
| 1889 | proto.write(UserControllerProto.STARTED_USER_ARRAY, mStartedUserArray[i]); |
| 1890 | } |
| 1891 | for (int i = 0; i < mUserLru.size(); i++) { |
| 1892 | proto.write(UserControllerProto.USER_LRU, mUserLru.get(i)); |
| 1893 | } |
| 1894 | if (mUserProfileGroupIds.size() > 0) { |
| 1895 | for (int i = 0; i < mUserProfileGroupIds.size(); i++) { |
| 1896 | final long uToken = proto.start(UserControllerProto.USER_PROFILE_GROUP_IDS); |
| 1897 | proto.write(UserControllerProto.UserProfile.USER, |
| 1898 | mUserProfileGroupIds.keyAt(i)); |
| 1899 | proto.write(UserControllerProto.UserProfile.PROFILE, |
| 1900 | mUserProfileGroupIds.valueAt(i)); |
| 1901 | proto.end(uToken); |
| 1902 | } |
| 1903 | } |
| 1904 | proto.end(token); |
| 1905 | } |
| 1906 | } |
| 1907 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1908 | void dump(PrintWriter pw, boolean dumpAll) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1909 | synchronized (mLock) { |
| 1910 | pw.println(" mStartedUsers:"); |
| 1911 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1912 | UserState uss = mStartedUsers.valueAt(i); |
| 1913 | pw.print(" User #"); |
| 1914 | pw.print(uss.mHandle.getIdentifier()); |
| 1915 | pw.print(": "); |
| 1916 | uss.dump("", pw); |
| 1917 | } |
| 1918 | pw.print(" mStartedUserArray: ["); |
| 1919 | for (int i = 0; i < mStartedUserArray.length; i++) { |
| 1920 | if (i > 0) |
| 1921 | pw.print(", "); |
| 1922 | pw.print(mStartedUserArray[i]); |
| 1923 | } |
| 1924 | pw.println("]"); |
| 1925 | pw.print(" mUserLru: ["); |
| 1926 | for (int i = 0; i < mUserLru.size(); i++) { |
| 1927 | if (i > 0) |
| 1928 | pw.print(", "); |
| 1929 | pw.print(mUserLru.get(i)); |
| 1930 | } |
| 1931 | pw.println("]"); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1932 | if (mUserProfileGroupIds.size() > 0) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1933 | pw.println(" mUserProfileGroupIds:"); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1934 | for (int i=0; i< mUserProfileGroupIds.size(); i++) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1935 | pw.print(" User #"); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1936 | pw.print(mUserProfileGroupIds.keyAt(i)); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1937 | pw.print(" -> profile #"); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1938 | pw.println(mUserProfileGroupIds.valueAt(i)); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | } |
| 1942 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1943 | |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1944 | public boolean handleMessage(Message msg) { |
| 1945 | switch (msg.what) { |
| 1946 | case START_USER_SWITCH_FG_MSG: |
| 1947 | startUserInForeground(msg.arg1); |
| 1948 | break; |
| 1949 | case REPORT_USER_SWITCH_MSG: |
| 1950 | dispatchUserSwitch((UserState) msg.obj, msg.arg1, msg.arg2); |
| 1951 | break; |
| 1952 | case CONTINUE_USER_SWITCH_MSG: |
| 1953 | continueUserSwitch((UserState) msg.obj, msg.arg1, msg.arg2); |
| 1954 | break; |
| 1955 | case USER_SWITCH_TIMEOUT_MSG: |
| 1956 | timeoutUserSwitch((UserState) msg.obj, msg.arg1, msg.arg2); |
| 1957 | break; |
| 1958 | case USER_SWITCH_CALLBACKS_TIMEOUT_MSG: |
| 1959 | timeoutUserSwitchCallbacks(msg.arg1, msg.arg2); |
| 1960 | break; |
| 1961 | case START_PROFILES_MSG: |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 1962 | startProfiles(); |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1963 | break; |
| 1964 | case SYSTEM_USER_START_MSG: |
| 1965 | mInjector.batteryStatsServiceNoteEvent( |
| 1966 | BatteryStats.HistoryItem.EVENT_USER_RUNNING_START, |
| 1967 | Integer.toString(msg.arg1), msg.arg1); |
| 1968 | mInjector.getSystemServiceManager().startUser(msg.arg1); |
| 1969 | break; |
| 1970 | case SYSTEM_USER_UNLOCK_MSG: |
| 1971 | final int userId = msg.arg1; |
| 1972 | mInjector.getSystemServiceManager().unlockUser(userId); |
Fyodor Kupolov | 4c72df0 | 2017-11-14 11:43:40 -0800 | [diff] [blame] | 1973 | // Loads recents on a worker thread that allows disk I/O |
| 1974 | FgThread.getHandler().post(() -> { |
| 1975 | mInjector.loadUserRecents(userId); |
| 1976 | }); |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 1977 | finishUserUnlocked((UserState) msg.obj); |
| 1978 | break; |
| 1979 | case SYSTEM_USER_CURRENT_MSG: |
| 1980 | mInjector.batteryStatsServiceNoteEvent( |
| 1981 | BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_FINISH, |
| 1982 | Integer.toString(msg.arg2), msg.arg2); |
| 1983 | mInjector.batteryStatsServiceNoteEvent( |
| 1984 | BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_START, |
| 1985 | Integer.toString(msg.arg1), msg.arg1); |
| 1986 | |
| 1987 | mInjector.getSystemServiceManager().switchUser(msg.arg1); |
| 1988 | break; |
| 1989 | case FOREGROUND_PROFILE_CHANGED_MSG: |
| 1990 | dispatchForegroundProfileChanged(msg.arg1); |
| 1991 | break; |
| 1992 | case REPORT_USER_SWITCH_COMPLETE_MSG: |
| 1993 | dispatchUserSwitchComplete(msg.arg1); |
| 1994 | break; |
| 1995 | case REPORT_LOCKED_BOOT_COMPLETE_MSG: |
| 1996 | dispatchLockedBootComplete(msg.arg1); |
| 1997 | break; |
| 1998 | case START_USER_SWITCH_UI_MSG: |
| 1999 | showUserSwitchDialog((Pair<UserInfo, UserInfo>) msg.obj); |
| 2000 | break; |
| 2001 | } |
| 2002 | return false; |
| 2003 | } |
| 2004 | |
Fyodor Kupolov | 0d77c6d | 2017-10-11 17:53:23 -0700 | [diff] [blame] | 2005 | private static class UserProgressListener extends IProgressListener.Stub { |
| 2006 | private volatile long mUnlockStarted; |
| 2007 | @Override |
| 2008 | public void onStarted(int id, Bundle extras) throws RemoteException { |
| 2009 | Slog.d(TAG, "Started unlocking user " + id); |
| 2010 | mUnlockStarted = SystemClock.uptimeMillis(); |
| 2011 | } |
| 2012 | |
| 2013 | @Override |
| 2014 | public void onProgress(int id, int progress, Bundle extras) throws RemoteException { |
| 2015 | Slog.d(TAG, "Unlocking user " + id + " progress " + progress); |
| 2016 | } |
| 2017 | |
| 2018 | @Override |
| 2019 | public void onFinished(int id, Bundle extras) throws RemoteException { |
| 2020 | long unlockTime = SystemClock.uptimeMillis() - mUnlockStarted; |
| 2021 | |
| 2022 | // Report system user unlock time to perf dashboard |
| 2023 | if (id == UserHandle.USER_SYSTEM) { |
| 2024 | new TimingsTraceLog("SystemServerTiming", Trace.TRACE_TAG_SYSTEM_SERVER) |
| 2025 | .logDuration("SystemUserUnlock", unlockTime); |
| 2026 | } else { |
| 2027 | Slog.d(TAG, "Unlocking user " + id + " took " + unlockTime + " ms"); |
| 2028 | } |
| 2029 | } |
| 2030 | }; |
| 2031 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2032 | @VisibleForTesting |
| 2033 | static class Injector { |
| 2034 | private final ActivityManagerService mService; |
| 2035 | private UserManagerService mUserManager; |
| 2036 | private UserManagerInternal mUserManagerInternal; |
| 2037 | |
| 2038 | Injector(ActivityManagerService service) { |
| 2039 | mService = service; |
| 2040 | } |
| 2041 | |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 2042 | protected Handler getHandler(Handler.Callback callback) { |
| 2043 | return new Handler(mService.mHandlerThread.getLooper(), callback); |
| 2044 | } |
| 2045 | |
| 2046 | protected Handler getUiHandler(Handler.Callback callback) { |
| 2047 | return new Handler(mService.mUiHandler.getLooper(), callback); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | protected Context getContext() { |
| 2051 | return mService.mContext; |
| 2052 | } |
| 2053 | |
| 2054 | protected LockPatternUtils getLockPatternUtils() { |
| 2055 | return new LockPatternUtils(getContext()); |
| 2056 | } |
| 2057 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2058 | protected int broadcastIntent(Intent intent, String resolvedType, |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2059 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 2060 | Bundle resultExtras, String[] requiredPermissions, int appOp, Bundle bOptions, |
| 2061 | boolean ordered, boolean sticky, int callingPid, int callingUid, int userId) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2062 | // TODO b/64165549 Verify that mLock is not held before calling AMS methods |
| 2063 | synchronized (mService) { |
| 2064 | return mService.broadcastIntentLocked(null, null, intent, resolvedType, resultTo, |
| 2065 | resultCode, resultData, resultExtras, requiredPermissions, appOp, bOptions, |
| 2066 | ordered, sticky, callingPid, callingUid, userId); |
| 2067 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | int checkCallingPermission(String permission) { |
| 2071 | return mService.checkCallingPermission(permission); |
| 2072 | } |
| 2073 | |
| 2074 | WindowManagerService getWindowManager() { |
| 2075 | return mService.mWindowManager; |
| 2076 | } |
| 2077 | void activityManagerOnUserStopped(int userId) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2078 | synchronized (mService) { |
| 2079 | mService.onUserStoppedLocked(userId); |
| 2080 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | void systemServiceManagerCleanupUser(int userId) { |
| 2084 | mService.mSystemServiceManager.cleanupUser(userId); |
| 2085 | } |
| 2086 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2087 | protected UserManagerService getUserManager() { |
| 2088 | if (mUserManager == null) { |
| 2089 | IBinder b = ServiceManager.getService(Context.USER_SERVICE); |
| 2090 | mUserManager = (UserManagerService) IUserManager.Stub.asInterface(b); |
| 2091 | } |
| 2092 | return mUserManager; |
| 2093 | } |
| 2094 | |
| 2095 | UserManagerInternal getUserManagerInternal() { |
| 2096 | if (mUserManagerInternal == null) { |
| 2097 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
| 2098 | } |
| 2099 | return mUserManagerInternal; |
| 2100 | } |
| 2101 | |
| 2102 | KeyguardManager getKeyguardManager() { |
| 2103 | return mService.mContext.getSystemService(KeyguardManager.class); |
| 2104 | } |
| 2105 | |
| 2106 | void batteryStatsServiceNoteEvent(int code, String name, int uid) { |
| 2107 | mService.mBatteryStatsService.noteEvent(code, name, uid); |
| 2108 | } |
| 2109 | |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 2110 | boolean isRuntimeRestarted() { |
| 2111 | return mService.mSystemServiceManager.isRuntimeRestarted(); |
| 2112 | } |
| 2113 | |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 2114 | SystemServiceManager getSystemServiceManager() { |
| 2115 | return mService.mSystemServiceManager; |
| 2116 | } |
| 2117 | |
Fyodor Kupolov | 4ba91b9 | 2017-01-20 18:12:35 -0800 | [diff] [blame] | 2118 | boolean isFirstBootOrUpgrade() { |
| 2119 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 2120 | try { |
| 2121 | return pm.isFirstBoot() || pm.isUpgrade(); |
| 2122 | } catch (RemoteException e) { |
| 2123 | throw e.rethrowFromSystemServer(); |
| 2124 | } |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 2125 | } |
| 2126 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2127 | void sendPreBootBroadcast(int userId, boolean quiet, final Runnable onFinish) { |
| 2128 | new PreBootBroadcaster(mService, userId, null, quiet) { |
| 2129 | @Override |
| 2130 | public void onFinished() { |
| 2131 | onFinish.run(); |
| 2132 | } |
| 2133 | }.sendNext(); |
| 2134 | } |
| 2135 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2136 | void activityManagerForceStopPackage(int userId, String reason) { |
| 2137 | synchronized (mService) { |
| 2138 | mService.forceStopPackageLocked(null, -1, false, false, true, false, false, |
| 2139 | userId, reason); |
| 2140 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2141 | }; |
| 2142 | |
| 2143 | int checkComponentPermission(String permission, int pid, int uid, int owningUid, |
| 2144 | boolean exported) { |
| 2145 | return mService.checkComponentPermission(permission, pid, uid, owningUid, exported); |
| 2146 | } |
| 2147 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2148 | protected void startHomeActivity(int userId, String reason) { |
| 2149 | synchronized (mService) { |
| 2150 | mService.startHomeActivityLocked(userId, reason); |
| 2151 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2154 | void updateUserConfiguration() { |
| 2155 | synchronized (mService) { |
| 2156 | mService.updateUserConfigurationLocked(); |
| 2157 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2160 | void clearBroadcastQueueForUser(int userId) { |
| 2161 | synchronized (mService) { |
| 2162 | mService.clearBroadcastQueueForUserLocked(userId); |
| 2163 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2166 | void loadUserRecents(int userId) { |
| 2167 | synchronized (mService) { |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 2168 | mService.getRecentTasks().loadUserRecentsLocked(userId); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2169 | } |
Fyodor Kupolov | 58e732d | 2017-09-12 17:35:41 -0700 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | void startPersistentApps(int matchFlags) { |
| 2173 | mService.startPersistentApps(matchFlags); |
| 2174 | } |
| 2175 | |
| 2176 | void installEncryptionUnawareProviders(int userId) { |
| 2177 | mService.installEncryptionUnawareProviders(userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 2180 | void showUserSwitchingDialog(UserInfo fromUser, UserInfo toUser, |
| 2181 | String switchingFromSystemUserMessage, String switchingToSystemUserMessage) { |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 2182 | Dialog d; |
| 2183 | if (!mService.mContext.getPackageManager() |
| 2184 | .hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) { |
| 2185 | d = new UserSwitchingDialog(mService, mService.mContext, fromUser, toUser, |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 2186 | true /* above system */, switchingFromSystemUserMessage, |
| 2187 | switchingToSystemUserMessage); |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 2188 | } else { |
| 2189 | d = new CarUserSwitchingDialog(mService, mService.mContext, fromUser, toUser, |
| 2190 | true /* above system */, switchingFromSystemUserMessage, |
| 2191 | switchingToSystemUserMessage); |
| 2192 | } |
| 2193 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2194 | d.show(); |
| 2195 | } |
Tony Mak | 5c2cf03 | 2017-04-03 18:38:23 +0100 | [diff] [blame] | 2196 | |
Dianne Hackborn | ced5439 | 2018-02-26 13:07:42 -0800 | [diff] [blame] | 2197 | void reportGlobalUsageEventLocked(int event) { |
| 2198 | synchronized (mService) { |
| 2199 | mService.reportGlobalUsageEventLocked(event); |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | void reportCurWakefulnessUsageEvent() { |
| 2204 | synchronized (mService) { |
| 2205 | mService.reportCurWakefulnessUsageEventLocked(); |
| 2206 | } |
| 2207 | } |
| 2208 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2209 | void stackSupervisorRemoveUser(int userId) { |
| 2210 | synchronized (mService) { |
| 2211 | mService.mStackSupervisor.removeUserLocked(userId); |
| 2212 | } |
Tony Mak | 5c2cf03 | 2017-04-03 18:38:23 +0100 | [diff] [blame] | 2213 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 2214 | |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2215 | protected boolean stackSupervisorSwitchUser(int userId, UserState uss) { |
| 2216 | synchronized (mService) { |
| 2217 | return mService.mStackSupervisor.switchUserLocked(userId, uss); |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | protected void stackSupervisorResumeFocusedStackTopActivity() { |
| 2222 | synchronized (mService) { |
| 2223 | mService.mStackSupervisor.resumeFocusedStackTopActivityLocked(); |
| 2224 | } |
| 2225 | } |
| 2226 | |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 2227 | protected void clearAllLockedTasks(String reason) { |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2228 | synchronized (mService) { |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 2229 | mService.getLockTaskController().clearLockedTasks(reason); |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 2230 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 2231 | } |
Tony Mak | e839d70 | 2018-01-22 15:34:46 +0000 | [diff] [blame] | 2232 | |
| 2233 | protected boolean isCallerRecents(int callingUid) { |
| 2234 | return mService.getRecentTasks().isCallerRecents(callingUid); |
| 2235 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 2236 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 2237 | } |