From de5b080df4f0cdbc61aa3d79f154ce695443f50c Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 10 Sep 2022 22:16:42 +0530 Subject: [PATCH 1/3] Add the ability to block calls by pattern --- .../dialer/extensions/Call.kt | 16 +++++++++ .../dialer/services/CallService.kt | 36 +++++++++++-------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Call.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Call.kt index d48216ae..53ad1d1b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Call.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Call.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.dialer.extensions +import android.net.Uri import android.telecom.Call import android.telecom.Call.STATE_CONNECTING import android.telecom.Call.STATE_DIALING @@ -41,3 +42,18 @@ fun Call.isOutgoing(): Boolean { fun Call.hasCapability(capability: Int): Boolean = (details.callCapabilities and capability) != 0 fun Call?.isConference(): Boolean = this?.details?.hasProperty(Call.Details.PROPERTY_CONFERENCE) == true + +fun Call.getCallerNumber(): String? { + val handle = try { + details?.handle?.toString() + } catch (e: NullPointerException) { + null + } + if (handle != null) { + val uri = Uri.decode(handle) + if (uri.startsWith("tel:")) { + return uri.substringAfter("tel:") + } + } + return null +} diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt index 58b0d517..7c859dd4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt @@ -5,10 +5,10 @@ import android.content.ActivityNotFoundException import android.content.Context import android.telecom.Call import android.telecom.InCallService +import com.simplemobiletools.commons.extensions.isNumberBlocked +import com.simplemobiletools.commons.extensions.normalizePhoneNumber import com.simplemobiletools.dialer.activities.CallActivity -import com.simplemobiletools.dialer.extensions.config -import com.simplemobiletools.dialer.extensions.isOutgoing -import com.simplemobiletools.dialer.extensions.powerManager +import com.simplemobiletools.dialer.extensions.* import com.simplemobiletools.dialer.helpers.CallManager import com.simplemobiletools.dialer.helpers.CallNotificationManager import com.simplemobiletools.dialer.helpers.NoCall @@ -27,21 +27,27 @@ class CallService : InCallService() { override fun onCallAdded(call: Call) { super.onCallAdded(call) - CallManager.onCallAdded(call) - CallManager.inCallService = this - call.registerCallback(callListener) + val number = call.getCallerNumber().orEmpty() + if (!call.isOutgoing() && !call.isConference() && isNumberBlocked(number.normalizePhoneNumber())) { + call.reject(false, null) + return + } else { + CallManager.onCallAdded(call) + CallManager.inCallService = this + call.registerCallback(callListener) - val isScreenLocked = (getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager).isDeviceLocked - if (!powerManager.isInteractive || call.isOutgoing() || isScreenLocked || config.alwaysShowFullscreen) { - try { - callNotificationManager.setupNotification(true) - startActivity(CallActivity.getStartIntent(this)) - } catch (e: ActivityNotFoundException) { - // seems like startActivity can thrown AndroidRuntimeException and ActivityNotFoundException, not yet sure when and why, lets show a notification + val isScreenLocked = (getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager).isDeviceLocked + if (!powerManager.isInteractive || call.isOutgoing() || isScreenLocked || config.alwaysShowFullscreen) { + try { + callNotificationManager.setupNotification(true) + startActivity(CallActivity.getStartIntent(this)) + } catch (e: ActivityNotFoundException) { + // seems like startActivity can thrown AndroidRuntimeException and ActivityNotFoundException, not yet sure when and why, lets show a notification + callNotificationManager.setupNotification() + } + } else { callNotificationManager.setupNotification() } - } else { - callNotificationManager.setupNotification() } } From 129d9e9f660d14ee8e3a680fe675e0ceb4f25871 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 13 Sep 2022 22:33:43 +0530 Subject: [PATCH 2/3] Move pattern call blocking logic to CallScreeningService --- app/build.gradle | 2 +- .../dialer/extensions/Call.kt | 16 --------- .../dialer/services/CallService.kt | 36 ++++++++----------- .../services/SimpleCallScreeningService.kt | 20 ++++++----- 4 files changed, 27 insertions(+), 47 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 201a0d9f..12517af0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -62,7 +62,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:28e3b108e7' + implementation 'com.github.SimpleMobileTools:Simple-Commons:15c753bd01' implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61' implementation 'me.grantland:autofittextview:0.2.1' } diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Call.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Call.kt index 53ad1d1b..d48216ae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Call.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Call.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.dialer.extensions -import android.net.Uri import android.telecom.Call import android.telecom.Call.STATE_CONNECTING import android.telecom.Call.STATE_DIALING @@ -42,18 +41,3 @@ fun Call.isOutgoing(): Boolean { fun Call.hasCapability(capability: Int): Boolean = (details.callCapabilities and capability) != 0 fun Call?.isConference(): Boolean = this?.details?.hasProperty(Call.Details.PROPERTY_CONFERENCE) == true - -fun Call.getCallerNumber(): String? { - val handle = try { - details?.handle?.toString() - } catch (e: NullPointerException) { - null - } - if (handle != null) { - val uri = Uri.decode(handle) - if (uri.startsWith("tel:")) { - return uri.substringAfter("tel:") - } - } - return null -} diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt index 7c859dd4..58b0d517 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt @@ -5,10 +5,10 @@ import android.content.ActivityNotFoundException import android.content.Context import android.telecom.Call import android.telecom.InCallService -import com.simplemobiletools.commons.extensions.isNumberBlocked -import com.simplemobiletools.commons.extensions.normalizePhoneNumber import com.simplemobiletools.dialer.activities.CallActivity -import com.simplemobiletools.dialer.extensions.* +import com.simplemobiletools.dialer.extensions.config +import com.simplemobiletools.dialer.extensions.isOutgoing +import com.simplemobiletools.dialer.extensions.powerManager import com.simplemobiletools.dialer.helpers.CallManager import com.simplemobiletools.dialer.helpers.CallNotificationManager import com.simplemobiletools.dialer.helpers.NoCall @@ -27,27 +27,21 @@ class CallService : InCallService() { override fun onCallAdded(call: Call) { super.onCallAdded(call) - val number = call.getCallerNumber().orEmpty() - if (!call.isOutgoing() && !call.isConference() && isNumberBlocked(number.normalizePhoneNumber())) { - call.reject(false, null) - return - } else { - CallManager.onCallAdded(call) - CallManager.inCallService = this - call.registerCallback(callListener) + CallManager.onCallAdded(call) + CallManager.inCallService = this + call.registerCallback(callListener) - val isScreenLocked = (getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager).isDeviceLocked - if (!powerManager.isInteractive || call.isOutgoing() || isScreenLocked || config.alwaysShowFullscreen) { - try { - callNotificationManager.setupNotification(true) - startActivity(CallActivity.getStartIntent(this)) - } catch (e: ActivityNotFoundException) { - // seems like startActivity can thrown AndroidRuntimeException and ActivityNotFoundException, not yet sure when and why, lets show a notification - callNotificationManager.setupNotification() - } - } else { + val isScreenLocked = (getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager).isDeviceLocked + if (!powerManager.isInteractive || call.isOutgoing() || isScreenLocked || config.alwaysShowFullscreen) { + try { + callNotificationManager.setupNotification(true) + startActivity(CallActivity.getStartIntent(this)) + } catch (e: ActivityNotFoundException) { + // seems like startActivity can thrown AndroidRuntimeException and ActivityNotFoundException, not yet sure when and why, lets show a notification callNotificationManager.setupNotification() } + } else { + callNotificationManager.setupNotification() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/services/SimpleCallScreeningService.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/services/SimpleCallScreeningService.kt index b705cfcf..a2bf3942 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/services/SimpleCallScreeningService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/services/SimpleCallScreeningService.kt @@ -7,23 +7,25 @@ import android.telecom.CallScreeningService import androidx.annotation.RequiresApi import com.simplemobiletools.commons.extensions.baseConfig import com.simplemobiletools.commons.extensions.getMyContactsCursor +import com.simplemobiletools.commons.extensions.isNumberBlocked +import com.simplemobiletools.commons.extensions.normalizePhoneNumber import com.simplemobiletools.commons.helpers.SimpleContactsHelper @RequiresApi(Build.VERSION_CODES.N) class SimpleCallScreeningService : CallScreeningService() { override fun onScreenCall(callDetails: Call.Details) { - if (baseConfig.blockUnknownNumbers) { - if (callDetails.handle != null) { - val simpleContactsHelper = SimpleContactsHelper(this) - val number = Uri.decode(callDetails.handle?.toString() ?: "").substringAfter("tel:") - val privateCursor = getMyContactsCursor(false, true) - simpleContactsHelper.exists(number, privateCursor) { exists -> - respondToCall(callDetails, !exists) - } + val number = Uri.decode(callDetails.handle?.toString())?.substringAfter("tel:") + if (number != null && isNumberBlocked(number.normalizePhoneNumber())) { + respondToCall(callDetails, isBlocked = true) + } else if (number != null && baseConfig.blockUnknownNumbers) { + val simpleContactsHelper = SimpleContactsHelper(this) + val privateCursor = getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true) + simpleContactsHelper.exists(number, privateCursor) { exists -> + respondToCall(callDetails, isBlocked = !exists) } } else { - respondToCall(callDetails, false) + respondToCall(callDetails, isBlocked = false) } } From 811350a1be8d9a129933f8f8c5b93dd142cfc3dc Mon Sep 17 00:00:00 2001 From: Naveen Date: Wed, 14 Sep 2022 03:11:01 +0530 Subject: [PATCH 3/3] Catch all exceptions --- .../com/simplemobiletools/dialer/services/CallService.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt index 58b0d517..ed456b76 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/services/CallService.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.dialer.services import android.app.KeyguardManager -import android.content.ActivityNotFoundException import android.content.Context import android.telecom.Call import android.telecom.InCallService @@ -36,8 +35,8 @@ class CallService : InCallService() { try { callNotificationManager.setupNotification(true) startActivity(CallActivity.getStartIntent(this)) - } catch (e: ActivityNotFoundException) { - // seems like startActivity can thrown AndroidRuntimeException and ActivityNotFoundException, not yet sure when and why, lets show a notification + } catch (e: Exception) { + // seems like startActivity can throw AndroidRuntimeException and ActivityNotFoundException, not yet sure when and why, lets show a notification callNotificationManager.setupNotification() } } else {