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

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

Merge "MediaSession2: Add MediaSession2.CommandButton"

parents 4e9e8620 f5114f30
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -20,12 +20,14 @@ import android.content.Context;
import android.media.IMediaSession2;
import android.media.MediaBrowser2;
import android.media.MediaBrowser2.BrowserCallback;
import android.media.MediaSession2.CommandButton;
import android.media.SessionToken;
import android.media.update.MediaBrowser2Provider;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;

import java.util.List;
import java.util.concurrent.Executor;

public class MediaBrowser2Impl extends MediaController2Impl implements MediaBrowser2Provider {
@@ -65,4 +67,10 @@ public class MediaBrowser2Impl extends MediaController2Impl implements MediaBrow
            mCallback.onGetRootResult(rootHints, rootMediaId, rootExtra);
        });
    }

    public void onCustomLayoutChanged(final List<CommandButton> layout) {
        getCallbackExecutor().execute(() -> {
            mCallback.onCustomLayoutChanged(layout);
        });
    }
}
+32 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.media.IMediaSession2;
import android.media.IMediaSession2Callback;
import android.media.MediaSession2;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaController2;
import android.media.MediaController2.ControllerCallback;
@@ -461,8 +462,39 @@ public class MediaController2Impl implements MediaController2Provider {
                Log.w(TAG, "Don't fail silently here. Highly likely a bug");
                return;
            }
            if (browser == null) {
                // TODO(jaewan): Revisit here. Could be a bug
                return;
            }
            browser.onGetRootResult(rootHints, rootMediaId, rootExtra);
        }

        @Override
        public void onCustomLayoutChanged(List<Bundle> commandButtonlist) {
            if (commandButtonlist == null) {
                // Illegal call. Ignore
                return;
            }
            final MediaBrowser2Impl browser;
            try {
                browser = getBrowser();
            } catch (IllegalStateException e) {
                Log.w(TAG, "Don't fail silently here. Highly likely a bug");
                return;
            }
            if (browser == null) {
                // TODO(jaewan): Revisit here. Could be a bug
                return;
            }
            List<CommandButton> layout = new ArrayList<>();
            for (int i = 0; i < commandButtonlist.size(); i++) {
                CommandButton button = CommandButton.fromBundle(commandButtonlist.get(i));
                if (button != null) {
                    layout.add(button);
                }
            }
            browser.onCustomLayoutChanged(layout);
        }
    }

    // This will be called on the main thread.
+13 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.media.MediaController2;
import android.media.MediaPlayerBase;
import android.media.MediaSession2;
import android.media.MediaSession2.Builder;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.SessionCallback;
import android.media.SessionToken;
@@ -230,6 +231,18 @@ public class MediaSession2Impl implements MediaSession2Provider {
        }
    }

    @Override
    public void setCustomLayout_impl(ControllerInfo controller, List<CommandButton> layout) {
        ensureCallingThread();
        if (controller == null) {
            throw new IllegalArgumentException("controller shouldn't be null");
        }
        if (layout == null) {
            throw new IllegalArgumentException("layout shouldn't be null");
        }
        mSessionStub.notifyCustomLayoutNotLocked(controller, layout);
    }

    ///////////////////////////////////////////////////
    // Protected or private methods
    ///////////////////////////////////////////////////
+21 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.media.IMediaSession2Callback;
import android.media.MediaLibraryService2.MediaLibrarySessionCallback;
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.SessionCallback;
@@ -229,6 +230,26 @@ public class MediaSession2Stub extends IMediaSession2.Stub {
        }
    }

    public void notifyCustomLayoutNotLocked(ControllerInfo controller, List<CommandButton> layout) {
        // TODO(jaewan): It's OK to be called while it's connecting, but not OK if the connection
        //               is rejected. Handle the case.
        IMediaSession2Callback callbackBinder =
                ControllerInfoImpl.from(controller).getControllerBinder();
        try {
            List<Bundle> layoutBundles = new ArrayList<>();
            for (int i = 0; i < layout.size(); i++) {
                Bundle bundle = layout.get(i).toBundle();
                if (bundle != null) {
                    layoutBundles.add(bundle);
                }
            }
            callbackBinder.onCustomLayoutChanged(layoutBundles);
        } catch (RemoteException e) {
            Log.w(TAG, "Controller is gone", e);
            // TODO(jaewan): What to do when the controller is gone?
        }
    }

    // TODO(jaewan): Remove this. We should use Executor given by the session builder.
    private class CommandHandler extends Handler {
        public static final int MSG_CONNECT = 1000;