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

Commit 0af3e3d4 authored by Eric Laurent's avatar Eric Laurent Committed by Android Build Coastguard Worker
Browse files

Revert "AudioService: synchronize audio mode and focus for Telecom"

This reverts commit 15f9e56e.

Bug: 375018522
Bug: 350415723
Flag: EXEMPT bug fix
Test: make
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:5754baa4be5715b164d3a924e2cd0b327823ee74)
Merged-In: Id8b8a7933976c2a98f42ae23775f1eee66462b92
Change-Id: Id8b8a7933976c2a98f42ae23775f1eee66462b92
parent d7fa89a0
Loading
Loading
Loading
Loading
+5 −8
Original line number Diff line number Diff line
@@ -1321,9 +1321,9 @@ public class AudioDeviceBroker {
        sendLMsgNoDelay(MSG_II_SET_LE_AUDIO_OUT_VOLUME, SENDMSG_REPLACE, info);
    }

    /*package*/ void postSetModeOwner(int mode, int pid, int uid, boolean signal) {
        sendILMsgNoDelay(MSG_IL_SET_MODE_OWNER, SENDMSG_REPLACE,
                signal ? 1 : 0, new AudioModeInfo(mode, pid, uid));
    /*package*/ void postSetModeOwner(int mode, int pid, int uid) {
        sendLMsgNoDelay(MSG_I_SET_MODE_OWNER, SENDMSG_REPLACE,
                new AudioModeInfo(mode, pid, uid));
    }

    /*package*/ void postBluetoothDeviceConfigChange(@NonNull BtDeviceInfo info) {
@@ -2025,7 +2025,7 @@ public class AudioDeviceBroker {
                        mBtHelper.setAvrcpAbsoluteVolumeIndex(msg.arg1);
                    }
                    break;
                case MSG_IL_SET_MODE_OWNER:
                case MSG_I_SET_MODE_OWNER:
                    synchronized (mSetModeLock) {
                        synchronized (mDeviceStateLock) {
                            int btScoRequesterUid = bluetoothScoRequestOwnerUid();
@@ -2036,9 +2036,6 @@ public class AudioDeviceBroker {
                            }
                        }
                    }
                    if (msg.arg1 == 1 /*signal*/) {
                        mAudioService.decrementAudioModeResetCount();
                    }
                    break;

                case MSG_L_SET_COMMUNICATION_DEVICE_FOR_CLIENT:
@@ -2227,7 +2224,7 @@ public class AudioDeviceBroker {
    private static final int MSG_REPORT_NEW_ROUTES = 13;
    private static final int MSG_II_SET_HEARING_AID_VOLUME = 14;
    private static final int MSG_I_SET_AVRCP_ABSOLUTE_VOLUME = 15;
    private static final int MSG_IL_SET_MODE_OWNER = 16;
    private static final int MSG_I_SET_MODE_OWNER = 16;

    private static final int MSG_I_BT_SERVICE_DISCONNECTED_PROFILE = 22;
    private static final int MSG_IL_BT_SERVICE_CONNECTED_PROFILE = 23;
+27 −103
Original line number Diff line number Diff line
@@ -1918,7 +1918,7 @@ public class AudioService extends IAudioService.Stub
        // Restore call state
        synchronized (mDeviceBroker.mSetModeLock) {
            onUpdateAudioMode(AudioSystem.MODE_CURRENT, android.os.Process.myPid(),
                    mContext.getPackageName(), true /*force*/, false /*signal*/);
                    mContext.getPackageName(), true /*force*/);
        }
        final int forSys;
        synchronized (mSettingsLock) {
@@ -4746,47 +4746,14 @@ public class AudioService extends IAudioService.Stub
                }
            }
            if (updateAudioMode) {
                postUpdateAudioMode(existingMsgPolicy, AudioSystem.MODE_CURRENT,
                        android.os.Process.myPid(), mContext.getPackageName(),
                        false /*signal*/, delay);
            }
        }
    }
    static class UpdateAudioModeInfo {
        UpdateAudioModeInfo(int mode, int pid, String packageName, boolean signal) {
            mMode = mode;
            mPid = pid;
            mPackageName = packageName;
            mSignal = signal;
        }
        private final int mMode;
        private final int mPid;
        private final String mPackageName;
        private final boolean mSignal;
        int getMode() {
            return mMode;
        }
        int getPid() {
            return mPid;
        }
        String getPackageName() {
            return mPackageName;
        }
        boolean getSignal() {
            return mSignal;
        }
    }
    void postUpdateAudioMode(int msgPolicy, int mode, int pid, String packageName,
            boolean signal, int delay) {
        synchronized (mAudioModeResetLock) {
            if (signal) {
                mAudioModeResetCount++;
                sendMsg(mAudioHandler,
                        MSG_UPDATE_AUDIO_MODE,
                        existingMsgPolicy,
                        AudioSystem.MODE_CURRENT,
                        android.os.Process.myPid(),
                        mContext.getPackageName(),
                        delay);
            }
            sendMsg(mAudioHandler, MSG_UPDATE_AUDIO_MODE, msgPolicy, 0, 0,
                new UpdateAudioModeInfo(mode, pid, packageName, signal), delay);
        }
    }
@@ -6188,9 +6155,13 @@ public class AudioService extends IAudioService.Stub
                } else {
                    SetModeDeathHandler h = mSetModeDeathHandlers.get(index);
                    mSetModeDeathHandlers.remove(index);
                    postUpdateAudioMode(SENDMSG_QUEUE, AudioSystem.MODE_CURRENT,
                            android.os.Process.myPid(), mContext.getPackageName(),
                            false /*signal*/, 0);
                    sendMsg(mAudioHandler,
                            MSG_UPDATE_AUDIO_MODE,
                            SENDMSG_QUEUE,
                            AudioSystem.MODE_CURRENT,
                            android.os.Process.myPid(),
                            mContext.getPackageName(),
                            0);
                }
            }
        }
@@ -6436,14 +6407,19 @@ public class AudioService extends IAudioService.Stub
                }
            }
            postUpdateAudioMode(SENDMSG_REPLACE, mode, pid, callingPackage,
                    hasModifyPhoneStatePermission && mode == AudioSystem.MODE_NORMAL, 0);
            sendMsg(mAudioHandler,
                    MSG_UPDATE_AUDIO_MODE,
                    SENDMSG_REPLACE,
                    mode,
                    pid,
                    callingPackage,
                    0);
        }
    }
    @GuardedBy("mDeviceBroker.mSetModeLock")
    void onUpdateAudioMode(int requestedMode, int requesterPid, String requesterPackage,
                           boolean force, boolean signal) {
                           boolean force) {
        if (requestedMode == AudioSystem.MODE_CURRENT) {
            requestedMode = getMode();
        }
@@ -6458,7 +6434,7 @@ public class AudioService extends IAudioService.Stub
        }
        if (DEBUG_MODE) {
            Log.v(TAG, "onUpdateAudioMode() new mode: " + mode + ", current mode: "
                    + mMode.get() + " requested mode: " + requestedMode + " signal: " + signal);
                    + mMode.get() + " requested mode: " + requestedMode);
        }
        if (mode != mMode.get() || force) {
            int status = AudioSystem.SUCCESS;
@@ -6504,7 +6480,7 @@ public class AudioService extends IAudioService.Stub
                // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all SCO
                // connections not started by the application changing the mode when pid changes
                mDeviceBroker.postSetModeOwner(mode, pid, uid, signal);
                mDeviceBroker.postSetModeOwner(mode, pid, uid);
            } else {
                Log.w(TAG, "onUpdateAudioMode: failed to set audio mode to: " + mode);
            }
@@ -10186,7 +10162,7 @@ public class AudioService extends IAudioService.Stub
                        h.setRecordingActive(isRecordingActiveForUid(h.getUid()));
                        if (wasActive != h.isActive()) {
                            onUpdateAudioMode(AudioSystem.MODE_CURRENT, android.os.Process.myPid(),
                                    mContext.getPackageName(), false /*force*/, false /*signal*/);
                                    mContext.getPackageName(), false /*force*/);
                        }
                    }
                    break;
@@ -10216,9 +10192,7 @@ public class AudioService extends IAudioService.Stub
                case MSG_UPDATE_AUDIO_MODE:
                    synchronized (mDeviceBroker.mSetModeLock) {
                        UpdateAudioModeInfo info = (UpdateAudioModeInfo) msg.obj;
                        onUpdateAudioMode(info.getMode(), info.getPid(), info.getPackageName(),
                                false /*force*/, info.getSignal());
                        onUpdateAudioMode(msg.arg1, msg.arg2, (String) msg.obj, false /*force*/);
                    }
                    break;
@@ -10921,59 +10895,9 @@ public class AudioService extends IAudioService.Stub
            return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
        }
        mmi.record();
        //delay abandon focus requests from Telecom if an audio mode reset from Telecom
        // is still being processed
        final boolean abandonFromTelecom = (mContext.checkCallingOrSelfPermission(
                    MODIFY_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)
                && ((aa != null && aa.getUsage() == AudioAttributes.USAGE_VOICE_COMMUNICATION)
                        || AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId));
        if (abandonFromTelecom) {
            synchronized (mAudioModeResetLock) {
                final long start = java.lang.System.currentTimeMillis();
                long elapsed = 0;
                while (mAudioModeResetCount > 0) {
                    if (DEBUG_MODE) {
                        Log.i(TAG, "Abandon focus from Telecom, waiting for mode change");
                    }
                    try {
                        mAudioModeResetLock.wait(
                                AUDIO_MODE_RESET_TIMEOUT_MS - elapsed);
                    } catch (InterruptedException e) {
                        Log.w(TAG, "Interrupted while waiting for audio mode reset");
                    }
                    elapsed = java.lang.System.currentTimeMillis() - start;
                    if (elapsed >= AUDIO_MODE_RESET_TIMEOUT_MS) {
                        Log.e(TAG, "Timeout waiting for audio mode reset");
                        break;
                    }
                }
                if (DEBUG_MODE && elapsed != 0) {
                    Log.i(TAG, "Abandon focus from Telecom done waiting");
                }
            }
        }
        return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa, callingPackageName);
    }
    /** synchronization between setMode(NORMAL) and abandonAudioFocus() frmo Telecom */
    private static final long AUDIO_MODE_RESET_TIMEOUT_MS = 3000;
    private final Object mAudioModeResetLock = new Object();
    @GuardedBy("mAudioModeResetLock")
    private int mAudioModeResetCount = 0;
    void decrementAudioModeResetCount() {
        synchronized (mAudioModeResetLock) {
            if (mAudioModeResetCount > 0) {
                mAudioModeResetCount--;
            } else {
                Log.w(TAG, "mAudioModeResetCount already 0");
            }
            mAudioModeResetLock.notify();
        }
    }
    /** see {@link AudioManager#abandonAudioFocusForTest(AudioFocusRequest, String)} */
    public int abandonAudioFocusForTest(IAudioFocusDispatcher fd, String clientId,
            AudioAttributes aa, String callingPackageName) {