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

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

Move mCurConnection to the service and rename

In later changes, when we allow swapping the ServiceState, we
should not swap the current connection, since a callback
execution might be ongoing.

Bug: b/185136506
Test: m (non-functional refactor)
Change-Id: Ie097398656936445684fd7905b7d953e24c0a6db
parent 685cc6f9
Loading
Loading
Loading
Loading
+17 −13
Original line number Diff line number Diff line
@@ -107,6 +107,10 @@ public abstract class MediaBrowserService extends Service {

    private final ServiceState mServiceState = new ServiceState();

    // Holds the connection record associated with the currently executing callback operation, if
    // any. See getCurrentBrowserInfo for an example. Must only be accessed on mHandler.
    @Nullable private ConnectionRecord mCurrentConnectionOnHandler;

    /**
     * All the info about a connection.
     */
@@ -462,12 +466,12 @@ public abstract class MediaBrowserService extends Service {
     * @see MediaBrowserService.BrowserRoot#EXTRA_SUGGESTED
     */
    public final Bundle getBrowserRootHints() {
        ConnectionRecord curConnection = mServiceState.mCurConnection;
        if (curConnection == null) {
        ConnectionRecord currentConnection = mCurrentConnectionOnHandler;
        if (currentConnection == null) {
            throw new IllegalStateException("This should be called inside of onGetRoot or"
                    + " onLoadChildren or onLoadItem methods");
        }
        return curConnection.rootHints == null ? null : new Bundle(curConnection.rootHints);
        return currentConnection.rootHints == null ? null : new Bundle(currentConnection.rootHints);
    }

    /**
@@ -478,12 +482,13 @@ public abstract class MediaBrowserService extends Service {
     * @see MediaSessionManager#isTrustedForMediaControl(RemoteUserInfo)
     */
    public final RemoteUserInfo getCurrentBrowserInfo() {
        ConnectionRecord curConnection = mServiceState.mCurConnection;
        if (curConnection == null) {
        ConnectionRecord currentConnection = mCurrentConnectionOnHandler;
        if (currentConnection == null) {
            throw new IllegalStateException("This should be called inside of onGetRoot or"
                    + " onLoadChildren or onLoadItem methods");
        }
        return new RemoteUserInfo(curConnection.pkg, curConnection.pid, curConnection.uid);
        return new RemoteUserInfo(
                currentConnection.pkg, currentConnection.pid, currentConnection.uid);
    }

    /**
@@ -622,7 +627,6 @@ public abstract class MediaBrowserService extends Service {

        // Fields accessed from mHandler only.
        @NonNull private final ArrayMap<IBinder, ConnectionRecord> mConnections = new ArrayMap<>();
        @Nullable private ConnectionRecord mCurConnection;

        public void postOnHandler(Runnable runnable) {
            mHandler.post(runnable);
@@ -705,7 +709,7 @@ public abstract class MediaBrowserService extends Service {

            // Temporarily sets a placeholder ConnectionRecord to make getCurrentBrowserInfo() work
            // in onGetRoot().
            mCurConnection =
            mCurrentConnectionOnHandler =
                    new ConnectionRecord(
                            /* serviceState= */ this,
                            pkg,
@@ -715,7 +719,7 @@ public abstract class MediaBrowserService extends Service {
                            callbacks,
                            /* root= */ null);
            BrowserRoot root = onGetRoot(pkg, uid, rootHints);
            mCurConnection = null;
            mCurrentConnectionOnHandler = null;

            // If they didn't return something, don't allow this client.
            if (root == null) {
@@ -831,13 +835,13 @@ public abstract class MediaBrowserService extends Service {
                        }
                    };

            mCurConnection = connection;
            mCurrentConnectionOnHandler = connection;
            if (options == null) {
                onLoadChildren(parentId, result);
            } else {
                onLoadChildren(parentId, result, options);
            }
            mCurConnection = null;
            mCurrentConnectionOnHandler = null;

            if (!result.isDone()) {
                throw new IllegalStateException(
@@ -886,9 +890,9 @@ public abstract class MediaBrowserService extends Service {
                        }
                    };

            mCurConnection = connection;
            mCurrentConnectionOnHandler = connection;
            onLoadItem(itemId, result);
            mCurConnection = null;
            mCurrentConnectionOnHandler = null;

            if (!result.isDone()) {
                throw new IllegalStateException(