mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2024-12-22 06:38:28 -05:00
Initial draft of Event-based download management
This commit is contained in:
parent
1f116fe6ff
commit
8a83548ab8
@ -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
|
||||
|
@ -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<Book> 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(
|
||||
@ -94,4 +98,11 @@ public class DownloadManager {
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadEvents() {
|
||||
new EventBookRefreshTask(downloadBus).execute(getInstallersArray());
|
||||
}
|
||||
|
||||
public EventBus getDownloadBus() {
|
||||
return this.downloadBus;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user