|
@@ -37,6 +37,7 @@ import xn.huaxue.update.http.bean.response.UpdateTask;
|
|
|
|
|
|
public class TimeTickReceiver extends BroadcastReceiver {
|
|
public class TimeTickReceiver extends BroadcastReceiver {
|
|
private volatile boolean isRunning = false;
|
|
private volatile boolean isRunning = false;
|
|
|
|
+ private long lastRunTime;
|
|
private DownloadManager downloadManager;
|
|
private DownloadManager downloadManager;
|
|
private BroadcastReceiver downloadApkReceiver;
|
|
private BroadcastReceiver downloadApkReceiver;
|
|
private long downloadId;
|
|
private long downloadId;
|
|
@@ -53,8 +54,14 @@ public class TimeTickReceiver extends BroadcastReceiver {
|
|
Intent newIntent = new Intent("XN_ACTION");
|
|
Intent newIntent = new Intent("XN_ACTION");
|
|
intent.putExtra("heartbeat", "heartbeat");
|
|
intent.putExtra("heartbeat", "heartbeat");
|
|
context.sendBroadcast(newIntent);
|
|
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) {
|
|
if (!isRunning) {
|
|
|
|
+ lastRunTime = System.currentTimeMillis();
|
|
|
|
+ isRunning = true;
|
|
if (null == downloadManager) {
|
|
if (null == downloadManager) {
|
|
downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
|
downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
|
downloadApkReceiver = new BroadcastReceiver() {
|
|
downloadApkReceiver = new BroadcastReceiver() {
|
|
@@ -63,60 +70,69 @@ public class TimeTickReceiver extends BroadcastReceiver {
|
|
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
|
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
|
if (downloadId == id) {
|
|
if (downloadId == id) {
|
|
if (null != task) {
|
|
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 {
|
|
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) {
|
|
} catch (IOException | JSONException e) {
|
|
LogUtils.e(Log.getStackTraceString(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 {
|
|
try {
|
|
HttpTool.INSTANCE.updateCallBack(task.getTaskId(), task.getDeviceCode(), true, true);
|
|
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) {
|
|
} catch (IOException | JSONException e) {
|
|
LogUtils.e(Log.getStackTraceString(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>() {
|
|
ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Boolean>() {
|
|
@SuppressLint({"UnspecifiedRegisterReceiverFlag", "SdCardPath"})
|
|
@SuppressLint({"UnspecifiedRegisterReceiverFlag", "SdCardPath"})
|
|
@Override
|
|
@Override
|
|
public Boolean doInBackground() throws Throwable {
|
|
public Boolean doInBackground() throws Throwable {
|
|
- isRunning = true;
|
|
|
|
|
|
+
|
|
try {
|
|
try {
|
|
Response response = HttpTool.INSTANCE.update();
|
|
Response response = HttpTool.INSTANCE.update();
|
|
if (response.isSuccessful()) {
|
|
if (response.isSuccessful()) {
|
|
@@ -147,12 +163,12 @@ public class TimeTickReceiver extends BroadcastReceiver {
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
LogUtils.e(Log.getStackTraceString(e));
|
|
LogUtils.e(Log.getStackTraceString(e));
|
|
}
|
|
}
|
|
- isRunning = false;
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void onSuccess(Boolean result) {
|
|
public void onSuccess(Boolean result) {
|
|
|
|
+ isRunning = result;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|