diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index a8b2446ab..451a7c435 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -344,6 +344,10 @@ public class GBApplication extends Application { return VERSION.SDK_INT >= Build.VERSION_CODES.O; } + public static boolean isRunningPieOrLater() { + return VERSION.SDK_INT >= Build.VERSION_CODES.P; + } + private static boolean isPrioritySender(int prioritySenders, String number) { if (prioritySenders == Policy.PRIORITY_SENDERS_ANY) { return true; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBCallControlReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBCallControlReceiver.java index 42a3d4620..e2bd99d4b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBCallControlReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBCallControlReceiver.java @@ -16,7 +16,6 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.service.receivers; -import android.annotation.TargetApi; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -24,6 +23,8 @@ import android.os.Build; import android.telecom.TelecomManager; import android.telephony.TelephonyManager; +import androidx.annotation.RequiresApi; + import com.android.internal.telephony.ITelephony; import org.slf4j.Logger; @@ -43,24 +44,24 @@ public class GBCallControlReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { GBDeviceEventCallControl.Event callCmd = GBDeviceEventCallControl.Event.values()[intent.getIntExtra("event", 0)]; - if (Build.VERSION.SDK_INT >= 28){ + if (GBApplication.isRunningPieOrLater()) { handleCallCmdTelecomManager(callCmd); - }else { + } else { switch (callCmd) { case END: case REJECT: case START: try { - TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - Class clazz = Class.forName(telephonyManager.getClass().getName()); - Method method = clazz.getDeclaredMethod("getITelephony"); - method.setAccessible(true); - ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager); - if (callCmd == GBDeviceEventCallControl.Event.END || callCmd == GBDeviceEventCallControl.Event.REJECT) { - telephonyService.endCall(); - } else { - telephonyService.answerRingingCall(); - } + TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + Class clazz = Class.forName(telephonyManager.getClass().getName()); + Method method = clazz.getDeclaredMethod("getITelephony"); + method.setAccessible(true); + ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager); + if (callCmd == GBDeviceEventCallControl.Event.END || callCmd == GBDeviceEventCallControl.Event.REJECT) { + telephonyService.endCall(); + } else { + telephonyService.answerRingingCall(); + } } catch (Exception e) { LOG.warn("could not start or hangup call"); } @@ -70,22 +71,20 @@ public class GBCallControlReceiver extends BroadcastReceiver { } } - @TargetApi(28) - public void handleCallCmdTelecomManager(GBDeviceEventCallControl.Event callCmd){ + @RequiresApi(api = Build.VERSION_CODES.P) + public void handleCallCmdTelecomManager(GBDeviceEventCallControl.Event callCmd) { try { TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); if (callCmd == GBDeviceEventCallControl.Event.END || callCmd == GBDeviceEventCallControl.Event.REJECT) { tm.endCall(); - } - else if (callCmd == GBDeviceEventCallControl.Event.START || callCmd == GBDeviceEventCallControl.Event.ACCEPT) { + } else if (callCmd == GBDeviceEventCallControl.Event.START || callCmd == GBDeviceEventCallControl.Event.ACCEPT) { tm.acceptRingingCall(); } - }catch (SecurityException e){ + } catch (SecurityException e) { LOG.warn("no permission to start or hangup call"); - } - catch (Exception e) { + } catch (Exception e) { LOG.warn("could not start or hangup call"); } }