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

Commit 92dea33b authored by Jaewan Kim's avatar Jaewan Kim Committed by Jae Seo
Browse files

Simplify the media button handling logic

Here's the new media handling logic.

1. If the foreground app consumes the media key event
  (Activity.onKeyDown/Up() or Activity.setMediaController()),
  we stop here.
2. Find the media app whose AudioPlaybackConfiguration becomes
  PLAYER_STATE_STARTED lastly across the boot. The media app is the app
  with the media session.
  - If its session is still alive, send the media key event
    to the media session.
  - If its session has been released but the session set the media
    button receiver, send the media key event through the media button
    receiver.
  - If it has multiple media sessions, pick the media session whose
    PlaybackState matches with the AudioPlaybackConfiguration's player
    state.
3. Stop here. Don't try revive any other app.

This approach has two limitations.
1. If an app has multiple AudioPlaybackConfigurations and/or
   media sessions, the MediaSessionService may pick the wrong media
   session to send media key events.
2. If an app with a media session plays sound effects differently from
   the SoundPool class, the MediaSessionService would consider the app
   as the lastly played media app.

Bug: 33032080
Test: Manual tests as follows
  1. Checked that the lastly played app receives the media key events
     although the app's media session doesn't report its playback state.
  2. Checked that the lastly played app receives the media key events
     although the app's media session is released.
  3. Checked that the lastly played app doesn't receive the media key
     events if its playback is remote.
Change-Id: I1fd6f9eee0750da4dea9fcc7401fdb1c3f249a72
parent 7c665f78
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -24337,9 +24337,9 @@ package android.media.session {
    method public void setRepeatMode(int);
    method public void setSessionActivity(android.app.PendingIntent);
    method public void setShuffleModeEnabled(boolean);
    field public static final int FLAG_HANDLES_MEDIA_BUTTONS = 1; // 0x1
    field public static final deprecated int FLAG_HANDLES_MEDIA_BUTTONS = 1; // 0x1
    field public static final int FLAG_HANDLES_QUEUE_COMMANDS = 4; // 0x4
    field public static final int FLAG_HANDLES_TRANSPORT_CONTROLS = 2; // 0x2
    field public static final deprecated int FLAG_HANDLES_TRANSPORT_CONTROLS = 2; // 0x2
  }
  public static abstract class MediaSession.Callback {
+2 −2
Original line number Diff line number Diff line
@@ -26207,9 +26207,9 @@ package android.media.session {
    method public void setRepeatMode(int);
    method public void setSessionActivity(android.app.PendingIntent);
    method public void setShuffleModeEnabled(boolean);
    field public static final int FLAG_HANDLES_MEDIA_BUTTONS = 1; // 0x1
    field public static final deprecated int FLAG_HANDLES_MEDIA_BUTTONS = 1; // 0x1
    field public static final int FLAG_HANDLES_QUEUE_COMMANDS = 4; // 0x4
    field public static final int FLAG_HANDLES_TRANSPORT_CONTROLS = 2; // 0x2
    field public static final deprecated int FLAG_HANDLES_TRANSPORT_CONTROLS = 2; // 0x2
  }
  public static abstract class MediaSession.Callback {
+2 −2
Original line number Diff line number Diff line
@@ -24448,9 +24448,9 @@ package android.media.session {
    method public void setRepeatMode(int);
    method public void setSessionActivity(android.app.PendingIntent);
    method public void setShuffleModeEnabled(boolean);
    field public static final int FLAG_HANDLES_MEDIA_BUTTONS = 1; // 0x1
    field public static final deprecated int FLAG_HANDLES_MEDIA_BUTTONS = 1; // 0x1
    field public static final int FLAG_HANDLES_QUEUE_COMMANDS = 4; // 0x4
    field public static final int FLAG_HANDLES_TRANSPORT_CONTROLS = 2; // 0x2
    field public static final deprecated int FLAG_HANDLES_TRANSPORT_CONTROLS = 2; // 0x2
  }
  public static abstract class MediaSession.Callback {
+6 −0
Original line number Diff line number Diff line
@@ -77,13 +77,19 @@ public final class MediaSession {
    /**
     * Set this flag on the session to indicate that it can handle media button
     * events.
     * @deprecated This flag is no longer used. All media sessions are expected to handle media
     * button events now.
     */
    @Deprecated
    public static final int FLAG_HANDLES_MEDIA_BUTTONS = 1 << 0;

    /**
     * Set this flag on the session to indicate that it handles transport
     * control commands through its {@link Callback}.
     * @deprecated This flag is no longer used. All media sessions are expected to handle transport
     * controls now.
     */
    @Deprecated
    public static final int FLAG_HANDLES_TRANSPORT_CONTROLS = 1 << 1;

    /**
+2 −2
Original line number Diff line number Diff line
@@ -6555,7 +6555,7 @@ public class AudioService extends IAudioService.Stub

    public void registerPlaybackCallback(IPlaybackConfigDispatcher pcdb) {
        final boolean isPrivileged =
                (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission(
                (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission(
                        android.Manifest.permission.MODIFY_AUDIO_ROUTING));
        mPlaybackMonitor.registerPlaybackCallback(pcdb, isPrivileged);
    }
@@ -6566,7 +6566,7 @@ public class AudioService extends IAudioService.Stub

    public List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() {
        final boolean isPrivileged =
                (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission(
                (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission(
                        android.Manifest.permission.MODIFY_AUDIO_ROUTING));
        return mPlaybackMonitor.getActivePlaybackConfigurations(isPrivileged);
    }
Loading