Skip to content

Commit

Permalink
feat: Use libsu for root access
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Feb 18, 2024
1 parent 2f1c8dd commit d0286bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ https://youtube.com/watch?v=yvZD2E7kgG0
Open source libraries used:
- [Material Design Components](https://github.com/material-components/material-components-android) used for the app user interface.
- [FloatingActionButtonSpeedDial](https://github.com/leinardi/FloatingActionButtonSpeedDial)
- [libsu](https://github.com/topjohnwu/libsu)

[Some code](./app/src/main/java/com/genymobile/scrcpy) from the [scrcpy](https://github.com/Genymobile/scrcpy) project was used for implementing multi-touch support in the keymapper.

## Copyright and License
This project is licensed under the GPL v3.
The source code is licensed under the GPL v3.
Do not publish unofficial APKs to the play store.
```
XtMapper
Expand Down
43 changes: 22 additions & 21 deletions app/src/main/java/xtr/keymapper/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import android.content.pm.PackageManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import com.topjohnwu.superuser.CallbackList;
import com.topjohnwu.superuser.Shell;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
import java.util.List;

import xtr.keymapper.activity.MainActivity;
import xtr.keymapper.server.RemoteService;
Expand All @@ -21,7 +23,6 @@ public class Server {
public File script;
public MainActivity.Callback mCallback;


private void writeScript(ApplicationInfo ai) throws IOException, InterruptedException {
final String className = RemoteService.class.getName();

Expand Down Expand Up @@ -52,26 +53,26 @@ public void setupServer (Context context) {
if (!script.exists()) mCallback.updateCmdView1("failed to write script: permission denied\n");
}

private final List<String> callbackList = new CallbackList<>() {
@Override
public void onAddElement(String line) {
mCallback.updateCmdView1("stdout: " + line + "\n");
if (line.equals("Waiting for overlay..."))
mCallback.alertActivation();
}
};

public void startServer() {
mCallback.updateCmdView1("exec sh " + script.getPath() + "\n");
try {
Process sh = Utils.getRootAccess();
DataOutputStream outputStream = new DataOutputStream(sh.getOutputStream());
outputStream.writeBytes("/system/bin/sh " + script.getPath());
outputStream.close();

BufferedReader stdout = new BufferedReader(new InputStreamReader(sh.getInputStream()));
String line;
while ((line = stdout.readLine()) != null) {
mCallback.updateCmdView1("stdout: " + line + "\n");
if (line.equals("Waiting for overlay..."))
mCallback.alertActivation();
Shell.getShell(shell -> {
if (!shell.isRoot()) mCallback.alertRootAccessNotFound();
try {
Shell.cmd(new FileInputStream(script)).to(callbackList).submit();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
if (sh.waitFor(5, TimeUnit.SECONDS)) mCallback.alertRootAccessNotFound();
sh.destroy();
} catch (IOException | InterruptedException ex) {
Log.e("Server", ex.toString());
}
});
}

}
9 changes: 0 additions & 9 deletions app/src/main/java/xtr/keymapper/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

Expand All @@ -27,12 +26,4 @@ public static BufferedReader geteventStream(String nativeLibraryDir) throws IOEx
return new BufferedReader(new InputStreamReader(sh.getInputStream()));
}

public static Process getRootAccess() throws IOException {
String[] paths = {"/sbin/su", "/system/sbin/su", "/system/bin/su", "/system/xbin/su", "/su/bin/su", "/magisk/.core/bin/su"};
for (String path : paths) {
if (new File(path).canExecute())
return Runtime.getRuntime().exec(path);
}
return Runtime.getRuntime().exec("echo root access not found!");
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/xtr/keymapper/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void startEditor(){
private void startServer(boolean autorun){
checkOverlayPermission();
if(Settings.canDrawOverlays(this)) {
if (autorun) new Thread(server::startServer).start();
if (autorun) server.startServer();
else mCallback.updateCmdView1("run in adb shell:\n sh " + server.script.getPath());
}
}
Expand Down

0 comments on commit d0286bd

Please sign in to comment.