Put system live audio only routes at bottom of MediaRouteChooserDialog

We don't yet have a way to tell whether a given media route includes a
bluetooth endpoint. This code change tries to make the best guess based
on the currently available route information only. Future work, possibly
API changes might be required to make route sorting more accurate.

Bug: 23116377
Change-Id: Iad2b3fd8ef7f650b96ca94d6686c7dd0e12ae661
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
index 779ae8b..9dea547 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
@@ -20,8 +20,9 @@
 import android.content.Context;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
-import android.support.v7.media.MediaRouter;
+import android.support.v7.media.MediaControlIntent;
 import android.support.v7.media.MediaRouteSelector;
+import android.support.v7.media.MediaRouter;
 import android.support.v7.mediarouter.R;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
@@ -260,11 +261,32 @@
     }
 
     private static final class RouteComparator implements Comparator<MediaRouter.RouteInfo> {
+        // Should match to SystemMediaRouteProvider.PACKAGE_NAME.
+        static final String SYSTEM_MEDIA_ROUTE_PROVIDER_PACKAGE_NAME = "android";
+
         public static final RouteComparator sInstance = new RouteComparator();
 
         @Override
         public int compare(MediaRouter.RouteInfo lhs, MediaRouter.RouteInfo rhs) {
+            if (isSystemLiveAudioOnlyRoute(lhs))  {
+                if (!isSystemLiveAudioOnlyRoute(rhs)) {
+                    return 1;
+                }
+            } else if (isSystemLiveAudioOnlyRoute(rhs)) {
+                return -1;
+            }
             return lhs.getName().compareTo(rhs.getName());
         }
+
+        private boolean isSystemLiveAudioOnlyRoute(MediaRouter.RouteInfo route) {
+            return isSystemMediaRouteProvider(route)
+                    && route.supportsControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
+                    && !route.supportsControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO);
+        }
+
+        private 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/media/MediaRouter.java b/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
index d37ed95..67d1bb1 100644
--- a/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
+++ b/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
@@ -17,7 +17,6 @@
 package android.support.v7.media;
 
 import android.app.ActivityManager;
-import android.app.PendingIntent;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -1340,7 +1339,8 @@
             return mDescriptorId;
         }
 
-        MediaRouteProvider getProviderInstance() {
+        /** @hide */
+        public MediaRouteProvider getProviderInstance() {
             return mProvider.getProviderInstance();
         }
     }