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 | |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.CREATE_USERS; |
| 20 | import static android.Manifest.permission.MANAGE_USERS; |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 21 | import static android.car.drivingstate.CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP; |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 22 | |
Felipe Leme | 5523672 | 2020-10-16 16:54:32 -0700 | [diff] [blame] | 23 | import static com.android.car.PermissionHelper.checkHasAtLeastOnePermissionGranted; |
| 24 | import static com.android.car.PermissionHelper.checkHasDumpPermissionGranted; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 25 | |
| 26 | import android.annotation.NonNull; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 28 | import android.annotation.UserIdInt; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 29 | import android.app.ActivityManager; |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 30 | import android.app.ActivityTaskManager.RootTaskInfo; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 31 | import android.app.IActivityManager; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 32 | import android.car.CarOccupantZoneManager; |
| 33 | import android.car.CarOccupantZoneManager.OccupantTypeEnum; |
| 34 | import android.car.CarOccupantZoneManager.OccupantZoneInfo; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 35 | import android.car.ICarUserService; |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 36 | import android.car.drivingstate.CarUxRestrictions; |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 37 | import android.car.drivingstate.ICarUxRestrictionsChangeListener; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame] | 38 | import android.car.settings.CarSettings; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 39 | import android.car.user.CarUserManager; |
Mayank Garg | 3f56658 | 2020-10-02 22:10:16 -0700 | [diff] [blame] | 40 | import android.car.user.CarUserManager.UserIdentificationAssociationSetValue; |
| 41 | import android.car.user.CarUserManager.UserIdentificationAssociationType; |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 42 | import android.car.user.CarUserManager.UserLifecycleEvent; |
| 43 | import android.car.user.CarUserManager.UserLifecycleListener; |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 44 | import android.car.user.UserCreationResult; |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 45 | import android.car.user.UserIdentificationAssociationResponse; |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 46 | import android.car.user.UserRemovalResult; |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 47 | import android.car.user.UserStartResult; |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 48 | import android.car.user.UserStopResult; |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 49 | import android.car.user.UserSwitchResult; |
felipeal | 19e3d73 | 2020-03-18 12:07:32 -0700 | [diff] [blame] | 50 | import android.car.userlib.HalCallback; |
| 51 | import android.car.userlib.UserHalHelper; |
Mayank Garg | e90a408 | 2020-09-30 12:57:34 -0700 | [diff] [blame] | 52 | import android.car.userlib.UserHelper; |
Mayank Garg | a480dd9 | 2020-05-14 03:14:57 -0700 | [diff] [blame] | 53 | import android.content.ComponentName; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 54 | import android.content.Context; |
Mayank Garg | a480dd9 | 2020-05-14 03:14:57 -0700 | [diff] [blame] | 55 | import android.content.pm.PackageManager; |
| 56 | import android.content.pm.PackageManager.NameNotFoundException; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 57 | import android.content.pm.UserInfo; |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 58 | import android.content.pm.UserInfo.UserInfoFlag; |
Felipe Leme | 315a53b | 2020-03-12 10:51:04 -0700 | [diff] [blame] | 59 | import android.content.res.Resources; |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 60 | import android.graphics.Bitmap; |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 61 | import android.hardware.automotive.vehicle.V2_0.CreateUserRequest; |
| 62 | import android.hardware.automotive.vehicle.V2_0.CreateUserStatus; |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 63 | import android.hardware.automotive.vehicle.V2_0.InitialUserInfoRequestType; |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 64 | import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponseAction; |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 65 | import android.hardware.automotive.vehicle.V2_0.RemoveUserRequest; |
Mayank Garg | eb37d09 | 2020-06-02 14:37:57 -0700 | [diff] [blame] | 66 | import android.hardware.automotive.vehicle.V2_0.SwitchUserRequest; |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 67 | import android.hardware.automotive.vehicle.V2_0.SwitchUserStatus; |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 68 | import android.hardware.automotive.vehicle.V2_0.UserIdentificationGetRequest; |
| 69 | import android.hardware.automotive.vehicle.V2_0.UserIdentificationResponse; |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 70 | import android.hardware.automotive.vehicle.V2_0.UserIdentificationSetAssociation; |
| 71 | import android.hardware.automotive.vehicle.V2_0.UserIdentificationSetRequest; |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 72 | import android.hardware.automotive.vehicle.V2_0.UsersInfo; |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 73 | import android.location.LocationManager; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 74 | import android.os.Binder; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 75 | import android.os.Bundle; |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 76 | import android.os.Handler; |
| 77 | import android.os.HandlerThread; |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 78 | import android.os.Process; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 79 | import android.os.RemoteException; |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 80 | import android.os.Trace; |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 81 | import android.os.UserHandle; |
Ying Zheng | 8f90edb | 2018-06-13 12:42:31 -0700 | [diff] [blame] | 82 | import android.os.UserManager; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame] | 83 | import android.provider.Settings; |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 84 | import android.sysprop.CarProperties; |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 85 | import android.text.TextUtils; |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 86 | import android.util.EventLog; |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 87 | import android.util.IndentingPrintWriter; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 88 | import android.util.Log; |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 89 | import android.util.Slog; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 90 | import android.util.SparseArray; |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 91 | import android.util.SparseBooleanArray; |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 92 | import android.util.TimingsTraceLog; |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 93 | import android.view.Display; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 94 | |
Mayank Garg | 72c71d2 | 2021-02-03 23:54:45 -0800 | [diff] [blame] | 95 | import com.android.car.CarLog; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 96 | import com.android.car.CarServiceBase; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 97 | import com.android.car.CarServiceUtils; |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 98 | import com.android.car.CarUxRestrictionsManagerService; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 99 | import com.android.car.R; |
Felipe Leme | 5841220 | 2020-01-09 13:45:33 -0800 | [diff] [blame] | 100 | import com.android.car.hal.UserHalService; |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 101 | import com.android.car.internal.ICarServiceHelper; |
Mayank Garg | f59f95b | 2020-10-01 14:55:11 -0700 | [diff] [blame] | 102 | import com.android.car.internal.common.CommonConstants.UserLifecycleEventType; |
Mayank Garg | 801ea6a | 2020-09-29 15:43:49 -0700 | [diff] [blame] | 103 | import com.android.car.internal.common.EventLogTags; |
| 104 | import com.android.car.internal.common.UserHelperLite; |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 105 | import com.android.car.power.CarPowerManagementService; |
Mayank Garg | 665c20b | 2020-08-07 16:19:28 -0700 | [diff] [blame] | 106 | import com.android.car.user.InitialUserSetter.InitialUserInfo; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 107 | import com.android.internal.annotations.GuardedBy; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 108 | import com.android.internal.annotations.VisibleForTesting; |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 109 | import com.android.internal.infra.AndroidFuture; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 110 | import com.android.internal.os.IResultReceiver; |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 111 | import com.android.internal.util.ArrayUtils; |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 112 | import com.android.internal.util.FunctionalUtils; |
Mayank Garg | e19c292 | 2020-03-30 18:05:53 -0700 | [diff] [blame] | 113 | import com.android.internal.util.Preconditions; |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 114 | import com.android.internal.util.UserIcons; |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 115 | import com.android.server.utils.Slogf; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 116 | |
| 117 | import java.io.PrintWriter; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 118 | import java.util.ArrayList; |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 119 | import java.util.Arrays; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 120 | import java.util.Iterator; |
| 121 | import java.util.List; |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 122 | import java.util.Objects; |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 123 | import java.util.concurrent.CopyOnWriteArrayList; |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 124 | import java.util.concurrent.CountDownLatch; |
| 125 | import java.util.concurrent.TimeUnit; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * User service for cars. Manages users at boot time. Including: |
| 129 | * |
| 130 | * <ol> |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 131 | * <li> Creates a user used as driver. |
| 132 | * <li> Creates a user used as passenger. |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 133 | * <li> Creates a secondary admin user on first run. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 134 | * <li> Switch drivers. |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 135 | * <ol/> |
| 136 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 137 | public final class CarUserService extends ICarUserService.Stub implements CarServiceBase { |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 138 | |
Mayank Garg | 72c71d2 | 2021-02-03 23:54:45 -0800 | [diff] [blame] | 139 | private static final String TAG = CarLog.tagFor(CarUserService.class); |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 140 | |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 141 | /** {@code int} extra used to represent a user id in a {@link IResultReceiver} response. */ |
Mayank Garg | 8f93282 | 2020-09-17 16:09:12 -0700 | [diff] [blame] | 142 | public static final String BUNDLE_USER_ID = "user.id"; |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 143 | /** {@code int} extra used to represent user flags in a {@link IResultReceiver} response. */ |
Mayank Garg | 8f93282 | 2020-09-17 16:09:12 -0700 | [diff] [blame] | 144 | public static final String BUNDLE_USER_FLAGS = "user.flags"; |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 145 | /** {@code String} extra used to represent a user name in a {@link IResultReceiver} response. */ |
Mayank Garg | 8f93282 | 2020-09-17 16:09:12 -0700 | [diff] [blame] | 146 | public static final String BUNDLE_USER_NAME = "user.name"; |
felipeal | a68ecef | 2020-05-19 12:46:08 -0700 | [diff] [blame] | 147 | /** |
| 148 | * {@code int} extra used to represent the user locales in a {@link IResultReceiver} response. |
| 149 | */ |
Mayank Garg | 8f93282 | 2020-09-17 16:09:12 -0700 | [diff] [blame] | 150 | public static final String BUNDLE_USER_LOCALES = "user.locales"; |
felipeal | a68ecef | 2020-05-19 12:46:08 -0700 | [diff] [blame] | 151 | /** |
| 152 | * {@code int} extra used to represent the info action in a {@link IResultReceiver} response. |
| 153 | */ |
Mayank Garg | 8f93282 | 2020-09-17 16:09:12 -0700 | [diff] [blame] | 154 | public static final String BUNDLE_INITIAL_INFO_ACTION = "initial_info.action"; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 155 | |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 156 | public static final String VEHICLE_HAL_NOT_SUPPORTED = "Vehicle Hal not supported."; |
| 157 | |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 158 | public static final String HANDLER_THREAD_NAME = "UserService"; |
| 159 | |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 160 | private final Context mContext; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 161 | private final IActivityManager mAm; |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 162 | private final UserManager mUserManager; |
| 163 | private final int mMaxRunningUsers; |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 164 | private final InitialUserSetter mInitialUserSetter; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 165 | private final boolean mEnablePassengerSupport; |
Mayank Garg | 9732d60 | 2020-08-09 21:02:40 -0700 | [diff] [blame] | 166 | private final UserPreCreator mUserPreCreator; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 167 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 168 | private final Object mLockUser = new Object(); |
| 169 | @GuardedBy("mLockUser") |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 170 | private boolean mUser0Unlocked; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 171 | @GuardedBy("mLockUser") |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 172 | private final ArrayList<Runnable> mUser0UnlockTasks = new ArrayList<>(); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 173 | // Only one passenger is supported. |
| 174 | @GuardedBy("mLockUser") |
| 175 | private @UserIdInt int mLastPassengerId; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Background users that will be restarted in garage mode. This list can include the |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 178 | * current foreground user but the current foreground user should not be restarted. |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 179 | */ |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 180 | @GuardedBy("mLockUser") |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 181 | private final ArrayList<Integer> mBackgroundUsersToRestart = new ArrayList<>(); |
| 182 | /** |
| 183 | * Keep the list of background users started here. This is wholly for debugging purpose. |
| 184 | */ |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 185 | @GuardedBy("mLockUser") |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 186 | private final ArrayList<Integer> mBackgroundUsersRestartedHere = new ArrayList<>(); |
| 187 | |
Felipe Leme | 5841220 | 2020-01-09 13:45:33 -0800 | [diff] [blame] | 188 | private final UserHalService mHal; |
| 189 | |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 190 | private final HandlerThread mHandlerThread = CarServiceUtils.getHandlerThread( |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 191 | HANDLER_THREAD_NAME); |
| 192 | private final Handler mHandler; |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 193 | |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 194 | /** |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 195 | * List of listeners to be notified on new user activities events. |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 196 | * This collection should be accessed and manipulated by mHandlerThread only. |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 197 | */ |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 198 | private final List<UserLifecycleListener> mUserLifecycleListeners = new ArrayList<>(); |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 199 | |
| 200 | /** |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 201 | * List of lifecycle listeners by uid. |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 202 | * This collection should be accessed and manipulated by mHandlerThread only. |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 203 | */ |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 204 | private final SparseArray<IResultReceiver> mAppLifecycleListeners = new SparseArray<>(); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 205 | |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 206 | /** |
| 207 | * User Id for the user switch in process, if any. |
| 208 | */ |
| 209 | @GuardedBy("mLockUser") |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 210 | private int mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Request Id for the user switch in process, if any. |
| 213 | */ |
| 214 | @GuardedBy("mLockUser") |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 215 | private int mRequestIdForUserSwitchInProcess; |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 216 | private final int mHalTimeoutMs = CarProperties.user_hal_timeout().orElse(5_000); |
| 217 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 218 | private final CopyOnWriteArrayList<PassengerCallback> mPassengerCallbacks = |
| 219 | new CopyOnWriteArrayList<>(); |
| 220 | |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 221 | // TODO(b/163566866): Use mSwitchGuestUserBeforeSleep for new create guest request |
| 222 | private final boolean mSwitchGuestUserBeforeSleep; |
| 223 | |
felipeal | 61ce373 | 2020-04-03 11:01:00 -0700 | [diff] [blame] | 224 | @Nullable |
| 225 | @GuardedBy("mLockUser") |
| 226 | private UserInfo mInitialUser; |
| 227 | |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 228 | private IResultReceiver mUserSwitchUiReceiver; |
| 229 | |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 230 | private final CarUxRestrictionsManagerService mCarUxRestrictionService; |
| 231 | |
| 232 | /** |
| 233 | * Whether some operations - like user switch - are restricted by driving safety constraints. |
| 234 | */ |
| 235 | @GuardedBy("mLockUser") |
| 236 | private boolean mUxRestricted; |
| 237 | |
| 238 | /** |
Mayank Garg | 784a086 | 2021-05-26 00:20:50 -0700 | [diff] [blame^] | 239 | * If {@code false}, garage mode operations (background users start at garage mode entry and |
| 240 | * background users stop at garage mode exit) will be skipped. Controlled using car shell |
| 241 | * command {@code adb shell set-start-bg-users-on-garage-mode [true|false]} |
| 242 | * Purpose: Garage mode testing and simulation |
| 243 | */ |
| 244 | @GuardedBy("mLockUser") |
| 245 | private boolean mStartBackgroundUsersOnGarageMode = true; |
| 246 | |
| 247 | /** |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 248 | * Callback to notify {@code CarServiceHelper} about driving safety changes (through |
| 249 | * {@link ICarServiceHelper#setSafetyMode(boolean). |
| 250 | * |
| 251 | * <p>NOTE: in theory, that logic should belong to {@code CarDevicePolicyService}, but it's |
| 252 | * simpler to do it here (and that service already depends on this one). |
| 253 | */ |
| 254 | @GuardedBy("mLockUser") |
| 255 | private ICarServiceHelper mICarServiceHelper; |
| 256 | |
| 257 | private final ICarUxRestrictionsChangeListener mCarUxRestrictionsChangeListener = |
| 258 | new ICarUxRestrictionsChangeListener.Stub() { |
| 259 | @Override |
| 260 | public void onUxRestrictionsChanged(CarUxRestrictions restrictions) { |
| 261 | setUxRestrictions(restrictions); |
| 262 | } |
| 263 | }; |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 264 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 265 | /** Interface for callbaks related to passenger activities. */ |
| 266 | public interface PassengerCallback { |
| 267 | /** Called when passenger is started at a certain zone. */ |
| 268 | void onPassengerStarted(@UserIdInt int passengerId, int zoneId); |
| 269 | /** Called when passenger is stopped. */ |
| 270 | void onPassengerStopped(@UserIdInt int passengerId); |
| 271 | } |
| 272 | |
| 273 | /** Interface for delegating zone-related implementation to CarOccupantZoneService. */ |
| 274 | public interface ZoneUserBindingHelper { |
| 275 | /** Gets occupant zones corresponding to the occupant type. */ |
| 276 | @NonNull |
| 277 | List<OccupantZoneInfo> getOccupantZones(@OccupantTypeEnum int occupantType); |
| 278 | /** Assigns the user to the occupant zone. */ |
| 279 | boolean assignUserToOccupantZone(@UserIdInt int userId, int zoneId); |
| 280 | /** Makes the occupant zone unoccupied. */ |
| 281 | boolean unassignUserFromOccupantZone(@UserIdInt int userId); |
| 282 | /** Returns whether there is a passenger display. */ |
| 283 | boolean isPassengerDisplayAvailable(); |
| 284 | } |
| 285 | |
| 286 | private final Object mLockHelper = new Object(); |
| 287 | @GuardedBy("mLockHelper") |
| 288 | private ZoneUserBindingHelper mZoneUserBindingHelper; |
| 289 | |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 290 | /** Map used to avoid calling UserHAL when a user was removed because HAL creation failed. */ |
| 291 | @GuardedBy("mLockUser") |
| 292 | private final SparseBooleanArray mFailedToCreateUserIds = new SparseBooleanArray(1); |
| 293 | |
Felipe Leme | 5841220 | 2020-01-09 13:45:33 -0800 | [diff] [blame] | 294 | public CarUserService(@NonNull Context context, @NonNull UserHalService hal, |
Mayank Garg | e90a408 | 2020-09-30 12:57:34 -0700 | [diff] [blame] | 295 | @NonNull UserManager userManager, |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 296 | @NonNull IActivityManager am, int maxRunningUsers, |
| 297 | @NonNull CarUxRestrictionsManagerService uxRestrictionService) { |
Mayank Garg | e90a408 | 2020-09-30 12:57:34 -0700 | [diff] [blame] | 298 | this(context, hal, userManager, am, maxRunningUsers, |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 299 | /* initialUserSetter= */ null, /* userPreCreator= */ null, uxRestrictionService, |
| 300 | null); |
Mayank Garg | 71661ea | 2020-04-29 01:25:03 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @VisibleForTesting |
| 304 | CarUserService(@NonNull Context context, @NonNull UserHalService hal, |
Mayank Garg | e90a408 | 2020-09-30 12:57:34 -0700 | [diff] [blame] | 305 | @NonNull UserManager userManager, |
Mayank Garg | ccad806 | 2020-08-30 15:05:10 -0700 | [diff] [blame] | 306 | @NonNull IActivityManager am, int maxRunningUsers, |
Mayank Garg | 1bb1c38 | 2020-09-03 17:11:11 -0700 | [diff] [blame] | 307 | @Nullable InitialUserSetter initialUserSetter, |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 308 | @Nullable UserPreCreator userPreCreator, |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 309 | @NonNull CarUxRestrictionsManagerService uxRestrictionService, |
| 310 | @Nullable Handler handler) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 311 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
Felipe Leme | 8725a2f | 2020-12-09 16:04:51 -0800 | [diff] [blame] | 312 | Slog.d(TAG, "constructed for user " + context.getUserId()); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 313 | } |
| 314 | mContext = context; |
Felipe Leme | 5841220 | 2020-01-09 13:45:33 -0800 | [diff] [blame] | 315 | mHal = hal; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 316 | mAm = am; |
| 317 | mMaxRunningUsers = maxRunningUsers; |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 318 | mUserManager = userManager; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 319 | mLastPassengerId = UserHandle.USER_NULL; |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 320 | mHandler = handler == null ? new Handler(mHandlerThread.getLooper()) : handler; |
Mayank Garg | 1bb1c38 | 2020-09-03 17:11:11 -0700 | [diff] [blame] | 321 | mInitialUserSetter = |
Felipe Leme | cceb299 | 2020-11-18 18:22:44 -0800 | [diff] [blame] | 322 | initialUserSetter == null ? new InitialUserSetter(context, this, |
| 323 | (u) -> setInitialUser(u)) : initialUserSetter; |
Mayank Garg | 1bb1c38 | 2020-09-03 17:11:11 -0700 | [diff] [blame] | 324 | mUserPreCreator = |
| 325 | userPreCreator == null ? new UserPreCreator(mUserManager) : userPreCreator; |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 326 | Resources resources = context.getResources(); |
| 327 | mEnablePassengerSupport = resources.getBoolean(R.bool.enablePassengerSupport); |
| 328 | mSwitchGuestUserBeforeSleep = resources.getBoolean( |
| 329 | R.bool.config_switchGuestUserBeforeGoingSleep); |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 330 | mCarUxRestrictionService = uxRestrictionService; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void init() { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 335 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 336 | Slog.d(TAG, "init()"); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 337 | } |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 338 | mCarUxRestrictionService.registerUxRestrictionsChangeListener( |
| 339 | mCarUxRestrictionsChangeListener, Display.DEFAULT_DISPLAY); |
| 340 | |
| 341 | setUxRestrictions(mCarUxRestrictionService.getCurrentUxRestrictions()); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void release() { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 346 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 347 | Slog.d(TAG, "release()"); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 348 | } |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 349 | mCarUxRestrictionService |
| 350 | .unregisterUxRestrictionsChangeListener(mCarUxRestrictionsChangeListener); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 354 | public void dump(@NonNull IndentingPrintWriter writer) { |
Felipe Leme | 5523672 | 2020-10-16 16:54:32 -0700 | [diff] [blame] | 355 | checkHasDumpPermissionGranted("dump()"); |
| 356 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 357 | writer.println("*CarUserService*"); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 358 | String indent = " "; |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 359 | handleDumpListeners(writer, indent); |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 360 | writer.printf("User switch UI receiver %s\n", mUserSwitchUiReceiver); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 361 | synchronized (mLockUser) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 362 | writer.println("User0Unlocked: " + mUser0Unlocked); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 363 | writer.println("BackgroundUsersToRestart: " + mBackgroundUsersToRestart); |
| 364 | writer.println("BackgroundUsersRestarted: " + mBackgroundUsersRestartedHere); |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 365 | if (mFailedToCreateUserIds.size() > 0) { |
| 366 | writer.println("FailedToCreateUserIds: " + mFailedToCreateUserIds); |
| 367 | } |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 368 | writer.printf("Is UX restricted: %b\n", mUxRestricted); |
Mayank Garg | 784a086 | 2021-05-26 00:20:50 -0700 | [diff] [blame^] | 369 | writer.printf("Start Background Users On Garage Mode=%s\n", |
| 370 | mStartBackgroundUsersOnGarageMode); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 371 | } |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 372 | |
| 373 | writer.println("SwitchGuestUserBeforeSleep: " + mSwitchGuestUserBeforeSleep); |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 374 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 375 | writer.println("MaxRunningUsers: " + mMaxRunningUsers); |
| 376 | List<UserInfo> allDrivers = getAllDrivers(); |
| 377 | int driversSize = allDrivers.size(); |
| 378 | writer.println("NumberOfDrivers: " + driversSize); |
| 379 | for (int i = 0; i < driversSize; i++) { |
| 380 | int driverId = allDrivers.get(i).id; |
| 381 | writer.print(indent + "#" + i + ": id=" + driverId); |
| 382 | List<UserInfo> passengers = getPassengers(driverId); |
| 383 | int passengersSize = passengers.size(); |
| 384 | writer.print(" NumberPassengers: " + passengersSize); |
| 385 | if (passengersSize > 0) { |
| 386 | writer.print(" ["); |
| 387 | for (int j = 0; j < passengersSize; j++) { |
| 388 | writer.print(passengers.get(j).id); |
| 389 | if (j < passengersSize - 1) { |
| 390 | writer.print(" "); |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 391 | } |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 392 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 393 | writer.print("]"); |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 394 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 395 | writer.println(); |
| 396 | } |
| 397 | writer.printf("EnablePassengerSupport: %s\n", mEnablePassengerSupport); |
| 398 | writer.printf("User HAL timeout: %dms\n", mHalTimeoutMs); |
| 399 | writer.printf("Initial user: %s\n", mInitialUser); |
felipeal | bf32765 | 2020-06-03 11:33:29 -0700 | [diff] [blame] | 400 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 401 | writer.println("Relevant overlayable properties"); |
| 402 | Resources res = mContext.getResources(); |
| 403 | writer.printf("%sowner_name=%s\n", indent, |
| 404 | res.getString(com.android.internal.R.string.owner_name)); |
| 405 | writer.printf("%sdefault_guest_name=%s\n", indent, |
| 406 | res.getString(R.string.default_guest_name)); |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 407 | writer.printf("User switch in process=%d\n", mUserIdForUserSwitchInProcess); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 408 | writer.printf("Request Id for the user switch in process=%d\n ", |
| 409 | mRequestIdForUserSwitchInProcess); |
Mayank Garg | a480dd9 | 2020-05-14 03:14:57 -0700 | [diff] [blame] | 410 | writer.printf("System UI package name=%s\n", getSystemUiPackageName()); |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 411 | |
felipeal | bf32765 | 2020-06-03 11:33:29 -0700 | [diff] [blame] | 412 | writer.println("Relevant Global settings"); |
| 413 | dumpGlobalProperty(writer, indent, CarSettings.Global.LAST_ACTIVE_USER_ID); |
| 414 | dumpGlobalProperty(writer, indent, CarSettings.Global.LAST_ACTIVE_PERSISTENT_USER_ID); |
| 415 | |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 416 | mInitialUserSetter.dump(writer); |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 417 | } |
| 418 | |
felipeal | bf32765 | 2020-06-03 11:33:29 -0700 | [diff] [blame] | 419 | private void dumpGlobalProperty(PrintWriter writer, String indent, String property) { |
| 420 | String value = Settings.Global.getString(mContext.getContentResolver(), property); |
| 421 | writer.printf("%s%s=%s\n", indent, property, value); |
| 422 | } |
| 423 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 424 | private void handleDumpListeners(@NonNull PrintWriter writer, String indent) { |
| 425 | CountDownLatch latch = new CountDownLatch(1); |
| 426 | mHandler.post(() -> { |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 427 | handleDumpServiceLifecycleListeners(writer); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 428 | handleDumpAppLifecycleListeners(writer, indent); |
| 429 | latch.countDown(); |
| 430 | }); |
| 431 | int timeout = 5; |
| 432 | try { |
| 433 | if (!latch.await(timeout, TimeUnit.SECONDS)) { |
| 434 | writer.printf("Handler thread didn't respond in %ds when dumping listeners\n", |
| 435 | timeout); |
| 436 | } |
| 437 | } catch (InterruptedException e) { |
| 438 | Thread.currentThread().interrupt(); |
| 439 | writer.println("Interrupted waiting for handler thread to dump app and user listeners"); |
| 440 | } |
| 441 | } |
| 442 | |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 443 | private void handleDumpServiceLifecycleListeners(@NonNull PrintWriter writer) { |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 444 | if (mUserLifecycleListeners.isEmpty()) { |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 445 | writer.println("No lifecycle listeners for internal services"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 448 | int size = mUserLifecycleListeners.size(); |
| 449 | writer.printf("%d lifecycle listener%s for services\n", size, size == 1 ? "" : "s"); |
| 450 | String indent = " "; |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 451 | for (UserLifecycleListener listener : mUserLifecycleListeners) { |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 452 | writer.printf("%s%s\n", indent, FunctionalUtils.getLambdaName(listener)); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | private void handleDumpAppLifecycleListeners(@NonNull PrintWriter writer, String indent) { |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 457 | int size = mAppLifecycleListeners.size(); |
| 458 | if (size == 0) { |
| 459 | writer.println("No lifecycle listeners for apps"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 460 | return; |
| 461 | } |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 462 | writer.printf("%d lifecycle listener%s for apps \n", size, size == 1 ? "" : "s"); |
| 463 | for (int i = 0; i < size; i++) { |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 464 | int uid = mAppLifecycleListeners.keyAt(i); |
| 465 | IResultReceiver listener = mAppLifecycleListeners.valueAt(i); |
Felipe Leme | ea4b532 | 2020-10-26 15:33:36 -0700 | [diff] [blame] | 466 | writer.printf("%suid: %d listener: %s\n", indent, uid, |
| 467 | FunctionalUtils.getLambdaName(listener)); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 468 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 472 | * @see ExperimentalCarUserManager.createDriver |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 473 | */ |
| 474 | @Override |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 475 | public AndroidFuture<UserCreationResult> createDriver(@NonNull String name, boolean admin) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 476 | checkManageUsersPermission("createDriver"); |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 477 | Objects.requireNonNull(name, "name cannot be null"); |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 478 | |
| 479 | AndroidFuture<UserCreationResult> future = new AndroidFuture<UserCreationResult>() { |
| 480 | @Override |
| 481 | protected void onCompleted(UserCreationResult result, Throwable err) { |
| 482 | if (result == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 483 | Slog.w(TAG, "createDriver(" + name + "," + admin + ") failed: " + err); |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 484 | } else { |
| 485 | if (result.getStatus() == UserCreationResult.STATUS_SUCCESSFUL) { |
| 486 | assignDefaultIcon(result.getUser()); |
| 487 | } |
| 488 | } |
| 489 | super.onCompleted(result, err); |
| 490 | }; |
| 491 | }; |
| 492 | int flags = 0; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 493 | if (admin) { |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 494 | if (!(mUserManager.isAdminUser() || mUserManager.isSystemUser())) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 495 | Slog.e(TAG, "Only admin users and system user can create other admins."); |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 496 | sendUserCreationResultFailure(future, UserCreationResult.STATUS_INVALID_REQUEST); |
| 497 | return future; |
| 498 | } |
| 499 | flags = UserInfo.FLAG_ADMIN; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 500 | } |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 501 | createUser(name, UserInfo.getDefaultUserType(flags), flags, mHalTimeoutMs, future); |
| 502 | return future; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /** |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 506 | * @see ExperimentalCarUserManager.createPassenger |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 507 | */ |
| 508 | @Override |
| 509 | @Nullable |
| 510 | public UserInfo createPassenger(@NonNull String name, @UserIdInt int driverId) { |
| 511 | checkManageUsersPermission("createPassenger"); |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 512 | Objects.requireNonNull(name, "name cannot be null"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 513 | UserInfo driver = mUserManager.getUserInfo(driverId); |
| 514 | if (driver == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 515 | Slog.w(TAG, "the driver is invalid"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 516 | return null; |
| 517 | } |
| 518 | if (driver.isGuest()) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 519 | Slog.w(TAG, "a guest driver cannot create a passenger"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 520 | return null; |
| 521 | } |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 522 | // createPassenger doesn't use user HAL because user HAL doesn't support profile user yet. |
Bookatz | 42fb1a6 | 2019-10-30 11:45:01 -0700 | [diff] [blame] | 523 | UserInfo user = mUserManager.createProfileForUser(name, |
| 524 | UserManager.USER_TYPE_PROFILE_MANAGED, /* flags */ 0, driverId); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 525 | if (user == null) { |
| 526 | // Couldn't create user, most likely because there are too many. |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 527 | Slog.w(TAG, "can't create a profile for user" + driverId); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 528 | return null; |
| 529 | } |
| 530 | // Passenger user should be a non-admin user. |
Mayank Garg | e90a408 | 2020-09-30 12:57:34 -0700 | [diff] [blame] | 531 | UserHelper.setDefaultNonAdminRestrictions(mContext, user, /* enable= */ true); |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 532 | assignDefaultIcon(user); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 533 | return user; |
| 534 | } |
| 535 | |
| 536 | /** |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 537 | * @see ExperimentalCarUserManager.switchDriver |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 538 | */ |
| 539 | @Override |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame] | 540 | public void switchDriver(@UserIdInt int driverId, AndroidFuture<UserSwitchResult> receiver) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 541 | checkManageUsersPermission("switchDriver"); |
Mayank Garg | 94f3eb9 | 2020-08-12 12:38:58 -0700 | [diff] [blame] | 542 | if (UserHelperLite.isHeadlessSystemUser(driverId)) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 543 | // System user doesn't associate with real person, can not be switched to. |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 544 | Slog.w(TAG, "switching to system user in headless system user mode is not allowed"); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 545 | sendUserSwitchResult(receiver, UserSwitchResult.STATUS_INVALID_REQUEST); |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame] | 546 | return; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 547 | } |
| 548 | int userSwitchable = mUserManager.getUserSwitchability(); |
| 549 | if (userSwitchable != UserManager.SWITCHABILITY_STATUS_OK) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 550 | Slog.w(TAG, "current process is not allowed to switch user"); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 551 | sendUserSwitchResult(receiver, UserSwitchResult.STATUS_INVALID_REQUEST); |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame] | 552 | return; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 553 | } |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame] | 554 | switchUser(driverId, mHalTimeoutMs, receiver); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /** |
Eric Jeong | e7916fc | 2019-12-20 14:03:34 -0800 | [diff] [blame] | 558 | * Returns all drivers who can occupy the driving zone. Guest users are included in the list. |
| 559 | * |
| 560 | * @return the list of {@link UserInfo} who can be a driver on the device. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 561 | */ |
| 562 | @Override |
| 563 | @NonNull |
| 564 | public List<UserInfo> getAllDrivers() { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 565 | checkManageUsersOrDumpPermission("getAllDrivers"); |
Mayank Garg | 94f3eb9 | 2020-08-12 12:38:58 -0700 | [diff] [blame] | 566 | return getUsers((user) -> !UserHelperLite.isHeadlessSystemUser(user.id) && user.isEnabled() |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 567 | && !user.isManagedProfile() && !user.isEphemeral()); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
Eric Jeong | e7916fc | 2019-12-20 14:03:34 -0800 | [diff] [blame] | 571 | * Returns all passengers under the given driver. |
| 572 | * |
| 573 | * @param driverId User id of a driver. |
| 574 | * @return the list of {@link UserInfo} who is a passenger under the given driver. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 575 | */ |
| 576 | @Override |
| 577 | @NonNull |
| 578 | public List<UserInfo> getPassengers(@UserIdInt int driverId) { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 579 | checkManageUsersOrDumpPermission("getPassengers"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 580 | return getUsers((user) -> { |
Mayank Garg | 94f3eb9 | 2020-08-12 12:38:58 -0700 | [diff] [blame] | 581 | return !UserHelperLite.isHeadlessSystemUser(user.id) && user.isEnabled() |
Eric Jeong | 40f8fa3 | 2020-05-12 12:23:33 -0700 | [diff] [blame] | 582 | && user.isManagedProfile() && user.profileGroupId == driverId; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 583 | }); |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * @see CarUserManager.startPassenger |
| 588 | */ |
| 589 | @Override |
| 590 | public boolean startPassenger(@UserIdInt int passengerId, int zoneId) { |
| 591 | checkManageUsersPermission("startPassenger"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 592 | synchronized (mLockUser) { |
| 593 | try { |
| 594 | if (!mAm.startUserInBackgroundWithListener(passengerId, null)) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 595 | Slog.w(TAG, "could not start passenger"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 596 | return false; |
| 597 | } |
| 598 | } catch (RemoteException e) { |
| 599 | // ignore |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 600 | Slog.w(TAG, "error while starting passenger", e); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 601 | return false; |
| 602 | } |
| 603 | if (!assignUserToOccupantZone(passengerId, zoneId)) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 604 | Slog.w(TAG, "could not assign passenger to zone"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 605 | return false; |
| 606 | } |
| 607 | mLastPassengerId = passengerId; |
| 608 | } |
| 609 | for (PassengerCallback callback : mPassengerCallbacks) { |
| 610 | callback.onPassengerStarted(passengerId, zoneId); |
| 611 | } |
| 612 | return true; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /** |
| 616 | * @see CarUserManager.stopPassenger |
| 617 | */ |
| 618 | @Override |
| 619 | public boolean stopPassenger(@UserIdInt int passengerId) { |
| 620 | checkManageUsersPermission("stopPassenger"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 621 | return stopPassengerInternal(passengerId, true); |
| 622 | } |
| 623 | |
| 624 | private boolean stopPassengerInternal(@UserIdInt int passengerId, boolean checkCurrentDriver) { |
| 625 | synchronized (mLockUser) { |
| 626 | UserInfo passenger = mUserManager.getUserInfo(passengerId); |
| 627 | if (passenger == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 628 | Slog.w(TAG, "passenger " + passengerId + " doesn't exist"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 629 | return false; |
| 630 | } |
| 631 | if (mLastPassengerId != passengerId) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 632 | Slog.w(TAG, "passenger " + passengerId + " hasn't been started"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 633 | return true; |
| 634 | } |
| 635 | if (checkCurrentDriver) { |
| 636 | int currentUser = ActivityManager.getCurrentUser(); |
| 637 | if (passenger.profileGroupId != currentUser) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 638 | Slog.w(TAG, "passenger " + passengerId |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 639 | + " is not a profile of the current user"); |
| 640 | return false; |
| 641 | } |
| 642 | } |
| 643 | // Passenger is a profile, so cannot be stopped through activity manager. |
| 644 | // Instead, activities started by the passenger are stopped and the passenger is |
| 645 | // unassigned from the zone. |
| 646 | stopAllTasks(passengerId); |
| 647 | if (!unassignUserFromOccupantZone(passengerId)) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 648 | Slog.w(TAG, "could not unassign user from occupant zone"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 649 | return false; |
| 650 | } |
| 651 | mLastPassengerId = UserHandle.USER_NULL; |
| 652 | } |
| 653 | for (PassengerCallback callback : mPassengerCallbacks) { |
| 654 | callback.onPassengerStopped(passengerId); |
| 655 | } |
| 656 | return true; |
| 657 | } |
| 658 | |
| 659 | private void stopAllTasks(@UserIdInt int userId) { |
| 660 | try { |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 661 | for (RootTaskInfo info : mAm.getAllRootTaskInfos()) { |
| 662 | for (int i = 0; i < info.childTaskIds.length; i++) { |
| 663 | if (info.childTaskUserIds[i] == userId) { |
| 664 | int taskId = info.childTaskIds[i]; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 665 | if (!mAm.removeTask(taskId)) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 666 | Slog.w(TAG, "could not remove task " + taskId); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } catch (RemoteException e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 672 | Slog.e(TAG, "could not get stack info", e); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 673 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 676 | @Override |
| 677 | public void setLifecycleListenerForUid(IResultReceiver listener) { |
| 678 | int uid = Binder.getCallingUid(); |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 679 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SET_LIFECYCLE_LISTENER, uid); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 680 | checkInteractAcrossUsersPermission("setLifecycleListenerForUid" + uid); |
| 681 | |
| 682 | try { |
| 683 | listener.asBinder().linkToDeath(() -> onListenerDeath(uid), 0); |
| 684 | } catch (RemoteException e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 685 | Slog.wtf(TAG, "Cannot listen to death of " + uid); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 686 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 687 | mHandler.post(() -> mAppLifecycleListeners.append(uid, listener)); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | private void onListenerDeath(int uid) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 691 | Slog.i(TAG, "Removing listeners for uid " + uid + " on binder death"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 692 | mHandler.post(() -> mAppLifecycleListeners.remove(uid)); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | @Override |
| 696 | public void resetLifecycleListenerForUid() { |
| 697 | int uid = Binder.getCallingUid(); |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 698 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_RESET_LIFECYCLE_LISTENER, uid); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 699 | checkInteractAcrossUsersPermission("resetLifecycleListenerForUid-" + uid); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 700 | mHandler.post(() -> mAppLifecycleListeners.remove(uid)); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 703 | /** |
felipeal | 61ce373 | 2020-04-03 11:01:00 -0700 | [diff] [blame] | 704 | * Gets the initial foreground user after the device boots or resumes from suspension. |
| 705 | * |
| 706 | * <p>When the OEM supports the User HAL, the initial user won't be available until the HAL |
| 707 | * returns the initial value to {@code CarService} - if HAL takes too long or times out, this |
| 708 | * method returns {@code null}. |
| 709 | * |
| 710 | * <p>If the HAL eventually times out, {@code CarService} will fallback to its default behavior |
| 711 | * (like switching to the last active user), and this method will return the result of such |
| 712 | * operation. |
| 713 | * |
| 714 | * <p>Notice that if {@code CarService} crashes, subsequent calls to this method will return |
| 715 | * {@code null}. |
| 716 | * |
| 717 | * @hide |
| 718 | */ |
| 719 | @Nullable |
| 720 | public UserInfo getInitialUser() { |
| 721 | checkInteractAcrossUsersPermission("getInitialUser"); |
| 722 | synchronized (mLockUser) { |
| 723 | return mInitialUser; |
| 724 | } |
| 725 | } |
| 726 | |
felipeal | 61ce373 | 2020-04-03 11:01:00 -0700 | [diff] [blame] | 727 | /** |
| 728 | * Sets the initial foreground user after the device boots or resumes from suspension. |
| 729 | */ |
| 730 | public void setInitialUser(@Nullable UserInfo user) { |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 731 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SET_INITIAL_USER, |
| 732 | user == null ? UserHandle.USER_NULL : user.id); |
felipeal | 61ce373 | 2020-04-03 11:01:00 -0700 | [diff] [blame] | 733 | synchronized (mLockUser) { |
| 734 | mInitialUser = user; |
| 735 | } |
| 736 | if (user == null) { |
| 737 | // This mean InitialUserSetter failed and could not fallback, so the initial user was |
| 738 | // not switched (and most likely is SYSTEM_USER). |
| 739 | // TODO(b/153104378): should we set it to ActivityManager.getCurrentUser() instead? |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 740 | Slog.wtf(TAG, "Initial user set to null"); |
felipeal | 61ce373 | 2020-04-03 11:01:00 -0700 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 744 | private void initResumeReplaceGuest() { |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 745 | int currentUserId = ActivityManager.getCurrentUser(); |
| 746 | UserInfo currentUser = mUserManager.getUserInfo(currentUserId); |
| 747 | |
| 748 | if (!mInitialUserSetter.canReplaceGuestUser(currentUser)) return; // Not a guest |
| 749 | |
| 750 | InitialUserInfo info = |
| 751 | new InitialUserSetter.Builder(InitialUserSetter.TYPE_REPLACE_GUEST).build(); |
| 752 | |
| 753 | mInitialUserSetter.set(info); |
| 754 | } |
| 755 | |
| 756 | /** |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 757 | * Calls to switch user at the power suspend. |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 758 | * |
| 759 | * <p><b>Note:</b> Should be used only by {@link CarPowerManagementService} |
| 760 | * |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 761 | */ |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 762 | public void onSuspend() { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 763 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 764 | Slog.d(TAG, "onSuspend called."); |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 767 | if (mSwitchGuestUserBeforeSleep) { |
| 768 | initResumeReplaceGuest(); |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 769 | } |
Mayank Garg | 1bb1c38 | 2020-09-03 17:11:11 -0700 | [diff] [blame] | 770 | |
Mayank Garg | a3b72d0 | 2021-03-14 23:05:51 -0700 | [diff] [blame] | 771 | preCreateUsersInternal(); |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /** |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 775 | * Calls to switch user at the power resume. |
| 776 | * |
| 777 | * <p> |
| 778 | * <b>Note:</b> Should be used only by {@link CarPowerManagementService} |
| 779 | * |
| 780 | */ |
| 781 | public void onResume() { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 782 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 783 | Slog.d(TAG, "onResume called."); |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 786 | mHandler.post(() -> initBootUser(InitialUserInfoRequestType.RESUME)); |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /** |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 790 | * Calls to start user at the android startup. |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 791 | */ |
| 792 | public void initBootUser() { |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 793 | mHandler.post(() -> initBootUser(getInitialUserInfoRequestType())); |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 796 | private void initBootUser(int requestType) { |
| 797 | boolean replaceGuest = |
| 798 | requestType == InitialUserInfoRequestType.RESUME && !mSwitchGuestUserBeforeSleep; |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 799 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_REQ, requestType, |
| 800 | mHalTimeoutMs); |
| 801 | checkManageUsersPermission("startInitialUser"); |
| 802 | |
| 803 | if (!isUserHalSupported()) { |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 804 | fallbackToDefaultInitialUserBehavior(/* userLocales= */ null, replaceGuest); |
Mayank Garg | fd1e069 | 2021-05-25 13:33:48 -0700 | [diff] [blame] | 805 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_REQ_COMPLETE, |
| 806 | requestType); |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 807 | return; |
| 808 | } |
| 809 | |
| 810 | UsersInfo usersInfo = UserHalHelper.newUsersInfo(mUserManager); |
| 811 | mHal.getInitialUserInfo(requestType, mHalTimeoutMs, usersInfo, (status, resp) -> { |
| 812 | if (resp != null) { |
| 813 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_RESP, |
| 814 | status, resp.action, resp.userToSwitchOrCreate.userId, |
| 815 | resp.userToSwitchOrCreate.flags, resp.userNameToCreate, resp.userLocales); |
| 816 | |
| 817 | String userLocales = resp.userLocales; |
| 818 | InitialUserInfo info; |
| 819 | switch (resp.action) { |
| 820 | case InitialUserInfoResponseAction.SWITCH: |
| 821 | int userId = resp.userToSwitchOrCreate.userId; |
| 822 | if (userId <= 0) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 823 | Slog.w(TAG, "invalid (or missing) user id sent by HAL: " + userId); |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 824 | fallbackToDefaultInitialUserBehavior(userLocales, replaceGuest); |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 825 | break; |
| 826 | } |
| 827 | info = new InitialUserSetter.Builder(InitialUserSetter.TYPE_SWITCH) |
| 828 | .setUserLocales(userLocales) |
| 829 | .setSwitchUserId(userId) |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 830 | .setReplaceGuest(replaceGuest) |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 831 | .build(); |
| 832 | mInitialUserSetter.set(info); |
| 833 | break; |
| 834 | |
| 835 | case InitialUserInfoResponseAction.CREATE: |
| 836 | int halFlags = resp.userToSwitchOrCreate.flags; |
| 837 | String userName = resp.userNameToCreate; |
| 838 | info = new InitialUserSetter.Builder(InitialUserSetter.TYPE_CREATE) |
| 839 | .setUserLocales(userLocales) |
| 840 | .setNewUserName(userName) |
| 841 | .setNewUserFlags(halFlags) |
| 842 | .build(); |
| 843 | mInitialUserSetter.set(info); |
| 844 | break; |
| 845 | |
| 846 | case InitialUserInfoResponseAction.DEFAULT: |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 847 | fallbackToDefaultInitialUserBehavior(userLocales, replaceGuest); |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 848 | break; |
| 849 | default: |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 850 | Slog.w(TAG, "invalid response action on " + resp); |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 851 | fallbackToDefaultInitialUserBehavior(/* user locale */ null, replaceGuest); |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 852 | break; |
| 853 | |
| 854 | } |
| 855 | } else { |
| 856 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_RESP, status); |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 857 | fallbackToDefaultInitialUserBehavior(/* user locale */ null, replaceGuest); |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 858 | } |
Mayank Garg | fd1e069 | 2021-05-25 13:33:48 -0700 | [diff] [blame] | 859 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_REQ_COMPLETE, |
| 860 | requestType); |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 861 | }); |
| 862 | } |
| 863 | |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 864 | private void fallbackToDefaultInitialUserBehavior(String userLocales, boolean replaceGuest) { |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 865 | InitialUserInfo info = new InitialUserSetter.Builder( |
| 866 | InitialUserSetter.TYPE_DEFAULT_BEHAVIOR) |
| 867 | .setUserLocales(userLocales) |
Mayank Garg | 4bdfbf7 | 2020-08-06 13:27:43 -0700 | [diff] [blame] | 868 | .setReplaceGuest(replaceGuest) |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 869 | .build(); |
| 870 | mInitialUserSetter.set(info); |
| 871 | } |
| 872 | |
| 873 | @VisibleForTesting |
| 874 | int getInitialUserInfoRequestType() { |
Mayank Garg | e90a408 | 2020-09-30 12:57:34 -0700 | [diff] [blame] | 875 | if (!mInitialUserSetter.hasInitialUser()) { |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 876 | return InitialUserInfoRequestType.FIRST_BOOT; |
| 877 | } |
| 878 | if (mContext.getPackageManager().isDeviceUpgrading()) { |
| 879 | return InitialUserInfoRequestType.FIRST_BOOT_AFTER_OTA; |
| 880 | } |
| 881 | return InitialUserInfoRequestType.COLD_BOOT; |
| 882 | } |
| 883 | |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 884 | /** |
| 885 | * Sets the {@link ICarServiceHelper} so it can receive UX restriction updates. |
| 886 | */ |
| 887 | public void setCarServiceHelper(ICarServiceHelper helper) { |
| 888 | boolean restricted; |
| 889 | synchronized (mLockUser) { |
| 890 | mICarServiceHelper = helper; |
| 891 | restricted = mUxRestricted; |
| 892 | } |
| 893 | updateSafetyMode(helper, restricted); |
| 894 | } |
| 895 | |
| 896 | private void updateSafetyMode(@Nullable ICarServiceHelper helper, boolean restricted) { |
| 897 | if (helper == null) return; |
| 898 | |
| 899 | boolean isSafe = !restricted; |
| 900 | try { |
| 901 | helper.setSafetyMode(isSafe); |
| 902 | } catch (Exception e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 903 | Slog.e(TAG, "Exception calling helper.setDpmSafetyMode(" + isSafe + ")", e); |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
| 907 | private void setUxRestrictions(@Nullable CarUxRestrictions restrictions) { |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 908 | boolean restricted = restrictions != null |
| 909 | && (restrictions.getActiveRestrictions() & UX_RESTRICTIONS_NO_SETUP) |
| 910 | == UX_RESTRICTIONS_NO_SETUP; |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 911 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 912 | Slog.d(TAG, "setUxRestrictions(" + restrictions + "): restricted=" + restricted); |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 913 | } else { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 914 | Slog.i(TAG, "Setting UX restricted to " + restricted); |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 915 | } |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 916 | |
| 917 | ICarServiceHelper helper = null; |
| 918 | |
| 919 | synchronized (mLockUser) { |
| 920 | mUxRestricted = restricted; |
| 921 | if (mICarServiceHelper == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 922 | Slog.e(TAG, "onUxRestrictionsChanged(): no mICarServiceHelper"); |
Felipe Leme | 56ef9ad | 2020-11-05 18:39:03 -0800 | [diff] [blame] | 923 | } |
| 924 | helper = mICarServiceHelper; |
| 925 | } |
| 926 | updateSafetyMode(helper, restricted); |
| 927 | } |
| 928 | |
| 929 | private boolean isUxRestricted() { |
| 930 | synchronized (mLockUser) { |
| 931 | return mUxRestricted; |
| 932 | } |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Mayank Garg | 70732a8 | 2020-08-05 20:17:47 -0700 | [diff] [blame] | 935 | /** |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 936 | * Calls the {@link UserHalService} and {@link IActivityManager} for user switch. |
| 937 | * |
| 938 | * <p> |
Mayank Garg | b08f677 | 2020-05-01 18:06:48 -0700 | [diff] [blame] | 939 | * When everything works well, the workflow is: |
| 940 | * <ol> |
| 941 | * <li> {@link UserHalService} is called for HAL user switch with ANDROID_SWITCH request |
| 942 | * type, current user id, target user id, and a callback. |
| 943 | * <li> HAL called back with SUCCESS. |
| 944 | * <li> {@link IActivityManager} is called for Android user switch. |
| 945 | * <li> Receiver would receive {@code STATUS_SUCCESSFUL}. |
| 946 | * <li> Once user is unlocked, {@link UserHalService} is again called with ANDROID_POST_SWITCH |
| 947 | * request type, current user id, and target user id. In this case, the current and target |
| 948 | * user IDs would be same. |
| 949 | * <ol/> |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 950 | * |
| 951 | * <p> |
Mayank Garg | b08f677 | 2020-05-01 18:06:48 -0700 | [diff] [blame] | 952 | * Corner cases: |
| 953 | * <ul> |
| 954 | * <li> If target user is already the current user, no user switch is performed and receiver |
Mayank Garg | ef1b933 | 2020-06-11 17:36:56 -0700 | [diff] [blame] | 955 | * would receive {@code STATUS_OK_USER_ALREADY_IN_FOREGROUND} right away. |
Mayank Garg | b08f677 | 2020-05-01 18:06:48 -0700 | [diff] [blame] | 956 | * <li> If HAL user switch call fails, no Android user switch. Receiver would receive |
| 957 | * {@code STATUS_HAL_INTERNAL_FAILURE}. |
| 958 | * <li> If HAL user switch call is successful, but android user switch call fails, |
| 959 | * {@link UserHalService} is again called with request type POST_SWITCH, current user id, and |
| 960 | * target user id, but in this case the current and target user IDs would be different. |
| 961 | * <li> If another user switch request for the same target user is received while previous |
| 962 | * request is in process, receiver would receive |
| 963 | * {@code STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO} for the new request right away. |
| 964 | * <li> If a user switch request is received while another user switch request for different |
| 965 | * target user is in process, the previous request would be abandoned and new request will be |
| 966 | * processed. No POST_SWITCH would be sent for the previous request. |
| 967 | * <ul/> |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 968 | * |
Mayank Garg | e19c292 | 2020-03-30 18:05:53 -0700 | [diff] [blame] | 969 | * @param targetUserId - target user Id |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 970 | * @param timeoutMs - timeout for HAL to wait |
| 971 | * @param receiver - receiver for the results |
| 972 | */ |
Mayank Garg | e19c292 | 2020-03-30 18:05:53 -0700 | [diff] [blame] | 973 | @Override |
| 974 | public void switchUser(@UserIdInt int targetUserId, int timeoutMs, |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 975 | @NonNull AndroidFuture<UserSwitchResult> receiver) { |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 976 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SWITCH_USER_REQ, targetUserId, timeoutMs); |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 977 | checkManageOrCreateUsersPermission("switchUser"); |
Mayank Garg | e19c292 | 2020-03-30 18:05:53 -0700 | [diff] [blame] | 978 | Objects.requireNonNull(receiver); |
| 979 | UserInfo targetUser = mUserManager.getUserInfo(targetUserId); |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 980 | Preconditions.checkArgument(targetUser != null, "Target user doesn't exist"); |
Mayank Garg | a479cb3 | 2021-04-05 22:23:04 -0700 | [diff] [blame] | 981 | if (mUserManager.getUserSwitchability() != UserManager.SWITCHABILITY_STATUS_OK) { |
| 982 | sendUserSwitchResult(receiver, UserSwitchResult.STATUS_NOT_SWITCHABLE); |
| 983 | return; |
| 984 | } |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 985 | mHandler.post(() -> handleSwitchUser(targetUser, timeoutMs, receiver)); |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 986 | } |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 987 | |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 988 | private void handleSwitchUser(@NonNull UserInfo targetUser, int timeoutMs, |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 989 | @NonNull AndroidFuture<UserSwitchResult> receiver) { |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 990 | int currentUser = ActivityManager.getCurrentUser(); |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 991 | int targetUserId = targetUser.id; |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 992 | if (currentUser == targetUserId) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 993 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 994 | Slog.d(TAG, "Current user is same as requested target user: " + targetUserId); |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 995 | } |
Mayank Garg | ef1b933 | 2020-06-11 17:36:56 -0700 | [diff] [blame] | 996 | int resultStatus = UserSwitchResult.STATUS_OK_USER_ALREADY_IN_FOREGROUND; |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 997 | sendUserSwitchResult(receiver, resultStatus); |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 1001 | if (isUxRestricted()) { |
| 1002 | sendUserSwitchResult(receiver, UserSwitchResult.STATUS_UX_RESTRICTION_FAILURE); |
| 1003 | return; |
| 1004 | } |
| 1005 | |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 1006 | // If User Hal is not supported, just android user switch. |
| 1007 | if (!isUserHalSupported()) { |
| 1008 | try { |
| 1009 | if (mAm.switchUser(targetUserId)) { |
| 1010 | sendUserSwitchResult(receiver, UserSwitchResult.STATUS_SUCCESSFUL); |
| 1011 | return; |
| 1012 | } |
| 1013 | } catch (RemoteException e) { |
| 1014 | // ignore |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1015 | Slog.w(TAG, "error while switching user " + targetUser.toFullString(), e); |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 1016 | } |
| 1017 | sendUserSwitchResult(receiver, UserSwitchResult.STATUS_ANDROID_FAILURE); |
| 1018 | return; |
| 1019 | } |
| 1020 | |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1021 | synchronized (mLockUser) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1022 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1023 | Slog.d(TAG, "switchUser(" + targetUserId + "): currentuser=" + currentUser |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 1024 | + ", mUserIdForUserSwitchInProcess=" + mUserIdForUserSwitchInProcess); |
| 1025 | } |
| 1026 | |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1027 | // If there is another request for the same target user, return another request in |
| 1028 | // process, else {@link mUserIdForUserSwitchInProcess} is updated and {@link |
| 1029 | // mRequestIdForUserSwitchInProcess} is reset. It is possible that there may be another |
| 1030 | // user switch request in process for different target user, but that request is now |
| 1031 | // ignored. |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 1032 | if (mUserIdForUserSwitchInProcess == targetUserId) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1033 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1034 | Slog.d(TAG, |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1035 | "Another user switch request in process for the requested target user: " |
| 1036 | + targetUserId); |
| 1037 | } |
| 1038 | |
| 1039 | int resultStatus = UserSwitchResult.STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO; |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1040 | sendUserSwitchResult(receiver, resultStatus); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1041 | return; |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 1042 | } else { |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1043 | mUserIdForUserSwitchInProcess = targetUserId; |
| 1044 | mRequestIdForUserSwitchInProcess = 0; |
| 1045 | } |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1048 | UsersInfo usersInfo = UserHalHelper.newUsersInfo(mUserManager); |
Mayank Garg | eb37d09 | 2020-06-02 14:37:57 -0700 | [diff] [blame] | 1049 | SwitchUserRequest request = createUserSwitchRequest(targetUserId, usersInfo); |
| 1050 | |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 1051 | mHal.switchUser(request, timeoutMs, (halCallbackStatus, resp) -> { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1052 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1053 | Slog.d(TAG, "switch response: status=" |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 1054 | + UserHalHelper.halCallbackStatusToString(halCallbackStatus) |
| 1055 | + ", resp=" + resp); |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 1058 | int resultStatus = UserSwitchResult.STATUS_HAL_INTERNAL_FAILURE; |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 1059 | |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1060 | synchronized (mLockUser) { |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 1061 | if (halCallbackStatus != HalCallback.STATUS_OK) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1062 | Slog.w(TAG, "invalid callback status (" |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 1063 | + UserHalHelper.halCallbackStatusToString(halCallbackStatus) |
| 1064 | + ") for response " + resp); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1065 | sendUserSwitchResult(receiver, resultStatus); |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1066 | mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
| 1067 | return; |
| 1068 | } |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 1069 | |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1070 | if (mUserIdForUserSwitchInProcess != targetUserId) { |
| 1071 | // Another user switch request received while HAL responded. No need to process |
| 1072 | // this request further |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1073 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1074 | Slog.d(TAG, "Another user switch received while HAL responsed. Request" |
| 1075 | + " abondoned for : " + targetUserId + ". Current user in process: " |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1076 | + mUserIdForUserSwitchInProcess); |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 1077 | } |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1078 | resultStatus = |
| 1079 | UserSwitchResult.STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST; |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1080 | sendUserSwitchResult(receiver, resultStatus); |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1081 | mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
| 1082 | return; |
| 1083 | } |
| 1084 | |
| 1085 | switch (resp.status) { |
| 1086 | case SwitchUserStatus.SUCCESS: |
| 1087 | boolean switched; |
| 1088 | try { |
| 1089 | switched = mAm.switchUser(targetUserId); |
| 1090 | if (switched) { |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1091 | sendUserSwitchUiCallback(targetUserId); |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1092 | resultStatus = UserSwitchResult.STATUS_SUCCESSFUL; |
| 1093 | mRequestIdForUserSwitchInProcess = resp.requestId; |
| 1094 | } else { |
| 1095 | resultStatus = UserSwitchResult.STATUS_ANDROID_FAILURE; |
| 1096 | postSwitchHalResponse(resp.requestId, targetUserId); |
| 1097 | } |
| 1098 | } catch (RemoteException e) { |
| 1099 | // ignore |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1100 | Slog.w(TAG, |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1101 | "error while switching user " + targetUser.toFullString(), e); |
| 1102 | } |
| 1103 | break; |
| 1104 | case SwitchUserStatus.FAILURE: |
| 1105 | // HAL failed to switch user |
| 1106 | resultStatus = UserSwitchResult.STATUS_HAL_FAILURE; |
| 1107 | break; |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1108 | default: |
| 1109 | // Shouldn't happen because UserHalService validates the status |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1110 | Slog.wtf(TAG, "Received invalid user switch status from HAL: " + resp); |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | if (mRequestIdForUserSwitchInProcess == 0) { |
| 1114 | mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
| 1115 | } |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 1116 | } |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 1117 | sendUserSwitchResult(receiver, halCallbackStatus, resultStatus, resp.errorMessage); |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 1118 | }); |
| 1119 | } |
| 1120 | |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 1121 | @Override |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1122 | public void removeUser(@UserIdInt int userId, AndroidFuture<UserRemovalResult> receiver) { |
| 1123 | removeUser(userId, /* hasCallerRestrictions= */ false, receiver); |
Felipe Leme | 5523672 | 2020-10-16 16:54:32 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * Internal implementation of {@code removeUser()}, which is used by both |
| 1128 | * {@code ICarUserService} and {@code ICarDevicePolicyService}. |
| 1129 | * |
| 1130 | * @param userId user to be removed |
| 1131 | * @param hasCallerRestrictions when {@code true}, if the caller user is not an admin, it can |
| 1132 | * only remove itself. |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1133 | * @param receiver to post results |
Felipe Leme | 5523672 | 2020-10-16 16:54:32 -0700 | [diff] [blame] | 1134 | */ |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1135 | public void removeUser(@UserIdInt int userId, boolean hasCallerRestrictions, |
| 1136 | AndroidFuture<UserRemovalResult> receiver) { |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 1137 | checkManageOrCreateUsersPermission("removeUser"); |
Felipe Leme | 5523672 | 2020-10-16 16:54:32 -0700 | [diff] [blame] | 1138 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_REMOVE_USER_REQ, userId, |
| 1139 | hasCallerRestrictions ? 1 : 0); |
Felipe Leme | e5bb26b | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1140 | |
Felipe Leme | e5bb26b | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1141 | if (hasCallerRestrictions) { |
| 1142 | // Restrictions: non-admin user can only remove itself, admins have no restrictions |
| 1143 | int callingUserId = Binder.getCallingUserHandle().getIdentifier(); |
| 1144 | UserInfo callingUser = mUserManager.getUserInfo(callingUserId); |
| 1145 | if (!callingUser.isAdmin() && userId != callingUserId) { |
| 1146 | throw new SecurityException("Non-admin user " + callingUserId |
| 1147 | + " can only remove itself"); |
| 1148 | } |
| 1149 | } |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1150 | mHandler.post(() -> handleRemoveUser(userId, hasCallerRestrictions, receiver)); |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1151 | } |
Felipe Leme | e5bb26b | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1152 | |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1153 | private void handleRemoveUser(@UserIdInt int userId, boolean hasCallerRestrictions, |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1154 | AndroidFuture<UserRemovalResult> receiver) { |
| 1155 | UserInfo userInfo = mUserManager.getUserInfo(userId); |
| 1156 | if (userInfo == null) { |
| 1157 | sendUserRemovalResult(userId, UserRemovalResult.STATUS_USER_DOES_NOT_EXIST, receiver); |
| 1158 | return; |
| 1159 | } |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 1160 | android.hardware.automotive.vehicle.V2_0.UserInfo halUser = |
| 1161 | new android.hardware.automotive.vehicle.V2_0.UserInfo(); |
| 1162 | halUser.userId = userInfo.id; |
| 1163 | halUser.flags = UserHalHelper.convertFlags(userInfo); |
| 1164 | UsersInfo usersInfo = UserHalHelper.newUsersInfo(mUserManager); |
| 1165 | |
Mayank Garg | e9b7d2c | 2020-09-15 12:36:06 -0700 | [diff] [blame] | 1166 | // check if the user is last admin user. |
| 1167 | boolean isLastAdmin = false; |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 1168 | if (UserHalHelper.isAdmin(halUser.flags)) { |
| 1169 | int size = usersInfo.existingUsers.size(); |
| 1170 | int totalAdminUsers = 0; |
| 1171 | for (int i = 0; i < size; i++) { |
| 1172 | if (UserHalHelper.isAdmin(usersInfo.existingUsers.get(i).flags)) { |
| 1173 | totalAdminUsers++; |
| 1174 | } |
| 1175 | } |
| 1176 | if (totalAdminUsers == 1) { |
Mayank Garg | e9b7d2c | 2020-09-15 12:36:06 -0700 | [diff] [blame] | 1177 | isLastAdmin = true; |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | // First remove user from android and then remove from HAL because HAL remove user is one |
| 1182 | // way call. |
Felipe Leme | 9a75191 | 2021-01-05 17:45:48 -0800 | [diff] [blame] | 1183 | // TODO(b/170887769): rename hasCallerRestrictions to fromCarDevicePolicyManager (or use an |
| 1184 | // int / enum to indicate if it's called from CarUserManager or CarDevicePolicyManager), as |
| 1185 | // it's counter-intuitive that it's "allowed even when disallowed" when it |
| 1186 | // "has caller restrictions" |
| 1187 | boolean evenWhenDisallowed = hasCallerRestrictions; |
| 1188 | int result = mUserManager.removeUserOrSetEphemeral(userId, evenWhenDisallowed); |
Jordan Jozwiak | ffcdca5 | 2020-10-15 15:22:01 -0700 | [diff] [blame] | 1189 | if (result == UserManager.REMOVE_RESULT_ERROR) { |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1190 | sendUserRemovalResult(userId, UserRemovalResult.STATUS_ANDROID_FAILURE, receiver); |
| 1191 | return; |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Mayank Garg | e9b7d2c | 2020-09-15 12:36:06 -0700 | [diff] [blame] | 1194 | if (isLastAdmin) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1195 | Slog.w(TAG, |
Jordan Jozwiak | ffcdca5 | 2020-10-15 15:22:01 -0700 | [diff] [blame] | 1196 | "Last admin user successfully removed or set ephemeral. User Id: " + userId); |
Mayank Garg | e9b7d2c | 2020-09-15 12:36:06 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Jordan Jozwiak | ffcdca5 | 2020-10-15 15:22:01 -0700 | [diff] [blame] | 1199 | switch (result) { |
| 1200 | case UserManager.REMOVE_RESULT_REMOVED: |
| 1201 | case UserManager.REMOVE_RESULT_ALREADY_BEING_REMOVED: |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1202 | sendUserRemovalResult(userId, |
Jordan Jozwiak | ffcdca5 | 2020-10-15 15:22:01 -0700 | [diff] [blame] | 1203 | isLastAdmin ? UserRemovalResult.STATUS_SUCCESSFUL_LAST_ADMIN_REMOVED |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1204 | : UserRemovalResult.STATUS_SUCCESSFUL, receiver); |
Jordan Jozwiak | ffcdca5 | 2020-10-15 15:22:01 -0700 | [diff] [blame] | 1205 | case UserManager.REMOVE_RESULT_SET_EPHEMERAL: |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1206 | sendUserRemovalResult(userId, |
Jordan Jozwiak | ffcdca5 | 2020-10-15 15:22:01 -0700 | [diff] [blame] | 1207 | isLastAdmin ? UserRemovalResult.STATUS_SUCCESSFUL_LAST_ADMIN_SET_EPHEMERAL |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1208 | : UserRemovalResult.STATUS_SUCCESSFUL_SET_EPHEMERAL, receiver); |
Jordan Jozwiak | ffcdca5 | 2020-10-15 15:22:01 -0700 | [diff] [blame] | 1209 | default: |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1210 | sendUserRemovalResult(userId, UserRemovalResult.STATUS_ANDROID_FAILURE, receiver); |
Jordan Jozwiak | ffcdca5 | 2020-10-15 15:22:01 -0700 | [diff] [blame] | 1211 | } |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Felipe Leme | 67d564c | 2020-11-13 15:35:34 -0800 | [diff] [blame] | 1214 | /** |
| 1215 | * Should be called by {@code ICarImpl} only. |
| 1216 | */ |
| 1217 | public void onUserRemoved(@NonNull UserInfo user) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1218 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1219 | Slog.d(TAG, "onUserRemoved: " + user.toFullString()); |
Felipe Leme | 67d564c | 2020-11-13 15:35:34 -0800 | [diff] [blame] | 1220 | } |
| 1221 | notifyHalUserRemoved(user); |
| 1222 | } |
| 1223 | |
| 1224 | private void notifyHalUserRemoved(@NonNull UserInfo user) { |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1225 | if (!isUserHalSupported()) return; |
| 1226 | |
Felipe Leme | 67d564c | 2020-11-13 15:35:34 -0800 | [diff] [blame] | 1227 | if (user == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1228 | Slog.wtf(TAG, "notifyHalUserRemoved() called for null user"); |
Felipe Leme | 67d564c | 2020-11-13 15:35:34 -0800 | [diff] [blame] | 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | int userId = user.id; |
| 1233 | |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1234 | if (userId == UserHandle.USER_NULL) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1235 | Slog.wtf(TAG, "notifyHalUserRemoved() called for UserHandle.USER_NULL"); |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1236 | return; |
| 1237 | } |
| 1238 | |
| 1239 | synchronized (mLockUser) { |
| 1240 | if (mFailedToCreateUserIds.get(userId)) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1241 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1242 | Slog.d(TAG, "notifyHalUserRemoved(): skipping " + userId); |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1243 | } |
| 1244 | mFailedToCreateUserIds.delete(userId); |
| 1245 | return; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | android.hardware.automotive.vehicle.V2_0.UserInfo halUser = |
| 1250 | new android.hardware.automotive.vehicle.V2_0.UserInfo(); |
| 1251 | halUser.userId = userId; |
Felipe Leme | 67d564c | 2020-11-13 15:35:34 -0800 | [diff] [blame] | 1252 | halUser.flags = UserHalHelper.convertFlags(user); |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1253 | |
| 1254 | RemoveUserRequest request = new RemoveUserRequest(); |
| 1255 | request.removedUserInfo = halUser; |
| 1256 | request.usersInfo = UserHalHelper.newUsersInfo(mUserManager); |
| 1257 | mHal.removeUser(request); |
| 1258 | } |
| 1259 | |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1260 | private void sendUserRemovalResult(@UserIdInt int userId, @UserRemovalResult.Status int result, |
| 1261 | AndroidFuture<UserRemovalResult> receiver) { |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 1262 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_REMOVE_USER_RESP, userId, result); |
Mayank Garg | c926648 | 2021-03-01 10:27:37 -0800 | [diff] [blame] | 1263 | receiver.complete(new UserRemovalResult(result)); |
Mayank Garg | a55c309 | 2020-05-28 03:19:24 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1266 | private void sendUserSwitchUiCallback(@UserIdInt int targetUserId) { |
| 1267 | if (mUserSwitchUiReceiver == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1268 | Slog.w(TAG, "No User switch UI receiver."); |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1269 | return; |
| 1270 | } |
| 1271 | |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1272 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SWITCH_USER_UI_REQ, targetUserId); |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1273 | try { |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1274 | mUserSwitchUiReceiver.send(targetUserId, null); |
| 1275 | } catch (RemoteException e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1276 | Slog.e(TAG, "Error calling user switch UI receiver.", e); |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
Felipe Leme | cceb299 | 2020-11-18 18:22:44 -0800 | [diff] [blame] | 1280 | /** |
| 1281 | * Used to create the initial user, even when it's disallowed by {@code DevicePolicyManager}. |
| 1282 | */ |
| 1283 | @Nullable |
| 1284 | UserInfo createUserEvenWhenDisallowed(@Nullable String name, @NonNull String userType, |
| 1285 | @UserInfoFlag int flags) { |
| 1286 | if (mICarServiceHelper == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1287 | Slog.wtf(TAG, "createUserEvenWhenDisallowed(): mICarServiceHelper not set yet", |
Felipe Leme | cceb299 | 2020-11-18 18:22:44 -0800 | [diff] [blame] | 1288 | new Exception()); |
| 1289 | return null; |
| 1290 | } |
| 1291 | try { |
| 1292 | return mICarServiceHelper.createUserEvenWhenDisallowed(name, userType, flags); |
| 1293 | } catch (RemoteException e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1294 | Slog.e(TAG, "createUserEvenWhenDisallowed(" + UserHelperLite.safeName(name) + ", " |
Felipe Leme | cceb299 | 2020-11-18 18:22:44 -0800 | [diff] [blame] | 1295 | + userType + ", " + UserInfo.flagsToString(flags) + ") failed", e); |
| 1296 | return null; |
| 1297 | } |
| 1298 | } |
| 1299 | |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 1300 | @Override |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1301 | public void createUser(@Nullable String name, @NonNull String userType, @UserInfoFlag int flags, |
| 1302 | int timeoutMs, @NonNull AndroidFuture<UserCreationResult> receiver) { |
Felipe Leme | 8c888ab | 2020-11-02 17:39:52 -0800 | [diff] [blame] | 1303 | createUser(name, userType, flags, timeoutMs, receiver, /* hasCallerRestrictions= */ false); |
| 1304 | } |
| 1305 | |
| 1306 | /** |
| 1307 | * Internal implementation of {@code createUser()}, which is used by both |
| 1308 | * {@code ICarUserService} and {@code ICarDevicePolicyService}. |
| 1309 | * |
| 1310 | * @param hasCallerRestrictions when {@code true}, if the caller user is not an admin, it can |
| 1311 | * only create admin users |
| 1312 | */ |
| 1313 | public void createUser(@Nullable String name, @NonNull String userType, @UserInfoFlag int flags, |
| 1314 | int timeoutMs, @NonNull AndroidFuture<UserCreationResult> receiver, |
| 1315 | boolean hasCallerRestrictions) { |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1316 | Objects.requireNonNull(userType, "user type cannot be null"); |
| 1317 | Objects.requireNonNull(receiver, "receiver cannot be null"); |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 1318 | checkManageOrCreateUsersPermission(flags); |
Mayank Garg | 94f3eb9 | 2020-08-12 12:38:58 -0700 | [diff] [blame] | 1319 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_CREATE_USER_REQ, |
Felipe Leme | 8c888ab | 2020-11-02 17:39:52 -0800 | [diff] [blame] | 1320 | UserHelperLite.safeName(name), userType, flags, timeoutMs, |
| 1321 | hasCallerRestrictions ? 1 : 0); |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1322 | mHandler.post(() -> handleCreateUser(name, userType, flags, timeoutMs, receiver, |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 1323 | hasCallerRestrictions)); |
Felipe Leme | 8c888ab | 2020-11-02 17:39:52 -0800 | [diff] [blame] | 1324 | |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1327 | private void handleCreateUser(@Nullable String name, @NonNull String userType, |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 1328 | @UserInfoFlag int flags, int timeoutMs, |
| 1329 | @NonNull AndroidFuture<UserCreationResult> receiver, |
| 1330 | boolean hasCallerRestrictions) { |
Felipe Leme | 8c888ab | 2020-11-02 17:39:52 -0800 | [diff] [blame] | 1331 | if (hasCallerRestrictions) { |
| 1332 | // Restrictions: |
| 1333 | // - type/flag can only be normal user, admin, or guest |
| 1334 | // - non-admin user can only create non-admin users |
| 1335 | |
| 1336 | boolean validCombination; |
| 1337 | switch (userType) { |
| 1338 | case UserManager.USER_TYPE_FULL_SECONDARY: |
| 1339 | validCombination = flags == 0 |
| 1340 | || (flags & UserInfo.FLAG_ADMIN) == UserInfo.FLAG_ADMIN; |
| 1341 | break; |
| 1342 | case UserManager.USER_TYPE_FULL_GUEST: |
| 1343 | validCombination = flags == 0; |
| 1344 | break; |
| 1345 | default: |
| 1346 | validCombination = false; |
| 1347 | } |
| 1348 | if (!validCombination) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1349 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1350 | Slog.d(TAG, "Invalid combination of user type(" + userType |
Felipe Leme | 71d296f | 2020-11-04 12:11:42 -0800 | [diff] [blame] | 1351 | + ") and flags (" + UserInfo.flagsToString(flags) |
| 1352 | + ") for caller with restrictions"); |
| 1353 | } |
| 1354 | sendUserCreationResultFailure(receiver, UserCreationResult.STATUS_INVALID_REQUEST); |
| 1355 | return; |
| 1356 | |
Felipe Leme | 8c888ab | 2020-11-02 17:39:52 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | int callingUserId = Binder.getCallingUserHandle().getIdentifier(); |
| 1360 | UserInfo callingUser = mUserManager.getUserInfo(callingUserId); |
| 1361 | if (!callingUser.isAdmin() && (flags & UserInfo.FLAG_ADMIN) == UserInfo.FLAG_ADMIN) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1362 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1363 | Slog.d(TAG, "Non-admin user " + callingUserId |
Felipe Leme | 71d296f | 2020-11-04 12:11:42 -0800 | [diff] [blame] | 1364 | + " can only create non-admin users"); |
| 1365 | } |
| 1366 | sendUserCreationResultFailure(receiver, UserCreationResult.STATUS_INVALID_REQUEST); |
| 1367 | return; |
Felipe Leme | 8c888ab | 2020-11-02 17:39:52 -0800 | [diff] [blame] | 1368 | } |
Felipe Leme | 71d296f | 2020-11-04 12:11:42 -0800 | [diff] [blame] | 1369 | |
Felipe Leme | 8c888ab | 2020-11-02 17:39:52 -0800 | [diff] [blame] | 1370 | } |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1371 | |
| 1372 | UserInfo newUser; |
| 1373 | try { |
| 1374 | newUser = mUserManager.createUser(name, userType, flags); |
| 1375 | if (newUser == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1376 | Slog.w(TAG, "um.createUser() returned null for user of type " + userType |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1377 | + " and flags " + UserInfo.flagsToString(flags)); |
| 1378 | sendUserCreationResultFailure(receiver, UserCreationResult.STATUS_ANDROID_FAILURE); |
| 1379 | return; |
| 1380 | } |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1381 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1382 | Slog.d(TAG, "Created user: " + newUser.toFullString()); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1383 | } |
| 1384 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_CREATE_USER_USER_CREATED, newUser.id, |
Mayank Garg | 94f3eb9 | 2020-08-12 12:38:58 -0700 | [diff] [blame] | 1385 | UserHelperLite.safeName(newUser.name), newUser.userType, newUser.flags); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1386 | } catch (RuntimeException e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1387 | Slog.e(TAG, "Error creating user of type " + userType + " and flags" |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1388 | + UserInfo.flagsToString(flags), e); |
| 1389 | sendUserCreationResultFailure(receiver, UserCreationResult.STATUS_ANDROID_FAILURE); |
| 1390 | return; |
| 1391 | } |
| 1392 | |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 1393 | if (!isUserHalSupported()) { |
| 1394 | sendUserCreationResult(receiver, UserCreationResult.STATUS_SUCCESSFUL, newUser, null); |
| 1395 | return; |
| 1396 | } |
| 1397 | |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1398 | CreateUserRequest request = new CreateUserRequest(); |
| 1399 | request.usersInfo = UserHalHelper.newUsersInfo(mUserManager); |
| 1400 | if (!TextUtils.isEmpty(name)) { |
| 1401 | request.newUserName = name; |
| 1402 | } |
| 1403 | request.newUserInfo.userId = newUser.id; |
| 1404 | request.newUserInfo.flags = UserHalHelper.convertFlags(newUser); |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1405 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1406 | Slog.d(TAG, "Create user request: " + request); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | try { |
| 1410 | mHal.createUser(request, timeoutMs, (status, resp) -> { |
| 1411 | int resultStatus = UserCreationResult.STATUS_HAL_INTERNAL_FAILURE; |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1412 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1413 | Slog.d(TAG, "createUserResponse: status=" |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1414 | + UserHalHelper.halCallbackStatusToString(status) + ", resp=" + resp); |
| 1415 | } |
| 1416 | UserInfo user = null; // user returned in the result |
| 1417 | if (status != HalCallback.STATUS_OK) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1418 | Slog.w(TAG, "invalid callback status (" |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1419 | + UserHalHelper.halCallbackStatusToString(status) + ") for response " |
| 1420 | + resp); |
| 1421 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_CREATE_USER_RESP, status, |
| 1422 | resultStatus, resp.errorMessage); |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1423 | removeCreatedUser(newUser, "HAL call failed with " |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1424 | + UserHalHelper.halCallbackStatusToString(status)); |
| 1425 | sendUserCreationResult(receiver, resultStatus, user, /* errorMsg= */ null); |
| 1426 | return; |
| 1427 | } |
| 1428 | |
| 1429 | switch (resp.status) { |
| 1430 | case CreateUserStatus.SUCCESS: |
| 1431 | resultStatus = UserCreationResult.STATUS_SUCCESSFUL; |
| 1432 | user = newUser; |
| 1433 | break; |
| 1434 | case CreateUserStatus.FAILURE: |
| 1435 | // HAL failed to switch user |
| 1436 | resultStatus = UserCreationResult.STATUS_HAL_FAILURE; |
| 1437 | break; |
| 1438 | default: |
| 1439 | // Shouldn't happen because UserHalService validates the status |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1440 | Slog.wtf(TAG, "Received invalid user switch status from HAL: " + resp); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1441 | } |
| 1442 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_CREATE_USER_RESP, status, |
| 1443 | resultStatus, resp.errorMessage); |
| 1444 | if (user == null) { |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1445 | removeCreatedUser(newUser, "HAL returned " |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1446 | + UserCreationResult.statusToString(resultStatus)); |
| 1447 | } |
| 1448 | sendUserCreationResult(receiver, resultStatus, user, resp.errorMessage); |
| 1449 | }); |
| 1450 | } catch (Exception e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1451 | Slog.w(TAG, "mHal.createUser(" + request + ") failed", e); |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1452 | removeCreatedUser(newUser, "mHal.createUser() failed"); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1453 | sendUserCreationResultFailure(receiver, UserCreationResult.STATUS_HAL_INTERNAL_FAILURE); |
| 1454 | } |
| 1455 | } |
| 1456 | |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1457 | private void removeCreatedUser(@NonNull UserInfo user, @NonNull String reason) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1458 | Slog.i(TAG, "removing " + user.toFullString() + "; reason: " + reason); |
Felipe Leme | 67d564c | 2020-11-13 15:35:34 -0800 | [diff] [blame] | 1459 | |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1460 | int userId = user.id; |
| 1461 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_CREATE_USER_USER_REMOVED, userId, reason); |
| 1462 | |
| 1463 | synchronized (mLockUser) { |
| 1464 | mFailedToCreateUserIds.put(userId, true); |
| 1465 | } |
| 1466 | |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1467 | try { |
Felipe Leme | 6b34fc3 | 2020-10-26 15:49:17 -0700 | [diff] [blame] | 1468 | if (!mUserManager.removeUser(userId)) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1469 | Slog.w(TAG, "Failed to remove user " + user.toFullString()); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1470 | } |
| 1471 | } catch (Exception e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1472 | Slog.e(TAG, "Failed to remove user " + user.toFullString(), e); |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | @Override |
Mayank Garg | 3f56658 | 2020-10-02 22:10:16 -0700 | [diff] [blame] | 1477 | public UserIdentificationAssociationResponse getUserIdentificationAssociation( |
| 1478 | @UserIdentificationAssociationType int[] types) { |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 1479 | if (!isUserHalUserAssociationSupported()) { |
| 1480 | return UserIdentificationAssociationResponse.forFailure(VEHICLE_HAL_NOT_SUPPORTED); |
| 1481 | } |
| 1482 | |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 1483 | Preconditions.checkArgument(!ArrayUtils.isEmpty(types), "must have at least one type"); |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 1484 | checkManageOrCreateUsersPermission("getUserIdentificationAssociation"); |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 1485 | |
| 1486 | int uid = getCallingUid(); |
| 1487 | int userId = UserHandle.getUserId(uid); |
| 1488 | EventLog.writeEvent(EventLogTags.CAR_USER_MGR_GET_USER_AUTH_REQ, uid, userId); |
| 1489 | |
| 1490 | UserIdentificationGetRequest request = new UserIdentificationGetRequest(); |
| 1491 | request.userInfo.userId = userId; |
| 1492 | request.userInfo.flags = getHalUserInfoFlags(userId); |
| 1493 | |
| 1494 | request.numberAssociationTypes = types.length; |
| 1495 | for (int i = 0; i < types.length; i++) { |
| 1496 | request.associationTypes.add(types[i]); |
| 1497 | } |
| 1498 | |
| 1499 | UserIdentificationResponse halResponse = mHal.getUserAssociation(request); |
| 1500 | if (halResponse == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1501 | Slog.w(TAG, "getUserIdentificationAssociation(): HAL returned null for " |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 1502 | + Arrays.toString(types)); |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 1503 | return UserIdentificationAssociationResponse.forFailure(); |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | int[] values = new int[halResponse.associations.size()]; |
| 1507 | for (int i = 0; i < values.length; i++) { |
| 1508 | values[i] = halResponse.associations.get(i).value; |
| 1509 | } |
| 1510 | EventLog.writeEvent(EventLogTags.CAR_USER_MGR_GET_USER_AUTH_RESP, values.length); |
| 1511 | |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 1512 | return UserIdentificationAssociationResponse.forSuccess(values, halResponse.errorMessage); |
| 1513 | } |
| 1514 | |
| 1515 | @Override |
Mayank Garg | 3f56658 | 2020-10-02 22:10:16 -0700 | [diff] [blame] | 1516 | public void setUserIdentificationAssociation(int timeoutMs, |
| 1517 | @UserIdentificationAssociationType int[] types, |
| 1518 | @UserIdentificationAssociationSetValue int[] values, |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 1519 | AndroidFuture<UserIdentificationAssociationResponse> result) { |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 1520 | if (!isUserHalUserAssociationSupported()) { |
| 1521 | result.complete( |
| 1522 | UserIdentificationAssociationResponse.forFailure(VEHICLE_HAL_NOT_SUPPORTED)); |
| 1523 | return; |
| 1524 | } |
| 1525 | |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 1526 | Preconditions.checkArgument(!ArrayUtils.isEmpty(types), "must have at least one type"); |
| 1527 | Preconditions.checkArgument(!ArrayUtils.isEmpty(values), "must have at least one value"); |
| 1528 | if (types.length != values.length) { |
| 1529 | throw new IllegalArgumentException("types (" + Arrays.toString(types) + ") and values (" |
| 1530 | + Arrays.toString(values) + ") should have the same length"); |
| 1531 | } |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 1532 | checkManageOrCreateUsersPermission("setUserIdentificationAssociation"); |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 1533 | |
| 1534 | int uid = getCallingUid(); |
| 1535 | int userId = UserHandle.getUserId(uid); |
| 1536 | EventLog.writeEvent(EventLogTags.CAR_USER_MGR_SET_USER_AUTH_REQ, uid, userId, types.length); |
| 1537 | |
| 1538 | UserIdentificationSetRequest request = new UserIdentificationSetRequest(); |
| 1539 | request.userInfo.userId = userId; |
| 1540 | request.userInfo.flags = getHalUserInfoFlags(userId); |
| 1541 | |
| 1542 | request.numberAssociations = types.length; |
| 1543 | for (int i = 0; i < types.length; i++) { |
| 1544 | UserIdentificationSetAssociation association = new UserIdentificationSetAssociation(); |
| 1545 | association.type = types[i]; |
| 1546 | association.value = values[i]; |
| 1547 | request.associations.add(association); |
| 1548 | } |
| 1549 | |
| 1550 | mHal.setUserAssociation(timeoutMs, request, (status, resp) -> { |
| 1551 | if (status != HalCallback.STATUS_OK) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1552 | Slog.w(TAG, "setUserIdentificationAssociation(): invalid callback status (" |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 1553 | + UserHalHelper.halCallbackStatusToString(status) + ") for response " |
| 1554 | + resp); |
| 1555 | if (resp == null || TextUtils.isEmpty(resp.errorMessage)) { |
| 1556 | EventLog.writeEvent(EventLogTags.CAR_USER_MGR_SET_USER_AUTH_RESP, 0); |
| 1557 | result.complete(UserIdentificationAssociationResponse.forFailure()); |
| 1558 | return; |
| 1559 | } |
| 1560 | EventLog.writeEvent(EventLogTags.CAR_USER_MGR_SET_USER_AUTH_RESP, 0, |
| 1561 | resp.errorMessage); |
| 1562 | result.complete( |
| 1563 | UserIdentificationAssociationResponse.forFailure(resp.errorMessage)); |
| 1564 | return; |
| 1565 | } |
| 1566 | int respSize = resp.associations.size(); |
| 1567 | EventLog.writeEvent(EventLogTags.CAR_USER_MGR_SET_USER_AUTH_RESP, respSize, |
| 1568 | resp.errorMessage); |
| 1569 | |
| 1570 | int[] responseTypes = new int[respSize]; |
| 1571 | for (int i = 0; i < respSize; i++) { |
| 1572 | responseTypes[i] = resp.associations.get(i).value; |
| 1573 | } |
| 1574 | UserIdentificationAssociationResponse response = UserIdentificationAssociationResponse |
| 1575 | .forSuccess(responseTypes, resp.errorMessage); |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1576 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1577 | Slog.d(TAG, "setUserIdentificationAssociation(): resp= " + resp |
felipeal | 159a2a4 | 2020-05-08 10:32:11 -0700 | [diff] [blame] | 1578 | + ", converted=" + response); |
| 1579 | } |
| 1580 | result.complete(response); |
| 1581 | }); |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /** |
| 1585 | * Gets the User HAL flags for the given user. |
| 1586 | * |
| 1587 | * @throws IllegalArgumentException if the user does not exist. |
| 1588 | */ |
| 1589 | private int getHalUserInfoFlags(@UserIdInt int userId) { |
| 1590 | UserInfo user = mUserManager.getUserInfo(userId); |
| 1591 | Preconditions.checkArgument(user != null, "no user for id %d", userId); |
| 1592 | return UserHalHelper.convertFlags(user); |
| 1593 | } |
| 1594 | |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 1595 | private void sendResult(@NonNull IResultReceiver receiver, int resultCode, |
| 1596 | @Nullable Bundle resultData) { |
| 1597 | try { |
| 1598 | receiver.send(resultCode, resultData); |
| 1599 | } catch (RemoteException e) { |
| 1600 | // ignore |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1601 | Slog.w(TAG, "error while sending results", e); |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 1602 | } |
| 1603 | } |
| 1604 | |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1605 | private void sendUserSwitchResult(@NonNull AndroidFuture<UserSwitchResult> receiver, |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 1606 | @UserSwitchResult.Status int userSwitchStatus) { |
| 1607 | sendUserSwitchResult(receiver, HalCallback.STATUS_INVALID, userSwitchStatus, |
| 1608 | /* errorMessage= */ null); |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1611 | private void sendUserSwitchResult(@NonNull AndroidFuture<UserSwitchResult> receiver, |
Felipe Leme | 5d5ab14 | 2020-10-27 13:49:10 -0700 | [diff] [blame] | 1612 | @HalCallback.HalCallbackStatus int halCallbackStatus, |
| 1613 | @UserSwitchResult.Status int userSwitchStatus, @Nullable String errorMessage) { |
| 1614 | if (errorMessage != null) { |
| 1615 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SWITCH_USER_RESP, halCallbackStatus, |
| 1616 | userSwitchStatus, errorMessage); |
| 1617 | } else { |
| 1618 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SWITCH_USER_RESP, halCallbackStatus, |
| 1619 | userSwitchStatus); |
| 1620 | } |
| 1621 | receiver.complete(new UserSwitchResult(userSwitchStatus, errorMessage)); |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1624 | private void sendUserCreationResultFailure(@NonNull AndroidFuture<UserCreationResult> receiver, |
| 1625 | @UserCreationResult.Status int status) { |
| 1626 | sendUserCreationResult(receiver, status, /* user= */ null, /* errorMessage= */ null); |
| 1627 | } |
| 1628 | |
| 1629 | private void sendUserCreationResult(@NonNull AndroidFuture<UserCreationResult> receiver, |
| 1630 | @UserCreationResult.Status int status, @NonNull UserInfo user, |
| 1631 | @Nullable String errorMessage) { |
| 1632 | if (TextUtils.isEmpty(errorMessage)) { |
| 1633 | errorMessage = null; |
| 1634 | } |
| 1635 | receiver.complete(new UserCreationResult(status, user, errorMessage)); |
| 1636 | } |
| 1637 | |
Mayank Garg | 6307fe4 | 2020-04-15 23:09:03 -0700 | [diff] [blame] | 1638 | /** |
| 1639 | * Calls activity manager for user switch. |
| 1640 | * |
| 1641 | * <p><b>NOTE</b> This method is meant to be called just by UserHalService. |
| 1642 | * |
| 1643 | * @param requestId for the user switch request |
| 1644 | * @param targetUserId of the target user |
| 1645 | * |
| 1646 | * @hide |
| 1647 | */ |
| 1648 | public void switchAndroidUserFromHal(int requestId, @UserIdInt int targetUserId) { |
| 1649 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SWITCH_USER_FROM_HAL_REQ, requestId, |
| 1650 | targetUserId); |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1651 | Slog.i(TAG, "User hal requested a user switch. Target user id " + targetUserId); |
Mayank Garg | 6307fe4 | 2020-04-15 23:09:03 -0700 | [diff] [blame] | 1652 | |
| 1653 | try { |
| 1654 | boolean result = mAm.switchUser(targetUserId); |
| 1655 | if (result) { |
| 1656 | updateUserSwitchInProcess(requestId, targetUserId); |
| 1657 | } else { |
| 1658 | postSwitchHalResponse(requestId, targetUserId); |
| 1659 | } |
| 1660 | } catch (RemoteException e) { |
| 1661 | // ignore |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1662 | Slog.w(TAG, "error while switching user " + targetUserId, e); |
Mayank Garg | 6307fe4 | 2020-04-15 23:09:03 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | private void updateUserSwitchInProcess(int requestId, @UserIdInt int targetUserId) { |
| 1667 | synchronized (mLockUser) { |
| 1668 | if (mUserIdForUserSwitchInProcess != UserHandle.USER_NULL) { |
| 1669 | // Some other user switch is in process. |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1670 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1671 | Slog.d(TAG, "User switch for user: " + mUserIdForUserSwitchInProcess |
Mayank Garg | 6307fe4 | 2020-04-15 23:09:03 -0700 | [diff] [blame] | 1672 | + " is in process. Abandoning it as a new user switch is requested" |
| 1673 | + " for the target user: " + targetUserId); |
| 1674 | } |
| 1675 | } |
| 1676 | mUserIdForUserSwitchInProcess = targetUserId; |
| 1677 | mRequestIdForUserSwitchInProcess = requestId; |
| 1678 | } |
| 1679 | } |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 1680 | |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1681 | private void postSwitchHalResponse(int requestId, @UserIdInt int targetUserId) { |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 1682 | if (!isUserHalSupported()) return; |
| 1683 | |
felipeal | dfdf851 | 2020-06-01 09:35:45 -0700 | [diff] [blame] | 1684 | UsersInfo usersInfo = UserHalHelper.newUsersInfo(mUserManager); |
Mayank Garg | eb37d09 | 2020-06-02 14:37:57 -0700 | [diff] [blame] | 1685 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_POST_SWITCH_USER_REQ, requestId, |
| 1686 | targetUserId, usersInfo.currentUser.userId); |
| 1687 | SwitchUserRequest request = createUserSwitchRequest(targetUserId, usersInfo); |
| 1688 | request.requestId = requestId; |
| 1689 | mHal.postSwitchResponse(request); |
| 1690 | } |
| 1691 | |
| 1692 | private SwitchUserRequest createUserSwitchRequest(@UserIdInt int targetUserId, |
| 1693 | @NonNull UsersInfo usersInfo) { |
| 1694 | UserInfo targetUser = mUserManager.getUserInfo(targetUserId); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1695 | android.hardware.automotive.vehicle.V2_0.UserInfo halTargetUser = |
| 1696 | new android.hardware.automotive.vehicle.V2_0.UserInfo(); |
| 1697 | halTargetUser.userId = targetUser.id; |
| 1698 | halTargetUser.flags = UserHalHelper.convertFlags(targetUser); |
Mayank Garg | eb37d09 | 2020-06-02 14:37:57 -0700 | [diff] [blame] | 1699 | SwitchUserRequest request = new SwitchUserRequest(); |
| 1700 | request.targetUser = halTargetUser; |
| 1701 | request.usersInfo = usersInfo; |
| 1702 | return request; |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 1705 | /** |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 1706 | * Checks if the User HAL is supported. |
| 1707 | */ |
| 1708 | public boolean isUserHalSupported() { |
| 1709 | return mHal.isSupported(); |
| 1710 | } |
| 1711 | |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1712 | /** |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 1713 | * Checks if the User HAL user association is supported. |
| 1714 | */ |
| 1715 | @Override |
| 1716 | public boolean isUserHalUserAssociationSupported() { |
| 1717 | return mHal.isUserAssociationSupported(); |
| 1718 | } |
| 1719 | |
| 1720 | /** |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1721 | * Sets a callback which is invoked before user switch. |
| 1722 | * |
| 1723 | * <p> |
| 1724 | * This method should only be called by the Car System UI. The purpose of this call is to notify |
| 1725 | * Car System UI to show the user switch UI before the user switch. |
| 1726 | */ |
| 1727 | @Override |
| 1728 | public void setUserSwitchUiCallback(@NonNull IResultReceiver receiver) { |
Yan Zhu | 67a383e | 2020-05-11 20:46:24 -0700 | [diff] [blame] | 1729 | checkManageUsersPermission("setUserSwitchUiCallback"); |
Mayank Garg | a480dd9 | 2020-05-14 03:14:57 -0700 | [diff] [blame] | 1730 | |
| 1731 | // Confirm that caller is system UI. |
| 1732 | String systemUiPackageName = getSystemUiPackageName(); |
| 1733 | if (systemUiPackageName == null) { |
| 1734 | throw new IllegalStateException("System UI package not found."); |
| 1735 | } |
| 1736 | |
| 1737 | try { |
| 1738 | int systemUiUid = mContext |
| 1739 | .createContextAsUser(UserHandle.SYSTEM, /* flags= */ 0).getPackageManager() |
| 1740 | .getPackageUid(systemUiPackageName, PackageManager.MATCH_SYSTEM_ONLY); |
| 1741 | int callerUid = Binder.getCallingUid(); |
| 1742 | if (systemUiUid != callerUid) { |
| 1743 | throw new SecurityException("Invalid caller. Only" + systemUiPackageName |
| 1744 | + " is allowed to make this call"); |
| 1745 | } |
| 1746 | } catch (NameNotFoundException e) { |
| 1747 | throw new IllegalStateException("Package " + systemUiPackageName + " not found."); |
| 1748 | } |
| 1749 | |
Mayank Garg | 587f194 | 2020-05-06 01:41:34 -0700 | [diff] [blame] | 1750 | mUserSwitchUiReceiver = receiver; |
| 1751 | } |
| 1752 | |
Mayank Garg | a480dd9 | 2020-05-14 03:14:57 -0700 | [diff] [blame] | 1753 | // TODO(157082995): This information can be taken from |
| 1754 | // PackageManageInternalImpl.getSystemUiServiceComponent |
| 1755 | @Nullable |
| 1756 | private String getSystemUiPackageName() { |
| 1757 | try { |
| 1758 | ComponentName componentName = ComponentName.unflattenFromString(mContext.getResources() |
| 1759 | .getString(com.android.internal.R.string.config_systemUIServiceComponent)); |
| 1760 | return componentName.getPackageName(); |
| 1761 | } catch (RuntimeException e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1762 | Slog.w(TAG, "error while getting system UI package name.", e); |
Mayank Garg | a480dd9 | 2020-05-14 03:14:57 -0700 | [diff] [blame] | 1763 | return null; |
| 1764 | } |
| 1765 | } |
| 1766 | |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 1767 | private void updateDefaultUserRestriction() { |
| 1768 | // We want to set restrictions on system and guest users only once. These are persisted |
| 1769 | // onto disk, so it's sufficient to do it once + we minimize the number of disk writes. |
| 1770 | if (Settings.Global.getInt(mContext.getContentResolver(), |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1771 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, /* default= */ 0) != 0) { |
| 1772 | return; |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 1773 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1774 | // Only apply the system user restrictions if the system user is headless. |
| 1775 | if (UserManager.isHeadlessSystemUserMode()) { |
| 1776 | setSystemUserRestrictions(); |
| 1777 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1778 | Settings.Global.putInt(mContext.getContentResolver(), |
| 1779 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, 1); |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1782 | private boolean isPersistentUser(@UserIdInt int userId) { |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 1783 | return !mUserManager.getUserInfo(userId).isEphemeral(); |
| 1784 | } |
| 1785 | |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 1786 | /** |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 1787 | * Adds a new {@link UserLifecycleListener} to listen to user activity events. |
| 1788 | */ |
| 1789 | public void addUserLifecycleListener(@NonNull UserLifecycleListener listener) { |
| 1790 | Objects.requireNonNull(listener, "listener cannot be null"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1791 | mHandler.post(() -> mUserLifecycleListeners.add(listener)); |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /** |
| 1795 | * Removes previously added {@link UserLifecycleListener}. |
| 1796 | */ |
| 1797 | public void removeUserLifecycleListener(@NonNull UserLifecycleListener listener) { |
| 1798 | Objects.requireNonNull(listener, "listener cannot be null"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1799 | mHandler.post(() -> mUserLifecycleListeners.remove(listener)); |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 1800 | } |
| 1801 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1802 | /** Adds callback to listen to passenger activity events. */ |
| 1803 | public void addPassengerCallback(@NonNull PassengerCallback callback) { |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 1804 | Objects.requireNonNull(callback, "callback cannot be null"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1805 | mPassengerCallbacks.add(callback); |
| 1806 | } |
| 1807 | |
| 1808 | /** Removes previously added callback to listen passenger events. */ |
| 1809 | public void removePassengerCallback(@NonNull PassengerCallback callback) { |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 1810 | Objects.requireNonNull(callback, "callback cannot be null"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1811 | mPassengerCallbacks.remove(callback); |
| 1812 | } |
| 1813 | |
| 1814 | /** Sets the implementation of ZoneUserBindingHelper. */ |
| 1815 | public void setZoneUserBindingHelper(@NonNull ZoneUserBindingHelper helper) { |
| 1816 | synchronized (mLockHelper) { |
| 1817 | mZoneUserBindingHelper = helper; |
| 1818 | } |
| 1819 | } |
| 1820 | |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 1821 | private void onUserUnlocked(@UserIdInt int userId) { |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1822 | ArrayList<Runnable> tasks = null; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1823 | synchronized (mLockUser) { |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1824 | sendPostSwitchToHalLocked(userId); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1825 | if (userId == UserHandle.USER_SYSTEM) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1826 | if (!mUser0Unlocked) { // user 0, unlocked, do this only once |
| 1827 | updateDefaultUserRestriction(); |
| 1828 | tasks = new ArrayList<>(mUser0UnlockTasks); |
| 1829 | mUser0UnlockTasks.clear(); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1830 | mUser0Unlocked = true; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1831 | } |
| 1832 | } else { // none user0 |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1833 | Integer user = userId; |
| 1834 | if (isPersistentUser(userId)) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1835 | // current foreground user should stay in top priority. |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 1836 | if (userId == ActivityManager.getCurrentUser()) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1837 | mBackgroundUsersToRestart.remove(user); |
| 1838 | mBackgroundUsersToRestart.add(0, user); |
| 1839 | } |
| 1840 | // -1 for user 0 |
| 1841 | if (mBackgroundUsersToRestart.size() > (mMaxRunningUsers - 1)) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1842 | int userToDrop = mBackgroundUsersToRestart.get( |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1843 | mBackgroundUsersToRestart.size() - 1); |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1844 | Slog.i(TAG, "New user unlocked:" + userId |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1845 | + ", dropping least recently user from restart list:" + userToDrop); |
| 1846 | // Drop the least recently used user. |
| 1847 | mBackgroundUsersToRestart.remove(mBackgroundUsersToRestart.size() - 1); |
| 1848 | } |
| 1849 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | if (tasks != null && tasks.size() > 0) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1853 | Slog.d(TAG, "User0 unlocked, run queued tasks:" + tasks.size()); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1854 | for (Runnable r : tasks) { |
| 1855 | r.run(); |
| 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | /** |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1861 | * Starts the specified user in the background. |
| 1862 | * |
| 1863 | * @param userId user to start in background |
| 1864 | * @param receiver to post results |
| 1865 | */ |
| 1866 | public void startUserInBackground(@UserIdInt int userId, |
| 1867 | @NonNull AndroidFuture<UserStartResult> receiver) { |
bkchoi | 5ab7aa3 | 2021-04-27 11:50:53 -0700 | [diff] [blame] | 1868 | checkManageOrCreateUsersPermission("startUserInBackground"); |
| 1869 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_START_USER_IN_BACKGROUND_REQ, userId); |
| 1870 | |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1871 | mHandler.post(() -> handleStartUserInBackground(userId, receiver)); |
| 1872 | } |
| 1873 | |
| 1874 | private void handleStartUserInBackground(@UserIdInt int userId, |
| 1875 | @NonNull AndroidFuture<UserStartResult> receiver) { |
| 1876 | // If the requested user is the current user, do nothing and return success. |
| 1877 | if (ActivityManager.getCurrentUser() == userId) { |
bkchoi | 5ab7aa3 | 2021-04-27 11:50:53 -0700 | [diff] [blame] | 1878 | sendUserStartResult( |
| 1879 | userId, UserStartResult.STATUS_SUCCESSFUL_USER_IS_CURRENT_USER, receiver); |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1880 | return; |
| 1881 | } |
| 1882 | // If requested user does not exist, return error. |
| 1883 | if (mUserManager.getUserInfo(userId) == null) { |
| 1884 | Slogf.w(TAG, "User %d does not exist", userId); |
bkchoi | 5ab7aa3 | 2021-04-27 11:50:53 -0700 | [diff] [blame] | 1885 | sendUserStartResult(userId, UserStartResult.STATUS_USER_DOES_NOT_EXIST, receiver); |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1886 | return; |
| 1887 | } |
| 1888 | |
| 1889 | try { |
| 1890 | if (!mAm.startUserInBackground(userId)) { |
| 1891 | Slogf.w(TAG, "Failed to start user %d in background", userId); |
bkchoi | 5ab7aa3 | 2021-04-27 11:50:53 -0700 | [diff] [blame] | 1892 | sendUserStartResult(userId, UserStartResult.STATUS_ANDROID_FAILURE, receiver); |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1893 | return; |
| 1894 | } |
| 1895 | } catch (RemoteException e) { |
| 1896 | Slogf.w(TAG, e, "Failed to start user %d in background", userId); |
| 1897 | } |
| 1898 | |
| 1899 | // TODO(b/181331178): We are not updating mBackgroundUsersToRestart or |
| 1900 | // mBackgroundUsersRestartedHere, which were only used for the garage mode. Consider |
| 1901 | // renaming them to make it more clear. |
bkchoi | 5ab7aa3 | 2021-04-27 11:50:53 -0700 | [diff] [blame] | 1902 | sendUserStartResult(userId, UserStartResult.STATUS_SUCCESSFUL, receiver); |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
bkchoi | 5ab7aa3 | 2021-04-27 11:50:53 -0700 | [diff] [blame] | 1905 | private void sendUserStartResult(@UserIdInt int userId, @UserStartResult.Status int result, |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1906 | @NonNull AndroidFuture<UserStartResult> receiver) { |
bkchoi | 5ab7aa3 | 2021-04-27 11:50:53 -0700 | [diff] [blame] | 1907 | EventLog.writeEvent( |
| 1908 | EventLogTags.CAR_USER_SVC_START_USER_IN_BACKGROUND_RESP, userId, result); |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 1909 | receiver.complete(new UserStartResult(result)); |
| 1910 | } |
| 1911 | |
| 1912 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1913 | * Starts all background users that were active in system. |
| 1914 | * |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1915 | * @return list of background users started successfully. |
| 1916 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1917 | @NonNull |
Mayank Garg | 784a086 | 2021-05-26 00:20:50 -0700 | [diff] [blame^] | 1918 | public ArrayList<Integer> startAllBackgroundUsersInGarageMode() { |
| 1919 | synchronized (mLockUser) { |
| 1920 | if (!mStartBackgroundUsersOnGarageMode) { |
| 1921 | Slogf.i(TAG, "Background users are not started as mStartBackgroundUsersOnGarageMode" |
| 1922 | + " is false."); |
| 1923 | return new ArrayList<>(); |
| 1924 | } |
| 1925 | } |
| 1926 | |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1927 | ArrayList<Integer> users; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1928 | synchronized (mLockUser) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1929 | users = new ArrayList<>(mBackgroundUsersToRestart); |
| 1930 | mBackgroundUsersRestartedHere.clear(); |
| 1931 | mBackgroundUsersRestartedHere.addAll(mBackgroundUsersToRestart); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1932 | } |
| 1933 | ArrayList<Integer> startedUsers = new ArrayList<>(); |
| 1934 | for (Integer user : users) { |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 1935 | if (user == ActivityManager.getCurrentUser()) { |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1936 | continue; |
| 1937 | } |
| 1938 | try { |
| 1939 | if (mAm.startUserInBackground(user)) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1940 | if (mUserManager.isUserUnlockingOrUnlocked(user)) { |
| 1941 | // already unlocked / unlocking. No need to unlock. |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1942 | startedUsers.add(user); |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1943 | } else if (mAm.unlockUser(user, null, null, null)) { |
| 1944 | startedUsers.add(user); |
| 1945 | } else { // started but cannot unlock |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1946 | Slog.w(TAG, "Background user started but cannot be unlocked:" + user); |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1947 | if (mUserManager.isUserRunning(user)) { |
| 1948 | // add to started list so that it can be stopped later. |
| 1949 | startedUsers.add(user); |
| 1950 | } |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1951 | } |
| 1952 | } |
| 1953 | } catch (RemoteException e) { |
| 1954 | // ignore |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 1955 | Slog.w(TAG, "error while starting user in background", e); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1956 | } |
| 1957 | } |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1958 | // Keep only users that were re-started in mBackgroundUsersRestartedHere |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1959 | synchronized (mLockUser) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1960 | ArrayList<Integer> usersToRemove = new ArrayList<>(); |
| 1961 | for (Integer user : mBackgroundUsersToRestart) { |
| 1962 | if (!startedUsers.contains(user)) { |
| 1963 | usersToRemove.add(user); |
| 1964 | } |
| 1965 | } |
| 1966 | mBackgroundUsersRestartedHere.removeAll(usersToRemove); |
| 1967 | } |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1968 | return startedUsers; |
| 1969 | } |
| 1970 | |
| 1971 | /** |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 1972 | * Stops the specified background user. |
| 1973 | * |
| 1974 | * @param userId user to stop |
| 1975 | * @param receiver to post results |
| 1976 | */ |
| 1977 | public void stopUser(@UserIdInt int userId, @NonNull AndroidFuture<UserStopResult> receiver) { |
bkchoi | a8c7fb9 | 2021-04-28 10:58:57 -0700 | [diff] [blame] | 1978 | checkManageOrCreateUsersPermission("stopUser"); |
| 1979 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_STOP_USER_REQ, userId); |
| 1980 | |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 1981 | mHandler.post(() -> handleStopUser(userId, receiver)); |
| 1982 | } |
| 1983 | |
| 1984 | private void handleStopUser( |
| 1985 | @UserIdInt int userId, @NonNull AndroidFuture<UserStopResult> receiver) { |
| 1986 | @UserStopResult.Status int userStopStatus = stopBackgroundUserInternal(userId); |
bkchoi | a8c7fb9 | 2021-04-28 10:58:57 -0700 | [diff] [blame] | 1987 | sendUserStopResult(userId, userStopStatus, receiver); |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
bkchoi | a8c7fb9 | 2021-04-28 10:58:57 -0700 | [diff] [blame] | 1990 | private void sendUserStopResult(@UserIdInt int userId, @UserStopResult.Status int result, |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 1991 | @NonNull AndroidFuture<UserStopResult> receiver) { |
bkchoi | a8c7fb9 | 2021-04-28 10:58:57 -0700 | [diff] [blame] | 1992 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_STOP_USER_RESP, userId, result); |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 1993 | receiver.complete(new UserStopResult(result)); |
| 1994 | } |
| 1995 | |
| 1996 | private @UserStopResult.Status int stopBackgroundUserInternal(@UserIdInt int userId) { |
| 1997 | try { |
| 1998 | int r = mAm.stopUserWithDelayedLocking(userId, true, null); |
| 1999 | switch(r) { |
| 2000 | case ActivityManager.USER_OP_SUCCESS: |
| 2001 | return UserStopResult.STATUS_SUCCESSFUL; |
| 2002 | case ActivityManager.USER_OP_ERROR_IS_SYSTEM: |
| 2003 | Slogf.w(TAG, "Cannot stop the system user: %d", userId); |
| 2004 | return UserStopResult.STATUS_FAILURE_SYSTEM_USER; |
| 2005 | case ActivityManager.USER_OP_IS_CURRENT: |
| 2006 | Slogf.w(TAG, "Cannot stop the current user: %d", userId); |
| 2007 | return UserStopResult.STATUS_FAILURE_CURRENT_USER; |
| 2008 | case ActivityManager.USER_OP_UNKNOWN_USER: |
| 2009 | Slogf.w(TAG, "Cannot stop the user that does not exist: %d", userId); |
| 2010 | return UserStopResult.STATUS_USER_DOES_NOT_EXIST; |
| 2011 | default: |
| 2012 | Slogf.w(TAG, "stopUser failed, user: %d, err: %d", userId, r); |
| 2013 | } |
| 2014 | } catch (RemoteException e) { |
| 2015 | // ignore the exception |
| 2016 | Slogf.w(TAG, e, "error while stopping user: %d", userId); |
| 2017 | } |
| 2018 | return UserStopResult.STATUS_ANDROID_FAILURE; |
| 2019 | } |
| 2020 | |
| 2021 | /** |
Mayank Garg | 784a086 | 2021-05-26 00:20:50 -0700 | [diff] [blame^] | 2022 | * Sets boolean to control background user operations during garage mode. |
| 2023 | */ |
| 2024 | public void setStartBackgroundUsersOnGarageMode(boolean enable) { |
| 2025 | synchronized (mLockUser) { |
| 2026 | mStartBackgroundUsersOnGarageMode = enable; |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | /** |
Bo-Kyung (BK) Choi | a6503f4 | 2021-04-19 14:34:00 -0700 | [diff] [blame] | 2031 | * Stops a background user. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 2032 | * |
| 2033 | * @return whether stopping succeeds. |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 2034 | */ |
Mayank Garg | 784a086 | 2021-05-26 00:20:50 -0700 | [diff] [blame^] | 2035 | public boolean stopBackgroundUserInGagageMode(@UserIdInt int userId) { |
| 2036 | synchronized (mLockUser) { |
| 2037 | if (!mStartBackgroundUsersOnGarageMode) { |
| 2038 | Slogf.i(TAG, "Background users are not stopped as mStartBackgroundUsersOnGarageMode" |
| 2039 | + " is false."); |
| 2040 | return false; |
| 2041 | } |
| 2042 | } |
| 2043 | |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 2044 | @UserStopResult.Status int userStopStatus = stopBackgroundUserInternal(userId); |
| 2045 | if (UserStopResult.isSuccess(userStopStatus)) { |
| 2046 | // Remove the stopped user from the mBackgroundUserRestartedHere list. |
| 2047 | synchronized (mLockUser) { |
| 2048 | mBackgroundUsersRestartedHere.remove(Integer.valueOf(userId)); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 2049 | } |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 2050 | return true; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 2051 | } |
bkchoi | d77d0bc | 2021-04-22 13:43:03 -0700 | [diff] [blame] | 2052 | return false; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | /** |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2056 | * Notifies all registered {@link UserLifecycleListener} with the event passed as argument. |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 2057 | */ |
Mayank Garg | ccad806 | 2020-08-30 15:05:10 -0700 | [diff] [blame] | 2058 | public void onUserLifecycleEvent(@UserLifecycleEventType int eventType, |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 2059 | @UserIdInt int fromUserId, @UserIdInt int toUserId) { |
| 2060 | int userId = toUserId; |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 2061 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2062 | // Handle special cases first... |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 2063 | if (eventType == CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING) { |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2064 | onUserSwitching(fromUserId, toUserId); |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 2065 | } else if (eventType == CarUserManager.USER_LIFECYCLE_EVENT_TYPE_UNLOCKED) { |
| 2066 | onUserUnlocked(userId); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2067 | } |
| 2068 | |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 2069 | // ...then notify listeners. |
Yan Zhu | e792152 | 2020-04-16 15:59:25 -0700 | [diff] [blame] | 2070 | UserLifecycleEvent event = new UserLifecycleEvent(eventType, fromUserId, userId); |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 2071 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2072 | mHandler.post(() -> { |
| 2073 | handleNotifyServiceUserLifecycleListeners(event); |
| 2074 | handleNotifyAppUserLifecycleListeners(event); |
| 2075 | }); |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 2078 | private void sendPostSwitchToHalLocked(@UserIdInt int userId) { |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 2079 | if (mUserIdForUserSwitchInProcess == UserHandle.USER_NULL |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 2080 | || mUserIdForUserSwitchInProcess != userId |
| 2081 | || mRequestIdForUserSwitchInProcess == 0) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2082 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 2083 | Slog.d(TAG, "No user switch request Id. No android post switch sent."); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 2084 | } |
| 2085 | return; |
| 2086 | } |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 2087 | postSwitchHalResponse(mRequestIdForUserSwitchInProcess, mUserIdForUserSwitchInProcess); |
| 2088 | mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 2089 | mRequestIdForUserSwitchInProcess = 0; |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2092 | private void handleNotifyAppUserLifecycleListeners(UserLifecycleEvent event) { |
| 2093 | int listenersSize = mAppLifecycleListeners.size(); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2094 | if (listenersSize == 0) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2095 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 2096 | Slog.d(TAG, "No app listener to be notified of " + event); |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 2097 | } |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2098 | return; |
| 2099 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2100 | // Must use a different TimingsTraceLog because it's another thread |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2101 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 2102 | Slog.d(TAG, "Notifying " + listenersSize + " app listeners of " + event); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2103 | } |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 2104 | int userId = event.getUserId(); |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2105 | TimingsTraceLog t = new TimingsTraceLog(TAG, Trace.TRACE_TAG_SYSTEM_SERVER); |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 2106 | int eventType = event.getEventType(); |
| 2107 | t.traceBegin("notify-app-listeners-user-" + userId + "-event-" + eventType); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2108 | for (int i = 0; i < listenersSize; i++) { |
| 2109 | int uid = mAppLifecycleListeners.keyAt(i); |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 2110 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2111 | IResultReceiver listener = mAppLifecycleListeners.valueAt(i); |
| 2112 | Bundle data = new Bundle(); |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 2113 | data.putInt(CarUserManager.BUNDLE_PARAM_ACTION, eventType); |
Yan Zhu | e792152 | 2020-04-16 15:59:25 -0700 | [diff] [blame] | 2114 | |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 2115 | int fromUserId = event.getPreviousUserId(); |
| 2116 | if (fromUserId != UserHandle.USER_NULL) { |
| 2117 | data.putInt(CarUserManager.BUNDLE_PARAM_PREVIOUS_USER_ID, fromUserId); |
Yan Zhu | e792152 | 2020-04-16 15:59:25 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2120 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 2121 | Slog.d(TAG, "Notifying listener for uid " + uid); |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 2122 | } |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 2123 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_NOTIFY_APP_LIFECYCLE_LISTENER, |
| 2124 | uid, eventType, fromUserId, userId); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2125 | try { |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 2126 | t.traceBegin("notify-app-listener-uid-" + uid); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2127 | listener.send(userId, data); |
| 2128 | } catch (RemoteException e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2129 | Slog.e(TAG, "Error calling lifecycle listener", e); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2130 | } finally { |
| 2131 | t.traceEnd(); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2132 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2133 | } |
| 2134 | t.traceEnd(); // notify-app-listeners-user-USERID-event-EVENT_TYPE |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2137 | private void handleNotifyServiceUserLifecycleListeners(UserLifecycleEvent event) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2138 | TimingsTraceLog t = new TimingsTraceLog(TAG, Trace.TRACE_TAG_SYSTEM_SERVER); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2139 | if (mUserLifecycleListeners.isEmpty()) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2140 | Slog.w(TAG, "Not notifying internal UserLifecycleListeners"); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2141 | return; |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2142 | } else if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 2143 | Slog.d(TAG, "Notifying " + mUserLifecycleListeners.size() |
| 2144 | + " service listeners of " + event); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2145 | } |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 2146 | |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 2147 | int userId = event.getUserId(); |
| 2148 | int eventType = event.getEventType(); |
| 2149 | t.traceBegin("notify-listeners-user-" + userId + "-event-" + eventType); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2150 | for (UserLifecycleListener listener : mUserLifecycleListeners) { |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 2151 | String listenerName = FunctionalUtils.getLambdaName(listener); |
felipeal | de1e16d | 2020-06-03 13:20:48 -0700 | [diff] [blame] | 2152 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_NOTIFY_INTERNAL_LIFECYCLE_LISTENER, |
| 2153 | listenerName, eventType, event.getPreviousUserId(), userId); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2154 | try { |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2155 | t.traceBegin("notify-listener-" + listenerName); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2156 | listener.onEvent(event); |
| 2157 | } catch (RuntimeException e) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2158 | Slog.e(TAG, |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 2159 | "Exception raised when invoking onEvent for " + listenerName, e); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2160 | } finally { |
| 2161 | t.traceEnd(); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2162 | } |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2163 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 2164 | t.traceEnd(); // notify-listeners-user-USERID-event-EVENT_TYPE |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2167 | private void onUserSwitching(@UserIdInt int fromUserId, @UserIdInt int toUserId) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2168 | Slog.i(TAG, "onUserSwitching() callback for user " + toUserId); |
| 2169 | TimingsTraceLog t = new TimingsTraceLog(TAG, Trace.TRACE_TAG_SYSTEM_SERVER); |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2170 | t.traceBegin("onUserSwitching-" + toUserId); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 2171 | |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2172 | // Switch HAL users if user switch is not requested by CarUserService |
| 2173 | notifyHalLegacySwitch(fromUserId, toUserId); |
| 2174 | |
Mayank Garg | e90a408 | 2020-09-30 12:57:34 -0700 | [diff] [blame] | 2175 | mInitialUserSetter.setLastActiveUser(toUserId); |
felipeal | bf32765 | 2020-06-03 11:33:29 -0700 | [diff] [blame] | 2176 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2177 | if (mLastPassengerId != UserHandle.USER_NULL) { |
| 2178 | stopPassengerInternal(mLastPassengerId, false); |
| 2179 | } |
| 2180 | if (mEnablePassengerSupport && isPassengerDisplayAvailable()) { |
| 2181 | setupPassengerUser(); |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2182 | startFirstPassenger(toUserId); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2183 | } |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 2184 | t.traceEnd(); |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2187 | private void notifyHalLegacySwitch(@UserIdInt int fromUserId, @UserIdInt int toUserId) { |
| 2188 | synchronized (mLockUser) { |
felipeal | 7d12ee2 | 2020-06-05 09:30:19 -0700 | [diff] [blame] | 2189 | if (mUserIdForUserSwitchInProcess != UserHandle.USER_NULL) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2190 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 2191 | Slog.d(TAG, "notifyHalLegacySwitch(" + fromUserId + ", " + toUserId |
felipeal | 7d12ee2 | 2020-06-05 09:30:19 -0700 | [diff] [blame] | 2192 | + "): not needed, normal switch for " + mUserIdForUserSwitchInProcess); |
| 2193 | } |
| 2194 | return; |
| 2195 | } |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Mayank Garg | 9ed099e | 2020-06-04 16:05:20 -0700 | [diff] [blame] | 2198 | if (!isUserHalSupported()) return; |
| 2199 | |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2200 | // switch HAL user |
felipeal | 7d12ee2 | 2020-06-05 09:30:19 -0700 | [diff] [blame] | 2201 | UsersInfo usersInfo = UserHalHelper.newUsersInfo(mUserManager, fromUserId); |
Mayank Garg | eb37d09 | 2020-06-02 14:37:57 -0700 | [diff] [blame] | 2202 | SwitchUserRequest request = createUserSwitchRequest(toUserId, usersInfo); |
| 2203 | mHal.legacyUserSwitch(request); |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 2206 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 2207 | * 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] | 2208 | * run inside this call. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 2209 | * |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 2210 | * @param r Runnable to run. |
| 2211 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 2212 | public void runOnUser0Unlock(@NonNull Runnable r) { |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 2213 | Objects.requireNonNull(r, "runnable cannot be null"); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 2214 | boolean runNow = false; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2215 | synchronized (mLockUser) { |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 2216 | if (mUser0Unlocked) { |
| 2217 | runNow = true; |
| 2218 | } else { |
| 2219 | mUser0UnlockTasks.add(r); |
| 2220 | } |
| 2221 | } |
| 2222 | if (runNow) { |
| 2223 | r.run(); |
| 2224 | } |
| 2225 | } |
| 2226 | |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 2227 | @VisibleForTesting |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 2228 | @NonNull |
| 2229 | ArrayList<Integer> getBackgroundUsersToRestart() { |
| 2230 | ArrayList<Integer> backgroundUsersToRestart = null; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2231 | synchronized (mLockUser) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 2232 | backgroundUsersToRestart = new ArrayList<>(mBackgroundUsersToRestart); |
| 2233 | } |
| 2234 | return backgroundUsersToRestart; |
| 2235 | } |
| 2236 | |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 2237 | private void setSystemUserRestrictions() { |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 2238 | // Disable Location service for system user. |
| 2239 | LocationManager locationManager = |
| 2240 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
Anthony Hugh | 04ee04f | 2019-12-17 16:11:33 -0800 | [diff] [blame] | 2241 | locationManager.setLocationEnabledForUser( |
| 2242 | /* enabled= */ false, UserHandle.of(UserHandle.USER_SYSTEM)); |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 2243 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 2244 | |
| 2245 | /** |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 2246 | * Assigns a default icon to a user according to the user's id. |
| 2247 | * |
| 2248 | * @param userInfo User whose avatar is set to default icon. |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 2249 | */ |
Eric Jeong | b2dc6ff | 2020-06-05 17:00:26 -0700 | [diff] [blame] | 2250 | private void assignDefaultIcon(UserInfo userInfo) { |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 2251 | int idForIcon = userInfo.isGuest() ? UserHandle.USER_NULL : userInfo.id; |
| 2252 | Bitmap bitmap = UserIcons.convertToBitmap( |
| 2253 | UserIcons.getDefaultUserIcon(mContext.getResources(), idForIcon, false)); |
| 2254 | mUserManager.setUserIcon(userInfo.id, bitmap); |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 2257 | private interface UserFilter { |
| 2258 | boolean isEligibleUser(UserInfo user); |
| 2259 | } |
| 2260 | |
| 2261 | /** Returns all users who are matched by the given filter. */ |
| 2262 | private List<UserInfo> getUsers(UserFilter filter) { |
Colin Cross | 0df71ea | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 2263 | List<UserInfo> users = mUserManager.getAliveUsers(); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 2264 | |
| 2265 | for (Iterator<UserInfo> iterator = users.iterator(); iterator.hasNext(); ) { |
| 2266 | UserInfo user = iterator.next(); |
| 2267 | if (!filter.isEligibleUser(user)) { |
| 2268 | iterator.remove(); |
| 2269 | } |
| 2270 | } |
| 2271 | return users; |
| 2272 | } |
| 2273 | |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 2274 | private static void checkManageUsersOrDumpPermission(String message) { |
Felipe Leme | 5523672 | 2020-10-16 16:54:32 -0700 | [diff] [blame] | 2275 | checkHasAtLeastOnePermissionGranted(message, |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 2276 | android.Manifest.permission.MANAGE_USERS, |
| 2277 | android.Manifest.permission.DUMP); |
| 2278 | } |
| 2279 | |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 2280 | private void checkInteractAcrossUsersPermission(String message) { |
Felipe Leme | 5523672 | 2020-10-16 16:54:32 -0700 | [diff] [blame] | 2281 | checkHasAtLeastOnePermissionGranted(message, |
| 2282 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 2283 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 2284 | } |
| 2285 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2286 | private int getNumberOfManagedProfiles(@UserIdInt int userId) { |
Colin Cross | 0df71ea | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 2287 | List<UserInfo> users = mUserManager.getAliveUsers(); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2288 | // Count all users that are managed profiles of the given user. |
| 2289 | int managedProfilesCount = 0; |
| 2290 | for (UserInfo user : users) { |
| 2291 | if (user.isManagedProfile() && user.profileGroupId == userId) { |
| 2292 | managedProfilesCount++; |
| 2293 | } |
| 2294 | } |
| 2295 | return managedProfilesCount; |
| 2296 | } |
| 2297 | |
| 2298 | /** |
| 2299 | * Starts the first passenger of the given driver and assigns the passenger to the front |
| 2300 | * passenger zone. |
| 2301 | * |
| 2302 | * @param driverId User id of the driver. |
| 2303 | * @return whether it succeeds. |
| 2304 | */ |
| 2305 | private boolean startFirstPassenger(@UserIdInt int driverId) { |
| 2306 | int zoneId = getAvailablePassengerZone(); |
| 2307 | if (zoneId == OccupantZoneInfo.INVALID_ZONE_ID) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2308 | Slog.w(TAG, "passenger occupant zone is not found"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2309 | return false; |
| 2310 | } |
| 2311 | List<UserInfo> passengers = getPassengers(driverId); |
| 2312 | if (passengers.size() < 1) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2313 | Slog.w(TAG, "passenger is not found"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2314 | return false; |
| 2315 | } |
| 2316 | // Only one passenger is supported. If there are two or more passengers, the first passenger |
| 2317 | // is chosen. |
| 2318 | int passengerId = passengers.get(0).id; |
| 2319 | if (!startPassenger(passengerId, zoneId)) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2320 | Slog.w(TAG, "cannot start passenger " + passengerId); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2321 | return false; |
| 2322 | } |
| 2323 | return true; |
| 2324 | } |
| 2325 | |
| 2326 | private int getAvailablePassengerZone() { |
| 2327 | int[] occupantTypes = new int[] {CarOccupantZoneManager.OCCUPANT_TYPE_FRONT_PASSENGER, |
| 2328 | CarOccupantZoneManager.OCCUPANT_TYPE_REAR_PASSENGER}; |
| 2329 | for (int occupantType : occupantTypes) { |
| 2330 | int zoneId = getZoneId(occupantType); |
| 2331 | if (zoneId != OccupantZoneInfo.INVALID_ZONE_ID) { |
| 2332 | return zoneId; |
| 2333 | } |
| 2334 | } |
| 2335 | return OccupantZoneInfo.INVALID_ZONE_ID; |
| 2336 | } |
| 2337 | |
| 2338 | /** |
| 2339 | * Creates a new passenger user when there is no passenger user. |
| 2340 | */ |
| 2341 | private void setupPassengerUser() { |
| 2342 | int currentUser = ActivityManager.getCurrentUser(); |
| 2343 | int profileCount = getNumberOfManagedProfiles(currentUser); |
| 2344 | if (profileCount > 0) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2345 | Slog.w(TAG, "max profile of user" + currentUser |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2346 | + " is exceeded: current profile count is " + profileCount); |
| 2347 | return; |
| 2348 | } |
| 2349 | // TODO(b/140311342): Use resource string for the default passenger name. |
| 2350 | UserInfo passenger = createPassenger("Passenger", currentUser); |
| 2351 | if (passenger == null) { |
| 2352 | // Couldn't create user, most likely because there are too many. |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2353 | Slog.w(TAG, "cannot create a passenger user"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2354 | return; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | @NonNull |
| 2359 | private List<OccupantZoneInfo> getOccupantZones(@OccupantTypeEnum int occupantType) { |
| 2360 | ZoneUserBindingHelper helper = null; |
| 2361 | synchronized (mLockHelper) { |
| 2362 | if (mZoneUserBindingHelper == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2363 | Slog.w(TAG, "implementation is not delegated"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2364 | return new ArrayList<OccupantZoneInfo>(); |
| 2365 | } |
| 2366 | helper = mZoneUserBindingHelper; |
| 2367 | } |
| 2368 | return helper.getOccupantZones(occupantType); |
| 2369 | } |
| 2370 | |
| 2371 | private boolean assignUserToOccupantZone(@UserIdInt int userId, int zoneId) { |
| 2372 | ZoneUserBindingHelper helper = null; |
| 2373 | synchronized (mLockHelper) { |
| 2374 | if (mZoneUserBindingHelper == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2375 | Slog.w(TAG, "implementation is not delegated"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2376 | return false; |
| 2377 | } |
| 2378 | helper = mZoneUserBindingHelper; |
| 2379 | } |
| 2380 | return helper.assignUserToOccupantZone(userId, zoneId); |
| 2381 | } |
| 2382 | |
| 2383 | private boolean unassignUserFromOccupantZone(@UserIdInt int userId) { |
| 2384 | ZoneUserBindingHelper helper = null; |
| 2385 | synchronized (mLockHelper) { |
| 2386 | if (mZoneUserBindingHelper == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2387 | Slog.w(TAG, "implementation is not delegated"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2388 | return false; |
| 2389 | } |
| 2390 | helper = mZoneUserBindingHelper; |
| 2391 | } |
| 2392 | return helper.unassignUserFromOccupantZone(userId); |
| 2393 | } |
| 2394 | |
| 2395 | private boolean isPassengerDisplayAvailable() { |
| 2396 | ZoneUserBindingHelper helper = null; |
| 2397 | synchronized (mLockHelper) { |
| 2398 | if (mZoneUserBindingHelper == null) { |
Eric Jeong | f75d413 | 2020-12-21 13:48:23 -0800 | [diff] [blame] | 2399 | Slog.w(TAG, "implementation is not delegated"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 2400 | return false; |
| 2401 | } |
| 2402 | helper = mZoneUserBindingHelper; |
| 2403 | } |
| 2404 | return helper.isPassengerDisplayAvailable(); |
| 2405 | } |
| 2406 | |
| 2407 | /** |
| 2408 | * Gets the zone id of the given occupant type. If there are two or more zones, the first found |
| 2409 | * zone is returned. |
| 2410 | * |
| 2411 | * @param occupantType The type of an occupant. |
| 2412 | * @return The zone id of the given occupant type. {@link OccupantZoneInfo.INVALID_ZONE_ID}, |
| 2413 | * if not found. |
| 2414 | */ |
| 2415 | private int getZoneId(@OccupantTypeEnum int occupantType) { |
| 2416 | List<OccupantZoneInfo> zoneInfos = getOccupantZones(occupantType); |
| 2417 | return (zoneInfos.size() > 0) ? zoneInfos.get(0).zoneId : OccupantZoneInfo.INVALID_ZONE_ID; |
| 2418 | } |
Mayank Garg | 9732d60 | 2020-08-09 21:02:40 -0700 | [diff] [blame] | 2419 | |
| 2420 | /** |
| 2421 | * Manages the required number of pre-created users. |
| 2422 | */ |
Mayank Garg | a3b72d0 | 2021-03-14 23:05:51 -0700 | [diff] [blame] | 2423 | @Override |
| 2424 | public void updatePreCreatedUsers() { |
| 2425 | checkManageOrCreateUsersPermission("preCreateUsers"); |
| 2426 | preCreateUsersInternal(); |
Mayank Garg | 0257697 | 2021-02-18 16:46:07 -0800 | [diff] [blame] | 2427 | } |
| 2428 | |
Mayank Garg | a3b72d0 | 2021-03-14 23:05:51 -0700 | [diff] [blame] | 2429 | private void preCreateUsersInternal() { |
| 2430 | mHandler.post(() -> mUserPreCreator.managePreCreatedUsers()); |
Mayank Garg | 9732d60 | 2020-08-09 21:02:40 -0700 | [diff] [blame] | 2431 | } |
Felipe Leme | 1779920 | 2020-09-03 12:55:53 -0700 | [diff] [blame] | 2432 | |
| 2433 | // TODO(b/167698977): members below were copied from UserManagerService; it would be better to |
| 2434 | // move them to some internal android.os class instead. |
| 2435 | |
| 2436 | private static final int ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION = |
| 2437 | UserInfo.FLAG_MANAGED_PROFILE |
| 2438 | | UserInfo.FLAG_PROFILE |
| 2439 | | UserInfo.FLAG_EPHEMERAL |
| 2440 | | UserInfo.FLAG_RESTRICTED |
| 2441 | | UserInfo.FLAG_GUEST |
| 2442 | | UserInfo.FLAG_DEMO |
| 2443 | | UserInfo.FLAG_FULL; |
| 2444 | |
| 2445 | private static void checkManageUsersPermission(String message) { |
| 2446 | if (!hasManageUsersPermission()) { |
| 2447 | throw new SecurityException("You need " + MANAGE_USERS + " permission to: " + message); |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | private static void checkManageOrCreateUsersPermission(String message) { |
| 2452 | if (!hasManageOrCreateUsersPermission()) { |
| 2453 | throw new SecurityException( |
| 2454 | "You either need " + MANAGE_USERS + " or " + CREATE_USERS + " permission to: " |
| 2455 | + message); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | private static void checkManageOrCreateUsersPermission(int creationFlags) { |
| 2460 | if ((creationFlags & ~ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION) == 0) { |
| 2461 | if (!hasManageOrCreateUsersPermission()) { |
| 2462 | throw new SecurityException("You either need " + MANAGE_USERS + " or " |
| 2463 | + CREATE_USERS + "permission to create a user with flags " |
| 2464 | + creationFlags); |
| 2465 | } |
| 2466 | } else if (!hasManageUsersPermission()) { |
| 2467 | throw new SecurityException("You need " + MANAGE_USERS + " permission to create a user" |
| 2468 | + " with flags " + creationFlags); |
| 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | private static boolean hasManageUsersPermission() { |
| 2473 | final int callingUid = Binder.getCallingUid(); |
| 2474 | return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 2475 | || callingUid == Process.ROOT_UID |
| 2476 | || hasPermissionGranted(MANAGE_USERS, callingUid); |
| 2477 | } |
| 2478 | |
| 2479 | private static boolean hasManageUsersOrPermission(String alternativePermission) { |
| 2480 | final int callingUid = Binder.getCallingUid(); |
| 2481 | return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 2482 | || callingUid == Process.ROOT_UID |
| 2483 | || hasPermissionGranted(MANAGE_USERS, callingUid) |
| 2484 | || hasPermissionGranted(alternativePermission, callingUid); |
| 2485 | } |
| 2486 | |
| 2487 | private static boolean hasManageOrCreateUsersPermission() { |
| 2488 | return hasManageUsersOrPermission(CREATE_USERS); |
| 2489 | } |
| 2490 | |
| 2491 | private static boolean hasPermissionGranted(String permission, int uid) { |
| 2492 | return ActivityManager.checkComponentPermission(permission, uid, /* owningUid= */ -1, |
| 2493 | /* exported= */ true) == PackageManager.PERMISSION_GRANTED; |
| 2494 | } |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 2495 | } |