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

Commit 613a77ad authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Prevent callbacks being called before the constructor

BluetoothRouteProvider requires a listener in the constructor, but
the listener method can be called before the constructor finished.
This CL makes the listemer mether be called after the constructor
finished.

Bug: 149751047
Test: pass MediaRouter2 tests
Change-Id: I3fae3f64253f3f7d3417f71bae76e2f483db3e69
parent e78f4abb
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -85,7 +85,9 @@ class BluetoothRouteProvider {
        mListener = listener;
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        buildBluetoothRoutes();
    }

    public void start() {
        mBluetoothAdapter.getProfileProxy(mContext, mProfileListener, BluetoothProfile.A2DP);
        mBluetoothAdapter.getProfileProxy(mContext, mProfileListener, BluetoothProfile.HEARING_AID);

+37 −39
Original line number Diff line number Diff line
@@ -103,25 +103,20 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
            publishProviderState();

            boolean sessionInfoChanged;
            synchronized (mLock) {
                sessionInfoChanged = updateSessionInfosIfNeededLocked();
            }
            sessionInfoChanged = updateSessionInfosIfNeeded();
            if (sessionInfoChanged) {
                notifySessionInfoUpdated();
            }
        });

        mHandler.post(() -> notifyProviderState());

        //TODO: clean up this
        // This is required because it is not instantiated in the main thread and
        // BluetoothRoutesUpdatedListener can be called before here
        synchronized (mLock) {
            updateSessionInfosIfNeededLocked();
        }
        updateSessionInfosIfNeeded();

        mContext.registerReceiver(new VolumeChangeReceiver(),
                new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION));

        mHandler.post(() -> {
            mBtRouteProvider.start();
            notifyProviderState();
        });
    }

    @Override
@@ -220,10 +215,12 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
    /**
     * Updates the mSessionInfo. Returns true if the session info is changed.
     */
    boolean updateSessionInfosIfNeededLocked() {
    boolean updateSessionInfosIfNeeded() {
        synchronized (mLock) {
            // Prevent to execute this method before mBtRouteProvider is created.
            if (mBtRouteProvider == null) return false;
        RoutingSessionInfo oldSessionInfo = mSessionInfos.isEmpty() ? null : mSessionInfos.get(0);
            RoutingSessionInfo oldSessionInfo = mSessionInfos.isEmpty() ? null : mSessionInfos.get(
                    0);

            RoutingSessionInfo.Builder builder = new RoutingSessionInfo.Builder(
                    SYSTEM_SESSION_ID, "" /* clientPackageName */)
@@ -251,6 +248,7 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
                return true;
            }
        }
    }

    void publishProviderState() {
        updateProviderState();