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

Commit 5287e24b authored by Jin Seok Park's avatar Jin Seok Park
Browse files

[Media ML] Refactor APIs for dispatching key events

There are currently two APIs with the same name:
dispatchMediaKeyEventAsSystemService(KeyEvent), and
dispatchMediaKeyEventAsSystemService(KeyEvent, MediaSession.Token),
but their internal implementations and return types do not overlap.

Instead rename the second API to
dispatchMediaKeyEventToSessionAsSystemService(...) to avoid
associating the two APIs.

The same logic is applied to dispatchVolumeKeyEventAsSystemService.

Bug: 166262380
Test: build
Change-Id: I20f7c6d4dd97eff6625307cf9bbc14ff824c3286
parent e2b1a7f3
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -76,9 +76,9 @@ package android.media.session {
  public final class MediaSessionManager {
    method public void addOnActiveSessionsChangedListener(@NonNull android.media.session.MediaSessionManager.OnActiveSessionsChangedListener, @Nullable android.content.ComponentName, int, @Nullable android.os.Handler);
    method public void dispatchMediaKeyEventAsSystemService(@NonNull android.view.KeyEvent);
    method public boolean dispatchMediaKeyEventAsSystemService(@NonNull android.media.session.MediaSession.Token, @NonNull android.view.KeyEvent);
    method public boolean dispatchMediaKeyEventToSessionAsSystemService(@NonNull android.view.KeyEvent, @NonNull android.media.session.MediaSession.Token);
    method public void dispatchVolumeKeyEventAsSystemService(@NonNull android.view.KeyEvent, int);
    method public void dispatchVolumeKeyEventAsSystemService(@NonNull android.media.session.MediaSession.Token, @NonNull android.view.KeyEvent);
    method public void dispatchVolumeKeyEventToSessionAsSystemService(@NonNull android.view.KeyEvent, @NonNull android.media.session.MediaSession.Token);
    field public static final int RESULT_MEDIA_KEY_HANDLED = 1; // 0x1
    field public static final int RESULT_MEDIA_KEY_NOT_HANDLED = 0; // 0x0
  }
+8 −8
Original line number Diff line number Diff line
@@ -1916,8 +1916,8 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                // If we have a session send it the volume command, otherwise
                // use the suggested stream.
                if (mMediaController != null) {
                    getMediaSessionManager().dispatchVolumeKeyEventAsSystemService(
                            mMediaController.getSessionToken(), event);
                    getMediaSessionManager().dispatchVolumeKeyEventToSessionAsSystemService(event,
                            mMediaController.getSessionToken());
                } else {
                    getMediaSessionManager().dispatchVolumeKeyEventAsSystemService(event,
                            mVolumeControlStreamType);
@@ -1938,8 +1938,8 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
            case KeyEvent.KEYCODE_MEDIA_RECORD:
            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
                if (mMediaController != null) {
                    if (getMediaSessionManager().dispatchMediaKeyEventAsSystemService(
                            mMediaController.getSessionToken(), event)) {
                    if (getMediaSessionManager().dispatchMediaKeyEventToSessionAsSystemService(
                            event, mMediaController.getSessionToken())) {
                        return true;
                    }
                }
@@ -2010,8 +2010,8 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                // If we have a session send it the volume command, otherwise
                // use the suggested stream.
                if (mMediaController != null) {
                    getMediaSessionManager().dispatchVolumeKeyEventAsSystemService(
                            mMediaController.getSessionToken(), event);
                    getMediaSessionManager().dispatchVolumeKeyEventToSessionAsSystemService(event,
                            mMediaController.getSessionToken());
                } else {
                    getMediaSessionManager().dispatchVolumeKeyEventAsSystemService(
                            event, mVolumeControlStreamType);
@@ -2041,8 +2041,8 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
            case KeyEvent.KEYCODE_MEDIA_RECORD:
            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
                if (mMediaController != null) {
                    if (getMediaSessionManager().dispatchMediaKeyEventAsSystemService(
                            mMediaController.getSessionToken(), event)) {
                    if (getMediaSessionManager().dispatchMediaKeyEventToSessionAsSystemService(
                            event, mMediaController.getSessionToken())) {
                        return true;
                    }
                }
+2 −2
Original line number Diff line number Diff line
@@ -44,11 +44,11 @@ interface ISessionManager {
    void dispatchMediaKeyEvent(String packageName, boolean asSystemService, in KeyEvent keyEvent,
            boolean needWakeLock);
    boolean dispatchMediaKeyEventToSessionAsSystemService(String packageName,
            in MediaSession.Token sessionToken, in KeyEvent keyEvent);
            in KeyEvent keyEvent, in MediaSession.Token sessionToken);
    void dispatchVolumeKeyEvent(String packageName, String opPackageName, boolean asSystemService,
            in KeyEvent keyEvent, int stream, boolean musicOnly);
    void dispatchVolumeKeyEventToSessionAsSystemService(String packageName, String opPackageName,
            in MediaSession.Token sessionToken, in KeyEvent keyEvent);
            in KeyEvent keyEvent, in MediaSession.Token sessionToken);
    void dispatchAdjustVolume(String packageName, String opPackageName, int suggestedStream,
            int delta, int flags);
    void addSessionsListener(in IActiveSessionsListener listener, in ComponentName compName,
+45 −35
Original line number Diff line number Diff line
@@ -493,43 +493,46 @@ public final class MediaSessionManager {
    }

    /**
     * Send a media key event. The receiver will be selected automatically.
     * Sends a media key event. The receiver will be selected automatically.
     *
     * @param keyEvent The KeyEvent to send.
     * @param keyEvent the key event to send
     * @hide
     */
    public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent) {
        dispatchMediaKeyEvent(keyEvent, false);
        dispatchMediaKeyEventInternal(keyEvent, /*asSystemService=*/false, /*needWakeLock=*/false);
    }

    /**
     * Send a media key event. The receiver will be selected automatically.
     * Sends a media key event. The receiver will be selected automatically.
     *
     * @param keyEvent The KeyEvent to send.
     * @param needWakeLock True if a wake lock should be held while sending the key.
     * @param keyEvent the key event to send
     * @param needWakeLock true if a wake lock should be held while sending the key
     * @hide
     */
    public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent, boolean needWakeLock) {
        dispatchMediaKeyEventInternal(false, keyEvent, needWakeLock);
        dispatchMediaKeyEventInternal(keyEvent, /*asSystemService=*/false, needWakeLock);
    }

    /**
     * Send a media key event as system component. The receiver will be selected automatically.
     * Sends a media key event as system service. The receiver will be selected automatically.
     * <p>
     * Should be only called by the {@link com.android.internal.policy.PhoneWindow} or
     * {@link android.view.FallbackEventHandler} when the foreground activity didn't consume the key
     * from the hardware devices.
     *
     * @param keyEvent The KeyEvent to send.
     * @param keyEvent the key event to send
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public void dispatchMediaKeyEventAsSystemService(@NonNull KeyEvent keyEvent) {
        dispatchMediaKeyEventInternal(true, keyEvent, false);
        dispatchMediaKeyEventInternal(keyEvent, /*asSystemService=*/true, /*needWakeLock=*/false);
    }

    private void dispatchMediaKeyEventInternal(boolean asSystemService, @NonNull KeyEvent keyEvent,
    private void dispatchMediaKeyEventInternal(KeyEvent keyEvent, boolean asSystemService,
            boolean needWakeLock) {
        if (keyEvent == null) {
            throw new NullPointerException("keyEvent shouldn't be null");
        }
        try {
            mService.dispatchMediaKeyEvent(mContext.getPackageName(), asSystemService, keyEvent,
                    needWakeLock);
@@ -539,31 +542,31 @@ public final class MediaSessionManager {
    }

    /**
     * Dispatches the media button event as system service to the session.
     * Sends a media key event as system service to the given session.
     * <p>
     * Should be only called by the {@link com.android.internal.policy.PhoneWindow} when the
     * foreground activity didn't consume the key from the hardware devices.
     *
     * @param sessionToken session token
     * @param keyEvent media key event
     * @param keyEvent the key event to send
     * @param sessionToken the session token to which the key event should be dispatched
     * @return {@code true} if the event was sent to the session, {@code false} otherwise
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public boolean dispatchMediaKeyEventAsSystemService(@NonNull MediaSession.Token sessionToken,
            @NonNull KeyEvent keyEvent) {
    public boolean dispatchMediaKeyEventToSessionAsSystemService(@NonNull KeyEvent keyEvent,
            @NonNull MediaSession.Token sessionToken) {
        if (sessionToken == null) {
            throw new IllegalArgumentException("sessionToken shouldn't be null");
            throw new NullPointerException("sessionToken shouldn't be null");
        }
        if (keyEvent == null) {
            throw new IllegalArgumentException("keyEvent shouldn't be null");
            throw new NullPointerException("keyEvent shouldn't be null");
        }
        if (!KeyEvent.isMediaSessionKey(keyEvent.getKeyCode())) {
            return false;
        }
        try {
            return mService.dispatchMediaKeyEventToSessionAsSystemService(mContext.getPackageName(),
                    sessionToken, keyEvent);
            return mService.dispatchMediaKeyEventToSessionAsSystemService(
                    mContext.getPackageName(), keyEvent, sessionToken);
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to send key event.", e);
        }
@@ -571,13 +574,16 @@ public final class MediaSessionManager {
    }

    /**
     * Send a volume key event. The receiver will be selected automatically.
     * Sends a volume key event. The receiver will be selected automatically.
     *
     * @param keyEvent The volume KeyEvent to send.
     * @param keyEvent the volume key event to send
     * @param streamType type of stream
     * @param musicOnly true if key event should only be sent to music stream
     * @hide
     */
    public void dispatchVolumeKeyEvent(@NonNull KeyEvent keyEvent, int stream, boolean musicOnly) {
        dispatchVolumeKeyEventInternal(false, keyEvent, stream, musicOnly);
    public void dispatchVolumeKeyEvent(@NonNull KeyEvent keyEvent, int streamType,
            boolean musicOnly) {
        dispatchVolumeKeyEventInternal(keyEvent, streamType, musicOnly, /*asSystemService=*/false);
    }

    /**
@@ -592,17 +598,21 @@ public final class MediaSessionManager {
     * Valid stream types include {@link AudioManager.PublicStreamTypes} and
     * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}.
     *
     * @param keyEvent volume key event
     * @param keyEvent the volume key event to send
     * @param streamType type of stream
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public void dispatchVolumeKeyEventAsSystemService(@NonNull KeyEvent keyEvent, int streamType) {
        dispatchVolumeKeyEventInternal(true, keyEvent, streamType, false);
        dispatchVolumeKeyEventInternal(keyEvent, streamType, /*musicOnly=*/false,
                /*asSystemService=*/true);
    }

    private void dispatchVolumeKeyEventInternal(boolean asSystemService, @NonNull KeyEvent keyEvent,
            int stream, boolean musicOnly) {
    private void dispatchVolumeKeyEventInternal(@NonNull KeyEvent keyEvent, int stream,
            boolean musicOnly, boolean asSystemService) {
        if (keyEvent == null) {
            throw new NullPointerException("keyEvent shouldn't be null");
        }
        try {
            mService.dispatchVolumeKeyEvent(mContext.getPackageName(), mContext.getOpPackageName(),
                    asSystemService, keyEvent, stream, musicOnly);
@@ -617,22 +627,22 @@ public final class MediaSessionManager {
     * Should be only called by the {@link com.android.internal.policy.PhoneWindow} when the
     * foreground activity didn't consume the key from the hardware devices.
     *
     * @param sessionToken sessionToken
     * @param keyEvent volume key event
     * @param keyEvent the volume key event to send
     * @param sessionToken the session token to which the key event should be dispatched
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public void dispatchVolumeKeyEventAsSystemService(@NonNull MediaSession.Token sessionToken,
            @NonNull KeyEvent keyEvent) {
    public void dispatchVolumeKeyEventToSessionAsSystemService(@NonNull KeyEvent keyEvent,
            @NonNull MediaSession.Token sessionToken) {
        if (sessionToken == null) {
            throw new IllegalArgumentException("sessionToken shouldn't be null");
            throw new NullPointerException("sessionToken shouldn't be null");
        }
        if (keyEvent == null) {
            throw new IllegalArgumentException("keyEvent shouldn't be null");
            throw new NullPointerException("keyEvent shouldn't be null");
        }
        try {
            mService.dispatchVolumeKeyEventToSessionAsSystemService(mContext.getPackageName(),
                    mContext.getOpPackageName(), sessionToken, keyEvent);
                    mContext.getOpPackageName(), keyEvent, sessionToken);
        } catch (RemoteException e) {
            Log.wtf(TAG, "Error calling dispatchVolumeKeyEventAsSystemService", e);
        }
+2 −2
Original line number Diff line number Diff line
@@ -76,9 +76,9 @@ package android.media.session {
  public final class MediaSessionManager {
    method public void addOnActiveSessionsChangedListener(@NonNull android.media.session.MediaSessionManager.OnActiveSessionsChangedListener, @Nullable android.content.ComponentName, int, @Nullable android.os.Handler);
    method public void dispatchMediaKeyEventAsSystemService(@NonNull android.view.KeyEvent);
    method public boolean dispatchMediaKeyEventAsSystemService(@NonNull android.media.session.MediaSession.Token, @NonNull android.view.KeyEvent);
    method public boolean dispatchMediaKeyEventToSessionAsSystemService(@NonNull android.view.KeyEvent, @NonNull android.media.session.MediaSession.Token);
    method public void dispatchVolumeKeyEventAsSystemService(@NonNull android.view.KeyEvent, int);
    method public void dispatchVolumeKeyEventAsSystemService(@NonNull android.media.session.MediaSession.Token, @NonNull android.view.KeyEvent);
    method public void dispatchVolumeKeyEventToSessionAsSystemService(@NonNull android.view.KeyEvent, @NonNull android.media.session.MediaSession.Token);
    field public static final int RESULT_MEDIA_KEY_HANDLED = 1; // 0x1
    field public static final int RESULT_MEDIA_KEY_NOT_HANDLED = 0; // 0x0
  }
Loading