mirror of
https://gitlab.com/xynngh/YetAnotherCallBlocker.git
synced 2025-02-16 20:00:35 +01:00
Refactor PhoneUtils
This commit is contained in:
parent
d01cb2cf69
commit
089d0e114d
@ -13,10 +13,6 @@
|
||||
<uses-permission android:name="android.permission.READ_LOG" android:maxSdkVersion="15" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
|
||||
<!-- may be needed for call blocking on Android 9, also requires to be installed as a system app -->
|
||||
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"
|
||||
tools:ignore="ProtectedPermissions" />
|
||||
|
||||
<application
|
||||
android:name=".App"
|
||||
android:allowBackup="true"
|
||||
|
@ -100,8 +100,8 @@ public class PhoneStateHandler {
|
||||
settings.getCachedAutoDetectedCountryCode(), false);
|
||||
|
||||
boolean blocked = false;
|
||||
if (blockingEnabled && !isOffHook && numberInfoService.shouldBlock(numberInfo)) {
|
||||
blocked = PhoneUtils.endCall(context);
|
||||
if (blockingEnabled && numberInfoService.shouldBlock(numberInfo)) {
|
||||
blocked = PhoneUtils.endCall(context, isOffHook);
|
||||
|
||||
if (blocked) {
|
||||
notificationService.notifyCallBlocked(numberInfo);
|
||||
|
@ -22,36 +22,50 @@ public class PhoneUtils {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PhoneUtils.class);
|
||||
|
||||
public static boolean endCall(@NonNull Context context) {
|
||||
public static boolean endCall(@NonNull Context context, boolean offHook) {
|
||||
LOG.debug("endCall() started");
|
||||
|
||||
if (offHook) {
|
||||
// According to docs, it should work with TelecomManager,
|
||||
// but it doesn't (the ongoing call is ended instead).
|
||||
LOG.warn("endCall() cannot end a new call while off-hook");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
try {
|
||||
TelecomManager telecomManager = requireNonNull(
|
||||
(TelecomManager) context.getSystemService(Context.TELECOM_SERVICE));
|
||||
|
||||
telecomManagerEndCall(telecomManager);
|
||||
LOG.info("Rejected call using TelecomManager");
|
||||
if (telecomManagerEndCall(telecomManager)) {
|
||||
LOG.info("endCall() ended call using TelecomManager");
|
||||
} else {
|
||||
LOG.warn("endCall() TelecomManager returned false");
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Error while rejecting call on API 28+", e);
|
||||
LOG.warn("endCall() error while ending call with TelecomManager", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
TelephonyManager tm = requireNonNull(
|
||||
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
|
||||
|
||||
try {
|
||||
TelephonyManager tm = requireNonNull(
|
||||
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
|
||||
Method m = tm.getClass().getDeclaredMethod("getITelephony");
|
||||
m.setAccessible(true);
|
||||
ITelephony telephony = requireNonNull((ITelephony) m.invoke(tm));
|
||||
|
||||
@SuppressLint("DiscouragedPrivateApi") // no choice
|
||||
Method m = tm.getClass().getDeclaredMethod("getITelephony");
|
||||
m.setAccessible(true);
|
||||
ITelephony telephony = requireNonNull((ITelephony) m.invoke(tm));
|
||||
if (telephony.endCall()) {
|
||||
LOG.info("endCall() ended call using ITelephony");
|
||||
} else {
|
||||
LOG.warn("endCall() ITelephony returned false");
|
||||
}
|
||||
|
||||
telephony.endCall();
|
||||
LOG.info("Rejected call using ITelephony");
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Error while rejecting call", e);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("endCall() error while ending call with ITelephony", e);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -60,8 +74,8 @@ public class PhoneUtils {
|
||||
@SuppressWarnings({"deprecation", "RedundantSuppression"}) // no choice
|
||||
@SuppressLint("MissingPermission") // maybe shouldn't
|
||||
@RequiresApi(Build.VERSION_CODES.P)
|
||||
private static void telecomManagerEndCall(TelecomManager telecomManager) {
|
||||
telecomManager.endCall();
|
||||
private static boolean telecomManagerEndCall(TelecomManager telecomManager) {
|
||||
return telecomManager.endCall();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user