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

Commit 622b1f1b authored by Christofer Åkersten's avatar Christofer Åkersten
Browse files

Move MediaSession2 builder to impl

Bug: 72665881
Test: runtest-MediaComponents
Change-Id: I22a299dac9d9576e4383fe9538fe696f09afaae5
parent 4cc91d06
Loading
Loading
Loading
Loading
+14 −39
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.media;
import android.annotation.CallbackExecutor;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.PendingIntent;
import android.content.Context;
import android.media.MediaSession2.BuilderBase;
@@ -63,26 +64,16 @@ public abstract class MediaLibraryService2 extends MediaSessionService2 {
    /**
     * Session for the media library service.
     */
    public class MediaLibrarySession extends MediaSession2 {
    public static class MediaLibrarySession extends MediaSession2 {
        private final MediaLibrarySessionProvider mProvider;

        MediaLibrarySession(Context context, MediaPlayerInterface player, String id,
                VolumeProvider volumeProvider, int ratingType, PendingIntent sessionActivity,
                Executor callbackExecutor, SessionCallback callback) {
            super(context, player, id, volumeProvider, ratingType, sessionActivity,
                    callbackExecutor, callback);
            mProvider = (MediaLibrarySessionProvider) getProvider();
        }

        @Override
        MediaSession2Provider createProvider(Context context, MediaPlayerInterface player,
                String id, VolumeProvider volumeProvider, int ratingType,
                PendingIntent sessionActivity, Executor callbackExecutor,
                SessionCallback callback) {
            return ApiLoader.getProvider(context)
                    .createMediaLibraryService2MediaLibrarySession(context, this, player, id,
                            volumeProvider, ratingType, sessionActivity,
                            callbackExecutor, (MediaLibrarySessionCallback) callback);
        /**
         * @hide
         */
        @SystemApi
        public MediaLibrarySession(MediaLibrarySessionProvider provider) {
            super(provider);
            mProvider = provider;
        }

        /**
@@ -208,31 +199,15 @@ public abstract class MediaLibraryService2 extends MediaSessionService2 {
    /**
     * Builder for {@link MediaLibrarySession}.
     */
    // TODO(jaewan): Move this to updatable.
    public class MediaLibrarySessionBuilder
            extends BuilderBase<MediaLibrarySessionBuilder, MediaLibrarySessionCallback> {
    public class MediaLibrarySessionBuilder extends BuilderBase<MediaLibrarySession,
            MediaLibrarySessionBuilder, MediaLibrarySessionCallback> {
        public MediaLibrarySessionBuilder(
                @NonNull Context context, @NonNull MediaPlayerInterface player,
                @NonNull @CallbackExecutor Executor callbackExecutor,
                @NonNull MediaLibrarySessionCallback callback) {
            super(context, player);
            setSessionCallback(callbackExecutor, callback);
        }

        @Override
        public MediaLibrarySessionBuilder setSessionCallback(
                @NonNull @CallbackExecutor Executor callbackExecutor,
                @NonNull MediaLibrarySessionCallback callback) {
            if (callback == null) {
                throw new IllegalArgumentException("MediaLibrarySessionCallback cannot be null");
            }
            return super.setSessionCallback(callbackExecutor, callback);
        }

        @Override
        public MediaLibrarySession build() {
            return new MediaLibrarySession(mContext, mPlayer, mId, mVolumeProvider, mRatingType,
                    mSessionActivity, mCallbackExecutor, mCallback);
            super((instance) -> ApiLoader.getProvider(context).createMediaLibraryService2Builder(
                    context, (MediaLibrarySessionBuilder) instance, player, callbackExecutor,
                    callback));
        }
    }

+30 −84
Original line number Diff line number Diff line
@@ -30,9 +30,11 @@ import android.media.session.MediaSession.Callback;
import android.media.session.PlaybackState;
import android.media.update.ApiLoader;
import android.media.update.MediaSession2Provider;
import android.media.update.MediaSession2Provider.BuilderBaseProvider;
import android.media.update.MediaSession2Provider.CommandGroupProvider;
import android.media.update.MediaSession2Provider.CommandProvider;
import android.media.update.MediaSession2Provider.ControllerInfoProvider;
import android.media.update.ProviderCreator;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
@@ -403,36 +405,11 @@ public class MediaSession2 implements AutoCloseable {
     * @hide
     */
    static abstract class BuilderBase
            <T extends MediaSession2.BuilderBase<T, C>, C extends SessionCallback> {
        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 BuilderBase(@NonNull Context context, @NonNull 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 = "";
            <T extends MediaSession2, U extends BuilderBase<T, U, C>, C extends SessionCallback> {
        private final BuilderBaseProvider<T, C> mProvider;

        BuilderBase(ProviderCreator<BuilderBase<T, U, C>, BuilderBaseProvider<T, C>> creator) {
            mProvider = creator.createProvider(this);
        }

        /**
@@ -444,9 +421,9 @@ public class MediaSession2 implements AutoCloseable {
         *
         * @param volumeProvider The provider that will handle volume changes. Can be {@code null}
         */
        public T setVolumeProvider(@Nullable VolumeProvider volumeProvider) {
            mVolumeProvider = volumeProvider;
            return (T) this;
        public U setVolumeProvider(@Nullable VolumeProvider volumeProvider) {
            mProvider.setVolumeProvider_impl(volumeProvider);
            return (U) this;
        }

        /**
@@ -462,9 +439,9 @@ public class MediaSession2 implements AutoCloseable {
         * <li>{@link Rating2#RATING_THUMB_UP_DOWN}</li>
         * </ul>
         */
        public T setRatingType(@Rating2.Style int type) {
            mRatingType = type;
            return (T) this;
        public U setRatingType(@Rating2.Style int type) {
            mProvider.setRatingType_impl(type);
            return (U) this;
        }

        /**
@@ -474,9 +451,9 @@ public class MediaSession2 implements AutoCloseable {
         *
         * @param pi The intent to launch to show UI for this session.
         */
        public T setSessionActivity(@Nullable PendingIntent pi) {
            mSessionActivity = pi;
            return (T) this;
        public U setSessionActivity(@Nullable PendingIntent pi) {
            mProvider.setSessionActivity_impl(pi);
            return (U) this;
        }

        /**
@@ -489,12 +466,9 @@ public class MediaSession2 implements AutoCloseable {
         * @throws IllegalArgumentException if id is {@code null}
         * @return
         */
        public T setId(@NonNull String id) {
            if (id == null) {
                throw new IllegalArgumentException("id shouldn't be null");
            }
            mId = id;
            return (T) this;
        public U setId(@NonNull String id) {
            mProvider.setId_impl(id);
            return (U) this;
        }

        /**
@@ -504,17 +478,10 @@ public class MediaSession2 implements AutoCloseable {
         * @param callback session callback.
         * @return
         */
        public T setSessionCallback(@NonNull @CallbackExecutor Executor executor,
        public U setSessionCallback(@NonNull @CallbackExecutor Executor executor,
                @NonNull 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;
            return (T) this;
            mProvider.setSessionCallback_impl(executor, callback);
            return (U) this;
        }

        /**
@@ -524,7 +491,9 @@ public class MediaSession2 implements AutoCloseable {
         * @throws IllegalStateException if the session with the same id is already exists for the
         *      package.
         */
        public abstract MediaSession2 build();
        public T build() {
            return mProvider.build_impl();
        }
    }

    /**
@@ -533,24 +502,12 @@ public class MediaSession2 implements AutoCloseable {
     * Any incoming event from the {@link MediaController2} will be handled on the thread
     * that created session with the {@link Builder#build()}.
     */
    // TODO(jaewan): Move this to updatable
    // TODO(jaewan): Add setRatingType()
    // TODO(jaewan): Add setSessionActivity()
    public static final class Builder extends BuilderBase<Builder, SessionCallback> {
    public static final class Builder extends BuilderBase<MediaSession2, Builder, SessionCallback> {
        public Builder(Context context, @NonNull MediaPlayerInterface player) {
            super(context, player);
        }

        @Override
        public MediaSession2 build() {
            if (mCallbackExecutor == null) {
                mCallbackExecutor = mContext.getMainExecutor();
            }
            if (mCallback == null) {
                mCallback = new SessionCallback(mContext);
            }
            return new MediaSession2(mContext, mPlayer, mId, mVolumeProvider, mRatingType,
                    mSessionActivity, mCallbackExecutor, mCallback);
            super((instance) -> ApiLoader.getProvider(context).createMediaSession2Builder(
                    context, (Builder) instance, player));
        }
    }

@@ -932,21 +889,10 @@ public class MediaSession2 implements AutoCloseable {
     *       framework had to add heuristics to figure out if an app is
     * @hide
     */
    MediaSession2(Context context, MediaPlayerInterface player, String id,
            VolumeProvider volumeProvider, int ratingType, PendingIntent sessionActivity,
            Executor callbackExecutor, SessionCallback callback) {
    @SystemApi
    public MediaSession2(MediaSession2Provider provider) {
        super();
        mProvider = createProvider(context, player, id, volumeProvider, ratingType, sessionActivity,
                callbackExecutor, callback);
        mProvider.initialize();
    }

    MediaSession2Provider createProvider(Context context, MediaPlayerInterface player, String id,
            VolumeProvider volumeProvider, int ratingType, PendingIntent sessionActivity,
            Executor callbackExecutor, SessionCallback callback) {
        return ApiLoader.getProvider(context)
                .createMediaSession2(context, this, player, id, volumeProvider, ratingType,
                        sessionActivity, callbackExecutor, callback);
        mProvider = provider;
    }

    @SystemApi
+3 −0
Original line number Diff line number Diff line
@@ -17,12 +17,15 @@
package android.media.update;

import android.annotation.SystemApi;
import android.media.MediaLibraryService2.MediaLibrarySession;
import android.media.MediaLibraryService2.MediaLibrarySessionCallback;
import android.media.MediaSession2.ControllerInfo;
import android.os.Bundle;

/**
 * @hide
 */
// TODO: @SystemApi
public interface MediaLibraryService2Provider extends MediaSessionService2Provider {
    // Nothing new for now

+12 −2
Original line number Diff line number Diff line
@@ -16,15 +16,18 @@

package android.media.update;

import android.app.PendingIntent;
import android.media.MediaItem2;
import android.media.MediaMetadata2;
import android.media.MediaPlayerInterface;
import android.media.MediaPlayerInterface.PlaybackListener;
import android.media.MediaSession2;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.SessionCallback;
import android.media.SessionToken2;
import android.media.VolumeProvider;
import android.os.Bundle;
@@ -38,8 +41,6 @@ import java.util.concurrent.Executor;
 */
// TODO: @SystemApi
public interface MediaSession2Provider extends TransportControlProvider {
    void initialize();

    void close_impl();
    void setPlayer_impl(MediaPlayerInterface player);
    void setPlayer_impl(MediaPlayerInterface player, VolumeProvider volumeProvider);
@@ -95,4 +96,13 @@ public interface MediaSession2Provider extends TransportControlProvider {
        MediaMetadata2 getPlaylistMetadata_impl();
        Bundle toBundle_impl();
    }

    interface BuilderBaseProvider<T extends MediaSession2, C extends SessionCallback> {
        void setVolumeProvider_impl(VolumeProvider volumeProvider);
        void setRatingType_impl(int type);
        void setSessionActivity_impl(PendingIntent pi);
        void setId_impl(String id);
        void setSessionCallback_impl(Executor executor, C callback);
        T build_impl();
    }
}
+23 −0
Original line number Diff line number Diff line
/*
 * Copyright 2018 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.update;

/** @hide */
@FunctionalInterface
public interface ProviderCreator<T, U> {
    U createProvider(T instance);
}
Loading