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