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

Commit 4646d288 authored by RoboErik's avatar RoboErik
Browse files

Add UserRecords to separate user interactions

Each user record maintains the list of sessions and providers that
are running under that user. Lifecycle for providers has been modified
to stop discovery when the user is no longer current but keep the
binder connection open so long as there's a session that has selected
a route from that provider. When a user is stopped all providers on
that user will be unbound even if they were still in use.

Change-Id: Iadf1efded3415f7ecf384d3a73513883de9c86b0
parent 515396a6
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -15548,7 +15548,7 @@ package android.media.session {
    method public void addCallback(android.media.session.Session.Callback);
    method public void addCallback(android.media.session.Session.Callback, android.os.Handler);
    method public void connect(android.media.session.RouteInfo, android.media.session.RouteOptions);
    method public void disconnect(android.media.session.RouteInfo);
    method public void disconnect();
    method public android.media.session.SessionToken getSessionToken();
    method public android.media.session.TransportPerformer getTransportPerformer();
    method public boolean isActive();
@@ -15558,6 +15558,11 @@ package android.media.session {
    method public void setActive(boolean);
    method public void setFlags(int);
    method public void setRouteOptions(java.util.List<android.media.session.RouteOptions>);
    field public static final int DISCONNECT_REASON_PROVIDER_DISCONNECTED = 2; // 0x2
    field public static final int DISCONNECT_REASON_ROUTE_CHANGED = 3; // 0x3
    field public static final int DISCONNECT_REASON_SESSION_DESTROYED = 5; // 0x5
    field public static final int DISCONNECT_REASON_SESSION_DISCONNECTED = 4; // 0x4
    field public static final int DISCONNECT_REASON_USER_STOPPING = 1; // 0x1
    field public static final int FLAG_HANDLES_MEDIA_BUTTONS = 1; // 0x1
    field public static final int FLAG_HANDLES_TRANSPORT_CONTROLS = 2; // 0x2
  }
+1 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ interface ISession {
    boolean setRoute(in RouteInfo route);
    void setRouteOptions(in List<RouteOptions> options);
    void connectToRoute(in RouteInfo route, in RouteOptions options);
    void disconnectFromRoute(in RouteInfo route);
    void sendRouteCommand(in RouteCommand event, in ResultReceiver cb);

    // These commands are for the TransportPerformer
+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ oneway interface ISessionCallback {
    void onMediaButton(in Intent mediaButtonIntent);
    void onRequestRouteChange(in RouteInfo route);
    void onRouteConnected(in RouteInfo route, in RouteOptions options);
    void onRouteDisconnected(in RouteInfo route, int reason);
    void onRouteStateChange(int state);
    void onRouteEvent(in RouteEvent event);

+67 −4
Original line number Diff line number Diff line
@@ -86,10 +86,39 @@ public final class Session {
     */
    public static final int FLAG_EXCLUSIVE_GLOBAL_PRIORITY = 1 << 16;

    /**
     * Indicates the session was disconnected because the user that the session
     * belonged to is stopping.
     */
    public static final int DISCONNECT_REASON_USER_STOPPING = 1;

    /**
     * Indicates the session was disconnected because the provider disconnected
     * the route.
     */
    public static final int DISCONNECT_REASON_PROVIDER_DISCONNECTED = 2;

    /**
     * Indicates the session was disconnected because the route has changed.
     */
    public static final int DISCONNECT_REASON_ROUTE_CHANGED = 3;

    /**
     * Indicates the session was disconnected because the session owner
     * requested it disconnect.
     */
    public static final int DISCONNECT_REASON_SESSION_DISCONNECTED = 4;

    /**
     * Indicates the session was disconnected because it was destroyed.
     */
    public static final int DISCONNECT_REASON_SESSION_DESTROYED = 5;

    private static final int MSG_MEDIA_BUTTON = 1;
    private static final int MSG_COMMAND = 2;
    private static final int MSG_ROUTE_CHANGE = 3;
    private static final int MSG_ROUTE_CONNECTED = 4;
    private static final int MSG_ROUTE_DISCONNECTED = 5;

    private static final String KEY_COMMAND = "command";
    private static final String KEY_EXTRAS = "extras";
@@ -302,11 +331,15 @@ public final class Session {
    /**
     * Disconnect from the current route. After calling you will be switched
     * back to the default route.
     *
     * @param route The route to disconnect from.
     */
    public void disconnect(RouteInfo route) {
        // TODO
    public void disconnect() {
        if (mRoute != null) {
            try {
                mBinder.disconnectFromRoute(mRoute.getRouteInfo());
            } catch (RemoteException e) {
                Log.wtf(TAG, "Error disconnecting from route");
            }
        }
    }

    /**
@@ -406,6 +439,16 @@ public final class Session {
        }
    }

    private void postRouteDisconnected(RouteInfo route, int reason) {
        synchronized (mLock) {
            if (mRoute != null && TextUtils.equals(mRoute.getRouteInfo().getId(), route.getId())) {
                for (int i = mCallbacks.size() - 1; i >= 0; i--) {
                    mCallbacks.get(i).post(MSG_ROUTE_DISCONNECTED, mRoute, reason);
                }
            }
        }
    }

    /**
     * Receives commands or updates from controllers and routes. An app can
     * specify what commands and buttons it supports by setting them on the
@@ -467,6 +510,11 @@ public final class Session {
         * <p>
         * Valid reasons are:
         * <ul>
         * <li>{@link #DISCONNECT_REASON_USER_STOPPING}</li>
         * <li>{@link #DISCONNECT_REASON_PROVIDER_DISCONNECTED}</li>
         * <li>{@link #DISCONNECT_REASON_ROUTE_CHANGED}</li>
         * <li>{@link #DISCONNECT_REASON_SESSION_DISCONNECTED}</li>
         * <li>{@link #DISCONNECT_REASON_SESSION_DESTROYED}</li>
         * </ul>
         *
         * @param route The route that disconnected
@@ -519,6 +567,14 @@ public final class Session {
            }
        }

        @Override
        public void onRouteDisconnected(RouteInfo route, int reason) {
            Session session = mMediaSession.get();
            if (session != null) {
                session.postRouteDisconnected(route, reason);
            }
        }

        @Override
        public void onPlay() throws RemoteException {
            Session session = mMediaSession.get();
@@ -668,6 +724,9 @@ public final class Session {
                    case MSG_ROUTE_CONNECTED:
                        mCallback.onRouteConnected((Route) msg.obj);
                        break;
                    case MSG_ROUTE_DISCONNECTED:
                        mCallback.onRouteDisconnected((Route) msg.obj, msg.arg1);
                        break;
                }
            }
        }
@@ -675,6 +734,10 @@ public final class Session {
        public void post(int what, Object obj) {
            obtainMessage(what, obj).sendToTarget();
        }

        public void post(int what, Object obj, int arg1) {
            obtainMessage(what, arg1, 0, obj).sendToTarget();
        }
    }

    private static final class Command {
+27 −2
Original line number Diff line number Diff line
@@ -58,12 +58,16 @@ public class MediaRouteProviderProxy {
    private final int mUserId;
    // Interfaces declared in the manifest
    private final ArrayList<String> mInterfaces = new ArrayList<String>();
    private final ArrayList<RouteConnectionRecord> mConnections = new ArrayList<RouteConnectionRecord>();
    private final ArrayList<RouteConnectionRecord> mConnections
            = new ArrayList<RouteConnectionRecord>();
    // The sessions that have a route from this provider selected
    private final ArrayList<MediaSessionRecord> mSessions = new ArrayList<MediaSessionRecord>();
    private final Handler mHandler = new Handler();

    private Intent mBindIntent;
    private IRouteProvider mBinder;
    private boolean mRunning;
    private boolean mPaused;
    private boolean mInterested;
    private boolean mBound;
    private int mRetryCount;
@@ -83,6 +87,12 @@ public class MediaRouteProviderProxy {
        mBindIntent.setComponent(mComponentName);
    }

    public void destroy() {
        stop();
        mSessions.clear();
        updateBinding();
    }

    /**
     * Send any cleanup messages and unbind from the media route provider
     */
@@ -236,6 +246,19 @@ public class MediaRouteProviderProxy {
        return mId;
    }

    public void addSession(MediaSessionRecord session) {
        mSessions.add(session);
    }

    public void removeSession(MediaSessionRecord session) {
        mSessions.remove(session);
        updateBinding();
    }

    public int getSessionCount() {
        return mSessions.size();
    }

    public void dump(PrintWriter pw, String prefix) {
        pw.println(prefix + mId + " " + this);
        String indent = prefix + "  ";
@@ -257,8 +280,10 @@ public class MediaRouteProviderProxy {
        }
    }

    // We want to bind as long as we're interested in this provider or there are
    // sessions connected to it.
    private boolean shouldBind() {
        return mRunning && mInterested;
        return (mRunning && mInterested) || (!mSessions.isEmpty());
    }

    private void bind() {
Loading