Skip to content

Commit 2f84355

Browse files
committed
Refactor and add KDoc to MyCallReceiver
1 parent b036c3b commit 2f84355

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed
Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,62 @@
11
package com.addev.listaspam
22

3-
import android.Manifest
43
import android.annotation.SuppressLint
5-
import android.app.NotificationChannel
6-
import android.app.NotificationManager
74
import android.content.BroadcastReceiver
85
import android.content.Context
96
import android.content.Intent
10-
import android.content.pm.PackageManager
117
import android.os.Build
128
import android.telecom.TelecomManager
139
import android.telephony.TelephonyManager
14-
import android.widget.Toast
1510
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
2211

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)
2320
class MyCallReceiver : BroadcastReceiver() {
2421

25-
val spamUtils = SpamUtils()
22+
private val spamUtils = SpamUtils()
2623

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+
*/
2829
override fun onReceive(context: Context, intent: Intent) {
2930
if (intent.action == "android.intent.action.PHONE_STATE") {
3031
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
3333

3434
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)
4236
}
4337
}
4438
}
4539

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+
*/
4757
@SuppressLint("MissingPermission")
4858
private fun endCall(context: Context) {
4959
val telMgr = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
5060
telMgr.endCall()
5161
}
52-
5362
}

0 commit comments

Comments
 (0)