Implement CarDevicePolicyService#startUserInBackground,
which will be called by CarDevicePolicyManager.
Test: atest CarDevicePolicyServiceTest CarUserServiceTest
Bug: 181331178
Change-Id: Id6b2376acc592cfcb77d1e33c4dbc1d76ed9fa39
diff --git a/car-lib/src/android/car/admin/ICarDevicePolicyService.aidl b/car-lib/src/android/car/admin/ICarDevicePolicyService.aidl
index 3d9bec1..cefc47a 100644
--- a/car-lib/src/android/car/admin/ICarDevicePolicyService.aidl
+++ b/car-lib/src/android/car/admin/ICarDevicePolicyService.aidl
@@ -18,10 +18,12 @@
import android.car.user.UserRemovalResult;
import android.car.user.UserCreationResult;
+import android.car.user.UserStartResult;
import com.android.internal.infra.AndroidFuture;
/** @hide */
interface ICarDevicePolicyService {
void removeUser(int userId, in AndroidFuture<UserRemovalResult> receiver);
void createUser(String name, int flags, in AndroidFuture<UserCreationResult> receiver);
+ void startUserInBackground(int userId, in AndroidFuture<UserStartResult> receiver);
}
diff --git a/car-lib/src/android/car/user/UserStartResult.aidl b/car-lib/src/android/car/user/UserStartResult.aidl
new file mode 100644
index 0000000..d5333ec
--- /dev/null
+++ b/car-lib/src/android/car/user/UserStartResult.aidl
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.car.user;
+
+parcelable UserStartResult;
diff --git a/car-lib/src/android/car/user/UserStartResult.java b/car-lib/src/android/car/user/UserStartResult.java
new file mode 100644
index 0000000..d1a5d32
--- /dev/null
+++ b/car-lib/src/android/car/user/UserStartResult.java
@@ -0,0 +1,242 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.car.user;
+
+import android.os.Parcelable;
+
+import com.android.internal.util.DataClass;
+
+/**
+ * User start results.
+ *
+ * @hide
+ */
+@DataClass(
+ genToString = true,
+ genHiddenConstructor = true,
+ genHiddenConstDefs = true)
+public final class UserStartResult implements Parcelable, OperationResult {
+
+ /**
+ * When user start is successful.
+ *
+ * @hide
+ */
+ public static final int STATUS_SUCCESSFUL = CommonResults.STATUS_SUCCESSFUL;
+
+ /**
+ * When user start failed.
+ *
+ * @hide
+ */
+ public static final int STATUS_ANDROID_FAILURE = CommonResults.STATUS_ANDROID_FAILURE;
+
+ /**
+ * When user to start is same as current user.
+ *
+ * @hide
+ */
+ public static final int STATUS_SUCCESSFUL_USER_IS_CURRENT_USER =
+ CommonResults.LAST_COMMON_STATUS + 1;
+
+ /**
+ * When user to start does not exist.
+ *
+ * @hide
+ */
+ public static final int STATUS_USER_DOES_NOT_EXIST = CommonResults.LAST_COMMON_STATUS + 2;
+
+ /**
+ * Gets the user start result status.
+ *
+ * @return either {@link UserStartRsult#STATUS_SUCCESSFUL},
+ * {@link UserStartResult#STATUS_SUCCESSFUL_USER_IS_CURRENT_USER},
+ * {@link UserStartResult#STATUS_ANDROID_FAILURE},
+ * {@link UserStartResult#STATUS_USER_DOES_NOT_EXIST}, or
+ */
+ private final @Status int mStatus;
+
+ @Override
+ public boolean isSuccess() {
+ return mStatus == STATUS_SUCCESSFUL || mStatus == STATUS_SUCCESSFUL_USER_IS_CURRENT_USER;
+ }
+
+
+ // Code below generated by codegen v1.0.23.
+ //
+ // DO NOT MODIFY!
+ // CHECKSTYLE:OFF Generated code
+ //
+ // To regenerate run:
+ // $ codegen $ANDROID_BUILD_TOP/packages/services/Car/car-lib/src/android/car/user/UserStartResult.java
+ //
+ // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
+ // Settings > Editor > Code Style > Formatter Control
+ //@formatter:off
+
+
+ /** @hide */
+ @android.annotation.IntDef(prefix = "STATUS_", value = {
+ STATUS_SUCCESSFUL,
+ STATUS_ANDROID_FAILURE,
+ STATUS_SUCCESSFUL_USER_IS_CURRENT_USER,
+ STATUS_USER_DOES_NOT_EXIST
+ })
+ @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE)
+ @DataClass.Generated.Member
+ public @interface Status {}
+
+ /** @hide */
+ @DataClass.Generated.Member
+ public static String statusToString(@Status int value) {
+ switch (value) {
+ case STATUS_SUCCESSFUL:
+ return "STATUS_SUCCESSFUL";
+ case STATUS_ANDROID_FAILURE:
+ return "STATUS_ANDROID_FAILURE";
+ case STATUS_SUCCESSFUL_USER_IS_CURRENT_USER:
+ return "STATUS_SUCCESSFUL_USER_IS_CURRENT_USER";
+ case STATUS_USER_DOES_NOT_EXIST:
+ return "STATUS_USER_DOES_NOT_EXIST";
+ default: return Integer.toHexString(value);
+ }
+ }
+
+ /**
+ * Creates a new UserStartResult.
+ *
+ * @param status
+ * Gets the user start result status.
+ *
+ * @return either {@link UserStartRsult#STATUS_SUCCESSFUL},
+ * {@link UserStartResult#STATUS_ANDROID_FAILURE},
+ * {@link UserStartResult#STATUS_SUCCESSFUL_USER_IS_CURRENT_USER},
+ * {@link UserStartResult#STATUS_USER_DOES_NOT_EXIST}, or
+ * @hide
+ */
+ @DataClass.Generated.Member
+ public UserStartResult(
+ @Status int status) {
+ this.mStatus = status;
+
+ if (!(mStatus == STATUS_SUCCESSFUL)
+ && !(mStatus == STATUS_ANDROID_FAILURE)
+ && !(mStatus == STATUS_SUCCESSFUL_USER_IS_CURRENT_USER)
+ && !(mStatus == STATUS_USER_DOES_NOT_EXIST)) {
+ throw new java.lang.IllegalArgumentException(
+ "status was " + mStatus + " but must be one of: "
+ + "STATUS_SUCCESSFUL(" + STATUS_SUCCESSFUL + "), "
+ + "STATUS_ANDROID_FAILURE(" + STATUS_ANDROID_FAILURE + "), "
+ + "STATUS_SUCCESSFUL_USER_IS_CURRENT_USER(" + STATUS_SUCCESSFUL_USER_IS_CURRENT_USER + "), "
+ + "STATUS_USER_DOES_NOT_EXIST(" + STATUS_USER_DOES_NOT_EXIST + ")");
+ }
+
+
+ // onConstructed(); // You can define this method to get a callback
+ }
+
+ /**
+ * Gets the user start result status.
+ *
+ * @return either {@link UserStartRsult#STATUS_SUCCESSFUL},
+ * {@link UserStartResult#STATUS_ANDROID_FAILURE},
+ * {@link UserStartResult#STATUS_SUCCESSFUL_USER_IS_CURRENT_USER},
+ * {@link UserStartResult#STATUS_USER_DOES_NOT_EXIST}, or
+ */
+ @DataClass.Generated.Member
+ public @Status int getStatus() {
+ return mStatus;
+ }
+
+ @Override
+ @DataClass.Generated.Member
+ public String toString() {
+ // You can override field toString logic by defining methods like:
+ // String fieldNameToString() { ... }
+
+ return "UserStartResult { " +
+ "status = " + statusToString(mStatus) +
+ " }";
+ }
+
+ @Override
+ @DataClass.Generated.Member
+ public void writeToParcel(@android.annotation.NonNull android.os.Parcel dest, int flags) {
+ // You can override field parcelling by defining methods like:
+ // void parcelFieldName(Parcel dest, int flags) { ... }
+
+ dest.writeInt(mStatus);
+ }
+
+ @Override
+ @DataClass.Generated.Member
+ public int describeContents() { return 0; }
+
+ /** @hide */
+ @SuppressWarnings({"unchecked", "RedundantCast"})
+ @DataClass.Generated.Member
+ /* package-private */ UserStartResult(@android.annotation.NonNull android.os.Parcel in) {
+ // You can override field unparcelling by defining methods like:
+ // static FieldType unparcelFieldName(Parcel in) { ... }
+
+ int status = in.readInt();
+
+ this.mStatus = status;
+
+ if (!(mStatus == STATUS_SUCCESSFUL)
+ && !(mStatus == STATUS_ANDROID_FAILURE)
+ && !(mStatus == STATUS_SUCCESSFUL_USER_IS_CURRENT_USER)
+ && !(mStatus == STATUS_USER_DOES_NOT_EXIST)) {
+ throw new java.lang.IllegalArgumentException(
+ "status was " + mStatus + " but must be one of: "
+ + "STATUS_SUCCESSFUL(" + STATUS_SUCCESSFUL + "), "
+ + "STATUS_ANDROID_FAILURE(" + STATUS_ANDROID_FAILURE + "), "
+ + "STATUS_SUCCESSFUL_USER_IS_CURRENT_USER(" + STATUS_SUCCESSFUL_USER_IS_CURRENT_USER + "), "
+ + "STATUS_USER_DOES_NOT_EXIST(" + STATUS_USER_DOES_NOT_EXIST + ")");
+ }
+
+
+ // onConstructed(); // You can define this method to get a callback
+ }
+
+ @DataClass.Generated.Member
+ public static final @android.annotation.NonNull Parcelable.Creator<UserStartResult> CREATOR
+ = new Parcelable.Creator<UserStartResult>() {
+ @Override
+ public UserStartResult[] newArray(int size) {
+ return new UserStartResult[size];
+ }
+
+ @Override
+ public UserStartResult createFromParcel(@android.annotation.NonNull android.os.Parcel in) {
+ return new UserStartResult(in);
+ }
+ };
+
+ @DataClass.Generated(
+ time = 1618957103487L,
+ codegenVersion = "1.0.23",
+ sourceFile = "packages/services/Car/car-lib/src/android/car/user/UserStartResult.java",
+ inputSignatures = "public static final int STATUS_SUCCESSFUL\npublic static final int STATUS_ANDROID_FAILURE\npublic static final int STATUS_SUCCESSFUL_USER_IS_CURRENT_USER\npublic static final int STATUS_USER_DOES_NOT_EXIST\nprivate final @android.car.user.UserStartResult.Status int mStatus\npublic @java.lang.Override boolean isSuccess()\nclass UserStartResult extends java.lang.Object implements [android.os.Parcelable, android.car.user.OperationResult]\n@com.android.internal.util.DataClass(genToString=true, genHiddenConstructor=true, genHiddenConstDefs=true)")
+ @Deprecated
+ private void __metadata() {}
+
+
+ //@formatter:on
+ // End of generated code
+
+}
diff --git a/service/src/com/android/car/admin/CarDevicePolicyService.java b/service/src/com/android/car/admin/CarDevicePolicyService.java
index db7fe83..20601f8 100644
--- a/service/src/com/android/car/admin/CarDevicePolicyService.java
+++ b/service/src/com/android/car/admin/CarDevicePolicyService.java
@@ -25,6 +25,7 @@
import android.car.admin.ICarDevicePolicyService;
import android.car.user.UserCreationResult;
import android.car.user.UserRemovalResult;
+import android.car.user.UserStartResult;
import android.content.pm.UserInfo;
import android.os.UserManager;
import android.sysprop.CarProperties;
@@ -102,6 +103,12 @@
}
@Override
+ public void startUserInBackground(@UserIdInt int userId,
+ AndroidFuture<UserStartResult> receiver) {
+ mCarUserService.startUserInBackground(userId, receiver);
+ }
+
+ @Override
public void dump(@NonNull IndentingPrintWriter writer) {
checkHasDumpPermissionGranted("dump()");
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
index 685a7ce..0390866 100644
--- a/service/src/com/android/car/user/CarUserService.java
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -44,6 +44,7 @@
import android.car.user.UserCreationResult;
import android.car.user.UserIdentificationAssociationResponse;
import android.car.user.UserRemovalResult;
+import android.car.user.UserStartResult;
import android.car.user.UserSwitchResult;
import android.car.userlib.HalCallback;
import android.car.userlib.UserHalHelper;
@@ -110,6 +111,7 @@
import com.android.internal.util.FunctionalUtils;
import com.android.internal.util.Preconditions;
import com.android.internal.util.UserIcons;
+import com.android.server.utils.Slogf;
import java.io.PrintWriter;
import java.util.ArrayList;
@@ -964,10 +966,10 @@
sendUserSwitchResult(receiver, UserSwitchResult.STATUS_NOT_SWITCHABLE);
return;
}
- mHandler.post(()-> switchUserInternal(targetUser, timeoutMs, receiver));
+ mHandler.post(() -> handleSwitchUser(targetUser, timeoutMs, receiver));
}
- private void switchUserInternal(@NonNull UserInfo targetUser, int timeoutMs,
+ private void handleSwitchUser(@NonNull UserInfo targetUser, int timeoutMs,
@NonNull AndroidFuture<UserSwitchResult> receiver) {
int currentUser = ActivityManager.getCurrentUser();
int targetUserId = targetUser.id;
@@ -1129,10 +1131,10 @@
+ " can only remove itself");
}
}
- mHandler.post(()-> removeUserInternal(userId, hasCallerRestrictions, receiver));
+ mHandler.post(() -> handleRemoveUser(userId, hasCallerRestrictions, receiver));
}
- private void removeUserInternal(@UserIdInt int userId, boolean hasCallerRestrictions,
+ private void handleRemoveUser(@UserIdInt int userId, boolean hasCallerRestrictions,
AndroidFuture<UserRemovalResult> receiver) {
UserInfo userInfo = mUserManager.getUserInfo(userId);
if (userInfo == null) {
@@ -1301,12 +1303,12 @@
EventLog.writeEvent(EventLogTags.CAR_USER_SVC_CREATE_USER_REQ,
UserHelperLite.safeName(name), userType, flags, timeoutMs,
hasCallerRestrictions ? 1 : 0);
- mHandler.post(() -> createUserInternal(name, userType, flags, timeoutMs, receiver,
+ mHandler.post(() -> handleCreateUser(name, userType, flags, timeoutMs, receiver,
hasCallerRestrictions));
}
- private void createUserInternal(@Nullable String name, @NonNull String userType,
+ private void handleCreateUser(@Nullable String name, @NonNull String userType,
@UserInfoFlag int flags, int timeoutMs,
@NonNull AndroidFuture<UserCreationResult> receiver,
boolean hasCallerRestrictions) {
@@ -1840,6 +1842,53 @@
}
/**
+ * Starts the specified user in the background.
+ *
+ * @param userId user to start in background
+ * @param receiver to post results
+ */
+ public void startUserInBackground(@UserIdInt int userId,
+ @NonNull AndroidFuture<UserStartResult> receiver) {
+ mHandler.post(() -> handleStartUserInBackground(userId, receiver));
+ }
+
+ private void handleStartUserInBackground(@UserIdInt int userId,
+ @NonNull AndroidFuture<UserStartResult> receiver) {
+ // If the requested user is the current user, do nothing and return success.
+ if (ActivityManager.getCurrentUser() == userId) {
+ sendUserStartResult(UserStartResult.STATUS_SUCCESSFUL_USER_IS_CURRENT_USER, receiver);
+ return;
+ }
+ // If requested user does not exist, return error.
+ if (mUserManager.getUserInfo(userId) == null) {
+ Slogf.w(TAG, "User %d does not exist", userId);
+ sendUserStartResult(UserStartResult.STATUS_USER_DOES_NOT_EXIST, receiver);
+ return;
+ }
+
+ try {
+ if (!mAm.startUserInBackground(userId)) {
+ Slogf.w(TAG, "Failed to start user %d in background", userId);
+ sendUserStartResult(UserStartResult.STATUS_ANDROID_FAILURE, receiver);
+ return;
+ }
+ } catch (RemoteException e) {
+ Slogf.w(TAG, e, "Failed to start user %d in background", userId);
+ }
+
+ // TODO(b/181331178): We are not updating mBackgroundUsersToRestart or
+ // mBackgroundUsersRestartedHere, which were only used for the garage mode. Consider
+ // renaming them to make it more clear.
+ sendUserStartResult(UserStartResult.STATUS_SUCCESSFUL, receiver);
+ }
+
+ private void sendUserStartResult(@UserStartResult.Status int result,
+ @NonNull AndroidFuture<UserStartResult> receiver) {
+ // TODO(b/181331178): Add event log calls.
+ receiver.complete(new UserStartResult(result));
+ }
+
+ /**
* Starts all background users that were active in system.
*
* @return list of background users started successfully.
@@ -1891,7 +1940,7 @@
}
/**
- * Stops all background users that were active in system.
+ * Stops a background user.
*
* @return whether stopping succeeds.
*/
diff --git a/tests/carservice_unit_test/src/com/android/car/admin/CarDevicePolicyServiceTest.java b/tests/carservice_unit_test/src/com/android/car/admin/CarDevicePolicyServiceTest.java
index da1eb72..cb100b5 100644
--- a/tests/carservice_unit_test/src/com/android/car/admin/CarDevicePolicyServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/admin/CarDevicePolicyServiceTest.java
@@ -26,6 +26,7 @@
import android.car.test.mocks.AbstractExtendedMockitoTestCase;
import android.car.user.UserCreationResult;
import android.car.user.UserRemovalResult;
+import android.car.user.UserStartResult;
import android.content.pm.UserInfo;
import android.content.pm.UserInfo.UserInfoFlag;
import android.os.UserManager;
@@ -44,11 +45,11 @@
private CarDevicePolicyService mService;
- private AndroidFuture<UserRemovalResult> mUserRemovalResult =
- new AndroidFuture<UserRemovalResult>();
+ private AndroidFuture<UserRemovalResult> mUserRemovalResult = new AndroidFuture<>();
- private AndroidFuture<UserCreationResult> mUserCreationResult =
- new AndroidFuture<UserCreationResult>();
+ private AndroidFuture<UserCreationResult> mUserCreationResult = new AndroidFuture<>();
+
+ private AndroidFuture<UserStartResult> mUserStartResult = new AndroidFuture<>();
@Before
public void setFixtures() {
@@ -104,4 +105,11 @@
verify(mCarUserService).createUser(eq("name"), eq(userType), eq(flags),
/* timeoutMs= */ anyInt(), eq(mUserCreationResult));
}
+
+ @Test
+ public void testStartUserInBackground() {
+ mService.startUserInBackground(42, mUserStartResult);
+
+ verify(mCarUserService).startUserInBackground(42, mUserStartResult);
+ }
}
diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
index adafb57..6b9e2c2 100644
--- a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
@@ -72,6 +72,7 @@
import android.car.user.UserCreationResult;
import android.car.user.UserIdentificationAssociationResponse;
import android.car.user.UserRemovalResult;
+import android.car.user.UserStartResult;
import android.car.user.UserSwitchResult;
import android.car.userlib.HalCallback;
import android.car.userlib.HalCallback.HalCallbackStatus;
@@ -1775,6 +1776,69 @@
}
@Test
+ public void testStartUserInBackground_success() throws Exception {
+ int userId = 101;
+ UserInfo userInfo = new UserInfo(userId, "user1", NO_USER_INFO_FLAGS);
+ mockCurrentUser(mRegularUser);
+ mockUmGetUserInfo(mMockedUserManager, userInfo);
+ mockAmStartUserInBackground(userId, true);
+
+ AndroidFuture<UserStartResult> userStartResult = new AndroidFuture<>();
+ mCarUserService.startUserInBackground(userId, userStartResult);
+
+ assertThat(getResult(userStartResult).getStatus())
+ .isEqualTo(UserStartResult.STATUS_SUCCESSFUL);
+ assertThat(getResult(userStartResult).isSuccess()).isTrue();
+ }
+
+ @Test
+ public void testStartUserInBackground_fail() throws Exception {
+ int userId = 101;
+ UserInfo userInfo = new UserInfo(userId, "user1", NO_USER_INFO_FLAGS);
+ mockCurrentUser(mRegularUser);
+ mockUmGetUserInfo(mMockedUserManager, userInfo);
+ mockAmStartUserInBackground(userId, false);
+
+ AndroidFuture<UserStartResult> userStartResult = new AndroidFuture<>();
+ mCarUserService.startUserInBackground(userId, userStartResult);
+
+ assertThat(getResult(userStartResult).getStatus())
+ .isEqualTo(UserStartResult.STATUS_ANDROID_FAILURE);
+ assertThat(getResult(userStartResult).isSuccess()).isFalse();
+ }
+
+ @Test
+ public void testStartUserInBackground_currentUser() throws Exception {
+ int userId = 101;
+ UserInfo userInfo = new UserInfo(userId, "user1", NO_USER_INFO_FLAGS);
+ mockGetCurrentUser(userId);
+ mockUmGetUserInfo(mMockedUserManager, userInfo);
+ mockAmStartUserInBackground(userId, true);
+
+ AndroidFuture<UserStartResult> userStartResult = new AndroidFuture<>();
+ mCarUserService.startUserInBackground(userId, userStartResult);
+
+ assertThat(getResult(userStartResult).getStatus())
+ .isEqualTo(UserStartResult.STATUS_SUCCESSFUL_USER_IS_CURRENT_USER);
+ assertThat(getResult(userStartResult).isSuccess()).isTrue();
+ }
+
+ @Test
+ public void testStartUserInBackground_userDoesNotExist() throws Exception {
+ int userId = 101;
+ mockCurrentUser(mRegularUser);
+ when(mMockedUserManager.getUserInfo(userId)).thenReturn(null);
+ mockAmStartUserInBackground(userId, true);
+
+ AndroidFuture<UserStartResult> userStartResult = new AndroidFuture<>();
+ mCarUserService.startUserInBackground(userId, userStartResult);
+
+ assertThat(getResult(userStartResult).getStatus())
+ .isEqualTo(UserStartResult.STATUS_USER_DOES_NOT_EXIST);
+ assertThat(getResult(userStartResult).isSuccess()).isFalse();
+ }
+
+ @Test
public void testIsHalSupported() throws Exception {
when(mUserHal.isSupported()).thenReturn(true);
assertThat(mCarUserService.isUserHalSupported()).isTrue();
@@ -2286,6 +2350,11 @@
mockGetCurrentUser(user.id);
}
+ private void mockAmStartUserInBackground(@UserIdInt int userId, boolean result)
+ throws Exception {
+ when(mMockedIActivityManager.startUserInBackground(userId)).thenReturn(result);
+ }
+
private void mockAmSwitchUser(@NonNull UserInfo user, boolean result) throws Exception {
when(mMockedIActivityManager.switchUser(user.id)).thenReturn(result);
}