mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2024-11-16 04:58:26 -05:00
[broken] Convert BookDownloadManager to Rx
This commit is contained in:
parent
ca1ccd9942
commit
1a7364da86
@ -7,7 +7,7 @@ buildscript {
|
|||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
|
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
|
||||||
classpath 'me.tatarka:gradle-retrolambda:1.3.2'
|
classpath 'me.tatarka:gradle-retrolambda:1.3.2'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -19,25 +19,18 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
compile project(path: ':jsword-minimalbible', configuration: 'buildJSword')
|
compile project(path: ':jsword-minimalbible', configuration: 'buildJSword')
|
||||||
compile project(':appcompat_v7')
|
compile project(':appcompat_v7')
|
||||||
|
|
||||||
apt 'com.squareup.dagger:dagger-compiler:1.2.0'
|
apt 'com.squareup.dagger:dagger-compiler:1.2.0'
|
||||||
compile 'com.squareup.dagger:dagger:1.2.0'
|
compile 'com.squareup.dagger:dagger:1.2.0'
|
||||||
|
|
||||||
apt 'com.jakewharton:butterknife:5.0.1'
|
apt 'com.jakewharton:butterknife:5.0.1'
|
||||||
compile 'com.jakewharton:butterknife:5.0.1'
|
compile 'com.jakewharton:butterknife:5.0.1'
|
||||||
|
|
||||||
compile 'de.devland.esperandro:esperandro-api:1.1.2'
|
compile 'de.devland.esperandro:esperandro-api:1.1.2'
|
||||||
apt 'de.devland.esperandro:esperandro:1.1.2'
|
apt 'de.devland.esperandro:esperandro:1.1.2'
|
||||||
|
|
||||||
// compile 'com.f2prateek.dart:dart:1.1.0'
|
// compile 'com.f2prateek.dart:dart:1.1.0'
|
||||||
|
|
||||||
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
|
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
|
||||||
// compile 'de.greenrobot:eventbus:2.2.0'
|
// compile 'de.greenrobot:eventbus:2.2.0'
|
||||||
compile 'com.netflix.rxjava:rxjava-android:0.19.0'
|
compile 'com.netflix.rxjava:rxjava-android:0.19.0'
|
||||||
|
|
||||||
// Handled by appcompat
|
// Handled by appcompat
|
||||||
// compile 'com.google.android:support-v4:r7'
|
// compile 'com.google.android:support-v4:r7'
|
||||||
|
|
||||||
// And our unit testing needs some specific stuff (and specific stuff included again)
|
// And our unit testing needs some specific stuff (and specific stuff included again)
|
||||||
androidTestCompile 'junit:junit:4.11+'
|
androidTestCompile 'junit:junit:4.11+'
|
||||||
androidTestCompile 'com.jayway.awaitility:awaitility:1.6.0'
|
androidTestCompile 'com.jayway.awaitility:awaitility:1.6.0'
|
||||||
@ -46,8 +39,7 @@ dependencies {
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 19
|
compileSdkVersion 19
|
||||||
buildToolsVersion "19.1.0"
|
buildToolsVersion '19.1.0'
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
main {
|
main {
|
||||||
manifest.srcFile 'AndroidManifest.xml'
|
manifest.srcFile 'AndroidManifest.xml'
|
||||||
@ -71,7 +63,6 @@ android {
|
|||||||
debug.setRoot('build-types/debug')
|
debug.setRoot('build-types/debug')
|
||||||
release.setRoot('build-types/release')
|
release.setRoot('build-types/release')
|
||||||
}
|
}
|
||||||
|
|
||||||
packagingOptions {
|
packagingOptions {
|
||||||
exclude 'META-INF/LICENSE.txt'
|
exclude 'META-INF/LICENSE.txt'
|
||||||
exclude 'LICENSE.txt'
|
exclude 'LICENSE.txt'
|
||||||
@ -81,12 +72,16 @@ android {
|
|||||||
exclude 'META-INF/NOTICE'
|
exclude 'META-INF/NOTICE'
|
||||||
exclude 'META-INF/services/javax.annotation.processing.Processor'
|
exclude 'META-INF/services/javax.annotation.processing.Processor'
|
||||||
}
|
}
|
||||||
|
|
||||||
lintOptions {
|
lintOptions {
|
||||||
abortOnError false
|
abortOnError false
|
||||||
}
|
}
|
||||||
|
compileOptions {
|
||||||
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
defaultConfig {}
|
||||||
|
productFlavors {
|
||||||
|
}
|
||||||
|
buildTypes {
|
||||||
}
|
}
|
||||||
|
|
||||||
retrolambda {
|
|
||||||
jdk '/usr/lib/jvm/java-8-oracle'
|
|
||||||
}
|
}
|
@ -12,6 +12,7 @@ import org.crosswire.jsword.book.Books;
|
|||||||
import org.crosswire.jsword.book.BooksEvent;
|
import org.crosswire.jsword.book.BooksEvent;
|
||||||
import org.crosswire.jsword.book.BooksListener;
|
import org.crosswire.jsword.book.BooksListener;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -19,7 +20,10 @@ import javax.inject.Inject;
|
|||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import de.greenrobot.event.EventBus;
|
import rx.Observable;
|
||||||
|
import rx.subjects.PublishSubject;
|
||||||
|
import rx.subjects.ReplaySubject;
|
||||||
|
import rx.subjects.Subject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper to convert JSword progress events to MinimalBible EventBus-based
|
* Wrapper to convert JSword progress events to MinimalBible EventBus-based
|
||||||
@ -39,6 +43,8 @@ public class BookDownloadManager implements WorkListener, BooksListener {
|
|||||||
*/
|
*/
|
||||||
private Map<Book, DLProgressEvent> inProgressDownloads;
|
private Map<Book, DLProgressEvent> inProgressDownloads;
|
||||||
|
|
||||||
|
private PublishSubject<DLProgressEvent> downloadEvents = PublishSubject.create();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
Provider<BookDownloadThread> dlThreadProvider;
|
Provider<BookDownloadThread> dlThreadProvider;
|
||||||
|
|
||||||
@ -66,20 +72,19 @@ public class BookDownloadManager implements WorkListener, BooksListener {
|
|||||||
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());
|
Log.d("BookDownloadManager", "Download in progress: " + job.getJobID() + " - " + job.getJobName() + " " + job.getWorkDone() + "/" + job.getTotalWork());
|
||||||
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());
|
||||||
|
|
||||||
if (job.getWorkDone() == job.getTotalWork()) {
|
if (job.getWorkDone() == job.getTotalWork()) {
|
||||||
// Download is complete
|
// Download is complete
|
||||||
inProgressDownloads.remove(bookMappings.get(job.getJobID()));
|
inProgressDownloads.remove(bookMappings.get(job.getJobID()));
|
||||||
downloadBus.post(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b));
|
downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b));
|
||||||
} else {
|
} else {
|
||||||
// Track the ongoing download
|
// Track the ongoing download
|
||||||
DLProgressEvent event = new DLProgressEvent(job.getWorkDone(),
|
DLProgressEvent event = new DLProgressEvent(job.getWorkDone(),
|
||||||
job.getTotalWork(), b);
|
job.getTotalWork(), b);
|
||||||
inProgressDownloads.put(b, event);
|
inProgressDownloads.put(b, event);
|
||||||
downloadBus.post(event);
|
downloadEvents.onNext(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,8 +119,7 @@ public class BookDownloadManager implements WorkListener, BooksListener {
|
|||||||
// Not sure why, but the inProgressDownloads might not have our book,
|
// Not sure why, but the inProgressDownloads might not have our book,
|
||||||
// so we always trigger the PROGRESS_COMPLETE event.
|
// so we always trigger the PROGRESS_COMPLETE event.
|
||||||
// TODO: Make sure all books get to the inProgressDownloads
|
// TODO: Make sure all books get to the inProgressDownloads
|
||||||
downloadManager.getDownloadBus()
|
downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b));
|
||||||
.post(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,86 +0,0 @@
|
|||||||
package org.bspeice.minimalbible.activities.downloader.manager;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.net.ConnectivityManager;
|
|
||||||
import android.net.NetworkInfo;
|
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import org.bspeice.minimalbible.MinimalBible;
|
|
||||||
import org.bspeice.minimalbible.activities.downloader.DownloadPrefs;
|
|
||||||
import org.crosswire.jsword.book.Book;
|
|
||||||
import org.crosswire.jsword.book.install.InstallException;
|
|
||||||
import org.crosswire.jsword.book.install.Installer;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
|
||||||
private static final String TAG = "EventBookRefreshTask";
|
|
||||||
|
|
||||||
// If last refresh was before the below, force an internet refresh
|
|
||||||
private final Long refreshAfter = System.currentTimeMillis() - 604800000L; // 1 Week in millis
|
|
||||||
|
|
||||||
@Inject DownloadPrefs downloadPrefs;
|
|
||||||
|
|
||||||
@Inject DownloadManager downloadManager;
|
|
||||||
@Inject InstalledManager installedManager;
|
|
||||||
|
|
||||||
public BookRefreshTask() {
|
|
||||||
MinimalBible.getApplication().inject(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected List<Book> doInBackground(Installer... params) {
|
|
||||||
Map<Installer, List<Book>> bookList = new HashMap<Installer, List<Book>>();
|
|
||||||
|
|
||||||
int index = 0;
|
|
||||||
for (Installer i : params) {
|
|
||||||
if (doRefresh()) {
|
|
||||||
try {
|
|
||||||
i.reloadBookList();
|
|
||||||
downloadPrefs.downloadRefreshedOn(System.currentTimeMillis());
|
|
||||||
} catch (InstallException e) {
|
|
||||||
Log.e(TAG,
|
|
||||||
"Error downloading books from installer: "
|
|
||||||
+ i.toString(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bookList.put(i, i.getBooks());
|
|
||||||
publishProgress(++index, params.length);
|
|
||||||
}
|
|
||||||
//TODO: Filter duplicates
|
|
||||||
|
|
||||||
installedManager.initialize();
|
|
||||||
EventBookList event = new EventBookList(bookList);
|
|
||||||
downloadManager.getDownloadBus().post(event);
|
|
||||||
|
|
||||||
return event.getBookList();
|
|
||||||
}
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
return (isWifi() && downloadEnabled() && needsRefresh());
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isWifi() {
|
|
||||||
ConnectivityManager mgr = (ConnectivityManager)MinimalBible.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
||||||
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
||||||
return networkInfo.isConnected();
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean downloadEnabled() {
|
|
||||||
return downloadPrefs.hasEnabledDownload();
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean needsRefresh() {
|
|
||||||
return (downloadPrefs.downloadRefreshedOn() > refreshAfter);
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,22 +11,14 @@ import org.crosswire.jsword.book.sword.SwordBookPath;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import de.greenrobot.event.EventBus;
|
|
||||||
|
|
||||||
// TODO: Listen to BookInstall events?
|
// TODO: Listen to BookInstall events?
|
||||||
@Singleton
|
@Singleton
|
||||||
public class DownloadManager {
|
public class DownloadManager {
|
||||||
|
|
||||||
private final String TAG = "DownloadManager";
|
private final String TAG = "DownloadManager";
|
||||||
|
|
||||||
@Inject
|
|
||||||
protected 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,
|
||||||
BookCategory.MAPS };
|
BookCategory.MAPS };
|
||||||
@ -47,15 +39,6 @@ public class DownloadManager {
|
|||||||
return new InstallManager().getInstallers();
|
return new InstallManager().getInstallers();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper method to transform the installers map to an array
|
|
||||||
* @return Array with all available {@link org.crosswire.jsword.book.install.Installer} objects
|
|
||||||
*/
|
|
||||||
public Installer[] getInstallersArray() {
|
|
||||||
Map<String, Installer> installers = getInstallers();
|
|
||||||
return installers.values().toArray(new Installer[installers.size()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notify jSword that it needs to store files in the Android internal directory
|
* Notify jSword that it needs to store files in the Android internal directory
|
||||||
* NOTE: Android will uninstall these files if you uninstall MinimalBible.
|
* NOTE: Android will uninstall these files if you uninstall MinimalBible.
|
||||||
@ -71,14 +54,4 @@ public class DownloadManager {
|
|||||||
SwordBookPath.setDownloadDir(new File(home));
|
SwordBookPath.setDownloadDir(new File(home));
|
||||||
Log.d(TAG, "Sword download path: " + SwordBookPath.getSwordDownloadDir());
|
Log.d(TAG, "Sword download path: " + SwordBookPath.getSwordDownloadDir());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current download bus
|
|
||||||
* Used to broker refresh events, and ongoing download events
|
|
||||||
*/
|
|
||||||
public EventBus getDownloadBus() {
|
|
||||||
return this.downloadBus;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user