diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 27f04ee40038b..74164811c3a18 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -2326,13 +2326,18 @@ public static void setShowGTalkServiceStatus(ContentResolver cr, boolean flag) { * Display style of the status bar battery information * 0: Display the stock battery information * 1: Display cm battery percentage - * 2: Display status bar battery - * 3: Hide the battery information + * 2: Hide the battery information + * 3: Display status bar battery * default: 0 * @hide */ public static final String STATUS_BAR_BATTERY = "status_bar_battery"; - + + /** + * Color for the status bar battery style + */ + public static final String STATUS_BAR_BATTERY_COLOR = "status_bar_battery_color"; + /** * Whether to show the clock in status bar * of the stock battery icon diff --git a/packages/SystemUI/res/drawable/battery_bar.xml b/packages/SystemUI/res/drawable/battery_bar.xml index a481c72856ebe..5c93b8abc65b6 100644 --- a/packages/SystemUI/res/drawable/battery_bar.xml +++ b/packages/SystemUI/res/drawable/battery_bar.xml @@ -16,7 +16,7 @@ - + @@ -26,15 +26,14 @@ + android:startColor="#ff7aa600" + android:endColor="#ff94d400" + android:centerColor="#ff89bf00" + android:angle="0" /> - \ No newline at end of file + diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/CmBatteryBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/CmBatteryBar.java index 58bcf7e3bfc6f..21d80a9000709 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/CmBatteryBar.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/CmBatteryBar.java @@ -7,15 +7,27 @@ import android.content.Intent; import android.content.IntentFilter; import android.database.ContentObserver; +import android.graphics.Color; +import android.graphics.PorterDuff; +import android.graphics.drawable.Animatable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.LayerDrawable; import android.os.BatteryManager; import android.os.Handler; import android.provider.Settings; +import android.text.TextUtils; import android.util.AttributeSet; import android.widget.ProgressBar; -public class CmBatteryBar extends ProgressBar { +public class CmBatteryBar extends ProgressBar implements Animatable, Runnable { - private static final String TAG = "CmBatteryBar"; + private static final String TAG = CmBatteryBar.class.getSimpleName(); + + // Total animation duration + private static final int ANIM_DURATION = 5000; // 5 seconds + + // Duration between frames of charging animation + private static final int FRAME_DURATION = ANIM_DURATION / 100; // Are we listening for actions? private boolean mAttached = false; @@ -29,9 +41,6 @@ public class CmBatteryBar extends ProgressBar { // Current "step" of charging animation private int mChargingLevel = -1; - // Duration between frames of charging animation - private int mAnimDuration = 500; - // Are we charging? private boolean mBatteryCharging = false; @@ -47,6 +56,9 @@ void observer() { ContentResolver resolver = mContext.getContentResolver(); resolver.registerContentObserver( Settings.System.getUriFor(Settings.System.STATUS_BAR_BATTERY), false, this); + resolver.registerContentObserver( + Settings.System.getUriFor(Settings.System.STATUS_BAR_BATTERY_COLOR), false, + this); } @Override @@ -55,31 +67,6 @@ public void onChange(boolean selfChange) { } } - private final Runnable onFakeTimer = new Runnable() { - @Override - public void run() { - if (mChargingLevel > -1) { - if (mChargingLevel < 20) { - mChargingLevel += mChargingLevel % 5; - } else if (mChargingLevel < 90) { - mChargingLevel += mChargingLevel % 10; - } - setProgress(mChargingLevel); - if (mChargingLevel >= 100) { - mChargingLevel = mBatteryLevel; - } else { - if (mChargingLevel < 20) { - mChargingLevel += 5; - } else { - mChargingLevel += 10; - } - } - invalidate(); - mHandler.postDelayed(onFakeTimer, mAnimDuration); - } - } - }; - public CmBatteryBar(Context context) { this(context, null); } @@ -128,59 +115,91 @@ public void onReceive(Context context, Intent intent) { mBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); mBatteryCharging = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0) == BatteryManager.BATTERY_STATUS_CHARGING; if (mBatteryCharging && mBatteryLevel < 100) { - startTimer(); - if (mBatteryLevel % 10 == 0) { - updateAnimDuration(); - } + start(); } else { - stopTimer(); + stop(); } } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { - stopTimer(); + stop(); } else if (Intent.ACTION_SCREEN_ON.equals(action)) { if (mBatteryCharging && mBatteryLevel < 100) { - startTimer(); + start(); } } } }; - private void updateAnimDuration() { - mAnimDuration = 200 + (mBatteryLevel / 10) * 50; - } - - private void startTimer() { - if (mChargingLevel == -1) { - mHandler.removeCallbacks(onFakeTimer); - updateAnimDuration(); - mChargingLevel = mBatteryLevel; - invalidate(); - mHandler.postDelayed(onFakeTimer, mAnimDuration); - } - } - - private void stopTimer() { - mHandler.removeCallbacks(onFakeTimer); - setProgress(mBatteryLevel); - mChargingLevel = -1; - invalidate(); - } - private void updateSettings() { ContentResolver resolver = mContext.getContentResolver(); mShowCmBatteryBar = (Settings.System.getInt(resolver, - Settings.System.STATUS_BAR_BATTERY, 0) == 2); + Settings.System.STATUS_BAR_BATTERY, 0) == 3); if (mShowCmBatteryBar) { setVisibility(VISIBLE); } else { setVisibility(GONE); } + Drawable d = getProgressDrawable(); + if (d instanceof LayerDrawable) { + Drawable bar = ((LayerDrawable) d) + .findDrawableByLayerId(com.android.internal.R.id.progress); + if (bar != null) { + String color = Settings.System + .getString(resolver, Settings.System.STATUS_BAR_BATTERY_COLOR); + Integer barColor = null; + if (!TextUtils.isEmpty(color)) { + try { + barColor = Color.parseColor(color); + } catch (IllegalArgumentException e) { + } + } + if (barColor != null) { + bar.setColorFilter(barColor, PorterDuff.Mode.SRC); + } else { + bar.clearColorFilter(); + } + invalidate(); + } + } + if (mBatteryCharging && mBatteryLevel < 100) { - startTimer(); + start(); } else { - stopTimer(); + stop(); + } + } + + @Override + public void run() { + mChargingLevel++; + if (mChargingLevel > 100) { + mChargingLevel = mBatteryLevel; + } + setProgress(mChargingLevel); + mHandler.postDelayed(this, FRAME_DURATION); + } + + @Override + public void start() { + if (!isRunning()) { + mHandler.removeCallbacks(this); + mChargingLevel = mBatteryLevel; + mHandler.postDelayed(this, FRAME_DURATION); + } + } + + @Override + public void stop() { + if (isRunning()) { + mHandler.removeCallbacks(this); + mChargingLevel = -1; } + setProgress(mBatteryLevel); + } + + @Override + public boolean isRunning() { + return mChargingLevel != -1; } }