Skip to content

Commit 88ad743

Browse files
CopilotTomTasche
andcommitted
Implement proper solution: one-time cleanup instead of repeated URI accessibility checks
Co-authored-by: TomTasche <128734+TomTasche@users.noreply.github.com>
1 parent d808a1a commit 88ad743

1 file changed

Lines changed: 40 additions & 13 deletions

File tree

app/src/main/java/at/tomtasche/reader/background/RecentDocumentsUtil.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,62 @@
1919
public class RecentDocumentsUtil {
2020

2121
private static final String FILENAME = "recent_documents.json";
22+
private static final String CLEANUP_PREF_KEY = "recent_docs_cleanup_v1_done";
2223

2324
public static Map<String, String> getRecentDocuments(Context context)
2425
throws IOException, JSONException {
26+
// Perform one-time cleanup of inaccessible documents
27+
performOneTimeCleanupIfNeeded(context);
28+
2529
Map<String, String> result = new HashMap<String, String>();
2630

2731
JSONArray jsonArray = getRecentDocumentsJson(context);
28-
JSONArray filteredArray = new JSONArray();
29-
boolean hasInaccessibleDocuments = false;
3032

3133
for (int i = 0; i < jsonArray.length(); i++) {
3234
JSONObject document = jsonArray.getJSONObject(i);
3335
String filename = document.getString("filename");
3436
String uri = document.getString("uri");
3537

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);
4339
}
4440

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
4849
}
4950

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+
}
5178
}
5279

5380
private static JSONArray getRecentDocumentsJson(Context context)

0 commit comments

Comments
 (0)