From 8631b3db9872f25df53f3fb1aa3f72f3019b5202 Mon Sep 17 00:00:00 2001 From: Daniel Shokouhi Date: Fri, 29 Oct 2021 13:07:48 -0700 Subject: [PATCH] Fix notification command permissions not launching by directing user to open the app (#1848) --- .../android/notifications/MessagingService.kt | 46 ++++++++++++++++--- app/src/main/res/values/strings.xml | 1 + 2 files changed, 41 insertions(+), 6 deletions(-) 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 7cb402f89db..4d78b2a5bed 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 @@ -1,5 +1,6 @@ package io.homeassistant.companion.android.notifications +import android.app.ActivityManager import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager @@ -62,6 +63,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.json.JSONObject import java.net.URL @@ -423,7 +425,7 @@ class MessagingService : FirebaseMessagingService() { val notificationManager = applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager if (!notificationManager.isNotificationPolicyAccessGranted) { - requestDNDPermission() + notifyMissingPermission(data[MESSAGE].toString()) } else { when (title) { DND_ALARMS_ONLY -> notificationManager.setInterruptionFilter( @@ -447,7 +449,7 @@ class MessagingService : FirebaseMessagingService() { val notificationManager = applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager if (!notificationManager.isNotificationPolicyAccessGranted) { - requestDNDPermission() + notifyMissingPermission(data[MESSAGE].toString()) } else { processRingerMode(audioManager, title) } @@ -500,7 +502,7 @@ class MessagingService : FirebaseMessagingService() { val notificationManager = applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager if (!notificationManager.isNotificationPolicyAccessGranted) { - requestDNDPermission() + notifyMissingPermission(data[MESSAGE].toString()) } else { processStreamVolume( audioManager, @@ -544,7 +546,7 @@ class MessagingService : FirebaseMessagingService() { COMMAND_ACTIVITY -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (!Settings.canDrawOverlays(applicationContext)) - requestSystemAlertPermission() + notifyMissingPermission(data[MESSAGE].toString()) else processActivityCommand(data) } else @@ -553,7 +555,7 @@ class MessagingService : FirebaseMessagingService() { COMMAND_WEBVIEW -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (!Settings.canDrawOverlays(applicationContext)) - requestSystemAlertPermission() + notifyMissingPermission(data[MESSAGE].toString()) else openWebview(title) } else @@ -581,7 +583,7 @@ class MessagingService : FirebaseMessagingService() { COMMAND_MEDIA -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { if (!NotificationManagerCompat.getEnabledListenerPackages(applicationContext).contains(applicationContext.packageName)) - requestNotificationPermission() + notifyMissingPermission(data[MESSAGE].toString()) else { processMediaCommand(data) } @@ -1359,6 +1361,38 @@ class MessagingService : FirebaseMessagingService() { Log.e(TAG, "Unable to open webview", e) } } + + private fun notifyMissingPermission(type: String) { + val appManager = + applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + val currentProcess = appManager.runningAppProcesses + if (currentProcess != null) { + for (item in currentProcess) { + if (applicationContext.applicationInfo.processName == item.processName) { + if (item.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { + val data = + mutableMapOf(MESSAGE to getString(R.string.missing_command_permission)) + runBlocking { + sendNotification(data) + } + } else { + when (type) { + COMMAND_WEBVIEW, COMMAND_ACTIVITY -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + requestSystemAlertPermission() + } + COMMAND_RINGER_MODE, COMMAND_DND, COMMAND_VOLUME_LEVEL -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + requestDNDPermission() + } + COMMAND_MEDIA -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { + requestNotificationPermission() + } + } + } + } + } + } + } + /** * Called if InstanceID token is updated. This may occur if the security of * the previous token had been compromised. Note that this is called when the InstanceID token diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4c1e67c29e0..86877aeed5e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -641,4 +641,5 @@ like to connect to: Current state and attribute of any entity Control any media player and see current now playing image Render any template with HTML formatting + Please open the Home Assistant app and send the command again in order to grant the proper permissions.