1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package com.artifex.mupdfdemo;
import java.io.UnsupportedEncodingException;
import android.content.Context;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.Base64;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MuPDFReflowView extends WebView implements MuPDFView {
private final MuPDFCore mCore;
private final Point mParentSize;
private int mPage;
private int mContentHeight;
AsyncTask<Void,Void,String> mLoadHTML;
public MuPDFReflowView(Context c, MuPDFCore core, Point parentSize) {
super(c);
mCore = core;
mParentSize = parentSize;
mContentHeight = parentSize.y;
}
private void requestHeight() {
// Get the webview to report the content height via the interface setup
// above. Workaround for getContentHeight not working
loadUrl("javascript:elem=document.getElementById('content');window.HTMLOUT.reportContentHeight("+mParentSize.x+"*elem.offsetHeight/elem.offsetWidth)");
}
public void setPage(int page, PointF size) {
mPage = page;
getSettings().setJavaScriptEnabled(true);
addJavascriptInterface(new Object(){
public void reportContentHeight(String value) {
mContentHeight = (int)Float.parseFloat(value);
}
}, "HTMLOUT");
setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
requestHeight();
}
});
mLoadHTML = new AsyncTask<Void,Void,String>() {
@Override
protected String doInBackground(Void... params) {
return mCore.html(mPage);
}
@Override
protected void onPostExecute(String result) {
byte [] utf8;
try {
utf8 = result.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
utf8 = result.getBytes();
}
String b64 = Base64.encodeToString(utf8, Base64.DEFAULT);
loadData(b64, "text/html; charset=utf-8", "base64");
}
};
mLoadHTML.execute();
}
public int getPage() {
return mPage;
}
public void setScale(float scale) {
loadUrl("javascript:document.getElementById('content').style.fontSize=\""+(int)(scale*100)+"%\"");
requestHeight();
}
public void blank(int page) {
}
public boolean passClickEvent(float x, float y) {
return false;
}
public LinkInfo hitLink(float x, float y) {
return null;
}
public void selectText(float x0, float y0, float x1, float y1) {
}
public void deselectText() {
}
public boolean copySelection() {
return false;
}
public void strikeOutSelection() {
}
public void setSearchBoxes(RectF[] searchBoxes) {
}
public void setLinkHighlighting(boolean f) {
}
public void setChangeReporter(Runnable reporter) {
}
public void update() {
}
public void addHq(boolean update) {
}
public void removeHq() {
}
public void releaseResources() {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int x, y;
switch(View.MeasureSpec.getMode(widthMeasureSpec)) {
case View.MeasureSpec.UNSPECIFIED:
x = mParentSize.x;
break;
default:
x = View.MeasureSpec.getSize(widthMeasureSpec);
}
switch(View.MeasureSpec.getMode(heightMeasureSpec)) {
case View.MeasureSpec.UNSPECIFIED:
y = mContentHeight;
break;
default:
y = View.MeasureSpec.getSize(heightMeasureSpec);
}
setMeasuredDimension(x, y);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return false;
}
}
|