Commit 168208bd by pye52

1、增加避免重复安装同一个安装文件的机制

2、更新daemon第三方库版本
3、增加daemon的权限,使其可以正确输出日志文件及删除更新包文件
parent 47898798
...@@ -8,7 +8,6 @@ import androidx.work.WorkerParameters; ...@@ -8,7 +8,6 @@ import androidx.work.WorkerParameters;
import com.bgycc.smartcanteen.BuildConfig; import com.bgycc.smartcanteen.BuildConfig;
import com.bgycc.smartcanteen.api.SCRetrofit; import com.bgycc.smartcanteen.api.SCRetrofit;
import com.bgycc.smartcanteen.entity.CommandUpdate; import com.bgycc.smartcanteen.entity.CommandUpdate;
import com.bgycc.smartcanteen.executor.SCTaskExecutor;
import com.bgycc.smartcanteen.utils.DeviceProxy; import com.bgycc.smartcanteen.utils.DeviceProxy;
import com.bgycc.smartcanteen.utils.TrustAllCerts; import com.bgycc.smartcanteen.utils.TrustAllCerts;
import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.AppUtils;
...@@ -20,7 +19,6 @@ import java.io.File; ...@@ -20,7 +19,6 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
...@@ -41,8 +39,7 @@ public class UpdateCommandWorker extends CommandWorker { ...@@ -41,8 +39,7 @@ public class UpdateCommandWorker extends CommandWorker {
private static final String UPDATE_APK = "SmartCanteen-update.apk"; private static final String UPDATE_APK = "SmartCanteen-update.apk";
private static final String UPDATE_APK_PATH = PathUtils.getExternalStoragePath(); private static final String UPDATE_APK_PATH = PathUtils.getExternalStoragePath();
private static final int BUFFER_SIZE = 8 * 1024; private static final int BUFFER_SIZE = 8 * 1024;
// 下载完毕到唤起安装的延迟 private static final long DEFAULT_DELAY = 3 * 1000;
private static final long INSTALL_DELAY = 500;
// 保证更新任务不会在同一时间内重复执行 // 保证更新任务不会在同一时间内重复执行
private CommandUpdate commandUpdate; private CommandUpdate commandUpdate;
...@@ -61,6 +58,18 @@ public class UpdateCommandWorker extends CommandWorker { ...@@ -61,6 +58,18 @@ public class UpdateCommandWorker extends CommandWorker {
return failed("更新包地址异常"); return failed("更新包地址异常");
} }
File updateApk = new File(UPDATE_APK_PATH, UPDATE_APK); File updateApk = new File(UPDATE_APK_PATH, UPDATE_APK);
if (updateApk.exists()) {
// 若文件存在,则检查安装文件版本是否与当前应用版本一致
// 若一致则说明已安装完毕,直接返回即可
AppUtils.AppInfo info = AppUtils.getApkInfo(updateApk);
if (info != null
&& info.getPackageName().equals(BuildConfig.APPLICATION_ID)
&& info.getVersionCode() == BuildConfig.VERSION_CODE) {
LogUtils.d(TAG, "版本" + info.getVersionCode() + "已安装完毕");
updateApk.deleteOnExit();
return success("已是最新版本");
}
}
updateApk.deleteOnExit(); updateApk.deleteOnExit();
String url = commandUpdate.getData().getUrl(); String url = commandUpdate.getData().getUrl();
...@@ -121,6 +130,13 @@ public class UpdateCommandWorker extends CommandWorker { ...@@ -121,6 +130,13 @@ public class UpdateCommandWorker extends CommandWorker {
} }
} }
// 强制显示一次100%进度,避免下载太快跳帧
inProgress("下载进度: 100%");
// 强制等待一段时间,避免下载太快跳帧
try {
Thread.sleep(DEFAULT_DELAY);
} catch (InterruptedException ignored) {
}
// 检查apk是否为合法安装文件并进入安装状态 // 检查apk是否为合法安装文件并进入安装状态
AppUtils.AppInfo info = AppUtils.getApkInfo(updateApk); AppUtils.AppInfo info = AppUtils.getApkInfo(updateApk);
LogUtils.d(TAG, "更新包下载成功,开始安装: " + (info == null ? "null" : info.getPackageName())); LogUtils.d(TAG, "更新包下载成功,开始安装: " + (info == null ? "null" : info.getPackageName()));
...@@ -137,17 +153,13 @@ public class UpdateCommandWorker extends CommandWorker { ...@@ -137,17 +153,13 @@ public class UpdateCommandWorker extends CommandWorker {
LogUtils.d(TAG, "不允许安装低版本"); LogUtils.d(TAG, "不允许安装低版本");
inProgress("不允许安装低版本"); inProgress("不允许安装低版本");
} else { } else {
// 安装任务需要抛到其他线程进行操作,当前任务直接返回
SCTaskExecutor.getInstance()
.schedule(() -> {
LogUtils.d(TAG, "开始安装更新包: " + updateApk.getAbsolutePath()); LogUtils.d(TAG, "开始安装更新包: " + updateApk.getAbsolutePath());
if (DeviceProxy.updateApp(updateApk)) { if (DeviceProxy.updateApp(updateApk)) {
inProgress("开始安装"); inProgress("开始安装");
} else { } else {
inProgress("安装文件权限修改失败"); inProgress("安装文件权限修改失败");
} }
}, INSTALL_DELAY, TimeUnit.MILLISECONDS);
} }
return success("开始安装更新包"); return success("下载成功,开始安装");
} }
} }
buildscript { buildscript {
ext { ext {
daemon_verson_code = 10 daemon_verson_code = 11
daemon_verson_name = "1.0" daemon_verson_name = "1.1"
} }
repositories { repositories {
google() google()
......
...@@ -100,5 +100,5 @@ dependencies { ...@@ -100,5 +100,5 @@ dependencies {
testImplementation 'junit:junit:4.13' testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.blankj:utilcodex:1.26.0' implementation 'com.blankj:utilcodex:1.28.4'
} }
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bgycc.smartcanteen.daemon"> package="com.bgycc.smartcanteen.daemon">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
......
...@@ -12,18 +12,20 @@ import java.io.File; ...@@ -12,18 +12,20 @@ import java.io.File;
public class ServiceApplication extends Application { public class ServiceApplication extends Application {
private static final String LOG_PREFIX = "daemon"; private static final String LOG_PREFIX = "daemon";
private static final String LOG_DIR = "log"; private static final String LOG_DIR = "log";
// 日志文件保留周期
private static final int LOG_SAVE_DAYS = 7;
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Utils.init(getApplicationContext()); Utils.init(this);
CrashUtils.init();
String logDir = PathUtils.getExternalStoragePath() + File.separator + LOG_DIR; String logDir = PathUtils.getExternalStoragePath() + File.separator + LOG_DIR;
CrashUtils.init(logDir); CrashUtils.init(logDir);
LogUtils.getConfig() LogUtils.getConfig()
.setDir(logDir) .setDir(logDir)
.setLog2FileSwitch(true) .setLog2FileSwitch(true)
.setBorderSwitch(false) .setBorderSwitch(false)
.setFilePrefix(LOG_PREFIX); .setFilePrefix(LOG_PREFIX)
.setSaveDays(LOG_SAVE_DAYS);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment