From 951f6697e86bc66f4f395ed62c1460152700618f Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 25 Jun 2025 19:02:51 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/studyapp/autoJS/AutoJsUtil.java | 2 +- .../studyapp/device/ChangeDeviceInfoUtil.java | 8 +-- .../com/example/studyapp/task/TaskUtil.java | 72 +++++++++++++------ .../studyapp/worker/LoadDeviceWorker.java | 3 + 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/example/studyapp/autoJS/AutoJsUtil.java b/app/src/main/java/com/example/studyapp/autoJS/AutoJsUtil.java index 66a95c4..f962583 100644 --- a/app/src/main/java/com/example/studyapp/autoJS/AutoJsUtil.java +++ b/app/src/main/java/com/example/studyapp/autoJS/AutoJsUtil.java @@ -32,7 +32,7 @@ public class AutoJsUtil { LogFileUtil.logAndWrite(android.util.Log.INFO, "AutoJsUtil", "-------脚本运行开始:--------" + count++,null); File scriptDir = new File(Environment.getExternalStorageDirectory(), "script");//todo scriptDir.delete(); - File scriptFile = downloadCodeFile("main.js", scriptDir);//todo + File scriptFile = downloadCodeFile("mainold.js", scriptDir);//todo if (scriptFile == null || !scriptFile.exists()) { runOnUiThread(() -> Toast.makeText(context, "下载脚本文件失败", Toast.LENGTH_SHORT).show()); LogFileUtil.logAndWrite(android.util.Log.ERROR, "AutoJsUtil", "下载脚本文件失败",null); diff --git a/app/src/main/java/com/example/studyapp/device/ChangeDeviceInfoUtil.java b/app/src/main/java/com/example/studyapp/device/ChangeDeviceInfoUtil.java index fc71374..1ffbc62 100644 --- a/app/src/main/java/com/example/studyapp/device/ChangeDeviceInfoUtil.java +++ b/app/src/main/java/com/example/studyapp/device/ChangeDeviceInfoUtil.java @@ -89,21 +89,15 @@ public class ChangeDeviceInfoUtil { LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error occurred during query", null); return false; } - - if (isValidResponse(response)) { try { synchronized (ChangeDeviceInfoUtil.class) { // 防止并发访问 parseAndSetDeviceObjects(response); } return true; - } catch (JSONException e) { + } catch (Exception e) { LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error parsing JSON", e); 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) { diff --git a/app/src/main/java/com/example/studyapp/task/TaskUtil.java b/app/src/main/java/com/example/studyapp/task/TaskUtil.java index 7e74a78..657a2e3 100644 --- a/app/src/main/java/com/example/studyapp/task/TaskUtil.java +++ b/app/src/main/java/com/example/studyapp/task/TaskUtil.java @@ -622,28 +622,7 @@ public class TaskUtil { if (response.isSuccessful() && response.body() != null) { Log.d("TaskUtil", "Response is successful. Preparing to save file."); // 记录成功响应 - // 检查目录是否存在 - 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; + return saveDownloadedFile(response, filesLocationDir, fileName); } else { 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() { if (okHttpClient == null) { throw new IllegalStateException("HttpClient is not initialized"); diff --git a/app/src/main/java/com/example/studyapp/worker/LoadDeviceWorker.java b/app/src/main/java/com/example/studyapp/worker/LoadDeviceWorker.java index 7fa3403..5575fbe 100644 --- a/app/src/main/java/com/example/studyapp/worker/LoadDeviceWorker.java +++ b/app/src/main/java/com/example/studyapp/worker/LoadDeviceWorker.java @@ -47,9 +47,12 @@ public class LoadDeviceWorker extends CoroutineWorker { boolean result = ChangeDeviceInfoUtil.getDeviceInfoSync(taskId, androidId); String packageName = ChangeDeviceInfoUtil.packageName; String zipName = ChangeDeviceInfoUtil.zipName; + Log.d("TAG", "doWork: "+result+" "+packageName+" "+zipName); if (result && !TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(zipName)){ ChangeDeviceInfoUtil.processPackageInfoWithDeviceInfo(packageName,zipName, getApplicationContext(), androidId, taskId); executeSingleLogic(getApplicationContext()); + }else { + Log.d("TAG", "doWork: get Device info false"); } return Result.success(); }