MediaRouter: Loosen up the condition to show the play control layout

This partly reverts ag/802026.

Bug: 25595250, Bug: 24888113
Change-Id: I887c96c37c0d0d5e9f18d57cf071323dfc88bf40
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
index 9beb8d9..8e5e2c6 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
@@ -28,7 +28,6 @@
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.support.annotation.NonNull;
-import android.support.v7.media.MediaControlIntent;
 import android.support.v7.media.MediaRouteSelector;
 import android.support.v7.media.MediaRouter;
 import android.support.v7.mediarouter.R;
@@ -65,9 +64,6 @@
 public class MediaRouteChooserDialog extends Dialog {
     private static final String TAG = "MediaRouteChooserDialog";
 
-    // Should match to SystemMediaRouteProvider.PACKAGE_NAME.
-    static final String SYSTEM_MEDIA_ROUTE_PROVIDER_PACKAGE_NAME = "android";
-
     private final MediaRouter mRouter;
     private final MediaRouterCallback mCallback;
 
@@ -322,7 +318,7 @@
                 // Only speakers can be grouped for now.
                 return mSpeakerGroupIcon;
             }
-            if (isSystemLiveAudioOnlyRoute(route)) {
+            if (route.isDeviceTypeBluetooth()) {
                 return mBluetoothIcon;
             }
             return mDefaultIcon;
@@ -365,11 +361,16 @@
 
         @Override
         public int compare(MediaRouter.RouteInfo lhs, MediaRouter.RouteInfo rhs) {
-            if (isSystemLiveAudioOnlyRoute(lhs)) {
-                if (!isSystemLiveAudioOnlyRoute(rhs)) {
+            if (lhs == null) {
+                return rhs == null ? 0 : -1;
+            } else if (rhs == null) {
+                return 1;
+            }
+            if (lhs.isDeviceTypeBluetooth()) {
+                if (!rhs.isDeviceTypeBluetooth()) {
                     return 1;
                 }
-            } else if (isSystemLiveAudioOnlyRoute(rhs)) {
+            } else if (rhs.isDeviceTypeBluetooth()) {
                 return -1;
             }
             Float lhsUsageScore = sRouteUsageScoreMap.get(lhs.getId());
@@ -430,17 +431,4 @@
             prefEditor.commit();
         }
     }
-
-    // Used to determine whether the route represents a bluetooth device.
-    // TODO: Find a better way to precisely detect bluetooth routes.
-    private static boolean isSystemLiveAudioOnlyRoute(MediaRouter.RouteInfo route) {
-        return isSystemMediaRouteProvider(route)
-                && route.supportsControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
-                && !route.supportsControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO);
-    }
-
-    private static boolean isSystemMediaRouteProvider(MediaRouter.RouteInfo route) {
-        return TextUtils.equals(route.getProviderInstance().getMetadata().getPackageName(),
-                SYSTEM_MEDIA_ROUTE_PROVIDER_PACKAGE_NAME);
-    }
 }
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
index 537b382..bf0a9db 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
@@ -37,7 +37,6 @@
 import android.support.v4.media.session.PlaybackStateCompat;
 import android.support.v4.view.accessibility.AccessibilityEventCompat;
 import android.support.v7.graphics.Palette;
-import android.support.v7.media.MediaControlIntent;
 import android.support.v7.media.MediaRouteSelector;
 import android.support.v7.media.MediaRouter;
 import android.support.v7.mediarouter.R;
@@ -467,14 +466,7 @@
     }
 
     private boolean canShowPlaybackControlLayout() {
-        // If a route does not support remote playback, it means that the route is dedicated for
-        // audio or video streaming such as A2DP speaker or headset. In this case, the route
-        // provider does not provide any playback information such as metadata or playback status.
-        // But, for live video, playback control UI shows a message that the screen is being
-        // mirrored, while it does not show anything for live audio.
-        return mCustomControlView == null && (mDescription != null || mState != null)
-                && (mRoute.supportsControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
-                || mRoute.supportsControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO));
+        return mCustomControlView == null && (mDescription != null || mState != null);
     }
 
     /**
@@ -718,8 +710,12 @@
                 mTitleView.setText(R.string.mr_controller_casting_screen);
                 showTitle = true;
             } else if (mState == null || mState.getState() == PlaybackStateCompat.STATE_NONE) {
-                mTitleView.setText(R.string.mr_controller_no_media_selected);
-                showTitle = true;
+                // Show "No media selected" as we don't yet know the playback state.
+                // (Only exception is bluetooth where we don't show anything.)
+                if (!mRoute.isDeviceTypeBluetooth()) {
+                    mTitleView.setText(R.string.mr_controller_no_media_selected);
+                    showTitle = true;
+                }
             } else if (!hasTitle && !hasSubtitle) {
                 mTitleView.setText(R.string.mr_controller_no_info_available);
                 showTitle = true;
diff --git a/v7/mediarouter/src/android/support/v7/media/MediaRouter.java b/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
index 18bcc85..b2e74e4 100644
--- a/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
+++ b/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
@@ -40,6 +40,7 @@
 import android.support.v4.util.Pair;
 import android.support.v7.media.MediaRouteProvider.ProviderMetadata;
 import android.support.v7.media.MediaRouteProvider.RouteController;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.Display;
 
@@ -913,6 +914,9 @@
         static final int CHANGE_VOLUME = 1 << 1;
         static final int CHANGE_PRESENTATION_DISPLAY = 1 << 2;
 
+        // Should match to SystemMediaRouteProvider.PACKAGE_NAME.
+        static final String SYSTEM_MEDIA_ROUTE_PROVIDER_PACKAGE_NAME = "android";
+
         RouteInfo(ProviderInfo provider, String descriptorId, String uniqueId) {
             mProvider = provider;
             mDescriptorId = descriptorId;
@@ -1226,6 +1230,33 @@
             return mDeviceType;
         }
 
+
+        /**
+         * Gets whether the type of the receiver device associated with this route is
+         * {@link #DEVICE_TYPE_BLUETOOTH}.
+         * <p>
+         * This is a workaround for platform version 23 or below where the system route provider
+         * doesn't specify device type for bluetooth media routes.
+         * </p>
+         *
+         * @return True if the receiver device type can be assumed to be
+         *         {@link #DEVICE_TYPE_BLUETOOTH}, false otherwise.
+         * @hide
+         */
+        public boolean isDeviceTypeBluetooth() {
+            if (mDeviceType == DEVICE_TYPE_BLUETOOTH) {
+                return true;
+            }
+            return isSystemMediaRouteProvider(this)
+                    && supportsControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
+                    && !supportsControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO);
+        }
+
+        private static boolean isSystemMediaRouteProvider(MediaRouter.RouteInfo route) {
+            return TextUtils.equals(route.getProviderInstance().getMetadata().getPackageName(),
+                    SYSTEM_MEDIA_ROUTE_PROVIDER_PACKAGE_NAME);
+        }
+
         /**
          * Gets information about how volume is handled on the route.
          *