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