reader/android: prepare WebView for embedding pdf.js

This sets up the WebView to intercept resource requests and returns an
InputStream of the pdf file content. From WebView, the JavaScript code
can make an xhr and receive an arraybuffer of the content.

Change-Id: Ib8b94a09728dbf10e9de2fe16b51bdce27c8e5a8
diff --git a/android/app/src/main/assets/pdfjs/index.html b/android/app/src/main/assets/pdfjs/index.html
new file mode 100644
index 0000000..fa867f8
--- /dev/null
+++ b/android/app/src/main/assets/pdfjs/index.html
@@ -0,0 +1,12 @@
+<!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
new file mode 100644
index 0000000..cfc4ae3
--- /dev/null
+++ b/android/app/src/main/assets/pdfjs/test.js
@@ -0,0 +1,29 @@
+// 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 2ea4544..54c4cc1 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
@@ -6,27 +6,72 @@
 
 import android.content.Context;
 import android.util.AttributeSet;
+import android.util.Log;
+import android.webkit.WebChromeClient;
+import android.webkit.WebResourceRequest;
+import android.webkit.WebResourceResponse;
+import android.webkit.WebSettings;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
 
-import com.joanzapata.pdfview.PDFView;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 
 /**
  * Wrapper class for the PDF Viewer library.
  *
  * May be replaced with another library if needed.
  */
-public class PdfViewWrapper extends PDFView {
+public class PdfViewWrapper extends WebView {
+
+    private static final String TAG = PdfViewWrapper.class.getSimpleName();
 
     public PdfViewWrapper(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
 
+    public void init() {
+        WebSettings settings = getSettings();
+        settings.setJavaScriptEnabled(true);
+        settings.setAllowUniversalAccessFromFileURLs(true);
+
+        setWebChromeClient(new WebChromeClient());
+
+        setWebViewClient(new WebViewClient() {
+            @Override
+            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
+                Log.i(TAG, "shouldInterceptRequest called");
+
+                File file = new File(request.getUrl().getPath());
+                Log.i(TAG, "file path: " + file.getPath());
+
+                try {
+                    // Should NOT close the stream here, so that the stream can be read by WebView.
+                    InputStream inputStream = new FileInputStream(file);
+
+                    Log.i(TAG, "returning a custom WebResourceResponse");
+                    return new WebResourceResponse("application/pdf", "binary", inputStream);
+                } catch (IOException e) {
+                    Log.i(TAG, "falling back to super.shouldInterceptRequest");
+                    return super.shouldInterceptRequest(view, request);
+                }
+            }
+        });
+    }
+
     /**
      * Jumps to the given page number. Page number is one-based.
      *
      * @param page the page number to jump to. Page number is one-based.
      */
     public void setPage(int page) {
-        jumpTo(page);
+        // TODO(youngseokyoon): implement this.
+    }
+
+    public int getPageCount() {
+        return 0;
     }
 
 }
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 388413f..a97099d 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
@@ -76,6 +76,7 @@
         setContentView(R.layout.activity_pdf_viewer);
 
         mPdfView = (PdfViewWrapper) findViewById(R.id.pdfview);
+        mPdfView.init();
 
         mButtonPrev = (Button) findViewById(R.id.button_prev);
         mButtonNext = (Button) findViewById(R.id.button_next);
@@ -240,10 +241,8 @@
         }
 
         // Initialize the pdf viewer widget with the file content.
-        // TODO(youngseokyoon): enable swipe and handle the page change events.
-        mPdfView.fromFile(jFile)
-                .enableSwipe(false)
-                .load();
+        Log.i(TAG, "File path: " + jFile.getPath());
+        mPdfView.loadUrl("file:///android_asset/pdfjs/index.html?file=" + jFile.getPath());
 
         // Create a new device meta, and update the device set with it.
         Log.i(TAG, "Joining device set: " + ds.getId());
diff --git a/android/app/src/main/java/io/v/android/apps/reader/db/DB.java b/android/app/src/main/java/io/v/android/apps/reader/db/DB.java
index 582c304..a5b781c 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/db/DB.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/db/DB.java
@@ -30,8 +30,8 @@
                     result = instance;
                     if (result == null) {
                         // uncomment either one
-//                        instance = result = new FakeDB(context);
-                        instance = result = new SyncbaseDB(context);
+                        instance = result = new FakeDB(context);
+//                        instance = result = new SyncbaseDB(context);
                     }
                 }
             }