Skip to content

Commit bd91bdf

Browse files
committed
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).
1 parent eb01e53 commit bd91bdf

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

app/src/main/java/com/studio4plus/homerplayer/model/AudioBookManager.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.studio4plus.homerplayer.model;
22

3+
import android.os.Handler;
4+
import android.os.Looper;
35
import android.support.annotation.MainThread;
46
import android.support.annotation.NonNull;
57

@@ -17,6 +19,7 @@
1719
import java.util.Collections;
1820
import java.util.Comparator;
1921
import java.util.List;
22+
import java.util.concurrent.TimeUnit;
2023

2124
import javax.inject.Inject;
2225

@@ -30,13 +33,13 @@ public class AudioBookManager {
3033
private final Storage storage;
3134
private AudioBook currentBook;
3235
private boolean isInitialized = false;
36+
private boolean isFirstScan = true;
3337

3438
@Inject
3539
@MainThread
3640
public AudioBookManager(EventBus eventBus, FileScanner fileScanner, Storage storage) {
3741
this.fileScanner = fileScanner;
3842
this.storage = storage;
39-
scanFiles();
4043
eventBus.register(this);
4144
}
4245

@@ -109,6 +112,19 @@ public void onException(@NonNull Throwable t) {
109112

110113
@MainThread
111114
private void processScanResult(@NonNull List<FileSet> fileSets) {
115+
if (isFirstScan && fileSets.isEmpty()) {
116+
// The first scan may fail if it is just after booting and the SD card is not yet
117+
// mounted. Retry in a while.
118+
Handler handler = new Handler(Looper.myLooper());
119+
handler.postDelayed(new Runnable() {
120+
@Override
121+
public void run() {
122+
scanFiles();
123+
}
124+
}, TimeUnit.SECONDS.toMillis(10));
125+
}
126+
isFirstScan = false;
127+
112128
// This isn't very efficient but there shouldn't be more than a dozen audio books on the
113129
// device.
114130
List<AudioBook> booksToRemove = new ArrayList<>();

app/src/main/java/com/studio4plus/homerplayer/ui/UiControllerMain.java

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void onActivityCreated() {
6565

6666
void onActivityStart() {
6767
Crashlytics.log("activity start");
68+
audioBookManager.scanFiles();
6869
maybeSetInitialState();
6970
}
7071

0 commit comments

Comments
 (0)