AsyncTaskLoader.java revision a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8
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
29        private D result;
30
31        /* Runs on a worker thread */
32        @Override
33        protected D doInBackground(Void... params) {
34            result = AsyncTaskLoader.this.loadInBackground();
35            return result;
36        }
37
38        /* Runs on the UI thread */
39        @Override
40        protected void onPostExecute(D data) {
41            AsyncTaskLoader.this.dispatchOnLoadComplete(data);
42        }
43
44        @Override
45        protected void onCancelled() {
46            AsyncTaskLoader.this.onCancelled(result);
47        }
48    }
49
50    LoadTask mTask;
51
52    public AsyncTaskLoader(Context context) {
53        super(context);
54    }
55
56    @Override
57    protected void onForceLoad() {
58        cancelLoad();
59        mTask = new LoadTask();
60        mTask.execute((Void[]) null);
61    }
62
63    /**
64     * Attempt to cancel the current load task. See {@link AsyncTask#cancel(boolean)}
65     * for more info.
66     *
67     * @return <tt>false</tt> if the task could not be canceled,
68     *         typically because it has already completed normally, or
69     *         because {@link #startLoading()} hasn't been called, and
70     *         <tt>true</tt> otherwise
71     */
72    public boolean cancelLoad() {
73        if (mTask != null) {
74            boolean cancelled = mTask.cancel(false);
75            mTask = null;
76            return cancelled;
77        }
78        return false;
79    }
80
81    /**
82     * Called if the task was canceled before it was completed.  Gives the class a chance
83     * to properly dispose of the result.
84     */
85    public void onCancelled(D data) {
86    }
87
88    void dispatchOnLoadComplete(D data) {
89        mTask = null;
90        deliverResult(data);
91    }
92
93    /**
94     * Called on a worker thread to perform the actual load. Implementations should not deliver the
95     * results directly, but should return them from this method, which will eventually end up
96     * calling deliverResult on the UI thread. If implementations need to process
97     * the results on the UI thread they may override deliverResult and do so
98     * there.
99     *
100     * @return the result of the load
101     */
102    public abstract D loadInBackground();
103}
104