Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7859 views
1
package com.artifex.mupdfdemo;
2
3
import android.content.Context;
4
import android.graphics.Point;
5
import android.graphics.PointF;
6
import android.graphics.RectF;
7
import android.os.Handler;
8
import android.util.Base64;
9
import android.view.MotionEvent;
10
import android.view.View;
11
import android.webkit.WebView;
12
import android.webkit.WebViewClient;
13
14
public class MuPDFReflowView extends WebView implements MuPDFView {
15
private final MuPDFCore mCore;
16
private final Handler mHandler;
17
private final Point mParentSize;
18
private int mPage;
19
private float mScale;
20
private int mContentHeight;
21
AsyncTask<Void,Void,byte[]> mLoadHTML;
22
23
public MuPDFReflowView(Context c, MuPDFCore core, Point parentSize) {
24
super(c);
25
mHandler = new Handler();
26
mCore = core;
27
mParentSize = parentSize;
28
mScale = 1.0f;
29
mContentHeight = parentSize.y;
30
getSettings().setJavaScriptEnabled(true);
31
addJavascriptInterface(new Object(){
32
public void reportContentHeight(String value) {
33
mContentHeight = (int)Float.parseFloat(value);
34
mHandler.post(new Runnable() {
35
public void run() {
36
requestLayout();
37
}
38
});
39
}
40
}, "HTMLOUT");
41
setWebViewClient(new WebViewClient() {
42
@Override
43
public void onPageFinished(WebView view, String url) {
44
setScale(mScale);
45
}
46
});
47
}
48
49
private void requestHeight() {
50
// Get the webview to report the content height via the interface setup
51
// above. Workaround for getContentHeight not working
52
loadUrl("javascript:elem=document.getElementById('content');window.HTMLOUT.reportContentHeight("+mParentSize.x+"*elem.offsetHeight/elem.offsetWidth)");
53
}
54
55
public void setPage(int page, PointF size) {
56
mPage = page;
57
if (mLoadHTML != null) {
58
mLoadHTML.cancel(true);
59
}
60
mLoadHTML = new AsyncTask<Void,Void,byte[]>() {
61
@Override
62
protected byte[] doInBackground(Void... params) {
63
return mCore.html(mPage);
64
}
65
@Override
66
protected void onPostExecute(byte[] result) {
67
String b64 = Base64.encodeToString(result, Base64.DEFAULT);
68
loadData(b64, "text/html; charset=utf-8", "base64");
69
}
70
};
71
mLoadHTML.execute();
72
}
73
74
public int getPage() {
75
return mPage;
76
}
77
78
public void setScale(float scale) {
79
mScale = scale;
80
loadUrl("javascript:document.getElementById('content').style.zoom=\""+(int)(mScale*100)+"%\"");
81
requestHeight();
82
}
83
84
public void blank(int page) {
85
}
86
87
public Hit passClickEvent(float x, float y) {
88
return Hit.Nothing;
89
}
90
91
public LinkInfo hitLink(float x, float y) {
92
return null;
93
}
94
95
public void selectText(float x0, float y0, float x1, float y1) {
96
}
97
98
public void deselectText() {
99
}
100
101
public boolean copySelection() {
102
return false;
103
}
104
105
public boolean markupSelection(Annotation.Type type) {
106
return false;
107
}
108
109
public void startDraw(float x, float y) {
110
}
111
112
public void continueDraw(float x, float y) {
113
}
114
115
public void cancelDraw() {
116
}
117
118
public boolean saveDraw() {
119
return false;
120
}
121
122
public void setSearchBoxes(RectF[] searchBoxes) {
123
}
124
125
public void setLinkHighlighting(boolean f) {
126
}
127
128
public void deleteSelectedAnnotation() {
129
}
130
131
public void deselectAnnotation() {
132
}
133
134
public void setChangeReporter(Runnable reporter) {
135
}
136
137
public void update() {
138
}
139
140
public void updateHq(boolean update) {
141
}
142
143
public void removeHq() {
144
}
145
146
public void releaseResources() {
147
if (mLoadHTML != null) {
148
mLoadHTML.cancel(true);
149
mLoadHTML = null;
150
}
151
}
152
153
public void releaseBitmaps() {
154
}
155
156
@Override
157
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
158
int x, y;
159
switch(View.MeasureSpec.getMode(widthMeasureSpec)) {
160
case View.MeasureSpec.UNSPECIFIED:
161
x = mParentSize.x;
162
break;
163
default:
164
x = View.MeasureSpec.getSize(widthMeasureSpec);
165
}
166
switch(View.MeasureSpec.getMode(heightMeasureSpec)) {
167
case View.MeasureSpec.UNSPECIFIED:
168
y = mContentHeight;
169
break;
170
default:
171
y = View.MeasureSpec.getSize(heightMeasureSpec);
172
}
173
174
setMeasuredDimension(x, y);
175
}
176
177
@Override
178
public boolean onTouchEvent(MotionEvent ev) {
179
// TODO Auto-generated method stub
180
return false;
181
}
182
}
183
184