@@ -3,38 +3,41 @@ package org.fossify.phone.helpers
33import android.annotation.SuppressLint
44import android.app.Notification
55import android.app.NotificationChannel
6- import android.app.NotificationManager
6+ import android.app.NotificationManager.IMPORTANCE_DEFAULT
7+ import android.app.NotificationManager.IMPORTANCE_HIGH
78import android.app.PendingIntent
89import android.content.Context
910import android.content.Intent
1011import android.telecom.Call
1112import android.widget.RemoteViews
12- import androidx.core.app.NotificationCompat
1313import org.fossify.commons.extensions.notificationManager
1414import org.fossify.commons.extensions.setText
1515import org.fossify.commons.extensions.setVisibleIf
1616import org.fossify.commons.helpers.isOreoPlus
1717import org.fossify.phone.R
1818import org.fossify.phone.activities.CallActivity
19- import org.fossify.phone.extensions.powerManager
2019import org.fossify.phone.receivers.CallActionReceiver
2120
2221class CallNotificationManager (private val context : Context ) {
23- private val CALL_NOTIFICATION_ID = 42
24- private val ACCEPT_CALL_CODE = 0
25- private val DECLINE_CALL_CODE = 1
22+ companion object {
23+ private const val CALL_NOTIFICATION_ID = 42
24+ private const val ACCEPT_CALL_CODE = 0
25+ private const val DECLINE_CALL_CODE = 1
26+ }
27+
2628 private val notificationManager = context.notificationManager
2729 private val callContactAvatarHelper = CallContactAvatarHelper (context)
2830
2931 @SuppressLint(" NewApi" )
30- fun setupNotification (forceLowPriority : Boolean = false) {
32+ fun setupNotification (lowPriority : Boolean = false) {
3133 getCallContact(context.applicationContext, CallManager .getPrimaryCall()) { callContact ->
3234 val callContactAvatar = callContactAvatarHelper.getCallContactAvatar(callContact)
3335 val callState = CallManager .getState()
34- val isHighPriority = context.powerManager.isInteractive && callState == Call .STATE_RINGING && ! forceLowPriority
35- val channelId = if (isHighPriority) " simple_dialer_call_high_priority" else " simple_dialer_call"
36+ val isHighPriority = callState == Call .STATE_RINGING && ! lowPriority
37+ val channelId =
38+ if (isHighPriority) " simple_dialer_call_high_priority" else " simple_dialer_call"
39+ val importance = if (isHighPriority) IMPORTANCE_HIGH else IMPORTANCE_DEFAULT
3640 if (isOreoPlus()) {
37- val importance = if (isHighPriority) NotificationManager .IMPORTANCE_HIGH else NotificationManager .IMPORTANCE_DEFAULT
3841 val name = if (isHighPriority) {
3942 context.getString(R .string.call_notification_channel_high_priority)
4043 } else {
@@ -48,19 +51,30 @@ class CallNotificationManager(private val context: Context) {
4851 }
4952
5053 val openAppIntent = CallActivity .getStartIntent(context)
51- val openAppPendingIntent = PendingIntent .getActivity(context, 0 , openAppIntent, PendingIntent .FLAG_MUTABLE )
54+ val openAppPendingIntent =
55+ PendingIntent .getActivity(context, 0 , openAppIntent, PendingIntent .FLAG_MUTABLE )
5256
5357 val acceptCallIntent = Intent (context, CallActionReceiver ::class .java)
5458 acceptCallIntent.action = ACCEPT_CALL
5559 val acceptPendingIntent =
56- PendingIntent .getBroadcast(context, ACCEPT_CALL_CODE , acceptCallIntent, PendingIntent .FLAG_CANCEL_CURRENT or PendingIntent .FLAG_MUTABLE )
60+ PendingIntent .getBroadcast(
61+ context,
62+ ACCEPT_CALL_CODE ,
63+ acceptCallIntent,
64+ PendingIntent .FLAG_CANCEL_CURRENT or PendingIntent .FLAG_MUTABLE
65+ )
5766
5867 val declineCallIntent = Intent (context, CallActionReceiver ::class .java)
5968 declineCallIntent.action = DECLINE_CALL
6069 val declinePendingIntent =
61- PendingIntent .getBroadcast(context, DECLINE_CALL_CODE , declineCallIntent, PendingIntent .FLAG_CANCEL_CURRENT or PendingIntent .FLAG_MUTABLE )
62-
63- var callerName = if (callContact.name.isNotEmpty()) callContact.name else context.getString(R .string.unknown_caller)
70+ PendingIntent .getBroadcast(
71+ context,
72+ DECLINE_CALL_CODE ,
73+ declineCallIntent,
74+ PendingIntent .FLAG_CANCEL_CURRENT or PendingIntent .FLAG_MUTABLE
75+ )
76+
77+ var callerName = callContact.name.ifEmpty { context.getString(R .string.unknown_caller) }
6478 if (callContact.numberLabel.isNotEmpty()) {
6579 callerName + = " - ${callContact.numberLabel} "
6680 }
@@ -82,21 +96,22 @@ class CallNotificationManager(private val context: Context) {
8296 setOnClickPendingIntent(R .id.notification_accept_call, acceptPendingIntent)
8397
8498 if (callContactAvatar != null ) {
85- setImageViewBitmap(R .id.notification_thumbnail, callContactAvatarHelper.getCircularBitmap(callContactAvatar))
99+ setImageViewBitmap(
100+ R .id.notification_thumbnail,
101+ callContactAvatarHelper.getCircularBitmap(callContactAvatar)
102+ )
86103 }
87104 }
88105
89- val builder = NotificationCompat .Builder (context, channelId)
106+ val builder = Notification .Builder (context, channelId)
90107 .setSmallIcon(R .drawable.ic_phone_vector)
91108 .setContentIntent(openAppPendingIntent)
92- .setPriority(if (isHighPriority) NotificationManager .IMPORTANCE_HIGH else NotificationCompat .PRIORITY_DEFAULT )
93109 .setCategory(Notification .CATEGORY_CALL )
94110 .setCustomContentView(collapsedView)
95111 .setOngoing(true )
96- .setSound(null )
97112 .setUsesChronometer(callState == Call .STATE_ACTIVE )
98113 .setChannelId(channelId)
99- .setStyle(NotificationCompat .DecoratedCustomViewStyle ())
114+ .setStyle(Notification .DecoratedCustomViewStyle ())
100115
101116 if (isHighPriority) {
102117 builder.setFullScreenIntent(openAppPendingIntent, true )
0 commit comments