11package org.fossify.phone.receivers
22
3+ import android.app.Notification
34import android.app.NotificationChannel
45import android.app.NotificationManager
56import android.app.PendingIntent
@@ -10,68 +11,137 @@ import android.graphics.drawable.Icon
1011import android.net.Uri
1112import android.os.Build
1213import android.telecom.TelecomManager
14+ import android.util.Log
1315import androidx.annotation.RequiresApi
1416import androidx.core.app.NotificationCompat
1517import org.fossify.commons.extensions.getLaunchIntent
1618import org.fossify.commons.extensions.notificationManager
19+ import org.fossify.commons.extensions.telecomManager
1720import org.fossify.commons.helpers.SimpleContactsHelper
1821import org.fossify.phone.R
1922import org.fossify.phone.helpers.MISSED_CALLS
23+ import org.fossify.phone.helpers.MISSED_CALL_BACK
24+ import org.fossify.phone.helpers.MISSED_CALL_CANCEL
25+ import org.fossify.phone.helpers.MISSED_CALL_MESSAGE
2026import kotlin.random.Random
2127
2228@RequiresApi(Build .VERSION_CODES .O )
2329class MissedCallReceiver : BroadcastReceiver () {
2430 override fun onReceive (context : Context , intent : Intent ) {
25- if (intent.action == TelecomManager .ACTION_SHOW_MISSED_CALLS_NOTIFICATION ) {
26- val extras = intent.extras!!
27- val notificationCount = extras.getInt(TelecomManager .EXTRA_NOTIFICATION_COUNT )
28- if (notificationCount != 0 ) {
29- val phoneNumber = extras.getString(TelecomManager .EXTRA_NOTIFICATION_PHONE_NUMBER )!!
30- val helper = SimpleContactsHelper (context)
31- val name = helper.getNameFromPhoneNumber(phoneNumber)
32- val photoUri = helper.getPhotoUriFromPhoneNumber(phoneNumber)
31+ val extras = intent.extras ? : return
32+ var phoneNumber = extras.getString(" phoneNumber" )
33+ var notificationId = extras.getInt(" notificationId" , - 1 )
34+ val notificationManager = context.notificationManager
3335
34- val notificationManager = context.notificationManager
35- val channel = NotificationChannel (
36- " missed_call_channel" ,
37- context.getString(R .string.missed_call_channel),
38- NotificationManager .IMPORTANCE_LOW
39- )
40- notificationManager.createNotificationChannel(channel)
36+ when (intent.action) {
37+ TelecomManager .ACTION_SHOW_MISSED_CALLS_NOTIFICATION -> {
38+ notificationId = Random .nextInt()
39+ phoneNumber = extras.getString(TelecomManager .EXTRA_NOTIFICATION_PHONE_NUMBER )
40+ val notificationCount = extras.getInt(TelecomManager .EXTRA_NOTIFICATION_COUNT )
41+ if (notificationCount != 0 ) {
42+ createNotificationChannel(context)
43+ notificationManager.notify(MISSED_CALLS .hashCode(), getNotificationGroup(context))
44+ notificationManager.notify(notificationId, buildNotification(context, notificationId, phoneNumber ? : return ))
45+ }
46+ null
47+ }
4148
42- val pendingIntent = PendingIntent .getActivity(
43- context, 0 , context.getLaunchIntent(), PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
44- )
45- val callBack = Intent (Intent .ACTION_CALL ).apply {
46- data = Uri .fromParts(" tel" , phoneNumber, null )
49+ MISSED_CALL_BACK -> phoneNumber?.let {
50+ Intent (Intent .ACTION_CALL ).apply {
51+ data = Uri .fromParts(" tel" , it, null )
52+ flags = Intent .FLAG_ACTIVITY_NEW_TASK
4753 }
48- val callBackIntent = PendingIntent .getActivity(
49- context, 0 , callBack, PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
50- )
51- val smsIntent = Intent (Intent .ACTION_VIEW ).apply {
52- data = Uri .fromParts(" sms" , phoneNumber, null )
54+ }
55+
56+ MISSED_CALL_MESSAGE -> phoneNumber?.let {
57+ Intent (Intent .ACTION_VIEW ).apply {
58+ data = Uri .fromParts(" sms" , it, null )
59+ flags = Intent .FLAG_ACTIVITY_NEW_TASK
5360 }
54- val messageIntent = PendingIntent .getActivity(
55- context, 0 , smsIntent, PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
56- )
57- val cancel = Intent (" android.intent.action.CANCEL_MISSED_CALLS_NOTIFICATION" )
58- val cancelIntent = PendingIntent .getActivity(
59- context, 0 , cancel, PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
60- )
61- val notification = NotificationCompat .Builder (context, " missed_call_channel" )
62- .setSmallIcon(android.R .drawable.sym_call_missed)
63- .setContentTitle(context.getString(R .string.missed_call))
64- .setContentText(context.getString(R .string.missed_call_from, name))
65- .setLargeIcon(Icon .createWithContentUri(photoUri))
66- .setAutoCancel(true )
67- .setGroup(MISSED_CALLS )
68- .setContentIntent(pendingIntent)
69- .addAction(0 , context.getString(R .string.call_back), callBackIntent)
70- .addAction(0 , context.getString(R .string.message), messageIntent)
71- .setDeleteIntent(cancelIntent)
72- .build()
73- notificationManager.notify(Random .nextInt(), notification)
7461 }
62+
63+ MISSED_CALL_CANCEL -> {
64+ context.telecomManager.cancelMissedCallsNotification()
65+ null
66+ }
67+
68+ else -> null
69+ }?.let {
70+ Log .d(" MISSEDCALL" , it.toString())
71+ context.startActivity(it)
72+ context.notificationManager.cancel(notificationId)
73+ }
74+ }
75+
76+ private fun createNotificationChannel (context : Context ) {
77+ val notificationManager = context.notificationManager
78+ val channel = NotificationChannel (
79+ " missed_call_channel" ,
80+ context.getString(R .string.missed_call_channel),
81+ NotificationManager .IMPORTANCE_LOW
82+ )
83+ notificationManager.createNotificationChannel(channel)
84+ }
85+
86+ private fun launchIntent (context : Context ): PendingIntent {
87+ return PendingIntent .getActivity(
88+ context, 0 , context.getLaunchIntent(), PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
89+ )
90+ }
91+
92+ private fun getNotificationGroup (context : Context ): Notification {
93+ return NotificationCompat .Builder (context, " missed_call_channel" )
94+ .setSmallIcon(android.R .drawable.sym_call_missed)
95+ .setAutoCancel(true )
96+ .setGroupSummary(true )
97+ .setGroup(MISSED_CALLS )
98+ .setContentIntent(launchIntent(context))
99+ .build()
100+ }
101+
102+ private fun buildNotification (context : Context , notificationId : Int , phoneNumber : String ): Notification {
103+ val helper = SimpleContactsHelper (context)
104+ val name = helper.getNameFromPhoneNumber(phoneNumber)
105+ val photoUri = helper.getPhotoUriFromPhoneNumber(phoneNumber)
106+
107+ val callBack = Intent (context, MissedCallReceiver ::class .java).apply {
108+ action = MISSED_CALL_BACK
109+ putExtra(" notificationId" , notificationId)
110+ putExtra(" phoneNumber" , phoneNumber)
75111 }
112+ val callBackIntent = PendingIntent .getBroadcast(
113+ context, 0 , callBack, PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
114+ )
115+
116+ val smsIntent = Intent (context, MissedCallReceiver ::class .java).apply {
117+ action = MISSED_CALL_MESSAGE
118+ putExtra(" notificationId" , notificationId)
119+ putExtra(" phoneNumber" , phoneNumber)
120+ }
121+ val messageIntent = PendingIntent .getBroadcast(
122+ context, 0 , smsIntent, PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
123+ )
124+
125+ val cancel = Intent (context, MissedCallReceiver ::class .java).apply {
126+ action = MISSED_CALL_CANCEL
127+ putExtra(" notificationId" , notificationId)
128+ putExtra(" phoneNumber" , phoneNumber)
129+ }
130+ val cancelIntent = PendingIntent .getActivity(
131+ context, 0 , cancel, PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
132+ )
133+
134+ return NotificationCompat .Builder (context, " missed_call_channel" )
135+ .setSmallIcon(android.R .drawable.sym_call_missed)
136+ .setContentTitle(context.resources.getQuantityString(R .plurals.missed_calls, 1 , 1 ))
137+ .setContentText(context.getString(R .string.missed_call_from, name))
138+ .setLargeIcon(Icon .createWithContentUri(photoUri))
139+ .setAutoCancel(true )
140+ .setGroup(MISSED_CALLS )
141+ .setContentIntent(launchIntent(context))
142+ .addAction(0 , context.getString(R .string.call_back), callBackIntent)
143+ .addAction(0 , context.getString(R .string.message), messageIntent)
144+ .setDeleteIntent(cancelIntent)
145+ .build()
76146 }
77147}
0 commit comments