From b5bd332ff5c10e7c47164c89f2a5b3251b747695 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 3 Oct 2021 15:09:07 +0200 Subject: [PATCH] data with root --- .../java/com/jens/automation2/Action.java | 2 +- .../java/com/jens/automation2/Actions.java | 269 ++++++++++++------ .../jens/automation2/ActivityPermissions.java | 12 +- app/src/main/res/values/strings.xml | 1 + 4 files changed, 191 insertions(+), 93 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index 54f2ca8..c35f225 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -365,7 +365,7 @@ public class Action Actions.setUsbTethering(context, getParameter1(), toggleActionIfPossible); break; case setWifi: - Actions.setWifi(context, getParameter1(), toggleActionIfPossible); + Actions.WifiStuff.setWifi(context, getParameter1(), 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 762bb06..612a38b 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -24,7 +24,6 @@ import android.telephony.SmsManager; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.util.Log; -import android.view.View; import android.view.WindowManager; import android.widget.Toast; @@ -73,48 +72,85 @@ public class Actions public static final String wireguard_tunnel_down = "com.wireguard.android.action.SET_TUNNEL_DOWN"; public static final String wireguard_tunnel_refresh = "com.wireguard.android.action.REFRESH_TUNNEL_STATES"; - public static Boolean setWifi(Context context, Boolean desiredState, boolean toggleActionIfPossible) + public static class WifiStuff { - Miscellaneous.logEvent("i", "Wifi", "Changing Wifi to " + String.valueOf(desiredState), 4); - - if (desiredState && Settings.useWifiForPositioning) - WifiBroadcastReceiver.startWifiReceiver(autoMationServerRef.getLocationProvider()); - - WifiManager myWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); - - // toggle - if (toggleActionIfPossible) + public static Boolean setWifi(Context context, Boolean desiredState, boolean toggleActionIfPossible) { - Toast.makeText(context, context.getResources().getString(R.string.toggling) + " " + context.getResources().getString(R.string.wifi), Toast.LENGTH_LONG).show(); - desiredState = !myWifi.isWifiEnabled(); + if(context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.Q) + return setWifiWithRoot(context, desiredState, toggleActionIfPossible); + else + return setWifiOldFashioned(context, desiredState, toggleActionIfPossible); } - // Only perform action if necessary - if ((!myWifi.isWifiEnabled() && desiredState) | (myWifi.isWifiEnabled() && !desiredState)) + public static Boolean setWifiWithRoot(Context context, Boolean desiredState, boolean toggleActionIfPossible) { - String wifiString = ""; + Miscellaneous.logEvent("i", "Wifi", "Changing wifi to " + String.valueOf(desiredState), 4); - if (desiredState) - { - wifiString = context.getResources().getString(R.string.activating) + " " + context.getResources().getString(R.string.wifi); - } + String command = null; + int state = 0; + + String desiredStateString; + if(desiredState) + desiredStateString = "enable"; else + desiredStateString = "disable"; + + try { - wifiString = context.getResources().getString(R.string.deactivating) + " " + context.getResources().getString(R.string.wifi); + command = "svc wifi " + desiredStateString; + Miscellaneous.logEvent("i", "setWifiWithRoot()", "Running command: " + command.toString(), 5); + return executeCommandViaSu(new String[]{command}); + } + catch (Exception e) + { + // Oops! Something went wrong, so we throw the exception here. + throw e; } - - Toast.makeText(context, wifiString, Toast.LENGTH_LONG).show(); - - boolean returnValue = myWifi.setWifiEnabled(desiredState); - if (!returnValue) - Miscellaneous.logEvent("i", "Wifi", "Error changing Wifi to " + String.valueOf(desiredState), 2); - else - Miscellaneous.logEvent("i", "Wifi", "Wifi changed to " + String.valueOf(desiredState), 2); - - return returnValue; } - return true; + public static Boolean setWifiOldFashioned(Context context, Boolean desiredState, boolean toggleActionIfPossible) + { + Miscellaneous.logEvent("i", "Wifi", "Changing wifi to " + String.valueOf(desiredState), 4); + + if (desiredState && Settings.useWifiForPositioning) + WifiBroadcastReceiver.startWifiReceiver(autoMationServerRef.getLocationProvider()); + + WifiManager myWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + + // toggle + if (toggleActionIfPossible) + { + Toast.makeText(context, context.getResources().getString(R.string.toggling) + " " + context.getResources().getString(R.string.wifi), Toast.LENGTH_LONG).show(); + desiredState = !myWifi.isWifiEnabled(); + } + + // Only perform action if necessary + if ((!myWifi.isWifiEnabled() && desiredState) | (myWifi.isWifiEnabled() && !desiredState)) + { + String wifiString = ""; + + if (desiredState) + { + wifiString = context.getResources().getString(R.string.activating) + " " + context.getResources().getString(R.string.wifi); + } + else + { + wifiString = context.getResources().getString(R.string.deactivating) + " " + context.getResources().getString(R.string.wifi); + } + + Toast.makeText(context, wifiString, Toast.LENGTH_LONG).show(); + + boolean returnValue = myWifi.setWifiEnabled(desiredState); + if (!returnValue) + Miscellaneous.logEvent("i", "Wifi", "Error changing Wifi to " + String.valueOf(desiredState), 2); + else + Miscellaneous.logEvent("i", "Wifi", "Wifi changed to " + String.valueOf(desiredState), 2); + + return returnValue; + } + + return true; + } } public static void setDisplayRotation(Context myContext, Boolean desiredState, boolean toggleActionIfPossible) @@ -1199,7 +1235,7 @@ public class Actions desiredState = !isEnabled; } - if (Build.VERSION.SDK_INT <= 20) + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT_WATCH) { for (Method m : iConnectivityManagerClass.getDeclaredMethods()) { @@ -1233,15 +1269,45 @@ public class Actions if (enable) desiredState = 1; - if (MobileDataStuff.setMobileNetworkFromLollipop(desiredState, autoMationServerRef)) + + if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) { - Miscellaneous.logEvent("i", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootSuccess), 2); - return true; + if(MobileDataStuff.setMobileNetworkFromAndroid9(desiredState, autoMationServerRef)) + { + Miscellaneous.logEvent("i", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootSuccess), 2); + return true; + } + else + { + Miscellaneous.logEvent("e", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootFail), 2); + return false; + } + } + else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) + { + if (MobileDataStuff.setMobileNetworkTillAndroid5(desiredState, autoMationServerRef)) + { + Miscellaneous.logEvent("i", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootSuccess), 2); + return true; + } + else + { + Miscellaneous.logEvent("e", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootFail), 2); + return false; + } } else { - Miscellaneous.logEvent("e", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootFail), 2); - return false; + if (MobileDataStuff.setMobileNetworkAndroid6Till8(desiredState, autoMationServerRef)) + { + Miscellaneous.logEvent("i", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootSuccess), 2); + return true; + } + else + { + Miscellaneous.logEvent("e", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootFail), 2); + return false; + } } } catch (Exception e) @@ -1258,71 +1324,33 @@ public class Actions } @SuppressLint("NewApi") - public static boolean setMobileNetworkFromLollipop(int desiredState, Context context) throws Exception + public static boolean setMobileNetworkAndroid6Till8(int desiredState, Context context) throws Exception { String command = null; int state = 0; try { - if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) + // Get the current state of the mobile network. + state = isMobileDataEnabled() ? 0 : 1; + // Get the value of the "TRANSACTION_setDataEnabled" field. + String transactionCode = getTransactionCode(context); + // Android 5.1+ (API 22) and later. + SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); + // Loop through the subscription list i.e. SIM list. + for (int i = 0; i < mSubscriptionManager.getActiveSubscriptionInfoCountMax(); i++) { - /* - Android 8.1 is the last version on which the transaction code can be determined - with the below method. From 9.0 on the field TRANSACTION_setDataEnabled does not - exist anymore. Usually it was 83 and we'll just try this number hardcoded. - Alternatively the bottom of this might be an approach: - https://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later - */ - SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); - // Loop through the subscription list i.e. SIM list. - for (int i = 0; i < mSubscriptionManager.getActiveSubscriptionInfoCountMax(); i++) + if (transactionCode != null && transactionCode.length() > 0) { // Get the active subscription ID for a given SIM card. int subscriptionId = mSubscriptionManager.getActiveSubscriptionInfoList().get(i).getSubscriptionId(); // Execute the command via `su` to turn off // mobile network for a subscription service. - command = "service call phone " + "83" + " i32 " + subscriptionId + " i32 " + desiredState; - Miscellaneous.logEvent("i", "setDataConnectionWithRoot()", "Running command: " + command.toString(), 5); + command = "service call phone " + transactionCode + " i32 " + subscriptionId + " i32 " + desiredState; + Miscellaneous.logEvent("i", "setMobileNetworkAndroid6Till8()", "Running command: " + command.toString(), 5); return executeCommandViaSu(new String[]{command}); } } - else - { - // Get the current state of the mobile network. - state = isMobileDataEnabled() ? 0 : 1; - // Get the value of the "TRANSACTION_setDataEnabled" field. - String transactionCode = getTransactionCode(context); - // Android 5.1+ (API 22) and later. - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) - { - SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); - // Loop through the subscription list i.e. SIM list. - for (int i = 0; i < mSubscriptionManager.getActiveSubscriptionInfoCountMax(); i++) - { - if (transactionCode != null && transactionCode.length() > 0) - { - // Get the active subscription ID for a given SIM card. - int subscriptionId = mSubscriptionManager.getActiveSubscriptionInfoList().get(i).getSubscriptionId(); - // Execute the command via `su` to turn off - // mobile network for a subscription service. - command = "service call phone " + transactionCode + " i32 " + subscriptionId + " i32 " + desiredState; - Miscellaneous.logEvent("i", "setDataConnectionWithRoot()", "Running command: " + command.toString(), 5); - return executeCommandViaSu(new String[]{command}); - } - } - } - else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) - { - // Android 5.0 (API 21) only. - if (transactionCode != null && transactionCode.length() > 0) - { - // Execute the command via `su` to turn off mobile network. - command = "service call phone " + transactionCode + " i32 " + desiredState; - return executeCommandViaSu(new String[]{command}); - } - } - } } catch (Exception e) { @@ -1333,6 +1361,71 @@ public class Actions return false; } + @SuppressLint("NewApi") + public static boolean setMobileNetworkTillAndroid5(int desiredState, Context context) throws Exception + { + String command = null; + int state = 0; + + try + { + // Get the current state of the mobile network. + state = isMobileDataEnabled() ? 0 : 1; + // Get the value of the "TRANSACTION_setDataEnabled" field. + String transactionCode = getTransactionCode(context); + // Android 5.0 (API 21) only. + if (transactionCode != null && transactionCode.length() > 0) + { + // Execute the command via `su` to turn off mobile network. + command = "service call phone " + transactionCode + " i32 " + desiredState; + Miscellaneous.logEvent("i", "setMobileNetworkTillAndroid5()", "Running command: " + command.toString(), 5); + return executeCommandViaSu(new String[]{command}); + } + } + catch (Exception e) + { + // Oops! Something went wrong, so we throw the exception here. + throw e; + } + + return false; + } + + @SuppressLint("NewApi") + public static boolean setMobileNetworkFromAndroid9(int desiredState, Context context) throws Exception + { + String command = null; + int state = 0; + + String desiredStateString; + if(desiredState == 0) + desiredStateString = "enable"; + else + desiredStateString = "disable"; + + try + { + /* + Android 8.1 is the last version on which the transaction code can be determined + with the below method. From 9.0 on the field TRANSACTION_setDataEnabled does not + exist anymore. Usually it was 83 and we'll just try this number hardcoded. + Alternatively the bottom of this might be an approach: + https://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later + */ + + // Execute the command via `su` to turn off + // mobile network for a subscription service. + command = "svc data " + desiredStateString; + Miscellaneous.logEvent("i", "setMobileNetworkFromAndroid9()", "Running command: " + command.toString(), 5); + return executeCommandViaSu(new String[]{command}); + } + catch (Exception e) + { + // Oops! Something went wrong, so we throw the exception here. + throw e; + } + } + @SuppressLint("NewApi") public static boolean isMobileDataEnabled() { diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index 17b8aa5..54681cd 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -19,6 +19,7 @@ import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; import com.jens.automation2.receivers.NotificationListener; @@ -494,7 +495,7 @@ public class ActivityPermissions extends Activity break; case sendTextMessage: addToArrayListUnique(Manifest.permission.SEND_SMS, requiredPermissions); - checkPermissionsInVariableUse(action.getParameter2(), requiredPermissions); + checkPermissionsInVariableUse(action.getParameter2(), requiredPermissions); break; case setAirplaneMode: addToArrayListUnique(Manifest.permission.WRITE_SETTINGS, requiredPermissions); @@ -970,8 +971,8 @@ public class ActivityPermissions extends Activity if(requiredPermissions.contains(Manifest.permission.SEND_SMS)) { if(!ActivityPermissions.isPermissionDeclaratedInManifest(Miscellaneous.getAnyContext(), Manifest.permission.SEND_SMS) - && - Miscellaneous.isGooglePlayInstalled(Miscellaneous.getAnyContext()) +// && +// Miscellaneous.isGooglePlayInstalled(Miscellaneous.getAnyContext()) ) { requiredPermissions.remove(Manifest.permission.SEND_SMS); @@ -989,7 +990,10 @@ public class ActivityPermissions extends Activity Miscellaneous.logEvent("i", "Permissions", "Requesting permissions: " + permissions, 2); // Toast.makeText(ActivityPermissions.this, "Requesting permissions. Amount: " + String.valueOf(requiredPermissions.size()), Toast.LENGTH_LONG).show(); - requestPermissions(requiredPermissions.toArray(new String[requiredPermissions.size()]), requestCodeForPermissions); + if(requiredPermissions.size() > 0) + requestPermissions(requiredPermissions.toArray(new String[requiredPermissions.size()]), requestCodeForPermissions); +// else +// Miscellaneous.messageBox(getResources().getString(R.string.warning), getResources().getString(R.string.permissionsRequiredNotAvailable), ActivityPermissions.this).show(); } else setHaveAllPermissions(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8bb6f53..f4ec279 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -701,4 +701,5 @@ Let alarms through Let nothing through Fine tuning (like allowing phone calls, picking specific numbers, etc.) can only be done from the system\'s settings. + Your rules required permissions which cannot be requested from this installed flavor of Automation. \ No newline at end of file