summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/helper/CountDrawable.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/app/fedilab/android/helper/CountDrawable.java')
-rw-r--r--app/src/main/java/app/fedilab/android/helper/CountDrawable.java116
1 files changed, 116 insertions, 0 deletions
diff --git a/app/src/main/java/app/fedilab/android/helper/CountDrawable.java b/app/src/main/java/app/fedilab/android/helper/CountDrawable.java
new file mode 100644
index 000000000..3688109ea
--- /dev/null
+++ b/app/src/main/java/app/fedilab/android/helper/CountDrawable.java
@@ -0,0 +1,116 @@
+package app.fedilab.android.helper;
+/* Copyright 2022 Thomas Schneider
+ *
+ * This file is a part of Fedilab
+ *
+ * This program is free software; you can redistribute it and/or modify it under the terms of the
+ * GNU General Public License as published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
+ * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+ * Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with Fedilab; if not,
+ * see <http://www.gnu.org/licenses>. */
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.ColorFilter;
+import android.graphics.Paint;
+import android.graphics.PixelFormat;
+import android.graphics.Rect;
+import android.graphics.Typeface;
+import android.graphics.drawable.Drawable;
+
+import androidx.annotation.NonNull;
+import androidx.core.content.ContextCompat;
+
+import app.fedilab.android.R;
+
+
+public class CountDrawable extends Drawable {
+
+ private final Paint mBadgePaint;
+ private final Paint mTextPaint;
+ private final Rect mTxtRect = new Rect();
+
+ private String mCount = "";
+ private boolean mWillDraw;
+
+ public CountDrawable(Context context) {
+ float mTextSize = context.getResources().getDimension(R.dimen.badge_count_textsize);
+
+ mBadgePaint = new Paint();
+ mBadgePaint.setColor(ContextCompat.getColor(context, R.color.red_1));
+ mBadgePaint.setAntiAlias(true);
+ mBadgePaint.setStyle(Paint.Style.FILL);
+
+ mTextPaint = new Paint();
+ mTextPaint.setColor(Color.WHITE);
+ mTextPaint.setTypeface(Typeface.DEFAULT);
+ mTextPaint.setTextSize(mTextSize);
+ mTextPaint.setAntiAlias(true);
+ mTextPaint.setTextAlign(Paint.Align.CENTER);
+ }
+
+ @Override
+ public void draw(@NonNull Canvas canvas) {
+
+ if (!mWillDraw) {
+ return;
+ }
+ Rect bounds = getBounds();
+ float width = bounds.right - bounds.left;
+ float height = bounds.bottom - bounds.top;
+
+ // Position the badge in the top-right quadrant of the icon.
+
+ /*Using Math.max rather than Math.min */
+
+ float radius = ((Math.max(width, height) / 2)) / 2;
+ float centerX = (width - radius - 1) + 5;
+ float centerY = radius - 5;
+ if (mCount.length() <= 2) {
+ // Draw badge circle.
+ canvas.drawCircle(centerX, centerY, (int) (radius + 5.5), mBadgePaint);
+ } else {
+ canvas.drawCircle(centerX, centerY, (int) (radius + 6.5), mBadgePaint);
+ }
+ // Draw badge count text inside the circle.
+ mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
+ float textHeight = mTxtRect.bottom - mTxtRect.top;
+ float textY = centerY + (textHeight / 2f);
+ if (mCount.length() > 2)
+ canvas.drawText("99+", centerX, textY, mTextPaint);
+ else
+ canvas.drawText(mCount, centerX, textY, mTextPaint);
+ }
+
+ /*
+ Sets the count (i.e notifications) to display.
+ */
+ public void setCount(String count) {
+ mCount = count;
+
+ // Only draw a badge if there are notifications.
+ mWillDraw = !count.equalsIgnoreCase("0");
+ invalidateSelf();
+ }
+
+ @Override
+ public void setAlpha(int alpha) {
+ // do nothing
+ }
+
+ @Override
+ public void setColorFilter(ColorFilter cf) {
+ // do nothing
+ }
+
+ @Override
+ public int getOpacity() {
+ return PixelFormat.UNKNOWN;
+ }
+}