|
@@ -7,18 +7,30 @@ import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.content.IntentFilter;
|
|
|
import android.net.Uri;
|
|
|
+import android.os.AsyncTask;
|
|
|
import android.os.Environment;
|
|
|
import android.util.Log;
|
|
|
|
|
|
+import com.blankj.utilcode.util.ActivityUtils;
|
|
|
import com.blankj.utilcode.util.AppUtils;
|
|
|
import com.blankj.utilcode.util.FileUtils;
|
|
|
import com.blankj.utilcode.util.GsonUtils;
|
|
|
import com.blankj.utilcode.util.LogUtils;
|
|
|
+import com.blankj.utilcode.util.SPUtils;
|
|
|
+import com.blankj.utilcode.util.ShellUtils;
|
|
|
import com.blankj.utilcode.util.ThreadUtils;
|
|
|
|
|
|
+import org.json.JSONException;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
import java.util.List;
|
|
|
|
|
|
-import xn.update.http.bean.UpdateTask;
|
|
|
+import okhttp3.Response;
|
|
|
+import xn.update.http.HttpTool;
|
|
|
+import xn.update.http.bean.response.UpdateTask;
|
|
|
|
|
|
public class TimeTickReceiver extends BroadcastReceiver {
|
|
|
private volatile boolean isRunning = false;
|
|
@@ -28,11 +40,13 @@ public class TimeTickReceiver extends BroadcastReceiver {
|
|
|
@SuppressLint("SdCardPath")
|
|
|
private final String apkPath = "/sdcard/Download/apk/";
|
|
|
private final String apkName = "app.apk";
|
|
|
+ private UpdateTask.Task task;
|
|
|
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
+ LogUtils.d("分钟", isRunning, ActivityUtils.getActivityList());
|
|
|
// 空闲中
|
|
|
- if (!isRunning && !AppUtils.isAppDebug()) {
|
|
|
+ if (!isRunning) {
|
|
|
if (null == downloadManager) {
|
|
|
downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
downloadApkReceiver = new BroadcastReceiver() {
|
|
@@ -40,9 +54,41 @@ public class TimeTickReceiver extends BroadcastReceiver {
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
|
|
if (downloadId == id) {
|
|
|
- Tool.INSTANCE.cmd("pm install -d -g " + apkPath + apkName);
|
|
|
- Tool.INSTANCE.openApp("com.dlc.xn.eboard");
|
|
|
-// isRunning = false;
|
|
|
+ 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(() -> {
|
|
|
+ try {
|
|
|
+ HttpTool.INSTANCE.updateCallBack(task.getTaskId(), task.getDeviceCode(), true, true);
|
|
|
+ Tool.INSTANCE.cmd("pm install -d -g " + apkPath + apkName);
|
|
|
+ Tool.INSTANCE.openApp(task.getStartLaunchPackage());
|
|
|
+ } 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(() -> {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ isRunning = false;
|
|
|
}
|
|
|
}
|
|
|
};
|
|
@@ -53,37 +99,80 @@ public class TimeTickReceiver extends BroadcastReceiver {
|
|
|
public Boolean doInBackground() throws Throwable {
|
|
|
isRunning = true;
|
|
|
try {
|
|
|
- String json = "{\"code\":200,\"message\":\"操作成功\",\"data\":[{\"deviceCode\":\"EBOARD_FE2567751\",\"apkUrl\":\"http://192.168.1.8/api/statics/bigFile/20241206/22e4d366-2df8-4a19-8002-d11b205a1ba3.apk\",\"versionName\":\"1.3\",\"startLaunchPackage\":\"com.dlc.xn.eboard\",\"isForceUpdate\":false}]}";
|
|
|
- UpdateTask updateTask = GsonUtils.fromJson(json, UpdateTask.class);
|
|
|
- if (updateTask.getCode() == 200) {
|
|
|
- List<UpdateTask.Task> updateTaskData = updateTask.getData();
|
|
|
- if (null != updateTaskData && !updateTaskData.isEmpty()) {
|
|
|
- UpdateTask.Task task = updateTaskData.get(0);
|
|
|
- FileUtils.createOrExistsDir(apkPath);
|
|
|
- FileUtils.deleteAllInDir(apkPath);
|
|
|
- DownloadManager.Request request = new DownloadManager.Request(Uri.parse(task.getApkUrl()));
|
|
|
- request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "apk/" + apkName);
|
|
|
- request.setMimeType("application/vnd.android.package-archive");
|
|
|
- request.setVisibleInDownloadsUi(false);
|
|
|
- request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
|
|
|
- context.registerReceiver(downloadApkReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
|
|
- downloadId = downloadManager.enqueue(request);
|
|
|
- return true;
|
|
|
+ Response response = HttpTool.INSTANCE.update();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ String json = response.body().string();
|
|
|
+ UpdateTask updateTask = GsonUtils.fromJson(json, UpdateTask.class);
|
|
|
+ if (updateTask.getCode() == 200) {
|
|
|
+ List<UpdateTask.Task> updateTaskData = updateTask.getData();
|
|
|
+ if (null != updateTaskData && !updateTaskData.isEmpty()) {
|
|
|
+ task = updateTaskData.get(0);
|
|
|
+ FileUtils.createOrExistsDir(apkPath);
|
|
|
+ FileUtils.deleteAllInDir(apkPath);
|
|
|
+ if (!AppUtils.getAppPackageName().equals(task.getStartLaunchPackage())) {
|
|
|
+ SPUtils.getInstance().put("masterApp", task.getStartLaunchPackage());
|
|
|
+ }
|
|
|
+ DownloadManager.Request request = new DownloadManager.Request(Uri.parse(task.getApkUrl()));
|
|
|
+ request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "apk/" + apkName);
|
|
|
+ request.setMimeType("application/vnd.android.package-archive");
|
|
|
+ request.setVisibleInDownloadsUi(false);
|
|
|
+ request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
|
|
|
+ context.registerReceiver(downloadApkReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
|
|
+ downloadId = downloadManager.enqueue(request);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ LogUtils.e(updateTask.getMessage());
|
|
|
}
|
|
|
- } else {
|
|
|
- LogUtils.e(updateTask.getMessage());
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
LogUtils.e(Log.getStackTraceString(e));
|
|
|
}
|
|
|
+ isRunning = false;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onSuccess(Boolean result) {
|
|
|
- if (!result) {
|
|
|
- isRunning = false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!SPUtils.getInstance().getBoolean("isDebug")) {
|
|
|
+ ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInBackground() throws Throwable {
|
|
|
+ try {
|
|
|
+ ShellUtils.CommandResult commandResult = ShellUtils.execCmd("dumpsys activity activities", true);
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(commandResult.successMsg.getBytes())));
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ if (line.contains("mResumedActivity") || line.contains("mCurrentFocus")) {
|
|
|
+ String[] itemArray = line.split(" ", -1);
|
|
|
+ for (String item : itemArray) {
|
|
|
+ if (item.contains("/")) {
|
|
|
+ String packageName = item.substring(0, item.indexOf("/"));
|
|
|
+ if (!"com.dlc.xn.eboard".equals(packageName)) {
|
|
|
+ LogUtils.d("定时拉起主应用");
|
|
|
+ Tool.INSTANCE.stopApp("com.dlc.xn.eboard");
|
|
|
+ Tool.INSTANCE.startMasterApp();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtils.e(Log.getStackTraceString(e));
|
|
|
}
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(Object result) {
|
|
|
+
|
|
|
}
|
|
|
});
|
|
|
}
|