mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2024-12-04 13:59:16 -05:00
Can download books!
Progress doesn't show, but I'm about to fix that.
This commit is contained in:
parent
38181c8827
commit
6afb9b6f28
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -4,3 +4,6 @@
|
||||
[submodule "jsword-minimalbible"]
|
||||
path = jsword-minimalbible
|
||||
url = https://github.com/MinimalBible/jsword-minimalbible.git
|
||||
[submodule "ProgressWheel"]
|
||||
path = ProgressWheel
|
||||
url = git@github.com:Todd-Davies/ProgressWheel
|
||||
|
@ -1,30 +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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package org.bspeice.minimalbible.activities.downloader;
|
||||
|
||||
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.DownloadManager;
|
||||
import org.crosswire.common.progress.JobManager;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@ -19,7 +22,9 @@ import de.greenrobot.event.EventBus;
|
||||
BookListFragment.class,
|
||||
DownloadManager.class,
|
||||
BookRefreshTask.class,
|
||||
BookItemHolder.class
|
||||
BookItemHolder.class,
|
||||
BookDownloadManager.class,
|
||||
BookDownloadThread.class
|
||||
}
|
||||
)
|
||||
public class ActivityDownloaderModule {
|
||||
@ -40,7 +45,6 @@ public class ActivityDownloaderModule {
|
||||
return new EventBus();
|
||||
}
|
||||
|
||||
|
||||
@Provides //@Singleton
|
||||
DownloadPrefs provideDownloadPrefs() {
|
||||
return Esperandro.getPreferences(DownloadPrefs.class, MinimalBible.getAppContext());
|
||||
|
@ -18,6 +18,7 @@ import javax.inject.Inject;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
import butterknife.OnClick;
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
/**
|
||||
* Created by bspeice on 5/20/14.
|
||||
@ -54,16 +55,12 @@ public class BookItemHolder {
|
||||
|
||||
@OnClick(R.id.download_ibtn_download)
|
||||
public void onDownloadItem(View v) {
|
||||
downloadManager.getDownloadBus().register(this);
|
||||
downloadManager.getDownloadBus()
|
||||
.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));
|
||||
downloadManager.getRefreshBus().register(this);
|
||||
downloadManager.installBook(this.b);
|
||||
}
|
||||
|
||||
public void onEventMainThread(DownloadProgressEvent event) {
|
||||
if (event.getB().equals(this.b)) {
|
||||
if (event.getB().equals(b)) {
|
||||
displayProgress((int) event.toCircular());
|
||||
}
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ public class BookListFragment extends BaseFragment {
|
||||
List<Book> bookList = downloadManager.getBookList();
|
||||
if (bookList == null) {
|
||||
// downloadManager is in progress of refreshing
|
||||
downloadManager.getDownloadBus().register(this);
|
||||
downloadManager.getRefreshBus().register(this);
|
||||
refreshDialog = new ProgressDialog(getActivity());
|
||||
refreshDialog.setMessage("Refreshing available modules...");
|
||||
refreshDialog.setCancelable(false);
|
||||
|
@ -1,90 +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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,43 +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;
|
||||
}
|
||||
|
||||
}
|
||||
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 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.AsyncTask;
|
||||
import android.provider.ContactsContract;
|
||||
import android.util.Log;
|
||||
|
||||
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.Installer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@ -37,7 +41,7 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
||||
|
||||
@Override
|
||||
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;
|
||||
for (Installer i : params) {
|
||||
@ -51,13 +55,14 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
||||
+ i.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
books.addAll(i.getBooks());
|
||||
bookList.put(i, i.getBooks());
|
||||
publishProgress(++index, params.length);
|
||||
}
|
||||
|
||||
downloadBus.postSticky(new EventBookList(books));
|
||||
return books;
|
||||
EventBookList event = new EventBookList(bookList);
|
||||
downloadBus.post(event);
|
||||
|
||||
return event.getBookList();
|
||||
}
|
||||
|
||||
private boolean doRefresh() {
|
||||
|
@ -3,11 +3,14 @@ package org.bspeice.minimalbible.activities.downloader.manager;
|
||||
import android.util.Log;
|
||||
|
||||
import org.bspeice.minimalbible.MinimalBible;
|
||||
import org.crosswire.common.util.CWProject;
|
||||
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 org.crosswire.jsword.book.sword.SwordBookPath;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -18,6 +21,7 @@ import javax.inject.Singleton;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
// TODO: Rename to RefreshManager? Refactor to RefreshManager?
|
||||
@Singleton
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
@ -34,7 +38,9 @@ public class DownloadManager {
|
||||
private Map<Book, DownloadProgressEvent> inProgressDownloads;
|
||||
|
||||
@Inject
|
||||
protected EventBus downloadBus;
|
||||
protected EventBus refreshBus;
|
||||
|
||||
@Inject BookDownloadManager bookDownloadManager;
|
||||
|
||||
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
|
||||
BookCategory.COMMENTARY, BookCategory.DICTIONARY,
|
||||
@ -46,7 +52,10 @@ public class DownloadManager {
|
||||
public DownloadManager() {
|
||||
MinimalBible.getApplication().inject(this);
|
||||
setDownloadDir();
|
||||
|
||||
availableModules = new HashMap<Installer, List<Book>>();
|
||||
refreshModules();
|
||||
|
||||
inProgressDownloads = new HashMap<Book, DownloadProgressEvent>();
|
||||
}
|
||||
|
||||
@ -78,6 +87,9 @@ public class DownloadManager {
|
||||
String home = MinimalBible.getAppContext().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());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,8 +97,8 @@ public class DownloadManager {
|
||||
* when it's done.
|
||||
*/
|
||||
private void refreshModules() {
|
||||
downloadBus.register(this);
|
||||
new BookRefreshTask(downloadBus).execute(getInstallersArray());
|
||||
refreshBus.register(this);
|
||||
new BookRefreshTask(refreshBus).execute(getInstallersArray());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,7 +107,7 @@ public class DownloadManager {
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
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
|
||||
*/
|
||||
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.
|
||||
* @return The EventBus the DownloadManager is using
|
||||
*/
|
||||
public EventBus getDownloadBus() {
|
||||
return this.downloadBus;
|
||||
public EventBus getRefreshBus() {
|
||||
return this.refreshBus;
|
||||
}
|
||||
|
||||
// TODO: All code below should be migrated to BookDownloadManager
|
||||
|
||||
/**
|
||||
* Handle a book download progress event.
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,8 +12,13 @@ public class DownloadProgressEvent {
|
||||
public static final int PROGRESS_COMPLETE = 100;
|
||||
public static final int PROGRESS_BEGINNING = 0;
|
||||
|
||||
public DownloadProgressEvent(int progress, Book b) {
|
||||
this.progress = progress;
|
||||
public DownloadProgressEvent(int workDone, int totalWork, Book b) {
|
||||
this.progress = workDone / totalWork;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public DownloadProgressEvent(int workDone, Book b) {
|
||||
this.progress = workDone;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
@ -21,10 +26,6 @@ public class DownloadProgressEvent {
|
||||
return progress;
|
||||
}
|
||||
|
||||
public Book getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public float toCircular() {
|
||||
return ((float)progress) * 360 / 100;
|
||||
}
|
||||
@ -32,4 +33,8 @@ public class DownloadProgressEvent {
|
||||
public boolean isComplete() {
|
||||
return progress >= 100;
|
||||
}
|
||||
|
||||
public Book getB() {
|
||||
return this.b;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package org.bspeice.minimalbible.activities.downloader.manager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.crosswire.jsword.book.Book;
|
||||
import org.crosswire.jsword.book.install.Installer;
|
||||
|
||||
/**
|
||||
* POJO class for {@link de.greenrobot.event.EventBus} to broadcast whenever
|
||||
@ -10,13 +13,21 @@ import org.crosswire.jsword.book.Book;
|
||||
*/
|
||||
public class EventBookList {
|
||||
|
||||
private List<Book> bookList;
|
||||
private Map<Installer, List<Book>> bookMapping;
|
||||
|
||||
public EventBookList(List<Book> bookList) {
|
||||
this.bookList = bookList;
|
||||
public EventBookList(Map<Installer, List<Book>> bookList) {
|
||||
this.bookMapping = bookList;
|
||||
}
|
||||
|
||||
public List<Book> getBookList() {
|
||||
return bookList;
|
||||
|
||||
public Map<Installer, List<Book>> getInstallerMapping() {
|
||||
return bookMapping;
|
||||
}
|
||||
|
||||
public List<Book> getBookList() {
|
||||
List<Book> bookList = new ArrayList<Book>();
|
||||
for (Installer i: bookMapping.keySet()) {
|
||||
bookList.addAll(bookMapping.get(i));
|
||||
}
|
||||
return bookList;
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user