There's a weird trick you can do with notification listeners, listen to all notifications by creating a service, whenever a call is coming it'll definitely show a notification to ask the user whether to pick up or not.
Create a service
class AutoCallPickerService : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn)
sbn?.let {
it.notification.actions?.let { actions ->
actions.iterator().forEach { item ->
if (item.title.toString().equals("Answer", true)) {
val pendingIntent = item.actionIntent
pendingIntent.send()
}
}
}
}
}
override fun onNotificationRemoved(sbn: StatusBarNotification?) {
super.onNotificationRemoved(sbn)
}
}
Here we are assuming that the notification has an action with label as answer, thereby we are making a match and calling the corresponding intent related with it.
In the launcher activity ask the permission for accessing notifications
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
startActivity(intent)
}
}
Finally register the service for listening to notifications
<service
android:name=".AutoCallPickerService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action
android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
If the phone model doesn't show up a notification for incoming calls this code will be of total failure.