diff options
| author | Jeff Hamilton <jham@android.com> | 2010-05-15 02:20:31 -0500 |
|---|---|---|
| committer | Jeff Hamilton <jham@android.com> | 2010-05-18 00:07:15 -0500 |
| commit | 9911b7f83db2e960f72345e6d50df2b77ca75e3f (patch) | |
| tree | a9f807ac96c7cea9038ef6b251f78c7c1ce4755f /core/java/android/content/AsyncTaskLoader.java | |
| parent | 3c19d99042ee6ebbfda23cdfb65e3cc7fda72031 (diff) | |
Add the Loader and supporting classes.
Loaders are designed to make it easier to manage
asynchronously loading data.
Change-Id: I948db08c721411e94fca071dc6fb4db2b83ea4d6
Diffstat (limited to 'core/java/android/content/AsyncTaskLoader.java')
| -rw-r--r-- | core/java/android/content/AsyncTaskLoader.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/core/java/android/content/AsyncTaskLoader.java b/core/java/android/content/AsyncTaskLoader.java new file mode 100644 index 000000000000..52a1b352f378 --- /dev/null +++ b/core/java/android/content/AsyncTaskLoader.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.content; + +import android.os.AsyncTask; + +/** + * Abstract Loader that provides an {@link AsyncTask} to do the work. + * + * @param <D> the data type to be loaded. + */ +public abstract class AsyncTaskLoader<D> extends Loader<D> { + final class LoadTask extends AsyncTask<Void, Void, D> { + /* Runs on a worker thread */ + @Override + protected D doInBackground(Void... params) { + return AsyncTaskLoader.this.loadInBackground(); + } + + /* Runs on the UI thread */ + @Override + protected void onPostExecute(D data) { + AsyncTaskLoader.this.dispatchOnLoadComplete(data); + } + } + + LoadTask mTask; + + public AsyncTaskLoader(Context context) { + super(context); + } + + /** + * Force an asynchronous load. Unlike {@link #startLoading()} this will ignore a previously + * loaded data set and load a new one. + */ + @Override + public void forceLoad() { + mTask = new LoadTask(); + mTask.execute((Void[]) null); + } + + /** + * Attempt to cancel the current load task. See {@link AsyncTask#cancel(boolean)} + * for more info. + * + * @return <tt>false</tt> if the task could not be canceled, + * typically because it has already completed normally, or + * because {@link #startLoading()} hasn't been called, and + * <tt>true</tt> otherwise + */ + public boolean cancelLoad() { + if (mTask != null) { + return mTask.cancel(false); + } + return false; + } + + void dispatchOnLoadComplete(D data) { + mTask = null; + deliverResult(data); + } + + /** + * Called on a worker thread to perform the actual load. Implementations should not deliver the + * results directly, but should return them from this method and deliver them from + * {@link #onPostExecute()} + * + * @return the result of the load + */ + public abstract D loadInBackground(); +} |
