Commit 82fbbeb1 by pye52

Merge branch 'develop'

parents 4189ff05 ce425b44
......@@ -14,8 +14,6 @@ import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ZipUtils;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
......@@ -149,15 +147,14 @@ public class LogCommandWorker extends CommandWorker {
return zip;
}
@NotNull
private File getLogDirByType(String logType) {
File logDir;
if (logType.equals(BOOT_LOG)) {
logDir = new File(DeviceProxy.getSystemLogDir());
logDir = DeviceProxy.systemLogDir();
} else {
logDir = new File(LogUtils.getConfig().getDir());
}
LogUtils.d(TAG, "待上传日志文件类型: " + logType);
LogUtils.d(TAG, "待上传日志文件类型: " + logType + ", 日志文件夹路径: " + (logDir != null ? logDir : "null"));
return logDir;
}
......@@ -177,6 +174,9 @@ public class LogCommandWorker extends CommandWorker {
}
private boolean copyTargetFiles(File src, File descDir) {
if (src == null || descDir == null) {
return true;
}
FileFilter filter = file -> {
Date date = new Date(file.lastModified());
return date.after(startTime) && date.before(endTime);
......
......@@ -154,7 +154,7 @@ public class UpdateCommandWorker extends CommandWorker {
inProgress("不允许安装低版本");
} else {
LogUtils.d(TAG, "开始安装更新包: " + updateApk.getAbsolutePath());
if (DeviceProxy.updateApp(updateApk)) {
if (DeviceProxy.updateApp(getApplicationContext(), updateApk)) {
inProgress("开始安装");
} else {
inProgress("安装文件权限修改失败");
......
package com.bgycc.smartcanteen.device;
import android.content.Context;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import java.io.File;
public interface DeviceFeatures {
IQRCodeScan createQRCodeScan() throws Exception;
File systemLogDir();
boolean updateApp(Context context, File updateApk);
}
package com.bgycc.smartcanteen.device;
import android.content.Context;
import com.bgycc.smartcanteen.installer.QUADInstaller;
import com.bgycc.smartcanteen.qrcode.DefaultQRCodeScan;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import com.blankj.utilcode.util.PathUtils;
import com.telpo.tps550.api.DeviceAlreadyOpenException;
import java.io.File;
import java.io.IOException;
public class QUADDevice implements DeviceFeatures {
private static final String SYSTEM_LOG_DIR_NAME = "boot_log";
private static final String PORT = "/dev/ttyS6";
@Override
public IQRCodeScan createQRCodeScan() throws IOException, DeviceAlreadyOpenException {
return new DefaultQRCodeScan(PORT);
}
@Override
public File systemLogDir() {
return new File(PathUtils.getExternalStoragePath() + File.separator + SYSTEM_LOG_DIR_NAME);
}
@Override
public boolean updateApp(Context context, File updateApk) {
return new QUADInstaller().install(context, updateApk);
}
}
package com.bgycc.smartcanteen.device;
import android.content.Context;
import com.bgycc.smartcanteen.installer.TPSInstaller;
import com.bgycc.smartcanteen.qrcode.DefaultQRCodeScan;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import com.telpo.tps550.api.DeviceAlreadyOpenException;
import java.io.File;
import java.io.IOException;
public class TPSDevice implements DeviceFeatures {
private static final String PORT = "/dev/ttyS0";
@Override
public IQRCodeScan createQRCodeScan() throws IOException, DeviceAlreadyOpenException {
return new DefaultQRCodeScan(PORT);
}
@Override
public File systemLogDir() {
return null;
}
@Override
public boolean updateApp(Context context, File updateApk) {
return new TPSInstaller().install(context, updateApk);
}
}
package com.bgycc.smartcanteen.installer;
import android.content.Context;
import android.os.Build;
import com.blankj.utilcode.util.LogUtils;
import java.io.File;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
@SuppressWarnings("all")
public abstract class DefaultInstaller {
protected static final String ARG_PATH = "arg_path";
protected static final String ARG_COMPONENT = "arg_package";
// assets文件夹中守护进程apk名称
protected static final String DAEMON_APK_NAME = "Daemon.apk";
// 守护package name
protected static final String DAEMON_PACKAGE_NAME = "com.bgycc.smartcanteen.daemon";
// 守护服务类名称
protected static final String DAEMON_SERVICE_NAME = "com.bgycc.smartcanteen.daemon.DaemonService";
public final boolean install(Context context, File updateApk) {
String model = Build.MODEL;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
// 6.0以下安装包需要修改权限才能安装
try {
Process p = Runtime.getRuntime().exec("chmod 755 " + updateApk);
p.waitFor();
LogUtils.d(TAG, "安装文件权限修改成功");
} catch (Exception e) {
LogUtils.e(TAG, "安装文件权限修改失败");
return false;
}
}
return installActual(context, updateApk);
}
public abstract boolean installActual(Context context, File updateApk);
}
package com.bgycc.smartcanteen.installer;
import android.content.Context;
import com.blankj.utilcode.util.AppUtils;
import java.io.File;
public class QUADInstaller extends DefaultInstaller {
@Override
public boolean installActual(Context context, File updateApk) {
AppUtils.installApp(updateApk);
return true;
}
}
package com.bgycc.smartcanteen.utils;
package com.bgycc.smartcanteen.installer;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.bgycc.smartcanteen.BuildConfig;
import com.bgycc.smartcanteen.activity.MainActivity;
import com.bgycc.smartcanteen.utils.DangerousUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
......@@ -19,67 +20,27 @@ import java.io.InputStream;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
public class InstallManager {
private static final String ARG_PATH = "arg_path";
private static final String ARG_COMPONENT = "arg_package";
// assets文件夹中守护进程apk名称
private static final String DAEMON_APK_NAME = "Daemon.apk";
// 守护package name
private static final String DAEMON_PACKAGE_NAME = "com.bgycc.smartcanteen.daemon";
// 守护服务类名称
private static final String DAEMON_SERVICE_NAME = "com.bgycc.smartcanteen.daemon.DaemonService";
public static boolean install(File updateApk) {
String model = Build.MODEL;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
// 6.0以下安装包需要修改权限才能安装
try {
Process p = Runtime.getRuntime().exec("chmod 755 " + updateApk);
p.waitFor();
LogUtils.d(TAG, "安装文件权限修改成功");
} catch (Exception e) {
LogUtils.e(TAG, "安装文件权限修改失败");
return false;
public class TPSInstaller extends DefaultInstaller {
@Override
public boolean installActual(Context context, File updateApk) {
// 检查守护进程的安装情况,及其安装版本
if (!AppUtils.isAppInstalled(DAEMON_PACKAGE_NAME)) {
LogUtils.d(TAG, "守护app未安装,从assets复制并进行安装");
if (installDaemonFromAssets(false)) {
return DangerousUtils.installAppSilent(updateApk);
}
}
if (model.contains(DeviceProxy.DEVICE_MODEL_TPS)) {
// 检查守护进程的安装情况,及其安装版本
if (!AppUtils.isAppInstalled(DAEMON_PACKAGE_NAME)) {
LogUtils.d(TAG, "守护app未安装,从assets复制并进行安装");
if (installDaemonFromAssets(false)) {
} else {
// 若已安装,则检查是否需要更新
AppUtils.AppInfo daemonAppInfo = AppUtils.getAppInfo(DAEMON_PACKAGE_NAME);
if (daemonAppInfo.getVersionCode() < BuildConfig.DaemonVersion) {
LogUtils.d(TAG, "当前守护app版本: " + daemonAppInfo.getVersionCode() + ", 最新版本: " + BuildConfig.DaemonVersion);
if (installDaemonFromAssets(true)) {
return DangerousUtils.installAppSilent(updateApk);
}
} else {
// 若已安装,则检查是否需要更新
AppUtils.AppInfo daemonAppInfo = AppUtils.getAppInfo(DAEMON_PACKAGE_NAME);
if (daemonAppInfo.getVersionCode() < BuildConfig.DaemonVersion) {
LogUtils.d(TAG, "当前守护app版本: " + daemonAppInfo.getVersionCode() + ", 最新版本: " + BuildConfig.DaemonVersion);
if (installDaemonFromAssets(true)) {
return DangerousUtils.installAppSilent(updateApk);
}
}
}
startDaemonForInstall(updateApk);
return true;
} else if (model.contains(DeviceProxy.DEVICE_MODEL_QUAD)) {
AppUtils.installApp(updateApk);
return true;
} else {
throw new RuntimeException("不明设备型号: " + model);
}
}
private static void copyDaemonApkToTarget(File daemonApk) throws IOException {
InputStream is = Utils.getApp().getAssets().open(DAEMON_APK_NAME);
FileOutputStream fos = new FileOutputStream(daemonApk);
byte[] buffer = new byte[1024];
int byteCount;
while ((byteCount = is.read(buffer)) != -1) {
fos.write(buffer, 0, byteCount);
}
fos.flush();
is.close();
fos.close();
startDaemonForInstall(context, updateApk);
return true;
}
private static boolean installDaemonFromAssets(boolean update) {
......@@ -101,7 +62,20 @@ public class InstallManager {
return false;
}
private static void startDaemonForInstall(File updateApk) {
private static void copyDaemonApkToTarget(File daemonApk) throws IOException {
InputStream is = Utils.getApp().getAssets().open(DAEMON_APK_NAME);
FileOutputStream fos = new FileOutputStream(daemonApk);
byte[] buffer = new byte[1024];
int byteCount;
while ((byteCount = is.read(buffer)) != -1) {
fos.write(buffer, 0, byteCount);
}
fos.flush();
is.close();
fos.close();
}
private static void startDaemonForInstall(Context context, File updateApk) {
LogUtils.d(TAG, "启动守护app进行更新操作");
ComponentName componentName = new ComponentName(DAEMON_PACKAGE_NAME, DAEMON_SERVICE_NAME);
Intent intent = new Intent();
......@@ -109,6 +83,6 @@ public class InstallManager {
intent.putExtra(ARG_COMPONENT, BuildConfig.APPLICATION_ID + File.separator + MainActivity.class.getName());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(componentName);
Utils.getApp().startService(intent);
context.startService(intent);
}
}
package com.bgycc.smartcanteen.qrcode;
import com.blankj.utilcode.util.LogUtils;
import com.telpo.tps550.api.DeviceAlreadyOpenException;
import com.telpo.tps550.api.serial.Serial;
import java.io.IOException;
......@@ -13,7 +14,7 @@ public class DefaultQRCodeScan implements IQRCodeScan {
private Serial serial;
private InputStream serialPortIS;
public DefaultQRCodeScan(String portPath) throws Exception {
public DefaultQRCodeScan(String portPath) throws IOException, DeviceAlreadyOpenException {
serial = new Serial(portPath, 115200, 0);
this.serialPortIS = serial.getInputStream();
}
......
package com.bgycc.smartcanteen.qrcode;
import android.os.Build;
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(DeviceProxy.DEVICE_MODEL_TPS)) {
return new DefaultQRCodeScan(TPS_PORT);
} else if (model.contains(DeviceProxy.DEVICE_MODEL_QUAD)) {
return new DefaultQRCodeScan(QUAD_PORT);
} else {
throw new RuntimeException("不明设备型号: " + model);
}
}
}
package com.bgycc.smartcanteen.utils;
import android.content.Context;
import android.os.Build;
import com.bgycc.smartcanteen.device.DeviceFeatures;
import com.bgycc.smartcanteen.device.QUADDevice;
import com.bgycc.smartcanteen.device.TPSDevice;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import com.bgycc.smartcanteen.qrcode.QRCodeScanFactory;
import com.blankj.utilcode.util.PathUtils;
import java.io.File;
......@@ -13,26 +15,30 @@ import java.io.File;
* 设计该代理类目的是为了增加设备时,务必在下面所有方法的实际执行里进行适配
*/
public class DeviceProxy {
public static final String DEVICE_MODEL_TPS = "TPS";
public static final String DEVICE_MODEL_QUAD = "QUAD";
private static final String QUAD_SYSTEM_LOG = "boot_log";
private static final String DEVICE_MODEL_TPS = "TPS";
private 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);
}
public static String getSystemLogDir() {
private static DeviceFeatures deviceImpl;
static {
String model = Build.MODEL;
if (model.contains(DEVICE_MODEL_TPS)) {
return PathUtils.getExternalStoragePath() + File.separator + QUAD_SYSTEM_LOG;
deviceImpl = new TPSDevice();
} else if (model.contains(DEVICE_MODEL_QUAD)) {
return PathUtils.getExternalStoragePath() + File.separator + QUAD_SYSTEM_LOG;
deviceImpl = new QUADDevice();
} else {
return PathUtils.getExternalStoragePath() + File.separator + QUAD_SYSTEM_LOG;
deviceImpl = new TPSDevice();
}
}
public static IQRCodeScan createQRCodeScan() throws Exception {
return deviceImpl.createQRCodeScan();
}
public static boolean updateApp(Context context, File updateApk) {
return deviceImpl.updateApp(context, updateApk);
}
public static File systemLogDir() {
return deviceImpl.systemLogDir();
}
}
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