1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.drawable;
import android.annotation.ColorInt;
import android.annotation.DrawableRes;
import android.annotation.NonNull;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import com.android.settingslib.R;
/**
* Converts the user avatar icon to a circularly clipped one with an optional badge and frame
*/
public class UserIconDrawable extends Drawable implements Drawable.Callback {
private Drawable mUserDrawable;
private Bitmap mUserIcon;
private Bitmap mBitmap; // baked representation. Required for transparent border around badge
private final Paint mIconPaint = new Paint();
private final Paint mPaint = new Paint();
private final Matrix mIconMatrix = new Matrix();
private float mIntrinsicRadius;
private float mDisplayRadius;
private float mPadding = 0;
private int mSize = 0; // custom "intrinsic" size for this drawable if non-zero
private boolean mInvalidated = true;
private ColorStateList mTintColor = null;
private PorterDuff.Mode mTintMode = PorterDuff.Mode.SRC_ATOP;
private float mFrameWidth;
private float mFramePadding;
private ColorStateList mFrameColor = null;
private Paint mFramePaint;
private Drawable mBadge;
private Paint mClearPaint;
private float mBadgeRadius;
private float mBadgeMargin;
/**
* Gets the system default managed-user badge as a drawable. This drawable is tint-able.
* For badging purpose, consider
* {@link android.content.pm.PackageManager#getUserBadgedDrawableForDensity(Drawable, UserHandle, Rect, int)}.
*
* @param context
* @return drawable containing just the badge
*/
public static Drawable getManagedUserDrawable(Context context) {
return getDrawableForDisplayDensity
(context, com.android.internal.R.drawable.ic_corp_user_badge);
}
private static Drawable getDrawableForDisplayDensity(
Context context, @DrawableRes int drawable) {
int density = context.getResources().getDisplayMetrics().densityDpi;
return context.getResources().getDrawableForDensity(
drawable, density, context.getTheme());
}
/**
* Gets the preferred list-item size of this drawable.
* @param context
* @return size in pixels
*/
public static int getSizeForList(Context context) {
return (int) context.getResources().getDimension(R.dimen.circle_avatar_size);
}
public UserIconDrawable() {
this(0);
}
/**
* Use this constructor if the drawable is intended to be placed in listviews
* @param intrinsicSize if 0, the intrinsic size will come from the icon itself
*/
public UserIconDrawable(int intrinsicSize) {
super();
mIconPaint.setAntiAlias(true);
mIconPaint.setFilterBitmap(true);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
if (intrinsicSize > 0) {
setBounds(0, 0, intrinsicSize, intrinsicSize);
setIntrinsicSize(intrinsicSize);
}
setIcon(null);
}
public UserIconDrawable setIcon(Bitmap icon) {
if (mUserDrawable != null) {
mUserDrawable.setCallback(null);
mUserDrawable = null;
}
mUserIcon = icon;
if (mUserIcon == null) {
mIconPaint.setShader(null);
mBitmap = null;
} else {
mIconPaint.setShader(new BitmapShader(icon, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP));
}
onBoundsChange(getBounds());
return this;
}
public UserIconDrawable setIconDrawable(Drawable icon) {
if (mUserDrawable != null) {
mUserDrawable.setCallback(null);
}
mUserIcon = null;
mUserDrawable = icon;
if (mUserDrawable == null) {
mBitmap = null;
} else {
mUserDrawable.setCallback(this);
}
onBoundsChange(getBounds());
return this;
}
public UserIconDrawable setBadge(Drawable badge) {
mBadge = badge;
if (mBadge != null) {
if (mClearPaint == null) {
mClearPaint = new Paint();
mClearPaint.setAntiAlias(true);
mClearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mClearPaint.setStyle(Paint.Style.FILL);
}
// update metrics
onBoundsChange(getBounds());
} else {
invalidateSelf();
}
return this;
}
public UserIconDrawable setBadgeIfManagedUser(Context context, int userId) {
Drawable badge = null;
if (userId != UserHandle.USER_NULL) {
boolean isManaged = context.getSystemService(DevicePolicyManager.class)
.getProfileOwnerAsUser(userId) != null;
if (isManaged) {
badge = getDrawableForDisplayDensity(
context, com.android.internal.R.drawable.ic_corp_badge_case);
}
}
return setBadge(badge);
}
/**
* Sets the managed badge to this user icon if the device has a device owner.
*/
public UserIconDrawable setBadgeIfManagedDevice(Context context) {
Drawable badge = null;
boolean deviceOwnerExists = context.getSystemService(DevicePolicyManager.class)
.getDeviceOwnerComponentOnAnyUser() != null;
if (deviceOwnerExists) {
badge = getDrawableForDisplayDensity(
context, com.android.internal.R.drawable.ic_corp_badge_case);
}
return setBadge(badge);
}
public void setBadgeRadius(float radius) {
mBadgeRadius = radius;
onBoundsChange(getBounds());
}
public void setBadgeMargin(float margin) {
mBadgeMargin = margin;
onBoundsChange(getBounds());
}
/**
* Sets global padding of icon/frame. Doesn't effect the badge.
* @param padding
*/
public void setPadding(float padding) {
mPadding = padding;
onBoundsChange(getBounds());
}
private void initFramePaint() {
if (mFramePaint == null) {
mFramePaint = new Paint();
mFramePaint.setStyle(Paint.Style.STROKE);
mFramePaint.setAntiAlias(true);
}
}
public void setFrameWidth(float width) {
initFramePaint();
mFrameWidth = width;
mFramePaint.setStrokeWidth(width);
onBoundsChange(getBounds());
}
public void setFramePadding(float padding) {
initFramePaint();
mFramePadding = padding;
onBoundsChange(getBounds());
}
public void setFrameColor(int color) {
initFramePaint();
mFramePaint.setColor(color);
invalidateSelf();
}
public void setFrameColor(ColorStateList colorList) {
initFramePaint();
mFrameColor = colorList;
invalidateSelf();
}
/**
* This sets the "intrinsic" size of this drawable. Useful for views which use the drawable's
* intrinsic size for layout. It is independent of the bounds.
* @param size if 0, the intrinsic size will be set to the displayed icon's size
*/
public void setIntrinsicSize(int size) {
mSize = size;
}
@Override
public void draw(Canvas canvas) {
if (mInvalidated) {
rebake();
}
if (mBitmap != null) {
if (mTintColor == null) {
mPaint.setColorFilter(null);
} else {
int color = mTintColor.getColorForState(getState(), mTintColor.getDefaultColor());
if (shouldUpdateColorFilter(color, mTintMode)) {
mPaint.setColorFilter(new PorterDuffColorFilter(color, mTintMode));
}
}
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
}
}
private boolean shouldUpdateColorFilter(@ColorInt int color, PorterDuff.Mode mode) {
ColorFilter colorFilter = mPaint.getColorFilter();
if (colorFilter instanceof PorterDuffColorFilter) {
PorterDuffColorFilter porterDuffColorFilter = (PorterDuffColorFilter) colorFilter;
int currentColor = porterDuffColorFilter.getColor();
PorterDuff.Mode currentMode = porterDuffColorFilter.getMode();
return currentColor != color || currentMode != mode;
} else {
return true;
}
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
super.invalidateSelf();
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public void setTintList(ColorStateList tintList) {
mTintColor = tintList;
super.invalidateSelf();
}
@Override
public void setTintMode(@NonNull PorterDuff.Mode mode) {
mTintMode = mode;
super.invalidateSelf();
}
@Override
public ConstantState getConstantState() {
return new BitmapDrawable(mBitmap).getConstantState();
}
/**
* This 'bakes' the current state of this icon into a bitmap and removes/recycles the source
* bitmap/drawable. Use this when no more changes will be made and an intrinsic size is set.
* This effectively turns this into a static drawable.
*/
public UserIconDrawable bake() {
if (mSize <= 0) {
throw new IllegalStateException("Baking requires an explicit intrinsic size");
}
onBoundsChange(new Rect(0, 0, mSize, mSize));
rebake();
mFrameColor = null;
mFramePaint = null;
mClearPaint = null;
if (mUserDrawable != null) {
mUserDrawable.setCallback(null);
mUserDrawable = null;
} else if (mUserIcon != null) {
mUserIcon.recycle();
mUserIcon = null;
}
return this;
}
private void rebake() {
mInvalidated = false;
if (mBitmap == null || (mUserDrawable == null && mUserIcon == null)) {
return;
}
final Canvas canvas = new Canvas(mBitmap);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
if(mUserDrawable != null) {
mUserDrawable.draw(canvas);
} else if (mUserIcon != null) {
int saveId = canvas.save();
canvas.concat(mIconMatrix);
canvas.drawCircle(mUserIcon.getWidth() * 0.5f, mUserIcon.getHeight() * 0.5f,
mIntrinsicRadius, mIconPaint);
canvas.restoreToCount(saveId);
}
if (mFrameColor != null) {
mFramePaint.setColor(mFrameColor.getColorForState(getState(), Color.TRANSPARENT));
}
if ((mFrameWidth + mFramePadding) > 0.001f) {
float radius = mDisplayRadius - mPadding - mFrameWidth * 0.5f;
canvas.drawCircle(getBounds().exactCenterX(), getBounds().exactCenterY(),
radius, mFramePaint);
}
if ((mBadge != null) && (mBadgeRadius > 0.001f)) {
final float badgeDiameter = mBadgeRadius * 2f;
final float badgeTop = mBitmap.getHeight() - badgeDiameter;
float badgeLeft = mBitmap.getWidth() - badgeDiameter;
mBadge.setBounds((int) badgeLeft, (int) badgeTop,
(int) (badgeLeft + badgeDiameter), (int) (badgeTop + badgeDiameter));
final float borderRadius = mBadge.getBounds().width() * 0.5f + mBadgeMargin;
canvas.drawCircle(badgeLeft + mBadgeRadius, badgeTop + mBadgeRadius,
borderRadius, mClearPaint);
mBadge.draw(canvas);
}
}
@Override
protected void onBoundsChange(Rect bounds) {
if (bounds.isEmpty() || (mUserIcon == null && mUserDrawable == null)) {
return;
}
// re-create bitmap if applicable
float newDisplayRadius = Math.min(bounds.width(), bounds.height()) * 0.5f;
int size = (int) (newDisplayRadius * 2);
if (mBitmap == null || size != ((int) (mDisplayRadius * 2))) {
mDisplayRadius = newDisplayRadius;
if (mBitmap != null) {
mBitmap.recycle();
}
mBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
// update metrics
mDisplayRadius = Math.min(bounds.width(), bounds.height()) * 0.5f;
final float iconRadius = mDisplayRadius - mFrameWidth - mFramePadding - mPadding;
RectF dstRect = new RectF(bounds.exactCenterX() - iconRadius,
bounds.exactCenterY() - iconRadius,
bounds.exactCenterX() + iconRadius,
bounds.exactCenterY() + iconRadius);
if (mUserDrawable != null) {
Rect rounded = new Rect();
dstRect.round(rounded);
mIntrinsicRadius = Math.min(mUserDrawable.getIntrinsicWidth(),
mUserDrawable.getIntrinsicHeight()) * 0.5f;
mUserDrawable.setBounds(rounded);
} else if (mUserIcon != null) {
// Build square-to-square transformation matrix
final float iconCX = mUserIcon.getWidth() * 0.5f;
final float iconCY = mUserIcon.getHeight() * 0.5f;
mIntrinsicRadius = Math.min(iconCX, iconCY);
RectF srcRect = new RectF(iconCX - mIntrinsicRadius, iconCY - mIntrinsicRadius,
iconCX + mIntrinsicRadius, iconCY + mIntrinsicRadius);
mIconMatrix.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.FILL);
}
invalidateSelf();
}
@Override
public void invalidateSelf() {
super.invalidateSelf();
mInvalidated = true;
}
@Override
public boolean isStateful() {
return mFrameColor != null && mFrameColor.isStateful();
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public int getIntrinsicWidth() {
return (mSize <= 0 ? (int) mIntrinsicRadius * 2 : mSize);
}
@Override
public int getIntrinsicHeight() {
return getIntrinsicWidth();
}
@Override
public void invalidateDrawable(@NonNull Drawable who) {
invalidateSelf();
}
@Override
public void scheduleDrawable(@NonNull Drawable who, @NonNull Runnable what, long when) {
scheduleSelf(what, when);
}
@Override
public void unscheduleDrawable(@NonNull Drawable who, @NonNull Runnable what) {
unscheduleSelf(what);
}
}
|