diff --git a/app/src/main/java/com/xypower/mpapp/BridgeActivity.java b/app/src/main/java/com/xypower/mpapp/BridgeActivity.java index 9ea31d78..04124d06 100644 --- a/app/src/main/java/com/xypower/mpapp/BridgeActivity.java +++ b/app/src/main/java/com/xypower/mpapp/BridgeActivity.java @@ -10,7 +10,7 @@ import android.text.TextUtils; import android.util.Base64; import android.util.Log; -import com.xypower.common.FileUtils; +import com.xypower.common.FilesUtils; import com.xypower.common.JSONUtils; import com.xypower.common.MicroPhotoContext; import com.xypower.mpapp.v2.Camera2VideoActivity; @@ -73,8 +73,8 @@ public class BridgeActivity extends AppCompatActivity { boolean res = MicroPhotoService.genKeys(index); String path = intent.getStringExtra("path"); if (!TextUtils.isEmpty(path)) { - FileUtils.ensureParentDirectoryExisted(path); - FileUtils.writeTextFile(path, res ? "1" : "0"); + FilesUtils.ensureParentDirectoryExisted(path); + FilesUtils.writeTextFile(path, res ? "1" : "0"); } } else if (TextUtils.equals(action, ACTION_CERT_REQ)) { int index = intent.getIntExtra("index", 0); @@ -90,8 +90,8 @@ public class BridgeActivity extends AppCompatActivity { int bv = MicroPhotoService.getGpioInt(117); int bcv = MicroPhotoService.getGpioInt(112); if (!TextUtils.isEmpty(path)) { - FileUtils.ensureParentDirectoryExisted(path); - FileUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv)); + FilesUtils.ensureParentDirectoryExisted(path); + FilesUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv)); File file = new File(path + ".tmp"); file.renameTo(new File(path)); } @@ -118,7 +118,7 @@ public class BridgeActivity extends AppCompatActivity { if (configFile.exists()) { try { - FileUtils.copyFile(configFile, tmpConfigFile); + FilesUtils.copyFile(configFile, tmpConfigFile); } catch (Exception ex) { ex.printStackTrace(); @@ -155,7 +155,7 @@ public class BridgeActivity extends AppCompatActivity { if (file.exists()) { file.delete(); } else { - FileUtils.ensureParentDirectoryExisted(path); + FilesUtils.ensureParentDirectoryExisted(path); } MicroPhotoService.takePhoto(channel, preset, true, tmpConfigFile.getAbsolutePath(), path); diff --git a/app/src/main/java/com/xypower/mpapp/BridgeService.java b/app/src/main/java/com/xypower/mpapp/BridgeService.java index 21d1672f..37bf3278 100644 --- a/app/src/main/java/com/xypower/mpapp/BridgeService.java +++ b/app/src/main/java/com/xypower/mpapp/BridgeService.java @@ -3,7 +3,6 @@ package com.xypower.mpapp; import android.app.Service; import android.content.Intent; -import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.text.TextUtils; @@ -12,7 +11,7 @@ import android.util.Log; import androidx.annotation.Nullable; -import com.xypower.common.FileUtils; +import com.xypower.common.FilesUtils; import com.xypower.common.JSONUtils; import com.xypower.common.MicroPhotoContext; import com.xypower.mpapp.v2.Camera2VideoActivity; @@ -84,8 +83,8 @@ public class BridgeService extends Service { boolean res = MicroPhotoService.genKeys(index); String path = intent.getStringExtra("path"); if (!TextUtils.isEmpty(path)) { - FileUtils.ensureParentDirectoryExisted(path); - FileUtils.writeTextFile(path, res ? "1" : "0"); + FilesUtils.ensureParentDirectoryExisted(path); + FilesUtils.writeTextFile(path, res ? "1" : "0"); } } else if (TextUtils.equals(action, ACTION_CERT_REQ)) { int index = intent.getIntExtra("index", 0); @@ -101,8 +100,8 @@ public class BridgeService extends Service { int bv = MicroPhotoService.getGpioInt(117); int bcv = MicroPhotoService.getGpioInt(112); if (!TextUtils.isEmpty(path)) { - FileUtils.ensureParentDirectoryExisted(path); - FileUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv)); + FilesUtils.ensureParentDirectoryExisted(path); + FilesUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv)); File file = new File(path + ".tmp"); file.renameTo(new File(path)); } @@ -129,7 +128,7 @@ public class BridgeService extends Service { if (configFile.exists()) { try { - FileUtils.copyFile(configFile, tmpConfigFile); + FilesUtils.copyFile(configFile, tmpConfigFile); } catch (Exception ex) { ex.printStackTrace(); @@ -166,7 +165,7 @@ public class BridgeService extends Service { if (file.exists()) { file.delete(); } else { - FileUtils.ensureParentDirectoryExisted(path); + FilesUtils.ensureParentDirectoryExisted(path); } MicroPhotoService.takePhoto(channel, preset, true, tmpConfigFile.getAbsolutePath(), path); diff --git a/common/src/main/java/com/xypower/common/FileUtils.java b/common/src/main/java/com/xypower/common/FilesUtils.java similarity index 95% rename from common/src/main/java/com/xypower/common/FileUtils.java rename to common/src/main/java/com/xypower/common/FilesUtils.java index 81757fdc..730bf812 100644 --- a/common/src/main/java/com/xypower/common/FileUtils.java +++ b/common/src/main/java/com/xypower/common/FilesUtils.java @@ -5,6 +5,7 @@ import android.text.TextUtils; import org.w3c.dom.Text; import java.io.BufferedReader; +import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -17,9 +18,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -public class FileUtils { - - +public class FilesUtils { public static void copyFile(File source, File dest) throws IOException { InputStream input = null; @@ -44,6 +43,15 @@ public class FileUtils { } } + public static void closeFriendly(Closeable closeable) { + if (closeable != null) { + try { + closeable.close(); + } catch (Exception ex) { + } + closeable = null; + } + } public static boolean DeleteFilesInPath(String path, long seconds) { File pathFile = new File(path); if (!pathFile.exists() || !pathFile.isDirectory()) { diff --git a/common/src/main/java/com/xypower/common/MicroPhotoContext.java b/common/src/main/java/com/xypower/common/MicroPhotoContext.java index 1836806f..adb79959 100644 --- a/common/src/main/java/com/xypower/common/MicroPhotoContext.java +++ b/common/src/main/java/com/xypower/common/MicroPhotoContext.java @@ -169,17 +169,17 @@ public class MicroPhotoContext { File path = new File(appPath + "data/imgparams/"); if (path.exists() && path.isDirectory()) { - FileUtils.DeleteFilesInPath(path.getAbsolutePath()); + FilesUtils.DeleteFilesInPath(path.getAbsolutePath()); } path = new File(appPath + "data/schedules/"); if (path.exists() && path.isDirectory()) { - FileUtils.DeleteFilesInPath(path.getAbsolutePath()); + FilesUtils.DeleteFilesInPath(path.getAbsolutePath()); } path = new File(appPath + "data/videoparams/"); if (path.exists() && path.isDirectory()) { - FileUtils.DeleteFilesInPath(path.getAbsolutePath()); + FilesUtils.DeleteFilesInPath(path.getAbsolutePath()); } path = new File(appPath + "data/sampling"); @@ -232,7 +232,7 @@ public class MicroPhotoContext { AppConfig appConfig = new AppConfig(); try { - String content = FileUtils.readTextFile(path); + String content = FilesUtils.readTextFile(path); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); appConfig.cmdid = jsonObject.optString(jsonObject.has("cmdid") ? "cmdid" : "CMDID", ""); @@ -262,7 +262,7 @@ public class MicroPhotoContext { String appPath = buildMasterAppDir(context); try { - String content = FileUtils.readTextFile(appPath + "data/Master.json"); + String content = FilesUtils.readTextFile(appPath + "data/Master.json"); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); masterConfig.server = jsonObject.optString("server", ""); @@ -314,7 +314,7 @@ public class MicroPhotoContext { String path = appPath + "data/Master.json"; - String content = FileUtils.readTextFile(path); + String content = FilesUtils.readTextFile(path); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); jsonObject.put("server", masterConfig.server); @@ -427,7 +427,7 @@ public class MicroPhotoContext { dataPath.mkdirs(); } - String content = FileUtils.readTextFile(path + "data/App.json"); + String content = FilesUtils.readTextFile(path + "data/App.json"); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); jsonObject.put("CMDID", appConfig.cmdid); diff --git a/common/src/main/java/com/xypower/common/ZipUtils.java b/common/src/main/java/com/xypower/common/ZipUtils.java index 1227ecdf..8b89c461 100644 --- a/common/src/main/java/com/xypower/common/ZipUtils.java +++ b/common/src/main/java/com/xypower/common/ZipUtils.java @@ -4,85 +4,106 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FilenameFilter; import java.io.IOException; import java.util.List; +import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtils { - public static interface Filter { - boolean match(String file); - } - public static void ZipFolder(File srcFile, File zipFile, Filter filter) { - try { - ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile)); - ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip, filter); - outZip.finish(); - outZip.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - } - public static void ZipFolders(List srcFiles, File zipFile, Filter filter) { + public static void ZipFolder(File srcDirectory, File zipFile, FilenameFilter filter) { + ZipOutputStream outZip = null; + FileInputStream inputStream = null; + try { - ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile)); - for (File srcFile : srcFiles) { - ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip, filter); + outZip = new ZipOutputStream(new FileOutputStream(zipFile)); + int len; + byte[] buffer = new byte[1024 * 256]; + ZipEntry zipEntry = null; + + File[] subFiles = srcDirectory.listFiles(filter); + if (subFiles != null && subFiles.length > 0) { + for (File subFile : subFiles) { + zipEntry = new ZipEntry(subFile.getName()); + outZip.putNextEntry(zipEntry); + + inputStream = new FileInputStream(subFile); + + while ((len = inputStream.read(buffer)) != -1) { + outZip.write(buffer, 0, len); + } + FilesUtils.closeFriendly(inputStream); + + outZip.closeEntry(); + } } - outZip.finish(); - outZip.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); + } finally { + FilesUtils.closeFriendly(inputStream); + try { + outZip.finish(); + } catch (Exception ex) { + ex.printStackTrace(); + } + FilesUtils.closeFriendly(outZip); } } - private static void ZipFiles(String srcFileParentName, String srcFileName, ZipOutputStream zipOutputSteam, Filter filter) throws Exception { - if (zipOutputSteam == null) - return; - File file = new File(srcFileParentName + srcFileName); + public static void ZipFolders(Map srcDirectories, File zipFile, FilenameFilter filter) { + ZipOutputStream outZip = null; + FileInputStream inputStream = null; - if (file.isFile()) { - ZipEntry zipEntry = new ZipEntry(srcFileName); - FileInputStream inputStream = new FileInputStream(file); - zipOutputSteam.putNextEntry(zipEntry); + try { + outZip = new ZipOutputStream(new FileOutputStream(zipFile)); int len; byte[] buffer = new byte[1024 * 256]; - while ((len = inputStream.read(buffer)) != -1) { - zipOutputSteam.write(buffer, 0, len); - } - zipOutputSteam.closeEntry(); - } else { - // folder - String fileList[] = file.list(); - // NO sub file - if (fileList.length <= 0) { - ZipEntry zipEntry = new ZipEntry(srcFileName + File.separator); - zipOutputSteam.putNextEntry(zipEntry); - zipOutputSteam.closeEntry(); - } - // - for (int i = 0; i < fileList.length; i++) { - if (filter != null && !filter.match(fileList[i])) { + ZipEntry zipEntry = null; + + for (Map.Entry srcDirectory : srcDirectories.entrySet()) { + File[] subFiles = srcDirectory.getValue().listFiles(filter); + if (subFiles == null || subFiles.length == 0) { continue; } - ZipFiles(srcFileParentName + srcFileName + "/", fileList[i], zipOutputSteam, filter); + for (File subFile : subFiles) { + zipEntry = new ZipEntry(srcDirectory.getKey() + File.separator + subFile.getName()); + outZip.putNextEntry(zipEntry); + + inputStream = new FileInputStream(subFile); + + while ((len = inputStream.read(buffer)) != -1) { + outZip.write(buffer, 0, len); + } + FilesUtils.closeFriendly(inputStream); + + outZip.closeEntry(); + } } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + FilesUtils.closeFriendly(inputStream); + try { + outZip.finish(); + } catch (Exception ex) { + ex.printStackTrace(); + } + FilesUtils.closeFriendly(outZip); } } public static void ZipFiles(List srcFiles, File zipFile) { + + ZipOutputStream outZip = null; + FileInputStream inputStream = null; + try { - ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile)); + outZip = new ZipOutputStream(new FileOutputStream(zipFile)); // ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip); + int len = 0; byte[] buffer = new byte[1024 * 256]; for (String path : srcFiles) { @@ -91,24 +112,29 @@ public class ZipUtils { continue; } ZipEntry zipEntry = new ZipEntry(path); - FileInputStream inputStream = new FileInputStream(file); + inputStream = new FileInputStream(file); outZip.putNextEntry(zipEntry); - int len; while ((len = inputStream.read(buffer)) != -1) { outZip.write(buffer, 0, len); } + FilesUtils.closeFriendly(inputStream); + outZip.closeEntry(); } - outZip.finish(); - outZip.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); + } finally { + FilesUtils.closeFriendly(inputStream); + if (outZip != null) { + try { + outZip.finish(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + FilesUtils.closeFriendly(outZip); } } } diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 223d404e..09ceb821 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -33,6 +33,7 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; +import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -48,7 +49,9 @@ import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class AppMaster { @@ -922,16 +925,16 @@ public class AppMaster { final String mpAppDir = MicroPhotoContext.buildMpAppDir(mService.getApplicationContext()); final File logDir = new File(mpAppDir + "logs" + File.separator); - List folders = new ArrayList<>(); + Map folders = new HashMap<>(); if (logDir.exists()) { - folders.add(logDir); + folders.put("logs", logDir); } final String masterAppDir = MicroPhotoContext.buildMasterAppDir(mService.getApplicationContext()); final File mlogDir = new File(masterAppDir + "logs" + File.separator); if (logDir.exists()) { - folders.add(mlogDir); + folders.put("mlogs", mlogDir); } if (folders.isEmpty()) { @@ -939,15 +942,16 @@ public class AppMaster { return; } - ZipUtils.Filter filter = new ZipUtils.Filter() { + FilenameFilter filter = new FilenameFilter() { @Override - public boolean match(String fileName) { - if (!fileName.endsWith(".txt")) { + public boolean accept(File dir, String name) { + if (!name.endsWith(".txt")) { return false; } - if (!includingSpecData && fileName.startsWith("specdata")) { + if (!includingSpecData && name.startsWith("specdata")) { return false; } + return true; } }; @@ -991,9 +995,9 @@ public class AppMaster { return; } - ZipUtils.Filter filter = new ZipUtils.Filter() { + FilenameFilter filter = new FilenameFilter() { @Override - public boolean match(String fileName) { + public boolean accept(File dir, String name) { return fileName.endsWith(".txt"); } }; diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java index 06cd3590..78478976 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java @@ -135,8 +135,6 @@ public class MainActivity extends AppCompatActivity { stringBuilder.append("\r\n"); ((TextView) findViewById((R.id.cmdid))).setText(stringBuilder.toString()); - - } @Override diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java index 65f9a0f0..17c7ee48 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java @@ -6,9 +6,7 @@ import android.content.Intent; import android.os.Build; import android.os.Environment; -import androidx.annotation.RequiresApi; - -import com.xypower.common.FileUtils; +import com.xypower.common.FilesUtils; import com.xypower.common.JSONUtils; import com.xypower.common.MicroPhotoContext; @@ -303,12 +301,12 @@ public class UpdateSysConfigUtil { File path = new File(appPath + "photos/"); if (path.exists() && path.isDirectory()) { - FileUtils.DeleteFilesInPath(path.getAbsolutePath()); + FilesUtils.DeleteFilesInPath(path.getAbsolutePath()); } path = new File(appPath + "sentPhotos/"); if (path.exists() && path.isDirectory()) { - FileUtils.DeleteFilesInPath(path.getAbsolutePath()); + FilesUtils.DeleteFilesInPath(path.getAbsolutePath()); } } @@ -320,12 +318,12 @@ public class UpdateSysConfigUtil { File path = new File(appPath + "logs/"); if (path.exists() && path.isDirectory()) { - FileUtils.DeleteFilesInPath(path.getAbsolutePath()); + FilesUtils.DeleteFilesInPath(path.getAbsolutePath()); } path = new File(masterAppDir + "logs/"); if (path.exists() && path.isDirectory()) { - FileUtils.DeleteFilesInPath(path.getAbsolutePath()); + FilesUtils.DeleteFilesInPath(path.getAbsolutePath()); } }