@@ -7,8 +7,13 @@ import android.content.pm.ServiceInfo
77import android.os.Build
88import androidx.core.app.NotificationCompat
99import androidx.core.content.getSystemService
10+ import androidx.work.Constraints
1011import androidx.work.CoroutineWorker
12+ import androidx.work.ExistingPeriodicWorkPolicy
1113import androidx.work.ForegroundInfo
14+ import androidx.work.NetworkType
15+ import androidx.work.PeriodicWorkRequestBuilder
16+ import androidx.work.WorkManager
1217import androidx.work.WorkerParameters
1318import dagger.hilt.EntryPoint
1419import dagger.hilt.InstallIn
@@ -19,13 +24,13 @@ import io.homeassistant.companion.android.common.data.servers.ServerManager
1924import io.homeassistant.companion.android.common.util.CHANNEL_SENSOR_WORKER
2025import io.homeassistant.companion.android.common.util.CheckLocalNetworkPermissionUseCase
2126import io.homeassistant.companion.android.common.util.SdkVersion
27+ import java.util.concurrent.TimeUnit
2228import kotlinx.coroutines.Dispatchers
2329import kotlinx.coroutines.withContext
2430import timber.log.Timber
2531
26- abstract class SensorWorkerBase (val appContext : Context , workerParams : WorkerParameters ) :
27- CoroutineWorker (appContext, workerParams) {
28- protected abstract val sensorReceiver: SensorReceiverBase
32+ class SensorWorker (private val context : Context , workerParams : WorkerParameters ) :
33+ CoroutineWorker (context, workerParams) {
2934
3035 @EntryPoint
3136 @InstallIn(SingletonComponent ::class )
@@ -34,21 +39,36 @@ abstract class SensorWorkerBase(val appContext: Context, workerParams: WorkerPar
3439 fun checkLocalNetworkPermission (): CheckLocalNetworkPermissionUseCase
3540 fun lastUpdateManager (): LastUpdateManager
3641 fun sensorRepository (): SensorRepository
42+ fun sensorUpdater (): SensorUpdater
3743 }
3844
3945 companion object {
40- const val TAG = " SensorWorker"
41- const val NOTIFICATION_ID = 42
46+ private const val TAG = " SensorWorker"
47+ private const val NOTIFICATION_ID = 42
48+
49+ fun start (context : Context ) {
50+ val constraints = Constraints .Builder ()
51+ .setRequiredNetworkType(NetworkType .CONNECTED ).build()
52+
53+ val sensorWorker =
54+ PeriodicWorkRequestBuilder <SensorWorker >(15 , TimeUnit .MINUTES )
55+ .setConstraints(constraints)
56+ .build()
57+
58+ WorkManager .getInstance(context)
59+ .enqueueUniquePeriodicWork(TAG , ExistingPeriodicWorkPolicy .CANCEL_AND_REENQUEUE , sensorWorker)
60+ }
4261 }
4362
44- private val notificationManager = appContext .getSystemService<NotificationManager >()!!
63+ private val notificationManager = context .getSystemService<NotificationManager >()!!
4564
4665 override suspend fun doWork (): Result = withContext(Dispatchers .IO ) {
47- val entryPoint = EntryPointAccessors .fromApplication(appContext , SensorWorkerEntryPoint ::class .java)
66+ val entryPoint = EntryPointAccessors .fromApplication(context , SensorWorkerEntryPoint ::class .java)
4867 val sensorRepository = entryPoint.sensorRepository()
4968 val serverManager = entryPoint.serverManager()
5069 val checkLocalNetworkPermission = entryPoint.checkLocalNetworkPermission()
5170 val lastUpdateManager = entryPoint.lastUpdateManager()
71+ val sensorUpdater = entryPoint.sensorUpdater()
5272
5373 val enabledSensorCount = sensorRepository.getEnabledCount()
5474 if (
@@ -64,7 +84,7 @@ abstract class SensorWorkerBase(val appContext: Context, workerParams: WorkerPar
6484 createNotificationChannel()
6585 val notification = NotificationCompat .Builder (applicationContext, CHANNEL_SENSOR_WORKER )
6686 .setSmallIcon(commonR.drawable.ic_stat_ic_notification)
67- .setContentTitle(appContext .getString(commonR.string.updating_sensors))
87+ .setContentTitle(context .getString(commonR.string.updating_sensors))
6888 .setPriority(NotificationCompat .PRIORITY_LOW )
6989 .build()
7090
@@ -91,7 +111,7 @@ abstract class SensorWorkerBase(val appContext: Context, workerParams: WorkerPar
91111 if (lastUpdateSensor.any { it.enabled }) {
92112 lastUpdateManager.sendLastUpdate(TAG )
93113 }
94- sensorReceiver .updateSensors(appContext, serverManager, sensorRepository, null )
114+ sensorUpdater .updateSensors()
95115 }
96116
97117 // Cleanup orphaned sensors that may have been created by a slow or long running update
@@ -102,11 +122,11 @@ abstract class SensorWorkerBase(val appContext: Context, workerParams: WorkerPar
102122 Result .success()
103123 }
104124
105- protected fun createNotificationChannel () {
125+ private fun createNotificationChannel () {
106126 if (SdkVersion .isAtLeast(Build .VERSION_CODES .O )) {
107127 val notificationChannel = NotificationChannel (
108128 CHANNEL_SENSOR_WORKER ,
109- appContext .getString(commonR.string.sensor_updates),
129+ context .getString(commonR.string.sensor_updates),
110130 NotificationManager .IMPORTANCE_LOW ,
111131 )
112132 notificationManager.createNotificationChannel(notificationChannel)
0 commit comments