1
1
package io.homeassistant.companion.android.notifications
2
2
3
+ import android.app.ActivityManager
3
4
import android.app.Notification
4
5
import android.app.NotificationChannel
5
6
import android.app.NotificationManager
@@ -62,6 +63,7 @@ import kotlinx.coroutines.CoroutineScope
62
63
import kotlinx.coroutines.Dispatchers
63
64
import kotlinx.coroutines.Job
64
65
import kotlinx.coroutines.launch
66
+ import kotlinx.coroutines.runBlocking
65
67
import kotlinx.coroutines.withContext
66
68
import org.json.JSONObject
67
69
import java.net.URL
@@ -423,7 +425,7 @@ class MessagingService : FirebaseMessagingService() {
423
425
val notificationManager =
424
426
applicationContext.getSystemService(NOTIFICATION_SERVICE ) as NotificationManager
425
427
if (! notificationManager.isNotificationPolicyAccessGranted) {
426
- requestDNDPermission( )
428
+ notifyMissingPermission(data[ MESSAGE ].toString() )
427
429
} else {
428
430
when (title) {
429
431
DND_ALARMS_ONLY -> notificationManager.setInterruptionFilter(
@@ -447,7 +449,7 @@ class MessagingService : FirebaseMessagingService() {
447
449
val notificationManager =
448
450
applicationContext.getSystemService(NOTIFICATION_SERVICE ) as NotificationManager
449
451
if (! notificationManager.isNotificationPolicyAccessGranted) {
450
- requestDNDPermission( )
452
+ notifyMissingPermission(data[ MESSAGE ].toString() )
451
453
} else {
452
454
processRingerMode(audioManager, title)
453
455
}
@@ -500,7 +502,7 @@ class MessagingService : FirebaseMessagingService() {
500
502
val notificationManager =
501
503
applicationContext.getSystemService(NOTIFICATION_SERVICE ) as NotificationManager
502
504
if (! notificationManager.isNotificationPolicyAccessGranted) {
503
- requestDNDPermission( )
505
+ notifyMissingPermission(data[ MESSAGE ].toString() )
504
506
} else {
505
507
processStreamVolume(
506
508
audioManager,
@@ -544,7 +546,7 @@ class MessagingService : FirebaseMessagingService() {
544
546
COMMAND_ACTIVITY -> {
545
547
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
546
548
if (! Settings .canDrawOverlays(applicationContext))
547
- requestSystemAlertPermission( )
549
+ notifyMissingPermission(data[ MESSAGE ].toString() )
548
550
else
549
551
processActivityCommand(data)
550
552
} else
@@ -553,7 +555,7 @@ class MessagingService : FirebaseMessagingService() {
553
555
COMMAND_WEBVIEW -> {
554
556
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
555
557
if (! Settings .canDrawOverlays(applicationContext))
556
- requestSystemAlertPermission( )
558
+ notifyMissingPermission(data[ MESSAGE ].toString() )
557
559
else
558
560
openWebview(title)
559
561
} else
@@ -581,7 +583,7 @@ class MessagingService : FirebaseMessagingService() {
581
583
COMMAND_MEDIA -> {
582
584
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP_MR1 ) {
583
585
if (! NotificationManagerCompat .getEnabledListenerPackages(applicationContext).contains(applicationContext.packageName))
584
- requestNotificationPermission( )
586
+ notifyMissingPermission(data[ MESSAGE ].toString() )
585
587
else {
586
588
processMediaCommand(data)
587
589
}
@@ -1359,6 +1361,38 @@ class MessagingService : FirebaseMessagingService() {
1359
1361
Log .e(TAG , " Unable to open webview" , e)
1360
1362
}
1361
1363
}
1364
+
1365
+ private fun notifyMissingPermission (type : String ) {
1366
+ val appManager =
1367
+ applicationContext.getSystemService(Context .ACTIVITY_SERVICE ) as ActivityManager
1368
+ val currentProcess = appManager.runningAppProcesses
1369
+ if (currentProcess != null ) {
1370
+ for (item in currentProcess) {
1371
+ if (applicationContext.applicationInfo.processName == item.processName) {
1372
+ if (item.importance != ActivityManager .RunningAppProcessInfo .IMPORTANCE_FOREGROUND ) {
1373
+ val data =
1374
+ mutableMapOf (MESSAGE to getString(R .string.missing_command_permission))
1375
+ runBlocking {
1376
+ sendNotification(data)
1377
+ }
1378
+ } else {
1379
+ when (type) {
1380
+ COMMAND_WEBVIEW , COMMAND_ACTIVITY -> if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
1381
+ requestSystemAlertPermission()
1382
+ }
1383
+ COMMAND_RINGER_MODE , COMMAND_DND , COMMAND_VOLUME_LEVEL -> if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
1384
+ requestDNDPermission()
1385
+ }
1386
+ COMMAND_MEDIA -> if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP_MR1 ) {
1387
+ requestNotificationPermission()
1388
+ }
1389
+ }
1390
+ }
1391
+ }
1392
+ }
1393
+ }
1394
+ }
1395
+
1362
1396
/* *
1363
1397
* Called if InstanceID token is updated. This may occur if the security of
1364
1398
* the previous token had been compromised. Note that this is called when the InstanceID token
0 commit comments