|
19 | 19 | public class RecentDocumentsUtil { |
20 | 20 |
|
21 | 21 | private static final String FILENAME = "recent_documents.json"; |
| 22 | + private static final String CLEANUP_PREF_KEY = "recent_docs_cleanup_v1_done"; |
22 | 23 |
|
23 | 24 | public static Map<String, String> getRecentDocuments(Context context) |
24 | 25 | throws IOException, JSONException { |
| 26 | + // Perform one-time cleanup of inaccessible documents |
| 27 | + performOneTimeCleanupIfNeeded(context); |
| 28 | + |
25 | 29 | Map<String, String> result = new HashMap<String, String>(); |
26 | 30 |
|
27 | 31 | JSONArray jsonArray = getRecentDocumentsJson(context); |
28 | | - JSONArray filteredArray = new JSONArray(); |
29 | | - boolean hasInaccessibleDocuments = false; |
30 | 32 |
|
31 | 33 | for (int i = 0; i < jsonArray.length(); i++) { |
32 | 34 | JSONObject document = jsonArray.getJSONObject(i); |
33 | 35 | String filename = document.getString("filename"); |
34 | 36 | String uri = document.getString("uri"); |
35 | 37 |
|
36 | | - // Only include URIs that are still accessible |
37 | | - if (isUriAccessible(context, Uri.parse(uri))) { |
38 | | - result.put(filename, uri); |
39 | | - filteredArray.put(document); |
40 | | - } else { |
41 | | - hasInaccessibleDocuments = true; |
42 | | - } |
| 38 | + result.put(filename, uri); |
43 | 39 | } |
44 | 40 |
|
45 | | - // Remove inaccessible documents from storage to avoid checking them again |
46 | | - if (hasInaccessibleDocuments) { |
47 | | - saveJson(context, filteredArray); |
| 41 | + return result; |
| 42 | + } |
| 43 | + |
| 44 | + private static void performOneTimeCleanupIfNeeded(Context context) { |
| 45 | + // Check if cleanup has already been performed |
| 46 | + android.content.SharedPreferences prefs = context.getSharedPreferences("recent_docs", Context.MODE_PRIVATE); |
| 47 | + if (prefs.getBoolean(CLEANUP_PREF_KEY, false)) { |
| 48 | + return; // Cleanup already done |
48 | 49 | } |
49 | 50 |
|
50 | | - return result; |
| 51 | + try { |
| 52 | + JSONArray jsonArray = getRecentDocumentsJson(context); |
| 53 | + JSONArray filteredArray = new JSONArray(); |
| 54 | + boolean hasInaccessibleDocuments = false; |
| 55 | + |
| 56 | + for (int i = 0; i < jsonArray.length(); i++) { |
| 57 | + JSONObject document = jsonArray.getJSONObject(i); |
| 58 | + String uri = document.getString("uri"); |
| 59 | + |
| 60 | + // Remove documents that are no longer accessible |
| 61 | + if (isUriAccessible(context, Uri.parse(uri))) { |
| 62 | + filteredArray.put(document); |
| 63 | + } else { |
| 64 | + hasInaccessibleDocuments = true; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (hasInaccessibleDocuments) { |
| 69 | + saveJson(context, filteredArray); |
| 70 | + } |
| 71 | + |
| 72 | + // Mark cleanup as completed |
| 73 | + prefs.edit().putBoolean(CLEANUP_PREF_KEY, true).apply(); |
| 74 | + } catch (Exception e) { |
| 75 | + // If cleanup fails, mark it as done anyway to avoid repeated attempts |
| 76 | + prefs.edit().putBoolean(CLEANUP_PREF_KEY, true).apply(); |
| 77 | + } |
51 | 78 | } |
52 | 79 |
|
53 | 80 | private static JSONArray getRecentDocumentsJson(Context context) |
|
0 commit comments