reader/android: initial working version of pdf.js based rendering

- updates the gradle script to build and copy the pdf.js files.
- implements initial working version of pdf.js based rendering.
  file is rendered within the webview, and prev/next page buttons work

Change-Id: Id53fcf2d6c7326b9b7fb14eef155df80722c7c07
diff --git a/android/.gitignore b/android/.gitignore
index 46e2cab..eeceedf 100644
--- a/android/.gitignore
+++ b/android/.gitignore
@@ -5,3 +5,5 @@
 /build
 /captures
 **/*.iml
+**/assets/pdfjs/pdf*
+
diff --git a/android/app/build.gradle b/android/app/build.gradle
index 5618fa5..4346593 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -68,7 +68,6 @@
     compile 'com.android.support:appcompat-v7:22.2.1'
     compile 'com.android.support:cardview-v7:22.2.1'
     compile 'com.android.support:recyclerview-v7:22.2.1'
-    compile 'com.joanzapata.pdfview:android-pdfview:1.0.4@aar'
     compile 'io.v:vanadium-android:0.1'
 }
 
@@ -76,3 +75,20 @@
     inputPaths += new File(projectDir, '../../common').canonicalPath
     packageTranslations += 'vdl->io/v/android/apps/reader/vdl'
 }
+
+// The two tasks below are used for copying the pdf.js files into assets/pdfjs directory.
+task generatePdfJsFiles(type: Exec) {
+    workingDir '../../web'
+    commandLine 'make', 'public/pdf-web-view.js'
+}
+
+task copyPdfJsFiles(type: Copy, dependsOn: 'generatePdfJsFiles') {
+    from('../../web/public') {
+        include 'pdf*'
+    }
+
+    into 'src/main/assets/pdfjs'
+}
+
+// These two tasks should run before 'preBuild' task
+tasks['preBuild'].dependsOn('copyPdfJsFiles')
diff --git a/android/app/src/main/assets/pdfjs/index.html b/android/app/src/main/assets/pdfjs/index.html
deleted file mode 100644
index fa867f8..0000000
--- a/android/app/src/main/assets/pdfjs/index.html
+++ /dev/null
@@ -1,12 +0,0 @@
-<!DOCTYPE html>
-
-<html>
-
-<head>
-    <script src="./test.js" type="text/javascript"></script>
-</head>
-
-<body>
-</body>
-
-</html>
diff --git a/android/app/src/main/assets/pdfjs/test.js b/android/app/src/main/assets/pdfjs/test.js
deleted file mode 100644
index cfc4ae3..0000000
--- a/android/app/src/main/assets/pdfjs/test.js
+++ /dev/null
@@ -1,29 +0,0 @@
-// Copyright 2015 The Vanadium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-"use strict";
-
-function getParameterByName(name) {
-    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
-    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
-        results = regex.exec(location.search);
-    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
-}
-
-window.onload = function() {
-    var xhr = new XMLHttpRequest();
-    xhr.responseType = "arraybuffer";
-
-    var filePath = getParameterByName("file");
-    console.log("file path: " + filePath);
-    xhr.open("GET", filePath);
-
-    xhr.onload = function() {
-        // response should be an ArrayBuffer object.
-        var response = xhr.response;
-        console.log("arraybuffer received of length: " + response.byteLength);
-    };
-
-    xhr.send();
-};
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 54c4cc1..5d1d3d6 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
@@ -59,6 +59,16 @@
                 }
             }
         });
+
+        loadUrl("file:///android_asset/pdfjs/pdf-web-view.html");
+    }
+
+    /**
+     * Loads the PDF file at the given path into the pdf.js component within WebView.
+     * NOTE: must be called after the page loading is finished.
+     */
+    public void loadPdfFile(String filePath) {
+        evaluateJavascript("window.atom.href.set(\"" + filePath + "\");", null);
     }
 
     /**
@@ -67,11 +77,13 @@
      * @param page the page number to jump to. Page number is one-based.
      */
     public void setPage(int page) {
-        // TODO(youngseokyoon): implement this.
+        evaluateJavascript("window.atom.pages.current.set(" + page + ");", null);
     }
 
     public int getPageCount() {
-        return 0;
+        // 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;
     }
 
 }
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 a97099d..3322f93 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
@@ -13,6 +13,8 @@
 import android.provider.OpenableColumns;
 import android.util.Log;
 import android.view.View;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
 import android.widget.Button;
 import android.widget.Toast;
 
@@ -233,7 +235,7 @@
 
         // The pdf viewer widget requires the file to be an actual java.io.File object.
         // Create a temporary file and write the contents.
-        java.io.File jFile = new java.io.File(getCacheDir(), ds.getFileId());
+        final java.io.File jFile = new java.io.File(getCacheDir(), ds.getFileId());
         try (FileOutputStream out = new FileOutputStream(jFile)) {
             out.write(bytes);
         } catch (IOException e) {
@@ -242,7 +244,15 @@
 
         // Initialize the pdf viewer widget with the file content.
         Log.i(TAG, "File path: " + jFile.getPath());
-        mPdfView.loadUrl("file:///android_asset/pdfjs/index.html?file=" + jFile.getPath());
+
+        // TODO(youngseokyoon): move this logic to PdfViewWrapper
+        mPdfView.setWebViewClient(new WebViewClient() {
+            @Override
+            public void onPageFinished(WebView view, String url) {
+                super.onPageFinished(view, url);
+                mPdfView.loadPdfFile(jFile.getPath());
+            }
+        });
 
         // Create a new device meta, and update the device set with it.
         Log.i(TAG, "Joining device set: " + ds.getId());