i’m trying to write an app that intercepts a phone call and compares the number to a spam list and if it passes plays a message asking the caller to press 5 to continue. if the number is in the contacts list it lets it through without the message and the number is randomly generated. if the caller doesn’t get the number right it shows the call and asks if you want to accept it anyway.
i am not a very good programmer but i have an ai assistant now that writes code for me. the thing is, larry got close with this one but can’t finish it. there is still one error remaining according to the android sdk. if anyone would like to help me and look at the code it would be greatly appreciated.
package com.example.zapper // Update with your actual package name
import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.telephony.TelephonyManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import kotlin.random.Random
class R {
companion object
}
class MainActivity : AppCompatActivity() {
private val requestPermissionCode = 123
private var correctNumber = 0
private val phoneStateReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == TelephonyManager.ACTION_PHONE_STATE_CHANGED) {
val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
if (state == TelephonyManager.EXTRA_STATE_RINGING) {
// Incoming call
handleIncomingCall()
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Make sure activity_main.xml exists in your layout folder
checkPermissions()
}
// Check and request permissions
private fun checkPermissions() =
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, arrayOf(Manifest.permission.READ_PHONE_STATE), requestPermissionCode
)
} else {
startCallListener()
}
// Start listening for incoming calls
private fun startCallListener() {
// Register BroadcastReceiver for phone state changes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(phoneStateReceiver, IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED),
RECEIVER_NOT_EXPORTED
)
}
}
}
// Handle incoming call
private fun handleIncomingCall() {
// Play a message asking for the number
generateRandomNumber()
playMessage()
}
// Play a message asking for the number
private fun playMessage() {
// Here, you would implement playing a message asking for the number
// For simplicity, let's just display a toast message
Toast.makeText(this, "Please enter the following number to continue: $correctNumber", Toast.LENGTH_LONG).show()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == requestPermissionCode) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCallListener()
} else {
Toast.makeText(this, "Permission denied. App cannot function properly.", Toast.LENGTH_SHORT).show()
finish()
}
}
}
// Generate a random number
private fun generateRandomNumber() {
correctNumber = Random.nextInt(10) // Generates a random number between 0 and 9
}
override fun onDestroy() {
super.onDestroy()
// Unregister the BroadcastReceiver when the activity is destroyed
unregisterReceiver(phoneStateReceiver)
}
}