diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java index ecdc5e5..bc65ce9 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java @@ -5,6 +5,7 @@ import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManage import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadThread; import org.bspeice.minimalbible.activities.downloader.manager.BookRefreshTask; import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; +import org.bspeice.minimalbible.activities.downloader.manager.InstalledManager; import org.bspeice.minimalbible.activities.downloader.manager.RefreshManager; import org.crosswire.common.progress.JobManager; @@ -28,7 +29,8 @@ import de.greenrobot.event.EventBus; BookItemHolder.class, BookDownloadManager.class, BookDownloadThread.class, - RefreshManager.class + RefreshManager.class, + InstalledManager.class } ) public class ActivityDownloaderModule { diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java index 5d4a522..0061fe5 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java @@ -13,6 +13,7 @@ import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManager; import org.bspeice.minimalbible.activities.downloader.manager.DLProgressEvent; import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; +import org.bspeice.minimalbible.activities.downloader.manager.InstalledManager; import org.crosswire.jsword.book.Book; import javax.inject.Inject; @@ -36,6 +37,7 @@ public class BookItemHolder { @Inject DownloadManager downloadManager; @Inject BookDownloadManager bookDownloadManager; + @Inject InstalledManager installedManager; Book b; @@ -51,14 +53,13 @@ public class BookItemHolder { DLProgressEvent dlProgressEvent = bookDownloadManager.getInProgressDownloadProgress(b); if (dlProgressEvent != null) { displayProgress((int) dlProgressEvent.toCircular()); - } else if (downloadManager.isInstalled(b)) { + } else if (installedManager.isInstalled(b)) { displayInstalled(); } downloadManager.getDownloadBus().register(this); } private void displayInstalled() { - isDownloaded.setImageResource(android.R.color.transparent); isDownloaded.setImageResource(R.drawable.ic_action_cancel); } @@ -67,7 +68,6 @@ public class BookItemHolder { bookDownloadManager.installBook(this.b); } - @SuppressLint("") public void onEventMainThread(DLProgressEvent event) { if (event.getB().getOsisID().equals(b.getOsisID())) { displayProgress((int) event.toCircular()); diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java index 1a555d4..7120d57 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java @@ -8,6 +8,9 @@ import org.crosswire.common.progress.Progress; import org.crosswire.common.progress.WorkEvent; import org.crosswire.common.progress.WorkListener; import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.Books; +import org.crosswire.jsword.book.BooksEvent; +import org.crosswire.jsword.book.BooksListener; import java.util.HashMap; import java.util.Map; @@ -21,9 +24,10 @@ import de.greenrobot.event.EventBus; /** * Wrapper to convert JSword progress events to MinimalBible EventBus-based */ -//TODO: Listen for installed downloads in case the download took very little time? +//TODO: Make sure that jobs have the correct name +//TODO: Install indexes for Bibles @Singleton -public class BookDownloadManager implements WorkListener{ +public class BookDownloadManager implements WorkListener, BooksListener { /** * Mapping of Job ID to the EventBus we should trigger progress on @@ -45,6 +49,7 @@ public class BookDownloadManager implements WorkListener{ inProgressDownloads = new HashMap(); JobManager.addWorkListener(this); MinimalBible.getApplication().inject(this); + Books.installed().addBooksListener(this); } public void installBook(Book b) { @@ -60,6 +65,7 @@ public class BookDownloadManager implements WorkListener{ @Override public void workProgressed(WorkEvent ev) { Progress job = ev.getJob(); + Log.d("BookDownloadManager", "Download in progress: " + job.getJobID() + " - " + job.getJobName() + " " + job.getWorkDone() + "/" + job.getTotalWork()); EventBus downloadBus = downloadManager.getDownloadBus(); if (bookMappings.containsKey(job.getJobID())) { Book b = bookMappings.get(job.getJobID()); @@ -95,4 +101,25 @@ public class BookDownloadManager implements WorkListener{ public void workStateChanged(WorkEvent ev) { Log.d("BookDownloadManager", ev.toString()); } + + @Override + public void bookAdded(BooksEvent booksEvent) { + // It's possible the install finished before we received a progress event for it, + // we handle that case here. + Book b = booksEvent.getBook(); + Log.d("BookDownloadManager", "Book added: " + b.getName()); + if (inProgressDownloads.containsKey(b)) { + inProgressDownloads.remove(b); + } + // Not sure why, but the inProgressDownloads might not have our book, + // so we always trigger the PROGRESS_COMPLETE event. + // TODO: Make sure all books get to the inProgressDownloads + downloadManager.getDownloadBus() + .post(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b)); + } + + @Override + public void bookRemoved(BooksEvent booksEvent) { + // Not too worried about this just yet. + } } 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 index cf4ac3b..d61ca54 100644 --- 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 @@ -55,6 +55,6 @@ public class BookDownloadThread { * @return */ public static String getJobId(Book b) { - return "INSTALL_BOOK-" + b.getInitials().toUpperCase(); + return "INSTALL_BOOK-" + b.getInitials(); } } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java index a253e35..672d22a 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java @@ -4,7 +4,6 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; -import android.provider.ContactsContract; import android.util.Log; import org.bspeice.minimalbible.MinimalBible; @@ -13,26 +12,22 @@ import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.install.InstallException; import org.crosswire.jsword.book.install.Installer; -import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.inject.Inject; -import de.greenrobot.event.EventBus; - public class BookRefreshTask extends AsyncTask> { private static final String TAG = "EventBookRefreshTask"; // If last refresh was before the below, force an internet refresh private final Long refreshAfter = System.currentTimeMillis() - 604800000L; // 1 Week in millis - @Inject - DownloadPrefs downloadPrefs; + @Inject DownloadPrefs downloadPrefs; @Inject DownloadManager downloadManager; + @Inject InstalledManager installedManager; public BookRefreshTask() { MinimalBible.getApplication().inject(this); @@ -57,9 +52,9 @@ public class BookRefreshTask extends AsyncTask> { bookList.put(i, i.getBooks()); publishProgress(++index, params.length); } + //TODO: Filter duplicates - // Pre-cache the DownloadManager with the list of installed books - downloadManager.isInstalled(bookList.values().iterator().next().get(0)); + installedManager.initialize(); EventBookList event = new EventBookList(bookList); downloadManager.getDownloadBus().post(event); diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java index ea8000c..5836bfc 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java @@ -5,7 +5,10 @@ import android.util.Log; import org.bspeice.minimalbible.MinimalBible; import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.BookCategory; +import org.crosswire.jsword.book.BookList; import org.crosswire.jsword.book.Books; +import org.crosswire.jsword.book.BooksEvent; +import org.crosswire.jsword.book.BooksListener; import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.book.sword.SwordBookPath; @@ -28,7 +31,7 @@ public class DownloadManager { @Inject protected EventBus downloadBus; - private List installedBooks; + public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE, BookCategory.COMMENTARY, BookCategory.DICTIONARY, @@ -83,10 +86,5 @@ public class DownloadManager { return this.downloadBus; } - public boolean isInstalled(Book b) { - if (installedBooks == null) { - installedBooks = Books.installed().getBooks(); - } - return installedBooks.contains(b); - } + } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java new file mode 100644 index 0000000..36ba6a3 --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java @@ -0,0 +1,56 @@ +package org.bspeice.minimalbible.activities.downloader.manager; + +import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.Books; +import org.crosswire.jsword.book.BooksEvent; +import org.crosswire.jsword.book.BooksListener; + +import java.util.List; + +import javax.inject.Inject; +import javax.inject.Singleton; + +/** + * Created by bspeice on 5/25/14. + */ +@Singleton +public class InstalledManager implements BooksListener { + + @Inject DownloadManager downloadManager; + + private List installedBooks; + + /** + * Register our manager to receive events on Book install + * This is a relatively expensive operation, + * so we don't put it in the constructor. + */ + public void initialize() { + Books books = Books.installed(); + installedBooks = books.getBooks(); + books.addBooksListener(this); + } + + public boolean isInstalled(Book b) { + if (installedBooks == null) { + initialize(); + } + return installedBooks.contains(b); + } + + @Override + public void bookAdded(BooksEvent booksEvent) { + Book b = booksEvent.getBook(); + if (!installedBooks.contains(b)) { + installedBooks.add(b); + } + } + + @Override + public void bookRemoved(BooksEvent booksEvent) { + Book b = booksEvent.getBook(); + if (installedBooks.contains(b)) { + installedBooks.remove(b); + } + } +}