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

Commit 532cfe52 authored by Ziyang Cheng's avatar Ziyang Cheng
Browse files

Listen to DisplayManager to get screen on/off states.



-After TTW optimizations on Pixel watch, ACTION_SCREEN_ON/OFF
 broadcast is not guaranteed to be fired. Use a more reliable
 method to monitor screen on/off states.
-See: go/wear-dd-aod-off-ttw-opt.

Test: Performing tilt/untilt gestures on Pixel watch and verify
      SetParameters is called when screen is on/off.
Bug: 291313447
Change-Id: Ia374eb6634597283ac92328a6efa25c7aa9779a0
Signed-off-by: default avatarZiyang Cheng <ziyangch@google.com>
parent 36f53b79
Loading
Loading
Loading
Loading
+37 −13
Original line number Diff line number Diff line
@@ -72,6 +72,8 @@ import android.content.res.Resources;
import android.database.ContentObserver;
import android.hardware.SensorPrivacyManager;
import android.hardware.SensorPrivacyManagerInternal;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener;
import android.hardware.hdmi.HdmiAudioSystemClient;
import android.hardware.hdmi.HdmiClient;
import android.hardware.hdmi.HdmiControlManager;
@@ -180,6 +182,7 @@ import android.util.PrintWriterPrinter;
import android.util.Slog;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.Display;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;
@@ -977,6 +980,36 @@ public class AudioService extends IAudioService.Stub
    private AtomicBoolean mMasterMute = new AtomicBoolean(false);
    private DisplayManager mDisplayManager;
    private DisplayListener mDisplayListener =
      new DisplayListener() {
        @Override
        public void onDisplayAdded(int displayId) {}
        @Override
        public void onDisplayRemoved(int displayId) {}
        @Override
        public void onDisplayChanged(int displayId) {
            if (displayId != Display.DEFAULT_DISPLAY) {
                return;
            }
            int displayState = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY).getState();
            if (displayState == Display.STATE_ON) {
                if (mMonitorRotation) {
                    RotationHelper.enable();
                }
                AudioSystem.setParameters("screen_state=on");
            } else {
                if (mMonitorRotation) {
                    //reduce wakeups (save current) by only listening when display is on
                    RotationHelper.disable();
                }
                AudioSystem.setParameters("screen_state=off");
            }
        }
      };
    ///////////////////////////////////////////////////////////////////////////
    // Construction
@@ -1255,6 +1288,8 @@ public class AudioService extends IAudioService.Stub
                0 /* arg1 */,  0 /* arg2 */, null /* obj */,  0 /* delay */);
        queueMsgUnderWakeLock(mAudioHandler, MSG_INIT_SPATIALIZER,
                0 /* arg1 */, 0 /* arg2 */, null /* obj */, 0 /* delay */);
        mDisplayManager = context.getSystemService(DisplayManager.class);
    }
    private void initVolumeStreamStates() {
@@ -1351,8 +1386,6 @@ public class AudioService extends IAudioService.Stub
                new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
        intentFilter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
        intentFilter.addAction(Intent.ACTION_DOCK_EVENT);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
        intentFilter.addAction(Intent.ACTION_USER_BACKGROUND);
        intentFilter.addAction(Intent.ACTION_USER_FOREGROUND);
@@ -1382,6 +1415,8 @@ public class AudioService extends IAudioService.Stub
        } else {
            subscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionChangedListener);
        }
        mDisplayManager.registerDisplayListener(mDisplayListener, mAudioHandler);
    }
    public void systemReady() {
@@ -9552,17 +9587,6 @@ public class AudioService extends IAudioService.Stub
            } else if (action.equals(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED)
                    || action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
                mDeviceBroker.receiveBtEvent(intent);
            } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
                if (mMonitorRotation) {
                    RotationHelper.enable();
                }
                AudioSystem.setParameters("screen_state=on");
            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
                if (mMonitorRotation) {
                    //reduce wakeups (save current) by only listening when display is on
                    RotationHelper.disable();
                }
                AudioSystem.setParameters("screen_state=off");
            } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
                sendMsg(mAudioHandler,
                        MSG_CONFIGURATION_CHANGED,