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

Commit af1700aa authored by Adrian Roos's avatar Adrian Roos
Browse files

Ambient Display: Delay charging indicator if about to wake up

Delays the charging indicator on AOD such that it does not show if
the device is about to wake up due to being plugged in, thus avoiding
a perceived flicker.

Change-Id: I603cc3271245d68f7a36367ea55872958a033e53
Fixes: 62598404
Test: Turn off AOD, go to sleep, double tap to trigger ambient display, plug in device, verify no bolt is briefly visible
parent 1e3b49c1
Loading
Loading
Loading
Loading
+36 −1
Original line number Diff line number Diff line
@@ -19,9 +19,11 @@ package com.android.systemui;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.UserHandle;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.systemui.statusbar.policy.BatteryController;
import com.android.systemui.statusbar.policy.ConfigurationController;

@@ -34,14 +36,28 @@ public class ChargingView extends ImageView implements
        BatteryController.BatteryStateChangeCallback,
        ConfigurationController.ConfigurationListener {

    private static final long CHARGING_INDICATION_DELAY_MS = 1000;

    private final AmbientDisplayConfiguration mConfig;
    private final Runnable mClearSuppressCharging = this::clearSuppressCharging;
    private BatteryController mBatteryController;
    private int mImageResource;
    private boolean mCharging;
    private boolean mDark;
    private boolean mSuppressCharging;


    private void clearSuppressCharging() {
        mSuppressCharging = false;
        removeCallbacks(mClearSuppressCharging);
        updateVisibility();
    }

    public ChargingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mConfig = new AmbientDisplayConfiguration(context);

        TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.src});
        int srcResId = a.getResourceId(0, 0);

@@ -67,14 +83,30 @@ public class ChargingView extends ImageView implements
        super.onDetachedFromWindow();
        mBatteryController.removeCallback(this);
        Dependency.get(ConfigurationController.class).removeCallback(this);
        removeCallbacks(mClearSuppressCharging);
    }

    @Override
    public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
        boolean startCharging = charging && !mCharging;
        if (startCharging && deviceWillWakeUpWhenPluggedIn() && mDark) {
            // We're about to wake up, and thus don't want to show the indicator just for it to be
            // hidden again.
            clearSuppressCharging();
            mSuppressCharging = true;
            postDelayed(mClearSuppressCharging, CHARGING_INDICATION_DELAY_MS);
        }
        mCharging = charging;
        updateVisibility();
    }

    private boolean deviceWillWakeUpWhenPluggedIn() {
        boolean plugTurnsOnScreen = getResources().getBoolean(
                com.android.internal.R.bool.config_unplugTurnsOnScreen);
        boolean aod = mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT);
        return !aod && plugTurnsOnScreen;
    }

    @Override
    public void onDensityOrFontScaleChanged() {
        setImageResource(mImageResource);
@@ -82,10 +114,13 @@ public class ChargingView extends ImageView implements

    public void setDark(boolean dark) {
        mDark = dark;
        if (!dark) {
            clearSuppressCharging();
        }
        updateVisibility();
    }

    private void updateVisibility() {
        setVisibility(mCharging && mDark ? VISIBLE : INVISIBLE);
        setVisibility(mCharging && !mSuppressCharging && mDark ? VISIBLE : INVISIBLE);
    }
}