xiongzhu 1 год назад
Родитель
Сommit
a1769bbef7

+ 26 - 22
app/src/main/java/com/example/modifier/MainActivity.java

@@ -108,30 +108,34 @@ public class MainActivity extends AppCompatActivity {
             sendBroadcast(intent);
         });
 
-        executor.execute(() -> {
-            if (Utils.hasRootAccess()) {
-                if (!Utils.isAccessibilityEnabled()) {
-                    if (!Utils.enableAccessibility()) {
-                        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
-                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                        startActivity(intent);
-                        handler.postDelayed(this::finish, 100);
-                    }
 
+        handler.postDelayed(() -> {
+            executor.execute(() -> {
+                if (Utils.hasRootAccess()) {
+                    if (!Utils.isAccessibilityEnabled()) {
+                        if (!Utils.enableAccessibility()) {
+                            Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
+                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                            startActivity(intent);
+                            handler.postDelayed(this::finish, 1000);
+                        }
+
+                    }
+                } else {
+                    handler.post(() -> {
+                        new AlertDialog.Builder(this)
+                                .setTitle("No Root Access")
+                                .setMessage("Root access is required to run this app")
+                                .setCancelable(false)
+                                .setPositiveButton("Exit", (dialog, which) -> {
+                                    finish();
+                                })
+                                .show();
+                    });
                 }
-            } else {
-                handler.post(() -> {
-                    new AlertDialog.Builder(this)
-                            .setTitle("No Root Access")
-                            .setMessage("Root access is required to run this app")
-                            .setCancelable(false)
-                            .setPositiveButton("Exit", (dialog, which) -> {
-                                finish();
-                            })
-                            .show();
-                });
-            }
-        });
+            });
+        }, 1000);
+
     }
 
     private void onSave() {

+ 18 - 15
app/src/main/java/com/example/modifier/ModifierService.java

@@ -32,6 +32,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 
 import io.socket.client.IO;
@@ -55,7 +56,7 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
         Log.i(TAG, "Creating ModifierService");
         try {
             mSocketOpts.query = "model=" + Build.MANUFACTURER + " " + Build.MODEL + "&name=" + Build.DEVICE + "&id=" + Utils.getUniqueID() + "&canSend=" + canSend;
-            mSocket = IO.socket("http://192.168.6.215:3000", mSocketOpts);
+            mSocket = IO.socket("http://192.168.50.135:3000", mSocketOpts);
             mSocket.on("message", this);
             mSocket.on(Socket.EVENT_CONNECT, args -> {
                 Log.i(TAG, "Connected to server");
@@ -139,7 +140,7 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
             Utils.runAsRoot(cmd);
 
             Log.i(TAG, "Command executed successfully, waiting for app to open...");
-            mExecutor.schedule(() -> {
+            ScheduledFuture<?> f = mExecutor.schedule(() -> {
                 Log.i(TAG, "Getting root node...");
                 AccessibilityNodeInfo root = getRootInActiveWindow();
                 String packageName = root.getPackageName().toString();
@@ -153,8 +154,13 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
                 }
                 Log.i(TAG, "App: " + appName + " (" + packageName + ")");
                 traverseNode(root);
-            }, 1, java.util.concurrent.TimeUnit.SECONDS).wait();
-
+                Log.i(TAG, "!!!!!!!!!!!!!!!!");
+            }, 1, java.util.concurrent.TimeUnit.SECONDS);
+            synchronized (f) {
+                Log.i(TAG, "Waiting for task to complete...");
+                f.get();
+                Log.i(TAG, "Task completed");
+            }
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -173,6 +179,7 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
         map.put("bounds", rect);
 
         String name = node.getViewIdResourceName();
+        Log.i(TAG, "Node: class=" + node.getClassName() + ", text=" + node.getText() + ", name=" + name);
 
         if ("Compose:Draft:Send".equals(name)) {
             Log.i(TAG, "Found send button Node: class=" + node.getClassName() + ", text=" + node.getText() + ", name=" + name);
@@ -212,7 +219,7 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
         layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
         layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
         layoutParams.x = 0;
-        layoutParams.y = 400;
+        layoutParams.y = 800;
         layoutParams.gravity = Gravity.START | Gravity.TOP;
         ContextThemeWrapper newContext = new ContextThemeWrapper(getApplicationContext(), R.style.Theme_Modifier);
         LayoutInflater inflater = LayoutInflater.from(newContext);
@@ -233,20 +240,16 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
             getSharedPreferences(BuildConfig.APPLICATION_ID, MODE_PRIVATE).edit().putBoolean("canSend", isChecked).apply();
             canSend = isChecked;
 
-            RequestQueue queue = Volley.newRequestQueue(this);
-            String url = "http://192.168.6.215:3000/api/rcs/updateDevice/" + Utils.getUniqueID();
-            JSONObject jsonObject = new JSONObject();
+            JSONObject data = new JSONObject();
             try {
-                jsonObject.put("canSend", canSend);
+                data.put("action", "updateDevice");
+                JSONObject dataObj = new JSONObject();
+                dataObj.put("canSend", canSend);
+                data.put("data", dataObj);
             } catch (JSONException e) {
                 e.printStackTrace();
             }
-            queue.add(new JsonObjectRequest(Request.Method.POST, url, jsonObject,
-                    response -> {
-                        Log.i(TAG, "Response: " + response);
-                    }, error -> {
-                Log.e(TAG, "Error: " + error.getMessage());
-            }));
+            mSocket.emit("message", data);
         });
     }
 

+ 24 - 16
app/src/main/java/com/example/modifier/Utils.java

@@ -9,15 +9,13 @@ import android.text.TextUtils;
 import android.util.Log;
 import android.view.accessibility.AccessibilityManager;
 
-import androidx.appcompat.app.AlertDialog;
-
-import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.DataOutputStream;
-import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Method;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
@@ -42,14 +40,23 @@ public class Utils {
 
     public static String runAsRoot(String... cmds) throws IOException, InterruptedException {
         Log.i(TAG, "Trying to run as root");
-        Context context = getContext();
-        File outputDir = context.getCacheDir(); // context being the Activity pointer
-        File outputFile = File.createTempFile("su0000000", ".log", outputDir);
+        Process p = new ProcessBuilder("su").start();
 
-        Process p = new ProcessBuilder("su")
-                .redirectErrorStream(true)
-                .redirectOutput(outputFile)
-                .start();
+        StringBuilder res = new StringBuilder();
+        StringBuilder err = new StringBuilder();
+        new Thread(() -> {
+            try {
+                res.append(IOUtils.toString(p.getInputStream(), StandardCharsets.UTF_8));
+            } catch (IOException ignored) {
+            }
+        }).start();
+
+        new Thread(() -> {
+            try {
+                err.append(IOUtils.toString(p.getErrorStream(), StandardCharsets.UTF_8));
+            } catch (IOException ignored) {
+            }
+        }).start();
 
         DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
         for (String cmd : cmds) {
@@ -57,13 +64,14 @@ public class Utils {
             outputStream.flush();
             Log.i(TAG, "Running command: " + cmd);
         }
+        Thread.sleep(2000);
         outputStream.writeBytes("exit\n");
         outputStream.flush();
         p.waitFor();
-        String res = FileUtils.readFileToString(outputFile, "UTF-8");
-        Log.i(TAG, "Command output: \n" + res);
-        outputFile.delete();
-        return res;
+
+        Log.i(TAG, "Output: " + res);
+
+        return res.toString();
     }
 
     public static boolean isAccessibilityEnabled() {
@@ -84,7 +92,7 @@ public class Utils {
     public static boolean hasRootAccess() {
         boolean rootAccess = false;
         try {
-            String res = runAsRoot("echo 'imrooted'");
+            String res = runAsRoot("echo \"imrooted\"");
             if (res.contains("imrooted")) {
                 rootAccess = true;
             }