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

Update sensor enabled by default logic to be more granular #3315

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {

private fun handleSleepUpdate(intent: Intent, context: Context) {
Log.d(TAG, "Received sleep update")
if (SleepClassifyEvent.hasEvents(intent) && isEnabled(context, sleepConfidence.id)) {
if (SleepClassifyEvent.hasEvents(intent) && isEnabled(context, sleepConfidence)) {
Log.d(TAG, "Sleep classify event detected")
val sleepClassifyEvent = SleepClassifyEvent.extractEvents(intent)
if (sleepClassifyEvent.size > 0) {
Expand All @@ -138,7 +138,7 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
SensorReceiver.updateAllSensors(context)
}
}
if (SleepSegmentEvent.hasEvents(intent) && isEnabled(context, sleepSegment.id)) {
if (SleepSegmentEvent.hasEvents(intent) && isEnabled(context, sleepSegment)) {
Log.d(TAG, "Sleep segment event detected")
val sleepSegmentEvent = SleepSegmentEvent.extractEvents(intent)
if (sleepSegmentEvent.size > 0) {
Expand Down Expand Up @@ -191,8 +191,6 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
return "https://companion.home-assistant.io/docs/core/sensors#activity-sensors"
}

override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_activity

Expand All @@ -211,7 +209,7 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
}

override fun requestSensorUpdate(context: Context) {
if (isEnabled(context, activity.id)) {
if (isEnabled(context, activity)) {
val actReg = ActivityRecognition.getClient(context)
val pendingIntent = getActivityPendingIntent(context)
Log.d(TAG, "Unregistering for activity updates.")
Expand All @@ -222,19 +220,19 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
actReg.requestActivityUpdates(TimeUnit.MINUTES.toMillis(if (fastUpdate) 1 else 2), pendingIntent)
}
if ((
isEnabled(context, sleepConfidence.id) || isEnabled(
isEnabled(context, sleepConfidence) || isEnabled(
context,
sleepSegment.id
sleepSegment
)
) && !sleepRegistration
) {
val pendingIntent = getSleepPendingIntent(context)
Log.d(TAG, "Registering for sleep updates")
val task = when {
(
isEnabled(context, sleepConfidence.id) && !isEnabled(
isEnabled(context, sleepConfidence) && !isEnabled(
context,
sleepSegment.id
sleepSegment
)
) -> {
Log.d(TAG, "Registering for sleep confidence updates only")
Expand All @@ -244,9 +242,9 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
)
}
(
!isEnabled(context, sleepConfidence.id) && isEnabled(
!isEnabled(context, sleepConfidence) && isEnabled(
context,
sleepSegment.id
sleepSegment
)
) -> {
Log.d(TAG, "Registering for sleep segment updates only")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class AndroidAutoSensorManager : SensorManager, Observer<Int> {
)
}

override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_android_auto

Expand All @@ -48,7 +46,7 @@ class AndroidAutoSensorManager : SensorManager, Observer<Int> {

override fun requestSensorUpdate(context: Context) {
this.context = context
if (!isEnabled(context, androidAutoConnected.id)) {
if (!isEnabled(context, androidAutoConnected)) {
return
}
CoroutineScope(Dispatchers.Main + Job()).launch {
Expand All @@ -60,7 +58,7 @@ class AndroidAutoSensorManager : SensorManager, Observer<Int> {
}

override fun onChanged(type: Int?) {
if (!isEnabled(context, androidAutoConnected.id)) {
if (!isEnabled(context, androidAutoConnected)) {
CoroutineScope(Dispatchers.Main + Job()).launch {
carConnection?.type?.removeObserver(this@AndroidAutoSensorManager)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class GeocodeSensorManager : SensorManager {
override fun docsLink(): String {
return "https://companion.home-assistant.io/docs/core/sensors#geocoded-location-sensor"
}
override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_geolocation
override suspend fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
Expand Down Expand Up @@ -72,7 +70,7 @@ class GeocodeSensorManager : SensorManager {
}

private suspend fun updateGeocodedLocation(context: Context) {
if (!isEnabled(context, geocodedLocation.id) || !checkPermission(context, geocodedLocation.id)) {
if (!isEnabled(context, geocodedLocation) || !checkPermission(context, geocodedLocation.id)) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ class LocationSensorManager : LocationSensorManagerBase() {
return
}

val backgroundEnabled = isEnabled(latestContext, backgroundLocation.id)
val zoneEnabled = isEnabled(latestContext, zoneLocation.id)
val backgroundEnabled = isEnabled(latestContext, backgroundLocation)
val zoneEnabled = isEnabled(latestContext, zoneLocation)

ioScope.launch {
try {
Expand Down Expand Up @@ -285,8 +285,8 @@ class LocationSensorManager : LocationSensorManagerBase() {
private suspend fun setupBackgroundLocation(backgroundEnabled: Boolean? = null, zoneEnabled: Boolean? = null) {
var isBackgroundEnabled = backgroundEnabled
var isZoneEnable = zoneEnabled
if (isBackgroundEnabled == null) isBackgroundEnabled = isEnabled(latestContext, backgroundLocation.id)
if (isZoneEnable == null) isZoneEnable = isEnabled(latestContext, zoneLocation.id)
if (isBackgroundEnabled == null) isBackgroundEnabled = isEnabled(latestContext, backgroundLocation)
if (isZoneEnable == null) isZoneEnable = isEnabled(latestContext, zoneLocation)

if (isBackgroundEnabled) {
val updateIntervalHighAccuracySeconds = getHighAccuracyModeUpdateInterval()
Expand Down Expand Up @@ -675,7 +675,7 @@ class LocationSensorManager : LocationSensorManagerBase() {

private fun handleGeoUpdate(intent: Intent) {
Log.d(TAG, "Received geofence update.")
if (!isEnabled(latestContext, zoneLocation.id)) {
if (!isEnabled(latestContext, zoneLocation)) {
isZoneLocationSetup = false
Log.w(TAG, "Unregistering geofences as zone tracking is disabled and intent was received")
removeGeofenceUpdateRequests()
Expand Down Expand Up @@ -958,7 +958,7 @@ class LocationSensorManager : LocationSensorManagerBase() {
}

private fun getHighAccuracyModeTriggerRange(): Int {
val enabled = isEnabled(latestContext, zoneLocation.id)
val enabled = isEnabled(latestContext, zoneLocation)

if (!enabled) return 0

Expand All @@ -982,7 +982,7 @@ class LocationSensorManager : LocationSensorManagerBase() {
}

private fun getHighAccuracyModeZones(expandedZones: Boolean): List<String> {
val enabled = isEnabled(latestContext, zoneLocation.id)
val enabled = isEnabled(latestContext, zoneLocation)

if (!enabled) return emptyList()

Expand All @@ -1007,7 +1007,7 @@ class LocationSensorManager : LocationSensorManagerBase() {
Log.w(TAG, "Not getting single accurate location because of permissions.")
return
}
if (!isEnabled(latestContext, singleAccurateLocation.id)) {
if (!isEnabled(latestContext, singleAccurateLocation)) {
Log.w(TAG, "Requested single accurate location but it is not enabled.")
return
}
Expand Down Expand Up @@ -1104,8 +1104,6 @@ class LocationSensorManager : LocationSensorManagerBase() {
override fun docsLink(): String {
return "https://companion.home-assistant.io/docs/core/location"
}
override val enabledByDefault: Boolean
get() = false

override val name: Int
get() = commonR.string.sensor_name_location
Expand Down Expand Up @@ -1142,13 +1140,13 @@ class LocationSensorManager : LocationSensorManagerBase() {
context: Context
) {
latestContext = context
if (isEnabled(context, zoneLocation.id) || isEnabled(context, backgroundLocation.id))
if (isEnabled(context, zoneLocation) || isEnabled(context, backgroundLocation))
setupLocationTracking()
val sensorDao = AppDatabase.getInstance(latestContext).sensorDao()
val sensorSetting = sensorDao.getSettings(singleAccurateLocation.id)
val includeSensorUpdate = sensorSetting.firstOrNull { it.name == SETTING_INCLUDE_SENSOR_UPDATE }?.value ?: "false"
if (includeSensorUpdate == "true") {
if (isEnabled(context, singleAccurateLocation.id)) {
if (isEnabled(context, singleAccurateLocation)) {
context.sendBroadcast(
Intent(context, this.javaClass).apply {
action = ACTION_REQUEST_ACCURATE_LOCATION_UPDATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class DevicePolicyManager : SensorManager {

private var isManagedProfileAvailable: Boolean = false

override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = R.string.sensor_name_device_policy

Expand Down Expand Up @@ -55,7 +53,7 @@ class DevicePolicyManager : SensorManager {

private fun updateWorkProfile(context: Context) {

if (!isEnabled(context, isWorkProfile.id))
if (!isEnabled(context, isWorkProfile))
return

onSensorUpdated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class DynamicColorSensorManager : SensorManager {
override fun docsLink(): String {
return "https://companion.home-assistant.io/docs/core/sensors#dynamic-color-sensor"
}
override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_dynamic_color

Expand All @@ -48,7 +46,7 @@ class DynamicColorSensorManager : SensorManager {

private fun updateAccentColor(context: Context) {

if (!isEnabled(context, accentColorSensor.id))
if (!isEnabled(context, accentColorSensor))
return

val dynamicColorContext = DynamicColors.wrapContextIfAvailable(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class LastAppSensorManager : SensorManager {
override fun docsLink(): String {
return "https://companion.home-assistant.io/docs/core/sensors#last-used-app-sensor"
}
override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_last_app

Expand All @@ -53,7 +51,7 @@ class LastAppSensorManager : SensorManager {

@RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private fun updateLastApp(context: Context) {
if (!isEnabled(context, last_used.id))
if (!isEnabled(context, last_used))
return

val usageStats = context.getSystemService<UsageStatsManager>()!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {
override suspend fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
return listOf(lastNotification, lastRemovedNotification, activeNotificationCount, mediaSession)
}
override val enabledByDefault: Boolean
get() = false

override fun requiredPermissions(sensorId: String): Array<String> {
return arrayOf(Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE)
Expand Down Expand Up @@ -109,7 +107,7 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {

updateActiveNotificationCount()

if (!isEnabled(applicationContext, lastNotification.id))
if (!isEnabled(applicationContext, lastNotification))
return

val allowPackages = getSetting(
Expand Down Expand Up @@ -167,7 +165,7 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {

updateActiveNotificationCount()

if (!isEnabled(applicationContext, lastRemovedNotification.id))
if (!isEnabled(applicationContext, lastRemovedNotification))
return

val allowPackages = getSetting(
Expand Down Expand Up @@ -221,7 +219,7 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {
}

private fun updateActiveNotificationCount() {
if (!isEnabled(applicationContext, activeNotificationCount.id) || !listenerConnected)
if (!isEnabled(applicationContext, activeNotificationCount) || !listenerConnected)
return

try {
Expand Down Expand Up @@ -250,7 +248,7 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {
}

private fun updateMediaSession(context: Context) {
if (!isEnabled(context, mediaSession.id))
if (!isEnabled(context, mediaSession))
return

val mediaSessionManager = context.getSystemService<MediaSessionManager>()!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class QuestSensorManager : SensorManager {
return "https://companion.home-assistant.io/docs/oculus-quest/"
}

override val enabledByDefault: Boolean
get() = false

override val name: Int
get() = commonR.string.sensor_name_quest

Expand Down Expand Up @@ -62,7 +59,7 @@ class QuestSensorManager : SensorManager {
}

private fun updateHeadsetMount(context: Context, intent: Intent) {
if (!isEnabled(context, headsetMounted.id))
if (!isEnabled(context, headsetMounted))
return

val state: Boolean = getHeadsetState(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class SensorSettingsViewModel @Inject constructor(
) &&
(
sensorFilter == SensorFilter.ALL ||
(sensorFilter == SensorFilter.ENABLED && manager.isEnabled(app.applicationContext, sensor.id)) ||
(sensorFilter == SensorFilter.DISABLED && !manager.isEnabled(app.applicationContext, sensor.id))
(sensorFilter == SensorFilter.ENABLED && manager.isEnabled(app.applicationContext, sensor)) ||
(sensorFilter == SensorFilter.DISABLED && !manager.isEnabled(app.applicationContext, sensor))
)
}
.mapNotNull { sensor -> sensorsList.firstOrNull { it.id == sensor.id } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ fun SensorDetailView(

val sensorEnabled = viewModel.sensor?.sensor?.enabled
?: (
viewModel.basicSensor != null && viewModel.sensorManager?.enabledByDefault == true &&
viewModel.sensorManager.checkPermission(context, viewModel.basicSensor.id)
viewModel.basicSensor != null && viewModel.basicSensor.enabledByDefault && viewModel.sensorManager?.checkPermission(context, viewModel.basicSensor.id) == true
)

val scaffoldState = rememberScaffoldState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
}
for (manager in SensorReceiver.MANAGERS) {
for (basicSensor in manager.getAvailableSensors(this)) {
if (manager.isEnabled(this, basicSensor.id)) {
if (manager.isEnabled(this, basicSensor)) {
var permissions = manager.requiredPermissions(basicSensor.id)

val fineLocation = DisabledLocationHandler.containsLocationPermission(permissions, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
override fun onReceive(context: Context, intent: Intent) {
// Noop
}

override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_activity

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import io.homeassistant.companion.android.common.R as commonR

class AndroidAutoSensorManager : SensorManager {

override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_android_auto

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class GeocodeSensorManager : SensorManager {
)
}

override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_geolocation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class LocationSensorManager : LocationSensorManagerBase(), SensorManager {
// Noop
}

override val enabledByDefault: Boolean
get() = false
override val name: Int
get() = commonR.string.sensor_name_location

Expand Down
Loading