From a046e9fa0aa0ec0567b02f083642d48482108977 Mon Sep 17 00:00:00 2001 From: DjBushido Date: Thu, 8 May 2014 21:39:29 -0400 Subject: [PATCH] Refactor download prefs, when to d/l over internet --- MinimalBible/AndroidManifest.xml | 1 + .../minimalbible/MinimalBibleConstants.java | 10 ---- .../activities/ActivityModules.java | 2 +- .../ActivityDownloaderModule.java | 19 +++++-- .../downloader/BookListFragment.java | 28 +++------- .../downloader/DownloadPrefsManager.java | 51 +++++++++++++++++++ .../downloader/manager/BookRefreshTask.java | 50 ++++++++++++------ 7 files changed, 110 insertions(+), 51 deletions(-) delete mode 100644 MinimalBible/src/org/bspeice/minimalbible/MinimalBibleConstants.java rename MinimalBible/src/org/bspeice/minimalbible/activities/downloader/{manager => }/ActivityDownloaderModule.java (56%) create mode 100644 MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadPrefsManager.java diff --git a/MinimalBible/AndroidManifest.xml b/MinimalBible/AndroidManifest.xml index 12288b4..cd934cb 100644 --- a/MinimalBible/AndroidManifest.xml +++ b/MinimalBible/AndroidManifest.xml @@ -8,6 +8,7 @@ android:minSdkVersion="8" android:targetSdkVersion="19" /> + > { private static final String TAG = "EventBookRefreshTask"; + // Refresh if last refresh date is after time below + private final Date refreshBefore = new Date(System.currentTimeMillis() - 604800000L); // 1 Week in millis + + @Inject protected DownloadPrefsManager prefsManager; + private EventBus downloadBus; private BookFilter filter; public BookRefreshTask(EventBus downloadBus) { this.downloadBus = downloadBus; + MinimalBible.getApplication().inject(this); } public BookRefreshTask(EventBus downloadBus, BookFilter f) { this.downloadBus = downloadBus; this.filter = f; + MinimalBible.getApplication().inject(this); } @Override @@ -41,6 +52,7 @@ public class BookRefreshTask extends AsyncTask> { if (doRefresh()) { try { i.reloadBookList(); + prefsManager.setDownloadRefreshedOn(new Date(System.currentTimeMillis())); } catch (InstallException e) { Log.e(TAG, "Error downloading books from installer: " @@ -66,12 +78,20 @@ public class BookRefreshTask extends AsyncTask> { // copy - likely something time-based, also check network state. // Fun fact - jSword handles the caching for us. - SharedPreferences prefs = MinimalBible.getAppContext() - .getSharedPreferences( - MinimalBibleConstants.DOWNLOAD_PREFS_FILE, - Context.MODE_PRIVATE); - - // Refresh if download enabled - return prefs.getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, false); + 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 prefsManager.getDownloadEnabled(); + } + + private boolean needsRefresh() { + return (prefsManager.getDownloadRefreshedOn().before(refreshBefore)); + } }