Added resume and pause settings for bluetooth device connection / disconnection

This commit is contained in:
Nite 2020-09-28 17:12:36 +02:00
parent 046916221d
commit c805005b02
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
15 changed files with 190 additions and 21 deletions

View File

@ -146,7 +146,7 @@
<action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/>
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED"/>
<action android:name="android.bluetooth.a2dp.action.SINK_STATE_CHANGED"/>
<action android:name="android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED"/>
</intent-filter>
</receiver>
<receiver

View File

@ -1,5 +1,7 @@
package org.moire.ultrasonic.fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
@ -7,6 +9,8 @@ import android.os.Bundle;
import android.preference.*;
import android.provider.SearchRecentSuggestions;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import android.util.Log;
import android.view.View;
@ -19,6 +23,7 @@ import org.moire.ultrasonic.featureflags.FeatureStorage;
import org.moire.ultrasonic.filepicker.FilePickerDialog;
import org.moire.ultrasonic.filepicker.OnFileSelectedListener;
import org.moire.ultrasonic.provider.SearchSuggestionProvider;
import org.moire.ultrasonic.service.Consumer;
import org.moire.ultrasonic.service.MediaPlayerController;
import org.moire.ultrasonic.util.*;
@ -120,6 +125,7 @@ public class SettingsFragment extends PreferenceFragment
setupGaplessControlSettingsV14();
setupFeatureFlagsPreferences();
setupCacheLocationPreference();
setupBluetoothDevicePreferences();
// After API26 foreground services must be used for music playback, and they must have a notification
if (Build.VERSION.SDK_INT >= 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<Integer>() {
@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<Integer>() {
@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<Integer> 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() {

View File

@ -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));

View File

@ -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;

View File

@ -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);
}
}

View File

@ -396,6 +396,12 @@
<string name="settings.image_loader_concurrency_12">12</string>
<string name="albumArt">Album Cover</string>
<string name="common_multiple_years">Mehrere Jahre</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a Bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a Bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All Bluetooth devices</string>
<string name="settings.playback.bluetooth_a2dp">Only A2dp devices</string>
<string name="settings.playback.bluetooth_disabled">Disabled</string>
<string name="permissions.access_error">Ultrasonic can\'t access the music file cache. Cache location was reset to the default path.</string>
<string name="permissions.message_box_title">Warning</string>
<string name="permissions.permission_missing">Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value.</string>

View File

@ -397,6 +397,12 @@
<string name="settings.image_loader_concurrency_12">12</string>
<string name="albumArt">Caratula del Álbum</string>
<string name="common_multiple_years">Múltiples años</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a Bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a Bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All Bluetooth devices</string>
<string name="settings.playback.bluetooth_a2dp">Only A2dp devices</string>
<string name="settings.playback.bluetooth_disabled">Disabled</string>
<string name="permissions.access_error">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.</string>
<string name="permissions.message_box_title">Atención</string>
<string name="permissions.permission_missing">Ultrasonic necesita permiso de lectura / escritura para el directorio caché de música. El directorio caché se restableció a su valor predeterminado.</string>

View File

@ -397,6 +397,12 @@
<string name="settings.image_loader_concurrency_12">12</string>
<string name="albumArt">albumArt</string>
<string name="common_multiple_years">Multiple Years</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a Bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a Bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All Bluetooth devices</string>
<string name="settings.playback.bluetooth_a2dp">Only A2dp devices</string>
<string name="settings.playback.bluetooth_disabled">Disabled</string>
<string name="permissions.access_error">Ultrasonic can\'t access the music file cache. Cache location was reset to the default path.</string>
<string name="permissions.message_box_title">Warning</string>
<string name="permissions.permission_missing">Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value.</string>

View File

@ -229,7 +229,7 @@
<string name="settings.max_bitrate_unlimited">Korlátlan</string>
<string name="settings.max_bitrate_wifi">Max. bitráta - Wi-Fi kapcsolat</string>
<string name="settings.max_songs">Dalok max. találati száma</string>
<string name="settings.media_button_summary">Telefon irányítása a bluetooth eszköz, vagy a fülhallgató vezérlőgombjaival.</string>
<string name="settings.media_button_summary">Telefon irányítása a Bluetooth eszköz, vagy a fülhallgató vezérlőgombjaival.</string>
<string name="settings.media_button_title">Média vezérlőgombok</string>
<string name="settings.network_timeout">Hálózati időtúllépés</string>
<string name="settings.network_timeout_105000">105 másodperc</string>
@ -396,7 +396,13 @@
<string name="settings.image_loader_concurrency_11">11</string>
<string name="settings.image_loader_concurrency_12">12</string>
<string name="albumArt">albumArt</string>
<string name="common_multiple_years">Multiple Years</string>
<string name="common_multiple_years">Több év</string>
<string name="settings.playback.resume_on_bluetooth_device">Folytatás Bluetooth eszköz csatlakozásakor</string>
<string name="settings.playback.pause_on_bluetooth_device">Szünet Bluetooth eszköz kikapcsolásakor</string>
<string name="settings.playback.bluetooth_all">Minden Bluetooth eszköz</string>
<string name="settings.playback.bluetooth_a2dp">Csak A2dp eszközök</string>
<string name="settings.playback.bluetooth_disabled">Kikapcsolva</string>
<string name="permissions.access_error">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.</string>
<string name="permissions.message_box_title">Figyelem</string>
<string name="permissions.permission_missing">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.</string>

View File

@ -397,6 +397,12 @@
<string name="settings.image_loader_concurrency_12">12</string>
<string name="albumArt">Albumhoes</string>
<string name="common_multiple_years">Meerdere jaren</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a Bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a Bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All Bluetooth devices</string>
<string name="settings.playback.bluetooth_a2dp">Only A2dp devices</string>
<string name="settings.playback.bluetooth_disabled">Disabled</string>
<string name="permissions.access_error">Ultrasonic can\'t access the music file cache. Cache location was reset to the default path.</string>
<string name="permissions.message_box_title">Warning</string>
<string name="permissions.permission_missing">Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value.</string>

View File

@ -397,6 +397,12 @@ ponieważ api Subsonic nie wspiera nowego sposobu autoryzacji dla użytkowników
<string name="settings.image_loader_concurrency_12">12</string>
<string name="albumArt">Okładka</string>
<string name="common_multiple_years">Z różnych lat</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a Bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a Bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All Bluetooth devices</string>
<string name="settings.playback.bluetooth_a2dp">Only A2dp devices</string>
<string name="settings.playback.bluetooth_disabled">Disabled</string>
<string name="permissions.access_error">Ultrasonic can\'t access the music file cache. Cache location was reset to the default path.</string>
<string name="permissions.message_box_title">Warning</string>
<string name="permissions.permission_missing">Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value.</string>

View File

@ -397,6 +397,12 @@
<string name="settings.image_loader_concurrency_12">12</string>
<string name="albumArt">albumArt</string>
<string name="common_multiple_years">Múltiplos Anos</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a Bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a Bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All Bluetooth devices</string>
<string name="settings.playback.bluetooth_a2dp">Only A2dp devices</string>
<string name="settings.playback.bluetooth_disabled">Disabled</string>
<string name="permissions.access_error">Ultrasonic can\'t access the music file cache. Cache location was reset to the default path.</string>
<string name="permissions.message_box_title">Warning</string>
<string name="permissions.permission_missing">Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value.</string>

View File

@ -397,6 +397,12 @@
<string name="settings.image_loader_concurrency_12">12</string>
<string name="albumArt">albumArt</string>
<string name="common_multiple_years">Múltiplos Anos</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a Bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a Bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All Bluetooth devices</string>
<string name="settings.playback.bluetooth_a2dp">Only A2dp devices</string>
<string name="settings.playback.bluetooth_disabled">Disabled</string>
<string name="permissions.access_error">Ultrasonic can\'t access the music file cache. Cache location was reset to the default path.</string>
<string name="permissions.message_box_title">Warning</string>
<string name="permissions.permission_missing">Ultrasonic needs read/write permission to the music cache directory. Cache directory was reset to its default value.</string>

View File

@ -291,5 +291,10 @@
<item>11</item>
<item>12</item>
</string-array>
<string-array name="bluetoothDeviceSettingNames" translatable="false">
<item>@string/settings.playback.bluetooth_all</item>
<item>@string/settings.playback.bluetooth_a2dp</item>
<item>@string/settings.playback.bluetooth_disabled</item>
</string-array>
</resources>

View File

@ -400,9 +400,9 @@
<string name="albumArt">albumArt</string>
<string name="common_multiple_years">Multiple Years</string>
<string name="settings.server_address_unset" translatable="false">http://example.com</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All bluetooth devices</string>
<string name="settings.playback.resume_on_bluetooth_device">Resume when a Bluetooth device is connected</string>
<string name="settings.playback.pause_on_bluetooth_device">Pause when a Bluetooth device is disconnected</string>
<string name="settings.playback.bluetooth_all">All Bluetooth devices</string>
<string name="settings.playback.bluetooth_a2dp">Only A2dp devices</string>
<string name="settings.playback.bluetooth_disabled">Disabled</string>