-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathReaderView.java
More file actions
6712 lines (6105 loc) · 206 KB
/
ReaderView.java
File metadata and controls
6712 lines (6105 loc) · 206 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CoolReader for Android
* Copyright (C) 2010-2015,2020 Vadim Lopatin <coolreader.org@gmail.com>
* Copyright (C) 2011 a_lone
* Copyright (C) 2011 alexstsv
* Copyright (C) 2012 Michael Berganovsky <mike0berg@gmail.com>
* Copyright (C) 2012 Jasper Poppe <jpoppe@ebay.com>
* Copyright (C) 2012,2013 Jeff Doozan <jeff@doozan.com>
* Copyright (C) 2012 Daniel Savard <daniels@xsoli.com>
* Copyright (C) 2012,2014 klush
* Copyright (C) 2018 norbi24 <norbert.bartalsky@gmail.com>
* Copyright (C) 2018 Yuri Plotnikov <plotnikovya@gmail.com>
* Copyright (C) 2018 S-trace <S-trace@list.ru>
* Copyright (C) 2018-2021 Aleksey Chernov <valexlin@gmail.com>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.coolreader.crengine;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.text.ClipboardManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import org.coolreader.CoolReader;
import org.coolreader.R;
import org.coolreader.crengine.InputDialog.InputHandler;
import org.koekak.android.ebookdownloader.SonyBookSelector;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;
public class ReaderView implements android.view.SurfaceHolder.Callback, Settings, DocProperties, OnKeyListener, OnTouchListener, OnFocusChangeListener {
public static final Logger log = L.create("rv", Log.VERBOSE);
public static final Logger alog = L.create("ra", Log.WARN);
private final SurfaceView surface;
private final BookView bookView;
public SurfaceView getSurface() {
return surface;
}
public interface BookView {
void draw();
void draw(boolean isPartially);
void invalidate();
void onPause();
void onResume();
}
public class ReaderSurface extends SurfaceView implements BookView {
public ReaderSurface(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void onPause() {
}
@Override
public void onResume() {
}
@Override
protected void onDraw(Canvas canvas) {
try {
log.d("onDraw() called");
draw();
} catch (Exception e) {
log.e("exception while drawing", e);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
log.d("View.onDetachedFromWindow() is called");
}
@Override
public boolean onTrackballEvent(MotionEvent event) {
log.d("onTrackballEvent(" + event + ")");
if (mSettings.getBool(PROP_APP_TRACKBALL_DISABLED, false)) {
log.d("trackball is disabled in settings");
return true;
}
mActivity.onUserActivity();
return super.onTrackballEvent(event);
}
@Override
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
log.i("onSizeChanged(" + w + ", " + h + ")" + " activity.isDialogActive=" + getActivity().isDialogActive());
super.onSizeChanged(w, h, oldw, oldh);
requestResize(w, h);
}
@Override
public void onWindowVisibilityChanged(int visibility) {
if (visibility == VISIBLE) {
if (DeviceInfo.EINK_SCREEN)
mEinkScreen.refreshScreen(surface);
startStats();
checkSize();
} else
stopStats();
super.onWindowVisibilityChanged(visibility);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if (hasWindowFocus) {
if (DeviceInfo.EINK_SCREEN)
BackgroundThread.instance().postGUI(() -> mEinkScreen.refreshScreen(surface), 400);
startStats();
checkSize();
} else
stopStats();
super.onWindowFocusChanged(hasWindowFocus);
}
protected void doDraw(Canvas canvas) {
try {
log.d("doDraw() called");
if (isProgressActive()) {
log.d("onDraw() -- drawing progress " + (currentProgressPosition / 100));
drawPageBackground(canvas);
doDrawProgress(canvas, currentProgressPosition, currentProgressTitle);
} else if (mInitialized && mCurrentPageInfo != null && mCurrentPageInfo.bitmap != null) {
log.d("onDraw() -- drawing page image");
if (currentAutoScrollAnimation != null) {
currentAutoScrollAnimation.draw(canvas);
} else if (currentAnimation != null) {
currentAnimation.draw(canvas);
} else {
Rect dst = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
Rect src = new Rect(0, 0, mCurrentPageInfo.bitmap.getWidth(), mCurrentPageInfo.bitmap.getHeight());
if (dontStretchWhileDrawing) {
if (dst.right > src.right)
dst.right = src.right;
if (dst.bottom > src.bottom)
dst.bottom = src.bottom;
if (src.right > dst.right)
src.right = dst.right;
if (src.bottom > dst.bottom)
src.bottom = dst.bottom;
if (centerPageInsteadOfResizing) {
int ddx = (canvas.getWidth() - dst.width()) / 2;
int ddy = (canvas.getHeight() - dst.height()) / 2;
dst.left += ddx;
dst.right += ddx;
dst.top += ddy;
dst.bottom += ddy;
}
}
if (dst.width() != canvas.getWidth() || dst.height() != canvas.getHeight())
canvas.drawColor(Color.rgb(32, 32, 32));
drawDimmedBitmap(canvas, mCurrentPageInfo.bitmap, src, dst);
}
if (isCloudSyncProgressActive()) {
// draw progressbar on top
doDrawCloudSyncProgress(canvas, currentCloudSyncProgressPosition);
}
} else {
log.d("onDraw() -- drawing empty screen");
drawPageBackground(canvas);
if (isCloudSyncProgressActive()) {
// draw progressbar on top
doDrawCloudSyncProgress(canvas, currentCloudSyncProgressPosition);
}
}
} catch (Exception e) {
log.e("exception while drawing", e);
}
}
@Override
public void draw() {
draw(false);
}
@Override
public void draw(boolean isPartially) {
drawCallback(this::doDraw, null, isPartially);
}
@Override
public void invalidate() {
super.invalidate();
}
}
private DocView doc;
// additional key codes for Nook
public static final int NOOK_KEY_PREV_LEFT = 96;
public static final int NOOK_KEY_PREV_RIGHT = 98;
public static final int NOOK_KEY_NEXT_RIGHT = 97;
public static final int NOOK_KEY_SHIFT_UP = 101;
public static final int NOOK_KEY_SHIFT_DOWN = 100;
// nook 1 & 2
public static final int NOOK_12_KEY_NEXT_LEFT = 95;
// Nook touch buttons
public static final int KEYCODE_PAGE_BOTTOMLEFT = 0x5d; // fwd = 93 (
// public static final int KEYCODE_PAGE_BOTTOMRIGHT = 158; // 0x5f; // fwd = 95
public static final int KEYCODE_PAGE_TOPLEFT = 0x5c; // back = 92
public static final int KEYCODE_PAGE_TOPRIGHT = 0x5e; // back = 94
public static final int SONY_DPAD_UP_SCANCODE = 105;
public static final int SONY_DPAD_DOWN_SCANCODE = 106;
public static final int SONY_DPAD_LEFT_SCANCODE = 125;
public static final int SONY_DPAD_RIGHT_SCANCODE = 126;
public static final int KEYCODE_ESCAPE = 111; // KeyEvent constant since API 11
// public static final int SONY_MENU_SCANCODE = 357;
// public static final int SONY_BACK_SCANCODE = 158;
// public static final int SONY_HOME_SCANCODE = 102;
public static final int PAGE_ANIMATION_NONE = 0;
public static final int PAGE_ANIMATION_PAPER = 1;
public static final int PAGE_ANIMATION_SLIDE = 2;
public static final int PAGE_ANIMATION_SLIDE2 = 3;
public static final int PAGE_ANIMATION_MAX = 3;
public static final int SEL_CMD_SELECT_FIRST_SENTENCE_ON_PAGE = 1;
public static final int SEL_CMD_NEXT_SENTENCE = 2;
public static final int SEL_CMD_PREV_SENTENCE = 3;
// Double tap selections within this radius are are assumed to be attempts to select a single point
public static final int DOUBLE_TAP_RADIUS = 60;
private final static int BRIGHTNESS_TYPE_COMMON = 0;
private final static int BRIGHTNESS_TYPE_WARM = 1;
private final static int BRIGHTNESS_TYPE_BOTH = 2;
/// Always sync this constants with crengine/include/lvdocview.h!
/// Battery state: no battery
public static final int BATTERY_STATE_NO_BATTERY = -2;
/// Battery state: battery is charging
public static final int BATTERY_STATE_CHARGING = -1;
/// Battery state: battery is discharging
public static final int BATTERY_STATE_DISCHARGING = -3;
/// Battery charger connection: no connection
public static final int BATTERY_CHARGER_NO = 1;
/// Battery charger connection: AC adapter
public static final int BATTERY_CHARGER_AC = 2;
/// Battery charger connection: USB
public static final int BATTERY_CHARGER_USB = 3;
/// Battery charger connection: Wireless
public static final int BATTERY_CHARGER_WIRELESS = 4;
private ViewMode viewMode = ViewMode.PAGES;
private void execute(Engine.EngineTask task) {
mEngine.execute(task);
}
private void post(Engine.EngineTask task) {
mEngine.post(task);
}
private abstract class Task implements Engine.EngineTask {
public void done() {
// override to do something useful
}
public void fail(Exception e) {
// do nothing, just log exception
// override to do custom action
log.e("Task " + this.getClass().getSimpleName() + " is failed with exception " + e.getMessage(), e);
}
}
static class Sync<T> extends Object {
private volatile T result = null;
private volatile boolean completed = false;
public void set(T res) {
log.d("sync.set() called from " + Thread.currentThread().getName());
result = res;
completed = true;
synchronized (this) {
notify();
}
log.d("sync.set() returned from notify " + Thread.currentThread().getName());
}
public T get() {
log.d("sync.get() called from " + Thread.currentThread().getName());
while (!completed) {
try {
log.d("sync.get() before wait " + Thread.currentThread().getName());
synchronized (this) {
if (!completed)
wait();
}
log.d("sync.get() after wait wait " + Thread.currentThread().getName());
} catch (InterruptedException e) {
log.d("sync.get() exception", e);
// ignore
} catch (Exception e) {
log.d("sync.get() exception", e);
// ignore
}
}
log.d("sync.get() returning " + Thread.currentThread().getName());
return result;
}
}
private final CoolReader mActivity;
private final Engine mEngine;
private final EinkScreen mEinkScreen;
private BookInfo mBookInfo;
private Properties mSettings = new Properties();
public Engine getEngine() {
return mEngine;
}
public CoolReader getActivity() {
return mActivity;
}
private int lastResizeTaskId = 0;
public boolean isBookLoaded() {
return mOpened;
}
public int getOrientation() {
int angle = mSettings.getInt(PROP_APP_SCREEN_ORIENTATION, 0);
if (angle == 4)
angle = mActivity.getOrientationFromSensor();
return angle;
}
private int overrideKey(int keyCode) {
return keyCode;
}
public int getTapZone(int x, int y, int dx, int dy) {
int x1 = dx / 3;
int x2 = dx * 2 / 3;
int y1 = dy / 3;
int y2 = dy * 2 / 3;
int zone = 0;
if (y < y1) {
if (x < x1)
zone = 1;
else if (x < x2)
zone = 2;
else
zone = 3;
} else if (y < y2) {
if (x < x1)
zone = 4;
else if (x < x2)
zone = 5;
else
zone = 6;
} else {
if (x < x1)
zone = 7;
else if (x < x2)
zone = 8;
else
zone = 9;
}
return zone;
}
public ReaderAction findTapZoneAction(int zone, int tapActionType) {
ReaderAction action = ReaderAction.NONE;
boolean isSecondaryAction = (secondaryTapActionType == tapActionType);
if (tapActionType == TAP_ACTION_TYPE_SHORT) {
action = ReaderAction.findForTap(zone, mSettings);
} else {
if (isSecondaryAction)
action = ReaderAction.findForLongTap(zone, mSettings);
else if (doubleTapSelectionEnabled || tapActionType == TAP_ACTION_TYPE_LONGPRESS)
action = ReaderAction.START_SELECTION;
}
return action;
}
public FileInfo getOpenedFileInfo() {
if (isBookLoaded() && mBookInfo != null)
return mBookInfo.getFileInfo();
return null;
}
public final int LONG_KEYPRESS_TIME = 900;
public final int AUTOREPEAT_KEYPRESS_TIME = 700;
public final int DOUBLE_CLICK_INTERVAL = 400;
private ReaderAction currentDoubleClickAction = null;
private ReaderAction currentSingleClickAction = null;
private long currentDoubleClickActionStart = 0;
private int currentDoubleClickActionKeyCode = 0;
// boolean VOLUME_KEYS_ZOOM = false;
//private boolean backKeyDownHere = false;
private long statStartTime;
private long statTimeElapsed;
public void startStats() {
if (statStartTime == 0) {
statStartTime = android.os.SystemClock.uptimeMillis();
log.d("stats: started reading");
}
}
public void stopStats() {
if (statStartTime > 0) {
statTimeElapsed += android.os.SystemClock.uptimeMillis() - statStartTime;
statStartTime = 0;
log.d("stats: stopped reading");
}
}
public long getTimeElapsed() {
if (statStartTime > 0)
return statTimeElapsed + android.os.SystemClock.uptimeMillis() - statStartTime;
else
return statTimeElapsed++;
}
public void setTimeElapsed(long timeElapsed) {
statTimeElapsed = timeElapsed;
}
public void onAppPause() {
stopTracking();
if (currentAutoScrollAnimation != null)
stopAutoScroll();
Bookmark bmk = getCurrentPositionBookmark();
if (bmk != null)
savePositionBookmark(bmk);
if (!mAvgDrawAnimationStats.isEmpty())
setSetting(PROP_APP_VIEW_ANIM_DURATION, String.valueOf(mAvgDrawAnimationStats.average()), false, true, false);
log.i("calling bookView.onPause()");
bookView.onPause();
}
private long lastAppResumeTs = 0;
public void onAppResume() {
lastAppResumeTs = System.currentTimeMillis();
log.i("calling bookView.onResume()");
bookView.onResume();
}
private boolean startTrackingKey(KeyEvent event) {
if (event.getRepeatCount() == 0) {
stopTracking();
trackedKeyEvent = event;
return true;
}
return false;
}
private void stopTracking() {
trackedKeyEvent = null;
actionToRepeat = null;
repeatActionActive = false;
if (currentTapHandler != null)
currentTapHandler.cancel();
}
private boolean isTracked(KeyEvent event) {
if (trackedKeyEvent != null) {
int tkeKc = trackedKeyEvent.getKeyCode();
int eKc = event.getKeyCode();
// check if tracked key and current key are the same
if (tkeKc == eKc) {
long tkeDt = trackedKeyEvent.getDownTime();
long eDt = event.getDownTime();
// empirical value (could be changed or moved to constant)
long delta = 300l;
// time difference between tracked and current event
long diff = eDt - tkeDt;
// needed for correct function on HTC Desire for CENTER_KEY
if (delta > diff)
return true;
} else {
log.v("isTracked( trackedKeyEvent=" + trackedKeyEvent + ", event=" + event + " )");
}
}
stopTracking();
return false;
}
private KeyEvent trackedKeyEvent = null;
private ReaderAction actionToRepeat = null;
private boolean repeatActionActive = false;
private SparseArray<Long> keyDownTimestampMap = new SparseArray<Long>();
private int translateKeyCode(int keyCode) {
if (DeviceInfo.REVERT_LANDSCAPE_VOLUME_KEYS && (mActivity.getScreenOrientation() & 1) != 0) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
return KeyEvent.KEYCODE_VOLUME_UP;
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
return KeyEvent.KEYCODE_VOLUME_DOWN;
}
return keyCode;
}
private int nextUpdateId = 0;
private void updateSelection(int startX, int startY, int endX, int endY, final boolean isUpdateEnd) {
final Selection sel = new Selection();
final int myId = ++nextUpdateId;
sel.startX = startX;
sel.startY = startY;
sel.endX = endX;
sel.endY = endY;
mEngine.execute(new Task() {
@Override
public void work() throws Exception {
if (myId != nextUpdateId && !isUpdateEnd)
return;
doc.updateSelection(sel);
if (!sel.isEmpty()) {
invalidImages = true;
BitmapInfo bi = preparePageImage(0);
if (bi != null) {
bookView.draw(true);
}
}
}
@Override
public void done() {
if (isUpdateEnd) {
String text = sel.text;
if (text != null && text.length() > 0) {
onSelectionComplete(sel);
} else {
clearSelection();
}
}
}
});
}
public static boolean isMultiSelection(Selection sel) {
String str = sel.text;
if (str != null) {
for (int i = 0; i < str.length(); i++) {
if (Character.isWhitespace(str.charAt(i))) {
return true;
}
}
}
return false;
}
private int mSelectionAction = SELECTION_ACTION_TOOLBAR;
private int mMultiSelectionAction = SELECTION_ACTION_TOOLBAR;
private void onSelectionComplete(Selection sel) {
int iSelectionAction;
iSelectionAction = isMultiSelection(sel) ? mMultiSelectionAction : mSelectionAction;
switch (iSelectionAction) {
case SELECTION_ACTION_TOOLBAR:
SelectionToolbarDlg.showDialog(mActivity, ReaderView.this, sel);
break;
case SELECTION_ACTION_COPY:
copyToClipboard(sel.text);
clearSelection();
break;
case SELECTION_ACTION_DICTIONARY:
mActivity.findInDictionary(sel.text);
if (!getSettings().getBool(PROP_APP_SELECTION_PERSIST, false))
clearSelection();
break;
case SELECTION_ACTION_BOOKMARK:
clearSelection();
showNewBookmarkDialog(sel);
break;
case SELECTION_ACTION_FIND:
clearSelection();
showSearchDialog(sel.text);
break;
default:
clearSelection();
break;
}
}
public void showNewBookmarkDialog(Selection sel) {
if (mBookInfo == null)
return;
Bookmark bmk = new Bookmark();
bmk.setType(Bookmark.TYPE_COMMENT);
bmk.setPosText(sel.text);
bmk.setStartPos(sel.startPos);
bmk.setEndPos(sel.endPos);
bmk.setPercent(sel.percent);
bmk.setTitleText(sel.chapter);
BookmarkEditDialog dlg = new BookmarkEditDialog(mActivity, this, bmk, true);
dlg.show();
}
public void sendQuotationInEmail(Selection sel) {
StringBuilder buf = new StringBuilder();
if (mBookInfo.getFileInfo().authors != null)
buf.append("|" + mBookInfo.getFileInfo().authors + "\n");
if (mBookInfo.getFileInfo().title != null)
buf.append("|" + mBookInfo.getFileInfo().title + "\n");
if (sel.chapter != null && sel.chapter.length() > 0)
buf.append("|" + sel.chapter + "\n");
buf.append(sel.text + "\n");
mActivity.sendBookFragment(mBookInfo, buf.toString());
}
public void copyToClipboard(String text) {
if (text != null && text.length() > 0) {
ClipboardManager cm = mActivity.getClipboardmanager();
cm.setText(text);
log.i("Setting clipboard text: " + text);
mActivity.showToast("Selection text copied to clipboard");
}
}
// private void cancelSelection() {
// //
// selectionInProgress = false;
// clearSelection();
// }
private int isBacklightControlFlick = 1;
private int isWarmBacklightControlFlick = 2;
private boolean isColdWarmBacklightControlTogether = false;
private boolean isTouchScreenEnabled = true;
// private boolean isManualScrollActive = false;
// private boolean isBrightnessControlActive = false;
// private int manualScrollStartPosX = -1;
// private int manualScrollStartPosY = -1;
// volatile private boolean touchEventIgnoreNextUp = false;
// volatile private int longTouchId = 0;
// volatile private long currentDoubleTapActionStart = 0;
// private boolean selectionInProgress = false;
// private int selectionStartX = 0;
// private int selectionStartY = 0;
// private int selectionEndX = 0;
// private int selectionEndY = 0;
private boolean doubleTapSelectionEnabled = false;
private int mBounceTapInterval = 150;
private int mGesturePageFlipsPerFullSwipe;
private boolean mIsPageMode;
private int secondaryTapActionType = TAP_ACTION_TYPE_LONGPRESS;
private boolean selectionModeActive = false;
public void toggleSelectionMode() {
selectionModeActive = !selectionModeActive;
mActivity.showToast(selectionModeActive ? R.string.action_toggle_selection_mode_on : R.string.action_toggle_selection_mode_off);
}
private ImageViewer currentImageViewer;
private class ImageViewer extends SimpleOnGestureListener {
private ImageInfo currentImage;
final GestureDetector detector;
int oldOrientation;
public ImageViewer(ImageInfo image) {
lockOrientation();
detector = new GestureDetector(this);
if (image.bufHeight / image.height >= 2 && image.bufWidth / image.width >= 2) {
image.scaledHeight *= 2;
image.scaledWidth *= 2;
}
centerIfLessThanScreen(image);
currentImage = image;
}
private void lockOrientation() {
oldOrientation = mActivity.getScreenOrientation();
if (oldOrientation == 4)
mActivity.setScreenOrientation(mActivity.getOrientationFromSensor());
}
private void unlockOrientation() {
if (oldOrientation == 4)
mActivity.setScreenOrientation(oldOrientation);
}
private void centerIfLessThanScreen(ImageInfo image) {
if (image.scaledHeight < image.bufHeight)
image.y = (image.bufHeight - image.scaledHeight) / 2;
if (image.scaledWidth < image.bufWidth)
image.x = (image.bufWidth - image.scaledWidth) / 2;
}
private void fixScreenBounds(ImageInfo image) {
if (image.scaledHeight > image.bufHeight) {
if (image.y < image.bufHeight - image.scaledHeight)
image.y = image.bufHeight - image.scaledHeight;
if (image.y > 0)
image.y = 0;
}
if (image.scaledWidth > image.bufWidth) {
if (image.x < image.bufWidth - image.scaledWidth)
image.x = image.bufWidth - image.scaledWidth;
if (image.x > 0)
image.x = 0;
}
}
private void updateImage(ImageInfo image) {
centerIfLessThanScreen(image);
fixScreenBounds(image);
if (!currentImage.equals(image)) {
currentImage = image;
drawPage();
}
}
public void zoomIn() {
ImageInfo image = new ImageInfo(currentImage);
if (image.scaledHeight >= image.height) {
int scale = image.scaledHeight / image.height;
if (scale < 4)
scale++;
image.scaledHeight = image.height * scale;
image.scaledWidth = image.width * scale;
} else {
int scale = image.height / image.scaledHeight;
if (scale > 1)
scale--;
image.scaledHeight = image.height / scale;
image.scaledWidth = image.width / scale;
}
updateImage(image);
}
public void zoomOut() {
ImageInfo image = new ImageInfo(currentImage);
if (image.scaledHeight > image.height) {
int scale = image.scaledHeight / image.height;
if (scale > 1)
scale--;
image.scaledHeight = image.height * scale;
image.scaledWidth = image.width * scale;
} else {
int scale = image.height / image.scaledHeight;
if (image.scaledHeight > image.bufHeight || image.scaledWidth > image.bufWidth)
scale++;
image.scaledHeight = image.height / scale;
image.scaledWidth = image.width / scale;
}
updateImage(image);
}
public int getStep() {
ImageInfo image = currentImage;
int max = image.bufHeight;
if (max < image.bufWidth)
max = image.bufWidth;
return max / 10;
}
public void moveBy(int dx, int dy) {
ImageInfo image = new ImageInfo(currentImage);
image.x += dx;
image.y += dy;
updateImage(image);
}
public boolean onKeyDown(int keyCode, final KeyEvent event) {
if (keyCode == 0)
keyCode = event.getScanCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
zoomIn();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
zoomOut();
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_ENDCALL:
close();
return true;
case KeyEvent.KEYCODE_DPAD_LEFT:
moveBy(getStep(), 0);
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
moveBy(-getStep(), 0);
return true;
case KeyEvent.KEYCODE_DPAD_UP:
moveBy(0, getStep());
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
moveBy(0, -getStep());
return true;
}
return false;
}
public boolean onKeyUp(int keyCode, final KeyEvent event) {
if (keyCode == 0)
keyCode = event.getScanCode();
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_ENDCALL:
close();
return true;
}
return false;
}
public boolean onTouchEvent(MotionEvent event) {
// int aindex = event.getActionIndex();
// if (event.getAction() == MotionEvent.ACTION_POINTER_DOWN) {
// log.v("ACTION_POINTER_DOWN");
// }
return detector.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
log.v("onFling()");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
log.v("onScroll() " + distanceX + ", " + distanceY);
int dx = (int) distanceX;
int dy = (int) distanceY;
moveBy(-dx, -dy);
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
log.v("onSingleTapConfirmed()");
ImageInfo image = new ImageInfo(currentImage);
int x = (int) e.getX();
int y = (int) e.getY();
int zone = 0;
int zw = mActivity.getDensityDpi() / 2;
int w = image.bufWidth;
int h = image.bufHeight;
if (image.rotation == 0) {
if (x < zw && y > h - zw)
zone = 1;
if (x > w - zw && y > h - zw)
zone = 2;
} else {
if (x < zw && y < zw)
zone = 1;
if (x < zw && y > h - zw)
zone = 2;
}
if (zone != 0) {
if (zone == 1)
zoomIn();
else
zoomOut();
return true;
}
close();
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
public void close() {
if (currentImageViewer == null)
return;
currentImageViewer = null;
unlockOrientation();
BackgroundThread.instance().postBackground(() -> doc.closeImage());
drawPage();
}
public BitmapInfo prepareImage() {
// called from background thread
ImageInfo img = currentImage;
img.bufWidth = internalDX;
img.bufHeight = internalDY;
if (mCurrentPageInfo != null) {
if (img.equals(mCurrentPageInfo.imageInfo))
return mCurrentPageInfo;
mCurrentPageInfo.recycle();
mCurrentPageInfo = null;
}
PositionProperties currpos = doc.getPositionProps(null, false);
BitmapInfo bi = new BitmapInfo();
bi.imageInfo = new ImageInfo(img);
bi.bitmap = factory.get(internalDX, internalDY);
bi.position = currpos;
doc.drawImage(bi.bitmap, bi.imageInfo);
mCurrentPageInfo = bi;
return mCurrentPageInfo;
}
}
private void startImageViewer(ImageInfo image) {
currentImageViewer = new ImageViewer(image);
drawPage();
}
private boolean isImageViewMode() {
return currentImageViewer != null;
}
private void stopImageViewer() {
if (currentImageViewer != null)
currentImageViewer.close();
}
private TapHandler currentTapHandler = null;
private long firstTapTimeStamp;
public class TapHandler {
private final static int STATE_INITIAL = 0; // no events yet
private final static int STATE_DOWN_1 = 1; // down first time