diff options
author | Matt Holgate <matt@emobix.co.uk> | 2014-06-20 10:21:40 +0100 |
---|---|---|
committer | Matt Holgate <matt@emobix.co.uk> | 2014-06-20 10:21:40 +0100 |
commit | d7ece4132d6219ee10ba9ed85a9f2a052a6bb92c (patch) | |
tree | 47b751a13321ad9d7fe4fa8261b41ff29587a353 /platform/android/src/com/artifex/mupdfdemo/CancellableAsyncTask.java | |
parent | 7ba8c60c5af8ff745336c50c7504bec4f9b22a76 (diff) | |
download | mupdf-d7ece4132d6219ee10ba9ed85a9f2a052a6bb92c.tar.xz |
Improvement which should hopefully help with bug #693607 - MupdfActivity crash when rotating the device.
When cancelling a render async task, we now wait for it to actually finish
before continuing. The benefit of this is that we should be able to guarantee
that its Bitmap becomes eligible for GC before we continue to create any
new bitmaps.
This should hopefully help with the OOM errors seen when rotating
the device and trying to create the new bitmaps.
To prevent the UI thread from being blocked for too long while we're waiting
for the async task to finish, we use a fz_cookie and set the 'abort' flag to
request the render be stopped as soon as possible.
Diffstat (limited to 'platform/android/src/com/artifex/mupdfdemo/CancellableAsyncTask.java')
-rw-r--r-- | platform/android/src/com/artifex/mupdfdemo/CancellableAsyncTask.java | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/platform/android/src/com/artifex/mupdfdemo/CancellableAsyncTask.java b/platform/android/src/com/artifex/mupdfdemo/CancellableAsyncTask.java new file mode 100644 index 00000000..fcb1b744 --- /dev/null +++ b/platform/android/src/com/artifex/mupdfdemo/CancellableAsyncTask.java @@ -0,0 +1,79 @@ +package com.artifex.mupdfdemo; + +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; + +// Ideally this would be a subclass of AsyncTask, however the cancel() method is final, and cannot +// be overridden. I felt that having two different, but similar cancel methods was a bad idea. +public class CancellableAsyncTask<Params, Result> +{ + private final AsyncTask<Params, Void, Result> asyncTask; + private final CancellableTaskDefinition<Params, Result> ourTask; + + public void onPreExecute() + { + + } + + public void onPostExecute(Result result) + { + + } + + public CancellableAsyncTask(final CancellableTaskDefinition<Params, Result> task) + { + if (task == null) + throw new IllegalArgumentException(); + + this.ourTask = task; + asyncTask = new AsyncTask<Params, Void, Result>() + { + @Override + protected Result doInBackground(Params... params) + { + return task.doInBackground(params); + } + + @Override + protected void onPreExecute() + { + CancellableAsyncTask.this.onPreExecute(); + } + + @Override + protected void onPostExecute(Result result) + { + CancellableAsyncTask.this.onPostExecute(result); + task.doCleanup(); + } + }; + } + + public void cancelAndWait() + { + this.asyncTask.cancel(true); + ourTask.doCancel(); + + try + { + this.asyncTask.get(); + } + catch (InterruptedException e) + { + } + catch (ExecutionException e) + { + } + catch (CancellationException e) + { + } + + ourTask.doCleanup(); + } + + public void execute(Params ... params) + { + asyncTask.execute(params); + } + +} |