From ff22a5c5493ef96290f0133809eb82a776286653 Mon Sep 17 00:00:00 2001 From: xynngh Date: Thu, 16 Jan 2020 12:42:29 +0400 Subject: [PATCH] Call blocking on Android 9 --- README.md | 2 +- app/src/main/AndroidManifest.xml | 3 ++- .../yetanothercallblocker/CallReceiver.java | 21 ++++++++++++++++++- .../PermissionHelper.java | 3 +++ .../android/en-US/full_description.txt | 2 +- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4db3065..438434f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Currently implemented (more or less): * Showing a notification with some caller info (positive/negative rating, category, number of negative reviews and such) when the phone's ringing. *Works offline.* * Loading and displaying a list of reviews for a number (accessed by pressing the notification). *Requires internet.* -* Automatic blocking of unwanted calls (may not work on some devices; may work as a system app on Android 9; doesn't work on Android 10+). *Works offline.* +* Automatic blocking of unwanted calls (may not work on some devices; doesn't work on Android 10+). *Works offline.* * Automatic update of the database (for offline use). *Requires internet.* diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 023a6f5..608bd3f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,8 +7,9 @@ + - + diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java b/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java index e180081..e58638e 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java @@ -1,11 +1,15 @@ package dummydomain.yetanothercallblocker; +import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import androidx.annotation.NonNull; + +import android.os.Build; +import android.telecom.TelecomManager; import android.telephony.TelephonyManager; import com.android.internal.telephony.ITelephony; @@ -108,14 +112,29 @@ public class CallReceiver extends BroadcastReceiver { return numberInfo; } + @SuppressLint("MissingPermission") private boolean rejectCall(@NonNull Context context) { - TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + TelecomManager telecomManager = (TelecomManager) + context.getSystemService(Context.TELECOM_SERVICE); + try { + telecomManager.endCall(); + LOG.info("Rejected call using TelecomManager"); + + return true; + } catch (Exception e) { + LOG.warn("Error while rejecting call on API 26+", e); + } + } + + TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); try { Method m = tm.getClass().getDeclaredMethod("getITelephony"); m.setAccessible(true); ITelephony telephony = (ITelephony)m.invoke(tm); telephony.endCall(); + LOG.info("Rejected call using ITelephony"); return true; } catch (Exception e) { diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/PermissionHelper.java b/app/src/main/java/dummydomain/yetanothercallblocker/PermissionHelper.java index 585ac8b..4263eee 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/PermissionHelper.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/PermissionHelper.java @@ -21,6 +21,9 @@ public class PermissionHelper { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { requiredPermissions.add(Manifest.permission.READ_CALL_LOG); } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + requiredPermissions.add(Manifest.permission.ANSWER_PHONE_CALLS); + } List missingPermissions = new ArrayList<>(); diff --git a/fastlane/metadata/android/en-US/full_description.txt b/fastlane/metadata/android/en-US/full_description.txt index 601fa94..0db2304 100644 --- a/fastlane/metadata/android/en-US/full_description.txt +++ b/fastlane/metadata/android/en-US/full_description.txt @@ -5,7 +5,7 @@ Features: * Showing a notification with some caller info (positive/negative rating, category, number of negative reviews and such) when the phone's ringing. ''Works offline.'' * Loading and displaying a list of reviews for a number. ''Requires internet.'' -* Automatic blocking of unwanted calls (may not work on some devices; may work as a system app on Android 9; doesn't work on Android 10+). ''Works offline.'' +* Automatic blocking of unwanted calls (may not work on some devices; doesn't work on Android 10+). ''Works offline.'' * Automatic update of the database (for offline use). ''Requires internet.'' How to use: