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

Commit 435142ba authored by Jaewan Kim's avatar Jaewan Kim
Browse files

MediaSession2: Move MediaSession2.CommandButton to updatable

Bug: 72665718
Test: Run all MediaComponents test once
Change-Id: I5e6b382695b9d8c2f9634531ded32fdac438965c
parent a7cf21b4
Loading
Loading
Loading
Loading
+28 −86
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ 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.CommandButtonProvider;
import android.media.update.MediaSession2Provider.CommandGroupProvider;
import android.media.update.MediaSession2Provider.CommandProvider;
import android.media.update.MediaSession2Provider.ControllerInfoProvider;
@@ -768,32 +769,15 @@ public class MediaSession2 implements AutoCloseable {
     * <p>
     * It's up to the controller's decision to respect or ignore this customization request.
     */
    // TODO(jaewan): Move this to updatable.
    public static class CommandButton {
        private static final String KEY_COMMAND
                = "android.media.media_session2.command_button.command";
        private static final String KEY_ICON_RES_ID
                = "android.media.media_session2.command_button.icon_res_id";
        private static final String KEY_DISPLAY_NAME
                = "android.media.media_session2.command_button.display_name";
        private static final String KEY_EXTRA
                = "android.media.media_session2.command_button.extra";
        private static final String KEY_ENABLED
                = "android.media.media_session2.command_button.enabled";

        private Command mCommand;
        private int mIconResId;
        private String mDisplayName;
        private Bundle mExtra;
        private boolean mEnabled;

        private CommandButton(@Nullable Command command, int iconResId,
                @Nullable String displayName, Bundle extra, boolean enabled) {
            mCommand = command;
            mIconResId = iconResId;
            mDisplayName = displayName;
            mExtra = extra;
            mEnabled = enabled;
        private final CommandButtonProvider mProvider;

        /**
         * @hide
         */
        @SystemApi
        public CommandButton(CommandButtonProvider provider) {
            mProvider = provider;
        }

        /**
@@ -803,7 +787,7 @@ public class MediaSession2 implements AutoCloseable {
         * @return command or {@code null}
         */
        public @Nullable Command getCommand() {
            return mCommand;
            return mProvider.getCommand_impl();
        }

        /**
@@ -813,7 +797,7 @@ public class MediaSession2 implements AutoCloseable {
         * @return resource id of the icon. Can be {@code 0}.
         */
        public int getIconResId() {
            return mIconResId;
            return mProvider.getIconResId_impl();
        }

        /**
@@ -823,7 +807,7 @@ public class MediaSession2 implements AutoCloseable {
         * @return custom display name. Can be {@code null} or empty.
         */
        public @Nullable String getDisplayName() {
            return mDisplayName;
            return mProvider.getDisplayName_impl();
        }

        /**
@@ -832,7 +816,7 @@ public class MediaSession2 implements AutoCloseable {
         * @return
         */
        public @Nullable Bundle getExtra() {
            return mExtra;
            return mProvider.getExtra_impl();
        }

        /**
@@ -841,92 +825,50 @@ public class MediaSession2 implements AutoCloseable {
         * @return {@code true} if enabled. {@code false} otherwise.
         */
        public boolean isEnabled() {
            return mEnabled;
            return mProvider.isEnabled_impl();
        }

        /**
         * @hide
         */
        // TODO(jaewan): @SystemApi
        public @NonNull Bundle toBundle() {
            Bundle bundle = new Bundle();
            bundle.putBundle(KEY_COMMAND, mCommand.toBundle());
            bundle.putInt(KEY_ICON_RES_ID, mIconResId);
            bundle.putString(KEY_DISPLAY_NAME, mDisplayName);
            bundle.putBundle(KEY_EXTRA, mExtra);
            bundle.putBoolean(KEY_ENABLED, mEnabled);
            return bundle;
        }

        /**
         * @hide
         */
        // TODO(jaewan): @SystemApi
        public static @Nullable CommandButton fromBundle(Context context, Bundle bundle) {
            Builder builder = new Builder();
            builder.setCommand(Command.fromBundle(context, bundle.getBundle(KEY_COMMAND)));
            builder.setIconResId(bundle.getInt(KEY_ICON_RES_ID, 0));
            builder.setDisplayName(bundle.getString(KEY_DISPLAY_NAME));
            builder.setExtra(bundle.getBundle(KEY_EXTRA));
            builder.setEnabled(bundle.getBoolean(KEY_ENABLED));
            try {
                return builder.build();
            } catch (IllegalStateException e) {
                // Malformed or version mismatch. Return null for now.
                return null;
            }
        @SystemApi
        public CommandButtonProvider getProvider() {
            return mProvider;
        }

        /**
         * Builder for {@link CommandButton}.
         */
        public static class Builder {
            private Command mCommand;
            private int mIconResId;
            private String mDisplayName;
            private Bundle mExtra;
            private boolean mEnabled;
            private final CommandButtonProvider.BuilderProvider mProvider;

            public Builder() {
                mEnabled = true;
            public Builder(@NonNull Context context) {
                mProvider = ApiLoader.getProvider(context)
                        .createMediaSession2CommandButtonBuilder(context, this);
            }

            public Builder setCommand(Command command) {
                mCommand = command;
                return this;
                return mProvider.setCommand_impl(command);
            }

            public Builder setIconResId(int resId) {
                mIconResId = resId;
                return this;
                return mProvider.setIconResId_impl(resId);
            }

            public Builder setDisplayName(String displayName) {
                mDisplayName = displayName;
                return this;
                return mProvider.setDisplayName_impl(displayName);
            }

            public Builder setEnabled(boolean enabled) {
                mEnabled = enabled;
                return this;
                return mProvider.setEnabled_impl(enabled);
            }

            public Builder setExtra(Bundle extra) {
                mExtra = extra;
                return this;
                return mProvider.setExtra_impl(extra);
            }

            public CommandButton build() {
                if (mEnabled && mCommand == null) {
                    throw new IllegalStateException("Enabled button needs Command"
                            + " for controller to invoke the command");
                }
                if (mCommand != null && mCommand.getCommandCode() == COMMAND_CODE_CUSTOM
                        && (mIconResId == 0 || TextUtils.isEmpty(mDisplayName))) {
                    throw new IllegalStateException("Custom commands needs icon and"
                            + " and name to display");
                }
                return new CommandButton(mCommand, mIconResId, mDisplayName, mExtra, mEnabled);
                return mProvider.build_impl();
            }
        }
    }
+18 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.media.MediaPlayerInterface.PlaybackListener;
import android.media.MediaSession2;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandButton.Builder;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.PlaylistParams;
@@ -82,6 +83,23 @@ public interface MediaSession2Provider extends TransportControlProvider {
        Bundle toBundle_impl();
    }

    interface CommandButtonProvider {
        Command getCommand_impl();
        int getIconResId_impl();
        String getDisplayName_impl();
        Bundle getExtra_impl();
        boolean isEnabled_impl();

        interface BuilderProvider {
            Builder setCommand_impl(Command command);
            Builder setIconResId_impl(int resId);
            Builder setDisplayName_impl(String displayName);
            Builder setEnabled_impl(boolean enabled);
            Builder setExtra_impl(Bundle extra);
            CommandButton build_impl();
        }
    }

    interface ControllerInfoProvider {
        String getPackageName_impl();
        int getUid_impl();
+3 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.media.MediaLibraryService2.MediaLibrarySessionCallback;
import android.media.MediaMetadata2;
import android.media.MediaPlayerInterface;
import android.media.MediaSession2;
import android.media.MediaSession2.CommandButton.Builder;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.SessionCallback;
import android.media.MediaSessionService2;
@@ -42,6 +43,7 @@ import android.media.SessionPlayer2;
import android.media.SessionToken2;
import android.media.VolumeProvider2;
import android.media.update.MediaSession2Provider.BuilderBaseProvider;
import android.media.update.MediaSession2Provider.CommandButtonProvider.BuilderProvider;
import android.media.update.MediaSession2Provider.CommandGroupProvider;
import android.media.update.MediaSession2Provider.CommandProvider;
import android.media.update.MediaSession2Provider.ControllerInfoProvider;
@@ -82,6 +84,7 @@ public interface StaticProvider {
            PlaylistParams playlistParams, int repeatMode, int shuffleMode,
            MediaMetadata2 playlistMetadata);
    PlaylistParams fromBundle_PlaylistParams(Context context, Bundle bundle);
    BuilderProvider createMediaSession2CommandButtonBuilder(Context context, Builder builder);
    BuilderBaseProvider<MediaSession2, SessionCallback> createMediaSession2Builder(
            Context context, MediaSession2.Builder instance, MediaPlayerInterface player);