Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eaceb190 authored by Rajeev Kumar's avatar Rajeev Kumar
Browse files

Add custom attributes to DonutView.

Note: These changes are required for Data balance UI.
Bug: 62349208
Test: Manual
Change-Id: I50f304580f681dc1461fb82d84ce0fede21b4b87
parent 250595ae
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -171,4 +171,12 @@
        <attr name="android:gravity" />
    </declare-styleable>

    <!-- For DonutView -->
    <declare-styleable name="DonutView">
        <attr name="meterBackgroundColor" format="color" />
        <attr name="meterConsumedColor" format="color" />
        <attr name="applyColorAccent" format="boolean" />
        <attr name="showPercentString" format="boolean" />
        <attr name="thickness" format="dimension" />
    </declare-styleable>
</resources>
+47 −12
Original line number Diff line number Diff line
@@ -17,16 +17,17 @@ package com.android.settings.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface;
import android.support.annotation.ColorRes;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.android.settings.R;
import com.android.settings.Utils;

@@ -46,6 +47,7 @@ public class DonutView extends View {
    private TextPaint mBigNumberPaint;
    private String mPercentString;
    private String mFullString;
    private boolean mShowPercentString = true;

    public DonutView(Context context) {
        super(context);
@@ -53,29 +55,50 @@ public class DonutView extends View {

    public DonutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mStrokeWidth = context.getResources().getDimension(R.dimen.storage_donut_thickness);
        final ColorFilter mAccentColorFilter =
                new PorterDuffColorFilter(
                        Utils.getColorAttr(context, android.R.attr.colorAccent),
                        PorterDuff.Mode.SRC_IN);
        int meterBackgroundColor = context.getColor(R.color.meter_background_color);
        int meterConsumedColor = Utils.getDefaultColor(mContext, R.color.meter_consumed_color);
        boolean applyColorAccent = true;
        Resources resources = context.getResources();
        mStrokeWidth = resources.getDimension(R.dimen.storage_donut_thickness);

        if (attrs != null) {
            TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.DonutView);
            meterBackgroundColor = styledAttrs.getColor(R.styleable.DonutView_meterBackgroundColor,
                    meterBackgroundColor);
            meterConsumedColor = styledAttrs.getColor(R.styleable.DonutView_meterConsumedColor,
                    meterConsumedColor);
            applyColorAccent = styledAttrs.getBoolean(R.styleable.DonutView_applyColorAccent,
                    true);
            mShowPercentString = styledAttrs.getBoolean(R.styleable.DonutView_showPercentString,
                    true);
            mStrokeWidth = styledAttrs.getDimensionPixelSize(R.styleable.DonutView_thickness,
                    (int) mStrokeWidth);
            styledAttrs.recycle();
        }

        mBackgroundCircle = new Paint();
        mBackgroundCircle.setAntiAlias(true);
        mBackgroundCircle.setStrokeCap(Paint.Cap.BUTT);
        mBackgroundCircle.setStyle(Paint.Style.STROKE);
        mBackgroundCircle.setStrokeWidth(mStrokeWidth);
        mBackgroundCircle.setColorFilter(mAccentColorFilter);
        mBackgroundCircle.setColor(context.getColor(R.color.meter_background_color));
        mBackgroundCircle.setColor(meterBackgroundColor);

        mFilledArc = new Paint();
        mFilledArc.setAntiAlias(true);
        mFilledArc.setStrokeCap(Paint.Cap.BUTT);
        mFilledArc.setStyle(Paint.Style.STROKE);
        mFilledArc.setStrokeWidth(mStrokeWidth);
        mFilledArc.setColor(Utils.getDefaultColor(mContext, R.color.meter_consumed_color));
        mFilledArc.setColor(meterConsumedColor);

        if (applyColorAccent) {
            final ColorFilter mAccentColorFilter =
                    new PorterDuffColorFilter(
                            Utils.getColorAttr(context, android.R.attr.colorAccent),
                            PorterDuff.Mode.SRC_IN);
            mBackgroundCircle.setColorFilter(mAccentColorFilter);
            mFilledArc.setColorFilter(mAccentColorFilter);
        }

        Resources resources = context.getResources();
        mTextPaint = new TextPaint();
        mTextPaint.setColor(Utils.getColorAccent(getContext()));
        mTextPaint.setAntiAlias(true);
@@ -98,8 +121,10 @@ public class DonutView extends View {
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawDonut(canvas);
        if (mShowPercentString) {
            drawInnerText(canvas);
        }
    }

    private void drawDonut(Canvas canvas) {
        canvas.drawArc(
@@ -154,6 +179,16 @@ public class DonutView extends View {
        invalidate();
    }

    public void setMeterBackgroundColor(@ColorRes int meterBackgroundColor) {
        mBackgroundCircle.setColor(meterBackgroundColor);
        invalidate();
    }

    public void setMeterConsumedColor(@ColorRes int meterConsumedColor) {
        mFilledArc.setColor(meterConsumedColor);
        invalidate();
    }

    private float getTextHeight(TextPaint paint) {
        // Technically, this should be the cap height, but I can live with the descent - ascent.
        return paint.descent() - paint.ascent();