mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2025-07-07 16:54:48 -04:00
Path refactoring
Also fixed an issue with View recycling
This commit is contained in:
@ -0,0 +1,63 @@
|
||||
package org.bspeice.minimalbible;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
import dagger.ObjectGraph;
|
||||
|
||||
public class MinimalBible extends Application {
|
||||
|
||||
/**
|
||||
* The graph used by Dagger to track dependencies
|
||||
*/
|
||||
private ObjectGraph graph;
|
||||
|
||||
/**
|
||||
* A singleton reference to the Application currently being run.
|
||||
* Used mostly so we have a fixed point to get the App Context from
|
||||
*/
|
||||
private static MinimalBible instance;
|
||||
|
||||
/**
|
||||
* Create the application, and persist the application Context
|
||||
*/
|
||||
public MinimalBible() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Application Context. Please note, all attempts to get the App Context should come
|
||||
* through here, and please be sure that the Application won't satisfy what you need.
|
||||
* @return The Application Context
|
||||
*/
|
||||
public static Context getAppContext() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Application, rather than just the Application Context. You likely should be using
|
||||
* this, rather than {@link #getAppContext()}
|
||||
* @return The MinimalBible {@link android.app.Application} object
|
||||
*/
|
||||
public static MinimalBible getApplication() {
|
||||
return (MinimalBible)getAppContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the {@link android.app.Application}. Responsible for building and
|
||||
* holding on to the master ObjectGraph.
|
||||
*/
|
||||
@Override
|
||||
public void onCreate() {
|
||||
graph = ObjectGraph.create(MinimalBibleModules.class);
|
||||
graph.inject(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject a Dagger object
|
||||
* @param o The object to be injected
|
||||
*/
|
||||
public void inject(Object o) {
|
||||
graph.inject(o);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package org.bspeice.minimalbible;
|
||||
|
||||
import org.bspeice.minimalbible.activities.ActivityModules;
|
||||
|
||||
import dagger.Module;
|
||||
|
||||
/**
|
||||
* Master module for MinimalBible
|
||||
*/
|
||||
@Module(
|
||||
injects = {
|
||||
MinimalBible.class
|
||||
},
|
||||
includes = {
|
||||
ActivityModules.class
|
||||
}
|
||||
)
|
||||
public class MinimalBibleModules {
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package org.bspeice.minimalbible.activities;
|
||||
|
||||
import org.bspeice.minimalbible.activities.downloader.ActivityDownloaderModule;
|
||||
|
||||
import dagger.Module;
|
||||
|
||||
/**
|
||||
* Modules for all activities
|
||||
*/
|
||||
@Module(
|
||||
includes = {
|
||||
ActivityDownloaderModule.class
|
||||
}
|
||||
)
|
||||
public class ActivityModules {
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package org.bspeice.minimalbible.activities;
|
||||
|
||||
import org.bspeice.minimalbible.R;
|
||||
|
||||
import com.readystatesoftware.systembartint.SystemBarTintManager;
|
||||
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package org.bspeice.minimalbible.activities;
|
||||
|
||||
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.
|
||||
*/
|
||||
public 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());
|
||||
}
|
||||
}
|
@ -0,0 +1,307 @@
|
||||
package org.bspeice.minimalbible.activities;
|
||||
|
||||
import org.bspeice.minimalbible.R;
|
||||
|
||||
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.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.readystatesoftware.systembartint.SystemBarTintManager;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
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 dagger.Module;
|
||||
import dagger.Provides;
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
/**
|
||||
* Module mappings for the classes under the Download Activity
|
||||
*/
|
||||
@Module(
|
||||
injects = {
|
||||
BookListFragment.class,
|
||||
DownloadManager.class,
|
||||
BookRefreshTask.class
|
||||
}
|
||||
)
|
||||
public class ActivityDownloaderModule {
|
||||
|
||||
/**
|
||||
* Provide a Singleton DownloadManager for injection
|
||||
* Note that we need to annotate Singleton here, only annotating on the
|
||||
* DownloadManager itself is not enough.
|
||||
* @return Global DownloadManager instance
|
||||
*/
|
||||
@Provides @Singleton
|
||||
DownloadManager provideDownloadManager() {
|
||||
return new DownloadManager();
|
||||
}
|
||||
|
||||
@Provides
|
||||
EventBus provideBus() {
|
||||
return new EventBus();
|
||||
}
|
||||
|
||||
|
||||
@Provides //@Singleton
|
||||
DownloadPrefs_ provideDownloadPrefs() {
|
||||
return new DownloadPrefs_(MinimalBible.getApplication());
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.todddavies.components.progressbar.ProgressWheel;
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.crosswire.jsword.book.Book;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
import butterknife.OnClick;
|
||||
|
||||
/**
|
||||
* Adapter to inflate list_download_items.xml
|
||||
*/
|
||||
public class BookListAdapter extends BaseAdapter {
|
||||
private List<Book> bookList;
|
||||
|
||||
private LayoutInflater inflater;
|
||||
|
||||
public BookListAdapter(LayoutInflater inflater, List<Book> bookList) {
|
||||
this.bookList = bookList;
|
||||
this.inflater = inflater;
|
||||
}
|
||||
|
||||
@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, null);
|
||||
viewHolder = new BookItemHolder(convertView);
|
||||
} else {
|
||||
viewHolder = (BookItemHolder) convertView.getTag();
|
||||
}
|
||||
|
||||
viewHolder.bindHolder(position);
|
||||
return convertView;
|
||||
}
|
||||
|
||||
public class BookItemHolder {
|
||||
|
||||
@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;
|
||||
|
||||
public BookItemHolder(View v) {
|
||||
ButterKnife.inject(this, v);
|
||||
}
|
||||
|
||||
public void bindHolder(int position) {
|
||||
Book b = BookListAdapter.this.getItem(position);
|
||||
acronym.setText(b.getInitials());
|
||||
itemName.setText(b.getName());
|
||||
}
|
||||
|
||||
@OnClick(R.id.download_ibtn_download)
|
||||
public void onDownloadItem(View v) {
|
||||
Log.d("BookListAdapter", v.toString());
|
||||
isDownloaded.setVisibility(View.GONE);
|
||||
downloadProgress.setVisibility(View.VISIBLE);
|
||||
downloadProgress.setProgress(75); // Out of 360
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,197 @@
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.f2prateek.dart.InjectExtra;
|
||||
|
||||
import org.bspeice.minimalbible.MinimalBible;
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.bspeice.minimalbible.activities.BaseFragment;
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.EventBookList;
|
||||
import org.crosswire.jsword.book.Book;
|
||||
import org.crosswire.jsword.book.BookCategory;
|
||||
import org.crosswire.jsword.book.BookComparators;
|
||||
import org.crosswire.jsword.book.BookFilter;
|
||||
import org.crosswire.jsword.book.FilterUtil;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private static final String ARG_BOOK_CATEGORY = "book_category";
|
||||
|
||||
private final String TAG = "BookListFragment";
|
||||
|
||||
@InjectView(R.id.lst_download_available)
|
||||
ListView downloadsAvailable;
|
||||
|
||||
@Inject DownloadManager downloadManager;
|
||||
|
||||
@Inject DownloadPrefs_ downloadPrefs;
|
||||
|
||||
private 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);
|
||||
MinimalBible.getApplication().inject(this); // Injection for Dagger goes here, not ctor
|
||||
}
|
||||
|
||||
@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.
|
||||
*/
|
||||
public void displayModules() {
|
||||
boolean dialogDisplayed = downloadPrefs.showedDownloadDialog().get();
|
||||
|
||||
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
|
||||
List<Book> bookList = downloadManager.getBookList();
|
||||
if (bookList == null) {
|
||||
// downloadManager is in progress of refreshing
|
||||
downloadManager.getDownloadBus().register(this);
|
||||
refreshDialog = new ProgressDialog(getActivity());
|
||||
refreshDialog.setMessage("Refreshing available modules...");
|
||||
refreshDialog.setCancelable(false);
|
||||
refreshDialog.show();
|
||||
} else {
|
||||
displayBooks(bookList);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by GreenRobot for notifying us that the book refresh is complete
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void onEventMainThread(EventBookList event) {
|
||||
if (refreshDialog != null) {
|
||||
refreshDialog.cancel();
|
||||
}
|
||||
displayBooks(event.getBookList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the hard work of creating the Adapter and displaying books.
|
||||
* @param bookList The (unfiltered) list of {link org.crosswire.jsword.Book}s to display
|
||||
*/
|
||||
public void displayBooks(List<Book> bookList) {
|
||||
try {
|
||||
// TODO: Should the filter be applied earlier in the process?
|
||||
List<Book> displayList;
|
||||
|
||||
BookCategory c = BookCategory.fromString(getArguments().getString(ARG_BOOK_CATEGORY));
|
||||
BookFilter f = FilterUtil.filterFromCategory(c);
|
||||
displayList = FilterUtil.applyFilter(bookList, f);
|
||||
Collections.sort(displayList, BookComparators.getInitialComparator());
|
||||
|
||||
downloadsAvailable.setAdapter(new BookListAdapter(inflater, displayList));
|
||||
setInsets(getActivity(), downloadsAvailable);
|
||||
} catch (FilterUtil.InvalidFilterCategoryMappingException e) {
|
||||
// To be honest, there should be no reason you end up here.
|
||||
Log.e(TAG, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private class DownloadDialogListener implements
|
||||
DialogInterface.OnClickListener {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
downloadPrefs.showedDownloadDialog().put(true);
|
||||
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
// Clicked ready to continue - allow downloading in the future
|
||||
downloadPrefs.hasEnabledDownload().put(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().put(false);
|
||||
Toast.makeText(getActivity(),
|
||||
"Disabling downloading. Re-enable it in settings.",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
refreshModules();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.bspeice.minimalbible.activities.BaseActivity;
|
||||
import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
|
||||
|
||||
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;
|
||||
|
||||
public class DownloadActivity extends BaseActivity implements
|
||||
BaseNavigationDrawerFragment.NavigationDrawerCallbacks {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
|
||||
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
This is brutally ugly, but until https://github.com/square/dagger/issues/410 is resolved,
|
||||
this is the best I can do while making sure that I can easily switch the API later
|
||||
*/
|
||||
//
|
||||
// DO NOT EDIT THIS FILE, IT HAS BEEN GENERATED USING AndroidAnnotations 3.0.1.
|
||||
//
|
||||
|
||||
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.androidannotations.api.sharedpreferences.BooleanPrefEditorField;
|
||||
import org.androidannotations.api.sharedpreferences.BooleanPrefField;
|
||||
import org.androidannotations.api.sharedpreferences.EditorHelper;
|
||||
import org.androidannotations.api.sharedpreferences.LongPrefEditorField;
|
||||
import org.androidannotations.api.sharedpreferences.LongPrefField;
|
||||
import org.androidannotations.api.sharedpreferences.SharedPreferencesHelper;
|
||||
|
||||
public final class DownloadPrefs_
|
||||
extends SharedPreferencesHelper
|
||||
{
|
||||
|
||||
private Context context_;
|
||||
|
||||
public DownloadPrefs_(Context context) {
|
||||
super(context.getSharedPreferences("DownloadPrefs", 0));
|
||||
this.context_ = context;
|
||||
}
|
||||
|
||||
public DownloadPrefs_.DownloadPrefsEditor_ edit() {
|
||||
return new DownloadPrefs_.DownloadPrefsEditor_(getSharedPreferences());
|
||||
}
|
||||
|
||||
public BooleanPrefField hasEnabledDownload() {
|
||||
return booleanField("hasEnabledDownload", false);
|
||||
}
|
||||
|
||||
public BooleanPrefField showedDownloadDialog() {
|
||||
return booleanField("showedDownloadDialog", false);
|
||||
}
|
||||
|
||||
public LongPrefField downloadRefreshedOn() {
|
||||
return longField("downloadRefreshedOn", 0L);
|
||||
}
|
||||
|
||||
public final static class DownloadPrefsEditor_
|
||||
extends EditorHelper<DownloadPrefs_.DownloadPrefsEditor_>
|
||||
{
|
||||
|
||||
|
||||
DownloadPrefsEditor_(SharedPreferences sharedPreferences) {
|
||||
super(sharedPreferences);
|
||||
}
|
||||
|
||||
public BooleanPrefEditorField<DownloadPrefs_.DownloadPrefsEditor_> hasEnabledDownload() {
|
||||
return booleanField("hasEnabledDownload");
|
||||
}
|
||||
|
||||
public BooleanPrefEditorField<DownloadPrefs_.DownloadPrefsEditor_> showedDownloadDialog() {
|
||||
return booleanField("showedDownloadDialog");
|
||||
}
|
||||
|
||||
public LongPrefEditorField<DownloadPrefs_.DownloadPrefsEditor_> downloadRefreshedOn() {
|
||||
return longField("downloadRefreshedOn");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
import org.androidannotations.annotations.sharedpreferences.DefaultBoolean;
|
||||
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
|
||||
import org.androidannotations.annotations.sharedpreferences.SharedPref;
|
||||
|
||||
/**
|
||||
* Renamed while waiting for https://github.com/square/dagger/issues/410 to get resolved.
|
||||
* Once the issue is fixed, this should go back to being DownloadPrefs
|
||||
*/
|
||||
@SharedPref(value= SharedPref.Scope.UNIQUE)
|
||||
public interface _DownloadPrefs {
|
||||
|
||||
@DefaultBoolean(false)
|
||||
boolean hasEnabledDownload();
|
||||
|
||||
@DefaultBoolean(false)
|
||||
boolean showedDownloadDialog();
|
||||
|
||||
@DefaultLong(0)
|
||||
long downloadRefreshedOn();
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package org.bspeice.minimalbible.activities.downloader.manager;
|
||||
|
||||
import android.content.Context;
|
||||
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.activities.downloader.DownloadPrefs_;
|
||||
import org.crosswire.jsword.book.Book;
|
||||
import org.crosswire.jsword.book.install.InstallException;
|
||||
import org.crosswire.jsword.book.install.Installer;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
||||
private static final String TAG = "EventBookRefreshTask";
|
||||
|
||||
// If last refresh was before the below, force an internet refresh
|
||||
private final Long refreshAfter = System.currentTimeMillis() - 604800000L; // 1 Week in millis
|
||||
|
||||
@Inject
|
||||
DownloadPrefs_ downloadPrefs;
|
||||
|
||||
private EventBus downloadBus;
|
||||
|
||||
public BookRefreshTask(EventBus downloadBus) {
|
||||
this.downloadBus = downloadBus;
|
||||
MinimalBible.getApplication().inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Book> doInBackground(Installer... params) {
|
||||
List<Book> books = new LinkedList<Book>();
|
||||
|
||||
int index = 0;
|
||||
for (Installer i : params) {
|
||||
if (doRefresh()) {
|
||||
try {
|
||||
i.reloadBookList();
|
||||
downloadPrefs.downloadRefreshedOn().put(System.currentTimeMillis());
|
||||
} catch (InstallException e) {
|
||||
Log.e(TAG,
|
||||
"Error downloading books from installer: "
|
||||
+ i.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
books.addAll(i.getBooks());
|
||||
publishProgress(++index, params.length);
|
||||
}
|
||||
|
||||
downloadBus.postSticky(new EventBookList(books));
|
||||
return books;
|
||||
}
|
||||
|
||||
private boolean doRefresh() {
|
||||
// Check if we should refresh over the internet, or use the local copy
|
||||
// TODO: Discover if we need to refresh over Internet, or use a cached
|
||||
// copy - likely something time-based, also check network state.
|
||||
// Fun fact - jSword handles the caching for us.
|
||||
|
||||
return (isWifi() && downloadEnabled() && needsRefresh());
|
||||
}
|
||||
|
||||
private boolean isWifi() {
|
||||
ConnectivityManager mgr = (ConnectivityManager)MinimalBible.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||
return networkInfo.isConnected();
|
||||
}
|
||||
|
||||
private boolean downloadEnabled() {
|
||||
return downloadPrefs.hasEnabledDownload().get();
|
||||
}
|
||||
|
||||
private boolean needsRefresh() {
|
||||
return (downloadPrefs.downloadRefreshedOn().get() > refreshAfter);
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
package org.bspeice.minimalbible.activities.downloader.manager;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.bspeice.minimalbible.MinimalBible;
|
||||
import org.crosswire.jsword.book.Book;
|
||||
import org.crosswire.jsword.book.BookCategory;
|
||||
import org.crosswire.jsword.book.install.InstallManager;
|
||||
import org.crosswire.jsword.book.install.Installer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
@Singleton
|
||||
public class DownloadManager {
|
||||
|
||||
private final String TAG = "DownloadManager";
|
||||
|
||||
/**
|
||||
* Cached copy of modules that are available so we don't refresh for everyone who requests it.
|
||||
*/
|
||||
private List<Book> availableModules = null;
|
||||
|
||||
@Inject
|
||||
protected EventBus downloadBus;
|
||||
|
||||
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
|
||||
BookCategory.COMMENTARY, BookCategory.DICTIONARY,
|
||||
BookCategory.MAPS };
|
||||
|
||||
/**
|
||||
* Set up the DownloadManager, and notify jSword of where it should store files at
|
||||
*/
|
||||
public DownloadManager() {
|
||||
MinimalBible.getApplication().inject(this);
|
||||
setDownloadDir();
|
||||
refreshModules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the installers available to jSword - this is how we get access to the actual modules
|
||||
* @return All available {@link org.crosswire.jsword.book.install.Installer}s
|
||||
*/
|
||||
public Map<String, Installer> getInstallers() {
|
||||
return new InstallManager().getInstallers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to transform the installers map to an array
|
||||
* @return Array with all available {@link org.crosswire.jsword.book.install.Installer} objects
|
||||
*/
|
||||
public Installer[] getInstallersArray() {
|
||||
Map<String, Installer> installers = getInstallers();
|
||||
return installers.values().toArray(new Installer[installers.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the work of kicking off the AsyncTask to refresh books, and make sure we know
|
||||
* when it's done.
|
||||
*/
|
||||
private void refreshModules() {
|
||||
downloadBus.register(this);
|
||||
new BookRefreshTask(downloadBus).execute(getInstallersArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* When book refresh is done, cache the list so we can give that to someone else
|
||||
* @param event A POJO wrapper around the Book list
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void onEvent(EventBookList event) {
|
||||
this.availableModules = event.getBookList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cached book list
|
||||
* @return The cached book list, or null
|
||||
*/
|
||||
public List<Book> getBookList() {
|
||||
return availableModules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current download bus if you want to know when refresh is done.
|
||||
* Please note that you will not be notified if the book refresh has already
|
||||
* been completed, make sure to check {@link #getBookList()} first.
|
||||
* @return The EventBus the DownloadManager is using
|
||||
*/
|
||||
public EventBus getDownloadBus() {
|
||||
return this.downloadBus;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.bspeice.minimalbible.activities.downloader.manager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.crosswire.jsword.book.Book;
|
||||
|
||||
/**
|
||||
* POJO class for {@link de.greenrobot.event.EventBus} to broadcast whenever
|
||||
* we've finished updating the book list.
|
||||
*/
|
||||
public class EventBookList {
|
||||
|
||||
private List<Book> bookList;
|
||||
|
||||
public EventBookList(List<Book> bookList) {
|
||||
this.bookList = bookList;
|
||||
}
|
||||
|
||||
public List<Book> getBookList() {
|
||||
return bookList;
|
||||
}
|
||||
}
|
@ -0,0 +1,161 @@
|
||||
package org.bspeice.minimalbible.activities.viewer;
|
||||
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.bspeice.minimalbible.R.id;
|
||||
import org.bspeice.minimalbible.R.layout;
|
||||
import org.bspeice.minimalbible.R.menu;
|
||||
import org.bspeice.minimalbible.R.string;
|
||||
import org.bspeice.minimalbible.activities.BaseActivity;
|
||||
import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
|
||||
import org.bspeice.minimalbible.activities.downloader.DownloadActivity;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
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.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.readystatesoftware.systembartint.SystemBarTintManager;
|
||||
|
||||
public class BibleViewer extends BaseActivity implements
|
||||
BaseNavigationDrawerFragment.NavigationDrawerCallbacks {
|
||||
|
||||
/**
|
||||
* Fragment managing the behaviors, interactions and presentation of the
|
||||
* navigation drawer.
|
||||
*/
|
||||
private ViewerNavDrawerFragment mNavigationDrawerFragment;
|
||||
|
||||
/**
|
||||
* Used to store the last screen title. For use in
|
||||
* {@link #restoreActionBar()}.
|
||||
*/
|
||||
private CharSequence mTitle;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_bible_viewer);
|
||||
|
||||
mNavigationDrawerFragment = (ViewerNavDrawerFragment) 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.main, 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;
|
||||
} else if (id == R.id.action_downloads) {
|
||||
startActivity(new Intent(this, DownloadActivity.class));
|
||||
}
|
||||
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_main, 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);
|
||||
((BibleViewer) activity).onSectionAttached(getArguments().getInt(
|
||||
ARG_SECTION_NUMBER));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package org.bspeice.minimalbible.activities.viewer;
|
||||
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
|
||||
|
||||
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;
|
||||
|
||||
public class ViewerNavDrawerFragment 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);
|
||||
}
|
||||
});
|
||||
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar()
|
||||
.getThemedContext(), android.R.layout.simple_list_item_1,
|
||||
android.R.id.text1, new String[] {
|
||||
getString(R.string.title_section1),
|
||||
getString(R.string.title_section2),
|
||||
getString(R.string.title_section3)}));
|
||||
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
|
||||
return mDrawerListView;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user