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.PorterDuff;
24 import android.graphics.drawable.Drawable;
25 import android.util.AttributeSet;
26 import android.view.View;
27 
28 import com.android.settingslib.drawable.UserIconDrawable;
29 import com.android.systemui.R;
30 
31 /**
32  * A view that displays a user image cropped to a circle with an optional frame.
33  */
34 public class UserAvatarView extends View {
35 
36     private final UserIconDrawable mDrawable = new UserIconDrawable();
37 
UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)38     public UserAvatarView(Context context, AttributeSet attrs,
39             int defStyleAttr,
40             int defStyleRes) {
41         super(context, attrs, defStyleAttr, defStyleRes);
42 
43         final TypedArray a = context.obtainStyledAttributes(
44                 attrs, R.styleable.UserAvatarView, defStyleAttr, defStyleRes);
45         final int N = a.getIndexCount();
46         for (int i = 0; i < N; i++) {
47             int attr = a.getIndex(i);
48             switch (attr) {
49                 case R.styleable.UserAvatarView_avatarPadding:
50                     setAvatarPadding(a.getDimension(attr, 0));
51                     break;
52                 case R.styleable.UserAvatarView_frameWidth:
53                     setFrameWidth(a.getDimension(attr, 0));
54                     break;
55                 case R.styleable.UserAvatarView_framePadding:
56                     setFramePadding(a.getDimension(attr, 0));
57                     break;
58                 case R.styleable.UserAvatarView_frameColor:
59                     setFrameColor(a.getColorStateList(attr));
60                     break;
61                 case R.styleable.UserAvatarView_badgeDiameter:
62                     setBadgeDiameter(a.getDimension(attr, 0));
63                     break;
64                 case R.styleable.UserAvatarView_badgeMargin:
65                     setBadgeMargin(a.getDimension(attr, 0));
66                     break;
67             }
68         }
69         a.recycle();
70         setBackground(mDrawable);
71     }
72 
UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr)73     public UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
74         this(context, attrs, defStyleAttr, 0);
75     }
76 
UserAvatarView(Context context, AttributeSet attrs)77     public UserAvatarView(Context context, AttributeSet attrs) {
78         this(context, attrs, 0);
79     }
80 
UserAvatarView(Context context)81     public UserAvatarView(Context context) {
82         this(context, null);
83     }
84 
85     /**
86      * @deprecated use {@link #setAvatar(Bitmap)} instead.
87      */
88     @Deprecated
setBitmap(Bitmap bitmap)89     public void setBitmap(Bitmap bitmap) {
90         setAvatar(bitmap);
91     }
92 
setFrameColor(ColorStateList color)93     public void setFrameColor(ColorStateList color) {
94         mDrawable.setFrameColor(color);
95     }
96 
setFrameWidth(float frameWidth)97     public void setFrameWidth(float frameWidth) {
98         mDrawable.setFrameWidth(frameWidth);
99     }
100 
setFramePadding(float framePadding)101     public void setFramePadding(float framePadding) {
102         mDrawable.setFramePadding(framePadding);
103     }
104 
setAvatarPadding(float avatarPadding)105     public void setAvatarPadding(float avatarPadding) {
106         mDrawable.setPadding(avatarPadding);
107     }
108 
setBadgeDiameter(float diameter)109     public void setBadgeDiameter(float diameter) {
110         mDrawable.setBadgeRadius(diameter * 0.5f);
111     }
112 
setBadgeMargin(float margin)113     public void setBadgeMargin(float margin) {
114         mDrawable.setBadgeMargin(margin);
115     }
116 
setAvatar(Bitmap avatar)117     public void setAvatar(Bitmap avatar) {
118         mDrawable.setIcon(avatar);
119         mDrawable.setBadge(null);
120     }
121 
setAvatarWithBadge(Bitmap avatar, int userId)122     public void setAvatarWithBadge(Bitmap avatar, int userId) {
123         mDrawable.setIcon(avatar);
124         mDrawable.setBadgeIfManagedUser(getContext(), userId);
125     }
126 
setDrawable(Drawable d)127     public void setDrawable(Drawable d) {
128         if (d instanceof UserIconDrawable) {
129             throw new RuntimeException("Recursively adding UserIconDrawable");
130         }
131         mDrawable.setIconDrawable(d);
132         mDrawable.setBadge(null);
133     }
134 
setDrawableWithBadge(Drawable d, int userId)135     public void setDrawableWithBadge(Drawable d, int userId) {
136         if (d instanceof UserIconDrawable) {
137             throw new RuntimeException("Recursively adding UserIconDrawable");
138         }
139         mDrawable.setIconDrawable(d);
140         mDrawable.setBadgeIfManagedUser(getContext(), userId);
141     }
142 }
143