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

Commit 8c3a767b authored by jiabin's avatar jiabin
Browse files

Avoid race condition when broadcasting device list changed.

Since broadcastDeviceListChanged could be called in different threads,
there would be race condition causing mutilple callback due to
mPreviousPorts is not thread safe.

Bug: 80138804
Test: run TestDeviceList app in toolbox and cts
Change-Id: I0aa70dc45594bca263ea6f36703f22fe0293f679
parent 8dd9ef19
Loading
Loading
Loading
Loading
+21 −19
Original line number Diff line number Diff line
@@ -4717,7 +4717,7 @@ public class AudioManager {
                NativeEventHandlerDelegate delegate =
                        new NativeEventHandlerDelegate(callback, handler);
                mDeviceCallbacks.put(callback, delegate);
                broadcastDeviceListChange(delegate.getHandler());
                broadcastDeviceListChange_sync(delegate.getHandler());
            }
        }
    }
@@ -4836,9 +4836,9 @@ public class AudioManager {

    /**
     * Internal method to compute and generate add/remove messages and then send to any
     * registered callbacks.
     * registered callbacks. Must be called synchronized on mDeviceCallbacks.
     */
    private void broadcastDeviceListChange(Handler handler) {
    private void broadcastDeviceListChange_sync(Handler handler) {
        int status;

        // Get the new current set of ports
@@ -4860,7 +4860,6 @@ public class AudioManager {
            AudioDeviceInfo[] removed_devices =
                    calcListDeltas(current_ports, mPreviousPorts, GET_DEVICES_ALL);
            if (added_devices.length != 0 || removed_devices.length != 0) {
                synchronized (mDeviceCallbacks) {
                for (int i = 0; i < mDeviceCallbacks.size(); i++) {
                    handler = mDeviceCallbacks.valueAt(i).getHandler();
                    if (handler != null) {
@@ -4878,7 +4877,6 @@ public class AudioManager {
                }
            }
        }
        }

        mPreviousPorts = current_ports;
    }
@@ -4889,7 +4887,9 @@ public class AudioManager {
    private class OnAmPortUpdateListener implements AudioManager.OnAudioPortUpdateListener {
        static final String TAG = "OnAmPortUpdateListener";
        public void onAudioPortListUpdate(AudioPort[] portList) {
            broadcastDeviceListChange(null);
            synchronized (mDeviceCallbacks) {
                broadcastDeviceListChange_sync(null);
            }
        }

        /**
@@ -4903,7 +4903,9 @@ public class AudioManager {
         * Callback method called when the mediaserver dies
         */
        public void onServiceDied() {
            broadcastDeviceListChange(null);
            synchronized (mDeviceCallbacks) {
                broadcastDeviceListChange_sync(null);
            }
        }
    }