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

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

Merge "Add fromBundle/toBundle to PlaybackState2/PlaylistParams"

parents 6cb0712c 5b2e35ad
Loading
Loading
Loading
Loading
+51 −1
Original line number Diff line number Diff line
@@ -849,7 +849,6 @@ public class MediaSession2 implements AutoCloseable {
    /**
     * Parameter for the playlist.
     */
    // TODO(jaewan): add fromBundle()/toBundle()
    public static class PlaylistParams {
        /**
         * @hide
@@ -905,6 +904,16 @@ public class MediaSession2 implements AutoCloseable {
         */
        public static final int SHUFFLE_MODE_GROUP = 2;

        /**
         * Keys used for converting a PlaylistParams object to a bundle object and vice versa.
         */
        private static final String KEY_REPEAT_MODE =
                "android.media.session2.playlistparams2.repeat_mode";
        private static final String KEY_SHUFFLE_MODE =
                "android.media.session2.playlistparams2.shuffle_mode";
        private static final String KEY_MEDIA_METADATA2_BUNDLE =
                "android.media.session2.playlistparams2.metadata2_bundle";

        private @RepeatMode int mRepeatMode;
        private @ShuffleMode int mShuffleMode;

@@ -928,6 +937,47 @@ public class MediaSession2 implements AutoCloseable {
        public MediaMetadata2 getPlaylistMetadata() {
            return mPlaylistMetadata;
        }

        /**
         * Returns this object as a bundle to share between processes.
         *
         * @hide
         */
        public Bundle toBundle() {
            Bundle bundle = new Bundle();
            bundle.putInt(KEY_REPEAT_MODE, mRepeatMode);
            bundle.putInt(KEY_SHUFFLE_MODE, mShuffleMode);
            if (mPlaylistMetadata != null) {
                bundle.putBundle(KEY_MEDIA_METADATA2_BUNDLE, mPlaylistMetadata.getBundle());
            }
            return bundle;
        }

        /**
         * Creates an instance from a bundle which is previously created by {@link #toBundle()}.
         *
         * @param bundle A bundle created by {@link #toBundle()}.
         * @return A new {@link PlaylistParams} instance. Returns {@code null} if the given
         *         {@param bundle} is null, or if the {@param bundle} has no playlist parameters.
         * @hide
         */
        public static PlaylistParams fromBundle(Bundle bundle) {
            if (bundle == null) {
                return null;
            }
            if (!bundle.containsKey(KEY_REPEAT_MODE) || !bundle.containsKey(KEY_SHUFFLE_MODE)) {
                return null;
            }

            Bundle metadataBundle = bundle.getBundle(KEY_MEDIA_METADATA2_BUNDLE);
            MediaMetadata2 metadata =
                    metadataBundle == null ? null : new MediaMetadata2(metadataBundle);

            return new PlaylistParams(
                    bundle.getInt(KEY_REPEAT_MODE),
                    bundle.getInt(KEY_SHUFFLE_MODE),
                    metadata);
        }
    }

    /**
+49 −12
Original line number Diff line number Diff line
@@ -31,8 +31,6 @@ import java.lang.annotation.RetentionPolicy;
public final class PlaybackState2 {
    private static final String TAG = "PlaybackState2";

    private static final String KEY_STATE = "android.media.playbackstate2.state";

    // TODO(jaewan): Replace states from MediaPlayer2
    /**
     * @hide
@@ -90,13 +88,25 @@ public final class PlaybackState2 {
     */
    public final static long PLAYBACK_POSITION_UNKNOWN = -1;

    /**
     * Keys used for converting a PlaybackState2 to a bundle object and vice versa.
     */
    private static final String KEY_STATE = "android.media.playbackstate2.state";
    private static final String KEY_POSITION = "android.media.playbackstate2.position";
    private static final String KEY_BUFFERED_POSITION =
            "android.media.playbackstate2.buffered_position";
    private static final String KEY_SPEED = "android.media.playbackstate2.speed";
    private static final String KEY_ERROR_MESSAGE = "android.media.playbackstate2.error_message";
    private static final String KEY_UPDATE_TIME = "android.media.playbackstate2.update_time";
    private static final String KEY_ACTIVE_ITEM_ID = "android.media.playbackstate2.active_item_id";

    private final int mState;
    private final long mPosition;
    private final long mBufferedPosition;
    private final float mSpeed;
    private final CharSequence mErrorMessage;
    private final long mUpdateTime;
    private final float mSpeed;
    private final long mBufferedPosition;
    private final long mActiveItemId;
    private final CharSequence mErrorMessage;

    public PlaybackState2(int state, long position, long updateTime, float speed,
            long bufferedPosition, long activeItemId, CharSequence error) {
@@ -194,22 +204,49 @@ public final class PlaybackState2 {
    }

    /**
     * @return Bundle object for this to share between processes.
     * Returns this object as a bundle to share between processes.
     */
    public Bundle toBundle() {
        // TODO(jaewan): Include other variables.
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_STATE, mState);
        bundle.putLong(KEY_POSITION, mPosition);
        bundle.putLong(KEY_UPDATE_TIME, mUpdateTime);
        bundle.putFloat(KEY_SPEED, mSpeed);
        bundle.putLong(KEY_BUFFERED_POSITION, mBufferedPosition);
        bundle.putLong(KEY_ACTIVE_ITEM_ID, mActiveItemId);
        bundle.putCharSequence(KEY_ERROR_MESSAGE, mErrorMessage);
        return bundle;
    }

    /**
     * @param bundle input
     * @return
     * Creates an instance from a bundle which is previously created by {@link #toBundle()}.
     *
     * @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 static PlaybackState2 fromBundle(Bundle bundle) {
        // TODO(jaewan): Include other variables.
        final int state = bundle.getInt(KEY_STATE);
        return new PlaybackState2(state, 0, 0, 0, 0, 0, null);
        if (bundle == null) {
            return null;
        }

        if (!bundle.containsKey(KEY_STATE)
                || !bundle.containsKey(KEY_POSITION)
                || !bundle.containsKey(KEY_UPDATE_TIME)
                || !bundle.containsKey(KEY_SPEED)
                || !bundle.containsKey(KEY_BUFFERED_POSITION)
                || !bundle.containsKey(KEY_ACTIVE_ITEM_ID)
                || !bundle.containsKey(KEY_ERROR_MESSAGE)) {
            return null;
        }

        return new PlaybackState2(
                bundle.getInt(KEY_STATE),
                bundle.getLong(KEY_POSITION),
                bundle.getLong(KEY_UPDATE_TIME),
                bundle.getFloat(KEY_SPEED),
                bundle.getLong(KEY_BUFFERED_POSITION),
                bundle.getLong(KEY_ACTIVE_ITEM_ID),
                bundle.getCharSequence(KEY_ERROR_MESSAGE));
    }
}
 No newline at end of file