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