Skip to content

Commit

Permalink
Fix notification for missing notification command permissions (#3435)
Browse files Browse the repository at this point in the history
- Fixes notifications for missing notification command permissions crashing the app because there was no server ID included for the notification
  • Loading branch information
jpelgrom committed Apr 1, 2023
1 parent b947157 commit 4aadcd0
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,14 @@ class MessagingManager @Inject constructor(
private fun handleDeviceCommands(data: Map<String, String>) {
val message = data[NotificationData.MESSAGE]
val command = data[NotificationData.COMMAND]
val serverId = data[THIS_SERVER_ID]!!
when (message) {
COMMAND_DND -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val notificationManager =
context.getSystemService<NotificationManager>()
if (notificationManager?.isNotificationPolicyAccessGranted == false) {
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
} else {
when (command) {
DND_ALARMS_ONLY -> notificationManager?.setInterruptionFilter(
Expand All @@ -589,7 +590,7 @@ class MessagingManager @Inject constructor(
val notificationManager =
context.getSystemService<NotificationManager>()
if (notificationManager?.isNotificationPolicyAccessGranted == false) {
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
} else {
processRingerMode(audioManager!!, command)
}
Expand Down Expand Up @@ -629,7 +630,7 @@ class MessagingManager @Inject constructor(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val notificationManager = context.getSystemService<NotificationManager>()
if (notificationManager?.isNotificationPolicyAccessGranted == false) {
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
} else {
processStreamVolume(
audioManager!!,
Expand All @@ -654,7 +655,7 @@ class MessagingManager @Inject constructor(
}
else -> {
Log.e(TAG, "Missing Bluetooth permissions, notifying user to grant permissions")
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
}
}
}
Expand All @@ -680,7 +681,7 @@ class MessagingManager @Inject constructor(
COMMAND_ACTIVITY -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(context)) {
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
} else if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED && data["tag"] == Intent.ACTION_CALL) {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
Expand All @@ -705,7 +706,7 @@ class MessagingManager @Inject constructor(
COMMAND_WEBVIEW -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(context)) {
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
} else {
openWebview(command, data)
}
Expand Down Expand Up @@ -737,7 +738,7 @@ class MessagingManager @Inject constructor(
if (!NotificationManagerCompat.getEnabledListenerPackages(context)
.contains(context.packageName)
) {
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
} else {
processMediaCommand(data)
}
Expand All @@ -746,7 +747,7 @@ class MessagingManager @Inject constructor(
COMMAND_LAUNCH_APP -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(context)) {
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
} else {
launchApp(data)
}
Expand All @@ -764,7 +765,7 @@ class MessagingManager @Inject constructor(
mainScope.launch { sendNotification(data) }
}
} else {
notifyMissingPermission(message.toString())
notifyMissingPermission(message.toString(), serverId)
}
} else if (!processScreenCommands(data)) {
mainScope.launch { sendNotification(data) }
Expand Down Expand Up @@ -1775,16 +1776,18 @@ class MessagingManager @Inject constructor(
return success
}

private fun notifyMissingPermission(type: String) {
private fun notifyMissingPermission(type: String, serverId: String) {
val appManager =
context.getSystemService<ActivityManager>()
val currentProcess = appManager?.runningAppProcesses
if (currentProcess != null) {
for (item in currentProcess) {
if (context.applicationInfo.processName == item.processName) {
if (item.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
val data =
mutableMapOf(NotificationData.MESSAGE to context.getString(commonR.string.missing_command_permission))
val data = mutableMapOf(
NotificationData.MESSAGE to context.getString(commonR.string.missing_command_permission),
THIS_SERVER_ID to serverId
)
runBlocking {
sendNotification(data)
}
Expand Down

0 comments on commit 4aadcd0

Please sign in to comment.