From 991a5e695a90224ca6185899587097fd39d9f68c Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Sat, 18 Nov 2023 17:18:20 +0100 Subject: [PATCH] Added missed call(s) notification --- .../notifications/NotificationsManager.kt | 66 ++++++++++++++++++- .../java/org/linphone/utils/LinphoneUtils.kt | 9 +++ app/src/main/res/drawable/phone_x.xml | 9 +++ app/src/main/res/values/strings.xml | 5 ++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/drawable/phone_x.xml diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index 5da0852e0..1d54eed7e 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -88,7 +88,10 @@ class NotificationsManager @MainThread constructor(private val context: Context) const val INTENT_REMOTE_ADDRESS = "REMOTE_ADDRESS" const val CHAT_TAG = "Chat" + private const val MISSED_CALL_TAG = "Missed call" const val CHAT_NOTIFICATIONS_GROUP = "CHAT_NOTIF_GROUP" + + private const val MISSED_CALL_ID = 10 } private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) @@ -124,6 +127,11 @@ class NotificationsManager @MainThread constructor(private val context: Context) ) dismissCallNotification(call) } + Call.State.Released -> { + if (LinphoneUtils.isCallLogMissed(call.callLog)) { + showMissedCallNotification(call) + } + } else -> { } } @@ -320,6 +328,7 @@ class NotificationsManager @MainThread constructor(private val context: Context) init { createServiceChannel() createIncomingCallNotificationChannel() + createMissedCallNotificationChannel() createActiveCallNotificationChannel() createMessageChannel() @@ -409,6 +418,47 @@ class NotificationsManager @MainThread constructor(private val context: Context) notify(notifiable.notificationId, notification) } + @WorkerThread + private fun showMissedCallNotification(call: Call) { + val missedCallCount: Int = coreContext.core.missedCallsCount + val body: String + if (missedCallCount > 1) { + body = context.getString(R.string.notification_missed_calls) + .format(missedCallCount.toString()) + Log.i("$TAG Updating missed calls notification count to $missedCallCount") + } else { + val remoteAddress = call.remoteAddress + val friend: Friend? = coreContext.contactsManager.findContactByAddress(remoteAddress) + body = context.getString(R.string.notification_missed_call) + .format(friend?.name ?: LinphoneUtils.getDisplayName(remoteAddress)) + Log.i("$TAG Creating missed call notification") + } + + val pendingIntent = NavDeepLinkBuilder(context) + .setComponentName(MainActivity::class.java) + .setGraph(R.navigation.main_nav_graph) + .setDestination(R.id.historyListFragment) + .createPendingIntent() + + val builder = NotificationCompat.Builder( + context, + context.getString(R.string.notification_channel_missed_call_id) + ) + .setContentTitle(context.getString(R.string.notification_missed_call_title)) + .setContentText(body) + .setSmallIcon(R.drawable.phone_x) + .setAutoCancel(true) + .setCategory(NotificationCompat.CATEGORY_MISSED_CALL) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setWhen(System.currentTimeMillis()) + .setShowWhen(true) + .setNumber(missedCallCount) + .setContentIntent(pendingIntent) + + val notification = builder.build() + notify(MISSED_CALL_ID, notification, MISSED_CALL_TAG) + } + @WorkerThread private fun startCallForeground() { Log.i("$TAG Trying to start foreground Service using call notification") @@ -828,7 +878,6 @@ class NotificationsManager @MainThread constructor(private val context: Context) .setSmallIcon(R.drawable.chat_teardrop_text) .setAutoCancel(true) .setLargeIcon(largeIcon) - .setColor(AppUtils.getColor(R.color.orange_main_500)) .setCategory(NotificationCompat.CATEGORY_MESSAGE) .setGroup(CHAT_NOTIFICATIONS_GROUP) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) @@ -1034,6 +1083,21 @@ class NotificationsManager @MainThread constructor(private val context: Context) notificationManager.createNotificationChannel(channel) } + @MainThread + private fun createMissedCallNotificationChannel() { + val id = context.getString(R.string.notification_channel_missed_call_id) + val name = context.getString(R.string.notification_channel_missed_call_name) + + val channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH) + channel.description = name + channel.lightColor = context.getColor(R.color.orange_main_500) + channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC + channel.enableVibration(true) + channel.enableLights(true) + channel.setShowBadge(false) + notificationManager.createNotificationChannel(channel) + } + @MainThread private fun createActiveCallNotificationChannel() { val id = context.getString(R.string.notification_channel_call_id) diff --git a/app/src/main/java/org/linphone/utils/LinphoneUtils.kt b/app/src/main/java/org/linphone/utils/LinphoneUtils.kt index 5900a2ab3..427c24579 100644 --- a/app/src/main/java/org/linphone/utils/LinphoneUtils.kt +++ b/app/src/main/java/org/linphone/utils/LinphoneUtils.kt @@ -33,6 +33,7 @@ import org.linphone.core.Address import org.linphone.core.Call import org.linphone.core.Call.Dir import org.linphone.core.Call.Status +import org.linphone.core.CallLog import org.linphone.core.ChatMessage import org.linphone.core.ChatRoom import org.linphone.core.ConferenceInfo @@ -159,6 +160,14 @@ class LinphoneUtils { return true } + @WorkerThread + fun isCallLogMissed(callLog: CallLog): Boolean { + if (callLog.dir == Dir.Outgoing) return false + return callLog.status == Status.Missed || + callLog.status == Status.Aborted || + callLog.status == Status.EarlyAborted + } + @AnyThread @IntegerRes fun getCallIconResId(callStatus: Status, callDir: Dir): Int { diff --git a/app/src/main/res/drawable/phone_x.xml b/app/src/main/res/drawable/phone_x.xml new file mode 100644 index 000000000..a61986118 --- /dev/null +++ b/app/src/main/res/drawable/phone_x.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a4099e3ca..6a9708c26 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,6 +12,7 @@ linphone.org/contact linphone_notification_call_id linphone_notification_incoming_call_id + linphone_notification_missed_call_id linphone_notification_service_id linphone_notification_chat_id @@ -56,11 +57,15 @@ &appName; active calls notifications &appName; incoming calls notifications + &appName; missed calls notifications &appName; service notification &appName; instant messages notifications %s has reacted by %s to: %s Mark as read Reply + Missed call from %s + %s missed calls + Missed call Welcome in Linphone