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

Commit ea4e7228 authored by Santiago Seifert's avatar Santiago Seifert
Browse files

Simplify MediaRouter2Manager.Client's lifecycle

This is a cherrypick of commit 3aa15125.

Test: atest MediaRouter2ManagerTest
Bug: 239926177
Change-Id: I363dc3918465de7fdb74f833fae78f46bde72a37
Merged-In: I363dc3918465de7fdb74f833fae78f46bde72a37
parent 0952e037
Loading
Loading
Loading
Loading
+20 −33
Original line number Original line Diff line number Diff line
@@ -79,8 +79,9 @@ public final class MediaRouter2Manager {
    final String mPackageName;
    final String mPackageName;


    private final Context mContext;
    private final Context mContext;
    @GuardedBy("sLock")

    private Client mClient;
    private final Client mClient;

    private final IMediaRouterService mMediaRouterService;
    private final IMediaRouterService mMediaRouterService;
    private final AtomicInteger mScanRequestCount = new AtomicInteger(/* initialValue= */ 0);
    private final AtomicInteger mScanRequestCount = new AtomicInteger(/* initialValue= */ 0);
    final Handler mHandler;
    final Handler mHandler;
@@ -120,7 +121,12 @@ public final class MediaRouter2Manager {
                .getSystemService(Context.MEDIA_SESSION_SERVICE);
                .getSystemService(Context.MEDIA_SESSION_SERVICE);
        mPackageName = mContext.getPackageName();
        mPackageName = mContext.getPackageName();
        mHandler = new Handler(context.getMainLooper());
        mHandler = new Handler(context.getMainLooper());
        mHandler.post(this::getOrCreateClient);
        mClient = new Client();
        try {
            mMediaRouterService.registerManager(mClient, mPackageName);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }
    }


    /**
    /**
@@ -167,7 +173,7 @@ public final class MediaRouter2Manager {
    public void registerScanRequest() {
    public void registerScanRequest() {
        if (mScanRequestCount.getAndIncrement() == 0) {
        if (mScanRequestCount.getAndIncrement() == 0) {
            try {
            try {
                mMediaRouterService.startScan(getOrCreateClient());
                mMediaRouterService.startScan(mClient);
            } catch (RemoteException ex) {
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
                throw ex.rethrowFromSystemServer();
            }
            }
@@ -194,7 +200,7 @@ public final class MediaRouter2Manager {
                })
                })
                == 0) {
                == 0) {
            try {
            try {
                mMediaRouterService.stopScan(getOrCreateClient());
                mMediaRouterService.stopScan(mClient);
            } catch (RemoteException ex) {
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
                throw ex.rethrowFromSystemServer();
            }
            }
@@ -358,8 +364,7 @@ public final class MediaRouter2Manager {
    @Nullable
    @Nullable
    public RoutingSessionInfo getSystemRoutingSession(@Nullable String packageName) {
    public RoutingSessionInfo getSystemRoutingSession(@Nullable String packageName) {
        try {
        try {
            return mMediaRouterService.getSystemSessionInfoForPackage(
            return mMediaRouterService.getSystemSessionInfoForPackage(mClient, packageName);
                    getOrCreateClient(), packageName);
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -424,7 +429,7 @@ public final class MediaRouter2Manager {
    @NonNull
    @NonNull
    public List<RoutingSessionInfo> getRemoteSessions() {
    public List<RoutingSessionInfo> getRemoteSessions() {
        try {
        try {
            return mMediaRouterService.getRemoteSessions(getOrCreateClient());
            return mMediaRouterService.getRemoteSessions(mClient);
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -512,8 +517,7 @@ public final class MediaRouter2Manager {


        try {
        try {
            int requestId = mNextRequestId.getAndIncrement();
            int requestId = mNextRequestId.getAndIncrement();
            mMediaRouterService.setRouteVolumeWithManager(
            mMediaRouterService.setRouteVolumeWithManager(mClient, requestId, route, volume);
                    getOrCreateClient(), requestId, route, volume);
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -540,7 +544,7 @@ public final class MediaRouter2Manager {
        try {
        try {
            int requestId = mNextRequestId.getAndIncrement();
            int requestId = mNextRequestId.getAndIncrement();
            mMediaRouterService.setSessionVolumeWithManager(
            mMediaRouterService.setSessionVolumeWithManager(
                    getOrCreateClient(), requestId, sessionInfo.getId(), volume);
                    mClient, requestId, sessionInfo.getId(), volume);
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -802,7 +806,7 @@ public final class MediaRouter2Manager {
        try {
        try {
            int requestId = mNextRequestId.getAndIncrement();
            int requestId = mNextRequestId.getAndIncrement();
            mMediaRouterService.selectRouteWithManager(
            mMediaRouterService.selectRouteWithManager(
                    getOrCreateClient(), requestId, sessionInfo.getId(), route);
                    mClient, requestId, sessionInfo.getId(), route);
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -841,7 +845,7 @@ public final class MediaRouter2Manager {
        try {
        try {
            int requestId = mNextRequestId.getAndIncrement();
            int requestId = mNextRequestId.getAndIncrement();
            mMediaRouterService.deselectRouteWithManager(
            mMediaRouterService.deselectRouteWithManager(
                    getOrCreateClient(), requestId, sessionInfo.getId(), route);
                    mClient, requestId, sessionInfo.getId(), route);
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -862,8 +866,7 @@ public final class MediaRouter2Manager {


        try {
        try {
            int requestId = mNextRequestId.getAndIncrement();
            int requestId = mNextRequestId.getAndIncrement();
            mMediaRouterService.releaseSessionWithManager(
            mMediaRouterService.releaseSessionWithManager(mClient, requestId, sessionInfo.getId());
                    getOrCreateClient(), requestId, sessionInfo.getId());
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -880,7 +883,7 @@ public final class MediaRouter2Manager {


        try {
        try {
            mMediaRouterService.transferToRouteWithManager(
            mMediaRouterService.transferToRouteWithManager(
                    getOrCreateClient(), requestId, session.getId(), route);
                    mClient, requestId, session.getId(), route);
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -897,7 +900,7 @@ public final class MediaRouter2Manager {


        try {
        try {
            mMediaRouterService.requestCreateSessionWithManager(
            mMediaRouterService.requestCreateSessionWithManager(
                    getOrCreateClient(), requestId, oldSession, route);
                    mClient, requestId, oldSession, route);
        } catch (RemoteException ex) {
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
            throw ex.rethrowFromSystemServer();
        }
        }
@@ -943,22 +946,6 @@ public final class MediaRouter2Manager {
                sessionInfo.getOwnerPackageName());
                sessionInfo.getOwnerPackageName());
    }
    }


    private Client getOrCreateClient() {
        synchronized (sLock) {
            if (mClient != null) {
                return mClient;
            }
            Client client = new Client();
            try {
                mMediaRouterService.registerManager(client, mPackageName);
                mClient = client;
                return client;
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
        }
    }

    /**
    /**
     * Interface for receiving events about media routing changes.
     * Interface for receiving events about media routing changes.
     */
     */