diff --git a/ultrasonic/src/main/AndroidManifest.xml b/ultrasonic/src/main/AndroidManifest.xml index 5c9a7d61..4957fe6d 100644 --- a/ultrasonic/src/main/AndroidManifest.xml +++ b/ultrasonic/src/main/AndroidManifest.xml @@ -146,7 +146,7 @@ - + = Build.VERSION_CODES.O) { @@ -204,15 +210,84 @@ public class SettingsFragment extends PreferenceFragment } private void setupBluetoothDevicePreferences() { - resumeOnBluetoothDevice.setSummary(settings.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, - FileUtil.getDefaultMusicDirectory(getActivity()).getPath())); + final int resumeSetting = Util.getResumeOnBluetoothDevice(getActivity()); + final int pauseSetting = Util.getPauseOnBluetoothDevice(getActivity()); + + resumeOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(resumeSetting)); + pauseOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(pauseSetting)); resumeOnBluetoothDevice.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { - return true; + showBluetoothDevicePreferenceDialog( + R.string.settings_playback_resume_on_bluetooth_device, + Util.getResumeOnBluetoothDevice(getActivity()), + new Consumer() { + @Override + public void accept(Integer choice) { + SharedPreferences.Editor editor = resumeOnBluetoothDevice.getEditor(); + editor.putInt(Constants.PREFERENCES_KEY_RESUME_ON_BLUETOOTH_DEVICE, choice); + editor.commit(); + resumeOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(choice)); + } + }); + return true; } }); + + pauseOnBluetoothDevice.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + showBluetoothDevicePreferenceDialog( + R.string.settings_playback_pause_on_bluetooth_device, + Util.getPauseOnBluetoothDevice(getActivity()), + new Consumer() { + @Override + public void accept(Integer choice) { + SharedPreferences.Editor editor = pauseOnBluetoothDevice.getEditor(); + editor.putInt(Constants.PREFERENCES_KEY_PAUSE_ON_BLUETOOTH_DEVICE, choice); + editor.commit(); + pauseOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(choice)); + } + }); + return true; + } + }); + } + + private void showBluetoothDevicePreferenceDialog(@StringRes int title, int defaultChoice, final Consumer onChosen) { + final int[] choice = {defaultChoice}; + new AlertDialog.Builder(getActivity()).setTitle(title) + .setSingleChoiceItems(R.array.bluetoothDeviceSettingNames, defaultChoice, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + choice[0] = i; + } + }) + .setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + dialogInterface.cancel(); + } + }) + .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + onChosen.accept(choice[0]); + dialogInterface.dismiss(); + } + }) + .create().show(); + } + + private String bluetoothDevicePreferenceToString(int preferenceValue) { + switch (preferenceValue) { + case Constants.PREFERENCE_VALUE_ALL: return getString(R.string.settings_playback_bluetooth_all); + case Constants.PREFERENCE_VALUE_A2DP: return getString(R.string.settings_playback_bluetooth_a2dp); + case Constants.PREFERENCE_VALUE_DISABLED: return getString(R.string.settings_playback_bluetooth_disabled); + default: return ""; + } } private void setupClearSearchPreference() { diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/BluetoothIntentReceiver.java b/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/BluetoothIntentReceiver.java index abee751e..5c70c13c 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/BluetoothIntentReceiver.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/receiver/BluetoothIntentReceiver.java @@ -19,6 +19,7 @@ package org.moire.ultrasonic.receiver; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothProfile; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -39,28 +40,50 @@ public class BluetoothIntentReceiver extends BroadcastReceiver @Override public void onReceive(Context context, Intent intent) { - int state = intent.getIntExtra("android.bluetooth.a2dp.extra.SINK_STATE", -1); + int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String action = intent.getAction(); String name = device != null ? device.getName() : "Unknown"; String address = device != null ? device.getAddress() : "Unknown"; - Log.d(TAG, String.format("Sink State: %d; Action: %s; Device: %s; Address: %s", state, action, name, address)); + Log.d(TAG, String.format("A2DP State: %d; Action: %s; Device: %s; Address: %s", state, action, name, address)); - boolean actionConnected = false; - boolean actionDisconnected = false; + boolean actionBluetoothDeviceConnected = false; + boolean actionBluetoothDeviceDisconnected = false; + boolean actionA2dpConnected = false; + boolean actionA2dpDisconnected = false; if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { - actionConnected = true; + actionBluetoothDeviceConnected = true; } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) || BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) { - actionDisconnected = true; + actionBluetoothDeviceDisconnected = true; } - boolean connected = state == android.bluetooth.BluetoothA2dp.STATE_CONNECTED || actionConnected; - boolean disconnected = state == android.bluetooth.BluetoothA2dp.STATE_DISCONNECTED || actionDisconnected; + if (state == android.bluetooth.BluetoothA2dp.STATE_CONNECTED) actionA2dpConnected = true; + else if (state == android.bluetooth.BluetoothA2dp.STATE_DISCONNECTED) actionA2dpDisconnected = true; + + boolean connected = actionA2dpConnected || actionBluetoothDeviceConnected; + boolean resume = false; + boolean pause = false; + + switch (Util.getResumeOnBluetoothDevice(context)) + { + case Constants.PREFERENCE_VALUE_ALL: resume = actionA2dpConnected || actionBluetoothDeviceConnected; + break; + case Constants.PREFERENCE_VALUE_A2DP: resume = actionA2dpConnected; + break; + } + + switch (Util.getPauseOnBluetoothDevice(context)) + { + case Constants.PREFERENCE_VALUE_ALL: pause = actionA2dpDisconnected || actionBluetoothDeviceDisconnected; + break; + case Constants.PREFERENCE_VALUE_A2DP: pause = actionA2dpDisconnected; + break; + } if (connected) { @@ -68,7 +91,13 @@ public class BluetoothIntentReceiver extends BroadcastReceiver Util.registerMediaButtonEventReceiver(context, false); } - if (disconnected) + if (resume) + { + Log.i(TAG, String.format("Connected to Bluetooth device %s address %s, resuming playback.", name, address)); + context.sendBroadcast(new Intent(Constants.CMD_PLAY)); + } + + if (pause) { Log.i(TAG, String.format("Disconnected from Bluetooth device %s address %s, requesting pause.", name, address)); context.sendBroadcast(new Intent(Constants.CMD_PAUSE)); diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Constants.java b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Constants.java index 792d33ab..3b330ec3 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Constants.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Constants.java @@ -134,9 +134,9 @@ public final class Constants public static final String PREFERENCES_KEY_RESUME_ON_BLUETOOTH_DEVICE = "resumeOnBluetoothDevice"; public static final String PREFERENCES_KEY_PAUSE_ON_BLUETOOTH_DEVICE = "pauseOnBluetoothDevice"; - public static final String PREFERENCE_VALUE_ALL = "all"; - public static final String PREFERENCE_VALUE_A2DP = "a2dp"; - public static final String PREFERENCE_VALUE_DISABLED = "disabled"; + public static final int PREFERENCE_VALUE_ALL = 0; + public static final int PREFERENCE_VALUE_A2DP = 1; + public static final int PREFERENCE_VALUE_DISABLED = 2; // Number of free trial days for non-licensed servers. public static final int FREE_TRIAL_DAYS = 30; diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java index c6c1525d..50a3d6bc 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/util/Util.java @@ -1447,4 +1447,16 @@ public class Util editor.apply(); return true; } + + public static int getResumeOnBluetoothDevice(Context context) + { + SharedPreferences preferences = getPreferences(context); + return preferences.getInt(Constants.PREFERENCES_KEY_RESUME_ON_BLUETOOTH_DEVICE, Constants.PREFERENCE_VALUE_DISABLED); + } + + public static int getPauseOnBluetoothDevice(Context context) + { + SharedPreferences preferences = getPreferences(context); + return preferences.getInt(Constants.PREFERENCES_KEY_PAUSE_ON_BLUETOOTH_DEVICE, Constants.PREFERENCE_VALUE_A2DP); + } } diff --git a/ultrasonic/src/main/res/values-de/strings.xml b/ultrasonic/src/main/res/values-de/strings.xml index fa71dfd4..caf340dc 100644 --- a/ultrasonic/src/main/res/values-de/strings.xml +++ b/ultrasonic/src/main/res/values-de/strings.xml @@ -396,6 +396,12 @@ 12 Album Cover Mehrere Jahre + Resume when a Bluetooth device is connected + Pause when a Bluetooth device is disconnected + All Bluetooth devices + Only A2dp devices + Disabled + Ultrasonic can\'t access the music file cache. Cache location was reset to the default path. Warning Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value. diff --git a/ultrasonic/src/main/res/values-es/strings.xml b/ultrasonic/src/main/res/values-es/strings.xml index 6b001c61..68ed5111 100644 --- a/ultrasonic/src/main/res/values-es/strings.xml +++ b/ultrasonic/src/main/res/values-es/strings.xml @@ -397,6 +397,12 @@ 12 Caratula del Álbum Múltiples años + Resume when a Bluetooth device is connected + Pause when a Bluetooth device is disconnected + All Bluetooth devices + Only A2dp devices + Disabled + Ultrasonic no puede acceder a la caché de los ficheros de música. La ubicación de la caché se restableció a la ruta predeterminada. Atención Ultrasonic necesita permiso de lectura / escritura para el directorio caché de música. El directorio caché se restableció a su valor predeterminado. diff --git a/ultrasonic/src/main/res/values-fr/strings.xml b/ultrasonic/src/main/res/values-fr/strings.xml index 0d9f6962..fdfc39e5 100644 --- a/ultrasonic/src/main/res/values-fr/strings.xml +++ b/ultrasonic/src/main/res/values-fr/strings.xml @@ -397,6 +397,12 @@ 12 albumArt Multiple Years + Resume when a Bluetooth device is connected + Pause when a Bluetooth device is disconnected + All Bluetooth devices + Only A2dp devices + Disabled + Ultrasonic can\'t access the music file cache. Cache location was reset to the default path. Warning Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value. diff --git a/ultrasonic/src/main/res/values-hu/strings.xml b/ultrasonic/src/main/res/values-hu/strings.xml index 8147adaa..cfe49083 100644 --- a/ultrasonic/src/main/res/values-hu/strings.xml +++ b/ultrasonic/src/main/res/values-hu/strings.xml @@ -229,7 +229,7 @@ Korlátlan Max. bitráta - Wi-Fi kapcsolat Dalok max. találati száma - Telefon irányítása a bluetooth eszköz, vagy a fülhallgató vezérlőgombjaival. + Telefon irányítása a Bluetooth eszköz, vagy a fülhallgató vezérlőgombjaival. Média vezérlőgombok Hálózati időtúllépés 105 másodperc @@ -396,7 +396,13 @@ 11 12 albumArt - Multiple Years + Több év + Folytatás Bluetooth eszköz csatlakozásakor + Szünet Bluetooth eszköz kikapcsolásakor + Minden Bluetooth eszköz + Csak A2dp eszközök + Kikapcsolva + Az Ultrasonic nem éri el a zenei fájl gyorsítótárat. A gyorsítótár helye visszaállítva az alapbeállításra. Figyelem Az Ultrasonic működéséhez írás/olvasás hozzáférés szükséges a zenei fájl gyorsítótárhoz. A gyorsítótár helye visszaállítva az alapbeállításra. diff --git a/ultrasonic/src/main/res/values-nl/strings.xml b/ultrasonic/src/main/res/values-nl/strings.xml index e51f879b..97a43337 100644 --- a/ultrasonic/src/main/res/values-nl/strings.xml +++ b/ultrasonic/src/main/res/values-nl/strings.xml @@ -397,6 +397,12 @@ 12 Albumhoes Meerdere jaren + Resume when a Bluetooth device is connected + Pause when a Bluetooth device is disconnected + All Bluetooth devices + Only A2dp devices + Disabled + Ultrasonic can\'t access the music file cache. Cache location was reset to the default path. Warning Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value. diff --git a/ultrasonic/src/main/res/values-pl/strings.xml b/ultrasonic/src/main/res/values-pl/strings.xml index 2b857769..2769dd48 100644 --- a/ultrasonic/src/main/res/values-pl/strings.xml +++ b/ultrasonic/src/main/res/values-pl/strings.xml @@ -397,6 +397,12 @@ ponieważ api Subsonic nie wspiera nowego sposobu autoryzacji dla użytkowników 12 Okładka Z różnych lat + Resume when a Bluetooth device is connected + Pause when a Bluetooth device is disconnected + All Bluetooth devices + Only A2dp devices + Disabled + Ultrasonic can\'t access the music file cache. Cache location was reset to the default path. Warning Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value. diff --git a/ultrasonic/src/main/res/values-pt-rBR/strings.xml b/ultrasonic/src/main/res/values-pt-rBR/strings.xml index 61293cc1..92310141 100644 --- a/ultrasonic/src/main/res/values-pt-rBR/strings.xml +++ b/ultrasonic/src/main/res/values-pt-rBR/strings.xml @@ -397,6 +397,12 @@ 12 albumArt Múltiplos Anos + Resume when a Bluetooth device is connected + Pause when a Bluetooth device is disconnected + All Bluetooth devices + Only A2dp devices + Disabled + Ultrasonic can\'t access the music file cache. Cache location was reset to the default path. Warning Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value. diff --git a/ultrasonic/src/main/res/values-pt/strings.xml b/ultrasonic/src/main/res/values-pt/strings.xml index 4ef98706..43574ee8 100644 --- a/ultrasonic/src/main/res/values-pt/strings.xml +++ b/ultrasonic/src/main/res/values-pt/strings.xml @@ -397,6 +397,12 @@ 12 albumArt Múltiplos Anos + Resume when a Bluetooth device is connected + Pause when a Bluetooth device is disconnected + All Bluetooth devices + Only A2dp devices + Disabled + Ultrasonic can\'t access the music file cache. Cache location was reset to the default path. Warning Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value. diff --git a/ultrasonic/src/main/res/values/arrays.xml b/ultrasonic/src/main/res/values/arrays.xml index af24917f..fc178dea 100644 --- a/ultrasonic/src/main/res/values/arrays.xml +++ b/ultrasonic/src/main/res/values/arrays.xml @@ -291,5 +291,10 @@ 11 12 + + @string/settings.playback.bluetooth_all + @string/settings.playback.bluetooth_a2dp + @string/settings.playback.bluetooth_disabled + \ No newline at end of file diff --git a/ultrasonic/src/main/res/values/strings.xml b/ultrasonic/src/main/res/values/strings.xml index f01e8e34..6607b34c 100644 --- a/ultrasonic/src/main/res/values/strings.xml +++ b/ultrasonic/src/main/res/values/strings.xml @@ -400,9 +400,9 @@ albumArt Multiple Years http://example.com - Resume when a bluetooth device is connected - Pause when a bluetooth device is disconnected - All bluetooth devices + Resume when a Bluetooth device is connected + Pause when a Bluetooth device is disconnected + All Bluetooth devices Only A2dp devices Disabled