Skip to content

Commit

Permalink
only trigger check when audio-bluetooth devices disconnect
Browse files Browse the repository at this point in the history
(fixes watch muting the device while content is playing)
  • Loading branch information
newhinton committed Jun 23, 2024
1 parent 44ef6f7 commit b5c6cdd
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.felixnuesse.timedsilence.receiver

import android.bluetooth.BluetoothClass.Device.Major.AUDIO_VIDEO
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
Expand All @@ -23,7 +24,7 @@ class BluetoothBroadcastReciever : BroadcastReceiver(){
Log.e(TAG(), "BluetoothBroadcastReciever: Device connected!")

val bluetoothDevice: BluetoothDevice? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE,BluetoothDevice::class.java)
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
} else {
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
}
Expand All @@ -48,17 +49,34 @@ class BluetoothBroadcastReciever : BroadcastReceiver(){
}

if (intent.action == BluetoothDevice.ACTION_ACL_DISCONNECTED) {
Log.e(TAG(), "BluetoothBroadcastReciever: Device disconnected!")

val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) as BluetoothDevice?
val name = if (device != null) {
device.name
} else {
"Unknown!"
}

Log.e(TAG(), "BluetoothBroadcastReciever: Device disconnected: $name!")

var isAudioDevice = false
if(device?.bluetoothClass?.majorDeviceClass == AUDIO_VIDEO) {
isAudioDevice = true
}

try {
// Why do we sleep here? So that the device is gone when we check?
Thread.sleep(1000)
} catch (ex: InterruptedException) {
Log.e(TAG(), "BluetoothBroadcastReciever: Could not sleep!")
}

if(Trigger(context).checkIfNextAlarmExists()){
var volumeHandler = VolumeHandler(context)
volumeHandler.ignoreMusicPlaying(true)
// is this what we want? Override existing playing content?
// yes, but only if the device we are loosing are headphones.
// Todo: Check if headphones disconnected!
volumeHandler.ignoreMusicPlaying(isAudioDevice)
volumeHandler.setVolumeStateAndApply(StateGenerator(context).stateAt(System.currentTimeMillis()))
} else {
Log.e(TAG(), "BluetoothBroadcastReciever: No next alarm scheduled, don't update!")
Expand Down

0 comments on commit b5c6cdd

Please sign in to comment.