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

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

Merge "Remove ControllerCallbackLink"

parents 95f616da 4e4459b4
Loading
Loading
Loading
Loading
+0 −18
Original line number Diff line number Diff line
/*
 * Copyright 2019 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.session;

parcelable ControllerCallbackLink;
+0 −327
Original line number Diff line number Diff line
/*
 * Copyright 2019 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.session;

import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.MediaMetadata;
import android.media.MediaParceledListSlice;
import android.media.session.MediaController.PlaybackInfo;
import android.media.session.MediaSession.QueueItem;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Process;
import android.os.RemoteException;

import java.util.List;

/**
 * Handles incoming commands to {@link MediaController.Callback}.
 * @hide
 */
public final class ControllerCallbackLink implements Parcelable {
    final Context mContext;
    final CallbackStub mCallbackStub;
    final ISessionControllerCallback mIControllerCallback;

    /**
     * Constructor for stub (Callee)
     */
    public ControllerCallbackLink(@NonNull Context context, @NonNull CallbackStub callbackStub) {
        mContext = context;
        mCallbackStub = callbackStub;
        mIControllerCallback = new CallbackStubProxy();
    }

    /**
     * Constructor for interface (Caller)
     */
    public ControllerCallbackLink(IBinder binder) {
        mContext = null;
        mCallbackStub = null;
        mIControllerCallback = ISessionControllerCallback.Stub.asInterface(binder);
    }

    /**
     * Notify controller that the connected session is destroyed.
     */
    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
    public void notifySessionDestroyed() {
        try {
            mIControllerCallback.notifySessionDestroyed();
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Notify controller that the connected session sends an event.
     *
     * @param event the name of the event
     * @param extras the extras included with the event
     */
    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
    public void notifyEvent(@NonNull String event, @Nullable Bundle extras) {
        try {
            mIControllerCallback.notifyEvent(event, extras);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Notify controller that the current playback state is changed.
     *
     * @param state the new playback state
     */
    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
    public void notifyPlaybackStateChanged(@Nullable PlaybackState state) {
        try {
            mIControllerCallback.notifyPlaybackStateChanged(state);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Notify controller that the current metadata is changed.
     *
     * @param metadata the new metadata
     */
    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
    public void notifyMetadataChanged(@Nullable MediaMetadata metadata) {
        try {
            mIControllerCallback.notifyMetadataChanged(metadata);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Notify controller that the current queue is changed.
     *
     * @param queue the new queue
     */
    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
    public void notifyQueueChanged(@Nullable List<QueueItem> queue) {
        try {
            mIControllerCallback.notifyQueueChanged(queue == null ? null :
                    new MediaParceledListSlice(queue));
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Notify controller that the current queue title is changed.
     *
     * @param title the new queue title
     */
    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
    public void notifyQueueTitleChanged(@Nullable CharSequence title) {
        try {
            mIControllerCallback.notifyQueueTitleChanged(title);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Notify controller that the extras are changed.
     *
     * @param extras the new extras
     */
    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
    public void notifyExtrasChanged(@Nullable Bundle extras) {
        try {
            mIControllerCallback.notifyExtrasChanged(extras);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Notify controller that the playback info is changed.
     *
     * @param info the new playback info
     */
    @RequiresPermission(Manifest.permission.MEDIA_CONTENT_CONTROL)
    public void notifyVolumeInfoChanged(@NonNull PlaybackInfo info) {
        try {
            mIControllerCallback.notifyVolumeInfoChanged(info);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    /** Gets the binder */
    @NonNull
    public IBinder getBinder() {
        return mIControllerCallback.asBinder();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStrongBinder(mIControllerCallback.asBinder());
    }

    public static final @android.annotation.NonNull Parcelable.Creator<ControllerCallbackLink> CREATOR =
            new Parcelable.Creator<ControllerCallbackLink>() {
        @Override
        public ControllerCallbackLink createFromParcel(Parcel in) {
            return new ControllerCallbackLink(in.readStrongBinder());
        }

        @Override
        public ControllerCallbackLink[] newArray(int size) {
            return new ControllerCallbackLink[size];
        }
    };

    /**
     * Class for Stub implementation
     */
    public abstract static class CallbackStub {
        /** Stub method for ISessionControllerCallback.notifySessionDestroyed */
        public void onSessionDestroyed() {
        }

        /** Stub method for ISessionControllerCallback.notifyEvent */
        public void onEvent(@NonNull String event, @Nullable Bundle extras) {
        }

        /** Stub method for ISessionControllerCallback.notifyPlaybackStateChanged */
        public void onPlaybackStateChanged(@Nullable PlaybackState state) {
        }

        /** Stub method for ISessionControllerCallback.notifyMetadataChanged */
        public void onMetadataChanged(@Nullable MediaMetadata metadata) {
        }

        /** Stub method for ISessionControllerCallback.notifyQueueChanged */
        public void onQueueChanged(@Nullable List<QueueItem> queue) {
        }

        /** Stub method for ISessionControllerCallback.notifyQueueTitleChanged */
        public void onQueueTitleChanged(@Nullable CharSequence title) {
        }

        /** Stub method for ISessionControllerCallback.notifyExtrasChanged */
        public void onExtrasChanged(@Nullable Bundle extras) {
        }

        /** Stub method for ISessionControllerCallback.notifyVolumeInfoChanged */
        public void onVolumeInfoChanged(@NonNull PlaybackInfo info) {
        }
    }

    private class CallbackStubProxy extends ISessionControllerCallback.Stub {
        @Override
        public void notifyEvent(String event, Bundle extras) {
            mCallbackStub.onEvent(event, extras);
        }

        @Override
        public void notifySessionDestroyed() {
            mCallbackStub.onSessionDestroyed();
        }

        @Override
        public void notifyPlaybackStateChanged(PlaybackState state) {
            ensureMediaControlPermission();
            final long token = Binder.clearCallingIdentity();
            try {
                mCallbackStub.onPlaybackStateChanged(state);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        @Override
        public void notifyMetadataChanged(MediaMetadata metadata) {
            ensureMediaControlPermission();
            final long token = Binder.clearCallingIdentity();
            try {
                mCallbackStub.onMetadataChanged(metadata);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        @Override
        public void notifyQueueChanged(MediaParceledListSlice queue) {
            ensureMediaControlPermission();
            final long token = Binder.clearCallingIdentity();
            try {
                mCallbackStub.onQueueChanged(queue == null ? null : queue.getList());
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        @Override
        public void notifyQueueTitleChanged(CharSequence title) {
            ensureMediaControlPermission();
            final long token = Binder.clearCallingIdentity();
            try {
                mCallbackStub.onQueueTitleChanged(title);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        @Override
        public void notifyExtrasChanged(Bundle extras) {
            mCallbackStub.onExtrasChanged(extras);
        }

        @Override
        public void notifyVolumeInfoChanged(PlaybackInfo info) {
            ensureMediaControlPermission();
            final long token = Binder.clearCallingIdentity();
            try {
                mCallbackStub.onVolumeInfoChanged(info);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        private void ensureMediaControlPermission() {
            // Check if it's system server or has MEDIA_CONTENT_CONTROL.
            // Note that system server doesn't have MEDIA_CONTENT_CONTROL, so we need extra
            // check here.
            if (getCallingUid() == Process.SYSTEM_UID || mContext.checkCallingPermission(
                    android.Manifest.permission.MEDIA_CONTENT_CONTROL)
                    == PackageManager.PERMISSION_GRANTED) {
                return;
            }
            throw new SecurityException("Must hold the MEDIA_CONTENT_CONTROL permission.");
        }
    }
}
+24 −24
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ package android.media.session;

import android.content.Intent;
import android.media.Rating;
import android.media.session.ControllerCallbackLink;
import android.media.session.ISessionControllerCallback;
import android.net.Uri;
import android.os.Bundle;
import android.os.ResultReceiver;
@@ -26,48 +26,48 @@ import android.os.ResultReceiver;
 * @hide
 */
oneway interface ISessionCallback {
    void onCommand(String packageName, int pid, int uid, in ControllerCallbackLink caller,
    void onCommand(String packageName, int pid, int uid, ISessionControllerCallback caller,
            String command, in Bundle args, in ResultReceiver cb);
    void onMediaButton(String packageName, int pid, int uid, in Intent mediaButtonIntent,
            int sequenceNumber, in ResultReceiver cb);
    void onMediaButtonFromController(String packageName, int pid, int uid,
            in ControllerCallbackLink caller, in Intent mediaButtonIntent);
            ISessionControllerCallback caller, in Intent mediaButtonIntent);

    // These callbacks are for the TransportControls
    void onPrepare(String packageName, int pid, int uid, in ControllerCallbackLink caller);
    void onPrepare(String packageName, int pid, int uid, ISessionControllerCallback caller);
    void onPrepareFromMediaId(String packageName, int pid, int uid,
            in ControllerCallbackLink caller, String mediaId, in Bundle extras);
            ISessionControllerCallback caller, String mediaId, in Bundle extras);
    void onPrepareFromSearch(String packageName, int pid, int uid,
            in ControllerCallbackLink caller, String query, in Bundle extras);
            ISessionControllerCallback caller, String query, in Bundle extras);
    void onPrepareFromUri(String packageName, int pid, int uid,
            in ControllerCallbackLink caller, in Uri uri, in Bundle extras);
    void onPlay(String packageName, int pid, int uid, in ControllerCallbackLink caller);
            ISessionControllerCallback caller, in Uri uri, in Bundle extras);
    void onPlay(String packageName, int pid, int uid, ISessionControllerCallback caller);
    void onPlayFromMediaId(String packageName, int pid, int uid,
            in ControllerCallbackLink caller, String mediaId, in Bundle extras);
            ISessionControllerCallback caller, String mediaId, in Bundle extras);
    void onPlayFromSearch(String packageName, int pid, int uid,
            in ControllerCallbackLink caller, String query, in Bundle extras);
    void onPlayFromUri(String packageName, int pid, int uid, in ControllerCallbackLink caller,
            ISessionControllerCallback caller, String query, in Bundle extras);
    void onPlayFromUri(String packageName, int pid, int uid, ISessionControllerCallback caller,
            in Uri uri, in Bundle extras);
    void onSkipToTrack(String packageName, int pid, int uid, in ControllerCallbackLink caller,
    void onSkipToTrack(String packageName, int pid, int uid, ISessionControllerCallback caller,
            long id);
    void onPause(String packageName, int pid, int uid, in ControllerCallbackLink caller);
    void onStop(String packageName, int pid, int uid, in ControllerCallbackLink caller);
    void onNext(String packageName, int pid, int uid, in ControllerCallbackLink caller);
    void onPrevious(String packageName, int pid, int uid, in ControllerCallbackLink caller);
    void onFastForward(String packageName, int pid, int uid, in ControllerCallbackLink caller);
    void onRewind(String packageName, int pid, int uid, in ControllerCallbackLink caller);
    void onSeekTo(String packageName, int pid, int uid, in ControllerCallbackLink caller,
    void onPause(String packageName, int pid, int uid, ISessionControllerCallback caller);
    void onStop(String packageName, int pid, int uid, ISessionControllerCallback caller);
    void onNext(String packageName, int pid, int uid, ISessionControllerCallback caller);
    void onPrevious(String packageName, int pid, int uid, ISessionControllerCallback caller);
    void onFastForward(String packageName, int pid, int uid, ISessionControllerCallback caller);
    void onRewind(String packageName, int pid, int uid, ISessionControllerCallback caller);
    void onSeekTo(String packageName, int pid, int uid, ISessionControllerCallback caller,
            long pos);
    void onRate(String packageName, int pid, int uid, in ControllerCallbackLink caller,
    void onRate(String packageName, int pid, int uid, ISessionControllerCallback caller,
            in Rating rating);
    void onSetPlaybackSpeed(String packageName, int pid, int uid,
            in ControllerCallbackLink caller, float speed);
    void onCustomAction(String packageName, int pid, int uid, in ControllerCallbackLink caller,
            ISessionControllerCallback caller, float speed);
    void onCustomAction(String packageName, int pid, int uid, ISessionControllerCallback caller,
            String action, in Bundle args);

    // These callbacks are for volume handling
    void onAdjustVolume(String packageName, int pid, int uid, in ControllerCallbackLink caller,
    void onAdjustVolume(String packageName, int pid, int uid, ISessionControllerCallback caller,
            int direction);
    void onSetVolumeTo(String packageName, int pid, int uid,
            in ControllerCallbackLink caller, int value);
            ISessionControllerCallback caller, int value);
}
+26 −26
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ import android.content.Intent;
import android.media.MediaMetadata;
import android.media.MediaParceledListSlice;
import android.media.Rating;
import android.media.session.ControllerCallbackLink;
import android.media.session.ISessionControllerCallback;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
@@ -36,12 +36,12 @@ import java.util.List;
 * @hide
 */
interface ISessionController {
    void sendCommand(String packageName, in ControllerCallbackLink caller,
    void sendCommand(String packageName, in ISessionControllerCallback caller,
            String command, in Bundle args, in ResultReceiver cb);
    boolean sendMediaButton(String packageName, in ControllerCallbackLink caller,
    boolean sendMediaButton(String packageName, in ISessionControllerCallback caller,
            in KeyEvent mediaButton);
    void registerCallback(String packageName, in ControllerCallbackLink cb);
    void unregisterCallback(in ControllerCallbackLink cb);
    void registerCallback(String packageName, in ISessionControllerCallback cb);
    void unregisterCallback(in ISessionControllerCallback cb);
    String getPackageName();
    String getTag();
    Bundle getSessionInfo();
@@ -49,36 +49,36 @@ interface ISessionController {
    long getFlags();
    MediaController.PlaybackInfo getVolumeAttributes();
    void adjustVolume(String packageName, String opPackageName,
            in ControllerCallbackLink caller, int direction, int flags);
    void setVolumeTo(String packageName, String opPackageName, in ControllerCallbackLink caller,
            in ISessionControllerCallback caller, int direction, int flags);
    void setVolumeTo(String packageName, String opPackageName, in ISessionControllerCallback caller,
            int value, int flags);

    // These commands are for the TransportControls
    void prepare(String packageName, in ControllerCallbackLink caller);
    void prepareFromMediaId(String packageName, in ControllerCallbackLink caller,
    void prepare(String packageName, in ISessionControllerCallback caller);
    void prepareFromMediaId(String packageName, in ISessionControllerCallback caller,
            String mediaId, in Bundle extras);
    void prepareFromSearch(String packageName, in ControllerCallbackLink caller,
    void prepareFromSearch(String packageName, in ISessionControllerCallback caller,
            String string, in Bundle extras);
    void prepareFromUri(String packageName, in ControllerCallbackLink caller,
    void prepareFromUri(String packageName, in ISessionControllerCallback caller,
            in Uri uri, in Bundle extras);
    void play(String packageName, in ControllerCallbackLink caller);
    void playFromMediaId(String packageName, in ControllerCallbackLink caller,
    void play(String packageName, in ISessionControllerCallback caller);
    void playFromMediaId(String packageName, in ISessionControllerCallback caller,
            String mediaId, in Bundle extras);
    void playFromSearch(String packageName, in ControllerCallbackLink caller,
    void playFromSearch(String packageName, in ISessionControllerCallback caller,
            String string, in Bundle extras);
    void playFromUri(String packageName, in ControllerCallbackLink caller,
    void playFromUri(String packageName, in ISessionControllerCallback caller,
            in Uri uri, in Bundle extras);
    void skipToQueueItem(String packageName, in ControllerCallbackLink caller, long id);
    void pause(String packageName, in ControllerCallbackLink caller);
    void stop(String packageName, in ControllerCallbackLink caller);
    void next(String packageName, in ControllerCallbackLink caller);
    void previous(String packageName, in ControllerCallbackLink caller);
    void fastForward(String packageName, in ControllerCallbackLink caller);
    void rewind(String packageName, in ControllerCallbackLink caller);
    void seekTo(String packageName, in ControllerCallbackLink caller, long pos);
    void rate(String packageName, in ControllerCallbackLink caller, in Rating rating);
    void setPlaybackSpeed(String packageName, in ControllerCallbackLink caller, float speed);
    void sendCustomAction(String packageName, in ControllerCallbackLink caller,
    void skipToQueueItem(String packageName, in ISessionControllerCallback caller, long id);
    void pause(String packageName, in ISessionControllerCallback caller);
    void stop(String packageName, in ISessionControllerCallback caller);
    void next(String packageName, in ISessionControllerCallback caller);
    void previous(String packageName, in ISessionControllerCallback caller);
    void fastForward(String packageName, in ISessionControllerCallback caller);
    void rewind(String packageName, in ISessionControllerCallback caller);
    void seekTo(String packageName, in ISessionControllerCallback caller, long pos);
    void rate(String packageName, in ISessionControllerCallback caller, in Rating rating);
    void setPlaybackSpeed(String packageName, in ISessionControllerCallback caller, float speed);
    void sendCustomAction(String packageName, in ISessionControllerCallback caller,
            String action, in Bundle args);
    MediaMetadata getMetadata();
    PlaybackState getPlaybackState();
+8 −8
Original line number Diff line number Diff line
@@ -25,14 +25,14 @@ import android.os.Bundle;
 * @hide
 */
oneway interface ISessionControllerCallback {
    void notifyEvent(String event, in Bundle extras);
    void notifySessionDestroyed();
    void onEvent(String event, in Bundle extras);
    void onSessionDestroyed();

    // These callbacks are for the TransportController
    void notifyPlaybackStateChanged(in PlaybackState state);
    void notifyMetadataChanged(in MediaMetadata metadata);
    void notifyQueueChanged(in MediaParceledListSlice queue);
    void notifyQueueTitleChanged(CharSequence title);
    void notifyExtrasChanged(in Bundle extras);
    void notifyVolumeInfoChanged(in MediaController.PlaybackInfo info);
    void onPlaybackStateChanged(in PlaybackState state);
    void onMetadataChanged(in MediaMetadata metadata);
    void onQueueChanged(in MediaParceledListSlice queue);
    void onQueueTitleChanged(CharSequence title);
    void onExtrasChanged(in Bundle extras);
    void onVolumeInfoChanged(in MediaController.PlaybackInfo info);
}
Loading