Merge "Fix output switcher will show wrong active device" into rvc-dev am: 8911616d84

Change-Id: I64e58c50ce098642f7f0d3892c05ddd545233ca5
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java b/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java
index 008a433..959c42fe 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java
@@ -56,7 +56,7 @@
 public class InfoMediaManager extends MediaManager {
 
     private static final String TAG = "InfoMediaManager";
-    private static final boolean DEBUG = false;
+    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);;
     @VisibleForTesting
     final RouterManagerCallback mMediaRouterCallback = new RouterManagerCallback();
     @VisibleForTesting
@@ -364,8 +364,8 @@
     private void buildAvailableRoutes() {
         for (MediaRoute2Info route : mRouterManager.getAvailableRoutes(mPackageName)) {
             if (DEBUG) {
-                Log.d(TAG, "buildAvailableRoutes() route : " + route.getName()
-                        + ", type : " + route.getType());
+                Log.d(TAG, "buildAvailableRoutes() route : " + route.getName() + ", volume : "
+                        + route.getVolume() + ", type : " + route.getType());
             }
             addMediaDevice(route);
         }
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java b/packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java
index 7d95f19..f34903c 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java
@@ -394,7 +394,9 @@
         return mPackageName;
     }
 
-    private MediaDevice updateCurrentConnectedDevice() {
+    @VisibleForTesting
+    MediaDevice updateCurrentConnectedDevice() {
+        MediaDevice connectedDevice = null;
         synchronized (mMediaDevicesLock) {
             for (MediaDevice device : mMediaDevices) {
                 if (device instanceof BluetoothMediaDevice) {
@@ -402,12 +404,12 @@
                         return device;
                     }
                 } else if (device instanceof PhoneMediaDevice) {
-                    return device;
+                    connectedDevice = device;
                 }
             }
         }
-        Log.w(TAG, "updateCurrentConnectedDevice() can't found current connected device");
-        return null;
+
+        return connectedDevice;
     }
 
     private boolean isActiveDevice(CachedBluetoothDevice device) {
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaManagerTest.java
index 734866f..c514671 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/InfoMediaManagerTest.java
@@ -590,7 +590,7 @@
         final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
         assertThat(mediaDevice).isNull();
 
-        mInfoMediaManager.mMediaRouterCallback.onTransferred(null, null);
+        mInfoMediaManager.mMediaRouterCallback.onTransferred(sessionInfo, sessionInfo);
 
         final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
         assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
@@ -602,6 +602,7 @@
     @Test
     public void onTransferred_buildAllRoutes_shouldAddMediaDevice() {
         final MediaRoute2Info info = mock(MediaRoute2Info.class);
+        final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
         mInfoMediaManager.registerCallback(mCallback);
 
         when(info.getId()).thenReturn(TEST_ID);
@@ -616,7 +617,7 @@
         assertThat(mediaDevice).isNull();
 
         mInfoMediaManager.mPackageName = "";
-        mInfoMediaManager.mMediaRouterCallback.onTransferred(null, null);
+        mInfoMediaManager.mMediaRouterCallback.onTransferred(sessionInfo, sessionInfo);
 
         final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
         assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/LocalMediaManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/LocalMediaManagerTest.java
index 517071b..009f75a 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/LocalMediaManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/LocalMediaManagerTest.java
@@ -31,6 +31,7 @@
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothClass;
 import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothProfile;
 import android.content.Context;
 import android.media.MediaRoute2Info;
 import android.media.MediaRouter2Manager;
@@ -725,4 +726,44 @@
 
         verify(mInfoMediaManager).adjustSessionVolume(info, 10);
     }
+
+    @Test
+    public void updateCurrentConnectedDevice_bluetoothDeviceIsActive_returnBluetoothDevice() {
+        final BluetoothMediaDevice device1 = mock(BluetoothMediaDevice.class);
+        final BluetoothMediaDevice device2 = mock(BluetoothMediaDevice.class);
+        final PhoneMediaDevice phoneDevice = mock(PhoneMediaDevice.class);
+        final CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
+        final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
+
+        when(device1.getCachedDevice()).thenReturn(cachedDevice1);
+        when(device2.getCachedDevice()).thenReturn(cachedDevice2);
+        when(cachedDevice1.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
+        when(cachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(true);
+
+        mLocalMediaManager.mMediaDevices.add(device1);
+        mLocalMediaManager.mMediaDevices.add(phoneDevice);
+        mLocalMediaManager.mMediaDevices.add(device2);
+
+        assertThat(mLocalMediaManager.updateCurrentConnectedDevice()).isEqualTo(device2);
+    }
+
+    @Test
+    public void updateCurrentConnectedDevice_phoneDeviceIsActive_returnPhoneDevice() {
+        final BluetoothMediaDevice device1 = mock(BluetoothMediaDevice.class);
+        final BluetoothMediaDevice device2 = mock(BluetoothMediaDevice.class);
+        final PhoneMediaDevice phoneDevice = mock(PhoneMediaDevice.class);
+        final CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
+        final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
+
+        when(device1.getCachedDevice()).thenReturn(cachedDevice1);
+        when(device2.getCachedDevice()).thenReturn(cachedDevice2);
+        when(cachedDevice1.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
+        when(cachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
+
+        mLocalMediaManager.mMediaDevices.add(device1);
+        mLocalMediaManager.mMediaDevices.add(phoneDevice);
+        mLocalMediaManager.mMediaDevices.add(device2);
+
+        assertThat(mLocalMediaManager.updateCurrentConnectedDevice()).isEqualTo(phoneDevice);
+    }
 }