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

Commit 2cd68694 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "MediaRoute2Info: Reorder methods / fields"

parents 75d45584 0d934524
Loading
Loading
Loading
Loading
+238 −220
Original line number Diff line number Diff line
@@ -134,105 +134,52 @@ public final class MediaRoute2Info implements Parcelable {
     */
    public static final int DEVICE_TYPE_BLUETOOTH = 3;

    @NonNull
    final String mId;
    @Nullable
    final String mProviderId;
    @NonNull
    final CharSequence mName;
    @Nullable
    final CharSequence mDescription;
    @Nullable
    final @ConnectionState int mConnectionState;
    @Nullable
    final List<String> mFeatures;
    @DeviceType
    final int mDeviceType;
    final Uri mIconUri;
    @Nullable
    final CharSequence mDescription;
    @ConnectionState
    final int mConnectionState;
    final String mClientPackageName;
    @NonNull
    final List<String> mFeatures;
    final int mVolume;
    final int mVolumeMax;
    final int mVolumeHandling;
    final @DeviceType int mDeviceType;
    @Nullable
    final Bundle mExtras;
    final String mProviderId;

    MediaRoute2Info(@NonNull Builder builder) {
        mId = builder.mId;
        mProviderId = builder.mProviderId;
        mName = builder.mName;
        mFeatures = builder.mFeatures;
        mDeviceType = builder.mDeviceType;
        mIconUri = builder.mIconUri;
        mDescription = builder.mDescription;
        mConnectionState = builder.mConnectionState;
        mIconUri = builder.mIconUri;
        mClientPackageName = builder.mClientPackageName;
        mFeatures = builder.mFeatures;
        mVolume = builder.mVolume;
        mVolumeMax = builder.mVolumeMax;
        mVolumeHandling = builder.mVolumeHandling;
        mDeviceType = builder.mDeviceType;
        mVolumeMax = builder.mVolumeMax;
        mVolume = builder.mVolume;
        mExtras = builder.mExtras;
        mProviderId = builder.mProviderId;
    }

    MediaRoute2Info(@NonNull Parcel in) {
        mId = in.readString();
        mProviderId = in.readString();
        mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
        mFeatures = in.createStringArrayList();
        mDeviceType = in.readInt();
        mIconUri = in.readParcelable(null);
        mDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
        mConnectionState = in.readInt();
        mIconUri = in.readParcelable(null);
        mClientPackageName = in.readString();
        mFeatures = in.createStringArrayList();
        mVolume = in.readInt();
        mVolumeMax = in.readInt();
        mVolumeHandling = in.readInt();
        mDeviceType = in.readInt();
        mVolumeMax = in.readInt();
        mVolume = in.readInt();
        mExtras = in.readBundle();
    }

    /**
     * Returns true if the route info has all of the required field.
     * A route info only obtained from {@link com.android.server.media.MediaRouterService}
     * is valid.
     * @hide
     */
    //TODO: Reconsider the validity of a route info when fields are added.
    public boolean isValid() {
        if (TextUtils.isEmpty(getId()) || TextUtils.isEmpty(getName())
                || TextUtils.isEmpty(getProviderId())) {
            return false;
        }
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof MediaRoute2Info)) {
            return false;
        }
        MediaRoute2Info other = (MediaRoute2Info) obj;
        return Objects.equals(mId, other.mId)
                && Objects.equals(mProviderId, other.mProviderId)
                && Objects.equals(mName, other.mName)
                && Objects.equals(mDescription, other.mDescription)
                && (mConnectionState == other.mConnectionState)
                && Objects.equals(mIconUri, other.mIconUri)
                && Objects.equals(mClientPackageName, other.mClientPackageName)
                && Objects.equals(mFeatures, other.mFeatures)
                && (mVolume == other.mVolume)
                && (mVolumeMax == other.mVolumeMax)
                && (mVolumeHandling == other.mVolumeHandling)
                && (mDeviceType == other.mDeviceType)
                //TODO: This will be evaluated as false in most cases. Try not to.
                && Objects.equals(mExtras, other.mExtras);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mId, mName, mDescription, mConnectionState, mIconUri,
                mFeatures, mVolume, mVolumeMax, mVolumeHandling, mDeviceType);
        mProviderId = in.readString();
    }

    /**
@@ -254,31 +201,48 @@ public final class MediaRoute2Info implements Parcelable {
    }

    /**
     * Gets the original id set by {@link Builder#Builder(String, CharSequence)}.
     * @hide
     * Gets the user-visible name of the route.
     */
    @NonNull
    public String getOriginalId() {
        return mId;
    public CharSequence getName() {
        return mName;
    }

    /**
     * Gets the provider id of the route. It is assigned automatically by
     * {@link com.android.server.media.MediaRouterService}.
     * Gets the supported features of the route.
     */
    @NonNull
    public List<String> getFeatures() {
        return mFeatures;
    }

    /**
     * Gets the type of the receiver device associated with this route.
     *
     * @return provider id of the route or null if it's not set.
     * @hide
     * @return The type of the receiver device associated with this route:
     * {@link #DEVICE_TYPE_REMOTE_TV}, {@link #DEVICE_TYPE_REMOTE_SPEAKER},
     * {@link #DEVICE_TYPE_BLUETOOTH}.
     */
    @Nullable
    public String getProviderId() {
        return mProviderId;
    @DeviceType
    public int getDeviceType() {
        return mDeviceType;
    }

    @NonNull
    public CharSequence getName() {
        return mName;
    /**
     * Gets the URI of the icon representing this route.
     * <p>
     * This icon will be used in picker UIs if available.
     *
     * @return The URI of the icon representing this route, or null if none.
     */
    @Nullable
    public Uri getIconUri() {
        return mIconUri;
    }

    /**
     * Gets the user-visible description of the route.
     */
    @Nullable
    public CharSequence getDescription() {
        return mDescription;
@@ -295,21 +259,9 @@ public final class MediaRoute2Info implements Parcelable {
        return mConnectionState;
    }

    /**
     * Gets the URI of the icon representing this route.
     * <p>
     * This icon will be used in picker UIs if available.
     *
     * @return The URI of the icon representing this route, or null if none.
     */
    @Nullable
    public Uri getIconUri() {
        return mIconUri;
    }

    /**
     * Gets the package name of the client that uses the route.
     * Returns null if no clients use this.
     * Returns null if no clients use this route.
     * @hide
     */
    @Nullable
@@ -318,23 +270,19 @@ public final class MediaRoute2Info implements Parcelable {
    }

    /**
     * Gets the supported categories of the route.
     * Gets information about how volume is handled on the route.
     *
     * @return {@link #PLAYBACK_VOLUME_FIXED} or {@link #PLAYBACK_VOLUME_VARIABLE}
     */
    @NonNull
    public List<String> getFeatures() {
        return mFeatures;
    public int getVolumeHandling() {
        return mVolumeHandling;
    }

    /**
     * Gets the type of the receiver device associated with this route.
     *
     * @return The type of the receiver device associated with this route:
     * {@link #DEVICE_TYPE_REMOTE_TV}, {@link #DEVICE_TYPE_REMOTE_SPEAKER},
     * {@link #DEVICE_TYPE_BLUETOOTH}.
     * Gets the maximum volume of the route.
     */
    @DeviceType
    public int getDeviceType() {
        return mDeviceType;
    public int getVolumeMax() {
        return mVolumeMax;
    }

    /**
@@ -344,25 +292,30 @@ public final class MediaRoute2Info implements Parcelable {
        return mVolume;
    }

    @Nullable
    public Bundle getExtras() {
        return mExtras == null ? null : new Bundle(mExtras);
    }

    /**
     * Gets the maximum volume of the route.
     * Gets the original id set by {@link Builder#Builder(String, CharSequence)}.
     * @hide
     */
    public int getVolumeMax() {
        return mVolumeMax;
    @NonNull
    public String getOriginalId() {
        return mId;
    }

    /**
     * Gets information about how volume is handled on the route.
     * Gets the provider id of the route. It is assigned automatically by
     * {@link com.android.server.media.MediaRouterService}.
     *
     * @return {@link #PLAYBACK_VOLUME_FIXED} or {@link #PLAYBACK_VOLUME_VARIABLE}
     * @return provider id of the route or null if it's not set.
     * @hide
     */
    public int getVolumeHandling() {
        return mVolumeHandling;
    }

    @Nullable
    public Bundle getExtras() {
        return mExtras;
    public String getProviderId() {
        return mProviderId;
    }

    /**
@@ -381,65 +334,117 @@ public final class MediaRoute2Info implements Parcelable {
        return false;
    }

    /**
     * Returns true if the route info has all of the required field.
     * A route info only obtained from {@link com.android.server.media.MediaRouterService}
     * is valid.
     * @hide
     */
    //TODO: Reconsider the validity of a route info when fields are added.
    public boolean isValid() {
        if (TextUtils.isEmpty(getId()) || TextUtils.isEmpty(getName())
                || TextUtils.isEmpty(getProviderId())) {
            return false;
        }
        return true;
    }

    @Override
    public int describeContents() {
        return 0;
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof MediaRoute2Info)) {
            return false;
        }
        MediaRoute2Info other = (MediaRoute2Info) obj;

        // Note: mExtras is not included.
        return Objects.equals(mId, other.mId)
                && Objects.equals(mName, other.mName)
                && Objects.equals(mFeatures, other.mFeatures)
                && (mDeviceType == other.mDeviceType)
                && Objects.equals(mIconUri, other.mIconUri)
                && Objects.equals(mDescription, other.mDescription)
                && (mConnectionState == other.mConnectionState)
                && Objects.equals(mClientPackageName, other.mClientPackageName)
                && (mVolumeHandling == other.mVolumeHandling)
                && (mVolumeMax == other.mVolumeMax)
                && (mVolume == other.mVolume)
                && Objects.equals(mProviderId, other.mProviderId);
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeString(mId);
        dest.writeString(mProviderId);
        TextUtils.writeToParcel(mName, dest, flags);
        TextUtils.writeToParcel(mDescription, dest, flags);
        dest.writeInt(mConnectionState);
        dest.writeParcelable(mIconUri, flags);
        dest.writeString(mClientPackageName);
        dest.writeStringList(mFeatures);
        dest.writeInt(mVolume);
        dest.writeInt(mVolumeMax);
        dest.writeInt(mVolumeHandling);
        dest.writeInt(mDeviceType);
        dest.writeBundle(mExtras);
    public int hashCode() {
        // Note: mExtras is not included.
        return Objects.hash(mId, mName, mFeatures, mDeviceType, mIconUri, mDescription,
                mConnectionState, mClientPackageName, mVolumeHandling, mVolumeMax, mVolume,
                mProviderId);
    }

    @Override
    public String toString() {
        // Note: mExtras is not printed here.
        StringBuilder result = new StringBuilder()
                .append("MediaRouteInfo{ ")
                .append("MediaRoute2Info{ ")
                .append("id=").append(getId())
                .append(", name=").append(getName())
                .append(", features=").append(getFeatures())
                .append(", deviceType=").append(getDeviceType())
                .append(", iconUri=").append(getIconUri())
                .append(", description=").append(getDescription())
                .append(", connectionState=").append(getConnectionState())
                .append(", iconUri=").append(getIconUri())
                .append(", volume=").append(getVolume())
                .append(", volumeMax=").append(getVolumeMax())
                .append(", clientPackageName=").append(getClientPackageName())
                .append(", volumeHandling=").append(getVolumeHandling())
                .append(", deviceType=").append(getDeviceType())
                .append(", volumeMax=").append(getVolumeMax())
                .append(", volume=").append(getVolume())
                .append(", providerId=").append(getProviderId())
                .append(" }");
        return result.toString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeString(mId);
        TextUtils.writeToParcel(mName, dest, flags);
        dest.writeStringList(mFeatures);
        dest.writeInt(mDeviceType);
        dest.writeParcelable(mIconUri, flags);
        TextUtils.writeToParcel(mDescription, dest, flags);
        dest.writeInt(mConnectionState);
        dest.writeString(mClientPackageName);
        dest.writeInt(mVolumeHandling);
        dest.writeInt(mVolumeMax);
        dest.writeInt(mVolume);
        dest.writeBundle(mExtras);
        dest.writeString(mProviderId);
    }

    /**
     * Builder for {@link MediaRoute2Info media route info}.
     */
    public static final class Builder {
        final String mId;
        String mProviderId;
        final CharSequence mName;
        final List<String> mFeatures;

        @DeviceType
        int mDeviceType = DEVICE_TYPE_UNKNOWN;
        Uri mIconUri;
        CharSequence mDescription;
        @ConnectionState
        int mConnectionState;
        Uri mIconUri;
        String mClientPackageName;
        List<String> mFeatures;
        int mVolume;
        int mVolumeMax;
        int mVolumeHandling = PLAYBACK_VOLUME_FIXED;
        @DeviceType
        int mDeviceType = DEVICE_TYPE_UNKNOWN;
        int mVolumeMax;
        int mVolume;
        Bundle mExtras;
        String mProviderId;

        /**
         * Constructor for builder to create {@link MediaRoute2Info}.
@@ -448,8 +453,8 @@ public final class MediaRoute2Info implements Parcelable {
         * obtained from {@link MediaRouter2} can be different from what was set in
         * {@link MediaRoute2ProviderService}.
         * </p>
         * @param id
         * @param name
         * @param id The ID of the route. Must not be empty.
         * @param name The user-visible name of the route.
         */
        public Builder(@NonNull String id, @NonNull CharSequence name) {
            if (TextUtils.isEmpty(id)) {
@@ -463,62 +468,71 @@ public final class MediaRoute2Info implements Parcelable {
            mFeatures = new ArrayList<>();
        }

        /**
         * Constructor for builder to create {@link MediaRoute2Info} with
         * existing {@link MediaRoute2Info} instance.
         *
         * @param routeInfo the existing instance to copy data from.
         */
        public Builder(@NonNull MediaRoute2Info routeInfo) {
            if (routeInfo == null) {
                throw new IllegalArgumentException("route info must not be null");
            }
            Objects.requireNonNull(routeInfo, "routeInfo must not be null");

            mId = routeInfo.mId;
            mName = routeInfo.mName;

            if (!TextUtils.isEmpty(routeInfo.mProviderId)) {
                setProviderId(routeInfo.mProviderId);
            }
            mFeatures = new ArrayList<>(routeInfo.mFeatures);
            mDeviceType = routeInfo.mDeviceType;
            mIconUri = routeInfo.mIconUri;
            mDescription = routeInfo.mDescription;
            mConnectionState = routeInfo.mConnectionState;
            mIconUri = routeInfo.mIconUri;
            setClientPackageName(routeInfo.mClientPackageName);
            mFeatures = new ArrayList<>(routeInfo.mFeatures);
            setVolume(routeInfo.mVolume);
            setVolumeMax(routeInfo.mVolumeMax);
            setVolumeHandling(routeInfo.mVolumeHandling);
            setDeviceType(routeInfo.mDeviceType);
            mClientPackageName = routeInfo.mClientPackageName;
            mVolumeHandling = routeInfo.mVolumeHandling;
            mVolumeMax = routeInfo.mVolumeMax;
            mVolume = routeInfo.mVolume;
            if (routeInfo.mExtras != null) {
                mExtras = new Bundle(routeInfo.mExtras);
            }
            mProviderId = routeInfo.mProviderId;
        }

        /**
         * Sets the provider id of the route.
         * @hide
         * Adds a feature for the route.
         */
        @NonNull
        public Builder setProviderId(@NonNull String providerId) {
            if (TextUtils.isEmpty(providerId)) {
                throw new IllegalArgumentException("providerId must not be null or empty");
        public Builder addFeature(@NonNull String feature) {
            if (TextUtils.isEmpty(feature)) {
                throw new IllegalArgumentException("feature must not be null or empty");
            }
            mProviderId = providerId;
            mFeatures.add(feature);
            return this;
        }

        /**
         * Sets the user-visible description of the route.
         * Adds features for the route. A route must support at least one route type.
         */
        @NonNull
        public Builder setDescription(@Nullable CharSequence description) {
            mDescription = description;
        public Builder addFeatures(@NonNull Collection<String> features) {
            Objects.requireNonNull(features, "features must not be null");
            for (String feature : features) {
                addFeature(feature);
            }
            return this;
        }

        /**
        * Sets the route's connection state.
        *
        * {@link #CONNECTION_STATE_DISCONNECTED},
        * {@link #CONNECTION_STATE_CONNECTING}, or
        * {@link #CONNECTION_STATE_CONNECTED}.
         * Clears the features of the route. A route must support at least one route type.
         */
        @NonNull
        public Builder setConnectionState(@ConnectionState int connectionState) {
            mConnectionState = connectionState;
        public Builder clearFeatures() {
            mFeatures.clear();
            return this;
        }

        /**
         * Sets the route's device type.
         */
        @NonNull
        public Builder setDeviceType(@DeviceType int deviceType) {
            mDeviceType = deviceType;
            return this;
        }

@@ -543,53 +557,42 @@ public final class MediaRoute2Info implements Parcelable {
        }

        /**
         * Sets the package name of the app using the route.
         */
        @NonNull
        public Builder setClientPackageName(@Nullable String packageName) {
            mClientPackageName = packageName;
            return this;
        }

        /**
         * Clears the features of the route.
         * Sets the user-visible description of the route.
         */
        @NonNull
        public Builder clearFeatures() {
            mFeatures = new ArrayList<>();
        public Builder setDescription(@Nullable CharSequence description) {
            mDescription = description;
            return this;
        }

        /**
         * Adds features for the route.
        * Sets the route's connection state.
        *
        * {@link #CONNECTION_STATE_DISCONNECTED},
        * {@link #CONNECTION_STATE_CONNECTING}, or
        * {@link #CONNECTION_STATE_CONNECTED}.
        */
        @NonNull
        public Builder addFeatures(@NonNull Collection<String> features) {
            Objects.requireNonNull(features, "features must not be null");
            for (String feature : features) {
                addFeature(feature);
            }
        public Builder setConnectionState(@ConnectionState int connectionState) {
            mConnectionState = connectionState;
            return this;
        }

        /**
         * Adds a feature for the route.
         * Sets the package name of the app using the route.
         */
        @NonNull
        public Builder addFeature(@NonNull String feature) {
            if (TextUtils.isEmpty(feature)) {
                throw new IllegalArgumentException("feature must not be null or empty");
            }
            mFeatures.add(feature);
        public Builder setClientPackageName(@Nullable String packageName) {
            mClientPackageName = packageName;
            return this;
        }

        /**
         * Sets the route's current volume, or 0 if unknown.
         * Sets the route's volume handling.
         */
        @NonNull
        public Builder setVolume(int volume) {
            mVolume = volume;
        public Builder setVolumeHandling(int volumeHandling) {
            mVolumeHandling = volumeHandling;
            return this;
        }

@@ -603,37 +606,52 @@ public final class MediaRoute2Info implements Parcelable {
        }

        /**
         * Sets the route's volume handling.
         * Sets the route's current volume, or 0 if unknown.
         */
        @NonNull
        public Builder setVolumeHandling(int volumeHandling) {
            mVolumeHandling = volumeHandling;
        public Builder setVolume(int volume) {
            mVolume = volume;
            return this;
        }

        /**
         * Sets the route's device type.
         * Sets a bundle of extras for the route.
         * <p>
         * Note: The extras will not affect the result of {@link MediaRoute2Info#equals(Object)}.
         */
        @NonNull
        public Builder setDeviceType(@DeviceType int deviceType) {
            mDeviceType = deviceType;
        public Builder setExtras(@Nullable Bundle extras) {
            if (extras == null) {
                mExtras = null;
                return this;
            }
            mExtras = new Bundle(extras);
            return this;
        }

        /**
         * Sets a bundle of extras for the route.
         * Sets the provider id of the route.
         * @hide
         */
        @NonNull
        public Builder setExtras(@Nullable Bundle extras) {
            mExtras = new Bundle(extras);
        public Builder setProviderId(@NonNull String providerId) {
            if (TextUtils.isEmpty(providerId)) {
                throw new IllegalArgumentException("providerId must not be null or empty");
            }
            mProviderId = providerId;
            return this;
        }

        /**
         * Builds the {@link MediaRoute2Info media route info}.
         *
         * @throws IllegalArgumentException if no features are added.
         */
        @NonNull
        public MediaRoute2Info build() {
            if (mFeatures.isEmpty()) {
                throw new IllegalArgumentException("features must not be empty!");
            }
            return new MediaRoute2Info(this);
        }
    }
+363 −0

File added.

Preview size limit exceeded, changes collapsed.

+3 −56

File changed.

Preview size limit exceeded, changes collapsed.

+0 −15
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import static android.media.MediaRoute2Info.PLAYBACK_VOLUME_FIXED;
import static android.media.MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@@ -143,20 +142,6 @@ public class MediaRouterManagerTest {
        clearCallbacks();
    }

    //TODO: Move to a separate file
    @Test
    public void testMediaRoute2Info() {
        MediaRoute2Info routeInfo1 = new MediaRoute2Info.Builder("id", "name")
                .build();
        MediaRoute2Info routeInfo2 = new MediaRoute2Info.Builder(routeInfo1).build();

        MediaRoute2Info routeInfo3 = new MediaRoute2Info.Builder(routeInfo1)
                .setClientPackageName(mPackageName).build();

        assertEquals(routeInfo1, routeInfo2);
        assertNotEquals(routeInfo1, routeInfo3);
    }

    /**
     * Tests if routes are added correctly when a new callback is registered.
     */