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.internal.util.Preconditions; 28 import com.android.settingslib.drawable.UserIconDrawable; 29 30 // TODO(b/176262528): copied from com.android.systemui, ideally it should be provided by a common 31 // library like SettingsLib. If not, then this whole project / package should be renamed to 32 // "car-user-ui-libs", not "car-admin-ui-libs". 33 34 /** 35 * A view that displays a user image cropped to a circle with an optional frame. 36 */ 37 public class UserAvatarView extends View { 38 39 private final UserIconDrawable mDrawable = new UserIconDrawable(); 40 UserAvatarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)41 public UserAvatarView(Context context, AttributeSet attrs, 42 int defStyleAttr, 43 int defStyleRes) { 44 super(context, attrs, defStyleAttr, defStyleRes); 45 46 final TypedArray a = context.obtainStyledAttributes( 47 attrs, R.styleable.UserAvatarView, defStyleAttr, defStyleRes); 48 final int n = a.getIndexCount(); 49 for (int i = 0; i < n; i++) { 50 int attr = a.getIndex(i); 51 if (attr == R.styleable.UserAvatarView_avatarPadding) { 52 setAvatarPadding(a.getDimension(attr, 0)); 53 } else if (attr == R.styleable.UserAvatarView_frameWidth) { 54 setFrameWidth(a.getDimension(attr, 0)); 55 } else if (attr == R.styleable.UserAvatarView_framePadding) { 56 setFramePadding(a.getDimension(attr, 0)); 57 } else if (attr == R.styleable.UserAvatarView_frameColor) { 58 setFrameColor(a.getColorStateList(attr)); 59 } else if (attr == R.styleable.UserAvatarView_badgeDiameter) { 60 setBadgeDiameter(a.getDimension(attr, 0)); 61 } else if (attr == R.styleable.UserAvatarView_badgeMargin) { 62 setBadgeMargin(a.getDimension(attr, 0)); 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 Preconditions.checkArgument(!(d instanceof UserIconDrawable), 125 "Recursively adding UserIconDrawable: %s", d); 126 mDrawable.setIconDrawable(d); 127 mDrawable.setBadge(null); 128 } 129 setDrawableWithBadge(Drawable d, int userId)130 public void setDrawableWithBadge(Drawable d, int userId) { 131 Preconditions.checkArgument(!(d instanceof UserIconDrawable), 132 "Recursively adding UserIconDrawable: %s", d); 133 mDrawable.setIconDrawable(d); 134 mDrawable.setBadgeIfManagedUser(getContext(), userId); 135 } 136 setDrawableWithBadge(Drawable d)137 public void setDrawableWithBadge(Drawable d) { 138 Preconditions.checkArgument(!(d instanceof UserIconDrawable), 139 "Recursively adding UserIconDrawable: %s", d); 140 mDrawable.setIconDrawable(d); 141 mDrawable.setBadgeIfManagedDevice(getContext()); 142 } 143 getUserIconDrawable()144 public UserIconDrawable getUserIconDrawable() { 145 return mDrawable; 146 } 147 } 148