diff --git a/README.md b/README.md index 718e8a89..d41672ff 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/src/main/java/xtr/keymapper/Server.java b/app/src/main/java/xtr/keymapper/Server.java index 26c03bd6..159c8886 100644 --- a/app/src/main/java/xtr/keymapper/Server.java +++ b/app/src/main/java/xtr/keymapper/Server.java @@ -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; @@ -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(); @@ -52,26 +53,26 @@ public void setupServer (Context context) { if (!script.exists()) mCallback.updateCmdView1("failed to write script: permission denied\n"); } + private final List 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()); - } + }); } } diff --git a/app/src/main/java/xtr/keymapper/Utils.java b/app/src/main/java/xtr/keymapper/Utils.java index 5867485a..e7ac07cb 100644 --- a/app/src/main/java/xtr/keymapper/Utils.java +++ b/app/src/main/java/xtr/keymapper/Utils.java @@ -2,7 +2,6 @@ import java.io.BufferedReader; import java.io.DataOutputStream; -import java.io.File; import java.io.IOException; import java.io.InputStreamReader; @@ -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!"); - } } diff --git a/app/src/main/java/xtr/keymapper/activity/MainActivity.java b/app/src/main/java/xtr/keymapper/activity/MainActivity.java index bc15c85d..eeac7bc1 100644 --- a/app/src/main/java/xtr/keymapper/activity/MainActivity.java +++ b/app/src/main/java/xtr/keymapper/activity/MainActivity.java @@ -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()); } }