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; |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 25 | import static android.content.Context.KEYGUARD_SERVICE; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 26 | import static android.os.Process.SYSTEM_UID; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [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; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 35 | import static com.android.server.am.ActivityManagerService.REPORT_LOCKED_BOOT_COMPLETE_MSG; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 36 | import static com.android.server.am.ActivityManagerService.REPORT_USER_SWITCH_COMPLETE_MSG; |
| 37 | import static com.android.server.am.ActivityManagerService.REPORT_USER_SWITCH_MSG; |
| 38 | import static com.android.server.am.ActivityManagerService.SYSTEM_USER_CURRENT_MSG; |
| 39 | import static com.android.server.am.ActivityManagerService.SYSTEM_USER_START_MSG; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 40 | import static com.android.server.am.ActivityManagerService.SYSTEM_USER_UNLOCK_MSG; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 41 | import static com.android.server.am.ActivityManagerService.USER_SWITCH_TIMEOUT_MSG; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 42 | import static com.android.server.am.UserState.STATE_BOOTING; |
| 43 | import static com.android.server.am.UserState.STATE_RUNNING_LOCKED; |
| 44 | import static com.android.server.am.UserState.STATE_RUNNING_UNLOCKED; |
| 45 | import static com.android.server.am.UserState.STATE_RUNNING_UNLOCKING; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 46 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 47 | import android.annotation.NonNull; |
Tony Mak | 8c536f9 | 2016-03-21 12:20:41 +0000 | [diff] [blame] | 48 | import android.annotation.UserIdInt; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 49 | import android.app.ActivityManager; |
Fyodor Kupolov | 4ba91b9 | 2017-01-20 18:12:35 -0800 | [diff] [blame] | 50 | import android.app.AppGlobals; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 51 | import android.app.AppOpsManager; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 52 | import android.app.Dialog; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 53 | import android.app.IStopUserCallback; |
| 54 | import android.app.IUserSwitchObserver; |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 55 | import android.app.KeyguardManager; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 56 | import android.content.Context; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 57 | import android.content.IIntentReceiver; |
| 58 | import android.content.Intent; |
Fyodor Kupolov | 4ba91b9 | 2017-01-20 18:12:35 -0800 | [diff] [blame] | 59 | import android.content.pm.IPackageManager; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 60 | import android.content.pm.PackageManager; |
| 61 | import android.content.pm.UserInfo; |
| 62 | import android.os.BatteryStats; |
| 63 | import android.os.Binder; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 64 | import android.os.Build; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 65 | import android.os.Bundle; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 66 | import android.os.Debug; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 67 | import android.os.Handler; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 68 | import android.os.IBinder; |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 69 | import android.os.IProgressListener; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 70 | import android.os.IRemoteCallback; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 71 | import android.os.IUserManager; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 72 | import android.os.Process; |
| 73 | import android.os.RemoteCallbackList; |
| 74 | import android.os.RemoteException; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 75 | import android.os.ServiceManager; |
James Hawkins | 899608a | 2016-05-27 11:15:06 -0700 | [diff] [blame] | 76 | import android.os.SystemClock; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 77 | import android.os.UserHandle; |
| 78 | import android.os.UserManager; |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 79 | import android.os.UserManagerInternal; |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 80 | import android.os.storage.IStorageManager; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 81 | import android.os.storage.StorageManager; |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 82 | import android.util.ArraySet; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 83 | import android.util.IntArray; |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 84 | import android.util.Pair; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 85 | import android.util.Slog; |
| 86 | import android.util.SparseArray; |
| 87 | import android.util.SparseIntArray; |
| 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; |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 96 | import com.android.server.LocalServices; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 97 | import com.android.server.pm.UserManagerService; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 98 | import com.android.server.wm.WindowManagerService; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 99 | |
| 100 | import java.io.PrintWriter; |
| 101 | import java.util.ArrayList; |
| 102 | import java.util.Arrays; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 103 | import java.util.HashSet; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 104 | import java.util.List; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 105 | import java.util.Objects; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 106 | import java.util.Set; |
Fyodor Kupolov | 3864183 | 2016-06-28 17:37:09 -0700 | [diff] [blame] | 107 | import java.util.concurrent.atomic.AtomicInteger; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Helper class for {@link ActivityManagerService} responsible for multi-user functionality. |
| 111 | */ |
| 112 | final class UserController { |
| 113 | private static final String TAG = TAG_WITH_CLASS_NAME ? "UserController" : TAG_AM; |
Jeff Sharkey | fd24108 | 2016-04-19 15:58:24 -0600 | [diff] [blame] | 114 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 115 | // Maximum number of users we allow to be running at a time. |
| 116 | static final int MAX_RUNNING_USERS = 3; |
| 117 | |
| 118 | // Amount of time we wait for observers to handle a user switch before |
| 119 | // giving up on them and unfreezing the screen. |
Fyodor Kupolov | 0e0986e | 2016-09-22 18:01:36 -0700 | [diff] [blame] | 120 | static final int USER_SWITCH_TIMEOUT = 3 * 1000; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 121 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 122 | private final Object mLock; |
| 123 | private final Injector mInjector; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 124 | private final Handler mHandler; |
| 125 | |
| 126 | // Holds the current foreground user's id |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 127 | @GuardedBy("mLock") |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 128 | private int mCurrentUserId = UserHandle.USER_SYSTEM; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 129 | // Holds the target user's id during a user switch |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 130 | @GuardedBy("mLock") |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 131 | private int mTargetUserId = UserHandle.USER_NULL; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Which users have been started, so are allowed to run code. |
| 135 | */ |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 136 | @GuardedBy("mLock") |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 137 | private final SparseArray<UserState> mStartedUsers = new SparseArray<>(); |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 138 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 139 | /** |
| 140 | * LRU list of history of current users. Most recently current is at the end. |
| 141 | */ |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 142 | @GuardedBy("mLock") |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 143 | private final ArrayList<Integer> mUserLru = new ArrayList<>(); |
| 144 | |
| 145 | /** |
| 146 | * Constant array of the users that are currently started. |
| 147 | */ |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 148 | @GuardedBy("mLock") |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 149 | private int[] mStartedUserArray = new int[] { 0 }; |
| 150 | |
| 151 | // If there are multiple profiles for the current user, their ids are here |
| 152 | // Currently only the primary user can have managed profiles |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 153 | @GuardedBy("mLock") |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 154 | private int[] mCurrentProfileIds = new int[] {}; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Mapping from each known user ID to the profile group ID it is associated with. |
| 158 | */ |
| 159 | private final SparseIntArray mUserProfileGroupIdsSelfLocked = new SparseIntArray(); |
| 160 | |
| 161 | /** |
| 162 | * Registered observers of the user switching mechanics. |
| 163 | */ |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 164 | private final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 165 | = new RemoteCallbackList<>(); |
| 166 | |
Evan Rosky | 3e0c25b | 2016-08-09 12:43:33 -0700 | [diff] [blame] | 167 | boolean mUserSwitchUiEnabled = true; |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 168 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 169 | /** |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 170 | * Currently active user switch callbacks. |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 171 | */ |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 172 | @GuardedBy("mLock") |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 173 | private volatile ArraySet<String> mCurWaitingUserSwitchCallbacks; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 174 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 175 | private volatile UserManagerService mUserManager; |
| 176 | |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 177 | private final LockPatternUtils mLockPatternUtils; |
| 178 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 179 | UserController(ActivityManagerService service) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 180 | this(new Injector(service)); |
| 181 | } |
| 182 | |
| 183 | @VisibleForTesting |
| 184 | UserController(Injector injector) { |
| 185 | mInjector = injector; |
| 186 | mLock = injector.getLock(); |
| 187 | mHandler = injector.getHandler(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 188 | // User 0 is the first and only user that runs at boot. |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 189 | final UserState uss = new UserState(UserHandle.SYSTEM); |
| 190 | mStartedUsers.put(UserHandle.USER_SYSTEM, uss); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 191 | mUserLru.add(UserHandle.USER_SYSTEM); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 192 | mLockPatternUtils = mInjector.getLockPatternUtils(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 193 | updateStartedUserArrayLocked(); |
| 194 | } |
| 195 | |
| 196 | void finishUserSwitch(UserState uss) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 197 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 198 | finishUserBoot(uss); |
| 199 | |
| 200 | startProfilesLocked(); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 201 | stopRunningUsersLocked(MAX_RUNNING_USERS); |
| 202 | } |
| 203 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 204 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 205 | void stopRunningUsersLocked(int maxRunningUsers) { |
| 206 | int num = mUserLru.size(); |
| 207 | int i = 0; |
| 208 | while (num > maxRunningUsers && i < mUserLru.size()) { |
| 209 | Integer oldUserId = mUserLru.get(i); |
| 210 | UserState oldUss = mStartedUsers.get(oldUserId); |
| 211 | if (oldUss == null) { |
| 212 | // Shouldn't happen, but be sane if it does. |
| 213 | mUserLru.remove(i); |
| 214 | num--; |
| 215 | continue; |
| 216 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 217 | if (oldUss.state == UserState.STATE_STOPPING |
| 218 | || oldUss.state == UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 219 | // This user is already stopping, doesn't count. |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 220 | num--; |
| 221 | i++; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 222 | continue; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 223 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 224 | if (oldUserId == UserHandle.USER_SYSTEM || oldUserId == mCurrentUserId) { |
| 225 | // Owner/System user and current user can't be stopped. We count it as running |
| 226 | // when it is not a pure system user. |
| 227 | if (UserInfo.isSystemOnly(oldUserId)) { |
| 228 | num--; |
| 229 | } |
| 230 | i++; |
| 231 | continue; |
| 232 | } |
| 233 | // This is a user to be stopped. |
| 234 | if (stopUsersLocked(oldUserId, false, null) != USER_OP_SUCCESS) { |
| 235 | num--; |
| 236 | } |
| 237 | num--; |
| 238 | i++; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Fyodor Kupolov | 2e6acce | 2016-01-28 15:26:52 -0800 | [diff] [blame] | 242 | private void finishUserBoot(UserState uss) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 243 | finishUserBoot(uss, null); |
| 244 | } |
| 245 | |
Fyodor Kupolov | 2e6acce | 2016-01-28 15:26:52 -0800 | [diff] [blame] | 246 | private void finishUserBoot(UserState uss, IIntentReceiver resultTo) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 247 | final int userId = uss.mHandle.getIdentifier(); |
Suprabh Shukla | cc30b0e7 | 2016-05-20 14:41:22 -0700 | [diff] [blame] | 248 | |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 249 | Slog.d(TAG, "Finishing user boot " + userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 250 | synchronized (mLock) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 251 | // Bail if we ended up with a stale user |
| 252 | if (mStartedUsers.get(userId) != uss) return; |
| 253 | |
| 254 | // We always walk through all the user lifecycle states to send |
| 255 | // consistent developer events. We step into RUNNING_LOCKED here, |
| 256 | // but we might immediately step into RUNNING below if the user |
| 257 | // storage is already unlocked. |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 258 | if (uss.setState(STATE_BOOTING, STATE_RUNNING_LOCKED)) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 259 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Fyodor Kupolov | 4ba91b9 | 2017-01-20 18:12:35 -0800 | [diff] [blame] | 260 | // Do not report secondary users, runtime restarts or first boot/upgrade |
| 261 | if (userId == UserHandle.USER_SYSTEM |
| 262 | && !mInjector.isRuntimeRestarted() && !mInjector.isFirstBootOrUpgrade()) { |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 263 | int uptimeSeconds = (int)(SystemClock.elapsedRealtime() / 1000); |
| 264 | MetricsLogger.histogram(mInjector.getContext(), |
| 265 | "framework_locked_boot_completed", uptimeSeconds); |
Fyodor Kupolov | 24e12dc | 2017-01-17 12:20:36 -0800 | [diff] [blame] | 266 | final int MAX_UPTIME_SECONDS = 120; |
| 267 | if (uptimeSeconds > MAX_UPTIME_SECONDS) { |
| 268 | Slog.wtf("SystemServerTiming", |
| 269 | "finishUserBoot took too long. uptimeSeconds=" + uptimeSeconds); |
| 270 | } |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 271 | } |
James Hawkins | 899608a | 2016-05-27 11:15:06 -0700 | [diff] [blame] | 272 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 273 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_LOCKED_BOOT_COMPLETE_MSG, |
| 274 | userId, 0)); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 275 | Intent intent = new Intent(Intent.ACTION_LOCKED_BOOT_COMPLETED, null); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 276 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Jeff Sharkey | a242f82 | 2015-12-17 15:38:20 -0700 | [diff] [blame] | 277 | intent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT |
| 278 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 279 | mInjector.broadcastIntentLocked(intent, null, resultTo, 0, null, null, |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 280 | new String[] { android.Manifest.permission.RECEIVE_BOOT_COMPLETED }, |
| 281 | AppOpsManager.OP_NONE, null, true, false, MY_PID, SYSTEM_UID, userId); |
| 282 | } |
| 283 | |
Jeff Sharkey | 7661a31 | 2016-04-07 12:39:57 -0600 | [diff] [blame] | 284 | // We need to delay unlocking managed profiles until the parent user |
| 285 | // is also unlocked. |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 286 | if (mInjector.getUserManager().isManagedProfile(userId)) { |
| 287 | final UserInfo parent = mInjector.getUserManager().getProfileParent(userId); |
Jeff Sharkey | 7661a31 | 2016-04-07 12:39:57 -0600 | [diff] [blame] | 288 | if (parent != null |
| 289 | && isUserRunningLocked(parent.id, ActivityManager.FLAG_AND_UNLOCKED)) { |
| 290 | Slog.d(TAG, "User " + userId + " (parent " + parent.id |
| 291 | + "): attempting unlock because parent is unlocked"); |
| 292 | maybeUnlockUser(userId); |
| 293 | } else { |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 294 | String parentId = (parent == null) ? "<null>" : String.valueOf(parent.id); |
| 295 | Slog.d(TAG, "User " + userId + " (parent " + parentId |
Jeff Sharkey | 7661a31 | 2016-04-07 12:39:57 -0600 | [diff] [blame] | 296 | + "): delaying unlock because parent is locked"); |
| 297 | } |
Jeff Sharkey | 5dab713 | 2016-04-07 01:20:58 -0600 | [diff] [blame] | 298 | } else { |
| 299 | maybeUnlockUser(userId); |
| 300 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 305 | * Step from {@link UserState#STATE_RUNNING_LOCKED} to |
| 306 | * {@link UserState#STATE_RUNNING_UNLOCKING}. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 307 | */ |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 308 | private void finishUserUnlocking(final UserState uss) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 309 | final int userId = uss.mHandle.getIdentifier(); |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 310 | boolean proceedWithUnlock = false; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 311 | synchronized (mLock) { |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 312 | // Bail if we ended up with a stale user |
| 313 | if (mStartedUsers.get(uss.mHandle.getIdentifier()) != uss) return; |
| 314 | |
| 315 | // Only keep marching forward if user is actually unlocked |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 316 | if (!StorageManager.isUserKeyUnlocked(userId)) return; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 317 | |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 318 | if (uss.setState(STATE_RUNNING_LOCKED, STATE_RUNNING_UNLOCKING)) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 319 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 320 | proceedWithUnlock = true; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 321 | } |
| 322 | } |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 323 | |
| 324 | if (proceedWithUnlock) { |
| 325 | uss.mUnlockProgress.start(); |
| 326 | |
| 327 | // Prepare app storage before we go any further |
| 328 | uss.mUnlockProgress.setProgress(5, |
Jorim Jaggi | 526694e | 2016-08-03 17:15:23 +0200 | [diff] [blame] | 329 | mInjector.getContext().getString(R.string.android_start_title)); |
| 330 | mInjector.getUserManager().onBeforeUnlockUser(userId); |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 331 | uss.mUnlockProgress.setProgress(20); |
| 332 | |
| 333 | // Dispatch unlocked to system services; when fully dispatched, |
| 334 | // that calls through to the next "unlocked" phase |
| 335 | mHandler.obtainMessage(SYSTEM_USER_UNLOCK_MSG, userId, 0, uss) |
| 336 | .sendToTarget(); |
| 337 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Step from {@link UserState#STATE_RUNNING_UNLOCKING} to |
| 342 | * {@link UserState#STATE_RUNNING_UNLOCKED}. |
| 343 | */ |
| 344 | void finishUserUnlocked(final UserState uss) { |
| 345 | final int userId = uss.mHandle.getIdentifier(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 346 | synchronized (mLock) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 347 | // Bail if we ended up with a stale user |
| 348 | if (mStartedUsers.get(uss.mHandle.getIdentifier()) != uss) return; |
| 349 | |
| 350 | // Only keep marching forward if user is actually unlocked |
| 351 | if (!StorageManager.isUserKeyUnlocked(userId)) return; |
| 352 | |
| 353 | if (uss.setState(STATE_RUNNING_UNLOCKING, STATE_RUNNING_UNLOCKED)) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 354 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 355 | uss.mUnlockProgress.finish(); |
Jeff Sharkey | 083807d | 2016-04-06 14:07:09 -0600 | [diff] [blame] | 356 | |
Jeff Sharkey | 155e5d6 | 2016-04-11 12:13:06 -0600 | [diff] [blame] | 357 | // Dispatch unlocked to external apps |
| 358 | final Intent unlockedIntent = new Intent(Intent.ACTION_USER_UNLOCKED); |
| 359 | unlockedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 360 | unlockedIntent.addFlags( |
| 361 | Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 362 | mInjector.broadcastIntentLocked(unlockedIntent, null, null, 0, null, |
Jeff Sharkey | 155e5d6 | 2016-04-11 12:13:06 -0600 | [diff] [blame] | 363 | null, null, AppOpsManager.OP_NONE, null, false, false, MY_PID, SYSTEM_UID, |
| 364 | userId); |
| 365 | |
| 366 | if (getUserInfo(userId).isManagedProfile()) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 367 | UserInfo parent = mInjector.getUserManager().getProfileParent(userId); |
Jeff Sharkey | 155e5d6 | 2016-04-11 12:13:06 -0600 | [diff] [blame] | 368 | if (parent != null) { |
| 369 | final Intent profileUnlockedIntent = new Intent( |
| 370 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED); |
| 371 | profileUnlockedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(userId)); |
| 372 | profileUnlockedIntent.addFlags( |
| 373 | Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 374 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 375 | mInjector.broadcastIntentLocked(profileUnlockedIntent, |
Jeff Sharkey | 155e5d6 | 2016-04-11 12:13:06 -0600 | [diff] [blame] | 376 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 377 | null, false, false, MY_PID, SYSTEM_UID, |
| 378 | parent.id); |
| 379 | } |
| 380 | } |
| 381 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 382 | // Send PRE_BOOT broadcasts if user fingerprint changed; we |
| 383 | // purposefully block sending BOOT_COMPLETED until after all |
| 384 | // PRE_BOOT receivers are finished to avoid ANR'ing apps |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 385 | final UserInfo info = getUserInfo(userId); |
| 386 | if (!Objects.equals(info.lastLoggedInFingerprint, Build.FINGERPRINT)) { |
Jeff Sharkey | 24d9491 | 2016-07-07 12:33:48 -0600 | [diff] [blame] | 387 | // Suppress double notifications for managed profiles that |
Jeff Sharkey | ba06657 | 2016-07-18 10:11:36 -0600 | [diff] [blame] | 388 | // were unlocked automatically as part of their parent user |
| 389 | // being unlocked. |
| 390 | final boolean quiet; |
| 391 | if (info.isManagedProfile()) { |
| 392 | quiet = !uss.tokenProvided |
| 393 | || !mLockPatternUtils.isSeparateProfileChallengeEnabled(userId); |
| 394 | } else { |
| 395 | quiet = false; |
| 396 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 397 | mInjector.sendPreBootBroadcast(userId, quiet, |
| 398 | () -> finishUserUnlockedCompleted(uss)); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 399 | } else { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 400 | finishUserUnlockedCompleted(uss); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 406 | private void finishUserUnlockedCompleted(UserState uss) { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 407 | final int userId = uss.mHandle.getIdentifier(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 408 | synchronized (mLock) { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 409 | // Bail if we ended up with a stale user |
| 410 | if (mStartedUsers.get(uss.mHandle.getIdentifier()) != uss) return; |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 411 | final UserInfo userInfo = getUserInfo(userId); |
| 412 | if (userInfo == null) { |
| 413 | return; |
| 414 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 415 | |
| 416 | // Only keep marching forward if user is actually unlocked |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 417 | if (!StorageManager.isUserKeyUnlocked(userId)) return; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 418 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 419 | // Remember that we logged in |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 420 | mInjector.getUserManager().onUserLoggedIn(userId); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 421 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 422 | if (!userInfo.isInitialized()) { |
| 423 | if (userId != UserHandle.USER_SYSTEM) { |
| 424 | Slog.d(TAG, "Initializing user #" + userId); |
| 425 | Intent intent = new Intent(Intent.ACTION_USER_INITIALIZE); |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 426 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND |
| 427 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 428 | mInjector.broadcastIntentLocked(intent, null, |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 429 | new IIntentReceiver.Stub() { |
| 430 | @Override |
| 431 | public void performReceive(Intent intent, int resultCode, |
| 432 | String data, Bundle extras, boolean ordered, |
| 433 | boolean sticky, int sendingUser) { |
| 434 | // Note: performReceive is called with mService lock held |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 435 | mInjector.getUserManager().makeInitialized(userInfo.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 436 | } |
| 437 | }, 0, null, null, null, AppOpsManager.OP_NONE, |
| 438 | null, true, false, MY_PID, SYSTEM_UID, userId); |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 439 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 440 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 441 | |
| 442 | Slog.d(TAG, "Sending BOOT_COMPLETE user #" + userId); |
Fyodor Kupolov | 4ba91b9 | 2017-01-20 18:12:35 -0800 | [diff] [blame] | 443 | // Do not report secondary users, runtime restarts or first boot/upgrade |
| 444 | if (userId == UserHandle.USER_SYSTEM |
| 445 | && !mInjector.isRuntimeRestarted() && !mInjector.isFirstBootOrUpgrade()) { |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 446 | int uptimeSeconds = (int) (SystemClock.elapsedRealtime() / 1000); |
| 447 | MetricsLogger.histogram(mInjector.getContext(), "framework_boot_completed", |
| 448 | uptimeSeconds); |
| 449 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 450 | final Intent bootIntent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 451 | bootIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 452 | bootIntent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT |
| 453 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 454 | mInjector.broadcastIntentLocked(bootIntent, null, null, 0, null, null, |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 455 | new String[] { android.Manifest.permission.RECEIVE_BOOT_COMPLETED }, |
| 456 | AppOpsManager.OP_NONE, null, true, false, MY_PID, SYSTEM_UID, userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 460 | int restartUser(final int userId, final boolean foreground) { |
| 461 | return stopUser(userId, /* force */ true, new IStopUserCallback.Stub() { |
| 462 | @Override |
| 463 | public void userStopped(final int userId) { |
| 464 | // Post to the same handler that this callback is called from to ensure the user |
| 465 | // cleanup is complete before restarting. |
| 466 | mHandler.post(() -> startUser(userId, foreground)); |
| 467 | } |
| 468 | @Override |
| 469 | public void userStopAborted(final int userId) {} |
| 470 | }); |
| 471 | } |
| 472 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 473 | int stopUser(final int userId, final boolean force, final IStopUserCallback callback) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 474 | if (mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 475 | != PackageManager.PERMISSION_GRANTED) { |
| 476 | String msg = "Permission Denial: switchUser() from pid=" |
| 477 | + Binder.getCallingPid() |
| 478 | + ", uid=" + Binder.getCallingUid() |
| 479 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 480 | Slog.w(TAG, msg); |
| 481 | throw new SecurityException(msg); |
| 482 | } |
| 483 | if (userId < 0 || userId == UserHandle.USER_SYSTEM) { |
| 484 | throw new IllegalArgumentException("Can't stop system user " + userId); |
| 485 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 486 | mInjector.enforceShellRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, userId); |
| 487 | synchronized (mLock) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 488 | return stopUsersLocked(userId, force, callback); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 492 | /** |
| 493 | * Stops the user along with its related users. The method calls |
| 494 | * {@link #getUsersToStopLocked(int)} to determine the list of users that should be stopped. |
| 495 | */ |
| 496 | private int stopUsersLocked(final int userId, boolean force, final IStopUserCallback callback) { |
| 497 | if (userId == UserHandle.USER_SYSTEM) { |
| 498 | return USER_OP_ERROR_IS_SYSTEM; |
| 499 | } |
| 500 | if (isCurrentUserLocked(userId)) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 501 | return USER_OP_IS_CURRENT; |
| 502 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 503 | int[] usersToStop = getUsersToStopLocked(userId); |
| 504 | // If one of related users is system or current, no related users should be stopped |
| 505 | for (int i = 0; i < usersToStop.length; i++) { |
| 506 | int relatedUserId = usersToStop[i]; |
| 507 | if ((UserHandle.USER_SYSTEM == relatedUserId) || isCurrentUserLocked(relatedUserId)) { |
| 508 | if (DEBUG_MU) Slog.i(TAG, "stopUsersLocked cannot stop related user " |
| 509 | + relatedUserId); |
| 510 | // We still need to stop the requested user if it's a force stop. |
| 511 | if (force) { |
Fyodor Kupolov | 7b4a8a4 | 2016-01-04 12:47:22 -0800 | [diff] [blame] | 512 | Slog.i(TAG, |
| 513 | "Force stop user " + userId + ". Related users will not be stopped"); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 514 | stopSingleUserLocked(userId, callback); |
Fyodor Kupolov | 7b4a8a4 | 2016-01-04 12:47:22 -0800 | [diff] [blame] | 515 | return USER_OP_SUCCESS; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 516 | } |
| 517 | return USER_OP_ERROR_RELATED_USERS_CANNOT_STOP; |
| 518 | } |
| 519 | } |
| 520 | if (DEBUG_MU) Slog.i(TAG, "stopUsersLocked usersToStop=" + Arrays.toString(usersToStop)); |
| 521 | for (int userIdToStop : usersToStop) { |
| 522 | stopSingleUserLocked(userIdToStop, userIdToStop == userId ? callback : null); |
| 523 | } |
| 524 | return USER_OP_SUCCESS; |
| 525 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 526 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 527 | private void stopSingleUserLocked(final int userId, final IStopUserCallback callback) { |
| 528 | if (DEBUG_MU) Slog.i(TAG, "stopSingleUserLocked userId=" + userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 529 | final UserState uss = mStartedUsers.get(userId); |
| 530 | if (uss == null) { |
| 531 | // User is not started, nothing to do... but we do need to |
| 532 | // callback if requested. |
| 533 | if (callback != null) { |
| 534 | mHandler.post(new Runnable() { |
| 535 | @Override |
| 536 | public void run() { |
| 537 | try { |
| 538 | callback.userStopped(userId); |
| 539 | } catch (RemoteException e) { |
| 540 | } |
| 541 | } |
| 542 | }); |
| 543 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 544 | return; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | if (callback != null) { |
| 548 | uss.mStopCallbacks.add(callback); |
| 549 | } |
| 550 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 551 | if (uss.state != UserState.STATE_STOPPING |
| 552 | && uss.state != UserState.STATE_SHUTDOWN) { |
| 553 | uss.setState(UserState.STATE_STOPPING); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 554 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 555 | updateStartedUserArrayLocked(); |
| 556 | |
| 557 | long ident = Binder.clearCallingIdentity(); |
| 558 | try { |
| 559 | // We are going to broadcast ACTION_USER_STOPPING and then |
| 560 | // once that is done send a final ACTION_SHUTDOWN and then |
| 561 | // stop the user. |
| 562 | final Intent stoppingIntent = new Intent(Intent.ACTION_USER_STOPPING); |
| 563 | stoppingIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 564 | stoppingIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 565 | stoppingIntent.putExtra(Intent.EXTRA_SHUTDOWN_USERSPACE_ONLY, true); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 566 | // This is the result receiver for the initial stopping broadcast. |
| 567 | final IIntentReceiver stoppingReceiver = new IIntentReceiver.Stub() { |
| 568 | @Override |
| 569 | public void performReceive(Intent intent, int resultCode, String data, |
| 570 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 571 | mHandler.post(new Runnable() { |
| 572 | @Override |
| 573 | public void run() { |
| 574 | finishUserStopping(userId, uss); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 575 | } |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 576 | }); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 577 | } |
| 578 | }; |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 579 | // Clear broadcast queue for the user to avoid delivering stale broadcasts |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 580 | mInjector.clearBroadcastQueueForUserLocked(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 581 | // Kick things off. |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 582 | mInjector.broadcastIntentLocked(stoppingIntent, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 583 | null, stoppingReceiver, 0, null, null, |
| 584 | new String[]{INTERACT_ACROSS_USERS}, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 585 | null, true, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 586 | } finally { |
| 587 | Binder.restoreCallingIdentity(ident); |
| 588 | } |
| 589 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 592 | void finishUserStopping(final int userId, final UserState uss) { |
| 593 | // On to the next. |
| 594 | final Intent shutdownIntent = new Intent(Intent.ACTION_SHUTDOWN); |
| 595 | // This is the result receiver for the final shutdown broadcast. |
| 596 | final IIntentReceiver shutdownReceiver = new IIntentReceiver.Stub() { |
| 597 | @Override |
| 598 | public void performReceive(Intent intent, int resultCode, String data, |
| 599 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 600 | mHandler.post(new Runnable() { |
| 601 | @Override |
| 602 | public void run() { |
| 603 | finishUserStopped(uss); |
| 604 | } |
| 605 | }); |
| 606 | } |
| 607 | }; |
| 608 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 609 | synchronized (mLock) { |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 610 | if (uss.state != UserState.STATE_STOPPING) { |
| 611 | // Whoops, we are being started back up. Abort, abort! |
| 612 | return; |
| 613 | } |
| 614 | uss.setState(UserState.STATE_SHUTDOWN); |
| 615 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 616 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 617 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 618 | mInjector.batteryStatsServiceNoteEvent( |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 619 | BatteryStats.HistoryItem.EVENT_USER_RUNNING_FINISH, |
| 620 | Integer.toString(userId), userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 621 | mInjector.systemServiceManagerStopUser(userId); |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 622 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 623 | synchronized (mLock) { |
| 624 | mInjector.broadcastIntentLocked(shutdownIntent, |
Amith Yamasani | 98c0556 | 2016-03-30 13:15:26 -0700 | [diff] [blame] | 625 | null, shutdownReceiver, 0, null, null, null, |
| 626 | AppOpsManager.OP_NONE, |
| 627 | null, true, false, MY_PID, SYSTEM_UID, userId); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void finishUserStopped(UserState uss) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 632 | final int userId = uss.mHandle.getIdentifier(); |
| 633 | boolean stopped; |
| 634 | ArrayList<IStopUserCallback> callbacks; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 635 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 636 | callbacks = new ArrayList<>(uss.mStopCallbacks); |
| 637 | if (mStartedUsers.get(userId) != uss) { |
| 638 | stopped = false; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 639 | } else if (uss.state != UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 640 | stopped = false; |
| 641 | } else { |
| 642 | stopped = true; |
| 643 | // User can no longer run. |
| 644 | mStartedUsers.remove(userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 645 | mInjector.getUserManagerInternal().removeUserState(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 646 | mUserLru.remove(Integer.valueOf(userId)); |
| 647 | updateStartedUserArrayLocked(); |
| 648 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 649 | mInjector.activityManagerOnUserStopped(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 650 | // Clean up all state and processes associated with the user. |
| 651 | // Kill all the processes for the user. |
| 652 | forceStopUserLocked(userId, "finish user"); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | for (int i = 0; i < callbacks.size(); i++) { |
| 657 | try { |
| 658 | if (stopped) callbacks.get(i).userStopped(userId); |
| 659 | else callbacks.get(i).userStopAborted(userId); |
| 660 | } catch (RemoteException e) { |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | if (stopped) { |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 665 | // Evict the user's credential encryption key |
| 666 | try { |
| 667 | getStorageManager().lockUserKey(userId); |
| 668 | } catch (RemoteException re) { |
| 669 | throw re.rethrowAsRuntimeException(); |
| 670 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 671 | mInjector.systemServiceManagerCleanupUser(userId); |
| 672 | synchronized (mLock) { |
Tony Mak | 5c2cf03 | 2017-04-03 18:38:23 +0100 | [diff] [blame] | 673 | mInjector.getActivityStackSupervisor().removeUserLocked(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 674 | } |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 675 | // Remove the user if it is ephemeral. |
| 676 | if (getUserInfo(userId).isEphemeral()) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 677 | mInjector.getUserManager().removeUser(userId); |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 678 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 682 | /** |
| 683 | * Determines the list of users that should be stopped together with the specified |
| 684 | * {@code userId}. The returned list includes {@code userId}. |
| 685 | */ |
| 686 | private @NonNull int[] getUsersToStopLocked(int userId) { |
| 687 | int startedUsersSize = mStartedUsers.size(); |
| 688 | IntArray userIds = new IntArray(); |
| 689 | userIds.add(userId); |
| 690 | synchronized (mUserProfileGroupIdsSelfLocked) { |
| 691 | int userGroupId = mUserProfileGroupIdsSelfLocked.get(userId, |
| 692 | UserInfo.NO_PROFILE_GROUP_ID); |
| 693 | for (int i = 0; i < startedUsersSize; i++) { |
| 694 | UserState uss = mStartedUsers.valueAt(i); |
| 695 | int startedUserId = uss.mHandle.getIdentifier(); |
| 696 | // Skip unrelated users (profileGroupId mismatch) |
| 697 | int startedUserGroupId = mUserProfileGroupIdsSelfLocked.get(startedUserId, |
| 698 | UserInfo.NO_PROFILE_GROUP_ID); |
| 699 | boolean sameGroup = (userGroupId != UserInfo.NO_PROFILE_GROUP_ID) |
| 700 | && (userGroupId == startedUserGroupId); |
| 701 | // userId has already been added |
| 702 | boolean sameUserId = startedUserId == userId; |
| 703 | if (!sameGroup || sameUserId) { |
| 704 | continue; |
| 705 | } |
| 706 | userIds.add(startedUserId); |
| 707 | } |
| 708 | } |
| 709 | return userIds.toArray(); |
| 710 | } |
| 711 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 712 | private void forceStopUserLocked(int userId, String reason) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 713 | mInjector.activityManagerForceStopPackageLocked(userId, reason); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 714 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
| 715 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 716 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 717 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 718 | mInjector.broadcastIntentLocked(intent, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 719 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 720 | null, false, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 723 | /** |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 724 | * 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] | 725 | */ |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 726 | private void stopGuestOrEphemeralUserIfBackground() { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 727 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 728 | final int num = mUserLru.size(); |
| 729 | for (int i = 0; i < num; i++) { |
| 730 | Integer oldUserId = mUserLru.get(i); |
| 731 | UserState oldUss = mStartedUsers.get(oldUserId); |
| 732 | if (oldUserId == UserHandle.USER_SYSTEM || oldUserId == mCurrentUserId |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 733 | || oldUss.state == UserState.STATE_STOPPING |
| 734 | || oldUss.state == UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 735 | continue; |
| 736 | } |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 737 | UserInfo userInfo = getUserInfo(oldUserId); |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 738 | if (userInfo.isEphemeral()) { |
| 739 | LocalServices.getService(UserManagerInternal.class) |
| 740 | .onEphemeralUserStop(oldUserId); |
| 741 | } |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 742 | if (userInfo.isGuest() || userInfo.isEphemeral()) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 743 | // This is a user to be stopped. |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 744 | stopUsersLocked(oldUserId, true, null); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | void startProfilesLocked() { |
| 752 | if (DEBUG_MU) Slog.i(TAG, "startProfilesLocked"); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 753 | List<UserInfo> profiles = mInjector.getUserManager().getProfiles( |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 754 | mCurrentUserId, false /* enabledOnly */); |
| 755 | List<UserInfo> profilesToStart = new ArrayList<>(profiles.size()); |
| 756 | for (UserInfo user : profiles) { |
| 757 | if ((user.flags & UserInfo.FLAG_INITIALIZED) == UserInfo.FLAG_INITIALIZED |
Rubin Xu | f13c980 | 2016-01-21 18:06:00 +0000 | [diff] [blame] | 758 | && user.id != mCurrentUserId && !user.isQuietModeEnabled()) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 759 | profilesToStart.add(user); |
| 760 | } |
| 761 | } |
| 762 | final int profilesToStartSize = profilesToStart.size(); |
| 763 | int i = 0; |
| 764 | for (; i < profilesToStartSize && i < (MAX_RUNNING_USERS - 1); ++i) { |
| 765 | startUser(profilesToStart.get(i).id, /* foreground= */ false); |
| 766 | } |
| 767 | if (i < profilesToStartSize) { |
| 768 | Slog.w(TAG, "More profiles than MAX_RUNNING_USERS"); |
| 769 | } |
| 770 | } |
| 771 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 772 | private IStorageManager getStorageManager() { |
| 773 | return IStorageManager.Stub.asInterface(ServiceManager.getService("mount")); |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 776 | /** |
| 777 | * Start user, if its not already running. |
| 778 | * <p>The user will be brought to the foreground, if {@code foreground} parameter is set. |
| 779 | * When starting the user, multiple intents will be broadcast in the following order:</p> |
| 780 | * <ul> |
| 781 | * <li>{@link Intent#ACTION_USER_STARTED} - sent to registered receivers of the new user |
| 782 | * <li>{@link Intent#ACTION_USER_BACKGROUND} - sent to registered receivers of the outgoing |
| 783 | * user and all profiles of this user. Sent only if {@code foreground} parameter is true |
| 784 | * <li>{@link Intent#ACTION_USER_FOREGROUND} - sent to registered receivers of the new |
| 785 | * user and all profiles of this user. Sent only if {@code foreground} parameter is true |
| 786 | * <li>{@link Intent#ACTION_USER_SWITCHED} - sent to registered receivers of the new user. |
| 787 | * Sent only if {@code foreground} parameter is true |
| 788 | * <li>{@link Intent#ACTION_USER_STARTING} - ordered broadcast sent to registered receivers |
| 789 | * of the new fg user |
| 790 | * <li>{@link Intent#ACTION_LOCKED_BOOT_COMPLETED} - ordered broadcast sent to receivers of |
| 791 | * the new user |
| 792 | * <li>{@link Intent#ACTION_USER_UNLOCKED} - sent to registered receivers of the new user |
| 793 | * <li>{@link Intent#ACTION_PRE_BOOT_COMPLETED} - ordered broadcast sent to receivers of the |
| 794 | * new user. Sent only when the user is booting after a system update. |
| 795 | * <li>{@link Intent#ACTION_USER_INITIALIZE} - ordered broadcast sent to receivers of the |
| 796 | * new user. Sent only the first time a user is starting. |
| 797 | * <li>{@link Intent#ACTION_BOOT_COMPLETED} - ordered broadcast sent to receivers of the new |
| 798 | * user. Indicates that the user has finished booting. |
| 799 | * </ul> |
| 800 | * |
| 801 | * @param userId ID of the user to start |
| 802 | * @param foreground true if user should be brought to the foreground |
| 803 | * @return true if the user has been successfully started |
| 804 | */ |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 805 | boolean startUser(final int userId, final boolean foreground) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 806 | if (mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 807 | != PackageManager.PERMISSION_GRANTED) { |
| 808 | String msg = "Permission Denial: switchUser() from pid=" |
| 809 | + Binder.getCallingPid() |
| 810 | + ", uid=" + Binder.getCallingUid() |
| 811 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 812 | Slog.w(TAG, msg); |
| 813 | throw new SecurityException(msg); |
| 814 | } |
| 815 | |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 816 | Slog.i(TAG, "Starting userid:" + userId + " fg:" + foreground); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 817 | |
| 818 | final long ident = Binder.clearCallingIdentity(); |
| 819 | try { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 820 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 821 | final int oldUserId = mCurrentUserId; |
| 822 | if (oldUserId == userId) { |
| 823 | return true; |
| 824 | } |
| 825 | |
Tony Mak | 5c2cf03 | 2017-04-03 18:38:23 +0100 | [diff] [blame] | 826 | if (foreground) { |
| 827 | mInjector.getActivityStackSupervisor().setLockTaskModeLocked( |
| 828 | null, ActivityManager.LOCK_TASK_MODE_NONE, "startUser", false); |
| 829 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 830 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 831 | final UserInfo userInfo = getUserInfo(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 832 | if (userInfo == null) { |
| 833 | Slog.w(TAG, "No user info for user #" + userId); |
| 834 | return false; |
| 835 | } |
| 836 | if (foreground && userInfo.isManagedProfile()) { |
| 837 | Slog.w(TAG, "Cannot switch to User #" + userId + ": not a full user"); |
| 838 | return false; |
| 839 | } |
| 840 | |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 841 | if (foreground && mUserSwitchUiEnabled) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 842 | mInjector.getWindowManager().startFreezingScreen( |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 843 | R.anim.screen_user_exit, R.anim.screen_user_enter); |
| 844 | } |
| 845 | |
| 846 | boolean needStart = false; |
| 847 | |
| 848 | // If the user we are switching to is not currently started, then |
| 849 | // we need to start it now. |
| 850 | if (mStartedUsers.get(userId) == null) { |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 851 | UserState userState = new UserState(UserHandle.of(userId)); |
| 852 | mStartedUsers.put(userId, userState); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 853 | mInjector.getUserManagerInternal().setUserState(userId, userState.state); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 854 | updateStartedUserArrayLocked(); |
| 855 | needStart = true; |
| 856 | } |
| 857 | |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 858 | final UserState uss = mStartedUsers.get(userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 859 | final Integer userIdInt = userId; |
| 860 | mUserLru.remove(userIdInt); |
| 861 | mUserLru.add(userIdInt); |
| 862 | |
| 863 | if (foreground) { |
| 864 | mCurrentUserId = userId; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 865 | mInjector.updateUserConfigurationLocked(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 866 | mTargetUserId = UserHandle.USER_NULL; // reset, mCurrentUserId has caught up |
| 867 | updateCurrentProfileIdsLocked(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 868 | mInjector.getWindowManager().setCurrentUser(userId, mCurrentProfileIds); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 869 | // Once the internal notion of the active user has switched, we lock the device |
| 870 | // with the option to show the user switcher on the keyguard. |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 871 | if (mUserSwitchUiEnabled) { |
| 872 | mInjector.getWindowManager().setSwitchingUser(true); |
| 873 | mInjector.getWindowManager().lockNow(null); |
| 874 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 875 | } else { |
| 876 | final Integer currentUserIdInt = mCurrentUserId; |
| 877 | updateCurrentProfileIdsLocked(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 878 | mInjector.getWindowManager().setCurrentProfileIds(mCurrentProfileIds); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 879 | mUserLru.remove(currentUserIdInt); |
| 880 | mUserLru.add(currentUserIdInt); |
| 881 | } |
| 882 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 883 | // Make sure user is in the started state. If it is currently |
| 884 | // stopping, we need to knock that off. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 885 | if (uss.state == UserState.STATE_STOPPING) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 886 | // If we are stopping, we haven't sent ACTION_SHUTDOWN, |
| 887 | // so we can just fairly silently bring the user back from |
| 888 | // the almost-dead. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 889 | uss.setState(uss.lastState); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 890 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 891 | updateStartedUserArrayLocked(); |
| 892 | needStart = true; |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 893 | } else if (uss.state == UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 894 | // This means ACTION_SHUTDOWN has been sent, so we will |
| 895 | // need to treat this as a new boot of the user. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 896 | uss.setState(UserState.STATE_BOOTING); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 897 | mInjector.getUserManagerInternal().setUserState(userId, uss.state); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 898 | updateStartedUserArrayLocked(); |
| 899 | needStart = true; |
| 900 | } |
| 901 | |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 902 | if (uss.state == UserState.STATE_BOOTING) { |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 903 | // Give user manager a chance to propagate user restrictions |
| 904 | // to other services and prepare app storage |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 905 | mInjector.getUserManager().onBeforeStartUser(userId); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 906 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 907 | // Booting up a new user, need to tell system services about it. |
| 908 | // Note that this is on the same handler as scheduling of broadcasts, |
| 909 | // which is important because it needs to go first. |
| 910 | mHandler.sendMessage(mHandler.obtainMessage(SYSTEM_USER_START_MSG, userId, 0)); |
| 911 | } |
| 912 | |
| 913 | if (foreground) { |
| 914 | mHandler.sendMessage(mHandler.obtainMessage(SYSTEM_USER_CURRENT_MSG, userId, |
| 915 | oldUserId)); |
| 916 | mHandler.removeMessages(REPORT_USER_SWITCH_MSG); |
| 917 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 918 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG, |
| 919 | oldUserId, userId, uss)); |
| 920 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG, |
| 921 | oldUserId, userId, uss), USER_SWITCH_TIMEOUT); |
| 922 | } |
| 923 | |
| 924 | if (needStart) { |
| 925 | // Send USER_STARTED broadcast |
| 926 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
| 927 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 928 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 929 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 930 | mInjector.broadcastIntentLocked(intent, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 931 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 932 | null, false, false, MY_PID, SYSTEM_UID, userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 935 | if (foreground) { |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 936 | moveUserToForegroundLocked(uss, oldUserId, userId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 937 | } else { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 938 | finishUserBoot(uss); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | if (needStart) { |
| 942 | Intent intent = new Intent(Intent.ACTION_USER_STARTING); |
| 943 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 944 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 945 | mInjector.broadcastIntentLocked(intent, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 946 | null, new IIntentReceiver.Stub() { |
| 947 | @Override |
| 948 | public void performReceive(Intent intent, int resultCode, |
| 949 | String data, Bundle extras, boolean ordered, boolean sticky, |
| 950 | int sendingUser) throws RemoteException { |
| 951 | } |
| 952 | }, 0, null, null, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 953 | new String[] {INTERACT_ACROSS_USERS}, AppOpsManager.OP_NONE, |
| 954 | null, true, false, MY_PID, SYSTEM_UID, UserHandle.USER_ALL); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | } finally { |
| 958 | Binder.restoreCallingIdentity(ident); |
| 959 | } |
| 960 | |
| 961 | return true; |
| 962 | } |
| 963 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 964 | /** |
| 965 | * Start user, if its not already running, and bring it to foreground. |
| 966 | */ |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 967 | void startUserInForeground(final int targetUserId) { |
| 968 | boolean success = startUser(targetUserId, /* foreground */ true); |
| 969 | if (!success) { |
| 970 | mInjector.getWindowManager().setSwitchingUser(false); |
| 971 | } |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 974 | boolean unlockUser(final int userId, byte[] token, byte[] secret, IProgressListener listener) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 975 | if (mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 976 | != PackageManager.PERMISSION_GRANTED) { |
| 977 | String msg = "Permission Denial: unlockUser() from pid=" |
| 978 | + Binder.getCallingPid() |
| 979 | + ", uid=" + Binder.getCallingUid() |
| 980 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 981 | Slog.w(TAG, msg); |
| 982 | throw new SecurityException(msg); |
| 983 | } |
| 984 | |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 985 | final long binderToken = Binder.clearCallingIdentity(); |
| 986 | try { |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 987 | return unlockUserCleared(userId, token, secret, listener); |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 988 | } finally { |
| 989 | Binder.restoreCallingIdentity(binderToken); |
| 990 | } |
| 991 | } |
| 992 | |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 993 | /** |
| 994 | * Attempt to unlock user without a credential token. This typically |
| 995 | * succeeds when the device doesn't have credential-encrypted storage, or |
| 996 | * when the the credential-encrypted storage isn't tied to a user-provided |
| 997 | * PIN or pattern. |
| 998 | */ |
| 999 | boolean maybeUnlockUser(final int userId) { |
| 1000 | // Try unlocking storage using empty token |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1001 | return unlockUserCleared(userId, null, null, null); |
| 1002 | } |
| 1003 | |
| 1004 | private static void notifyFinished(int userId, IProgressListener listener) { |
| 1005 | if (listener == null) return; |
| 1006 | try { |
| 1007 | listener.onFinished(userId, null); |
| 1008 | } catch (RemoteException ignored) { |
| 1009 | } |
Jeff Sharkey | b9fe537 | 2015-12-03 15:23:08 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1012 | boolean unlockUserCleared(final int userId, byte[] token, byte[] secret, |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1013 | IProgressListener listener) { |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1014 | UserState uss; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1015 | synchronized (mLock) { |
Ricky Wai | 4266fee | 2016-05-23 15:33:04 +0100 | [diff] [blame] | 1016 | // TODO Move this block outside of synchronized if it causes lock contention |
| 1017 | if (!StorageManager.isUserKeyUnlocked(userId)) { |
| 1018 | final UserInfo userInfo = getUserInfo(userId); |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1019 | final IStorageManager storageManager = getStorageManager(); |
Ricky Wai | 4266fee | 2016-05-23 15:33:04 +0100 | [diff] [blame] | 1020 | try { |
| 1021 | // We always want to unlock user storage, even user is not started yet |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 1022 | storageManager.unlockUserKey(userId, userInfo.serialNumber, token, secret); |
Ricky Wai | 4266fee | 2016-05-23 15:33:04 +0100 | [diff] [blame] | 1023 | } catch (RemoteException | RuntimeException e) { |
| 1024 | Slog.w(TAG, "Failed to unlock: " + e.getMessage()); |
| 1025 | } |
| 1026 | } |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1027 | // Bail if user isn't actually running, otherwise register the given |
| 1028 | // listener to watch for unlock progress |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1029 | uss = mStartedUsers.get(userId); |
Ricky Wai | f6ef828 | 2016-04-14 10:00:22 +0100 | [diff] [blame] | 1030 | if (uss == null) { |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1031 | notifyFinished(userId, listener); |
Ricky Wai | f6ef828 | 2016-04-14 10:00:22 +0100 | [diff] [blame] | 1032 | return false; |
Jeff Sharkey | 84a4c97 | 2016-04-18 15:36:39 -0600 | [diff] [blame] | 1033 | } else { |
| 1034 | uss.mUnlockProgress.addListener(listener); |
Jeff Sharkey | 24d9491 | 2016-07-07 12:33:48 -0600 | [diff] [blame] | 1035 | uss.tokenProvided = (token != null); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1036 | } |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1037 | } |
Jeff Sharkey | 8924e87 | 2015-11-30 12:52:10 -0700 | [diff] [blame] | 1038 | |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1039 | finishUserUnlocking(uss); |
| 1040 | |
| 1041 | final ArraySet<Integer> childProfilesToUnlock = new ArraySet<>(); |
Jorim Jaggi | 526694e | 2016-08-03 17:15:23 +0200 | [diff] [blame] | 1042 | synchronized (mLock) { |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 1043 | |
Jeff Sharkey | 7661a31 | 2016-04-07 12:39:57 -0600 | [diff] [blame] | 1044 | // We just unlocked a user, so let's now attempt to unlock any |
| 1045 | // managed profiles under that user. |
Jeff Sharkey | 5dab713 | 2016-04-07 01:20:58 -0600 | [diff] [blame] | 1046 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1047 | final int testUserId = mStartedUsers.keyAt(i); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1048 | final UserInfo parent = mInjector.getUserManager().getProfileParent(testUserId); |
Jeff Sharkey | 5dab713 | 2016-04-07 01:20:58 -0600 | [diff] [blame] | 1049 | if (parent != null && parent.id == userId && testUserId != userId) { |
Jeff Sharkey | 7661a31 | 2016-04-07 12:39:57 -0600 | [diff] [blame] | 1050 | Slog.d(TAG, "User " + testUserId + " (parent " + parent.id |
| 1051 | + "): attempting unlock because parent was just unlocked"); |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1052 | childProfilesToUnlock.add(testUserId); |
Jeff Sharkey | 5dab713 | 2016-04-07 01:20:58 -0600 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
Jorim Jaggi | 6f4d7b3 | 2016-08-01 14:31:02 +0200 | [diff] [blame] | 1057 | final int size = childProfilesToUnlock.size(); |
| 1058 | for (int i = 0; i < size; i++) { |
| 1059 | maybeUnlockUser(childProfilesToUnlock.valueAt(i)); |
| 1060 | } |
| 1061 | |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
| 1064 | |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 1065 | void showUserSwitchDialog(Pair<UserInfo, UserInfo> fromToUserPair) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1066 | // The dialog will show and then initiate the user switch by calling startUserInForeground |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1067 | mInjector.showUserSwitchingDialog(fromToUserPair.first, fromToUserPair.second); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1070 | void dispatchForegroundProfileChanged(int userId) { |
| 1071 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 1072 | for (int i = 0; i < observerCount; i++) { |
| 1073 | try { |
| 1074 | mUserSwitchObservers.getBroadcastItem(i).onForegroundProfileSwitch(userId); |
| 1075 | } catch (RemoteException e) { |
| 1076 | // Ignore |
| 1077 | } |
| 1078 | } |
| 1079 | mUserSwitchObservers.finishBroadcast(); |
| 1080 | } |
| 1081 | |
| 1082 | /** Called on handler thread */ |
| 1083 | void dispatchUserSwitchComplete(int userId) { |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 1084 | mInjector.getWindowManager().setSwitchingUser(false); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1085 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 1086 | for (int i = 0; i < observerCount; i++) { |
| 1087 | try { |
| 1088 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(userId); |
| 1089 | } catch (RemoteException e) { |
| 1090 | } |
| 1091 | } |
| 1092 | mUserSwitchObservers.finishBroadcast(); |
| 1093 | } |
| 1094 | |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 1095 | void dispatchLockedBootComplete(int userId) { |
| 1096 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 1097 | for (int i = 0; i < observerCount; i++) { |
| 1098 | try { |
| 1099 | mUserSwitchObservers.getBroadcastItem(i).onLockedBootComplete(userId); |
| 1100 | } catch (RemoteException e) { |
| 1101 | // Ignore |
| 1102 | } |
| 1103 | } |
| 1104 | mUserSwitchObservers.finishBroadcast(); |
| 1105 | } |
| 1106 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1107 | private void stopBackgroundUsersIfEnforced(int oldUserId) { |
| 1108 | // Never stop system user |
| 1109 | if (oldUserId == UserHandle.USER_SYSTEM) { |
| 1110 | return; |
| 1111 | } |
| 1112 | // For now, only check for user restriction. Additional checks can be added here |
| 1113 | boolean disallowRunInBg = hasUserRestriction(UserManager.DISALLOW_RUN_IN_BACKGROUND, |
| 1114 | oldUserId); |
| 1115 | if (!disallowRunInBg) { |
| 1116 | return; |
| 1117 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1118 | synchronized (mLock) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1119 | if (DEBUG_MU) Slog.i(TAG, "stopBackgroundUsersIfEnforced stopping " + oldUserId |
| 1120 | + " and related users"); |
| 1121 | stopUsersLocked(oldUserId, false, null); |
| 1122 | } |
| 1123 | } |
| 1124 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1125 | void timeoutUserSwitch(UserState uss, int oldUserId, int newUserId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1126 | synchronized (mLock) { |
| 1127 | Slog.wtf(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1128 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 1132 | void dispatchUserSwitch(final UserState uss, final int oldUserId, final int newUserId) { |
| 1133 | Slog.d(TAG, "Dispatch onUserSwitching oldUser #" + oldUserId + " newUser #" + newUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1134 | final int observerCount = mUserSwitchObservers.beginBroadcast(); |
| 1135 | if (observerCount > 0) { |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1136 | final ArraySet<String> curWaitingUserSwitchCallbacks = new ArraySet<>(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1137 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1138 | uss.switching = true; |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1139 | mCurWaitingUserSwitchCallbacks = curWaitingUserSwitchCallbacks; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1140 | } |
Fyodor Kupolov | 3864183 | 2016-06-28 17:37:09 -0700 | [diff] [blame] | 1141 | final AtomicInteger waitingCallbacksCount = new AtomicInteger(observerCount); |
Fyodor Kupolov | 0e0986e | 2016-09-22 18:01:36 -0700 | [diff] [blame] | 1142 | final long dispatchStartedTime = SystemClock.elapsedRealtime(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1143 | for (int i = 0; i < observerCount; i++) { |
| 1144 | try { |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1145 | // Prepend with unique prefix to guarantee that keys are unique |
| 1146 | final String name = "#" + i + " " + mUserSwitchObservers.getBroadcastCookie(i); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1147 | synchronized (mLock) { |
Fyodor Kupolov | 599d43b | 2016-06-24 13:46:18 -0700 | [diff] [blame] | 1148 | curWaitingUserSwitchCallbacks.add(name); |
| 1149 | } |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1150 | final IRemoteCallback callback = new IRemoteCallback.Stub() { |
| 1151 | @Override |
| 1152 | public void sendResult(Bundle data) throws RemoteException { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1153 | synchronized (mLock) { |
Fyodor Kupolov | 0e0986e | 2016-09-22 18:01:36 -0700 | [diff] [blame] | 1154 | long delay = SystemClock.elapsedRealtime() - dispatchStartedTime; |
| 1155 | if (delay > USER_SWITCH_TIMEOUT) { |
| 1156 | Slog.wtf(TAG, "User switch timeout: observer " + name |
| 1157 | + " sent result after " + delay + " ms"); |
| 1158 | } |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1159 | // Early return if this session is no longer valid |
| 1160 | if (curWaitingUserSwitchCallbacks |
| 1161 | != mCurWaitingUserSwitchCallbacks) { |
| 1162 | return; |
| 1163 | } |
| 1164 | curWaitingUserSwitchCallbacks.remove(name); |
| 1165 | // Continue switching if all callbacks have been notified |
Fyodor Kupolov | 3864183 | 2016-06-28 17:37:09 -0700 | [diff] [blame] | 1166 | if (waitingCallbacksCount.decrementAndGet() == 0) { |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1167 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | }; |
| 1172 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitching(newUserId, callback); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1173 | } catch (RemoteException e) { |
| 1174 | } |
| 1175 | } |
| 1176 | } else { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1177 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1178 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 1179 | } |
| 1180 | } |
| 1181 | mUserSwitchObservers.finishBroadcast(); |
| 1182 | } |
| 1183 | |
| 1184 | void sendContinueUserSwitchLocked(UserState uss, int oldUserId, int newUserId) { |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1185 | mCurWaitingUserSwitchCallbacks = null; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1186 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 1187 | mHandler.sendMessage(mHandler.obtainMessage(ActivityManagerService.CONTINUE_USER_SWITCH_MSG, |
| 1188 | oldUserId, newUserId, uss)); |
| 1189 | } |
| 1190 | |
| 1191 | void continueUserSwitch(UserState uss, int oldUserId, int newUserId) { |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 1192 | Slog.d(TAG, "Continue user switch oldUser #" + oldUserId + ", newUser #" + newUserId); |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 1193 | if (mUserSwitchUiEnabled) { |
| 1194 | synchronized (mLock) { |
| 1195 | mInjector.getWindowManager().stopFreezingScreen(); |
| 1196 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1197 | } |
Fyodor Kupolov | c94c249 | 2016-04-20 17:44:00 -0700 | [diff] [blame] | 1198 | uss.switching = false; |
| 1199 | mHandler.removeMessages(REPORT_USER_SWITCH_COMPLETE_MSG); |
| 1200 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_COMPLETE_MSG, |
| 1201 | newUserId, 0)); |
Lenka Trochtova | b4484ba | 2015-12-16 12:32:31 +0100 | [diff] [blame] | 1202 | stopGuestOrEphemeralUserIfBackground(); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1203 | stopBackgroundUsersIfEnforced(oldUserId); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | void moveUserToForegroundLocked(UserState uss, int oldUserId, int newUserId) { |
Tony Mak | 5c2cf03 | 2017-04-03 18:38:23 +0100 | [diff] [blame] | 1207 | boolean homeInFront = |
| 1208 | mInjector.getActivityStackSupervisor().switchUserLocked(newUserId, uss); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1209 | if (homeInFront) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1210 | mInjector.startHomeActivityLocked(newUserId, "moveUserToForeground"); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1211 | } else { |
Tony Mak | 5c2cf03 | 2017-04-03 18:38:23 +0100 | [diff] [blame] | 1212 | mInjector.getActivityStackSupervisor().resumeFocusedStackTopActivityLocked(); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1213 | } |
| 1214 | EventLogTags.writeAmSwitchUser(newUserId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1215 | sendUserSwitchBroadcastsLocked(oldUserId, newUserId); |
| 1216 | } |
| 1217 | |
| 1218 | void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) { |
| 1219 | long ident = Binder.clearCallingIdentity(); |
| 1220 | try { |
| 1221 | Intent intent; |
| 1222 | if (oldUserId >= 0) { |
| 1223 | // Send USER_BACKGROUND broadcast to all profiles of the outgoing user |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1224 | List<UserInfo> profiles = mInjector.getUserManager().getProfiles(oldUserId, false); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1225 | int count = profiles.size(); |
| 1226 | for (int i = 0; i < count; i++) { |
| 1227 | int profileUserId = profiles.get(i).id; |
| 1228 | intent = new Intent(Intent.ACTION_USER_BACKGROUND); |
| 1229 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1230 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 1231 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileUserId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1232 | mInjector.broadcastIntentLocked(intent, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1233 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 1234 | null, false, false, MY_PID, SYSTEM_UID, profileUserId); |
| 1235 | } |
| 1236 | } |
| 1237 | if (newUserId >= 0) { |
| 1238 | // Send USER_FOREGROUND broadcast to all profiles of the incoming user |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1239 | List<UserInfo> profiles = mInjector.getUserManager().getProfiles(newUserId, false); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1240 | int count = profiles.size(); |
| 1241 | for (int i = 0; i < count; i++) { |
| 1242 | int profileUserId = profiles.get(i).id; |
| 1243 | intent = new Intent(Intent.ACTION_USER_FOREGROUND); |
| 1244 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1245 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 1246 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileUserId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1247 | mInjector.broadcastIntentLocked(intent, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1248 | null, null, 0, null, null, null, AppOpsManager.OP_NONE, |
| 1249 | null, false, false, MY_PID, SYSTEM_UID, profileUserId); |
| 1250 | } |
| 1251 | intent = new Intent(Intent.ACTION_USER_SWITCHED); |
| 1252 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1253 | | Intent.FLAG_RECEIVER_FOREGROUND); |
| 1254 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1255 | mInjector.broadcastIntentLocked(intent, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1256 | null, null, 0, null, null, |
| 1257 | new String[] {android.Manifest.permission.MANAGE_USERS}, |
| 1258 | AppOpsManager.OP_NONE, null, false, false, MY_PID, SYSTEM_UID, |
| 1259 | UserHandle.USER_ALL); |
| 1260 | } |
| 1261 | } finally { |
| 1262 | Binder.restoreCallingIdentity(ident); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | |
| 1267 | int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 1268 | int allowMode, String name, String callerPackage) { |
| 1269 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 1270 | if (callingUserId == userId) { |
| 1271 | return userId; |
| 1272 | } |
| 1273 | |
| 1274 | // Note that we may be accessing mCurrentUserId outside of a lock... |
| 1275 | // shouldn't be a big deal, if this is being called outside |
| 1276 | // of a locked context there is intrinsically a race with |
| 1277 | // the value the caller will receive and someone else changing it. |
| 1278 | // We assume that USER_CURRENT_OR_SELF will use the current user; later |
| 1279 | // we will switch to the calling user if access to the current user fails. |
| 1280 | int targetUserId = unsafeConvertIncomingUserLocked(userId); |
| 1281 | |
| 1282 | if (callingUid != 0 && callingUid != SYSTEM_UID) { |
| 1283 | final boolean allow; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1284 | if (mInjector.checkComponentPermission(INTERACT_ACROSS_USERS_FULL, callingPid, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1285 | callingUid, -1, true) == PackageManager.PERMISSION_GRANTED) { |
| 1286 | // If the caller has this permission, they always pass go. And collect $200. |
| 1287 | allow = true; |
| 1288 | } else if (allowMode == ALLOW_FULL_ONLY) { |
| 1289 | // We require full access, sucks to be you. |
| 1290 | allow = false; |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1291 | } else if (mInjector.checkComponentPermission(INTERACT_ACROSS_USERS, callingPid, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1292 | callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 1293 | // If the caller does not have either permission, they are always doomed. |
| 1294 | allow = false; |
| 1295 | } else if (allowMode == ALLOW_NON_FULL) { |
| 1296 | // We are blanket allowing non-full access, you lucky caller! |
| 1297 | allow = true; |
| 1298 | } else if (allowMode == ALLOW_NON_FULL_IN_PROFILE) { |
| 1299 | // We may or may not allow this depending on whether the two users are |
| 1300 | // in the same profile. |
| 1301 | allow = isSameProfileGroup(callingUserId, targetUserId); |
| 1302 | } else { |
| 1303 | throw new IllegalArgumentException("Unknown mode: " + allowMode); |
| 1304 | } |
| 1305 | if (!allow) { |
| 1306 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 1307 | // In this case, they would like to just execute as their |
| 1308 | // owner user instead of failing. |
| 1309 | targetUserId = callingUserId; |
| 1310 | } else { |
| 1311 | StringBuilder builder = new StringBuilder(128); |
| 1312 | builder.append("Permission Denial: "); |
| 1313 | builder.append(name); |
| 1314 | if (callerPackage != null) { |
| 1315 | builder.append(" from "); |
| 1316 | builder.append(callerPackage); |
| 1317 | } |
| 1318 | builder.append(" asks to run as user "); |
| 1319 | builder.append(userId); |
| 1320 | builder.append(" but is calling from user "); |
| 1321 | builder.append(UserHandle.getUserId(callingUid)); |
| 1322 | builder.append("; this requires "); |
| 1323 | builder.append(INTERACT_ACROSS_USERS_FULL); |
| 1324 | if (allowMode != ALLOW_FULL_ONLY) { |
| 1325 | builder.append(" or "); |
| 1326 | builder.append(INTERACT_ACROSS_USERS); |
| 1327 | } |
| 1328 | String msg = builder.toString(); |
| 1329 | Slog.w(TAG, msg); |
| 1330 | throw new SecurityException(msg); |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | if (!allowAll && targetUserId < 0) { |
| 1335 | throw new IllegalArgumentException( |
| 1336 | "Call does not support special user #" + targetUserId); |
| 1337 | } |
| 1338 | // Check shell permission |
| 1339 | if (callingUid == Process.SHELL_UID && targetUserId >= UserHandle.USER_SYSTEM) { |
| 1340 | if (hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, targetUserId)) { |
| 1341 | throw new SecurityException("Shell does not have permission to access user " |
| 1342 | + targetUserId + "\n " + Debug.getCallers(3)); |
| 1343 | } |
| 1344 | } |
| 1345 | return targetUserId; |
| 1346 | } |
| 1347 | |
| 1348 | int unsafeConvertIncomingUserLocked(int userId) { |
| 1349 | return (userId == UserHandle.USER_CURRENT || userId == UserHandle.USER_CURRENT_OR_SELF) |
| 1350 | ? getCurrentUserIdLocked(): userId; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1353 | void registerUserSwitchObserver(IUserSwitchObserver observer, String name) { |
| 1354 | Preconditions.checkNotNull(name, "Observer name cannot be null"); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1355 | if (mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1356 | != PackageManager.PERMISSION_GRANTED) { |
| 1357 | final String msg = "Permission Denial: registerUserSwitchObserver() from pid=" |
| 1358 | + Binder.getCallingPid() |
| 1359 | + ", uid=" + Binder.getCallingUid() |
| 1360 | + " requires " + INTERACT_ACROSS_USERS_FULL; |
| 1361 | Slog.w(TAG, msg); |
| 1362 | throw new SecurityException(msg); |
| 1363 | } |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1364 | mUserSwitchObservers.register(observer, name); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | void unregisterUserSwitchObserver(IUserSwitchObserver observer) { |
| 1368 | mUserSwitchObservers.unregister(observer); |
| 1369 | } |
| 1370 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1371 | UserState getStartedUserStateLocked(int userId) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1372 | return mStartedUsers.get(userId); |
| 1373 | } |
| 1374 | |
| 1375 | boolean hasStartedUserState(int userId) { |
| 1376 | return mStartedUsers.get(userId) != null; |
| 1377 | } |
| 1378 | |
| 1379 | private void updateStartedUserArrayLocked() { |
| 1380 | int num = 0; |
| 1381 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1382 | UserState uss = mStartedUsers.valueAt(i); |
| 1383 | // This list does not include stopping users. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1384 | if (uss.state != UserState.STATE_STOPPING |
| 1385 | && uss.state != UserState.STATE_SHUTDOWN) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1386 | num++; |
| 1387 | } |
| 1388 | } |
| 1389 | mStartedUserArray = new int[num]; |
| 1390 | num = 0; |
| 1391 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1392 | UserState uss = mStartedUsers.valueAt(i); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1393 | if (uss.state != UserState.STATE_STOPPING |
| 1394 | && uss.state != UserState.STATE_SHUTDOWN) { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 1395 | mStartedUserArray[num++] = mStartedUsers.keyAt(i); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | void sendBootCompletedLocked(IIntentReceiver resultTo) { |
| 1401 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1402 | UserState uss = mStartedUsers.valueAt(i); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1403 | finishUserBoot(uss, resultTo); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | |
Wale Ogunwale | f80170f | 2016-02-04 15:12:29 -0800 | [diff] [blame] | 1407 | void onSystemReady() { |
| 1408 | updateCurrentProfileIdsLocked(); |
Wale Ogunwale | f80170f | 2016-02-04 15:12:29 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1411 | /** |
| 1412 | * Refreshes the list of users related to the current user when either a |
| 1413 | * user switch happens or when a new related user is started in the |
| 1414 | * background. |
| 1415 | */ |
Wale Ogunwale | f80170f | 2016-02-04 15:12:29 -0800 | [diff] [blame] | 1416 | private void updateCurrentProfileIdsLocked() { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1417 | final List<UserInfo> profiles = mInjector.getUserManager().getProfiles(mCurrentUserId, |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1418 | false /* enabledOnly */); |
| 1419 | int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null |
| 1420 | for (int i = 0; i < currentProfileIds.length; i++) { |
| 1421 | currentProfileIds[i] = profiles.get(i).id; |
| 1422 | } |
| 1423 | mCurrentProfileIds = currentProfileIds; |
| 1424 | |
| 1425 | synchronized (mUserProfileGroupIdsSelfLocked) { |
| 1426 | mUserProfileGroupIdsSelfLocked.clear(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1427 | final List<UserInfo> users = mInjector.getUserManager().getUsers(false); |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1428 | for (int i = 0; i < users.size(); i++) { |
| 1429 | UserInfo user = users.get(i); |
| 1430 | if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 1431 | mUserProfileGroupIdsSelfLocked.put(user.id, user.profileGroupId); |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | int[] getStartedUserArrayLocked() { |
| 1438 | return mStartedUserArray; |
| 1439 | } |
| 1440 | |
Nicolas Prevot | 1219c92 | 2016-06-20 17:25:12 +0100 | [diff] [blame] | 1441 | boolean isUserStoppingOrShuttingDownLocked(int userId) { |
| 1442 | UserState state = getStartedUserStateLocked(userId); |
| 1443 | if (state == null) { |
| 1444 | return false; |
| 1445 | } |
| 1446 | return state.state == UserState.STATE_STOPPING |
| 1447 | || state.state == UserState.STATE_SHUTDOWN; |
| 1448 | } |
| 1449 | |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 1450 | boolean isUserRunningLocked(int userId, int flags) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1451 | UserState state = getStartedUserStateLocked(userId); |
| 1452 | if (state == null) { |
| 1453 | return false; |
| 1454 | } |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 1455 | if ((flags & ActivityManager.FLAG_OR_STOPPED) != 0) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1456 | return true; |
| 1457 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1458 | if ((flags & ActivityManager.FLAG_AND_LOCKED) != 0) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1459 | switch (state.state) { |
| 1460 | case UserState.STATE_BOOTING: |
| 1461 | case UserState.STATE_RUNNING_LOCKED: |
| 1462 | return true; |
| 1463 | default: |
| 1464 | return false; |
| 1465 | } |
| 1466 | } |
| 1467 | if ((flags & ActivityManager.FLAG_AND_UNLOCKING_OR_UNLOCKED) != 0) { |
| 1468 | switch (state.state) { |
| 1469 | case UserState.STATE_RUNNING_UNLOCKING: |
| 1470 | case UserState.STATE_RUNNING_UNLOCKED: |
| 1471 | return true; |
| 1472 | default: |
| 1473 | return false; |
| 1474 | } |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 1475 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1476 | if ((flags & ActivityManager.FLAG_AND_UNLOCKED) != 0) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1477 | switch (state.state) { |
| 1478 | case UserState.STATE_RUNNING_UNLOCKED: |
| 1479 | return true; |
| 1480 | default: |
| 1481 | return false; |
| 1482 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | // One way or another, we're running! |
| 1486 | return true; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1489 | UserInfo getCurrentUser() { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1490 | if ((mInjector.checkCallingPermission(INTERACT_ACROSS_USERS) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1491 | != PackageManager.PERMISSION_GRANTED) && ( |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1492 | mInjector.checkCallingPermission(INTERACT_ACROSS_USERS_FULL) |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1493 | != PackageManager.PERMISSION_GRANTED)) { |
| 1494 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 1495 | + Binder.getCallingPid() |
| 1496 | + ", uid=" + Binder.getCallingUid() |
| 1497 | + " requires " + INTERACT_ACROSS_USERS; |
| 1498 | Slog.w(TAG, msg); |
| 1499 | throw new SecurityException(msg); |
| 1500 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1501 | synchronized (mLock) { |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1502 | return getCurrentUserLocked(); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | UserInfo getCurrentUserLocked() { |
| 1507 | int userId = mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId; |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1508 | return getUserInfo(userId); |
| 1509 | } |
| 1510 | |
| 1511 | int getCurrentOrTargetUserIdLocked() { |
| 1512 | return mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | int getCurrentUserIdLocked() { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1516 | return mCurrentUserId; |
| 1517 | } |
| 1518 | |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1519 | private boolean isCurrentUserLocked(int userId) { |
Amith Yamasani | 458ac46 | 2015-12-18 11:21:31 -0800 | [diff] [blame] | 1520 | return userId == getCurrentOrTargetUserIdLocked(); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1523 | int setTargetUserIdLocked(int targetUserId) { |
| 1524 | return mTargetUserId = targetUserId; |
| 1525 | } |
| 1526 | |
| 1527 | int[] getUsers() { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1528 | UserManagerService ums = mInjector.getUserManager(); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1529 | return ums != null ? ums.getUserIds() : new int[] { 0 }; |
| 1530 | } |
| 1531 | |
| 1532 | UserInfo getUserInfo(int userId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1533 | return mInjector.getUserManager().getUserInfo(userId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | int[] getUserIds() { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1537 | return mInjector.getUserManager().getUserIds(); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | boolean exists(int userId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1541 | return mInjector.getUserManager().exists(userId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | boolean hasUserRestriction(String restriction, int userId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1545 | return mInjector.getUserManager().hasUserRestriction(restriction, userId); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | Set<Integer> getProfileIds(int userId) { |
| 1549 | Set<Integer> userIds = new HashSet<>(); |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1550 | final List<UserInfo> profiles = mInjector.getUserManager().getProfiles(userId, |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1551 | false /* enabledOnly */); |
| 1552 | for (UserInfo user : profiles) { |
| 1553 | userIds.add(user.id); |
| 1554 | } |
| 1555 | return userIds; |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | boolean isSameProfileGroup(int callingUserId, int targetUserId) { |
Makoto Onuki | 8198dea | 2016-07-28 13:10:42 -0700 | [diff] [blame] | 1559 | if (callingUserId == targetUserId) { |
| 1560 | return true; |
| 1561 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1562 | synchronized (mUserProfileGroupIdsSelfLocked) { |
| 1563 | int callingProfile = mUserProfileGroupIdsSelfLocked.get(callingUserId, |
| 1564 | UserInfo.NO_PROFILE_GROUP_ID); |
| 1565 | int targetProfile = mUserProfileGroupIdsSelfLocked.get(targetUserId, |
| 1566 | UserInfo.NO_PROFILE_GROUP_ID); |
| 1567 | return callingProfile != UserInfo.NO_PROFILE_GROUP_ID |
| 1568 | && callingProfile == targetProfile; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | boolean isCurrentProfileLocked(int userId) { |
| 1573 | return ArrayUtils.contains(mCurrentProfileIds, userId); |
| 1574 | } |
| 1575 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1576 | int[] getCurrentProfileIdsLocked() { |
| 1577 | return mCurrentProfileIds; |
| 1578 | } |
| 1579 | |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 1580 | /** |
| 1581 | * Returns whether the given user requires credential entry at this time. This is used to |
| 1582 | * intercept activity launches for work apps when the Work Challenge is present. |
| 1583 | */ |
| 1584 | boolean shouldConfirmCredentials(int userId) { |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1585 | synchronized (mLock) { |
Rubin Xu | b93522a | 2016-02-23 18:21:48 +0000 | [diff] [blame] | 1586 | if (mStartedUsers.get(userId) == null) { |
| 1587 | return false; |
| 1588 | } |
| 1589 | } |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 1590 | if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 1591 | return false; |
| 1592 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1593 | final KeyguardManager km = mInjector.getKeyguardManager(); |
Tony Mak | ae13e18 | 2016-05-17 16:36:14 +0100 | [diff] [blame] | 1594 | return km.isDeviceLocked(userId) && km.isDeviceSecure(userId); |
Clara Bayarri | ea9b10e | 2015-12-04 15:36:26 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Tony Mak | 8c536f9 | 2016-03-21 12:20:41 +0000 | [diff] [blame] | 1597 | boolean isLockScreenDisabled(@UserIdInt int userId) { |
| 1598 | return mLockPatternUtils.isLockScreenDisabled(userId); |
| 1599 | } |
| 1600 | |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1601 | void dump(PrintWriter pw, boolean dumpAll) { |
| 1602 | pw.println(" mStartedUsers:"); |
| 1603 | for (int i = 0; i < mStartedUsers.size(); i++) { |
| 1604 | UserState uss = mStartedUsers.valueAt(i); |
| 1605 | pw.print(" User #"); pw.print(uss.mHandle.getIdentifier()); |
| 1606 | pw.print(": "); uss.dump("", pw); |
| 1607 | } |
| 1608 | pw.print(" mStartedUserArray: ["); |
| 1609 | for (int i = 0; i < mStartedUserArray.length; i++) { |
| 1610 | if (i > 0) pw.print(", "); |
| 1611 | pw.print(mStartedUserArray[i]); |
| 1612 | } |
| 1613 | pw.println("]"); |
| 1614 | pw.print(" mUserLru: ["); |
| 1615 | for (int i = 0; i < mUserLru.size(); i++) { |
| 1616 | if (i > 0) pw.print(", "); |
| 1617 | pw.print(mUserLru.get(i)); |
| 1618 | } |
| 1619 | pw.println("]"); |
| 1620 | if (dumpAll) { |
| 1621 | pw.print(" mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray)); |
| 1622 | } |
| 1623 | synchronized (mUserProfileGroupIdsSelfLocked) { |
| 1624 | if (mUserProfileGroupIdsSelfLocked.size() > 0) { |
| 1625 | pw.println(" mUserProfileGroupIds:"); |
| 1626 | for (int i=0; i<mUserProfileGroupIdsSelfLocked.size(); i++) { |
| 1627 | pw.print(" User #"); |
| 1628 | pw.print(mUserProfileGroupIdsSelfLocked.keyAt(i)); |
| 1629 | pw.print(" -> profile #"); |
| 1630 | pw.println(mUserProfileGroupIdsSelfLocked.valueAt(i)); |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1635 | |
| 1636 | @VisibleForTesting |
| 1637 | static class Injector { |
| 1638 | private final ActivityManagerService mService; |
| 1639 | private UserManagerService mUserManager; |
| 1640 | private UserManagerInternal mUserManagerInternal; |
| 1641 | |
| 1642 | Injector(ActivityManagerService service) { |
| 1643 | mService = service; |
| 1644 | } |
| 1645 | |
| 1646 | protected Object getLock() { |
| 1647 | return mService; |
| 1648 | } |
| 1649 | |
| 1650 | protected Handler getHandler() { |
| 1651 | return mService.mHandler; |
| 1652 | } |
| 1653 | |
| 1654 | protected Context getContext() { |
| 1655 | return mService.mContext; |
| 1656 | } |
| 1657 | |
| 1658 | protected LockPatternUtils getLockPatternUtils() { |
| 1659 | return new LockPatternUtils(getContext()); |
| 1660 | } |
| 1661 | |
| 1662 | protected int broadcastIntentLocked(Intent intent, String resolvedType, |
| 1663 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 1664 | Bundle resultExtras, String[] requiredPermissions, int appOp, Bundle bOptions, |
| 1665 | boolean ordered, boolean sticky, int callingPid, int callingUid, int userId) { |
| 1666 | return mService.broadcastIntentLocked(null, null, intent, resolvedType, resultTo, |
| 1667 | resultCode, resultData, resultExtras, requiredPermissions, appOp, bOptions, |
| 1668 | ordered, sticky, callingPid, callingUid, userId); |
| 1669 | } |
| 1670 | |
| 1671 | int checkCallingPermission(String permission) { |
| 1672 | return mService.checkCallingPermission(permission); |
| 1673 | } |
| 1674 | |
| 1675 | WindowManagerService getWindowManager() { |
| 1676 | return mService.mWindowManager; |
| 1677 | } |
| 1678 | void activityManagerOnUserStopped(int userId) { |
| 1679 | mService.onUserStoppedLocked(userId); |
| 1680 | } |
| 1681 | |
| 1682 | void systemServiceManagerCleanupUser(int userId) { |
| 1683 | mService.mSystemServiceManager.cleanupUser(userId); |
| 1684 | } |
| 1685 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1686 | protected UserManagerService getUserManager() { |
| 1687 | if (mUserManager == null) { |
| 1688 | IBinder b = ServiceManager.getService(Context.USER_SERVICE); |
| 1689 | mUserManager = (UserManagerService) IUserManager.Stub.asInterface(b); |
| 1690 | } |
| 1691 | return mUserManager; |
| 1692 | } |
| 1693 | |
| 1694 | UserManagerInternal getUserManagerInternal() { |
| 1695 | if (mUserManagerInternal == null) { |
| 1696 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
| 1697 | } |
| 1698 | return mUserManagerInternal; |
| 1699 | } |
| 1700 | |
| 1701 | KeyguardManager getKeyguardManager() { |
| 1702 | return mService.mContext.getSystemService(KeyguardManager.class); |
| 1703 | } |
| 1704 | |
| 1705 | void batteryStatsServiceNoteEvent(int code, String name, int uid) { |
| 1706 | mService.mBatteryStatsService.noteEvent(code, name, uid); |
| 1707 | } |
| 1708 | |
| 1709 | void systemServiceManagerStopUser(int userId) { |
| 1710 | mService.mSystemServiceManager.stopUser(userId); |
| 1711 | } |
| 1712 | |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 1713 | boolean isRuntimeRestarted() { |
| 1714 | return mService.mSystemServiceManager.isRuntimeRestarted(); |
| 1715 | } |
| 1716 | |
Fyodor Kupolov | 4ba91b9 | 2017-01-20 18:12:35 -0800 | [diff] [blame] | 1717 | boolean isFirstBootOrUpgrade() { |
| 1718 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 1719 | try { |
| 1720 | return pm.isFirstBoot() || pm.isUpgrade(); |
| 1721 | } catch (RemoteException e) { |
| 1722 | throw e.rethrowFromSystemServer(); |
| 1723 | } |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 1724 | } |
| 1725 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1726 | void sendPreBootBroadcast(int userId, boolean quiet, final Runnable onFinish) { |
| 1727 | new PreBootBroadcaster(mService, userId, null, quiet) { |
| 1728 | @Override |
| 1729 | public void onFinished() { |
| 1730 | onFinish.run(); |
| 1731 | } |
| 1732 | }.sendNext(); |
| 1733 | } |
| 1734 | |
| 1735 | void activityManagerForceStopPackageLocked(int userId, String reason) { |
| 1736 | mService.forceStopPackageLocked(null, -1, false, false, true, false, false, |
| 1737 | userId, reason); |
| 1738 | }; |
| 1739 | |
| 1740 | int checkComponentPermission(String permission, int pid, int uid, int owningUid, |
| 1741 | boolean exported) { |
| 1742 | return mService.checkComponentPermission(permission, pid, uid, owningUid, exported); |
| 1743 | } |
| 1744 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1745 | void startHomeActivityLocked(int userId, String reason) { |
| 1746 | mService.startHomeActivityLocked(userId, reason); |
| 1747 | } |
| 1748 | |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1749 | void updateUserConfigurationLocked() { |
| 1750 | mService.updateUserConfigurationLocked(); |
| 1751 | } |
| 1752 | |
| 1753 | void clearBroadcastQueueForUserLocked(int userId) { |
| 1754 | mService.clearBroadcastQueueForUserLocked(userId); |
| 1755 | } |
| 1756 | |
| 1757 | void enforceShellRestriction(String restriction, int userId) { |
| 1758 | mService.enforceShellRestriction(restriction, userId); |
| 1759 | } |
| 1760 | |
| 1761 | void showUserSwitchingDialog(UserInfo fromUser, UserInfo toUser) { |
| 1762 | Dialog d = new UserSwitchingDialog(mService, mService.mContext, fromUser, toUser, |
| 1763 | true /* above system */); |
| 1764 | d.show(); |
| 1765 | } |
Tony Mak | 5c2cf03 | 2017-04-03 18:38:23 +0100 | [diff] [blame] | 1766 | |
| 1767 | ActivityStackSupervisor getActivityStackSupervisor() { |
| 1768 | return mService.mStackSupervisor; |
| 1769 | } |
Fyodor Kupolov | d6038db | 2016-06-22 16:53:19 -0700 | [diff] [blame] | 1770 | } |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 1771 | } |