diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7f31072e4..32a1a60ac 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,11 @@ + + + + + + + + + + + + = Build.VERSION_CODES.S) { + val settings = Settings(this) + if (settings.bluetoothAutoplay) { + ActivityCompat.requestPermissions( + this, + arrayOf(android.Manifest.permission.BLUETOOTH_CONNECT), + BLUETOOTH_PERMISSION_REQUEST_ID + ) + } + } } override fun onNewIntent(intent: Intent?) { @@ -132,5 +145,6 @@ class MainActivity : AppCompatActivity() { companion object { private const val KEY_INTENT_USED = BuildConfig.APPLICATION_ID + ".key.FILE_INTENT_USED" + private const val BLUETOOTH_PERMISSION_REQUEST_ID = 1337 * 42; } } diff --git a/app/src/main/java/org/oxycblt/auxio/playback/system/BluetoothConnectReceiver.kt b/app/src/main/java/org/oxycblt/auxio/playback/system/BluetoothConnectReceiver.kt new file mode 100644 index 000000000..0e33563cb --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/playback/system/BluetoothConnectReceiver.kt @@ -0,0 +1,33 @@ +package org.oxycblt.auxio.playback.system + +import android.bluetooth.BluetoothProfile +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.os.Build +import org.oxycblt.auxio.playback.state.InternalPlayer +import org.oxycblt.auxio.playback.state.PlaybackStateManager +import org.oxycblt.auxio.settings.Settings + +class BluetoothConnectReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + if (intent.action == android.bluetooth.BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED) { + val newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED) + if (newState == BluetoothProfile.STATE_CONNECTED) { + val settings = Settings(context) + if (settings.bluetoothAutoplay) { + // make sure required services are up and running + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(Intent(context, PlaybackService::class.java)) + } else { + context.startService(Intent(context, PlaybackService::class.java)) + } + // start playback + val playbackManager = PlaybackStateManager.getInstance() + playbackManager.startAction(InternalPlayer.Action.RestoreState) + playbackManager.changePlaying(true) + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/oxycblt/auxio/settings/Settings.kt b/app/src/main/java/org/oxycblt/auxio/settings/Settings.kt index d9e9072b2..234ac159f 100644 --- a/app/src/main/java/org/oxycblt/auxio/settings/Settings.kt +++ b/app/src/main/java/org/oxycblt/auxio/settings/Settings.kt @@ -230,6 +230,10 @@ class Settings(private val context: Context, private val callback: Callback? = n val headsetAutoplay: Boolean get() = inner.getBoolean(context.getString(R.string.set_key_headset_autoplay), false) + /** Whether a connected bluetooth device should cause Auxio to spawn and start playback */ + val bluetoothAutoplay: Boolean + get() = inner.getBoolean(context.getString(R.string.set_key_bluetooth_autoplay), false) + /** The current ReplayGain configuration */ val replayGainMode: ReplayGainMode get() = diff --git a/app/src/main/java/org/oxycblt/auxio/settings/prefs/PreferenceFragment.kt b/app/src/main/java/org/oxycblt/auxio/settings/prefs/PreferenceFragment.kt index 690448e5a..a6bc91045 100644 --- a/app/src/main/java/org/oxycblt/auxio/settings/prefs/PreferenceFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/settings/prefs/PreferenceFragment.kt @@ -17,6 +17,8 @@ package org.oxycblt.auxio.settings.prefs +import android.app.Activity +import android.os.Build import android.os.Bundle import android.view.View import androidx.annotation.DrawableRes @@ -41,6 +43,8 @@ import org.oxycblt.auxio.util.isNight import org.oxycblt.auxio.util.logD import org.oxycblt.auxio.util.showToast import org.oxycblt.auxio.util.systemBarInsetsCompat +import java.security.Permission +import java.util.jar.Manifest /** * The actual fragment containing the settings menu. Inherits [PreferenceFragmentCompat]. diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 67b2ae991..f0a83afbf 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -49,6 +49,8 @@ Audio Kopfhörer: automatische Wiedergabe Beginne die Wiedergabe immer, wenn Kopfhörer verbunden sind (funktioniert nicht auf allen Geräten) + Bluetooth: automatische Wiedergabe + Auxio starten und Wiedergabe fortführen, sobald ein Bluetooth Audio fähiges Gerät verbunden wurde ReplayGain-Strategie ReplayGain-Prälautverstärkung Während der Musikwiedergabe, trifft die Prälautverstärkung dem aktuellem Abgleich zu diff --git a/app/src/main/res/values/settings.xml b/app/src/main/res/values/settings.xml index ddf4e68da..dbb927cf6 100644 --- a/app/src/main/res/values/settings.xml +++ b/app/src/main/res/values/settings.xml @@ -14,6 +14,7 @@ auxio_separators auxio_headset_autoplay + auxio_bluetooth_autoplay auxio_replay_gain auxio_pre_amp auxio_pre_amp_with diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6c31654e1..0c21f5004 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -186,6 +186,8 @@ Audio Headset autoplay Always start playing when a headset is connected (may not work on all devices) + Bluetooth autoplay + A connected bluetooth audio device causes Auxio to spawn and start playback ReplayGain strategy Prefer track Prefer album