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

Commit 9ac93217 authored by Nick Pelly's avatar Nick Pelly Committed by The Android Open Source Project
Browse files

AI 145201: Hold wakelock while delaying for audio route switch after headset unplug.

  Fixes bug where we go to sleep before switching audio, and we lose audio until the apps CPU wakes up again.
  BUG=1774615

Automated import of CL 145201
parent 939151f1
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.UEventObserver;
import android.util.Log;
import android.media.AudioManager;
@@ -38,15 +40,19 @@ class HeadsetObserver extends UEventObserver {
    private static final String HEADSET_STATE_PATH = "/sys/class/switch/h2w/state";
    private static final String HEADSET_NAME_PATH = "/sys/class/switch/h2w/name";

    private Context mContext;

    private int mHeadsetState;
    private String mHeadsetName;
    private boolean mAudioRouteNeedsUpdate;
    private AudioManager mAudioManager;

    private final Context mContext;
    private final WakeLock mWakeLock;  // held while there is a pending route change

    public HeadsetObserver(Context context) {
        mContext = context;
        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "HeadsetObserver");
        mWakeLock.setReferenceCounted(false);

        startObserving(HEADSET_UEVENT_MATCH);

@@ -103,6 +109,7 @@ class HeadsetObserver extends UEventObserver {
                // immediate, so delay the route change by 1000ms.
                // This could be improved once the audio sub-system provides an
                // interface to clear the audio pipeline.
                mWakeLock.acquire();
                mHandler.sendEmptyMessageDelayed(0, 1000);
            } else {
                updateAudioRoute();
@@ -138,6 +145,7 @@ class HeadsetObserver extends UEventObserver {
        @Override
        public void handleMessage(Message msg) {
            updateAudioRoute();
            mWakeLock.release();
        }
    };