Use incoming call notification channel if service channel is disabled

This commit is contained in:
Sylvain Berfini 2021-08-30 11:55:01 +02:00
parent 185aaf20e3
commit 46b9478bf5
3 changed files with 45 additions and 2 deletions

View file

@ -113,6 +113,14 @@ class Api26Compatibility {
notificationManager.createNotificationChannel(channel)
}
fun getChannelImportance(
notificationManager: NotificationManagerCompat,
channelId: String
): Int {
val channel = notificationManager.getNotificationChannel(channelId)
return channel?.importance ?: NotificationManagerCompat.IMPORTANCE_NONE
}
fun getOverlayType(): Int {
return WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
}

View file

@ -102,6 +102,16 @@ class Compatibility {
}
}
fun getChannelImportance(
notificationManager: NotificationManagerCompat,
channelId: String
): Int {
if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
return Api26Compatibility.getChannelImportance(notificationManager, channelId)
}
return NotificationManagerCompat.IMPORTANCE_DEFAULT
}
fun getOverlayType(): Int {
if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
return Api26Compatibility.getOverlayType()

View file

@ -319,13 +319,19 @@ class NotificationsManager(private val context: Context) {
}
private fun createServiceNotification(useAutoStartDescription: Boolean = false) {
val serviceChannel = context.getString(R.string.notification_channel_service_id)
if (Compatibility.getChannelImportance(notificationManager, serviceChannel) == NotificationManagerCompat.IMPORTANCE_NONE) {
Log.w("[Notifications Manager] Service channel is disabled!")
return
}
val pendingIntent = NavDeepLinkBuilder(context)
.setComponentName(MainActivity::class.java)
.setGraph(R.navigation.main_nav_graph)
.setDestination(R.id.dialerFragment)
.createPendingIntent()
val builder = NotificationCompat.Builder(context, context.getString(R.string.notification_channel_service_id))
val builder = NotificationCompat.Builder(context, serviceChannel)
.setContentTitle(context.getString(R.string.service_name))
.setContentText(if (useAutoStartDescription) context.getString(R.string.service_auto_start_description) else context.getString(R.string.service_description))
.setSmallIcon(R.drawable.topbar_service_notification)
@ -484,6 +490,23 @@ class NotificationsManager(private val context: Context) {
fun displayCallNotification(call: Call, useAsForeground: Boolean = false) {
val notifiable = getNotifiableForCall(call)
val serviceChannel = context.getString(R.string.notification_channel_service_id)
val channelToUse = when (val serviceChannelImportance = Compatibility.getChannelImportance(notificationManager, serviceChannel)) {
NotificationManagerCompat.IMPORTANCE_NONE -> {
Log.w("[Notifications Manager] Service channel is disabled, using incoming call channel instead!")
context.getString(R.string.notification_channel_incoming_call_id)
}
NotificationManagerCompat.IMPORTANCE_LOW -> {
// Expected, nothing to do
serviceChannel
}
else -> {
// If user disables & enabled back service notifications channel, importance won't be low anymore but default!
Log.w("[Notifications Manager] Service channel importance is $serviceChannelImportance and not LOW (${NotificationManagerCompat.IMPORTANCE_LOW}) as expected!")
serviceChannel
}
}
val contact: Contact? = coreContext.contactsManager.findContactByAddress(call.remoteAddress)
val pictureUri = contact?.getContactThumbnailPictureUri()
val roundPicture = ImageUtils.getRoundBitmapFromUri(context, pictureUri)
@ -523,7 +546,7 @@ class NotificationsManager(private val context: Context) {
val pendingIntent = PendingIntent.getActivity(context, 0, callNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val builder = NotificationCompat.Builder(
context, context.getString(R.string.notification_channel_service_id))
context, channelToUse)
.setContentTitle(contact?.fullName ?: displayName)
.setContentText(context.getString(stringResourceId))
.setSmallIcon(iconResourceId)
@ -558,6 +581,8 @@ class NotificationsManager(private val context: Context) {
if (notifiable != null) {
cancel(notifiable.notificationId)
callNotificationsMap.remove(address)
} else {
Log.w("[Notifications Manager] No notification found for call ${call.callLog.callId}")
}
}