Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [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.content; |
| 18 | |
| 19 | import android.os.AsyncTask; |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.os.SystemClock; |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 22 | import android.util.Slog; |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 23 | import android.util.TimeUtils; |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 24 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 25 | import java.io.FileDescriptor; |
| 26 | import java.io.PrintWriter; |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 27 | import java.util.concurrent.CountDownLatch; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 28 | |
| 29 | /** |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 30 | * Abstract Loader that provides an {@link AsyncTask} to do the work. See |
| 31 | * {@link Loader} and {@link android.app.LoaderManager} for more details. |
| 32 | * |
| 33 | * <p>Here is an example implementation of an AsyncTaskLoader subclass that |
| 34 | * loads the currently installed applications from the package manager. This |
| 35 | * implementation takes care of retrieving the application labels and sorting |
| 36 | * its result set from them, monitoring for changes to the installed |
| 37 | * applications, and rebuilding the list when a change in configuration requires |
| 38 | * this (such as a locale change). |
| 39 | * |
| 40 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LoaderCustom.java |
| 41 | * loader} |
| 42 | * |
| 43 | * <p>An example implementation of a fragment that uses the above loader to show |
| 44 | * the currently installed applications in a list is below. |
| 45 | * |
| 46 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LoaderCustom.java |
| 47 | * fragment} |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 48 | * |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 49 | * @param <D> the data type to be loaded. |
| 50 | */ |
| 51 | public abstract class AsyncTaskLoader<D> extends Loader<D> { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 52 | static final String TAG = "AsyncTaskLoader"; |
| 53 | static final boolean DEBUG = false; |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 54 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 55 | final class LoadTask extends AsyncTask<Void, Void, D> implements Runnable { |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 56 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 57 | D result; |
| 58 | boolean waiting; |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 59 | |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 60 | private CountDownLatch done = new CountDownLatch(1); |
| 61 | |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 62 | /* Runs on a worker thread */ |
| 63 | @Override |
| 64 | protected D doInBackground(Void... params) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 65 | if (DEBUG) Slog.v(TAG, this + " >>> doInBackground"); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 66 | result = AsyncTaskLoader.this.onLoadInBackground(); |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 67 | if (DEBUG) Slog.v(TAG, this + " <<< doInBackground"); |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 68 | return result; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* Runs on the UI thread */ |
| 72 | @Override |
| 73 | protected void onPostExecute(D data) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 74 | if (DEBUG) Slog.v(TAG, this + " onPostExecute"); |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 75 | try { |
| 76 | AsyncTaskLoader.this.dispatchOnLoadComplete(this, data); |
| 77 | } finally { |
| 78 | done.countDown(); |
| 79 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 80 | } |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | protected void onCancelled() { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 84 | if (DEBUG) Slog.v(TAG, this + " onCancelled"); |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 85 | try { |
| 86 | AsyncTaskLoader.this.dispatchOnCancelled(this, result); |
| 87 | } finally { |
| 88 | done.countDown(); |
| 89 | } |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void run() { |
| 94 | waiting = false; |
| 95 | AsyncTaskLoader.this.executePendingTask(); |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 96 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 99 | volatile LoadTask mTask; |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 100 | volatile LoadTask mCancellingTask; |
| 101 | |
| 102 | long mUpdateThrottle; |
| 103 | long mLastLoadCompleteTime = -10000; |
| 104 | Handler mHandler; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 105 | |
| 106 | public AsyncTaskLoader(Context context) { |
| 107 | super(context); |
| 108 | } |
| 109 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 110 | /** |
| 111 | * Set amount to throttle updates by. This is the minimum time from |
| 112 | * when the last {@link #onLoadInBackground()} call has completed until |
| 113 | * a new load is scheduled. |
| 114 | * |
| 115 | * @param delayMS Amount of delay, in milliseconds. |
| 116 | */ |
| 117 | public void setUpdateThrottle(long delayMS) { |
| 118 | mUpdateThrottle = delayMS; |
| 119 | if (delayMS != 0) { |
| 120 | mHandler = new Handler(); |
| 121 | } |
| 122 | } |
| 123 | |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 124 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 125 | protected void onForceLoad() { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 126 | super.onForceLoad(); |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 127 | cancelLoad(); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 128 | mTask = new LoadTask(); |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 129 | if (DEBUG) Slog.v(TAG, "Preparing load: mTask=" + mTask); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 130 | executePendingTask(); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Attempt to cancel the current load task. See {@link AsyncTask#cancel(boolean)} |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 135 | * for more info. Must be called on the main thread of the process. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 136 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 137 | * <p>Cancelling is not an immediate operation, since the load is performed |
| 138 | * in a background thread. If there is currently a load in progress, this |
| 139 | * method requests that the load be cancelled, and notes this is the case; |
| 140 | * once the background thread has completed its work its remaining state |
| 141 | * will be cleared. If another load request comes in during this time, |
| 142 | * it will be held until the cancelled load is complete. |
| 143 | * |
| 144 | * @return Returns <tt>false</tt> if the task could not be cancelled, |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 145 | * typically because it has already completed normally, or |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 146 | * because {@link #startLoading()} hasn't been called; returns |
| 147 | * <tt>true</tt> otherwise. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 148 | */ |
| 149 | public boolean cancelLoad() { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 150 | if (DEBUG) Slog.v(TAG, "cancelLoad: mTask=" + mTask); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 151 | if (mTask != null) { |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 152 | if (mCancellingTask != null) { |
| 153 | // There was a pending task already waiting for a previous |
| 154 | // one being canceled; just drop it. |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 155 | if (DEBUG) Slog.v(TAG, |
| 156 | "cancelLoad: still waiting for cancelled task; dropping next"); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 157 | if (mTask.waiting) { |
| 158 | mTask.waiting = false; |
| 159 | mHandler.removeCallbacks(mTask); |
| 160 | } |
| 161 | mTask = null; |
| 162 | return false; |
| 163 | } else if (mTask.waiting) { |
| 164 | // There is a task, but it is waiting for the time it should |
| 165 | // execute. We can just toss it. |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 166 | if (DEBUG) Slog.v(TAG, "cancelLoad: task is waiting, dropping it"); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 167 | mTask.waiting = false; |
| 168 | mHandler.removeCallbacks(mTask); |
| 169 | mTask = null; |
| 170 | return false; |
| 171 | } else { |
| 172 | boolean cancelled = mTask.cancel(false); |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 173 | if (DEBUG) Slog.v(TAG, "cancelLoad: cancelled=" + cancelled); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 174 | if (cancelled) { |
| 175 | mCancellingTask = mTask; |
| 176 | } |
| 177 | mTask = null; |
| 178 | return cancelled; |
| 179 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 180 | } |
| 181 | return false; |
| 182 | } |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Called if the task was canceled before it was completed. Gives the class a chance |
| 186 | * to properly dispose of the result. |
| 187 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 188 | public void onCanceled(D data) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 189 | } |
Dmitri Plotnikov | 4afde4f | 2011-01-18 09:41:29 -0800 | [diff] [blame] | 190 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 191 | void executePendingTask() { |
| 192 | if (mCancellingTask == null && mTask != null) { |
| 193 | if (mTask.waiting) { |
| 194 | mTask.waiting = false; |
| 195 | mHandler.removeCallbacks(mTask); |
| 196 | } |
| 197 | if (mUpdateThrottle > 0) { |
| 198 | long now = SystemClock.uptimeMillis(); |
| 199 | if (now < (mLastLoadCompleteTime+mUpdateThrottle)) { |
| 200 | // Not yet time to do another load. |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 201 | if (DEBUG) Slog.v(TAG, "Waiting until " |
| 202 | + (mLastLoadCompleteTime+mUpdateThrottle) |
| 203 | + " to execute: " + mTask); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 204 | mTask.waiting = true; |
| 205 | mHandler.postAtTime(mTask, mLastLoadCompleteTime+mUpdateThrottle); |
| 206 | return; |
| 207 | } |
| 208 | } |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 209 | if (DEBUG) Slog.v(TAG, "Executing: " + mTask); |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 210 | mTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | void dispatchOnCancelled(LoadTask task, D data) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 215 | onCanceled(data); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 216 | if (mCancellingTask == task) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 217 | if (DEBUG) Slog.v(TAG, "Cancelled task is now canceled!"); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 218 | mLastLoadCompleteTime = SystemClock.uptimeMillis(); |
| 219 | mCancellingTask = null; |
| 220 | executePendingTask(); |
| 221 | } |
| 222 | } |
| 223 | |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 224 | void dispatchOnLoadComplete(LoadTask task, D data) { |
| 225 | if (mTask != task) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 226 | if (DEBUG) Slog.v(TAG, "Load complete of old task, trying to cancel"); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 227 | dispatchOnCancelled(task, data); |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 228 | } else { |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 229 | if (isAbandoned()) { |
| 230 | // This cursor has been abandoned; just cancel the new data. |
| 231 | onCanceled(data); |
| 232 | } else { |
| 233 | mLastLoadCompleteTime = SystemClock.uptimeMillis(); |
| 234 | mTask = null; |
| 235 | if (DEBUG) Slog.v(TAG, "Delivering result"); |
| 236 | deliverResult(data); |
| 237 | } |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 238 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 242 | */ |
| 243 | public abstract D loadInBackground(); |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 244 | |
| 245 | /** |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 246 | * Called on a worker thread to perform the actual load. Implementations should not deliver the |
| 247 | * result directly, but should return them from this method, which will eventually end up |
| 248 | * calling {@link #deliverResult} on the UI thread. If implementations need to process |
| 249 | * the results on the UI thread they may override {@link #deliverResult} and do so |
| 250 | * there. |
| 251 | * |
| 252 | * @return Implementations must return the result of their load operation. |
| 253 | */ |
| 254 | protected D onLoadInBackground() { |
| 255 | return loadInBackground(); |
| 256 | } |
| 257 | |
| 258 | /** |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 259 | * Locks the current thread until the loader completes the current load |
| 260 | * operation. Returns immediately if there is no load operation running. |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 261 | * Should not be called from the UI thread: calling it from the UI |
| 262 | * thread would cause a deadlock. |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 263 | * <p> |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 264 | * Use for testing only. <b>Never</b> call this from a UI thread. |
Dmitri Plotnikov | 4afde4f | 2011-01-18 09:41:29 -0800 | [diff] [blame] | 265 | * |
| 266 | * @hide |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 267 | */ |
| 268 | public void waitForLoader() { |
| 269 | LoadTask task = mTask; |
| 270 | if (task != null) { |
| 271 | try { |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 272 | task.done.await(); |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 273 | } catch (InterruptedException e) { |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 274 | // Ignore |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 278 | |
| 279 | @Override |
| 280 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 281 | super.dump(prefix, fd, writer, args); |
| 282 | if (mTask != null) { |
| 283 | writer.print(prefix); writer.print("mTask="); writer.print(mTask); |
| 284 | writer.print(" waiting="); writer.println(mTask.waiting); |
| 285 | } |
| 286 | if (mCancellingTask != null) { |
| 287 | writer.print(prefix); writer.print("mCancellingTask="); writer.print(mCancellingTask); |
| 288 | writer.print(" waiting="); writer.println(mCancellingTask.waiting); |
| 289 | } |
| 290 | if (mUpdateThrottle != 0) { |
| 291 | writer.print(prefix); writer.print("mUpdateThrottle="); |
| 292 | TimeUtils.formatDuration(mUpdateThrottle, writer); |
| 293 | writer.print(" mLastLoadCompleteTime="); |
| 294 | TimeUtils.formatDuration(mLastLoadCompleteTime, |
| 295 | SystemClock.uptimeMillis(), writer); |
| 296 | writer.println(); |
| 297 | } |
| 298 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 299 | } |