Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.util; |
michael@0 | 7 | |
michael@0 | 8 | import android.os.Handler; |
michael@0 | 9 | import android.os.Looper; |
michael@0 | 10 | |
michael@0 | 11 | /** |
michael@0 | 12 | * Executes a background task and publishes the result on the UI thread. |
michael@0 | 13 | * |
michael@0 | 14 | * The standard {@link android.os.AsyncTask} only runs onPostExecute on the |
michael@0 | 15 | * thread it is constructed on, so this is a convenience class for creating |
michael@0 | 16 | * tasks off the UI thread. |
michael@0 | 17 | */ |
michael@0 | 18 | public abstract class UiAsyncTask<Params, Progress, Result> { |
michael@0 | 19 | private volatile boolean mCancelled = false; |
michael@0 | 20 | private final Handler mBackgroundThreadHandler; |
michael@0 | 21 | private static Handler sHandler; |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * Creates a new asynchronous task. |
michael@0 | 25 | * |
michael@0 | 26 | * @param backgroundThreadHandler the handler to execute the background task on |
michael@0 | 27 | */ |
michael@0 | 28 | public UiAsyncTask(Handler backgroundThreadHandler) { |
michael@0 | 29 | mBackgroundThreadHandler = backgroundThreadHandler; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | private static synchronized Handler getUiHandler() { |
michael@0 | 33 | if (sHandler == null) { |
michael@0 | 34 | sHandler = new Handler(Looper.getMainLooper()); |
michael@0 | 35 | } |
michael@0 | 36 | return sHandler; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | private final class BackgroundTaskRunnable implements Runnable { |
michael@0 | 40 | private Params[] mParams; |
michael@0 | 41 | |
michael@0 | 42 | public BackgroundTaskRunnable(Params... params) { |
michael@0 | 43 | mParams = params; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | @Override |
michael@0 | 47 | public void run() { |
michael@0 | 48 | final Result result = doInBackground(mParams); |
michael@0 | 49 | |
michael@0 | 50 | getUiHandler().post(new Runnable() { |
michael@0 | 51 | @Override |
michael@0 | 52 | public void run() { |
michael@0 | 53 | if (mCancelled) |
michael@0 | 54 | onCancelled(); |
michael@0 | 55 | else |
michael@0 | 56 | onPostExecute(result); |
michael@0 | 57 | } |
michael@0 | 58 | }); |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | public final void execute(final Params... params) { |
michael@0 | 63 | getUiHandler().post(new Runnable() { |
michael@0 | 64 | @Override |
michael@0 | 65 | public void run() { |
michael@0 | 66 | onPreExecute(); |
michael@0 | 67 | mBackgroundThreadHandler.post(new BackgroundTaskRunnable(params)); |
michael@0 | 68 | } |
michael@0 | 69 | }); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | @SuppressWarnings({"UnusedParameters"}) |
michael@0 | 73 | public final boolean cancel(boolean mayInterruptIfRunning) { |
michael@0 | 74 | mCancelled = true; |
michael@0 | 75 | return mCancelled; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | public final boolean isCancelled() { |
michael@0 | 79 | return mCancelled; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | protected void onPreExecute() { } |
michael@0 | 83 | protected void onPostExecute(Result result) { } |
michael@0 | 84 | protected void onCancelled() { } |
michael@0 | 85 | protected abstract Result doInBackground(Params... params); |
michael@0 | 86 | } |