Add setting for next alarm allow list (#967)

* Add setting for next alarm allow list

* Switch to preference listener to save values
This commit is contained in:
Daniel Shokouhi 2020-09-23 05:35:08 -07:00 committed by GitHub
parent 1ebda5442b
commit a23e657b6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 8 deletions

View file

@ -4,6 +4,8 @@ import android.app.AlarmManager
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import io.homeassistant.companion.android.R import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.sensor.Setting
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Calendar import java.util.Calendar
import java.util.Date import java.util.Date
@ -13,6 +15,7 @@ import java.util.TimeZone
class NextAlarmManager : SensorManager { class NextAlarmManager : SensorManager {
companion object { companion object {
private const val TAG = "NextAlarm" private const val TAG = "NextAlarm"
private const val ALLOW_LIST = "Allow List"
val nextAlarm = SensorManager.BasicSensor( val nextAlarm = SensorManager.BasicSensor(
"next_alarm", "next_alarm",
@ -51,6 +54,10 @@ class NextAlarmManager : SensorManager {
var utc = "unavailable" var utc = "unavailable"
var pendingIntent = "" var pendingIntent = ""
val sensorDao = AppDatabase.getInstance(context).sensorDao()
val sensorSetting = sensorDao.getSettings(nextAlarm.id)
val allowPackageList = sensorSetting?.firstOrNull { it.name == ALLOW_LIST }?.value ?: ""
try { try {
val alarmManager: AlarmManager = val alarmManager: AlarmManager =
context.getSystemService(Context.ALARM_SERVICE) as AlarmManager context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
@ -58,6 +65,15 @@ class NextAlarmManager : SensorManager {
val alarmClockInfo = alarmManager.nextAlarmClock val alarmClockInfo = alarmManager.nextAlarmClock
if (alarmClockInfo != null) { if (alarmClockInfo != null) {
pendingIntent = alarmClockInfo.showIntent?.creatorPackage ?: "Unknown"
if (allowPackageList != "") {
val allowPackageListing = allowPackageList.split(", ")
if (pendingIntent !in allowPackageListing)
return
} else {
sensorDao.add(Setting(nextAlarm.id, ALLOW_LIST, allowPackageList, "list-apps"))
}
triggerTime = alarmClockInfo.triggerTime triggerTime = alarmClockInfo.triggerTime
val cal: Calendar = GregorianCalendar() val cal: Calendar = GregorianCalendar()
@ -68,8 +84,6 @@ class NextAlarmManager : SensorManager {
val sdf = SimpleDateFormat(dateFormat) val sdf = SimpleDateFormat(dateFormat)
sdf.timeZone = TimeZone.getTimeZone("UTC") sdf.timeZone = TimeZone.getTimeZone("UTC")
utc = sdf.format(Date(triggerTime)) utc = sdf.format(Date(triggerTime))
pendingIntent = alarmClockInfo.showIntent?.creatorPackage ?: "Unknown"
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Error getting the next alarm info", e) Log.e(TAG, "Error getting the next alarm info", e)

View file

@ -5,6 +5,7 @@ import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.text.InputType import android.text.InputType
import androidx.preference.EditTextPreference import androidx.preference.EditTextPreference
import androidx.preference.MultiSelectListPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceCategory import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
@ -177,8 +178,10 @@ class SensorDetailFragment(
pref.dialogTitle = setting.name pref.dialogTitle = setting.name
if (pref.text != null) if (pref.text != null)
pref.summaryProvider = EditTextPreference.SimpleSummaryProvider.getInstance() pref.summaryProvider = EditTextPreference.SimpleSummaryProvider.getInstance()
else else {
pref.summary = setting.value pref.summary = setting.value
pref.text = setting.value
}
pref.isIconSpaceReserved = false pref.isIconSpaceReserved = false
pref.setOnBindEditTextListener { fieldType -> pref.setOnBindEditTextListener { fieldType ->
@ -186,17 +189,51 @@ class SensorDetailFragment(
fieldType.inputType = InputType.TYPE_CLASS_NUMBER fieldType.inputType = InputType.TYPE_CLASS_NUMBER
} }
if (pref.text != null) pref.setOnPreferenceChangeListener { _, newValue ->
sensorDao.add( sensorDao.add(
Setting( Setting(
basicSensor.id, basicSensor.id,
setting.name, setting.name,
pref.text, newValue as String,
setting.valueType setting.valueType
) )
) )
return@setOnPreferenceChangeListener true
}
if (!it.contains(pref))
it.addPreference(pref)
} else if (setting.valueType == "list-apps") {
val packageManager: PackageManager? = context?.packageManager
val packages = packageManager?.getInstalledApplications(PackageManager.GET_META_DATA)
val packageName: MutableList<String> = ArrayList()
if (packages != null) {
for (packageItem in packages) {
packageName.add(packageItem.packageName)
}
packageName.sort()
}
val pref = findPreference(key) ?: MultiSelectListPreference(requireContext())
pref.key = key
pref.title = setting.name
pref.entries = packageName.toTypedArray()
pref.entryValues = packageName.toTypedArray()
pref.dialogTitle = setting.name
pref.isIconSpaceReserved = false
pref.setOnPreferenceChangeListener { _, newValue ->
sensorDao.add(
Setting(
basicSensor.id,
setting.name,
newValue.toString().replace("[", "").replace("]", ""),
"list-apps"
)
)
return@setOnPreferenceChangeListener true
}
if (pref.values != null)
pref.summary = pref.values.toString()
else else
pref.text = setting.value pref.summary = setting.value
if (!it.contains(pref)) if (!it.contains(pref))
it.addPreference(pref) it.addPreference(pref)
} }

View file

@ -202,7 +202,7 @@ like to connect to:</string>
<string name="sensor_description_location_zone">Import existing Home Assistant zones as geofences for zone based tracking. The Minimum Accuracy setting will allow you to decide how accurate the device location (in meters) has to be in order to send to Home Assistant.</string> <string name="sensor_description_location_zone">Import existing Home Assistant zones as geofences for zone based tracking. The Minimum Accuracy setting will allow you to decide how accurate the device location (in meters) has to be in order to send to Home Assistant.</string>
<string name="sensor_description_mic_muted">Whether or not the microphone is muted on the device</string> <string name="sensor_description_mic_muted">Whether or not the microphone is muted on the device</string>
<string name="sensor_description_music_active">Whether or not music is actively playing on the device</string> <string name="sensor_description_music_active">Whether or not music is actively playing on the device</string>
<string name="sensor_description_next_alarm">The date and time of the next scheduled alarm, any app or manufacturer can override the default behavior. The package attribute will tell you which app set the next scheduled alarm.</string> <string name="sensor_description_next_alarm">The date and time of the next scheduled alarm, any app or manufacturer can override the default behavior. The package attribute will tell you which app set the next scheduled alarm. The setting below will create an Allow List so you can specify what packages you want the alarm event from. This will ignore alarm events for packages not selected and the state will not update until the next scheduled alarm matches one of the selected packages.</string>
<string name="sensor_description_none">No description</string> <string name="sensor_description_none">No description</string>
<string name="sensor_description_phone_state">Whether or not the phone is ringing or in a call, no other caller information is stored</string> <string name="sensor_description_phone_state">Whether or not the phone is ringing or in a call, no other caller information is stored</string>
<string name="sensor_description_power_save">Whether or not the device is in Power Save mode</string> <string name="sensor_description_power_save">Whether or not the device is in Power Save mode</string>