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

Commit bbe9ce55 authored by Iván Budnik's avatar Iván Budnik
Browse files

Improve readability and add javadocs and comments

This non-functional change adds nullability annotations and simplifies
some methods related to `getSystemSessionInfoForPackage()`. Also, the
change documents some of those methods.

Test: Presubmit.
Bug: 192657812
Change-Id: Ida1d4ff62d2a47121b5c3f7766362cd17fa325d4
parent 4feda186
Loading
Loading
Loading
Loading
+13 −8
Original line number Diff line number Diff line
@@ -673,30 +673,35 @@ class MediaRouter2ServiceImpl {

        final long token = Binder.clearCallingIdentity();
        try {
            RoutingSessionInfo systemSessionInfo = null;
            synchronized (mLock) {
                UserRecord userRecord = getOrCreateUserRecordLocked(userId);
                List<RoutingSessionInfo> sessionInfos;
                if (hasModifyAudioRoutingPermission) {
                    if (setDeviceRouteSelected) {
                        systemSessionInfo = userRecord.mHandler.mSystemProvider
                        // Return a fake system session that shows the device route as selected and
                        // available bluetooth routes as transferable.
                        return userRecord.mHandler.mSystemProvider
                                .generateDeviceRouteSelectedSessionInfo(packageName);
                    } else {
                        sessionInfos = userRecord.mHandler.mSystemProvider.getSessionInfos();
                        if (sessionInfos != null && !sessionInfos.isEmpty()) {
                            systemSessionInfo = new RoutingSessionInfo.Builder(sessionInfos.get(0))
                                    .setClientPackageName(packageName).build();
                            // Return a copy of the current system session with no modification,
                            // except setting the client package name.
                            return new RoutingSessionInfo.Builder(sessionInfos.get(0))
                                    .setClientPackageName(packageName)
                                    .build();
                        } else {
                            Slog.w(TAG, "System provider does not have any session info.");
                        }
                    }
                } else {
                    systemSessionInfo = new RoutingSessionInfo.Builder(
                    return new RoutingSessionInfo.Builder(
                                    userRecord.mHandler.mSystemProvider.getDefaultSessionInfo())
                            .setClientPackageName(packageName).build();
                            .setClientPackageName(packageName)
                            .build();
                }
            }
            return systemSessionInfo;
            return null;
        } finally {
            Binder.restoreCallingIdentity(token);
        }
+20 −5
Original line number Diff line number Diff line
@@ -195,7 +195,8 @@ public final class MediaRouterService extends IMediaRouterService.Stub

    // Binder call
    @Override
    public void registerClientAsUser(IMediaRouterClient client, String packageName, int userId) {
    public void registerClientAsUser(
            IMediaRouterClient client, @NonNull String packageName, int userId) {
        final int uid = Binder.getCallingUid();
        if (!validatePackageName(uid, packageName)) {
            throw new SecurityException("packageName must match the calling uid");
@@ -693,8 +694,13 @@ public final class MediaRouterService extends IMediaRouterService.Stub
    }

    @GuardedBy("mLock")
    private void registerClientLocked(IMediaRouterClient client,
            int uid, int pid, String packageName, int userId, boolean trusted) {
    private void registerClientLocked(
            IMediaRouterClient client,
            int uid,
            int pid,
            @NonNull String packageName,
            int userId,
            boolean trusted) {
        final IBinder binder = client.asBinder();
        ClientRecord clientRecord = mAllClientRecords.get(binder);
        if (clientRecord == null) {
@@ -926,6 +932,10 @@ public final class MediaRouterService extends IMediaRouterService.Stub
        clientRecord.dispose();
    }

    /**
     * Validates whether the provided package name matches a given uid. Returns false if the package
     * name is null.
     */
    private boolean validatePackageName(int uid, String packageName) {
        if (packageName != null) {
            String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
@@ -974,8 +984,13 @@ public final class MediaRouterService extends IMediaRouterService.Stub
        public String mSelectedRouteId;
        public String mGroupId;

        public ClientRecord(UserRecord userRecord, IMediaRouterClient client,
                int uid, int pid, String packageName, boolean trusted) {
        ClientRecord(
                UserRecord userRecord,
                IMediaRouterClient client,
                int uid,
                int pid,
                @NonNull String packageName,
                boolean trusted) {
            mUserRecord = userRecord;
            mClient = client;
            mUid = uid;
+11 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.server.media;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@@ -253,6 +254,16 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
        return mDefaultSessionInfo;
    }

    /**
     * Builds a system {@link RoutingSessionInfo} with the selected route set to the currently
     * selected <b>device</b> route (wired or built-in, but not bluetooth) and transferable routes
     * set to the currently available (connected) bluetooth routes.
     *
     * <p>The session's client package name is set to the provided package name.
     *
     * <p>Returns {@code null} if there are no registered system sessions.
     */
    @Nullable
    public RoutingSessionInfo generateDeviceRouteSelectedSessionInfo(String packageName) {
        synchronized (mLock) {
            if (mSessionInfos.isEmpty()) {