Remove pre-EventBus code from Download

This commit is contained in:
Bradlee Speice 2014-04-29 23:25:38 -04:00
parent 72cf38a2fc
commit 09e0572612
5 changed files with 79 additions and 257 deletions

View File

@ -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<Book> results) {
if (dl != null) {
dl.cancel();
}
tv.setText(results.get(0).getName());
}
}
private class DownloadDialogListener implements
DialogInterface.OnClickListener {
@Override

View File

@ -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<Installer, Integer, List<Book>> {
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<Book> doInBackground(Installer... params) {
List<Book> books = new LinkedList<Book>();
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<Book> result) {
super.onPostExecute(result);
listener.onRefreshComplete(result);
}
public interface BookRefreshListener {
public void onRefreshComplete(List<Book> 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<Installer, Integer, List<Book>> {
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<Book> doInBackground(Installer... params) {
List<Book> books = new LinkedList<Book>();
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);
}
}

View File

@ -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<Book> 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<String, Installer> 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<Book> results) {
books = results;
listener.onRefreshComplete(results);
}
}
private void downloadEvents() {
new EventBookRefreshTask(downloadBus).execute(getInstallersArray());
new BookRefreshTask(downloadBus).execute(getInstallersArray());
}
public EventBus getDownloadBus() {

View File

@ -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<Installer, Integer, List<Book>> {
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<Book> doInBackground(Installer... params) {
List<Book> books = new LinkedList<Book>();
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);
}
}

View File

@ -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<Book> books;
public NoopBookRefreshTask(List<Book> books, BookRefreshListener listener) {
super(listener);
this.books = books;
}
@Override
protected List<Book> doInBackground(Installer... params) {
return books;
}
}