Merge "Add methods to user helper" into rvc-qpr-dev
diff --git a/tests/carservice_unit_test/src/android/car/userlib/UserHelperTest.java b/tests/carservice_unit_test/src/android/car/userlib/UserHelperTest.java
index c372a28..6a94c71 100644
--- a/tests/carservice_unit_test/src/android/car/userlib/UserHelperTest.java
+++ b/tests/carservice_unit_test/src/android/car/userlib/UserHelperTest.java
@@ -16,21 +16,48 @@
package android.car.userlib;
+import static android.car.test.util.UserTestingHelper.newUser;
+
import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertThrows;
+
import android.car.test.mocks.AbstractExtendedMockitoTestCase;
+import android.content.Context;
+import android.content.pm.UserInfo;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
import android.os.UserHandle;
import android.os.UserManager;
+import androidx.test.InstrumentationRegistry;
+
+import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mock;
public final class UserHelperTest extends AbstractExtendedMockitoTestCase {
+ @Mock private Context mContext;
+ @Mock private UserManager mUserManager;
+
+ // Not worth to mock because it would need to mock a Drawable used by UserIcons.
+ private final Resources mResources = InstrumentationRegistry.getTargetContext().getResources();
+
@Override
protected void onSessionBuilder(CustomMockitoSessionBuilder session) {
session.spyStatic(UserManager.class);
}
+ @Before
+ public void setUp() {
+ when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
+ when(mContext.getApplicationContext()).thenReturn(mContext);
+ when(mContext.getResources()).thenReturn(mResources);
+ }
+
@Test
public void testSafeName() {
assertThat(UserHelper.safeName(null)).isNull();
@@ -63,4 +90,57 @@
mockIsHeadlessSystemUserMode(false);
assertThat(UserHelper.isHeadlessSystemUser(10)).isFalse();
}
+
+ @Test
+ public void testDefaultNonAdminRestrictions() {
+ int userId = 20;
+ UserInfo newNonAdmin = newUser(userId);
+
+ UserHelper.setDefaultNonAdminRestrictions(mContext, newNonAdmin, /* enable= */ true);
+
+ verify(mUserManager).setUserRestriction(
+ UserManager.DISALLOW_FACTORY_RESET, /* enable= */ true, UserHandle.of(userId));
+ }
+
+ @Test
+ public void testDefaultNonAdminRestrictions_nullContext_throwsException() {
+ int userId = 20;
+ UserInfo newNonAdmin = newUser(userId);
+
+ assertThrows(IllegalArgumentException.class,
+ () -> UserHelper.setDefaultNonAdminRestrictions(/* context= */ null,
+ newNonAdmin, /* enable= */ true));
+ }
+
+ @Test
+ public void testDefaultNonAdminRestrictions_nullUser_throwsException() {
+ assertThrows(IllegalArgumentException.class,
+ () -> UserHelper.setDefaultNonAdminRestrictions(mContext, /* user= */
+ null, /* enable= */ true));
+ }
+
+ @Test
+ public void testAssignDefaultIcon() {
+ int userId = 20;
+ UserInfo newNonAdmin = newUser(userId);
+
+ Bitmap bitmap = UserHelper.assignDefaultIcon(mContext, newNonAdmin);
+
+ verify(mUserManager).setUserIcon(userId, bitmap);
+ }
+
+ @Test
+ public void testAssignDefaultIcon_nullContext_throwsException() {
+ int userId = 20;
+ UserInfo newNonAdmin = newUser(userId);
+
+ assertThrows(IllegalArgumentException.class,
+ () -> UserHelper.assignDefaultIcon(/* context= */ null, newNonAdmin));
+ }
+
+ @Test
+ public void testAssignDefaultIcon_nullUser_throwsException() {
+ assertThrows(IllegalArgumentException.class,
+ () -> UserHelper.assignDefaultIcon(mContext, /* user= */ null));
+ }
}
diff --git a/user/car-user-lib/src/android/car/userlib/UserHelper.java b/user/car-user-lib/src/android/car/userlib/UserHelper.java
index 9535ffb..ba3d679 100644
--- a/user/car-user-lib/src/android/car/userlib/UserHelper.java
+++ b/user/car-user-lib/src/android/car/userlib/UserHelper.java
@@ -15,16 +15,34 @@
*/
package android.car.userlib;
+import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
+import android.content.Context;
+import android.content.pm.UserInfo;
+import android.graphics.Bitmap;
import android.os.UserHandle;
import android.os.UserManager;
+import com.android.internal.util.Preconditions;
+import com.android.internal.util.UserIcons;
+
+import com.google.android.collect.Sets;
+
+import java.util.Set;
+
/**
* Provides utility methods for generic user-related functionalities that don't require a manager.
*/
public final class UserHelper {
+ /**
+ * Default set of restrictions for Non-Admin users.
+ */
+ private static final Set<String> DEFAULT_NON_ADMIN_RESTRICTIONS = Sets.newArraySet(
+ UserManager.DISALLOW_FACTORY_RESET
+ );
+
private UserHelper() {
throw new UnsupportedOperationException("contains only static methods");
}
@@ -43,4 +61,41 @@
public static boolean isHeadlessSystemUser(@UserIdInt int userId) {
return userId == UserHandle.USER_SYSTEM && UserManager.isHeadlessSystemUserMode();
}
+
+ /**
+ * Sets the values of default Non-Admin restrictions to the passed in value.
+ *
+ * @param context Current application context
+ * @param user User to set restrictions on.
+ * @param enable If true, restriction is ON, If false, restriction is OFF.
+ */
+ public static void setDefaultNonAdminRestrictions(@NonNull Context context,
+ @NonNull UserInfo user, boolean enable) {
+ Preconditions.checkArgument(context != null, "Context cannot be null");
+ Preconditions.checkArgument(user != null, "User cannot be null");
+
+ UserManager userManager = UserManager.get(context);
+ for (String restriction : DEFAULT_NON_ADMIN_RESTRICTIONS) {
+ userManager.setUserRestriction(restriction, enable, user.getUserHandle());
+ }
+ }
+
+ /**
+ * Assigns a default icon to a user according to the user's id.
+ *
+ * @param context Current application context
+ * @param user User whose avatar is set to default icon.
+ * @return Bitmap of the user icon.
+ */
+ @NonNull
+ public static Bitmap assignDefaultIcon(@NonNull Context context, @NonNull UserInfo user) {
+ Preconditions.checkArgument(context != null, "Context cannot be null");
+ Preconditions.checkArgument(user != null, "User cannot be null");
+
+ int idForIcon = user.isGuest() ? UserHandle.USER_NULL : user.id;
+ Bitmap bitmap = UserIcons.convertToBitmap(
+ UserIcons.getDefaultUserIcon(context.getResources(), idForIcon, false));
+ UserManager.get(context).setUserIcon(user.id, bitmap);
+ return bitmap;
+ }
}