From 8ce2a09b3bed1a35b36bd8280bb498fb3aad61a2 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 16 May 2021 14:27:54 +0200 Subject: [PATCH] Wifi trigger mgmt change --- .../java/com/jens/automation2/Rule.java | 6 +- .../jens/automation2/ActivityManageRule.java | 61 ++++++---- .../ActivityManageTriggerWifi.java | 115 +++++++++++++++++- .../jens/automation2/ActivityPermissions.java | 63 +++++++++- .../java/com/jens/automation2/Trigger.java | 16 +-- .../jens/automation2/XmlFileInterface.java | 4 +- .../location/WifiBroadcastReceiver.java | 4 +- .../layout/activity_manage_trigger_wifi.xml | 33 ++--- app/src/main/res/values/strings.xml | 3 + 9 files changed, 239 insertions(+), 66 deletions(-) diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 89034ef1..3c0a35e8 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -549,12 +549,12 @@ public class Rule implements Comparable Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), "Checking for wifi state", 4); if(oneTrigger.getTriggerParameter() == WifiBroadcastReceiver.lastConnectedState) // connected / disconnected { - if(oneTrigger.getWifiName().length() > 0) // only check if any wifi name specified, otherwise any wifi will do + if(oneTrigger.getTriggerParameter2().length() > 0) // only check if any wifi name specified, otherwise any wifi will do { Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), "Wifi name specified, checking that.", 4); - if(!WifiBroadcastReceiver.getLastWifiSsid().equals(oneTrigger.getWifiName())) + if(!WifiBroadcastReceiver.getLastWifiSsid().equals(oneTrigger.getTriggerParameter2())) { - Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleDoesntApplyNotTheCorrectSsid), oneTrigger.getWifiName(), WifiBroadcastReceiver.getLastWifiSsid()), 3); + Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleDoesntApplyNotTheCorrectSsid), oneTrigger.getTriggerParameter2(), WifiBroadcastReceiver.getLastWifiSsid()), 3); return false; } else diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index ccbf0c6f..a49cd5fc 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -101,6 +101,8 @@ public class ActivityManageRule extends Activity final static int requestCodeActionPlaySoundEdit = 502; final static int requestCodeTriggerPhoneCallAdd = 601; final static int requestCodeTriggerPhoneCallEdit = 602; + final static int requestCodeTriggerWifiAdd = 723; + final static int requestCodeTriggerWifiEdit = 724; public static ActivityManageRule getInstance() { @@ -264,6 +266,13 @@ public class ActivityManageRule extends Activity phoneCallEditor.putExtra("edit", true); startActivityForResult(phoneCallEditor, requestCodeTriggerPhoneCallEdit); break; + case wifiConnection: + Intent wifiEditor = new Intent(ActivityManageRule.this, ActivityManageTriggerWifi.class); + wifiEditor.putExtra("edit", true); + wifiEditor.putExtra("wifiState", selectedTrigger.getTriggerParameter()); + wifiEditor.putExtra("wifiName", selectedTrigger.getTriggerParameter2()); + startActivityForResult(wifiEditor, requestCodeTriggerWifiEdit); + break; default: break; } @@ -313,31 +322,10 @@ public class ActivityManageRule extends Activity break; case triggerUrl: Intent activityEditTriggerUrlIntent = new Intent(ActivityManageRule.this, ActivityManageActionTriggerUrl.class); -// activityEditTriggerUrlIntent.putExtra("urlToTrigger", a.getParameter2()); ActivityManageActionTriggerUrl.resultingAction = a; activityEditTriggerUrlIntent.putExtra("edit", true); startActivityForResult(activityEditTriggerUrlIntent, requestCodeActionTriggerUrlEdit); break; -// case turnBluetoothOff: -// break; -// case turnBluetoothOn: -// break; -// case turnUsbTetheringOff: -// break; -// case turnUsbTetheringOn: -// break; -// case turnWifiOff: -// break; -// case turnWifiOn: -// break; -// case turnWifiTetheringOff: -// break; -// case turnWifiTetheringOn: -// break; -// case waitBeforeNextAction: -// break; -// case wakeupDevice: -// break; case speakText: Intent activitySpeakTextIntent = new Intent(ActivityManageRule.this, ActivityManageActionSpeakText.class); ActivityManageActionSpeakText.resultingAction = a; @@ -560,7 +548,15 @@ public class ActivityManageRule extends Activity else if(triggerType == Trigger_Enum.speed | triggerType == Trigger_Enum.noiseLevel | triggerType == Trigger_Enum.batteryLevel) booleanChoices = new String[]{getResources().getString(R.string.exceeds), getResources().getString(R.string.dropsBelow)}; else if(triggerType == Trigger_Enum.wifiConnection) - booleanChoices = new String[]{getResources().getString(R.string.connected), getResources().getString(R.string.disconnected)}; + { + newTrigger.setTriggerType(Trigger_Enum.wifiConnection); + Intent wifiTriggerEditor = new Intent(myContext, ActivityManageTriggerWifi.class); + startActivityForResult(wifiTriggerEditor, requestCodeTriggerWifiAdd); + return; +// booleanChoices = new String[]{getResources().getString(R.string.started), getResources().getString(R.string.stopped)}; + } +// else if(triggerType == Trigger_Enum.wifiConnection) +// booleanChoices = new String[]{getResources().getString(R.string.connected), getResources().getString(R.string.disconnected)}; else if(triggerType == Trigger_Enum.process_started_stopped) booleanChoices = new String[]{getResources().getString(R.string.started), getResources().getString(R.string.stopped)}; else if(triggerType == Trigger_Enum.notification) @@ -840,7 +836,7 @@ public class ActivityManageRule extends Activity { public void onClick(DialogInterface dialog, int whichButton) { - newTrigger.setWifiName(input.getText().toString()); +// newTrigger.setWifiName(input.getText().toString()); ruleToEdit.getTriggerSet().add(newTrigger); refreshTriggerList(); } @@ -1147,6 +1143,25 @@ public class ActivityManageRule extends Activity else Miscellaneous.logEvent("w", "TimeFrameEdit", "No timeframe returned. Assuming abort.", 5); } + else if(requestCode == requestCodeTriggerWifiAdd) + { + if(resultCode == RESULT_OK) + { + newTrigger.setTriggerParameter(data.getBooleanExtra("wifiState", false)); + newTrigger.setTriggerParameter2(data.getStringExtra("wifiName")); + ruleToEdit.getTriggerSet().add(newTrigger); + this.refreshTriggerList(); + } + } + else if(requestCode == requestCodeTriggerWifiEdit) + { + if(resultCode == RESULT_OK) + { + newTrigger.setTriggerParameter(data.getBooleanExtra("wifiState", false)); + newTrigger.setTriggerParameter2(data.getStringExtra("wifiName")); + this.refreshTriggerList(); + } + } else if(requestCode == requestCodeActionStartActivityAdd) { // manage start of other activity diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerWifi.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerWifi.java index 23565e51..cc30f047 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerWifi.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerWifi.java @@ -1,22 +1,42 @@ package com.jens.automation2; +import android.Manifest; import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Spinner; +import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.app.ActivityCompat; + +import com.jens.automation2.receivers.BluetoothReceiver; + +import java.util.ArrayList; +import java.util.List; public class ActivityManageTriggerWifi extends Activity { RadioButton rbTriggerWifiConnected, rbTriggerWifiDisconnected; EditText etTriggerWifiName; Spinner spinnerWifiList; - Button btriggerWifiSave; + Button btriggerWifiSave, bLoadWifiList; + List wifiList = new ArrayList<>(); + ArrayAdapter wifiSpinnerAdapter; + private final static int requestCodeLocationPermission = 124; @Override protected void onCreate(@Nullable Bundle savedInstanceState) @@ -24,13 +44,17 @@ public class ActivityManageTriggerWifi extends Activity super.onCreate(savedInstanceState); setContentView(R.layout.activity_manage_trigger_wifi); - rbTriggerWifiConnected = (RadioButton)findViewById(R.id.rbTriggerWifiConnected); - rbTriggerWifiDisconnected = (RadioButton)findViewById(R.id.rbTriggerWifiDisconnected); + rbTriggerWifiConnected = (RadioButton) findViewById(R.id.rbTriggerWifiConnected); + rbTriggerWifiDisconnected = (RadioButton) findViewById(R.id.rbTriggerWifiDisconnected); etTriggerWifiName = (EditText) findViewById(R.id.etTriggerWifiName); - spinnerWifiList = (Spinner) findViewById(R.id. spinnerWifiList); - btriggerWifiSave = (Button) findViewById(R.id. btriggerWifiSave); + spinnerWifiList = (Spinner) findViewById(R.id.spinnerWifiList); + btriggerWifiSave = (Button) findViewById(R.id.btriggerWifiSave); + bLoadWifiList = (Button) findViewById(R.id.bLoadWifiList); - if(getIntent().hasExtra("edit")) + wifiSpinnerAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, wifiList); + spinnerWifiList.setAdapter(wifiSpinnerAdapter); + + if (getIntent().hasExtra("edit")) { boolean connected = getIntent().getBooleanExtra("wifiState", false); String wifiName = getIntent().getStringExtra("wifiName"); @@ -49,7 +73,86 @@ public class ActivityManageTriggerWifi extends Activity Intent response = new Intent(); response.putExtra("wifiState", rbTriggerWifiConnected.isChecked()); response.putExtra("wifiName", etTriggerWifiName.getText().toString()); + setResult(RESULT_OK, response); + finish(); + } + }); + + spinnerWifiList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() + { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) + { + etTriggerWifiName.setText(wifiList.get(position)); + } + + @Override + public void onNothingSelected(AdapterView parent) + { + + } + }); + + bLoadWifiList.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + loadWifis(); } }); } + + public void loadWifis() + { + if(!ActivityPermissions.havePermission(Manifest.permission.ACCESS_FINE_LOCATION, ActivityManageTriggerWifi.this)) + { + AlertDialog dialog = Miscellaneous.messageBox(getResources().getString(R.string.permissionsTitle), getResources().getString(R.string.needLocationPermForWifiList), ActivityManageTriggerWifi.this); + dialog.setOnDismissListener(new DialogInterface.OnDismissListener() + { + @Override + public void onDismiss(DialogInterface dialog) + { + ActivityCompat.requestPermissions(ActivityManageTriggerWifi.this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, requestCodeLocationPermission); + } + }); + dialog.show(); + } + else + { + reallyLoadWifiList(); + } + } + + void reallyLoadWifiList() + { + WifiManager myWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); + + for (WifiConfiguration wifi : myWifiManager.getConfiguredNetworks()) + wifiList.add(wifi.SSID.replaceAll("\"+$", "").replaceAll("^\"+", "")); + + if(wifiList.size() > 0) + spinnerWifiList.setEnabled(true); + else + { + spinnerWifiList.setEnabled(false); + Toast.makeText(ActivityManageTriggerWifi.this, getResources().getString(R.string.noKnownWifis), Toast.LENGTH_SHORT).show(); + } + + wifiSpinnerAdapter.notifyDataSetChanged(); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) + { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + switch (requestCode) + { + case requestCodeLocationPermission: + if(grantResults[0] == PackageManager.PERMISSION_GRANTED) + reallyLoadWifiList(); + break; + } + } } diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index c3fb0aea..714edb1a 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -511,6 +511,7 @@ public class ActivityPermissions extends Activity break; case sendTextMessage: addToArrayListUnique("android.permission.SEND_SMS", requiredPermissions); + checkPermissionsInVariableUse(action.getParameter2(), requiredPermissions); break; case setAirplaneMode: addToArrayListUnique(writeSystemSettingsPermissionName, requiredPermissions); @@ -555,6 +556,7 @@ public class ActivityPermissions extends Activity // addToArrayListUnique("android.permission.TETHER_PRIVILEGED", requiredPermissions); break; case speakText: + checkPermissionsInVariableUse(action.getParameter2(), requiredPermissions); break; case startOtherActivity: if( @@ -574,7 +576,7 @@ public class ActivityPermissions extends Activity break; case triggerUrl: addToArrayListUnique("android.permission.INTERNET", requiredPermissions); - // Hier m��te ein Hinweis kommen, da� nur die Variablen verwendet werden k�nnen, f�r die es Rechte gibt. + checkPermissionsInVariableUse(action.getParameter2(), requiredPermissions); break; case turnBluetoothOff: addToArrayListUnique("android.permission.BLUETOOTH_ADMIN", requiredPermissions); @@ -623,7 +625,6 @@ public class ActivityPermissions extends Activity break; default: break; - } } } @@ -1096,6 +1097,64 @@ public class ActivityPermissions extends Activity } } + static ArrayList checkPermissionsInVariableUse(String text, ArrayList permsList) + { + /* + [uniqueid] + [serialnr] + [latitude] + [longitude] + [phonenr] + [d] + [m] + [Y] + [h] + [H] + [i] + [s] + [ms] + [notificationTitle] + [notificationText] + */ + + if(text.contains("[uniqueid]")) + { + + } + else if(text.contains("[serialnr]")) + { + + } + else if(text.contains("[latitude]") || text.contains("[longitude]")) + { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) + { + addToArrayListUnique(permissionNameLocationBackground, permsList); + addToArrayListUnique(permissionNameLocationFine, permsList); + addToArrayListUnique(permissionNameLocationCoarse, permsList); + } + else + { + addToArrayListUnique(permissionNameLocationFine, permsList); + addToArrayListUnique(permissionNameLocationCoarse, permsList); + } + addToArrayListUnique("android.permission.ACCESS_NETWORK_STATE", permsList); + addToArrayListUnique("android.permission.INTERNET", permsList); + addToArrayListUnique("android.permission.ACCESS_WIFI_STATE", permsList); + } + else if(text.contains("[phonenr]")) + { + addToArrayListUnique("android.permission.READ_PHONE_STATE", permsList); + addToArrayListUnique(permissionNameCall, permsList); + } + else if(text.contains("[notificationTitle]") || text.contains("[notificationTitle]")) + { + addToArrayListUnique(permissionNameReadNotifications, permsList); + } + + return permsList; + } + private void setHaveAllPermissions() { setResult(RESULT_OK); diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index f77be33b..04e41983 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -87,8 +87,7 @@ public class Trigger private double speed; //km/h private long noiseLevelDb; - private String wifiName = ""; - private String processName = null; + private String processName = null; private int batteryLevel; private int phoneDirection = 0; // 0=any, 1=incoming, 2=outgoing private String phoneNumber = null; @@ -314,10 +313,10 @@ public class Trigger break; case wifiConnection: String wifiDisplayName = ""; - if(this.getWifiName().length() == 0) + if(this.getTriggerParameter2().length() == 0) wifiDisplayName += Miscellaneous.getAnyContext().getResources().getString(R.string.anyWifi); else - wifiDisplayName += this.getWifiName(); + wifiDisplayName += this.getTriggerParameter2(); if(getTriggerParameter()) returnString.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.connectedToWifi), wifiDisplayName)); @@ -604,15 +603,6 @@ public class Trigger return (String[])triggerTypesList.toArray(new String[triggerTypesList.size()]); } - public String getWifiName() - { - return wifiName; - } - - public void setWifiName(String wifiName) - { - this.wifiName = wifiName; - } public void setBluetoothEvent(String string) { this.bluetoothEvent = string; diff --git a/app/src/main/java/com/jens/automation2/XmlFileInterface.java b/app/src/main/java/com/jens/automation2/XmlFileInterface.java index f10a0705..7d69f5a7 100644 --- a/app/src/main/java/com/jens/automation2/XmlFileInterface.java +++ b/app/src/main/java/com/jens/automation2/XmlFileInterface.java @@ -255,7 +255,7 @@ public class XmlFileInterface else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.noiseLevel) serializer.text(String.valueOf(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getNoiseLevelDb())); else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.wifiConnection) - serializer.text(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getWifiName()); + serializer.text(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerParameter2()); else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.process_started_stopped) serializer.text(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getProcessName()); else if(Rule.getRuleCollection().get(i).getTriggerSet().get(j).getTriggerType() == Trigger_Enum.batteryLevel) @@ -916,7 +916,7 @@ public class XmlFileInterface } else if(newTrigger.getTriggerType() == Trigger_Enum.wifiConnection) { - newTrigger.setWifiName(triggerParameter2); +// newTrigger.setWifiName(triggerParameter2); newTrigger.setTriggerParameter2(triggerParameter2); } else if(newTrigger.getTriggerType() == Trigger_Enum.process_started_stopped) diff --git a/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java b/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java index 54f8a2f9..63aaca7f 100644 --- a/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java +++ b/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java @@ -28,9 +28,7 @@ public class WifiBroadcastReceiver extends BroadcastReceiver protected static WifiBroadcastReceiver wifiBrInstance; protected static IntentFilter wifiListenerIntentFilter; protected static boolean wifiListenerActive=false; - - - + public static String getLastWifiSsid() { return lastWifiSsid; diff --git a/app/src/main/res/layout/activity_manage_trigger_wifi.xml b/app/src/main/res/layout/activity_manage_trigger_wifi.xml index 0ba29bec..7ea198cb 100644 --- a/app/src/main/res/layout/activity_manage_trigger_wifi.xml +++ b/app/src/main/res/layout/activity_manage_trigger_wifi.xml @@ -55,22 +55,26 @@ android:paddingRight="@dimen/default_margin" android:text="@string/name"/> - + + + + + +