From 284f3dbd343b8f40e921b4b887384ebc14ba3dcb Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期二, 23 十二月 2025 13:57:39 +0800
Subject: [PATCH] 1.h264文件写入的时候检查tfcard剩余空间  ,并执行删除

---
 app/src/main/java/com/anyun/h264/util/FileUtil.java |  202 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 202 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/com/anyun/h264/util/FileUtil.java b/app/src/main/java/com/anyun/h264/util/FileUtil.java
index 9f84cd0..b41f232 100644
--- a/app/src/main/java/com/anyun/h264/util/FileUtil.java
+++ b/app/src/main/java/com/anyun/h264/util/FileUtil.java
@@ -2,10 +2,22 @@
 
 import android.content.Context;
 import android.os.storage.StorageManager;
+import android.os.StatFs;
 
+import java.io.File;
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+
+import timber.log.Timber;
 
 public class FileUtil {
     //鑾峰彇鎻掑叆鐨凾FCard鐩綍璺緞
@@ -44,4 +56,194 @@
         }
 
     }
+
+    /**
+     * 娓呯悊 TF 鍗′笂鐨� h264 鏂囦欢
+     * 褰� h264 鏂囦欢鎬诲ぇ灏忚秴杩囨寚瀹氶槇鍊兼垨 TF 鍗″墿浣欑┖闂村皬浜庢寚瀹氬�兼椂锛屽垹闄ゆ棩鏈熸渶鏃╃殑鏂囦欢澶�
+     * 
+     * @param context Context 瀵硅薄锛岀敤浜庤幏鍙� TF 鍗¤矾寰勫拰鍓╀綑绌洪棿
+     * @param h264RootDir h264 鏍圭洰褰曡矾寰勶紝渚嬪 "/sdcard/h264"
+     * @param maxTotalSizeGB 鏈�澶ф�诲ぇ灏忥紙GB锛夛紝榛樿 5GB
+     * @param minFreeSpaceGB 鏈�灏忓墿浣欑┖闂达紙GB锛夛紝榛樿 1GB
+     */
+    public static void cleanupH264Files(Context context, String h264RootDir, long maxTotalSizeGB, long minFreeSpaceGB) {
+        if (context == null || h264RootDir == null || h264RootDir.trim().isEmpty()) {
+            Timber.w("Context or h264 root directory is null, skip cleanup");
+            return;
+        }
+
+        File h264Root = new File(h264RootDir);
+        if (!h264Root.exists() || !h264Root.isDirectory()) {
+            Timber.d("H264 root directory does not exist: %s, skip cleanup", h264RootDir);
+            return;
+        }
+
+        try {
+            // 鑾峰彇 TF 鍗¤矾寰�
+            String tfCardPath = getStoragePath(context, true);
+            if (tfCardPath == null || tfCardPath.trim().isEmpty()) {
+                Timber.w("TF card path not available, skip cleanup");
+                return;
+            }
+
+            // 鑾峰彇 TF 鍗″墿浣欑┖闂达紙GB锛�
+            long freeSpaceGB = getFreeSpaceGB(tfCardPath);
+            Timber.d("TF card free space: %d GB", freeSpaceGB);
+
+            // 鎵弿鎵�鏈夋棩鏈熸枃浠跺す
+            File[] dateDirs = h264Root.listFiles(File::isDirectory);
+            if (dateDirs == null || dateDirs.length == 0) {
+                Timber.d("No date directories found in h264 root: %s", h264RootDir);
+                return;
+            }
+
+            // 鎸夋棩鏈熸帓搴忥紙鏈�鏃╃殑鍦ㄥ墠锛�
+            List<DateDirInfo> dateDirList = new ArrayList<>();
+            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
+            
+            for (File dateDir : dateDirs) {
+                String dirName = dateDir.getName();
+                // 鍙鐞嗙鍚堟棩鏈熸牸寮忕殑鏂囦欢澶癸紙yyyyMMdd锛�
+                if (dirName.length() == 8 && dirName.matches("\\d{8}")) {
+                    try {
+                        Date date = dateFormat.parse(dirName);
+                        long totalSize = calculateH264FilesSize(dateDir);
+                        dateDirList.add(new DateDirInfo(dateDir, date, totalSize));
+                    } catch (ParseException e) {
+                        Timber.w("Invalid date directory name: %s", dirName);
+                    }
+                }
+            }
+
+            // 鎸夋棩鏈熸帓搴忥紙鏈�鏃╃殑鍦ㄥ墠锛�
+            Collections.sort(dateDirList, new Comparator<DateDirInfo>() {
+                @Override
+                public int compare(DateDirInfo o1, DateDirInfo o2) {
+                    return o1.date.compareTo(o2.date);
+                }
+            });
+
+            // 璁$畻鎵�鏈� h264 鏂囦欢鐨勬�诲ぇ灏忥紙GB锛�
+            long totalSizeGB = 0;
+            for (DateDirInfo info : dateDirList) {
+                totalSizeGB += info.totalSize / (1024L * 1024L * 1024L);
+            }
+            Timber.d("Total h264 files size: %d GB, Max allowed: %d GB", totalSizeGB, maxTotalSizeGB);
+            Timber.d("TF card free space: %d GB, Min required: %d GB", freeSpaceGB, minFreeSpaceGB);
+
+            // 妫�鏌ユ槸鍚﹂渶瑕佹竻鐞�
+            boolean needCleanup = (totalSizeGB > maxTotalSizeGB) || (freeSpaceGB < minFreeSpaceGB);
+
+            if (!needCleanup) {
+                Timber.d("No cleanup needed");
+                return;
+            }
+
+            // 鍒犻櫎鏈�鏃╃殑鏃ユ湡鏂囦欢澶癸紝鐩村埌婊¤冻鏉′欢
+            int deletedCount = 0;
+            while (!dateDirList.isEmpty() && ((totalSizeGB > maxTotalSizeGB) || (freeSpaceGB < minFreeSpaceGB))) {
+                DateDirInfo oldestDir = dateDirList.remove(0);
+                
+                Timber.d("Deleting oldest date directory: %s (size: %d GB, date: %s)", 
+                        oldestDir.dir.getName(), oldestDir.totalSize / (1024L * 1024L * 1024L), 
+                        dateFormat.format(oldestDir.date));
+
+                // 鍒犻櫎鏂囦欢澶瑰強鍏舵墍鏈夊唴瀹�
+                if (deleteDirectory(oldestDir.dir)) {
+                    deletedCount++;
+                    totalSizeGB -= oldestDir.totalSize / (1024L * 1024L * 1024L);
+                    
+                    // 閲嶆柊鑾峰彇鍓╀綑绌洪棿
+                    freeSpaceGB = getFreeSpaceGB(tfCardPath);
+                    Timber.d("After deletion - Total size: %d GB, Free space: %d GB", 
+                            totalSizeGB, freeSpaceGB);
+                } else {
+                    Timber.e("Failed to delete directory: %s", oldestDir.dir.getAbsolutePath());
+                    break; // 鍒犻櫎澶辫触锛屽仠姝㈡竻鐞�
+                }
+            }
+
+            Timber.i("Cleanup completed. Deleted %d date directories. Final total size: %d GB, Free space: %d GB",
+                    deletedCount, totalSizeGB, freeSpaceGB);
+
+        } catch (Exception e) {
+            Timber.e(e, "Error during h264 files cleanup");
+        }
+    }
+
+    /**
+     * 娓呯悊 TF 鍗′笂鐨� h264 鏂囦欢锛堜娇鐢ㄩ粯璁ゅ弬鏁帮細鏈�澶�5GB锛屾渶灏忓墿浣欑┖闂�1GB锛�
+     */
+    public static void cleanupH264Files(Context context, String h264RootDir) {
+        cleanupH264Files(context, h264RootDir, 5, 1);
+    }
+
+    /**
+     * 璁$畻鐩綍涓嬫墍鏈� .h264 鏂囦欢鐨勬�诲ぇ灏忥紙瀛楄妭锛�
+     */
+    private static long calculateH264FilesSize(File dir) {
+        long totalSize = 0;
+        File[] files = dir.listFiles();
+        if (files != null) {
+            for (File file : files) {
+                if (file.isFile() && file.getName().toLowerCase().endsWith(".h264")) {
+                    totalSize += file.length();
+                }
+            }
+        }
+        return totalSize;
+    }
+
+    /**
+     * 鑾峰彇鎸囧畾璺緞鐨勫墿浣欑┖闂达紙GB锛�
+     */
+    private static long getFreeSpaceGB(String path) {
+        try {
+            StatFs statFs = new StatFs(path);
+            long blockSize = statFs.getBlockSizeLong();
+            long availableBlocks = statFs.getAvailableBlocksLong();
+            long freeBytes = availableBlocks * blockSize;
+            return freeBytes / (1024L * 1024L * 1024L); // 杞崲涓� GB
+        } catch (Exception e) {
+            Timber.e(e, "Error getting free space for path: %s", path);
+            return 0;
+        }
+    }
+
+    /**
+     * 閫掑綊鍒犻櫎鐩綍鍙婂叾鎵�鏈夊唴瀹�
+     */
+    private static boolean deleteDirectory(File dir) {
+        if (dir == null || !dir.exists()) {
+            return true;
+        }
+
+        if (dir.isDirectory()) {
+            File[] children = dir.listFiles();
+            if (children != null) {
+                for (File child : children) {
+                    if (!deleteDirectory(child)) {
+                        return false;
+                    }
+                }
+            }
+        }
+
+        return dir.delete();
+    }
+
+    /**
+     * 鏃ユ湡鏂囦欢澶逛俊鎭�
+     */
+    private static class DateDirInfo {
+        File dir;
+        Date date;
+        long totalSize; // 瀛楄妭
+
+        DateDirInfo(File dir, Date date, long totalSize) {
+            this.dir = dir;
+            this.date = date;
+            this.totalSize = totalSize;
+        }
+    }
 }

--
Gitblit v1.8.0