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

Commit 06480470 authored by Roman Birg's avatar Roman Birg Committed by Steve Kondik
Browse files

SystemUI: disable visualizers during power save mode



Change-Id: I2ab4502d2654d1bcfe19782a600fe35585085b9a
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent c0fee4c3
Loading
Loading
Loading
Loading
+36 −11
Original line number Original line Diff line number Diff line
@@ -18,8 +18,10 @@ package com.android.systemui.qs.tiles;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint;
@@ -30,6 +32,7 @@ import android.media.session.MediaSession;
import android.media.session.MediaSessionManager;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.media.session.PlaybackState;
import android.os.AsyncTask;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.view.Gravity;
import android.view.Gravity;
import android.view.View;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout;
@@ -58,6 +61,18 @@ public class VisualizerTile extends QSTile<QSTile.State>
    private boolean mLinked;
    private boolean mLinked;
    private boolean mIsAnythingPlaying;
    private boolean mIsAnythingPlaying;
    private boolean mListening;
    private boolean mListening;
    private boolean mPowerSaveModeEnabled;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGING.equals(intent.getAction())) {
                mPowerSaveModeEnabled = intent.getBooleanExtra(PowerManager.EXTRA_POWER_SAVE_MODE,
                        false);
                checkIfPlaying();
            }
        }
    };


    public VisualizerTile(Host host) {
    public VisualizerTile(Host host) {
        super(host);
        super(host);
@@ -66,15 +81,23 @@ public class VisualizerTile extends QSTile<QSTile.State>
        mKeyguardMonitor = host.getKeyguardMonitor();
        mKeyguardMonitor = host.getKeyguardMonitor();
        mKeyguardMonitor.addCallback(this);
        mKeyguardMonitor.addCallback(this);


        mContext.registerReceiver(mReceiver,
                new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING));
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        mPowerSaveModeEnabled = pm.isPowerSaveMode();

        // initialize state
        // initialize state
        if (!mPowerSaveModeEnabled) {
            List<MediaController> activeSessions = mMediaSessionManager.getActiveSessions(null);
            List<MediaController> activeSessions = mMediaSessionManager.getActiveSessions(null);
            for (MediaController activeSession : activeSessions) {
            for (MediaController activeSession : activeSessions) {
                PlaybackState playbackState = activeSession.getPlaybackState();
                PlaybackState playbackState = activeSession.getPlaybackState();
            if (playbackState != null && playbackState.getState() == PlaybackState.STATE_PLAYING) {
                if (playbackState != null && playbackState.getState()
                        == PlaybackState.STATE_PLAYING) {
                    mIsAnythingPlaying = true;
                    mIsAnythingPlaying = true;
                    break;
                    break;
                }
                }
            }
            }
        }
        if (mIsAnythingPlaying && !mLinked) {
        if (mIsAnythingPlaying && !mLinked) {
            AsyncTask.execute(mLinkVisualizer);
            AsyncTask.execute(mLinkVisualizer);
        } else if (!mIsAnythingPlaying && mLinked) {
        } else if (!mIsAnythingPlaying && mLinked) {
@@ -182,17 +205,19 @@ public class VisualizerTile extends QSTile<QSTile.State>
        }
        }
        mCallbacks.clear();
        mCallbacks.clear();
        mKeyguardMonitor.removeCallback(this);
        mKeyguardMonitor.removeCallback(this);
        mContext.unregisterReceiver(mReceiver);
    }
    }



    private void checkIfPlaying() {
    private void checkIfPlaying() {
        boolean anythingPlaying = false;
        boolean anythingPlaying = false;
        if (!mPowerSaveModeEnabled) {
            for (Map.Entry<MediaSession.Token, CallbackInfo> entry : mCallbacks.entrySet()) {
            for (Map.Entry<MediaSession.Token, CallbackInfo> entry : mCallbacks.entrySet()) {
                if (entry.getValue().isPlaying()) {
                if (entry.getValue().isPlaying()) {
                    anythingPlaying = true;
                    anythingPlaying = true;
                    break;
                    break;
                }
                }
            }
            }
        }
        if (anythingPlaying != mIsAnythingPlaying) {
        if (anythingPlaying != mIsAnythingPlaying) {
            mIsAnythingPlaying = anythingPlaying;
            mIsAnythingPlaying = anythingPlaying;
            if (mIsAnythingPlaying && !mLinked) {
            if (mIsAnythingPlaying && !mLinked) {
+26 −2
Original line number Original line Diff line number Diff line
@@ -39,6 +39,7 @@ import android.graphics.drawable.InsetDrawable;
import android.os.AsyncTask;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserHandle;
import android.provider.MediaStore;
import android.provider.MediaStore;
@@ -130,6 +131,7 @@ public class KeyguardBottomAreaView extends FrameLayout implements View.OnClickL
    private boolean mScreenOn;
    private boolean mScreenOn;
    private boolean mLinked;
    private boolean mLinked;
    private boolean mVisualizerEnabled;
    private boolean mVisualizerEnabled;
    private boolean mPowerSaveModeEnabled;
    private SettingsObserver mSettingsObserver;
    private SettingsObserver mSettingsObserver;


    public KeyguardBottomAreaView(Context context) {
    public KeyguardBottomAreaView(Context context) {
@@ -151,7 +153,6 @@ public class KeyguardBottomAreaView extends FrameLayout implements View.OnClickL
        mLinearOutSlowInInterpolator =
        mLinearOutSlowInInterpolator =
                AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
                AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
        mSettingsObserver = new SettingsObserver(new Handler());
        mSettingsObserver = new SettingsObserver(new Handler());
        mSettingsObserver.observe();
    }
    }


    private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
    private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
@@ -483,9 +484,19 @@ public class KeyguardBottomAreaView extends FrameLayout implements View.OnClickL
        }
        }
    }
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mContext.registerReceiver(mReceiver, new IntentFilter(
                PowerManager.ACTION_POWER_SAVE_MODE_CHANGING));
        mSettingsObserver.observe();
    }

    @Override
    @Override
    protected void onDetachedFromWindow() {
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        super.onDetachedFromWindow();
        mSettingsObserver.unobserve();
        mContext.unregisterReceiver(mReceiver);
        mTrustDrawable.stop();
        mTrustDrawable.stop();
        requestVisualizer(false, 0);
        requestVisualizer(false, 0);
    }
    }
@@ -653,6 +664,19 @@ public class KeyguardBottomAreaView extends FrameLayout implements View.OnClickL
        }
        }
    };
    };


    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGING.equals(intent.getAction())) {
                mPowerSaveModeEnabled = intent.getBooleanExtra(PowerManager.EXTRA_POWER_SAVE_MODE,
                        false);
                removeCallbacks(mStartVisualizer);
                removeCallbacks(mStopVisualizer);
                post(mPowerSaveModeEnabled ? mStopVisualizer : mStartVisualizer);
            }
        }
    };

    private final KeyguardUpdateMonitorCallback mUpdateMonitorCallback =
    private final KeyguardUpdateMonitorCallback mUpdateMonitorCallback =
            new KeyguardUpdateMonitorCallback() {
            new KeyguardUpdateMonitorCallback() {
        @Override
        @Override
@@ -720,7 +744,7 @@ public class KeyguardBottomAreaView extends FrameLayout implements View.OnClickL
    }
    }


    public void requestVisualizer(boolean show, int delay) {
    public void requestVisualizer(boolean show, int delay) {
        if (mVisualizer == null || !mVisualizerEnabled) {
        if (mVisualizer == null || !mVisualizerEnabled || mPowerSaveModeEnabled) {
            return;
            return;
        }
        }
        removeCallbacks(mStartVisualizer);
        removeCallbacks(mStartVisualizer);