mobile/android/base/util/UiAsyncTask.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/util/UiAsyncTask.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.util;
    1.10 +
    1.11 +import android.os.Handler;
    1.12 +import android.os.Looper;
    1.13 +
    1.14 +/**
    1.15 + * Executes a background task and publishes the result on the UI thread.
    1.16 + *
    1.17 + * The standard {@link android.os.AsyncTask} only runs onPostExecute on the
    1.18 + * thread it is constructed on, so this is a convenience class for creating
    1.19 + * tasks off the UI thread.
    1.20 + */
    1.21 +public abstract class UiAsyncTask<Params, Progress, Result> {
    1.22 +    private volatile boolean mCancelled = false;
    1.23 +    private final Handler mBackgroundThreadHandler;
    1.24 +    private static Handler sHandler;
    1.25 +
    1.26 +    /**
    1.27 +     * Creates a new asynchronous task.
    1.28 +     *
    1.29 +     * @param backgroundThreadHandler the handler to execute the background task on
    1.30 +     */
    1.31 +    public UiAsyncTask(Handler backgroundThreadHandler) {
    1.32 +        mBackgroundThreadHandler = backgroundThreadHandler;
    1.33 +    }
    1.34 +
    1.35 +    private static synchronized Handler getUiHandler() {
    1.36 +        if (sHandler == null) {
    1.37 +            sHandler = new Handler(Looper.getMainLooper());
    1.38 +        }
    1.39 +        return sHandler;
    1.40 +    }
    1.41 +
    1.42 +    private final class BackgroundTaskRunnable implements Runnable {
    1.43 +        private Params[] mParams;
    1.44 +
    1.45 +        public BackgroundTaskRunnable(Params... params) {
    1.46 +            mParams = params;
    1.47 +        }
    1.48 +
    1.49 +        @Override
    1.50 +        public void run() {
    1.51 +            final Result result = doInBackground(mParams);
    1.52 +
    1.53 +            getUiHandler().post(new Runnable() {
    1.54 +                @Override
    1.55 +                public void run() {
    1.56 +                    if (mCancelled)
    1.57 +                        onCancelled();
    1.58 +                    else
    1.59 +                        onPostExecute(result);
    1.60 +                }
    1.61 +            });
    1.62 +        }
    1.63 +    }
    1.64 +
    1.65 +    public final void execute(final Params... params) {
    1.66 +        getUiHandler().post(new Runnable() {
    1.67 +            @Override
    1.68 +            public void run() {
    1.69 +                onPreExecute();
    1.70 +                mBackgroundThreadHandler.post(new BackgroundTaskRunnable(params));
    1.71 +            }
    1.72 +        });
    1.73 +    }
    1.74 +
    1.75 +    @SuppressWarnings({"UnusedParameters"})
    1.76 +    public final boolean cancel(boolean mayInterruptIfRunning) {
    1.77 +        mCancelled = true;
    1.78 +        return mCancelled;
    1.79 +    }
    1.80 +
    1.81 +    public final boolean isCancelled() {
    1.82 +        return mCancelled;
    1.83 +    }
    1.84 +
    1.85 +    protected void onPreExecute() { }
    1.86 +    protected void onPostExecute(Result result) { }
    1.87 +    protected void onCancelled() { }
    1.88 +    protected abstract Result doInBackground(Params... params);
    1.89 +}

mercurial