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

Commit 83e0574c authored by Jaewan Kim's avatar Jaewan Kim Committed by Android (Google) Code Review
Browse files

Merge "MediaSession2: Unhide APIs"

parents 162ff7bd 6cc1a5de
Loading
Loading
Loading
Loading
+464 −0

File changed.

Preview size limit exceeded, changes collapsed.

+2 −2
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaMetadata2;
import android.media.MediaPlayerInterface;
import android.media.MediaPlayerBase;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
@@ -277,7 +277,7 @@ public class VideoView2 extends ViewGroupHelper<VideoView2Provider> {
     * @hide
     */
    public void setRouteAttributes(@NonNull List<String> routeCategories,
            @Nullable MediaPlayerInterface player) {
            @Nullable MediaPlayerBase player) {
        mProvider.setRouteAttributes_impl(routeCategories, player);
    }

+0 −1
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import java.util.concurrent.Executor;

/**
 * Browses media content offered by a {@link MediaLibraryService2}.
 * @hide
 */
public class MediaBrowser2 extends MediaController2 {
    // Equals to the ((MediaBrowser2Provider) getProvider())
+121 −23
Original line number Diff line number Diff line
@@ -19,13 +19,13 @@ 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.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.ErrorCode;
import android.media.MediaSession2.PlaylistParams;
import android.media.session.MediaSessionManager;
import android.media.update.ApiLoader;
@@ -64,9 +64,8 @@ import java.util.concurrent.Executor;
 * <p>
 * @see MediaSession2
 * @see MediaSessionService2
 * @hide
 */
public class MediaController2 implements AutoCloseable {
public class MediaController2 implements AutoCloseable, MediaPlaylistController {
    /**
     * Interface for listening to change in activeness of the {@link MediaSession2}.  It's
     * active if and only if it has set a player.
@@ -126,18 +125,68 @@ public class MediaController2 implements AutoCloseable {

        /**
         * Called when the playlist is changed.
         * <p>
         * When it's called, you should invalidate previous playback information such as position,
         * player state, current item, etc.
         *
         * @param playlist A new playlist set by the session.
         */
        // TODO(jaewan): Enhance doc
        public void onPlaylistChanged(@NonNull List<MediaItem2> playlist) { }

        /**
         * Called when the playback state is changed.
         *
         * @param state latest playback state
         * @hide
         */
        // TODo(jaewan): Remove
        public void onPlaybackStateChanged(@NonNull PlaybackState2 state) { }

        /**
         * Called when the player state is changed.
         *
         * @param state
         */
        public void onPlayerStateChanged(int state) { }

        /**
         * Called when the player's position is changed
         *
         * @param updateTimeMs timestamp when the position information is sent from the session
         * @param positionMs position in millis
         */
        public void onPositionUpdated(long updateTimeMs, long positionMs) { }

        /**
         * Called when playback speed is changed.
         *
         * @param speed speed
         */
        public void onPlaybackSpeedChanged(float speed) { }

        /**
         * Called when the player's buffering position
         *
         * @param positionMs buffering position in millis
         */
        public void onBufferedPositionChanged(long positionMs) { }

        /**
         * Called when a error from
         *
         * @param errorCode error code
         * @param extra extra information
         */
        public void onError(@ErrorCode int errorCode, int extra) { }

        /**
         * Called when the player's current playing item is changed
         *
         * @param item new item
         */
        public void onCurrentPlaylistItemChanged(MediaItem2 item) { }

        /**
         * Called when the playlist parameters are changed.
         *
@@ -166,7 +215,6 @@ public class MediaController2 implements AutoCloseable {
        /**
         * @hide
         */
        @SystemApi
        public PlaybackInfo(PlaybackInfoProvider provider) {
            mProvider = provider;
        }
@@ -174,7 +222,6 @@ public class MediaController2 implements AutoCloseable {
        /**
         * @hide
         */
        @SystemApi
        public PlaybackInfoProvider getProvider() {
            return mProvider;
        }
@@ -281,7 +328,6 @@ public class MediaController2 implements AutoCloseable {
    /**
     * @hide
     */
    @SystemApi
    public MediaController2Provider getProvider() {
        return mProvider;
    }
@@ -325,7 +371,7 @@ public class MediaController2 implements AutoCloseable {
     * Request that the player prepare its playback. In other words, other sessions can continue
     * to play during the preparation of this session. This method can be used to speed up the
     * start of the playback. Once the preparation is done, the session will change its playback
     * state to {@link PlaybackState2#STATE_PAUSED}. Afterwards, {@link #play} can be called to
     * state to {@link MediaPlayerBase#STATE_PAUSED}. Afterwards, {@link #play} can be called to
     * start playback.
     */
    public void prepare() {
@@ -360,12 +406,13 @@ public class MediaController2 implements AutoCloseable {
    /**
     * Sets the index of current DataSourceDesc in the play list to be played.
     *
     * @param index the index of DataSourceDesc in the play list you want to play
     * @param item the index of DataSourceDesc in the play list you want to play
     * @throws IllegalArgumentException if the play list is null
     * @throws NullPointerException if index is outside play list range
     */
    public void setCurrentPlaylistItem(int index) {
        mProvider.setCurrentPlaylistItem_impl(index);
    @Override
    public void skipToPlaylistItem(@NonNull MediaItem2 item) {
        mProvider.skipToPlaylistItem_impl(item);
    }

    /**
@@ -375,7 +422,7 @@ public class MediaController2 implements AutoCloseable {
     * @param params A {@link PlaylistParams} object to set.
     * @throws IllegalArgumentException if given {@param param} is null.
     */
    public void setPlaylistParams(PlaylistParams params) {
    public void setPlaylistParams(@NonNull PlaylistParams params) {
        mProvider.setPlaylistParams_impl(params);
    }

@@ -428,12 +475,11 @@ public class MediaController2 implements AutoCloseable {
        mProvider.playFromUri_impl(uri, extras);
    }


    /**
     * Request that the player prepare playback for a specific media id. In other words, other
     * sessions can continue to play during the preparation of this session. This method can be
     * used to speed up the start of the playback. Once the preparation is done, the session
     * will change its playback state to {@link PlaybackState2#STATE_PAUSED}. Afterwards,
     * will change its playback state to {@link MediaPlayerBase#STATE_PAUSED}. Afterwards,
     * {@link #play} can be called to start playback. If the preparation is not needed,
     * {@link #playFromMediaId} can be directly called without this method.
     *
@@ -450,7 +496,7 @@ public class MediaController2 implements AutoCloseable {
     * query should be treated as a request to prepare any music. In other words, other sessions
     * can continue to play during the preparation of this session. This method can be used to
     * speed up the start of the playback. Once the preparation is done, the session will
     * change its playback state to {@link PlaybackState2#STATE_PAUSED}. Afterwards,
     * change its playback state to {@link MediaPlayerBase#STATE_PAUSED}. Afterwards,
     * {@link #play} can be called to start playback. If the preparation is not needed,
     * {@link #playFromSearch} can be directly called without this method.
     *
@@ -466,7 +512,7 @@ public class MediaController2 implements AutoCloseable {
     * Request that the player prepare playback for a specific {@link Uri}. In other words,
     * other sessions can continue to play during the preparation of this session. This method
     * can be used to speed up the start of the playback. Once the preparation is done, the
     * session will change its playback state to {@link PlaybackState2#STATE_PAUSED}. Afterwards,
     * session will change its playback state to {@link MediaPlayerBase#STATE_PAUSED}. Afterwards,
     * {@link #play} can be called to start playback. If the preparation is not needed,
     * {@link #playFromUri} can be directly called without this method.
     *
@@ -537,11 +583,64 @@ public class MediaController2 implements AutoCloseable {
     * playback state.
     *
     * @return a playback state. Can be {@code null}
     * @hide
     */
    public @Nullable PlaybackState2 getPlaybackState() {
        return mProvider.getPlaybackState_impl();
    }

    /**
     * Get the lastly cached player state from {@link ControllerCallback#onPlayerStateChanged(int)}.
     *
     * @return player state
     */
    public int getPlayerState() {
        return mProvider.getPlayerState_impl();
    }

    /**
     * Get the lastly cached position from {@link ControllerCallback#onPositionUpdated(long, long)}.
     * <p>
     * This returns the calculated value of the position, based on the difference between the
     * update time and current time.
     *
     * @return position
     */
    public long getPosition() {
        return mProvider.getPosition_impl();
    }

    /**
     * Get the lastly cached playback speed from
     * {@link ControllerCallback#onPlaybackSpeedChanged(float)}.
     *
     * @return speed
     */
    public float getPlaybackSpeed() {
        return mProvider.getPlaybackSpeed_impl();
    }

    /**
     * Get the lastly cached buffered position from
     * {@link ControllerCallback#onBufferedPositionChanged(long)}.
     *
     * @return buffering position in millis
     */
    public long getBufferedPosition() {
        return mProvider.getBufferedPosition_impl();
    }

    /**
     * Get the lastly cached current item from
     * {@link ControllerCallback#onCurrentPlaylistItemChanged(MediaItem2)}.
     *
     * @return index of the current item
     */
    @Override
    public MediaItem2 getCurrentPlaylistItem() {
        return mProvider.getCurrentPlaylistItem_impl();
    }

    /**
     * Get the current playback info for this session.
     *
@@ -584,6 +683,7 @@ public class MediaController2 implements AutoCloseable {
     *
     * @return playlist. Can be {@code null} if the controller doesn't have enough permission.
     */
    @Override
    public @Nullable List<MediaItem2> getPlaylist() {
        return mProvider.getPlaylist_impl();
    }
@@ -603,13 +703,11 @@ public class MediaController2 implements AutoCloseable {
     * If index is same as the current index of the playlist, current playback
     * will be stopped and playback moves to next source in the list.
     *
     * @return the removed DataSourceDesc at index in the play list
     * @throws IllegalArgumentException if the play list is null
     * @throws IndexOutOfBoundsException if index is outside play list range
     */
    // TODO(jaewan): Remove with index was previously rejected by council (b/36524925)
    // TODO(jaewan): Should we also add movePlaylistItem from index to index?
    public void removePlaylistItem(MediaItem2 item) {
    @Override
    public void removePlaylistItem(@NonNull MediaItem2 item) {
        mProvider.removePlaylistItem_impl(item);
    }

@@ -620,12 +718,12 @@ public class MediaController2 implements AutoCloseable {
     * If index is less than or equal to the current index of the play list,
     * the current index of the play list will be incremented correspondingly.
     *
     * @param index the index you want to add dsd to the play list
     * @param item the media item you want to add to the play list
     * @param index the index you want to add
     * @param item the media item you want to add
     * @throws IndexOutOfBoundsException if index is outside play list range
     * @throws NullPointerException if dsd is null
     */
    public void addPlaylistItem(int index, MediaItem2 item) {
    @Override
    public void addPlaylistItem(int index, @NonNull MediaItem2 item) {
        mProvider.addPlaylistItem_impl(index, item);
    }
}
+85 −3
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package android.media;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.content.Context;
import android.media.update.ApiLoader;
import android.media.update.MediaItem2Provider;
@@ -35,7 +34,6 @@ import java.lang.annotation.RetentionPolicy;
 * When it's sent to a controller or browser, it's anonymized and data descriptor wouldn't be sent.
 * <p>
 * This object isn't a thread safe.
 * @hide
 */
public class MediaItem2 {
    /** @hide */
@@ -64,7 +62,9 @@ public class MediaItem2 {
     * @param mediaId id of this item. It must be unique whithin this app
     * @param metadata metadata with the media id.
     * @param flags The flags for this item.
     * @hide
     */
    // TODO(jaewan): Remove this
    public MediaItem2(@NonNull Context context, @NonNull String mediaId,
            @NonNull DataSourceDesc dsd, @Nullable MediaMetadata2 metadata,
            @Flags int flags) {
@@ -76,7 +76,6 @@ public class MediaItem2 {
     * Create a new media item
     * @hide
     */
    @SystemApi
    public MediaItem2(MediaItem2Provider provider) {
        mProvider = provider;
    }
@@ -156,4 +155,87 @@ public class MediaItem2 {
    public @Nullable DataSourceDesc getDataSourceDesc() {
        return mProvider.getDataSourceDesc_impl();
    }

    /**
     * Build {@link MediaItem2}
     */
    // TODO(jaewan): Move it to updatable
    public static final class Builder {
        private Context mContext;
        private @Flags int mFlags;
        private String mMediaId;
        private MediaMetadata2 mMetadata;
        private DataSourceDesc mDataSourceDesc;

        /**
         * Constructor for {@link Builder}
         *
         * @param context
         * @param flags
         */
        public Builder(@NonNull Context context, @Flags int flags) {
            mContext = context;
            mFlags = flags;
        }

        /**
         * Set the media id of this instance. {@code null} for unset.
         * <p>
         * Media id is used to identify a media contents between session and controller.
         * <p>
         * If the metadata is set with the {@link #setMetadata(MediaMetadata2)} and it has
         * media id, id from {@link #setMediaId(String)} will be ignored and metadata's id will be
         * used instead. If the id isn't set neither by {@link #setMediaId(String)} nor
         * {@link #setMetadata(MediaMetadata2)}, id will be automatically generated.
         *
         * @param mediaId media id
         * @return this instance for chaining
         */
        public Builder setMediaId(@Nullable String mediaId) {
            mMediaId = mediaId;
            return this;
        }

        /**
         * Set the metadata of this instance. {@code null} for unset.
         * <p>
         * If the metadata is set with the {@link #setMetadata(MediaMetadata2)} and it has
         * media id, id from {@link #setMediaId(String)} will be ignored and metadata's id will be
         * used instead. If the id isn't set neither by {@link #setMediaId(String)} nor
         * {@link #setMetadata(MediaMetadata2)}, id will be automatically generated.
         *
         * @param metadata metadata
         * @return this instance for chaining
         */
        public Builder setMetadata(@Nullable MediaMetadata2 metadata) {
            mMetadata = metadata;
            return this;
        }

        /**
         * Set the data source descriptor for this instance. {@code null} for unset.
         *
         * @param dataSourceDesc data source descriptor
         * @return this instance for chaining
         */
        public Builder setDataSourceDesc(@Nullable DataSourceDesc dataSourceDesc) {
            mDataSourceDesc = dataSourceDesc;
            return this;
        }

        /**
         * Build {@link MediaItem2}.
         *
         * @return a new {@link MediaItem2}.
         */
        public MediaItem2 build() {
            String id = (mMetadata != null)
                    ? mMetadata.getString(MediaMetadata2.METADATA_KEY_MEDIA_ID) : null;
            if (id == null) {
                //  TODO(jaewan): Double check if its sufficient (e.g. Use UUID instead?)
                id = (mMediaId != null) ? mMediaId : toString();
            }
            return new MediaItem2(mContext, id, mDataSourceDesc, mMetadata, mFlags);
        }
    }
}
Loading