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