diff options
author | Paul Gardiner <paulg.artifex@glidos.net> | 2013-03-29 11:18:49 +0000 |
---|---|---|
committer | Paul Gardiner <paulg.artifex@glidos.net> | 2013-03-29 11:18:49 +0000 |
commit | e6b829c3c7f64d8e68fa4a379b1011c13a4bf767 (patch) | |
tree | 75203d44230d743c77170ab35c12c4d9c316af48 /android/src/com/artifex | |
parent | ae7ea7e93ee5998fa428ffa4340bcb28f7b76abb (diff) | |
download | mupdf-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/src/com/artifex')
-rw-r--r-- | android/src/com/artifex/mupdfdemo/MuPDFActivity.java | 25 | ||||
-rw-r--r-- | android/src/com/artifex/mupdfdemo/PrintDialogActivity.java | 142 |
2 files changed, 167 insertions, 0 deletions
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)"); + } + } + } +} |