Skip to content

Commit

Permalink
manually kick-off our periodic update if any audio-video device connects
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastGimbus committed Sep 23, 2023
1 parent 369d75d commit bd454b6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
9 changes: 9 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,14 @@
android:resource="@xml/battery_widget_config" />
</receiver>

<receiver
android:name=".BluetoothDeviceConnectedReceiver"
android:permission="android.permission.BLUETOOTH_CONNECT"
android:exported="true">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
</intent-filter>
</receiver>

</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}

0 comments on commit bd454b6

Please sign in to comment.