Initial draft of Event-based download management

This commit is contained in:
DjBushido 2014-04-28 15:07:39 -04:00
parent 1f116fe6ff
commit 8a83548ab8
4 changed files with 122 additions and 9 deletions

View File

@ -9,6 +9,8 @@ import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.BookFilter; import org.crosswire.jsword.book.BookFilter;
import org.crosswire.jsword.book.BookFilters; import org.crosswire.jsword.book.BookFilters;
import de.greenrobot.event.EventBus;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.ProgressDialog; import android.app.ProgressDialog;
@ -33,6 +35,7 @@ public class BookListFragment extends Fragment {
private static final String ARG_BOOK_CATEGORY = "book_category"; private static final String ARG_BOOK_CATEGORY = "book_category";
protected TextView tv; protected TextView tv;
private ProgressDialog refreshDialog;
/** /**
* Returns a new instance of this fragment for the given section number. * Returns a new instance of this fragment for the given section number.
@ -121,16 +124,20 @@ public class BookListFragment extends Fragment {
break; break;
} }
DownloadManager dm = DownloadManager.getInstance(); DownloadManager dm = DownloadManager.getInstance();
EventBus downloadBus = dm.getDownloadBus();
downloadBus.registerSticky(this);
if (!dm.isLoaded()) { refreshDialog = new ProgressDialog(getActivity());
ProgressDialog refreshDialog = new ProgressDialog(getActivity());
refreshDialog.setMessage("Refreshing available modules..."); refreshDialog.setMessage("Refreshing available modules...");
refreshDialog.setCancelable(false); refreshDialog.setCancelable(false);
refreshDialog.show(); refreshDialog.show();
dm.fetchAvailableBooks(f, new DlBookRefreshListener(refreshDialog));
} else {
dm.fetchAvailableBooks(f, new DlBookRefreshListener(null));
} }
public void onEventMainThread(EventBookList event) {
if (refreshDialog != null) {
refreshDialog.cancel();
}
tv.setText(event.getBookList().get(0).getName());
} }
private class DlBookRefreshListener implements private class DlBookRefreshListener implements

View File

@ -11,6 +11,8 @@ import org.crosswire.jsword.book.BookFilter;
import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.book.install.Installer;
import de.greenrobot.event.EventBus;
import android.util.Log; import android.util.Log;
public class DownloadManager { public class DownloadManager {
@ -18,6 +20,7 @@ public class DownloadManager {
private final String TAG = "DownloadManager"; private final String TAG = "DownloadManager";
private static DownloadManager instance; private static DownloadManager instance;
private List<Book> books; private List<Book> books;
private EventBus downloadBus;
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE, public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
BookCategory.COMMENTARY, BookCategory.DICTIONARY, BookCategory.COMMENTARY, BookCategory.DICTIONARY,
@ -32,6 +35,7 @@ public class DownloadManager {
private DownloadManager() { private DownloadManager() {
setDownloadDir(); setDownloadDir();
downloadBus = new EventBus();
} }
public BookRefreshTask fetchAvailableBooks( public BookRefreshTask fetchAvailableBooks(
@ -94,4 +98,11 @@ public class DownloadManager {
} }
} }
private void downloadEvents() {
new EventBookRefreshTask(downloadBus).execute(getInstallersArray());
}
public EventBus getDownloadBus() {
return this.downloadBus;
}
} }

View File

@ -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<Book> bookList;
public EventBookList(List<Book> bookList) {
this.bookList = bookList;
}
public List<Book> getBookList() {
return bookList;
}
}

View File

@ -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<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);
}
}