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

Commit b6a0dfb0 authored by Jean-Michel Trivi's avatar Jean-Michel Trivi Committed by Android (Google) Code Review
Browse files

Merge "Fix bug 3275151 Request and abandon audio focus with audio mode changes"

parents ad3ec1b9 2ade5761
Loading
Loading
Loading
Loading
+38 −19
Original line number Original line Diff line number Diff line
@@ -752,6 +752,10 @@ public class AudioService extends IAudioService.Stub {
                mode = mMode;
                mode = mMode;
            }
            }
            if (mode != mMode) {
            if (mode != mMode) {

                // automatically handle audio focus for mode changes
                handleFocusForCalls(mMode, mode);

                if (AudioSystem.setPhoneState(mode) == AudioSystem.AUDIO_STATUS_OK) {
                if (AudioSystem.setPhoneState(mode) == AudioSystem.AUDIO_STATUS_OK) {
                    mMode = mode;
                    mMode = mode;


@@ -807,6 +811,38 @@ public class AudioService extends IAudioService.Stub {
        }
        }
    }
    }


    /** pre-condition: oldMode != newMode */
    private void handleFocusForCalls(int oldMode, int newMode) {
        // if ringing
        if (newMode == AudioSystem.MODE_RINGTONE) {
            // if not ringing silently
            int ringVolume = AudioService.this.getStreamVolume(AudioManager.STREAM_RING);
            if (ringVolume > 0) {
                // request audio focus for the communication focus entry
                requestAudioFocus(AudioManager.STREAM_RING,
                        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
                        null, null /* both allowed to be null only for this clientId */,
                        IN_VOICE_COMM_FOCUS_ID /*clientId*/);

            }
        }
        // if entering call
        else if ((newMode == AudioSystem.MODE_IN_CALL)
                || (newMode == AudioSystem.MODE_IN_COMMUNICATION)) {
            // request audio focus for the communication focus entry
            // (it's ok if focus was already requested during ringing)
            requestAudioFocus(AudioManager.STREAM_RING,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
                    null, null /* both allowed to be null only for this clientId */,
                    IN_VOICE_COMM_FOCUS_ID /*clientId*/);
        }
        // if exiting call
        else if (newMode == AudioSystem.MODE_NORMAL) {
            // abandon audio focus for communication focus entry
            abandonAudioFocus(null, IN_VOICE_COMM_FOCUS_ID);
        }
    }

    /** @see AudioManager#getMode() */
    /** @see AudioManager#getMode() */
    public int getMode() {
    public int getMode() {
        return mMode;
        return mMode;
@@ -2093,28 +2129,11 @@ public class AudioService extends IAudioService.Stub {
                synchronized(mRingingLock) {
                synchronized(mRingingLock) {
                    mIsRinging = true;
                    mIsRinging = true;
                }
                }
                int ringVolume = AudioService.this.getStreamVolume(AudioManager.STREAM_RING);
            } else if ((state == TelephonyManager.CALL_STATE_OFFHOOK)
                if (ringVolume > 0) {
                    || (state == TelephonyManager.CALL_STATE_IDLE)) {
                    requestAudioFocus(AudioManager.STREAM_RING,
                                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
                                null, null /* both allowed to be null only for this clientId */,
                                IN_VOICE_COMM_FOCUS_ID /*clientId*/);
                }
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                //Log.v(TAG, " CALL_STATE_OFFHOOK");
                synchronized(mRingingLock) {
                synchronized(mRingingLock) {
                    mIsRinging = false;
                    mIsRinging = false;
                }
                }
                requestAudioFocus(AudioManager.STREAM_RING,
                        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
                        null, null /* both allowed to be null only for this clientId */,
                        IN_VOICE_COMM_FOCUS_ID /*clientId*/);
            } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                //Log.v(TAG, " CALL_STATE_IDLE");
                synchronized(mRingingLock) {
                    mIsRinging = false;
                }
                abandonAudioFocus(null, IN_VOICE_COMM_FOCUS_ID);
            }
            }
        }
        }
    };
    };