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