Implement bluetooth autospawn / autoplayback feature #235

This commit is contained in:
Markus Ebner
2022-09-08 18:16:41 +02:00
parent 574224ff99
commit d0f9b049de
9 changed files with 79 additions and 0 deletions
@@ -23,6 +23,7 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.ActivityCompat
import androidx.core.view.WindowCompat
import androidx.core.view.updatePadding
import org.oxycblt.auxio.databinding.ActivityMainBinding
@@ -73,6 +74,18 @@ class MainActivity : AppCompatActivity() {
if (!startIntentAction(intent)) {
playbackModel.startAction(InternalPlayer.Action.RestoreState)
}
// Check bluetooth connect permissions if required for bt autoconnect feature
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val settings = Settings(this)
if (settings.bluetoothAutoplay) {
ActivityCompat.requestPermissions(
this,
arrayOf<String>(android.Manifest.permission.BLUETOOTH_CONNECT),
BLUETOOTH_PERMISSION_REQUEST_ID
)
}
}
}
override fun onNewIntent(intent: Intent?) {
@@ -141,5 +154,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;
}
}
@@ -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)
}
}
}
}
}
@@ -135,6 +135,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() =
@@ -17,6 +17,8 @@
package org.oxycblt.auxio.settings
import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.DrawableRes
@@ -44,6 +46,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].