1+ package com.contusfly.call
2+
3+ import android.app.NotificationChannel
4+ import android.app.NotificationManager
5+ import android.content.Context
6+ import android.content.Intent
7+ import android.media.AudioAttributes
8+ import android.net.Uri
9+ import android.os.Build
10+ import androidx.core.app.NotificationCompat
11+ import androidx.core.content.ContextCompat
12+ import com.mirrorflysdk.flycommons.LogMessage
13+ import com.mirrorflysdk.flycall.call.utils.CallConstants
14+ import com.mirrorflysdk.flycall.webrtc.api.CallLogManager
15+ import com.mirrorflysdk.flycommons.PendingIntentHelper
16+ import com.contusfly.R
17+ import com.contusfly.TAG
18+ import com.contusfly.activities.DashboardActivity
19+ import com.contusfly.call.groupcall.utils.CallUtils
20+ import com.contusfly.emptyString
21+ import com.contusfly.getDisplayName
22+ import com.contusfly.notification.NotificationBuilder
23+ import com.contusfly.utils.Constants
24+ import com.contusfly.utils.NotifyRefererUtils
25+ import com.contusfly.utils.ProfileDetailsUtils
26+ import com.contusfly.utils.SharedPreferenceManager
27+ import com.mirrorflysdk.api.FlyMessenger
28+ import com.mirrorflysdk.flycall.webrtc.CallType
29+ import java.security.SecureRandom
30+
31+ object MissedCallNotificationUtils {
32+
33+ var unReadCallCount = 0
34+ var missedCallNotificationCount = 0
35+ private var missedCallNotificationUserJidList = ArrayList <String >()
36+ var missedCallNotificationUserNames = emptyString()
37+ private var missedCallTypeList = ArrayList <String >()
38+ var missedCallNotificationCallType = emptyString()
39+ /* *
40+ * Creates the missed call notification
41+ *
42+ * @param context Instance of Context
43+ * @param message message
44+ * @param messageContent notification message content
45+ */
46+ fun createNotification (context : Context , message : String? , messageContent : String? ) {
47+ val randomNumberGenerator = SecureRandom ()
48+ val bound = 1000
49+ val channelId = randomNumberGenerator.nextInt(bound).toString()
50+ val notificationManager = context
51+ .getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
52+
53+ unReadCallCount + = 1
54+ unReadCallCount = if (NotificationBuilder .chatNotifications.size == 0 ) getTotalUnReadCount() else unReadCallCount
55+
56+ LogMessage .i(TAG ," unReadCallCount $unReadCallCount " )
57+ val notBuilder = NotificationCompat .Builder (context, channelId)
58+ notBuilder.setSmallIcon(getNotificationIcon())
59+ notBuilder.color = ContextCompat .getColor(context, R .color.colorAccent)
60+ notBuilder.setContentTitle(message)
61+ notBuilder.setContentText(messageContent)
62+ notBuilder.setAutoCancel(true )
63+ notBuilder.setNumber(unReadCallCount)
64+ val createdChannel: NotificationChannel
65+ val notificationSoundUri = Uri .parse(SharedPreferenceManager .getString(Constants .NOTIFICATION_URI ))
66+ val isRing = SharedPreferenceManager .getBoolean(Constants .NOTIFICATION_SOUND )
67+ val isVibrate = SharedPreferenceManager .getBoolean(Constants .VIBRATION )
68+ val channelImportance = getChannelImportance(isRing, isVibrate)
69+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
70+ // Create the channel for the notification
71+
72+ when {
73+ isRing -> {
74+ val mChannel = NotificationChannel (channelId, channelId, channelImportance)
75+ val audioAttributes = AudioAttributes .Builder ()
76+ .setUsage(AudioAttributes .USAGE_NOTIFICATION )
77+ .build()
78+ if (notificationSoundUri != null ) {
79+ NotifyRefererUtils .setNotificationChannelSound(notificationSoundUri,audioAttributes,
80+ mChannel,context)
81+ if (isVibrate) {
82+ mChannel.vibrationPattern =
83+ NotifyRefererUtils .defaultVibrationPattern
84+ } else {
85+ mChannel.vibrationPattern = longArrayOf(0L , 0L , 0L , 0L , 0L )
86+ }
87+ } else {
88+ notificationSoundUri?.let {
89+ NotifyRefererUtils .setNotificationChannelSound(
90+ it,audioAttributes,
91+ mChannel,context)
92+ }
93+ }
94+ createdChannel = mChannel
95+ }
96+ isVibrate -> {
97+ val priorityChannel = NotificationChannel (channelId, channelId, channelImportance)
98+ priorityChannel.shouldVibrate()
99+ priorityChannel.vibrationPattern = NotifyRefererUtils .defaultVibrationPattern
100+ priorityChannel.shouldVibrate()
101+ priorityChannel.enableVibration(true )
102+ priorityChannel.setSound(null , null )
103+ createdChannel = priorityChannel
104+ }
105+ else -> {
106+ val lowPriorityChannel = NotificationChannel (channelId, channelId, channelImportance)
107+ createdChannel = lowPriorityChannel
108+ }
109+ }
110+ // Set the Notification Channel for the Notification Manager.
111+
112+ notificationManager.createNotificationChannel(createdChannel)
113+ notBuilder.setChannelId(channelId)
114+
115+ } else {
116+ NotifyRefererUtils .setNotificationSound(notBuilder)
117+ }
118+
119+ val notificationIntent = Intent (context, DashboardActivity ::class .java)
120+ notificationIntent.flags = Intent .FLAG_ACTIVITY_CLEAR_TASK or Intent .FLAG_ACTIVITY_CLEAR_TOP
121+ notificationIntent.putExtra(CallUtils .IS_CALL_NOTIFICATION , true )
122+ val pendingIntent = PendingIntentHelper .getActivity(
123+ context, CallConstants .CALL_NOTIFICATION_ID ,
124+ notificationIntent
125+ )
126+ notBuilder.setContentIntent(pendingIntent)
127+ val notification = notBuilder.build()
128+ if (! SharedPreferenceManager .getBoolean(Constants .MUTE_NOTIFICATION ))
129+ notificationManager.notify(CallConstants .CALL_NOTIFICATION_ID , notification)
130+ }
131+
132+ private fun getChannelImportance (isRing : Boolean , isVibrate : Boolean ): Int {
133+ return if (isRing || isVibrate)
134+ NotificationManager .IMPORTANCE_HIGH
135+ else
136+ NotificationManager .IMPORTANCE_LOW
137+ }
138+
139+ private fun getNotificationIcon (): Int {
140+ LogMessage .i(TAG , " ${com.contus.call.CallConstants .CALL_UI } getNotificationIcon()" )
141+ return R .drawable.ic_notification_blue
142+ }
143+
144+ private fun getTotalUnReadCount (): Int {
145+ return if (NotificationBuilder .chatNotifications.size == 0 ) FlyMessenger .getUnreadMessageCountExceptMutedChat() + CallLogManager .getUnreadMissedCallCount() else 1
146+ }
147+
148+ fun cancelNotifications () {
149+ unReadCallCount = 0
150+ }
151+
152+ fun clearMissedCallNotificationDetails (){
153+ missedCallNotificationCount = 0
154+ missedCallNotificationUserJidList.clear()
155+ missedCallNotificationUserNames = emptyString()
156+ missedCallTypeList.clear()
157+ missedCallNotificationCallType = emptyString()
158+ }
159+
160+ fun addMissedCallNotificationUsers (isOneToOneCall : Boolean , userJid : String , groupId : String? , callType : String ) {
161+ missedCallNotificationCount + = 1
162+ if (! missedCallTypeList.contains(callType))
163+ missedCallTypeList.add(callType)
164+ if (isOneToOneCall) {
165+ if (! missedCallNotificationUserJidList.contains(userJid)) {
166+ missedCallNotificationUserJidList.add(userJid)
167+ }
168+ sortMissedCallList(userJid)
169+ } else {
170+ if (! missedCallNotificationUserJidList.contains(groupId)) {
171+ missedCallNotificationUserJidList.add(groupId!! )
172+ }
173+ sortMissedCallList(groupId!! )
174+ }
175+ missedCallNotificationCallType = " " + identifyCallType(missedCallTypeList)
176+ }
177+
178+ private fun sortMissedCallList (userJid : String ) {
179+ val index = missedCallNotificationUserJidList.indexOf(userJid)
180+ if (index != - 1 && index != 0 ) {
181+ // Remove the userJid from its current position and add it to the first position
182+ missedCallNotificationUserJidList.removeAt(index)
183+ missedCallNotificationUserJidList.add(0 , userJid)
184+ }
185+ missedCallNotificationUserNames =
186+ getSortedUserNames().joinToString(separator = " , " )
187+ }
188+
189+ private fun identifyCallType (missedCallTypeList : List <String >): String {
190+ var hasAudio = false
191+ var hasVideo = false
192+
193+ // Check if "audio" & "video" is present in the list
194+ for (callType in missedCallTypeList) {
195+ when (callType) {
196+ CallType .AUDIO_CALL -> hasAudio = true
197+ CallType .VIDEO_CALL -> hasVideo = true
198+ }
199+ if (hasAudio && hasVideo) {
200+ return " missed calls"
201+ }
202+ }
203+
204+ // Determine the call type ("audio" or "video") present in the list
205+ return when {
206+ hasAudio -> " missed audio calls"
207+ hasVideo -> " missed video calls"
208+ else -> " missed calls"
209+ }
210+ }
211+
212+ private fun getSortedUserNames (): List <String > {
213+ val sortedNames = mutableListOf<String >()
214+ for (userJid in missedCallNotificationUserJidList) {
215+ val displayName = ProfileDetailsUtils .getProfileDetails(userJid)?.getDisplayName()
216+ if (displayName != null ) {
217+ sortedNames.add(displayName)
218+ }
219+ }
220+ return sortedNames
221+ }
222+
223+
224+ }
0 commit comments