From 4c3a2ba5b578b67cb10cbf588ac0ede1d02709af Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 21 Apr 2014 22:01:52 -0400 Subject: [PATCH] Some refactoring work to support an optional refresh --- .../downloader/BookRefreshTask.java | 67 +++++++++++++++++ .../downloader/DownloadActivity.java | 10 +-- .../downloader/DownloadManager.java | 71 ++++++------------- .../downloader/DownloadNavDrawerFragment.java | 12 +--- 4 files changed, 93 insertions(+), 67 deletions(-) create mode 100644 MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookRefreshTask.java diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookRefreshTask.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookRefreshTask.java new file mode 100644 index 0000000..86f8795 --- /dev/null +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookRefreshTask.java @@ -0,0 +1,67 @@ +package org.bspeice.minimalbible.activities.downloader; + +import java.util.LinkedList; +import java.util.List; + +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 android.os.AsyncTask; +import android.util.Log; + +public class BookRefreshTask extends AsyncTask> { + + private static final String TAG = "BookRefreshTask"; + + private BookRefreshListener listener; + private boolean refresh; + private BookFilter filter; + + public BookRefreshTask(boolean refresh, BookRefreshListener listener) { + this.refresh = refresh; + this.listener = listener; + } + + public BookRefreshTask(boolean refresh, BookFilter f, BookRefreshListener listener) { + this.refresh = refresh; + this.filter = f; + this.listener = listener; + } + + @Override + protected List doInBackground(Installer... params) { + List books = new LinkedList(); + + for (Installer i: params) { + if (refresh) { + 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()); + } + } + + return books; + } + + @Override + protected void onPostExecute(List result) { + super.onPostExecute(result); + listener.onRefreshComplete(result); + } + + public interface BookRefreshListener { + public void onRefreshComplete(List results); + } +} + + diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java index fdc08ac..1efcf50 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java @@ -6,7 +6,6 @@ import org.bspeice.minimalbible.MinimalBibleConstants; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activities.BaseActivity; import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment; -import org.bspeice.minimalbible.activities.downloader.DownloadManager.BookRefreshListener; import org.crosswire.jsword.book.Book; import android.app.Activity; @@ -31,8 +30,6 @@ import android.widget.Toast; public class DownloadActivity extends BaseActivity implements BaseNavigationDrawerFragment.NavigationDrawerCallbacks { - private ProgressDialog refreshDialog; - /** * Fragment managing the behaviors, interactions and presentation of the * navigation drawer. @@ -186,8 +183,7 @@ public class DownloadActivity extends BaseActivity implements } private void refreshModules() { - // TODO: Discover if we need to refresh over Internet, or use a cached - // copy + // TODO: Discover if we need to refresh over Internet, or use a cached copy // Fun fact - jSword handles the caching for us. ProgressDialog refreshDialog = new ProgressDialog(this); refreshDialog.setMessage("Refreshing available modules..."); @@ -225,8 +221,8 @@ public class DownloadActivity extends BaseActivity implements } } - - private class DlBookRefreshListener implements BookRefreshListener { + + private class DlBookRefreshListener implements BookRefreshTask.BookRefreshListener { // TODO: Figure out why I need to pass in the ProgressDialog, and can't cancel it from onRefreshComplete. ProgressDialog dl; public DlBookRefreshListener(ProgressDialog dl) { diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java index 43e8d16..5e635fc 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java @@ -1,77 +1,46 @@ package org.bspeice.minimalbible.activities.downloader; -import java.util.LinkedList; -import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutionException; import org.bspeice.minimalbible.MinimalBible; -import org.crosswire.jsword.book.Book; -import org.crosswire.jsword.book.install.InstallException; +import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.Installer; -import android.os.AsyncTask; import android.util.Log; public class DownloadManager { - + private final String TAG = "DownloadManager"; - + + public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE, + BookCategory.COMMENTARY, BookCategory.DICTIONARY, + BookCategory.IMAGES, BookCategory.MAPS }; + public DownloadManager() { setDownloadDir(); } - - public InstallerReloadTask fetchAvailableBooks(boolean forceRefresh, BookRefreshListener bookRefreshListener) { - + + public BookRefreshTask fetchAvailableBooks(boolean refresh, + BookRefreshTask.BookRefreshListener bookRefreshListener) { + Map installers = getInstallers(); - - return (InstallerReloadTask) - new InstallerReloadTask(bookRefreshListener).execute(installers.values().toArray(new Installer[installers.size()])); + + return (BookRefreshTask) new BookRefreshTask(refresh, + bookRefreshListener).execute(installers.values().toArray( + new Installer[installers.size()])); } - + public Map getInstallers() { - return new InstallManager().getInstallers(); + return new InstallManager().getInstallers(); } - + private void setDownloadDir() { - // We need to set the download directory for jSword to stick with Android. + // We need to set the download directory for jSword to stick with + // Android. String home = MinimalBible.getAppContext().getFilesDir().toString(); Log.d(TAG, "Setting jsword.home to: " + home); System.setProperty("jsword.home", home); } - - public class InstallerReloadTask extends AsyncTask> { - private BookRefreshListener listener; - - public InstallerReloadTask(BookRefreshListener listener) { - this.listener = listener; - } - - @Override - protected List doInBackground(Installer... params) { - List books = new LinkedList(); - for (Installer i: params) { - try { - i.reloadBookList(); - } catch (InstallException e) { - Log.e(TAG, "Error downloading books from installer: " + i.toString(), e); - } - books.addAll(i.getBooks()); - } - - return books; - } - - @Override - protected void onPostExecute(List result) { - super.onPostExecute(result); - listener.onRefreshComplete(result); - } - } - - public interface BookRefreshListener { - public void onRefreshComplete(List results); - } } diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java index d56a867..5aace3e 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java @@ -2,8 +2,6 @@ package org.bspeice.minimalbible.activities.downloader; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment; -import org.crosswire.jsword.book.BookCategory; -import org.crosswire.jsword.book.BookFilters; import android.os.Bundle; import android.view.LayoutInflater; @@ -15,10 +13,6 @@ import android.widget.ListView; public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment { - private final BookCategory[] displayCategories = {BookCategory.BIBLE, BookCategory.COMMENTARY, - BookCategory.DICTIONARY, BookCategory.IMAGES, BookCategory.MAPS - }; - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -33,9 +27,9 @@ public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment { } }); - String[] sCategories = new String[displayCategories.length]; - for (int i = 0; i < displayCategories.length; i++) { - sCategories[i] = displayCategories[i].toString(); + String[] sCategories = new String[DownloadManager.VALID_CATEGORIES.length]; + for (int i = 0; i < DownloadManager.VALID_CATEGORIES.length; i++) { + sCategories[i] = DownloadManager.VALID_CATEGORIES[i].toString(); } mDrawerListView.setAdapter(new ArrayAdapter(getActionBar()