Use a foreground service for the sensor worker to make updates more reliable (#854)

This commit is contained in:
Daniel Shokouhi 2020-09-01 21:44:13 -07:00 committed by GitHub
parent 2ec8abd1b3
commit 68b8987704
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View file

@ -1,14 +1,21 @@
package io.homeassistant.companion.android.sensors
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ForegroundInfo
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
import io.homeassistant.companion.android.domain.integration.IntegrationUseCase
import java.util.concurrent.TimeUnit
@ -22,10 +29,15 @@ class SensorWorker(
) : CoroutineWorker(appContext, workerParams) {
companion object {
private const val TAG = "SensorWorker"
const val channelId = "Sensor Worker"
const val NOTIFICATION_ID = 42
var notificationText = ""
fun start(context: Context) {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED).build()
notificationText = context.getString(R.string.updating_sensors)
val sensorWorker =
PeriodicWorkRequestBuilder<SensorWorker>(15, TimeUnit.MINUTES)
.setConstraints(constraints)
@ -38,6 +50,8 @@ class SensorWorker(
@Inject
lateinit var integrationUseCase: IntegrationUseCase
private val notificationManager = appContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
init {
DaggerSensorComponent.builder()
.appComponent((appContext.applicationContext as GraphComponentAccessor).appComponent)
@ -47,7 +61,30 @@ class SensorWorker(
override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
Log.d(TAG, "Updating all Sensors.")
createNotificationChannel()
val notification = NotificationCompat.Builder(applicationContext, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(notificationText)
.setPriority(NotificationCompat.PRIORITY_LOW)
.build()
val foregroundInfo = ForegroundInfo(NOTIFICATION_ID, notification)
setForeground(foregroundInfo)
SensorReceiver().updateSensors(appContext, integrationUseCase)
Result.success()
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var notificationChannel =
notificationManager?.getNotificationChannel(channelId)
if (notificationChannel == null) {
notificationChannel = NotificationChannel(
channelId, TAG, NotificationManager.IMPORTANCE_LOW
)
notificationManager?.createNotificationChannel(notificationChannel)
}
}
}
}

View file

@ -185,6 +185,7 @@ like to connect to:</string>
<string name="themes_title_settings">Theme</string>
<string name="unable_to_register">Unable to Register Application</string>
<string name="unique_id">Unique Id</string>
<string name="updating_sensors">Updating Sensors</string>
<string name="url_invalid">Url Invalid</string>
<string name="url_parse_error">Unable to parse your Home Assistant URL. It should look like https://example.com</string>
<string name="username">Username</string>