Can download books!

Progress doesn't show, but I'm about to fix that.
This commit is contained in:
Bradlee Speice 2014-05-24 21:12:20 -04:00
parent 38181c8827
commit 6afb9b6f28
14 changed files with 436 additions and 236 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "jsword-minimalbible"]
path = jsword-minimalbible
url = https://github.com/MinimalBible/jsword-minimalbible.git
[submodule "ProgressWheel"]
path = ProgressWheel
url = git@github.com:Todd-Davies/ProgressWheel

View File

@ -1,8 +1,11 @@
package org.bspeice.minimalbible.activities.downloader;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManager;
import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadThread;
import org.bspeice.minimalbible.activities.downloader.manager.BookRefreshTask;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
import org.crosswire.common.progress.JobManager;
import javax.inject.Singleton;
@ -19,7 +22,9 @@ import de.greenrobot.event.EventBus;
BookListFragment.class,
DownloadManager.class,
BookRefreshTask.class,
BookItemHolder.class
BookItemHolder.class,
BookDownloadManager.class,
BookDownloadThread.class
}
)
public class ActivityDownloaderModule {
@ -40,7 +45,6 @@ public class ActivityDownloaderModule {
return new EventBus();
}
@Provides //@Singleton
DownloadPrefs provideDownloadPrefs() {
return Esperandro.getPreferences(DownloadPrefs.class, MinimalBible.getAppContext());

View File

@ -18,6 +18,7 @@ import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
/**
* Created by bspeice on 5/20/14.
@ -54,16 +55,12 @@ public class BookItemHolder {
@OnClick(R.id.download_ibtn_download)
public void onDownloadItem(View v) {
downloadManager.getDownloadBus().register(this);
downloadManager.getDownloadBus()
.post(new DownloadProgressEvent(DownloadProgressEvent.PROGRESS_BEGINNING, b));
// TODO: Kick off a service to actually do the downloading, rather than simulate
downloadManager.getDownloadBus().post(new DownloadProgressEvent(47, b));
downloadManager.getRefreshBus().register(this);
downloadManager.installBook(this.b);
}
public void onEventMainThread(DownloadProgressEvent event) {
if (event.getB().equals(this.b)) {
if (event.getB().equals(b)) {
displayProgress((int) event.toCircular());
}
}

View File

@ -119,7 +119,7 @@ public class BookListFragment extends BaseFragment {
List<Book> bookList = downloadManager.getBookList();
if (bookList == null) {
// downloadManager is in progress of refreshing
downloadManager.getDownloadBus().register(this);
downloadManager.getRefreshBus().register(this);
refreshDialog = new ProgressDialog(getActivity());
refreshDialog.setMessage("Refreshing available modules...");
refreshDialog.setCancelable(false);

View File

@ -0,0 +1,72 @@
package org.bspeice.minimalbible.activities.downloader.manager;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.common.progress.JobManager;
import org.crosswire.common.progress.Progress;
import org.crosswire.common.progress.WorkEvent;
import org.crosswire.common.progress.WorkListener;
import org.crosswire.jsword.book.Book;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import de.greenrobot.event.EventBus;
/**
* Wrapper to convert JSword progress events to MinimalBible EventBus-based
*/
@Singleton
public class BookDownloadManager implements WorkListener{
/**
* Mapping of Job ID to the EventBus we should trigger progress on
*/
private Map<String, Book> bookMappings;
@Inject
Provider<BookDownloadThread> dlThreadProvider;
/* Going to fix this in the next commit, right now it's circular
@Inject
*/
DownloadManager downloadManager;
public BookDownloadManager() {
bookMappings = new HashMap<String, Book>();
JobManager.addWorkListener(this);
MinimalBible.getApplication().inject(this);
}
public void downloadBook(Book b) {
BookDownloadThread dlThread = dlThreadProvider.get();
dlThread.downloadBook(b);
addJob(BookDownloadThread.getJobId(b), b);
}
public void addJob(String jobId, Book b) {
bookMappings.put(jobId, b);
}
@Override
public void workProgressed(WorkEvent ev) {
Log.d("BookDownloadManager", ev.toString());
Progress job = ev.getJob();
if (bookMappings.containsKey(job.getJobID())) {
downloadManager.getRefreshBus()
.post(new DownloadProgressEvent(job.getTotalWork(), job.getWorkDone(),
bookMappings.get(job.getJobID())));
}
}
@Override
public void workStateChanged(WorkEvent ev) {
Log.d("BookDownloadManager", ev.toString());
}
}

View File

@ -0,0 +1,63 @@
package org.bspeice.minimalbible.activities.downloader.manager;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.common.progress.JobManager;
import org.crosswire.common.progress.WorkEvent;
import org.crosswire.common.progress.WorkListener;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer;
import java.util.UUID;
import javax.inject.Inject;
import de.greenrobot.event.EventBus;
/**
* Created by bspeice on 5/12/14.
*/
public class BookDownloadThread {
private final String TAG = "BookDownloadThread";
@Inject
DownloadManager downloadManager;
public BookDownloadThread() {
MinimalBible.getApplication().inject(this);
}
public void downloadBook(final Book b) {
// So, the JobManager can't be injected, but we'll make do
// First, look up where the Book came from
final Installer i = downloadManager.installerFromBook(b);
final Thread worker = new Thread() {
@Override
public void run() {
try {
i.install(b);
} catch (InstallException e) {
Log.d(TAG, e.getMessage());
}
}
};
worker.start();
JobManager.createJob(getJobId(b), b.getName(), worker);
downloadManager.getRefreshBus().post(new DownloadProgressEvent(DownloadProgressEvent.PROGRESS_BEGINNING, b));
}
/**
* Return a job ID for a given book. Must be consistent per book.
* @param b
* @return A string representing this job IDs
*/
public static String getJobId(Book b) {
return b.toString();
}
}

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
@ -12,8 +13,11 @@ import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
@ -37,7 +41,7 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
@Override
protected List<Book> doInBackground(Installer... params) {
List<Book> books = new LinkedList<Book>();
Map<Installer, List<Book>> bookList = new HashMap<Installer, List<Book>>();
int index = 0;
for (Installer i : params) {
@ -51,13 +55,14 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
+ i.toString(), e);
}
}
books.addAll(i.getBooks());
bookList.put(i, i.getBooks());
publishProgress(++index, params.length);
}
downloadBus.postSticky(new EventBookList(books));
return books;
EventBookList event = new EventBookList(bookList);
downloadBus.post(event);
return event.getBookList();
}
private boolean doRefresh() {

View File

@ -3,11 +3,14 @@ package org.bspeice.minimalbible.activities.downloader.manager;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.common.util.CWProject;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.Installer;
import org.crosswire.jsword.book.sword.SwordBookPath;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -18,6 +21,7 @@ import javax.inject.Singleton;
import de.greenrobot.event.EventBus;
// TODO: Rename to RefreshManager? Refactor to RefreshManager?
@Singleton
public class DownloadManager {
@ -26,7 +30,7 @@ public class DownloadManager {
/**
* Cached copy of modules that are available so we don't refresh for everyone who requests it.
*/
private List<Book> availableModules;
private Map<Installer, List<Book>> availableModules;
/**
* Cached copy of downloads in progress so views displaying this info can get it quickly.
@ -34,7 +38,9 @@ public class DownloadManager {
private Map<Book, DownloadProgressEvent> inProgressDownloads;
@Inject
protected EventBus downloadBus;
protected EventBus refreshBus;
@Inject BookDownloadManager bookDownloadManager;
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
BookCategory.COMMENTARY, BookCategory.DICTIONARY,
@ -46,7 +52,10 @@ public class DownloadManager {
public DownloadManager() {
MinimalBible.getApplication().inject(this);
setDownloadDir();
availableModules = new HashMap<Installer, List<Book>>();
refreshModules();
inProgressDownloads = new HashMap<Book, DownloadProgressEvent>();
}
@ -78,6 +87,9 @@ public class DownloadManager {
String home = MinimalBible.getAppContext().getFilesDir().toString();
Log.d(TAG, "Setting jsword.home to: " + home);
System.setProperty("jsword.home", home);
System.setProperty("sword.home", home);
SwordBookPath.setDownloadDir(new File(home));
Log.d(TAG, "Sword download path: " + SwordBookPath.getSwordDownloadDir());
}
/**
@ -85,8 +97,8 @@ public class DownloadManager {
* when it's done.
*/
private void refreshModules() {
downloadBus.register(this);
new BookRefreshTask(downloadBus).execute(getInstallersArray());
refreshBus.register(this);
new BookRefreshTask(refreshBus).execute(getInstallersArray());
}
/**
@ -95,7 +107,7 @@ public class DownloadManager {
*/
@SuppressWarnings("unused")
public void onEvent(EventBookList event) {
this.availableModules = event.getBookList();
this.availableModules = event.getInstallerMapping();
}
/**
@ -103,7 +115,15 @@ public class DownloadManager {
* @return The cached book list, or null
*/
public List<Book> getBookList() {
return availableModules;
if (availableModules.values().size() == 0) {
return null;
} else {
List<Book> bookList = new ArrayList<Book>();
for (List<Book> l : availableModules.values()) {
bookList.addAll(l);
}
return bookList;
}
}
/**
@ -112,10 +132,12 @@ public class DownloadManager {
* been completed, make sure to check {@link #getBookList()} first.
* @return The EventBus the DownloadManager is using
*/
public EventBus getDownloadBus() {
return this.downloadBus;
public EventBus getRefreshBus() {
return this.refreshBus;
}
// TODO: All code below should be migrated to BookDownloadManager
/**
* Handle a book download progress event.
* Mostly responsible for caching the in progress status to check on it easily
@ -142,4 +164,22 @@ public class DownloadManager {
}
}
/**
* Find the installer that a Book comes from.
* @param b The book to search for
* @return The Installer that should be used for this book.
*/
public Installer installerFromBook(Book b) {
for (Map.Entry<Installer, List<Book>> entry : availableModules.entrySet()) {
if (entry.getValue().contains(b)) {
return entry.getKey();
}
}
return null;
}
public void installBook(Book b) {
bookDownloadManager.downloadBook(b);
}
}

View File

@ -12,8 +12,13 @@ public class DownloadProgressEvent {
public static final int PROGRESS_COMPLETE = 100;
public static final int PROGRESS_BEGINNING = 0;
public DownloadProgressEvent(int progress, Book b) {
this.progress = progress;
public DownloadProgressEvent(int workDone, int totalWork, Book b) {
this.progress = workDone / totalWork;
this.b = b;
}
public DownloadProgressEvent(int workDone, Book b) {
this.progress = workDone;
this.b = b;
}
@ -21,10 +26,6 @@ public class DownloadProgressEvent {
return progress;
}
public Book getB() {
return b;
}
public float toCircular() {
return ((float)progress) * 360 / 100;
}
@ -32,4 +33,8 @@ public class DownloadProgressEvent {
public boolean isComplete() {
return progress >= 100;
}
public Book getB() {
return this.b;
}
}

View File

@ -1,8 +1,11 @@
package org.bspeice.minimalbible.activities.downloader.manager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.Installer;
/**
* POJO class for {@link de.greenrobot.event.EventBus} to broadcast whenever
@ -10,13 +13,21 @@ import org.crosswire.jsword.book.Book;
*/
public class EventBookList {
private List<Book> bookList;
private Map<Installer, List<Book>> bookMapping;
public EventBookList(List<Book> bookList) {
this.bookList = bookList;
public EventBookList(Map<Installer, List<Book>> bookList) {
this.bookMapping = bookList;
}
public List<Book> getBookList() {
return bookList;
public Map<Installer, List<Book>> getInstallerMapping() {
return bookMapping;
}
public List<Book> getBookList() {
List<Book> bookList = new ArrayList<Book>();
for (Installer i: bookMapping.keySet()) {
bookList.addAll(bookMapping.get(i));
}
return bookList;
}
}