data with root

This commit is contained in:
jens 2021-10-03 15:09:07 +02:00
parent 969937f8a0
commit b5bd332ff5
4 changed files with 191 additions and 93 deletions

View File

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

View File

@ -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,9 +72,45 @@ 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 class WifiStuff
{
public static Boolean setWifi(Context context, Boolean desiredState, boolean toggleActionIfPossible)
{
Miscellaneous.logEvent("i", "Wifi", "Changing Wifi to " + String.valueOf(desiredState), 4);
if(context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.Q)
return setWifiWithRoot(context, desiredState, toggleActionIfPossible);
else
return setWifiOldFashioned(context, desiredState, toggleActionIfPossible);
}
public static Boolean setWifiWithRoot(Context context, Boolean desiredState, boolean toggleActionIfPossible)
{
Miscellaneous.logEvent("i", "Wifi", "Changing wifi to " + String.valueOf(desiredState), 4);
String command = null;
int state = 0;
String desiredStateString;
if(desiredState)
desiredStateString = "enable";
else
desiredStateString = "disable";
try
{
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;
}
}
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());
@ -116,6 +151,7 @@ public class Actions
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,7 +1269,10 @@ public class Actions
if (enable)
desiredState = 1;
if (MobileDataStuff.setMobileNetworkFromLollipop(desiredState, autoMationServerRef))
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1)
{
if(MobileDataStuff.setMobileNetworkFromAndroid9(desiredState, autoMationServerRef))
{
Miscellaneous.logEvent("i", "setDataConnectionWithRoot()", Miscellaneous.getAnyContext().getResources().getString(R.string.dataConWithRootSuccess), 2);
return true;
@ -1244,6 +1283,33 @@ public class Actions
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
{
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)
{
String rootString;
@ -1258,44 +1324,18 @@ 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)
{
/*
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++)
{
// 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);
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++)
@ -1307,23 +1347,11 @@ public class Actions
// 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);
Miscellaneous.logEvent("i", "setMobileNetworkAndroid6Till8()", "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)
{
// Oops! Something went wrong, so we throw the exception here.
@ -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()
{

View File

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

View File

@ -701,4 +701,5 @@
<string name="dndAlarms">Let alarms through</string>
<string name="dndNothing">Let nothing through</string>
<string name="dndRemarks">Fine tuning (like allowing phone calls, picking specific numbers, etc.) can only be done from the system\'s settings.</string>
<string name="permissionsRequiredNotAvailable">Your rules required permissions which cannot be requested from this installed flavor of Automation.</string>
</resources>