Permalink
Browse files

Battery tile bolt/text should not be clear

This was kind of tricky because for the statusbar, the color of
text/bolt is only set for circle style (PorterDuff.Mode OVERLAY).
All other styles simply set it to clear (PorterDuff.Mode CLEAR).
This poses issues in the QS tile because the background shown is
the app currently open.
Setting all styles to OVERLAY doesn't work either, because that
would cause the statusbar battery icon to not be consistent with
the rest of the statusbar icons.
This was solved by allowing to force blendMode to OVERLAY, so
that the QS tile could force it, without affecting the statusbar
icon. Color was then set to either white for circle style, or
the QS panel background color for all other styles.

Change-Id: I5d9bfe9f77af0082247e1053187245425b2c33ae
  • Loading branch information...
tsubus authored and xlxfoxxlx committed Nov 20, 2016
1 parent ec64977 commit 14cda8136c4fd285f2282d0bc178019b064e276f
@@ -91,6 +91,8 @@
private final int mCriticalLevel;
private int mChargeColor;
private int mStyle;
private boolean mBoltOverlay;
private final Path mBoltPath = new Path();
private final Path mPlusPath = new Path();
@@ -157,13 +159,19 @@ public void run() {
public BatteryMeterDrawable(Context context, Handler handler, int frameColor) {
// Portrait is the default drawable style
this(context, handler, frameColor, BATTERY_STYLE_PORTRAIT);
this(context, handler, frameColor, BATTERY_STYLE_PORTRAIT, false);
}
public BatteryMeterDrawable(Context context, Handler handler, int frameColor, int style) {
this(context, handler, frameColor, style, false);
}
public BatteryMeterDrawable(Context context, Handler handler, int frameColor, int style, boolean boltOverlay) {
mContext = context;
mHandler = handler;
mStyle = style;
mBoltOverlay = boltOverlay;
final Resources res = context.getResources();
TypedArray levels = res.obtainTypedArray(R.array.batterymeter_color_levels);
TypedArray colors = res.obtainTypedArray(R.array.batterymeter_color_values);
@@ -196,8 +204,11 @@ public BatteryMeterDrawable(Context context, Handler handler, int frameColor, in
if (resId != 0) {
TypedArray a = mContext.obtainStyledAttributes(resId, attrs);
mTextGravity = a.getInt(0, Gravity.CENTER);
xferMode = PorterDuff.intToMode(a.getInt(1, PorterDuff.modeToInt(PorterDuff.Mode.XOR)));
a.recycle();
if (mBoltOverlay) {
xferMode = PorterDuff.Mode.OVERLAY;
} else {
xferMode = PorterDuff.intToMode(a.getInt(1, PorterDuff.modeToInt(PorterDuff.Mode.XOR)));
}
} else {
mTextGravity = Gravity.CENTER;
}
@@ -207,8 +218,7 @@ public BatteryMeterDrawable(Context context, Handler handler, int frameColor, in
mTextAndBoltPaint.setTypeface(font);
mTextAndBoltPaint.setTextAlign(getPaintAlignmentFromGravity(mTextGravity));
mTextAndBoltPaint.setXfermode(new PorterDuffXfermode(xferMode));
mTextAndBoltPaint.setColor(mCurrentFillColor != 0
? mCurrentFillColor : res.getColor(R.color.batterymeter_bolt_color));
mTextAndBoltPaint.setColor(mBoltOverlay || mCurrentFillColor == 0 ? getBoltColor() : mCurrentFillColor);
mWarningTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mWarningTextPaint.setColor(mColors[1]);
@@ -356,7 +366,25 @@ private void updateShowPercent() {
}
private int getColorForLevel(int percent) {
return getColorForLevel(percent, false);
}
private int getColorForLevel(int percent, boolean isChargeLevel) {
if (mBoltOverlay) {
if (mPowerSaveEnabled || percent > mColors[0]) {
if (isChargeLevel) {
return mColors[mColors.length-1];
} else {
return getBoltColor();
}
} else {
if (mStyle == BATTERY_STYLE_CIRCLE && !mPluggedIn) {
return mColors[1];
} else if (!isChargeLevel) {
return getBoltColor();
}
}
}
// If we are in power save mode, always use the normal color.
if (mPowerSaveEnabled) {
return mColors[mColors.length - 1];
@@ -490,6 +518,7 @@ private void loadBatteryDrawables(Resources res, int style) {
final Drawable levelDrawable = mBatteryDrawable.findDrawableByLayerId(R.id.battery_fill);
mLevelDrawable = new StopMotionVectorDrawable(levelDrawable);
mBoltDrawable = mBatteryDrawable.findDrawableByLayerId(R.id.battery_charge_indicator);
mBoltDrawable.setTint(getBoltColor());
}
private void checkBatteryMeterDrawableValid(Resources res, int style) {
@@ -569,6 +598,13 @@ private int getBatteryDrawableStyleResourceForStyle(final int style) {
}
}
private int getBoltColor() {
if (mBoltOverlay) {
return mContext.getResources().getColor(mStyle == BATTERY_STYLE_CIRCLE ? R.color.batterymeter_bolt_color : R.color.system_primary_color);
}
return mContext.getResources().getColor(R.color.batterymeter_bolt_color);
}
/**
* Initializes all size dependent variables
*/
@@ -634,6 +670,9 @@ private void updateBoltDrawableLayer(LayerDrawable batteryDrawable, Drawable bol
newBoltDrawable.setBounds(bounds);
}
newBoltDrawable.getPaint().set(mTextAndBoltPaint);
if (mBoltOverlay) {
newBoltDrawable.setTint(getBoltColor());
}
batteryDrawable.setDrawableByLayerId(R.id.battery_charge_indicator, newBoltDrawable);
}
@@ -671,14 +710,17 @@ private void drawBattery(Canvas canvas) {
// has been set to clear. Clear always clears regardless of alpha level ;)
final BitmapDrawable bd = (BitmapDrawable) d;
bd.getPaint().set(mPluggedIn ? mTextAndBoltPaint : mClearPaint);
if (mBoltOverlay) {
mBoltDrawable.setTint(getBoltColor());
}
} else {
d.setAlpha(mPluggedIn ? 255 : 0);
}
// Now draw the level indicator
// Set the level and tint color of the fill drawable
mLevelDrawable.setCurrentFraction(level / 100f);
mLevelDrawable.setTint(getColorForLevel(level));
mLevelDrawable.setTint(getColorForLevel(level, true));
mBatteryDrawable.draw(canvas);
// If chosen by options, draw percentage text in the middle
@@ -695,6 +737,9 @@ private void drawPercentageText(Canvas canvas) {
String pctText = String.valueOf(SINGLE_DIGIT_PERCENT ? (level / 10) : level);
mTextAndBoltPaint.setColor(getColorForLevel(level));
canvas.drawText(pctText, mTextX, mTextY, mTextAndBoltPaint);
if (mBoltOverlay) {
mBoltDrawable.setTint(getBoltColor());
}
} else if (level <= mCriticalLevel) {
// Draw the warning text
canvas.drawText(mWarningString, mTextX, mTextY, mWarningTextPaint);
@@ -148,7 +148,7 @@ public Drawable getDrawable(Context context) {
}
BatteryMeterDrawable drawable =
new BatteryMeterDrawable(context, new Handler(Looper.getMainLooper()),
context.getColor(R.color.batterymeter_frame_color), mBatteryStyle);
context.getColor(R.color.batterymeter_frame_color), mBatteryStyle, true);
drawable.onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
drawable.onPowerSaveChanged(mPowerSave);
return drawable;
@@ -194,7 +194,7 @@ public void onPowerSaveChanged(boolean isPowerSave) {
private final class BatteryDetail implements DetailAdapter, OnClickListener,
OnAttachStateChangeListener {
private final BatteryMeterDrawable mDrawable = new BatteryMeterDrawable(mHost.getContext(),
new Handler(), mHost.getContext().getColor(R.color.batterymeter_frame_color), mBatteryStyle);
new Handler(), mHost.getContext().getColor(R.color.batterymeter_frame_color), mBatteryStyle, true);
private View mCurrentView;
@Override

0 comments on commit 14cda81

Please sign in to comment.