From 96a87dc0d4df044da91b40252d85ceaa832eaa9c Mon Sep 17 00:00:00 2001 From: Paul Gardiner Date: Wed, 25 Apr 2012 11:56:45 +0100 Subject: Android app: build safe AsyncTask behaviour into a derived class --- android/src/com/artifex/mupdf/MuPDFActivity.java | 7 +++---- android/src/com/artifex/mupdf/MuPDFPageAdapter.java | 19 +++---------------- android/src/com/artifex/mupdf/PageView.java | 12 ++++++------ android/src/com/artifex/mupdf/SafeAsyncTask.java | 21 +++++++++++++++++++++ 4 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 android/src/com/artifex/mupdf/SafeAsyncTask.java (limited to 'android') 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 mSearchTask; + private SafeAsyncTask 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() { + mSearchTask = new SafeAsyncTask() { @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 sizingTask = new AsyncTask() { + SafeAsyncTask sizingTask = new SafeAsyncTask() { @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 mDrawEntire; + private SafeAsyncTask mDrawEntire; private Point mPatchViewSize; // View size on the basis of which the patch was created private Rect mPatchArea; private ImageView mPatch; - private AsyncTask mDrawPatch; + private SafeAsyncTask 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() { + mDrawEntire = new SafeAsyncTask() { 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() { + mDrawPatch = new SafeAsyncTask() { 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 extends AsyncTask { + 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)); + } + } + } +} -- cgit v1.2.3