1package eu.siacs.conversations.ui.widget;
2
3import android.content.Context;
4import android.content.res.TypedArray;
5import android.graphics.Canvas;
6import android.graphics.Color;
7import android.graphics.Paint;
8import android.graphics.Typeface;
9import android.util.AttributeSet;
10import android.view.View;
11
12import androidx.annotation.NonNull;
13import androidx.core.content.ContextCompat;
14
15import eu.siacs.conversations.R;
16
17public class UnreadCountCustomView extends View {
18
19 private int unreadCount;
20 private Paint paint, textPaint;
21 private int backgroundColor = 0xff326130;
22 private int textColor = Color.WHITE;
23
24 public UnreadCountCustomView(Context context) {
25 super(context);
26 init();
27 }
28
29 public UnreadCountCustomView(Context context, AttributeSet attrs) {
30 super(context, attrs);
31 initXMLAttrs(context, attrs);
32 init();
33 }
34
35 public UnreadCountCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
36 super(context, attrs, defStyleAttr);
37 initXMLAttrs(context, attrs);
38 init();
39 }
40
41 private void initXMLAttrs(Context context, AttributeSet attrs) {
42 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UnreadCountCustomView);
43 setBackgroundColor(a.getColor(a.getIndex(0), ContextCompat.getColor(context, R.color.md_theme_light_tertiaryContainer)));
44 this.textColor = a.getColor(a.getIndex(1),ContextCompat.getColor(context, R.color.md_theme_light_onTertiaryContainer));
45 a.recycle();
46 }
47
48 void init() {
49 paint = new Paint();
50 paint.setColor(backgroundColor);
51 paint.setAntiAlias(true);
52 textPaint = new Paint();
53 textPaint.setColor(textColor);
54 textPaint.setTextAlign(Paint.Align.CENTER);
55 textPaint.setAntiAlias(true);
56 textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
57 }
58
59 @Override
60 protected void onDraw(@NonNull Canvas canvas) {
61 super.onDraw(canvas);
62 float midx = canvas.getWidth() / 2.0f;
63 float midy = canvas.getHeight() / 2.0f;
64 float radius = Math.min(canvas.getWidth(), canvas.getHeight()) / 2.0f;
65 float textOffset = canvas.getWidth() / 6.0f;
66 textPaint.setTextSize(0.95f * radius);
67 canvas.drawCircle(midx, midy, radius * 0.94f, paint);
68 canvas.drawText(unreadCount > 999 ? "\u221E" : String.valueOf(unreadCount), midx, midy + textOffset, textPaint);
69
70 }
71
72 public void setUnreadCount(int unreadCount) {
73 this.unreadCount = unreadCount;
74 invalidate();
75 }
76
77 public void setBackgroundColor(int backgroundColor) {
78 this.backgroundColor = backgroundColor;
79 }
80}