Try to support controlling multiple music players

This tries to control the last player that played a (new) song.
It is very limited since we cannot get the source of an intent.
Instead we try to guess from the Action.
The problem is that we cannot support players that use only the action "com.android.music.XXXX" and not something own.
Also try to blindly support getting spotify metadata (for testing #112)
live-activity-data
Andreas Shimokawa 2015-09-15 01:36:33 +02:00
parent aa524cc5ea
commit de74a033f6
3 changed files with 29 additions and 0 deletions

View File

@ -230,6 +230,7 @@
<intent-filter>
<action android:name="com.andrew.apollo.metachanged" />
<action android:name="com.cyanogenmod.eleven.metachanged" />
<action android:name="com.spotify.music.metadatachanged" />
</intent-filter>
</receiver>
<receiver

View File

@ -3,6 +3,8 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,8 +14,23 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
public class MusicPlaybackReceiver extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(MusicPlaybackReceiver.class);
private static String mLastSource;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int lastDot = action.lastIndexOf(".");
String source = action.substring(0, lastDot);
if (!source.equals(mLastSource)) {
mLastSource = source;
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = sharedPrefs.edit();
edit.putString("last_audiosource", mLastSource);
LOG.info("set last audiosource to " + mLastSource);
edit.apply();
}
String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");

View File

@ -3,8 +3,10 @@ package nodomain.freeyourgadget.gadgetbridge.service.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import org.slf4j.Logger;
@ -51,16 +53,25 @@ public class GBMusicControlReceiver extends BroadcastReceiver {
}
if (keyCode != -1) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String audioSource = sharedPrefs.getString("last_audiosource", null);
long eventtime = SystemClock.uptimeMillis();
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
if (audioSource != null) {
downIntent.setPackage(audioSource);
}
context.sendOrderedBroadcast(downIntent, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, keyCode, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
if (audioSource != null) {
upIntent.setPackage(audioSource);
}
context.sendOrderedBroadcast(upIntent, null);
}
}