Faster 'Detected Activity' sensor while updating fast (#2420)

- Request an update for the detected activity sensor every minute if the app is currently doing faster sensor updates, instead of every 2 minutes, to try to have an up-to-date recognition value every time the data is sent.
 - Fix inconsistent update behaviour after changing the setting but before restarting when registered for the ACTION_TIME_TICK intent by always checking the setting, and not only when set to 'fast while charging'.
This commit is contained in:
Joris Pelgröm 2022-04-03 14:28:27 +02:00 committed by GitHub
parent aaef43ae38
commit c2f0293837
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View file

@ -15,6 +15,8 @@ import com.google.android.gms.location.SleepSegmentEvent
import com.google.android.gms.location.SleepSegmentRequest
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.sensors.SensorReceiverBase
import java.util.concurrent.TimeUnit
import io.homeassistant.companion.android.common.R as commonR
@AndroidEntryPoint
@ -216,7 +218,8 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
actReg.removeActivityUpdates(pendingIntent)
Log.d(TAG, "Registering for activity updates.")
actReg.requestActivityUpdates(120000, pendingIntent)
val fastUpdate = SensorReceiverBase.shouldDoFastUpdates(context)
actReg.requestActivityUpdates(TimeUnit.MINUTES.toMillis(if (fastUpdate) 1 else 2), pendingIntent)
}
if ((
isEnabled(context, sleepConfidence.id) || isEnabled(

View file

@ -19,6 +19,21 @@ import java.util.Locale
import javax.inject.Inject
abstract class SensorReceiverBase : BroadcastReceiver() {
companion object {
fun shouldDoFastUpdates(context: Context): Boolean {
val settingDao = AppDatabase.getInstance(context).settingsDao().get(0)
return when (settingDao?.sensorUpdateFrequency) {
SensorUpdateFrequencySetting.FAST_ALWAYS -> true
SensorUpdateFrequencySetting.FAST_WHILE_CHARGING -> {
val batteryStatusIntent =
context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
return batteryStatusIntent?.let { BatterySensorManager.getIsCharging(it) } ?: false
}
else -> false
}
}
}
private val ioScope: CoroutineScope = CoroutineScope(Dispatchers.IO + Job())
protected abstract val tag: String
@ -84,14 +99,8 @@ abstract class SensorReceiverBase : BroadcastReceiver() {
}
ioScope.launch {
val settingDao = AppDatabase.getInstance(context).settingsDao().get(0)
val batteryStatusIntent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
val isCharging = batteryStatusIntent?.let { BatterySensorManager.getIsCharging(it) }
if (isCharging != true && settingDao != null &&
settingDao.sensorUpdateFrequency == SensorUpdateFrequencySetting.FAST_WHILE_CHARGING &&
intent.action == Intent.ACTION_TIME_TICK
) {
Log.i(tag, "Skipping faster update as device is not charging")
if (intent.action == Intent.ACTION_TIME_TICK && !shouldDoFastUpdates(context)) {
Log.i(tag, "Skipping faster update because not charging/different preference")
return@launch
}
updateSensors(context, integrationUseCase, intent)