Commit 06652865 by pye52

将设备相关的代码迁移到代理类中,让代码结构更加清晰

parent 4b4b9d07
package com.bgycc.smartcanteen.command;
import android.os.Build;
import com.bgycc.smartcanteen.BuildConfig;
import com.bgycc.smartcanteen.api.SCRetrofit;
import com.bgycc.smartcanteen.entity.Command;
import com.bgycc.smartcanteen.entity.CommandResponse;
import com.bgycc.smartcanteen.entity.CommandUpdate;
import com.bgycc.smartcanteen.entity.ProgressResponseBody;
import com.bgycc.smartcanteen.utils.DangerousUtils;
import com.bgycc.smartcanteen.utils.DeviceProxy;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.FileUtils;
......@@ -85,7 +83,6 @@ public class UpdateCommandHandler extends CommandHandler {
.build();
this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class);
this.updateApk = new File(Utils.getApp().getCacheDir(), UPDATE_APK);
FileUtils.delete(updateApk);
}
private void init(Command command, Gson gson, CommandProgressCallback callback) {
......@@ -94,7 +91,6 @@ public class UpdateCommandHandler extends CommandHandler {
this.commandProgressCallback = callback;
this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class);
this.updateApk = new File(Utils.getApp().getCacheDir(), UPDATE_APK);
FileUtils.delete(updateApk);
}
@Override
......@@ -104,6 +100,7 @@ public class UpdateCommandHandler extends CommandHandler {
return null;
}
start = true;
FileUtils.delete(updateApk);
if (executor == null) {
executor = Executors.newScheduledThreadPool(1);
}
......@@ -158,12 +155,11 @@ public class UpdateCommandHandler extends CommandHandler {
start = false;
return;
}
FileIOUtils.writeFileFromIS(updateApk, body.byteStream());
LogUtils.d(TAG, "更新包下载成功,开始安装");
boolean result = FileIOUtils.writeFileFromIS(updateApk, body.byteStream());
AppUtils.AppInfo info = AppUtils.getApkInfo(updateApk);
LogUtils.d(TAG, "更新包下载: " + (result ? "成功" : "失败") + ",开始安装: " + (info == null ? "null" : info.getPackageName()));
if (info == null || !info.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
FileUtils.delete(updateApk);
LogUtils.w(TAG, "更新包包名非法: " + (info == null ? "null" : info.getPackageName()));
idle("", 0);
start = false;
return;
......@@ -176,22 +172,10 @@ public class UpdateCommandHandler extends CommandHandler {
LogUtils.d(TAG, "不允许安装低版本");
failed("不允许安装低版本", 0);
} else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
// 6.0以下安装包需要修改权限才能安装
try {
Process p = Runtime.getRuntime().exec("chmod 755 " + updateApk);
p.waitFor();
DangerousUtils.installAppSilent(updateApk);
LogUtils.d(TAG, "开始安装");
success("开始安装", 100);
} catch (Exception e) {
LogUtils.e(TAG, "安装文件权限修改失败");
failed("安装文件权限修改失败", 0);
}
} else {
DangerousUtils.installAppSilent(updateApk);
LogUtils.d(TAG, "开始安装");
if (DeviceProxy.updateApp(updateApk)) {
success("开始安装", 100);
} else {
failed("安装文件权限修改失败", 0);
}
}
try {
......
......@@ -2,17 +2,16 @@ package com.bgycc.smartcanteen.qrcode;
import android.os.Build;
public class QRCodeScanFactory {
private static final String TPS = "TPS";
private static final String QUAD = "QUAD";
import com.bgycc.smartcanteen.utils.DeviceProxy;
public class QRCodeScanFactory {
private static final String TPS_PORT = "/dev/ttyS0";
private static final String QUAD_PORT = "/dev/ttyS6";
public static IQRCodeScan create() throws Exception {
String model = Build.MODEL;
if (model.contains(TPS)) {
if (model.contains(DeviceProxy.DEVICE_MODEL_TPS)) {
return new TPS(TPS_PORT);
} else if (model.contains(QUAD)) {
} else if (model.contains(DeviceProxy.DEVICE_MODEL_QUAD)) {
return new QUAD(QUAD_PORT);
} else {
throw new RuntimeException("不明设备型号: " + model);
......
package com.bgycc.smartcanteen.utils;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import com.bgycc.smartcanteen.qrcode.QRCodeScanFactory;
import java.io.File;
/**
* 多设备相关方案的代理类 <br/>
* 设计该代理类目的是为了增加设备时,务必在下面所有方法的实际执行里进行适配
*/
public class DeviceProxy {
public static final String DEVICE_MODEL_TPS = "TPS";
public static final String DEVICE_MODEL_QUAD = "QUAD";
public static IQRCodeScan createQRCodeScan() throws Exception {
return QRCodeScanFactory.create();
}
public static boolean updateApp(File updateApk) {
return InstallManager.install(updateApk);
}
}
package com.bgycc.smartcanteen.utils;
import android.os.Build;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import java.io.File;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
public class InstallManager {
public static boolean install(File updateApk) {
String model = Build.MODEL;
if (model.contains(DeviceProxy.DEVICE_MODEL_TPS)) {
// 6.0以下安装包需要修改权限才能安装
try {
Process p = Runtime.getRuntime().exec("chmod 755 " + updateApk);
p.waitFor();
DangerousUtils.installAppSilent(updateApk);
LogUtils.d(TAG, "开始安装");
return true;
} catch (Exception e) {
LogUtils.e(TAG, "安装文件权限修改失败");
return false;
}
} else if (model.contains(DeviceProxy.DEVICE_MODEL_QUAD)) {
AppUtils.installApp(updateApk);
return true;
} else {
throw new RuntimeException("不明设备型号: " + model);
}
}
}
......@@ -9,11 +9,11 @@ import androidx.lifecycle.ViewModel;
import com.bgycc.smartcanteen.entity.Command;
import com.bgycc.smartcanteen.entity.PayData;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import com.bgycc.smartcanteen.qrcode.QRCodeScanFactory;
import com.bgycc.smartcanteen.state.QRCodeState;
import com.bgycc.smartcanteen.executor.SCTaskExecutor;
import com.bgycc.smartcanteen.repository.CommandRepository;
import com.bgycc.smartcanteen.repository.PayDataRepository;
import com.bgycc.smartcanteen.utils.DeviceProxy;
import com.bgycc.smartcanteen.utils.SmartCanteenUtils;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.JsonObject;
......@@ -58,7 +58,7 @@ public class QRCodeViewModel extends ViewModel {
public void initialize() {
try {
scan = QRCodeScanFactory.create();
scan = DeviceProxy.createQRCodeScan();
scan();
} catch (Exception e) {
LogUtils.e(TAG, "串口初始化失败: " + e.getMessage(), e);
......
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