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

Commit 95fae49f authored by Santiago Seifert's avatar Santiago Seifert
Browse files

Make PackageNameUserHandlePair Parcelable

For this, we need to move into a standalone class. We are also renaming
it to AppId.

This is a preliminary refactor for the implementation of volume sliders
for "Cast mirroring" sessions.

Bug: b/396394220
Test: atest CtsMediaRouterTestCases
Flag: EXEMPT refactor
Change-Id: I19504d00240178c0f1981eca34e6172e15abea7c
parent 3cafd329
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
/*
 * Copyright 2025 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 android.media;

parcelable AppId;
+94 −0
Original line number Diff line number Diff line
/*
 * Copyright 2025 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 android.media;

import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;

import java.util.Objects;

/**
 * A pair of {@link UserHandle} and package name to uniquely identify apps.
 *
 * @hide
 */
public final class AppId implements Parcelable {

    public static final Creator<AppId> CREATOR =
            new Creator<>() {
                @Override
                public AppId createFromParcel(Parcel in) {
                    return new AppId(
                            in.readString(),
                            in.readParcelable(UserHandle.class.getClassLoader(), UserHandle.class));
                }

                @Override
                public AppId[] newArray(int size) {
                    return new AppId[size];
                }
            };

    public final String mPackageName;
    public final UserHandle mUserHandle;

    /** Constructor. */
    public AppId(String mPackageName, UserHandle mUserHandle) {
        this.mPackageName = mPackageName;
        this.mUserHandle = mUserHandle;
    }

    // Parcelable implementation.

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mPackageName);
        dest.writeParcelable(mUserHandle, flags);
    }

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

    // Object implementation.

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null || obj.getClass() != this.getClass()) return false;
        var that = (AppId) obj;
        return Objects.equals(this.mPackageName, that.mPackageName)
                && Objects.equals(this.mUserHandle, that.mUserHandle);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mPackageName, mUserHandle);
    }

    @Override
    public String toString() {
        return "AppId["
                + "mPackageName='"
                + mPackageName
                + "', "
                + "mUserHandle="
                + mUserHandle
                + ']';
    }
}
+3 −7
Original line number Diff line number Diff line
@@ -196,13 +196,10 @@ public final class MediaRouter2 {
    // The manager request ID representing that no manager is involved.
    private static final long MANAGER_REQUEST_ID_NONE = MediaRoute2ProviderService.REQUEST_ID_NONE;

    private record PackageNameUserHandlePair(String packageName, UserHandle user) {}

    private record InstanceInvalidatedCallbackRecord(Executor executor, Runnable runnable) {}

    @GuardedBy("sSystemRouterLock")
    private static final Map<PackageNameUserHandlePair, MediaRouter2> sAppToProxyRouterMap =
            new ArrayMap<>();
    private static final Map<AppId, MediaRouter2> sAppToProxyRouterMap = new ArrayMap<>();

    @GuardedBy("sRouterLock")
    private static MediaRouter2 sInstance;
@@ -494,7 +491,7 @@ public final class MediaRouter2 {
            }
        }

        PackageNameUserHandlePair key = new PackageNameUserHandlePair(clientPackageName, user);
        AppId key = new AppId(clientPackageName, user);

        synchronized (sSystemRouterLock) {
            MediaRouter2 instance = sAppToProxyRouterMap.get(key);
@@ -3692,8 +3689,7 @@ public final class MediaRouter2 {
            // After this block, all following getInstance() calls should throw a SecurityException,
            // so no new onInstanceInvalidatedListeners can be registered to this instance.
            synchronized (sSystemRouterLock) {
                PackageNameUserHandlePair key =
                        new PackageNameUserHandlePair(mClientPackageName, mClientUser);
                AppId key = new AppId(mClientPackageName, mClientUser);
                sAppToProxyRouterMap.remove(key);
            }