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