From e356c8d1fceed5703e48773f36464e97b370aa4d Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 10 Jun 2014 22:40:46 -0400 Subject: [PATCH] Revert "[broken] Remove the BookDownloadThread again" This reverts commit 8f346f17e4caad22d087db4e91a93505e6f2b68e. --- .../manager/BookDownloadThread.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadThread.java diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadThread.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadThread.java new file mode 100644 index 0000000..70edeec --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadThread.java @@ -0,0 +1,60 @@ +package org.bspeice.minimalbible.activities.downloader.manager; + +import android.util.Log; + +import org.bspeice.minimalbible.MinimalBible; +import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.install.InstallException; +import org.crosswire.jsword.book.install.Installer; + +import javax.inject.Inject; + +/** + * Thread that handles downloading a book + */ +//TODO: Refactor to BookDownloadManager, downloadBook() creates its own thread +public class BookDownloadThread { + + private final String TAG = "BookDownloadThread"; + + @Inject + DownloadManager downloadManager; + @Inject RefreshManager refreshManager; + + public BookDownloadThread() { + MinimalBible.getApplication().inject(this); + } + + public void downloadBook(final Book b) { + // So, the JobManager can't be injected, but we'll make do + + // First, look up where the Book came from + final Installer i = refreshManager.installerFromBook(b); + + final Thread worker = new Thread() { + @Override + public void run() { + try { + i.install(b); + } catch (InstallException e) { + Log.d(TAG, e.getMessage()); + } + } + }; + worker.start(); + // The worker automatically communicates with the JobManager for its progress. + + downloadManager.getDownloadBus().post(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b)); + } + + /** + * Build what the installer creates the job name as. + * Likely prone to be brittle. + * TODO: Make sure to test that this is an accurate job name + * @param b The book to predict the download job name of + * @return The name of the job that will/is download/ing this book + */ + public static String getJobId(Book b) { + return "INSTALL_BOOK-" + b.getInitials(); + } +}