|
1 | 1 | package com.addev.listaspam |
2 | 2 |
|
3 | | -import android.Manifest |
4 | 3 | import android.annotation.SuppressLint |
5 | | -import android.app.NotificationChannel |
6 | | -import android.app.NotificationManager |
7 | 4 | import android.content.BroadcastReceiver |
8 | 5 | import android.content.Context |
9 | 6 | import android.content.Intent |
10 | | -import android.content.pm.PackageManager |
11 | 7 | import android.os.Build |
12 | 8 | import android.telecom.TelecomManager |
13 | 9 | import android.telephony.TelephonyManager |
14 | | -import android.widget.Toast |
15 | 10 | import androidx.annotation.RequiresApi |
16 | | -import androidx.core.app.ActivityCompat |
17 | | -import androidx.core.app.NotificationCompat |
18 | | -import androidx.core.app.NotificationManagerCompat |
19 | | -import okhttp3.* |
20 | | -import org.jsoup.Jsoup |
21 | | -import java.io.IOException |
22 | 11 |
|
| 12 | +/** |
| 13 | + * BroadcastReceiver for handling incoming call events and checking for spam numbers. |
| 14 | + * Requires Android P (API level 28) or higher. |
| 15 | + * |
| 16 | + * Note: This class will not work on Android Q (API level 29) or higher due to changes in privacy permissions. |
| 17 | + * For Android Q or higher, use MyCallScreeningService instead. |
| 18 | + */ |
| 19 | +@RequiresApi(Build.VERSION_CODES.P) |
23 | 20 | class MyCallReceiver : BroadcastReceiver() { |
24 | 21 |
|
25 | | - val spamUtils = SpamUtils() |
| 22 | + private val spamUtils = SpamUtils() |
26 | 23 |
|
27 | | - @RequiresApi(Build.VERSION_CODES.P) |
| 24 | + /** |
| 25 | + * Called when the BroadcastReceiver is receiving an Intent broadcast. |
| 26 | + * @param context Context for accessing resources. |
| 27 | + * @param intent The received Intent. |
| 28 | + */ |
28 | 29 | override fun onReceive(context: Context, intent: Intent) { |
29 | 30 | if (intent.action == "android.intent.action.PHONE_STATE") { |
30 | 31 | val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE) |
31 | | - val incomingNumber = |
32 | | - intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) ?: return |
| 32 | + val incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) ?: return |
33 | 33 |
|
34 | 34 | if (state == TelephonyManager.EXTRA_STATE_RINGING) { |
35 | | - incomingNumber?.let { |
36 | | - spamUtils.checkSpamNumber(context, it) { isSpam -> |
37 | | - if (isSpam) { |
38 | | - endCall(context) |
39 | | - } |
40 | | - } |
41 | | - } |
| 35 | + handleIncomingCall(context, incomingNumber) |
42 | 36 | } |
43 | 37 | } |
44 | 38 | } |
45 | 39 |
|
46 | | - @RequiresApi(Build.VERSION_CODES.P) |
| 40 | + /** |
| 41 | + * Handles the incoming call by checking if the number is spam. |
| 42 | + * @param context Context for accessing resources. |
| 43 | + * @param incomingNumber The incoming phone number. |
| 44 | + */ |
| 45 | + private fun handleIncomingCall(context: Context, incomingNumber: String) { |
| 46 | + spamUtils.checkSpamNumber(context, incomingNumber) { isSpam -> |
| 47 | + if (isSpam) { |
| 48 | + endCall(context) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Ends the incoming call. |
| 55 | + * @param context Context for accessing resources. |
| 56 | + */ |
47 | 57 | @SuppressLint("MissingPermission") |
48 | 58 | private fun endCall(context: Context) { |
49 | 59 | val telMgr = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager |
50 | 60 | telMgr.endCall() |
51 | 61 | } |
52 | | - |
53 | 62 | } |
0 commit comments