3131import android .util .Log ;
3232import android .widget .RelativeLayout ;
3333
34- import com .github .barteksc .pdfviewer .exception .FileNotFoundException ;
3534import com .github .barteksc .pdfviewer .listener .OnDrawListener ;
3635import com .github .barteksc .pdfviewer .listener .OnErrorListener ;
3736import com .github .barteksc .pdfviewer .listener .OnLoadCompleteListener ;
3837import com .github .barteksc .pdfviewer .listener .OnPageChangeListener ;
3938import com .github .barteksc .pdfviewer .listener .OnPageScrollListener ;
4039import com .github .barteksc .pdfviewer .model .PagePart ;
4140import com .github .barteksc .pdfviewer .scroll .ScrollHandle ;
41+ import com .github .barteksc .pdfviewer .source .AssetSource ;
42+ import com .github .barteksc .pdfviewer .source .ByteArraySource ;
43+ import com .github .barteksc .pdfviewer .source .DocumentSource ;
44+ import com .github .barteksc .pdfviewer .source .FileSource ;
45+ import com .github .barteksc .pdfviewer .source .InputStreamSource ;
46+ import com .github .barteksc .pdfviewer .source .UriSource ;
4247import com .github .barteksc .pdfviewer .util .ArrayUtils ;
4348import com .github .barteksc .pdfviewer .util .Constants ;
4449import com .github .barteksc .pdfviewer .util .MathUtils ;
6671 * - DocumentPage = A page of the PDF document.
6772 * - UserPage = A page as defined by the user.
6873 * By default, they're the same. But the user can change the pages order
69- * using {@link #load(String, boolean , String, OnLoadCompleteListener, OnErrorListener, int[])}. In this
74+ * using {@link #load(DocumentSource , String, OnLoadCompleteListener, OnErrorListener, int[])}. In this
7075 * particular case, a userPage of 5 can refer to a documentPage of 17.
7176 */
7277public class PDFView extends RelativeLayout {
@@ -279,11 +284,11 @@ public PDFView(Context context, AttributeSet set) {
279284 setWillNotDraw (false );
280285 }
281286
282- private void load (String path , boolean isAsset , String password , OnLoadCompleteListener listener , OnErrorListener onErrorListener ) {
283- load (path , isAsset , password , listener , onErrorListener , null );
287+ private void load (DocumentSource docSource , String password , OnLoadCompleteListener listener , OnErrorListener onErrorListener ) {
288+ load (docSource , password , listener , onErrorListener , null );
284289 }
285290
286- private void load (String path , boolean isAsset , String password , OnLoadCompleteListener onLoadCompleteListener , OnErrorListener onErrorListener , int [] userPages ) {
291+ private void load (DocumentSource docSource , String password , OnLoadCompleteListener onLoadCompleteListener , OnErrorListener onErrorListener , int [] userPages ) {
287292
288293 if (!recycled ) {
289294 throw new IllegalStateException ("Don't call load on a PDF View without recycling it first." );
@@ -301,7 +306,7 @@ private void load(String path, boolean isAsset, String password, OnLoadCompleteL
301306
302307 recycled = false ;
303308 // Start decoding document
304- decodingAsyncTask = new DecodingAsyncTask (path , isAsset , password , this , pdfiumCore );
309+ decodingAsyncTask = new DecodingAsyncTask (docSource , password , this , pdfiumCore );
305310 decodingAsyncTask .executeOnExecutor (AsyncTask .THREAD_POOL_EXECUTOR );
306311 }
307312
@@ -1108,47 +1113,48 @@ public List<PdfDocument.Bookmark> getTableOfContents() {
11081113 * Use an asset file as the pdf source
11091114 */
11101115 public Configurator fromAsset (String assetName ) {
1111- InputStream stream = null ;
1112- try {
1113- stream = getContext ().getAssets ().open (assetName );
1114- return new Configurator (assetName , true );
1115- } catch (IOException e ) {
1116- throw new FileNotFoundException (assetName + " does not exist." , e );
1117- } finally {
1118- try {
1119- if (stream != null ) {
1120- stream .close ();
1121- }
1122- } catch (IOException e ) {
1123-
1124- }
1125- }
1116+ return new Configurator (new AssetSource (assetName ));
11261117 }
11271118
11281119 /**
11291120 * Use a file as the pdf source
11301121 */
11311122 public Configurator fromFile (File file ) {
1132- if (!file .exists ()) {
1133- throw new FileNotFoundException (file .getAbsolutePath () + " does not exist." );
1134- }
1135- return new Configurator (file .getAbsolutePath (), false );
1123+ return new Configurator (new FileSource (file ));
11361124 }
11371125
11381126 /**
1139- * Use Uri as the pdf source, for use with content provider
1127+ * Use URI as the pdf source, for use with content providers
11401128 */
11411129 public Configurator fromUri (Uri uri ) {
1142- return new Configurator (uri .toString (), false );
1130+ return new Configurator (new UriSource (uri ));
1131+ }
1132+
1133+ /**
1134+ * Use bytearray as the pdf source, documents is not saved
1135+ * @param bytes
1136+ * @return
1137+ */
1138+ public Configurator fromBytes (byte [] bytes ) {
1139+ return new Configurator (new ByteArraySource (bytes ));
1140+ }
1141+
1142+ public Configurator fromStream (InputStream stream ) {
1143+ return new Configurator (new InputStreamSource (stream ));
1144+ }
1145+
1146+ /**
1147+ * Use custom source as pdf source
1148+ */
1149+ public Configurator fromSource (DocumentSource docSource ) {
1150+ return new Configurator (docSource );
11431151 }
11441152
11451153 private enum State {DEFAULT , LOADED , SHOWN , ERROR }
11461154
11471155 public class Configurator {
11481156
1149- private final String path ;
1150-
1151- private final boolean isAsset ;
1157+ private final DocumentSource documentSource ;
11521158
11531159 private int [] pageNumbers = null ;
11541160
@@ -1176,9 +1182,8 @@ public class Configurator {
11761182
11771183 private ScrollHandle scrollHandle = null ;
11781184
1179- private Configurator (String path , boolean isAsset ) {
1180- this .path = path ;
1181- this .isAsset = isAsset ;
1185+ private Configurator (DocumentSource documentSource ) {
1186+ this .documentSource = documentSource ;
11821187 }
11831188
11841189 public Configurator pages (int ... pageNumbers ) {
@@ -1259,9 +1264,9 @@ public void load() {
12591264 PDFView .this .setScrollHandle (scrollHandle );
12601265 PDFView .this .dragPinchManager .setSwipeVertical (swipeVertical );
12611266 if (pageNumbers != null ) {
1262- PDFView .this .load (path , isAsset , password , onLoadCompleteListener , onErrorListener , pageNumbers );
1267+ PDFView .this .load (documentSource , password , onLoadCompleteListener , onErrorListener , pageNumbers );
12631268 } else {
1264- PDFView .this .load (path , isAsset , password , onLoadCompleteListener , onErrorListener );
1269+ PDFView .this .load (documentSource , password , onLoadCompleteListener , onErrorListener );
12651270 }
12661271 }
12671272 }
0 commit comments