From 490485b9829b66a21e41fa9456cca3c17629f5af Mon Sep 17 00:00:00 2001 From: Daniel Shokouhi Date: Sat, 10 Oct 2020 20:59:06 -0700 Subject: [PATCH] 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 --- .../android/notifications/MessagingService.kt | 54 +++++++++++++++++++ app/src/main/AndroidManifest.xml | 1 + 2 files changed, 55 insertions(+) diff --git a/app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt b/app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt index 028a08a97..bea6a26af 100644 --- a/app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt +++ b/app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt @@ -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. * diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d380e0aa4..9d7cfb1ba 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,6 +18,7 @@ +