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