2025-09-15 23:24:20 +09:30
|
|
|
package com.cappielloantonio.tempo.util;
|
|
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
|
|
import androidx.documentfile.provider.DocumentFile;
|
|
|
|
|
|
|
|
|
|
import com.cappielloantonio.tempo.App;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.Child;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.PodcastEpisode;
|
|
|
|
|
|
|
|
|
|
import java.text.Normalizer;
|
|
|
|
|
import java.util.HashMap;
|
2025-09-16 23:22:18 +09:30
|
|
|
import java.util.HashSet;
|
2025-09-15 23:24:20 +09:30
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.Map;
|
2025-09-16 23:22:18 +09:30
|
|
|
import java.util.Set;
|
2025-09-15 23:24:20 +09:30
|
|
|
|
|
|
|
|
public class ExternalAudioReader {
|
|
|
|
|
|
|
|
|
|
private static final Map<String, DocumentFile> cache = new HashMap<>();
|
|
|
|
|
private static String cachedDirUri;
|
|
|
|
|
|
|
|
|
|
private static String sanitizeFileName(String name) {
|
|
|
|
|
String sanitized = name.replaceAll("[\\/:*?\\\"<>|]", "_");
|
|
|
|
|
sanitized = sanitized.replaceAll("\\s+", " ").trim();
|
|
|
|
|
return sanitized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String normalizeForComparison(String name) {
|
|
|
|
|
String s = sanitizeFileName(name);
|
|
|
|
|
s = Normalizer.normalize(s, Normalizer.Form.NFKD);
|
|
|
|
|
s = s.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
|
|
|
|
|
return s.toLowerCase(Locale.ROOT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static synchronized void ensureCache() {
|
|
|
|
|
String uriString = Preferences.getDownloadDirectoryUri();
|
|
|
|
|
if (uriString == null) {
|
|
|
|
|
cache.clear();
|
|
|
|
|
cachedDirUri = null;
|
2025-09-16 23:22:18 +09:30
|
|
|
ExternalDownloadMetadataStore.clear();
|
2025-09-15 23:24:20 +09:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (uriString.equals(cachedDirUri)) return;
|
|
|
|
|
|
|
|
|
|
cache.clear();
|
|
|
|
|
DocumentFile directory = DocumentFile.fromTreeUri(App.getContext(), Uri.parse(uriString));
|
2025-09-16 23:22:18 +09:30
|
|
|
Map<String, Long> expectedSizes = ExternalDownloadMetadataStore.snapshot();
|
|
|
|
|
Set<String> verifiedKeys = new HashSet<>();
|
2025-09-15 23:24:20 +09:30
|
|
|
if (directory != null && directory.canRead()) {
|
|
|
|
|
for (DocumentFile file : directory.listFiles()) {
|
2025-09-16 23:22:18 +09:30
|
|
|
if (file == null || file.isDirectory()) continue;
|
2025-09-15 23:24:20 +09:30
|
|
|
String existing = file.getName();
|
2025-09-16 23:22:18 +09:30
|
|
|
if (existing == null) continue;
|
|
|
|
|
|
|
|
|
|
String base = existing.replaceFirst("\\.[^\\.]+$", "");
|
|
|
|
|
String key = normalizeForComparison(base);
|
|
|
|
|
Long expected = expectedSizes.get(key);
|
|
|
|
|
long actualLength = file.length();
|
|
|
|
|
|
|
|
|
|
if (expected != null && expected > 0 && actualLength == expected) {
|
|
|
|
|
cache.put(key, file);
|
|
|
|
|
verifiedKeys.add(key);
|
|
|
|
|
} else {
|
|
|
|
|
ExternalDownloadMetadataStore.remove(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!expectedSizes.isEmpty()) {
|
|
|
|
|
if (verifiedKeys.isEmpty()) {
|
|
|
|
|
ExternalDownloadMetadataStore.clear();
|
|
|
|
|
} else {
|
|
|
|
|
for (String key : expectedSizes.keySet()) {
|
|
|
|
|
if (!verifiedKeys.contains(key)) {
|
|
|
|
|
ExternalDownloadMetadataStore.remove(key);
|
|
|
|
|
}
|
2025-09-15 23:24:20 +09:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cachedDirUri = uriString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized void refreshCache() {
|
|
|
|
|
cachedDirUri = null;
|
|
|
|
|
cache.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String buildKey(String artist, String title, String album) {
|
|
|
|
|
String name = artist != null && !artist.isEmpty() ? artist + " - " + title : title;
|
|
|
|
|
if (album != null && !album.isEmpty()) name += " (" + album + ")";
|
|
|
|
|
return normalizeForComparison(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Uri findUri(String artist, String title, String album) {
|
|
|
|
|
ensureCache();
|
|
|
|
|
if (cachedDirUri == null) return null;
|
|
|
|
|
|
|
|
|
|
DocumentFile file = cache.get(buildKey(artist, title, album));
|
|
|
|
|
return file != null && file.exists() ? file.getUri() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Uri getUri(Child media) {
|
|
|
|
|
return findUri(media.getArtist(), media.getTitle(), media.getAlbum());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Uri getUri(PodcastEpisode episode) {
|
|
|
|
|
return findUri(episode.getArtist(), episode.getTitle(), episode.getAlbum());
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 22:57:17 +09:30
|
|
|
public static synchronized void removeMetadata(Child media) {
|
|
|
|
|
if (media == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String key = buildKey(media.getArtist(), media.getTitle(), media.getAlbum());
|
|
|
|
|
cache.remove(key);
|
|
|
|
|
ExternalDownloadMetadataStore.remove(key);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 23:24:20 +09:30
|
|
|
public static boolean delete(Child media) {
|
|
|
|
|
ensureCache();
|
|
|
|
|
if (cachedDirUri == null) return false;
|
|
|
|
|
|
|
|
|
|
String key = buildKey(media.getArtist(), media.getTitle(), media.getAlbum());
|
|
|
|
|
DocumentFile file = cache.get(key);
|
|
|
|
|
boolean deleted = false;
|
|
|
|
|
if (file != null && file.exists()) {
|
|
|
|
|
deleted = file.delete();
|
|
|
|
|
}
|
|
|
|
|
if (deleted) {
|
|
|
|
|
cache.remove(key);
|
2025-09-16 23:22:18 +09:30
|
|
|
ExternalDownloadMetadataStore.remove(key);
|
2025-09-15 23:24:20 +09:30
|
|
|
}
|
|
|
|
|
return deleted;
|
|
|
|
|
}
|
|
|
|
|
}
|