From 6afb9b6f28428b309a4f617482d7ae0deca0f933 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 24 May 2014 21:12:20 -0400 Subject: [PATCH] Can download books! Progress doesn't show, but I'm about to fix that. --- .gitmodules | 3 + .../minimalbible/activities/BaseActivity.java | 60 +++--- .../downloader/ActivityDownloaderModule.java | 8 +- .../activities/downloader/BookItemHolder.java | 11 +- .../downloader/BookListFragment.java | 2 +- .../downloader/DownloadActivity.java | 180 +++++++++--------- .../downloader/DownloadNavDrawerFragment.java | 86 ++++----- .../manager/BookDownloadManager.java | 72 +++++++ .../manager/BookDownloadThread.java | 63 ++++++ .../downloader/manager/BookRefreshTask.java | 15 +- .../downloader/manager/DownloadManager.java | 56 +++++- .../manager/DownloadProgressEvent.java | 17 +- .../downloader/manager/EventBookList.java | 23 ++- .../viewer/ViewerNavDrawerFragment.java | 76 ++++---- 14 files changed, 436 insertions(+), 236 deletions(-) create mode 100644 MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java create mode 100644 MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadThread.java diff --git a/.gitmodules b/.gitmodules index f05ad71..613789c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/BaseActivity.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/BaseActivity.java index de85f3d..0617e6a 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/BaseActivity.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/BaseActivity.java @@ -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)); + } + } + +} diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java index 78e64eb..d6e1efe 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java @@ -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()); diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java index b63e9d9..26ab5e2 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java @@ -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()); } } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListFragment.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListFragment.java index 8a7030e..5405c0b 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListFragment.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListFragment.java @@ -119,7 +119,7 @@ public class BookListFragment extends BaseFragment { List 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); diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java index 765854f..b00d505 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/DownloadActivity.java @@ -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); + } +} diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java index a742ae1..203afba 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/DownloadNavDrawerFragment.java @@ -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(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(getActionBar() + .getThemedContext(), android.R.layout.simple_list_item_1, + android.R.id.text1, sCategories)); + mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); + return mDrawerListView; + } + +} diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java new file mode 100644 index 0000000..e4733ab --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java @@ -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 bookMappings; + + @Inject + Provider dlThreadProvider; + + /* Going to fix this in the next commit, right now it's circular + @Inject + */ + DownloadManager downloadManager; + + public BookDownloadManager() { + bookMappings = new HashMap(); + 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()); + } + +} diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadThread.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadThread.java new file mode 100644 index 0000000..7ab7e5a --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadThread.java @@ -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(); + } +} diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java index 778c1d5..40179e9 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookRefreshTask.java @@ -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> { @Override protected List doInBackground(Installer... params) { - List books = new LinkedList(); + Map> bookList = new HashMap>(); int index = 0; for (Installer i : params) { @@ -51,13 +55,14 @@ public class BookRefreshTask extends AsyncTask> { + 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() { diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java index 56b3b95..b665005 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java @@ -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 availableModules; + private Map> 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 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>(); refreshModules(); + inProgressDownloads = new HashMap(); } @@ -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 getBookList() { - return availableModules; + if (availableModules.values().size() == 0) { + return null; + } else { + List bookList = new ArrayList(); + for (List 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> entry : availableModules.entrySet()) { + if (entry.getValue().contains(b)) { + return entry.getKey(); + } + } + return null; + } + + public void installBook(Book b) { + bookDownloadManager.downloadBook(b); + } + } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadProgressEvent.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadProgressEvent.java index 7182f27..45d109c 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadProgressEvent.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadProgressEvent.java @@ -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; + } } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/EventBookList.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/EventBookList.java index 1b4d0da..353788f 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/EventBookList.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/EventBookList.java @@ -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 bookList; + private Map> bookMapping; - public EventBookList(List bookList) { - this.bookList = bookList; + public EventBookList(Map> bookList) { + this.bookMapping = bookList; } - - public List getBookList() { - return bookList; + + public Map> getInstallerMapping() { + return bookMapping; } + + public List getBookList() { + List bookList = new ArrayList(); + for (Installer i: bookMapping.keySet()) { + bookList.addAll(bookMapping.get(i)); + } + return bookList; + } } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/ViewerNavDrawerFragment.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/ViewerNavDrawerFragment.java index 2889f0e..46087f4 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/ViewerNavDrawerFragment.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/ViewerNavDrawerFragment.java @@ -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(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(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; + } +}