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

Commit 96d36cee authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

MediaRouter: Add MediaRoute2Info#isSystemRoute

MediaRoute2Info#isSystemRoute returns true only if
it is generated by SystemMediaRoute2Provider.

In addition, MediaRouter2 and MediaRouter2Manager
always include system routes when getRoutes or getAvailableRoutes
is called.

This CL will change the behavior of MediaRouter2#getRoutes(), however,
without this change, there doesn't seem to be a good way to get
system routes. (e.g. how can we support multiple bt routes in xMR?)

Bug: 145632124
Test: atest android.media.cts.MediaRoute2InfoTest
  && atest android.media.cts.MediaRouter2Test
  && atest android.media.cts.RouteDiscoveryPreferenceTest
  && atest android.media.cts.RoutingSessionInfoTest
  && atest mediaroutertest
Change-Id: I18ea188b68e6e1335cb0e25c3cc3674910d4d645
parent 1605af29
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -26729,6 +26729,7 @@ package android.media {
    method public int getVolume();
    method public int getVolumeHandling();
    method public int getVolumeMax();
    method public boolean isSystemRoute();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field public static final int CONNECTION_STATE_CONNECTED = 2; // 0x2
    field public static final int CONNECTION_STATE_CONNECTING = 1; // 0x1
+29 −1
Original line number Diff line number Diff line
@@ -197,6 +197,7 @@ public final class MediaRoute2Info implements Parcelable {
    final List<String> mFeatures;
    @DeviceType
    final int mDeviceType;
    final boolean mIsSystem;
    final Uri mIconUri;
    final CharSequence mDescription;
    @ConnectionState
@@ -213,6 +214,7 @@ public final class MediaRoute2Info implements Parcelable {
        mName = builder.mName;
        mFeatures = builder.mFeatures;
        mDeviceType = builder.mDeviceType;
        mIsSystem = builder.mIsSystem;
        mIconUri = builder.mIconUri;
        mDescription = builder.mDescription;
        mConnectionState = builder.mConnectionState;
@@ -229,6 +231,7 @@ public final class MediaRoute2Info implements Parcelable {
        mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
        mFeatures = in.createStringArrayList();
        mDeviceType = in.readInt();
        mIsSystem = in.readBoolean();
        mIconUri = in.readParcelable(null);
        mDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
        mConnectionState = in.readInt();
@@ -286,6 +289,17 @@ public final class MediaRoute2Info implements Parcelable {
        return mDeviceType;
    }

    /**
     * Returns whether the route is a system route or not.
     * <p>
     * System routes are media routes directly controlled by the system
     * such as phone speaker, wired headset, and Bluetooth devices.
     * </p>
     */
    public boolean isSystemRoute() {
        return mIsSystem;
    }

    /**
     * Gets the URI of the icon representing this route.
     * <p>
@@ -423,6 +437,7 @@ public final class MediaRoute2Info implements Parcelable {
                && Objects.equals(mName, other.mName)
                && Objects.equals(mFeatures, other.mFeatures)
                && (mDeviceType == other.mDeviceType)
                && (mIsSystem == other.mIsSystem)
                && Objects.equals(mIconUri, other.mIconUri)
                && Objects.equals(mDescription, other.mDescription)
                && (mConnectionState == other.mConnectionState)
@@ -436,7 +451,7 @@ public final class MediaRoute2Info implements Parcelable {
    @Override
    public int hashCode() {
        // Note: mExtras is not included.
        return Objects.hash(mId, mName, mFeatures, mDeviceType, mIconUri, mDescription,
        return Objects.hash(mId, mName, mFeatures, mDeviceType, mIsSystem, mIconUri, mDescription,
                mConnectionState, mClientPackageName, mVolumeHandling, mVolumeMax, mVolume,
                mProviderId);
    }
@@ -473,6 +488,7 @@ public final class MediaRoute2Info implements Parcelable {
        TextUtils.writeToParcel(mName, dest, flags);
        dest.writeStringList(mFeatures);
        dest.writeInt(mDeviceType);
        dest.writeBoolean(mIsSystem);
        dest.writeParcelable(mIconUri, flags);
        TextUtils.writeToParcel(mDescription, dest, flags);
        dest.writeInt(mConnectionState);
@@ -494,6 +510,7 @@ public final class MediaRoute2Info implements Parcelable {

        @DeviceType
        int mDeviceType = DEVICE_TYPE_UNKNOWN;
        boolean mIsSystem;
        Uri mIconUri;
        CharSequence mDescription;
        @ConnectionState
@@ -540,6 +557,7 @@ public final class MediaRoute2Info implements Parcelable {
            mName = routeInfo.mName;
            mFeatures = new ArrayList<>(routeInfo.mFeatures);
            mDeviceType = routeInfo.mDeviceType;
            mIsSystem = routeInfo.mIsSystem;
            mIconUri = routeInfo.mIconUri;
            mDescription = routeInfo.mDescription;
            mConnectionState = routeInfo.mConnectionState;
@@ -607,6 +625,16 @@ public final class MediaRoute2Info implements Parcelable {
            return this;
        }

        /**
         * Sets whether the route is a system route or not.
         * @hide
         */
        @NonNull
        public Builder setSystemRoute(boolean isSystem) {
            mIsSystem = isSystem;
            return this;
        }

        /**
         * Sets the URI of the icon representing this route.
         * <p>
+17 −0
Original line number Diff line number Diff line
@@ -179,6 +179,23 @@ public final class MediaRoute2ProviderInfo implements Parcelable {
            return this;
        }

        /**
         * Sets whether the provider provides system routes or not
         */
        @NonNull
        public Builder setSystemRouteProvider(boolean isSystem) {
            int count = mRoutes.size();
            for (int i = 0; i < count; i++) {
                MediaRoute2Info route = mRoutes.valueAt(i);
                if (route.isSystemRoute() != isSystem) {
                    mRoutes.setValueAt(i, new MediaRoute2Info.Builder(route)
                            .setSystemRoute(isSystem)
                            .build());
                }
            }
            return this;
        }

        /**
         * Adds a route to the provider
         */
+12 −1
Original line number Diff line number Diff line
@@ -230,7 +230,11 @@ public class MediaRouter2 {
    /**
     * Gets the unmodifiable list of {@link MediaRoute2Info routes} currently
     * known to the media router.
     * <p>
     * {@link MediaRoute2Info#isSystemRoute() System routes} such as phone speaker,
     * Bluetooth devices are always included in the list.
     * Please note that the list can be changed before callbacks are invoked.
     * </p>
     *
     * @return the list of routes that contains at least one of the route features in discovery
     * preferences registered by the application
@@ -243,7 +247,8 @@ public class MediaRouter2 {

                List<MediaRoute2Info> filteredRoutes = new ArrayList<>();
                for (MediaRoute2Info route : mRoutes.values()) {
                    if (route.hasAnyFeatures(mDiscoveryPreference.getPreferredFeatures())) {
                    if (route.isSystemRoute()
                            || route.hasAnyFeatures(mDiscoveryPreference.getPreferredFeatures())) {
                        filteredRoutes.add(route);
                    }
                }
@@ -307,12 +312,18 @@ public class MediaRouter2 {
     * with the given route.
     *
     * @param route the route you want to create a controller with.
     * @throws IllegalArgumentException if the given route is
     * {@link MediaRoute2Info#isSystemRoute() system route}
     *
     * @see RoutingControllerCallback#onControllerCreated
     * @see RoutingControllerCallback#onControllerCreationFailed
     */
    public void requestCreateController(@NonNull MediaRoute2Info route) {
        Objects.requireNonNull(route, "route must not be null");
        if (route.isSystemRoute()) {
            throw new IllegalArgumentException("Can't create a route controller with "
                    + "a system route. Use getSystemController().");
        }
        // TODO: Check the given route exists

        final int requestId;
+4 −3
Original line number Diff line number Diff line
@@ -161,14 +161,15 @@ public class MediaRouter2Manager {
    public List<MediaRoute2Info> getAvailableRoutes(@NonNull String packageName) {
        Objects.requireNonNull(packageName, "packageName must not be null");

        List<MediaRoute2Info> routes = new ArrayList<>();

        List<String> preferredFeatures = mPreferredFeaturesMap.get(packageName);
        if (preferredFeatures == null) {
            return Collections.emptyList();
            preferredFeatures = Collections.emptyList();
        }
        List<MediaRoute2Info> routes = new ArrayList<>();
        synchronized (mRoutesLock) {
            for (MediaRoute2Info route : mRoutes.values()) {
                if (route.hasAnyFeatures(preferredFeatures)) {
                if (route.isSystemRoute() || route.hasAnyFeatures(preferredFeatures)) {
                    routes.add(route);
                }
            }
Loading