2014-04-21 22:28:49 -04:00
|
|
|
package org.bspeice.minimalbible.activities.downloader;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.support.v4.app.Fragment;
|
2014-05-05 01:10:42 -04:00
|
|
|
import android.util.Log;
|
2014-04-21 22:28:49 -04:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2014-05-05 01:10:42 -04:00
|
|
|
import org.androidannotations.annotations.AfterViews;
|
|
|
|
import org.androidannotations.annotations.EFragment;
|
|
|
|
import org.androidannotations.annotations.FragmentArg;
|
|
|
|
import org.androidannotations.annotations.Trace;
|
|
|
|
import org.androidannotations.annotations.ViewById;
|
|
|
|
import org.bspeice.minimalbible.MinimalBibleConstants;
|
|
|
|
import org.bspeice.minimalbible.R;
|
|
|
|
import org.crosswire.jsword.book.Book;
|
|
|
|
import org.crosswire.jsword.book.BookCategory;
|
|
|
|
import org.crosswire.jsword.book.BookFilter;
|
|
|
|
import org.crosswire.jsword.book.FilterUtil;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-04-21 22:28:49 -04:00
|
|
|
/**
|
|
|
|
* A placeholder fragment containing a simple view.
|
|
|
|
*/
|
2014-05-05 01:10:42 -04:00
|
|
|
@EFragment(R.layout.fragment_download)
|
2014-04-21 22:28:49 -04:00
|
|
|
public class BookListFragment extends Fragment {
|
|
|
|
/**
|
|
|
|
* The fragment argument representing the section number for this fragment.
|
|
|
|
*/
|
|
|
|
|
2014-05-05 01:10:42 -04:00
|
|
|
private final String TAG = "BookListFragment";
|
2014-04-21 23:11:16 -04:00
|
|
|
|
2014-05-05 01:10:42 -04:00
|
|
|
@FragmentArg
|
|
|
|
BookCategory bookCategory;
|
2014-04-21 22:28:49 -04:00
|
|
|
|
2014-05-05 01:10:42 -04:00
|
|
|
@ViewById(R.id.section_label)
|
|
|
|
protected TextView tv;
|
2014-04-21 22:28:49 -04:00
|
|
|
|
2014-05-05 01:10:42 -04:00
|
|
|
private ProgressDialog refreshDialog;
|
2014-04-21 22:28:49 -04:00
|
|
|
|
2014-05-05 01:10:42 -04:00
|
|
|
@Trace
|
|
|
|
@AfterViews
|
|
|
|
public void updateName() {
|
|
|
|
((DownloadActivity) getActivity()).onSectionAttached(bookCategory.toString());
|
|
|
|
tv.setText(bookCategory.toString());
|
|
|
|
displayModules();
|
|
|
|
}
|
2014-04-21 22:28:49 -04:00
|
|
|
|
|
|
|
public void displayModules() {
|
2014-04-21 23:11:16 -04:00
|
|
|
SharedPreferences prefs = getActivity()
|
|
|
|
.getSharedPreferences(
|
|
|
|
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
boolean dialogDisplayed = prefs.getBoolean(
|
|
|
|
MinimalBibleConstants.KEY_SHOWED_DOWNLOAD_DIALOG, false);
|
|
|
|
|
|
|
|
if (!dialogDisplayed) {
|
2014-04-21 22:28:49 -04:00
|
|
|
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() {
|
2014-04-23 00:11:13 -04:00
|
|
|
DownloadManager dm = DownloadManager.getInstance();
|
2014-05-05 01:10:42 -04:00
|
|
|
EventBookList bookList = dm.getDownloadBus().getStickyEvent(EventBookList.class);
|
|
|
|
if (bookList == null) {
|
|
|
|
dm.getDownloadBus().registerSticky(this);
|
|
|
|
refreshDialog = new ProgressDialog(getActivity());
|
|
|
|
refreshDialog.setMessage("Refreshing available modules...");
|
|
|
|
refreshDialog.setCancelable(false);
|
|
|
|
refreshDialog.show();
|
|
|
|
} else {
|
|
|
|
displayBooks(bookList.getBookList());
|
|
|
|
}
|
2014-04-28 15:07:39 -04:00
|
|
|
}
|
2014-05-05 01:10:42 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
Used by GreenRobot for notifying us that the book refresh is complete
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unused")
|
2014-04-28 15:07:39 -04:00
|
|
|
public void onEventMainThread(EventBookList event) {
|
|
|
|
if (refreshDialog != null) {
|
|
|
|
refreshDialog.cancel();
|
2014-04-21 22:28:49 -04:00
|
|
|
}
|
2014-05-05 01:10:42 -04:00
|
|
|
displayBooks(event.getBookList());
|
2014-04-21 22:28:49 -04:00
|
|
|
}
|
|
|
|
|
2014-05-05 01:10:42 -04:00
|
|
|
public void displayBooks(List<Book> bookList) {
|
|
|
|
try {
|
|
|
|
BookFilter f = FilterUtil.filterFromCategory(bookCategory);
|
|
|
|
List<Book> filteredBooks = FilterUtil.applyFilter(bookList, f);
|
|
|
|
tv.setText(filteredBooks.get(0).getName());
|
|
|
|
} catch (FilterUtil.InvalidFilterCategoryMappingException e) {
|
|
|
|
// To be honest, there should be no reason you end up here.
|
|
|
|
Log.e(TAG, e.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-21 22:28:49 -04:00
|
|
|
private class DownloadDialogListener implements
|
|
|
|
DialogInterface.OnClickListener {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2014-04-21 23:11:16 -04:00
|
|
|
SharedPreferences prefs = getActivity().getSharedPreferences(
|
|
|
|
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
|
|
|
Context.MODE_PRIVATE);
|
|
|
|
prefs.edit().putBoolean(MinimalBibleConstants.KEY_SHOWED_DOWNLOAD_DIALOG, true)
|
|
|
|
.commit();
|
|
|
|
|
2014-04-21 22:28:49 -04:00
|
|
|
switch (which) {
|
|
|
|
case DialogInterface.BUTTON_POSITIVE:
|
|
|
|
// Clicked ready to continue - allow downloading in the future
|
|
|
|
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:
|
2014-04-21 23:11:16 -04:00
|
|
|
// Clicked to not download - Permanently disable downloading
|
|
|
|
prefs.edit()
|
|
|
|
.putBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED,
|
|
|
|
false).commit();
|
|
|
|
Toast.makeText(getActivity(),
|
|
|
|
"Disabling downloading. Re-enable it in settings.",
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
refreshModules();
|
2014-04-21 22:28:49 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|