Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eba850e8 authored by Lei Yu's avatar Lei Yu
Browse files

Update color filter for battery saver mode

Fixes: 129498556
Test: Manual
Change-Id: I57809c77e488b9828bea50d4c4f410d49912cd4b
parent 4b54ad9b
Loading
Loading
Loading
Loading
+20 −10
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.settings.fuelgauge;

import android.annotation.Nullable;
import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
@@ -38,7 +37,8 @@ public class BatteryMeterView extends ImageView {
    ColorFilter mErrorColorFilter;
    @VisibleForTesting
    ColorFilter mAccentColorFilter;
    private boolean mPowerSaveEnabled;
    @VisibleForTesting
    ColorFilter mForegroundColorFilter;

    public BatteryMeterView(Context context) {
        this(context, null, 0);
@@ -57,7 +57,9 @@ public class BatteryMeterView extends ImageView {
                PorterDuff.Mode.SRC);
        mErrorColorFilter = new PorterDuffColorFilter(
                context.getColor(R.color.battery_icon_color_error), PorterDuff.Mode.SRC_IN);

        mForegroundColorFilter =new PorterDuffColorFilter(
                Utils.getColorAttrDefaultColor(context, android.R.attr.colorForeground),
                PorterDuff.Mode.SRC);
        mDrawable = new BatteryMeterDrawable(context, frameColor);
        mDrawable.setColorFilter(mAccentColorFilter);
        setImageDrawable(mDrawable);
@@ -65,20 +67,16 @@ public class BatteryMeterView extends ImageView {

    public void setBatteryLevel(int level) {
        mDrawable.setBatteryLevel(level);
        if (level < mDrawable.getCriticalLevel()) {
            mDrawable.setColorFilter(mErrorColorFilter);
        } else {
            mDrawable.setColorFilter(mAccentColorFilter);
        }
        updateColorFilter();
    }

    public void setPowerSave(boolean powerSave) {
        mDrawable.setPowerSaveEnabled(powerSave);
        mPowerSaveEnabled = powerSave;
        updateColorFilter();
    }

    public boolean getPowerSave() {
        return mPowerSaveEnabled;
        return mDrawable.getPowerSaveEnabled();
    }

    public int getBatteryLevel() {
@@ -94,6 +92,18 @@ public class BatteryMeterView extends ImageView {
        return mDrawable.getCharging();
    }

    private void updateColorFilter() {
        final boolean powerSaveEnabled = mDrawable.getPowerSaveEnabled();
        final int level = mDrawable.getBatteryLevel();
        if (powerSaveEnabled) {
            mDrawable.setColorFilter(mForegroundColorFilter);
        } else if (level < mDrawable.getCriticalLevel()) {
            mDrawable.setColorFilter(mErrorColorFilter);
        } else {
            mDrawable.setColorFilter(mAccentColorFilter);
        }
    }

    public static class BatteryMeterDrawable extends ThemedBatteryDrawable {
        private final int mIntrinsicWidth;
        private final int mIntrinsicHeight;
+4 −0
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ public class BatteryMeterViewTest {
    private ColorFilter mErrorColorFilter;
    @Mock
    private ColorFilter mAccentColorFilter;
    @Mock
    private ColorFilter mForegroundColorFilter;
    private Context mContext;
    private BatteryMeterView mBatteryMeterView;
    private BatteryMeterView.BatteryMeterDrawable mDrawable;
@@ -58,6 +60,7 @@ public class BatteryMeterViewTest {
        mBatteryMeterView.mDrawable = mDrawable;
        mBatteryMeterView.mAccentColorFilter = mAccentColorFilter;
        mBatteryMeterView.mErrorColorFilter = mErrorColorFilter;
        mBatteryMeterView.mForegroundColorFilter = mForegroundColorFilter;

        when(mDrawable.getCriticalLevel()).thenReturn(BATTERY_CRITICAL_LEVEL);
    }
@@ -88,5 +91,6 @@ public class BatteryMeterViewTest {
        mBatteryMeterView.setPowerSave(true);

        assertThat(mBatteryMeterView.getPowerSave()).isEqualTo(true);
        verify(mDrawable).setColorFilter(mForegroundColorFilter);
    }
}