AVRCP: Launch player when selected and not running

When a carkit selects a player, start it if we don't have a session
running, instead of trying to start it's browsing service instead.

Test: select browsable player as active player from carkit
Change-Id: I50a5817daf9e9a666871f01c22ffbe7fedb15ec1
Bug: 37919422
(cherry picked from commit eb3c55797d3ba16916327effa6b36acec5f1de36)
(cherry picked from commit 07331cbd302f65c1cc72be3f7f20004127cdd1fb)
diff --git a/src/com/android/bluetooth/avrcp/Avrcp.java b/src/com/android/bluetooth/avrcp/Avrcp.java
index a668979..4573ace 100644
--- a/src/com/android/bluetooth/avrcp/Avrcp.java
+++ b/src/com/android/bluetooth/avrcp/Avrcp.java
@@ -1488,31 +1488,41 @@
     }
 
     private void setAddressedPlayer(byte[] bdaddr, int selectedId) {
-        int status = AvrcpConstants.RSP_NO_ERROR;
+        String functionTag = "setAddressedPlayer(" + selectedId + "): ";
 
         synchronized (mMediaPlayerInfoList) {
             if (mMediaPlayerInfoList.isEmpty()) {
-                status = AvrcpConstants.RSP_NO_AVBL_PLAY;
-                Log.w(TAG, " No Available Players to set, sending response back ");
-            } else if (!mMediaPlayerInfoList.containsKey(selectedId)) {
-                status = AvrcpConstants.RSP_INV_PLAYER;
-                Log.w(TAG, " Invalid Player id: " + selectedId + " to set, sending response back ");
-            } else if (!isPlayerAlreadyAddressed(selectedId)) {
-                // register new Media Controller Callback and update the current Ids
-                if (!updateCurrentController(selectedId, mCurrBrowsePlayerID)) {
-                    status = AvrcpConstants.RSP_INTERNAL_ERR;
-                    Log.e(TAG, "register for new Address player failed: " + mCurrAddrPlayerID);
-                }
-            } else {
+                Log.w(TAG, functionTag + "no players, send no available players");
+                setAddressedPlayerRspNative(bdaddr, AvrcpConstants.RSP_NO_AVBL_PLAY);
+                return;
+            }
+            if (!mMediaPlayerInfoList.containsKey(selectedId)) {
+                Log.w(TAG, functionTag + "invalid id, sending response back ");
+                setAddressedPlayerRspNative(bdaddr, AvrcpConstants.RSP_INV_PLAYER);
+                return;
+            }
+
+            if (isPlayerAlreadyAddressed(selectedId)) {
                 MediaPlayerInfo info = getAddressedPlayerInfo();
-                Log.i(TAG, "addressed player " + info + "is already focused");
+                Log.i(TAG, functionTag + "player already addressed: " + info);
+                setAddressedPlayerRspNative(bdaddr, AvrcpConstants.RSP_NO_ERROR);
+                return;
+            }
+            // register new Media Controller Callback and update the current IDs
+            if (!updateCurrentController(selectedId, mCurrBrowsePlayerID)) {
+                Log.e(TAG, functionTag + "updateCurrentController failed!");
+                setAddressedPlayerRspNative(bdaddr, AvrcpConstants.RSP_INTERNAL_ERR);
+                return;
+            }
+            // If we don't have a controller, try to launch the player
+            MediaPlayerInfo info = getAddressedPlayerInfo();
+            if (info.getMediaController() == null) {
+                Intent launch = mPackageManager.getLaunchIntentForPackage(info.getPackageName());
+                Log.i(TAG, functionTag + "launching player " + launch);
+                mContext.startActivity(launch);
             }
         }
-
-        if (DEBUG) Log.d(TAG, "setAddressedPlayer for selectedId: " + selectedId +
-                " , status: " + status);
-        // Sending address player response to remote
-        setAddressedPlayerRspNative(bdaddr, status);
+        setAddressedPlayerRspNative(bdaddr, AvrcpConstants.RSP_NO_ERROR);
     }
 
     private void setBrowsedPlayer(byte[] bdaddr, int selectedId) {
@@ -1724,20 +1734,6 @@
         }
     }
 
-    private void startBrowsedPlayer(int browseId) {
-        if (browseId < 0 || browseId >= mBrowsePlayerInfoList.size()) return;
-        BrowsePlayerInfo player = mBrowsePlayerInfoList.get(browseId);
-
-        Intent intent = new Intent();
-        intent.setComponent(new ComponentName(player.packageName, player.serviceClass));
-        Log.i(TAG, "Starting service:" + player.packageName + ", " + player.serviceClass);
-        try {
-            mContext.startService(intent);
-        } catch (SecurityException ex) {
-            Log.e(TAG, "Can't start " + player.serviceClass + ": " + ex.getMessage());
-        }
-    }
-
     /* Initializes list of media players identified from session manager active sessions */
     private void initMediaPlayersList() {
         synchronized (mMediaPlayerInfoList) {
@@ -2109,14 +2105,6 @@
 
         MediaController newController = null;
         MediaPlayerInfo info = getAddressedPlayerInfo();
-        if (info != null) {
-            newController = info.getMediaController();
-            if (newController == null) {
-                // Browsable player, try to start it, which will trigger an update via
-                // MesiaSessionManager
-                startBrowsedPlayer(getBrowseId(info.getPackageName()));
-            }
-        }
 
         if (DEBUG)
             Log.d(TAG, "updateCurrentController: " + mMediaController + " to " + newController);