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

Commit 5736a614 authored by Hyundo Moon's avatar Hyundo Moon
Browse files

Make MediaRouter2#getSelectedRoute() non null

In the constructor of MediaRouter2, it gets system routes from
MediaRouterService.

Test: atest mediaroutertest:MediaRouter2Test
Change-Id: I26512343faefe11b353734184f0bbae566025b4b
parent d187933f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ interface IMediaRouterService {
    void setControlCategories(IMediaRouterClient client, in List<String> categories);

    // Methods for media router 2
    List<MediaRoute2Info> getSystemRoutes();
    void registerClient2(IMediaRouter2Client client, String packageName);
    void unregisterClient2(IMediaRouter2Client client);
    void sendControlRequest(IMediaRouter2Client client, in MediaRoute2Info route, in Intent request);
+23 −0
Original line number Diff line number Diff line
@@ -131,6 +131,25 @@ public class MediaRouter2 {
        mPackageName = mContext.getPackageName();
        //TODO: read control categories from the manifest
        mHandler = new Handler(Looper.getMainLooper());

        List<MediaRoute2Info> currentSystemRoutes = null;
        try {
            currentSystemRoutes = mMediaRouterService.getSystemRoutes();
        } catch (RemoteException ex) {
            Log.e(TAG, "Unable to get current currentSystemRoutes", ex);
        }

        if (currentSystemRoutes == null || currentSystemRoutes.isEmpty()) {
            throw new RuntimeException("Null or empty currentSystemRoutes. Something is wrong.");
        }

        for (MediaRoute2Info route : currentSystemRoutes) {
            mRoutes.put(route.getId(), route);
        }
        // The first route is the currently selected system route.
        // For example, if there are two system routes (BT and device speaker),
        // BT will be the first route in the list.
        mSelectedRoute = currentSystemRoutes.get(0);
    }

    /**
@@ -409,6 +428,10 @@ public class MediaRouter2 {
    }

    void addRoutesOnHandler(List<MediaRoute2Info> routes) {
        // TODO: When onRoutesAdded is first called,
        //  1) clear mRoutes before adding the routes
        //  2) Call onRouteSelected(system_route, reason_fallback) if previously selected route
        //     does not exist anymore. => We may need 'boolean MediaRoute2Info#isSystemRoute()'.
        List<MediaRoute2Info> addedRoutes = new ArrayList<>();
        for (MediaRoute2Info route : routes) {
            mRoutes.put(route.getUniqueId(), route);
+53 −0
Original line number Diff line number Diff line
/*
 * Copyright 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.assertNotNull;

import android.content.Context;
import android.media.MediaRoute2Info;
import android.media.MediaRouter2;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

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

@RunWith(AndroidJUnit4.class)
@SmallTest
public class MediaRouter2Test {
    Context mContext;

    @Before
    public void setUp() throws Exception {
        mContext = InstrumentationRegistry.getTargetContext();
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testGetSelectedRoute_afterCreation() throws Exception {
        MediaRouter2 router = MediaRouter2.getInstance(mContext);
        MediaRoute2Info initiallySelectedRoute = router.getSelectedRoute();
        assertNotNull(initiallySelectedRoute);
    }
}
+18 −0
Original line number Diff line number Diff line
@@ -83,6 +83,24 @@ class MediaRouter2ServiceImpl {
        mContext = context;
    }

    @NonNull
    public List<MediaRoute2Info> getSystemRoutes() {
        final int uid = Binder.getCallingUid();
        final int userId = UserHandle.getUserId(uid);

        Collection<MediaRoute2Info> systemRoutes;
        synchronized (mLock) {
            UserRecord userRecord = mUserRecords.get(userId);
            if (userRecord == null) {
                userRecord = new UserRecord(userId);
                mUserRecords.put(userId, userRecord);
                initializeUserLocked(userRecord);
            }
            systemRoutes = userRecord.mHandler.mSystemProvider.getProviderInfo().getRoutes();
        }
        return new ArrayList<>(systemRoutes);
    }

    public void registerClient(@NonNull IMediaRouter2Client client,
            @NonNull String packageName) {
        Objects.requireNonNull(client, "client must not be null");
+6 −0
Original line number Diff line number Diff line
@@ -441,6 +441,12 @@ public final class MediaRouterService extends IMediaRouterService.Stub
        }
    }

    // Binder call
    @Override
    public List<MediaRoute2Info> getSystemRoutes() {
        return mService2.getSystemRoutes();
    }

    // Binder call
    @Override
    public void registerClient2(IMediaRouter2Client client, String packageName) {
Loading