1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.systemui.statusbar.phone; 18 19 import android.content.Context; 20 import android.content.res.ColorStateList; 21 import android.content.res.TypedArray; 22 import android.graphics.Bitmap; 23 import android.graphics.drawable.Drawable; 24 import android.util.AttributeSet; 25 import android.view.View; 26 27 import com.android.settingslib.drawable.UserIconDrawable; 28 import com.android.systemui.R; 29 30 /** 31 * A view that displays a user image cropped to a circle with an optional frame. 32 */ 33 public class UserAvatarView extends View { 34 35 private final UserIconDrawable mDrawable = new UserIconDrawable(); 36 UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)37 public UserAvatarView(Context context, AttributeSet attrs, 38 int defStyleAttr, 39 int defStyleRes) { 40 super(context, attrs, defStyleAttr, defStyleRes); 41 42 final TypedArray a = context.obtainStyledAttributes( 43 attrs, R.styleable.UserAvatarView, defStyleAttr, defStyleRes); 44 final int N = a.getIndexCount(); 45 for (int i = 0; i < N; i++) { 46 int attr = a.getIndex(i); 47 if (attr == R.styleable.UserAvatarView_avatarPadding) { 48 setAvatarPadding(a.getDimension(attr, 0)); 49 } else if (attr == R.styleable.UserAvatarView_frameWidth) { 50 setFrameWidth(a.getDimension(attr, 0)); 51 } else if (attr == R.styleable.UserAvatarView_framePadding) { 52 setFramePadding(a.getDimension(attr, 0)); 53 } else if (attr == R.styleable.UserAvatarView_frameColor) { 54 setFrameColor(a.getColorStateList(attr)); 55 } else if (attr == R.styleable.UserAvatarView_badgeDiameter) { 56 setBadgeDiameter(a.getDimension(attr, 0)); 57 } else if (attr == R.styleable.UserAvatarView_badgeMargin) { 58 setBadgeMargin(a.getDimension(attr, 0)); 59 } 60 } 61 a.recycle(); 62 setBackground(mDrawable); 63 } 64 UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr)65 public UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr) { 66 this(context, attrs, defStyleAttr, 0); 67 } 68 UserAvatarView(Context context, AttributeSet attrs)69 public UserAvatarView(Context context, AttributeSet attrs) { 70 this(context, attrs, 0); 71 } 72 UserAvatarView(Context context)73 public UserAvatarView(Context context) { 74 this(context, null); 75 } 76 77 @Override setActivated(boolean activated)78 public void setActivated(boolean activated) { 79 super.setActivated(activated); 80 mDrawable.invalidateSelf(); 81 } 82 83 /** 84 * @deprecated use {@link #setAvatar(Bitmap)} instead. 85 */ 86 @Deprecated setBitmap(Bitmap bitmap)87 public void setBitmap(Bitmap bitmap) { 88 setAvatar(bitmap); 89 } 90 setFrameColor(ColorStateList color)91 public void setFrameColor(ColorStateList color) { 92 mDrawable.setFrameColor(color); 93 } 94 setFrameWidth(float frameWidth)95 public void setFrameWidth(float frameWidth) { 96 mDrawable.setFrameWidth(frameWidth); 97 } 98 setFramePadding(float framePadding)99 public void setFramePadding(float framePadding) { 100 mDrawable.setFramePadding(framePadding); 101 } 102 setAvatarPadding(float avatarPadding)103 public void setAvatarPadding(float avatarPadding) { 104 mDrawable.setPadding(avatarPadding); 105 } 106 setBadgeDiameter(float diameter)107 public void setBadgeDiameter(float diameter) { 108 mDrawable.setBadgeRadius(diameter * 0.5f); 109 } 110 setBadgeMargin(float margin)111 public void setBadgeMargin(float margin) { 112 mDrawable.setBadgeMargin(margin); 113 } 114 setAvatar(Bitmap avatar)115 public void setAvatar(Bitmap avatar) { 116 mDrawable.setIcon(avatar); 117 mDrawable.setBadge(null); 118 } 119 setAvatarWithBadge(Bitmap avatar, int userId)120 public void setAvatarWithBadge(Bitmap avatar, int userId) { 121 mDrawable.setIcon(avatar); 122 mDrawable.setBadgeIfManagedUser(getContext(), userId); 123 } 124 setDrawable(Drawable d)125 public void setDrawable(Drawable d) { 126 if (d instanceof UserIconDrawable) { 127 throw new RuntimeException("Recursively adding UserIconDrawable"); 128 } 129 mDrawable.setIconDrawable(d); 130 mDrawable.setBadge(null); 131 } 132 setDrawableWithBadge(Drawable d, int userId)133 public void setDrawableWithBadge(Drawable d, int userId) { 134 if (d instanceof UserIconDrawable) { 135 throw new RuntimeException("Recursively adding UserIconDrawable"); 136 } 137 mDrawable.setIconDrawable(d); 138 mDrawable.setBadgeIfManagedUser(getContext(), userId); 139 } 140 } 141