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

Commit 9b906717 authored by Hyundo Moon's avatar Hyundo Moon Committed by Android (Google) Code Review
Browse files

Merge "RouteSessionInfo: Remove isValid() and override equals()/hashCode()"

parents 94bd736a 459dee33
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -145,7 +145,6 @@ public abstract class MediaRoute2ProviderService extends Service {
    /**
     * Updates the information of a session.
     * If the session is destroyed or not created before, it will be ignored.
     * A session will be destroyed if it has no selected route.
     * Call {@link #updateProviderInfo(MediaRoute2ProviderInfo)} to notify clients of
     * session info changes.
     *
@@ -156,10 +155,6 @@ public abstract class MediaRoute2ProviderService extends Service {
    public final void updateSessionInfo(@NonNull RouteSessionInfo sessionInfo) {
        Objects.requireNonNull(sessionInfo, "sessionInfo must not be null");
        String sessionId = sessionInfo.getId();
        if (sessionInfo.getSelectedRoutes().isEmpty()) {
            releaseSession(sessionId);
            return;
        }

        synchronized (mSessionLock) {
            if (mSessionInfo.containsKey(sessionId)) {
+84 −36
Original line number Diff line number Diff line
@@ -33,8 +33,7 @@ import java.util.Objects;
 * Describes a route session that is made when a media route is selected.
 * @hide
 */
public class RouteSessionInfo implements Parcelable {

public final class RouteSessionInfo implements Parcelable {
    @NonNull
    public static final Creator<RouteSessionInfo> CREATOR =
            new Creator<RouteSessionInfo>() {
@@ -70,6 +69,7 @@ public class RouteSessionInfo implements Parcelable {
        mRouteFeature = builder.mRouteFeature;
        mProviderId = builder.mProviderId;

        // TODO: Needs to check that the routes already have unique IDs.
        mSelectedRoutes = Collections.unmodifiableList(
                convertToUniqueRouteIds(builder.mSelectedRoutes));
        mSelectableRoutes = Collections.unmodifiableList(
@@ -112,18 +112,6 @@ public class RouteSessionInfo implements Parcelable {
        return Collections.emptyList();
    }

    /**
     * Returns whether the session info is valid or not
     *
     * TODO in this CL: Remove this method.
     */
    public boolean isValid() {
        return !TextUtils.isEmpty(mId)
                && !TextUtils.isEmpty(mClientPackageName)
                && !TextUtils.isEmpty(mRouteFeature)
                && mSelectedRoutes.size() > 0;
    }

    /**
     * Gets the id of the session. The sessions which are given by {@link MediaRouter2} will have
     * unique IDs.
@@ -235,6 +223,32 @@ public class RouteSessionInfo implements Parcelable {
        dest.writeBundle(mControlHints);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof RouteSessionInfo)) {
            return false;
        }

        RouteSessionInfo other = (RouteSessionInfo) obj;
        return Objects.equals(mId, other.mId)
                && Objects.equals(mClientPackageName, other.mClientPackageName)
                && Objects.equals(mRouteFeature, other.mRouteFeature)
                && Objects.equals(mProviderId, other.mProviderId)
                && Objects.equals(mSelectedRoutes, other.mSelectedRoutes)
                && Objects.equals(mSelectableRoutes, other.mSelectableRoutes)
                && Objects.equals(mDeselectableRoutes, other.mDeselectableRoutes)
                && Objects.equals(mTransferrableRoutes, other.mTransferrableRoutes);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mId, mClientPackageName, mRouteFeature, mProviderId,
                mSelectedRoutes, mSelectableRoutes, mDeselectableRoutes, mTransferrableRoutes);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder()
@@ -297,6 +311,10 @@ public class RouteSessionInfo implements Parcelable {
         * {@link MediaRoute2ProviderService}.
         * </p>
         *
         * @param id ID of the session. Must not be empty.
         * @param clientPackageName package name of the client app which uses this session.
         *                          If is is unknown, then just use an empty string.
         * @param routeFeature the route feature of session. Must not be empty.
         * @see MediaRoute2Info#getId()
         */
        public Builder(@NonNull String id, @NonNull String clientPackageName,
@@ -304,10 +322,14 @@ public class RouteSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(id)) {
                throw new IllegalArgumentException("id must not be empty");
            }
            Objects.requireNonNull(clientPackageName, "clientPackageName must not be null");
            if (TextUtils.isEmpty(routeFeature)) {
                throw new IllegalArgumentException("routeFeature must not be empty");
            }

            mId = id;
            mClientPackageName = Objects.requireNonNull(
                    clientPackageName, "clientPackageName must not be null");
            mRouteFeature = Objects.requireNonNull(routeFeature, "routeFeature must not be null");
            mClientPackageName = clientPackageName;
            mRouteFeature = routeFeature;
            mSelectedRoutes = new ArrayList<>();
            mSelectableRoutes = new ArrayList<>();
            mDeselectableRoutes = new ArrayList<>();
@@ -338,8 +360,6 @@ public class RouteSessionInfo implements Parcelable {

        /**
         * Sets the provider ID of the session.
         * Also, calling this method will make all type of route IDs be unique by adding
         * {@code providerId:} as a prefix. So do NOT call this method twice on same instance.
         *
         * @hide
         */
@@ -362,20 +382,26 @@ public class RouteSessionInfo implements Parcelable {
        }

        /**
         * Adds a route to the selected routes.
         * Adds a route to the selected routes. The {@code routeId} must not be empty.
         */
        @NonNull
        public Builder addSelectedRoute(@NonNull String routeId) {
            mSelectedRoutes.add(Objects.requireNonNull(routeId, "routeId must not be null"));
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mSelectedRoutes.add(routeId);
            return this;
        }

        /**
         * Removes a route from the selected routes.
         * Removes a route from the selected routes. The {@code routeId} must not be empty.
         */
        @NonNull
        public Builder removeSelectedRoute(@NonNull String routeId) {
            mSelectedRoutes.remove(Objects.requireNonNull(routeId, "routeId must not be null"));
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mSelectedRoutes.remove(routeId);
            return this;
        }

@@ -389,20 +415,26 @@ public class RouteSessionInfo implements Parcelable {
        }

        /**
         * Adds a route to the selectable routes.
         * Adds a route to the selectable routes. The {@code routeId} must not be empty.
         */
        @NonNull
        public Builder addSelectableRoute(@NonNull String routeId) {
            mSelectableRoutes.add(Objects.requireNonNull(routeId, "routeId must not be null"));
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mSelectableRoutes.add(routeId);
            return this;
        }

        /**
         * Removes a route from the selectable routes.
         * Removes a route from the selectable routes. The {@code routeId} must not be empty.
         */
        @NonNull
        public Builder removeSelectableRoute(@NonNull String routeId) {
            mSelectableRoutes.remove(Objects.requireNonNull(routeId, "routeId must not be null"));
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mSelectableRoutes.remove(routeId);
            return this;
        }

@@ -416,20 +448,26 @@ public class RouteSessionInfo implements Parcelable {
        }

        /**
         * Adds a route to the deselectable routes.
         * Adds a route to the deselectable routes. The {@code routeId} must not be empty.
         */
        @NonNull
        public Builder addDeselectableRoute(@NonNull String routeId) {
            mDeselectableRoutes.add(Objects.requireNonNull(routeId, "routeId must not be null"));
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mDeselectableRoutes.add(routeId);
            return this;
        }

        /**
         * Removes a route from the deselectable routes.
         * Removes a route from the deselectable routes. The {@code routeId} must not be empty.
         */
        @NonNull
        public Builder removeDeselectableRoute(@NonNull String routeId) {
            mDeselectableRoutes.remove(Objects.requireNonNull(routeId, "routeId must not be null"));
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mDeselectableRoutes.remove(routeId);
            return this;
        }

@@ -443,21 +481,26 @@ public class RouteSessionInfo implements Parcelable {
        }

        /**
         * Adds a route to the transferrable routes.
         * Adds a route to the transferrable routes. The {@code routeId} must not be empty.
         */
        @NonNull
        public Builder addTransferrableRoute(@NonNull String routeId) {
            mTransferrableRoutes.add(Objects.requireNonNull(routeId, "routeId must not be null"));
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mTransferrableRoutes.add(routeId);
            return this;
        }

        /**
         * Removes a route from the transferrable routes.
         * Removes a route from the transferrable routes. The {@code routeId} must not be empty.
         */
        @NonNull
        public Builder removeTransferrableRoute(@NonNull String routeId) {
            mTransferrableRoutes.remove(
                    Objects.requireNonNull(routeId, "routeId must not be null"));
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mTransferrableRoutes.remove(routeId);
            return this;
        }

@@ -472,9 +515,14 @@ public class RouteSessionInfo implements Parcelable {

        /**
         * Builds a route session info.
         *
         * @throws IllegalArgumentException if no selected routes are added.
         */
        @NonNull
        public RouteSessionInfo build() {
            if (mSelectedRoutes.isEmpty()) {
                throw new IllegalArgumentException("selectedRoutes must not be empty");
            }
            return new RouteSessionInfo(this);
        }
    }
+8 −1
Original line number Diff line number Diff line
@@ -237,13 +237,20 @@ public class SampleMediaRoute2ProviderService extends MediaRoute2ProviderService
        MediaRoute2Info route = mRoutes.get(routeId);

        mRouteSessionMap.remove(routeId);
        if (sessionInfo == null || route == null) {
        if (sessionInfo == null || route == null
                || !sessionInfo.getSelectedRoutes().contains(routeId)) {
            return;
        }

        mRoutes.put(routeId, new MediaRoute2Info.Builder(route)
                .setClientPackageName(null)
                .build());

        if (sessionInfo.getSelectedRoutes().size() == 1) {
            releaseSession(sessionId);
            return;
        }

        RouteSessionInfo newSessionInfo = new RouteSessionInfo.Builder(sessionInfo)
                .removeSelectedRoute(routeId)
                .addSelectableRoute(routeId)
+559 −0

File added.

Preview size limit exceeded, changes collapsed.

+0 −63
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mediaroutertest;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.media.RouteSessionInfo;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class RouteSessionTest {
    private static final String TEST_SESSION_ID = "test_session_id";
    private static final String TEST_PACKAGE_NAME = "com.android.mediaroutertest";
    private static final String TEST_CONTROL_CATEGORY = "com.android.mediaroutertest.category";

    private static final String TEST_ROUTE_ID1 = "route_id1";

    @Test
    public void testValidity() {
        RouteSessionInfo emptyPackageSession = new RouteSessionInfo.Builder(TEST_SESSION_ID,
                "",
                TEST_CONTROL_CATEGORY)
                .addSelectedRoute(TEST_ROUTE_ID1)
                .build();
        RouteSessionInfo emptyCategorySession = new RouteSessionInfo.Builder(TEST_SESSION_ID,
                TEST_PACKAGE_NAME, "")
                .addSelectedRoute(TEST_ROUTE_ID1)
                .build();

        RouteSessionInfo emptySelectedRouteSession = new RouteSessionInfo.Builder(TEST_SESSION_ID,
                TEST_PACKAGE_NAME, TEST_CONTROL_CATEGORY)
                .build();

        RouteSessionInfo validSession = new RouteSessionInfo.Builder(emptySelectedRouteSession)
                .addSelectedRoute(TEST_ROUTE_ID1)
                .build();

        assertFalse(emptyPackageSession.isValid());
        assertFalse(emptyCategorySession.isValid());
        assertFalse(emptySelectedRouteSession.isValid());
        assertTrue(validSession.isValid());
    }
}