AsyncTaskLoader.java revision e6c6d09a7eb9a7cd6e94c164e75a08dc499fd577
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content;
18
19import android.os.AsyncTask;
20
21/**
22 * Abstract Loader that provides an {@link AsyncTask} to do the work.
23 *
24 * @param <D> the data type to be loaded.
25 */
26public abstract class AsyncTaskLoader<D> extends Loader<D> {
27    final class LoadTask extends AsyncTask<Void, Void, D> {
28        /* Runs on a worker thread */
29        @Override
30        protected D doInBackground(Void... params) {
31            return AsyncTaskLoader.this.loadInBackground();
32        }
33
34        /* Runs on the UI thread */
35        @Override
36        protected void onPostExecute(D data) {
37            AsyncTaskLoader.this.dispatchOnLoadComplete(data);
38        }
39    }
40
41    LoadTask mTask;
42
43    public AsyncTaskLoader(Context context) {
44        super(context);
45    }
46
47    /**
48     * Force an asynchronous load. Unlike {@link #startLoading()} this will ignore a previously
49     * loaded data set and load a new one.
50     */
51    @Override
52    public void forceLoad() {
53        mTask = new LoadTask();
54        mTask.execute((Void[]) null);
55    }
56
57    /**
58     * Attempt to cancel the current load task. See {@link AsyncTask#cancel(boolean)}
59     * for more info.
60     *
61     * @return <tt>false</tt> if the task could not be canceled,
62     *         typically because it has already completed normally, or
63     *         because {@link #startLoading()} hasn't been called, and
64     *         <tt>true</tt> otherwise
65     */
66    public boolean cancelLoad() {
67        if (mTask != null) {
68            return mTask.cancel(false);
69        }
70        return false;
71    }
72
73    void dispatchOnLoadComplete(D data) {
74        mTask = null;
75        deliverResult(data);
76    }
77
78    /**
79     * Called on a worker thread to perform the actual load. Implementations should not deliver the
80     * results directly, but should return them from this method, which will eventually end up
81     * calling {@link #deliverResult(Object)} on the UI thread. If implementations need to process
82     * the results on the UI thread they may override {@link #deliverResult(Object)} and do so
83     * there.
84     *
85     * @return the result of the load
86     */
87    public abstract D loadInBackground();
88}
89