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

Commit 15d3ba62 authored by Hyundo Moon's avatar Hyundo Moon Committed by Android (Google) Code Review
Browse files

Merge "MediaSession2: Replace PlaybackState2" into pi-dev

parents f0927b07 c006dc2a
Loading
Loading
Loading
Loading
+0 −25
Original line number Diff line number Diff line
@@ -155,17 +155,6 @@ public class MediaController2 implements AutoCloseable {
        public void onPlaylistChanged(@NonNull MediaController2 controller,
                @NonNull List<MediaItem2> playlist) { }

        /**
         * Called when the playback state is changed.
         *
         * @param controller the controller for this event
         * @param state latest playback state
         * @hide
         */
        // TODO(jaewan): Remove (b/73971431)
        public void onPlaybackStateChanged(@NonNull MediaController2 controller,
                @NonNull PlaybackState2 state) { }

        /**
         * Called when the player state is changed.
         *
@@ -646,20 +635,6 @@ public class MediaController2 implements AutoCloseable {
        return mProvider.getSessionActivity_impl();
    }

    /**
     * Get the lastly cached {@link PlaybackState2} from
     * {@link ControllerCallback#onPlaybackStateChanged(MediaController2, PlaybackState2)}.
     * <p>
     * It may return {@code null} before the first callback or session has sent {@code null}
     * 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(MediaController2, int)}.
+29 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.media;

import static android.media.MediaPlayerBase.PLAYER_STATE_IDLE;

import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.NonNull;
@@ -1639,13 +1641,36 @@ public class MediaSession2 implements AutoCloseable {
    }

    /**
     * Return the {@link PlaybackState2} from the player.
     * Get the player state.
     *
     * @return player state
     * @hide
     */
    public @PlayerState int getPlayerState() {
        // TODO(jaewan): implement this (b/74578458)
        return PLAYER_STATE_IDLE;
    }

    /**
     * Get the current position.
     *
     * @return playback state
     * @return position
     * @hide
     */
    public PlaybackState2 getPlaybackState() {
        return mProvider.getPlaybackState_impl();
    public long getCurrentPosition() {
        // TODO(jaewan): implement this (b/74578458)
        return -1;
    }

    /**
     * Get the buffered position.
     *
     * @return buffered position
     * @hide
     */
    public long getBufferedPosition() {
        // TODO(jaewan): implement this (b/74578458)
        return -1;
    }

    /**
+0 −217
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;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.media.update.ApiLoader;
import android.media.update.PlaybackState2Provider;
import android.os.Bundle;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Playback state for a {@link MediaPlayerBase}, to be shared between {@link MediaSession2} and
 * {@link MediaController2}. This includes a playback state {@link #STATE_PLAYING},
 * the current playback position and extra.
 * @hide
 */
// TODO(jaewan): Remove this (b/73971431)
public final class PlaybackState2 {
    // Similar to the PlaybackState with following changes
    //    - Not implement Parcelable and added from/toBundle()
    //    - Removed playback state that doesn't match with the MediaPlayer2
    //      Full list should be finalized when the MediaPlayer2 has getter for the playback state.
    //      Here's table for the MP2 state and PlaybackState2.State.
    //         +----------------------------------------+----------------------------------------+
    //         | MediaPlayer2 state                     | Matching PlaybackState2.State          |
    //         | (Names are from MP2' Javadoc)          |                                        |
    //         +----------------------------------------+----------------------------------------+
    //         | Idle: Just finished creating MP2       | STATE_NONE                             |
    //         |     or reset() is called               |                                        |
    //         +----------------------------------------+----------------------------------------+
    //         | Initialized: setDataSource/Playlist    | N/A (Session/Controller don't          |
    //         |                                        |     differentiate with Prepared)       |
    //         +----------------------------------------+----------------------------------------+
    //         | Prepared: Prepared after initialized   | STATE_PAUSED                           |
    //         +----------------------------------------+----------------------------------------+
    //         | Started: Started playback              | STATE_PLAYING                          |
    //         +----------------------------------------+----------------------------------------+
    //         | Paused: Paused playback                | STATE_PAUSED                           |
    //         +----------------------------------------+----------------------------------------+
    //         | PlaybackCompleted: Playback is done    | STATE_PAUSED                           |
    //         +----------------------------------------+----------------------------------------+
    //         | Stopped: MP2.stop() is called.         | STATE_STOPPED                          |
    //         |     prepare() is needed to play again  |                                        |
    //         |     (Seemingly the same as initialized |                                        |
    //         |     because cannot set data source     |                                        |
    //         |     after this)                        |                                        |
    //         +----------------------------------------+----------------------------------------+
    //         | Error: an API is called in a state     | STATE_ERROR                            |
    //         |     that the API isn't supported       |                                        |
    //         +----------------------------------------+----------------------------------------+
    //         | End: MP2.close() is called to release  | N/A (MediaSession will be gone)        |
    //         |    MP2. Cannot be reused anymore       |                                        |
    //         +----------------------------------------+----------------------------------------+
    //         | Started, but                           | STATE_BUFFERING                        |
    //         |    EventCallback.onBufferingUpdate()   |                                        |
    //         +----------------------------------------+----------------------------------------+
    //    - Removed actions and custom actions.
    //    - Removed error string
    //    - Repeat mode / shuffle mode is now in the PlaylistParams
    /**
     * @hide
     */
    @IntDef({STATE_NONE, STATE_STOPPED, STATE_PAUSED, STATE_PLAYING, STATE_BUFFERING, STATE_ERROR})
    @Retention(RetentionPolicy.SOURCE)
    public @interface State {}

    /**
     * This is the default playback state and indicates that no media has been
     * added yet, or the performer has been reset and has no content to play.
     */
    public final static int STATE_NONE = 0;

    /**
     * State indicating this item is currently stopped.
     */
    public final static int STATE_STOPPED = 1;

    /**
     * State indicating this item is currently paused.
     */
    public final static int STATE_PAUSED = 2;

    /**
     * State indicating this item is currently playing.
     */
    public final static int STATE_PLAYING = 3;

    /**
     * State indicating this item is currently buffering and will begin playing
     * when enough data has buffered.
     */
    public final static int STATE_BUFFERING = 4;

    /**
     * State indicating this item is currently in an error state.
     */
    public final static int STATE_ERROR = 5;

    /**
     * Use this value for the position to indicate the position is not known.
     */
    public final static long PLAYBACK_POSITION_UNKNOWN = -1;

    private final PlaybackState2Provider mProvider;

    public PlaybackState2(@NonNull Context context, int state, long position, long updateTime,
            float speed, long bufferedPosition, long activeItemId) {
        mProvider = ApiLoader.getProvider(context).createPlaybackState2(context, this, state,
                position, updateTime, speed, bufferedPosition, activeItemId);
    }

    @Override
    public String toString() {
        return mProvider.toString_impl();
    }

    /**
     * Get the current state of playback. One of the following:
     * <ul>
     * <li> {@link PlaybackState2#STATE_NONE}</li>
     * <li> {@link PlaybackState2#STATE_STOPPED}</li>
     * <li> {@link PlaybackState2#STATE_PAUSED}</li>
     * <li> {@link PlaybackState2#STATE_PLAYING}</li>
     * <li> {@link PlaybackState2#STATE_BUFFERING}</li>
     * <li> {@link PlaybackState2#STATE_ERROR}</li>
     * </ul>
     */
    @State
    public int getState() {
        return mProvider.getState_impl();
    }

    /**
     * Get the current playback position in ms.
     */
    public long getPosition() {
        return mProvider.getPosition_impl();
    }

    /**
     * Get the current buffered position in ms. This is the farthest playback
     * point that can be reached from the current position using only buffered
     * content.
     */
    public long getBufferedPosition() {
        return mProvider.getBufferedPosition_impl();
    }

    /**
     * Get the current playback speed as a multiple of normal playback. This
     * should be negative when rewinding. A value of 1 means normal playback and
     * 0 means paused.
     *
     * @return The current speed of playback.
     */
    public float getPlaybackSpeed() {
        return mProvider.getPlaybackSpeed_impl();
    }

    /**
     * Get the elapsed real time at which position was last updated. If the
     * position has never been set this will return 0;
     *
     * @return The last time the position was updated.
     */
    public long getLastPositionUpdateTime() {
        return mProvider.getLastPositionUpdateTime_impl();
    }

    /**
     * Get the id of the currently active item in the playlist.
     *
     * @return The id of the currently active item in the queue
     */
    public long getCurrentPlaylistItemIndex() {
        return mProvider.getCurrentPlaylistItemIndex_impl();
    }

    /**
     * Returns this object as a bundle to share between processes.
     */
    public @NonNull Bundle toBundle() {
        return mProvider.toBundle_impl();
    }

    /**
     * Creates an instance from a bundle which is previously created by {@link #toBundle()}.
     *
     * @param context context
     * @param bundle A bundle created by {@link #toBundle()}.
     * @return A new {@link PlaybackState2} instance. Returns {@code null} if the given
     *         {@param bundle} is null, or if the {@param bundle} has no playback state parameters.
     */
    public @Nullable static PlaybackState2 fromBundle(@NonNull Context context,
            @Nullable Bundle bundle) {
        return ApiLoader.getProvider(context).fromBundle_PlaybackState2(context, bundle);
    }
}
+0 −3
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package android.media.update;

import android.annotation.NonNull;
import android.app.PendingIntent;
import android.media.AudioAttributes;
import android.media.MediaController2.PlaybackInfo;
@@ -24,7 +23,6 @@ import android.media.MediaItem2;
import android.media.MediaMetadata2;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.PlaylistParams;
import android.media.PlaybackState2;
import android.media.Rating2;
import android.media.SessionToken2;
import android.net.Uri;
@@ -69,7 +67,6 @@ public interface MediaController2Provider extends TransportControlProvider {

    PlaylistParams getPlaylistParams_impl();
    void setPlaylistParams_impl(PlaylistParams params);
    PlaybackState2 getPlaybackState_impl();
    int getPlayerState_impl();
    long getPosition_impl();
    float getPlaybackSpeed_impl();
+0 −1
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import android.app.Notification;
import android.content.Intent;
import android.media.MediaSession2;
import android.media.MediaSessionService2.MediaNotification;
import android.media.PlaybackState2;
import android.os.IBinder;

/**
Loading