From 611cfcbb3a51f8e20d3c060dc851e5f9b519040b Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 10 Sep 2024 14:16:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=8E=B7=E5=8F=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=97=E8=A1=A8=E7=9A=84=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/mpmaster/AppMaster.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 776e4f29..2eb45928 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -29,6 +29,7 @@ import org.json.JSONObject; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; +import java.io.FileFilter; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; @@ -70,6 +71,7 @@ public class AppMaster { public static final String CMD_PUSH_FILE = "push_file"; public static final String CMD_DOWNLOAD_FILE = "dl_file"; public static final String CMD_DELETE_FILE = "del_file"; + public static final String CMD_LIST_FILES = "list_files"; public static final String CMD_HOT_SPOT = "yw_cmd_hot_spot"; public static final String CMD_ENABLE_GPS = "yw_cmd_enable_gps"; public static final String CMD_ENABLE_OTG = "yw_cmd_enable_otg"; @@ -666,6 +668,11 @@ public class AppMaster { String path = jsonObject.optString("path", null); mService.logger.warning("Recv Del File: " + path); deleteFile(path); + } else if (TextUtils.equals(cmd, CMD_LIST_FILES)) { + String path = jsonObject.optString("path", null); + String filter = jsonObject.optString("filter", null); + mService.logger.warning("Recv List Files: " + path); + listFiles(cid, cmd, path, filter); } else if (TextUtils.equals(cmd, CMD_IMPORT_PUB_KEY)) { importPublicKey(jsonObject); } else if (TextUtils.equals(cmd, CMD_UPD_OTA)) { @@ -812,6 +819,44 @@ public class AppMaster { } } + private void listFiles(long cid, String cmd, String path, final String filter) { + try { + File file = new File(path); + if (!file.exists()) { + sendResult(cid, 0, cmd, "Path NOT existed"); + return; + } + if (!file.isDirectory()) { + sendResult(cid, 0, cmd, "Path is NOT a directory"); + return; + } + + FilenameFilter filenameFilter = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.indexOf(filter) != -1; + } + }; + + File[] subFiles = TextUtils.isEmpty(filter) ? file.listFiles() : file.listFiles(filenameFilter); + JSONArray jsonObjects = new JSONArray(); + for (File subFile : subFiles) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("n", subFile.getName()); + if (subFile.isFile()) { + jsonObject.put("s", subFile.length()); + } + jsonObject.put("m", subFile.lastModified()); + jsonObjects.put(jsonObject); + } + + sendResult(cid, 1, cmd, jsonObjects.toString(0)); + } catch (Exception ex) { + ex.printStackTrace(); + } + // sendResult(long cid, int result, String action, String content) { + } + private void updateConfigs(JSONObject jsonObject) { JSONArray jsonConfigs = null; try {