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