This commit is contained in:
parent
4642f6e459
commit
951f6697e8
|
@ -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);
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue