2025-05-29 16:49:38 +08:00
|
|
|
|
package com.example.studyapp.autoJS;
|
|
|
|
|
|
2025-06-10 19:54:32 +08:00
|
|
|
|
import static androidx.core.content.ContextCompat.startActivity;
|
2025-06-13 19:02:53 +08:00
|
|
|
|
import static com.example.studyapp.MainActivity.broadcastLock;
|
|
|
|
|
import static com.example.studyapp.MainActivity.taskLock;
|
2025-06-18 14:31:11 +08:00
|
|
|
|
import static com.example.studyapp.task.TaskUtil.infoUpload;
|
2025-06-10 19:54:32 +08:00
|
|
|
|
|
2025-06-12 22:55:55 +08:00
|
|
|
|
import android.Manifest;
|
2025-06-10 19:54:32 +08:00
|
|
|
|
import android.content.ActivityNotFoundException;
|
2025-05-29 16:49:38 +08:00
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
import android.content.pm.PackageManager;
|
2025-06-12 22:55:55 +08:00
|
|
|
|
import android.os.Build;
|
2025-05-29 16:49:38 +08:00
|
|
|
|
import android.os.Environment;
|
2025-06-10 19:54:32 +08:00
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.Looper;
|
|
|
|
|
import android.util.Log;
|
2025-05-29 16:49:38 +08:00
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
|
|
|
|
|
import com.example.studyapp.MainActivity;
|
|
|
|
|
import com.example.studyapp.request.ScriptResultRequest;
|
|
|
|
|
import com.example.studyapp.service.CloudPhoneManageService;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
2025-06-18 14:31:11 +08:00
|
|
|
|
import java.io.IOException;
|
2025-05-29 16:49:38 +08:00
|
|
|
|
import retrofit2.Call;
|
|
|
|
|
import retrofit2.Callback;
|
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
|
|
|
|
|
public class AutoJsUtil {
|
|
|
|
|
|
|
|
|
|
public static BroadcastReceiver scriptResultReceiver;
|
2025-06-12 22:55:55 +08:00
|
|
|
|
public static volatile boolean flag;
|
2025-05-29 16:49:38 +08:00
|
|
|
|
|
2025-06-13 19:02:53 +08:00
|
|
|
|
private static int count;
|
2025-06-18 14:31:11 +08:00
|
|
|
|
public static void runAutojsScript(Context context) {
|
2025-06-10 19:54:32 +08:00
|
|
|
|
// 检查脚本文件
|
2025-06-13 19:02:53 +08:00
|
|
|
|
Log.i("AutoJsUtil", "-------脚本运行开始:--------"+ count++ );
|
2025-06-12 11:31:46 +08:00
|
|
|
|
File scriptFile = new File(Environment.getExternalStorageDirectory(), "script/main.js");
|
2025-05-29 16:49:38 +08:00
|
|
|
|
if (!scriptFile.exists()) {
|
2025-06-12 22:55:55 +08:00
|
|
|
|
runOnUiThread(() -> Toast.makeText(context, "脚本文件未找到: " + scriptFile.getAbsolutePath(), Toast.LENGTH_SHORT).show());
|
|
|
|
|
Log.e("AutoJsUtil", "脚本文件未找到, 路径: " + scriptFile.getAbsolutePath());
|
2025-05-29 16:49:38 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 19:54:32 +08:00
|
|
|
|
// 检查是否安装 Auto.js
|
|
|
|
|
if (!isAppInstalled("org.autojs.autojs6", context.getPackageManager())) {
|
|
|
|
|
runOnUiThread(() -> Toast.makeText(context, "Auto.js app not installed", Toast.LENGTH_SHORT).show());
|
2025-05-29 16:49:38 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 22:55:55 +08:00
|
|
|
|
// 启动 AutoJs
|
2025-05-29 16:49:38 +08:00
|
|
|
|
Intent intent = new Intent();
|
|
|
|
|
intent.setClassName("org.autojs.autojs6", "org.autojs.autojs.external.open.RunIntentActivity");
|
2025-06-10 19:54:32 +08:00
|
|
|
|
intent.putExtra("path", scriptFile.getAbsolutePath());
|
2025-05-29 16:49:38 +08:00
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
try {
|
|
|
|
|
context.startActivity(intent);
|
2025-06-13 19:02:53 +08:00
|
|
|
|
flag = false;
|
2025-06-12 22:55:55 +08:00
|
|
|
|
Log.i("AutoJsUtil", "脚本运行中:" + scriptFile.getAbsolutePath());
|
2025-05-29 16:49:38 +08:00
|
|
|
|
} catch (Exception e) {
|
2025-06-12 22:55:55 +08:00
|
|
|
|
Log.e("AutoJsUtil", "运行脚本失败", e);
|
|
|
|
|
runOnUiThread(() -> Toast.makeText(context, "运行脚本失败: " + e.getMessage(), Toast.LENGTH_SHORT).show());
|
2025-05-29 16:49:38 +08:00
|
|
|
|
}
|
2025-06-12 22:55:55 +08:00
|
|
|
|
// 注意:unregisterScriptResultReceiver 不应到此时立即调用
|
2025-05-29 16:49:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 22:55:55 +08:00
|
|
|
|
public static void registerScriptResultReceiver(Context context) {
|
2025-06-13 19:02:53 +08:00
|
|
|
|
|
2025-06-12 22:55:55 +08:00
|
|
|
|
if (scriptResultReceiver == null) {
|
|
|
|
|
// 创建广播接收器
|
|
|
|
|
scriptResultReceiver = new BroadcastReceiver() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2025-06-13 19:02:53 +08:00
|
|
|
|
Log.d("MainActivity", "----脚本运行结束通知一次------; 当前线程:" + Thread.currentThread().getName());
|
|
|
|
|
String scriptResult = intent.getStringExtra(SCRIPT_RESULT_KEY);
|
2025-06-12 22:55:55 +08:00
|
|
|
|
if (scriptResult != null && !scriptResult.isEmpty()) {
|
2025-06-13 19:02:53 +08:00
|
|
|
|
synchronized (broadcastLock) {
|
|
|
|
|
AutoJsUtil.flag = true;
|
|
|
|
|
}
|
|
|
|
|
synchronized (taskLock) {
|
2025-06-18 14:31:11 +08:00
|
|
|
|
try {
|
|
|
|
|
infoUpload(context, MainActivity.androidId, scriptResult);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 例如:可以显示给用户一条错误消息
|
|
|
|
|
Log.e("AutoJsUtil", "File upload failed: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 19:02:53 +08:00
|
|
|
|
taskLock.notifyAll(); // 唤醒任务线程
|
2025-06-12 22:55:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-13 19:02:53 +08:00
|
|
|
|
|
2025-06-12 22:55:55 +08:00
|
|
|
|
};
|
|
|
|
|
// 注册广播接收器
|
|
|
|
|
try {
|
|
|
|
|
IntentFilter filter = new IntentFilter(AUTOJS_SCRIPT_FINISHED_ACTION);
|
|
|
|
|
Context appContext = context.getApplicationContext();
|
|
|
|
|
ContextCompat.registerReceiver(appContext, scriptResultReceiver, filter, ContextCompat.RECEIVER_EXPORTED);
|
2025-06-13 19:02:53 +08:00
|
|
|
|
Log.d("MainActivity", "广播接收器成功注册");
|
2025-06-12 22:55:55 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e("MainActivity", "Failed to register receiver", e);
|
2025-06-13 19:02:53 +08:00
|
|
|
|
scriptResultReceiver = null; // 确保状态一致
|
2025-06-12 22:55:55 +08:00
|
|
|
|
}
|
2025-06-13 19:02:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
Log.w("MainActivity", "广播接收器已注册,无需重复注册");
|
2025-06-12 11:31:46 +08:00
|
|
|
|
}
|
2025-06-10 19:54:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 11:31:46 +08:00
|
|
|
|
private static boolean isActivityAvailable(Context context, String packageName, String className) {
|
|
|
|
|
Intent intent = new Intent();
|
|
|
|
|
intent.setClassName(packageName, className);
|
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
|
return pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 19:54:32 +08:00
|
|
|
|
// 在主线程运行
|
|
|
|
|
private static void runOnUiThread(Runnable action) {
|
|
|
|
|
new Handler(Looper.getMainLooper()).post(action);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-29 16:49:38 +08:00
|
|
|
|
// 检查目标应用是否安装
|
|
|
|
|
public static boolean isAppInstalled(String packageName,PackageManager packageManager) {
|
|
|
|
|
try {
|
|
|
|
|
packageManager.getPackageInfo(packageName, 0);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 22:55:55 +08:00
|
|
|
|
private static final String AUTOJS_SCRIPT_FINISHED_ACTION = "org.autojs.SCRIPT_FINISHED";
|
2025-06-18 14:31:11 +08:00
|
|
|
|
private static final String SCRIPT_RESULT_KEY = "package";
|
2025-05-29 16:49:38 +08:00
|
|
|
|
|
2025-06-12 22:55:55 +08:00
|
|
|
|
public static void stopAutojsScript(Context context) {
|
|
|
|
|
// 停止运行脚本的 Intent
|
|
|
|
|
Intent stopIntent = new Intent();
|
|
|
|
|
stopIntent.setClassName("org.autojs.autojs6", "org.autojs.autojs.external.open.StopServiceActivity");
|
|
|
|
|
stopIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
2025-05-29 16:49:38 +08:00
|
|
|
|
|
2025-06-12 22:55:55 +08:00
|
|
|
|
// 检查目标活动是否存在
|
|
|
|
|
boolean activityAvailable = isActivityAvailable(context, "org.autojs.autojs6", "org.autojs.autojs.external.open.StopServiceActivity");
|
|
|
|
|
Log.d("AutoJsUtil", "是否找到目标活动: " + activityAvailable);
|
|
|
|
|
|
|
|
|
|
if (activityAvailable) {
|
|
|
|
|
try {
|
|
|
|
|
context.startActivity(stopIntent); // 尝试发送停止脚本的 Intent
|
|
|
|
|
Toast.makeText(context, "脚本停止命令已发送", Toast.LENGTH_SHORT).show();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Toast.makeText(context, "无法发送停止命令,请检查 AutoJs 配置", Toast.LENGTH_SHORT).show();
|
|
|
|
|
Log.e("AutoJsUtil", "发送停止命令时发生错误", e);
|
2025-05-29 16:49:38 +08:00
|
|
|
|
}
|
2025-06-12 22:55:55 +08:00
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(context, "目标活动未找到或已更改,请检查 AutoJs 配置", Toast.LENGTH_LONG).show();
|
|
|
|
|
Log.e("AutoJsUtil", "目标活动未找到: org.autojs.autojs.external.open.StopServiceActivity");
|
|
|
|
|
}
|
2025-05-29 16:49:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|