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

Commit 231877fb authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Move MediaSession2 builder to impl"

parents 34c238ea d0cff6b0
Loading
Loading
Loading
Loading
+31 −4
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.app.PendingIntent;
import android.content.Context;
import android.media.MediaLibraryService2;
import android.media.MediaLibraryService2.MediaLibrarySession;
import android.media.MediaLibraryService2.MediaLibrarySessionBuilder;
import android.media.MediaLibraryService2.MediaLibrarySessionCallback;
import android.media.MediaPlayerInterface;
import android.media.MediaSession2;
@@ -30,6 +31,8 @@ import android.media.VolumeProvider;
import android.media.update.MediaLibraryService2Provider;
import android.os.Bundle;

import com.android.media.MediaSession2Impl.BuilderBaseImpl;

import java.util.concurrent.Executor;

public class MediaLibraryService2Impl extends MediaSessionService2Impl implements
@@ -61,19 +64,27 @@ public class MediaLibraryService2Impl extends MediaSessionService2Impl implement

    public static class MediaLibrarySessionImpl extends MediaSession2Impl
            implements MediaLibrarySessionProvider {
        private final MediaLibrarySession mInstance;
        private final MediaLibrarySessionCallback mCallback;

        public MediaLibrarySessionImpl(Context context, MediaLibrarySession instance,
        public MediaLibrarySessionImpl(Context context,
                MediaPlayerInterface player, String id, VolumeProvider volumeProvider,
                int ratingType, PendingIntent sessionActivity, Executor callbackExecutor,
                MediaLibrarySessionCallback callback)  {
            super(context, instance, player, id, volumeProvider, ratingType, sessionActivity,
            super(context, player, id, volumeProvider, ratingType, sessionActivity,
                    callbackExecutor, callback);
            mInstance = instance;
            mCallback = callback;
        }

        @Override
        MediaLibrarySession createInstance() {
            return new MediaLibrarySession(this);
        }

        @Override
        MediaLibrarySession getInstance() {
            return (MediaLibrarySession) super.getInstance();
        }

        @Override
        public void notifyChildrenChanged_impl(ControllerInfo controller, String parentId,
                Bundle options) {
@@ -85,4 +96,20 @@ public class MediaLibraryService2Impl extends MediaSessionService2Impl implement
            // TODO(jaewan): Implements
        }
    }

    public static class BuilderImpl
            extends BuilderBaseImpl<MediaLibrarySession, MediaLibrarySessionCallback> {
        public BuilderImpl(Context context, MediaLibrarySessionBuilder instance,
            MediaPlayerInterface player, Executor callbackExecutor,
            MediaLibrarySessionCallback callback) {
            super(context, player);
            setSessionCallback_impl(callbackExecutor, callback);
        }

        @Override
        public MediaLibrarySession build_impl() {
            return new MediaLibrarySessionImpl(mContext, mPlayer, mId, mVolumeProvider, mRatingType,
                    mSessionActivity, mCallbackExecutor, mCallback).getInstance();
        }
    }
}
+105 −22
Original line number Diff line number Diff line
@@ -101,11 +101,11 @@ public class MediaSession2Impl implements MediaSession2Provider {
     * @param ratingType
     * @param sessionActivity
     */
    public MediaSession2Impl(Context context, MediaSession2 instance, MediaPlayerInterface player,
    public MediaSession2Impl(Context context, MediaPlayerInterface player,
            String id, VolumeProvider volumeProvider, int ratingType, PendingIntent sessionActivity,
            Executor callbackExecutor, SessionCallback callback) {
        mInstance = instance;
        // TODO(jaewan): Keep other params.
        mInstance = createInstance();

        // Argument checks are done by builder already.
        // Initialize finals first.
@@ -113,8 +113,6 @@ public class MediaSession2Impl implements MediaSession2Provider {
        mId = id;
        mCallback = callback;
        mCallbackExecutor = callbackExecutor;
        // Only remember player. Actual settings will be done in the initialize().
        mPlayer = player;
        mSessionStub = new MediaSession2Stub(this);

        // Infer type from the id and package name.
@@ -133,6 +131,24 @@ public class MediaSession2Impl implements MediaSession2Provider {
            mSessionToken = new SessionToken2Impl(context, Process.myUid(), TYPE_SESSION,
                    mContext.getPackageName(), null, id, mSessionStub).getInstance();
        }

        setPlayerLocked(player);

        // Ask server for the sanity check, and starts
        // Sanity check for making session ID unique 'per package' cannot be done in here.
        // Server can only know if the package has another process and has another session with the
        // same id. Note that 'ID is unique per package' is important for controller to distinguish
        // a session in another package.
        MediaSessionManager manager =
                (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
        if (!manager.onSessionCreated(mSessionToken)) {
            throw new IllegalStateException("Session with the same id is already used by"
                    + " another process. Use MediaController2 instead.");
        }
    }

    MediaSession2 createInstance() {
        return new MediaSession2(this);
    }

    private static String getServiceName(Context context, String serviceAction, String id) {
@@ -160,24 +176,6 @@ public class MediaSession2Impl implements MediaSession2Provider {
        return serviceName;
    }

    @Override
    public void initialize() {
        synchronized (mLock) {
            setPlayerLocked(mPlayer);
        }
        // Ask server for the sanity check, and starts
        // Sanity check for making session ID unique 'per package' cannot be done in here.
        // Server can only know if the package has another process and has another session with the
        // same id. Note that 'ID is unique per package' is important for controller to distinguish
        // a session in another package.
        MediaSessionManager manager =
                (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
        if (!manager.onSessionCreated(mSessionToken)) {
            throw new IllegalStateException("Session with the same id is already used by"
                    + " another process. Use MediaController2 instead.");
        }
    }

    // TODO(jaewan): Add explicit release() and do not remove session object with the
    //               setPlayer(null). Token can be available when player is null, and
    //               controller can also attach to session.
@@ -887,4 +885,89 @@ public class MediaSession2Impl implements MediaSession2Provider {
                    metadata);
        }
    }

    public static abstract class BuilderBaseImpl<T extends MediaSession2, C extends SessionCallback>
            implements BuilderBaseProvider<T, C> {
        final Context mContext;
        final MediaPlayerInterface mPlayer;
        String mId;
        Executor mCallbackExecutor;
        C mCallback;
        VolumeProvider mVolumeProvider;
        int mRatingType;
        PendingIntent mSessionActivity;

        /**
         * Constructor.
         *
         * @param context a context
         * @param player a player to handle incoming command from any controller.
         * @throws IllegalArgumentException if any parameter is null, or the player is a
         *      {@link MediaSession2} or {@link MediaController2}.
         */
        // TODO(jaewan): Also need executor
        public BuilderBaseImpl(Context context, MediaPlayerInterface player) {
            if (context == null) {
                throw new IllegalArgumentException("context shouldn't be null");
            }
            if (player == null) {
                throw new IllegalArgumentException("player shouldn't be null");
            }
            mContext = context;
            mPlayer = player;
            // Ensure non-null
            mId = "";
        }

        public void setVolumeProvider_impl(VolumeProvider volumeProvider) {
            mVolumeProvider = volumeProvider;
        }

        public void setRatingType_impl(int type) {
            mRatingType = type;
        }

        public void setSessionActivity_impl(PendingIntent pi) {
            mSessionActivity = pi;
        }

        public void setId_impl(String id) {
            if (id == null) {
                throw new IllegalArgumentException("id shouldn't be null");
            }
            mId = id;
        }

        public void setSessionCallback_impl(Executor executor, C callback) {
            if (executor == null) {
                throw new IllegalArgumentException("executor shouldn't be null");
            }
            if (callback == null) {
                throw new IllegalArgumentException("callback shouldn't be null");
            }
            mCallbackExecutor = executor;
            mCallback = callback;
        }

        public abstract T build_impl();
    }

    public static class BuilderImpl extends BuilderBaseImpl<MediaSession2, SessionCallback> {
        public BuilderImpl(Context context, Builder instance, MediaPlayerInterface player) {
            super(context, player);
        }

        @Override
        public MediaSession2 build_impl() {
            if (mCallbackExecutor == null) {
                mCallbackExecutor = mContext.getMainExecutor();
            }
            if (mCallback == null) {
                mCallback = new SessionCallback(mContext);
            }

            return new MediaSession2Impl(mContext, mPlayer, mId, mVolumeProvider, mRatingType,
                    mSessionActivity, mCallbackExecutor, mCallback).getInstance();
        }
    }
}
+12 −15
Original line number Diff line number Diff line
@@ -28,9 +28,9 @@ import android.media.MediaController2.ControllerCallback;
import android.media.MediaItem2;
import android.media.MediaLibraryService2;
import android.media.MediaLibraryService2.MediaLibrarySession;
import android.media.MediaLibraryService2.MediaLibrarySessionBuilder;
import android.media.MediaLibraryService2.MediaLibrarySessionCallback;
import android.media.MediaMetadata2;
import android.media.MediaMetadata2.Builder;
import android.media.MediaPlayerInterface;
import android.media.MediaSession2;
import android.media.MediaSession2.Command;
@@ -50,6 +50,7 @@ import android.media.update.MediaItem2Provider;
import android.media.update.MediaLibraryService2Provider.MediaLibrarySessionProvider;
import android.media.update.MediaMetadata2Provider;
import android.media.update.MediaSession2Provider;
import android.media.update.MediaSession2Provider.BuilderBaseProvider;
import android.media.update.MediaSession2Provider.PlaylistParamsProvider;
import android.media.update.MediaSessionService2Provider;
import android.media.update.SessionPlayer2Provider;
@@ -102,15 +103,6 @@ public class ApiFactory implements StaticProvider {
        return new MediaBrowser2Impl(context, instance, token, executor, callback);
    }

    @Override
    public MediaSession2Provider createMediaSession2(Context context, MediaSession2 instance,
            MediaPlayerInterface player, String id, VolumeProvider volumeProvider,
            int ratingType, PendingIntent sessionActivity, Executor callbackExecutor,
            SessionCallback callback) {
        return new MediaSession2Impl(context, instance, player, id, volumeProvider, ratingType,
                sessionActivity, callbackExecutor, callback);
    }

    @Override
    public MediaSession2Provider.CommandProvider createMediaSession2Command(
            Command instance, int commandCode, String action, Bundle extra) {
@@ -158,6 +150,11 @@ public class ApiFactory implements StaticProvider {
        return PlaylistParamsImpl.fromBundle(context, bundle);
    }

    public BuilderBaseProvider<MediaSession2, SessionCallback> createMediaSession2Builder(
            Context context, MediaSession2.Builder instance, MediaPlayerInterface player) {
        return new MediaSession2Impl.BuilderImpl(context, instance, player);
    }

    @Override
    public MediaSessionService2Provider createMediaSessionService2(
            MediaSessionService2 instance) {
@@ -171,12 +168,12 @@ public class ApiFactory implements StaticProvider {
    }

    @Override
    public MediaLibrarySessionProvider createMediaLibraryService2MediaLibrarySession(
            Context context, MediaLibrarySession instance, MediaPlayerInterface player,
            String id, VolumeProvider volumeProvider, int ratingType, PendingIntent sessionActivity,
    public BuilderBaseProvider<MediaLibrarySession, MediaLibrarySessionCallback>
        createMediaLibraryService2Builder(
            Context context, MediaLibrarySessionBuilder instance, MediaPlayerInterface player,
            Executor callbackExecutor, MediaLibrarySessionCallback callback) {
        return new MediaLibrarySessionImpl(context, instance, player, id, volumeProvider,
                ratingType, sessionActivity, callbackExecutor, callback);
        return new MediaLibraryService2Impl.BuilderImpl(context, instance, player, callbackExecutor,
                callback);
    }

    @Override