Commit 06652865 by pye52

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

parent 4b4b9d07
package com.bgycc.smartcanteen.command; package com.bgycc.smartcanteen.command;
import android.os.Build;
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.Command; import com.bgycc.smartcanteen.entity.Command;
import com.bgycc.smartcanteen.entity.CommandResponse; import com.bgycc.smartcanteen.entity.CommandResponse;
import com.bgycc.smartcanteen.entity.CommandUpdate; import com.bgycc.smartcanteen.entity.CommandUpdate;
import com.bgycc.smartcanteen.entity.ProgressResponseBody; 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.AppUtils;
import com.blankj.utilcode.util.FileIOUtils; import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.FileUtils; import com.blankj.utilcode.util.FileUtils;
...@@ -85,7 +83,6 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -85,7 +83,6 @@ public class UpdateCommandHandler extends CommandHandler {
.build(); .build();
this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class); this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class);
this.updateApk = new File(Utils.getApp().getCacheDir(), UPDATE_APK); this.updateApk = new File(Utils.getApp().getCacheDir(), UPDATE_APK);
FileUtils.delete(updateApk);
} }
private void init(Command command, Gson gson, CommandProgressCallback callback) { private void init(Command command, Gson gson, CommandProgressCallback callback) {
...@@ -94,7 +91,6 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -94,7 +91,6 @@ public class UpdateCommandHandler extends CommandHandler {
this.commandProgressCallback = callback; this.commandProgressCallback = callback;
this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class); this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class);
this.updateApk = new File(Utils.getApp().getCacheDir(), UPDATE_APK); this.updateApk = new File(Utils.getApp().getCacheDir(), UPDATE_APK);
FileUtils.delete(updateApk);
} }
@Override @Override
...@@ -104,6 +100,7 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -104,6 +100,7 @@ public class UpdateCommandHandler extends CommandHandler {
return null; return null;
} }
start = true; start = true;
FileUtils.delete(updateApk);
if (executor == null) { if (executor == null) {
executor = Executors.newScheduledThreadPool(1); executor = Executors.newScheduledThreadPool(1);
} }
...@@ -158,12 +155,11 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -158,12 +155,11 @@ public class UpdateCommandHandler extends CommandHandler {
start = false; start = false;
return; return;
} }
FileIOUtils.writeFileFromIS(updateApk, body.byteStream()); boolean result = FileIOUtils.writeFileFromIS(updateApk, body.byteStream());
LogUtils.d(TAG, "更新包下载成功,开始安装");
AppUtils.AppInfo info = AppUtils.getApkInfo(updateApk); AppUtils.AppInfo info = AppUtils.getApkInfo(updateApk);
LogUtils.d(TAG, "更新包下载: " + (result ? "成功" : "失败") + ",开始安装: " + (info == null ? "null" : info.getPackageName()));
if (info == null || !info.getPackageName().equals(BuildConfig.APPLICATION_ID)) { if (info == null || !info.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
FileUtils.delete(updateApk); FileUtils.delete(updateApk);
LogUtils.w(TAG, "更新包包名非法: " + (info == null ? "null" : info.getPackageName()));
idle("", 0); idle("", 0);
start = false; start = false;
return; return;
...@@ -176,22 +172,10 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -176,22 +172,10 @@ public class UpdateCommandHandler extends CommandHandler {
LogUtils.d(TAG, "不允许安装低版本"); LogUtils.d(TAG, "不允许安装低版本");
failed("不允许安装低版本", 0); failed("不允许安装低版本", 0);
} else { } else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { if (DeviceProxy.updateApp(updateApk)) {
// 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, "开始安装");
success("开始安装", 100); success("开始安装", 100);
} else {
failed("安装文件权限修改失败", 0);
} }
} }
try { try {
......
...@@ -2,17 +2,16 @@ package com.bgycc.smartcanteen.qrcode; ...@@ -2,17 +2,16 @@ package com.bgycc.smartcanteen.qrcode;
import android.os.Build; import android.os.Build;
public class QRCodeScanFactory { import com.bgycc.smartcanteen.utils.DeviceProxy;
private static final String TPS = "TPS";
private static final String QUAD = "QUAD";
public class QRCodeScanFactory {
private static final String TPS_PORT = "/dev/ttyS0"; private static final String TPS_PORT = "/dev/ttyS0";
private static final String QUAD_PORT = "/dev/ttyS6"; private static final String QUAD_PORT = "/dev/ttyS6";
public static IQRCodeScan create() throws Exception { public static IQRCodeScan create() throws Exception {
String model = Build.MODEL; String model = Build.MODEL;
if (model.contains(TPS)) { if (model.contains(DeviceProxy.DEVICE_MODEL_TPS)) {
return new TPS(TPS_PORT); return new TPS(TPS_PORT);
} else if (model.contains(QUAD)) { } else if (model.contains(DeviceProxy.DEVICE_MODEL_QUAD)) {
return new QUAD(QUAD_PORT); return new QUAD(QUAD_PORT);
} else { } else {
throw new RuntimeException("不明设备型号: " + model); 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; ...@@ -9,11 +9,11 @@ import androidx.lifecycle.ViewModel;
import com.bgycc.smartcanteen.entity.Command; import com.bgycc.smartcanteen.entity.Command;
import com.bgycc.smartcanteen.entity.PayData; import com.bgycc.smartcanteen.entity.PayData;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan; import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import com.bgycc.smartcanteen.qrcode.QRCodeScanFactory;
import com.bgycc.smartcanteen.state.QRCodeState; import com.bgycc.smartcanteen.state.QRCodeState;
import com.bgycc.smartcanteen.executor.SCTaskExecutor; import com.bgycc.smartcanteen.executor.SCTaskExecutor;
import com.bgycc.smartcanteen.repository.CommandRepository; import com.bgycc.smartcanteen.repository.CommandRepository;
import com.bgycc.smartcanteen.repository.PayDataRepository; import com.bgycc.smartcanteen.repository.PayDataRepository;
import com.bgycc.smartcanteen.utils.DeviceProxy;
import com.bgycc.smartcanteen.utils.SmartCanteenUtils; import com.bgycc.smartcanteen.utils.SmartCanteenUtils;
import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.LogUtils;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
...@@ -58,7 +58,7 @@ public class QRCodeViewModel extends ViewModel { ...@@ -58,7 +58,7 @@ public class QRCodeViewModel extends ViewModel {
public void initialize() { public void initialize() {
try { try {
scan = QRCodeScanFactory.create(); scan = DeviceProxy.createQRCodeScan();
scan(); scan();
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "串口初始化失败: " + e.getMessage(), 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