Compare commits

...

5 Commits

Author SHA1 Message Date
Jens
49d272be1e PhoneStatusListener 2023-04-16 22:31:01 +02:00
Jens
d9e54c7780 PhoneStatusListener 2023-04-14 23:16:47 +02:00
Jens
170dbbc7e8 PhoneStatusListener 2023-04-13 00:10:21 +02:00
3fc1dd1a26 Telephony receiver 2023-04-12 18:05:00 +02:00
Jens
0acb52099c Added overlay permission 2023-03-31 23:58:11 +02:00
15 changed files with 195 additions and 62 deletions

View File

@ -11,8 +11,8 @@ android {
compileSdkVersion 31
buildToolsVersion '29.0.2'
useLibrary 'org.apache.http.legacy'
versionCode 127
versionName "1.7.12"
versionCode 129
versionName "1.7.13"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

View File

@ -68,6 +68,8 @@
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-feature
android:name="android.hardware.telephony"

View File

@ -66,6 +66,8 @@
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-feature
android:name="android.hardware.telephony"

View File

@ -64,6 +64,8 @@
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
<uses-permission android:name="com.wireguard.android.permission.CONTROL_TUNNELS"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<application
android:allowBackup="true"

View File

@ -25,6 +25,8 @@ import android.widget.TextView;
import com.jens.automation2.receivers.NotificationListener;
import org.w3c.dom.DOMImplementationSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -48,6 +50,7 @@ public class ActivityPermissions extends Activity
private static final int requestCodeForPermissionsDeviceAdmin = 12047;
private static final int requestCodeForPermissionsBatteryOptimization = 12048;
private static final int requestCodeForPermissionNotificationAccessAndroid13 = 12049;
private static final int requestCodeForPermissionsManageOverlay = 12050;
protected String[] specificPermissionsToRequest = null;
public static String intentExtraName = "permissionsToBeRequested";
@ -297,6 +300,10 @@ public class ActivityPermissions extends Activity
String packageName = context.getApplicationContext().getPackageName();
return pm.isIgnoringBatteryOptimizations(packageName);
}
else if (s.equals(Manifest.permission.SYSTEM_ALERT_WINDOW))
{
return android.provider.Settings.canDrawOverlays(Miscellaneous.getAnyContext());
}
else
{
int res = context.checkCallingOrSelfPermission(s);
@ -315,6 +322,11 @@ public class ActivityPermissions extends Activity
return active;
}
public static void requestOverlay()
{
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
ActivityPermissions.getInstance().startActivityForResult(intent, requestCodeForPermissionsManageOverlay);
}
public static void requestDeviceAdmin()
{
if(!haveDeviceAdmin())
@ -473,6 +485,7 @@ public class ActivityPermissions extends Activity
case phoneCall:
addToArrayListUnique(Manifest.permission.READ_PHONE_STATE, requiredPermissions);
addToArrayListUnique(Manifest.permission.PROCESS_OUTGOING_CALLS, requiredPermissions);
addToArrayListUnique(Manifest.permission.READ_CALL_LOG, requiredPermissions);
break;
case pointOfInterest:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
@ -634,6 +647,8 @@ public class ActivityPermissions extends Activity
// action.getParameter2().contains("eu.faircode.netguard.STOP_PORT_FORWARD")
// )
// addToArrayListUnique("net.kollnig.missioncontrol.permission.ADMIN", requiredPermissions);
if(Build.VERSION.SDK_INT >= 29)
addToArrayListUnique(Manifest.permission.SYSTEM_ALERT_WINDOW, requiredPermissions);
break;
case triggerUrl:
addToArrayListUnique(Manifest.permission.INTERNET, requiredPermissions);
@ -863,6 +878,7 @@ public class ActivityPermissions extends Activity
usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName));
break;
case Manifest.permission.PROCESS_OUTGOING_CALLS:
case Manifest.permission.READ_CALL_LOG:
for(String ruleName : getRulesUsing(Trigger.Trigger_Enum.phoneCall))
usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName));
break;
@ -890,6 +906,10 @@ public class ActivityPermissions extends Activity
for(String ruleName : getRulesUsing(Action.Action_Enum.startPhoneCall))
usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName));
break;
case Manifest.permission.SYSTEM_ALERT_WINDOW:
for(String ruleName : getRulesUsing(Action.Action_Enum.startOtherActivity))
usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName));
break;
case Manifest.permission.ANSWER_PHONE_CALLS:
for(String ruleName : getRulesUsing(Action.Action_Enum.stopPhoneCall))
usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName));
@ -988,6 +1008,10 @@ public class ActivityPermissions extends Activity
if (requestCode == requestCodeForPermissionsBatteryOptimization)
if(havePermission(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, ActivityPermissions.this))
requestPermissions(cachedPermissionsToRequest, true);
if (requestCode == requestCodeForPermissionsManageOverlay)
if(havePermission(Manifest.permission.SYSTEM_ALERT_WINDOW, ActivityPermissions.this))
requestPermissions(cachedPermissionsToRequest, true);
}
}
@ -1051,7 +1075,22 @@ public class ActivityPermissions extends Activity
cachedPermissionsToRequest = requiredPermissions;
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult(intent, requestCodeForPermissionsNotificationPolicy);
return;
}
else if (s.equalsIgnoreCase(Manifest.permission.SYSTEM_ALERT_WINDOW))
{
AlertDialog diag = Miscellaneous.messageBox(getResources().getString(R.string.info), getResources().getString(R.string.overlayPermissionHint), ActivityPermissions.this);
diag.setOnDismissListener(new DialogInterface.OnDismissListener()
{
@Override
public void onDismiss(DialogInterface dialogInterface)
{
requiredPermissions.remove(s);
cachedPermissionsToRequest = requiredPermissions;
requestOverlay();
}
});
diag.show();
return;
}
else if (s.equalsIgnoreCase(Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE))

View File

@ -3,11 +3,9 @@ package com.jens.automation2.receivers;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.bluetooth.BluetoothDevice;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

View File

@ -6,10 +6,15 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.annotation.RequiresApi;
import com.jens.automation2.ActivityPermissions;
import com.jens.automation2.AutomationService;
import com.jens.automation2.Miscellaneous;
@ -22,6 +27,7 @@ import java.util.ArrayList;
public class PhoneStatusListener implements AutomationListenerInterface
{
static int problematicAndroidLevel = 29;
protected static String lastPhoneNumber="";
protected static int lastPhoneDirection = -1; //0=incoming, 1=outgoing
protected static int currentState = -1;
@ -70,7 +76,76 @@ public class PhoneStatusListener implements AutomationListenerInterface
return currentState;
}
public static class IncomingCallsReceiver extends PhoneStateListener
public static interface IncomingCallsReceiver
{
}
protected static void workWithIncomingCallData(int state, String incomingNumber)
{
if(lastPhoneDirection == 2 && currentState != TelephonyManager.CALL_STATE_IDLE)
{
// This status update is actually for an outgoing call
setCurrentState(state);
if(incomingNumber != null && incomingNumber.length() > 0) // check for null in case call comes in with suppressed number.
setLastPhoneNumber(incomingNumber);
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_IDLE", 4);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_OFFHOOK", 4);
break;
case TelephonyManager.CALL_STATE_RINGING:
Miscellaneous.logEvent("i", "Call state", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.outgoingCallTo), incomingNumber), 4);
break;
}
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).getsGreenLight(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
}
else
{
setCurrentState(state);
setLastPhoneDirection(1);
if (incomingNumber != null && incomingNumber.length() > 0) // check for null in case call comes in with suppressed number.
setLastPhoneNumber(incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_IDLE", 4);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_OFFHOOK", 4);
break;
case TelephonyManager.CALL_STATE_RINGING:
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);
for (int i = 0; i < ruleCandidates.size(); i++)
{
AutomationService asInstance = AutomationService.getInstance();
if (asInstance != null)
if (ruleCandidates.get(i).getsGreenLight(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
}
}
public static class IncomingCallsReceiverOld extends PhoneStateListener implements IncomingCallsReceiver
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
@ -86,65 +161,42 @@ public class PhoneStatusListener implements AutomationListenerInterface
If the last call was outgoing the state has not changed to idle this is kind of a fake alert.
*/
if(lastPhoneDirection == 2 && currentState != TelephonyManager.CALL_STATE_IDLE)
workWithIncomingCallData(state, incomingNumber);
}
}
@RequiresApi(api = Build.VERSION_CODES.S)
public static class IncomingCallsReceiverNew extends BroadcastReceiver implements IncomingCallsReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
/*
this code detects both incoming and outgoing,
if the state changes idle => ringing you know it's an incoming call,
if the state changes idle => offhook, you know it's an outgoing call
*/
if (!intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
{
// This status update is actually for an outgoing call
setCurrentState(state);
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
int state = 99;
if(incomingNumber != null && incomingNumber.length() > 0) // check for null in case call comes in with suppressed number.
setLastPhoneNumber(incomingNumber);
switch(state)
switch(stateStr)
{
case TelephonyManager.CALL_STATE_IDLE:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_IDLE", 4);
case "RINGING":
state = TelephonyManager.CALL_STATE_RINGING;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_OFFHOOK", 4);
case "IDLE":
state = TelephonyManager.CALL_STATE_IDLE;
break;
case TelephonyManager.CALL_STATE_RINGING:
Miscellaneous.logEvent("i", "Call state", String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.outgoingCallTo), incomingNumber), 4);
case "OFFHOOK":
state = TelephonyManager.CALL_STATE_OFFHOOK;
break;
}
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).getsGreenLight(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
}
else
{
setCurrentState(state);
setLastPhoneDirection(1);
if (incomingNumber != null && incomingNumber.length() > 0) // check for null in case call comes in with suppressed number.
setLastPhoneNumber(incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_IDLE", 4);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Miscellaneous.logEvent("i", "Call state", "New call state: CALL_STATE_OFFHOOK", 4);
break;
case TelephonyManager.CALL_STATE_RINGING:
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);
for (int i = 0; i < ruleCandidates.size(); i++)
{
AutomationService asInstance = AutomationService.getInstance();
if (asInstance != null)
if (ruleCandidates.get(i).getsGreenLight(asInstance))
ruleCandidates.get(i).activate(asInstance, false);
}
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("test", "test");
}
}
}
@ -201,7 +253,12 @@ public class PhoneStatusListener implements AutomationListenerInterface
}
if(incomingCallsReceiverInstance == null)
incomingCallsReceiverInstance = new IncomingCallsReceiver();
{
// if(Build.VERSION.SDK_INT >= 31)
// incomingCallsReceiverInstance = new IncomingCallsReceiverNew();
// else
incomingCallsReceiverInstance = new IncomingCallsReceiverOld();
}
if(outgoingCallsReceiverInstance == null)
outgoingCallsReceiverInstance = new OutgoingCallsReceiver();
@ -211,8 +268,17 @@ public class PhoneStatusListener implements AutomationListenerInterface
if(!incomingCallsReceiverActive)
{
Miscellaneous.logEvent("i", "PhoneStatusListener", "Starting PhoneStatusListener->incomingCallsReceiver", 4);
TelephonyManager tm = (TelephonyManager)automationService.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(incomingCallsReceiverInstance, PhoneStateListener.LISTEN_CALL_STATE);
// if(Build.VERSION.SDK_INT >= problematicAndroidLevel)
// {
// IntentFilter callsFilter = new IntentFilter();
// callsFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
// automationService.registerReceiver((IncomingCallsReceiverNew)incomingCallsReceiverInstance, callsFilter);
// }
// else
// {
TelephonyManager tm = (TelephonyManager) automationService.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen((IncomingCallsReceiverOld)incomingCallsReceiverInstance, PhoneStateListener.LISTEN_CALL_STATE);
// }
incomingCallsReceiverActive = true;
}
@ -236,8 +302,15 @@ public class PhoneStatusListener implements AutomationListenerInterface
if(incomingCallsReceiverActive)
{
Miscellaneous.logEvent("i", "PhoneStatusListener", "Stopping phoneStatusListener", 4);
TelephonyManager tm = (TelephonyManager)automationService.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(incomingCallsReceiverInstance, PhoneStateListener.LISTEN_NONE);
// if(Build.VERSION.SDK_INT >= 31)
// {
// automationService.unregisterReceiver((IncomingCallsReceiverNew)incomingCallsReceiverInstance);
// }
// else
// {
TelephonyManager tm = (TelephonyManager) automationService.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen((IncomingCallsReceiverOld)incomingCallsReceiverInstance, PhoneStateListener.LISTEN_NONE);
// }
incomingCallsReceiverActive = false;
}

View File

@ -793,4 +793,7 @@
<string name="wifiMonitoringAlsoRequiresThis">Diese Berechtigung wird auch benötigt, wenn man die aktuelle WLAN Verbindung auslesen möchte.</string>
<string name="copyTextToClipboard">Text in die Zwischenablage kopieren</string>
<string name="textToCopy">Text, der kopiert werden soll</string>
<string name="android.permission.SYSTEM_ALERT_WINDOW">Über anderen Anwendungen anzeigen</string>
<string name="overlayPermissionHint">Nachdem Sie auf OK geklickt haben, werden Sie zu einem Systemdialog weitergeleitet. Bitte wählen Sie dort Automation aus und erlauben Sie die Anzeige über anderen Apps.</string>
<string name="overlayPermissionHint">Nachdem Sie auf OK geklickt haben, werden Sie zu einem Systemdialog weitergeleitet. Bitte wählen Sie dort Automatisierung aus und lassen Sie das Zeichnen über andere Apps zu.</string>
</resources>

View File

@ -792,4 +792,6 @@
<string name="wifiMonitoringAlsoRequiresThis">Este permiso también es necesario si el disparador está utilizando la conexión wifi actual.</string>
<string name="copyTextToClipboard">Copiar texto al portapapeles</string>
<string name="textToCopy">Texto para copiar</string>
<string name="android.permission.SYSTEM_ALERT_WINDOW">Dibujar encima otras aplicaciones</string>
<string name="overlayPermissionHint">Después de hacer cliquear en Aceptar, se le enviará a un cuadro de diálogo del sistema. Seleccione Automation allí y permita dibujar encima otras aplicaciones.</string>
</resources>

View File

@ -792,4 +792,6 @@
<string name="wifiMonitoringAlsoRequiresThis">Cette autorisation est également requise si le déclencheur utilise la connexion wifi actuelle.</string>
<string name="copyTextToClipboard">Copier du texte dans le presse-papiers</string>
<string name="textToCopy">Texte à copier</string>
<string name="android.permission.SYSTEM_ALERT_WINDOW">Dessiner sur d\'autres applications</string>
<string name="overlayPermissionHint">Après avoir cliqué sur OK, vous serez redirigé vers une boîte de dialogue système. Veuillez sélectionner Automatisation et autoriser le tirage sur d\'autres applications.</string>
</resources>

View File

@ -793,4 +793,6 @@
<string name="wifiMonitoringAlsoRequiresThis">Questa autorizzazione è necessaria anche se il trigger utilizza la connessione wifi corrente.</string>
<string name="copyTextToClipboard">Copiare testo negli appunti</string>
<string name="textToCopy">Testo da copiare</string>
<string name="android.permission.SYSTEM_ALERT_WINDOW">Disegna su altre app</string>
<string name="overlayPermissionHint">Dopo aver fatto clic su OK, verrai indirizzato a una finestra di dialogo di sistema. Seleziona Automazione lì e consenti il disegno su altre app.</string>
</resources>

View File

@ -791,5 +791,7 @@
<string name="wifiMonitoringAlsoRequiresThis">Deze toestemming is ook vereist als de trigger de huidige wifi-verbinding gebruikt.</string>
<string name="copyTextToClipboard">Tekst naar klembord kopiëren</string>
<string name="textToCopy">Tekst om te kopiëren</string>
<string name="android.permission.SYSTEM_ALERT_WINDOW">Tekenen over andere apps</string>
<string name="overlayPermissionHint">Nadat u op OK hebt geklikt, wordt u naar een systeemdialoogvenster gestuurd. Selecteer daar Automatisering en sta tekenen toe over andere apps.</string>
</resources>

View File

@ -850,4 +850,6 @@
<string name="wifiMonitoringAlsoRequiresThis">Это разрешение также требуется, если триггер использует текущее соединение Wi-Fi.</string>
<string name="copyTextToClipboard">Копирование текста в буфер обмена</string>
<string name="textToCopy">Текст для копирования</string>
<string name="android.permission.SYSTEM_ALERT_WINDOW">Рисование поверх других приложений</string>
<string name="overlayPermissionHint">После нажатия кнопки «ОК» вы попадете в системный диалог. Пожалуйста, выберите там «Автоматизация» и разрешите рисовать поверх других приложений.</string>
</resources>

View File

@ -882,4 +882,6 @@
<string name="wifiMonitoringAlsoRequiresThis">This permission is also required if the trigger is using the current wifi connection.</string>
<string name="copyTextToClipboard">Copy text to clipboard</string>
<string name="textToCopy">Text to copy</string>
<string name="android.permission.SYSTEM_ALERT_WINDOW">Draw over other apps</string>
<string name="overlayPermissionHint">After clicking OK you\'ll be sent to a system dialog. Please select Automation there and allow "draw over other apps".</string>
</resources>

View File

@ -0,0 +1,2 @@
* Fixed: Introduced overlay permission to enable startActivity() to work from the background
* Fixed: Phone number available as variable again (permission READ_CALL_LOGS required)