Centralized user icon drawing with badges
Renamed CircleFramedDrawable to UserIconDrawable and
added badge/frame support.
Replaced several instances of user icon drawing with
UserIconDrawable
Bug: 27483956
Change-Id: I10d665406ea8b085667aa8c71ffcedffe72dafbd
diff --git a/packages/SystemUI/src/com/android/systemui/BitmapHelper.java b/packages/SystemUI/src/com/android/systemui/BitmapHelper.java
deleted file mode 100644
index 1933bbc..0000000
--- a/packages/SystemUI/src/com/android/systemui/BitmapHelper.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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.systemui;
-
-import android.graphics.Bitmap;
-import android.graphics.BitmapShader;
-import android.graphics.Canvas;
-import android.graphics.Matrix;
-import android.graphics.Paint;
-import android.graphics.RectF;
-import android.graphics.Shader;
-
-public class BitmapHelper {
- /**
- * Generate a new bitmap (width x height pixels, ARGB_8888) with the input bitmap scaled
- * to fit and clipped to an inscribed circle.
- * @param input Bitmap to resize and clip
- * @param width Width of output bitmap (and diameter of circle)
- * @param height Height of output bitmap
- * @return A shiny new bitmap for you to use
- */
- public static Bitmap createCircularClip(Bitmap input, int width, int height) {
- if (input == null) return null;
-
- final int inWidth = input.getWidth();
- final int inHeight = input.getHeight();
- final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
- final Canvas canvas = new Canvas(output);
- final Paint paint = new Paint();
- paint.setShader(new BitmapShader(input, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
- paint.setAntiAlias(true);
- final RectF srcRect = new RectF(0, 0, inWidth, inHeight);
- final RectF dstRect = new RectF(0, 0, width, height);
- final Matrix m = new Matrix();
- m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER);
- canvas.setMatrix(m);
- canvas.drawCircle(inWidth / 2, inHeight / 2, inWidth / 2, paint);
- return output;
- }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/UserDetailItemView.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/UserDetailItemView.java
index fcf758b..99eae02 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/UserDetailItemView.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/UserDetailItemView.java
@@ -30,6 +30,7 @@
import android.widget.TextView;
import com.android.internal.util.ArrayUtils;
+import com.android.settingslib.drawable.UserIconDrawable;
import com.android.systemui.FontSizeUtils;
import com.android.systemui.R;
import com.android.systemui.statusbar.phone.UserAvatarView;
@@ -87,25 +88,29 @@
return (UserDetailItemView) convertView;
}
- public void bind(String name, Bitmap picture) {
+ public void bind(String name, Bitmap picture, int userId) {
mName.setText(name);
- mAvatar.setBitmap(picture);
+ mAvatar.setAvatarWithBadge(picture, userId);
}
- public void bind(String name, Drawable picture) {
+ public void bind(String name, Drawable picture, int userId) {
mName.setText(name);
- mAvatar.setDrawable(picture);
+ mAvatar.setDrawableWithBadge(picture, userId);
+ }
+
+ public void setAvatarEnabled(boolean enabled) {
+ mAvatar.setEnabled(enabled);
}
public void setDisabledByAdmin(boolean disabled) {
mRestrictedPadlock.setVisibility(disabled ? View.VISIBLE : View.GONE);
mName.setEnabled(!disabled);
- mAvatar.setDisabled(disabled);
+ mAvatar.setEnabled(!disabled);
}
public void setEnabled(boolean enabled) {
mName.setEnabled(enabled);
- mAvatar.setDisabled(!enabled);
+ mAvatar.setEnabled(enabled);
}
@Override
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/UserDetailView.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/UserDetailView.java
index da98762..d4fa765 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/UserDetailView.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/UserDetailView.java
@@ -76,9 +76,9 @@
}
String name = getName(mContext, item);
if (item.picture == null) {
- v.bind(name, getDrawable(mContext, item));
+ v.bind(name, getDrawable(mContext, item), item.resolveId());
} else {
- v.bind(name, item.picture);
+ v.bind(name, item.picture, item.info.id);
}
v.setActivated(item.isCurrent);
v.setDisabledByAdmin(item.isDisabledByAdmin);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/UserAvatarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/UserAvatarView.java
index 093a827..dc1b35d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/UserAvatarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/UserAvatarView.java
@@ -17,69 +17,57 @@
package com.android.systemui.statusbar.phone;
import android.content.Context;
+import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
-import android.graphics.BitmapShader;
-import android.graphics.Canvas;
-import android.graphics.Matrix;
-import android.graphics.Paint;
import android.graphics.PorterDuff;
-import android.graphics.PorterDuffColorFilter;
-import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
+import com.android.settingslib.drawable.UserIconDrawable;
import com.android.systemui.R;
/**
- * A view that displays a user image cropped to a circle with a frame.
+ * A view that displays a user image cropped to a circle with an optional frame.
*/
public class UserAvatarView extends View {
- private int mActiveFrameColor;
- private int mFrameColor;
- private float mFrameWidth;
- private float mFramePadding;
- private Bitmap mBitmap;
- private Drawable mDrawable;
- private boolean mIsDisabled;
-
- private final Paint mFramePaint = new Paint();
- private final Paint mBitmapPaint = new Paint();
- private final Matrix mDrawMatrix = new Matrix();
-
- private float mScale = 1;
+ private final UserIconDrawable mDrawable = new UserIconDrawable();
public UserAvatarView(Context context, AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
+
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.UserAvatarView, defStyleAttr, defStyleRes);
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr) {
+ case R.styleable.UserAvatarView_avatarPadding:
+ setAvatarPadding(a.getDimension(attr, 0));
+ break;
case R.styleable.UserAvatarView_frameWidth:
setFrameWidth(a.getDimension(attr, 0));
break;
case R.styleable.UserAvatarView_framePadding:
setFramePadding(a.getDimension(attr, 0));
break;
- case R.styleable.UserAvatarView_activeFrameColor:
- setActiveFrameColor(a.getColor(attr, 0));
- break;
case R.styleable.UserAvatarView_frameColor:
- setFrameColor(a.getColor(attr, 0));
+ setFrameColor(a.getColorStateList(attr));
+ break;
+ case R.styleable.UserAvatarView_badgeDiameter:
+ setBadgeDiameter(a.getDimension(attr, 0));
+ break;
+ case R.styleable.UserAvatarView_badgeMargin:
+ setBadgeMargin(a.getDimension(attr, 0));
break;
}
}
a.recycle();
-
- mFramePaint.setAntiAlias(true);
- mFramePaint.setStyle(Paint.Style.STROKE);
- mBitmapPaint.setAntiAlias(true);
+ setBackground(mDrawable);
}
public UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
@@ -94,180 +82,61 @@
this(context, null);
}
+ /**
+ * @deprecated use {@link #setAvatar(Bitmap)} instead.
+ */
+ @Deprecated
public void setBitmap(Bitmap bitmap) {
- setDrawable(null);
- mBitmap = bitmap;
- if (mBitmap != null) {
- mBitmapPaint.setShader(new BitmapShader(
- bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
- } else {
- mBitmapPaint.setShader(null);
- }
- configureBounds();
- invalidate();
+ setAvatar(bitmap);
}
- public void setFrameColor(int frameColor) {
- mFrameColor = frameColor;
- invalidate();
- }
-
- public void setActiveFrameColor(int activeFrameColor) {
- mActiveFrameColor = activeFrameColor;
- invalidate();
+ public void setFrameColor(ColorStateList color) {
+ mDrawable.setFrameColor(color);
}
public void setFrameWidth(float frameWidth) {
- mFrameWidth = frameWidth;
- invalidate();
+ mDrawable.setFrameWidth(frameWidth);
}
public void setFramePadding(float framePadding) {
- mFramePadding = framePadding;
- invalidate();
+ mDrawable.setFramePadding(framePadding);
}
- @Override
- protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
- super.onLayout(changed, left, top, right, bottom);
- configureBounds();
+ public void setAvatarPadding(float avatarPadding) {
+ mDrawable.setPadding(avatarPadding);
}
- public void configureBounds() {
- int vwidth = getWidth() - mPaddingLeft - mPaddingRight;
- int vheight = getHeight() - mPaddingTop - mPaddingBottom;
-
- int dwidth;
- int dheight;
- if (mBitmap != null) {
- dwidth = mBitmap.getWidth();
- dheight = mBitmap.getHeight();
- } else if (mDrawable != null) {
- vwidth -= 2 * (mFrameWidth - 1);
- vheight -= 2 * (mFrameWidth - 1);
- dwidth = vwidth;
- dheight = vheight;
- mDrawable.setBounds(0, 0, dwidth, dheight);
- } else {
- return;
- }
-
- float scale;
- float dx;
- float dy;
-
- scale = Math.min((float) vwidth / (float) dwidth,
- (float) vheight / (float) dheight);
-
- dx = (int) ((vwidth - dwidth * scale) * 0.5f + 0.5f);
- dy = (int) ((vheight - dheight * scale) * 0.5f + 0.5f);
-
- mDrawMatrix.setScale(scale, scale);
- mDrawMatrix.postTranslate(dx, dy);
- mScale = scale;
+ public void setBadgeDiameter(float diameter) {
+ mDrawable.setBadgeRadius(diameter * 0.5f);
}
- @Override
- protected void onDraw(Canvas canvas) {
- int frameColor = isActivated() ? mActiveFrameColor : mFrameColor;
- float halfW = getWidth() / 2f;
- float halfH = getHeight() / 2f;
- float halfSW = Math.min(halfH, halfW);
- updateDrawableIfDisabled();
- if (mBitmap != null && mScale > 0) {
- int saveCount = canvas.getSaveCount();
- canvas.save();
- canvas.translate(mPaddingLeft, mPaddingTop);
- canvas.concat(mDrawMatrix);
- float halfBW = mBitmap.getWidth() / 2f;
- float halfBH = mBitmap.getHeight() / 2f;
- float halfBSW = Math.min(halfBH, halfBW);
- canvas.drawCircle(halfBW, halfBH, halfBSW - mFrameWidth / mScale + 1, mBitmapPaint);
- canvas.restoreToCount(saveCount);
- } else if (mDrawable != null && mScale > 0) {
- int saveCount = canvas.getSaveCount();
- canvas.save();
- canvas.translate(mPaddingLeft, mPaddingTop);
- canvas.translate(mFrameWidth - 1, mFrameWidth - 1);
- canvas.concat(mDrawMatrix);
- mDrawable.draw(canvas);
- canvas.restoreToCount(saveCount);
- }
- if (frameColor != 0) {
- mFramePaint.setColor(frameColor);
- mFramePaint.setStrokeWidth(mFrameWidth);
- canvas.drawCircle(halfW, halfH, halfSW + (mFramePadding - mFrameWidth) / 2f,
- mFramePaint);
- }
+ public void setBadgeMargin(float margin) {
+ mDrawable.setBadgeMargin(margin);
+ }
+
+ public void setAvatar(Bitmap avatar) {
+ mDrawable.setIcon(avatar);
+ mDrawable.setBadge(null);
+ }
+
+ public void setAvatarWithBadge(Bitmap avatar, int userId) {
+ mDrawable.setIcon(avatar);
+ mDrawable.setBadgeIfManagedUser(getContext(), userId);
}
public void setDrawable(Drawable d) {
- if (mDrawable != null) {
- mDrawable.setCallback(null);
- unscheduleDrawable(mDrawable);
+ if (d instanceof UserIconDrawable) {
+ throw new RuntimeException("Recursively adding UserIconDrawable");
}
- mDrawable = d;
- if (d != null) {
- d.setCallback(this);
- if (d.isStateful()) {
- d.setState(getDrawableState());
- }
- d.setLayoutDirection(getLayoutDirection());
- configureBounds();
- }
- if (d != null) {
- mBitmap = null;
- }
- configureBounds();
- invalidate();
+ mDrawable.setIconDrawable(d);
+ mDrawable.setBadge(null);
}
- @Override
- public void invalidateDrawable(Drawable dr) {
- if (dr == mDrawable) {
- invalidate();
- } else {
- super.invalidateDrawable(dr);
+ public void setDrawableWithBadge(Drawable d, int userId) {
+ if (d instanceof UserIconDrawable) {
+ throw new RuntimeException("Recursively adding UserIconDrawable");
}
- }
-
- @Override
- protected boolean verifyDrawable(Drawable who) {
- return who == mDrawable || super.verifyDrawable(who);
- }
-
- @Override
- protected void drawableStateChanged() {
- super.drawableStateChanged();
- if (mDrawable != null && mDrawable.isStateful()) {
- mDrawable.setState(getDrawableState());
- }
- }
-
- public void setDisabled(boolean disabled) {
- if (mIsDisabled == disabled) {
- return;
- }
- mIsDisabled = disabled;
- invalidate();
- }
-
- private void updateDrawableIfDisabled() {
- int disabledColor = getContext().getColor(R.color.qs_tile_disabled_color);
- PorterDuffColorFilter filter = new PorterDuffColorFilter(disabledColor,
- PorterDuff.Mode.SRC_ATOP);
- if (mBitmap != null) {
- if (mIsDisabled) {
- mBitmapPaint.setColorFilter(filter);
- } else {
- mBitmapPaint.setColorFilter(null);
- }
- } else if (mDrawable != null) {
- if (mIsDisabled) {
- mDrawable.setColorFilter(filter);
- } else {
- mDrawable.setColorFilter(null);
- }
- }
+ mDrawable.setIconDrawable(d);
+ mDrawable.setBadgeIfManagedUser(getContext(), userId);
}
}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcher.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcher.java
index fb310a6..c39d718 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcher.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcher.java
@@ -253,17 +253,13 @@
UserDetailItemView v = (UserDetailItemView) convertView;
String name = getName(mContext, item);
- Drawable drawable;
if (item.picture == null) {
- drawable = getDrawable(mContext, item).mutate();
+ v.bind(name, getDrawable(mContext, item).mutate(), item.resolveId());
} else {
- drawable = new BitmapDrawable(mContext.getResources(), item.picture);
+ v.bind(name, item.picture, item.info.id);
}
// Disable the icon if switching is disabled
- if (!item.isSwitchToEnabled) {
- drawable.setTint(mContext.getColor(R.color.qs_tile_disabled_color));
- }
- v.bind(name, drawable);
+ v.setAvatarEnabled(item.isSwitchToEnabled);
convertView.setActivated(item.isCurrent);
convertView.setTag(item);
return convertView;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserInfoController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserInfoController.java
index 85ac755..bae5bda 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserInfoController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserInfoController.java
@@ -26,7 +26,6 @@
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
-import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.RemoteException;
@@ -37,7 +36,7 @@
import android.util.Pair;
import com.android.internal.util.UserIcons;
-import com.android.systemui.BitmapHelper;
+import com.android.settingslib.drawable.UserIconDrawable;
import com.android.systemui.R;
import java.util.ArrayList;
@@ -155,8 +154,8 @@
Drawable avatar = null;
Bitmap rawAvatar = um.getUserIcon(userId);
if (rawAvatar != null) {
- avatar = new BitmapDrawable(mContext.getResources(),
- BitmapHelper.createCircularClip(rawAvatar, avatarSize, avatarSize));
+ avatar = new UserIconDrawable(avatarSize)
+ .setIcon(rawAvatar).setBadgeIfManagedUser(mContext, userId).bake();
} else {
avatar = UserIcons.getDefaultUserIcon(isGuest? UserHandle.USER_NULL : userId,
/* light= */ true);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
index ea0bdf2..c82ba3b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
@@ -50,7 +50,6 @@
import com.android.internal.logging.MetricsProto.MetricsEvent;
import com.android.internal.util.UserIcons;
import com.android.settingslib.RestrictedLockUtils;
-import com.android.systemui.BitmapHelper;
import com.android.systemui.GuestResumeSessionReceiver;
import com.android.systemui.R;
import com.android.systemui.SystemUISecondaryUserService;
@@ -197,8 +196,6 @@
boolean canSwitchUsers = mUserManager.canSwitchUsers();
UserInfo currentUserInfo = null;
UserRecord guestRecord = null;
- int avatarSize = mContext.getResources()
- .getDimensionPixelSize(R.dimen.max_avatar_size);
for (UserInfo info : infos) {
boolean isCurrent = currentId == info.id;
@@ -219,8 +216,10 @@
picture = mUserManager.getUserIcon(info.id);
if (picture != null) {
- picture = BitmapHelper.createCircularClip(
- picture, avatarSize, avatarSize);
+ int avatarSize = mContext.getResources()
+ .getDimensionPixelSize(R.dimen.max_avatar_size);
+ picture = Bitmap.createScaledBitmap(
+ picture, avatarSize, avatarSize, true);
}
}
int index = isCurrent ? 0 : records.size();
@@ -664,8 +663,7 @@
if (item.isAddUser) {
return context.getDrawable(R.drawable.ic_add_circle_qs);
}
- return UserIcons.getDefaultUserIcon(item.isGuest ? UserHandle.USER_NULL : item.info.id,
- /* light= */ true);
+ return UserIcons.getDefaultUserIcon(item.resolveId(), /* light= */ true);
}
public void refresh() {
@@ -718,6 +716,13 @@
isSwitchToEnabled);
}
+ public int resolveId() {
+ if (isGuest || info == null) {
+ return UserHandle.USER_NULL;
+ }
+ return info.id;
+ }
+
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("UserRecord(");