Skip to content

Commit

Permalink
Scan for new books more often.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
msimonides committed Mar 4, 2018
1 parent eb01e53 commit bd91bdf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;

Expand All @@ -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);
}

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

@MainThread
private void processScanResult(@NonNull List<FileSet> 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<AudioBook> booksToRemove = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void onActivityCreated() {

void onActivityStart() {
Crashlytics.log("activity start");
audioBookManager.scanFiles();
maybeSetInitialState();
}

Expand Down

0 comments on commit bd91bdf

Please sign in to comment.