x1ongzhu 1 жил өмнө
parent
commit
fccf540478

+ 45 - 2
app/src/main/java/com/example/modifier/ModifierService.java

@@ -1,15 +1,22 @@
 package com.example.modifier;
 
 import android.accessibilityservice.AccessibilityServiceInfo;
+import android.annotation.SuppressLint;
+import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
+import android.graphics.Color;
 import android.graphics.PixelFormat;
 import android.net.Uri;
 import android.os.Build;
+import android.util.DisplayMetrics;
 import android.util.Log;
+import android.view.GestureDetector;
 import android.view.Gravity;
 import android.view.LayoutInflater;
+import android.view.MotionEvent;
+import android.view.View;
 import android.view.WindowManager;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.accessibility.AccessibilityNodeInfo;
@@ -19,16 +26,18 @@ import androidx.annotation.NonNull;
 import androidx.appcompat.view.ContextThemeWrapper;
 
 import com.example.modifier.databinding.FloatingWindowBinding;
+import com.google.android.material.color.DynamicColors;
+import com.google.android.material.color.DynamicColorsOptions;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
-import java.net.URISyntaxException;
 import java.util.Optional;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.atomic.AtomicReference;
 
 import io.socket.client.IO;
 import io.socket.client.Socket;
@@ -235,6 +244,7 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
         }
     }
 
+    @SuppressLint("ClickableViewAccessibility")
     @Override
     protected void onServiceConnected() {
         super.onServiceConnected();
@@ -248,7 +258,15 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
         info.notificationTimeout = 100;
         this.setServiceInfo(info);
 
+        DisplayMetrics displayMetrics = new DisplayMetrics();
         WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
+        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
+        int height = displayMetrics.heightPixels;
+        int width = displayMetrics.widthPixels;
+        int maxX = width - Utils.dp2px(this, 150);
+        int maxY = height - Utils.dp2px(this, 100);
+
+
         FrameLayout mLayout = new FrameLayout(this);
         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
         layoutParams.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
@@ -259,11 +277,36 @@ public class ModifierService extends android.accessibilityservice.AccessibilityS
         layoutParams.x = 0;
         layoutParams.y = 800;
         layoutParams.gravity = Gravity.START | Gravity.TOP;
-        ContextThemeWrapper newContext = new ContextThemeWrapper(getApplicationContext(), R.style.AppTheme);
+
+        Context newContext = DynamicColors.wrapContextIfAvailable(getApplicationContext(), R.style.AppTheme);
         LayoutInflater inflater = LayoutInflater.from(newContext);
         binding = FloatingWindowBinding.inflate(inflater, mLayout, true);
         windowManager.addView(mLayout, layoutParams);
 
+        AtomicReference<Float> downX = new AtomicReference<>(0f);
+        AtomicReference<Float> downY = new AtomicReference<>(0f);
+        AtomicReference<Integer> downParamX = new AtomicReference<>(0);
+        AtomicReference<Integer> downParamY = new AtomicReference<>(0);
+
+        binding.floatingWindow.setOnTouchListener((v, event) -> {
+            switch (event.getAction()) {
+                case MotionEvent.ACTION_DOWN:
+                    downX.set(event.getRawX());
+                    downY.set(event.getRawY());
+                    downParamX.set(layoutParams.x);
+                    downParamY.set(layoutParams.y);
+                    return true;
+
+                case MotionEvent.ACTION_MOVE:
+                    layoutParams.x = (int) Math.min(Math.max(downParamX.get() + (event.getRawX() - downX.get()), 0), maxX);
+                    layoutParams.y = (int) Math.min(Math.max(downParamY.get() + (event.getRawY() - downY.get()), 0), maxY);
+                    windowManager.updateViewLayout(mLayout, layoutParams);
+                    return true;
+            }
+            return false;
+        });
+
+
         binding.swConnect.setChecked(true);
         binding.swConnect.setOnCheckedChangeListener((buttonView, isChecked) -> {
             if (isChecked) {

+ 5 - 0
app/src/main/java/com/example/modifier/Utils.java

@@ -249,4 +249,9 @@ public class Utils {
             return false;
         }
     }
+
+    public static int dp2px(Context context, int dp) {
+        float scale = context.getResources().getDisplayMetrics().density;
+        return (int) (dp * scale + 0.5f);
+    }
 }

+ 20 - 18
app/src/main/java/com/example/modifier/fragments/SettingsFragment.java

@@ -77,24 +77,6 @@ public class SettingsFragment extends Fragment {
         binding.tlImei.setEndIconOnClickListener(v -> {
             binding.etImei.setText(Utils.generateIMEI());
         });
-        Gson gson = new Gson();
-        try {
-            File file = new File(ContextCompat.getDataDir(getContext()), "config.json");
-            if (file.exists()) {
-                FileReader reader = new FileReader(file);
-                Config config = gson.fromJson(reader, Config.class);
-                binding.etNumber.setText(config.getNumber());
-                binding.etMcc.setText(config.getMcc());
-                binding.etMnc.setText(config.getMnc());
-                binding.etIccid.setText(config.getIccid());
-                binding.etImsi.setText(config.getImsi());
-                binding.etImei.setText(config.getImei());
-                binding.etCountry.setText(config.getCountry());
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
         binding.btnSave.setOnClickListener(v -> {
             onSave();
         });
@@ -139,6 +121,26 @@ public class SettingsFragment extends Fragment {
             }, 500);
         });
 
+        handler.post(() -> {
+            Gson gson = new Gson();
+            try {
+                File file = new File(ContextCompat.getDataDir(getContext()), "config.json");
+                if (file.exists()) {
+                    FileReader reader = new FileReader(file);
+                    Config config = gson.fromJson(reader, Config.class);
+                    binding.etNumber.setText(config.getNumber());
+                    binding.etMcc.setText(config.getMcc());
+                    binding.etMnc.setText(config.getMnc());
+                    binding.etIccid.setText(config.getIccid());
+                    binding.etImsi.setText(config.getImsi());
+                    binding.etImei.setText(config.getImei());
+                    binding.etCountry.setText(config.getCountry());
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        });
+
 
         return binding.getRoot();
     }

+ 6 - 0
app/src/main/res/drawable/bg_floating.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <corners
+        android:radius="0dp"  />
+    <solid android:color="#77ffffff" />
+</shape>

+ 11 - 6
app/src/main/res/layout/floating_window.xml

@@ -1,17 +1,22 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:background="#66000000"
-    android:orientation="vertical"
-    android:paddingEnd="8dp">
+    android:id="@+id/floating_window"
+    android:layout_width="100dp"
+    android:layout_height="100dp"
+    android:background="@drawable/bg_floating"
+    android:orientation="vertical">
+
+    <com.google.android.material.progressindicator.LinearProgressIndicator
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:indeterminate="true" />
 
     <com.google.android.material.checkbox.MaterialCheckBox
         android:id="@+id/sw_connect"
         style="@style/Widget.Material3.CompoundButton.CheckBox"
         android:layout_width="wrap_content"
-        android:layout_height="match_parent"
+        android:layout_height="wrap_content"
         android:checked="true"
         android:text="Connect"
         android:textSize="14sp" />