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)); + } }