From 8a83548ab855916f7d9fe059d3b5687d77e739be Mon Sep 17 00:00:00 2001 From: DjBushido Date: Mon, 28 Apr 2014 15:07:39 -0400 Subject: [PATCH] Initial draft of Event-based download management --- .../downloader/BookListFragment.java | 23 ++++-- .../downloader/DownloadManager.java | 13 +++- .../activities/downloader/EventBookList.java | 18 +++++ .../downloader/EventBookRefreshTask.java | 77 +++++++++++++++++++ 4 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookList.java create mode 100644 MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookRefreshTask.java diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java index a95d4dd..403926a 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java @@ -9,6 +9,8 @@ import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.BookFilter; import org.crosswire.jsword.book.BookFilters; +import de.greenrobot.event.EventBus; + import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; @@ -33,6 +35,7 @@ public class BookListFragment extends Fragment { private static final String ARG_BOOK_CATEGORY = "book_category"; protected TextView tv; + private ProgressDialog refreshDialog; /** * Returns a new instance of this fragment for the given section number. @@ -121,16 +124,20 @@ public class BookListFragment extends Fragment { break; } DownloadManager dm = DownloadManager.getInstance(); + EventBus downloadBus = dm.getDownloadBus(); + downloadBus.registerSticky(this); - if (!dm.isLoaded()) { - ProgressDialog refreshDialog = new ProgressDialog(getActivity()); - refreshDialog.setMessage("Refreshing available modules..."); - refreshDialog.setCancelable(false); - refreshDialog.show(); - dm.fetchAvailableBooks(f, new DlBookRefreshListener(refreshDialog)); - } else { - dm.fetchAvailableBooks(f, new DlBookRefreshListener(null)); + refreshDialog = new ProgressDialog(getActivity()); + refreshDialog.setMessage("Refreshing available modules..."); + refreshDialog.setCancelable(false); + refreshDialog.show(); + } + + public void onEventMainThread(EventBookList event) { + if (refreshDialog != null) { + refreshDialog.cancel(); } + tv.setText(event.getBookList().get(0).getName()); } private class DlBookRefreshListener implements diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java index 2ba56cc..da12dc7 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java @@ -11,6 +11,8 @@ import org.crosswire.jsword.book.BookFilter; import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.Installer; +import de.greenrobot.event.EventBus; + import android.util.Log; public class DownloadManager { @@ -18,6 +20,7 @@ public class DownloadManager { private final String TAG = "DownloadManager"; private static DownloadManager instance; private List books; + private EventBus downloadBus; public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE, BookCategory.COMMENTARY, BookCategory.DICTIONARY, @@ -32,6 +35,7 @@ public class DownloadManager { private DownloadManager() { setDownloadDir(); + downloadBus = new EventBus(); } public BookRefreshTask fetchAvailableBooks( @@ -93,5 +97,12 @@ public class DownloadManager { listener.onRefreshComplete(results); } } - + + private void downloadEvents() { + new EventBookRefreshTask(downloadBus).execute(getInstallersArray()); + } + + public EventBus getDownloadBus() { + return this.downloadBus; + } } diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookList.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookList.java new file mode 100644 index 0000000..10e0f1b --- /dev/null +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookList.java @@ -0,0 +1,18 @@ +package org.bspeice.minimalbible.activities.downloader; + +import java.util.List; + +import org.crosswire.jsword.book.Book; + +public class EventBookList { + + private List bookList; + + public EventBookList(List bookList) { + this.bookList = bookList; + } + + public List getBookList() { + return bookList; + } +} diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookRefreshTask.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookRefreshTask.java new file mode 100644 index 0000000..773725c --- /dev/null +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookRefreshTask.java @@ -0,0 +1,77 @@ +package org.bspeice.minimalbible.activities.downloader; + +import java.util.LinkedList; +import java.util.List; + +import org.bspeice.minimalbible.MinimalBible; +import org.bspeice.minimalbible.MinimalBibleConstants; +import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.BookFilter; +import org.crosswire.jsword.book.install.InstallException; +import org.crosswire.jsword.book.install.Installer; + +import de.greenrobot.event.EventBus; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.AsyncTask; +import android.util.Log; + +public class EventBookRefreshTask extends AsyncTask> { + private static final String TAG = "EventBookRefreshTask"; + + private EventBus downloadBus; + private BookFilter filter; + + public EventBookRefreshTask(EventBus downloadBus) { + this.downloadBus = downloadBus; + } + + public EventBookRefreshTask(EventBus downloadBus, BookFilter f) { + this.downloadBus = downloadBus; + this.filter = f; + } + + @Override + protected List doInBackground(Installer... params) { + List books = new LinkedList(); + + int index = 0; + for (Installer i : params) { + if (doRefresh()) { + try { + i.reloadBookList(); + } catch (InstallException e) { + Log.e(TAG, + "Error downloading books from installer: " + + i.toString(), e); + } + } + + if (filter != null) { + books.addAll(i.getBooks(filter)); + } else { + books.addAll(i.getBooks()); + } + publishProgress(++index, params.length); + } + + downloadBus.postSticky(new EventBookList(books)); + return books; + } + + private boolean doRefresh() { + // Check if we should refresh over the internet, or use the local copy + // TODO: Discover if we need to refresh over Internet, or use a cached + // copy - likely something time-based, also check network state. + // Fun fact - jSword handles the caching for us. + + SharedPreferences prefs = MinimalBible.getAppContext() + .getSharedPreferences( + MinimalBibleConstants.DOWNLOAD_PREFS_FILE, + Context.MODE_PRIVATE); + + // Refresh if download enabled + return prefs.getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, false); + } +}