From bd91bdfa20aeeb2c89f2d0f1d00fa8e384958400 Mon Sep 17 00:00:00 2001 From: Marcin Simonides Date: Sun, 4 Mar 2018 17:10:18 +0100 Subject: [PATCH] Scan for new books more often. Audiobooks are additionally rescanned when: - the application UI is shown - this should help if the ContentObserver is not called after files are changed. Hopefully this helps with issues like #17, - if no books are found during the initial scan rescan in 10 seconds which may help on devices where Homer Player is started before an SD card with audiobooks is attached (see #12). --- .../homerplayer/model/AudioBookManager.java | 18 +++++++++++++++++- .../homerplayer/ui/UiControllerMain.java | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/studio4plus/homerplayer/model/AudioBookManager.java b/app/src/main/java/com/studio4plus/homerplayer/model/AudioBookManager.java index c78a0c65..42892462 100644 --- a/app/src/main/java/com/studio4plus/homerplayer/model/AudioBookManager.java +++ b/app/src/main/java/com/studio4plus/homerplayer/model/AudioBookManager.java @@ -1,5 +1,7 @@ package com.studio4plus.homerplayer.model; +import android.os.Handler; +import android.os.Looper; import android.support.annotation.MainThread; import android.support.annotation.NonNull; @@ -17,6 +19,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.concurrent.TimeUnit; import javax.inject.Inject; @@ -30,13 +33,13 @@ public class AudioBookManager { private final Storage storage; private AudioBook currentBook; private boolean isInitialized = false; + private boolean isFirstScan = true; @Inject @MainThread public AudioBookManager(EventBus eventBus, FileScanner fileScanner, Storage storage) { this.fileScanner = fileScanner; this.storage = storage; - scanFiles(); eventBus.register(this); } @@ -109,6 +112,19 @@ public void onException(@NonNull Throwable t) { @MainThread private void processScanResult(@NonNull List fileSets) { + if (isFirstScan && fileSets.isEmpty()) { + // The first scan may fail if it is just after booting and the SD card is not yet + // mounted. Retry in a while. + Handler handler = new Handler(Looper.myLooper()); + handler.postDelayed(new Runnable() { + @Override + public void run() { + scanFiles(); + } + }, TimeUnit.SECONDS.toMillis(10)); + } + isFirstScan = false; + // This isn't very efficient but there shouldn't be more than a dozen audio books on the // device. List booksToRemove = new ArrayList<>(); diff --git a/app/src/main/java/com/studio4plus/homerplayer/ui/UiControllerMain.java b/app/src/main/java/com/studio4plus/homerplayer/ui/UiControllerMain.java index a764c51e..8754f07f 100644 --- a/app/src/main/java/com/studio4plus/homerplayer/ui/UiControllerMain.java +++ b/app/src/main/java/com/studio4plus/homerplayer/ui/UiControllerMain.java @@ -65,6 +65,7 @@ void onActivityCreated() { void onActivityStart() { Crashlytics.log("activity start"); + audioBookManager.scanFiles(); maybeSetInitialState(); }