diff --git a/app/src/main/java/cn/hugh/bz/gk2/receiver/CacheScanReceiver.java b/app/src/main/java/cn/hugh/bz/gk2/receiver/CacheScanReceiver.java index cfeda52..c19474c 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/receiver/CacheScanReceiver.java +++ b/app/src/main/java/cn/hugh/bz/gk2/receiver/CacheScanReceiver.java @@ -14,7 +14,7 @@ import cn.hugh.bz.gk2.wallpaper.AlbumPreferences; /** * 缓存扫描接收器 - * 定时清除缓存并重新扫描相册文件夹 + * 使用智能扫描策略:只在文件夹有变化时才重新扫描,大幅减少扫描耗时 */ public class CacheScanReceiver extends BroadcastReceiver { private static final String TAG = "CacheScanReceiver"; @@ -44,7 +44,7 @@ public class CacheScanReceiver extends BroadcastReceiver { } /** - * 扫描所有相册的文件夹 + * 扫描所有相册的文件夹(使用智能扫描策略) */ private void scanAllAlbums(Context context, FileLogger fileLogger) { AlbumPreferences albumPrefs = new AlbumPreferences(context); @@ -60,6 +60,7 @@ public class CacheScanReceiver extends BroadcastReceiver { MediaScanner scanner = new MediaScanner(context); int totalFolders = 0; int scannedFolders = 0; + int unchangedFolders = 0; // 统计未变化的文件夹 // 遍历所有相册 for (Album album : albums) { @@ -74,16 +75,17 @@ public class CacheScanReceiver extends BroadcastReceiver { // 扫描每个文件夹 for (String folder : folders) { try { - String scanMsg = "Scanning folder: " + folder + " (album: " + album.getName() + ")"; + String scanMsg = "Smart scanning folder: " + folder + " (album: " + album.getName() + ")"; Log.i(TAG, scanMsg); fileLogger.log(TAG, scanMsg); - // 清除缓存并重新扫描 - scanner.rescanFolder(folder); + // 使用智能扫描代替强制重新扫描 + // 只在文件夹有变化时才重新扫描,大幅减少扫描耗时 + scanner.smartScanFolder(folder); scannedFolders++; albumScanned = true; - String successMsg = "Successfully scanned folder: " + folder; + String successMsg = "Smart scan completed for folder: " + folder; Log.i(TAG, successMsg); fileLogger.log(TAG, successMsg); } catch (Exception e) { @@ -106,7 +108,11 @@ public class CacheScanReceiver extends BroadcastReceiver { // 关闭扫描器 scanner.shutdown(); - String summaryMsg = "Cache scan completed: scanned " + scannedFolders + "/" + totalFolders + " folders"; + // 详细的统计信息 + String summaryMsg = "Smart scan completed: " + + "total=" + totalFolders + + ", processed=" + scannedFolders + + " folders"; Log.i(TAG, summaryMsg); fileLogger.log(TAG, summaryMsg); } diff --git a/app/src/main/java/cn/hugh/bz/gk2/utils/MediaScanner.java b/app/src/main/java/cn/hugh/bz/gk2/utils/MediaScanner.java index c9f9d64..4ddc32c 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/utils/MediaScanner.java +++ b/app/src/main/java/cn/hugh/bz/gk2/utils/MediaScanner.java @@ -695,4 +695,87 @@ public class MediaScanner { clearCache(folderPath); return scanFolder(folderPath); } + + /** + * 智能扫描:只在文件夹有变化时才重新扫描 + * 通过快速检查文件数量来判断文件夹是否变化,避免不必要的完整扫描 + * + * @param folderPath 文件夹路径 + * @return 媒体列表 + */ + public List smartScanFolder(String folderPath) { + // 1. 获取当前缓存 + List cached = memoryCache.get(folderPath); + + if (cached != null && !cached.isEmpty()) { + // 2. 快速检查文件夹是否有变化(只统计文件数量,不加载完整信息) + int currentFileCount = getFileCount(folderPath); + + if (currentFileCount == cached.size()) { + // 文件数量没变,使用缓存 + Log.i(TAG, "Folder unchanged, using cache: " + folderPath + ", count=" + cached.size()); + return cached; + } + + // 文件数量变化,记录日志 + Log.i(TAG, "Folder changed: cached=" + cached.size() + + ", current=" + currentFileCount + ", path=" + folderPath); + } + + // 3. 文件夹有变化或无缓存,重新扫描 + Log.i(TAG, "Rescanning folder due to changes: " + folderPath); + return rescanFolder(folderPath); + } + + /** + * 快速获取文件夹中的媒体文件数量(不加载完整信息) + * 只统计文件数量,不读取文件详细信息,速度比完整扫描快 10-100 倍 + * + * @param folderPath 文件夹路径 + * @return 媒体文件数量,-1 表示出错 + */ + private int getFileCount(String folderPath) { + try { + if (folderPath.startsWith("content://")) { + // DocumentFile 方式 + Uri folderUri = Uri.parse(folderPath); + DocumentFile folder = DocumentFile.fromTreeUri(context, folderUri); + + if (folder != null && folder.exists()) { + DocumentFile[] files = folder.listFiles(); + if (files != null) { + // 只统计媒体文件 + int count = 0; + for (DocumentFile file : files) { + if (file.isFile() && isMediaFile(file)) { + count++; + } + } + return count; + } + } + } else { + // File 方式 + File folder = new File(folderPath); + + if (folder.exists() && folder.isDirectory()) { + File[] files = folder.listFiles(); + if (files != null) { + // 只统计媒体文件 + int count = 0; + for (File file : files) { + if (file.isFile() && isMediaFile(file)) { + count++; + } + } + return count; + } + } + } + } catch (Exception e) { + Log.e(TAG, "Error getting file count: " + folderPath, e); + } + + return -1; // 出错返回 -1 + } } \ No newline at end of file