From 191ad904a31a3cdaa2a1e96ea6c8ea91546a49eb Mon Sep 17 00:00:00 2001 From: jens Date: Fri, 14 May 2021 20:07:46 +0200 Subject: [PATCH] Phone Listener changed. --- .../java/com/jens/automation2/Rule.java | 27 ++++++++++++++++--- .../jens/automation2/XmlFileInterface.java | 5 ++-- .../receivers/PhoneStatusListener.java | 19 ++++++++++--- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 1472c5d5..f35c0500 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -7,6 +7,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Looper; import android.service.notification.StatusBarNotification; +import android.telephony.TelephonyManager; import android.util.Log; import android.widget.Toast; @@ -601,13 +602,31 @@ public class Rule implements Comparable } else if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.phoneCall)) { - if(oneTrigger.getPhoneNumber().equals("any") || PhoneStatusListener.getLastPhoneNumber().matches(oneTrigger.getPhoneNumber())) + String[] elements = oneTrigger.getTriggerParameter2().split(triggerParameter2Split); + // state dir number + + if(elements[2].equals(Trigger.triggerPhoneCallNumberAny) || PhoneStatusListener.getLastPhoneNumber().matches(elements[2])) { - if(PhoneStatusListener.isInACall() == oneTrigger.getTriggerParameter()) + //if(PhoneStatusListener.isInACall() == oneTrigger.getTriggerParameter()) + if( + elements[0].equals(Trigger.triggerPhoneCallStateAny) + || + (elements[0].equals(Trigger.triggerPhoneCallStateRinging) && PhoneStatusListener.getLastState() == TelephonyManager.CALL_STATE_RINGING) + || + (elements[0].equals(Trigger.triggerPhoneCallStateStarted) && PhoneStatusListener.getLastState() == TelephonyManager.CALL_STATE_OFFHOOK) + || + (elements[0].equals(Trigger.triggerPhoneCallStateStopped) && PhoneStatusListener.getLastState() == TelephonyManager.CALL_STATE_IDLE) + ) { - if(oneTrigger.getPhoneDirection() == 0 || (oneTrigger.getPhoneDirection() == PhoneStatusListener.getLastPhoneDirection())) + if( + elements[1].equals(Trigger.triggerPhoneCallDirectionAny) + || + (elements[1].equals(Trigger.triggerPhoneCallDirectionIncoming) && PhoneStatusListener.getLastPhoneDirection() == 1) + || + (elements[1].equals(Trigger.triggerPhoneCallDirectionOutgoing) && PhoneStatusListener.getLastPhoneDirection() == 2) + ) { - // Everything's allright + // Trigger conditions are met } else { diff --git a/app/src/main/java/com/jens/automation2/XmlFileInterface.java b/app/src/main/java/com/jens/automation2/XmlFileInterface.java index 1a60dfeb..e10a642a 100644 --- a/app/src/main/java/com/jens/automation2/XmlFileInterface.java +++ b/app/src/main/java/com/jens/automation2/XmlFileInterface.java @@ -928,8 +928,9 @@ public class XmlFileInterface if(elements.length < 3) //old format { // 0/1/2,number - int direction = Integer.parseInt(triggerParameter2.substring(0, 1)); - String number = triggerParameter2.substring(2); + int direction = Integer.parseInt(elements[0]); + + String number = elements[1]; newTrigger.setPhoneDirection(direction); newTrigger.setPhoneNumber(number); diff --git a/app/src/main/java/com/jens/automation2/receivers/PhoneStatusListener.java b/app/src/main/java/com/jens/automation2/receivers/PhoneStatusListener.java index b49ced43..bbdcb3c9 100644 --- a/app/src/main/java/com/jens/automation2/receivers/PhoneStatusListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/PhoneStatusListener.java @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.provider.Telephony; import android.telecom.Call; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; @@ -24,6 +25,7 @@ public class PhoneStatusListener implements AutomationListenerInterface protected static int currentStateOutgoing = -1; protected static String lastPhoneNumber=""; protected static int lastPhoneDirection = -1; //0=incoming, 1=outgoing + protected static int lastState = -1; protected static boolean incomingCallsReceiverActive = false; protected static boolean outgoingCallsReceiverActive = false; @@ -59,7 +61,12 @@ public class PhoneStatusListener implements AutomationListenerInterface { return lastPhoneNumber; } - + + public static int getLastState() + { + return lastState; + } + public static class IncomingCallsReceiver extends PhoneStateListener { @Override @@ -67,6 +74,8 @@ public class PhoneStatusListener implements AutomationListenerInterface { // Miscellaneous.logEvent("i", "Call state", "New call state: " + String.valueOf(state), 4); + lastState = state; + if(incomingNumber != null && incomingNumber.length() > 0) // check for null in case call comes in with suppressed number. setLastPhoneNumber(incomingNumber); @@ -106,7 +115,9 @@ public class PhoneStatusListener implements AutomationListenerInterface @Override public void onReceive(Context context, Intent intent) { - setCurrentStateOutgoing(2); + lastState = TelephonyManager.CALL_STATE_RINGING; + lastPhoneDirection = 2; + setCurrentStateOutgoing(2); // das kommt hier auch bei nur klingeln setLastPhoneNumber(intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)); Miscellaneous.logEvent("i", "Call state", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.outgoingCallFrom), getLastPhoneNumber()), 4); } @@ -236,7 +247,9 @@ public class PhoneStatusListener implements AutomationListenerInterface return currentStateOutgoing; } - +/* +Apps that redirect outgoing calls should use the android.telecom.CallRedirectionService API. Apps that perform call screening should use the android.telecom.CallScreeningService API. + */ public static void startPhoneStatusListener(AutomationService automationService) {