This commit is contained in:
Administrator 2025-06-25 19:02:51 +08:00
parent 4642f6e459
commit 951f6697e8
4 changed files with 55 additions and 30 deletions

View File

@ -32,7 +32,7 @@ public class AutoJsUtil {
LogFileUtil.logAndWrite(android.util.Log.INFO, "AutoJsUtil", "-------脚本运行开始:--------" + count++,null); LogFileUtil.logAndWrite(android.util.Log.INFO, "AutoJsUtil", "-------脚本运行开始:--------" + count++,null);
File scriptDir = new File(Environment.getExternalStorageDirectory(), "script");//todo File scriptDir = new File(Environment.getExternalStorageDirectory(), "script");//todo
scriptDir.delete(); scriptDir.delete();
File scriptFile = downloadCodeFile("main.js", scriptDir);//todo File scriptFile = downloadCodeFile("mainold.js", scriptDir);//todo
if (scriptFile == null || !scriptFile.exists()) { if (scriptFile == null || !scriptFile.exists()) {
runOnUiThread(() -> Toast.makeText(context, "下载脚本文件失败", Toast.LENGTH_SHORT).show()); runOnUiThread(() -> Toast.makeText(context, "下载脚本文件失败", Toast.LENGTH_SHORT).show());
LogFileUtil.logAndWrite(android.util.Log.ERROR, "AutoJsUtil", "下载脚本文件失败",null); LogFileUtil.logAndWrite(android.util.Log.ERROR, "AutoJsUtil", "下载脚本文件失败",null);

View File

@ -89,21 +89,15 @@ public class ChangeDeviceInfoUtil {
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error occurred during query", null); LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error occurred during query", null);
return false; return false;
} }
if (isValidResponse(response)) {
try { try {
synchronized (ChangeDeviceInfoUtil.class) { // 防止并发访问 synchronized (ChangeDeviceInfoUtil.class) { // 防止并发访问
parseAndSetDeviceObjects(response); parseAndSetDeviceObjects(response);
} }
return true; return true;
} catch (JSONException e) { } catch (Exception e) {
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error parsing JSON", e); LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error parsing JSON", e);
return false; return false;
} }
} else {
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error occurred during query",null);
return false;
}
} }
public static void getDeviceInfo(String taskId, String androidId) { public static void getDeviceInfo(String taskId, String androidId) {

View File

@ -622,28 +622,7 @@ public class TaskUtil {
if (response.isSuccessful() && response.body() != null) { if (response.isSuccessful() && response.body() != null) {
Log.d("TaskUtil", "Response is successful. Preparing to save file."); // 记录成功响应 Log.d("TaskUtil", "Response is successful. Preparing to save file."); // 记录成功响应
// 检查目录是否存在 return saveDownloadedFile(response, filesLocationDir, fileName);
if (!filesLocationDir.exists()) {
boolean dirCreated = filesLocationDir.mkdirs();
Log.d("TaskUtil", "Directory created: " + filesLocationDir.getAbsolutePath() + " - " + dirCreated);
}
File saveFile = new File(filesLocationDir, fileName);
Log.d("TaskUtil", "Target file path: " + saveFile.getAbsolutePath());
try (InputStream is = response.body().byteStream();
OutputStream os = new BufferedOutputStream(new FileOutputStream(saveFile))) {
Log.d("TaskUtil", "Starting to write file...");
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
Log.i("TaskUtil", "File saved successfully to: " + saveFile.getAbsolutePath());
}
return saveFile;
} else { } else {
Log.w("TaskUtil", "Download failed. HTTP code: " + response.code() + ", Message: " + response.message()); Log.w("TaskUtil", "Download failed. HTTP code: " + response.code() + ", Message: " + response.message());
@ -666,6 +645,55 @@ public class TaskUtil {
} }
} }
private static File saveDownloadedFile(Response response, File filesLocationDir, String fileName) throws IOException {
// 1. 更严格的目录检查和处理
if (!filesLocationDir.exists()) {
if (!filesLocationDir.mkdirs()) {
throw new IOException("Failed to create directory: " + filesLocationDir.getAbsolutePath());
}
Log.d("TaskUtil", "Directory created: " + filesLocationDir.getAbsolutePath());
}
// 2. 检查目录是否可写
if (!filesLocationDir.canWrite()) {
throw new IOException("Directory not writable: " + filesLocationDir.getAbsolutePath());
}
// 3. 处理文件名冲突
File saveFile = new File(filesLocationDir, fileName);
if (saveFile.exists()) {
saveFile.delete();
}
// 4. 更完善的写入流程
File tempFile = new File(filesLocationDir, fileName + ".tmp");
try (InputStream is = response.body().byteStream();
OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile))) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
// 5. 原子性重命名确保文件完整
if (!tempFile.renameTo(saveFile)) {
throw new IOException("Failed to rename temp file to: " + saveFile.getAbsolutePath());
}
Log.i("TaskUtil", "File saved successfully to: " + saveFile.getAbsolutePath());
return saveFile;
} catch (IOException e) {
// 6. 清理不完整文件
if (tempFile.exists()) {
boolean deleted = tempFile.delete();
Log.w("TaskUtil", "Deleted incomplete file: " + deleted);
}
throw e;
}
}
private static void validate() { private static void validate() {
if (okHttpClient == null) { if (okHttpClient == null) {
throw new IllegalStateException("HttpClient is not initialized"); throw new IllegalStateException("HttpClient is not initialized");

View File

@ -47,9 +47,12 @@ public class LoadDeviceWorker extends CoroutineWorker {
boolean result = ChangeDeviceInfoUtil.getDeviceInfoSync(taskId, androidId); boolean result = ChangeDeviceInfoUtil.getDeviceInfoSync(taskId, androidId);
String packageName = ChangeDeviceInfoUtil.packageName; String packageName = ChangeDeviceInfoUtil.packageName;
String zipName = ChangeDeviceInfoUtil.zipName; String zipName = ChangeDeviceInfoUtil.zipName;
Log.d("TAG", "doWork: "+result+" "+packageName+" "+zipName);
if (result && !TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(zipName)){ if (result && !TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(zipName)){
ChangeDeviceInfoUtil.processPackageInfoWithDeviceInfo(packageName,zipName, getApplicationContext(), androidId, taskId); ChangeDeviceInfoUtil.processPackageInfoWithDeviceInfo(packageName,zipName, getApplicationContext(), androidId, taskId);
executeSingleLogic(getApplicationContext()); executeSingleLogic(getApplicationContext());
}else {
Log.d("TAG", "doWork: get Device info false");
} }
return Result.success(); return Result.success();
} }