Some refactoring work to support an optional refresh

This commit is contained in:
Bradlee Speice 2014-04-21 22:01:52 -04:00
parent 76004e34d0
commit 4c3a2ba5b5
4 changed files with 93 additions and 67 deletions

View File

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

View File

@ -6,7 +6,6 @@ import org.bspeice.minimalbible.MinimalBibleConstants;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.BaseActivity; import org.bspeice.minimalbible.activities.BaseActivity;
import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment; import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
import org.bspeice.minimalbible.activities.downloader.DownloadManager.BookRefreshListener;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import android.app.Activity; import android.app.Activity;
@ -31,8 +30,6 @@ import android.widget.Toast;
public class DownloadActivity extends BaseActivity implements public class DownloadActivity extends BaseActivity implements
BaseNavigationDrawerFragment.NavigationDrawerCallbacks { BaseNavigationDrawerFragment.NavigationDrawerCallbacks {
private ProgressDialog refreshDialog;
/** /**
* Fragment managing the behaviors, interactions and presentation of the * Fragment managing the behaviors, interactions and presentation of the
* navigation drawer. * navigation drawer.
@ -186,8 +183,7 @@ public class DownloadActivity extends BaseActivity implements
} }
private void refreshModules() { private void refreshModules() {
// TODO: Discover if we need to refresh over Internet, or use a cached // TODO: Discover if we need to refresh over Internet, or use a cached copy
// copy
// Fun fact - jSword handles the caching for us. // Fun fact - jSword handles the caching for us.
ProgressDialog refreshDialog = new ProgressDialog(this); ProgressDialog refreshDialog = new ProgressDialog(this);
refreshDialog.setMessage("Refreshing available modules..."); refreshDialog.setMessage("Refreshing available modules...");
@ -226,7 +222,7 @@ 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. // TODO: Figure out why I need to pass in the ProgressDialog, and can't cancel it from onRefreshComplete.
ProgressDialog dl; ProgressDialog dl;
public DlBookRefreshListener(ProgressDialog dl) { public DlBookRefreshListener(ProgressDialog dl) {

View File

@ -1,33 +1,34 @@
package org.bspeice.minimalbible.activities.downloader; package org.bspeice.minimalbible.activities.downloader;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.install.InstallException;
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 android.os.AsyncTask;
import android.util.Log; import android.util.Log;
public class DownloadManager { public class DownloadManager {
private final String TAG = "DownloadManager"; private final String TAG = "DownloadManager";
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
BookCategory.COMMENTARY, BookCategory.DICTIONARY,
BookCategory.IMAGES, BookCategory.MAPS };
public DownloadManager() { public DownloadManager() {
setDownloadDir(); setDownloadDir();
} }
public InstallerReloadTask fetchAvailableBooks(boolean forceRefresh, BookRefreshListener bookRefreshListener) { public BookRefreshTask fetchAvailableBooks(boolean refresh,
BookRefreshTask.BookRefreshListener bookRefreshListener) {
Map<String, Installer> installers = getInstallers(); Map<String, Installer> installers = getInstallers();
return (InstallerReloadTask) return (BookRefreshTask) new BookRefreshTask(refresh,
new InstallerReloadTask(bookRefreshListener).execute(installers.values().toArray(new Installer[installers.size()])); bookRefreshListener).execute(installers.values().toArray(
new Installer[installers.size()]));
} }
public Map<String, Installer> getInstallers() { public Map<String, Installer> getInstallers() {
@ -35,43 +36,11 @@ public class DownloadManager {
} }
private void setDownloadDir() { 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(); String home = MinimalBible.getAppContext().getFilesDir().toString();
Log.d(TAG, "Setting jsword.home to: " + home); Log.d(TAG, "Setting jsword.home to: " + home);
System.setProperty("jsword.home", home); System.setProperty("jsword.home", home);
} }
public class InstallerReloadTask extends AsyncTask<Installer, Float, List<Book>> {
private BookRefreshListener listener;
public InstallerReloadTask(BookRefreshListener listener) {
this.listener = listener;
}
@Override
protected List<Book> doInBackground(Installer... params) {
List<Book> books = new LinkedList<Book>();
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<Book> result) {
super.onPostExecute(result);
listener.onRefreshComplete(result);
}
}
public interface BookRefreshListener {
public void onRefreshComplete(List<Book> results);
}
} }

View File

@ -2,8 +2,6 @@ package org.bspeice.minimalbible.activities.downloader;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment; import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.BookFilters;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -15,10 +13,6 @@ import android.widget.ListView;
public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment { public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment {
private final BookCategory[] displayCategories = {BookCategory.BIBLE, BookCategory.COMMENTARY,
BookCategory.DICTIONARY, BookCategory.IMAGES, BookCategory.MAPS
};
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -33,9 +27,9 @@ public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment {
} }
}); });
String[] sCategories = new String[displayCategories.length]; String[] sCategories = new String[DownloadManager.VALID_CATEGORIES.length];
for (int i = 0; i < displayCategories.length; i++) { for (int i = 0; i < DownloadManager.VALID_CATEGORIES.length; i++) {
sCategories[i] = displayCategories[i].toString(); sCategories[i] = DownloadManager.VALID_CATEGORIES[i].toString();
} }
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar() mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar()