From bd454b6d3106b81c4b9b08aac66f4765c312519a Mon Sep 17 00:00:00 2001 From: TheLastGimbus Date: Sat, 23 Sep 2023 20:12:47 +0200 Subject: [PATCH] manually kick-off our periodic update if any audio-video device connects --- android/app/build.gradle | 4 ++ android/app/src/main/AndroidManifest.xml | 9 +++ .../BluetoothDeviceConnectedReceiver.kt | 70 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 android/app/src/main/kotlin/com/lastgimbus/the/freebuddy/BluetoothDeviceConnectedReceiver.kt diff --git a/android/app/build.gradle b/android/app/build.gradle index 4d6db5e..b715b84 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -108,4 +108,8 @@ dependencies { implementation "androidx.glance:glance-appwidget:1.0.0" implementation "androidx.glance:glance-material3:1.0.0" implementation 'androidx.compose.ui:ui-unit-android:1.5.1' + + // this is to manually launch our routine update - same version as workmanager plugin 0.5.1 + def work_version = "2.7.1" + implementation "androidx.work:work-runtime:$work_version" } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 939a7f1..b979197 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -58,5 +58,14 @@ android:resource="@xml/battery_widget_config" /> + + + + + + diff --git a/android/app/src/main/kotlin/com/lastgimbus/the/freebuddy/BluetoothDeviceConnectedReceiver.kt b/android/app/src/main/kotlin/com/lastgimbus/the/freebuddy/BluetoothDeviceConnectedReceiver.kt new file mode 100644 index 0000000..cab4a03 --- /dev/null +++ b/android/app/src/main/kotlin/com/lastgimbus/the/freebuddy/BluetoothDeviceConnectedReceiver.kt @@ -0,0 +1,70 @@ +package com.lastgimbus.the.freebuddy + +import android.Manifest +import android.bluetooth.BluetoothClass +import android.bluetooth.BluetoothDevice +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Build +import android.util.Log +import androidx.core.app.ActivityCompat +import androidx.work.Data +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager +import be.tramckrijte.workmanager.BackgroundWorker + +/** + * This reacts to a new bluetooth device being connected (literally any) + * + * That's why it then filters out to only AUDIO_VIDEO devices, and (currently): + * - launches one-off workmanager work to update the widget + */ +class BluetoothDeviceConnectedReceiver : BroadcastReceiver() { + companion object { + const val TAG = "BtDevConnReceiver" + const val TASK_ID_ROUTINE_UPDATE = "freebuddy.routine_update" + } + + override fun onReceive(context: Context, intent: Intent) { + when (intent.action) { + BluetoothDevice.ACTION_ACL_CONNECTED -> { + val device = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java) + } else { + intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) + } + if (device == null) { + Log.wtf(TAG, "device is null!!") + return + } + Log.d(TAG, "Connected to dev: $device ; Class: ${device.bluetoothClass.majorDeviceClass}") + if (ActivityCompat.checkSelfPermission( + context, + Manifest.permission.BLUETOOTH_CONNECT + ) != PackageManager.PERMISSION_GRANTED + ) { + Log.i(TAG, "No BLUETOOTH_CONNECT permission :(") + return + } + if (device.bluetoothClass.majorDeviceClass != BluetoothClass.Device.Major.AUDIO_VIDEO + ) { + Log.v(TAG, "$device is not AUDIO_VIDEO, skipping...") + return + } + Log.i(TAG, "Scheduling one time work to update widget n stuff...") + // this is stuff imported from be.tramckrijte.workmanager + val oneOffTaskRequest = OneTimeWorkRequest.Builder(BackgroundWorker::class.java) + .setInputData( + Data.Builder() + .putString(BackgroundWorker.DART_TASK_KEY, TASK_ID_ROUTINE_UPDATE) + .putBoolean(BackgroundWorker.IS_IN_DEBUG_MODE_KEY, false) + .build() + ) + .build() + WorkManager.getInstance(context).enqueue(oneOffTaskRequest) + } + } + } +} \ No newline at end of file