瀏覽代碼

1.修复日志被错误删除的BUG

JaycePC 1 月之前
父節點
當前提交
a33e6b21c2

+ 1 - 1
app/build.gradle

@@ -12,7 +12,7 @@ android {
         //noinspection ExpiredTargetSdkVersion
         targetSdk 28
         versionCode 1
-        versionName "1.5"
+        versionName "1.7"
 
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
     }

+ 53 - 37
app/src/main/java/xn/huaxue/update/receiver/TimeTickReceiver.java

@@ -37,6 +37,7 @@ import xn.huaxue.update.http.bean.response.UpdateTask;
 
 public class TimeTickReceiver extends BroadcastReceiver {
     private volatile boolean isRunning = false;
+    private long lastRunTime;
     private DownloadManager downloadManager;
     private BroadcastReceiver downloadApkReceiver;
     private long downloadId;
@@ -53,8 +54,14 @@ public class TimeTickReceiver extends BroadcastReceiver {
         Intent newIntent = new Intent("XN_ACTION");
         intent.putExtra("heartbeat", "heartbeat");
         context.sendBroadcast(newIntent);
-        LogUtils.d("分钟", isRunning, ActivityUtils.getActivityList());
+        // 若是3分钟都没下载完成,则认为下载失败
+        long runTime = (System.currentTimeMillis() - lastRunTime);
+        if (lastRunTime > 0 && runTime > 1000 * 60 * 3) {
+            isRunning = false;
+        }
         if (!isRunning) {
+            lastRunTime = System.currentTimeMillis();
+            isRunning = true;
             if (null == downloadManager) {
                 downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
                 downloadApkReceiver = new BroadcastReceiver() {
@@ -63,60 +70,69 @@ public class TimeTickReceiver extends BroadcastReceiver {
                         long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                         if (downloadId == id) {
                             if (null != task) {
-                                AsyncTask.execute(() -> {
-                                    try {
-                                        HttpTool.INSTANCE.updateCallBack(task.getTaskId(), task.getDeviceCode(), true, false);
-                                    } catch (IOException | JSONException e) {
-                                        LogUtils.e(Log.getStackTraceString(e));
-                                    }
-                                });
-                                if (task.getStartLaunchPackage().equals(AppUtils.getAppPackageName())) {
-                                    AsyncTask.execute(() -> {
+                                AsyncTask.execute(new Runnable() {
+                                    @Override
+                                    public void run() {
                                         try {
-                                            HttpTool.INSTANCE.updateCallBack(task.getTaskId(), task.getDeviceCode(), true, true);
-                                            if (AppUtils.getAppPackageName().equals(task.getAppPackageName())) {
-                                                Tool.INSTANCE.cmd("pm install -d -g " + apkPath + apkName);
-                                                Tool.INSTANCE.openApp(task.getStartLaunchPackage());
-                                            } else {
-                                                ShellUtils.execCmdAsync("pm uninstall " + task.getAppPackageName(), true, new Utils.Consumer<ShellUtils.CommandResult>() {
-                                                    @Override
-                                                    public void accept(ShellUtils.CommandResult commandResult) {
-                                                        LogUtils.d(commandResult);
-                                                        Tool.INSTANCE.cmd("pm install -d -g " + apkPath + apkName);
-                                                        Tool.INSTANCE.openApp(task.getStartLaunchPackage());
-                                                    }
-                                                });
-                                            }
+                                            HttpTool.INSTANCE.updateCallBack(task.getTaskId(), task.getDeviceCode(), true, false);
                                         } catch (IOException | JSONException e) {
                                             LogUtils.e(Log.getStackTraceString(e));
                                         }
-                                    });
-                                } else {
-                                    ShellUtils.CommandResult commandResult = ShellUtils.execCmd("pm install -d -g " + apkPath + apkName, true);
-                                    if (commandResult.result == 0) {
-                                        AsyncTask.execute(() -> {
+
+                                        if (task.getStartLaunchPackage().equals(AppUtils.getAppPackageName())) {
                                             try {
                                                 HttpTool.INSTANCE.updateCallBack(task.getTaskId(), task.getDeviceCode(), true, true);
+                                                if (AppUtils.getAppPackageName().equals(task.getAppPackageName())) {
+                                                    Tool.INSTANCE.cmd("pm install -d -g " + apkPath + apkName);
+                                                    Tool.INSTANCE.openApp(task.getStartLaunchPackage());
+                                                } else {
+                                                    ShellUtils.execCmdAsync("pm uninstall " + task.getAppPackageName(), true, new Utils.Consumer<ShellUtils.CommandResult>() {
+                                                        @Override
+                                                        public void accept(ShellUtils.CommandResult commandResult) {
+                                                            LogUtils.d(commandResult);
+                                                            Tool.INSTANCE.cmd("pm install -d -g " + apkPath + apkName);
+                                                            Tool.INSTANCE.openApp(task.getStartLaunchPackage());
+                                                        }
+                                                    });
+                                                }
                                             } catch (IOException | JSONException e) {
                                                 LogUtils.e(Log.getStackTraceString(e));
                                             }
-                                        });
-                                        Tool.INSTANCE.startMasterApp();
-                                    } else {
-                                        LogUtils.e(commandResult.errorMsg);
+                                        } else {
+                                            ShellUtils.CommandResult commandResult = ShellUtils.execCmd("pm install -d -g " + apkPath + apkName, true);
+
+                                            if (commandResult.result == 0) {
+                                                try {
+                                                    HttpTool.INSTANCE.updateCallBack(task.getTaskId(), task.getDeviceCode(), true, true);
+                                                } catch (IOException | JSONException e) {
+                                                    LogUtils.e(Log.getStackTraceString(e));
+                                                }
+                                                Tool.INSTANCE.startMasterApp();
+                                            } else {
+                                                LogUtils.e(commandResult.errorMsg);
+                                            }
+                                        }
+                                        try {
+                                            Thread.sleep(1000 * 30);
+                                        } catch (InterruptedException e) {
+                                            LogUtils.e(Log.getStackTraceString(e));
+                                        }
+                                        isRunning = false;
                                     }
-                                }
+                                });
                             }
-                            isRunning = false;
                         }
                     }
                 };
             }
+
+
+
             ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Boolean>() {
                 @SuppressLint({"UnspecifiedRegisterReceiverFlag", "SdCardPath"})
                 @Override
                 public Boolean doInBackground() throws Throwable {
-                    isRunning = true;
+
                     try {
                         Response response = HttpTool.INSTANCE.update();
                         if (response.isSuccessful()) {
@@ -147,12 +163,12 @@ public class TimeTickReceiver extends BroadcastReceiver {
                     } catch (Exception e) {
                         LogUtils.e(Log.getStackTraceString(e));
                     }
-                    isRunning = false;
                     return false;
                 }
 
                 @Override
                 public void onSuccess(Boolean result) {
+                    isRunning = result;
                 }
             });
 

+ 9 - 7
app/src/main/java/xn/huaxue/update/service/TaskService.java

@@ -112,7 +112,12 @@ public class TaskService extends Service {
         }
     }
 
+    private Notification notification;
+
     private Notification createNotification() {
+        if (null != notification) {
+            return notification;
+        }
         NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "task")
                 .setContentTitle("Task")
                 .setContentText("Task is running")
@@ -120,7 +125,8 @@ public class TaskService extends Service {
                 .setContentIntent(null)
                 .setSilent(true)
                 .setPriority(NotificationCompat.PRIORITY_DEFAULT);
-        return builder.build();
+        notification = builder.build();
+        return notification;
     }
 
     private void initDev() {
@@ -234,13 +240,9 @@ public class TaskService extends Service {
                 List<File> fileList = FileUtils.listFilesInDir("/sdcard/logs/");
                 for (int i = 0; i < fileList.size(); i++) {
                     File file = fileList.get(i);
-                    String fileName = file.getName();
-                    fileName = fileName.replace(AppUtils.getAppName() + "_", "");
-                    fileName = fileName.replace("_" + AppUtils.getAppPackageName(), "");
-                    fileName = fileName.replace(".txt", "");
-                    long fileTime = TimeUtils.string2Millis(fileName, new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()));
                     long oneMonth = 30 * 86400 * 1000L;
-                    if ((System.currentTimeMillis() - fileTime) > oneMonth) {
+                    if ((System.currentTimeMillis() - file.lastModified()) > oneMonth) {
+                        LogUtils.d("删除日志文件", file);
                         FileUtils.delete(file);
                     }
                 }