diff options
author | Ying Zheng <yizheng@google.com> | 2018-06-22 16:54:51 -0700 |
---|---|---|
committer | Ying Zheng <yizheng@google.com> | 2018-06-25 10:28:21 -0700 |
commit | cf20f44f515ca802baad7d73f86cbd829e1a2183 (patch) | |
tree | 41550771b70052f3539b496665800a8e956741b1 | |
parent | c3cb52abdbd7dd9b0fb70947242f452440db736c (diff) | |
download | Car-cf20f44f515ca802baad7d73f86cbd829e1a2183.tar.gz |
Update last active user.
Test: Unit test
Bug: 110156344
Change-Id: If8967f3acc434759e4f5b8bc25d6d913c1fa4ee6
3 files changed, 22 insertions, 18 deletions
diff --git a/car-lib/src/android/car/user/CarUserManagerHelper.java b/car-lib/src/android/car/user/CarUserManagerHelper.java index a0f4e15d7b..db28d21cba 100644 --- a/car-lib/src/android/car/user/CarUserManagerHelper.java +++ b/car-lib/src/android/car/user/CarUserManagerHelper.java @@ -34,7 +34,6 @@ import android.os.UserManager; import android.provider.Settings; import android.util.Log; -import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.UserIcons; import com.google.android.collect.Sets; @@ -109,9 +108,7 @@ public class CarUserManagerHelper { * Set default boot into user. * * @param userId default user id to boot into. - * @deprecated Setting default user is obsolete */ - @Deprecated public void setDefaultBootUser(int userId) { Settings.Global.putInt( mContext.getContentResolver(), @@ -136,9 +133,7 @@ public class CarUserManagerHelper { * Get user id for the default boot into user. * * @return user id of the default boot into user - * @deprecated Use {@link #getLastActiveUser()} instead. */ - @Deprecated public int getDefaultBootUser() { // Make user 10 the original default boot user. return Settings.Global.getInt( @@ -371,10 +366,7 @@ public class CarUserManagerHelper { * * @param userInfo User to check against system user. * @return {@code true} if is default user, {@code false} otherwise. - * - * @deprecated Default user is obsolete */ - @Deprecated public boolean isDefaultUser(UserInfo userInfo) { return userInfo.id == getDefaultBootUser(); } @@ -426,6 +418,17 @@ public class CarUserManagerHelper { } /** + * Checks if the given user is non-ephemeral. + * + * @param userId User to check + * @return {@code true} if given user is persistent user. + */ + public boolean isPersistentUser(int userId) { + UserInfo user = mUserManager.getUserInfo(userId); + return !user.isEphemeral(); + } + + /** * Returns whether this user can be removed from the system. * * @param userInfo User to be removed diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java index bc5f2025f7..cc30588284 100644 --- a/service/src/com/android/car/user/CarUserService.java +++ b/service/src/com/android/car/user/CarUserService.java @@ -23,6 +23,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.UserInfo; +import android.os.UserHandle; import android.os.UserManager; import android.util.Log; @@ -88,7 +89,7 @@ public class CarUserService extends BroadcastReceiver implements CarServiceBase Log.d(TAG, "onReceive " + intent); } - if (intent.getAction() == Intent.ACTION_LOCKED_BOOT_COMPLETED) { + if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) { if (mCarUserManagerHelper.getAllUsers().size() == 0) { // Disable adding accounts for user 0. mCarUserManagerHelper.setUserRestriction(mCarUserManagerHelper.getSystemUserInfo(), @@ -101,13 +102,13 @@ public class CarUserService extends BroadcastReceiver implements CarServiceBase } else { mCarUserManagerHelper.switchToUserId(mCarUserManagerHelper.getInitialUser()); } - } - if (intent.getAction() == Intent.ACTION_USER_SWITCHED) { - // Update last active user if foreground user is not ephemeral. - if (!mCarUserManagerHelper.isForegroundUserEphemeral()) { + } else if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) { + // Update last active user if the switched-to user is a persistent, non-system user. + int currentUser = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); + if (currentUser > UserHandle.USER_SYSTEM + && mCarUserManagerHelper.isPersistentUser(currentUser)) { mCarUserManagerHelper.setLastActiveUser( - mCarUserManagerHelper.getCurrentForegroundUserId(), - /* skipGlobalSettings= */ false); + currentUser, /* skipGlobalSetting= */ false); } } } 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 132a7ba9f5..03a692f43b 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 @@ -181,10 +181,10 @@ public class CarUserServiceTest { int lastActiveUserId = 11; doReturn(false).when(mCarUserManagerHelper).isForegroundUserEphemeral(); - doReturn(lastActiveUserId).when(mCarUserManagerHelper).getCurrentForegroundUserId(); - mCarUserService.onReceive(mMockContext, - new Intent(Intent.ACTION_USER_SWITCHED)); + Intent intent = new Intent(Intent.ACTION_USER_SWITCHED); + intent.putExtra(Intent.EXTRA_USER_HANDLE, lastActiveUserId); + mCarUserService.onReceive(mMockContext, intent); verify(mCarUserManagerHelper).setLastActiveUser( lastActiveUserId, /* skipGlobalSetting= */ false); |