Skip to content

Commit

Permalink
Hide some sensors from unsupported devices (#3740)
Browse files Browse the repository at this point in the history
* Hide some sensors from unsupported devices

* Move UI manager to condition
  • Loading branch information
dshokouhi authored Jul 31, 2023
1 parent 644c4ad commit 9441380
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import com.google.android.gms.location.ActivityRecognition
Expand Down Expand Up @@ -208,6 +209,14 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
}
}

override fun hasSensor(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
!context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)
} else {
true
}
}

override fun requestSensorUpdate(context: Context) {
if (isEnabled(context, activity)) {
val actReg = ActivityRecognition.getClient(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class LastAppSensorManager : SensorManager {
}

override fun hasSensor(context: Context): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
return if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
false
} else {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
}
}

@RequiresApi(Build.VERSION_CODES.M)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.Manifest
import android.app.UiModeManager
import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.media.MediaMetadata
import android.media.session.MediaSessionManager
Expand Down Expand Up @@ -69,8 +70,12 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {
return "https://companion.home-assistant.io/docs/core/sensors#notification-sensors"
}
override fun hasSensor(context: Context): Boolean {
val uiManager = context.getSystemService<UiModeManager>()
return uiManager?.currentModeType != Configuration.UI_MODE_TYPE_TELEVISION
return if (!context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
val uiManager = context.getSystemService<UiModeManager>()
uiManager?.currentModeType != Configuration.UI_MODE_TYPE_TELEVISION
} else {
false
}
}
override val name: Int
get() = commonR.string.sensor_name_last_notification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ class BatterySensorManager : SensorManager {
)
}

override fun hasSensor(context: Context): Boolean {
val intent = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
return intent?.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false) == true
}

override fun requiredPermissions(sensorId: String): Array<String> {
return emptyArray()
}
Expand Down Expand Up @@ -298,6 +303,7 @@ class BatterySensorManager : SensorManager {
BatteryManager.BATTERY_PLUGGED_AC -> "ac"
BatteryManager.BATTERY_PLUGGED_USB -> "usb"
BatteryManager.BATTERY_PLUGGED_WIRELESS -> "wireless"
BatteryManager.BATTERY_PLUGGED_DOCK -> "dock"
else -> "none"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.homeassistant.companion.android.common.sensors

import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.RequiresApi
Expand Down Expand Up @@ -46,7 +47,11 @@ class DNDSensorManager : SensorManager {

@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.M)
override fun hasSensor(context: Context): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
return if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
false
} else {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
}
}

private fun updateDNDState(context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.homeassistant.companion.android.common.sensors

import android.app.AlarmManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.database.AppDatabase
Expand All @@ -11,6 +13,7 @@ import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.GregorianCalendar
import java.util.Locale
import java.util.TimeZone
import io.homeassistant.companion.android.common.R as commonR

Expand Down Expand Up @@ -40,6 +43,14 @@ class NextAlarmManager : SensorManager {
return listOf(nextAlarm)
}

override fun hasSensor(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
!context.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)
} else {
true
}
}

override fun requiredPermissions(sensorId: String): Array<String> {
return emptyArray()
}
Expand Down Expand Up @@ -89,7 +100,7 @@ class NextAlarmManager : SensorManager {
local = cal.time.toString()

val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
val sdf = SimpleDateFormat(dateFormat)
val sdf = SimpleDateFormat(dateFormat, Locale.getDefault())
sdf.timeZone = TimeZone.getTimeZone("UTC")
utc = sdf.format(Date(triggerTime))
} else {
Expand Down

0 comments on commit 9441380

Please sign in to comment.