From 9a8519d3e3a44d87e1713f1da68658db56e240d7 Mon Sep 17 00:00:00 2001 From: jens Date: Sat, 15 May 2021 19:55:48 +0200 Subject: [PATCH] Phone Listener changed. --- .../java/com/jens/automation2/Rule.java | 2 +- .../java/com/jens/automation2/Rule.java | 2 +- .../java/com/jens/automation2/Rule.java | 3 +- .../ActivityManageActionSendTextMessage.java | 9 +- .../ActivityManageTriggerPhoneCall.java | 96 ++++++++++++++++++- .../com/jens/automation2/Miscellaneous.java | 29 ++++++ .../activity_manage_trigger_phone_call.xml | 13 +++ app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values/strings.xml | 4 +- 10 files changed, 149 insertions(+), 13 deletions(-) diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 431c831e..89034ef1 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -605,7 +605,7 @@ public class Rule implements Comparable String[] elements = oneTrigger.getTriggerParameter2().split(triggerParameter2Split); // state dir number - if(elements[2].equals(Trigger.triggerPhoneCallNumberAny) || PhoneStatusListener.getLastPhoneNumber().matches(elements[2])) + if(elements[2].equals(Trigger.triggerPhoneCallNumberAny) || Miscellaneous.comparePhoneNumbers(PhoneStatusListener.getLastPhoneNumber(), elements[2]) || (Miscellaneous.isRegularExpression(elements[2]) && PhoneStatusListener.getLastPhoneNumber().matches(elements[2]))) { //if(PhoneStatusListener.isInACall() == oneTrigger.getTriggerParameter()) if( diff --git a/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java b/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java index 97300b11..731dd7b2 100644 --- a/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java @@ -602,7 +602,7 @@ public class Rule implements Comparable String[] elements = oneTrigger.getTriggerParameter2().split(triggerParameter2Split); // state dir number - if(elements[2].equals(Trigger.triggerPhoneCallNumberAny) || PhoneStatusListener.getLastPhoneNumber().matches(elements[2])) + if(elements[2].equals(Trigger.triggerPhoneCallNumberAny) || Miscellaneous.comparePhoneNumbers(PhoneStatusListener.getLastPhoneNumber(), elements[2]) || (Miscellaneous.isRegularExpression(elements[2]) && PhoneStatusListener.getLastPhoneNumber().matches(elements[2]))) { //if(PhoneStatusListener.isInACall() == oneTrigger.getTriggerParameter()) if( diff --git a/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java b/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java index 21b0ef5f..0616500d 100644 --- a/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/googlePlayFlavor/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; @@ -603,7 +604,7 @@ public class Rule implements Comparable String[] elements = oneTrigger.getTriggerParameter2().split(triggerParameter2Split); // state dir number - if(elements[2].equals(Trigger.triggerPhoneCallNumberAny) || PhoneStatusListener.getLastPhoneNumber().matches(elements[2])) + if(elements[2].equals(Trigger.triggerPhoneCallNumberAny) || Miscellaneous.comparePhoneNumbers(PhoneStatusListener.getLastPhoneNumber(), elements[2]) || (Miscellaneous.isRegularExpression(elements[2]) && PhoneStatusListener.getLastPhoneNumber().matches(elements[2]))) { //if(PhoneStatusListener.isInACall() == oneTrigger.getTriggerParameter()) if( diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java b/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java index 5419eeae..54abcb32 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java @@ -25,6 +25,7 @@ public class ActivityManageActionSendTextMessage extends Activity EditText etPhoneNumber, etSendTextMessage; protected final static int requestCodeForContactsPermissions = 9876; + protected final static int requestCodeGetContact = 3235; // private String existingUrl = ""; @@ -145,16 +146,14 @@ public class ActivityManageActionSendTextMessage extends Activity private void openContactsDialogue() { -// Toast.makeText(ActivityEditSendTextMessage.this, "Opening contacts dialogue", Toast.LENGTH_LONG).show(); - - Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); - startActivityForResult(intent, 1000); + Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); + startActivityForResult(intent, requestCodeGetContact); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if(requestCode == 1000) + if(requestCode == requestCodeGetContact) { if(resultCode == Activity.RESULT_OK) { diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerPhoneCall.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerPhoneCall.java index d120bfa2..f55148f1 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerPhoneCall.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerPhoneCall.java @@ -3,12 +3,19 @@ package com.jens.automation2; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; +import android.content.pm.PackageManager; +import android.database.Cursor; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; +import android.provider.ContactsContract; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; +import androidx.annotation.NonNull; + import static com.jens.automation2.Trigger.triggerParameter2Split; public class ActivityManageTriggerPhoneCall extends Activity @@ -17,6 +24,8 @@ public class ActivityManageTriggerPhoneCall extends Activity boolean edit = false; public static Trigger resultingTrigger; ProgressDialog progressDialog = null; + protected final static int requestCodeForContactsPermissions = 2345; + protected final static int requestCodeGetContact = 3235; EditText etTriggerPhoneCallPhoneNumber; RadioButton rbTriggerPhoneCallStateRinging, rbTriggerPhoneCallStateStarted, rbTriggerPhoneCallStateStopped, rbTriggerPhoneCallDirectionAny, rbTriggerPhoneCallDirectionIncoming, rbTriggerPhoneCallDirectionOutgoing; @@ -86,6 +95,20 @@ public class ActivityManageTriggerPhoneCall extends Activity } }); + bTriggerPhoneCallImportFromContacts.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !ActivityPermissions.havePermission("android.permission.READ_CONTACTS", ActivityManageTriggerPhoneCall.this)) + { + requestPermissions("android.permission.READ_CONTACTS"); + } + else + openContactsDialogue(); + } + }); + Intent i = getIntent(); if(i.getBooleanExtra("edit", false) == true) { @@ -94,6 +117,31 @@ public class ActivityManageTriggerPhoneCall extends Activity } } + protected void requestPermissions(String... requiredPermissions) + { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + { + if(requiredPermissions.length > 0) + { + StringBuilder permissions = new StringBuilder(); + for (String perm : requiredPermissions) + permissions.append(perm + "; "); + if (permissions.length() > 0) + permissions.delete(permissions.length() - 2, permissions.length()); + + Miscellaneous.logEvent("i", "Permissions", "Requesting permissions: " + permissions, 2); + + requestPermissions(requiredPermissions, requestCodeForContactsPermissions); + } + } + } + + private void openContactsDialogue() + { + Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); + startActivityForResult(intent, requestCodeGetContact); + } + private void loadValuesIntoGui() { String[] parts = editedPhoneCallTrigger.getTriggerParameter2().split(triggerParameter2Split); @@ -115,4 +163,50 @@ public class ActivityManageTriggerPhoneCall extends Activity if(!parts[2].equals(Trigger.triggerPhoneCallNumberAny)) etTriggerPhoneCallPhoneNumber.setText(parts[2]); } -} + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) + { + if(requestCode == requestCodeGetContact) + { + if(resultCode == Activity.RESULT_OK) + { + String phoneNo = null; + String name = null; + + Uri uri = data.getData(); + Cursor cursor = ActivityManageTriggerPhoneCall.this.getContentResolver().query(uri, null, null, null, null); + + if (cursor.moveToFirst()) + { + int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); + int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); + + phoneNo = cursor.getString(phoneIndex); + name = cursor.getString(nameIndex); + + etTriggerPhoneCallPhoneNumber.setText(phoneNo); + } + } + } + //super.onActivityResult(requestCode, resultCode, data); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) + { + if(requestCode == requestCodeForContactsPermissions) + { + for(int i=0; i Build.VERSION_CODES.Q) + { + TelephonyManager tm = (TelephonyManager)Miscellaneous.getAnyContext().getSystemService(Context.TELEPHONY_SERVICE); + return PhoneNumberUtils.areSamePhoneNumber(number1, number2, tm.getNetworkCountryIso()); + } + else*/ + return PhoneNumberUtils.compare(number1, number2); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_manage_trigger_phone_call.xml b/app/src/main/res/layout/activity_manage_trigger_phone_call.xml index 1aceb61f..b4629864 100644 --- a/app/src/main/res/layout/activity_manage_trigger_phone_call.xml +++ b/app/src/main/res/layout/activity_manage_trigger_phone_call.xml @@ -153,6 +153,19 @@ + + + + + + diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 03704451..640deaf4 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -653,7 +653,7 @@ Keine Dateien konnten importiert werden. Nicht alle passenden Dateien konnten importiert werden. Webseite mit Beispielen öffnen - Sie können eine bestimmte Nummer eingeben, aber müssen nicht. Wenn Sie eine angeben wollen, können Sie auch eine aus dem Adressbuch auswählen. Außerdem können Sie reguläre Audrücke verwenden. + Sie können eine bestimmte Nummer eingeben, aber müssen nicht. Wenn Sie eine angeben wollen, können Sie auch eine aus dem Adressbuch auswählen. Außerdem können Sie reguläre Audrücke verwenden. Zum Testen selbiger mag ich die Seite: Fehler beim Importieren der Einstellungen. Regeln und Orte wurden erfolgreich importiert. Fehler beim Importieren der Regeln. diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 8b28bcaa..ba1d523a 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -312,5 +312,5 @@ Valor del parámetro Añadir pareja intento Tipo del parámetro - Puedes entrar un numero, pero es opciónal. Si quieres usar un puedes elegir un de su directorio o entrar un manualmente. Adiciónalmente puedes usar expresiónes regulares. + Puedes entrar un numero, pero es opciónal. Si quieres usar un puedes elegir un de su directorio o entrar un manualmente. Adiciónalmente puedes usar expresiónes regulares. Para testar esos me gusta la pagina: \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 27b4f2e4..db911692 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -644,7 +644,7 @@ Enter a valid action Enter a valid package name. State - You can enter a specific phone number, but you don\'t have to. If you want to specify one you can either pick one from your address book or enter it manually. In addition you may use regular expressions. + You can enter a specific phone number, but you don\'t have to. If you want to specify one you can either pick one from your address book or enter it manually. In addition you may use regular expressions. To test a regular expression I like this page: Import configuration Export configuration More settings @@ -671,5 +671,5 @@ from to matching - + https://regex101.com/ \ No newline at end of file