@@ -6,7 +6,6 @@ import android.app.NotificationManager
66import android.content.Context
77import android.content.pm.PackageManager
88import android.database.Cursor
9- import android.os.Build
109import android.os.Handler
1110import android.os.Looper
1211import android.provider.ContactsContract
@@ -16,7 +15,6 @@ import androidx.core.app.NotificationCompat
1615import androidx.core.app.NotificationManagerCompat
1716import androidx.core.content.ContextCompat
1817import com.addev.listaspam.R
19- import com.addev.listaspam.model.SpamData
2018import kotlinx.coroutines.CoroutineScope
2119import kotlinx.coroutines.Dispatchers
2220import kotlinx.coroutines.launch
@@ -56,6 +54,10 @@ class SpamUtils {
5654 */
5755 fun checkSpamNumber (context : Context , number : String , callback : (isSpam: Boolean ) -> Unit ) {
5856 CoroutineScope (Dispatchers .IO ).launch {
57+ if (! isBlockingEnabled(context)) {
58+ return @launch
59+ }
60+
5961 if (isNumberBlockedLocally(context, number)) {
6062 handleSpamNumber(context, number, callback)
6163 return @launch
@@ -65,13 +67,24 @@ class SpamUtils {
6567 if (isNumberInAgenda(context, number)) {
6668 handleNonSpamNumber(context, number, callback)
6769 return @launch
70+ } else if (shouldBlockNonContacts(context)) {
71+ handleSpamNumber(context, number, callback)
6872 }
73+
74+
6975 }
7076
71- val spamCheckers = listOf (
72- ::checkListaSpam,
73- ::checkResponderono,
74- )
77+ // List to hold the functions that should be used
78+ val spamCheckers = mutableListOf< suspend (String ) -> Boolean > ()
79+
80+ // Add functions based on preferences
81+ if (shouldFilterWithListaSpam(context)) {
82+ spamCheckers.add(::checkListaSpam)
83+ }
84+
85+ if (shouldFilterWithResponderONo(context)) {
86+ spamCheckers.add(::checkResponderono)
87+ }
7588
7689 val isSpam = spamCheckers.any { checker ->
7790 runCatching { checker(number) }.getOrDefault(false )
@@ -271,38 +284,32 @@ class SpamUtils {
271284 }
272285 }
273286
274- /* *
275- * Sends a notification indicating that a spam number has been blocked.
276- * @param context Context for sending the notification.
277- * @param number Phone number that was blocked.
278- */
279287 private fun sendNotification (context : Context , number : String ) {
280288 createNotificationChannel(context)
281- val notification = NotificationCompat .Builder (context, context.getString(R .string.notification_channel_id))
282- .setSmallIcon(R .mipmap.ic_launcher)
283- .setContentTitle(context.getString(R .string.notification_title_spam_blocked))
284- .setContentText(context.getString(R .string.notification_text_spam_blocked, number))
285- .setPriority(NotificationCompat .PRIORITY_HIGH )
286- .build()
287289
288290 if (ActivityCompat .checkSelfPermission(
289291 context,
290292 Manifest .permission.POST_NOTIFICATIONS
291- ) != PackageManager .PERMISSION_GRANTED
293+ ) != PackageManager .PERMISSION_GRANTED ||
294+ ! shouldShowNotification(context)
292295 ) {
296+ // Aquí deberías solicitar el permiso si no está concedido.
293297 return
294298 }
295299
300+ val notification = NotificationCompat .Builder (context, NOTIFICATION_CHANNEL_ID )
301+ .setSmallIcon(R .mipmap.ic_launcher)
302+ .setContentTitle(context.getString(R .string.notification_title_spam_blocked))
303+ .setContentText(context.getString(R .string.notification_text_spam_blocked, number))
304+ .setPriority(NotificationCompat .PRIORITY_HIGH )
305+ .build()
306+
296307 NotificationManagerCompat .from(context).notify(NOTIFICATION_ID , notification)
297308 }
298309
299- /* *
300- * Creates a notification channel for spam notifications.
301- * @param context Context for creating the notification channel.
302- */
303310 private fun createNotificationChannel (context : Context ) {
304- val name = context.getString(R .string.notification_channel_id )
305- val descriptionText = context.getString(R .string.notification_channel_id )
311+ val name = context.getString(R .string.notification_channel_name )
312+ val descriptionText = context.getString(R .string.notification_channel_name )
306313 val importance = NotificationManager .IMPORTANCE_HIGH
307314 val channel = NotificationChannel (NOTIFICATION_CHANNEL_ID , name, importance).apply {
308315 description = descriptionText
@@ -312,4 +319,5 @@ class SpamUtils {
312319 context.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
313320 notificationManager.createNotificationChannel(channel)
314321 }
322+
315323}
0 commit comments