PowerManger permission denied with Magisk and SuperSU - Android Studio

First of all, we need to reboot the device by button click.
Device: Mediatek MT6737M Android 6.0 running Marshmallow
I currently have three devices. One rooted with Magisk another SuperSU the third with Kingroot. In the settings on all three root managers I have it set to grant all permissions. When we try and restart Kingroot flashes a prompt to allow permission which we can work around but Kingroot is riddled with garbage that we don't want. We want to use Magisk or SuperSU because they are clean with less baggage. Magisk and SuperSU however throw an error: java.io.IOException:
Code:
Error running exec(). Command: [/system/bin/su, -c, reboot now] Working Directory: null Environment: null.
This is the code we're using to access PowerManager:
Code:
void reboot() {
if (reboot) {
try {
Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot"});
proc.waitFor();
} catch (Exception ex) {
Log.e(TAG, "Error ", ex);
}
}
}
Is there a way to grant permission to this process using either Magisk or SuperSU? If not is there another rooting management tool or software that is clean and malware free that can achieve that?
NOTE: Code by senior developer I am a Baby, Jr dev here tasked with finding the answer. Please be patient if I ask dumb questions.

Ok so first thing I'll start by saying that you should be making su calls in a background thread. The su call is a blocking call so it will block the UI thread which can cause ANRs (crashes) and overall bad user experience. AsyncTask is perfect for su calls to run in the background instead.
You should also stick with Magisk as it is actively being developed and is the current standard.
Let's get started. Create a new class and call it RootShell.java. Then copy the following code into it.
Java:
class RootShell {
private static void exec(String[] cmd) {
new Execute().execute(cmd);
}
private static class Execute extends AsyncTask<String, Void, Void> {
Override
protected Void doInBackground(String... cmds) {
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd + "\n");
}
os.writeBytes("exit\n");
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
Keep in mind that XDA has awful code formatting so the Override should have the @ symbol in front of it.
Now you can pass single or multiple root commands to this class which will be executed in a background thread. Here's how...
Lets say you're using a button in another class to reboot the phone. This is what it would look like...
Java:
private void rebootButton(View view) {
String[] cmd = { "reboot" };
RootShell.exec(cmd);
}
To pass multiple commands your button would look like this.
This is to reboot into safe mode...
Java:
private void rebootSafeMode(View view) {
String[] cmd = {
"setprop persist.sys.safemode 1",
"setprop ctl.restart zygote"
};
RootShell.exec(cmd);
}
For more info on how to properly su you should check out the following link...
https://su.chainfire.eu/
Hope this helps you with your app and good luck.

Related

[Q] Dev - How to launch an app from another one

Hi there,
I'm working on my LeoParts and integrated ROM Updater in my ROM.
Now I want my LeoParts to launch ROM Updater on a PreferenceScreen.
I tried two things:
1. With the XML
Code:
<PreferenceScreen
android:key="rom_update"
android:title="@string/updates_title"
android:summary="@string/updates_summary">
<intent android:action="android.intent.action.MAIN"
android:targetPackage="org.elegosproject.romupdater"
android:targetClass="org.elegosproject.romupdater.VersionsList"/>
</PreferenceScreen>
2. With the Java code
Code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("org.elegosproject.romupdater", "org.elegosproject.romupdater.VersionsList");
startActivity(intent);
3. From the shell
Code:
am start -a android.intent.action.MAIN -n org.elegosproject.romupdater/.VersionsList
This last one works, but I would like something less hacky...
Here is the error I get from a logcat:
Code:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cmp=org.elegosproject.romupdater/.VersionsList } from ProcessRecord{407e1ed8 775:com.leonnib4.leo_parts/10044} (pid=775, uid=10044) requires null
I know this as something to do with permissions, most likely in the AndroidManifest.xml, but I can't get it to work.
Please help me out
Thx
I got arround by doing:
Code:
Intent intent = getPackageManager().getLaunchIntentForPackage("org.elegosproject.romupdater");
if (intent != null) {
toast.show();
String commands[] = {
"am start -a android.intent.action.MAIN -n org.elegosproject.romupdater/.VersionsList"
};
// Execute in a shell
} else {
// Toast notif "ROM Updater is missing"
}
But that is still hacky and I'm still looking for a better way to do this...
Thx

[Q] How to enable WifiAP / tethering programmatically from within app on SGS I9000?

I try to activate WifiAP (wifi tethering) via my application code. Since there is no direct API for it, it needs to be done via reflection.
Using the approach as mentioned on StackOverflow.com.
It works fine on Nexus One, HTC Desire, Huawei Ideos (all running 2.2.x), but it doesn't run on the SGS with 2.2.1.
Checking the android.net.wifi.WifiManager class via reflection, I see that Samsung has obviously modified the class and it has 57 instead of 37 methods.
Unfortunately the modified WifiManager class is not open sourced by Samsung and the error messages don't give any details or stack trace.
I am looking for somebody who can enable WifiAP activation programmatically on the SGS, eventually by modifying the underlying system, since I'm myself am an application developer, not working on system level. Also considering contract work!
There are two things I am trying:
1) activate WifiAP directly via reflection:
Code:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
Log.d(TAG, "enableMobileAP methods " + wmMethods.length);
for(Method method: wmMethods){
Log.d(TAG, "enableMobileAP method.getName() " + method.getName());
if(method.getName().equals("setWifiApEnabled")) {
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "MyWifiAP";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
try {
Log.d(TAG, "enableMobileAP try: ");
method.invoke(wifi, netConfig, enabled);
if (netConfig.wepKeys!=null && netConfig.wepKeys.length>=1)
Log.d(TAG, "enableMobileAP key : " + netConfig.wepKeys[0]);
Log.d(TAG, "enableMobileAP enabled: ");
mIsWifiEnabled = enabled;
} catch (Exception e) {
Log.e(TAG, "enableMobileAP failed: ", e);
}
}
}
Attached are the logfiles I am getting on Nexus One (success) and SGS (failure).
The noticable part in the SGS log are maybe these lines:
Code:
D/WifiService( 2818): setWifiApEnabled: allowWifiAp: true
E/WifiService( 2818): setWifiApEnabledState 2
E/SoftapController( 2681): Softap already stopped
E/WifiService( 2818): Exception in stopAccessPoint()
E/WifiService( 2818): setWifiApEnabledState 4
D/WifiAp (23523): enableMobileAP key : null
2) another thing I try - since the modified SGS WifiManager (only that one) has a method named 'showApDialog' without any parameter taken, I try to call that method, but this also just gives an 'Error' in the log without further explanation.)
Code:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
Log.d(TAG, "enableMobileAP methods " + wmMethods.length);
for(Method method: wmMethods){
Log.d(TAG, "enableMobileAP method.getName() " + method.getName());
if(method.getName().equals("showApDialog")) {
WifiConfiguration netConfig = new WifiConfiguration();
try {
method.invoke(wifi);
} catch (Exception e) {
Log.e("Main", "Can't open ap dialog.", e);
}
}
}
I have been able to enable an existing configuration by passing null as the WifiConfiguration parameter to the setWifiApEnabled method.
I had the same problems as you when I tried to use this function to create a new configuration. I also failed when I tried to pass the existing configuration as a parameter, after invoking getWifiApConfiguration.
I noticed in the Android source code that there is a similar function called setWifiApConfiguration, but calling wifiManager.getClass().getMethod("setWifiApConfiguration") throws NoSuchMethodException when I run it on my SGS.
I also wondered if I can use addNetwork to create a new configuration, but suspect it is only for connecting to APs, rather than creating one.
As I only wanted to toggle the existing configuration, which I have now achieved, I stopped investigating at this point.
Hope that helps,
Good luck...
Yes, I've also got it to work meanwhile but didn't update this thread. You're right, the problem were the configuration, which had to be left empty. Cheers
thank you for sharing this, it really made my day
it seems that (on Android 2.3.4) it only works if you first disable the WiFi (Nexus One).

Work with files in hook

Hi, I have hook
Code:
findAndHookMethod(packageManagerService, null,
"installPackageWithVerificationAndEncryption", Uri.class,
"android.content.pm.IPackageInstallObserver", int.class,
String.class, "android.content.pm.VerificationParams",
"android.content.pm.ContainerEncryptionParams",
installPackageHook);
I want to create files while I am in hook.
Code:
installPackageHook = new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param)
throws Throwable {
File backupApkFile = new File(APP_DIR + File.separator + "App.apk");
backupApkFile.createNewFile();
I get EACCESS permission denied. Framework has all permissions, no?
pyler said:
I get EACCESS permission denied. Framework has all permissions, no?
Click to expand...
Click to collapse
Your code runs in the hooked process, so you only have that process' permissions.
You can try hooking the PermissionGranter class to give the hooked package additional permissions, or e.g. send a broadcast to/directly start your own activity/service to write the file (from your own app's process, without Xposed).

[Q] SU Command from hook

Im trying to run a su command from within a hook,
Code:
Process p;
try {
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
toastMessage("root");
return true;
} else {
toastMessage("not root");
}
} catch (InterruptedException e) {
toastMessage("not root");
}
} catch (IOException e) {
toastMessage("not root");
}
But even though I grant the shell su access I still get error:
tmp-mksh: <stdin>[1]: can't create /system/sd/temporary.txt Read-only file system
Any ideas?
mount system as rw?
Ty, I'll give it a shot. Could be Selinux related too but no errors other than in su log. Might need to post this to root section. Not really a problem with the hook I don't think.

[Q] Android Studio app says "Error In Network Connection"

I have created a login and register system, when ever I try to login, register, change password or reset password the app just says "Error In Network Connection".
I am using strong internet connection, could you please advise as I have tried to on WI-FI and 4G, however it still says the same error. I am using WAMP server to connect to myphpadmin database. Please could you help and advise. I have used the internet method as well as using the doinbackground method.
Emulator , Use Device
If you use emulator. You will get this type of problem.
Use below code to check the online condition for mobile.
public boolean isNetworkOnline() {
boolean status=false;
try{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(0);
if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
status= true;
}else {
netInfo = cm.getNetworkInfo(1);
if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
status= true;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return status;
}

Categories

Resources