From 1a7364da86d3e94f7ad9f50e13c72cbca6ffd4d6 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 10 Jun 2014 22:35:40 -0400 Subject: [PATCH] [broken] Convert BookDownloadManager to Rx --- MinimalBible/build.gradle | 27 +++--- .../manager/BookDownloadManager.java | 16 ++-- .../downloader/manager/BookRefreshTask.java | 86 ------------------- .../downloader/manager/DownloadManager.java | 27 ------ 4 files changed, 21 insertions(+), 135 deletions(-) delete mode 100644 MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java diff --git a/MinimalBible/build.gradle b/MinimalBible/build.gradle index 5c9dde0..acfd8f7 100644 --- a/MinimalBible/build.gradle +++ b/MinimalBible/build.gradle @@ -7,7 +7,7 @@ buildscript { mavenCentral() } 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' } } @@ -19,25 +19,18 @@ repositories { dependencies { compile project(path: ':jsword-minimalbible', configuration: 'buildJSword') compile project(':appcompat_v7') - apt 'com.squareup.dagger:dagger-compiler:1.2.0' compile 'com.squareup.dagger:dagger:1.2.0' - apt 'com.jakewharton:butterknife:5.0.1' compile 'com.jakewharton:butterknife:5.0.1' - compile 'de.devland.esperandro:esperandro-api:1.1.2' apt 'de.devland.esperandro:esperandro:1.1.2' - // compile 'com.f2prateek.dart:dart:1.1.0' - compile 'com.readystatesoftware.systembartint:systembartint:1.0.3' // compile 'de.greenrobot:eventbus:2.2.0' compile 'com.netflix.rxjava:rxjava-android:0.19.0' - // Handled by appcompat // compile 'com.google.android:support-v4:r7' - // And our unit testing needs some specific stuff (and specific stuff included again) androidTestCompile 'junit:junit:4.11+' androidTestCompile 'com.jayway.awaitility:awaitility:1.6.0' @@ -46,8 +39,7 @@ dependencies { android { compileSdkVersion 19 - buildToolsVersion "19.1.0" - + buildToolsVersion '19.1.0' sourceSets { main { manifest.srcFile 'AndroidManifest.xml' @@ -71,7 +63,6 @@ android { debug.setRoot('build-types/debug') release.setRoot('build-types/release') } - packagingOptions { exclude 'META-INF/LICENSE.txt' exclude 'LICENSE.txt' @@ -81,12 +72,16 @@ android { exclude 'META-INF/NOTICE' exclude 'META-INF/services/javax.annotation.processing.Processor' } - lintOptions { abortOnError false } -} - -retrolambda { - jdk '/usr/lib/jvm/java-8-oracle' + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + defaultConfig {} + productFlavors { + } + buildTypes { + } } \ No newline at end of file diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java index 63c7b6b..0bb7951 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java @@ -12,6 +12,7 @@ import org.crosswire.jsword.book.Books; import org.crosswire.jsword.book.BooksEvent; import org.crosswire.jsword.book.BooksListener; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -19,7 +20,10 @@ import javax.inject.Inject; import javax.inject.Provider; 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 @@ -39,6 +43,8 @@ public class BookDownloadManager implements WorkListener, BooksListener { */ private Map inProgressDownloads; + private PublishSubject downloadEvents = PublishSubject.create(); + @Inject Provider dlThreadProvider; @@ -66,20 +72,19 @@ public class BookDownloadManager implements WorkListener, BooksListener { public void workProgressed(WorkEvent ev) { Progress job = ev.getJob(); Log.d("BookDownloadManager", "Download in progress: " + job.getJobID() + " - " + job.getJobName() + " " + job.getWorkDone() + "/" + job.getTotalWork()); - EventBus downloadBus = downloadManager.getDownloadBus(); if (bookMappings.containsKey(job.getJobID())) { Book b = bookMappings.get(job.getJobID()); if (job.getWorkDone() == job.getTotalWork()) { // Download is complete inProgressDownloads.remove(bookMappings.get(job.getJobID())); - downloadBus.post(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b)); + downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b)); } else { // Track the ongoing download DLProgressEvent event = new DLProgressEvent(job.getWorkDone(), job.getTotalWork(), b); 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, // 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)); + downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b)); } @Override diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java deleted file mode 100644 index 672d22a..0000000 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java +++ /dev/null @@ -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> { - 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 doInBackground(Installer... params) { - Map> bookList = new HashMap>(); - - 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); - } -} diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java index 15bd66f..c4996a8 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java @@ -11,22 +11,14 @@ import org.crosswire.jsword.book.sword.SwordBookPath; import java.io.File; import java.util.Map; -import javax.inject.Inject; import javax.inject.Singleton; -import de.greenrobot.event.EventBus; - // TODO: Listen to BookInstall events? @Singleton public class DownloadManager { private final String TAG = "DownloadManager"; - @Inject - protected EventBus downloadBus; - - - public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE, BookCategory.COMMENTARY, BookCategory.DICTIONARY, BookCategory.MAPS }; @@ -47,15 +39,6 @@ public class DownloadManager { 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 installers = getInstallers(); - return installers.values().toArray(new Installer[installers.size()]); - } - /** * Notify jSword that it needs to store files in the Android internal directory * NOTE: Android will uninstall these files if you uninstall MinimalBible. @@ -71,14 +54,4 @@ public class DownloadManager { SwordBookPath.setDownloadDir(new File(home)); 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; - } - - }