Permalink
Browse files

Battery icon: allow to set a custom charging color [1/2]

Colorize both battery icon and bolt with the custom
charging color the user want.

Take care of darkintensity for white statusbar when needed.

Change-Id: I55c7e319a3d5a5abdd8627b9b7d36f0be90e3084
  • Loading branch information...
ezio84 authored and xlxfoxxlx committed Nov 25, 2016
1 parent 4d10e9c commit b731cdfcb443ae7864559717688b8725c9c401d4
@@ -7342,6 +7342,13 @@ public static boolean putFloatForUser(ContentResolver cr, String name, float val
*/
public static final String STATUS_BAR_BATTERY_STYLE_TILE = "status_bar_battery_style_tile";
/**
* battery icon color when charging
*
* @hide
*/
public static final String STATUS_BAR_CHARGE_COLOR = "status_bar_charge_color";
/**
* Whether the camera double twist gesture to flip between front and back mode should be
* enabled.
@@ -59,6 +59,8 @@
public static final String TAG = BatteryMeterDrawable.class.getSimpleName();
private static final String STATUS_BAR_SHOW_BATTERY_PERCENT =
Settings.Secure.STATUS_BAR_SHOW_BATTERY_PERCENT;
private static final String STATUS_BAR_CHARGE_COLOR =
Settings.Secure.STATUS_BAR_CHARGE_COLOR;
private static final boolean SINGLE_DIGIT_PERCENT = false;
@@ -194,6 +196,7 @@ public BatteryMeterDrawable(Context context, Handler handler, int frameColor, in
R.fraction.battery_subpixel_smoothing_left, 1, 1);
mSubpixelSmoothingRight = context.getResources().getFraction(
R.fraction.battery_subpixel_smoothing_right, 1, 1);
mChargeColor = mContext.getResources().getColor(R.color.batterymeter_charge_color);
loadBatteryDrawables(res, style);
@@ -260,7 +263,11 @@ public void startListening() {
mContext.getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.STATUS_BAR_SHOW_BATTERY_PERCENT),
false, mSettingObserver);
mContext.getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.STATUS_BAR_CHARGE_COLOR),
false, mSettingObserver);
updateShowPercent();
updateChargeColor();
mBatteryController.addStateChangedCallback(this);
}
@@ -365,6 +372,17 @@ private void updateShowPercent() {
Settings.Secure.STATUS_BAR_SHOW_BATTERY_PERCENT, 0) == 1;
}
private void updateChargeColor() {
mChargeColor = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.STATUS_BAR_CHARGE_COLOR,
mContext.getResources().getColor(R.color.batterymeter_charge_color));
}
private int updateDarkDensityChargeColor() {
updateChargeColor();
return mChargeColor;
}
private int getColorForLevel(int percent) {
return getColorForLevel(percent, false);
}
@@ -385,26 +403,30 @@ private int getColorForLevel(int percent, boolean isChargeLevel) {
}
}
}
// If we are in power save mode, always use the normal color.
if (mPowerSaveEnabled) {
return mColors[mColors.length - 1];
}
int thresh = 0;
int color = 0;
for (int i = 0; i < mColors.length; i += 2) {
thresh = mColors[i];
color = mColors[i + 1];
if (percent <= thresh) {
// Respect tinting for "normal" level
if (i == mColors.length - 2) {
return mIconTint;
} else {
return color;
if (mPluggedIn) {
int chargeColor = mChargeColor;
return chargeColor;
} else {
// If we are in power save mode, always use the normal color.
if (mPowerSaveEnabled) {
return mColors[mColors.length-1];
}
int thresh, color = 0;
for (int i=0; i<mColors.length; i+=2) {
thresh = mColors[i];
color = mColors[i+1];
if (percent <= thresh) {
// Respect tinting for "normal" level
if (i == mColors.length-2) {
return mIconTint;
} else {
return color;
}
}
}
return color;
}
return color;
}
public void animateSolidBattery(int level, boolean pluggedIn, boolean charging) {
@@ -445,8 +467,13 @@ public void setDarkIntensity(float darkIntensity) {
mCurrentBackgroundColor = getBackgroundColor(darkIntensity);
mCurrentFillColor = getFillColor(darkIntensity);
mIconTint = mCurrentFillColor;
// Make bolt fully opaque for increased visibility
mBoltDrawable.setTint(0xff000000 | mCurrentFillColor);
if (darkIntensity == 0f) {
updateChargeColor();
mBoltDrawable.setTint(0xff000000 | mChargeColor);
} else {
mChargeColor = mCurrentFillColor;
mBoltDrawable.setTint(0xff000000 | mCurrentFillColor);
}
mFrameDrawable.setTint(mCurrentBackgroundColor);
updateBoltDrawableLayer(mBatteryDrawable, mBoltDrawable);
invalidateSelf();
@@ -499,6 +526,7 @@ public SettingObserver() {
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
updateShowPercent();
updateChargeColor();
postInvalidate();
}
}
@@ -602,6 +630,11 @@ private int getBoltColor() {
if (mBoltOverlay) {
return mContext.getResources().getColor(mStyle == BATTERY_STYLE_CIRCLE ? R.color.batterymeter_bolt_color : R.color.system_primary_color);
}
if (mStyle == BATTERY_STYLE_CIRCLE) {
updateChargeColor();
int chargeColor = mChargeColor;
return chargeColor;
}
return mContext.getResources().getColor(R.color.batterymeter_bolt_color);
}
@@ -614,6 +647,7 @@ private void init() {
final float widthDiv2 = mWidth / 2f;
// text size is width / 2 - 2dp for wiggle room
final float textSize;
switch(mStyle) {
case 2:
@@ -626,6 +660,7 @@ private void init() {
textSize = widthDiv2;
break;
}
mTextAndBoltPaint.setTextSize(textSize);
mWarningTextPaint.setTextSize(textSize);
@@ -33,6 +33,8 @@
private static final String STATUS_BAR_BATTERY_STYLE =
Settings.Secure.STATUS_BAR_BATTERY_STYLE;
private static final String STATUS_BAR_CHARGE_COLOR =
Settings.Secure.STATUS_BAR_CHARGE_COLOR;
private BatteryMeterDrawable mDrawable;
private final String mSlotBattery;
@@ -80,6 +82,8 @@ public boolean hasOverlappingRendering() {
public void onTuningChanged(String key, String newValue) {
if (STATUS_BAR_BATTERY_STYLE.equals(key)) {
updateBatteryStyle(newValue);
} else if (STATUS_BAR_CHARGE_COLOR.equals(key)) {
updateBoltColor();
}
}
@@ -89,7 +93,7 @@ public void onAttachedToWindow() {
mBatteryController.addStateChangedCallback(this);
mDrawable.startListening();
TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST,
STATUS_BAR_BATTERY_STYLE);
STATUS_BAR_BATTERY_STYLE, STATUS_BAR_CHARGE_COLOR);
}
@Override
@@ -141,6 +145,19 @@ private void updateBatteryStyle(String styleStr) {
requestLayout();
}
private void updateBoltColor() {
final int style = Settings.Secure.getInt(getContext().getContentResolver(), STATUS_BAR_BATTERY_STYLE, 0);
if (style == BatteryMeterDrawable.BATTERY_STYLE_TEXT || style == BatteryMeterDrawable.BATTERY_STYLE_HIDDEN) {
return;
} else {
mDrawable = new BatteryMeterDrawable(mContext, new Handler(), mFrameColor, style);
setImageDrawable(mDrawable);
setVisibility(View.VISIBLE);
restoreDrawableAttributes();
requestLayout();
}
}
private void restoreDrawableAttributes() {
mDrawable.setBatteryController(mBatteryController);
mDrawable.startListening();

0 comments on commit b731cdf

Please sign in to comment.