summaryrefslogtreecommitdiff
path: root/android
diff options
context:
space:
mode:
authorPaul Gardiner <paul@glidos.net>2012-04-25 11:56:45 +0100
committerPaul Gardiner <paul@glidos.net>2012-04-25 11:56:45 +0100
commit96a87dc0d4df044da91b40252d85ceaa832eaa9c (patch)
treed073aeb11aa645112a78a2d81dbdc630cd739231 /android
parentaf5a6088a3c3e4d61f84d2c93a8968c04069864d (diff)
downloadmupdf-96a87dc0d4df044da91b40252d85ceaa832eaa9c.tar.xz
Android app: build safe AsyncTask behaviour into a derived class
Diffstat (limited to 'android')
-rw-r--r--android/src/com/artifex/mupdf/MuPDFActivity.java7
-rw-r--r--android/src/com/artifex/mupdf/MuPDFPageAdapter.java19
-rw-r--r--android/src/com/artifex/mupdf/PageView.java12
-rw-r--r--android/src/com/artifex/mupdf/SafeAsyncTask.java21
4 files changed, 33 insertions, 26 deletions
diff --git a/android/src/com/artifex/mupdf/MuPDFActivity.java b/android/src/com/artifex/mupdf/MuPDFActivity.java
index da3c024e..bdc1971f 100644
--- a/android/src/com/artifex/mupdf/MuPDFActivity.java
+++ b/android/src/com/artifex/mupdf/MuPDFActivity.java
@@ -10,7 +10,6 @@ import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.RectF;
import android.net.Uri;
-import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
@@ -93,7 +92,7 @@ public class MuPDFActivity extends Activity
private ImageButton mSearchBack;
private ImageButton mSearchFwd;
private EditText mSearchText;
- private AsyncTask<Integer,Integer,SearchTaskResult> mSearchTask;
+ private SafeAsyncTask<Integer,Integer,SearchTaskResult> mSearchTask;
//private SearchTaskResult mSearchTaskResult;
private AlertDialog.Builder mAlertBuilder;
private LinkState mLinkState = LinkState.DEFAULT;
@@ -648,7 +647,7 @@ public class MuPDFActivity extends Activity
});
progressDialog.setMax(core.countPages());
- mSearchTask = new AsyncTask<Integer,Integer,SearchTaskResult>() {
+ mSearchTask = new SafeAsyncTask<Integer,Integer,SearchTaskResult>() {
@Override
protected SearchTaskResult doInBackground(Integer... params) {
int index;
@@ -712,6 +711,6 @@ public class MuPDFActivity extends Activity
}
};
- mSearchTask.execute(new Integer(direction));
+ mSearchTask.safeExecute(new Integer(direction));
}
}
diff --git a/android/src/com/artifex/mupdf/MuPDFPageAdapter.java b/android/src/com/artifex/mupdf/MuPDFPageAdapter.java
index 4587e244..e015ceb4 100644
--- a/android/src/com/artifex/mupdf/MuPDFPageAdapter.java
+++ b/android/src/com/artifex/mupdf/MuPDFPageAdapter.java
@@ -48,7 +48,7 @@ public class MuPDFPageAdapter extends BaseAdapter {
// Page size as yet unknown. Blank it for now, and
// start a background task to find the size
pageView.blank(position);
- AsyncTask<Void,Void,PointF> sizingTask = new AsyncTask<Void,Void,PointF>() {
+ SafeAsyncTask<Void,Void,PointF> sizingTask = new SafeAsyncTask<Void,Void,PointF>() {
@Override
protected PointF doInBackground(Void... arg0) {
return mCore.getPageSize(position);
@@ -65,21 +65,8 @@ public class MuPDFPageAdapter extends BaseAdapter {
pageView.setPage(position, result);
}
};
- try
- {
- sizingTask.execute((Void)null);
- }
- catch (java.util.concurrent.RejectedExecutionException e)
- {
- // If we can't do it in the background, just
- // do it in the foreground.
- PointF result = mCore.getPageSize(position);
- mPageSizes.put(position, result);
- // Check that this view hasn't been reused for
- // another page since we started
- if (pageView.getPage() == position)
- pageView.setPage(position, result);
- }
+
+ sizingTask.safeExecute((Void)null);
}
return pageView;
}
diff --git a/android/src/com/artifex/mupdf/PageView.java b/android/src/com/artifex/mupdf/PageView.java
index b0507182..123d5037 100644
--- a/android/src/com/artifex/mupdf/PageView.java
+++ b/android/src/com/artifex/mupdf/PageView.java
@@ -54,12 +54,12 @@ public abstract class PageView extends ViewGroup {
private ImageView mEntire; // Image rendered at minimum zoom
private Bitmap mEntireBm;
- private AsyncTask<Void,Void,LinkInfo[]> mDrawEntire;
+ private SafeAsyncTask<Void,Void,LinkInfo[]> mDrawEntire;
private Point mPatchViewSize; // View size on the basis of which the patch was created
private Rect mPatchArea;
private ImageView mPatch;
- private AsyncTask<PatchInfo,Void,PatchInfo> mDrawPatch;
+ private SafeAsyncTask<PatchInfo,Void,PatchInfo> mDrawPatch;
private RectF mSearchBoxes[];
private LinkInfo mLinks[];
private View mSearchView;
@@ -144,7 +144,7 @@ public abstract class PageView extends ViewGroup {
}
// Render the page in the background
- mDrawEntire = new AsyncTask<Void,Void,LinkInfo[]>() {
+ mDrawEntire = new SafeAsyncTask<Void,Void,LinkInfo[]>() {
protected LinkInfo[] doInBackground(Void... v) {
drawPage(mEntireBm, mSize.x, mSize.y, 0, 0, mSize.x, mSize.y);
return getLinkInfo();
@@ -177,7 +177,7 @@ public abstract class PageView extends ViewGroup {
}
};
- mDrawEntire.execute();
+ mDrawEntire.safeExecute();
if (mSearchView == null) {
mSearchView = new View(mContext) {
@@ -319,7 +319,7 @@ public abstract class PageView extends ViewGroup {
Bitmap bm = Bitmap.createBitmap(patchArea.width(), patchArea.height(), Bitmap.Config.ARGB_8888);
- mDrawPatch = new AsyncTask<PatchInfo,Void,PatchInfo>() {
+ mDrawPatch = new SafeAsyncTask<PatchInfo,Void,PatchInfo>() {
protected PatchInfo doInBackground(PatchInfo... v) {
drawPage(v[0].bm, v[0].patchViewSize.x, v[0].patchViewSize.y,
v[0].patchArea.left, v[0].patchArea.top,
@@ -339,7 +339,7 @@ public abstract class PageView extends ViewGroup {
}
};
- mDrawPatch.execute(new PatchInfo(bm, patchViewSize, patchArea));
+ mDrawPatch.safeExecute(new PatchInfo(bm, patchViewSize, patchArea));
}
}
diff --git a/android/src/com/artifex/mupdf/SafeAsyncTask.java b/android/src/com/artifex/mupdf/SafeAsyncTask.java
new file mode 100644
index 00000000..fc1aa3cf
--- /dev/null
+++ b/android/src/com/artifex/mupdf/SafeAsyncTask.java
@@ -0,0 +1,21 @@
+package com.artifex.mupdf;
+
+import android.os.AsyncTask;
+import java.util.concurrent.RejectedExecutionException;
+
+
+public abstract class SafeAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
+ public void safeExecute(Params... params) {
+ try {
+ execute(params);
+ } catch(RejectedExecutionException e) {
+ // Failed to start in the background, so do it in the foreground
+ onPreExecute();
+ if (isCancelled()) {
+ onCancelled();
+ } else {
+ onPostExecute(doInBackground(params));
+ }
+ }
+ }
+}