mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2024-12-22 06:38:28 -05:00
Refactor download prefs, when to d/l over internet
This commit is contained in:
parent
1530364abc
commit
a046e9fa0a
@ -8,6 +8,7 @@
|
||||
android:minSdkVersion="8"
|
||||
android:targetSdkVersion="19" />
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
@ -1,10 +0,0 @@
|
||||
package org.bspeice.minimalbible;
|
||||
|
||||
public class MinimalBibleConstants {
|
||||
|
||||
public static final String DOWNLOAD_PREFS_FILE = "DOWNLOADER_PREFERENCES";
|
||||
|
||||
public static final String KEY_DOWNLOAD_ENABLED = "HAS_ENABLED_DOWNLOAD";
|
||||
public static final String KEY_SHOWED_DOWNLOAD_DIALOG = "SHOWED_DOWNLOAD_DIALOG";
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package org.bspeice.minimalbible.activities;
|
||||
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.ActivityDownloaderModule;
|
||||
import org.bspeice.minimalbible.activities.downloader.ActivityDownloaderModule;
|
||||
|
||||
import dagger.Module;
|
||||
|
||||
|
@ -1,6 +1,11 @@
|
||||
package org.bspeice.minimalbible.activities.downloader.manager;
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
import org.bspeice.minimalbible.activities.downloader.BookListFragment;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
||||
import org.bspeice.minimalbible.MinimalBible;
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.BookRefreshTask;
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@ -14,11 +19,14 @@ import de.greenrobot.event.EventBus;
|
||||
@Module(
|
||||
injects = {
|
||||
BookListFragment.class,
|
||||
DownloadManager.class
|
||||
DownloadManager.class,
|
||||
BookRefreshTask.class
|
||||
}
|
||||
)
|
||||
public class ActivityDownloaderModule {
|
||||
|
||||
private final Context ctx = MinimalBible.getAppContext();
|
||||
|
||||
/**
|
||||
* Provide a Singleton DownloadManager for injection
|
||||
* Note that we need to annotate Singleton here, only annotating on the
|
||||
@ -34,4 +42,9 @@ public class ActivityDownloaderModule {
|
||||
EventBus provideBus() {
|
||||
return new EventBus();
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
DownloadPrefsManager providePrefsManager() {
|
||||
return new DownloadPrefsManager(ctx);
|
||||
}
|
||||
}
|
@ -3,9 +3,7 @@ package org.bspeice.minimalbible.activities.downloader;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
@ -19,7 +17,6 @@ import android.widget.Toast;
|
||||
import com.readystatesoftware.systembartint.SystemBarTintManager;
|
||||
|
||||
import org.bspeice.minimalbible.MinimalBible;
|
||||
import org.bspeice.minimalbible.MinimalBibleConstants;
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.EventBookList;
|
||||
@ -50,8 +47,8 @@ public class BookListFragment extends Fragment {
|
||||
@InjectView(R.id.lst_download_available)
|
||||
ListView downloadsAvailable;
|
||||
|
||||
@Inject
|
||||
DownloadManager downloadManager;
|
||||
@Inject DownloadManager downloadManager;
|
||||
@Inject DownloadPrefsManager prefsManager;
|
||||
|
||||
private ProgressDialog refreshDialog;
|
||||
|
||||
@ -92,12 +89,7 @@ public class BookListFragment extends Fragment {
|
||||
}
|
||||
|
||||
public void displayModules() {
|
||||
SharedPreferences prefs = getActivity()
|
||||
.getSharedPreferences(
|
||||
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
||||
Context.MODE_PRIVATE);
|
||||
boolean dialogDisplayed = prefs.getBoolean(
|
||||
MinimalBibleConstants.KEY_SHOWED_DOWNLOAD_DIALOG, false);
|
||||
boolean dialogDisplayed = prefsManager.getShowedDownloadDialog();
|
||||
|
||||
if (!dialogDisplayed) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
@ -162,18 +154,12 @@ public class BookListFragment extends Fragment {
|
||||
DialogInterface.OnClickListener {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
SharedPreferences prefs = getActivity().getSharedPreferences(
|
||||
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
||||
Context.MODE_PRIVATE);
|
||||
prefs.edit().putBoolean(MinimalBibleConstants.KEY_SHOWED_DOWNLOAD_DIALOG, true)
|
||||
.commit();
|
||||
prefsManager.setShowedDownloadDialog(true);
|
||||
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
// Clicked ready to continue - allow downloading in the future
|
||||
prefs.edit()
|
||||
.putBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED,
|
||||
true).commit();
|
||||
prefsManager.setDownloadEnabled(true);
|
||||
|
||||
// And warn them that it has been enabled in the future.
|
||||
Toast.makeText(getActivity(),
|
||||
@ -184,9 +170,7 @@ public class BookListFragment extends Fragment {
|
||||
|
||||
case DialogInterface.BUTTON_NEGATIVE:
|
||||
// Clicked to not download - Permanently disable downloading
|
||||
prefs.edit()
|
||||
.putBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED,
|
||||
false).commit();
|
||||
prefsManager.setDownloadEnabled(false);
|
||||
Toast.makeText(getActivity(),
|
||||
"Disabling downloading. Re-enable it in settings.",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
|
@ -0,0 +1,51 @@
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Created by Bradlee Speice on 5/8/2014.
|
||||
*/
|
||||
@Singleton
|
||||
public class DownloadPrefsManager {
|
||||
private final SharedPreferences prefs;
|
||||
|
||||
public static final String DOWNLOAD_PREFS_FILE = "DOWNLOADER_PREFERENCES";
|
||||
|
||||
public static final String KEY_DOWNLOAD_ENABLED = "HAS_ENABLED_DOWNLOAD";
|
||||
public static final String KEY_SHOWED_DOWNLOAD_DIALOG = "SHOWED_DOWNLOAD_DIALOG";
|
||||
public static final String KEY_DOWNLOAD_REFRESHED_ON = "DOWNLOAD_REFRESHED_ON";
|
||||
|
||||
public DownloadPrefsManager(Context ctx) {
|
||||
prefs = ctx.getSharedPreferences(DOWNLOAD_PREFS_FILE, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
public boolean getDownloadEnabled() {
|
||||
return prefs.getBoolean(KEY_DOWNLOAD_ENABLED, false);
|
||||
}
|
||||
|
||||
public void setDownloadEnabled(boolean val) {
|
||||
prefs.edit().putBoolean(KEY_DOWNLOAD_ENABLED, val).commit();
|
||||
}
|
||||
|
||||
public boolean getShowedDownloadDialog() {
|
||||
return prefs.getBoolean(KEY_SHOWED_DOWNLOAD_DIALOG, false);
|
||||
}
|
||||
|
||||
public void setShowedDownloadDialog(boolean val) {
|
||||
prefs.edit().putBoolean(KEY_SHOWED_DOWNLOAD_DIALOG, val).commit();
|
||||
}
|
||||
|
||||
public Date getDownloadRefreshedOn() {
|
||||
return new Date(prefs.getLong(KEY_DOWNLOAD_REFRESHED_ON, 0));
|
||||
}
|
||||
|
||||
public void setDownloadRefreshedOn(Date d) {
|
||||
prefs.edit().putLong(KEY_DOWNLOAD_REFRESHED_ON, d.getTime()).commit();
|
||||
}
|
||||
|
||||
}
|
@ -1,35 +1,46 @@
|
||||
package org.bspeice.minimalbible.activities.downloader.manager;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
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.MinimalBibleConstants;
|
||||
import org.bspeice.minimalbible.activities.downloader.DownloadPrefsManager;
|
||||
import org.crosswire.jsword.book.Book;
|
||||
import org.crosswire.jsword.book.BookFilter;
|
||||
import org.crosswire.jsword.book.install.InstallException;
|
||||
import org.crosswire.jsword.book.install.Installer;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
||||
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<Installer, Integer, List<Book>> {
|
||||
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<Installer, Integer, List<Book>> {
|
||||
// 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);
|
||||
return (isWifi() && downloadEnabled() && needsRefresh());
|
||||
}
|
||||
|
||||
// Refresh if download enabled
|
||||
return prefs.getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, false);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user