Skip to content

Commit 9124e2c

Browse files
committed
Merge branch 'master' of https://github.com/Yazon2006/AndroidPdfViewer into Yazon2006-master
2 parents 6c61b74 + 0316099 commit 9124e2c

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/DecodingAsyncTask.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import com.shockwave.pdfium.PdfiumCore;
2323
import com.shockwave.pdfium.util.Size;
2424

25+
import java.lang.ref.WeakReference;
26+
2527
class DecodingAsyncTask extends AsyncTask<Void, Void, Throwable> {
2628

2729
private boolean cancelled;
2830

29-
private PDFView pdfView;
31+
private WeakReference<PDFView> pdfViewReference;
3032

3133
private PdfiumCore pdfiumCore;
3234
private String password;
@@ -38,35 +40,44 @@ class DecodingAsyncTask extends AsyncTask<Void, Void, Throwable> {
3840
this.docSource = docSource;
3941
this.userPages = userPages;
4042
this.cancelled = false;
41-
this.pdfView = pdfView;
43+
this.pdfViewReference = new WeakReference<>(pdfView);
4244
this.password = password;
4345
this.pdfiumCore = pdfiumCore;
4446
}
4547

4648
@Override
4749
protected Throwable doInBackground(Void... params) {
4850
try {
49-
PdfDocument pdfDocument = docSource.createDocument(pdfView.getContext(), pdfiumCore, password);
50-
pdfFile = new PdfFile(pdfiumCore, pdfDocument, pdfView.getPageFitPolicy(), getViewSize(),
51-
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx(), pdfView.doAutoSpacing());
52-
return null;
51+
PDFView pdfView = pdfViewReference.get();
52+
if (pdfView != null) {
53+
PdfDocument pdfDocument = docSource.createDocument(pdfView.getContext(), pdfiumCore, password);
54+
pdfFile = new PdfFile(pdfiumCore, pdfDocument, pdfView.getPageFitPolicy(), getViewSize(pdfView),
55+
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx(), pdfView.doAutoSpacing());
56+
return null;
57+
} else {
58+
return new NullPointerException("pdfView == null");
59+
}
60+
5361
} catch (Throwable t) {
5462
return t;
5563
}
5664
}
5765

58-
private Size getViewSize() {
66+
private Size getViewSize(PDFView pdfView) {
5967
return new Size(pdfView.getWidth(), pdfView.getHeight());
6068
}
6169

6270
@Override
6371
protected void onPostExecute(Throwable t) {
64-
if (t != null) {
65-
pdfView.loadError(t);
66-
return;
67-
}
68-
if (!cancelled) {
69-
pdfView.loadComplete(pdfFile);
72+
PDFView pdfView = pdfViewReference.get();
73+
if (pdfView != null) {
74+
if (t != null) {
75+
pdfView.loadError(t);
76+
return;
77+
}
78+
if (!cancelled) {
79+
pdfView.loadComplete(pdfFile);
80+
}
7081
}
7182
}
7283

0 commit comments

Comments
 (0)