mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2024-11-14 12:08:51 -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:minSdkVersion="8"
|
||||||
android:targetSdkVersion="19" />
|
android:targetSdkVersion="19" />
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
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;
|
package org.bspeice.minimalbible.activities;
|
||||||
|
|
||||||
import org.bspeice.minimalbible.activities.downloader.manager.ActivityDownloaderModule;
|
import org.bspeice.minimalbible.activities.downloader.ActivityDownloaderModule;
|
||||||
|
|
||||||
import dagger.Module;
|
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;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
@ -14,11 +19,14 @@ import de.greenrobot.event.EventBus;
|
|||||||
@Module(
|
@Module(
|
||||||
injects = {
|
injects = {
|
||||||
BookListFragment.class,
|
BookListFragment.class,
|
||||||
DownloadManager.class
|
DownloadManager.class,
|
||||||
|
BookRefreshTask.class
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public class ActivityDownloaderModule {
|
public class ActivityDownloaderModule {
|
||||||
|
|
||||||
|
private final Context ctx = MinimalBible.getAppContext();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide a Singleton DownloadManager for injection
|
* Provide a Singleton DownloadManager for injection
|
||||||
* Note that we need to annotate Singleton here, only annotating on the
|
* Note that we need to annotate Singleton here, only annotating on the
|
||||||
@ -34,4 +42,9 @@ public class ActivityDownloaderModule {
|
|||||||
EventBus provideBus() {
|
EventBus provideBus() {
|
||||||
return new EventBus();
|
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.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
@ -19,7 +17,6 @@ import android.widget.Toast;
|
|||||||
import com.readystatesoftware.systembartint.SystemBarTintManager;
|
import com.readystatesoftware.systembartint.SystemBarTintManager;
|
||||||
|
|
||||||
import org.bspeice.minimalbible.MinimalBible;
|
import org.bspeice.minimalbible.MinimalBible;
|
||||||
import org.bspeice.minimalbible.MinimalBibleConstants;
|
|
||||||
import org.bspeice.minimalbible.R;
|
import org.bspeice.minimalbible.R;
|
||||||
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
|
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
|
||||||
import org.bspeice.minimalbible.activities.downloader.manager.EventBookList;
|
import org.bspeice.minimalbible.activities.downloader.manager.EventBookList;
|
||||||
@ -50,8 +47,8 @@ public class BookListFragment extends Fragment {
|
|||||||
@InjectView(R.id.lst_download_available)
|
@InjectView(R.id.lst_download_available)
|
||||||
ListView downloadsAvailable;
|
ListView downloadsAvailable;
|
||||||
|
|
||||||
@Inject
|
@Inject DownloadManager downloadManager;
|
||||||
DownloadManager downloadManager;
|
@Inject DownloadPrefsManager prefsManager;
|
||||||
|
|
||||||
private ProgressDialog refreshDialog;
|
private ProgressDialog refreshDialog;
|
||||||
|
|
||||||
@ -92,12 +89,7 @@ public class BookListFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void displayModules() {
|
public void displayModules() {
|
||||||
SharedPreferences prefs = getActivity()
|
boolean dialogDisplayed = prefsManager.getShowedDownloadDialog();
|
||||||
.getSharedPreferences(
|
|
||||||
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
|
||||||
Context.MODE_PRIVATE);
|
|
||||||
boolean dialogDisplayed = prefs.getBoolean(
|
|
||||||
MinimalBibleConstants.KEY_SHOWED_DOWNLOAD_DIALOG, false);
|
|
||||||
|
|
||||||
if (!dialogDisplayed) {
|
if (!dialogDisplayed) {
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||||
@ -162,18 +154,12 @@ public class BookListFragment extends Fragment {
|
|||||||
DialogInterface.OnClickListener {
|
DialogInterface.OnClickListener {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
SharedPreferences prefs = getActivity().getSharedPreferences(
|
prefsManager.setShowedDownloadDialog(true);
|
||||||
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
|
||||||
Context.MODE_PRIVATE);
|
|
||||||
prefs.edit().putBoolean(MinimalBibleConstants.KEY_SHOWED_DOWNLOAD_DIALOG, true)
|
|
||||||
.commit();
|
|
||||||
|
|
||||||
switch (which) {
|
switch (which) {
|
||||||
case DialogInterface.BUTTON_POSITIVE:
|
case DialogInterface.BUTTON_POSITIVE:
|
||||||
// Clicked ready to continue - allow downloading in the future
|
// Clicked ready to continue - allow downloading in the future
|
||||||
prefs.edit()
|
prefsManager.setDownloadEnabled(true);
|
||||||
.putBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED,
|
|
||||||
true).commit();
|
|
||||||
|
|
||||||
// And warn them that it has been enabled in the future.
|
// And warn them that it has been enabled in the future.
|
||||||
Toast.makeText(getActivity(),
|
Toast.makeText(getActivity(),
|
||||||
@ -184,9 +170,7 @@ public class BookListFragment extends Fragment {
|
|||||||
|
|
||||||
case DialogInterface.BUTTON_NEGATIVE:
|
case DialogInterface.BUTTON_NEGATIVE:
|
||||||
// Clicked to not download - Permanently disable downloading
|
// Clicked to not download - Permanently disable downloading
|
||||||
prefs.edit()
|
prefsManager.setDownloadEnabled(false);
|
||||||
.putBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED,
|
|
||||||
false).commit();
|
|
||||||
Toast.makeText(getActivity(),
|
Toast.makeText(getActivity(),
|
||||||
"Disabling downloading. Re-enable it in settings.",
|
"Disabling downloading. Re-enable it in settings.",
|
||||||
Toast.LENGTH_SHORT).show();
|
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;
|
package org.bspeice.minimalbible.activities.downloader.manager;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import android.content.Context;
|
||||||
import java.util.List;
|
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.MinimalBible;
|
||||||
import org.bspeice.minimalbible.MinimalBibleConstants;
|
import org.bspeice.minimalbible.activities.downloader.DownloadPrefsManager;
|
||||||
import org.crosswire.jsword.book.Book;
|
import org.crosswire.jsword.book.Book;
|
||||||
import org.crosswire.jsword.book.BookFilter;
|
import org.crosswire.jsword.book.BookFilter;
|
||||||
import org.crosswire.jsword.book.install.InstallException;
|
import org.crosswire.jsword.book.install.InstallException;
|
||||||
import org.crosswire.jsword.book.install.Installer;
|
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 javax.inject.Inject;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.os.AsyncTask;
|
import de.greenrobot.event.EventBus;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
||||||
private static final String TAG = "EventBookRefreshTask";
|
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 EventBus downloadBus;
|
||||||
private BookFilter filter;
|
private BookFilter filter;
|
||||||
|
|
||||||
public BookRefreshTask(EventBus downloadBus) {
|
public BookRefreshTask(EventBus downloadBus) {
|
||||||
this.downloadBus = downloadBus;
|
this.downloadBus = downloadBus;
|
||||||
|
MinimalBible.getApplication().inject(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BookRefreshTask(EventBus downloadBus, BookFilter f) {
|
public BookRefreshTask(EventBus downloadBus, BookFilter f) {
|
||||||
this.downloadBus = downloadBus;
|
this.downloadBus = downloadBus;
|
||||||
this.filter = f;
|
this.filter = f;
|
||||||
|
MinimalBible.getApplication().inject(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -41,6 +52,7 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
|||||||
if (doRefresh()) {
|
if (doRefresh()) {
|
||||||
try {
|
try {
|
||||||
i.reloadBookList();
|
i.reloadBookList();
|
||||||
|
prefsManager.setDownloadRefreshedOn(new Date(System.currentTimeMillis()));
|
||||||
} catch (InstallException e) {
|
} catch (InstallException e) {
|
||||||
Log.e(TAG,
|
Log.e(TAG,
|
||||||
"Error downloading books from installer: "
|
"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.
|
// copy - likely something time-based, also check network state.
|
||||||
// Fun fact - jSword handles the caching for us.
|
// Fun fact - jSword handles the caching for us.
|
||||||
|
|
||||||
SharedPreferences prefs = MinimalBible.getAppContext()
|
return (isWifi() && downloadEnabled() && needsRefresh());
|
||||||
.getSharedPreferences(
|
}
|
||||||
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
|
||||||
Context.MODE_PRIVATE);
|
|
||||||
|
|
||||||
// Refresh if download enabled
|
private boolean isWifi() {
|
||||||
return prefs.getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, false);
|
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