Split out DL fragments, give them D/L responsibility

This commit is contained in:
Bradlee Speice 2014-04-21 22:28:49 -04:00
parent 4c3a2ba5b5
commit 4dc920186f
3 changed files with 175 additions and 157 deletions

View File

@ -0,0 +1,150 @@
package org.bspeice.minimalbible.activities.downloader;
import java.util.List;
import org.bspeice.minimalbible.MinimalBibleConstants;
import org.bspeice.minimalbible.R;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookCategory;
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.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
/**
* A placeholder fragment containing a simple view.
*/
public class BookListFragment extends Fragment {
/**
* The fragment argument representing the section number for this fragment.
*/
private static final String ARG_BOOK_CATEGORY = "book_category";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static BookListFragment newInstance(BookCategory c) {
BookListFragment fragment = new BookListFragment();
Bundle args = new Bundle();
args.putString(ARG_BOOK_CATEGORY, c.toString());
fragment.setArguments(args);
return fragment;
}
public BookListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_download, container,
false);
TextView textView = (TextView) rootView
.findViewById(R.id.section_label);
textView.setText(getArguments().getString(ARG_BOOK_CATEGORY));
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((DownloadActivity) activity).onSectionAttached(getArguments()
.getString(ARG_BOOK_CATEGORY));
}
public void displayModules() {
DownloadManager dm = new DownloadManager();
if (dm.willRefresh()) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
DownloadDialogListener dialogListener = new DownloadDialogListener();
builder.setMessage(
"About to contact servers to download content. Continue?")
.setPositiveButton("Yes", dialogListener)
.setNegativeButton("No", dialogListener)
.setCancelable(false).show();
} else {
refreshModules();
}
}
private void refreshModules() {
DownloadManager dm = new DownloadManager();
if (dm.willRefresh()) {
ProgressDialog refreshDialog = new ProgressDialog(getActivity());
refreshDialog.setMessage("Refreshing available modules...");
refreshDialog.setCancelable(false);
refreshDialog.show();
dm.fetchAvailableBooks(new DlBookRefreshListener(refreshDialog));
} else {
dm.fetchAvailableBooks(new DlBookRefreshListener());
}
}
private class DlBookRefreshListener implements
BookRefreshTask.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;
}
public DlBookRefreshListener() {
}
@Override
public void onRefreshComplete(List<Book> results) {
if (dl != null) {
dl.cancel();
}
for (Book b : results) {
Log.d("DlBookRefreshListener", b.getName());
}
}
}
private class DownloadDialogListener implements
DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// Clicked ready to continue - allow downloading in the future
SharedPreferences prefs = getActivity().getSharedPreferences(
MinimalBibleConstants.DOWNLOAD_PREFS_FILE, Context.MODE_PRIVATE);
prefs.edit()
.putBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED,
true).commit();
// And warn them that it has been enabled in the future.
Toast.makeText(getActivity(),
"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;
}
}
}
}

View File

@ -1,31 +1,15 @@
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.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;
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;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class DownloadActivity extends BaseActivity implements
BaseNavigationDrawerFragment.NavigationDrawerCallbacks {
@ -54,10 +38,6 @@ 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
@ -67,21 +47,11 @@ public class DownloadActivity extends BaseActivity implements
fragmentManager
.beginTransaction()
.replace(R.id.container,
PlaceholderFragment.newInstance(position + 1)).commit();
BookListFragment.newInstance(DownloadManager.VALID_CATEGORIES[position])).commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
public void onSectionAttached(String category) {
mTitle = category;
}
public void restoreActionBar() {
@ -115,126 +85,4 @@ public class DownloadActivity extends BaseActivity implements
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_download,
container, false);
TextView textView = (TextView) rootView
.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((DownloadActivity) activity).onSectionAttached(getArguments()
.getInt(ARG_SECTION_NUMBER));
}
}
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();
} else {
refreshModules();
}
}
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 {
@Override
public void onClick(DialogInterface dialog, int 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();
// 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();
refreshModules();
break;
case DialogInterface.BUTTON_NEGATIVE:
// Not going to continue, still show what has
// already been downloaded.
break;
}
}
}
private class DlBookRefreshListener implements BookRefreshTask.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<Book> results) {
dl.cancel();
for (Book b : results) {
Log.d("DlBookRefreshListener", b.getName());
}
}
}
}

View File

@ -3,10 +3,13 @@ package org.bspeice.minimalbible.activities.downloader;
import java.util.Map;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.MinimalBibleConstants;
import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.Installer;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
public class DownloadManager {
@ -21,16 +24,33 @@ public class DownloadManager {
setDownloadDir();
}
public BookRefreshTask fetchAvailableBooks(boolean refresh,
public BookRefreshTask fetchAvailableBooks(
BookRefreshTask.BookRefreshListener bookRefreshListener) {
Map<String, Installer> installers = getInstallers();
return (BookRefreshTask) new BookRefreshTask(refresh,
return (BookRefreshTask) new BookRefreshTask(willRefresh(),
bookRefreshListener).execute(installers.values().toArray(
new Installer[installers.size()]));
}
public boolean willRefresh() {
// Method to determine if we need a refresh
// Public, so other modules can predict and take action accordingly.
// TODO: Discover if we need to refresh over Internet, or use a cached
// copy
// Fun fact - jSword handles the caching for us.
SharedPreferences prefs = MinimalBible.getAppContext()
.getSharedPreferences(
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
Context.MODE_PRIVATE);
return (!prefs.getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED,
false) || prefs.getBoolean(
MinimalBibleConstants.KEY_PERM_DISABLE_DOWNLOAD, false));
}
public Map<String, Installer> getInstallers() {
return new InstallManager().getInstallers();
}