Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.car.user; |
| 18 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 19 | import static com.android.car.CarLog.TAG_USER; |
| 20 | |
| 21 | import android.annotation.NonNull; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 23 | import android.annotation.UserIdInt; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
| 25 | import android.app.IActivityManager; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 26 | import android.car.ICarUserService; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame] | 27 | import android.car.settings.CarSettings; |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 28 | import android.car.userlib.CarUserManagerHelper; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 30 | import android.content.pm.UserInfo; |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 32 | import android.location.LocationManager; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 33 | import android.os.Binder; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 34 | import android.os.RemoteException; |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Ying Zheng | 8f90edb | 2018-06-13 12:42:31 -0700 | [diff] [blame] | 36 | import android.os.UserManager; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame] | 37 | import android.provider.Settings; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 38 | import android.util.Log; |
| 39 | |
| 40 | import com.android.car.CarServiceBase; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 41 | import com.android.internal.annotations.GuardedBy; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 42 | import com.android.internal.annotations.VisibleForTesting; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 43 | import com.android.internal.util.Preconditions; |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 44 | import com.android.internal.util.UserIcons; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 45 | |
| 46 | import java.io.PrintWriter; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 48 | import java.util.Arrays; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 49 | import java.util.Iterator; |
| 50 | import java.util.List; |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 51 | import java.util.concurrent.CopyOnWriteArrayList; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * User service for cars. Manages users at boot time. Including: |
| 55 | * |
| 56 | * <ol> |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 57 | * <li> Creates a user used as driver. |
| 58 | * <li> Creates a user used as passenger. |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 59 | * <li> Creates a secondary admin user on first run. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 60 | * <li> Switch drivers. |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 61 | * <ol/> |
| 62 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 63 | public final class CarUserService extends ICarUserService.Stub implements CarServiceBase { |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 64 | private final Context mContext; |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 65 | private final CarUserManagerHelper mCarUserManagerHelper; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 66 | private final IActivityManager mAm; |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 67 | private final UserManager mUserManager; |
| 68 | private final int mMaxRunningUsers; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 69 | |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 70 | private final Object mLock = new Object(); |
| 71 | @GuardedBy("mLock") |
| 72 | private boolean mUser0Unlocked; |
| 73 | @GuardedBy("mLock") |
| 74 | private final ArrayList<Runnable> mUser0UnlockTasks = new ArrayList<>(); |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Background users that will be restarted in garage mode. This list can include the |
| 77 | * current foreground user bit the current foreground user should not be restarted. |
| 78 | */ |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 79 | @GuardedBy("mLock") |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 80 | private final ArrayList<Integer> mBackgroundUsersToRestart = new ArrayList<>(); |
| 81 | /** |
| 82 | * Keep the list of background users started here. This is wholly for debugging purpose. |
| 83 | */ |
| 84 | @GuardedBy("mLock") |
| 85 | private final ArrayList<Integer> mBackgroundUsersRestartedHere = new ArrayList<>(); |
| 86 | |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 87 | private final CopyOnWriteArrayList<UserCallback> mUserCallbacks = new CopyOnWriteArrayList<>(); |
| 88 | |
| 89 | /** Interface for callbacks related to user activities. */ |
| 90 | public interface UserCallback { |
| 91 | /** Gets called when user lock status has been changed. */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 92 | void onUserLockChanged(@UserIdInt int userId, boolean unlocked); |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 93 | /** Called when new foreground user started to boot. */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 94 | void onSwitchUser(@UserIdInt int userId); |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 97 | public CarUserService( |
| 98 | @NonNull Context context, @NonNull CarUserManagerHelper carUserManagerHelper, |
| 99 | @NonNull UserManager userManager, @NonNull IActivityManager am, int maxRunningUsers) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 100 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 101 | Log.d(TAG_USER, "constructed"); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 102 | } |
| 103 | mContext = context; |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 104 | mCarUserManagerHelper = carUserManagerHelper; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 105 | mAm = am; |
| 106 | mMaxRunningUsers = maxRunningUsers; |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 107 | mUserManager = userManager; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void init() { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 112 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 113 | Log.d(TAG_USER, "init"); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 114 | } |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void release() { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 119 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 120 | Log.d(TAG_USER, "release"); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 121 | } |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 125 | public void dump(@NonNull PrintWriter writer) { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 126 | checkAtLeastOnePermission("dump()", android.Manifest.permission.DUMP); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 127 | writer.println("*CarUserService*"); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 128 | synchronized (mLock) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 129 | writer.println("User0Unlocked: " + mUser0Unlocked); |
| 130 | writer.println("MaxRunningUsers: " + mMaxRunningUsers); |
| 131 | writer.println("BackgroundUsersToRestart: " + mBackgroundUsersToRestart); |
| 132 | writer.println("BackgroundUsersRestarted: " + mBackgroundUsersRestartedHere); |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 133 | List<UserInfo> allDrivers = getAllDrivers(); |
| 134 | int driversSize = allDrivers.size(); |
| 135 | writer.println("NumberOfDrivers: " + driversSize); |
| 136 | String prefix = " "; |
| 137 | for (int i = 0; i < driversSize; i++) { |
| 138 | int driverId = allDrivers.get(i).id; |
| 139 | writer.print(prefix + "#" + i + ": id=" + driverId); |
| 140 | List<UserInfo> passengers = getPassengers(driverId); |
| 141 | int passengersSize = passengers.size(); |
| 142 | writer.print(" NumberPassengers: " + passengersSize); |
| 143 | if (passengersSize > 0) { |
| 144 | writer.print(" ["); |
| 145 | for (int j = 0; j < passengersSize; j++) { |
| 146 | writer.print(passengers.get(j).id); |
| 147 | if (j < passengersSize - 1) { |
| 148 | writer.print(" "); |
| 149 | } |
| 150 | } |
| 151 | writer.print("]"); |
| 152 | } |
| 153 | writer.println(); |
| 154 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 155 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @see CarUserManager.createDriver |
| 160 | */ |
| 161 | @Override |
| 162 | @Nullable |
| 163 | public UserInfo createDriver(@NonNull String name, boolean admin) { |
| 164 | checkManageUsersPermission("createDriver"); |
| 165 | Preconditions.checkNotNull(name, "name cannot be null"); |
| 166 | if (admin) { |
| 167 | return createNewAdminUser(name); |
| 168 | } |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 169 | return mCarUserManagerHelper.createNewNonAdminUser(name); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @see CarUserManager.createPassenger |
| 174 | */ |
| 175 | @Override |
| 176 | @Nullable |
| 177 | public UserInfo createPassenger(@NonNull String name, @UserIdInt int driverId) { |
| 178 | checkManageUsersPermission("createPassenger"); |
| 179 | Preconditions.checkNotNull(name, "name cannot be null"); |
| 180 | UserInfo driver = mUserManager.getUserInfo(driverId); |
| 181 | if (driver == null) { |
| 182 | Log.w(TAG_USER, "the driver is invalid"); |
| 183 | return null; |
| 184 | } |
| 185 | if (driver.isGuest()) { |
| 186 | Log.w(TAG_USER, "a guest driver cannot create a passenger"); |
| 187 | return null; |
| 188 | } |
Bookatz | 42fb1a6 | 2019-10-30 11:45:01 -0700 | [diff] [blame^] | 189 | UserInfo user = mUserManager.createProfileForUser(name, |
| 190 | UserManager.USER_TYPE_PROFILE_MANAGED, /* flags */ 0, driverId); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 191 | if (user == null) { |
| 192 | // Couldn't create user, most likely because there are too many. |
| 193 | Log.w(TAG_USER, "can't create a profile for user" + driverId); |
| 194 | return null; |
| 195 | } |
| 196 | // Passenger user should be a non-admin user. |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 197 | mCarUserManagerHelper.setDefaultNonAdminRestrictions(user, /* enable= */ true); |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 198 | assignDefaultIcon(user); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 199 | return user; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @see CarUserManager.switchDriver |
| 204 | */ |
| 205 | @Override |
| 206 | public boolean switchDriver(@UserIdInt int driverId) { |
| 207 | checkManageUsersPermission("switchDriver"); |
| 208 | if (driverId == UserHandle.USER_SYSTEM && UserManager.isHeadlessSystemUserMode()) { |
| 209 | // System user doesn't associate with real person, can not be switched to. |
| 210 | Log.w(TAG_USER, "switching to system user in headless system user mode is not allowed"); |
| 211 | return false; |
| 212 | } |
| 213 | int userSwitchable = mUserManager.getUserSwitchability(); |
| 214 | if (userSwitchable != UserManager.SWITCHABILITY_STATUS_OK) { |
| 215 | Log.w(TAG_USER, "current process is not allowed to switch user"); |
| 216 | return false; |
| 217 | } |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 218 | if (driverId == ActivityManager.getCurrentUser()) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 219 | // The current user is already the given user. |
| 220 | return true; |
| 221 | } |
| 222 | try { |
| 223 | return mAm.switchUser(driverId); |
| 224 | } catch (RemoteException e) { |
| 225 | // ignore |
| 226 | Log.w(TAG_USER, "error while switching user", e); |
| 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @see CarUserManager.getAllDrivers |
| 233 | */ |
| 234 | @Override |
| 235 | @NonNull |
| 236 | public List<UserInfo> getAllDrivers() { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 237 | checkManageUsersOrDumpPermission("getAllDrivers"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 238 | return getUsers((user) -> { |
| 239 | return !isSystemUser(user.id) && user.isEnabled() && !user.isManagedProfile() |
| 240 | && !user.isEphemeral(); |
| 241 | }); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @see CarUserManager.getPassengers |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 246 | */ |
| 247 | @Override |
| 248 | @NonNull |
| 249 | public List<UserInfo> getPassengers(@UserIdInt int driverId) { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 250 | checkManageUsersOrDumpPermission("getPassengers"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 251 | return getUsers((user) -> { |
| 252 | return !isSystemUser(user.id) && user.isEnabled() && user.isManagedProfile() |
| 253 | && user.profileGroupId == driverId; |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @see CarUserManager.startPassenger |
| 259 | */ |
| 260 | @Override |
| 261 | public boolean startPassenger(@UserIdInt int passengerId, int zoneId) { |
| 262 | checkManageUsersPermission("startPassenger"); |
| 263 | // TODO(b/139190199): this method will be implemented when dynamic profile group is enabled. |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @see CarUserManager.stopPassenger |
| 269 | */ |
| 270 | @Override |
| 271 | public boolean stopPassenger(@UserIdInt int passengerId) { |
| 272 | checkManageUsersPermission("stopPassenger"); |
| 273 | // TODO(b/139190199): this method will be implemented when dynamic profile group is enabled. |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | /** Returns whether the given user is a system user. */ |
| 278 | private static boolean isSystemUser(@UserIdInt int userId) { |
| 279 | return userId == UserHandle.USER_SYSTEM; |
| 280 | } |
| 281 | |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 282 | private void updateDefaultUserRestriction() { |
| 283 | // We want to set restrictions on system and guest users only once. These are persisted |
| 284 | // onto disk, so it's sufficient to do it once + we minimize the number of disk writes. |
| 285 | if (Settings.Global.getInt(mContext.getContentResolver(), |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 286 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, /* default= */ 0) != 0) { |
| 287 | return; |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 288 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 289 | // Only apply the system user restrictions if the system user is headless. |
| 290 | if (UserManager.isHeadlessSystemUserMode()) { |
| 291 | setSystemUserRestrictions(); |
| 292 | } |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 293 | mCarUserManagerHelper.initDefaultGuestRestrictions(); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 294 | Settings.Global.putInt(mContext.getContentResolver(), |
| 295 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, 1); |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 298 | private boolean isPersistentUser(@UserIdInt int userId) { |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 299 | return !mUserManager.getUserInfo(userId).isEphemeral(); |
| 300 | } |
| 301 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 302 | /** Adds callback to listen to user activity events. */ |
| 303 | public void addUserCallback(@NonNull UserCallback callback) { |
| 304 | Preconditions.checkNotNull(callback, "callback cannot be null"); |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 305 | mUserCallbacks.add(callback); |
| 306 | } |
| 307 | |
Yabin Huang | 27712d7 | 2019-06-26 12:46:30 -0700 | [diff] [blame] | 308 | /** Removes previously added callback to listen user events. */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 309 | public void removeUserCallback(@NonNull UserCallback callback) { |
| 310 | Preconditions.checkNotNull(callback, "callback cannot be null"); |
Pavel Maltsev | 38da431 | 2019-04-08 10:38:38 -0700 | [diff] [blame] | 311 | mUserCallbacks.remove(callback); |
| 312 | } |
| 313 | |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 314 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 315 | * Sets user lock/unlocking status. This is coming from system server through ICar binder call. |
| 316 | * |
| 317 | * @param userId User id whoes lock status is changed. |
| 318 | * @param unlocked Unlocked (={@code true}) or locked (={@code false}). |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 319 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 320 | public void setUserLockStatus(@UserIdInt int userId, boolean unlocked) { |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 321 | for (UserCallback callback : mUserCallbacks) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 322 | callback.onUserLockChanged(userId, unlocked); |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 323 | } |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 324 | if (!unlocked) { // nothing else to do when it is locked back. |
| 325 | return; |
| 326 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 327 | ArrayList<Runnable> tasks = null; |
| 328 | synchronized (mLock) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 329 | if (userId == UserHandle.USER_SYSTEM) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 330 | if (!mUser0Unlocked) { // user 0, unlocked, do this only once |
| 331 | updateDefaultUserRestriction(); |
| 332 | tasks = new ArrayList<>(mUser0UnlockTasks); |
| 333 | mUser0UnlockTasks.clear(); |
| 334 | mUser0Unlocked = unlocked; |
| 335 | } |
| 336 | } else { // none user0 |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 337 | Integer user = userId; |
| 338 | if (isPersistentUser(userId)) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 339 | // current foreground user should stay in top priority. |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 340 | if (userId == ActivityManager.getCurrentUser()) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 341 | mBackgroundUsersToRestart.remove(user); |
| 342 | mBackgroundUsersToRestart.add(0, user); |
| 343 | } |
| 344 | // -1 for user 0 |
| 345 | if (mBackgroundUsersToRestart.size() > (mMaxRunningUsers - 1)) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 346 | int userToDrop = mBackgroundUsersToRestart.get( |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 347 | mBackgroundUsersToRestart.size() - 1); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 348 | Log.i(TAG_USER, "New user unlocked:" + userId |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 349 | + ", dropping least recently user from restart list:" + userToDrop); |
| 350 | // Drop the least recently used user. |
| 351 | mBackgroundUsersToRestart.remove(mBackgroundUsersToRestart.size() - 1); |
| 352 | } |
| 353 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | if (tasks != null && tasks.size() > 0) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 357 | Log.d(TAG_USER, "User0 unlocked, run queued tasks:" + tasks.size()); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 358 | for (Runnable r : tasks) { |
| 359 | r.run(); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 365 | * Starts all background users that were active in system. |
| 366 | * |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 367 | * @return list of background users started successfully. |
| 368 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 369 | @NonNull |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 370 | public ArrayList<Integer> startAllBackgroundUsers() { |
| 371 | ArrayList<Integer> users; |
| 372 | synchronized (mLock) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 373 | users = new ArrayList<>(mBackgroundUsersToRestart); |
| 374 | mBackgroundUsersRestartedHere.clear(); |
| 375 | mBackgroundUsersRestartedHere.addAll(mBackgroundUsersToRestart); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 376 | } |
| 377 | ArrayList<Integer> startedUsers = new ArrayList<>(); |
| 378 | for (Integer user : users) { |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 379 | if (user == ActivityManager.getCurrentUser()) { |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 380 | continue; |
| 381 | } |
| 382 | try { |
| 383 | if (mAm.startUserInBackground(user)) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 384 | if (mUserManager.isUserUnlockingOrUnlocked(user)) { |
| 385 | // already unlocked / unlocking. No need to unlock. |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 386 | startedUsers.add(user); |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 387 | } else if (mAm.unlockUser(user, null, null, null)) { |
| 388 | startedUsers.add(user); |
| 389 | } else { // started but cannot unlock |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 390 | Log.w(TAG_USER, "Background user started but cannot be unlocked:" + user); |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 391 | if (mUserManager.isUserRunning(user)) { |
| 392 | // add to started list so that it can be stopped later. |
| 393 | startedUsers.add(user); |
| 394 | } |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } catch (RemoteException e) { |
| 398 | // ignore |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 399 | Log.w(TAG_USER, "error while starting user in background", e); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 402 | // Keep only users that were re-started in mBackgroundUsersRestartedHere |
| 403 | synchronized (mLock) { |
| 404 | ArrayList<Integer> usersToRemove = new ArrayList<>(); |
| 405 | for (Integer user : mBackgroundUsersToRestart) { |
| 406 | if (!startedUsers.contains(user)) { |
| 407 | usersToRemove.add(user); |
| 408 | } |
| 409 | } |
| 410 | mBackgroundUsersRestartedHere.removeAll(usersToRemove); |
| 411 | } |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 412 | return startedUsers; |
| 413 | } |
| 414 | |
| 415 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 416 | * Stops all background users that were active in system. |
| 417 | * |
| 418 | * @return whether stopping succeeds. |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 419 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 420 | public boolean stopBackgroundUser(@UserIdInt int userId) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 421 | if (userId == UserHandle.USER_SYSTEM) { |
| 422 | return false; |
| 423 | } |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 424 | if (userId == ActivityManager.getCurrentUser()) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 425 | Log.i(TAG_USER, "stopBackgroundUser, already a FG user:" + userId); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 426 | return false; |
| 427 | } |
| 428 | try { |
| 429 | int r = mAm.stopUser(userId, true, null); |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 430 | if (r == ActivityManager.USER_OP_SUCCESS) { |
| 431 | synchronized (mLock) { |
| 432 | Integer user = userId; |
| 433 | mBackgroundUsersRestartedHere.remove(user); |
| 434 | } |
| 435 | } else if (r == ActivityManager.USER_OP_IS_CURRENT) { |
| 436 | return false; |
| 437 | } else { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 438 | Log.i(TAG_USER, "stopBackgroundUser failed, user:" + userId + " err:" + r); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | } catch (RemoteException e) { |
| 442 | // ignore |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 443 | Log.w(TAG_USER, "error while stopping user", e); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 444 | } |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | /** |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 449 | * Called when new foreground user started to boot. |
| 450 | * |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 451 | * @param userId User id of new user. |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 452 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 453 | public void onSwitchUser(@UserIdInt int userId) { |
| 454 | if (!isSystemUser(userId) && isPersistentUser(userId)) { |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 455 | mCarUserManagerHelper.setLastActiveUser(userId); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 456 | } |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 457 | for (UserCallback callback : mUserCallbacks) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 458 | callback.onSwitchUser(userId); |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| 462 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 463 | * Runs the given runnable when user 0 is unlocked. If user 0 is already unlocked, it is |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 464 | * run inside this call. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 465 | * |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 466 | * @param r Runnable to run. |
| 467 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 468 | public void runOnUser0Unlock(@NonNull Runnable r) { |
| 469 | Preconditions.checkNotNull(r, "runnable cannot be null"); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 470 | boolean runNow = false; |
| 471 | synchronized (mLock) { |
| 472 | if (mUser0Unlocked) { |
| 473 | runNow = true; |
| 474 | } else { |
| 475 | mUser0UnlockTasks.add(r); |
| 476 | } |
| 477 | } |
| 478 | if (runNow) { |
| 479 | r.run(); |
| 480 | } |
| 481 | } |
| 482 | |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 483 | @VisibleForTesting |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 484 | @NonNull |
| 485 | ArrayList<Integer> getBackgroundUsersToRestart() { |
| 486 | ArrayList<Integer> backgroundUsersToRestart = null; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 487 | synchronized (mLock) { |
| 488 | backgroundUsersToRestart = new ArrayList<>(mBackgroundUsersToRestart); |
| 489 | } |
| 490 | return backgroundUsersToRestart; |
| 491 | } |
| 492 | |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 493 | private void setSystemUserRestrictions() { |
| 494 | // Disable adding accounts for system user. |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 495 | UserHandle systemUserHandle = UserHandle.of(UserHandle.USER_SYSTEM); |
| 496 | mUserManager.setUserRestriction( |
| 497 | UserManager.DISALLOW_MODIFY_ACCOUNTS, /* value= */ true, systemUserHandle); |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 498 | |
| 499 | // Disable Location service for system user. |
| 500 | LocationManager locationManager = |
| 501 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 502 | locationManager.setLocationEnabledForUser(/* enabled= */ false, systemUserHandle); |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 503 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 504 | |
| 505 | /** |
| 506 | * Creates a new user on the system, the created user would be granted admin role. |
| 507 | * |
| 508 | * @param name Name to be given to the newly created user. |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 509 | * @return newly created admin user, {@code null} if it fails to create a user. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 510 | */ |
| 511 | @Nullable |
| 512 | private UserInfo createNewAdminUser(String name) { |
| 513 | if (!(mUserManager.isAdminUser() || mUserManager.isSystemUser())) { |
| 514 | // Only admins or system user can create other privileged users. |
| 515 | Log.e(TAG_USER, "Only admin users and system user can create other admins."); |
| 516 | return null; |
| 517 | } |
| 518 | |
| 519 | UserInfo user = mUserManager.createUser(name, UserInfo.FLAG_ADMIN); |
| 520 | if (user == null) { |
| 521 | // Couldn't create user, most likely because there are too many. |
| 522 | Log.w(TAG_USER, "can't create admin user."); |
| 523 | return null; |
| 524 | } |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 525 | assignDefaultIcon(user); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 526 | |
| 527 | return user; |
| 528 | } |
| 529 | |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 530 | /** |
| 531 | * Assigns a default icon to a user according to the user's id. |
| 532 | * |
| 533 | * @param userInfo User whose avatar is set to default icon. |
| 534 | * @return Bitmap of the user icon. |
| 535 | */ |
| 536 | private Bitmap assignDefaultIcon(UserInfo userInfo) { |
| 537 | int idForIcon = userInfo.isGuest() ? UserHandle.USER_NULL : userInfo.id; |
| 538 | Bitmap bitmap = UserIcons.convertToBitmap( |
| 539 | UserIcons.getDefaultUserIcon(mContext.getResources(), idForIcon, false)); |
| 540 | mUserManager.setUserIcon(userInfo.id, bitmap); |
| 541 | return bitmap; |
| 542 | } |
| 543 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 544 | private interface UserFilter { |
| 545 | boolean isEligibleUser(UserInfo user); |
| 546 | } |
| 547 | |
| 548 | /** Returns all users who are matched by the given filter. */ |
| 549 | private List<UserInfo> getUsers(UserFilter filter) { |
| 550 | List<UserInfo> users = mUserManager.getUsers(/* excludeDying= */ true); |
| 551 | |
| 552 | for (Iterator<UserInfo> iterator = users.iterator(); iterator.hasNext(); ) { |
| 553 | UserInfo user = iterator.next(); |
| 554 | if (!filter.isEligibleUser(user)) { |
| 555 | iterator.remove(); |
| 556 | } |
| 557 | } |
| 558 | return users; |
| 559 | } |
| 560 | |
| 561 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 562 | * Enforces that apps which have the |
| 563 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} |
| 564 | * can make certain calls to the CarUserManager. |
| 565 | * |
| 566 | * @param message used as message if SecurityException is thrown. |
| 567 | * @throws SecurityException if the caller is not system or root. |
| 568 | */ |
| 569 | private static void checkManageUsersPermission(String message) { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 570 | checkAtLeastOnePermission(message, android.Manifest.permission.MANAGE_USERS); |
| 571 | } |
| 572 | |
| 573 | private static void checkManageUsersOrDumpPermission(String message) { |
| 574 | checkAtLeastOnePermission(message, |
| 575 | android.Manifest.permission.MANAGE_USERS, |
| 576 | android.Manifest.permission.DUMP); |
| 577 | } |
| 578 | |
| 579 | private static void checkAtLeastOnePermission(String message, String...permissions) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 580 | int callingUid = Binder.getCallingUid(); |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 581 | if (!hasAtLeastOnePermissionGranted(callingUid, permissions)) { |
| 582 | throw new SecurityException("You need one of " + Arrays.toString(permissions) |
| 583 | + " to: " + message); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 587 | private static boolean hasAtLeastOnePermissionGranted(int uid, String... permissions) { |
| 588 | for (String permission : permissions) { |
| 589 | if (ActivityManager.checkComponentPermission(permission, uid, /* owningUid = */-1, |
| 590 | /* exported = */ true) |
| 591 | == android.content.pm.PackageManager.PERMISSION_GRANTED) { |
| 592 | return true; |
| 593 | } |
| 594 | } |
| 595 | return false; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 596 | } |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 597 | } |