Skip to content

Commit

Permalink
Rollback of androidx@64bd3bc
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 574766164
  • Loading branch information
tonihei authored and copybara-github committed Oct 19, 2023
1 parent cf37337 commit f0cab4d
Show file tree
Hide file tree
Showing 8 changed files with 688 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@
public static final String EXTRAS_KEY_ACTION_CUSTOM_EXTRAS =
"androidx.media3.session.EXTRAS_KEY_CUSTOM_NOTIFICATION_ACTION_EXTRAS";

/**
* Returns the {@link KeyEvent} that was included in the media action, or {@code null} if no
* {@link KeyEvent} is found in the {@code intent}.
*/
@Nullable
public static KeyEvent getKeyEvent(Intent intent) {
@Nullable Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(Intent.EXTRA_KEY_EVENT)) {
return extras.getParcelable(Intent.EXTRA_KEY_EVENT);
}
return null;
}

private final Service service;

private int customActionPendingIntentRequestCode = 0;
Expand Down Expand Up @@ -97,6 +110,7 @@ public NotificationCompat.Action createCustomActionFromCustomCommandButton(
mediaSession, customCommand.customAction, customCommand.customExtras));
}

@SuppressWarnings("PendingIntentMutability") // We can't use SaferPendingIntent
@Override
public PendingIntent createMediaActionPendingIntent(
MediaSession mediaSession, @Player.Command long command) {
Expand Down Expand Up @@ -136,6 +150,7 @@ private int toKeyCode(@Player.Command long action) {
return KEYCODE_UNKNOWN;
}

@SuppressWarnings("PendingIntentMutability") // We can't use SaferPendingIntent
private PendingIntent createCustomActionPendingIntent(
MediaSession mediaSession, String action, Bundle extras) {
Intent intent = new Intent(ACTION_CUSTOM);
Expand All @@ -162,19 +177,6 @@ public boolean isCustomAction(Intent intent) {
return ACTION_CUSTOM.equals(intent.getAction());
}

/**
* Returns the {@link KeyEvent} that was included in the media action, or {@code null} if no
* {@link KeyEvent} is found in the {@code intent}.
*/
@Nullable
public KeyEvent getKeyEvent(Intent intent) {
@Nullable Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(Intent.EXTRA_KEY_EVENT)) {
return extras.getParcelable(Intent.EXTRA_KEY_EVENT);
}
return null;
}

/**
* Returns the custom action that was included in the {@link #createCustomAction custom action},
* or {@code null} if no custom action is found in the {@code intent}.
Expand All @@ -201,6 +203,7 @@ public Bundle getCustomActionExtras(Intent intent) {
private static final class Api26 {
private Api26() {}

@SuppressWarnings("PendingIntentMutability") // We can't use SaferPendingIntent
public static PendingIntent createForegroundServicePendingIntent(
Service service, int keyCode, Intent intent) {
return PendingIntent.getForegroundService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@

import static android.app.Service.STOP_FOREGROUND_DETACH;
import static android.app.Service.STOP_FOREGROUND_REMOVE;
import static android.view.KeyEvent.KEYCODE_MEDIA_FAST_FORWARD;
import static android.view.KeyEvent.KEYCODE_MEDIA_NEXT;
import static android.view.KeyEvent.KEYCODE_MEDIA_PAUSE;
import static android.view.KeyEvent.KEYCODE_MEDIA_PLAY;
import static android.view.KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
import static android.view.KeyEvent.KEYCODE_MEDIA_PREVIOUS;
import static android.view.KeyEvent.KEYCODE_MEDIA_REWIND;
import static android.view.KeyEvent.KEYCODE_MEDIA_STOP;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import android.annotation.SuppressLint;
Expand All @@ -34,7 +26,6 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import androidx.annotation.DoNotInline;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
Expand Down Expand Up @@ -74,7 +65,7 @@
private final NotificationManagerCompat notificationManagerCompat;
private final Executor mainExecutor;
private final Intent startSelfIntent;
private final Map<MediaSession, ControllerAndListener> controllerAndListenerMap;
private final Map<MediaSession, ListenableFuture<MediaController>> controllerMap;

private int totalNotificationCount;
@Nullable private MediaNotification mediaNotification;
Expand All @@ -91,34 +82,30 @@ public MediaNotificationManager(
Handler mainHandler = new Handler(Looper.getMainLooper());
mainExecutor = (runnable) -> Util.postOrRun(mainHandler, runnable);
startSelfIntent = new Intent(mediaSessionService, mediaSessionService.getClass());
controllerAndListenerMap = new HashMap<>();
controllerMap = new HashMap<>();
startedInForeground = false;
}

public void addSession(MediaSession session) {
if (controllerAndListenerMap.containsKey(session)) {
if (controllerMap.containsKey(session)) {
return;
}
MediaControllerListener controllerListener =
new MediaControllerListener(mediaSessionService, session);
PlayerListener playerListener = new PlayerListener(mediaSessionService, session);
MediaControllerListener listener = new MediaControllerListener(mediaSessionService, session);
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(KEY_MEDIA_NOTIFICATION_MANAGER, true);
ListenableFuture<MediaController> controllerFuture =
new MediaController.Builder(mediaSessionService, session.getToken())
.setConnectionHints(connectionHints)
.setListener(controllerListener)
.setListener(listener)
.setApplicationLooper(Looper.getMainLooper())
.buildAsync();
controllerAndListenerMap.put(
session, new ControllerAndListener(controllerFuture, playerListener));
controllerMap.put(session, controllerFuture);
controllerFuture.addListener(
() -> {
try {
// Assert connection success.
controllerFuture.get(/* time= */ 0, MILLISECONDS);
controllerListener.onConnected(shouldShowNotification(session));
session.getImpl().addPlayerListener(playerListener);
MediaController controller = controllerFuture.get(/* time= */ 0, MILLISECONDS);
listener.onConnected(shouldShowNotification(session));
controller.addListener(listener);
} catch (CancellationException
| ExecutionException
| InterruptedException
Expand All @@ -131,52 +118,9 @@ public void addSession(MediaSession session) {
}

public void removeSession(MediaSession session) {
ControllerAndListener controllerAndListener = controllerAndListenerMap.remove(session);
if (controllerAndListener != null) {
session.getImpl().removePlayerListener(controllerAndListener.listener);
MediaController.releaseFuture(controllerAndListener.controller);
}
}

public void onMediaButtonEvent(MediaSession session, KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
@Nullable MediaController mediaController = getConnectedControllerForSession(session);
if (mediaController == null) {
session.getSessionCompat().getController().dispatchMediaButtonEvent(keyEvent);
return;
}
switch (keyCode) {
case KEYCODE_MEDIA_PLAY_PAUSE:
if (mediaController.getPlayWhenReady()) {
mediaController.pause();
} else {
mediaController.play();
}
break;
case KEYCODE_MEDIA_PLAY:
mediaController.play();
break;
case KEYCODE_MEDIA_PAUSE:
mediaController.pause();
break;
case KEYCODE_MEDIA_NEXT:
mediaController.seekToNext();
break;
case KEYCODE_MEDIA_PREVIOUS:
mediaController.seekToPrevious();
break;
case KEYCODE_MEDIA_FAST_FORWARD:
mediaController.seekForward();
break;
case KEYCODE_MEDIA_REWIND:
mediaController.seekBack();
break;
case KEYCODE_MEDIA_STOP:
mediaController.stop();
break;
default:
Log.w(TAG, "Received media button event with unsupported key code: " + keyCode);
break;
@Nullable ListenableFuture<MediaController> future = controllerMap.remove(session);
if (future != null) {
MediaController.releaseFuture(future);
}
}

Expand Down Expand Up @@ -210,11 +154,11 @@ public void updateNotification(MediaSession session, boolean startInForegroundRe

int notificationSequence = ++totalNotificationCount;
MediaController mediaNotificationController = null;
ControllerAndListener controllerAndListener = controllerAndListenerMap.get(session);
if (controllerAndListener != null && controllerAndListener.controller.isDone()) {
ListenableFuture<MediaController> controller = controllerMap.get(session);
if (controller != null && controller.isDone()) {
try {
mediaNotificationController = Futures.getDone(controllerAndListener.controller);
} catch (CancellationException | ExecutionException e) {
mediaNotificationController = Futures.getDone(controller);
} catch (ExecutionException e) {
// Ignore.
}
}
Expand Down Expand Up @@ -317,13 +261,13 @@ private boolean shouldShowNotification(MediaSession session) {

@Nullable
private MediaController getConnectedControllerForSession(MediaSession session) {
ControllerAndListener controllerAndListener = controllerAndListenerMap.get(session);
if (controllerAndListener == null) {
ListenableFuture<MediaController> controller = controllerMap.get(session);
if (controller == null) {
return null;
}
try {
return Futures.getDone(controllerAndListener.controller);
} catch (CancellationException | ExecutionException exception) {
return Futures.getDone(controller);
} catch (ExecutionException exception) {
// We should never reach this.
throw new IllegalStateException(exception);
}
Expand Down Expand Up @@ -361,7 +305,8 @@ public void onFailure(Throwable t) {
}
}

private static final class MediaControllerListener implements MediaController.Listener {
private static final class MediaControllerListener
implements MediaController.Listener, Player.Listener {
private final MediaSessionService mediaSessionService;
private final MediaSession session;

Expand Down Expand Up @@ -399,18 +344,6 @@ public void onDisconnected(MediaController controller) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
}
}

private static class PlayerListener implements Player.Listener {
private final MediaSessionService mediaSessionService;
private final MediaSession session;
private final Handler mainHandler;

public PlayerListener(MediaSessionService mediaSessionService, MediaSession session) {
this.mediaSessionService = mediaSessionService;
this.session = session;
mainHandler = new Handler(Looper.getMainLooper());
}

@Override
public void onEvents(Player player, Player.Events events) {
Expand All @@ -421,13 +354,8 @@ public void onEvents(Player player, Player.Events events) {
Player.EVENT_PLAY_WHEN_READY_CHANGED,
Player.EVENT_MEDIA_METADATA_CHANGED,
Player.EVENT_TIMELINE_CHANGED)) {
// onUpdateNotificationInternal is required to be called on the main thread and the
// application thread of the player may be a different thread.
Util.postOrRun(
mainHandler,
() ->
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false));
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
}
}
}
Expand Down Expand Up @@ -457,17 +385,6 @@ private void stopForeground(boolean removeNotifications) {
startedInForeground = false;
}

private static class ControllerAndListener {
public final ListenableFuture<MediaController> controller;
public final Player.Listener listener;

private ControllerAndListener(
ListenableFuture<MediaController> controller, Player.Listener listener) {
this.controller = controller;
this.listener = listener;
}
}

@RequiresApi(24)
private static class Api24 {

Expand Down
Loading

0 comments on commit f0cab4d

Please sign in to comment.