blob: 84effaf309488dc7430e72f8fcd18d3ec4a54333 [file] [log] [blame]
Daniel Lehmannc2687c32010-04-19 18:20:44 -07001/*
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.app.patterns;
18
19import android.content.Context;
20import android.os.AsyncTask;
21
22/**
23 * Abstract Loader that provides an {@link AsyncTask} to do the work.
Dmitri Plotnikov86053952010-04-29 10:22:01 -070024 *
Daniel Lehmannc2687c32010-04-19 18:20:44 -070025 * @param <D> the data type to be loaded.
26 */
27public abstract class AsyncTaskLoader<D> extends Loader<D> {
28 final class LoadListTask extends AsyncTask<Void, Void, D> {
29 /* Runs on a worker thread */
30 @Override
31 protected D doInBackground(Void... params) {
32 return AsyncTaskLoader.this.loadInBackground();
33 }
34
35 /* Runs on the UI thread */
36 @Override
37 protected void onPostExecute(D data) {
Dmitri Plotnikov86053952010-04-29 10:22:01 -070038 AsyncTaskLoader.this.dispatchOnLoadComplete(data);
Daniel Lehmannc2687c32010-04-19 18:20:44 -070039 }
40 }
41
Dmitri Plotnikov86053952010-04-29 10:22:01 -070042 private LoadListTask mTask;
43
Daniel Lehmannc2687c32010-04-19 18:20:44 -070044 public AsyncTaskLoader(Context context) {
45 super(context);
46 }
47
48 /**
Dmitri Plotnikov86053952010-04-29 10:22:01 -070049 * Force an asynchronous load. Unlike {@link #startLoading()} this will ignore a previously
50 * loaded data set and load a new one.
51 */
52 @Override
53 public void forceLoad() {
54 mTask = new LoadListTask();
55 mTask.execute((Void[]) null);
56 }
57
58 /**
59 * Attempt to cancel the current load task. See {@link AsyncTask#cancel(boolean)}
60 * for more info.
61 *
62 * @return <tt>false</tt> if the task could not be cancelled,
63 * typically because it has already completed normally, or
64 * because {@link startLoading()} hasn't been called, and
65 * <tt>true</tt> otherwise
66 */
67 public boolean cancelLoad() {
68 if (mTask != null) {
69 return mTask.cancel(false);
70 }
71 return false;
72 }
73
74 private void dispatchOnLoadComplete(D data) {
75 mTask = null;
76 onLoadComplete(data);
77 }
78
79 /**
Daniel Lehmannc2687c32010-04-19 18:20:44 -070080 * Called on a worker thread to perform the actual load. Implementions should not deliver the
81 * results directly, but should return them from this this method and deliver them from
82 * {@link #onPostExecute()}
83 *
84 * @return the result of the load
85 */
86 protected abstract D loadInBackground();
87
88 /**
89 * Called on the UI thread with the result of the load.
90 *
91 * @param data the result of the load
92 */
93 protected abstract void onLoadComplete(D data);
94}