java Copy code
import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKey;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import java.util.concurrent.CompletableFuture;
public class DoTouchPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
private static final String PREFS_NAME = "permissions";
private static final String PERMISSION_KEY = "hasPermission";
private static final String LOCK_PERMISSION_KEY = "hasLockPermission";
private MethodChannel channel;
private Context context;
private CompletableFuture<Void> permissionFuture;
private AccessibilityService accessibilityService;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
context = flutterPluginBinding.getApplicationContext();
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "do_touch");
channel.setMethodCallHandler(this);
// Инициализация защищенных настроек
MasterKey masterKey = new MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
EncryptedSharedPreferences prefs = EncryptedSharedPreferences.create(
context,
PREFS_NAME,
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
// Инициализация AccessibilityService
accessibilityService = new MyAccessibilityService();
context.bindService(new Intent(context, MyAccessibilityService.class), new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// Установка готовности к работе
permissionFuture = CompletableFuture.completedFuture(null);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// Обработка отключения сервиса
}
}, Context.BIND_AUTO_CREATE);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
switch (call.method) {
case "getPermission":
requestPermission(PERMISSION_KEY, result);
break;
case "getLockPermission":
requestPermission(LOCK_PERMISSION_KEY, result);
break;
case "touchPoint":
touchPoint(call.argument("x"), call.argument("y"), call.argument("canSwipe"));
result.success(true);
break;
case "touchSwipe":
touchSwipe(call.argument("x"), call.argument("y"), call.argument("x2"), call.argument("y2"), call.argument("duration"));
result.success(true);
break;
// ...
}
}
private void requestPermission(String key, Result result) {
EncryptedSharedPreferences prefs = getPrefs();
if (!prefs.getBoolean(key, false)) {
permissionFuture = permissionFuture.thenRunAsync(() -> {
if (key.equals(PERMISSION_KEY)) {
startActivity(Settings.ACTION_ACCESSIBILITY_SETTINGS);
} else {
startActivity(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + context.getPackageName()));
}
prefs.edit().putBoolean(key, true).apply();
});
}
result.success(true);
}
private void touchPoint(float x, float y, boolean canSwipe) {
accessibilityService.dispatchGesture(GestureDescription.Builder()
.addStroke(new GestureDescription.StrokeDescription(new Path().moveTo(x, y), 0, 50, false))
.build(), new GestureResultCallback() {
// ...
}, null);
}
private void touchSwipe(float x, float y, float x2, float y2, int duration) {
Path path = new Path();
path.moveTo(x, y);
path.lineTo(x2, y2);
accessibilityService.dispatchGesture(GestureDescription.Builder()
.addStroke(new GestureDescription.StrokeDescription(path, 0, duration, false))
.build(), new GestureResultCallback() {
// ...
}, null);
}
private EncryptedSharedPreferences getPrefs() {
return EncryptedSharedPreferences.create(
context,
PREFS_NAME,
MasterKey.getDefault(context),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
}
private void startActivity(String action, Uri uri) {
context.startActivity(new Intent(action, uri).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}