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