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.database.ContentObserver; |
| 20 | import android.os.Handler; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 21 | import android.util.DebugUtils; |
| 22 | |
| 23 | import java.io.FileDescriptor; |
| 24 | import java.io.PrintWriter; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 25 | |
| 26 | /** |
Mark Doliner | 9525f2a | 2014-01-02 11:17:47 -0800 | [diff] [blame] | 27 | * A class that performs asynchronous loading of data. While Loaders are active |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 28 | * they should monitor the source of their data and deliver new results when the contents |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 29 | * change. See {@link android.app.LoaderManager} for more detail. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 30 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 31 | * <p><b>Note on threading:</b> Clients of loaders should as a rule perform |
| 32 | * any calls on to a Loader from the main thread of their process (that is, |
| 33 | * the thread the Activity callbacks and other things occur on). Subclasses |
| 34 | * of Loader (such as {@link AsyncTaskLoader}) will often perform their work |
| 35 | * in a separate thread, but when delivering their results this too should |
| 36 | * be done on the main thread.</p> |
| 37 | * |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 38 | * <p>Subclasses generally must implement at least {@link #onStartLoading()}, |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 39 | * {@link #onStopLoading()}, {@link #onForceLoad()}, and {@link #onReset()}.</p> |
| 40 | * |
| 41 | * <p>Most implementations should not derive directly from this class, but |
| 42 | * instead inherit from {@link AsyncTaskLoader}.</p> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 43 | * |
| 44 | * <div class="special reference"> |
| 45 | * <h3>Developer Guides</h3> |
| 46 | * <p>For more information about using loaders, read the |
Mark Lu | 9f52b7a | 2016-08-12 15:48:05 -0700 | [diff] [blame] | 47 | * <a href="{@docRoot}guide/components/loaders.html">Loaders</a> developer guide.</p> |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 48 | * </div> |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 49 | * |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 50 | * @param <D> The result returned when the load is complete |
Ian Lake | 0a1feb8 | 2017-11-13 10:26:46 -0800 | [diff] [blame^] | 51 | * |
| 52 | * @deprecated Use {@link android.support.v4.content.Loader} |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 53 | */ |
Ian Lake | 0a1feb8 | 2017-11-13 10:26:46 -0800 | [diff] [blame^] | 54 | @Deprecated |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 55 | public class Loader<D> { |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 56 | int mId; |
| 57 | OnLoadCompleteListener<D> mListener; |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 58 | OnLoadCanceledListener<D> mOnLoadCanceledListener; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 59 | Context mContext; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 60 | boolean mStarted = false; |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 61 | boolean mAbandoned = false; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 62 | boolean mReset = true; |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 63 | boolean mContentChanged = false; |
Dianne Hackborn | ca614f7 | 2013-03-14 19:10:04 -0700 | [diff] [blame] | 64 | boolean mProcessingChange = false; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 65 | |
Dianne Hackborn | 7871bad | 2011-12-12 15:19:26 -0800 | [diff] [blame] | 66 | /** |
| 67 | * An implementation of a ContentObserver that takes care of connecting |
| 68 | * it to the Loader to have the loader re-load its data when the observer |
| 69 | * is told it has changed. You do not normally need to use this yourself; |
| 70 | * it is used for you by {@link CursorLoader} to take care of executing |
| 71 | * an update when the cursor's backing data changes. |
Ian Lake | 0a1feb8 | 2017-11-13 10:26:46 -0800 | [diff] [blame^] | 72 | * |
| 73 | * @deprecated Use {@link android.support.v4.content.Loader.ForceLoadContentObserver} |
Dianne Hackborn | 7871bad | 2011-12-12 15:19:26 -0800 | [diff] [blame] | 74 | */ |
Ian Lake | 0a1feb8 | 2017-11-13 10:26:46 -0800 | [diff] [blame^] | 75 | @Deprecated |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 76 | public final class ForceLoadContentObserver extends ContentObserver { |
| 77 | public ForceLoadContentObserver() { |
| 78 | super(new Handler()); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean deliverSelfNotifications() { |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onChange(boolean selfChange) { |
Makoto Onuki | ec37c88 | 2010-08-02 16:57:01 -0700 | [diff] [blame] | 88 | onContentChanged(); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Dianne Hackborn | 7871bad | 2011-12-12 15:19:26 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Interface that is implemented to discover when a Loader has finished |
| 94 | * loading its data. You do not normally need to implement this yourself; |
| 95 | * it is used in the implementation of {@link android.app.LoaderManager} |
| 96 | * to find out when a Loader it is managing has completed so that this can |
| 97 | * be reported to its client. This interface should only be used if a |
| 98 | * Loader is not being used in conjunction with LoaderManager. |
Ian Lake | 0a1feb8 | 2017-11-13 10:26:46 -0800 | [diff] [blame^] | 99 | * |
| 100 | * @deprecated Use {@link android.support.v4.content.Loader.OnLoadCompleteListener} |
Dianne Hackborn | 7871bad | 2011-12-12 15:19:26 -0800 | [diff] [blame] | 101 | */ |
Ian Lake | 0a1feb8 | 2017-11-13 10:26:46 -0800 | [diff] [blame^] | 102 | @Deprecated |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 103 | public interface OnLoadCompleteListener<D> { |
| 104 | /** |
| 105 | * Called on the thread that created the Loader when the load is complete. |
| 106 | * |
| 107 | * @param loader the loader that completed the load |
| 108 | * @param data the result of the load |
| 109 | */ |
| 110 | public void onLoadComplete(Loader<D> loader, D data); |
| 111 | } |
| 112 | |
| 113 | /** |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 114 | * Interface that is implemented to discover when a Loader has been canceled |
| 115 | * before it finished loading its data. You do not normally need to implement |
| 116 | * this yourself; it is used in the implementation of {@link android.app.LoaderManager} |
| 117 | * to find out when a Loader it is managing has been canceled so that it |
| 118 | * can schedule the next Loader. This interface should only be used if a |
| 119 | * Loader is not being used in conjunction with LoaderManager. |
Ian Lake | 0a1feb8 | 2017-11-13 10:26:46 -0800 | [diff] [blame^] | 120 | * |
| 121 | * @deprecated Use {@link android.support.v4.content.Loader.OnLoadCanceledListener} |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 122 | */ |
Ian Lake | 0a1feb8 | 2017-11-13 10:26:46 -0800 | [diff] [blame^] | 123 | @Deprecated |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 124 | public interface OnLoadCanceledListener<D> { |
| 125 | /** |
| 126 | * Called on the thread that created the Loader when the load is canceled. |
| 127 | * |
| 128 | * @param loader the loader that canceled the load |
| 129 | */ |
| 130 | public void onLoadCanceled(Loader<D> loader); |
| 131 | } |
| 132 | |
| 133 | /** |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 134 | * Stores away the application context associated with context. |
| 135 | * Since Loaders can be used across multiple activities it's dangerous to |
| 136 | * store the context directly; always use {@link #getContext()} to retrieve |
| 137 | * the Loader's Context, don't use the constructor argument directly. |
| 138 | * The Context returned by {@link #getContext} is safe to use across |
| 139 | * Activity instances. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 140 | * |
| 141 | * @param context used to retrieve the application context. |
| 142 | */ |
| 143 | public Loader(Context context) { |
| 144 | mContext = context.getApplicationContext(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sends the result of the load to the registered listener. Should only be called by subclasses. |
| 149 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 150 | * Must be called from the process's main thread. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 151 | * |
| 152 | * @param data the result of the load |
| 153 | */ |
| 154 | public void deliverResult(D data) { |
| 155 | if (mListener != null) { |
| 156 | mListener.onLoadComplete(this, data); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 161 | * Informs the registered {@link OnLoadCanceledListener} that the load has been canceled. |
| 162 | * Should only be called by subclasses. |
| 163 | * |
| 164 | * Must be called from the process's main thread. |
| 165 | */ |
| 166 | public void deliverCancellation() { |
| 167 | if (mOnLoadCanceledListener != null) { |
| 168 | mOnLoadCanceledListener.onLoadCanceled(this); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 173 | * @return an application context retrieved from the Context passed to the constructor. |
| 174 | */ |
| 175 | public Context getContext() { |
| 176 | return mContext; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @return the ID of this loader |
| 181 | */ |
| 182 | public int getId() { |
| 183 | return mId; |
| 184 | } |
| 185 | |
| 186 | /** |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 187 | * Registers a class that will receive callbacks when a load is complete. |
| 188 | * The callback will be called on the process's main thread so it's safe to |
| 189 | * pass the results to widgets. |
Makoto Onuki | ec37c88 | 2010-08-02 16:57:01 -0700 | [diff] [blame] | 190 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 191 | * <p>Must be called from the process's main thread. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 192 | */ |
| 193 | public void registerListener(int id, OnLoadCompleteListener<D> listener) { |
| 194 | if (mListener != null) { |
| 195 | throw new IllegalStateException("There is already a listener registered"); |
| 196 | } |
| 197 | mListener = listener; |
| 198 | mId = id; |
| 199 | } |
| 200 | |
| 201 | /** |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 202 | * Remove a listener that was previously added with {@link #registerListener}. |
| 203 | * |
| 204 | * Must be called from the process's main thread. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 205 | */ |
| 206 | public void unregisterListener(OnLoadCompleteListener<D> listener) { |
| 207 | if (mListener == null) { |
| 208 | throw new IllegalStateException("No listener register"); |
| 209 | } |
| 210 | if (mListener != listener) { |
| 211 | throw new IllegalArgumentException("Attempting to unregister the wrong listener"); |
| 212 | } |
| 213 | mListener = null; |
| 214 | } |
| 215 | |
| 216 | /** |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 217 | * Registers a listener that will receive callbacks when a load is canceled. |
| 218 | * The callback will be called on the process's main thread so it's safe to |
| 219 | * pass the results to widgets. |
| 220 | * |
| 221 | * Must be called from the process's main thread. |
| 222 | * |
| 223 | * @param listener The listener to register. |
| 224 | */ |
| 225 | public void registerOnLoadCanceledListener(OnLoadCanceledListener<D> listener) { |
| 226 | if (mOnLoadCanceledListener != null) { |
| 227 | throw new IllegalStateException("There is already a listener registered"); |
| 228 | } |
| 229 | mOnLoadCanceledListener = listener; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Unregisters a listener that was previously added with |
| 234 | * {@link #registerOnLoadCanceledListener}. |
| 235 | * |
| 236 | * Must be called from the process's main thread. |
| 237 | * |
| 238 | * @param listener The listener to unregister. |
| 239 | */ |
| 240 | public void unregisterOnLoadCanceledListener(OnLoadCanceledListener<D> listener) { |
| 241 | if (mOnLoadCanceledListener == null) { |
| 242 | throw new IllegalStateException("No listener register"); |
| 243 | } |
| 244 | if (mOnLoadCanceledListener != listener) { |
| 245 | throw new IllegalArgumentException("Attempting to unregister the wrong listener"); |
| 246 | } |
| 247 | mOnLoadCanceledListener = null; |
| 248 | } |
| 249 | |
| 250 | /** |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 251 | * Return whether this load has been started. That is, its {@link #startLoading()} |
| 252 | * has been called and no calls to {@link #stopLoading()} or |
| 253 | * {@link #reset()} have yet been made. |
| 254 | */ |
| 255 | public boolean isStarted() { |
| 256 | return mStarted; |
| 257 | } |
| 258 | |
| 259 | /** |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 260 | * Return whether this loader has been abandoned. In this state, the |
| 261 | * loader <em>must not</em> report any new data, and <em>must</em> keep |
| 262 | * its last reported data valid until it is finally reset. |
| 263 | */ |
| 264 | public boolean isAbandoned() { |
| 265 | return mAbandoned; |
| 266 | } |
| 267 | |
| 268 | /** |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 269 | * Return whether this load has been reset. That is, either the loader |
| 270 | * has not yet been started for the first time, or its {@link #reset()} |
| 271 | * has been called. |
| 272 | */ |
| 273 | public boolean isReset() { |
| 274 | return mReset; |
| 275 | } |
| 276 | |
| 277 | /** |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 278 | * This function will normally be called for you automatically by |
| 279 | * {@link android.app.LoaderManager} when the associated fragment/activity |
| 280 | * is being started. When using a Loader with {@link android.app.LoaderManager}, |
| 281 | * you <em>must not</em> call this method yourself, or you will conflict |
| 282 | * with its management of the Loader. |
| 283 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 284 | * Starts an asynchronous load of the Loader's data. When the result |
| 285 | * is ready the callbacks will be called on the process's main thread. |
| 286 | * If a previous load has been completed and is still valid |
| 287 | * the result may be passed to the callbacks immediately. |
| 288 | * The loader will monitor the source of |
| 289 | * the data set and may deliver future callbacks if the source changes. |
| 290 | * Calling {@link #stopLoading} will stop the delivery of callbacks. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 291 | * |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 292 | * <p>This updates the Loader's internal state so that |
| 293 | * {@link #isStarted()} and {@link #isReset()} will return the correct |
| 294 | * values, and then calls the implementation's {@link #onStartLoading()}. |
| 295 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 296 | * <p>Must be called from the process's main thread. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 297 | */ |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 298 | public final void startLoading() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 299 | mStarted = true; |
| 300 | mReset = false; |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 301 | mAbandoned = false; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 302 | onStartLoading(); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Subclasses must implement this to take care of loading their data, |
| 307 | * as per {@link #startLoading()}. This is not called by clients directly, |
| 308 | * but as a result of a call to {@link #startLoading()}. |
| 309 | */ |
| 310 | protected void onStartLoading() { |
| 311 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 312 | |
| 313 | /** |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 314 | * Attempt to cancel the current load task. |
| 315 | * Must be called on the main thread of the process. |
| 316 | * |
| 317 | * <p>Cancellation is not an immediate operation, since the load is performed |
| 318 | * in a background thread. If there is currently a load in progress, this |
| 319 | * method requests that the load be canceled, and notes this is the case; |
| 320 | * once the background thread has completed its work its remaining state |
| 321 | * will be cleared. If another load request comes in during this time, |
| 322 | * it will be held until the canceled load is complete. |
| 323 | * |
| 324 | * @return Returns <tt>false</tt> if the task could not be canceled, |
| 325 | * typically because it has already completed normally, or |
| 326 | * because {@link #startLoading()} hasn't been called; returns |
| 327 | * <tt>true</tt> otherwise. When <tt>true</tt> is returned, the task |
| 328 | * is still running and the {@link OnLoadCanceledListener} will be called |
| 329 | * when the task completes. |
| 330 | */ |
| 331 | public boolean cancelLoad() { |
| 332 | return onCancelLoad(); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Subclasses must implement this to take care of requests to {@link #cancelLoad()}. |
| 337 | * This will always be called from the process's main thread. |
| 338 | * |
| 339 | * @return Returns <tt>false</tt> if the task could not be canceled, |
| 340 | * typically because it has already completed normally, or |
| 341 | * because {@link #startLoading()} hasn't been called; returns |
| 342 | * <tt>true</tt> otherwise. When <tt>true</tt> is returned, the task |
| 343 | * is still running and the {@link OnLoadCanceledListener} will be called |
| 344 | * when the task completes. |
| 345 | */ |
| 346 | protected boolean onCancelLoad() { |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | /** |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 351 | * Force an asynchronous load. Unlike {@link #startLoading()} this will ignore a previously |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 352 | * loaded data set and load a new one. This simply calls through to the |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 353 | * implementation's {@link #onForceLoad()}. You generally should only call this |
| 354 | * when the loader is started -- that is, {@link #isStarted()} returns true. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 355 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 356 | * <p>Must be called from the process's main thread. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 357 | */ |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 358 | public void forceLoad() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 359 | onForceLoad(); |
| 360 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 361 | |
| 362 | /** |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 363 | * Subclasses must implement this to take care of requests to {@link #forceLoad()}. |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 364 | * This will always be called from the process's main thread. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 365 | */ |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 366 | protected void onForceLoad() { |
| 367 | } |
| 368 | |
| 369 | /** |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 370 | * This function will normally be called for you automatically by |
| 371 | * {@link android.app.LoaderManager} when the associated fragment/activity |
| 372 | * is being stopped. When using a Loader with {@link android.app.LoaderManager}, |
| 373 | * you <em>must not</em> call this method yourself, or you will conflict |
| 374 | * with its management of the Loader. |
| 375 | * |
| 376 | * <p>Stops delivery of updates until the next time {@link #startLoading()} is called. |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 377 | * Implementations should <em>not</em> invalidate their data at this point -- |
| 378 | * clients are still free to use the last data the loader reported. They will, |
| 379 | * however, typically stop reporting new data if the data changes; they can |
| 380 | * still monitor for changes, but must not report them to the client until and |
| 381 | * if {@link #startLoading()} is later called. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 382 | * |
| 383 | * <p>This updates the Loader's internal state so that |
| 384 | * {@link #isStarted()} will return the correct |
| 385 | * value, and then calls the implementation's {@link #onStopLoading()}. |
| 386 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 387 | * <p>Must be called from the process's main thread. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 388 | */ |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 389 | public void stopLoading() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 390 | mStarted = false; |
| 391 | onStopLoading(); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Subclasses must implement this to take care of stopping their loader, |
| 396 | * as per {@link #stopLoading()}. This is not called by clients directly, |
| 397 | * but as a result of a call to {@link #stopLoading()}. |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 398 | * This will always be called from the process's main thread. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 399 | */ |
| 400 | protected void onStopLoading() { |
| 401 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 402 | |
| 403 | /** |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 404 | * This function will normally be called for you automatically by |
| 405 | * {@link android.app.LoaderManager} when restarting a Loader. When using |
| 406 | * a Loader with {@link android.app.LoaderManager}, |
| 407 | * you <em>must not</em> call this method yourself, or you will conflict |
| 408 | * with its management of the Loader. |
| 409 | * |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 410 | * Tell the Loader that it is being abandoned. This is called prior |
| 411 | * to {@link #reset} to have it retain its current data but not report |
| 412 | * any new data. |
| 413 | */ |
| 414 | public void abandon() { |
| 415 | mAbandoned = true; |
| 416 | onAbandon(); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Subclasses implement this to take care of being abandoned. This is |
| 421 | * an optional intermediate state prior to {@link #onReset()} -- it means that |
| 422 | * the client is no longer interested in any new data from the loader, |
| 423 | * so the loader must not report any further updates. However, the |
| 424 | * loader <em>must</em> keep its last reported data valid until the final |
| 425 | * {@link #onReset()} happens. You can retrieve the current abandoned |
| 426 | * state with {@link #isAbandoned}. |
| 427 | */ |
Jeff Brown | c64ff37 | 2013-10-09 18:50:56 -0700 | [diff] [blame] | 428 | protected void onAbandon() { |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 432 | * This function will normally be called for you automatically by |
| 433 | * {@link android.app.LoaderManager} when destroying a Loader. When using |
| 434 | * a Loader with {@link android.app.LoaderManager}, |
| 435 | * you <em>must not</em> call this method yourself, or you will conflict |
| 436 | * with its management of the Loader. |
| 437 | * |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 438 | * Resets the state of the Loader. The Loader should at this point free |
| 439 | * all of its resources, since it may never be called again; however, its |
| 440 | * {@link #startLoading()} may later be called at which point it must be |
| 441 | * able to start running again. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 442 | * |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 443 | * <p>This updates the Loader's internal state so that |
| 444 | * {@link #isStarted()} and {@link #isReset()} will return the correct |
| 445 | * values, and then calls the implementation's {@link #onReset()}. |
| 446 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 447 | * <p>Must be called from the process's main thread. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 448 | */ |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 449 | public void reset() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 450 | onReset(); |
| 451 | mReset = true; |
| 452 | mStarted = false; |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 453 | mAbandoned = false; |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 454 | mContentChanged = false; |
Dianne Hackborn | ca614f7 | 2013-03-14 19:10:04 -0700 | [diff] [blame] | 455 | mProcessingChange = false; |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 459 | * Subclasses must implement this to take care of resetting their loader, |
| 460 | * as per {@link #reset()}. This is not called by clients directly, |
| 461 | * but as a result of a call to {@link #reset()}. |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 462 | * This will always be called from the process's main thread. |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 463 | */ |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 464 | protected void onReset() { |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 465 | } |
Makoto Onuki | ec37c88 | 2010-08-02 16:57:01 -0700 | [diff] [blame] | 466 | |
| 467 | /** |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 468 | * Take the current flag indicating whether the loader's content had |
| 469 | * changed while it was stopped. If it had, true is returned and the |
| 470 | * flag is cleared. |
| 471 | */ |
| 472 | public boolean takeContentChanged() { |
| 473 | boolean res = mContentChanged; |
| 474 | mContentChanged = false; |
Dianne Hackborn | ca614f7 | 2013-03-14 19:10:04 -0700 | [diff] [blame] | 475 | mProcessingChange |= res; |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 476 | return res; |
| 477 | } |
Dianne Hackborn | ca614f7 | 2013-03-14 19:10:04 -0700 | [diff] [blame] | 478 | |
| 479 | /** |
| 480 | * Commit that you have actually fully processed a content change that |
| 481 | * was returned by {@link #takeContentChanged}. This is for use with |
| 482 | * {@link #rollbackContentChanged()} to handle situations where a load |
| 483 | * is cancelled. Call this when you have completely processed a load |
| 484 | * without it being cancelled. |
| 485 | */ |
| 486 | public void commitContentChanged() { |
| 487 | mProcessingChange = false; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Report that you have abandoned the processing of a content change that |
| 492 | * was returned by {@link #takeContentChanged()} and would like to rollback |
| 493 | * to the state where there is again a pending content change. This is |
| 494 | * to handle the case where a data load due to a content change has been |
| 495 | * canceled before its data was delivered back to the loader. |
| 496 | */ |
| 497 | public void rollbackContentChanged() { |
| 498 | if (mProcessingChange) { |
Adam Powell | 4be84bb | 2016-04-27 13:45:38 -0700 | [diff] [blame] | 499 | onContentChanged(); |
Dianne Hackborn | ca614f7 | 2013-03-14 19:10:04 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 503 | /** |
| 504 | * Called when {@link ForceLoadContentObserver} detects a change. The |
| 505 | * default implementation checks to see if the loader is currently started; |
| 506 | * if so, it simply calls {@link #forceLoad()}; otherwise, it sets a flag |
| 507 | * so that {@link #takeContentChanged()} returns true. |
Makoto Onuki | ec37c88 | 2010-08-02 16:57:01 -0700 | [diff] [blame] | 508 | * |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 509 | * <p>Must be called from the process's main thread. |
Makoto Onuki | ec37c88 | 2010-08-02 16:57:01 -0700 | [diff] [blame] | 510 | */ |
| 511 | public void onContentChanged() { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 512 | if (mStarted) { |
| 513 | forceLoad(); |
| 514 | } else { |
| 515 | // This loader has been stopped, so we don't want to load |
| 516 | // new data right now... but keep track of it changing to |
| 517 | // refresh later if we start again. |
| 518 | mContentChanged = true; |
| 519 | } |
Makoto Onuki | ec37c88 | 2010-08-02 16:57:01 -0700 | [diff] [blame] | 520 | } |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 521 | |
| 522 | /** |
| 523 | * For debugging, converts an instance of the Loader's data class to |
| 524 | * a string that can be printed. Must handle a null data. |
| 525 | */ |
| 526 | public String dataToString(D data) { |
| 527 | StringBuilder sb = new StringBuilder(64); |
| 528 | DebugUtils.buildShortClassTag(data, sb); |
| 529 | sb.append("}"); |
| 530 | return sb.toString(); |
| 531 | } |
| 532 | |
| 533 | @Override |
| 534 | public String toString() { |
| 535 | StringBuilder sb = new StringBuilder(64); |
| 536 | DebugUtils.buildShortClassTag(this, sb); |
| 537 | sb.append(" id="); |
| 538 | sb.append(mId); |
| 539 | sb.append("}"); |
| 540 | return sb.toString(); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Print the Loader's state into the given stream. |
| 545 | * |
| 546 | * @param prefix Text to print at the front of each line. |
| 547 | * @param fd The raw file descriptor that the dump is being sent to. |
| 548 | * @param writer A PrintWriter to which the dump is to be set. |
| 549 | * @param args Additional arguments to the dump request. |
| 550 | */ |
| 551 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 552 | writer.print(prefix); writer.print("mId="); writer.print(mId); |
| 553 | writer.print(" mListener="); writer.println(mListener); |
Dianne Hackborn | ca614f7 | 2013-03-14 19:10:04 -0700 | [diff] [blame] | 554 | if (mStarted || mContentChanged || mProcessingChange) { |
| 555 | writer.print(prefix); writer.print("mStarted="); writer.print(mStarted); |
| 556 | writer.print(" mContentChanged="); writer.print(mContentChanged); |
| 557 | writer.print(" mProcessingChange="); writer.println(mProcessingChange); |
| 558 | } |
| 559 | if (mAbandoned || mReset) { |
| 560 | writer.print(prefix); writer.print("mAbandoned="); writer.print(mAbandoned); |
| 561 | writer.print(" mReset="); writer.println(mReset); |
| 562 | } |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 563 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 564 | } |