mirror of
https://github.com/antebudimir/tempus.git
synced 2026-01-01 18:03:33 +00:00
fix: Prevent externalAudioReader from hogging the main thread
This commit is contained in:
parent
1357c5c062
commit
620fba0a14
12 changed files with 242 additions and 90 deletions
|
|
@ -1,8 +1,12 @@
|
|||
package com.cappielloantonio.tempo.util;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import com.cappielloantonio.tempo.App;
|
||||
import com.cappielloantonio.tempo.subsonic.models.Child;
|
||||
|
|
@ -14,11 +18,20 @@ import java.util.HashSet;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class ExternalAudioReader {
|
||||
|
||||
private static final Map<String, DocumentFile> cache = new HashMap<>();
|
||||
private static String cachedDirUri;
|
||||
private static final Map<String, DocumentFile> cache = new ConcurrentHashMap<>();
|
||||
private static final Object LOCK = new Object();
|
||||
private static final ExecutorService REFRESH_EXECUTOR = Executors.newSingleThreadExecutor();
|
||||
private static final MutableLiveData<Long> refreshEvents = new MutableLiveData<>();
|
||||
|
||||
private static volatile String cachedDirUri;
|
||||
private static volatile boolean refreshInProgress = false;
|
||||
private static volatile boolean refreshQueued = false;
|
||||
|
||||
private static String sanitizeFileName(String name) {
|
||||
String sanitized = name.replaceAll("[\\/:*?\\\"<>|]", "_");
|
||||
|
|
@ -33,59 +46,59 @@ public class ExternalAudioReader {
|
|||
return s.toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private static synchronized void ensureCache() {
|
||||
private static void ensureCache() {
|
||||
String uriString = Preferences.getDownloadDirectoryUri();
|
||||
if (uriString == null) {
|
||||
cache.clear();
|
||||
cachedDirUri = null;
|
||||
synchronized (LOCK) {
|
||||
cache.clear();
|
||||
cachedDirUri = null;
|
||||
}
|
||||
ExternalDownloadMetadataStore.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if (uriString.equals(cachedDirUri)) return;
|
||||
|
||||
cache.clear();
|
||||
DocumentFile directory = DocumentFile.fromTreeUri(App.getContext(), Uri.parse(uriString));
|
||||
Map<String, Long> expectedSizes = ExternalDownloadMetadataStore.snapshot();
|
||||
Set<String> verifiedKeys = new HashSet<>();
|
||||
if (directory != null && directory.canRead()) {
|
||||
for (DocumentFile file : directory.listFiles()) {
|
||||
if (file == null || file.isDirectory()) continue;
|
||||
String existing = file.getName();
|
||||
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 (uriString.equals(cachedDirUri)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!expectedSizes.isEmpty()) {
|
||||
if (verifiedKeys.isEmpty()) {
|
||||
ExternalDownloadMetadataStore.clear();
|
||||
} else {
|
||||
for (String key : expectedSizes.keySet()) {
|
||||
if (!verifiedKeys.contains(key)) {
|
||||
ExternalDownloadMetadataStore.remove(key);
|
||||
}
|
||||
}
|
||||
boolean runSynchronously = false;
|
||||
synchronized (LOCK) {
|
||||
if (refreshInProgress) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
scheduleRefreshLocked();
|
||||
return;
|
||||
}
|
||||
|
||||
refreshInProgress = true;
|
||||
runSynchronously = true;
|
||||
}
|
||||
|
||||
cachedDirUri = uriString;
|
||||
if (runSynchronously) {
|
||||
try {
|
||||
rebuildCache();
|
||||
} finally {
|
||||
onRefreshFinished();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void refreshCache() {
|
||||
cachedDirUri = null;
|
||||
cache.clear();
|
||||
public static void refreshCache() {
|
||||
refreshCacheAsync();
|
||||
}
|
||||
|
||||
public static void refreshCacheAsync() {
|
||||
synchronized (LOCK) {
|
||||
cachedDirUri = null;
|
||||
cache.clear();
|
||||
}
|
||||
requestRefresh();
|
||||
}
|
||||
|
||||
public static LiveData<Long> getRefreshEvents() {
|
||||
return refreshEvents;
|
||||
}
|
||||
|
||||
private static String buildKey(String artist, String title, String album) {
|
||||
|
|
@ -136,4 +149,96 @@ public class ExternalAudioReader {
|
|||
}
|
||||
return deleted;
|
||||
}
|
||||
|
||||
private static void requestRefresh() {
|
||||
synchronized (LOCK) {
|
||||
scheduleRefreshLocked();
|
||||
}
|
||||
}
|
||||
|
||||
private static void scheduleRefreshLocked() {
|
||||
if (refreshInProgress) {
|
||||
refreshQueued = true;
|
||||
return;
|
||||
}
|
||||
|
||||
refreshInProgress = true;
|
||||
REFRESH_EXECUTOR.execute(() -> {
|
||||
try {
|
||||
rebuildCache();
|
||||
} finally {
|
||||
onRefreshFinished();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void rebuildCache() {
|
||||
String uriString = Preferences.getDownloadDirectoryUri();
|
||||
if (uriString == null) {
|
||||
synchronized (LOCK) {
|
||||
cache.clear();
|
||||
cachedDirUri = null;
|
||||
}
|
||||
ExternalDownloadMetadataStore.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
DocumentFile directory = DocumentFile.fromTreeUri(App.getContext(), Uri.parse(uriString));
|
||||
Map<String, Long> expectedSizes = ExternalDownloadMetadataStore.snapshot();
|
||||
Set<String> verifiedKeys = new HashSet<>();
|
||||
Map<String, DocumentFile> newEntries = new HashMap<>();
|
||||
|
||||
if (directory != null && directory.canRead()) {
|
||||
for (DocumentFile file : directory.listFiles()) {
|
||||
if (file == null || file.isDirectory()) continue;
|
||||
String existing = file.getName();
|
||||
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) {
|
||||
newEntries.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
synchronized (LOCK) {
|
||||
cache.clear();
|
||||
cache.putAll(newEntries);
|
||||
cachedDirUri = uriString;
|
||||
}
|
||||
}
|
||||
|
||||
private static void onRefreshFinished() {
|
||||
boolean runAgain;
|
||||
synchronized (LOCK) {
|
||||
refreshInProgress = false;
|
||||
runAgain = refreshQueued;
|
||||
refreshQueued = false;
|
||||
}
|
||||
|
||||
refreshEvents.postValue(SystemClock.elapsedRealtime());
|
||||
|
||||
if (runAgain) {
|
||||
requestRefresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue