michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.util; michael@0: michael@0: import android.os.Handler; michael@0: import android.os.Looper; michael@0: michael@0: /** michael@0: * Executes a background task and publishes the result on the UI thread. michael@0: * michael@0: * The standard {@link android.os.AsyncTask} only runs onPostExecute on the michael@0: * thread it is constructed on, so this is a convenience class for creating michael@0: * tasks off the UI thread. michael@0: */ michael@0: public abstract class UiAsyncTask { michael@0: private volatile boolean mCancelled = false; michael@0: private final Handler mBackgroundThreadHandler; michael@0: private static Handler sHandler; michael@0: michael@0: /** michael@0: * Creates a new asynchronous task. michael@0: * michael@0: * @param backgroundThreadHandler the handler to execute the background task on michael@0: */ michael@0: public UiAsyncTask(Handler backgroundThreadHandler) { michael@0: mBackgroundThreadHandler = backgroundThreadHandler; michael@0: } michael@0: michael@0: private static synchronized Handler getUiHandler() { michael@0: if (sHandler == null) { michael@0: sHandler = new Handler(Looper.getMainLooper()); michael@0: } michael@0: return sHandler; michael@0: } michael@0: michael@0: private final class BackgroundTaskRunnable implements Runnable { michael@0: private Params[] mParams; michael@0: michael@0: public BackgroundTaskRunnable(Params... params) { michael@0: mParams = params; michael@0: } michael@0: michael@0: @Override michael@0: public void run() { michael@0: final Result result = doInBackground(mParams); michael@0: michael@0: getUiHandler().post(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: if (mCancelled) michael@0: onCancelled(); michael@0: else michael@0: onPostExecute(result); michael@0: } michael@0: }); michael@0: } michael@0: } michael@0: michael@0: public final void execute(final Params... params) { michael@0: getUiHandler().post(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: onPreExecute(); michael@0: mBackgroundThreadHandler.post(new BackgroundTaskRunnable(params)); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: @SuppressWarnings({"UnusedParameters"}) michael@0: public final boolean cancel(boolean mayInterruptIfRunning) { michael@0: mCancelled = true; michael@0: return mCancelled; michael@0: } michael@0: michael@0: public final boolean isCancelled() { michael@0: return mCancelled; michael@0: } michael@0: michael@0: protected void onPreExecute() { } michael@0: protected void onPostExecute(Result result) { } michael@0: protected void onCancelled() { } michael@0: protected abstract Result doInBackground(Params... params); michael@0: }