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