From 81a6c68f737dc45ccf42ceeeb97594f448adbd1b Mon Sep 17 00:00:00 2001 From: Jens Date: Sun, 2 Apr 2023 00:00:21 +0200 Subject: [PATCH] PhoneStatusListener --- .../com/jens/automation2/Miscellaneous.java | 5 ++- .../receivers/PhoneStatusListener.java | 39 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Miscellaneous.java b/app/src/main/java/com/jens/automation2/Miscellaneous.java index 3197d37..3aab614 100644 --- a/app/src/main/java/com/jens/automation2/Miscellaneous.java +++ b/app/src/main/java/com/jens/automation2/Miscellaneous.java @@ -612,7 +612,10 @@ public class Miscellaneous extends Service if(lastPhoneNr != null && lastPhoneNr.length() > 0) source = source.replace("[phonenr]", PhoneStatusListener.getLastPhoneNumber()); else - Miscellaneous.logEvent("w", "TriggerURL", context.getResources().getString(R.string.triggerUrlReplacementPositionError), 3); + { + source = source.replace("[phonenr]", "phoneNrUnknown"); + Miscellaneous.logEvent("w", "TriggerURL", "phoneNr requested to be inserted as variable, but value is currently empty.", 3); + } } if(source.contains("[serialnr]")) 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 c1a9337..41d816b 100644 --- a/app/src/main/java/com/jens/automation2/receivers/PhoneStatusListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/PhoneStatusListener.java @@ -1,12 +1,16 @@ package com.jens.automation2.receivers; +import static com.jens.automation2.location.CellLocationChangedReceiver.telephonyManager; + import android.Manifest; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.os.Build; import android.telephony.PhoneStateListener; +import android.telephony.TelephonyCallback; import android.telephony.TelephonyManager; import android.util.Log; @@ -19,8 +23,9 @@ import com.jens.automation2.Trigger; import com.jens.automation2.Trigger.Trigger_Enum; import java.util.ArrayList; +import java.util.concurrent.Executor; -public class PhoneStatusListener implements AutomationListenerInterface +public class PhoneStatusListener implements AutomationListenerInterface, Executor { protected static String lastPhoneNumber=""; protected static int lastPhoneDirection = -1; //0=incoming, 1=outgoing @@ -70,6 +75,12 @@ public class PhoneStatusListener implements AutomationListenerInterface return currentState; } + @Override + public void execute(Runnable runnable) + { + + } + public static class IncomingCallsReceiver extends PhoneStateListener { @Override @@ -229,6 +240,32 @@ public class PhoneStatusListener implements AutomationListenerInterface } } + protected static void startReceiverAboveAndroid12(AutomationService automationService) + { + TelephonyManager tm = (TelephonyManager) automationService.getSystemService(Context.TELEPHONY_SERVICE); + + incomingCallsReceiverInstance = new IncomingCallsReceiver(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + { + tm.registerTelephonyCallback(incomingCallsReceiverInstance, new TelephonyCallback.CallStateListener() + { + @Override + public void onCallStateChanged(int i) + { + + } + }); + } + else + { + telephonyManager.listen(object :PhoneStateListener() { + override fun onCallStateChanged(state:Int, phoneNumber:String ?){ + } + },PhoneStateListener.LISTEN_CALL_STATE) + } + } + public static void stopPhoneStatusListener(AutomationService automationService) { try