Can download books!

Progress doesn't show, but I'm about to fix that.
This commit is contained in:
Bradlee Speice 2014-05-24 21:12:20 -04:00
parent 38181c8827
commit 6afb9b6f28
14 changed files with 436 additions and 236 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "jsword-minimalbible"] [submodule "jsword-minimalbible"]
path = jsword-minimalbible path = jsword-minimalbible
url = https://github.com/MinimalBible/jsword-minimalbible.git url = https://github.com/MinimalBible/jsword-minimalbible.git
[submodule "ProgressWheel"]
path = ProgressWheel
url = git@github.com:Todd-Davies/ProgressWheel

View File

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

View File

@ -1,8 +1,11 @@
package org.bspeice.minimalbible.activities.downloader; package org.bspeice.minimalbible.activities.downloader;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManager;
import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadThread;
import org.bspeice.minimalbible.activities.downloader.manager.BookRefreshTask; import org.bspeice.minimalbible.activities.downloader.manager.BookRefreshTask;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
import org.crosswire.common.progress.JobManager;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -19,7 +22,9 @@ import de.greenrobot.event.EventBus;
BookListFragment.class, BookListFragment.class,
DownloadManager.class, DownloadManager.class,
BookRefreshTask.class, BookRefreshTask.class,
BookItemHolder.class BookItemHolder.class,
BookDownloadManager.class,
BookDownloadThread.class
} }
) )
public class ActivityDownloaderModule { public class ActivityDownloaderModule {
@ -40,7 +45,6 @@ public class ActivityDownloaderModule {
return new EventBus(); return new EventBus();
} }
@Provides //@Singleton @Provides //@Singleton
DownloadPrefs provideDownloadPrefs() { DownloadPrefs provideDownloadPrefs() {
return Esperandro.getPreferences(DownloadPrefs.class, MinimalBible.getAppContext()); return Esperandro.getPreferences(DownloadPrefs.class, MinimalBible.getAppContext());

View File

@ -18,6 +18,7 @@ import javax.inject.Inject;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.InjectView; import butterknife.InjectView;
import butterknife.OnClick; import butterknife.OnClick;
import de.greenrobot.event.EventBus;
/** /**
* Created by bspeice on 5/20/14. * Created by bspeice on 5/20/14.
@ -54,16 +55,12 @@ public class BookItemHolder {
@OnClick(R.id.download_ibtn_download) @OnClick(R.id.download_ibtn_download)
public void onDownloadItem(View v) { public void onDownloadItem(View v) {
downloadManager.getDownloadBus().register(this); downloadManager.getRefreshBus().register(this);
downloadManager.getDownloadBus() downloadManager.installBook(this.b);
.post(new DownloadProgressEvent(DownloadProgressEvent.PROGRESS_BEGINNING, b));
// TODO: Kick off a service to actually do the downloading, rather than simulate
downloadManager.getDownloadBus().post(new DownloadProgressEvent(47, b));
} }
public void onEventMainThread(DownloadProgressEvent event) { public void onEventMainThread(DownloadProgressEvent event) {
if (event.getB().equals(this.b)) { if (event.getB().equals(b)) {
displayProgress((int) event.toCircular()); displayProgress((int) event.toCircular());
} }
} }

View File

@ -119,7 +119,7 @@ public class BookListFragment extends BaseFragment {
List<Book> bookList = downloadManager.getBookList(); List<Book> bookList = downloadManager.getBookList();
if (bookList == null) { if (bookList == null) {
// downloadManager is in progress of refreshing // downloadManager is in progress of refreshing
downloadManager.getDownloadBus().register(this); downloadManager.getRefreshBus().register(this);
refreshDialog = new ProgressDialog(getActivity()); refreshDialog = new ProgressDialog(getActivity());
refreshDialog.setMessage("Refreshing available modules..."); refreshDialog.setMessage("Refreshing available modules...");
refreshDialog.setCancelable(false); refreshDialog.setCancelable(false);

View File

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

View File

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

View File

@ -0,0 +1,72 @@
package org.bspeice.minimalbible.activities.downloader.manager;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
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 java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import de.greenrobot.event.EventBus;
/**
* Wrapper to convert JSword progress events to MinimalBible EventBus-based
*/
@Singleton
public class BookDownloadManager implements WorkListener{
/**
* Mapping of Job ID to the EventBus we should trigger progress on
*/
private Map<String, Book> bookMappings;
@Inject
Provider<BookDownloadThread> dlThreadProvider;
/* Going to fix this in the next commit, right now it's circular
@Inject
*/
DownloadManager downloadManager;
public BookDownloadManager() {
bookMappings = new HashMap<String, Book>();
JobManager.addWorkListener(this);
MinimalBible.getApplication().inject(this);
}
public void downloadBook(Book b) {
BookDownloadThread dlThread = dlThreadProvider.get();
dlThread.downloadBook(b);
addJob(BookDownloadThread.getJobId(b), b);
}
public void addJob(String jobId, Book b) {
bookMappings.put(jobId, b);
}
@Override
public void workProgressed(WorkEvent ev) {
Log.d("BookDownloadManager", ev.toString());
Progress job = ev.getJob();
if (bookMappings.containsKey(job.getJobID())) {
downloadManager.getRefreshBus()
.post(new DownloadProgressEvent(job.getTotalWork(), job.getWorkDone(),
bookMappings.get(job.getJobID())));
}
}
@Override
public void workStateChanged(WorkEvent ev) {
Log.d("BookDownloadManager", ev.toString());
}
}

View File

@ -0,0 +1,63 @@
package org.bspeice.minimalbible.activities.downloader.manager;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.common.progress.JobManager;
import org.crosswire.common.progress.WorkEvent;
import org.crosswire.common.progress.WorkListener;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer;
import java.util.UUID;
import javax.inject.Inject;
import de.greenrobot.event.EventBus;
/**
* Created by bspeice on 5/12/14.
*/
public class BookDownloadThread {
private final String TAG = "BookDownloadThread";
@Inject
DownloadManager downloadManager;
public BookDownloadThread() {
MinimalBible.getApplication().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
final Installer i = downloadManager.installerFromBook(b);
final Thread worker = new Thread() {
@Override
public void run() {
try {
i.install(b);
} catch (InstallException e) {
Log.d(TAG, e.getMessage());
}
}
};
worker.start();
JobManager.createJob(getJobId(b), b.getName(), worker);
downloadManager.getRefreshBus().post(new DownloadProgressEvent(DownloadProgressEvent.PROGRESS_BEGINNING, b));
}
/**
* Return a job ID for a given book. Must be consistent per book.
* @param b
* @return A string representing this job IDs
*/
public static String getJobId(Book b) {
return b.toString();
}
}

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
@ -12,8 +13,11 @@ import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.InstallException; import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.book.install.Installer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
@ -37,7 +41,7 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
@Override @Override
protected List<Book> doInBackground(Installer... params) { protected List<Book> doInBackground(Installer... params) {
List<Book> books = new LinkedList<Book>(); Map<Installer, List<Book>> bookList = new HashMap<Installer, List<Book>>();
int index = 0; int index = 0;
for (Installer i : params) { for (Installer i : params) {
@ -51,13 +55,14 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
+ i.toString(), e); + i.toString(), e);
} }
} }
bookList.put(i, i.getBooks());
books.addAll(i.getBooks());
publishProgress(++index, params.length); publishProgress(++index, params.length);
} }
downloadBus.postSticky(new EventBookList(books)); EventBookList event = new EventBookList(bookList);
return books; downloadBus.post(event);
return event.getBookList();
} }
private boolean doRefresh() { private boolean doRefresh() {

View File

@ -3,11 +3,14 @@ package org.bspeice.minimalbible.activities.downloader.manager;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.common.util.CWProject;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.book.install.Installer;
import org.crosswire.jsword.book.sword.SwordBookPath;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -18,6 +21,7 @@ import javax.inject.Singleton;
import de.greenrobot.event.EventBus; import de.greenrobot.event.EventBus;
// TODO: Rename to RefreshManager? Refactor to RefreshManager?
@Singleton @Singleton
public class DownloadManager { public class DownloadManager {
@ -26,7 +30,7 @@ public class DownloadManager {
/** /**
* Cached copy of modules that are available so we don't refresh for everyone who requests it. * Cached copy of modules that are available so we don't refresh for everyone who requests it.
*/ */
private List<Book> availableModules; private Map<Installer, List<Book>> availableModules;
/** /**
* Cached copy of downloads in progress so views displaying this info can get it quickly. * Cached copy of downloads in progress so views displaying this info can get it quickly.
@ -34,7 +38,9 @@ public class DownloadManager {
private Map<Book, DownloadProgressEvent> inProgressDownloads; private Map<Book, DownloadProgressEvent> inProgressDownloads;
@Inject @Inject
protected EventBus downloadBus; protected EventBus refreshBus;
@Inject BookDownloadManager bookDownloadManager;
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE, public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
BookCategory.COMMENTARY, BookCategory.DICTIONARY, BookCategory.COMMENTARY, BookCategory.DICTIONARY,
@ -46,7 +52,10 @@ public class DownloadManager {
public DownloadManager() { public DownloadManager() {
MinimalBible.getApplication().inject(this); MinimalBible.getApplication().inject(this);
setDownloadDir(); setDownloadDir();
availableModules = new HashMap<Installer, List<Book>>();
refreshModules(); refreshModules();
inProgressDownloads = new HashMap<Book, DownloadProgressEvent>(); inProgressDownloads = new HashMap<Book, DownloadProgressEvent>();
} }
@ -78,6 +87,9 @@ public class DownloadManager {
String home = MinimalBible.getAppContext().getFilesDir().toString(); String home = MinimalBible.getAppContext().getFilesDir().toString();
Log.d(TAG, "Setting jsword.home to: " + home); Log.d(TAG, "Setting jsword.home to: " + home);
System.setProperty("jsword.home", home); System.setProperty("jsword.home", home);
System.setProperty("sword.home", home);
SwordBookPath.setDownloadDir(new File(home));
Log.d(TAG, "Sword download path: " + SwordBookPath.getSwordDownloadDir());
} }
/** /**
@ -85,8 +97,8 @@ public class DownloadManager {
* when it's done. * when it's done.
*/ */
private void refreshModules() { private void refreshModules() {
downloadBus.register(this); refreshBus.register(this);
new BookRefreshTask(downloadBus).execute(getInstallersArray()); new BookRefreshTask(refreshBus).execute(getInstallersArray());
} }
/** /**
@ -95,7 +107,7 @@ public class DownloadManager {
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public void onEvent(EventBookList event) { public void onEvent(EventBookList event) {
this.availableModules = event.getBookList(); this.availableModules = event.getInstallerMapping();
} }
/** /**
@ -103,7 +115,15 @@ public class DownloadManager {
* @return The cached book list, or null * @return The cached book list, or null
*/ */
public List<Book> getBookList() { public List<Book> getBookList() {
return availableModules; if (availableModules.values().size() == 0) {
return null;
} else {
List<Book> bookList = new ArrayList<Book>();
for (List<Book> l : availableModules.values()) {
bookList.addAll(l);
}
return bookList;
}
} }
/** /**
@ -112,10 +132,12 @@ public class DownloadManager {
* been completed, make sure to check {@link #getBookList()} first. * been completed, make sure to check {@link #getBookList()} first.
* @return The EventBus the DownloadManager is using * @return The EventBus the DownloadManager is using
*/ */
public EventBus getDownloadBus() { public EventBus getRefreshBus() {
return this.downloadBus; return this.refreshBus;
} }
// TODO: All code below should be migrated to BookDownloadManager
/** /**
* Handle a book download progress event. * Handle a book download progress event.
* Mostly responsible for caching the in progress status to check on it easily * Mostly responsible for caching the in progress status to check on it easily
@ -142,4 +164,22 @@ public class DownloadManager {
} }
} }
/**
* 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 Installer installerFromBook(Book b) {
for (Map.Entry<Installer, List<Book>> entry : availableModules.entrySet()) {
if (entry.getValue().contains(b)) {
return entry.getKey();
}
}
return null;
}
public void installBook(Book b) {
bookDownloadManager.downloadBook(b);
}
} }

View File

@ -12,8 +12,13 @@ public class DownloadProgressEvent {
public static final int PROGRESS_COMPLETE = 100; public static final int PROGRESS_COMPLETE = 100;
public static final int PROGRESS_BEGINNING = 0; public static final int PROGRESS_BEGINNING = 0;
public DownloadProgressEvent(int progress, Book b) { public DownloadProgressEvent(int workDone, int totalWork, Book b) {
this.progress = progress; this.progress = workDone / totalWork;
this.b = b;
}
public DownloadProgressEvent(int workDone, Book b) {
this.progress = workDone;
this.b = b; this.b = b;
} }
@ -21,10 +26,6 @@ public class DownloadProgressEvent {
return progress; return progress;
} }
public Book getB() {
return b;
}
public float toCircular() { public float toCircular() {
return ((float)progress) * 360 / 100; return ((float)progress) * 360 / 100;
} }
@ -32,4 +33,8 @@ public class DownloadProgressEvent {
public boolean isComplete() { public boolean isComplete() {
return progress >= 100; return progress >= 100;
} }
public Book getB() {
return this.b;
}
} }

View File

@ -1,8 +1,11 @@
package org.bspeice.minimalbible.activities.downloader.manager; package org.bspeice.minimalbible.activities.downloader.manager;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.Installer;
/** /**
* POJO class for {@link de.greenrobot.event.EventBus} to broadcast whenever * POJO class for {@link de.greenrobot.event.EventBus} to broadcast whenever
@ -10,13 +13,21 @@ import org.crosswire.jsword.book.Book;
*/ */
public class EventBookList { public class EventBookList {
private List<Book> bookList; private Map<Installer, List<Book>> bookMapping;
public EventBookList(List<Book> bookList) { public EventBookList(Map<Installer, List<Book>> bookList) {
this.bookList = bookList; this.bookMapping = bookList;
} }
public List<Book> getBookList() { public Map<Installer, List<Book>> getInstallerMapping() {
return bookList; return bookMapping;
} }
public List<Book> getBookList() {
List<Book> bookList = new ArrayList<Book>();
for (Installer i: bookMapping.keySet()) {
bookList.addAll(bookMapping.get(i));
}
return bookList;
}
} }

View File

@ -1,38 +1,38 @@
package org.bspeice.minimalbible.activities.viewer; package org.bspeice.minimalbible.activities.viewer;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment; import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
public class ViewerNavDrawerFragment extends BaseNavigationDrawerFragment { public class ViewerNavDrawerFragment extends BaseNavigationDrawerFragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate( mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false); R.layout.fragment_navigation_drawer, container, false);
mDrawerListView mDrawerListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() { .setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, public void onItemClick(AdapterView<?> parent, View view,
int position, long id) { int position, long id) {
selectItem(position); selectItem(position);
} }
}); });
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar() mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar()
.getThemedContext(), android.R.layout.simple_list_item_1, .getThemedContext(), android.R.layout.simple_list_item_1,
android.R.id.text1, new String[] { android.R.id.text1, new String[] {
getString(R.string.title_section1), getString(R.string.title_section1),
getString(R.string.title_section2), getString(R.string.title_section2),
getString(R.string.title_section3)})); getString(R.string.title_section3)}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView; return mDrawerListView;
} }
} }