Skip to content
Snippets Groups Projects
Commit b096e059 authored by ezio84's avatar ezio84 Committed by Mohammad Hasan Keramat J
Browse files

VolumeKeyHandler: Add support for skipping tracks with remote control

For Spotify and other players that allow music controls for
remote devices (PC, PS4) through the media notification

Change-Id: I38887f8b1cff1a0c1e3adadbfe37d5af59b5cdcc
parent 71ee13cd
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (C) 2017 The LineageOS Project
* Copyright (C) 2018,2020 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -21,7 +21,10 @@ import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.media.session.MediaController;
import android.media.session.MediaSessionLegacyHelper;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.os.Handler;
import android.os.Message;
import android.os.UserHandle;
......@@ -30,6 +33,8 @@ import android.util.Slog;
import android.view.KeyEvent;
import android.view.ViewConfiguration;
import java.util.List;
public final class VolumeKeyHandler {
private final String TAG = "VolumeKeyHandler";
private final boolean DEBUG = false;
......@@ -37,7 +42,8 @@ public final class VolumeKeyHandler {
private static final int MSG_DISPATCH_VOLKEY_WITH_WAKELOCK = 1;
private final Context mContext;
private ButtonHandler mHandler;
private final ButtonHandler mHandler;
private final MediaSessionManager mMediaSessionManager;
private boolean mIsLongPress = false;
......@@ -53,8 +59,7 @@ public final class VolumeKeyHandler {
if (DEBUG) {
Slog.d(TAG, "Dispatching key to audio service");
}
dispatchMediaKeyToAudioService(ev);
dispatchMediaKeyToAudioService(KeyEvent.changeAction(ev, KeyEvent.ACTION_UP));
onSkipTrackEvent(ev);
break;
}
}
......@@ -63,6 +68,7 @@ public final class VolumeKeyHandler {
public VolumeKeyHandler(Context context) {
mContext = context;
mHandler = new ButtonHandler();
mMediaSessionManager = mContext.getSystemService(MediaSessionManager.class);
SettingsObserver observer = new SettingsObserver(new Handler());
observer.observe();
......@@ -128,11 +134,34 @@ public final class VolumeKeyHandler {
return true;
}
void dispatchMediaKeyToAudioService(KeyEvent ev) {
if (DEBUG) {
Slog.d(TAG, "Dispatching KeyEvent " + ev + " to audio service");
private void triggerKeyEvents(KeyEvent evDown, MediaController controller) {
final KeyEvent evUp = KeyEvent.changeAction(evDown, KeyEvent.ACTION_UP);
mHandler.post(() -> controller.dispatchMediaButtonEvent(evDown));
mHandler.postDelayed(() -> controller.dispatchMediaButtonEvent(evUp), 20);
}
public void onSkipTrackEvent(KeyEvent ev) {
if (mMediaSessionManager != null) {
final List<MediaController> sessions = mMediaSessionManager.getActiveSessionsForUser(
null, UserHandle.USER_ALL);
for (MediaController mediaController : sessions) {
if (PlaybackState.STATE_PLAYING ==
getMediaControllerPlaybackState(mediaController)) {
triggerKeyEvents(ev, mediaController);
break;
}
}
}
}
private int getMediaControllerPlaybackState(MediaController controller) {
if (controller != null) {
final PlaybackState playbackState = controller.getPlaybackState();
if (playbackState != null) {
return playbackState.getState();
}
}
MediaSessionLegacyHelper.getHelper(mContext).sendMediaButtonEvent(ev, true);
return PlaybackState.STATE_NONE;
}
class SettingsObserver extends ContentObserver {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment