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 /** 78 * @deprecated use {@link #setAvatar(Bitmap)} instead. 79 */ 80 @Deprecated setBitmap(Bitmap bitmap)81 public void setBitmap(Bitmap bitmap) { 82 setAvatar(bitmap); 83 } 84 setFrameColor(ColorStateList color)85 public void setFrameColor(ColorStateList color) { 86 mDrawable.setFrameColor(color); 87 } 88 setFrameWidth(float frameWidth)89 public void setFrameWidth(float frameWidth) { 90 mDrawable.setFrameWidth(frameWidth); 91 } 92 setFramePadding(float framePadding)93 public void setFramePadding(float framePadding) { 94 mDrawable.setFramePadding(framePadding); 95 } 96 setAvatarPadding(float avatarPadding)97 public void setAvatarPadding(float avatarPadding) { 98 mDrawable.setPadding(avatarPadding); 99 } 100 setBadgeDiameter(float diameter)101 public void setBadgeDiameter(float diameter) { 102 mDrawable.setBadgeRadius(diameter * 0.5f); 103 } 104 setBadgeMargin(float margin)105 public void setBadgeMargin(float margin) { 106 mDrawable.setBadgeMargin(margin); 107 } 108 setAvatar(Bitmap avatar)109 public void setAvatar(Bitmap avatar) { 110 mDrawable.setIcon(avatar); 111 mDrawable.setBadge(null); 112 } 113 setAvatarWithBadge(Bitmap avatar, int userId)114 public void setAvatarWithBadge(Bitmap avatar, int userId) { 115 mDrawable.setIcon(avatar); 116 mDrawable.setBadgeIfManagedUser(getContext(), userId); 117 } 118 setDrawable(Drawable d)119 public void setDrawable(Drawable d) { 120 if (d instanceof UserIconDrawable) { 121 throw new RuntimeException("Recursively adding UserIconDrawable"); 122 } 123 mDrawable.setIconDrawable(d); 124 mDrawable.setBadge(null); 125 } 126 setDrawableWithBadge(Drawable d, int userId)127 public void setDrawableWithBadge(Drawable d, int userId) { 128 if (d instanceof UserIconDrawable) { 129 throw new RuntimeException("Recursively adding UserIconDrawable"); 130 } 131 mDrawable.setIconDrawable(d); 132 mDrawable.setBadgeIfManagedUser(getContext(), userId); 133 } 134 } 135