From 09e057261270ba1a5a7e8087841d94611e6401df Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 29 Apr 2014 23:25:38 -0400 Subject: [PATCH] Remove pre-EventBus code from Download --- .../downloader/BookListFragment.java | 20 --- .../downloader/BookRefreshTask.java | 161 +++++++++--------- .../downloader/DownloadManager.java | 53 +----- .../downloader/EventBookRefreshTask.java | 77 --------- .../downloader/NoopBookRefreshTask.java | 25 --- 5 files changed, 79 insertions(+), 257 deletions(-) delete mode 100644 MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookRefreshTask.java delete mode 100644 MinimalBible/src/org/bspeice/minimalbible/activities/downloader/NoopBookRefreshTask.java diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java index 403926a..1693590 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java @@ -140,26 +140,6 @@ public class BookListFragment extends Fragment { tv.setText(event.getBookList().get(0).getName()); } - 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) { - this.dl = dl; - } - - @Override - public void onRefreshComplete(List results) { - if (dl != null) { - dl.cancel(); - } - tv.setText(results.get(0).getName()); - - } - } - private class DownloadDialogListener implements DialogInterface.OnClickListener { @Override diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookRefreshTask.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookRefreshTask.java index 154cf59..eae1564 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookRefreshTask.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookRefreshTask.java @@ -1,84 +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 android.content.Context; -import android.content.SharedPreferences; -import android.os.AsyncTask; -import android.util.Log; - -public class BookRefreshTask extends AsyncTask> { - - private static final String TAG = "BookRefreshTask"; - - private BookRefreshListener listener; - private BookFilter filter; - - public BookRefreshTask(BookRefreshListener listener) { - this.listener = listener; - } - - public BookRefreshTask(BookFilter f, - BookRefreshListener listener) { - this.filter = f; - this.listener = listener; - } - - @Override - protected List doInBackground(Installer... params) { - List books = new LinkedList(); - - 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()); - } - } - - 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); - } - - @Override - protected void onPostExecute(List result) { - super.onPostExecute(result); - listener.onRefreshComplete(result); - } - - public interface BookRefreshListener { - public void onRefreshComplete(List results); - } -} +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 BookRefreshTask extends AsyncTask> { + private static final String TAG = "EventBookRefreshTask"; + + private EventBus downloadBus; + private BookFilter filter; + + public BookRefreshTask(EventBus downloadBus) { + this.downloadBus = downloadBus; + } + + public BookRefreshTask(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); + } +} diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java index 68e9a5f..44bba4f 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java @@ -1,25 +1,19 @@ package org.bspeice.minimalbible.activities.downloader; -import java.util.List; import java.util.Map; import org.bspeice.minimalbible.MinimalBible; -import org.bspeice.minimalbible.activities.downloader.BookRefreshTask.BookRefreshListener; -import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.BookCategory; -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; +import de.greenrobot.event.EventBus; 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, @@ -39,34 +33,6 @@ public class DownloadManager { downloadBus = new EventBus(); } - public BookRefreshTask fetchAvailableBooks( - BookRefreshTask.BookRefreshListener bookRefreshListener) { - return _fetchAvailableBooks(null, bookRefreshListener); - } - - public BookRefreshTask fetchAvailableBooks(BookFilter f, - BookRefreshTask.BookRefreshListener bookRefreshListener) { - return _fetchAvailableBooks(f, bookRefreshListener); - } - - private BookRefreshTask _fetchAvailableBooks(BookFilter f, - BookRefreshTask.BookRefreshListener bookRefreshListener) { - - if (!isLoaded()) { - return (BookRefreshTask) new BookRefreshTask( - new DmBookRefreshListener(bookRefreshListener)) - .execute(getInstallersArray()); - } else { - return (BookRefreshTask) new NoopBookRefreshTask(books, - bookRefreshListener).execute(getInstallersArray()); - } - } - - public boolean isLoaded() { - // Let methods know if we're going to take a while to reload everything - return (books != null); - } - public Map getInstallers() { return new InstallManager().getInstallers(); } @@ -84,23 +50,8 @@ public class DownloadManager { System.setProperty("jsword.home", home); } - // Create our own refresh listener to save a reference to the books - private class DmBookRefreshListener implements BookRefreshListener { - private BookRefreshListener listener; - - public DmBookRefreshListener(BookRefreshListener listener) { - this.listener = listener; - } - - @Override - public void onRefreshComplete(List results) { - books = results; - listener.onRefreshComplete(results); - } - } - private void downloadEvents() { - new EventBookRefreshTask(downloadBus).execute(getInstallersArray()); + new BookRefreshTask(downloadBus).execute(getInstallersArray()); } public EventBus getDownloadBus() { diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookRefreshTask.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookRefreshTask.java deleted file mode 100644 index 773725c..0000000 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/EventBookRefreshTask.java +++ /dev/null @@ -1,77 +0,0 @@ -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); - } -} diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/NoopBookRefreshTask.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/NoopBookRefreshTask.java deleted file mode 100644 index 6d732af..0000000 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/NoopBookRefreshTask.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.bspeice.minimalbible.activities.downloader; - -import java.util.List; - -import org.crosswire.jsword.book.Book; -import org.crosswire.jsword.book.install.Installer; - -/* - * There's probably a better way of doing this, but this allows me to avoid networking, - * while still maintaining the code structure of being asynchronous. - */ -public class NoopBookRefreshTask extends BookRefreshTask { - BookRefreshListener listener; - List books; - - public NoopBookRefreshTask(List books, BookRefreshListener listener) { - super(listener); - this.books = books; - } - - @Override - protected List doInBackground(Installer... params) { - return books; - } -}