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 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 19 | import static com.android.car.CarLog.TAG_USER; |
| 20 | |
| 21 | import android.annotation.NonNull; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 23 | import android.annotation.UserIdInt; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 25 | import android.app.ActivityManager.StackInfo; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 26 | import android.app.IActivityManager; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 27 | import android.car.CarOccupantZoneManager; |
| 28 | import android.car.CarOccupantZoneManager.OccupantTypeEnum; |
| 29 | import android.car.CarOccupantZoneManager.OccupantZoneInfo; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 30 | import android.car.ICarUserService; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame] | 31 | import android.car.settings.CarSettings; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 32 | import android.car.user.CarUserManager; |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 33 | import android.car.user.CarUserManager.UserLifecycleEvent; |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 34 | import android.car.user.CarUserManager.UserLifecycleEventType; |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 35 | import android.car.user.CarUserManager.UserLifecycleListener; |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 36 | import android.car.user.GetUserIdentificationAssociationResponse; |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 37 | import android.car.user.UserSwitchResult; |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 38 | import android.car.userlib.CarUserManagerHelper; |
felipeal | 19e3d73 | 2020-03-18 12:07:32 -0700 | [diff] [blame] | 39 | import android.car.userlib.HalCallback; |
| 40 | import android.car.userlib.UserHalHelper; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 41 | import android.content.Context; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 42 | import android.content.pm.UserInfo; |
Felipe Leme | 315a53b | 2020-03-12 10:51:04 -0700 | [diff] [blame] | 43 | import android.content.res.Resources; |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 44 | import android.graphics.Bitmap; |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 45 | import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponse; |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 46 | import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponseAction; |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 47 | import android.hardware.automotive.vehicle.V2_0.SwitchUserStatus; |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 48 | import android.hardware.automotive.vehicle.V2_0.UserIdentificationGetRequest; |
| 49 | import android.hardware.automotive.vehicle.V2_0.UserIdentificationResponse; |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 50 | import android.hardware.automotive.vehicle.V2_0.UsersInfo; |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 51 | import android.location.LocationManager; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 52 | import android.os.Binder; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 53 | import android.os.Bundle; |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 54 | import android.os.Handler; |
| 55 | import android.os.HandlerThread; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 56 | import android.os.RemoteException; |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 57 | import android.os.Trace; |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 58 | import android.os.UserHandle; |
Ying Zheng | 8f90edb | 2018-06-13 12:42:31 -0700 | [diff] [blame] | 59 | import android.os.UserManager; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame] | 60 | import android.provider.Settings; |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 61 | import android.sysprop.CarProperties; |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 62 | import android.util.EventLog; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 63 | import android.util.Log; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 64 | import android.util.SparseArray; |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 65 | import android.util.TimingsTraceLog; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 66 | |
| 67 | import com.android.car.CarServiceBase; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 68 | import com.android.car.CarServiceUtils; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 69 | import com.android.car.R; |
Felipe Leme | 5841220 | 2020-01-09 13:45:33 -0800 | [diff] [blame] | 70 | import com.android.car.hal.UserHalService; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 71 | import com.android.internal.annotations.GuardedBy; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 72 | import com.android.internal.annotations.VisibleForTesting; |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 73 | import com.android.internal.car.EventLogTags; |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 74 | import com.android.internal.infra.AndroidFuture; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 75 | import com.android.internal.os.IResultReceiver; |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 76 | import com.android.internal.util.ArrayUtils; |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 77 | import com.android.internal.util.FunctionalUtils; |
Mayank Garg | e19c292 | 2020-03-30 18:05:53 -0700 | [diff] [blame] | 78 | import com.android.internal.util.Preconditions; |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 79 | import com.android.internal.util.UserIcons; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 80 | |
| 81 | import java.io.PrintWriter; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 82 | import java.util.ArrayList; |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 83 | import java.util.Arrays; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 84 | import java.util.Iterator; |
| 85 | import java.util.List; |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 86 | import java.util.Objects; |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 87 | import java.util.concurrent.CopyOnWriteArrayList; |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 88 | import java.util.concurrent.CountDownLatch; |
| 89 | import java.util.concurrent.TimeUnit; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * User service for cars. Manages users at boot time. Including: |
| 93 | * |
| 94 | * <ol> |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 95 | * <li> Creates a user used as driver. |
| 96 | * <li> Creates a user used as passenger. |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 97 | * <li> Creates a secondary admin user on first run. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 98 | * <li> Switch drivers. |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 99 | * <ol/> |
| 100 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 101 | public final class CarUserService extends ICarUserService.Stub implements CarServiceBase { |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 102 | |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 103 | private static final String TAG = TAG_USER; |
| 104 | |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 105 | /** {@code int} extra used to represent a user id in a {@link IResultReceiver} response. */ |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 106 | public static final String BUNDLE_USER_ID = "user.id"; |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 107 | /** {@code int} extra used to represent user flags in a {@link IResultReceiver} response. */ |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 108 | public static final String BUNDLE_USER_FLAGS = "user.flags"; |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 109 | /** {@code String} extra used to represent a user name in a {@link IResultReceiver} response. */ |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 110 | public static final String BUNDLE_USER_NAME = "user.name"; |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 111 | /** {@code int} extra used to represent the info action {@link IResultReceiver} response. */ |
| 112 | public static final String BUNDLE_INITIAL_INFO_ACTION = "initial_info.action"; |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 113 | |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 114 | private final Context mContext; |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 115 | private final CarUserManagerHelper mCarUserManagerHelper; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 116 | private final IActivityManager mAm; |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 117 | private final UserManager mUserManager; |
| 118 | private final int mMaxRunningUsers; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 119 | private final boolean mEnablePassengerSupport; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 120 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 121 | private final Object mLockUser = new Object(); |
| 122 | @GuardedBy("mLockUser") |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 123 | private boolean mUser0Unlocked; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 124 | @GuardedBy("mLockUser") |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 125 | private final ArrayList<Runnable> mUser0UnlockTasks = new ArrayList<>(); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 126 | // Only one passenger is supported. |
| 127 | @GuardedBy("mLockUser") |
| 128 | private @UserIdInt int mLastPassengerId; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 129 | /** |
| 130 | * 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] | 131 | * 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] | 132 | */ |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 133 | @GuardedBy("mLockUser") |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 134 | private final ArrayList<Integer> mBackgroundUsersToRestart = new ArrayList<>(); |
| 135 | /** |
| 136 | * Keep the list of background users started here. This is wholly for debugging purpose. |
| 137 | */ |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 138 | @GuardedBy("mLockUser") |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 139 | private final ArrayList<Integer> mBackgroundUsersRestartedHere = new ArrayList<>(); |
| 140 | |
Felipe Leme | 5841220 | 2020-01-09 13:45:33 -0800 | [diff] [blame] | 141 | private final UserHalService mHal; |
| 142 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 143 | // HandlerThread and Handler used when notifying app listeners (mAppLifecycleListeners). |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 144 | private final HandlerThread mHandlerThread = CarServiceUtils.getHandlerThread( |
| 145 | getClass().getSimpleName()); |
| 146 | private final Handler mHandler = new Handler(mHandlerThread.getLooper()); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 147 | |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 148 | /** |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 149 | * List of listeners to be notified on new user activities events. |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 150 | * This collection should be accessed and manipulated by mHandlerThread only. |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 151 | */ |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 152 | private final List<UserLifecycleListener> mUserLifecycleListeners = new ArrayList<>(); |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 155 | * List of lifecycle listeners by uid. |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 156 | * This collection should be accessed and manipulated by mHandlerThread only. |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 157 | */ |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 158 | private final SparseArray<IResultReceiver> mAppLifecycleListeners = new SparseArray<>(); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 159 | |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 160 | /** |
| 161 | * User Id for the user switch in process, if any. |
| 162 | */ |
| 163 | @GuardedBy("mLockUser") |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 164 | private int mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Request Id for the user switch in process, if any. |
| 167 | */ |
| 168 | @GuardedBy("mLockUser") |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 169 | private int mRequestIdForUserSwitchInProcess; |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 170 | private final int mHalTimeoutMs = CarProperties.user_hal_timeout().orElse(5_000); |
| 171 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 172 | private final CopyOnWriteArrayList<PassengerCallback> mPassengerCallbacks = |
| 173 | new CopyOnWriteArrayList<>(); |
| 174 | |
felipeal | 61ce373 | 2020-04-03 11:01:00 -0700 | [diff] [blame] | 175 | @Nullable |
| 176 | @GuardedBy("mLockUser") |
| 177 | private UserInfo mInitialUser; |
| 178 | |
Mayank Garg | 71661ea | 2020-04-29 01:25:03 -0700 | [diff] [blame] | 179 | private UserMetrics mUserMetrics; |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 180 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 181 | /** Interface for callbaks related to passenger activities. */ |
| 182 | public interface PassengerCallback { |
| 183 | /** Called when passenger is started at a certain zone. */ |
| 184 | void onPassengerStarted(@UserIdInt int passengerId, int zoneId); |
| 185 | /** Called when passenger is stopped. */ |
| 186 | void onPassengerStopped(@UserIdInt int passengerId); |
| 187 | } |
| 188 | |
| 189 | /** Interface for delegating zone-related implementation to CarOccupantZoneService. */ |
| 190 | public interface ZoneUserBindingHelper { |
| 191 | /** Gets occupant zones corresponding to the occupant type. */ |
| 192 | @NonNull |
| 193 | List<OccupantZoneInfo> getOccupantZones(@OccupantTypeEnum int occupantType); |
| 194 | /** Assigns the user to the occupant zone. */ |
| 195 | boolean assignUserToOccupantZone(@UserIdInt int userId, int zoneId); |
| 196 | /** Makes the occupant zone unoccupied. */ |
| 197 | boolean unassignUserFromOccupantZone(@UserIdInt int userId); |
| 198 | /** Returns whether there is a passenger display. */ |
| 199 | boolean isPassengerDisplayAvailable(); |
| 200 | } |
| 201 | |
| 202 | private final Object mLockHelper = new Object(); |
| 203 | @GuardedBy("mLockHelper") |
| 204 | private ZoneUserBindingHelper mZoneUserBindingHelper; |
| 205 | |
Felipe Leme | 5841220 | 2020-01-09 13:45:33 -0800 | [diff] [blame] | 206 | public CarUserService(@NonNull Context context, @NonNull UserHalService hal, |
Mayank Garg | 71661ea | 2020-04-29 01:25:03 -0700 | [diff] [blame] | 207 | @NonNull CarUserManagerHelper carUserManagerHelper, @NonNull UserManager userManager, |
| 208 | @NonNull IActivityManager am, int maxRunningUsers) { |
| 209 | this(context, hal, carUserManagerHelper, userManager, am, maxRunningUsers, |
| 210 | new UserMetrics()); |
| 211 | } |
| 212 | |
| 213 | @VisibleForTesting |
| 214 | CarUserService(@NonNull Context context, @NonNull UserHalService hal, |
| 215 | @NonNull CarUserManagerHelper carUserManagerHelper, @NonNull UserManager userManager, |
| 216 | @NonNull IActivityManager am, int maxRunningUsers, UserMetrics userMetrics) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 217 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 218 | Log.d(TAG_USER, "constructed"); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 219 | } |
| 220 | mContext = context; |
Felipe Leme | 5841220 | 2020-01-09 13:45:33 -0800 | [diff] [blame] | 221 | mHal = hal; |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 222 | mCarUserManagerHelper = carUserManagerHelper; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 223 | mAm = am; |
| 224 | mMaxRunningUsers = maxRunningUsers; |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 225 | mUserManager = userManager; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 226 | mLastPassengerId = UserHandle.USER_NULL; |
| 227 | mEnablePassengerSupport = context.getResources().getBoolean(R.bool.enablePassengerSupport); |
Mayank Garg | 71661ea | 2020-04-29 01:25:03 -0700 | [diff] [blame] | 228 | mUserMetrics = userMetrics; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void init() { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 233 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 234 | Log.d(TAG_USER, "init"); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 235 | } |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void release() { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 240 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 241 | Log.d(TAG_USER, "release"); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 242 | } |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | @Override |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 246 | public void dump(@NonNull PrintWriter writer) { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 247 | checkAtLeastOnePermission("dump()", android.Manifest.permission.DUMP); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 248 | writer.println("*CarUserService*"); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 249 | String indent = " "; |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 250 | handleDumpListeners(writer, indent); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 251 | synchronized (mLockUser) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 252 | writer.println("User0Unlocked: " + mUser0Unlocked); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 253 | writer.println("BackgroundUsersToRestart: " + mBackgroundUsersToRestart); |
| 254 | writer.println("BackgroundUsersRestarted: " + mBackgroundUsersRestartedHere); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 255 | } |
| 256 | writer.println("MaxRunningUsers: " + mMaxRunningUsers); |
| 257 | List<UserInfo> allDrivers = getAllDrivers(); |
| 258 | int driversSize = allDrivers.size(); |
| 259 | writer.println("NumberOfDrivers: " + driversSize); |
| 260 | for (int i = 0; i < driversSize; i++) { |
| 261 | int driverId = allDrivers.get(i).id; |
| 262 | writer.print(indent + "#" + i + ": id=" + driverId); |
| 263 | List<UserInfo> passengers = getPassengers(driverId); |
| 264 | int passengersSize = passengers.size(); |
| 265 | writer.print(" NumberPassengers: " + passengersSize); |
| 266 | if (passengersSize > 0) { |
| 267 | writer.print(" ["); |
| 268 | for (int j = 0; j < passengersSize; j++) { |
| 269 | writer.print(passengers.get(j).id); |
| 270 | if (j < passengersSize - 1) { |
| 271 | writer.print(" "); |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 272 | } |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 273 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 274 | writer.print("]"); |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 275 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 276 | writer.println(); |
| 277 | } |
| 278 | writer.printf("EnablePassengerSupport: %s\n", mEnablePassengerSupport); |
| 279 | writer.printf("User HAL timeout: %dms\n", mHalTimeoutMs); |
| 280 | writer.printf("Initial user: %s\n", mInitialUser); |
| 281 | writer.println("Relevant overlayable properties"); |
| 282 | Resources res = mContext.getResources(); |
| 283 | writer.printf("%sowner_name=%s\n", indent, |
| 284 | res.getString(com.android.internal.R.string.owner_name)); |
| 285 | writer.printf("%sdefault_guest_name=%s\n", indent, |
| 286 | res.getString(R.string.default_guest_name)); |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 287 | writer.printf("User switch in process=%d\n", mUserIdForUserSwitchInProcess); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 288 | writer.printf("Request Id for the user switch in process=%d\n ", |
| 289 | mRequestIdForUserSwitchInProcess); |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 290 | |
| 291 | dumpUserMetrics(writer); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Dumps user metrics. |
| 296 | */ |
| 297 | public void dumpUserMetrics(@NonNull PrintWriter writer) { |
| 298 | mUserMetrics.dump(writer); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Dumps first user unlocking time. |
| 303 | */ |
| 304 | public void dumpFirstUserUnlockDuration(PrintWriter writer) { |
| 305 | mUserMetrics.dumpFirstUserUnlockDuration(writer); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | private void handleDumpListeners(@NonNull PrintWriter writer, String indent) { |
| 309 | CountDownLatch latch = new CountDownLatch(1); |
| 310 | mHandler.post(() -> { |
| 311 | handleDumpUserLifecycleListeners(writer); |
| 312 | handleDumpAppLifecycleListeners(writer, indent); |
| 313 | latch.countDown(); |
| 314 | }); |
| 315 | int timeout = 5; |
| 316 | try { |
| 317 | if (!latch.await(timeout, TimeUnit.SECONDS)) { |
| 318 | writer.printf("Handler thread didn't respond in %ds when dumping listeners\n", |
| 319 | timeout); |
| 320 | } |
| 321 | } catch (InterruptedException e) { |
| 322 | Thread.currentThread().interrupt(); |
| 323 | writer.println("Interrupted waiting for handler thread to dump app and user listeners"); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | private void handleDumpUserLifecycleListeners(@NonNull PrintWriter writer) { |
| 328 | if (mUserLifecycleListeners.isEmpty()) { |
| 329 | writer.println("No user lifecycle listeners"); |
| 330 | return; |
| 331 | } |
| 332 | writer.printf("%d user lifecycle listeners\n", mUserLifecycleListeners.size()); |
| 333 | for (UserLifecycleListener listener : mUserLifecycleListeners) { |
| 334 | writer.printf("Listener %s\n", listener); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | private void handleDumpAppLifecycleListeners(@NonNull PrintWriter writer, String indent) { |
| 339 | int numberListeners = mAppLifecycleListeners.size(); |
| 340 | if (numberListeners == 0) { |
| 341 | writer.println("No lifecycle listeners"); |
| 342 | return; |
| 343 | } |
| 344 | writer.printf("%d lifecycle listeners\n", numberListeners); |
| 345 | for (int i = 0; i < numberListeners; i++) { |
| 346 | int uid = mAppLifecycleListeners.keyAt(i); |
| 347 | IResultReceiver listener = mAppLifecycleListeners.valueAt(i); |
| 348 | writer.printf("%suid: %d Listener %s\n", indent, uid, listener); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 349 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /** |
Eric Jeong | e7916fc | 2019-12-20 14:03:34 -0800 | [diff] [blame] | 353 | * Creates a driver who is a regular user and is allowed to login to the driving occupant zone. |
| 354 | * |
| 355 | * @param name The name of the driver to be created. |
| 356 | * @param admin Whether the created driver will be an admin. |
| 357 | * @return {@link UserInfo} object of the created driver, or {@code null} if the driver could |
| 358 | * not be created. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 359 | */ |
| 360 | @Override |
| 361 | @Nullable |
| 362 | public UserInfo createDriver(@NonNull String name, boolean admin) { |
| 363 | checkManageUsersPermission("createDriver"); |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 364 | Objects.requireNonNull(name, "name cannot be null"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 365 | if (admin) { |
| 366 | return createNewAdminUser(name); |
| 367 | } |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 368 | return mCarUserManagerHelper.createNewNonAdminUser(name); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
Eric Jeong | e7916fc | 2019-12-20 14:03:34 -0800 | [diff] [blame] | 372 | * Creates a passenger who is a profile of the given driver. |
| 373 | * |
| 374 | * @param name The name of the passenger to be created. |
| 375 | * @param driverId User id of the driver under whom a passenger is created. |
| 376 | * @return {@link UserInfo} object of the created passenger, or {@code null} if the passenger |
| 377 | * could not be created. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 378 | */ |
| 379 | @Override |
| 380 | @Nullable |
| 381 | public UserInfo createPassenger(@NonNull String name, @UserIdInt int driverId) { |
| 382 | checkManageUsersPermission("createPassenger"); |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 383 | Objects.requireNonNull(name, "name cannot be null"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 384 | UserInfo driver = mUserManager.getUserInfo(driverId); |
| 385 | if (driver == null) { |
| 386 | Log.w(TAG_USER, "the driver is invalid"); |
| 387 | return null; |
| 388 | } |
| 389 | if (driver.isGuest()) { |
| 390 | Log.w(TAG_USER, "a guest driver cannot create a passenger"); |
| 391 | return null; |
| 392 | } |
Bookatz | 42fb1a6 | 2019-10-30 11:45:01 -0700 | [diff] [blame] | 393 | UserInfo user = mUserManager.createProfileForUser(name, |
| 394 | UserManager.USER_TYPE_PROFILE_MANAGED, /* flags */ 0, driverId); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 395 | if (user == null) { |
| 396 | // Couldn't create user, most likely because there are too many. |
| 397 | Log.w(TAG_USER, "can't create a profile for user" + driverId); |
| 398 | return null; |
| 399 | } |
| 400 | // Passenger user should be a non-admin user. |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 401 | mCarUserManagerHelper.setDefaultNonAdminRestrictions(user, /* enable= */ true); |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 402 | assignDefaultIcon(user); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 403 | return user; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @see CarUserManager.switchDriver |
| 408 | */ |
| 409 | @Override |
| 410 | public boolean switchDriver(@UserIdInt int driverId) { |
| 411 | checkManageUsersPermission("switchDriver"); |
| 412 | if (driverId == UserHandle.USER_SYSTEM && UserManager.isHeadlessSystemUserMode()) { |
| 413 | // System user doesn't associate with real person, can not be switched to. |
| 414 | Log.w(TAG_USER, "switching to system user in headless system user mode is not allowed"); |
| 415 | return false; |
| 416 | } |
| 417 | int userSwitchable = mUserManager.getUserSwitchability(); |
| 418 | if (userSwitchable != UserManager.SWITCHABILITY_STATUS_OK) { |
| 419 | Log.w(TAG_USER, "current process is not allowed to switch user"); |
| 420 | return false; |
| 421 | } |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 422 | if (driverId == ActivityManager.getCurrentUser()) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 423 | // The current user is already the given user. |
| 424 | return true; |
| 425 | } |
| 426 | try { |
| 427 | return mAm.switchUser(driverId); |
| 428 | } catch (RemoteException e) { |
| 429 | // ignore |
| 430 | Log.w(TAG_USER, "error while switching user", e); |
| 431 | } |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | /** |
Eric Jeong | e7916fc | 2019-12-20 14:03:34 -0800 | [diff] [blame] | 436 | * Returns all drivers who can occupy the driving zone. Guest users are included in the list. |
| 437 | * |
| 438 | * @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] | 439 | */ |
| 440 | @Override |
| 441 | @NonNull |
| 442 | public List<UserInfo> getAllDrivers() { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 443 | checkManageUsersOrDumpPermission("getAllDrivers"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 444 | return getUsers((user) -> !isSystemUser(user.id) && user.isEnabled() |
| 445 | && !user.isManagedProfile() && !user.isEphemeral()); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
Eric Jeong | e7916fc | 2019-12-20 14:03:34 -0800 | [diff] [blame] | 449 | * Returns all passengers under the given driver. |
| 450 | * |
| 451 | * @param driverId User id of a driver. |
| 452 | * @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] | 453 | */ |
| 454 | @Override |
| 455 | @NonNull |
| 456 | public List<UserInfo> getPassengers(@UserIdInt int driverId) { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 457 | checkManageUsersOrDumpPermission("getPassengers"); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 458 | return getUsers((user) -> { |
| 459 | return !isSystemUser(user.id) && user.isEnabled() && user.isManagedProfile() |
| 460 | && user.profileGroupId == driverId; |
| 461 | }); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * @see CarUserManager.startPassenger |
| 466 | */ |
| 467 | @Override |
| 468 | public boolean startPassenger(@UserIdInt int passengerId, int zoneId) { |
| 469 | checkManageUsersPermission("startPassenger"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 470 | synchronized (mLockUser) { |
| 471 | try { |
| 472 | if (!mAm.startUserInBackgroundWithListener(passengerId, null)) { |
| 473 | Log.w(TAG_USER, "could not start passenger"); |
| 474 | return false; |
| 475 | } |
| 476 | } catch (RemoteException e) { |
| 477 | // ignore |
| 478 | Log.w(TAG_USER, "error while starting passenger", e); |
| 479 | return false; |
| 480 | } |
| 481 | if (!assignUserToOccupantZone(passengerId, zoneId)) { |
| 482 | Log.w(TAG_USER, "could not assign passenger to zone"); |
| 483 | return false; |
| 484 | } |
| 485 | mLastPassengerId = passengerId; |
| 486 | } |
| 487 | for (PassengerCallback callback : mPassengerCallbacks) { |
| 488 | callback.onPassengerStarted(passengerId, zoneId); |
| 489 | } |
| 490 | return true; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /** |
| 494 | * @see CarUserManager.stopPassenger |
| 495 | */ |
| 496 | @Override |
| 497 | public boolean stopPassenger(@UserIdInt int passengerId) { |
| 498 | checkManageUsersPermission("stopPassenger"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 499 | return stopPassengerInternal(passengerId, true); |
| 500 | } |
| 501 | |
| 502 | private boolean stopPassengerInternal(@UserIdInt int passengerId, boolean checkCurrentDriver) { |
| 503 | synchronized (mLockUser) { |
| 504 | UserInfo passenger = mUserManager.getUserInfo(passengerId); |
| 505 | if (passenger == null) { |
| 506 | Log.w(TAG_USER, "passenger " + passengerId + " doesn't exist"); |
| 507 | return false; |
| 508 | } |
| 509 | if (mLastPassengerId != passengerId) { |
| 510 | Log.w(TAG_USER, "passenger " + passengerId + " hasn't been started"); |
| 511 | return true; |
| 512 | } |
| 513 | if (checkCurrentDriver) { |
| 514 | int currentUser = ActivityManager.getCurrentUser(); |
| 515 | if (passenger.profileGroupId != currentUser) { |
| 516 | Log.w(TAG_USER, "passenger " + passengerId |
| 517 | + " is not a profile of the current user"); |
| 518 | return false; |
| 519 | } |
| 520 | } |
| 521 | // Passenger is a profile, so cannot be stopped through activity manager. |
| 522 | // Instead, activities started by the passenger are stopped and the passenger is |
| 523 | // unassigned from the zone. |
| 524 | stopAllTasks(passengerId); |
| 525 | if (!unassignUserFromOccupantZone(passengerId)) { |
| 526 | Log.w(TAG_USER, "could not unassign user from occupant zone"); |
| 527 | return false; |
| 528 | } |
| 529 | mLastPassengerId = UserHandle.USER_NULL; |
| 530 | } |
| 531 | for (PassengerCallback callback : mPassengerCallbacks) { |
| 532 | callback.onPassengerStopped(passengerId); |
| 533 | } |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | private void stopAllTasks(@UserIdInt int userId) { |
| 538 | try { |
| 539 | for (StackInfo info : mAm.getAllStackInfos()) { |
| 540 | for (int i = 0; i < info.taskIds.length; i++) { |
| 541 | if (info.taskUserIds[i] == userId) { |
| 542 | int taskId = info.taskIds[i]; |
| 543 | if (!mAm.removeTask(taskId)) { |
| 544 | Log.w(TAG_USER, "could not remove task " + taskId); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } catch (RemoteException e) { |
| 550 | Log.e(TAG_USER, "could not get stack info", e); |
| 551 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 554 | @Override |
| 555 | public void setLifecycleListenerForUid(IResultReceiver listener) { |
| 556 | int uid = Binder.getCallingUid(); |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 557 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SET_LIFECYCLE_LISTENER, uid); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 558 | checkInteractAcrossUsersPermission("setLifecycleListenerForUid" + uid); |
| 559 | |
| 560 | try { |
| 561 | listener.asBinder().linkToDeath(() -> onListenerDeath(uid), 0); |
| 562 | } catch (RemoteException e) { |
| 563 | Log.wtf(TAG_USER, "Cannot listen to death of " + uid); |
| 564 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 565 | mHandler.post(() -> mAppLifecycleListeners.append(uid, listener)); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | private void onListenerDeath(int uid) { |
| 569 | Log.i(TAG_USER, "Removing listeners for uid " + uid + " on binder death"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 570 | mHandler.post(() -> mAppLifecycleListeners.remove(uid)); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public void resetLifecycleListenerForUid() { |
| 575 | int uid = Binder.getCallingUid(); |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 576 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_RESET_LIFECYCLE_LISTENER, uid); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 577 | checkInteractAcrossUsersPermission("resetLifecycleListenerForUid-" + uid); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 578 | mHandler.post(() -> mAppLifecycleListeners.remove(uid)); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 581 | @Override |
| 582 | public void getInitialUserInfo(int requestType, int timeoutMs, |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 583 | @NonNull IResultReceiver receiver) { |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 584 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_REQ, requestType, |
| 585 | timeoutMs); |
Felipe Leme | e2600fc | 2020-02-26 11:06:04 -0800 | [diff] [blame] | 586 | Objects.requireNonNull(receiver, "receiver cannot be null"); |
felipeal | 0ec1ff0 | 2020-03-27 17:18:28 -0700 | [diff] [blame] | 587 | checkManageUsersPermission("getInitialInfo"); |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 588 | UsersInfo usersInfo = getUsersInfo(); |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 589 | mHal.getInitialUserInfo(requestType, timeoutMs, usersInfo, (status, resp) -> { |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 590 | Bundle resultData = null; |
| 591 | if (resp != null) { |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 592 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_RESP, |
| 593 | status, resp.action, resp.userToSwitchOrCreate.userId, |
| 594 | resp.userToSwitchOrCreate.flags, resp.userNameToCreate); |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 595 | switch (resp.action) { |
| 596 | case InitialUserInfoResponseAction.SWITCH: |
| 597 | resultData = new Bundle(); |
| 598 | resultData.putInt(BUNDLE_INITIAL_INFO_ACTION, resp.action); |
| 599 | resultData.putInt(BUNDLE_USER_ID, resp.userToSwitchOrCreate.userId); |
| 600 | break; |
| 601 | case InitialUserInfoResponseAction.CREATE: |
| 602 | resultData = new Bundle(); |
| 603 | resultData.putInt(BUNDLE_INITIAL_INFO_ACTION, resp.action); |
| 604 | resultData.putInt(BUNDLE_USER_FLAGS, resp.userToSwitchOrCreate.flags); |
| 605 | resultData.putString(BUNDLE_USER_NAME, resp.userNameToCreate); |
| 606 | break; |
| 607 | case InitialUserInfoResponseAction.DEFAULT: |
| 608 | resultData = new Bundle(); |
| 609 | resultData.putInt(BUNDLE_INITIAL_INFO_ACTION, resp.action); |
| 610 | break; |
| 611 | default: |
| 612 | // That's ok, it will be the same as DEFAULT... |
| 613 | Log.w(TAG_USER, "invalid response action on " + resp); |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 614 | } |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 615 | } else { |
| 616 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_RESP, status); |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 617 | } |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 618 | sendResult(receiver, status, resultData); |
Felipe Leme | c6e3c2a | 2020-02-19 16:53:57 -0800 | [diff] [blame] | 619 | }); |
| 620 | } |
| 621 | |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 622 | /** |
felipeal | 61ce373 | 2020-04-03 11:01:00 -0700 | [diff] [blame] | 623 | * Gets the initial foreground user after the device boots or resumes from suspension. |
| 624 | * |
| 625 | * <p>When the OEM supports the User HAL, the initial user won't be available until the HAL |
| 626 | * returns the initial value to {@code CarService} - if HAL takes too long or times out, this |
| 627 | * method returns {@code null}. |
| 628 | * |
| 629 | * <p>If the HAL eventually times out, {@code CarService} will fallback to its default behavior |
| 630 | * (like switching to the last active user), and this method will return the result of such |
| 631 | * operation. |
| 632 | * |
| 633 | * <p>Notice that if {@code CarService} crashes, subsequent calls to this method will return |
| 634 | * {@code null}. |
| 635 | * |
| 636 | * @hide |
| 637 | */ |
| 638 | @Nullable |
| 639 | public UserInfo getInitialUser() { |
| 640 | checkInteractAcrossUsersPermission("getInitialUser"); |
| 641 | synchronized (mLockUser) { |
| 642 | return mInitialUser; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // TODO(b/150413515): temporary method called by ICarImpl.setInitialUser(int userId), as for |
| 647 | // some reason passing the whole UserInfo through a raw binder transaction is not working. |
| 648 | /** |
| 649 | * Sets the initial foreground user after the device boots or resumes from suspension. |
| 650 | */ |
| 651 | public void setInitialUser(@UserIdInt int userId) { |
| 652 | UserInfo initialUser = userId == UserHandle.USER_NULL ? null |
| 653 | : mUserManager.getUserInfo(userId); |
| 654 | setInitialUser(initialUser); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Sets the initial foreground user after the device boots or resumes from suspension. |
| 659 | */ |
| 660 | public void setInitialUser(@Nullable UserInfo user) { |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 661 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SET_INITIAL_USER, |
| 662 | user == null ? UserHandle.USER_NULL : user.id); |
felipeal | 61ce373 | 2020-04-03 11:01:00 -0700 | [diff] [blame] | 663 | synchronized (mLockUser) { |
| 664 | mInitialUser = user; |
| 665 | } |
| 666 | if (user == null) { |
| 667 | // This mean InitialUserSetter failed and could not fallback, so the initial user was |
| 668 | // not switched (and most likely is SYSTEM_USER). |
| 669 | // TODO(b/153104378): should we set it to ActivityManager.getCurrentUser() instead? |
| 670 | Log.wtf(TAG_USER, "Initial user set to null"); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /** |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 675 | * Calls the User HAL to get the initial user info. |
| 676 | * |
| 677 | * @param requestType type as defined by {@code InitialUserInfoRequestType}. |
| 678 | * @param callback callback to receive the results. |
| 679 | */ |
| 680 | public void getInitialUserInfo(int requestType, |
| 681 | HalCallback<InitialUserInfoResponse> callback) { |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 682 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_INITIAL_USER_INFO_REQ, requestType, |
| 683 | mHalTimeoutMs); |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 684 | Objects.requireNonNull(callback, "callback cannot be null"); |
felipeal | 0ec1ff0 | 2020-03-27 17:18:28 -0700 | [diff] [blame] | 685 | checkManageUsersPermission("getInitialUserInfo"); |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 686 | UsersInfo usersInfo = getUsersInfo(); |
| 687 | mHal.getInitialUserInfo(requestType, mHalTimeoutMs, usersInfo, callback); |
| 688 | } |
| 689 | |
| 690 | /** |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 691 | * Calls the {@link UserHalService} and {@link IActivityManager} for user switch. |
| 692 | * |
| 693 | * <p> |
Mayank Garg | b08f677 | 2020-05-01 18:06:48 -0700 | [diff] [blame] | 694 | * When everything works well, the workflow is: |
| 695 | * <ol> |
| 696 | * <li> {@link UserHalService} is called for HAL user switch with ANDROID_SWITCH request |
| 697 | * type, current user id, target user id, and a callback. |
| 698 | * <li> HAL called back with SUCCESS. |
| 699 | * <li> {@link IActivityManager} is called for Android user switch. |
| 700 | * <li> Receiver would receive {@code STATUS_SUCCESSFUL}. |
| 701 | * <li> Once user is unlocked, {@link UserHalService} is again called with ANDROID_POST_SWITCH |
| 702 | * request type, current user id, and target user id. In this case, the current and target |
| 703 | * user IDs would be same. |
| 704 | * <ol/> |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 705 | * |
| 706 | * <p> |
Mayank Garg | b08f677 | 2020-05-01 18:06:48 -0700 | [diff] [blame] | 707 | * Corner cases: |
| 708 | * <ul> |
| 709 | * <li> If target user is already the current user, no user switch is performed and receiver |
| 710 | * would receive {@code STATUS_ALREADY_REQUESTED_USER} right away. |
| 711 | * <li> If HAL user switch call fails, no Android user switch. Receiver would receive |
| 712 | * {@code STATUS_HAL_INTERNAL_FAILURE}. |
| 713 | * <li> If HAL user switch call is successful, but android user switch call fails, |
| 714 | * {@link UserHalService} is again called with request type POST_SWITCH, current user id, and |
| 715 | * target user id, but in this case the current and target user IDs would be different. |
| 716 | * <li> If another user switch request for the same target user is received while previous |
| 717 | * request is in process, receiver would receive |
| 718 | * {@code STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO} for the new request right away. |
| 719 | * <li> If a user switch request is received while another user switch request for different |
| 720 | * target user is in process, the previous request would be abandoned and new request will be |
| 721 | * processed. No POST_SWITCH would be sent for the previous request. |
| 722 | * <ul/> |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 723 | * |
Mayank Garg | e19c292 | 2020-03-30 18:05:53 -0700 | [diff] [blame] | 724 | * @param targetUserId - target user Id |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 725 | * @param timeoutMs - timeout for HAL to wait |
| 726 | * @param receiver - receiver for the results |
| 727 | */ |
Mayank Garg | e19c292 | 2020-03-30 18:05:53 -0700 | [diff] [blame] | 728 | @Override |
| 729 | public void switchUser(@UserIdInt int targetUserId, int timeoutMs, |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 730 | @NonNull AndroidFuture<UserSwitchResult> receiver) { |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 731 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SWITCH_USER_REQ, targetUserId, timeoutMs); |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 732 | checkManageUsersPermission("switchUser"); |
Mayank Garg | e19c292 | 2020-03-30 18:05:53 -0700 | [diff] [blame] | 733 | Objects.requireNonNull(receiver); |
| 734 | UserInfo targetUser = mUserManager.getUserInfo(targetUserId); |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 735 | Preconditions.checkArgument(targetUser != null, "Target user doesn't exist"); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 736 | |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 737 | int currentUser = ActivityManager.getCurrentUser(); |
| 738 | if (currentUser == targetUserId) { |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 739 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 740 | Log.d(TAG_USER, "Current user is same as requested target user: " + targetUserId); |
| 741 | } |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 742 | int resultStatus = UserSwitchResult.STATUS_ALREADY_REQUESTED_USER; |
| 743 | sendResult(receiver, resultStatus); |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 747 | synchronized (mLockUser) { |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 748 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 749 | Log.d(TAG_USER, "switchUser(" + targetUserId + "): currentuser=" + currentUser |
| 750 | + ", mUserIdForUserSwitchInProcess=" + mUserIdForUserSwitchInProcess); |
| 751 | } |
| 752 | |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 753 | // If there is another request for the same target user, return another request in |
| 754 | // process, else {@link mUserIdForUserSwitchInProcess} is updated and {@link |
| 755 | // mRequestIdForUserSwitchInProcess} is reset. It is possible that there may be another |
| 756 | // user switch request in process for different target user, but that request is now |
| 757 | // ignored. |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 758 | if (mUserIdForUserSwitchInProcess == targetUserId) { |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 759 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 760 | Log.d(TAG_USER, |
| 761 | "Another user switch request in process for the requested target user: " |
| 762 | + targetUserId); |
| 763 | } |
| 764 | |
| 765 | int resultStatus = UserSwitchResult.STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO; |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 766 | sendResult(receiver, resultStatus); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 767 | return; |
| 768 | } |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 769 | else { |
| 770 | mUserIdForUserSwitchInProcess = targetUserId; |
| 771 | mRequestIdForUserSwitchInProcess = 0; |
| 772 | } |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 775 | UsersInfo usersInfo = getUsersInfo(); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 776 | android.hardware.automotive.vehicle.V2_0.UserInfo halTargetUser = |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 777 | new android.hardware.automotive.vehicle.V2_0.UserInfo(); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 778 | halTargetUser.userId = targetUser.id; |
| 779 | halTargetUser.flags = UserHalHelper.convertFlags(targetUser); |
| 780 | mHal.switchUser(halTargetUser, timeoutMs, usersInfo, (status, resp) -> { |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 781 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 782 | Log.d(TAG, "switch response: status=" |
| 783 | + UserHalHelper.halCallbackStatusToString(status) + ", resp=" + resp); |
| 784 | } |
| 785 | |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 786 | int resultStatus = UserSwitchResult.STATUS_HAL_INTERNAL_FAILURE; |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 787 | |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 788 | synchronized (mLockUser) { |
| 789 | if (status != HalCallback.STATUS_OK) { |
| 790 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SWITCH_USER_RESP, status); |
| 791 | Log.w(TAG, "invalid callback status (" |
| 792 | + UserHalHelper.halCallbackStatusToString(status) + ") for response " |
| 793 | + resp); |
| 794 | sendResult(receiver, resultStatus); |
| 795 | mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
| 796 | return; |
| 797 | } |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 798 | |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 799 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_SWITCH_USER_RESP, status, resp.status, |
| 800 | resp.errorMessage); |
| 801 | |
| 802 | if (mUserIdForUserSwitchInProcess != targetUserId) { |
| 803 | // Another user switch request received while HAL responded. No need to process |
| 804 | // this request further |
| 805 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 806 | Log.d(TAG_USER, "Another user switch received while HAL responsed. Request " |
| 807 | + "abondoned for : " + targetUserId + ". Current user in process: " |
| 808 | + mUserIdForUserSwitchInProcess); |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 809 | } |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 810 | resultStatus = |
| 811 | UserSwitchResult.STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST; |
| 812 | sendResult(receiver, resultStatus); |
| 813 | mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | switch (resp.status) { |
| 818 | case SwitchUserStatus.SUCCESS: |
| 819 | boolean switched; |
| 820 | try { |
| 821 | switched = mAm.switchUser(targetUserId); |
| 822 | if (switched) { |
| 823 | resultStatus = UserSwitchResult.STATUS_SUCCESSFUL; |
| 824 | mRequestIdForUserSwitchInProcess = resp.requestId; |
| 825 | } else { |
| 826 | resultStatus = UserSwitchResult.STATUS_ANDROID_FAILURE; |
| 827 | postSwitchHalResponse(resp.requestId, targetUserId); |
| 828 | } |
| 829 | } catch (RemoteException e) { |
| 830 | // ignore |
| 831 | Log.w(TAG_USER, |
| 832 | "error while switching user " + targetUser.toFullString(), e); |
| 833 | } |
| 834 | break; |
| 835 | case SwitchUserStatus.FAILURE: |
| 836 | // HAL failed to switch user |
| 837 | resultStatus = UserSwitchResult.STATUS_HAL_FAILURE; |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | if (mRequestIdForUserSwitchInProcess == 0) { |
| 842 | mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
| 843 | } |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 844 | } |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 845 | sendResult(receiver, resultStatus, resp.errorMessage); |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 846 | }); |
| 847 | } |
| 848 | |
felipeal | 5e3ede4 | 2020-04-23 18:04:07 -0700 | [diff] [blame] | 849 | @Override |
| 850 | public GetUserIdentificationAssociationResponse getUserIdentificationAssociation(int[] types) { |
| 851 | Preconditions.checkArgument(!ArrayUtils.isEmpty(types), "must have at least one type"); |
| 852 | checkManageUsersPermission("getUserIdentificationAssociation"); |
| 853 | |
| 854 | int uid = getCallingUid(); |
| 855 | int userId = UserHandle.getUserId(uid); |
| 856 | EventLog.writeEvent(EventLogTags.CAR_USER_MGR_GET_USER_AUTH_REQ, uid, userId); |
| 857 | |
| 858 | UserIdentificationGetRequest request = new UserIdentificationGetRequest(); |
| 859 | request.userInfo.userId = userId; |
| 860 | request.userInfo.flags = getHalUserInfoFlags(userId); |
| 861 | |
| 862 | request.numberAssociationTypes = types.length; |
| 863 | for (int i = 0; i < types.length; i++) { |
| 864 | request.associationTypes.add(types[i]); |
| 865 | } |
| 866 | |
| 867 | UserIdentificationResponse halResponse = mHal.getUserAssociation(request); |
| 868 | if (halResponse == null) { |
| 869 | Log.w(TAG, "getUserIdentificationAssociation(): HAL returned null for " |
| 870 | + Arrays.toString(types)); |
| 871 | return null; |
| 872 | } |
| 873 | |
| 874 | int[] values = new int[halResponse.associations.size()]; |
| 875 | for (int i = 0; i < values.length; i++) { |
| 876 | values[i] = halResponse.associations.get(i).value; |
| 877 | } |
| 878 | EventLog.writeEvent(EventLogTags.CAR_USER_MGR_GET_USER_AUTH_RESP, values.length); |
| 879 | |
| 880 | return new GetUserIdentificationAssociationResponse(halResponse.errorMessage, values); |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * Gets the User HAL flags for the given user. |
| 885 | * |
| 886 | * @throws IllegalArgumentException if the user does not exist. |
| 887 | */ |
| 888 | private int getHalUserInfoFlags(@UserIdInt int userId) { |
| 889 | UserInfo user = mUserManager.getUserInfo(userId); |
| 890 | Preconditions.checkArgument(user != null, "no user for id %d", userId); |
| 891 | return UserHalHelper.convertFlags(user); |
| 892 | } |
| 893 | |
Mayank Garg | 0e23914 | 2020-04-14 19:16:31 -0700 | [diff] [blame] | 894 | private void sendResult(@NonNull IResultReceiver receiver, int resultCode, |
| 895 | @Nullable Bundle resultData) { |
| 896 | try { |
| 897 | receiver.send(resultCode, resultData); |
| 898 | } catch (RemoteException e) { |
| 899 | // ignore |
| 900 | Log.w(TAG_USER, "error while sending results", e); |
| 901 | } |
| 902 | } |
| 903 | |
felipeal | e5bf032 | 2020-04-16 15:10:57 -0700 | [diff] [blame] | 904 | private void sendResult(@NonNull AndroidFuture<UserSwitchResult> receiver, |
| 905 | @UserSwitchResult.Status int status) { |
| 906 | sendResult(receiver, status, /* errorMessage= */ null); |
| 907 | } |
| 908 | |
| 909 | private void sendResult(@NonNull AndroidFuture<UserSwitchResult> receiver, |
| 910 | @UserSwitchResult.Status int status, @Nullable String errorMessage) { |
| 911 | receiver.complete(new UserSwitchResult(status, errorMessage)); |
| 912 | } |
| 913 | |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 914 | private void postSwitchHalResponse(int requestId, @UserIdInt int targetUserId) { |
| 915 | UserInfo targetUser = mUserManager.getUserInfo(targetUserId); |
| 916 | UsersInfo usersInfo = getUsersInfo(); |
| 917 | android.hardware.automotive.vehicle.V2_0.UserInfo halTargetUser = |
| 918 | new android.hardware.automotive.vehicle.V2_0.UserInfo(); |
| 919 | halTargetUser.userId = targetUser.id; |
| 920 | halTargetUser.flags = UserHalHelper.convertFlags(targetUser); |
felipeal | 312416a | 2020-04-14 12:28:24 -0700 | [diff] [blame] | 921 | EventLog.writeEvent(EventLogTags.CAR_USER_SVC_POST_SWITCH_USER_REQ, requestId, |
| 922 | targetUserId, usersInfo.currentUser.userId); |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 923 | mHal.postSwitchResponse(requestId, halTargetUser, usersInfo); |
| 924 | } |
| 925 | |
Mayank Garg | 59f2219 | 2020-03-27 00:51:45 -0700 | [diff] [blame] | 926 | /** |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 927 | * Checks if the User HAL is supported. |
| 928 | */ |
| 929 | public boolean isUserHalSupported() { |
| 930 | return mHal.isSupported(); |
| 931 | } |
| 932 | |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 933 | // TODO(b/144120654): use helper to generate UsersInfo |
| 934 | private UsersInfo getUsersInfo() { |
| 935 | UserInfo currentUser; |
| 936 | try { |
| 937 | currentUser = mAm.getCurrentUser(); |
| 938 | } catch (RemoteException e) { |
| 939 | // shouldn't happen |
| 940 | throw new IllegalStateException("Could not get current user: ", e); |
| 941 | } |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame^] | 942 | return getUsersInfo(currentUser); |
| 943 | } |
| 944 | |
| 945 | // TODO(b/144120654): use helper to generate UsersInfo |
| 946 | private UsersInfo getUsersInfo(@NonNull UserInfo currentUser) { |
Felipe Leme | abbf2da | 2020-02-24 18:25:29 -0800 | [diff] [blame] | 947 | List<UserInfo> existingUsers = mUserManager.getUsers(); |
| 948 | int size = existingUsers.size(); |
| 949 | |
| 950 | UsersInfo usersInfo = new UsersInfo(); |
| 951 | usersInfo.numberUsers = size; |
| 952 | usersInfo.currentUser.userId = currentUser.id; |
| 953 | usersInfo.currentUser.flags = UserHalHelper.convertFlags(currentUser); |
| 954 | |
| 955 | for (int i = 0; i < size; i++) { |
| 956 | UserInfo androidUser = existingUsers.get(i); |
| 957 | android.hardware.automotive.vehicle.V2_0.UserInfo halUser = |
| 958 | new android.hardware.automotive.vehicle.V2_0.UserInfo(); |
| 959 | halUser.userId = androidUser.id; |
| 960 | halUser.flags = UserHalHelper.convertFlags(androidUser); |
| 961 | usersInfo.existingUsers.add(halUser); |
| 962 | } |
| 963 | |
| 964 | return usersInfo; |
| 965 | } |
| 966 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 967 | /** Returns whether the given user is a system user. */ |
| 968 | private static boolean isSystemUser(@UserIdInt int userId) { |
| 969 | return userId == UserHandle.USER_SYSTEM; |
| 970 | } |
| 971 | |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 972 | private void updateDefaultUserRestriction() { |
| 973 | // We want to set restrictions on system and guest users only once. These are persisted |
| 974 | // onto disk, so it's sufficient to do it once + we minimize the number of disk writes. |
| 975 | if (Settings.Global.getInt(mContext.getContentResolver(), |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 976 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, /* default= */ 0) != 0) { |
| 977 | return; |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 978 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 979 | // Only apply the system user restrictions if the system user is headless. |
| 980 | if (UserManager.isHeadlessSystemUserMode()) { |
| 981 | setSystemUserRestrictions(); |
| 982 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 983 | Settings.Global.putInt(mContext.getContentResolver(), |
| 984 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, 1); |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 987 | private boolean isPersistentUser(@UserIdInt int userId) { |
Anthony Hugh | 9932a25 | 2019-06-12 16:19:56 -0700 | [diff] [blame] | 988 | return !mUserManager.getUserInfo(userId).isEphemeral(); |
| 989 | } |
| 990 | |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 991 | /** |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 992 | * Adds a new {@link UserLifecycleListener} to listen to user activity events. |
| 993 | */ |
| 994 | public void addUserLifecycleListener(@NonNull UserLifecycleListener listener) { |
| 995 | Objects.requireNonNull(listener, "listener cannot be null"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 996 | mHandler.post(() -> mUserLifecycleListeners.add(listener)); |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Removes previously added {@link UserLifecycleListener}. |
| 1001 | */ |
| 1002 | public void removeUserLifecycleListener(@NonNull UserLifecycleListener listener) { |
| 1003 | Objects.requireNonNull(listener, "listener cannot be null"); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1004 | mHandler.post(() -> mUserLifecycleListeners.remove(listener)); |
Antonio Kantek | c811475 | 2020-03-05 21:37:39 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1007 | /** Adds callback to listen to passenger activity events. */ |
| 1008 | public void addPassengerCallback(@NonNull PassengerCallback callback) { |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 1009 | Objects.requireNonNull(callback, "callback cannot be null"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1010 | mPassengerCallbacks.add(callback); |
| 1011 | } |
| 1012 | |
| 1013 | /** Removes previously added callback to listen passenger events. */ |
| 1014 | public void removePassengerCallback(@NonNull PassengerCallback callback) { |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 1015 | Objects.requireNonNull(callback, "callback cannot be null"); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1016 | mPassengerCallbacks.remove(callback); |
| 1017 | } |
| 1018 | |
| 1019 | /** Sets the implementation of ZoneUserBindingHelper. */ |
| 1020 | public void setZoneUserBindingHelper(@NonNull ZoneUserBindingHelper helper) { |
| 1021 | synchronized (mLockHelper) { |
| 1022 | mZoneUserBindingHelper = helper; |
| 1023 | } |
| 1024 | } |
| 1025 | |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 1026 | private void onUserUnlocked(@UserIdInt int userId) { |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1027 | ArrayList<Runnable> tasks = null; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1028 | synchronized (mLockUser) { |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1029 | sendPostSwitchToHalLocked(userId); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1030 | if (userId == UserHandle.USER_SYSTEM) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1031 | if (!mUser0Unlocked) { // user 0, unlocked, do this only once |
| 1032 | updateDefaultUserRestriction(); |
| 1033 | tasks = new ArrayList<>(mUser0UnlockTasks); |
| 1034 | mUser0UnlockTasks.clear(); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1035 | mUser0Unlocked = true; |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } else { // none user0 |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1038 | Integer user = userId; |
| 1039 | if (isPersistentUser(userId)) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1040 | // current foreground user should stay in top priority. |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 1041 | if (userId == ActivityManager.getCurrentUser()) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1042 | mBackgroundUsersToRestart.remove(user); |
| 1043 | mBackgroundUsersToRestart.add(0, user); |
| 1044 | } |
| 1045 | // -1 for user 0 |
| 1046 | if (mBackgroundUsersToRestart.size() > (mMaxRunningUsers - 1)) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1047 | int userToDrop = mBackgroundUsersToRestart.get( |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1048 | mBackgroundUsersToRestart.size() - 1); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1049 | Log.i(TAG_USER, "New user unlocked:" + userId |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1050 | + ", dropping least recently user from restart list:" + userToDrop); |
| 1051 | // Drop the least recently used user. |
| 1052 | mBackgroundUsersToRestart.remove(mBackgroundUsersToRestart.size() - 1); |
| 1053 | } |
| 1054 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | if (tasks != null && tasks.size() > 0) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1058 | Log.d(TAG_USER, "User0 unlocked, run queued tasks:" + tasks.size()); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1059 | for (Runnable r : tasks) { |
| 1060 | r.run(); |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1066 | * Starts all background users that were active in system. |
| 1067 | * |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1068 | * @return list of background users started successfully. |
| 1069 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1070 | @NonNull |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1071 | public ArrayList<Integer> startAllBackgroundUsers() { |
| 1072 | ArrayList<Integer> users; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1073 | synchronized (mLockUser) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1074 | users = new ArrayList<>(mBackgroundUsersToRestart); |
| 1075 | mBackgroundUsersRestartedHere.clear(); |
| 1076 | mBackgroundUsersRestartedHere.addAll(mBackgroundUsersToRestart); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1077 | } |
| 1078 | ArrayList<Integer> startedUsers = new ArrayList<>(); |
| 1079 | for (Integer user : users) { |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 1080 | if (user == ActivityManager.getCurrentUser()) { |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1081 | continue; |
| 1082 | } |
| 1083 | try { |
| 1084 | if (mAm.startUserInBackground(user)) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1085 | if (mUserManager.isUserUnlockingOrUnlocked(user)) { |
| 1086 | // already unlocked / unlocking. No need to unlock. |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1087 | startedUsers.add(user); |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1088 | } else if (mAm.unlockUser(user, null, null, null)) { |
| 1089 | startedUsers.add(user); |
| 1090 | } else { // started but cannot unlock |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1091 | 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] | 1092 | if (mUserManager.isUserRunning(user)) { |
| 1093 | // add to started list so that it can be stopped later. |
| 1094 | startedUsers.add(user); |
| 1095 | } |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | } catch (RemoteException e) { |
| 1099 | // ignore |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1100 | Log.w(TAG_USER, "error while starting user in background", e); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1103 | // Keep only users that were re-started in mBackgroundUsersRestartedHere |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1104 | synchronized (mLockUser) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1105 | ArrayList<Integer> usersToRemove = new ArrayList<>(); |
| 1106 | for (Integer user : mBackgroundUsersToRestart) { |
| 1107 | if (!startedUsers.contains(user)) { |
| 1108 | usersToRemove.add(user); |
| 1109 | } |
| 1110 | } |
| 1111 | mBackgroundUsersRestartedHere.removeAll(usersToRemove); |
| 1112 | } |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1113 | return startedUsers; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1117 | * Stops all background users that were active in system. |
| 1118 | * |
| 1119 | * @return whether stopping succeeds. |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1120 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1121 | public boolean stopBackgroundUser(@UserIdInt int userId) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1122 | if (userId == UserHandle.USER_SYSTEM) { |
| 1123 | return false; |
| 1124 | } |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 1125 | if (userId == ActivityManager.getCurrentUser()) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1126 | Log.i(TAG_USER, "stopBackgroundUser, already a FG user:" + userId); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1127 | return false; |
| 1128 | } |
| 1129 | try { |
Keun young Park | ed9e628 | 2019-09-19 17:38:26 -0700 | [diff] [blame] | 1130 | int r = mAm.stopUserWithDelayedLocking(userId, true, null); |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1131 | if (r == ActivityManager.USER_OP_SUCCESS) { |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1132 | synchronized (mLockUser) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1133 | Integer user = userId; |
| 1134 | mBackgroundUsersRestartedHere.remove(user); |
| 1135 | } |
| 1136 | } else if (r == ActivityManager.USER_OP_IS_CURRENT) { |
| 1137 | return false; |
| 1138 | } else { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1139 | Log.i(TAG_USER, "stopBackgroundUser failed, user:" + userId + " err:" + r); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1140 | return false; |
| 1141 | } |
| 1142 | } catch (RemoteException e) { |
| 1143 | // ignore |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1144 | Log.w(TAG_USER, "error while stopping user", e); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 1145 | } |
| 1146 | return true; |
| 1147 | } |
| 1148 | |
| 1149 | /** |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1150 | * Notifies all registered {@link UserLifecycleListener} with the event passed as argument. |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 1151 | */ |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 1152 | public void onUserLifecycleEvent(@UserLifecycleEventType int eventType, long timestampMs, |
| 1153 | @UserIdInt int fromUserId, @UserIdInt int toUserId) { |
| 1154 | int userId = toUserId; |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 1155 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1156 | // Handle special cases first... |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 1157 | if (eventType == CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING) { |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame^] | 1158 | onUserSwitching(fromUserId, toUserId); |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 1159 | } else if (eventType == CarUserManager.USER_LIFECYCLE_EVENT_TYPE_UNLOCKED) { |
| 1160 | onUserUnlocked(userId); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 1163 | // ...then notify listeners. |
Yan Zhu | e792152 | 2020-04-16 15:59:25 -0700 | [diff] [blame] | 1164 | UserLifecycleEvent event = new UserLifecycleEvent(eventType, fromUserId, userId); |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 1165 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1166 | mHandler.post(() -> { |
| 1167 | handleNotifyServiceUserLifecycleListeners(event); |
| 1168 | handleNotifyAppUserLifecycleListeners(event); |
| 1169 | }); |
felipeal | e8c5dce | 2020-04-15 11:27:06 -0700 | [diff] [blame] | 1170 | |
| 1171 | // Finally, update metrics. |
| 1172 | mUserMetrics.onEvent(eventType, timestampMs, fromUserId, toUserId); |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * Sets the first user unlocking metrics. |
| 1177 | */ |
| 1178 | public void onFirstUserUnlocked(@UserIdInt int userId, long timestampMs, long duration, |
| 1179 | int halResponseTime) { |
| 1180 | mUserMetrics.logFirstUnlockedUser(userId, timestampMs, duration, halResponseTime); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1183 | private void sendPostSwitchToHalLocked(@UserIdInt int userId) { |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 1184 | if (mUserIdForUserSwitchInProcess == UserHandle.USER_NULL |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1185 | || mUserIdForUserSwitchInProcess != userId |
| 1186 | || mRequestIdForUserSwitchInProcess == 0) { |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1187 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 1188 | Log.d(TAG_USER, "No user switch request Id. No android post switch sent."); |
| 1189 | } |
| 1190 | return; |
| 1191 | } |
felipeal | f736896 | 2020-04-16 12:55:19 -0700 | [diff] [blame] | 1192 | postSwitchHalResponse(mRequestIdForUserSwitchInProcess, mUserIdForUserSwitchInProcess); |
| 1193 | mUserIdForUserSwitchInProcess = UserHandle.USER_NULL; |
Mayank Garg | 1f20dcd | 2020-04-22 17:46:01 -0700 | [diff] [blame] | 1194 | mRequestIdForUserSwitchInProcess = 0; |
Mayank Garg | 7a114c8 | 2020-04-08 21:25:06 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1197 | private void handleNotifyAppUserLifecycleListeners(UserLifecycleEvent event) { |
| 1198 | int listenersSize = mAppLifecycleListeners.size(); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1199 | if (listenersSize == 0) { |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1200 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 1201 | Log.d(TAG_USER, "No app listener to be notified of " + event); |
| 1202 | } |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1203 | return; |
| 1204 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1205 | // Must use a different TimingsTraceLog because it's another thread |
| 1206 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 1207 | Log.d(TAG_USER, "Notifying " + listenersSize + " app listeners of " + event); |
| 1208 | } |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1209 | int userId = event.getUserId(); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1210 | TimingsTraceLog t = new TimingsTraceLog(TAG_USER, Trace.TRACE_TAG_SYSTEM_SERVER); |
| 1211 | t.traceBegin("notify-app-listeners-user-" + userId + "-event-" + event.getEventType()); |
| 1212 | for (int i = 0; i < listenersSize; i++) { |
| 1213 | int uid = mAppLifecycleListeners.keyAt(i); |
| 1214 | IResultReceiver listener = mAppLifecycleListeners.valueAt(i); |
| 1215 | Bundle data = new Bundle(); |
| 1216 | data.putInt(CarUserManager.BUNDLE_PARAM_ACTION, event.getEventType()); |
Yan Zhu | e792152 | 2020-04-16 15:59:25 -0700 | [diff] [blame] | 1217 | |
| 1218 | int fromUid = event.getPreviousUserId(); |
| 1219 | if (fromUid != UserHandle.USER_NULL) { |
| 1220 | data.putInt(CarUserManager.BUNDLE_PARAM_PREVIOUS_USER_ID, fromUid); |
| 1221 | } |
| 1222 | |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1223 | if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1224 | Log.d(TAG_USER, "Notifying listener for uid " + uid); |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1225 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1226 | try { |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1227 | t.traceBegin("notify-app-listener-" + uid); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1228 | listener.send(userId, data); |
| 1229 | } catch (RemoteException e) { |
| 1230 | Log.e(TAG_USER, "Error calling lifecycle listener", e); |
| 1231 | } finally { |
| 1232 | t.traceEnd(); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1233 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1234 | } |
| 1235 | t.traceEnd(); // notify-app-listeners-user-USERID-event-EVENT_TYPE |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1238 | private void handleNotifyServiceUserLifecycleListeners(UserLifecycleEvent event) { |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1239 | TimingsTraceLog t = new TimingsTraceLog(TAG_USER, Trace.TRACE_TAG_SYSTEM_SERVER); |
| 1240 | if (mUserLifecycleListeners.isEmpty()) { |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1241 | Log.w(TAG_USER, "Not notifying internal UserLifecycleListeners"); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1242 | return; |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1243 | } else if (Log.isLoggable(TAG_USER, Log.DEBUG)) { |
| 1244 | Log.d(TAG_USER, "Notifying " + mUserLifecycleListeners.size() + " service listeners of " |
| 1245 | + event); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1246 | } |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1247 | |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1248 | t.traceBegin("notify-listeners-user-" + event.getUserId() + "-event-" |
| 1249 | + event.getEventType()); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1250 | for (UserLifecycleListener listener : mUserLifecycleListeners) { |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1251 | String listenerName = FunctionalUtils.getLambdaName(listener); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1252 | try { |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1253 | t.traceBegin("notify-listener-" + listenerName); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1254 | listener.onEvent(event); |
| 1255 | } catch (RuntimeException e) { |
| 1256 | Log.e(TAG_USER, |
felipeal | 2a84d51 | 2020-04-06 18:52:15 -0700 | [diff] [blame] | 1257 | "Exception raised when invoking onEvent for " + listenerName, e); |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1258 | } finally { |
| 1259 | t.traceEnd(); |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1260 | } |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1261 | } |
Antonio Kantek | 7236a5b | 2020-04-06 19:53:55 -0700 | [diff] [blame] | 1262 | t.traceEnd(); // notify-listeners-user-USERID-event-EVENT_TYPE |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame^] | 1265 | private void onUserSwitching(@UserIdInt int fromUserId, @UserIdInt int toUserId) { |
| 1266 | Log.i(TAG_USER, "onSwitchUser() callback for user " + toUserId); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 1267 | TimingsTraceLog t = new TimingsTraceLog(TAG_USER, Trace.TRACE_TAG_SYSTEM_SERVER); |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame^] | 1268 | t.traceBegin("onUserSwitching-" + toUserId); |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 1269 | |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame^] | 1270 | // Switch HAL users if user switch is not requested by CarUserService |
| 1271 | notifyHalLegacySwitch(fromUserId, toUserId); |
| 1272 | |
| 1273 | if (!isSystemUser(toUserId)) { |
| 1274 | mCarUserManagerHelper.setLastActiveUser(toUserId); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1275 | } |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1276 | if (mLastPassengerId != UserHandle.USER_NULL) { |
| 1277 | stopPassengerInternal(mLastPassengerId, false); |
| 1278 | } |
| 1279 | if (mEnablePassengerSupport && isPassengerDisplayAvailable()) { |
| 1280 | setupPassengerUser(); |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame^] | 1281 | startFirstPassenger(toUserId); |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1282 | } |
felipeal | 98900c8 | 2020-04-09 09:05:02 -0700 | [diff] [blame] | 1283 | t.traceEnd(); |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Mayank Garg | e5de0f9 | 2020-04-23 21:38:38 -0700 | [diff] [blame^] | 1286 | private void notifyHalLegacySwitch(@UserIdInt int fromUserId, @UserIdInt int toUserId) { |
| 1287 | synchronized (mLockUser) { |
| 1288 | if (mUserIdForUserSwitchInProcess != UserHandle.USER_NULL) return; |
| 1289 | } |
| 1290 | |
| 1291 | // switch HAL user |
| 1292 | UserInfo targetUser = mUserManager.getUserInfo(toUserId); |
| 1293 | android.hardware.automotive.vehicle.V2_0.UserInfo halTargetUser = |
| 1294 | new android.hardware.automotive.vehicle.V2_0.UserInfo(); |
| 1295 | halTargetUser.userId = targetUser.id; |
| 1296 | halTargetUser.flags = UserHalHelper.convertFlags(targetUser); |
| 1297 | UserInfo currentUser = mUserManager.getUserInfo(fromUserId); |
| 1298 | UsersInfo usersInfo = getUsersInfo(currentUser); |
| 1299 | mHal.legacyUserSwitch(halTargetUser, usersInfo); |
| 1300 | } |
| 1301 | |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 1302 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1303 | * 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] | 1304 | * run inside this call. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1305 | * |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1306 | * @param r Runnable to run. |
| 1307 | */ |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1308 | public void runOnUser0Unlock(@NonNull Runnable r) { |
Daulet Zhanguzin | 4ded678 | 2020-01-03 15:47:10 +0000 | [diff] [blame] | 1309 | Objects.requireNonNull(r, "runnable cannot be null"); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1310 | boolean runNow = false; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1311 | synchronized (mLockUser) { |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 1312 | if (mUser0Unlocked) { |
| 1313 | runNow = true; |
| 1314 | } else { |
| 1315 | mUser0UnlockTasks.add(r); |
| 1316 | } |
| 1317 | } |
| 1318 | if (runNow) { |
| 1319 | r.run(); |
| 1320 | } |
| 1321 | } |
| 1322 | |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1323 | @VisibleForTesting |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1324 | @NonNull |
| 1325 | ArrayList<Integer> getBackgroundUsersToRestart() { |
| 1326 | ArrayList<Integer> backgroundUsersToRestart = null; |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1327 | synchronized (mLockUser) { |
Keun young Park | f3523cd | 2019-04-08 10:09:17 -0700 | [diff] [blame] | 1328 | backgroundUsersToRestart = new ArrayList<>(mBackgroundUsersToRestart); |
| 1329 | } |
| 1330 | return backgroundUsersToRestart; |
| 1331 | } |
| 1332 | |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 1333 | private void setSystemUserRestrictions() { |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 1334 | // Disable Location service for system user. |
| 1335 | LocationManager locationManager = |
| 1336 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
Anthony Hugh | 04ee04f | 2019-12-17 16:11:33 -0800 | [diff] [blame] | 1337 | locationManager.setLocationEnabledForUser( |
| 1338 | /* enabled= */ false, UserHandle.of(UserHandle.USER_SYSTEM)); |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 1339 | } |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1340 | |
| 1341 | /** |
| 1342 | * Creates a new user on the system, the created user would be granted admin role. |
| 1343 | * |
| 1344 | * @param name Name to be given to the newly created user. |
Eric Jeong | 3a793b0 | 2019-09-30 16:12:53 -0700 | [diff] [blame] | 1345 | * @return newly created admin user, {@code null} if it fails to create a user. |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1346 | */ |
| 1347 | @Nullable |
| 1348 | private UserInfo createNewAdminUser(String name) { |
| 1349 | if (!(mUserManager.isAdminUser() || mUserManager.isSystemUser())) { |
| 1350 | // Only admins or system user can create other privileged users. |
| 1351 | Log.e(TAG_USER, "Only admin users and system user can create other admins."); |
| 1352 | return null; |
| 1353 | } |
| 1354 | |
| 1355 | UserInfo user = mUserManager.createUser(name, UserInfo.FLAG_ADMIN); |
| 1356 | if (user == null) { |
| 1357 | // Couldn't create user, most likely because there are too many. |
| 1358 | Log.w(TAG_USER, "can't create admin user."); |
| 1359 | return null; |
| 1360 | } |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 1361 | assignDefaultIcon(user); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1362 | |
| 1363 | return user; |
| 1364 | } |
| 1365 | |
Anthony Hugh | 6fed1e9 | 2019-10-22 16:22:03 -0700 | [diff] [blame] | 1366 | /** |
| 1367 | * Assigns a default icon to a user according to the user's id. |
| 1368 | * |
| 1369 | * @param userInfo User whose avatar is set to default icon. |
| 1370 | * @return Bitmap of the user icon. |
| 1371 | */ |
| 1372 | private Bitmap assignDefaultIcon(UserInfo userInfo) { |
| 1373 | int idForIcon = userInfo.isGuest() ? UserHandle.USER_NULL : userInfo.id; |
| 1374 | Bitmap bitmap = UserIcons.convertToBitmap( |
| 1375 | UserIcons.getDefaultUserIcon(mContext.getResources(), idForIcon, false)); |
| 1376 | mUserManager.setUserIcon(userInfo.id, bitmap); |
| 1377 | return bitmap; |
| 1378 | } |
| 1379 | |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1380 | private interface UserFilter { |
| 1381 | boolean isEligibleUser(UserInfo user); |
| 1382 | } |
| 1383 | |
| 1384 | /** Returns all users who are matched by the given filter. */ |
| 1385 | private List<UserInfo> getUsers(UserFilter filter) { |
| 1386 | List<UserInfo> users = mUserManager.getUsers(/* excludeDying= */ true); |
| 1387 | |
| 1388 | for (Iterator<UserInfo> iterator = users.iterator(); iterator.hasNext(); ) { |
| 1389 | UserInfo user = iterator.next(); |
| 1390 | if (!filter.isEligibleUser(user)) { |
| 1391 | iterator.remove(); |
| 1392 | } |
| 1393 | } |
| 1394 | return users; |
| 1395 | } |
| 1396 | |
| 1397 | /** |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1398 | * Enforces that apps which have the |
| 1399 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} |
| 1400 | * can make certain calls to the CarUserManager. |
| 1401 | * |
| 1402 | * @param message used as message if SecurityException is thrown. |
| 1403 | * @throws SecurityException if the caller is not system or root. |
| 1404 | */ |
| 1405 | private static void checkManageUsersPermission(String message) { |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 1406 | checkAtLeastOnePermission(message, android.Manifest.permission.MANAGE_USERS); |
| 1407 | } |
| 1408 | |
| 1409 | private static void checkManageUsersOrDumpPermission(String message) { |
| 1410 | checkAtLeastOnePermission(message, |
| 1411 | android.Manifest.permission.MANAGE_USERS, |
| 1412 | android.Manifest.permission.DUMP); |
| 1413 | } |
| 1414 | |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 1415 | private void checkInteractAcrossUsersPermission(String message) { |
| 1416 | checkAtLeastOnePermission(message, android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 1417 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 1418 | } |
| 1419 | |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 1420 | private static void checkAtLeastOnePermission(String message, String...permissions) { |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1421 | int callingUid = Binder.getCallingUid(); |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 1422 | if (!hasAtLeastOnePermissionGranted(callingUid, permissions)) { |
| 1423 | throw new SecurityException("You need one of " + Arrays.toString(permissions) |
Felipe Leme | 5528ff7 | 2020-02-10 19:05:14 -0800 | [diff] [blame] | 1424 | + " to: " + message); |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | |
felipeal | 2d0483c | 2019-11-02 14:07:22 -0700 | [diff] [blame] | 1428 | private static boolean hasAtLeastOnePermissionGranted(int uid, String... permissions) { |
| 1429 | for (String permission : permissions) { |
| 1430 | if (ActivityManager.checkComponentPermission(permission, uid, /* owningUid = */-1, |
| 1431 | /* exported = */ true) |
| 1432 | == android.content.pm.PackageManager.PERMISSION_GRANTED) { |
| 1433 | return true; |
| 1434 | } |
| 1435 | } |
| 1436 | return false; |
Eric Jeong | 1545f3b | 2019-09-16 13:56:52 -0700 | [diff] [blame] | 1437 | } |
Eric Jeong | c91f945 | 2019-08-30 15:04:21 -0700 | [diff] [blame] | 1438 | |
| 1439 | private int getNumberOfManagedProfiles(@UserIdInt int userId) { |
| 1440 | List<UserInfo> users = mUserManager.getUsers(/* excludeDying= */true); |
| 1441 | // Count all users that are managed profiles of the given user. |
| 1442 | int managedProfilesCount = 0; |
| 1443 | for (UserInfo user : users) { |
| 1444 | if (user.isManagedProfile() && user.profileGroupId == userId) { |
| 1445 | managedProfilesCount++; |
| 1446 | } |
| 1447 | } |
| 1448 | return managedProfilesCount; |
| 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * Starts the first passenger of the given driver and assigns the passenger to the front |
| 1453 | * passenger zone. |
| 1454 | * |
| 1455 | * @param driverId User id of the driver. |
| 1456 | * @return whether it succeeds. |
| 1457 | */ |
| 1458 | private boolean startFirstPassenger(@UserIdInt int driverId) { |
| 1459 | int zoneId = getAvailablePassengerZone(); |
| 1460 | if (zoneId == OccupantZoneInfo.INVALID_ZONE_ID) { |
| 1461 | Log.w(TAG_USER, "passenger occupant zone is not found"); |
| 1462 | return false; |
| 1463 | } |
| 1464 | List<UserInfo> passengers = getPassengers(driverId); |
| 1465 | if (passengers.size() < 1) { |
| 1466 | Log.w(TAG_USER, "passenger is not found"); |
| 1467 | return false; |
| 1468 | } |
| 1469 | // Only one passenger is supported. If there are two or more passengers, the first passenger |
| 1470 | // is chosen. |
| 1471 | int passengerId = passengers.get(0).id; |
| 1472 | if (!startPassenger(passengerId, zoneId)) { |
| 1473 | Log.w(TAG_USER, "cannot start passenger " + passengerId); |
| 1474 | return false; |
| 1475 | } |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
| 1479 | private int getAvailablePassengerZone() { |
| 1480 | int[] occupantTypes = new int[] {CarOccupantZoneManager.OCCUPANT_TYPE_FRONT_PASSENGER, |
| 1481 | CarOccupantZoneManager.OCCUPANT_TYPE_REAR_PASSENGER}; |
| 1482 | for (int occupantType : occupantTypes) { |
| 1483 | int zoneId = getZoneId(occupantType); |
| 1484 | if (zoneId != OccupantZoneInfo.INVALID_ZONE_ID) { |
| 1485 | return zoneId; |
| 1486 | } |
| 1487 | } |
| 1488 | return OccupantZoneInfo.INVALID_ZONE_ID; |
| 1489 | } |
| 1490 | |
| 1491 | /** |
| 1492 | * Creates a new passenger user when there is no passenger user. |
| 1493 | */ |
| 1494 | private void setupPassengerUser() { |
| 1495 | int currentUser = ActivityManager.getCurrentUser(); |
| 1496 | int profileCount = getNumberOfManagedProfiles(currentUser); |
| 1497 | if (profileCount > 0) { |
| 1498 | Log.w(TAG_USER, "max profile of user" + currentUser |
| 1499 | + " is exceeded: current profile count is " + profileCount); |
| 1500 | return; |
| 1501 | } |
| 1502 | // TODO(b/140311342): Use resource string for the default passenger name. |
| 1503 | UserInfo passenger = createPassenger("Passenger", currentUser); |
| 1504 | if (passenger == null) { |
| 1505 | // Couldn't create user, most likely because there are too many. |
| 1506 | Log.w(TAG_USER, "cannot create a passenger user"); |
| 1507 | return; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | @NonNull |
| 1512 | private List<OccupantZoneInfo> getOccupantZones(@OccupantTypeEnum int occupantType) { |
| 1513 | ZoneUserBindingHelper helper = null; |
| 1514 | synchronized (mLockHelper) { |
| 1515 | if (mZoneUserBindingHelper == null) { |
| 1516 | Log.w(TAG_USER, "implementation is not delegated"); |
| 1517 | return new ArrayList<OccupantZoneInfo>(); |
| 1518 | } |
| 1519 | helper = mZoneUserBindingHelper; |
| 1520 | } |
| 1521 | return helper.getOccupantZones(occupantType); |
| 1522 | } |
| 1523 | |
| 1524 | private boolean assignUserToOccupantZone(@UserIdInt int userId, int zoneId) { |
| 1525 | ZoneUserBindingHelper helper = null; |
| 1526 | synchronized (mLockHelper) { |
| 1527 | if (mZoneUserBindingHelper == null) { |
| 1528 | Log.w(TAG_USER, "implementation is not delegated"); |
| 1529 | return false; |
| 1530 | } |
| 1531 | helper = mZoneUserBindingHelper; |
| 1532 | } |
| 1533 | return helper.assignUserToOccupantZone(userId, zoneId); |
| 1534 | } |
| 1535 | |
| 1536 | private boolean unassignUserFromOccupantZone(@UserIdInt int userId) { |
| 1537 | ZoneUserBindingHelper helper = null; |
| 1538 | synchronized (mLockHelper) { |
| 1539 | if (mZoneUserBindingHelper == null) { |
| 1540 | Log.w(TAG_USER, "implementation is not delegated"); |
| 1541 | return false; |
| 1542 | } |
| 1543 | helper = mZoneUserBindingHelper; |
| 1544 | } |
| 1545 | return helper.unassignUserFromOccupantZone(userId); |
| 1546 | } |
| 1547 | |
| 1548 | private boolean isPassengerDisplayAvailable() { |
| 1549 | ZoneUserBindingHelper helper = null; |
| 1550 | synchronized (mLockHelper) { |
| 1551 | if (mZoneUserBindingHelper == null) { |
| 1552 | Log.w(TAG_USER, "implementation is not delegated"); |
| 1553 | return false; |
| 1554 | } |
| 1555 | helper = mZoneUserBindingHelper; |
| 1556 | } |
| 1557 | return helper.isPassengerDisplayAvailable(); |
| 1558 | } |
| 1559 | |
| 1560 | /** |
| 1561 | * Gets the zone id of the given occupant type. If there are two or more zones, the first found |
| 1562 | * zone is returned. |
| 1563 | * |
| 1564 | * @param occupantType The type of an occupant. |
| 1565 | * @return The zone id of the given occupant type. {@link OccupantZoneInfo.INVALID_ZONE_ID}, |
| 1566 | * if not found. |
| 1567 | */ |
| 1568 | private int getZoneId(@OccupantTypeEnum int occupantType) { |
| 1569 | List<OccupantZoneInfo> zoneInfos = getOccupantZones(occupantType); |
| 1570 | return (zoneInfos.size() > 0) ? zoneInfos.get(0).zoneId : OccupantZoneInfo.INVALID_ZONE_ID; |
| 1571 | } |
Antonio Kantek | 4cf199d | 2020-03-27 15:56:13 -0700 | [diff] [blame] | 1572 | } |