PhoneStatusListener

This commit is contained in:
Jens 2023-04-02 00:00:21 +02:00
parent 0acb52099c
commit 81a6c68f73
2 changed files with 42 additions and 2 deletions

View File

@ -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]"))

View File

@ -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