Refactor PhoneUtils

This commit is contained in:
xynngh
2020-10-06 14:46:46 +04:00
parent d01cb2cf69
commit 089d0e114d
3 changed files with 36 additions and 26 deletions

View File

@@ -13,10 +13,6 @@
<uses-permission android:name="android.permission.READ_LOG" android:maxSdkVersion="15" /> <uses-permission android:name="android.permission.READ_LOG" android:maxSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <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 <application
android:name=".App" android:name=".App"
android:allowBackup="true" android:allowBackup="true"

View File

@@ -100,8 +100,8 @@ public class PhoneStateHandler {
settings.getCachedAutoDetectedCountryCode(), false); settings.getCachedAutoDetectedCountryCode(), false);
boolean blocked = false; boolean blocked = false;
if (blockingEnabled && !isOffHook && numberInfoService.shouldBlock(numberInfo)) { if (blockingEnabled && numberInfoService.shouldBlock(numberInfo)) {
blocked = PhoneUtils.endCall(context); blocked = PhoneUtils.endCall(context, isOffHook);
if (blocked) { if (blocked) {
notificationService.notifyCallBlocked(numberInfo); notificationService.notifyCallBlocked(numberInfo);

View File

@@ -22,36 +22,50 @@ public class PhoneUtils {
private static final Logger LOG = LoggerFactory.getLogger(PhoneUtils.class); 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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try { try {
TelecomManager telecomManager = requireNonNull( TelecomManager telecomManager = requireNonNull(
(TelecomManager) context.getSystemService(Context.TELECOM_SERVICE)); (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE));
telecomManagerEndCall(telecomManager); if (telecomManagerEndCall(telecomManager)) {
LOG.info("Rejected call using TelecomManager"); LOG.info("endCall() ended call using TelecomManager");
} else {
LOG.warn("endCall() TelecomManager returned false");
}
return true; return true;
} catch (Exception e) { } 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 { try {
TelephonyManager tm = requireNonNull( TelephonyManager tm = requireNonNull(
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
@SuppressLint("DiscouragedPrivateApi") // no choice
Method m = tm.getClass().getDeclaredMethod("getITelephony"); Method m = tm.getClass().getDeclaredMethod("getITelephony");
m.setAccessible(true); m.setAccessible(true);
ITelephony telephony = requireNonNull((ITelephony) m.invoke(tm)); ITelephony telephony = requireNonNull((ITelephony) m.invoke(tm));
telephony.endCall(); if (telephony.endCall()) {
LOG.info("Rejected call using ITelephony"); LOG.info("endCall() ended call using ITelephony");
} else {
LOG.warn("endCall() ITelephony returned false");
}
return true; return true;
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Error while rejecting call", e); LOG.warn("endCall() error while ending call with ITelephony", e);
}
} }
return false; return false;
@@ -60,8 +74,8 @@ public class PhoneUtils {
@SuppressWarnings({"deprecation", "RedundantSuppression"}) // no choice @SuppressWarnings({"deprecation", "RedundantSuppression"}) // no choice
@SuppressLint("MissingPermission") // maybe shouldn't @SuppressLint("MissingPermission") // maybe shouldn't
@RequiresApi(Build.VERSION_CODES.P) @RequiresApi(Build.VERSION_CODES.P)
private static void telecomManagerEndCall(TelecomManager telecomManager) { private static boolean telecomManagerEndCall(TelecomManager telecomManager) {
telecomManager.endCall(); return telecomManager.endCall();
} }
} }