Downloads are now thoroughly done

No bugs that I can see, likely needs some cleanup.
This commit is contained in:
Bradlee Speice 2014-05-25 17:48:39 -04:00
parent c1dc0da9e3
commit 1017f9a34d
7 changed files with 101 additions and 23 deletions

View File

@ -5,6 +5,7 @@ import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManage
import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadThread; import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadThread;
import org.bspeice.minimalbible.activities.downloader.manager.BookRefreshTask; import org.bspeice.minimalbible.activities.downloader.manager.BookRefreshTask;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
import org.bspeice.minimalbible.activities.downloader.manager.InstalledManager;
import org.bspeice.minimalbible.activities.downloader.manager.RefreshManager; import org.bspeice.minimalbible.activities.downloader.manager.RefreshManager;
import org.crosswire.common.progress.JobManager; import org.crosswire.common.progress.JobManager;
@ -28,7 +29,8 @@ import de.greenrobot.event.EventBus;
BookItemHolder.class, BookItemHolder.class,
BookDownloadManager.class, BookDownloadManager.class,
BookDownloadThread.class, BookDownloadThread.class,
RefreshManager.class RefreshManager.class,
InstalledManager.class
} }
) )
public class ActivityDownloaderModule { public class ActivityDownloaderModule {

View File

@ -13,6 +13,7 @@ import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManager; import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManager;
import org.bspeice.minimalbible.activities.downloader.manager.DLProgressEvent; import org.bspeice.minimalbible.activities.downloader.manager.DLProgressEvent;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
import org.bspeice.minimalbible.activities.downloader.manager.InstalledManager;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import javax.inject.Inject; import javax.inject.Inject;
@ -36,6 +37,7 @@ public class BookItemHolder {
@Inject DownloadManager downloadManager; @Inject DownloadManager downloadManager;
@Inject BookDownloadManager bookDownloadManager; @Inject BookDownloadManager bookDownloadManager;
@Inject InstalledManager installedManager;
Book b; Book b;
@ -51,14 +53,13 @@ public class BookItemHolder {
DLProgressEvent dlProgressEvent = bookDownloadManager.getInProgressDownloadProgress(b); DLProgressEvent dlProgressEvent = bookDownloadManager.getInProgressDownloadProgress(b);
if (dlProgressEvent != null) { if (dlProgressEvent != null) {
displayProgress((int) dlProgressEvent.toCircular()); displayProgress((int) dlProgressEvent.toCircular());
} else if (downloadManager.isInstalled(b)) { } else if (installedManager.isInstalled(b)) {
displayInstalled(); displayInstalled();
} }
downloadManager.getDownloadBus().register(this); downloadManager.getDownloadBus().register(this);
} }
private void displayInstalled() { private void displayInstalled() {
isDownloaded.setImageResource(android.R.color.transparent);
isDownloaded.setImageResource(R.drawable.ic_action_cancel); isDownloaded.setImageResource(R.drawable.ic_action_cancel);
} }
@ -67,7 +68,6 @@ public class BookItemHolder {
bookDownloadManager.installBook(this.b); bookDownloadManager.installBook(this.b);
} }
@SuppressLint("")
public void onEventMainThread(DLProgressEvent event) { public void onEventMainThread(DLProgressEvent event) {
if (event.getB().getOsisID().equals(b.getOsisID())) { if (event.getB().getOsisID().equals(b.getOsisID())) {
displayProgress((int) event.toCircular()); displayProgress((int) event.toCircular());

View File

@ -8,6 +8,9 @@ import org.crosswire.common.progress.Progress;
import org.crosswire.common.progress.WorkEvent; import org.crosswire.common.progress.WorkEvent;
import org.crosswire.common.progress.WorkListener; import org.crosswire.common.progress.WorkListener;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.book.BooksEvent;
import org.crosswire.jsword.book.BooksListener;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -21,9 +24,10 @@ import de.greenrobot.event.EventBus;
/** /**
* Wrapper to convert JSword progress events to MinimalBible EventBus-based * Wrapper to convert JSword progress events to MinimalBible EventBus-based
*/ */
//TODO: Listen for installed downloads in case the download took very little time? //TODO: Make sure that jobs have the correct name
//TODO: Install indexes for Bibles
@Singleton @Singleton
public class BookDownloadManager implements WorkListener{ public class BookDownloadManager implements WorkListener, BooksListener {
/** /**
* Mapping of Job ID to the EventBus we should trigger progress on * Mapping of Job ID to the EventBus we should trigger progress on
@ -45,6 +49,7 @@ public class BookDownloadManager implements WorkListener{
inProgressDownloads = new HashMap<Book, DLProgressEvent>(); inProgressDownloads = new HashMap<Book, DLProgressEvent>();
JobManager.addWorkListener(this); JobManager.addWorkListener(this);
MinimalBible.getApplication().inject(this); MinimalBible.getApplication().inject(this);
Books.installed().addBooksListener(this);
} }
public void installBook(Book b) { public void installBook(Book b) {
@ -60,6 +65,7 @@ public class BookDownloadManager implements WorkListener{
@Override @Override
public void workProgressed(WorkEvent ev) { public void workProgressed(WorkEvent ev) {
Progress job = ev.getJob(); Progress job = ev.getJob();
Log.d("BookDownloadManager", "Download in progress: " + job.getJobID() + " - " + job.getJobName() + " " + job.getWorkDone() + "/" + job.getTotalWork());
EventBus downloadBus = downloadManager.getDownloadBus(); EventBus downloadBus = downloadManager.getDownloadBus();
if (bookMappings.containsKey(job.getJobID())) { if (bookMappings.containsKey(job.getJobID())) {
Book b = bookMappings.get(job.getJobID()); Book b = bookMappings.get(job.getJobID());
@ -95,4 +101,25 @@ public class BookDownloadManager implements WorkListener{
public void workStateChanged(WorkEvent ev) { public void workStateChanged(WorkEvent ev) {
Log.d("BookDownloadManager", ev.toString()); Log.d("BookDownloadManager", ev.toString());
} }
@Override
public void bookAdded(BooksEvent booksEvent) {
// It's possible the install finished before we received a progress event for it,
// we handle that case here.
Book b = booksEvent.getBook();
Log.d("BookDownloadManager", "Book added: " + b.getName());
if (inProgressDownloads.containsKey(b)) {
inProgressDownloads.remove(b);
}
// Not sure why, but the inProgressDownloads might not have our book,
// so we always trigger the PROGRESS_COMPLETE event.
// TODO: Make sure all books get to the inProgressDownloads
downloadManager.getDownloadBus()
.post(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b));
}
@Override
public void bookRemoved(BooksEvent booksEvent) {
// Not too worried about this just yet.
}
} }

View File

@ -55,6 +55,6 @@ public class BookDownloadThread {
* @return * @return
*/ */
public static String getJobId(Book b) { public static String getJobId(Book b) {
return "INSTALL_BOOK-" + b.getInitials().toUpperCase(); return "INSTALL_BOOK-" + b.getInitials();
} }
} }

View File

@ -4,7 +4,6 @@ import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
@ -13,26 +12,22 @@ import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.InstallException; import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.book.install.Installer;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import de.greenrobot.event.EventBus;
public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> { public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
private static final String TAG = "EventBookRefreshTask"; private static final String TAG = "EventBookRefreshTask";
// If last refresh was before the below, force an internet refresh // If last refresh was before the below, force an internet refresh
private final Long refreshAfter = System.currentTimeMillis() - 604800000L; // 1 Week in millis private final Long refreshAfter = System.currentTimeMillis() - 604800000L; // 1 Week in millis
@Inject @Inject DownloadPrefs downloadPrefs;
DownloadPrefs downloadPrefs;
@Inject DownloadManager downloadManager; @Inject DownloadManager downloadManager;
@Inject InstalledManager installedManager;
public BookRefreshTask() { public BookRefreshTask() {
MinimalBible.getApplication().inject(this); MinimalBible.getApplication().inject(this);
@ -57,9 +52,9 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
bookList.put(i, i.getBooks()); bookList.put(i, i.getBooks());
publishProgress(++index, params.length); publishProgress(++index, params.length);
} }
//TODO: Filter duplicates
// Pre-cache the DownloadManager with the list of installed books installedManager.initialize();
downloadManager.isInstalled(bookList.values().iterator().next().get(0));
EventBookList event = new EventBookList(bookList); EventBookList event = new EventBookList(bookList);
downloadManager.getDownloadBus().post(event); downloadManager.getDownloadBus().post(event);

View File

@ -5,7 +5,10 @@ import android.util.Log;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.BookList;
import org.crosswire.jsword.book.Books; import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.book.BooksEvent;
import org.crosswire.jsword.book.BooksListener;
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 org.crosswire.jsword.book.sword.SwordBookPath; import org.crosswire.jsword.book.sword.SwordBookPath;
@ -28,7 +31,7 @@ public class DownloadManager {
@Inject @Inject
protected EventBus downloadBus; protected EventBus downloadBus;
private List<Book> installedBooks;
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE, public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
BookCategory.COMMENTARY, BookCategory.DICTIONARY, BookCategory.COMMENTARY, BookCategory.DICTIONARY,
@ -83,10 +86,5 @@ public class DownloadManager {
return this.downloadBus; return this.downloadBus;
} }
public boolean isInstalled(Book b) {
if (installedBooks == null) {
installedBooks = Books.installed().getBooks();
}
return installedBooks.contains(b);
}
} }

View File

@ -0,0 +1,56 @@
package org.bspeice.minimalbible.activities.downloader.manager;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.book.BooksEvent;
import org.crosswire.jsword.book.BooksListener;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* Created by bspeice on 5/25/14.
*/
@Singleton
public class InstalledManager implements BooksListener {
@Inject DownloadManager downloadManager;
private List<Book> installedBooks;
/**
* Register our manager to receive events on Book install
* This is a relatively expensive operation,
* so we don't put it in the constructor.
*/
public void initialize() {
Books books = Books.installed();
installedBooks = books.getBooks();
books.addBooksListener(this);
}
public boolean isInstalled(Book b) {
if (installedBooks == null) {
initialize();
}
return installedBooks.contains(b);
}
@Override
public void bookAdded(BooksEvent booksEvent) {
Book b = booksEvent.getBook();
if (!installedBooks.contains(b)) {
installedBooks.add(b);
}
}
@Override
public void bookRemoved(BooksEvent booksEvent) {
Book b = booksEvent.getBook();
if (installedBooks.contains(b)) {
installedBooks.remove(b);
}
}
}