From f5e523417a78920eeb9877c1d15032cf6004df60 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Fri, 18 Apr 2014 00:53:14 -0400 Subject: [PATCH] Oh man is it ugly, but I can download module lists. --- MinimalBible/AndroidManifest.xml | 4 +- .../bspeice/minimalbible/MinimalBible.java | 18 ++++ .../downloader/DownloadActivity.java | 93 ++++++++++++++----- .../downloader/DownloadManager.java | 77 +++++++++++++++ .../downloader/DownloadNavDrawerFragment.java | 2 +- 5 files changed, 169 insertions(+), 25 deletions(-) create mode 100644 MinimalBible/src/org/bspeice/minimalbible/MinimalBible.java create mode 100644 MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java diff --git a/MinimalBible/AndroidManifest.xml b/MinimalBible/AndroidManifest.xml index f42e055..e82455a 100644 --- a/MinimalBible/AndroidManifest.xml +++ b/MinimalBible/AndroidManifest.xml @@ -7,12 +7,14 @@ + + android:theme="@style/AppTheme" + android:name="org.bspeice.minimalbible.MinimalBible" > diff --git a/MinimalBible/src/org/bspeice/minimalbible/MinimalBible.java b/MinimalBible/src/org/bspeice/minimalbible/MinimalBible.java new file mode 100644 index 0000000..9d0705e --- /dev/null +++ b/MinimalBible/src/org/bspeice/minimalbible/MinimalBible.java @@ -0,0 +1,18 @@ +package org.bspeice.minimalbible; + +import android.app.Application; +import android.content.Context; + +public class MinimalBible extends Application { + + private static MinimalBible instance; + + public MinimalBible() { + instance = this; + } + + public static Context getAppContext() { + return instance; + } + +} diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java index d636851..fdc08ac 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java @@ -1,12 +1,17 @@ package org.bspeice.minimalbible.activities.downloader; +import java.util.List; + import org.bspeice.minimalbible.MinimalBibleConstants; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activities.BaseActivity; import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment; +import org.bspeice.minimalbible.activities.downloader.DownloadManager.BookRefreshListener; +import org.crosswire.jsword.book.Book; import android.app.Activity; import android.app.AlertDialog; +import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; @@ -14,6 +19,7 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -25,6 +31,8 @@ import android.widget.Toast; public class DownloadActivity extends BaseActivity implements BaseNavigationDrawerFragment.NavigationDrawerCallbacks { + private ProgressDialog refreshDialog; + /** * Fragment managing the behaviors, interactions and presentation of the * navigation drawer. @@ -49,6 +57,10 @@ public class DownloadActivity extends BaseActivity implements // Set up the drawer. mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); + + // Refresh our modules - prompts user if they do actually want to + // connect to the internet + doRefreshModules(); } @Override @@ -150,47 +162,82 @@ public class DownloadActivity extends BaseActivity implements .getInt(ARG_SECTION_NUMBER)); } } - - private void displayInternetWarning() { - SharedPreferences prefs = getSharedPreferences(MinimalBibleConstants.DOWNLOAD_PREFS_FILE, MODE_PRIVATE); - - // If downloading has not been enabled, or user has permanently disabled downloading, WARN THEM! - if (!prefs.getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, false) || prefs.getBoolean(MinimalBibleConstants.KEY_PERM_DISABLE_DOWNLOAD, false)) { + + private void doRefreshModules() { + SharedPreferences prefs = getSharedPreferences( + MinimalBibleConstants.DOWNLOAD_PREFS_FILE, MODE_PRIVATE); + + // If downloading has not been enabled, or user has permanently disabled + // downloading, WARN THEM! + if (!prefs + .getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, false) + || prefs.getBoolean( + MinimalBibleConstants.KEY_PERM_DISABLE_DOWNLOAD, false)) { AlertDialog.Builder builder = new AlertDialog.Builder(this); DownloadDialogListener dialogListener = new DownloadDialogListener(); - builder.setMessage("About to contact servers to download content. Continue?") - .setPositiveButton("Yes", dialogListener).setNegativeButton("No", dialogListener) - .setCancelable(false).show(); + builder.setMessage( + "About to contact servers to download content. Continue?") + .setPositiveButton("Yes", dialogListener) + .setNegativeButton("No", dialogListener) + .setCancelable(false).show(); } else { - downloadModules(); + refreshModules(); } } - - private void downloadModules() { - + + private void refreshModules() { + // TODO: Discover if we need to refresh over Internet, or use a cached + // copy + // Fun fact - jSword handles the caching for us. + ProgressDialog refreshDialog = new ProgressDialog(this); + refreshDialog.setMessage("Refreshing available modules..."); + refreshDialog.setCancelable(false); + refreshDialog.show(); + DownloadManager dm = new DownloadManager(); + dm.fetchAvailableBooks(true, new DlBookRefreshListener(refreshDialog)); } - - private class DownloadDialogListener implements DialogInterface.OnClickListener { + + private class DownloadDialogListener implements + DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { - switch (which){ + switch (which) { case DialogInterface.BUTTON_POSITIVE: // Clicked ready to continue - allow downloading in the future - SharedPreferences prefs = getSharedPreferences(MinimalBibleConstants.DOWNLOAD_PREFS_FILE, MODE_PRIVATE); - prefs.edit().putBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, true).commit(); - + SharedPreferences prefs = getSharedPreferences( + MinimalBibleConstants.DOWNLOAD_PREFS_FILE, MODE_PRIVATE); + prefs.edit() + .putBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, + true).commit(); + // And warn them that it has been enabled in the future. Toast.makeText(DownloadActivity.this, - "Downloading now enabled. Disable in settings.", Toast.LENGTH_SHORT).show(); - downloadModules(); + "Downloading now enabled. Disable in settings.", + Toast.LENGTH_SHORT).show(); + refreshModules(); break; - + case DialogInterface.BUTTON_NEGATIVE: // Not going to continue, still show what has // already been downloaded. break; } - + + } + } + + private class DlBookRefreshListener implements BookRefreshListener { + // TODO: Figure out why I need to pass in the ProgressDialog, and can't cancel it from onRefreshComplete. + ProgressDialog dl; + public DlBookRefreshListener(ProgressDialog dl) { + this.dl = dl; + } + @Override + public void onRefreshComplete(List results) { + dl.cancel(); + for (Book b : results) { + Log.d("DlBookRefreshListener", b.getName()); + } } } diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java new file mode 100644 index 0000000..43e8d16 --- /dev/null +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java @@ -0,0 +1,77 @@ +package org.bspeice.minimalbible.activities.downloader; + +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import org.bspeice.minimalbible.MinimalBible; +import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.install.InstallException; +import org.crosswire.jsword.book.install.InstallManager; +import org.crosswire.jsword.book.install.Installer; + +import android.os.AsyncTask; +import android.util.Log; + +public class DownloadManager { + + private final String TAG = "DownloadManager"; + + public DownloadManager() { + setDownloadDir(); + } + + public InstallerReloadTask fetchAvailableBooks(boolean forceRefresh, BookRefreshListener bookRefreshListener) { + + Map installers = getInstallers(); + + return (InstallerReloadTask) + new InstallerReloadTask(bookRefreshListener).execute(installers.values().toArray(new Installer[installers.size()])); + } + + public Map getInstallers() { + return new InstallManager().getInstallers(); + } + + private void setDownloadDir() { + // We need to set the download directory for jSword to stick with Android. + String home = MinimalBible.getAppContext().getFilesDir().toString(); + Log.d(TAG, "Setting jsword.home to: " + home); + System.setProperty("jsword.home", home); + } + + public class InstallerReloadTask extends AsyncTask> { + private BookRefreshListener listener; + + public InstallerReloadTask(BookRefreshListener listener) { + this.listener = listener; + } + + @Override + protected List doInBackground(Installer... params) { + List books = new LinkedList(); + for (Installer i: params) { + try { + i.reloadBookList(); + } catch (InstallException e) { + Log.e(TAG, "Error downloading books from installer: " + i.toString(), e); + } + books.addAll(i.getBooks()); + } + + return books; + } + + @Override + protected void onPostExecute(List result) { + super.onPostExecute(result); + listener.onRefreshComplete(result); + } + } + + public interface BookRefreshListener { + public void onRefreshComplete(List results); + } + +} diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java index c540fd2..d56a867 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java @@ -37,7 +37,7 @@ public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment { for (int i = 0; i < displayCategories.length; i++) { sCategories[i] = displayCategories[i].toString(); } - + mDrawerListView.setAdapter(new ArrayAdapter(getActionBar() .getThemedContext(), android.R.layout.simple_list_item_1, android.R.id.text1, sCategories));