diff --git a/res/values/attrs.xml b/res/values/attrs.xml index ce1573e8354..96718cf86f5 100644 --- a/res/values/attrs.xml +++ b/res/values/attrs.xml @@ -167,4 +167,12 @@ + + + + + + + + diff --git a/src/com/android/settings/widget/DonutView.java b/src/com/android/settings/widget/DonutView.java index b50a50a5f7f..d7366166af7 100644 --- a/src/com/android/settings/widget/DonutView.java +++ b/src/com/android/settings/widget/DonutView.java @@ -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.setColorFilter(mAccentColorFilter); + 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,7 +121,9 @@ public class DonutView extends View { protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawDonut(canvas); - drawInnerText(canvas); + if (mShowPercentString) { + drawInnerText(canvas); + } } private void drawDonut(Canvas canvas) { @@ -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();