Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new settings screen to change Sensor Update Frequency #2317

Merged
merged 11 commits into from
Mar 4, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import io.homeassistant.companion.android.common.R
import io.homeassistant.companion.android.common.sensors.BatterySensorManager
import io.homeassistant.companion.android.common.sensors.SensorWorkerBase
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.settings.SensorUpdateFrequencySetting
import io.homeassistant.companion.android.util.compose.InfoNotification
import io.homeassistant.companion.android.util.compose.RadioButtonRow
Expand All @@ -28,7 +25,6 @@ fun SensorUpdateFrequencyView(
onSettingChanged: (SensorUpdateFrequencySetting) -> Unit
) {
val scrollState = rememberScrollState()
val context = LocalContext.current
Column(
modifier = Modifier
.padding(20.dp)
Expand All @@ -46,25 +42,10 @@ fun SensorUpdateFrequencyView(
selected = sensorUpdateFrequency == SensorUpdateFrequencySetting.NORMAL,
onClick = { onSettingChanged(SensorUpdateFrequencySetting.NORMAL) }
)
val isCharging = AppDatabase.getInstance(context).sensorDao().get(BatterySensorManager.isChargingState.id)
val sensorStatus = isCharging != null && isCharging.enabled
RadioButtonRow(
text = stringResource(
when {
(sensorStatus) -> R.string.sensor_update_frequency_fast_charging
(sensorUpdateFrequency == SensorUpdateFrequencySetting.FAST_WHILE_CHARGING && !sensorStatus) ->
R.string.sensor_update_frequency_fast_charging_sensor_disabled_selected
(sensorUpdateFrequency != SensorUpdateFrequencySetting.FAST_WHILE_CHARGING && !sensorStatus) ->
R.string.sensor_update_frequency_fast_charging_sensor_disabled_unselected
else -> R.string.sensor_update_frequency_fast_charging
}
),
text = stringResource(R.string.sensor_update_frequency_fast_charging),
selected = sensorUpdateFrequency == SensorUpdateFrequencySetting.FAST_WHILE_CHARGING,
onClick = {
if (sensorStatus)
onSettingChanged(SensorUpdateFrequencySetting.FAST_WHILE_CHARGING)
},
enabled = sensorStatus
onClick = { onSettingChanged(SensorUpdateFrequencySetting.FAST_WHILE_CHARGING) }
)
RadioButtonRow(
text = stringResource(R.string.sensor_update_frequency_fast_always),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.compose.ui.unit.dp
fun RadioButtonRow(
text: String,
selected: Boolean,
enabled: Boolean = true,
onClick: () -> Unit
) {
Row(
Expand All @@ -26,7 +25,7 @@ fun RadioButtonRow(
.padding(top = 20.dp, bottom = 20.dp),
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(selected = selected, onClick = onClick, enabled = enabled)
RadioButton(selected = selected, onClick = onClick)
Text(text)
}
Divider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class BatterySensorManager : SensorManager {
stateClass = SensorManager.STATE_CLASS_MEASUREMENT,
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC
)

fun getIsCharging(intent: Intent): Boolean {
val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)

return status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL
}
}

override fun docsLink(): String {
Expand Down Expand Up @@ -223,13 +230,6 @@ class BatterySensorManager : SensorManager {
)
}

private fun getIsCharging(intent: Intent): Boolean {
val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)

return status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL
}

private fun getChargerType(intent: Intent): String {
return when (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) {
BatteryManager.BATTERY_PLUGGED_AC -> "ac"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.homeassistant.companion.android.common.sensors
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Configuration
import android.util.Log
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
Expand Down Expand Up @@ -83,11 +84,10 @@ abstract class SensorReceiverBase : BroadcastReceiver() {
}

ioScope.launch {
val sensorDao = AppDatabase.getInstance(context).sensorDao()
val isChargingSensor = sensorDao.get(BatterySensorManager.isChargingState.id)
val settingDao = AppDatabase.getInstance(context).settingsDao().get(0)
if (isChargingSensor != null && isChargingSensor.state == "false" &&
isChargingSensor.enabled && settingDao != null &&
val batteryStatusIntent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
val isCharging = batteryStatusIntent?.let { BatterySensorManager.getIsCharging(it) }
if (isCharging == false && settingDao != null &&
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
settingDao.sensorUpdateFrequency == SensorUpdateFrequencySetting.FAST_WHILE_CHARGING &&
intent.action == Intent.ACTION_TIME_TICK
) {
Expand Down
4 changes: 1 addition & 3 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,7 @@
<string name="sensor_update_frequency">Sensor Update Frequency</string>
<string name="sensor_update_frequency_description">Sensors will update either instantly or on a defined interval. If the sensor supports instant updates then it will always receive instant updates. Click on the help icon at the top right hand corner to learn which sensors update instantly.\n\nIf the sensor does not support instant updates then it will update based on one of the below selected options.\n\nYou must restart the application when you make any changes to this setting.</string>
<string name="sensor_update_frequency_normal">Normal\n\nSensors will update on a 15 minute interval.</string>
<string name="sensor_update_frequency_fast_charging">Fast While Charging\n\nSensors will update every minute while the device is charging. When not charging sensors will update every 15 minutes. This option requires the Is Charging battery sensor to be enabled.</string>
<string name="sensor_update_frequency_fast_charging_sensor_disabled_unselected">Fast While Charging\n\nPlease enable the Is Charging sensor to use this option.</string>
<string name="sensor_update_frequency_fast_charging_sensor_disabled_selected">Fast While Charging\n\nThe Is Charging sensor is disabled, currently updating sensors every minute. Please enable the Is Charging sensor so this option is respected.</string>
<string name="sensor_update_frequency_fast_charging">Fast While Charging\n\nSensors will update every minute while the device is charging. When not charging sensors will update every 15 minutes.</string>
<string name="sensor_update_frequency_fast_always">Fast Always\n\nSensors will update every minute, always.</string>
<string name="sensor_update_notification">In order for the app to send sensor updates in the background a notification will be created during the update. You may use the button below to manage the appearance of this notification. It is recommended to minimize the notification to hide the icon.</string>
<string name="sensor_worker_notification_channel">Manage Sensor Worker Notification</string>
Expand Down