1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 11:26:47 +01:00

Support control of multiple media playback applications (#807)

* Added Lollipop only detection of the playing media package, using MediaSessionManager. Replicates funtionality of the Official Pebble client.
This commit is contained in:
Gabe Schrecker 2017-09-10 14:17:21 +01:00 committed by Daniele Gobbetti
parent b66b33239d
commit 6f702778f4

View File

@ -18,17 +18,23 @@
package nodomain.freeyourgadget.gadgetbridge.service.receivers; package nodomain.freeyourgadget.gadgetbridge.service.receivers;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.session.MediaController;
import android.media.session.MediaSessionManager;
import android.os.SystemClock; import android.os.SystemClock;
import android.view.KeyEvent; import android.view.KeyEvent;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class GBMusicControlReceiver extends BroadcastReceiver { public class GBMusicControlReceiver extends BroadcastReceiver {
@ -73,6 +79,22 @@ public class GBMusicControlReceiver extends BroadcastReceiver {
Prefs prefs = GBApplication.getPrefs(); Prefs prefs = GBApplication.getPrefs();
String audioPlayer = prefs.getString("audio_player", "default"); String audioPlayer = prefs.getString("audio_player", "default");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
MediaSessionManager mediaSessionManager =
(MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
List<MediaController> controllers = mediaSessionManager.getActiveSessions(
new ComponentName(context, NotificationListener.class));
try {
MediaController controller = controllers.get(0);
audioPlayer = controller.getPackageName();
} catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException: " + e.getMessage());
}
}
LOG.debug("keypress: " + musicCmd.toString() + " sent to: " + audioPlayer);
long eventtime = SystemClock.uptimeMillis(); long eventtime = SystemClock.uptimeMillis();
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);