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; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 21 | import android.os.OperationCanceledException; |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 22 | import android.os.SystemClock; |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 23 | import android.util.Slog; |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 24 | import android.util.TimeUtils; |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 25 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 26 | import java.io.FileDescriptor; |
| 27 | import java.io.PrintWriter; |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 28 | import java.util.concurrent.CountDownLatch; |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 29 | import java.util.concurrent.Executor; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 30 | |
| 31 | /** |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 32 | * Abstract Loader that provides an {@link AsyncTask} to do the work. See |
| 33 | * {@link Loader} and {@link android.app.LoaderManager} for more details. |
| 34 | * |
| 35 | * <p>Here is an example implementation of an AsyncTaskLoader subclass that |
| 36 | * loads the currently installed applications from the package manager. This |
| 37 | * implementation takes care of retrieving the application labels and sorting |
| 38 | * its result set from them, monitoring for changes to the installed |
| 39 | * applications, and rebuilding the list when a change in configuration requires |
| 40 | * this (such as a locale change). |
| 41 | * |
| 42 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LoaderCustom.java |
| 43 | * loader} |
| 44 | * |
| 45 | * <p>An example implementation of a fragment that uses the above loader to show |
| 46 | * the currently installed applications in a list is below. |
| 47 | * |
| 48 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LoaderCustom.java |
| 49 | * fragment} |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 50 | * |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 51 | * @param <D> the data type to be loaded. |
| 52 | */ |
| 53 | public abstract class AsyncTaskLoader<D> extends Loader<D> { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 54 | static final String TAG = "AsyncTaskLoader"; |
| 55 | static final boolean DEBUG = false; |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 56 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 57 | final class LoadTask extends AsyncTask<Void, Void, D> implements Runnable { |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 58 | private final CountDownLatch mDone = new CountDownLatch(1); |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 59 | |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 60 | // Set to true to indicate that the task has been posted to a handler for |
| 61 | // execution at a later time. Used to throttle updates. |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 62 | boolean waiting; |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 63 | |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 64 | /* Runs on a worker thread */ |
| 65 | @Override |
| 66 | protected D doInBackground(Void... params) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 67 | if (DEBUG) Slog.v(TAG, this + " >>> doInBackground"); |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 68 | try { |
| 69 | D data = AsyncTaskLoader.this.onLoadInBackground(); |
| 70 | if (DEBUG) Slog.v(TAG, this + " <<< doInBackground"); |
| 71 | return data; |
| 72 | } catch (OperationCanceledException ex) { |
| 73 | if (!isCancelled()) { |
| 74 | // onLoadInBackground threw a canceled exception spuriously. |
| 75 | // This is problematic because it means that the LoaderManager did not |
| 76 | // cancel the Loader itself and still expects to receive a result. |
| 77 | // Additionally, the Loader's own state will not have been updated to |
| 78 | // reflect the fact that the task was being canceled. |
| 79 | // So we treat this case as an unhandled exception. |
| 80 | throw ex; |
| 81 | } |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 82 | if (DEBUG) Slog.v(TAG, this + " <<< doInBackground (was canceled)", ex); |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 83 | return null; |
| 84 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Runs on the UI thread */ |
| 88 | @Override |
| 89 | protected void onPostExecute(D data) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 90 | if (DEBUG) Slog.v(TAG, this + " onPostExecute"); |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 91 | try { |
| 92 | AsyncTaskLoader.this.dispatchOnLoadComplete(this, data); |
| 93 | } finally { |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 94 | mDone.countDown(); |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 95 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 96 | } |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 97 | |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 98 | /* Runs on the UI thread */ |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 99 | @Override |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 100 | protected void onCancelled(D data) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 101 | if (DEBUG) Slog.v(TAG, this + " onCancelled"); |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 102 | try { |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 103 | AsyncTaskLoader.this.dispatchOnCancelled(this, data); |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 104 | } finally { |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 105 | mDone.countDown(); |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 106 | } |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 109 | /* Runs on the UI thread, when the waiting task is posted to a handler. |
| 110 | * This method is only executed when task execution was deferred (waiting was true). */ |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public void run() { |
| 113 | waiting = false; |
| 114 | AsyncTaskLoader.this.executePendingTask(); |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 115 | } |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 116 | |
| 117 | /* Used for testing purposes to wait for the task to complete. */ |
| 118 | public void waitForLoader() { |
| 119 | try { |
| 120 | mDone.await(); |
| 121 | } catch (InterruptedException e) { |
| 122 | // Ignore |
| 123 | } |
| 124 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 127 | private final Executor mExecutor; |
| 128 | |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 129 | volatile LoadTask mTask; |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 130 | volatile LoadTask mCancellingTask; |
| 131 | |
| 132 | long mUpdateThrottle; |
| 133 | long mLastLoadCompleteTime = -10000; |
| 134 | Handler mHandler; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 135 | |
| 136 | public AsyncTaskLoader(Context context) { |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 137 | this(context, AsyncTask.THREAD_POOL_EXECUTOR); |
| 138 | } |
| 139 | |
| 140 | /** {@hide} */ |
| 141 | public AsyncTaskLoader(Context context, Executor executor) { |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 142 | super(context); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 143 | mExecutor = executor; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 146 | /** |
| 147 | * Set amount to throttle updates by. This is the minimum time from |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 148 | * when the last {@link #loadInBackground()} call has completed until |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 149 | * a new load is scheduled. |
| 150 | * |
| 151 | * @param delayMS Amount of delay, in milliseconds. |
| 152 | */ |
| 153 | public void setUpdateThrottle(long delayMS) { |
| 154 | mUpdateThrottle = delayMS; |
| 155 | if (delayMS != 0) { |
| 156 | mHandler = new Handler(); |
| 157 | } |
| 158 | } |
| 159 | |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 160 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 161 | protected void onForceLoad() { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 162 | super.onForceLoad(); |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 163 | cancelLoad(); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 164 | mTask = new LoadTask(); |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 165 | if (DEBUG) Slog.v(TAG, "Preparing load: mTask=" + mTask); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 166 | executePendingTask(); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 169 | @Override |
| 170 | protected boolean onCancelLoad() { |
| 171 | if (DEBUG) Slog.v(TAG, "onCancelLoad: mTask=" + mTask); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 172 | if (mTask != null) { |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 173 | if (mCancellingTask != null) { |
| 174 | // There was a pending task already waiting for a previous |
| 175 | // one being canceled; just drop it. |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 176 | if (DEBUG) Slog.v(TAG, |
| 177 | "cancelLoad: still waiting for cancelled task; dropping next"); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 178 | if (mTask.waiting) { |
| 179 | mTask.waiting = false; |
| 180 | mHandler.removeCallbacks(mTask); |
| 181 | } |
| 182 | mTask = null; |
| 183 | return false; |
| 184 | } else if (mTask.waiting) { |
| 185 | // There is a task, but it is waiting for the time it should |
| 186 | // execute. We can just toss it. |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 187 | if (DEBUG) Slog.v(TAG, "cancelLoad: task is waiting, dropping it"); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 188 | mTask.waiting = false; |
| 189 | mHandler.removeCallbacks(mTask); |
| 190 | mTask = null; |
| 191 | return false; |
| 192 | } else { |
| 193 | boolean cancelled = mTask.cancel(false); |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 194 | if (DEBUG) Slog.v(TAG, "cancelLoad: cancelled=" + cancelled); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 195 | if (cancelled) { |
| 196 | mCancellingTask = mTask; |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 197 | cancelLoadInBackground(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 198 | } |
| 199 | mTask = null; |
| 200 | return cancelled; |
| 201 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 202 | } |
| 203 | return false; |
| 204 | } |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Called if the task was canceled before it was completed. Gives the class a chance |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 208 | * to clean up post-cancellation and to properly dispose of the result. |
| 209 | * |
| 210 | * @param data The value that was returned by {@link #loadInBackground}, or null |
| 211 | * if the task threw {@link OperationCanceledException}. |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 212 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 213 | public void onCanceled(D data) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 214 | } |
Dmitri Plotnikov | 4afde4f | 2011-01-18 09:41:29 -0800 | [diff] [blame] | 215 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 216 | void executePendingTask() { |
| 217 | if (mCancellingTask == null && mTask != null) { |
| 218 | if (mTask.waiting) { |
| 219 | mTask.waiting = false; |
| 220 | mHandler.removeCallbacks(mTask); |
| 221 | } |
| 222 | if (mUpdateThrottle > 0) { |
| 223 | long now = SystemClock.uptimeMillis(); |
| 224 | if (now < (mLastLoadCompleteTime+mUpdateThrottle)) { |
| 225 | // Not yet time to do another load. |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 226 | if (DEBUG) Slog.v(TAG, "Waiting until " |
| 227 | + (mLastLoadCompleteTime+mUpdateThrottle) |
| 228 | + " to execute: " + mTask); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 229 | mTask.waiting = true; |
| 230 | mHandler.postAtTime(mTask, mLastLoadCompleteTime+mUpdateThrottle); |
| 231 | return; |
| 232 | } |
| 233 | } |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 234 | if (DEBUG) Slog.v(TAG, "Executing: " + mTask); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 235 | mTask.executeOnExecutor(mExecutor, (Void[]) null); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | void dispatchOnCancelled(LoadTask task, D data) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 240 | onCanceled(data); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 241 | if (mCancellingTask == task) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 242 | if (DEBUG) Slog.v(TAG, "Cancelled task is now canceled!"); |
Dianne Hackborn | ca614f7 | 2013-03-14 19:10:04 -0700 | [diff] [blame] | 243 | rollbackContentChanged(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 244 | mLastLoadCompleteTime = SystemClock.uptimeMillis(); |
| 245 | mCancellingTask = null; |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 246 | if (DEBUG) Slog.v(TAG, "Delivering cancellation"); |
| 247 | deliverCancellation(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 248 | executePendingTask(); |
| 249 | } |
| 250 | } |
| 251 | |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 252 | void dispatchOnLoadComplete(LoadTask task, D data) { |
| 253 | if (mTask != task) { |
Dianne Hackborn | 540f86a | 2011-01-11 17:52:22 -0800 | [diff] [blame] | 254 | 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] | 255 | dispatchOnCancelled(task, data); |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 256 | } else { |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 257 | if (isAbandoned()) { |
| 258 | // This cursor has been abandoned; just cancel the new data. |
| 259 | onCanceled(data); |
| 260 | } else { |
Dianne Hackborn | ca614f7 | 2013-03-14 19:10:04 -0700 | [diff] [blame] | 261 | commitContentChanged(); |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 262 | mLastLoadCompleteTime = SystemClock.uptimeMillis(); |
| 263 | mTask = null; |
| 264 | if (DEBUG) Slog.v(TAG, "Delivering result"); |
| 265 | deliverResult(data); |
| 266 | } |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 267 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 271 | * Called on a worker thread to perform the actual load and to return |
| 272 | * the result of the load operation. |
| 273 | * |
| 274 | * Implementations should not deliver the result directly, but should return them |
| 275 | * from this method, which will eventually end up calling {@link #deliverResult} on |
| 276 | * the UI thread. If implementations need to process the results on the UI thread |
| 277 | * they may override {@link #deliverResult} and do so there. |
| 278 | * |
| 279 | * To support cancellation, this method should periodically check the value of |
| 280 | * {@link #isLoadInBackgroundCanceled} and terminate when it returns true. |
| 281 | * Subclasses may also override {@link #cancelLoadInBackground} to interrupt the load |
| 282 | * directly instead of polling {@link #isLoadInBackgroundCanceled}. |
| 283 | * |
| 284 | * When the load is canceled, this method may either return normally or throw |
| 285 | * {@link OperationCanceledException}. In either case, the {@link Loader} will |
| 286 | * call {@link #onCanceled} to perform post-cancellation cleanup and to dispose of the |
| 287 | * result object, if any. |
| 288 | * |
| 289 | * @return The result of the load operation. |
| 290 | * |
| 291 | * @throws OperationCanceledException if the load is canceled during execution. |
| 292 | * |
| 293 | * @see #isLoadInBackgroundCanceled |
| 294 | * @see #cancelLoadInBackground |
| 295 | * @see #onCanceled |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 296 | */ |
| 297 | public abstract D loadInBackground(); |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 298 | |
| 299 | /** |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 300 | * Calls {@link #loadInBackground()}. |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 301 | * |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 302 | * This method is reserved for use by the loader framework. |
| 303 | * Subclasses should override {@link #loadInBackground} instead of this method. |
| 304 | * |
| 305 | * @return The result of the load operation. |
| 306 | * |
| 307 | * @throws OperationCanceledException if the load is canceled during execution. |
| 308 | * |
| 309 | * @see #loadInBackground |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 310 | */ |
| 311 | protected D onLoadInBackground() { |
| 312 | return loadInBackground(); |
| 313 | } |
| 314 | |
| 315 | /** |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 316 | * Called on the main thread to abort a load in progress. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 317 | * |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 318 | * Override this method to abort the current invocation of {@link #loadInBackground} |
| 319 | * that is running in the background on a worker thread. |
| 320 | * |
| 321 | * This method should do nothing if {@link #loadInBackground} has not started |
| 322 | * running or if it has already finished. |
| 323 | * |
| 324 | * @see #loadInBackground |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 325 | */ |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 326 | public void cancelLoadInBackground() { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 330 | * Returns true if the current invocation of {@link #loadInBackground} is being canceled. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 331 | * |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 332 | * @return True if the current invocation of {@link #loadInBackground} is being canceled. |
| 333 | * |
| 334 | * @see #loadInBackground |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 335 | */ |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 336 | public boolean isLoadInBackgroundCanceled() { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 337 | return mCancellingTask != null; |
| 338 | } |
| 339 | |
| 340 | /** |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 341 | * Locks the current thread until the loader completes the current load |
| 342 | * operation. Returns immediately if there is no load operation running. |
Dmitri Plotnikov | 59d8edd | 2011-01-09 11:05:50 -0800 | [diff] [blame] | 343 | * Should not be called from the UI thread: calling it from the UI |
| 344 | * thread would cause a deadlock. |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 345 | * <p> |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 346 | * 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] | 347 | * |
| 348 | * @hide |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 349 | */ |
| 350 | public void waitForLoader() { |
| 351 | LoadTask task = mTask; |
| 352 | if (task != null) { |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 353 | task.waitForLoader(); |
Dmitri Plotnikov | cd3676e | 2011-01-06 18:39:33 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 356 | |
| 357 | @Override |
| 358 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 359 | super.dump(prefix, fd, writer, args); |
| 360 | if (mTask != null) { |
| 361 | writer.print(prefix); writer.print("mTask="); writer.print(mTask); |
| 362 | writer.print(" waiting="); writer.println(mTask.waiting); |
| 363 | } |
| 364 | if (mCancellingTask != null) { |
| 365 | writer.print(prefix); writer.print("mCancellingTask="); writer.print(mCancellingTask); |
| 366 | writer.print(" waiting="); writer.println(mCancellingTask.waiting); |
| 367 | } |
| 368 | if (mUpdateThrottle != 0) { |
| 369 | writer.print(prefix); writer.print("mUpdateThrottle="); |
| 370 | TimeUtils.formatDuration(mUpdateThrottle, writer); |
| 371 | writer.print(" mLastLoadCompleteTime="); |
| 372 | TimeUtils.formatDuration(mLastLoadCompleteTime, |
| 373 | SystemClock.uptimeMillis(), writer); |
| 374 | writer.println(); |
| 375 | } |
| 376 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 377 | } |