mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-25 11:26:47 +01:00
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)
This commit is contained in:
parent
aa524cc5ea
commit
de74a033f6
@ -230,6 +230,7 @@
|
|||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="com.andrew.apollo.metachanged" />
|
<action android:name="com.andrew.apollo.metachanged" />
|
||||||
<action android:name="com.cyanogenmod.eleven.metachanged" />
|
<action android:name="com.cyanogenmod.eleven.metachanged" />
|
||||||
|
<action android:name="com.spotify.music.metadatachanged" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</receiver>
|
</receiver>
|
||||||
<receiver
|
<receiver
|
||||||
|
@ -3,6 +3,8 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
|||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -12,8 +14,23 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|||||||
public class MusicPlaybackReceiver extends BroadcastReceiver {
|
public class MusicPlaybackReceiver extends BroadcastReceiver {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(MusicPlaybackReceiver.class);
|
private static final Logger LOG = LoggerFactory.getLogger(MusicPlaybackReceiver.class);
|
||||||
|
|
||||||
|
private static String mLastSource;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
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 artist = intent.getStringExtra("artist");
|
||||||
String album = intent.getStringExtra("album");
|
String album = intent.getStringExtra("album");
|
||||||
String track = intent.getStringExtra("track");
|
String track = intent.getStringExtra("track");
|
||||||
|
@ -3,8 +3,10 @@ package nodomain.freeyourgadget.gadgetbridge.service.receivers;
|
|||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -51,16 +53,25 @@ public class GBMusicControlReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (keyCode != -1) {
|
if (keyCode != -1) {
|
||||||
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
String audioSource = sharedPrefs.getString("last_audiosource", null);
|
||||||
|
|
||||||
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);
|
||||||
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
|
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
|
||||||
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
|
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
|
||||||
|
if (audioSource != null) {
|
||||||
|
downIntent.setPackage(audioSource);
|
||||||
|
}
|
||||||
context.sendOrderedBroadcast(downIntent, null);
|
context.sendOrderedBroadcast(downIntent, null);
|
||||||
|
|
||||||
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
|
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
|
||||||
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, keyCode, 0);
|
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, keyCode, 0);
|
||||||
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
|
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
|
||||||
|
if (audioSource != null) {
|
||||||
|
upIntent.setPackage(audioSource);
|
||||||
|
}
|
||||||
context.sendOrderedBroadcast(upIntent, null);
|
context.sendOrderedBroadcast(upIntent, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user