From b63156cda9fabdde9e13594a07e7f1f1a56763bb Mon Sep 17 00:00:00 2001 From: hugh <1012760428@qq.com> Date: Tue, 20 Jan 2026 23:58:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20JsonHelper=20JSON?= =?UTF-8?q?=20=E7=B4=A2=E5=BC=95=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现了图片索引的 JSON 文件存储功能 - 保存文件夹扫描结果到 JSON 文件 - 从 JSON 文件加载图片索引 - 智能检测是否需要重新扫描 - 支持文件夹路径哈希命名 - 使用 Gson 进行 JSON 序列化 Co-Authored-By: Claude Sonnet 4.5 --- .../java/cn/hugh/bz/gk2/utils/JsonHelper.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 app/src/main/java/cn/hugh/bz/gk2/utils/JsonHelper.java diff --git a/app/src/main/java/cn/hugh/bz/gk2/utils/JsonHelper.java b/app/src/main/java/cn/hugh/bz/gk2/utils/JsonHelper.java new file mode 100644 index 0000000..5d7ae0e --- /dev/null +++ b/app/src/main/java/cn/hugh/bz/gk2/utils/JsonHelper.java @@ -0,0 +1,107 @@ +package cn.hugh.bz.gk2.utils; + +import android.content.Context; +import android.util.Log; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import cn.hugh.bz.gk2.model.WallpaperImage; + +public class JsonHelper { + private static final String TAG = "JsonHelper"; + private static final String INDEX_DIR = "image_indices"; + + private final Context context; + private final Gson gson; + private final File indicesDir; + + public JsonHelper(Context context) { + this.context = context; + this.gson = new GsonBuilder().setPrettyPrinting().create(); + this.indicesDir = new File(context.getFilesDir(), INDEX_DIR); + if (!indicesDir.exists()) { + indicesDir.mkdirs(); + } + } + + public void saveIndex(String folderPath, List images) { + String fileName = getIndexFileName(folderPath); + File indexFile = new File(indicesDir, fileName); + + IndexData data = new IndexData(folderPath, new File(folderPath).getName(), + System.currentTimeMillis(), images.size(), images); + + try (FileWriter writer = new FileWriter(indexFile)) { + gson.toJson(data, writer); + Log.i(TAG, "Saved index: " + fileName + " with " + images.size() + " images"); + } catch (IOException e) { + Log.e(TAG, "Failed to save index", e); + } + } + + public List loadImageIndex(String folderPath) { + String fileName = getIndexFileName(folderPath); + File indexFile = new File(indicesDir, fileName); + + if (!indexFile.exists()) { + return null; + } + + try (FileReader reader = new FileReader(indexFile)) { + IndexData data = gson.fromJson(reader, IndexData.class); + return data.images; + } catch (IOException e) { + Log.e(TAG, "Failed to load index", e); + return null; + } + } + + private String getIndexFileName(String folderPath) { + int hash = folderPath.hashCode(); + return "folder_" + Math.abs(hash) + ".json"; + } + + public boolean needsRescan(String folderPath) { + List cached = loadImageIndex(folderPath); + if (cached == null) { + return true; + } + + File folder = new File(folderPath); + if (!folder.exists()) { + return false; + } + + String fileName = getIndexFileName(folderPath); + File indexFile = new File(indicesDir, fileName); + + return folder.lastModified() > indexFile.lastModified(); + } + + private static class IndexData { + String folderPath; + String folderName; + long lastScanned; + int imageCount; + List images; + + IndexData() {} + + IndexData(String folderPath, String folderName, long lastScanned, + int imageCount, List images) { + this.folderPath = folderPath; + this.folderName = folderName; + this.lastScanned = lastScanned; + this.imageCount = imageCount; + this.images = images; + } + } +}