Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7859 views
1
package com.artifex.mupdfdemo;
2
3
import android.app.Activity;
4
import android.content.Context;
5
import android.content.Intent;
6
import android.net.Uri;
7
import android.util.AttributeSet;
8
import android.util.DisplayMetrics;
9
import android.view.MotionEvent;
10
import android.view.ScaleGestureDetector;
11
import android.view.View;
12
import android.view.WindowManager;
13
14
public class MuPDFReaderView extends ReaderView {
15
public enum Mode {Viewing, Selecting, Drawing}
16
private final Context mContext;
17
private boolean mLinksEnabled = false;
18
private Mode mMode = Mode.Viewing;
19
private boolean tapDisabled = false;
20
private int tapPageMargin;
21
22
protected void onTapMainDocArea() {}
23
protected void onDocMotion() {}
24
protected void onHit(Hit item) {};
25
26
public void setLinksEnabled(boolean b) {
27
mLinksEnabled = b;
28
resetupChildren();
29
}
30
31
public void setMode(Mode m) {
32
mMode = m;
33
}
34
35
private void setup()
36
{
37
// Get the screen size etc to customise tap margins.
38
// We calculate the size of 1 inch of the screen for tapping.
39
// On some devices the dpi values returned are wrong, so we
40
// sanity check it: we first restrict it so that we are never
41
// less than 100 pixels (the smallest Android device screen
42
// dimension I've seen is 480 pixels or so). Then we check
43
// to ensure we are never more than 1/5 of the screen width.
44
DisplayMetrics dm = new DisplayMetrics();
45
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
46
wm.getDefaultDisplay().getMetrics(dm);
47
tapPageMargin = (int)dm.xdpi;
48
if (tapPageMargin < 100)
49
tapPageMargin = 100;
50
if (tapPageMargin > dm.widthPixels/5)
51
tapPageMargin = dm.widthPixels/5;
52
}
53
54
public MuPDFReaderView(Context context) {
55
super(context);
56
mContext = context;
57
setup();
58
}
59
60
public MuPDFReaderView(Context context, AttributeSet attrs)
61
{
62
super(context, attrs);
63
mContext = context;
64
setup();
65
}
66
67
public boolean onSingleTapUp(MotionEvent e) {
68
LinkInfo link = null;
69
70
if (mMode == Mode.Viewing && !tapDisabled) {
71
MuPDFView pageView = (MuPDFView) getDisplayedView();
72
Hit item = pageView.passClickEvent(e.getX(), e.getY());
73
onHit(item);
74
if (item == Hit.Nothing) {
75
if (mLinksEnabled && pageView != null
76
&& (link = pageView.hitLink(e.getX(), e.getY())) != null) {
77
link.acceptVisitor(new LinkInfoVisitor() {
78
@Override
79
public void visitInternal(LinkInfoInternal li) {
80
// Clicked on an internal (GoTo) link
81
setDisplayedViewIndex(li.pageNumber);
82
}
83
84
@Override
85
public void visitExternal(LinkInfoExternal li) {
86
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
87
.parse(li.url));
88
mContext.startActivity(intent);
89
}
90
91
@Override
92
public void visitRemote(LinkInfoRemote li) {
93
// Clicked on a remote (GoToR) link
94
}
95
});
96
} else if (e.getX() < tapPageMargin) {
97
super.smartMoveBackwards();
98
} else if (e.getX() > super.getWidth() - tapPageMargin) {
99
super.smartMoveForwards();
100
} else if (e.getY() < tapPageMargin) {
101
super.smartMoveBackwards();
102
} else if (e.getY() > super.getHeight() - tapPageMargin) {
103
super.smartMoveForwards();
104
} else {
105
onTapMainDocArea();
106
}
107
}
108
}
109
return super.onSingleTapUp(e);
110
}
111
112
@Override
113
public boolean onDown(MotionEvent e) {
114
115
return super.onDown(e);
116
}
117
118
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
119
float distanceY) {
120
MuPDFView pageView = (MuPDFView)getDisplayedView();
121
switch (mMode) {
122
case Viewing:
123
if (!tapDisabled)
124
onDocMotion();
125
126
return super.onScroll(e1, e2, distanceX, distanceY);
127
case Selecting:
128
if (pageView != null)
129
pageView.selectText(e1.getX(), e1.getY(), e2.getX(), e2.getY());
130
return true;
131
default:
132
return true;
133
}
134
}
135
136
@Override
137
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
138
float velocityY) {
139
switch (mMode) {
140
case Viewing:
141
return super.onFling(e1, e2, velocityX, velocityY);
142
default:
143
return true;
144
}
145
}
146
147
public boolean onScaleBegin(ScaleGestureDetector d) {
148
// Disabled showing the buttons until next touch.
149
// Not sure why this is needed, but without it
150
// pinch zoom can make the buttons appear
151
tapDisabled = true;
152
return super.onScaleBegin(d);
153
}
154
155
public boolean onTouchEvent(MotionEvent event) {
156
157
if ( mMode == Mode.Drawing )
158
{
159
float x = event.getX();
160
float y = event.getY();
161
switch (event.getAction())
162
{
163
case MotionEvent.ACTION_DOWN:
164
touch_start(x, y);
165
break;
166
case MotionEvent.ACTION_MOVE:
167
touch_move(x, y);
168
break;
169
case MotionEvent.ACTION_UP:
170
touch_up();
171
break;
172
}
173
}
174
175
if ((event.getAction() & event.getActionMasked()) == MotionEvent.ACTION_DOWN)
176
{
177
tapDisabled = false;
178
}
179
180
return super.onTouchEvent(event);
181
}
182
183
private float mX, mY;
184
185
private static final float TOUCH_TOLERANCE = 2;
186
187
private void touch_start(float x, float y) {
188
189
MuPDFView pageView = (MuPDFView)getDisplayedView();
190
if (pageView != null)
191
{
192
pageView.startDraw(x, y);
193
}
194
mX = x;
195
mY = y;
196
}
197
198
private void touch_move(float x, float y) {
199
200
float dx = Math.abs(x - mX);
201
float dy = Math.abs(y - mY);
202
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
203
{
204
MuPDFView pageView = (MuPDFView)getDisplayedView();
205
if (pageView != null)
206
{
207
pageView.continueDraw(x, y);
208
}
209
mX = x;
210
mY = y;
211
}
212
}
213
214
private void touch_up() {
215
216
// NOOP
217
}
218
219
protected void onChildSetup(int i, View v) {
220
if (SearchTaskResult.get() != null
221
&& SearchTaskResult.get().pageNumber == i)
222
((MuPDFView) v).setSearchBoxes(SearchTaskResult.get().searchBoxes);
223
else
224
((MuPDFView) v).setSearchBoxes(null);
225
226
((MuPDFView) v).setLinkHighlighting(mLinksEnabled);
227
228
((MuPDFView) v).setChangeReporter(new Runnable() {
229
public void run() {
230
applyToChildren(new ReaderView.ViewMapper() {
231
@Override
232
void applyToView(View view) {
233
((MuPDFView) view).update();
234
}
235
});
236
}
237
});
238
}
239
240
protected void onMoveToChild(int i) {
241
if (SearchTaskResult.get() != null
242
&& SearchTaskResult.get().pageNumber != i) {
243
SearchTaskResult.set(null);
244
resetupChildren();
245
}
246
}
247
248
@Override
249
protected void onMoveOffChild(int i) {
250
View v = getView(i);
251
if (v != null)
252
((MuPDFView)v).deselectAnnotation();
253
}
254
255
protected void onSettle(View v) {
256
// When the layout has settled ask the page to render
257
// in HQ
258
((MuPDFView) v).updateHq(false);
259
}
260
261
protected void onUnsettle(View v) {
262
// When something changes making the previous settled view
263
// no longer appropriate, tell the page to remove HQ
264
((MuPDFView) v).removeHq();
265
}
266
267
@Override
268
protected void onNotInUse(View v) {
269
((MuPDFView) v).releaseResources();
270
}
271
272
@Override
273
protected void onScaleChild(View v, Float scale) {
274
((MuPDFView) v).setScale(scale);
275
}
276
}
277
278