Permalink
Browse files

Battery icon: let disable force text outside icon when charging [1/2]

When text outside the icon is disabled, remove the bolt and put the text
inside the icon.

Also cleanup the code a bit.

Change-Id: Ib7b079c8a0a6efbea184943f4a7f1fb945272885
  • Loading branch information...
ezio84 authored and xlxfoxxlx committed Nov 25, 2016
1 parent b731cdf commit 739f8aa4ae26274d59c13af101fa31da342ae77d
@@ -7349,6 +7349,13 @@ public static boolean putFloatForUser(ContentResolver cr, String name, float val
*/
public static final String STATUS_BAR_CHARGE_COLOR = "status_bar_charge_color";
/**
* Whether to force percentage text out of the battery icon when charging
*
* @hide
*/
public static final String FORCE_CHARGE_BATTERY_TEXT = "force_charge_battery_text";
/**
* Whether the camera double twist gesture to flip between front and back mode should be
* enabled.
@@ -33,11 +33,14 @@
Settings.Secure.STATUS_BAR_SHOW_BATTERY_PERCENT;
private static final String STATUS_BAR_BATTERY_STYLE =
Settings.Secure.STATUS_BAR_BATTERY_STYLE;
private static final String FORCE_CHARGE_BATTERY_TEXT =
Settings.Secure.FORCE_CHARGE_BATTERY_TEXT;
private BatteryController mBatteryController;
private boolean mRequestedVisibility;
private boolean mForceBatteryText;
private boolean mForceChargeBatteryText;
private boolean mBatteryCharging;
public BatteryLevelTextView(Context context, AttributeSet attrs) {
@@ -51,16 +54,18 @@ public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging
mBatteryCharging = charging;
if (changed) {
mForceBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
Settings.Secure.STATUS_BAR_BATTERY_STYLE, 0) == 6 ? true : false;
setVisibility(mBatteryCharging || mRequestedVisibility || mForceBatteryText ? View.VISIBLE : View.GONE);
STATUS_BAR_BATTERY_STYLE, 0) == 6 ? true : false;
mForceChargeBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
FORCE_CHARGE_BATTERY_TEXT, 1) == 1 ? true : false;
setVisibility((mBatteryCharging && mForceChargeBatteryText) || mRequestedVisibility || mForceBatteryText ? View.VISIBLE : View.GONE);
}
}
public void setBatteryController(BatteryController batteryController) {
mBatteryController = batteryController;
mBatteryController.addStateChangedCallback(this);
TunerService.get(getContext()).addTunable(this,
STATUS_BAR_SHOW_BATTERY_PERCENT, STATUS_BAR_BATTERY_STYLE);
STATUS_BAR_SHOW_BATTERY_PERCENT, STATUS_BAR_BATTERY_STYLE, FORCE_CHARGE_BATTERY_TEXT);
}
@Override
@@ -84,12 +89,18 @@ public void onTuningChanged(String key, String newValue) {
case STATUS_BAR_SHOW_BATTERY_PERCENT:
mRequestedVisibility = newValue != null && Integer.parseInt(newValue) == 2;
mForceBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
Settings.Secure.STATUS_BAR_BATTERY_STYLE, 0) == 6 ? true : false;
setVisibility(mBatteryCharging || mRequestedVisibility || mForceBatteryText ? View.VISIBLE : View.GONE);
STATUS_BAR_BATTERY_STYLE, 0) == 6 ? true : false;
mForceChargeBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
FORCE_CHARGE_BATTERY_TEXT, 1) == 1 ? true : false;
setVisibility((mBatteryCharging && mForceChargeBatteryText) || mRequestedVisibility || mForceBatteryText ? View.VISIBLE : View.GONE);
break;
case STATUS_BAR_BATTERY_STYLE:
final int value = newValue == null ?
BatteryMeterDrawable.BATTERY_STYLE_PORTRAIT : Integer.parseInt(newValue);
mForceBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
STATUS_BAR_BATTERY_STYLE, 0) == 6 ? true : false;
mForceChargeBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
FORCE_CHARGE_BATTERY_TEXT, 1) == 1 ? true : false;
switch (value) {
case BatteryMeterDrawable.BATTERY_STYLE_TEXT:
setVisibility(View.VISIBLE);
@@ -98,10 +109,15 @@ public void onTuningChanged(String key, String newValue) {
setVisibility(View.GONE);
break;
default:
setVisibility(mBatteryCharging || mRequestedVisibility || mForceBatteryText ? View.VISIBLE : View.GONE);
setVisibility((mBatteryCharging && mForceChargeBatteryText) || mRequestedVisibility || mForceBatteryText ? View.VISIBLE : View.GONE);
break;
}
break;
case FORCE_CHARGE_BATTERY_TEXT:
mForceChargeBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
FORCE_CHARGE_BATTERY_TEXT, 1) == 1 ? true : false;
setVisibility((mBatteryCharging && mForceChargeBatteryText) || mRequestedVisibility || mForceBatteryText ? View.VISIBLE : View.GONE);
break;
default:
break;
}
@@ -61,6 +61,8 @@
Settings.Secure.STATUS_BAR_SHOW_BATTERY_PERCENT;
private static final String STATUS_BAR_CHARGE_COLOR =
Settings.Secure.STATUS_BAR_CHARGE_COLOR;
private static final String FORCE_CHARGE_BATTERY_TEXT =
Settings.Secure.FORCE_CHARGE_BATTERY_TEXT;
private static final boolean SINGLE_DIGIT_PERCENT = false;
@@ -125,6 +127,7 @@
private int mLevel = -1;
private boolean mPluggedIn;
private boolean mForceChargeBatteryText;
private boolean mListening;
private static final int ADD_LEVEL = 10;
private static final int ANIM_DURATION = 500;
@@ -187,6 +190,7 @@ public BatteryMeterDrawable(Context context, Handler handler, int frameColor, in
levels.recycle();
colors.recycle();
updateShowPercent();
updateForceChargeBatteryText();
mWarningString = context.getString(R.string.battery_meter_very_low_overlay_symbol);
mCriticalLevel = mContext.getResources().getInteger(
com.android.internal.R.integer.config_criticalBatteryWarningLevel);
@@ -261,13 +265,17 @@ public int getIntrinsicWidth() {
public void startListening() {
mListening = true;
mContext.getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.STATUS_BAR_SHOW_BATTERY_PERCENT),
Settings.Secure.getUriFor(STATUS_BAR_SHOW_BATTERY_PERCENT),
false, mSettingObserver);
mContext.getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.STATUS_BAR_CHARGE_COLOR),
Settings.Secure.getUriFor(STATUS_BAR_CHARGE_COLOR),
false, mSettingObserver);
mContext.getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(FORCE_CHARGE_BATTERY_TEXT),
false, mSettingObserver);
updateShowPercent();
updateChargeColor();
updateForceChargeBatteryText();
mBatteryController.addStateChangedCallback(this);
}
@@ -369,12 +377,12 @@ public void setBounds(int left, int top, int right, int bottom) {
private void updateShowPercent() {
mShowPercent = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.STATUS_BAR_SHOW_BATTERY_PERCENT, 0) == 1;
STATUS_BAR_SHOW_BATTERY_PERCENT, 0) == 1;
}
private void updateChargeColor() {
mChargeColor = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.STATUS_BAR_CHARGE_COLOR,
STATUS_BAR_CHARGE_COLOR,
mContext.getResources().getColor(R.color.batterymeter_charge_color));
}
@@ -383,6 +391,11 @@ private int updateDarkDensityChargeColor() {
return mChargeColor;
}
private void updateForceChargeBatteryText() {
mForceChargeBatteryText = Settings.Secure.getInt(mContext.getContentResolver(),
FORCE_CHARGE_BATTERY_TEXT, 1) == 1 ? true : false;
}
private int getColorForLevel(int percent) {
return getColorForLevel(percent, false);
}
@@ -747,20 +760,20 @@ private void drawBattery(Canvas canvas) {
final int level = mLevel;
mTextAndBoltPaint.setColor(getColorForLevel(level));
updateForceChargeBatteryText();
// Make sure we don't draw the charge indicator if not plugged in
final Drawable d = mBatteryDrawable.findDrawableByLayerId(R.id.battery_charge_indicator);
if (d instanceof BitmapDrawable) {
// In case we are using a BitmapDrawable, which we should be unless something bad
// happened, we need to change the paint rather than the alpha in case the blendMode
// has been set to clear. Clear always clears regardless of alpha level ;)
final BitmapDrawable bd = (BitmapDrawable) d;
bd.getPaint().set(mPluggedIn ? mTextAndBoltPaint : mClearPaint);
bd.getPaint().set(mPluggedIn && mForceChargeBatteryText ? mTextAndBoltPaint : mClearPaint);
if (mBoltOverlay) {
mBoltDrawable.setTint(getBoltColor());
}
} else {
d.setAlpha(mPluggedIn ? 255 : 0);
d.setAlpha(mPluggedIn && mForceChargeBatteryText ? 255 : 0);
}
// Now draw the level indicator
@@ -771,7 +784,7 @@ private void drawBattery(Canvas canvas) {
// If chosen by options, draw percentage text in the middle
// Always skip percentage when 100, so layout doesnt break
if (!mPluggedIn) {
if (!mPluggedIn || (mPluggedIn && !mForceChargeBatteryText)) {
drawPercentageText(canvas);
}
}
@@ -75,6 +75,7 @@
private boolean mShowBatteryText;
private boolean mForceBatteryText;
private boolean mForceChargeBatteryText;
private ContentObserver mObserver = new ContentObserver(new Handler()) {
public void onChange(boolean selfChange, Uri uri) {
@@ -195,7 +196,7 @@ private void updateVisibilities() {
}
mBatteryLevel.setVisibility(
mBatteryCharging || mShowBatteryText || mForceBatteryText ? View.VISIBLE : View.GONE);
(mBatteryCharging && mForceChargeBatteryText) || mShowBatteryText || mForceBatteryText ? View.VISIBLE : View.GONE);
if (mCarrierLabel != null) {
if (mShowCarrierLabel == 1) {
@@ -370,6 +371,8 @@ public void onTuningChanged(String key, String newValue) {
mShowBatteryText = newValue == null ? false : Integer.parseInt(newValue) == 2;
mForceBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
Settings.Secure.STATUS_BAR_BATTERY_STYLE, 0) == 6 ? true : false;
mForceChargeBatteryText = Settings.Secure.getInt(getContext().getContentResolver(),
Settings.Secure.FORCE_CHARGE_BATTERY_TEXT, 1) == 1 ? true : false;
updateVisibilities();
}
}

0 comments on commit 739f8aa

Please sign in to comment.