Provide access to network state

Also fix the no books installed issue (again)
This commit is contained in:
Bradlee Speice 2014-11-06 20:06:17 -05:00
parent 72f375ff9f
commit 23bd5136b5
2 changed files with 36 additions and 57 deletions

View File

@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.bspeice.minimalbible"> package="org.bspeice.minimalbible">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application <application
android:name=".MinimalBible" android:name=".MinimalBible"
android:allowBackup="true" android:allowBackup="true"
@ -10,7 +13,7 @@
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity <activity
android:name=".activity.downloader.DownloadActivity" android:name=".activity.downloader.DownloadActivity"
android:label="@string/app_name"></activity> android:label="@string/app_name" />
<activity <activity
android:name=".activity.viewer.BibleViewer" android:name=".activity.viewer.BibleViewer"
android:label="@string/app_name"> android:label="@string/app_name">
@ -21,7 +24,4 @@
</intent-filter> </intent-filter>
</activity> </activity>
</application> </application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest> </manifest>

View File

@ -2,7 +2,6 @@ package org.bspeice.minimalbible.activity.viewer;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
@ -18,21 +17,20 @@ import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.BaseActivity;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity; import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment; import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment;
import org.bspeice.minimalbible.service.manager.BookManager;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import dagger.ObjectGraph; import dagger.ObjectGraph;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
public class BibleViewer extends BaseActivity implements public class BibleViewer extends BaseActivity implements
NavDrawerFragment.NavigationDrawerCallbacks, NavDrawerFragment.NavigationDrawerCallbacks,
Injector { Injector {
@Inject BookManager bookManager; @Inject
@Named("MainBook")
Book mainBook;
private ObjectGraph bvObjectGraph; private ObjectGraph bvObjectGraph;
/** /**
@ -75,44 +73,35 @@ public class BibleViewer extends BaseActivity implements
* @param savedInstanceState Android's savedInstanceState * @param savedInstanceState Android's savedInstanceState
*/ */
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
this.inject(this); this.inject(this);
// If no books are installed, we need to download one first. However, // If no books are installed, we need to download one first.
// RxJava will error if there's nothing installed. if (mainBook == null) {
bookManager.getInstalledBooks() // If there are no books installed...
.first() Log.e(getLocalClassName(), "No books are currently installed, starting DownloadManager");
.onErrorReturn(new Func1<Throwable, Book>() { Intent i = new Intent(BibleViewer.this, DownloadActivity.class);
@Override startActivityForResult(i, 0);
public Book call(Throwable throwable) { finish();
// If there are no books installed... } else {
Log.e(getLocalClassName(), "No books are currently installed, starting DownloadManager"); FragmentManager fragmentManager = getSupportFragmentManager();
Intent i = new Intent(BibleViewer.this, DownloadActivity.class); Fragment f = BookFragment.newInstance(mainBook.getName());
startActivityForResult(i, 0); fragmentManager.beginTransaction()
finish(); .replace(R.id.container, f)
return null; .commit();
} setContentView(R.layout.activity_bible_viewer);
})
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Book>() {
@Override
public void call(Book book) {
Log.e("BibleViewer", "Subscribed to display book: " + book.getName());
displayMainBook(book);
}
});
setContentView(R.layout.activity_bible_viewer);
mNavigationDrawerFragment = (ExpListNavDrawerFragment) getSupportFragmentManager() mNavigationDrawerFragment = (ExpListNavDrawerFragment) 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) {
@ -134,9 +123,10 @@ public class BibleViewer extends BaseActivity implements
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) { if (mNavigationDrawerFragment != null &&
// Only show items in the action bar relevant to this screen !mNavigationDrawerFragment.isDrawerOpen()) {
// if the drawer is not showing. Otherwise, let the drawer // 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. // decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu); getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar(); restoreActionBar();
@ -158,15 +148,4 @@ public class BibleViewer extends BaseActivity implements
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
private void displayMainBook(Book b) {
Log.d("BibleViewer", "Initializing main book: " + b.getName());
Log.d("MainThread?", Boolean.toString(Looper.myLooper() == Looper.getMainLooper()));
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment f = BookFragment.newInstance(b.getName());
fragmentManager.beginTransaction()
.replace(R.id.container, f)
.commit();
}
} }