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