Phone Listener changed.

This commit is contained in:
jens 2021-05-15 02:27:59 +02:00
parent 191ad904a3
commit 3844079781
6 changed files with 91 additions and 82 deletions

View File

@ -609,13 +609,11 @@ public class Rule implements Comparable<Rule>
{
//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.triggerPhoneCallStateRinging) && PhoneStatusListener.getCurrentState() == TelephonyManager.CALL_STATE_RINGING)
||
(elements[0].equals(Trigger.triggerPhoneCallStateStarted) && PhoneStatusListener.getLastState() == TelephonyManager.CALL_STATE_OFFHOOK)
(elements[0].equals(Trigger.triggerPhoneCallStateStarted) && PhoneStatusListener.getCurrentState() == TelephonyManager.CALL_STATE_OFFHOOK)
||
(elements[0].equals(Trigger.triggerPhoneCallStateStopped) && PhoneStatusListener.getLastState() == TelephonyManager.CALL_STATE_IDLE)
(elements[0].equals(Trigger.triggerPhoneCallStateStopped) && PhoneStatusListener.getCurrentState() == TelephonyManager.CALL_STATE_IDLE)
)
{
if(
@ -1362,28 +1360,28 @@ public class Rule implements Comparable<Rule>
return ruleCandidates;
}
public static ArrayList<Rule> findRuleCandidatesByPhoneCall(boolean triggerParameter)
{
ArrayList<Rule> ruleCandidates = new ArrayList<Rule>();
for(Rule oneRule : ruleCollection)
{
innerloop:
for(Trigger oneTrigger : oneRule.getTriggerSet())
{
if(oneTrigger.getTriggerType() == Trigger.Trigger_Enum.phoneCall)
{
if(oneTrigger.getTriggerParameter() == triggerParameter)
{
ruleCandidates.add(oneRule);
break innerloop; //we don't need to search the other triggers in the same rule
}
}
}
}
return ruleCandidates;
}
// public static ArrayList<Rule> findRuleCandidatesByPhoneCall()
// {
// ArrayList<Rule> ruleCandidates = new ArrayList<Rule>();
//
// for(Rule oneRule : ruleCollection)
// {
// innerloop:
// for(Trigger oneTrigger : oneRule.getTriggerSet())
// {
// if(oneTrigger.getTriggerType() == Trigger.Trigger_Enum.phoneCall)
// {
// if(oneTrigger.getTriggerParameter() == triggerParameter)
// {
// ruleCandidates.add(oneRule);
// break innerloop; //we don't need to search the other triggers in the same rule
// }
// }
// }
// }
//
// return ruleCandidates;
// }
public static ArrayList<Rule> findRuleCandidatesByNfc()
{

View File

@ -19,7 +19,7 @@ public class ActivityManageTriggerPhoneCall extends Activity
ProgressDialog progressDialog = null;
EditText etTriggerPhoneCallPhoneNumber;
RadioButton rbTriggerPhoneCallStateAny, rbTriggerPhoneCallStateRinging, rbTriggerPhoneCallStateStarted, rbTriggerPhoneCallStateStopped, rbTriggerPhoneCallDirectionAny, rbTriggerPhoneCallDirectionIncoming, rbTriggerPhoneCallDirectionOutgoing;
RadioButton rbTriggerPhoneCallStateRinging, rbTriggerPhoneCallStateStarted, rbTriggerPhoneCallStateStopped, rbTriggerPhoneCallDirectionAny, rbTriggerPhoneCallDirectionIncoming, rbTriggerPhoneCallDirectionOutgoing;
Button bSaveTriggerPhoneCall, bTriggerPhoneCallImportFromContacts;
@Override
@ -29,7 +29,6 @@ public class ActivityManageTriggerPhoneCall extends Activity
setContentView(R.layout.activity_manage_trigger_phone_call);
etTriggerPhoneCallPhoneNumber = (EditText)findViewById(R.id.etTriggerPhoneCallPhoneNumber);
rbTriggerPhoneCallStateAny = (RadioButton)findViewById(R.id.rbTriggerPhoneCallStateAny);
rbTriggerPhoneCallStateRinging = (RadioButton)findViewById(R.id.rbTriggerPhoneCallStateRinging);
rbTriggerPhoneCallStateStarted = (RadioButton)findViewById(R.id.rbTriggerPhoneCallStateStarted);
rbTriggerPhoneCallStateStopped = (RadioButton)findViewById(R.id.rbTriggerPhoneCallStateStopped);
@ -46,9 +45,7 @@ public class ActivityManageTriggerPhoneCall extends Activity
{
String tp2Result = "";
if(rbTriggerPhoneCallStateAny.isChecked())
tp2Result += Trigger.triggerPhoneCallStateAny;
else if(rbTriggerPhoneCallStateRinging.isChecked())
if(rbTriggerPhoneCallStateRinging.isChecked())
tp2Result += Trigger.triggerPhoneCallStateRinging;
else if(rbTriggerPhoneCallStateStarted.isChecked())
tp2Result += Trigger.triggerPhoneCallStateStarted;
@ -101,9 +98,7 @@ public class ActivityManageTriggerPhoneCall extends Activity
{
String[] parts = editedPhoneCallTrigger.getTriggerParameter2().split(triggerParameter2Split);
if(parts[0].equals(Trigger.triggerPhoneCallStateAny))
rbTriggerPhoneCallStateAny.setChecked(true);
else if(parts[0].equals(Trigger.triggerPhoneCallStateRinging))
if(parts[0].equals(Trigger.triggerPhoneCallStateRinging))
rbTriggerPhoneCallStateRinging.setChecked(true);
else if(parts[0].equals(Trigger.triggerPhoneCallStateStarted))
rbTriggerPhoneCallStateStarted.setChecked(true);

View File

@ -80,7 +80,6 @@ public class Trigger
public static String triggerPhoneCallStateRinging = "ringing";
public static String triggerPhoneCallStateStarted = "started";
public static String triggerPhoneCallStateStopped = "stopped";
public static String triggerPhoneCallStateAny = "any";
public static String triggerPhoneCallDirectionIncoming = "incoming";
public static String triggerPhoneCallDirectionOutgoing = "outgoing";
public static String triggerPhoneCallDirectionAny = "any";
@ -370,9 +369,7 @@ public class Trigger
returnString.append(" ");
if(elements[0].equals(Trigger.triggerPhoneCallStateAny))
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.any) + " " + Miscellaneous.getAnyContext().getResources().getString(R.string.state));
else if(elements[0].equals(Trigger.triggerPhoneCallStateRinging))
if(elements[0].equals(Trigger.triggerPhoneCallStateRinging))
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.ringing));
else if(elements[0].equals(Trigger.triggerPhoneCallStateStarted))
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.started));

View File

@ -260,8 +260,8 @@ public class XmlFileInterface
serializer.text(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getProcessName());
else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.batteryLevel)
serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getBatteryLevel()));
else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.phoneCall)
serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getPhoneDirection()) + "," + String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getPhoneNumber()));
// else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.phoneCall)
// serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getPhoneDirection()) + "," + String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getPhoneNumber()));
else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.nfcTag)
serializer.text(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getNfcTagId());
else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.activityDetection)
@ -275,6 +275,8 @@ public class XmlFileInterface
serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getHeadphoneType()));
else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.notification)
serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerParameter2()));
else
serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerParameter2()));
serializer.endTag(null, "TriggerParameter2");
serializer.endTag(null, "Trigger");
}
@ -925,7 +927,7 @@ public class XmlFileInterface
else if(newTrigger.getTriggerType() == Trigger_Enum.phoneCall)
{
String[] elements = triggerParameter2.split(",");
if(elements.length < 3) //old format
if(elements.length > 3 && elements.length < 3) //old format
{
// 0/1/2,number
int direction = Integer.parseInt(elements[0]);

View File

@ -1,11 +1,10 @@
package com.jens.automation2.receivers;
import android.app.Service;
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;
import android.util.Log;
@ -21,11 +20,11 @@ import java.util.ArrayList;
public class PhoneStatusListener implements AutomationListenerInterface
{
protected static int currentStateIncoming = -1;
protected static int currentStateOutgoing = -1;
// protected static int currentStateIncoming = -1;
// 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 int currentState = -1;
protected static boolean incomingCallsReceiverActive = false;
protected static boolean outgoingCallsReceiverActive = false;
@ -62,9 +61,14 @@ public class PhoneStatusListener implements AutomationListenerInterface
return lastPhoneNumber;
}
public static int getLastState()
public static void setCurrentState(int currentState)
{
return lastState;
PhoneStatusListener.currentState = currentState;
}
public static int getCurrentState()
{
return currentState;
}
public static class IncomingCallsReceiver extends PhoneStateListener
@ -74,39 +78,45 @@ public class PhoneStatusListener implements AutomationListenerInterface
{
// Miscellaneous.logEvent("i", "Call state", "New call state: " + String.valueOf(state), 4);
lastState = state;
setCurrentState(state);
if(incomingNumber != null && incomingNumber.length() > 0) // check for null in case call comes in with suppressed number.
setLastPhoneNumber(incomingNumber);
lastPhoneDirection = 1;
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_IDLE", 4);
if(currentStateIncoming == TelephonyManager.CALL_STATE_OFFHOOK)
setCurrentStateIncoming(state);
else if(currentStateOutgoing == TelephonyManager.CALL_STATE_OFFHOOK)
setCurrentStateOutgoing(state);
else
currentStateIncoming = state;
currentStateOutgoing = state;
// if(currentStateIncoming == TelephonyManager.CALL_STATE_OFFHOOK)
// setCurrentStateIncoming(state);
// else if(currentStateOutgoing == TelephonyManager.CALL_STATE_OFFHOOK)
// setCurrentStateOutgoing(state);
// else
// currentStateIncoming = state;
// currentStateOutgoing = state;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_OFFHOOK", 4);
if(currentStateIncoming == TelephonyManager.CALL_STATE_RINGING)
setCurrentStateIncoming(state);
else if(currentStateOutgoing == TelephonyManager.CALL_STATE_RINGING)
setCurrentStateOutgoing(state);
// if(currentStateIncoming == TelephonyManager.CALL_STATE_RINGING)
// setCurrentStateIncoming(state);
// else if(currentStateOutgoing == TelephonyManager.CALL_STATE_RINGING)
// setCurrentStateOutgoing(state);
break;
case TelephonyManager.CALL_STATE_RINGING:
String number = "unknown";
if(incomingNumber != null && incomingNumber.length() > 0)
number = incomingNumber;
Miscellaneous.logEvent("i", "Call state", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.incomingCallFrom), number), 4);
setCurrentStateIncoming(state);
Miscellaneous.logEvent("i", "Call state", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.incomingCallFrom), incomingNumber), 4);
break;
}
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger_Enum.phoneCall);
AutomationService asInstance = AutomationService.getInstance();
for(int i=0; i<ruleCandidates.size(); i++)
{
if(asInstance != null)
if(ruleCandidates.get(i).applies(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
}
}
@ -115,23 +125,36 @@ public class PhoneStatusListener implements AutomationListenerInterface
@Override
public void onReceive(Context context, Intent intent)
{
lastState = TelephonyManager.CALL_STATE_RINGING;
lastPhoneDirection = 2;
setCurrentStateOutgoing(2); // das kommt hier auch bei nur klingeln
// setCurrentStateOutgoing(2); // das kommt hier auch bei nur klingeln
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
setCurrentState(tm.getCallState());
setLastPhoneNumber(intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));
Miscellaneous.logEvent("i", "Call state", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.outgoingCallFrom), getLastPhoneNumber()), 4);
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger_Enum.phoneCall);
for(int i=0; i<ruleCandidates.size(); i++)
{
AutomationService asInstance = AutomationService.getInstance();
if(asInstance != null)
if(ruleCandidates.get(i).applies(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
}
}
public static boolean isInACall()
{
if(isInIncomingCall() | isInOutgoingCall())
return true;
return false;
return getCurrentState() != TelephonyManager.CALL_STATE_IDLE;
// if(isInIncomingCall() | isInOutgoingCall())
// return true;
//
// return false;
}
public static boolean isInIncomingCall()
/* public static boolean isInIncomingCall()
{
// Miscellaneous.logEvent("i", "Incoming call state", String.valueOf(currentStateIncoming), 5);
switch(currentStateIncoming)
@ -245,7 +268,7 @@ public class PhoneStatusListener implements AutomationListenerInterface
public static int getCurrentStateOutgoing()
{
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.

View File

@ -36,17 +36,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rbTriggerPhoneCallStateAny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/any" />
<RadioButton
android:id="@+id/rbTriggerPhoneCallStateRinging"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/ringing" />
<RadioButton