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