Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7859 views
1
package com.artifex.mupdfdemo;
2
3
import java.io.ByteArrayOutputStream;
4
import java.io.InputStream;
5
6
import android.app.Activity;
7
import android.content.ActivityNotFoundException;
8
import android.content.ContentResolver;
9
import android.content.Intent;
10
import android.os.Bundle;
11
import android.util.Base64;
12
import android.webkit.WebSettings;
13
import android.webkit.WebView;
14
import android.webkit.WebViewClient;
15
16
public class PrintDialogActivity extends Activity {
17
private static final String PRINT_DIALOG_URL = "https://www.google.com/cloudprint/dialog.html";
18
private static final String JS_INTERFACE = "AndroidPrintDialog";
19
private static final String CONTENT_TRANSFER_ENCODING = "base64";
20
21
private static final String ZXING_URL = "http://zxing.appspot.com";
22
private static final int ZXING_SCAN_REQUEST = 65743;
23
24
/**
25
* Post message that is sent by Print Dialog web page when the printing dialog
26
* needs to be closed.
27
*/
28
private static final String CLOSE_POST_MESSAGE_NAME = "cp-dialog-on-close";
29
30
/**
31
* Web view element to show the printing dialog in.
32
*/
33
private WebView dialogWebView;
34
35
/**
36
* Intent that started the action.
37
*/
38
Intent cloudPrintIntent;
39
40
private int resultCode;
41
42
@Override
43
public void onCreate(Bundle icicle) {
44
super.onCreate(icicle);
45
46
resultCode = RESULT_OK;
47
setContentView(R.layout.print_dialog);
48
dialogWebView = (WebView) findViewById(R.id.webview);
49
cloudPrintIntent = this.getIntent();
50
51
WebSettings settings = dialogWebView.getSettings();
52
settings.setJavaScriptEnabled(true);
53
54
dialogWebView.setWebViewClient(new PrintDialogWebClient());
55
dialogWebView.addJavascriptInterface(
56
new PrintDialogJavaScriptInterface(), JS_INTERFACE);
57
58
dialogWebView.loadUrl(PRINT_DIALOG_URL);
59
}
60
61
@Override
62
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
63
if (requestCode == ZXING_SCAN_REQUEST && resultCode == RESULT_OK) {
64
dialogWebView.loadUrl(intent.getStringExtra("SCAN_RESULT"));
65
}
66
}
67
68
final class PrintDialogJavaScriptInterface {
69
public String getType() {
70
return cloudPrintIntent.getType();
71
}
72
73
public String getTitle() {
74
return cloudPrintIntent.getExtras().getString("title");
75
}
76
77
public String getContent() {
78
try {
79
ContentResolver contentResolver = getContentResolver();
80
InputStream is = contentResolver.openInputStream(cloudPrintIntent.getData());
81
ByteArrayOutputStream baos = new ByteArrayOutputStream();
82
83
byte[] buffer = new byte[4096];
84
int n = is.read(buffer);
85
while (n >= 0) {
86
baos.write(buffer, 0, n);
87
n = is.read(buffer);
88
}
89
is.close();
90
baos.flush();
91
92
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
93
} catch (Throwable e) {
94
resultCode = RESULT_CANCELED;
95
setResult(resultCode);
96
finish();
97
e.printStackTrace();
98
}
99
return "";
100
}
101
102
public String getEncoding() {
103
return CONTENT_TRANSFER_ENCODING;
104
}
105
106
public void onPostMessage(String message) {
107
if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) {
108
setResult(resultCode);
109
finish();
110
}
111
}
112
}
113
114
private final class PrintDialogWebClient extends WebViewClient {
115
@Override
116
public boolean shouldOverrideUrlLoading(WebView view, String url) {
117
if (url.startsWith(ZXING_URL)) {
118
Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
119
intentScan.putExtra("SCAN_MODE", "QR_CODE_MODE");
120
try {
121
startActivityForResult(intentScan, ZXING_SCAN_REQUEST);
122
} catch (ActivityNotFoundException error) {
123
view.loadUrl(url);
124
}
125
} else {
126
view.loadUrl(url);
127
}
128
return false;
129
}
130
131
@Override
132
public void onPageFinished(WebView view, String url) {
133
if (PRINT_DIALOG_URL.equals(url)) {
134
// Submit print document.
135
view.loadUrl("javascript:printDialog.setPrintDocument(printDialog.createPrintDocument("
136
+ "window." + JS_INTERFACE + ".getType(),window." + JS_INTERFACE + ".getTitle(),"
137
+ "window." + JS_INTERFACE + ".getContent(),window." + JS_INTERFACE + ".getEncoding()))");
138
139
// Add post messages listener.
140
view.loadUrl("javascript:window.addEventListener('message',"
141
+ "function(evt){window." + JS_INTERFACE + ".onPostMessage(evt.data)}, false)");
142
}
143
}
144
}
145
}
146
147