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

Commit caa2c5bb authored by Jean-Michel Trivi's avatar Jean-Michel Trivi
Browse files

More info in audio focus stack entry

Add fields in an audio focus stack entry to track what type of audio
 focus was requested, and why it was lost.
Make audio focus dump easier to read.

Change-Id: I61f6e43c13a32328a07125a2ba0dce1053872c86
parent e58bb044
Loading
Loading
Loading
Loading
+55 −14
Original line number Original line Diff line number Diff line
@@ -66,6 +66,11 @@ public class MediaFocusControl implements OnFinished {
    /** Debug volumes */
    /** Debug volumes */
    protected static final boolean DEBUG_VOL = false;
    protected static final boolean DEBUG_VOL = false;


    /**
     * Used to indicate no audio focus has been gained or lost.
     */
    private static final int AUDIOFOCUS_NONE = 0;

    /** Used to alter media button redirection when the phone is ringing. */
    /** Used to alter media button redirection when the phone is ringing. */
    private boolean mIsRinging = false;
    private boolean mIsRinging = false;


@@ -267,15 +272,15 @@ public class MediaFocusControl implements OnFinished {
        synchronized(mAudioFocusLock) {
        synchronized(mAudioFocusLock) {
            if (!mFocusStack.empty() && (mFocusStack.peek().mFocusDispatcher != null)) {
            if (!mFocusStack.empty() && (mFocusStack.peek().mFocusDispatcher != null)) {
                // notify the current focus owner it lost focus after removing it from stack
                // notify the current focus owner it lost focus after removing it from stack
                FocusStackEntry focusOwner = mFocusStack.pop();
                final FocusStackEntry exFocusOwner = mFocusStack.pop();
                try {
                try {
                    focusOwner.mFocusDispatcher.dispatchAudioFocusChange(
                    exFocusOwner.mFocusDispatcher.dispatchAudioFocusChange(
                            AudioManager.AUDIOFOCUS_LOSS, focusOwner.mClientId);
                            AudioManager.AUDIOFOCUS_LOSS, exFocusOwner.mClientId);
                } catch (RemoteException e) {
                } catch (RemoteException e) {
                    Log.e(TAG, "Failure to signal loss of audio focus due to "+ e);
                    Log.e(TAG, "Failure to signal loss of audio focus due to "+ e);
                    e.printStackTrace();
                    e.printStackTrace();
                }
                }
                focusOwner.unlinkToDeath();
                exFocusOwner.unlinkToDeath();
                // clear RCD
                // clear RCD
                synchronized(mRCStack) {
                synchronized(mRCStack) {
                    clearRemoteControlDisplay_syncAfRcs();
                    clearRemoteControlDisplay_syncAfRcs();
@@ -288,9 +293,11 @@ public class MediaFocusControl implements OnFinished {
        // notify the top of the stack it gained focus
        // notify the top of the stack it gained focus
        if (!mFocusStack.empty() && (mFocusStack.peek().mFocusDispatcher != null)) {
        if (!mFocusStack.empty() && (mFocusStack.peek().mFocusDispatcher != null)) {
            if (canReassignAudioFocus()) {
            if (canReassignAudioFocus()) {
                final FocusStackEntry newFocusOwner = mFocusStack.peek();
                try {
                try {
                    mFocusStack.peek().mFocusDispatcher.dispatchAudioFocusChange(
                    newFocusOwner.mFocusDispatcher.dispatchAudioFocusChange(
                            AudioManager.AUDIOFOCUS_GAIN, mFocusStack.peek().mClientId);
                            AudioManager.AUDIOFOCUS_GAIN, mFocusStack.peek().mClientId);
                    newFocusOwner.mFocusLossReceived = AUDIOFOCUS_NONE;
                } catch (RemoteException e) {
                } catch (RemoteException e) {
                    Log.e(TAG, "Failure to signal gain of audio control focus due to "+ e);
                    Log.e(TAG, "Failure to signal gain of audio control focus due to "+ e);
                    e.printStackTrace();
                    e.printStackTrace();
@@ -304,22 +311,53 @@ public class MediaFocusControl implements OnFinished {
        public IAudioFocusDispatcher mFocusDispatcher = null;
        public IAudioFocusDispatcher mFocusDispatcher = null;
        public IBinder mSourceRef = null;
        public IBinder mSourceRef = null;
        public String mClientId;
        public String mClientId;
        public int mFocusChangeType;
        /** the audio focus gain request that caused the addition of this entry in the stack */
        public int mFocusGainRequest;
        public int mFocusLossReceived;
        public AudioFocusDeathHandler mHandler;
        public AudioFocusDeathHandler mHandler;
        public String mPackageName;
        public String mPackageName;
        public int mCallingUid;
        public int mCallingUid;


        public FocusStackEntry() {
        private static String focusChangeToString(int focus) {
            switch(focus) {
                case AudioManager.AUDIOFOCUS_GAIN:
                    return "GAIN";
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                    return "GAIN_TRANSIENT";
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    return "GAIN_TRANSIENT_MAY_DUCK";
                case AudioManager.AUDIOFOCUS_LOSS:
                    return "LOSS";
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    return "LOSS_TRANSIENT";
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    return "LOSS_TRANSIENT_CAN_DUCK";
                default:
                    return "[invalid focus change" + focus + "]";
            }
        }

        String focusGainToString() {
            return focusChangeToString(mFocusGainRequest);
        }

        String focusLossToString() {
            if (mFocusLossReceived == 0) {
                return "none";
            } else {
                return focusChangeToString(mFocusLossReceived);
            }
        }
        }


        public FocusStackEntry(int streamType, int duration,
        FocusStackEntry(int streamType, int focusRequest,
                IAudioFocusDispatcher afl, IBinder source, String id, AudioFocusDeathHandler hdlr,
                IAudioFocusDispatcher afl, IBinder source, String id, AudioFocusDeathHandler hdlr,
                String pn, int uid) {
                String pn, int uid) {
            mStreamType = streamType;
            mStreamType = streamType;
            mFocusDispatcher = afl;
            mFocusDispatcher = afl;
            mSourceRef = source;
            mSourceRef = source;
            mClientId = id;
            mClientId = id;
            mFocusChangeType = duration;
            mFocusGainRequest = focusRequest;
            mFocusLossReceived = AUDIOFOCUS_NONE;
            mHandler = hdlr;
            mHandler = hdlr;
            mPackageName = pn;
            mPackageName = pn;
            mCallingUid = uid;
            mCallingUid = uid;
@@ -358,7 +396,8 @@ public class MediaFocusControl implements OnFinished {
                pw.println("  source:" + fse.mSourceRef
                pw.println("  source:" + fse.mSourceRef
                        + " -- pack: " + fse.mPackageName
                        + " -- pack: " + fse.mPackageName
                        + " -- client: " + fse.mClientId
                        + " -- client: " + fse.mClientId
                        + " -- duration: " + fse.mFocusChangeType
                        + " -- gain: " + fse.focusGainToString()
                        + " -- loss: " + fse.focusLossToString()
                        + " -- uid: " + fse.mCallingUid
                        + " -- uid: " + fse.mCallingUid
                        + " -- stream: " + fse.mStreamType);
                        + " -- stream: " + fse.mStreamType);
            }
            }
@@ -513,7 +552,7 @@ public class MediaFocusControl implements OnFinished {
            if (!mFocusStack.empty() && mFocusStack.peek().mClientId.equals(clientId)) {
            if (!mFocusStack.empty() && mFocusStack.peek().mClientId.equals(clientId)) {
                // if focus is already owned by this client and the reason for acquiring the focus
                // if focus is already owned by this client and the reason for acquiring the focus
                // hasn't changed, don't do anything
                // hasn't changed, don't do anything
                if (mFocusStack.peek().mFocusChangeType == focusChangeHint) {
                if (mFocusStack.peek().mFocusGainRequest == focusChangeHint) {
                    // unlink death handler so it can be gc'ed.
                    // unlink death handler so it can be gc'ed.
                    // linkToDeath() creates a JNI global reference preventing collection.
                    // linkToDeath() creates a JNI global reference preventing collection.
                    cb.unlinkToDeath(afdh, 0);
                    cb.unlinkToDeath(afdh, 0);
@@ -527,12 +566,14 @@ public class MediaFocusControl implements OnFinished {


            // notify current top of stack it is losing focus
            // notify current top of stack it is losing focus
            if (!mFocusStack.empty() && (mFocusStack.peek().mFocusDispatcher != null)) {
            if (!mFocusStack.empty() && (mFocusStack.peek().mFocusDispatcher != null)) {
                final FocusStackEntry exFocusOwner = mFocusStack.peek();
                try {
                try {
                    mFocusStack.peek().mFocusDispatcher.dispatchAudioFocusChange(
                    exFocusOwner.mFocusDispatcher.dispatchAudioFocusChange(
                            -1 * focusChangeHint, // loss and gain codes are inverse of each other
                            -1 * focusChangeHint, // loss and gain codes are inverse of each other
                            mFocusStack.peek().mClientId);
                            mFocusStack.peek().mClientId);
                    exFocusOwner.mFocusLossReceived = -1 * focusChangeHint;
                } catch (RemoteException e) {
                } catch (RemoteException e) {
                    Log.e(TAG, " Failure to signal loss of focus due to "+ e);
                    Log.e(TAG, " Failure to signal loss of focus: ", e);
                    e.printStackTrace();
                    e.printStackTrace();
                }
                }
            }
            }
@@ -1557,7 +1598,7 @@ public class MediaFocusControl implements OnFinished {
            for (int index = mFocusStack.size()-1; index >= 0; index--) {
            for (int index = mFocusStack.size()-1; index >= 0; index--) {
                FocusStackEntry fse = mFocusStack.elementAt(index);
                FocusStackEntry fse = mFocusStack.elementAt(index);
                if ((fse.mStreamType == AudioManager.STREAM_MUSIC)
                if ((fse.mStreamType == AudioManager.STREAM_MUSIC)
                        || (fse.mFocusChangeType == AudioManager.AUDIOFOCUS_GAIN)) {
                        || (fse.mFocusGainRequest == AudioManager.AUDIOFOCUS_GAIN)) {
                    af = fse;
                    af = fse;
                    break;
                    break;
                }
                }