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