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

Commit 07ccd337 authored by David Duarte's avatar David Duarte
Browse files

BrowsablePlayerConnector: Always remove pending players on handler

After processing a message on it's Handler, BrowsablePlayerConnector
checks if there is no pending players to call a callback with all the
result.

The code handling MSG_CONNECT_CB was calling a function with a
callback and removing the player within this callback, as it's in a
different function that the Handler#processMessage, the check to
see if it was the last player was not executed and would only
run on the next message.

The Bluetooth app also registers a MediaBrowserService and this
one usually comes last, this player doesn't have any folder so it
was removed from within the callback (results.size() == 0) and the
only message happening after that was CONNECT_TIMEOUT_MS after 10s.

This means that you would only be able to do browsing on AVRCP only
if you opened AVRCP 10s after the startup of the stack.

pts-bot was triggering both cases, connecting before and after 10s
which made the test flaky.

Test: atest pts-bot:AVRCP/TG/MCN/CB/BI-01-C --iterations 100
Fix: 287463103
Change-Id: I8339a2e58a4880e6cfd0ebc2f2f2653da97853da
parent ec391bf5
Loading
Loading
Loading
Loading
+19 −18
Original line number Diff line number Diff line
@@ -119,16 +119,21 @@ public class BrowsablePlayerConnector {
                if (DEBUG) Log.d(TAG, "Received a message: msg.what=" + msg.what);
                switch(msg.what) {
                    case MSG_GET_FOLDER_ITEMS_CB: {
                        int status = msg.arg1;
                        int results_size = msg.arg2;
                        BrowsedPlayerWrapper wrapper = (BrowsedPlayerWrapper) msg.obj;

                        // If we failed to remove the wrapper from the pending set, that
                        // means a timeout occurred and the callback was triggered afterwards
                        if (!mPendingPlayers.remove(wrapper)) {
                            return;
                        }

                        if (status == BrowsedPlayerWrapper.STATUS_SUCCESS && results_size != 0) {
                            Log.i(TAG, "Successfully added package to results: "
                                    + wrapper.getPackageName());
                            mResults.add(wrapper);
                        }
                    } break;

                    case MSG_CONNECT_CB: {
@@ -136,9 +141,13 @@ public class BrowsablePlayerConnector {

                        if (msg.arg1 != BrowsedPlayerWrapper.STATUS_SUCCESS) {
                            Log.i(TAG, wrapper.getPackageName() + " is not browsable");
                            mPendingPlayers.remove(wrapper);
                            // If we failed to remove the wrapper from the pending set, that
                            // means a timeout occurred and the callback was triggered afterwards
                            if (!mPendingPlayers.remove(wrapper)) {
                                return;
                            }
                            break;
                        }

                        // Check to see if the root folder has any items
                        if (DEBUG) {
@@ -146,22 +155,14 @@ public class BrowsablePlayerConnector {
                        }
                        wrapper.getFolderItems(wrapper.getRootId(),
                                (int status, String mediaId, List<ListItem> results) -> {
                                    if (status != BrowsedPlayerWrapper.STATUS_SUCCESS) {
                                        mPendingPlayers.remove(wrapper);
                                        return;
                                    }

                                    if (results.size() == 0) {
                                        mPendingPlayers.remove(wrapper);
                                        return;
                                    }

                                    // Send the response as a message so that it is properly
                                    // synchronized
                                    Message success =
                                    Message cb =
                                            mHandler.obtainMessage(MSG_GET_FOLDER_ITEMS_CB);
                                    success.obj = wrapper;
                                    mHandler.sendMessage(success);
                                    cb.arg1 = status;
                                    cb.arg2 = results.size();
                                    cb.obj = wrapper;
                                    mHandler.sendMessage(cb);
                                });
                    } break;