-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run the audiobooks file scan on an IO thread.
This will allow triggering the scan directly from the UI without blocking it.
- Loading branch information
1 parent
bbbfcfc
commit eb01e53
Showing
16 changed files
with
369 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/studio4plus/homerplayer/concurrency/BackgroundDeferred.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.studio4plus.homerplayer.concurrency; | ||
|
||
import android.os.Handler; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
|
||
public class BackgroundDeferred<V> extends BaseDeferred<V> implements Runnable { | ||
|
||
private final @NonNull List<Listener<V>> listeners = new ArrayList<>(); | ||
|
||
private final @NonNull Callable<V> task; | ||
private final @NonNull Handler mainThreadHandler; | ||
|
||
BackgroundDeferred(@NonNull Callable<V> task, @NonNull Handler mainThreadHandler) { | ||
this.task = task; | ||
this.mainThreadHandler = mainThreadHandler; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
final @NonNull V newResult = task.call(); | ||
mainThreadHandler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
setResult(newResult); | ||
} | ||
}); | ||
} catch (final Exception e) { | ||
mainThreadHandler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
setException(e); | ||
} | ||
}); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/studio4plus/homerplayer/concurrency/BackgroundExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.studio4plus.homerplayer.concurrency; | ||
|
||
import android.os.Handler; | ||
import android.support.annotation.NonNull; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
public class BackgroundExecutor { | ||
|
||
private final @NonNull Handler mainThreadHandler; | ||
private final @NonNull Handler taskHandler; | ||
|
||
public BackgroundExecutor(@NonNull Handler mainThreadHandler, @NonNull Handler taskHandler) { | ||
this.mainThreadHandler = mainThreadHandler; | ||
this.taskHandler = taskHandler; | ||
} | ||
|
||
public <V> SimpleFuture<V> postTask(@NonNull Callable<V> task) { | ||
BackgroundDeferred<V> deferred = new BackgroundDeferred<>(task, mainThreadHandler); | ||
taskHandler.post(deferred); | ||
return deferred; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/studio4plus/homerplayer/concurrency/SimpleDeferred.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.studio4plus.homerplayer.concurrency; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
public class SimpleDeferred<V> extends BaseDeferred<V> { | ||
|
||
public void setResult(@NonNull V result) { | ||
super.setResult(result); | ||
} | ||
|
||
public void setException(@NonNull Throwable exception) { | ||
super.setException(exception); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...o4plus/homerplayer/util/SimpleFuture.java → ...homerplayer/concurrency/SimpleFuture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/studio4plus/homerplayer/filescanner/FileScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.studio4plus.homerplayer.filescanner; | ||
|
||
import android.content.Context; | ||
import android.os.Environment; | ||
|
||
import com.studio4plus.homerplayer.ApplicationScope; | ||
import com.studio4plus.homerplayer.concurrency.BackgroundExecutor; | ||
import com.studio4plus.homerplayer.concurrency.SimpleFuture; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
@ApplicationScope | ||
public class FileScanner { | ||
public static final String SAMPLE_BOOK_FILE_NAME = ".sample"; | ||
|
||
private final String audioBooksDirectoryPath; | ||
private final BackgroundExecutor ioExecutor; | ||
private final Context applicationContext; | ||
|
||
@Inject | ||
public FileScanner( | ||
@Named("AUDIOBOOKS_DIRECTORY") String audioBooksDirectoryPath, | ||
@Named("IO_EXECUTOR") BackgroundExecutor ioExecutor, | ||
Context applicationContext) { | ||
this.audioBooksDirectoryPath = audioBooksDirectoryPath; | ||
this.ioExecutor = ioExecutor; | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
public SimpleFuture<List<FileSet>> scanAudioBooksDirectories() { | ||
ScanFilesTask task = new ScanFilesTask(applicationContext, audioBooksDirectoryPath); | ||
return ioExecutor.postTask(task); | ||
} | ||
|
||
/** | ||
* Provide the default directory for audio books. | ||
* | ||
* The directory is in the devices external storage. Other than that there is nothing | ||
* special about it (e.g. it may be on an removable storage). | ||
*/ | ||
public File getDefaultAudioBooksDirectory() { | ||
File externalStorage = Environment.getExternalStorageDirectory(); | ||
return new File(externalStorage, audioBooksDirectoryPath); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tudio4plus/homerplayer/model/FileSet.java → ...plus/homerplayer/filescanner/FileSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.