1 /* 2 * Copyright (C) 2020 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 android.car.user; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.RequiresPermission; 22 import android.annotation.UserIdInt; 23 import android.car.Car; 24 import android.car.CarManagerBase; 25 import android.car.IExperimentalCarUserService; 26 import android.car.annotation.ExperimentalFeature; 27 import android.car.builtin.os.UserManagerHelper; 28 import android.car.util.concurrent.AndroidFuture; 29 import android.os.IBinder; 30 import android.os.RemoteException; 31 import android.os.UserHandle; 32 import android.util.Slog; 33 34 import com.android.internal.annotations.VisibleForTesting; 35 36 import java.util.ArrayList; 37 import java.util.Collections; 38 import java.util.List; 39 40 /** 41 * Temporary class containing {@link CarUserManager} features that are not ready yet. 42 * 43 * <p>New instances are created through {@link #from(CarUserManager)}. 44 * 45 * @hide 46 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 47 * supported. It will be marked {@code @removed} in the next major release and hard removed in the 48 * release after that. 49 */ 50 @ExperimentalFeature 51 @Deprecated 52 public final class ExperimentalCarUserManager extends CarManagerBase { 53 54 /** @hide 55 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 56 * supported. 57 */ 58 @Deprecated 59 public static final String TAG = ExperimentalCarUserManager.class.getSimpleName(); 60 61 /** 62 * User id representing invalid user. 63 */ 64 private static final int INVALID_USER_ID = UserManagerHelper.USER_NULL; 65 66 private final IExperimentalCarUserService mService; 67 68 /** 69 * @hide 70 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 71 * supported. It will be marked {@code @removed} in the next major release and hard removed in 72 * the release after that. 73 */ 74 @Deprecated ExperimentalCarUserManager(@onNull Car car, @NonNull IBinder service)75 public ExperimentalCarUserManager(@NonNull Car car, @NonNull IBinder service) { 76 this(car, IExperimentalCarUserService.Stub.asInterface(service)); 77 } 78 79 @VisibleForTesting ExperimentalCarUserManager( @onNull Car car, @NonNull IExperimentalCarUserService service)80 public ExperimentalCarUserManager( 81 @NonNull Car car, @NonNull IExperimentalCarUserService service) { 82 super(car); 83 84 mService = service; 85 } 86 87 /** 88 * Creates a driver who is a regular user and is allowed to login to the driving occupant zone. 89 * 90 * @param name The name of the driver to be created. 91 * @param admin Whether the created driver will be an admin. 92 * @return an {@link AndroidFuture} that can be used to track operation's completion and 93 * retrieve its result (if any). 94 * 95 * @hide 96 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 97 * supported. 98 */ 99 @Deprecated 100 @RequiresPermission(android.Manifest.permission.MANAGE_USERS) createDriver(@onNull String name, boolean admin)101 public AndroidFuture<UserCreationResult> createDriver(@NonNull String name, boolean admin) { 102 try { 103 return mService.createDriver(name, admin); 104 } catch (RemoteException e) { 105 AndroidFuture<UserCreationResult> future = new AndroidFuture<>(); 106 future.complete(new UserCreationResult(UserCreationResult.STATUS_HAL_INTERNAL_FAILURE)); 107 handleRemoteExceptionFromCarService(e); 108 return future; 109 } 110 } 111 112 /** 113 * Creates a passenger who is a profile of the given driver. 114 * 115 * @param name The name of the passenger to be created. 116 * @param driverId User id of the driver under whom a passenger is created. 117 * @return user id of the created passenger, or {@code INVALID_USER_ID} if the passenger 118 * could not be created. 119 * 120 * @hide 121 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 122 * supported. 123 */ 124 @Deprecated 125 @RequiresPermission(android.Manifest.permission.MANAGE_USERS) 126 @Nullable createPassenger(@onNull String name, @UserIdInt int driverId)127 public int createPassenger(@NonNull String name, @UserIdInt int driverId) { 128 try { 129 UserHandle ui = mService.createPassenger(name, driverId); 130 return ui != null ? ui.getIdentifier() : INVALID_USER_ID; 131 } catch (RemoteException e) { 132 return handleRemoteExceptionFromCarService(e, null); 133 } 134 } 135 136 /** 137 * Switches a driver to the given user. 138 * 139 * @param driverId User id of the driver to switch to. 140 * @return an {@link AndroidFuture} that can be used to track operation's completion and 141 * retrieve its result (if any). 142 * 143 * @hide 144 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 145 * supported. 146 */ 147 @Deprecated 148 @RequiresPermission(android.Manifest.permission.MANAGE_USERS) switchDriver(@serIdInt int driverId)149 public AndroidFuture<UserSwitchResult> switchDriver(@UserIdInt int driverId) { 150 try { 151 AndroidFuture<UserSwitchResult> future = new AndroidFuture<>() { 152 @Override 153 protected void onCompleted(UserSwitchResult result, Throwable err) { 154 if (result == null) { 155 Slog.w(TAG, "switchDriver(" + driverId + ") failed: " + err); 156 } 157 super.onCompleted(result, err); 158 } 159 }; 160 mService.switchDriver(driverId, future); 161 return future; 162 } catch (RemoteException e) { 163 AndroidFuture<UserSwitchResult> future = new AndroidFuture<>(); 164 future.complete( 165 new UserSwitchResult(UserSwitchResult.STATUS_HAL_INTERNAL_FAILURE, null)); 166 handleRemoteExceptionFromCarService(e); 167 return future; 168 } 169 } 170 171 /** 172 * Returns all drivers who can occupy the driving zone. Guest users are included in the list. 173 * 174 * @return the list of user ids who can be a driver on the device. 175 * 176 * @hide 177 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 178 * supported. 179 */ 180 @Deprecated 181 @RequiresPermission(android.Manifest.permission.MANAGE_USERS) 182 @NonNull getAllDrivers()183 public List<Integer> getAllDrivers() { 184 try { 185 return getUserIdsFromUserHandles(mService.getAllDrivers()); 186 } catch (RemoteException e) { 187 return handleRemoteExceptionFromCarService(e, Collections.emptyList()); 188 } 189 } 190 191 /** 192 * Returns all passengers under the given driver. 193 * 194 * @param driverId User id of a driver. 195 * @return the list of user ids who are passengers under the given driver. 196 * 197 * @hide 198 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 199 * supported. 200 */ 201 @Deprecated 202 @RequiresPermission(android.Manifest.permission.MANAGE_USERS) 203 @NonNull getPassengers(@serIdInt int driverId)204 public List<Integer> getPassengers(@UserIdInt int driverId) { 205 try { 206 return getUserIdsFromUserHandles(mService.getPassengers(driverId)); 207 } catch (RemoteException e) { 208 return handleRemoteExceptionFromCarService(e, Collections.emptyList()); 209 } 210 } 211 212 /** 213 * Assigns the passenger to the zone and starts the user if it is not started yet. 214 * 215 * @param passengerId User id of the passenger to be started. 216 * @param zoneId Zone id to which the passenger is assigned. 217 * @return {@code true} if the user is successfully started or the user is already running. 218 * Otherwise, {@code false}. 219 * 220 * @hide 221 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 222 * supported. 223 */ 224 @Deprecated 225 @RequiresPermission(android.Manifest.permission.MANAGE_USERS) startPassenger(@serIdInt int passengerId, int zoneId)226 public boolean startPassenger(@UserIdInt int passengerId, int zoneId) { 227 try { 228 return mService.startPassenger(passengerId, zoneId); 229 } catch (RemoteException e) { 230 return handleRemoteExceptionFromCarService(e, false); 231 } 232 } 233 234 /** 235 * Stops the given passenger. 236 * 237 * @param passengerId User id of the passenger to be stopped. 238 * @return {@code true} if successfully stopped, or {@code false} if failed. 239 * 240 * @hide 241 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 242 * supported. 243 */ 244 @Deprecated 245 @RequiresPermission(android.Manifest.permission.MANAGE_USERS) stopPassenger(@serIdInt int passengerId)246 public boolean stopPassenger(@UserIdInt int passengerId) { 247 try { 248 return mService.stopPassenger(passengerId); 249 } catch (RemoteException e) { 250 return handleRemoteExceptionFromCarService(e, false); 251 } 252 } 253 254 /** @hide 255 * @deprecated {@link ExperimentalCarUserManager} was an experimental feature and is no longer 256 * supported. 257 */ 258 @Deprecated 259 @Override onCarDisconnected()260 public void onCarDisconnected() { 261 // nothing to do 262 } 263 getUserIdsFromUserHandles(List<UserHandle> infos)264 private List<Integer> getUserIdsFromUserHandles(List<UserHandle> infos) { 265 List<Integer> ids = new ArrayList<>(infos.size()); 266 for (UserHandle ui : infos) { 267 ids.add(ui.getIdentifier()); 268 } 269 return ids; 270 } 271 } 272