mirror of
https://github.com/home-assistant/android
synced 2024-09-18 23:52:51 +00:00
Allow control of DND via notification (#1024)
* Allow notification command to turn on or off DND * Improve existing logic to account for additional commands and do better error handling
This commit is contained in:
parent
3fbb1874af
commit
490485b982
|
@ -66,6 +66,13 @@ class MessagingService : FirebaseMessagingService() {
|
|||
const val CLEAR_NOTIFICATION = "clear_notification"
|
||||
const val REMOVE_CHANNEL = "remove_channel"
|
||||
const val TTS = "TTS"
|
||||
const val COMMAND_DND = "command_dnd"
|
||||
const val DND_PRIORITY_ONLY = "priority_only"
|
||||
const val DND_ALARMS_ONLY = "alarms_only"
|
||||
const val DND_ALL = "off"
|
||||
const val DND_NONE = "total_silence"
|
||||
val DEVICE_COMMANDS = listOf(COMMAND_DND)
|
||||
val DND_COMMANDS = listOf(DND_ALARMS_ONLY, DND_ALL, DND_NONE, DND_PRIORITY_ONLY)
|
||||
}
|
||||
|
||||
@Inject
|
||||
|
@ -111,6 +118,29 @@ class MessagingService : FirebaseMessagingService() {
|
|||
Log.d(TAG, "Sending notification title to TTS")
|
||||
speakNotification(it[TITLE])
|
||||
}
|
||||
it[MESSAGE] in DEVICE_COMMANDS -> {
|
||||
Log.d(TAG, "Processing device command")
|
||||
when (it[MESSAGE]) {
|
||||
COMMAND_DND -> {
|
||||
if (it[TITLE] in DND_COMMANDS) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
||||
handleDeviceCommands(it[MESSAGE], it[TITLE])
|
||||
else {
|
||||
mainScope.launch {
|
||||
Log.d(TAG, "Posting notification to device as it does not support DND commands")
|
||||
sendNotification(it)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mainScope.launch {
|
||||
Log.d(TAG, "Invalid DND command received, posting notification to device")
|
||||
sendNotification(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> Log.d(TAG, "No command received")
|
||||
}
|
||||
}
|
||||
else -> mainScope.launch {
|
||||
Log.d(TAG, "Creating notification with following data: $it")
|
||||
sendNotification(it)
|
||||
|
@ -177,6 +207,30 @@ class MessagingService : FirebaseMessagingService() {
|
|||
}
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
private fun handleDeviceCommands(message: String?, title: String?) {
|
||||
when (message) {
|
||||
COMMAND_DND -> {
|
||||
val notificationManager =
|
||||
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
if (!notificationManager.isNotificationPolicyAccessGranted) {
|
||||
val intent =
|
||||
Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
startActivity(intent)
|
||||
} else {
|
||||
when (title) {
|
||||
DND_ALARMS_ONLY -> notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALARMS)
|
||||
DND_ALL -> notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL)
|
||||
DND_NONE -> notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)
|
||||
DND_PRIORITY_ONLY -> notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
|
||||
else -> Log.d(TAG, "Skipping invalid command")
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> Log.d(TAG, "No command received")
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Create and show a simple notification containing the received FCM message.
|
||||
*
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
<uses-permission android:name="android.permission.READ_CALL_LOG" />
|
||||
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
|
||||
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
|
||||
|
||||
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
|
||||
<uses-feature android:name="android.hardware.telephony" android:required="false"/>
|
||||
|
|
Loading…
Reference in a new issue