Initial DownloadActivity import

I wish all new activities were this easy...
Needs significant refactoring/testing attention, coming shortly.
This commit is contained in:
Bradlee Speice
2014-07-07 22:09:15 -04:00
parent 5c71e8f014
commit d6951fcc9f
48 changed files with 2208 additions and 192 deletions

View File

@ -0,0 +1,8 @@
package org.bspeice.minimalbible;
/**
* Created by bspeice on 7/2/14.
*/
public interface Injector {
public void inject(Object o);
}

View File

@ -2,19 +2,26 @@ package org.bspeice.minimalbible;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import org.crosswire.jsword.book.sword.SwordBookPath;
import java.io.File;
import dagger.ObjectGraph;
/**
* Created by Bradlee Speice on 7/5/2014.
*/
public class MinimalBible extends Application {
public class MinimalBible extends Application implements Injector {
private String TAG = "MinimalBible";
private ObjectGraph mObjectGraph;
@Override
public void onCreate() {
super.onCreate();
buildObjGraph();
setJswordHome();
}
public void buildObjGraph() {
@ -25,7 +32,27 @@ public class MinimalBible extends Application {
mObjectGraph.inject(o);
}
public ObjectGraph plus(Object... modules) {
return mObjectGraph.plus(modules);
}
public static MinimalBible get(Context ctx) {
return (MinimalBible)ctx.getApplicationContext();
}
/**
* Notify jSword that it needs to store files in the Android internal directory
* NOTE: Android will uninstall these files if you uninstall MinimalBible.
*/
@SuppressWarnings("null")
private void setJswordHome() {
// We need to set the download directory for jSword to stick with
// Android.
String home = this.getFilesDir().toString();
Log.d(TAG, "Setting jsword.home to: " + home);
System.setProperty("jsword.home", home);
System.setProperty("sword.home", home);
SwordBookPath.setDownloadDir(new File(home));
Log.d(TAG, "Sword download path: " + SwordBookPath.getSwordDownloadDir());
}
}

View File

@ -2,7 +2,7 @@ package org.bspeice.minimalbible;
import android.app.Application;
import org.bspeice.minimalbible.activity.download.DownloadActivity;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import javax.inject.Singleton;
@ -12,8 +12,7 @@ import dagger.Provides;
/**
* Entry point for the default modules used by MinimalBible
*/
@Module(injects = DownloadActivity.class,
library = true)
@Module(library = true)
public class MinimalBibleModules {
MinimalBible app;
@ -25,8 +24,4 @@ public class MinimalBibleModules {
Application provideApplication() {
return app;
}
@Provides CharSequence provideString() {
return "Main";
}
}

View File

@ -0,0 +1,30 @@
package org.bspeice.minimalbible.activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import org.bspeice.minimalbible.R;
/**
* Wrapper for activities in MinimalBible to make sure we can support
* common functionality between them all.
*/
public class BaseActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Only set the tint if the device is running KitKat or above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintColor(getResources().getColor(
R.color.statusbar));
}
}
}

View File

@ -0,0 +1,26 @@
package org.bspeice.minimalbible.activity;
import android.app.Activity;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.view.View;
import com.readystatesoftware.systembartint.SystemBarTintManager;
/**
* Base class that defines all behavior common to Fragments in MinimalBible
*/
public class BaseFragment extends Fragment {
/**
* Calculate the offset we need to display properly if the System bar is translucent
* @param context The {@link android.app.Activity} we are displaying in
* @param view The {@link android.view.View} we need to calculate the offset for.
*/
protected static void setInsets(Activity context, View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
SystemBarTintManager tintManager = new SystemBarTintManager(context);
SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
view.setPadding(0, config.getPixelInsetTop(true), config.getPixelInsetRight(), config.getPixelInsetBottom());
}
}

View File

@ -0,0 +1,302 @@
package org.bspeice.minimalbible.activity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import org.bspeice.minimalbible.R;
/**
* Fragment used for managing interactions for and presentation of a navigation
* drawer. See the <a href=
* "https://developer.android.com/design/patterns/navigation-drawer.html#Interaction"
* > design guidelines</a> for a complete explanation of the behaviors
* implemented here.
*/
public class BaseNavigationDrawerFragment extends Fragment {
/**
* Remember the position of the selected item.
*/
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
/**
* Per the design guidelines, you should show the drawer on launch until the
* user manually expands it. This shared preference tracks this.
*/
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
/**
* A pointer to the current callbacks instance (the Activity).
*/
private NavigationDrawerCallbacks mCallbacks;
/**
* Helper component that ties the action bar to the navigation drawer.
*/
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
protected ListView mDrawerListView;
private View mFragmentContainerView;
protected int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;
public BaseNavigationDrawerFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Read in the flag indicating whether or not the user has demonstrated
// awareness of the
// drawer. See PREF_USER_LEARNED_DRAWER for details.
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState
.getInt(STATE_SELECTED_POSITION);
mFromSavedInstanceState = true;
}
// Select either the default item (0) or the last selected item.
selectItem(mCurrentSelectedPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Indicate that this fragment would like to influence the set of
// actions in the action bar.
setHasOptionsMenu(true);
}
public boolean isDrawerOpen() {
return mDrawerLayout != null
&& mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
/**
* Users of this fragment must call this method to set up the navigation
* drawer interactions.
*
* @param fragmentId
* The android:id of this fragment in its activity's layout.
* @param drawerLayout
* The DrawerLayout containing this fragment's UI.
*/
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// set up the drawer's list view with items and click listener
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /*
* "open drawer" description for
* accessibility
*/
R.string.navigation_drawer_close /*
* "close drawer" description for
* accessibility
*/
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu(); // calls
// onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to
// prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true)
.commit();
}
getActivity().supportInvalidateOptionsMenu(); // calls
// onPrepareOptionsMenu()
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce
// them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
public void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavigationDrawerCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
"Activity must implement NavigationDrawerCallbacks.");
}
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Forward the new configuration the drawer toggle component.
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// If the drawer is open, show the global app actions in the action bar.
// See also
// showGlobalContextActionBar, which controls the top-left area of the
// action bar.
if (mDrawerLayout != null && isDrawerOpen()) {
inflater.inflate(R.menu.global, menu);
showGlobalContextActionBar();
}
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Per the navigation drawer design guidelines, updates the action bar to
* show the global app 'context', rather than just what's in the current
* screen.
*/
private void showGlobalContextActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
// actionBar.setTitle(R.string.app_name);
}
protected ActionBar getActionBar() {
return ((ActionBarActivity) getActivity()).getSupportActionBar();
}
public void setInsets(View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
return;
SystemBarTintManager tintManager = new SystemBarTintManager(getActivity());
SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
view.setPadding(0, config.getPixelInsetTop(true),
config.getPixelInsetRight(), config.getPixelInsetBottom());
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// This could also be a ScrollView
ListView list = (ListView) view.findViewById(R.id.list_nav_drawer);
// This could also be set in your layout, allows the list items to
// scroll through the bottom padded area (navigation bar)
list.setClipToPadding(false);
// Sets the padding to the insets (include action bar and navigation bar
// padding for the current device and orientation)
setInsets(list);
}
/**
* Callbacks interface that all activities using this fragment must
* implement.
*/
public static interface NavigationDrawerCallbacks {
/**
* Called when an item in the navigation drawer is selected.
*/
void onNavigationDrawerItemSelected(int position);
}
}

View File

@ -1,156 +0,0 @@
package org.bspeice.minimalbible.activity.download;
import android.app.Activity;
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.support.v7.app.ActionBarActivity;
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 org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.NavigationDrawerFragment;
import org.bspeice.minimalbible.R;
import javax.inject.Inject;
public class DownloadActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
protected CharSequence mTitle;
//TODO: This will need to be refactored out later, for now it's a proof of concept.
@Inject CharSequence testInject;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
MinimalBible.get(this).inject(this);
setContentView(R.layout.activity_download);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.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 restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.download, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
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);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((DownloadActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}

View File

@ -0,0 +1,156 @@
package org.bspeice.minimalbible.activity.downloader;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.todddavies.components.progressbar.ProgressWheel;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager;
import org.bspeice.minimalbible.activity.downloader.manager.DLProgressEvent;
import org.bspeice.minimalbible.activity.downloader.manager.InstalledManager;
import org.crosswire.jsword.book.Book;
import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
/**
* Created by bspeice on 5/20/14.
*/
public class BookItemHolder {
// TODO: The holder should register and unregister itself for DownloadProgress events
// so that we can display live updates.
@InjectView(R.id.download_txt_item_acronym)
TextView acronym;
@InjectView(R.id.txt_download_item_name)
TextView itemName;
@InjectView(R.id.download_ibtn_download)
ImageButton isDownloaded;
@InjectView(R.id.download_prg_download)
ProgressWheel downloadProgress;
@Inject
BookDownloadManager bookDownloadManager;
@Inject
InstalledManager installedManager;
private final Book b;
private Subscription subscription;
// TODO: Factory style?
public BookItemHolder(View v, Book b, DownloadActivity activity) {
ButterKnife.inject(this, v);
activity.inject(this);
this.b = b;
}
public void bindHolder() {
acronym.setText(b.getInitials());
itemName.setText(b.getName());
DLProgressEvent dlProgressEvent = bookDownloadManager.getInProgressDownloadProgress(b);
if (dlProgressEvent != null) {
displayProgress((int) dlProgressEvent.toCircular());
} else if (installedManager.isInstalled(b)) {
displayInstalled();
}
//TODO: Refactor
subscription = bookDownloadManager.getDownloadEvents()
.observeOn(AndroidSchedulers.mainThread())
.filter(new Func1<DLProgressEvent, Boolean>() {
@Override
public Boolean call(DLProgressEvent event) {
return event.getB().getInitials().equals(b.getInitials());
}
})
.subscribe(new Action1<DLProgressEvent>() {
@Override
public void call(DLProgressEvent event) {
BookItemHolder.this.displayProgress((int) event.toCircular());
}
});
}
private void displayInstalled() {
isDownloaded.setImageResource(R.drawable.ic_action_cancel);
}
@OnClick(R.id.download_ibtn_download)
public void onDownloadItem(View v) {
if (installedManager.isInstalled(b)) {
// Remove the book
installedManager.removeBook(b);
isDownloaded.setImageResource(R.drawable.ic_action_download);
} else {
bookDownloadManager.installBook(this.b);
}
}
/**
* Display the current progress of this download
* TODO: Clean up this logic if at all possible...
* @param progress The progress out of 360 (degrees of a circle)
*/
private void displayProgress(int progress) {
if (progress == DLProgressEvent.PROGRESS_BEGINNING) {
// Download starting
RelativeLayout.LayoutParams acronymParams =
(RelativeLayout.LayoutParams)acronym.getLayoutParams();
acronymParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId());
RelativeLayout.LayoutParams nameParams =
(RelativeLayout.LayoutParams)itemName.getLayoutParams();
nameParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId());
isDownloaded.setVisibility(View.GONE);
downloadProgress.setVisibility(View.VISIBLE);
downloadProgress.spin();
} else if (progress < 360) {
// Download in progress
RelativeLayout.LayoutParams acronymParams =
(RelativeLayout.LayoutParams)acronym.getLayoutParams();
acronymParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId());
RelativeLayout.LayoutParams nameParams =
(RelativeLayout.LayoutParams)itemName.getLayoutParams();
nameParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId());
isDownloaded.setVisibility(View.GONE);
downloadProgress.setVisibility(View.VISIBLE);
downloadProgress.stopSpinning();
downloadProgress.setProgress(progress);
} else {
// Download complete
subscription.unsubscribe();
RelativeLayout.LayoutParams acronymParams =
(RelativeLayout.LayoutParams)acronym.getLayoutParams();
acronymParams.addRule(RelativeLayout.LEFT_OF, isDownloaded.getId());
RelativeLayout.LayoutParams nameParams =
(RelativeLayout.LayoutParams)itemName.getLayoutParams();
nameParams.addRule(RelativeLayout.LEFT_OF, isDownloaded.getId());
isDownloaded.setVisibility(View.VISIBLE);
downloadProgress.setVisibility(View.GONE);
displayInstalled();
}
}
public void onScrollOffscreen() {
subscription.unsubscribe();
}
}

View File

@ -0,0 +1,65 @@
package org.bspeice.minimalbible.activity.downloader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import org.bspeice.minimalbible.R;
import org.crosswire.jsword.book.Book;
import java.util.List;
/**
* Adapter to inflate list_download_items.xml
*/
public class BookListAdapter extends BaseAdapter implements AbsListView.RecyclerListener {
private final List<Book> bookList;
private final LayoutInflater inflater;
private final DownloadActivity activity;
public BookListAdapter(LayoutInflater inflater, List<Book> bookList, DownloadActivity activity) {
this.bookList = bookList;
this.inflater = inflater;
this.activity = activity;
}
@Override
public int getCount() {
return bookList.size();
}
@Override
public Book getItem(int position) {
return bookList.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BookItemHolder viewHolder;
// Nasty Android issue - if you don't check the getTag(), Android will start recycling,
// and you'll get some really strange issues
if (convertView == null || convertView.getTag() == null) {
convertView = inflater.inflate(R.layout.list_download_items, parent, false);
viewHolder = new BookItemHolder(convertView, getItem(position), activity);
} else {
viewHolder = (BookItemHolder) convertView.getTag();
}
viewHolder.bindHolder();
return convertView;
}
@Override
public void onMovedToScrapHeap(View view) {
BookItemHolder holder = (BookItemHolder) view.getTag();
holder.onScrollOffscreen();
}
}

View File

@ -0,0 +1,193 @@
package org.bspeice.minimalbible.activity.downloader;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseFragment;
import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.BookComparators;
import java.util.List;
import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.InjectView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
/**
* A placeholder fragment containing a simple view.
*/
public class BookListFragment extends BaseFragment {
/**
* The fragment argument representing the section number for this fragment.
* Not a candidate for Dart (yet) because I would have to write a Parcelable around it.
*/
protected static final String ARG_BOOK_CATEGORY = "book_category";
private final String TAG = "BookListFragment";
@InjectView(R.id.lst_download_available)
ListView downloadsAvailable;
@Inject RefreshManager refreshManager;
@Inject protected DownloadPrefs downloadPrefs;
protected ProgressDialog refreshDialog;
private LayoutInflater inflater;
/**
* Returns a new instance of this fragment for the given section number.
* TODO: Switch to AutoFactory/@Provides rather than inline creation.
*/
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;
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
((Injector)getActivity()).inject(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.inflater = inflater;
View rootView = inflater.inflate(R.layout.fragment_download, container,
false);
ButterKnife.inject(this, rootView);
displayModules();
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((DownloadActivity) activity).onSectionAttached(getArguments()
.getString(ARG_BOOK_CATEGORY));
}
/**
* Trigger the functionality to display a list of modules. Prompts user if downloading
* from the internet is allowable.
*/
protected void displayModules() {
boolean dialogDisplayed = downloadPrefs.hasShownDownloadDialog();
if (!dialogDisplayed) {
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();
}
}
/**
* Do the work of refreshing modules (download manager handles using cached copy vs. actual
* refresh), and then displaying them when ready.
*/
private void refreshModules() {
// Check if the downloadManager has already refreshed everything
if (!refreshManager.isRefreshComplete()) {
// downloadManager is in progress of refreshing
refreshDialog = new ProgressDialog(getActivity());
refreshDialog.setMessage("Refreshing available modules...");
refreshDialog.setCancelable(false);
refreshDialog.show();
}
// Listen for the books!
refreshManager.getAvailableModulesFlattened()
.filter(new Func1<Book, Boolean>() {
@Override
public Boolean call(Book book) {
return book.getBookCategory() ==
BookCategory.fromString(BookListFragment.this.getArguments()
.getString(ARG_BOOK_CATEGORY));
}
})
// Repack all the books
.toSortedList(new Func2<Book, Book, Integer>() {
@Override
public Integer call(Book book1, Book book2) {
return BookComparators.getInitialComparator().compare(book1, book2);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Book>>() {
@Override
public void call(List<Book> books) {
downloadsAvailable.setAdapter(
new BookListAdapter(inflater, books, (DownloadActivity)getActivity()));
if (BookListFragment.this.getActivity() != null) {
// On a screen rotate, getActivity() will be null. But, the activity
// will already have been set up correctly, so we don't need to worry
// about it.
// If not null, we need to set it up now.
setInsets(BookListFragment.this.getActivity(), downloadsAvailable);
}
if (refreshDialog != null) {
refreshDialog.cancel();
}
}
});
}
private class DownloadDialogListener implements
DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadPrefs.hasShownDownloadDialog(true);
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// Clicked ready to continue - allow downloading in the future
downloadPrefs.hasEnabledDownload(true);
// 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:
// Clicked to not download - Permanently disable downloading
downloadPrefs.hasEnabledDownload(false);
Toast.makeText(getActivity(),
"Disabling downloading. Re-enable it in settings.",
Toast.LENGTH_SHORT).show();
refreshModules();
break;
}
}
}
}

View File

@ -0,0 +1,115 @@
package org.bspeice.minimalbible.activity.downloader;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseActivity;
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment;
import org.bspeice.minimalbible.activity.downloader.manager.DownloadManager;
import dagger.ObjectGraph;
public class DownloadActivity extends BaseActivity implements
BaseNavigationDrawerFragment.NavigationDrawerCallbacks,
Injector {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private DownloadNavDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
private ObjectGraph daObjectGraph;
/**
* Build a scoped object graph for anything used by the DownloadActivity
*/
private void buildObjGraph() {
if (daObjectGraph == null) {
daObjectGraph = MinimalBible.get(this)
.plus(new DownloadActivityModules(this));
}
}
@Override
public void inject(Object o) {
buildObjGraph();
daObjectGraph.inject(o);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
mNavigationDrawerFragment = (DownloadNavDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
//TODO: Switch to AutoFactory pattern, rather than newInstance()
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
BookListFragment.newInstance(DownloadManager.VALID_CATEGORIES[position])).commit();
}
public void onSectionAttached(String category) {
mTitle = category;
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.download, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -0,0 +1,48 @@
package org.bspeice.minimalbible.activity.downloader;
import org.bspeice.minimalbible.MinimalBibleModules;
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager;
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadThread;
import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import de.devland.esperandro.Esperandro;
/**
* Module mappings for the classes under the Download Activity
*/
@Module(
injects = {
BookListFragment.class,
BookItemHolder.class,
BookDownloadManager.class,
BookDownloadThread.class,
RefreshManager.class
},
addsTo = MinimalBibleModules.class
)
public class DownloadActivityModules {
DownloadActivity activity;
DownloadActivityModules(DownloadActivity activity) {
this.activity = activity;
}
@Provides @Singleton
DownloadPrefs provideDownloadPrefs() {
return Esperandro.getPreferences(DownloadPrefs.class, activity);
}
@Provides @Singleton
DownloadActivity provideDownloadActivity() {
return activity;
}
@Provides @Singleton
BookDownloadManager provideBookDownloadManager() {
return new BookDownloadManager(activity);
}
}

View File

@ -0,0 +1,43 @@
package org.bspeice.minimalbible.activity.downloader;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment;
import org.bspeice.minimalbible.activity.downloader.manager.DownloadManager;
public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItem(position);
}
});
String[] sCategories = new String[DownloadManager.VALID_CATEGORIES.length];
for (int i = 0; i < DownloadManager.VALID_CATEGORIES.length; i++) {
sCategories[i] = DownloadManager.VALID_CATEGORIES[i].toString();
}
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar()
.getThemedContext(), android.R.layout.simple_list_item_1,
android.R.id.text1, sCategories));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
}

View File

@ -0,0 +1,20 @@
package org.bspeice.minimalbible.activity.downloader;
import de.devland.esperandro.annotations.SharedPreferences;
/**
* SharedPreferences interface to be built by Esperandro
*/
@SharedPreferences(name="DownloadPrefs")
public interface DownloadPrefs {
boolean hasEnabledDownload();
void hasEnabledDownload(boolean hasEnabledDownload);
boolean hasShownDownloadDialog();
void hasShownDownloadDialog(boolean hasShownDownloadDialog);
long downloadRefreshedOn();
void downloadRefreshedOn(long downloadRefreshedOn);
}

View File

@ -0,0 +1,132 @@
package org.bspeice.minimalbible.activity.downloader.manager;
import android.content.Context;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import org.crosswire.common.progress.JobManager;
import org.crosswire.common.progress.Progress;
import org.crosswire.common.progress.WorkEvent;
import org.crosswire.common.progress.WorkListener;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.book.BooksEvent;
import org.crosswire.jsword.book.BooksListener;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import rx.subjects.PublishSubject;
/**
* Wrapper to convert JSword progress events to MinimalBible EventBus-based
*/
//TODO: Make sure that jobs have the correct name
//TODO: Install indexes for Bibles
@Singleton
public class BookDownloadManager implements WorkListener, BooksListener {
/**
* Mapping of Job ID to the EventBus we should trigger progress on
*/
private final Map<String, Book> bookMappings;
/**
* Cached copy of downloads in progress so views displaying this info can get it quickly.
*/
private final Map<Book, DLProgressEvent> inProgressDownloads;
private final PublishSubject<DLProgressEvent> downloadEvents = PublishSubject.create();
@Inject
Provider<BookDownloadThread> dlThreadProvider;
@Inject
public BookDownloadManager(DownloadActivity activity) {
bookMappings = new HashMap<String, Book>();
inProgressDownloads = new HashMap<Book, DLProgressEvent>();
JobManager.addWorkListener(this);
activity.inject(this);
Books.installed().addBooksListener(this);
}
public void installBook(Book b) {
BookDownloadThread dlThread = dlThreadProvider.get();
dlThread.downloadBook(b);
addJob(BookDownloadThread.getJobId(b), b);
downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b));
}
public void addJob(String jobId, Book b) {
bookMappings.put(jobId, b);
}
@Override
public void workProgressed(WorkEvent ev) {
Progress job = ev.getJob();
Log.d("BookDownloadManager", "Download in progress: " + job.getJobID() + " - " + job.getJobName() + " " + job.getWorkDone() + "/" + job.getTotalWork());
if (bookMappings.containsKey(job.getJobID())) {
Book b = bookMappings.get(job.getJobID());
if (job.getWorkDone() == job.getTotalWork()) {
// Download is complete
inProgressDownloads.remove(bookMappings.get(job.getJobID()));
bookMappings.remove(job.getJobID());
downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b));
} else {
// Track the ongoing download
DLProgressEvent event = new DLProgressEvent(job.getWorkDone(),
job.getTotalWork(), b);
inProgressDownloads.put(b, event);
downloadEvents.onNext(event);
}
}
}
/**
* Check the status of a book download in progress.
* @param b The book to get the current progress of
* @return The most recent DownloadProgressEvent for the book, or null if not downloading
*/
public DLProgressEvent getInProgressDownloadProgress(Book b) {
if (inProgressDownloads.containsKey(b)) {
return inProgressDownloads.get(b);
} else {
return null;
}
}
public PublishSubject<DLProgressEvent> getDownloadEvents() {
return downloadEvents;
}
@Override
public void workStateChanged(WorkEvent ev) {
Log.d("BookDownloadManager", ev.toString());
}
@Override
public void bookAdded(BooksEvent booksEvent) {
// It's possible the install finished before we received a progress event for it,
// we handle that case here.
Book b = booksEvent.getBook();
Log.d("BookDownloadManager", "Book added: " + b.getName());
if (inProgressDownloads.containsKey(b)) {
inProgressDownloads.remove(b);
}
// Not sure why, but the inProgressDownloads might not have our book,
// so we always trigger the PROGRESS_COMPLETE event.
// TODO: Make sure all books get to the inProgressDownloads
downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b));
}
@Override
public void bookRemoved(BooksEvent booksEvent) {
// Not too worried about this just yet.
}
}

View File

@ -0,0 +1,68 @@
package org.bspeice.minimalbible.activity.downloader.manager;
import android.content.Context;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer;
import javax.inject.Inject;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
/**
* Thread that handles downloading a book
*/
//TODO: Refactor to BookDownloadManager, downloadBook() creates its own thread
public class BookDownloadThread {
private final String TAG = "BookDownloadThread";
@Inject
BookDownloadManager bookDownloadManager;
@Inject
RefreshManager refreshManager;
@Inject
public BookDownloadThread(DownloadActivity activity) {
activity.inject(this);
}
public void downloadBook(final Book b) {
// So, the JobManager can't be injected, but we'll make do
// First, look up where the Book came from
refreshManager.installerFromBook(b)
.subscribeOn(Schedulers.io())
.subscribe(new Action1<Installer>() {
@Override
public void call(Installer installer) {
try {
installer.install(b);
} catch (InstallException e) {
Log.d(TAG, e.getMessage());
}
bookDownloadManager.getDownloadEvents()
.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b));
}
});
}
/**
* Build what the installer creates the job name as.
* Likely prone to be brittle.
* TODO: Make sure to test that this is an accurate job name
*
* @param b The book to predict the download job name of
* @return The name of the job that will/is download/ing this book
*/
public static String getJobId(Book b) {
return "INSTALL_BOOK-" + b.getInitials();
}
}

View File

@ -0,0 +1,40 @@
package org.bspeice.minimalbible.activity.downloader.manager;
import org.crosswire.jsword.book.Book;
/**
* Used for notifying that a book's download progress is ongoing
*/
public class DLProgressEvent {
private final int progress;
private final Book b;
public static final int PROGRESS_COMPLETE = 100;
public static final int PROGRESS_BEGINNING = 0;
public DLProgressEvent(int workDone, int totalWork, Book b) {
if (totalWork == 0) {
this.progress = 0;
} else {
this.progress = (int)((float) workDone / totalWork * 100);
}
this.b = b;
}
public DLProgressEvent(int workDone, Book b) {
this.progress = workDone;
this.b = b;
}
public int getProgress() {
return progress;
}
public float toCircular() {
return ((float)progress) * 360 / 100;
}
public Book getB() {
return this.b;
}
}

View File

@ -0,0 +1,14 @@
package org.bspeice.minimalbible.activity.downloader.manager;
import org.crosswire.jsword.book.BookCategory;
// TODO: Listen to BookInstall events?
public class DownloadManager {
private final String TAG = "DownloadManager";
// TODO: Inject this, don't have any static references
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
BookCategory.COMMENTARY, BookCategory.DICTIONARY,
BookCategory.MAPS };
}

View File

@ -0,0 +1,81 @@
package org.bspeice.minimalbible.activity.downloader.manager;
import android.util.Log;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookException;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.book.BooksEvent;
import org.crosswire.jsword.book.BooksListener;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* Manager to keep track of which books have been installed
*/
@Singleton
public class InstalledManager implements BooksListener {
private Books installedBooks;
private List<Book> installedBooksList;
private String TAG = "InstalledManager";
@Inject InstalledManager() {}
/**
* Register our manager to receive events on Book install
* This is a relatively expensive operation,
* so we don't put it in the constructor.
*/
public void initialize() {
//TODO: Move this to a true async, rather than separate initialize() function
installedBooks = Books.installed();
installedBooksList = installedBooks.getBooks();
installedBooks.addBooksListener(this);
}
public boolean isInstalled(Book b) {
if (installedBooks == null) {
initialize();
}
return installedBooksList.contains(b);
}
@Override
public void bookAdded(BooksEvent booksEvent) {
Log.d(TAG, "Book added: " + booksEvent.getBook().toString());
Book b = booksEvent.getBook();
if (!installedBooksList.contains(b)) {
installedBooksList.add(b);
}
}
@Override
public void bookRemoved(BooksEvent booksEvent) {
Log.d(TAG, "Book removed: " + booksEvent.getBook().toString());
Book b = booksEvent.getBook();
if (installedBooksList.contains(b)) {
installedBooksList.remove(b);
}
}
public void removeBook(Book b) {
if (installedBooks == null) {
initialize();
}
// Not sure why we need to call this multiple times, but...
while (Books.installed().getBooks().contains(b)) {
try {
// This worked in the past, but isn't now...
// installedBooks.remove(b);
Book realBook = installedBooks.getBook(b.getInitials());
b.getDriver().delete(realBook);
} catch (BookException e) {
Log.e("InstalledManager", "Unable to remove book (already uninstalled?): " + e.getLocalizedMessage());
}
}
}
}

View File

@ -0,0 +1,152 @@
package org.bspeice.minimalbible.activity.downloader.manager;
import android.content.Context;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.Installer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.inject.Inject;
import javax.inject.Singleton;
import rx.Observable;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;
/**
* Handle refreshing the list of books available as needed
*/
@Singleton
public class RefreshManager {
@Inject InstalledManager installedManager;
/**
* Cached copy of modules that are available so we don't refresh for everyone who requests it.
*/
private Observable<Map<Installer, List<Book>>> availableModules;
private final AtomicBoolean refreshComplete = new AtomicBoolean();
@Inject
public RefreshManager(DownloadActivity activity) {
activity.inject(this);
refreshModules();
}
/**
* Do the work of kicking off the AsyncTask to refresh books, and make sure we know
* when it's done.
* TODO: Should I have a better way of scheduling than Schedulers.io()?
*/
private void refreshModules() {
if (availableModules == null) {
availableModules = Observable.from(new InstallManager().getInstallers().values())
.map(new Func1<Installer, Map<Installer, List<Book>>>() {
@Override
public Map<Installer, List<Book>> call(Installer installer) {
Map<Installer, List<Book>> map = new HashMap<Installer, List<Book>>();
map.put(installer, installer.getBooks());
return map;
}
}).subscribeOn(Schedulers.io())
.cache();
// Set refresh complete when it is.
availableModules.observeOn(Schedulers.io())
.subscribe(new Action1<Map<Installer, List<Book>>>() {
@Override
public void call(Map<Installer, List<Book>> onNext) {}
}, new Action1<Throwable>() {
@Override
public void call(Throwable onError) {}
}, new Action0() {
@Override
public void call() {
refreshComplete.set(true);
}
});
}
}
public Observable<Map<Installer, List<Book>>> getAvailableModules() {
return availableModules;
}
public Observable<Book> getAvailableModulesFlattened() {
return availableModules
// First flatten the Map to its lists
.flatMap(new Func1<Map<Installer, List<Book>>, Observable<? extends List<Book>>>() {
@Override
public Observable<? extends List<Book>> call(Map<Installer, List<Book>> books) {
return Observable.from(books.values());
}
})
// Then flatten the lists
.flatMap(new Func1<List<Book>, Observable<? extends Book>>() {
@Override
public Observable<? extends Book> call(List<Book> t1) {
return Observable.from(t1);
}
});
}
/**
* Get the cached book list
* @return The cached book list, or null
*/
public List<Book> getBookList() {
List<Book> availableList = new ArrayList<Book>();
availableModules.reduce(availableList,
new Func2<List<Book>, Map<Installer, List<Book>>, List<Book>>() {
@Override
public List<Book> call(List<Book> books, Map<Installer, List<Book>> installerListMap) {
for (List<Book> l : installerListMap.values()) {
books.addAll(l);
}
return books;
}
});
return availableList;
}
/**
* Find the installer that a Book comes from.
* @param b The book to search for
* @return The Installer that should be used for this book.
*/
public Observable<Installer> installerFromBook(final Book b) {
return availableModules.filter(new Func1<Map<Installer, List<Book>>, Boolean>() {
@Override
public Boolean call(Map<Installer, List<Book>> installerListMap) {
for (List<Book> element : installerListMap.values()) {
if (element.contains(b)) {
return true;
}
}
return false;
}
})
.first()
.map(new Func1<Map<Installer, List<Book>>, Installer>() {
@Override
public Installer call(Map<Installer, List<Book>> element) {
return element.entrySet().iterator().next().getKey();
}
});
}
public boolean isRefreshComplete() {
return refreshComplete.get();
}
}