From f9f9b301165330f0e2fb2cc274ab91c3236137ba Mon Sep 17 00:00:00 2001 From: Jens Date: Thu, 14 Jul 2022 22:55:54 +0200 Subject: [PATCH 1/3] su changes --- .../java/com/jens/automation2/Actions.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index d9cd7fd0..f5462af9 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -83,10 +83,6 @@ public class Actions public static AutomationService automationServerRef; public static Context context; private static Intent playMusicIntent; - private static boolean suAvailable = false; - private static String suVersion = null; - private static String suVersionInternal = null; - private static List suResult = null; public final static String smsSeparator = "&sms&"; public final static String dummyPackageString = "dummyPkg239asd"; @@ -1865,6 +1861,12 @@ public class Actions protected static boolean executeCommandViaSu(String[] commands) { + boolean suAvailable = false; + String suVersion = null; + String suVersionInternal = null; +// List suResult = null; + int suResult; + boolean success = false; try @@ -1874,17 +1876,30 @@ public class Actions { suVersion = Shell.SU.version(false); suVersionInternal = Shell.SU.version(true); - suResult = Shell.SU.run(commands); - if (suResult != null) + Miscellaneous.logEvent("i", "executeCommandViaSu()", "suVersion: " + suVersion + ", suVersionInternal: " + suVersionInternal, 5); + +// suResult = Shell.SU.run(commands); + suResult = Shell.Pool.SU.run(commands); + +// if (suResult != null) +// success = true; + + Miscellaneous.logEvent("i", "executeCommandViaSu()", "RC=" + String.valueOf(suResult), 3); + + if(suResult == 0) success = true; } + else + Miscellaneous.logEvent("w", "executeCommandViaSu()", "su not available.", 4); } catch (Exception e) { success = false; } + Miscellaneous.logEvent("i", "executeCommandViaSu()", "Returning " + String.valueOf(success), 4); + return success; } From d07c1a05e48795b2e29c5c450ca3c4286be12878 Mon Sep 17 00:00:00 2001 From: Jens Date: Fri, 15 Jul 2022 22:37:55 +0200 Subject: [PATCH 2/3] set wifi with root fix --- .../java/com/jens/automation2/Action.java | 2 +- .../java/com/jens/automation2/Actions.java | 27 ++++++++++++++++--- .../res/layout/activity_control_center.xml | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index 528924c8..6c9aae2d 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -491,7 +491,7 @@ public class Action Actions.setUsbTethering(context, getParameter1(), toggleActionIfPossible); break; case setWifi: - Actions.WifiStuff.setWifi(context, getParameter1(), toggleActionIfPossible); + Actions.WifiStuff.setWifi(context, getParameter1(), getParameter2(), toggleActionIfPossible); break; case setWifiTethering: Actions.setWifiTethering(context, getParameter1(), toggleActionIfPossible); diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index f5462af9..d907475e 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -44,6 +44,7 @@ import com.jens.automation2.location.WifiBroadcastReceiver; import com.jens.automation2.receivers.ConnectivityReceiver; import com.jens.automation2.receivers.NotificationListener; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.client.HttpClient; import org.apache.http.conn.ClientConnectionManager; @@ -211,9 +212,20 @@ public class Actions public static class WifiStuff { - public static Boolean setWifi(Context context, Boolean desiredState, boolean toggleActionIfPossible) + public static Boolean setWifi(Context context, Boolean desiredState, String parameter2, boolean toggleActionIfPossible) { - if(context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.Q) + boolean forceUseRoot = false; + + try + { + forceUseRoot = Boolean.parseBoolean(parameter2); + } + catch(Exception e) + { + + } + + if(context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.Q || forceUseRoot) return setWifiWithRoot(context, desiredState, toggleActionIfPossible); else return setWifiOldFashioned(context, desiredState, toggleActionIfPossible); @@ -249,8 +261,15 @@ public class Actions { Miscellaneous.logEvent("i", "Wifi", "Changing wifi to " + String.valueOf(desiredState), 4); - if (desiredState && Settings.useWifiForPositioning) - WifiBroadcastReceiver.startWifiReceiver(automationServerRef.getLocationProvider()); + try + { + if (desiredState && Settings.useWifiForPositioning) + WifiBroadcastReceiver.startWifiReceiver(automationServerRef.getLocationProvider()); + } + catch(Exception e) + { + Miscellaneous.logEvent("w", "setWifiOldFashioned()", Log.getStackTraceString(e), 4); + } WifiManager myWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); diff --git a/app/src/main/res/layout/activity_control_center.xml b/app/src/main/res/layout/activity_control_center.xml index 6dcdaa0a..cada1108 100644 --- a/app/src/main/res/layout/activity_control_center.xml +++ b/app/src/main/res/layout/activity_control_center.xml @@ -100,6 +100,7 @@ android:id="@+id/bSendEmailToDev" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginBottom="@dimen/default_margin" android:text="@string/sendEmailToDev" /> Date: Sun, 17 Jul 2022 23:40:17 +0200 Subject: [PATCH 3/3] wifi trigger needs location permission --- .../apkFlavor/java/com/jens/automation2/Rule.java | 2 +- .../java/com/jens/automation2/Rule.java | 2 +- .../java/com/jens/automation2/Rule.java | 2 +- .../jens/automation2/ActivityManageTriggerWifi.java | 10 ++++++++++ .../com/jens/automation2/ActivityPermissions.java | 6 ++++++ .../java/com/jens/automation2/Miscellaneous.java | 5 +++++ .../automation2/location/WifiBroadcastReceiver.java | 2 +- .../res/layout/activity_manage_trigger_wifi.xml | 13 +++++++++++++ app/src/main/res/values/strings.xml | 1 + 9 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index a04976cd..842c4c6c 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -396,7 +396,7 @@ public class Rule implements Comparable return true; } - Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleIsDeactivatedCantApply), this.getName()), 3); + Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleIsDeactivatedCantApply), this.getName()), 4); return false; } diff --git a/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java b/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java index fd6b6c71..faffcdfe 100644 --- a/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/fdroidFlavor/java/com/jens/automation2/Rule.java @@ -393,7 +393,7 @@ public class Rule implements Comparable return true; } - Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleIsDeactivatedCantApply), this.getName()), 3); + Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleIsDeactivatedCantApply), this.getName()), 4); return false; } diff --git a/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java b/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java index b4306bcd..e433e507 100644 --- a/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/googlePlayFlavor/java/com/jens/automation2/Rule.java @@ -396,7 +396,7 @@ public class Rule implements Comparable return true; } - Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleIsDeactivatedCantApply), this.getName()), 3); + Miscellaneous.logEvent("i", String.format(context.getResources().getString(R.string.ruleCheckOf), this.getName()), String.format(context.getResources().getString(R.string.ruleIsDeactivatedCantApply), this.getName()), 4); return false; } diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerWifi.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerWifi.java index 99e17f1e..5a1abe9e 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerWifi.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerWifi.java @@ -20,6 +20,7 @@ import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Spinner; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; @@ -42,6 +43,7 @@ public class ActivityManageTriggerWifi extends Activity List wifiList = new ArrayList<>(); ArrayAdapter wifiSpinnerAdapter; private final static int requestCodeLocationPermission = 124; + TextView tvWifiTriggerNameLocationNotice; @Override protected void onCreate(@Nullable Bundle savedInstanceState) @@ -55,11 +57,19 @@ public class ActivityManageTriggerWifi extends Activity spinnerWifiList = (Spinner) findViewById(R.id.spinnerWifiList); bTriggerWifiSave = (Button) findViewById(R.id.bTriggerWifiSave); bLoadWifiList = (Button) findViewById(R.id.bLoadWifiList); + tvWifiTriggerNameLocationNotice = (TextView)findViewById(R.id.tvWifiTriggerNameLocationNotice); wifiSpinnerAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, wifiList); spinnerWifiList.setAdapter(wifiSpinnerAdapter); spinnerWifiList.setEnabled(false); // bug in Android; this only works when done in code, not in xml + if( + Miscellaneous.getTargetSDK(Miscellaneous.getAnyContext()) >= 29 + && + !ActivityPermissions.isPermissionDeclaratedInManifest(Miscellaneous.getAnyContext(), Manifest.permission.ACCESS_BACKGROUND_LOCATION) + ) + tvWifiTriggerNameLocationNotice.setVisibility(View.VISIBLE); + if (getIntent().hasExtra("edit")) { boolean connected = getIntent().getBooleanExtra("wifiState", false); diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index 34d04caf..915e802a 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -509,6 +509,12 @@ public class ActivityPermissions extends Activity case wifiConnection: addToArrayListUnique(Manifest.permission.ACCESS_NETWORK_STATE, requiredPermissions); addToArrayListUnique(Manifest.permission.ACCESS_WIFI_STATE, requiredPermissions); + if( + Miscellaneous.getTargetSDK(Miscellaneous.getAnyContext()) >= 29 + && + isPermissionDeclaratedInManifest(Miscellaneous.getAnyContext(), Manifest.permission.ACCESS_BACKGROUND_LOCATION) + ) + addToArrayListUnique(Manifest.permission.ACCESS_BACKGROUND_LOCATION, requiredPermissions); break; case notification: addToArrayListUnique(Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE, requiredPermissions); diff --git a/app/src/main/java/com/jens/automation2/Miscellaneous.java b/app/src/main/java/com/jens/automation2/Miscellaneous.java index 6ebc28f2..3a0f9310 100644 --- a/app/src/main/java/com/jens/automation2/Miscellaneous.java +++ b/app/src/main/java/com/jens/automation2/Miscellaneous.java @@ -1911,4 +1911,9 @@ public class Miscellaneous extends Service return null; } + + public static int getTargetSDK(Context context) + { + return context.getApplicationContext().getApplicationInfo().targetSdkVersion; + } } \ No newline at end of file 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 79e93d9c..2ecfa13e 100644 --- a/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java +++ b/app/src/main/java/com/jens/automation2/location/WifiBroadcastReceiver.java @@ -63,7 +63,7 @@ public class WifiBroadcastReceiver extends BroadcastReceiver // int state = -1; NetworkInfo myWifi = null; - if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) //gefeuert bei Trennung + if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) // fired upon disconnection { // state = intent.getIntExtra(WifiManager.NETWORK_STATE_CHANGED_ACTION, -1); // Miscellaneous.logEvent("i", "WifiReceiver", "NETWORK_STATE_CHANGED_ACTION: " + String.valueOf(state)); 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 c21adcfb..aad445ce 100644 --- a/app/src/main/res/layout/activity_manage_trigger_wifi.xml +++ b/app/src/main/res/layout/activity_manage_trigger_wifi.xml @@ -47,6 +47,19 @@ + + + + + Specify timeout Activate will keep the screen on while deactivate will let it turn off. For keeping it on you can either enter a duration after which it may turn off again or you can keep it on indefinitely. In the latter case you would need to create another keep-awake action (in this or another rule) in which you select deactivate. You may also enter a regular expression here. + To get the wifi SSID, the app requires location permissions. Because the Google version cannot get that, you can only use this trigger if you do not specify an SSID. So it can only be used to detect if wifi is connected or not. \ No newline at end of file