Pārlūkot izejas kodu

3.0 optimize:保存文件

xwh 1 mēnesi atpakaļ
vecāks
revīzija
b158550df2

+ 4 - 6
lib_base/src/main/java/com/hh/lib_base/Constants.java

@@ -114,12 +114,14 @@ public class Constants {
 
         public static final String LOG_STORAGE_FACE = "BPaaSApi--刷脸入库---";
 
-        public static final String LOG_FLOATINGEXITBUTTON = "悬浮返回按钮---";
+        public static final String LOG_FLOATINGEXITBUTTON = "悬浮返回按钮";
 
         public static final String LOG_M1CARD = "NFC_M1卡";
 
         public static final String LOG_ASR = "ASR-语音识别";
 
+        public static final String LOG_DOWNLOADFILE = "文件下载";
+
         // 对应web的日志类型
         public static final String NET_LOGCAT = "Logcat";
         public static final String NET_ERROR = "Error";
@@ -169,11 +171,7 @@ public class Constants {
         // 设备信息
         public static final String ACTION_DEVICE = "getDeviceInfo";
         // 保存文件
-        public static final String ACTION_SETSTORAGE = "setStorage";
-        // 保存文件列表
-        public static final String ACTION_GETSTORAGELIST = "getStorageList";
-        // 删除文件
-        public static final String ACTION_REMOVESTORAGE = "removeStorage";
+        public static final String ACTION_STORAGE = "storage";
 
         // 语音播报
         public static final String ACTION_SPEECH = "speech";

+ 97 - 25
lib_device/src/main/java/com/hh/lib_device/DevicesInvoker.java

@@ -25,12 +25,13 @@ import com.hh.lib_base.utils.device.DeviceUtils;
 import com.hh.lib_device.bean.action.AmpeDeviceConfigResultBean;
 import com.hh.lib_device.bean.action.AmpeDeviceConfigBean;
 import com.hh.lib_device.bean.action.AmpeDeviceInfoResultBean;
-import com.hh.lib_device.bean.action.AmpeDeviceSaveFileConfigBean;
+import com.hh.lib_device.bean.action.AmpeRequestDownloadFileBean;
 import com.hh.lib_device.bean.action.AmpeRequestFloatingBackBean;
+import com.hh.lib_device.bean.action.AmpeResultDownloadFileBean;
+import com.hh.lib_device.bean.download.AmpeDownloadFileEvent;
 import com.hh.lib_device.deviceui.BaseDeviceUI;
 import com.hh.lib_device.deviceui.IDeviceUI;
 import com.hh.lib_device.utils.DownloadFileUtils;
-import com.hh.lib_device.utils.FileSaver;
 import com.hh.lib_device.view.TouchBackView;
 
 
@@ -69,14 +70,17 @@ public class DevicesInvoker {
             case Constants.Ampe.ACTION_FLOATINGEXITBUTTON:
                 showFloatingBackButton(context,dataBean);
                 break;
-            case Constants.Ampe.ACTION_SETSTORAGE:
-                saveStorage(dataBean);
-                break;
-            case Constants.Ampe.ACTION_GETSTORAGELIST:
-                getStorageList(dataBean);
+            case Constants.Ampe.ACTION_STORAGE:
+                if (AmpeDownloadFileEvent.SET.equals(dataBean.getEvent())) {
+                    saveStorage(dataBean);
+                } else if (AmpeDownloadFileEvent.GET.equals(dataBean.getEvent())) {
+                    getStorageList(dataBean);
+                } else if (AmpeDownloadFileEvent.REMOVE.equals(dataBean.getEvent())) {
+                    removeStorage(dataBean);
+                }
                 break;
-            case Constants.Ampe.ACTION_REMOVESTORAGE:
-                removeStorage(dataBean);
+            default:
+                
                 break;
         }
     }
@@ -317,48 +321,116 @@ public class DevicesInvoker {
         if (requestBean == null) {
             return;
         }
-        BaseAmpeRequestBean<AmpeDeviceSaveFileConfigBean> newRequestBean = MyTypeToken
-                .toBaseAmpeRequestBean(requestBean, AmpeDeviceSaveFileConfigBean.class);
+        BaseAmpeRequestBean<AmpeRequestDownloadFileBean> newRequestBean = MyTypeToken
+                .toBaseAmpeRequestBean(requestBean, AmpeRequestDownloadFileBean.class);
         if (newRequestBean == null || newRequestBean.getParams() == null) {
             return;
         }
-        AmpeDeviceSaveFileConfigBean params= newRequestBean.getParams();
-        DownloadFileUtils.getInstance().downLoadFile(params.getUrl(), params.getFileName()
+        AmpeRequestDownloadFileBean params= newRequestBean.getParams();
+        DownloadFileUtils.getInstance().downLoadFile(params.getFileName(), params.getFileName()
                 , new DownloadFileUtils.DownloadFileListener() {
                     @Override
-                    public void saveSuccess(String fileName) {
-                        Log.i("___________123","saveSuccess:"+fileName);
+                    public void saveSuccess( AmpeResultDownloadFileBean data) {
+                        saveStorageCallBack(newRequestBean,data,true);
                     }
 
                     @Override
                     public void saveFailed(int code, String errMsg) {
-                        Log.i("___________123","saveFailed:"+code+"   "+errMsg);
+                        saveStorageCallBack(newRequestBean,null,false);
                     }
                 });
 
     }
+    /**
+     *
+     * @author xwh
+     * Time 2024/12/6
+     * Description: 保存文件返回事件
+     *
+    **/
+    private void saveStorageCallBack(BaseAmpeRequestBean requestBean, AmpeResultDownloadFileBean data, boolean isSuccess){
+        if(requestBean==null){
+            return;
+        }
+        BaseAmpeAppletResultBean result = new BaseAmpeAppletResultBean();
+        result.setTaskId(requestBean.getTaskId());
+        result.setAction(requestBean.getAction());
+        result.setEvent(requestBean.getEvent());
+        result.setSuccess(isSuccess);
+        result.setMessage(isSuccess?"成功":"失败");
+        if(isSuccess){
+            result.setData(data);
+        }
+        JsonObject jsonObject = GsonUtils.getGson().toJsonTree(result).getAsJsonObject();
+        if (requestBean.getBridgeCallback() != null) {
+            requestBean.getBridgeCallback().callback(jsonObject.toString());
+        }
+        String logLine = (new Throwable().getStackTrace())[0].toString();
+        UploadLogUtils.sendLogs(Constants.LogcatValue.LOG_DOWNLOADFILE,
+                Constants.Ampe.ACTION_STORAGE+"   "+requestBean.getEvent(),logLine
+                ,jsonObject.toString());
+    }
 
+    /**
+     *
+     * @author xwh
+     * Time 2024/12/6
+     * Description: 获取缓存文件列表
+     *
+    **/
     private void getStorageList(BaseAmpeRequestBean requestBean){
         if (requestBean == null) {
             return;
         }
-        Log.i("___________123","getStorageList:"+GsonUtils.getGson()
-                .toJson( DownloadFileUtils.getInstance().getFileList()));
+        BaseAmpeAppletResultBean result = new BaseAmpeAppletResultBean();
+        result.setTaskId(requestBean.getTaskId());
+        result.setAction(requestBean.getAction());
+        result.setEvent(requestBean.getEvent());
+        result.setSuccess(true);
+        result.setMessage("成功");
+        result.setData(DownloadFileUtils.getInstance().getFileList());
+        JsonObject jsonObject = GsonUtils.getGson().toJsonTree(result).getAsJsonObject();
+        if (requestBean.getBridgeCallback() != null) {
+            requestBean.getBridgeCallback().callback(jsonObject.toString());
+        }
+        String logLine = (new Throwable().getStackTrace())[0].toString();
+        UploadLogUtils.sendLogs(Constants.LogcatValue.LOG_DOWNLOADFILE,
+                Constants.Ampe.ACTION_STORAGE+"   "+requestBean.getEvent(),logLine
+                ,jsonObject.toString());
     }
+    /**
+     *
+     * @author xwh
+     * Time 2024/12/6
+     * Description: 删除文件
+     *
+    **/
     private void removeStorage(BaseAmpeRequestBean requestBean){
         if (requestBean == null) {
             return;
         }
-        BaseAmpeRequestBean<AmpeDeviceSaveFileConfigBean> newRequestBean = MyTypeToken
-                .toBaseAmpeRequestBean(requestBean, AmpeDeviceSaveFileConfigBean.class);
+        BaseAmpeRequestBean<AmpeRequestDownloadFileBean> newRequestBean = MyTypeToken
+                .toBaseAmpeRequestBean(requestBean, AmpeRequestDownloadFileBean.class);
         if (newRequestBean == null || newRequestBean.getParams() == null) {
             return;
         }
-        AmpeDeviceSaveFileConfigBean params= newRequestBean.getParams();
-       boolean isDeleted= DownloadFileUtils.getInstance().deleteFile(params.getRemovePath());
-        Log.i("___________123","removeStorage:"+isDeleted);
-        Log.i("___________123","removeStorage:"+GsonUtils.getGson()
-                .toJson( DownloadFileUtils.getInstance().getFileList()));
+        AmpeRequestDownloadFileBean params= newRequestBean.getParams();
+       boolean isDeleted= DownloadFileUtils.getInstance().deleteFile(params.getPath());
+       
+        BaseAmpeAppletResultBean result = new BaseAmpeAppletResultBean();
+        result.setTaskId(requestBean.getTaskId());
+        result.setAction(requestBean.getAction());
+        result.setEvent(requestBean.getEvent());
+        result.setSuccess(isDeleted);
+        result.setMessage(isDeleted?"成功":"失败");
+        JsonObject jsonObject = GsonUtils.getGson().toJsonTree(result).getAsJsonObject();
+        if (requestBean.getBridgeCallback() != null) {
+            requestBean.getBridgeCallback().callback(jsonObject.toString());
+        }
+        String logLine = (new Throwable().getStackTrace())[0].toString();
+        UploadLogUtils.sendLogs(Constants.LogcatValue.LOG_DOWNLOADFILE,
+                Constants.Ampe.ACTION_STORAGE+"   "+requestBean.getEvent(),logLine
+                ,jsonObject.toString());
     }
 
     public static DevicesInvoker getInstance() {

+ 0 - 37
lib_device/src/main/java/com/hh/lib_device/bean/action/AmpeDeviceSaveFileConfigBean.java

@@ -1,37 +0,0 @@
-package com.hh.lib_device.bean.action;
-
-/**
- * Description: $
- * @author xwh
- * time 2024/11/14
- **/
-public class AmpeDeviceSaveFileConfigBean {
-
-    private String fileName=""; //文件名
-    private String url=""; //链接
-    private String removePath="";  //删除文件绝对地址
-
-    public String getFileName() {
-        return fileName;
-    }
-
-    public void setFileName(String fileName) {
-        this.fileName = fileName;
-    }
-
-    public String getUrl() {
-        return url;
-    }
-
-    public void setUrl(String url) {
-        this.url = url;
-    }
-
-    public String getRemovePath() {
-        return removePath;
-    }
-
-    public void setRemovePath(String removePath) {
-        this.removePath = removePath;
-    }
-}

+ 37 - 0
lib_device/src/main/java/com/hh/lib_device/bean/action/AmpeRequestDownloadFileBean.java

@@ -0,0 +1,37 @@
+package com.hh.lib_device.bean.action;
+
+/**
+ * Description: $
+ * @author xwh
+ * time 2024/11/14
+ **/
+public class AmpeRequestDownloadFileBean {
+
+    private String fileName=""; //文件名
+    private String fileUrl=""; //链接
+    private String path="";  //删除文件绝对地址
+
+    public String getFileName() {
+        return fileName;
+    }
+
+    public void setFileName(String fileName) {
+        this.fileName = fileName;
+    }
+
+    public String getFileUrl() {
+        return fileUrl;
+    }
+
+    public void setFileUrl(String fileUrl) {
+        this.fileUrl = fileUrl;
+    }
+
+    public String getPath() {
+        return path;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+}

+ 39 - 0
lib_device/src/main/java/com/hh/lib_device/bean/action/AmpeResultDownloadFileBean.java

@@ -0,0 +1,39 @@
+package com.hh.lib_device.bean.action;
+
+/**
+ * Description: $
+ *
+ * @author xwh
+ * time 2024/12/6
+ **/
+public class AmpeResultDownloadFileBean {
+
+    private String fileName=""; //文件名
+    private String fileType=""; //类型
+    private String path="";
+
+
+    public String getFileName() {
+        return fileName;
+    }
+
+    public void setFileName(String fileName) {
+        this.fileName = fileName;
+    }
+
+    public String getFileType() {
+        return fileType;
+    }
+
+    public void setFileType(String fileType) {
+        this.fileType = fileType;
+    }
+
+    public String getPath() {
+        return path;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+}

+ 13 - 0
lib_device/src/main/java/com/hh/lib_device/bean/download/AmpeDownloadFileEvent.java

@@ -0,0 +1,13 @@
+package com.hh.lib_device.bean.download;
+
+/**
+ * Description: $
+ *
+ * @author xwh
+ * time 2024/12/6
+ **/
+public class AmpeDownloadFileEvent {
+    public static final String SET = "set";
+    public static final String REMOVE = "remove";
+    public static final String GET = "get";
+}

+ 10 - 0
lib_device/src/main/java/com/hh/lib_device/bean/download/FileBean.java

@@ -0,0 +1,10 @@
+package com.hh.lib_device.bean.download;
+
+/**
+ * Description: $
+ *
+ * @author xwh
+ * time 2024/12/6
+ **/
+public class FileBean {
+}

+ 28 - 9
lib_device/src/main/java/com/hh/lib_device/utils/DownloadFileUtils.java

@@ -4,16 +4,14 @@ import android.os.Environment;
 import android.text.TextUtils;
 
 import com.blankj.utilcode.util.FileUtils;
-import com.hh.lib_base.log.AppLogUtils;
 import com.hh.lib_device.bean.DownloadFileStatusEnum;
+import com.hh.lib_device.bean.action.AmpeResultDownloadFileBean;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import okhttp3.Call;
@@ -125,7 +123,11 @@ public class DownloadFileUtils {
                     }
                     // 文件下载完成
                     if(downloadFileListener!=null){
-                        downloadFileListener.saveSuccess(file.getAbsolutePath());
+                        AmpeResultDownloadFileBean data=new AmpeResultDownloadFileBean();
+                        data.setFileName(file.getName());
+                        data.setFileType(getFileExtension(file));
+                        data.setPath(file.getAbsolutePath());
+                        downloadFileListener.saveSuccess(data);
                     }
                     if (outputStream != null) {
                         outputStream.close();
@@ -182,8 +184,8 @@ public class DownloadFileUtils {
      * Description: 返回文件列表
      *
     **/
-    public ArrayList<String> getFileList(){
-        ArrayList<String> pathList = new ArrayList<>();
+    public ArrayList<AmpeResultDownloadFileBean> getFileList(){
+        ArrayList<AmpeResultDownloadFileBean> pathList = new ArrayList<>();
         if(TextUtils.isEmpty(rootFilePath)){
             return pathList;
         }
@@ -194,8 +196,14 @@ public class DownloadFileUtils {
         File[] files = file.listFiles();
         if (files != null) {
             for (File logFile : files) {
-                String absolutePath = logFile.getAbsolutePath();
-                pathList.add(absolutePath);
+                if (logFile == null || !logFile.exists()) {
+                    continue;
+                }
+                AmpeResultDownloadFileBean data=new AmpeResultDownloadFileBean();
+                data.setFileName(logFile.getName());
+                data.setFileType(getFileExtension(logFile));
+                data.setPath(logFile.getAbsolutePath());
+                pathList.add(data);
             }
         }
         return pathList;
@@ -223,6 +231,17 @@ public class DownloadFileUtils {
     }
 
 
+    public static String getFileExtension(File file) {
+        String name = file.getName();
+        int lastIndexOf = name.lastIndexOf('.');
+        if (lastIndexOf > 0 && lastIndexOf < name.length() - 1) {
+            return name.substring(lastIndexOf + 1);
+        } else {
+            return "";
+        }
+    }
+
+
     private OkHttpClient getOkHttpClient() {
         if(okHttpClient==null){
             synchronized (DownloadFileUtils.class){
@@ -261,7 +280,7 @@ public class DownloadFileUtils {
 
     public interface DownloadFileListener{
 
-        void saveSuccess(String fileName);
+        void saveSuccess( AmpeResultDownloadFileBean data);
         void saveFailed(int code,String errMsg);
 
     }