summaryrefslogtreecommitdiff
path: root/android
diff options
context:
space:
mode:
authorPaul Gardiner <paulg.artifex@glidos.net>2013-03-29 11:18:49 +0000
committerPaul Gardiner <paulg.artifex@glidos.net>2013-03-29 11:18:49 +0000
commite6b829c3c7f64d8e68fa4a379b1011c13a4bf767 (patch)
tree75203d44230d743c77170ab35c12c4d9c316af48 /android
parentae7ea7e93ee5998fa428ffa4340bcb28f7b76abb (diff)
downloadmupdf-e6b829c3c7f64d8e68fa4a379b1011c13a4bf767.tar.xz
Android: initial support for printing
This wont work for other than PDF documents Also, we should save the file before printing if it has been changed
Diffstat (limited to 'android')
-rw-r--r--android/AndroidManifest.xml6
-rw-r--r--android/res/drawable-ldpi/ic_print.pngbin0 -> 587 bytes
-rw-r--r--android/res/drawable-mdpi/ic_print.pngbin0 -> 751 bytes
-rw-r--r--android/res/layout/buttons.xml10
-rw-r--r--android/res/layout/print_dialog.xml9
-rw-r--r--android/res/values/strings.xml1
-rw-r--r--android/src/com/artifex/mupdfdemo/MuPDFActivity.java25
-rw-r--r--android/src/com/artifex/mupdfdemo/PrintDialogActivity.java142
8 files changed, 193 insertions, 0 deletions
diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml
index 34422f5d..2bc2217f 100644
--- a/android/AndroidManifest.xml
+++ b/android/AndroidManifest.xml
@@ -5,6 +5,7 @@
android:versionName="@string/version"
android:installLocation="auto">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+ <uses-permission android:name="android.permission.INTERNET" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
@@ -77,5 +78,10 @@
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
android:label="@string/outline_title">
</activity>
+ <activity
+ android:name="PrintDialogActivity"
+ android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
+ android:label="@string/print">
+ </activity>
</application>
</manifest>
diff --git a/android/res/drawable-ldpi/ic_print.png b/android/res/drawable-ldpi/ic_print.png
new file mode 100644
index 00000000..1f7ccf3c
--- /dev/null
+++ b/android/res/drawable-ldpi/ic_print.png
Binary files differ
diff --git a/android/res/drawable-mdpi/ic_print.png b/android/res/drawable-mdpi/ic_print.png
new file mode 100644
index 00000000..e6a745bd
--- /dev/null
+++ b/android/res/drawable-mdpi/ic_print.png
Binary files differ
diff --git a/android/res/layout/buttons.xml b/android/res/layout/buttons.xml
index e714b022..4a3bdeb2 100644
--- a/android/res/layout/buttons.xml
+++ b/android/res/layout/buttons.xml
@@ -235,6 +235,16 @@
android:src="@drawable/ic_cancel" />
<ImageButton
+ android:id="@+id/printButton"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_centerVertical="true"
+ android:layout_toLeftOf="@+id/selectButton"
+ android:contentDescription="@string/print"
+ android:background="@drawable/button"
+ android:src="@drawable/ic_print" />
+
+ <ImageButton
android:id="@+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
diff --git a/android/res/layout/print_dialog.xml b/android/res/layout/print_dialog.xml
new file mode 100644
index 00000000..a95840f3
--- /dev/null
+++ b/android/res/layout/print_dialog.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent">
+ <WebView android:id="@+id/webview"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"/>
+</RelativeLayout>
diff --git a/android/res/values/strings.xml b/android/res/values/strings.xml
index c8008591..ddb5f8e9 100644
--- a/android/res/values/strings.xml
+++ b/android/res/values/strings.xml
@@ -27,4 +27,5 @@
<string name="edit_annotations">Edit annotations</string>
<string name="ink">Ink</string>
<string name="save">Save</string>
+ <string name="print">Print</string>
</resources>
diff --git a/android/src/com/artifex/mupdfdemo/MuPDFActivity.java b/android/src/com/artifex/mupdfdemo/MuPDFActivity.java
index afeedaf0..d320befd 100644
--- a/android/src/com/artifex/mupdfdemo/MuPDFActivity.java
+++ b/android/src/com/artifex/mupdfdemo/MuPDFActivity.java
@@ -75,6 +75,7 @@ public class MuPDFActivity extends Activity
private ImageButton mInkButton;
private Button mSaveInkButton;
private ImageButton mCancelInkButton;
+ private ImageButton mPrintButton;
private ViewAnimator mTopBarSwitcher;
private ImageButton mLinkButton;
private TopBarMode mTopBarMode;
@@ -522,6 +523,12 @@ public class MuPDFActivity extends Activity
}
});
+ mPrintButton.setOnClickListener(new View.OnClickListener() {
+ public void onClick(View v) {
+ printDoc();
+ }
+ });
+
mSaveInkButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MuPDFView pageView = (MuPDFView) mDocView.getDisplayedView();
@@ -928,6 +935,23 @@ public class MuPDFActivity extends Activity
mPageNumberView.setText(String.format("%d / %d", index+1, core.countPages()));
}
+ private void printDoc() {
+ Intent myIntent = getIntent();
+ Uri docUri = myIntent != null ? myIntent.getData() : null;
+
+ if (docUri == null) {
+ showInfo("Print failed");
+ }
+
+ if (docUri.getScheme() == null)
+ docUri = Uri.parse("file://"+docUri.toString());
+
+ Intent printIntent = new Intent(this, PrintDialogActivity.class);
+ printIntent.setDataAndType(docUri, "aplication/pdf");
+ printIntent.putExtra("title", mFileName);
+ startActivity(printIntent);
+ }
+
private void showInfo(String message) {
mInfoView.setText(message);
@@ -984,6 +1008,7 @@ public class MuPDFActivity extends Activity
mInkButton = (ImageButton)mButtonsView.findViewById(R.id.inkButton);
mSaveInkButton = (Button)mButtonsView.findViewById(R.id.saveInkButton);
mCancelInkButton = (ImageButton)mButtonsView.findViewById(R.id.cancelInkButton);
+ mPrintButton = (ImageButton)mButtonsView.findViewById(R.id.printButton);
mTopBarSwitcher = (ViewAnimator)mButtonsView.findViewById(R.id.switcher);
mSearchBack = (ImageButton)mButtonsView.findViewById(R.id.searchBack);
mSearchFwd = (ImageButton)mButtonsView.findViewById(R.id.searchForward);
diff --git a/android/src/com/artifex/mupdfdemo/PrintDialogActivity.java b/android/src/com/artifex/mupdfdemo/PrintDialogActivity.java
new file mode 100644
index 00000000..cbfe07b9
--- /dev/null
+++ b/android/src/com/artifex/mupdfdemo/PrintDialogActivity.java
@@ -0,0 +1,142 @@
+package com.artifex.mupdfdemo;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import android.app.Activity;
+import android.content.ActivityNotFoundException;
+import android.content.ContentResolver;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Base64;
+import android.webkit.WebSettings;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+public class PrintDialogActivity extends Activity {
+ private static final String PRINT_DIALOG_URL = "https://www.google.com/cloudprint/dialog.html";
+ private static final String JS_INTERFACE = "AndroidPrintDialog";
+ private static final String CONTENT_TRANSFER_ENCODING = "base64";
+
+ private static final String ZXING_URL = "http://zxing.appspot.com";
+ private static final int ZXING_SCAN_REQUEST = 65743;
+
+ /**
+ * Post message that is sent by Print Dialog web page when the printing dialog
+ * needs to be closed.
+ */
+ private static final String CLOSE_POST_MESSAGE_NAME = "cp-dialog-on-close";
+
+ /**
+ * Web view element to show the printing dialog in.
+ */
+ private WebView dialogWebView;
+
+ /**
+ * Intent that started the action.
+ */
+ Intent cloudPrintIntent;
+
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+
+ setContentView(R.layout.print_dialog);
+ dialogWebView = (WebView) findViewById(R.id.webview);
+ cloudPrintIntent = this.getIntent();
+
+ WebSettings settings = dialogWebView.getSettings();
+ settings.setJavaScriptEnabled(true);
+
+ dialogWebView.setWebViewClient(new PrintDialogWebClient());
+ dialogWebView.addJavascriptInterface(
+ new PrintDialogJavaScriptInterface(), JS_INTERFACE);
+
+ dialogWebView.loadUrl(PRINT_DIALOG_URL);
+ }
+
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
+ if (requestCode == ZXING_SCAN_REQUEST && resultCode == RESULT_OK) {
+ dialogWebView.loadUrl(intent.getStringExtra("SCAN_RESULT"));
+ }
+ }
+
+ final class PrintDialogJavaScriptInterface {
+ public String getType() {
+ return cloudPrintIntent.getType();
+ }
+
+ public String getTitle() {
+ return cloudPrintIntent.getExtras().getString("title");
+ }
+
+ public String getContent() {
+ try {
+ ContentResolver contentResolver = getContentResolver();
+ InputStream is = contentResolver.openInputStream(cloudPrintIntent.getData());
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ byte[] buffer = new byte[4096];
+ int n = is.read(buffer);
+ while (n >= 0) {
+ baos.write(buffer, 0, n);
+ n = is.read(buffer);
+ }
+ is.close();
+ baos.flush();
+
+ return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ public String getEncoding() {
+ return CONTENT_TRANSFER_ENCODING;
+ }
+
+ public void onPostMessage(String message) {
+ if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) {
+ finish();
+ }
+ }
+ }
+
+ private final class PrintDialogWebClient extends WebViewClient {
+ @Override
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
+ if (url.startsWith(ZXING_URL)) {
+ Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
+ intentScan.putExtra("SCAN_MODE", "QR_CODE_MODE");
+ try {
+ startActivityForResult(intentScan, ZXING_SCAN_REQUEST);
+ } catch (ActivityNotFoundException error) {
+ view.loadUrl(url);
+ }
+ } else {
+ view.loadUrl(url);
+ }
+ return false;
+ }
+
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ if (PRINT_DIALOG_URL.equals(url)) {
+ // Submit print document.
+ view.loadUrl("javascript:printDialog.setPrintDocument(printDialog.createPrintDocument("
+ + "window." + JS_INTERFACE + ".getType(),window." + JS_INTERFACE + ".getTitle(),"
+ + "window." + JS_INTERFACE + ".getContent(),window." + JS_INTERFACE + ".getEncoding()))");
+
+ // Add post messages listener.
+ view.loadUrl("javascript:window.addEventListener('message',"
+ + "function(evt){window." + JS_INTERFACE + ".onPostMessage(evt.data)}, false)");
+ }
+ }
+ }
+}