• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.car.admin.ui;
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 
29 // TODO(b/176262528): copied from com.android.systemui, ideally it should be provided by a common
30 // library like SettingsLib. If not, then this whole project / package should be renamed to
31 // "car-user-ui-lib", not "car-admin-ui-lib".
32 
33 /**
34  * A view that displays a user image cropped to a circle with an optional frame.
35  */
36 public class UserAvatarView extends View {
37 
38     private final UserIconDrawable mDrawable = new UserIconDrawable();
39 
UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)40     public UserAvatarView(Context context, AttributeSet attrs,
41             int defStyleAttr,
42             int defStyleRes) {
43         super(context, attrs, defStyleAttr, defStyleRes);
44 
45         final TypedArray a = context.obtainStyledAttributes(
46                 attrs, R.styleable.UserAvatarView, defStyleAttr, defStyleRes);
47         final int N = a.getIndexCount();
48         for (int i = 0; i < N; i++) {
49             int attr = a.getIndex(i);
50             if (attr == R.styleable.UserAvatarView_avatarPadding) {
51                 setAvatarPadding(a.getDimension(attr, 0));
52             } else if (attr == R.styleable.UserAvatarView_frameWidth) {
53                 setFrameWidth(a.getDimension(attr, 0));
54             } else if (attr == R.styleable.UserAvatarView_framePadding) {
55                 setFramePadding(a.getDimension(attr, 0));
56             } else if (attr == R.styleable.UserAvatarView_frameColor) {
57                 setFrameColor(a.getColorStateList(attr));
58             } else if (attr == R.styleable.UserAvatarView_badgeDiameter) {
59                 setBadgeDiameter(a.getDimension(attr, 0));
60             } else if (attr == R.styleable.UserAvatarView_badgeMargin) {
61                 setBadgeMargin(a.getDimension(attr, 0));
62             }
63             else {
64                 setBadgeDiameter(a.getDimension(attr, 0));
65             }
66         }
67         a.recycle();
68         setBackground(mDrawable);
69     }
70 
UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr)71     public UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
72         this(context, attrs, defStyleAttr, 0);
73     }
74 
UserAvatarView(Context context, AttributeSet attrs)75     public UserAvatarView(Context context, AttributeSet attrs) {
76         this(context, attrs, 0);
77     }
78 
UserAvatarView(Context context)79     public UserAvatarView(Context context) {
80         this(context, null);
81     }
82 
83     @Override
setActivated(boolean activated)84     public void setActivated(boolean activated) {
85         super.setActivated(activated);
86         mDrawable.invalidateSelf();
87     }
88 
setFrameColor(ColorStateList color)89     public void setFrameColor(ColorStateList color) {
90         mDrawable.setFrameColor(color);
91     }
92 
setFrameWidth(float frameWidth)93     public void setFrameWidth(float frameWidth) {
94         mDrawable.setFrameWidth(frameWidth);
95     }
96 
setFramePadding(float framePadding)97     public void setFramePadding(float framePadding) {
98         mDrawable.setFramePadding(framePadding);
99     }
100 
setAvatarPadding(float avatarPadding)101     public void setAvatarPadding(float avatarPadding) {
102         mDrawable.setPadding(avatarPadding);
103     }
104 
setBadgeDiameter(float diameter)105     public void setBadgeDiameter(float diameter) {
106         mDrawable.setBadgeRadius(diameter * 0.5f);
107     }
108 
setBadgeMargin(float margin)109     public void setBadgeMargin(float margin) {
110         mDrawable.setBadgeMargin(margin);
111     }
112 
setAvatar(Bitmap avatar)113     public void setAvatar(Bitmap avatar) {
114         mDrawable.setIcon(avatar);
115         mDrawable.setBadge(null);
116     }
117 
setAvatarWithBadge(Bitmap avatar, int userId)118     public void setAvatarWithBadge(Bitmap avatar, int userId) {
119         mDrawable.setIcon(avatar);
120         mDrawable.setBadgeIfManagedUser(getContext(), userId);
121     }
122 
setDrawable(Drawable d)123     public void setDrawable(Drawable d) {
124         if (d instanceof UserIconDrawable) {
125             throw new RuntimeException("Recursively adding UserIconDrawable");
126         }
127         mDrawable.setIconDrawable(d);
128         mDrawable.setBadge(null);
129     }
130 
setDrawableWithBadge(Drawable d, int userId)131     public void setDrawableWithBadge(Drawable d, int userId) {
132         if (d instanceof UserIconDrawable) {
133             throw new RuntimeException("Recursively adding UserIconDrawable");
134         }
135         mDrawable.setIconDrawable(d);
136         mDrawable.setBadgeIfManagedUser(getContext(), userId);
137     }
138 
getUserIconDrawable()139     public UserIconDrawable getUserIconDrawable() {
140         return mDrawable;
141     }
142 }
143