Wifi trigger mgmt change

This commit is contained in:
jens 2021-05-16 14:27:54 +02:00
parent 4a18a6ed19
commit 8ce2a09b3b
9 changed files with 239 additions and 66 deletions

View File

@ -549,12 +549,12 @@ public class Rule implements Comparable<Rule>
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

View File

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

View File

@ -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<String> wifiList = new ArrayList<>();
ArrayAdapter<String> 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<String>(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;
}
}
}

View File

@ -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<EFBFBD><EFBFBD>te ein Hinweis kommen, da<EFBFBD> nur die Variablen verwendet werden k<EFBFBD>nnen, f<EFBFBD>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<String> checkPermissionsInVariableUse(String text, ArrayList<String> 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);

View File

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

View File

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

View File

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

View File

@ -55,22 +55,26 @@
android:paddingRight="@dimen/default_margin"
android:text="@string/name"/>
<androidx.appcompat.widget.LinearLayoutCompat
<EditText
android:id="@+id/etTriggerWifiName"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</TableRow>
<TableRow>
<Button
android:id="@+id/bLoadWifiList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loadWifiList" />
<Spinner
android:id="@+id/spinnerWifiList"
android:enabled="false"
android:layout_width="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etTriggerWifiName"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<Spinner
android:id="@+id/spinnerWifiList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.appcompat.widget.LinearLayoutCompat>
android:layout_height="wrap_content" />
</TableRow>
@ -78,6 +82,7 @@
<Button
android:id="@+id/btriggerWifiSave"
android:layout_marginTop="@dimen/default_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save" />

View File

@ -672,4 +672,7 @@
<string name="to">to</string>
<string name="matching">matching</string>
<string name="urlRegex" translatable="false">https://regex101.com/</string>
<string name="loadWifiList">Load wifi list</string>
<string name="needLocationPermForWifiList">For some reason unbeknownst to mankind the location permission is required to view the list of wifis. If you want to be able to pick one from the list you need to grant that permission. If not you can enter your wifi name manually.</string>
<string name="noKnownWifis">There are no known wifis on your device.</string>
</resources>