Assign default icon to users at creation time and in SystemUI.
Right now different code in System UI, Settings app and other places replace the
user icon with their own default. This tries to make it consistent by moving the
mechanism used in Settings in a helper class.
Bug: 17311038
Change-Id: Ic858c65bf82a98b9806dbba029e7cdcf441f372e
diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java
index d746745..68606836 100644
--- a/core/java/android/app/ApplicationPackageManager.java
+++ b/core/java/android/app/ApplicationPackageManager.java
@@ -66,6 +66,7 @@
import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;
+import com.android.internal.util.UserIcons;
import dalvik.system.VMRuntime;
@@ -1660,7 +1661,11 @@
*/
public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
- return new BitmapDrawable(getUserManager().getUserIcon(itemInfo.showUserIcon));
+ Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
+ if (bitmap == null) {
+ return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
+ }
+ return new BitmapDrawable(bitmap);
}
Drawable dr = null;
if (itemInfo.packageName != null) {
diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java
index c76ff11..c25278f 100644
--- a/core/java/android/os/UserManager.java
+++ b/core/java/android/os/UserManager.java
@@ -998,6 +998,7 @@
* Returns a file descriptor for the user's photo. PNG data can be read from this file.
* @param userHandle the user whose photo we want to read.
* @return a {@link Bitmap} of the user's photo, or null if there's no photo.
+ * @see com.android.internal.util.UserIcons#getDefaultUserIcon for a default.
* @hide
*/
public Bitmap getUserIcon(int userHandle) {
diff --git a/core/java/com/android/internal/util/UserIcons.java b/core/java/com/android/internal/util/UserIcons.java
new file mode 100644
index 0000000..e1e9d5e
--- /dev/null
+++ b/core/java/com/android/internal/util/UserIcons.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2014 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 com.android.internal.util;
+
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.PorterDuff.Mode;
+import android.graphics.drawable.Drawable;
+import android.os.UserHandle;
+
+import com.android.internal.R;
+
+/**
+ * Helper class that generates default user icons.
+ */
+public class UserIcons {
+
+ private static final int[] USER_ICON_COLORS = {
+ R.color.user_icon_1,
+ R.color.user_icon_2,
+ R.color.user_icon_3,
+ R.color.user_icon_4,
+ R.color.user_icon_5,
+ R.color.user_icon_6,
+ R.color.user_icon_7,
+ R.color.user_icon_8
+ };
+
+ /**
+ * Converts a given drawable to a bitmap.
+ */
+ public static Bitmap convertToBitmap(Drawable icon) {
+ if (icon == null) {
+ return null;
+ }
+ Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
+ Bitmap.Config.ARGB_8888);
+ icon.draw(new Canvas(bitmap));
+ return bitmap;
+ }
+
+ /**
+ * Returns a default user icon for the given user.
+ *
+ * Note that for guest users, you should pass in {@code UserHandle.USER_NULL}.
+ * @param userId the user id or {@code UserHandle.USER_NULL} for a non-user specific icon
+ * @param light whether we want a light icon (suitable for a dark background)
+ */
+ public static Drawable getDefaultUserIcon(int userId, boolean light) {
+ int colorResId = light ? R.color.user_icon_default_white : R.color.user_icon_default_gray;
+ if (userId != UserHandle.USER_NULL) {
+ // Return colored icon instead
+ colorResId = USER_ICON_COLORS[userId % USER_ICON_COLORS.length];
+ }
+ Drawable icon = Resources.getSystem().getDrawable(R.drawable.ic_account_circle).mutate();
+ icon.setColorFilter(Resources.getSystem().getColor(colorResId), Mode.SRC_IN);
+ icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
+ return icon;
+ }
+}
diff --git a/core/res/res/drawable/ic_account_circle.xml b/core/res/res/drawable/ic_account_circle.xml
new file mode 100644
index 0000000..a8c5b8c
--- /dev/null
+++ b/core/res/res/drawable/ic_account_circle.xml
@@ -0,0 +1,24 @@
+<!--
+Copyright (C) 2014 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.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48.0dp"
+ android:height="48.0dp"
+ android:viewportWidth="48.0"
+ android:viewportHeight="48.0">
+ <path
+ android:pathData="M24,0C10.8,0 0,10.8 0,24s10.8,24 24,24s24,-10.8 24,-24S37.200001,0 24,0zM24,7.2c3.96,0 7.2,3.24 7.2,7.2s-3.24,7.2 -7.2,7.2s-7.2,-3.24 -7.2,-7.2S20.040001,7.2 24,7.2zM24,41.279999c-6,0 -11.28,-3.12 -14.4,-7.68c0.12,-4.8 9.6,-7.44 14.4,-7.44s14.28,2.64 14.4,7.44C35.279999,38.16 30,41.279999 24,41.279999z"
+ android:fillColor="#FFFFFFFF"/>
+</vector>
diff --git a/core/res/res/values/colors.xml b/core/res/res/values/colors.xml
index c42683b..5a7e168 100644
--- a/core/res/res/values/colors.xml
+++ b/core/res/res/values/colors.xml
@@ -147,5 +147,16 @@
<color name="system_notification_accent_color">#ff607D8B</color>
<color name="battery_saver_mode_color">#fff4511e</color><!-- deep orange 600 -->
+ <!-- Default user icon colors -->
+ <color name="user_icon_1">#ffe91e63</color><!-- pink 500 -->
+ <color name="user_icon_2">#ff3f51b5</color><!-- indigo 500 -->
+ <color name="user_icon_3">#ff4285f4</color><!-- blue 500 -->
+ <color name="user_icon_4">#ff00bcd4</color><!-- teal 500 -->
+ <color name="user_icon_5">#ff0f9d58</color><!-- green 500 -->
+ <color name="user_icon_6">#ff8bc34a</color><!-- light green 500 -->
+ <color name="user_icon_7">#ffff9800</color><!-- orange 500 -->
+ <color name="user_icon_8">#ffff5722</color><!-- deep orange 500 -->
+ <color name="user_icon_default_gray">#ff9e9e9e</color><!-- gray 500 -->
+ <color name="user_icon_default_white">#ffffffff</color><!-- white -->
</resources>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 7fc522f..117e235 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -1170,6 +1170,18 @@
<java-symbol type="drawable" name="sim_dark_orange" />
<java-symbol type="drawable" name="sim_dark_purple" />
+ <java-symbol type="drawable" name="ic_account_circle" />
+ <java-symbol type="color" name="user_icon_1" />
+ <java-symbol type="color" name="user_icon_2" />
+ <java-symbol type="color" name="user_icon_3" />
+ <java-symbol type="color" name="user_icon_4" />
+ <java-symbol type="color" name="user_icon_5" />
+ <java-symbol type="color" name="user_icon_6" />
+ <java-symbol type="color" name="user_icon_7" />
+ <java-symbol type="color" name="user_icon_8" />
+ <java-symbol type="color" name="user_icon_default_gray" />
+ <java-symbol type="color" name="user_icon_default_white" />
+
<java-symbol type="layout" name="action_bar_home" />
<java-symbol type="layout" name="action_bar_title_item" />
<java-symbol type="layout" name="action_menu_item_layout" />