Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 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 | |
| 17 | package android.app.patterns; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.AsyncTask; |
| 21 | |
| 22 | /** |
| 23 | * Abstract Loader that provides an {@link AsyncTask} to do the work. |
| 24 | * |
| 25 | * @param <D> the data type to be loaded. |
| 26 | */ |
| 27 | public 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) { |
| 38 | AsyncTaskLoader.this.onLoadComplete(data); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public AsyncTaskLoader(Context context) { |
| 43 | super(context); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Called on a worker thread to perform the actual load. Implementions should not deliver the |
| 48 | * results directly, but should return them from this this method and deliver them from |
| 49 | * {@link #onPostExecute()} |
| 50 | * |
| 51 | * @return the result of the load |
| 52 | */ |
| 53 | protected abstract D loadInBackground(); |
| 54 | |
| 55 | /** |
| 56 | * Called on the UI thread with the result of the load. |
| 57 | * |
| 58 | * @param data the result of the load |
| 59 | */ |
| 60 | protected abstract void onLoadComplete(D data); |
| 61 | } |