/
PDFView.java
1096 lines (913 loc) · 39.9 KB
/
PDFView.java
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
/**
* Copyright 2014 Joan Zapata
*
* This file is part of Android-pdfview.
*
* Android-pdfview 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 3 of the License, or
* (at your option) any later version.
*
* Android-pdfview 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 Android-pdfview. If not, see <http://www.gnu.org/licenses/>.
*/
package com.joanzapata.pdfview;
import android.content.Context;
import android.graphics.*;
import android.graphics.Paint.Style;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.SurfaceView;
import com.joanzapata.pdfview.exception.FileNotFoundException;
import com.joanzapata.pdfview.listener.OnDrawListener;
import com.joanzapata.pdfview.listener.OnLoadCompleteListener;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import com.joanzapata.pdfview.model.PagePart;
import com.joanzapata.pdfview.util.ArrayUtils;
import com.joanzapata.pdfview.util.Constants;
import com.joanzapata.pdfview.util.FileUtils;
import com.joanzapata.pdfview.util.NumberUtils;
import org.vudroid.core.DecodeService;
import java.io.File;
import java.io.IOException;
import static com.joanzapata.pdfview.util.Constants.Cache.CACHE_SIZE;
/**
* @author Joan Zapata
* <p/>
* It supports animations, zoom, cache, and swipe.
* <p/>
* To fully understand this class you must know its principles :
* - The PDF document is seen as if we always want to draw all the pages.
* - The thing is that we only draw the visible parts.
* - All parts are the same size, this is because we can't interrupt a native page rendering,
* so we need these renderings to be as fast as possible, and be able to interrupt them
* as soon as we can.
* - The parts are loaded when the current offset or the current zoom level changes
* <p/>
* Important :
* - DocumentPage = A page of the PDF document.
* - UserPage = A page as defined by the user.
* By default, they're the same. But the user can change the pages order
* using {@link #load(Uri, OnLoadCompleteListener, int[])}. In this
* particular case, a userPage of 5 can refer to a documentPage of 17.
*/
public class PDFView extends SurfaceView {
private static final String TAG = PDFView.class.getSimpleName();
/** Rendered parts go to the cache manager */
private CacheManager cacheManager;
/** Animation manager manage all offset and zoom animation */
private AnimationManager animationManager;
/** Drag manager manage all touch events */
private DragPinchManager dragPinchManager;
/**
* The pages the user want to display in order
* (ex: 0, 2, 2, 8, 8, 1, 1, 1)
*/
private int[] originalUserPages;
/**
* The same pages but with a filter to avoid repetition
* (ex: 0, 2, 8, 1)
*/
private int[] filteredUserPages;
/**
* The same pages but with a filter to avoid repetition
* (ex: 0, 1, 1, 2, 2, 3, 3, 3)
*/
private int[] filteredUserPageIndexes;
/** Number of pages in the loaded PDF document */
private int documentPageCount;
/** The index of the current sequence */
private int currentPage;
/** The index of the current sequence */
private int currentFilteredPage;
/** The actual width and height of the pages in the PDF document */
private int pageWidth, pageHeight;
/** The optimal width and height of the pages to fit the component size */
private float optimalPageWidth, optimalPageHeight;
/**
* If you picture all the pages side by side in their optimal width,
* and taking into account the zoom level, the current offset is the
* position of the left border of the screen in this big picture
*/
private float currentXOffset = 0;
/**
* If you picture all the pages side by side in their optimal width,
* and taking into account the zoom level, the current offset is the
* position of the left border of the screen in this big picture
*/
private float currentYOffset = 0;
/** The zoom level, always >= 1 */
private float zoom = 1f;
/** Coordinates of the left mask on the screen */
private RectF leftMask;
/** Coordinates of the right mask on the screen */
private RectF rightMask;
/** True if the PDFView has been recycled */
private boolean recycled = true;
/** Current state of the view */
private State state = State.DEFAULT;
/** The VuDroid DecodeService used for decoding PDF and pages */
private DecodeService decodeService;
/** Async task used during the loading phase to decode a PDF document */
private DecodingAsyncTask decodingAsyncTask;
/** Async task always playing in the background and proceeding rendering tasks */
private RenderingAsyncTask renderingAsyncTask;
/** Call back object to call when the PDF is loaded */
private OnLoadCompleteListener onLoadCompleteListener;
/** Call back object to call when the page has changed */
private OnPageChangeListener onPageChangeListener;
/** Call back object to call when the above layer is to drawn */
private OnDrawListener onDrawListener;
/** Paint object for drawing */
private Paint paint;
/** Paint object for drawing mask */
private Paint maskPaint;
/** Paint object for drawing debug stuff */
private Paint debugPaint;
/** Paint object for minimap background */
private Paint paintMinimapBack;
private Paint paintMinimapFront;
/** True if should draw map on the top right corner */
private boolean miniMapRequired;
/** Bounds of the minimap */
private RectF minimapBounds;
/** Bounds of the minimap */
private RectF minimapScreenBounds;
private int defaultPage = 0;
private boolean userWantsMinimap = false;
/** True if should scroll through pages vertically instead of horizontally */
private boolean swipeVertical = false;
/** Construct the initial view */
public PDFView(Context context, AttributeSet set) {
super(context, set);
miniMapRequired = false;
cacheManager = new CacheManager();
animationManager = new AnimationManager(this);
dragPinchManager = new DragPinchManager(this);
paint = new Paint();
debugPaint = new Paint();
debugPaint.setStyle(Style.STROKE);
paintMinimapBack = new Paint();
paintMinimapBack.setStyle(Style.FILL);
paintMinimapBack.setColor(Color.BLACK);
paintMinimapBack.setAlpha(50);
paintMinimapFront = new Paint();
paintMinimapFront.setStyle(Style.FILL);
paintMinimapFront.setColor(Color.BLACK);
paintMinimapFront.setAlpha(50);
// A surface view does not call
// onDraw() as a default but we need it.
setWillNotDraw(false);
}
private void load(Uri uri, OnLoadCompleteListener listener) {
load(uri, listener, null);
}
private void load(Uri uri, OnLoadCompleteListener onLoadCompleteListener, int[] userPages) {
if (!recycled) {
throw new IllegalStateException("Don't call load on a PDF View without recycling it first.");
}
// Manage UserPages if not null
if (userPages != null) {
this.originalUserPages = userPages;
this.filteredUserPages = ArrayUtils.deleteDuplicatedPages(originalUserPages);
this.filteredUserPageIndexes = ArrayUtils.calculateIndexesInDuplicateArray(originalUserPages);
}
this.onLoadCompleteListener = onLoadCompleteListener;
// Start decoding document
decodingAsyncTask = new DecodingAsyncTask(uri, this);
decodingAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
renderingAsyncTask = new RenderingAsyncTask(this);
renderingAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* Go to the given page.
* @param page Page number starting from 1.
*/
public void jumpTo(int page) {
showPage(page - 1);
}
void showPage(int pageNb) {
state = State.SHOWN;
// Check the page number and makes the
// difference between UserPages and DocumentPages
pageNb = determineValidPageNumberFrom(pageNb);
currentPage = pageNb;
currentFilteredPage = pageNb;
if (filteredUserPageIndexes != null) {
if (pageNb >= 0 && pageNb < filteredUserPageIndexes.length) {
pageNb = filteredUserPageIndexes[pageNb];
currentFilteredPage = pageNb;
}
}
// Reset the zoom and center the page on the screen
resetZoom();
if (swipeVertical)
animationManager.startYAnimation(currentYOffset, calculateCenterOffsetForPage(pageNb));
else
animationManager.startXAnimation(currentXOffset, calculateCenterOffsetForPage(pageNb));
loadPages();
if (onPageChangeListener != null) {
onPageChangeListener.onPageChanged(currentPage + 1, getPageCount());
}
}
public int getPageCount() {
if (originalUserPages != null) {
return originalUserPages.length;
}
return documentPageCount;
}
public void enableSwipe(boolean enableSwipe) {
dragPinchManager.setSwipeEnabled(enableSwipe);
}
public void enableDoubletap(boolean enableDoubletap){
this.dragPinchManager.enableDoubletap(enableDoubletap);
}
private void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) {
this.onPageChangeListener = onPageChangeListener;
}
private void setOnDrawListener(OnDrawListener onDrawListener) {
this.onDrawListener = onDrawListener;
}
public void recycle() {
// Stop tasks
if (renderingAsyncTask != null) {
renderingAsyncTask.cancel(true);
}
if (decodingAsyncTask != null) {
decodingAsyncTask.cancel(true);
}
// Clear caches
cacheManager.recycle();
recycled = true;
state = State.DEFAULT;
}
@Override
protected void onDetachedFromWindow() {
recycle();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
animationManager.stopAll();
calculateOptimalWidthAndHeight();
loadPages();
if (swipeVertical)
moveTo(currentXOffset, calculateCenterOffsetForPage(currentFilteredPage));
else
moveTo(calculateCenterOffsetForPage(currentFilteredPage), currentYOffset);
}
@Override
protected void onDraw(Canvas canvas) {
// As I said in this class javadoc, we can think of this canvas as a huge
// strip on which we draw all the images. We actually only draw the rendered
// parts, of course, but we render them in the place they belong in this huge
// strip.
// That's where Canvas.translate(x, y) becomes very helpful.
// This is the situation :
// _______________________________________________
// | | |
// | the actual | The big strip |
// | canvas | |
// |_____________| |
// |_______________________________________________|
//
// If the rendered part is on the bottom right corner of the strip
// we can draw it but we won't see it because the canvas is not big enough.
// But if we call translate(-X, -Y) on the canvas just before drawing the object :
// _______________________________________________
// | _____________|
// | The big strip | |
// | | the actual |
// | | canvas |
// |_________________________________|_____________|
//
// The object will be on the canvas.
// This technique is massively used in this method, and allows
// abstraction of the screen position when rendering the parts.
// Draws background
canvas.drawColor(Color.WHITE);
if (state != State.SHOWN) {
return;
}
// Moves the canvas before drawing any element
float currentXOffset = this.currentXOffset;
float currentYOffset = this.currentYOffset;
canvas.translate(currentXOffset, currentYOffset);
// Draws thumb nails
for (PagePart part : cacheManager.getThumbnails()) {
drawPart(canvas, part);
}
// Draws parts
for (PagePart part : cacheManager.getPageParts()) {
drawPart(canvas, part);
}
// Draws the user layer
if (onDrawListener != null) {
canvas.translate(toCurrentScale(currentFilteredPage * optimalPageWidth), 0);
onDrawListener.onLayerDrawn(canvas, //
toCurrentScale(optimalPageWidth), //
toCurrentScale(optimalPageHeight),
currentPage);
canvas.translate(-toCurrentScale(currentFilteredPage * optimalPageWidth), 0);
}
// Restores the canvas position
canvas.translate(-currentXOffset, -currentYOffset);
// Draws mask around current page
canvas.drawRect(leftMask, maskPaint);
canvas.drawRect(rightMask, maskPaint);
// If minimap shown draws it
if (userWantsMinimap && miniMapRequired) {
drawMiniMap(canvas);
}
}
public void onLayerUpdate() {
invalidate();
}
/** Draw a given PagePart on the canvas */
private void drawPart(Canvas canvas, PagePart part) {
// Can seem strange, but avoid lot of calls
RectF pageRelativeBounds = part.getPageRelativeBounds();
Bitmap renderedBitmap = part.getRenderedBitmap();
// Move to the target page
float localTranslationX = 0;
float localTranslationY = 0;
if (swipeVertical)
localTranslationY = toCurrentScale(part.getUserPage() * optimalPageHeight);
else
localTranslationX = toCurrentScale(part.getUserPage() * optimalPageWidth);
canvas.translate(localTranslationX, localTranslationY);
Rect srcRect = new Rect(0, 0, renderedBitmap.getWidth(), //
renderedBitmap.getHeight());
float offsetX = toCurrentScale(pageRelativeBounds.left * optimalPageWidth);
float offsetY = toCurrentScale(pageRelativeBounds.top * optimalPageHeight);
float width = toCurrentScale(pageRelativeBounds.width() * optimalPageWidth);
float height = toCurrentScale(pageRelativeBounds.height() * optimalPageHeight);
// If we use float values for this rectangle, there will be
// a possible gap between page parts, especially when
// the zoom level is high.
RectF dstRect = new RectF((int) offsetX, (int) offsetY, //
(int) (offsetX + width), //
(int) (offsetY + height));
// Check if bitmap is in the screen
float translationX = currentXOffset + localTranslationX;
float translationY = currentYOffset + localTranslationY;
if (translationX + dstRect.left >= getWidth() || translationX + dstRect.right <= 0 ||
translationY + dstRect.top >= getHeight() || translationY + dstRect.bottom <= 0) {
canvas.translate(-localTranslationX, -localTranslationY);
return;
}
canvas.drawBitmap(renderedBitmap, srcRect, dstRect, paint);
if (Constants.DEBUG_MODE) {
debugPaint.setColor(part.getUserPage() % 2 == 0 ? Color.RED : Color.BLUE);
canvas.drawRect(dstRect, debugPaint);
}
// Restore the canvas position
canvas.translate(-localTranslationX, -localTranslationY);
}
private void drawMiniMap(Canvas canvas) {
canvas.drawRect(minimapBounds, paintMinimapBack);
canvas.drawRect(minimapScreenBounds, paintMinimapFront);
}
/**
* Load all the parts around the center of the screen,
* taking into account X and Y offsets, zoom level, and
* the current page displayed
*/
public void loadPages() {
if (optimalPageWidth == 0 || optimalPageHeight == 0) {
return;
}
// Cancel all current tasks
renderingAsyncTask.removeAllTasks();
cacheManager.makeANewSet();
// Find current index in filtered user pages
int index = currentPage;
if (filteredUserPageIndexes != null) {
index = filteredUserPageIndexes[currentPage];
}
// Loop through the pages like [...][4][2][0][1][3][...]
// loading as many parts as it can.
int parts = 0;
for (int i = 0; i <= Constants.LOADED_SIZE / 2 && parts < CACHE_SIZE; i++) {
parts += loadPage(index + i, CACHE_SIZE - parts);
if (i != 0 && parts < CACHE_SIZE) {
parts += loadPage(index - i, CACHE_SIZE - parts);
}
}
invalidate();
}
/**
* Render a page, creating 1 to <i>nbOfPartsLoadable</i> page parts. <br><br>
* <p/>
* This is one of the trickiest method of this library. It finds
* the DocumentPage associated with the given UserPage, loads its
* thumbnail, cut this page into 256x256 blocs considering the
* current zoom level, calculate the bloc containing the center of
* the screen, and start loading these parts in a spiral {@link SpiralLoopManager},
* only if the given part is not already in the Cache, in which case it
* moves the part up in the cache.
* @param userPage The user page to load.
* @param nbOfPartsLoadable Maximum number of parts it can load.
* @return The number of parts loaded.
*/
private int loadPage(final int userPage, final int nbOfPartsLoadable) {
// Finds the document page associated with the given userPage
int documentPage = userPage;
if (filteredUserPages != null) {
if (userPage < 0 || userPage >= filteredUserPages.length) {
return 0;
} else {
documentPage = filteredUserPages[userPage];
}
}
final int documentPageFinal = documentPage;
if (documentPage < 0 || userPage >= documentPageCount) {
return 0;
}
// Render thumbnail of the page
if (!cacheManager.containsThumbnail(userPage, documentPage, //
(int) (optimalPageWidth * Constants.THUMBNAIL_RATIO), //
(int) (optimalPageHeight * Constants.THUMBNAIL_RATIO), //
new RectF(0, 0, 1, 1))) {
renderingAsyncTask.addRenderingTask(userPage, documentPage, //
(int) (optimalPageWidth * Constants.THUMBNAIL_RATIO), //
(int) (optimalPageHeight * Constants.THUMBNAIL_RATIO), //
new RectF(0, 0, 1, 1), true, 0);
}
// When we want to render a 256x256 bloc, we also need to provide
// the bounds (left, top, right, bottom) of the rendered part in
// the PDF page. These four coordinates are ratios (0 -> 1), where
// (0,0) is the top left corner of the PDF page, and (1,1) is the
// bottom right corner.
float ratioX = 1f / (float) optimalPageWidth;
float ratioY = 1f / (float) optimalPageHeight;
final float partHeight = (Constants.PART_SIZE * ratioY) / zoom;
final float partWidth = (Constants.PART_SIZE * ratioX) / zoom;
final int nbRows = (int) Math.ceil(1f / partHeight);
final int nbCols = (int) Math.ceil(1f / partWidth);
final float pageRelativePartWidth = 1f / (float) nbCols;
final float pageRelativePartHeight = 1f / (float) nbRows;
// To improve user experience, we need to start displaying the
// 256x256 blocs with the middle of the screen. Imagine the cut
// page as a grid. This part calculates which cell of this grid
// is currently in the middle of the screen, given the current
// zoom level and the offsets.
float middleOfScreenX = (-currentXOffset + getWidth() / 2);
float middleOfScreenY = (-currentYOffset + getHeight() / 2);
float middleOfScreenPageX;
float middleOfScreenPageY;
if (!swipeVertical) {
middleOfScreenPageX = middleOfScreenX - userPage * toCurrentScale(optimalPageWidth);
middleOfScreenPageY = middleOfScreenY;
} else {
middleOfScreenPageY = middleOfScreenY - userPage * toCurrentScale(optimalPageHeight);
middleOfScreenPageX = middleOfScreenX;
}
float middleOfScreenPageXRatio = middleOfScreenPageX / toCurrentScale(optimalPageWidth);
float middleOfScreenPageYRatio = middleOfScreenPageY / toCurrentScale(optimalPageHeight);
int startingRow = (int) (middleOfScreenPageYRatio * nbRows);
int startingCol = (int) (middleOfScreenPageXRatio * nbCols);
// Avoid outside values
startingRow = NumberUtils.limit(startingRow, 0, nbRows);
startingCol = NumberUtils.limit(startingCol, 0, nbCols);
// Prepare the loop listener
class SpiralLoopListenerImpl implements SpiralLoopManager.SpiralLoopListener {
int nbItemTreated = 0;
@Override
public boolean onLoop(int row, int col) {
// Create relative page bounds
float relX = pageRelativePartWidth * col;
float relY = pageRelativePartHeight * row;
float relWidth = pageRelativePartWidth;
float relHeight = pageRelativePartHeight;
// Adjust width and height to
// avoid being outside the page
float renderWidth = Constants.PART_SIZE / relWidth;
float renderHeight = Constants.PART_SIZE / relHeight;
if (relX + relWidth > 1) {
relWidth = 1 - relX;
}
if (relY + relHeight > 1) {
relHeight = 1 - relY;
}
renderWidth *= relWidth;
renderHeight *= relHeight;
RectF pageRelativeBounds = new RectF(relX, relY, relX + relWidth, relY + relHeight);
if (renderWidth != 0 && renderHeight != 0) {
// Check it the calculated part is already contained in the Cache
// If it is, this call will insure the part will go to the right
// place in the cache and won't be deleted if the cache need space.
if (!cacheManager.upPartIfContained(userPage, documentPageFinal, //
renderWidth, renderHeight, pageRelativeBounds, nbItemTreated)) {
// If not already in cache, register the rendering
// task for further execution.
renderingAsyncTask.addRenderingTask(userPage, documentPageFinal, //
renderWidth, renderHeight, pageRelativeBounds, false, nbItemTreated);
}
}
nbItemTreated++;
if (nbItemTreated >= nbOfPartsLoadable) {
// Return false to stop the loop
return false;
}
return true;
}
}
// Starts the loop
SpiralLoopListenerImpl spiralLoopListener;
new SpiralLoopManager(spiralLoopListener = new SpiralLoopListenerImpl())//
.startLoop(nbRows, nbCols, startingRow, startingCol);
return spiralLoopListener.nbItemTreated;
}
/** Called when the PDF is loaded */
public void loadComplete(DecodeService decodeService) {
this.decodeService = decodeService;
this.documentPageCount = decodeService.getPageCount();
// We assume all the pages are the same size
this.pageWidth = decodeService.getPageWidth(0);
this.pageHeight = decodeService.getPageHeight(0);
state = State.LOADED;
calculateOptimalWidthAndHeight();
// Notify the listener
jumpTo(defaultPage);
if (onLoadCompleteListener != null) {
onLoadCompleteListener.loadComplete(documentPageCount);
}
}
/**
* Called when a rendering task is over and
* a PagePart has been freshly created.
* @param part The created PagePart.
*/
public void onBitmapRendered(PagePart part) {
if (part.isThumbnail()) {
cacheManager.cacheThumbnail(part);
} else {
cacheManager.cachePart(part);
}
invalidate();
}
/**
* Given the UserPage number, this method restrict it
* to be sure it's an existing page. It takes care of
* using the user defined pages if any.
* @param userPage A page number.
* @return A restricted valid page number (example : -2 => 0)
*/
private int determineValidPageNumberFrom(int userPage) {
if (userPage <= 0) {
return 0;
}
if (originalUserPages != null) {
if (userPage >= originalUserPages.length) {
return originalUserPages.length - 1;
}
} else {
if (userPage >= documentPageCount) {
return documentPageCount - 1;
}
}
return userPage;
}
/**
* Calculate the x/y-offset needed to have the given
* page centered on the screen. It doesn't take into
* account the zoom level.
* @param pageNb The page number.
* @return The x/y-offset to use to have the pageNb centered.
*/
private float calculateCenterOffsetForPage(int pageNb) {
if (swipeVertical) {
float imageY = -(pageNb * optimalPageHeight);
imageY += getHeight() / 2 - optimalPageHeight / 2;
return imageY;
} else {
float imageX = -(pageNb * optimalPageWidth);
imageX += getWidth() / 2 - optimalPageWidth / 2;
return imageX;
}
}
/**
* Calculate the optimal width and height of a page
* considering the area width and height
*/
private void calculateOptimalWidthAndHeight() {
if (state == State.DEFAULT || getWidth() == 0) {
return;
}
float maxWidth = getWidth(), maxHeight = getHeight();
float w = pageWidth, h = pageHeight;
float ratio = w / h;
w = maxWidth;
h = (float) Math.floor(maxWidth / ratio);
if (h > maxHeight) {
h = maxHeight;
w = (float) Math.floor(maxHeight * ratio);
}
optimalPageWidth = w;
optimalPageHeight = h;
calculateMasksBounds();
calculateMinimapBounds();
}
/**
* Place the minimap background considering the optimal width and height
* and the MINIMAP_MAX_SIZE.
*/
private void calculateMinimapBounds() {
float ratioX = Constants.MINIMAP_MAX_SIZE / optimalPageWidth;
float ratioY = Constants.MINIMAP_MAX_SIZE / optimalPageHeight;
float ratio = Math.min(ratioX, ratioY);
float minimapWidth = optimalPageWidth * ratio;
float minimapHeight = optimalPageHeight * ratio;
minimapBounds = new RectF(getWidth() - 5 - minimapWidth, 5, getWidth() - 5, 5 + minimapHeight);
calculateMinimapAreaBounds();
}
/**
* Place the minimap current rectangle considering the minimap bounds
* the zoom level, and the current X/Y offsets
*/
private void calculateMinimapAreaBounds() {
if (minimapBounds == null) {
return;
}
if (zoom == 1f) {
miniMapRequired = false;
} else {
// Calculates the bounds of the current displayed area
float x = (-currentXOffset - toCurrentScale(currentFilteredPage * optimalPageWidth)) //
/ toCurrentScale(optimalPageWidth) * minimapBounds.width();
float width = getWidth() / toCurrentScale(optimalPageWidth) * minimapBounds.width();
float y = -currentYOffset / toCurrentScale(optimalPageHeight) * minimapBounds.height();
float height = getHeight() / toCurrentScale(optimalPageHeight) * minimapBounds.height();
minimapScreenBounds = new RectF(minimapBounds.left + x, minimapBounds.top + y, //
minimapBounds.left + x + width, minimapBounds.top + y + height);
minimapScreenBounds.intersect(minimapBounds);
miniMapRequired = true;
}
}
/** Place the left and right masks around the current page. */
private void calculateMasksBounds() {
leftMask = new RectF(0, 0, getWidth() / 2 - toCurrentScale(optimalPageWidth) / 2, getHeight());
rightMask = new RectF(getWidth() / 2 + toCurrentScale(optimalPageWidth) / 2, 0, getWidth(), getHeight());
}
/**
* Move to the given X and Y offsets, but check them ahead of time
* to be sure not to go outside the the big strip.
* @param offsetX The big strip X offset to use as the left border of the screen.
* @param offsetY The big strip Y offset to use as the right border of the screen.
*/
public void moveTo(float offsetX, float offsetY) {
if (swipeVertical) {
// Check X offset
if (toCurrentScale(optimalPageWidth) < getWidth()) {
offsetX = getWidth() / 2 - toCurrentScale(optimalPageWidth) / 2;
} else {
if (offsetX > 0) {
offsetX = 0;
} else if (offsetX + toCurrentScale(optimalPageWidth) < getWidth()) {
offsetX = getWidth() - toCurrentScale(optimalPageWidth);
}
}
// Check Y offset
if (isZooming()) {
if (toCurrentScale(optimalPageHeight) < getHeight()) {
miniMapRequired = false;
offsetY = getHeight() / 2 - toCurrentScale((currentFilteredPage + 0.5f) * optimalPageHeight);
} else {
miniMapRequired = true;
if (offsetY + toCurrentScale(currentFilteredPage * optimalPageHeight) > 0) {
offsetY = -toCurrentScale(currentFilteredPage * optimalPageHeight);
} else if (offsetY + toCurrentScale((currentFilteredPage + 1) * optimalPageHeight) < getHeight()) {
offsetY = getHeight() - toCurrentScale((currentFilteredPage + 1) * optimalPageHeight);
}
}
} else {
float maxY = calculateCenterOffsetForPage(currentFilteredPage + 1);
float minY = calculateCenterOffsetForPage(currentFilteredPage - 1);
if (offsetY < maxY) {
offsetY = maxY;
} else if (offsetY > minY) {
offsetY = minY;
}
}
} else {
// Check Y offset
if (toCurrentScale(optimalPageHeight) < getHeight()) {
offsetY = getHeight() / 2 - toCurrentScale(optimalPageHeight) / 2;
} else {
if (offsetY > 0) {
offsetY = 0;
} else if (offsetY + toCurrentScale(optimalPageHeight) < getHeight()) {
offsetY = getHeight() - toCurrentScale(optimalPageHeight);
}
}
// Check X offset
if (isZooming()) {
if (toCurrentScale(optimalPageWidth) < getWidth()) {
miniMapRequired = false;
offsetX = getWidth() / 2 - toCurrentScale((currentFilteredPage + 0.5f) * optimalPageWidth);
} else {
miniMapRequired = true;
if (offsetX + toCurrentScale(currentFilteredPage * optimalPageWidth) > 0) {
offsetX = -toCurrentScale(currentFilteredPage * optimalPageWidth);
} else if (offsetX + toCurrentScale((currentFilteredPage + 1) * optimalPageWidth) < getWidth()) {
offsetX = getWidth() - toCurrentScale((currentFilteredPage + 1) * optimalPageWidth);
}
}
} else {
float maxX = calculateCenterOffsetForPage(currentFilteredPage + 1);
float minX = calculateCenterOffsetForPage(currentFilteredPage - 1);
if (offsetX < maxX) {
offsetX = maxX;
} else if (offsetX > minX) {
offsetX = minX;
}
}
}
currentXOffset = offsetX;
currentYOffset = offsetY;
calculateMinimapAreaBounds();
invalidate();
}
/**
* Move relatively to the current position.
* @param dx The X difference you want to apply.
* @param dy The Y difference you want to apply.
* @see #moveTo(float, float)
*/
public void moveRelativeTo(float dx, float dy) {
moveTo(currentXOffset + dx, currentYOffset + dy);
}
/** Change the zoom level */
public void zoomTo(float zoom) {
this.zoom = zoom;
calculateMasksBounds();
}
/**
* Change the zoom level, relatively to a pivot point.
* It will call moveTo() to make sure the given point stays
* in the middle of the screen.
* @param zoom The zoom level.
* @param pivot The point on the screen that should stays.
*/
public void zoomCenteredTo(float zoom, PointF pivot) {
float dzoom = zoom / this.zoom;
zoomTo(zoom);
float baseX = currentXOffset * dzoom;
float baseY = currentYOffset * dzoom;
baseX += (pivot.x - pivot.x * dzoom);
baseY += (pivot.y - pivot.y * dzoom);
moveTo(baseX, baseY);
}
/** @see #zoomCenteredTo(float, PointF) */
public void zoomCenteredRelativeTo(float dzoom, PointF pivot) {
zoomCenteredTo(zoom * dzoom, pivot);
}
public int getCurrentPage() {
return currentPage;
}
public float getCurrentXOffset() {
return currentXOffset;
}
public float getCurrentYOffset() {
return currentYOffset;
}
public float toRealScale(float size) {
return size / zoom;
}
public float toCurrentScale(float size) {
return size * zoom;
}
public float getZoom() {
return zoom;
}
DecodeService getDecodeService() {
return decodeService;
}
public boolean isZooming() {
return zoom != 1;
}
public float getOptimalPageWidth() {
return optimalPageWidth;
}
private void setUserWantsMinimap(boolean userWantsMinimap) {
this.userWantsMinimap = userWantsMinimap;
}
private void setDefaultPage(int defaultPage) {
this.defaultPage = defaultPage;
}
public void resetZoom() {
zoomTo(1);
}
public void resetZoomWithAnimation() {
animationManager.startZoomAnimation(zoom, 1f);
}
/** Use an asset file as the pdf source */
public Configurator fromAsset(String assetName) {
try {
File pdfFile = FileUtils.fileFromAsset(getContext(), assetName);
return fromFile(pdfFile);
} catch (IOException e) {
throw new FileNotFoundException(assetName + " does not exist.", e);
}
}
/** Use a file as the pdf source */
public Configurator fromFile(File file) {
if (!file.exists()) throw new FileNotFoundException(file.getAbsolutePath() + "does not exist.");
return new Configurator(Uri.fromFile(file));
}
private enum State {DEFAULT, LOADED, SHOWN}
public class Configurator {
private final Uri uri;
private int[] pageNumbers = null;
private boolean enableSwipe = true;
private boolean enableDoubletap = true ;
private OnDrawListener onDrawListener;
private OnLoadCompleteListener onLoadCompleteListener;
private OnPageChangeListener onPageChangeListener;
private int defaultPage = 1;
private boolean showMinimap = false;