1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +01:00

Allow to select preferred music player in preferences, closes #112

This commit is contained in:
Andreas Shimokawa 2015-09-16 00:53:54 +02:00
parent de74a033f6
commit 6e3c839608
6 changed files with 52 additions and 30 deletions

View File

@ -228,9 +228,7 @@
android:name=".externalevents.MusicPlaybackReceiver"
android:enabled="false">
<intent-filter>
<action android:name="com.andrew.apollo.metachanged" />
<action android:name="com.cyanogenmod.eleven.metachanged" />
<action android:name="com.spotify.music.metadatachanged" />
<action android:name="com.android.music.metachanged"/>
</intent-filter>
</receiver>
<receiver

View File

@ -1,10 +1,15 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.support.v4.content.LocalBroadcastManager;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity;
@ -68,13 +73,40 @@ public class SettingsActivity extends AbstractSettingsActivity {
}
});
// Get all receivers of Media Buttons
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
PackageManager pm = getPackageManager();
List<ResolveInfo> mediaReceivers = pm.queryBroadcastReceivers(mediaButtonIntent,
PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RESOLVED_FILTER);
CharSequence[] newEntries = new CharSequence[mediaReceivers.size() + 1];
CharSequence[] newValues = new CharSequence[mediaReceivers.size() + 1];
newEntries[0] = getString(R.string.pref_default);
newValues[0] = "default";
int i = 1;
for (ResolveInfo resolveInfo : mediaReceivers) {
newEntries[i] = resolveInfo.activityInfo.loadLabel(pm);
newValues[i] = resolveInfo.activityInfo.packageName;
i++;
}
final ListPreference audioPlayer = (ListPreference) findPreference("audio_player");
audioPlayer.setEntries(newEntries);
audioPlayer.setEntryValues(newValues);
}
@Override
protected String[] getPreferenceKeysWithSummary() {
return new String[]{
"audio_player",
"notification_mode_sms",
"notification_mode_k9mail",
"pebble_emu_addr",
"pebble_emu_port"
"pebble_emu_port",
};
}

View File

@ -18,19 +18,6 @@ public class MusicPlaybackReceiver extends BroadcastReceiver {
@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

@ -54,23 +54,23 @@ public class GBMusicControlReceiver extends BroadcastReceiver {
if (keyCode != -1) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String audioSource = sharedPrefs.getString("last_audiosource", null);
String audioPlayer = sharedPrefs.getString("audio_player", "default");
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);
if (!"default".equals(audioPlayer)) {
downIntent.setPackage(audioPlayer);
}
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);
if (audioPlayer != null) {
upIntent.setPackage(audioPlayer);
}
context.sendOrderedBroadcast(upIntent, null);
}

View File

@ -33,7 +33,8 @@
<string name="pref_header_general">General Settings</string>
<string name="pref_title_general_autoconnectonbluetooth">Connect to device when Bluetooth turned on</string>
<string name="pref_title_audo_player">Preferred audio player</string>
<string name="pref_default">default</string>
<string name="pref_header_datetime">Date and Time</string>
<string name="pref_title_datetime_syctimeonconnect">Sync time</string>
<string name="pref_summary_datetime_syctimeonconnect">Sync time to device when connecting and when time or timezone changes on Android</string>

View File

@ -7,6 +7,10 @@
android:defaultValue="false"
android:key="general_autoconnectonbluetooth"
android:title="@string/pref_title_general_autoconnectonbluetooth" />
<ListPreference
android:defaultValue="default"
android:key="audio_player"
android:title="@string/pref_title_audo_player" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_key_datetime"
@ -60,26 +64,26 @@
android:key="pref_key_device_specific_category"
android:title="@string/preferences_category_device_specific_settings">
<Preference
android:key="pref_key_miband"
android:icon="@drawable/ic_device_miband"
android:key="pref_key_miband"
android:title="@string/preferences_miband_settings" />
<PreferenceScreen
android:key="pref_key_pebble"
android:icon="@drawable/ic_device_pebble"
android:title="@string/pref_title_pebble_settings" >
android:key="pref_key_pebble"
android:title="@string/pref_title_pebble_settings">
<PreferenceCategory
android:key="pref_key_development"
android:title="@string/pref_header_development">
<CheckBoxPreference
android:defaultValue="false"
android:key="pebble_force_protocol"
android:title="@string/pref_title_pebble_forceprotocol"
android:summary="@string/pref_summary_pebble_forceprotocol" />
android:summary="@string/pref_summary_pebble_forceprotocol"
android:title="@string/pref_title_pebble_forceprotocol" />
<CheckBoxPreference
android:defaultValue="false"
android:key="pebble_force_untested"
android:title="@string/pref_title_pebble_forceuntested"
android:summary="@string/pref_summary_pebble_forceuntested" />
android:summary="@string/pref_summary_pebble_forceuntested"
android:title="@string/pref_title_pebble_forceuntested" />
<EditTextPreference
android:digits="0123456789."
android:key="pebble_emu_addr"