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

Commit 3b8b2bdb authored by Santiago Seifert's avatar Santiago Seifert
Browse files

Track providers' support for system media routing

Bug: b/362507305
Test: atest CtsMediaBetterTogetherTestCases CtsMediaHostTestCasts
Flag: com.android.media.flags.enable_mirroring_in_media_router_2
Change-Id: Ia2c551cbda3fa63892e9302e09bd283e43559493
parent 18da6182
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -887,6 +887,17 @@ public final class MediaRoute2Info implements Parcelable {
        return true;
    }

    /**
     * Returns whether this route supports routing of the system media.
     *
     * @hide
     */
    public boolean supportsSystemMediaRouting() {
        return (mRoutingTypeFlags
                        & (FLAG_ROUTING_TYPE_SYSTEM_VIDEO | FLAG_ROUTING_TYPE_SYSTEM_AUDIO))
                != 0;
    }

    /**
     * Returns true if the route info has all of the required field.
     * A route is valid if and only if it is obtained from
+16 −3
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider {
    private final int mUserId;
    private final Handler mHandler;
    private final boolean mIsSelfScanOnlyProvider;
    private final boolean mSupportsSystemMediaRouting;
    private final ServiceConnection mServiceConnection = new ServiceConnectionImpl();

    // Connection state
@@ -95,12 +96,14 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider {
            @NonNull Looper looper,
            @NonNull ComponentName componentName,
            boolean isSelfScanOnlyProvider,
            boolean supportsSystemMediaRouting,
            int userId) {
        super(componentName, /* isSystemRouteProvider= */ false);
        mContext = Objects.requireNonNull(context, "Context must not be null.");
        mRequestIdToSessionCreationRequest = new LongSparseArray<>();
        mSessionOriginalIdToTransferRequest = new HashMap<>();
        mIsSelfScanOnlyProvider = isSelfScanOnlyProvider;
        mSupportsSystemMediaRouting = supportsSystemMediaRouting;
        mUserId = userId;
        mHandler = new Handler(looper);
    }
@@ -651,11 +654,12 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider {
        }
        return TextUtils.formatSimple(
                "ProviderServiceProxy - package: %s, bound: %b, connection (active:%b, ready:%b), "
                        + "pending (session creations: %d, transfers: %d)",
                        + "system media=%b, pending (session creations: %d, transfers: %d)",
                mComponentName.getPackageName(),
                mBound,
                mActiveConnection != null,
                mConnectionReady,
                mSupportsSystemMediaRouting,
                pendingSessionCreationCount,
                pendingTransferCount);
    }
@@ -697,7 +701,7 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider {

        Connection(IMediaRoute2ProviderService serviceBinder) {
            mService = serviceBinder;
            mCallbackStub = new ServiceCallbackStub(this);
            mCallbackStub = new ServiceCallbackStub(this, mSupportsSystemMediaRouting);
        }

        public boolean register() {
@@ -811,9 +815,11 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider {
    private static final class ServiceCallbackStub extends
            IMediaRoute2ProviderServiceCallback.Stub {
        private final WeakReference<Connection> mConnectionRef;
        private final boolean mAllowSystemMediaRoutes;

        ServiceCallbackStub(Connection connection) {
        ServiceCallbackStub(Connection connection, boolean allowSystemMediaRoutes) {
            mConnectionRef = new WeakReference<>(connection);
            mAllowSystemMediaRoutes = allowSystemMediaRoutes;
        }

        public void dispose() {
@@ -846,6 +852,13 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider {
                                    + "Disallowed route: "
                                    + route);
                }

                if (route.supportsSystemMediaRouting() && !mAllowSystemMediaRoutes) {
                    throw new SecurityException(
                            "This provider is not allowed to publish routes that support system"
                                    + " media routing. Disallowed route: "
                                    + route);
                }
            }

            Connection connection = mConnectionRef.get();
+15 −2
Original line number Diff line number Diff line
@@ -17,7 +17,9 @@
package com.android.server.media;

import static android.content.pm.PackageManager.GET_RESOLVED_FILTER;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.Manifest;
import android.annotation.NonNull;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
@@ -130,22 +132,33 @@ final class MediaRoute2ProviderWatcher {
            ServiceInfo serviceInfo = resolveInfo.serviceInfo;
            if (serviceInfo != null) {
                boolean isSelfScanOnlyProvider = false;
                boolean supportsSystemMediaRouting = false;
                Iterator<String> categoriesIterator = resolveInfo.filter.categoriesIterator();
                if (categoriesIterator != null) {
                    while (categoriesIterator.hasNext()) {
                        String category = categoriesIterator.next();
                        isSelfScanOnlyProvider |=
                                MediaRoute2ProviderService.CATEGORY_SELF_SCAN_ONLY.equals(
                                        categoriesIterator.next());
                                MediaRoute2ProviderService.CATEGORY_SELF_SCAN_ONLY.equals(category);
                        supportsSystemMediaRouting |=
                                MediaRoute2ProviderService.SERVICE_INTERFACE_SYSTEM_MEDIA.equals(
                                        category);
                    }
                }
                int sourceIndex = findProvider(serviceInfo.packageName, serviceInfo.name);
                if (sourceIndex < 0) {
                    supportsSystemMediaRouting &= Flags.enableMirroringInMediaRouter2();
                    supportsSystemMediaRouting &=
                            mPackageManager.checkPermission(
                                            Manifest.permission.MODIFY_AUDIO_ROUTING,
                                            serviceInfo.packageName)
                                    == PERMISSION_GRANTED;
                    MediaRoute2ProviderServiceProxy proxy =
                            new MediaRoute2ProviderServiceProxy(
                                    mContext,
                                    mHandler.getLooper(),
                                    new ComponentName(serviceInfo.packageName, serviceInfo.name),
                                    isSelfScanOnlyProvider,
                                    supportsSystemMediaRouting,
                                    mUserId);
                    Slog.i(
                            TAG,