reader/android: set the correct page count from the JS side.

REL: #44
Change-Id: I25ee438bedf0be3ed1b1c9f1b00b6e9a4e87f430
diff --git a/android/app/src/main/java/io/v/android/apps/reader/PdfViewWrapper.java b/android/app/src/main/java/io/v/android/apps/reader/PdfViewWrapper.java
index 5d1d3d6..4af173b 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/PdfViewWrapper.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/PdfViewWrapper.java
@@ -7,6 +7,7 @@
 import android.content.Context;
 import android.util.AttributeSet;
 import android.util.Log;
+import android.webkit.JavascriptInterface;
 import android.webkit.WebChromeClient;
 import android.webkit.WebResourceRequest;
 import android.webkit.WebResourceResponse;
@@ -28,6 +29,8 @@
 
     private static final String TAG = PdfViewWrapper.class.getSimpleName();
 
+    private int mPageCount;
+
     public PdfViewWrapper(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
@@ -60,6 +63,8 @@
             }
         });
 
+        addJavascriptInterface(new JSInterface(), "android");
+
         loadUrl("file:///android_asset/pdfjs/pdf-web-view.html");
     }
 
@@ -69,6 +74,9 @@
      */
     public void loadPdfFile(String filePath) {
         evaluateJavascript("window.atom.href.set(\"" + filePath + "\");", null);
+
+        // leave the page count as 0 until the page count value is properly set from JS side.
+        mPageCount = 0;
     }
 
     /**
@@ -81,9 +89,21 @@
     }
 
     public int getPageCount() {
-        // TODO(youngseokyoon): provide a JS interface to set the page count from the JS side.
-        // For now, just return 10 for testing purposes.
-        return 10;
+        return mPageCount;
+    }
+
+    /**
+     * This class provides public methods that can be called from the JavaScript side.
+     */
+    private class JSInterface {
+
+        private final String TAG = JSInterface.class.getSimpleName();
+
+        @JavascriptInterface
+        public void setPageCount(int pageCount) {
+            Log.d(TAG, "setPageCount(" + pageCount + ") called.");
+            mPageCount = pageCount;
+        }
     }
 
 }
diff --git a/android/app/src/main/java/io/v/android/apps/reader/PdfViewerActivity.java b/android/app/src/main/java/io/v/android/apps/reader/PdfViewerActivity.java
index 3322f93..9be1342 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/PdfViewerActivity.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/PdfViewerActivity.java
@@ -331,7 +331,7 @@
      * Move all the linked pages to their previous pages.
      */
     private void prevPage() {
-        if (mCurrentDS == null) {
+        if (mCurrentDS == null || mPdfView.getPageCount() <= 0) {
             return;
         }
 
@@ -364,7 +364,7 @@
      * Move all the linked pages to their next pages.
      */
     private void nextPage() {
-        if (mCurrentDS == null) {
+        if (mCurrentDS == null || mPdfView.getPageCount() <= 0) {
             return;
         }
 
diff --git a/web/browser/pdf-web-view.js b/web/browser/pdf-web-view.js
index 8e3e220..42071d0 100644
--- a/web/browser/pdf-web-view.js
+++ b/web/browser/pdf-web-view.js
@@ -66,6 +66,12 @@
     pdf.getPage(current).then(success, error);
   });
 
+  // Watch for the total page number changes and give the new value to the
+  // Android client.
+  atom.pages.total(function totalchange(current) {
+    window.android.setPageCount(current);
+  });
+
   // Watch for changes on the PDF.js page object. When it is updated trigger a
   // render.
   // TODO(jasoncampbell): To prevent rendering errors with frequent state