blob: b0555d4ce7e09b942f79116190618fda88d7b098 [file] [log] [blame]
Jeff Hamilton9911b7f2010-05-15 02:20:31 -05001/*
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
17package android.content;
18
19import android.database.ContentObserver;
20import android.os.Handler;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080021import android.util.DebugUtils;
22
23import java.io.FileDescriptor;
24import java.io.PrintWriter;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050025
26/**
Mark Doliner9525f2a2014-01-02 11:17:47 -080027 * A class that performs asynchronous loading of data. While Loaders are active
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050028 * they should monitor the source of their data and deliver new results when the contents
Dianne Hackborn9567a662011-04-19 18:44:03 -070029 * change. See {@link android.app.LoaderManager} for more detail.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050030 *
Dianne Hackborn247fe742011-01-08 17:25:57 -080031 * <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 Hackborna2ea7472010-12-20 12:10:01 -080038 * <p>Subclasses generally must implement at least {@link #onStartLoading()},
Dianne Hackborn9567a662011-04-19 18:44:03 -070039 * {@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 Fernandezb54e7a32011-10-03 15:09:50 -070043 *
44 * <div class="special reference">
45 * <h3>Developer Guides</h3>
46 * <p>For more information about using loaders, read the
Mark Lu9f52b7a2016-08-12 15:48:05 -070047 * <a href="{@docRoot}guide/components/loaders.html">Loaders</a> developer guide.</p>
Joe Fernandezb54e7a32011-10-03 15:09:50 -070048 * </div>
Dianne Hackborna2ea7472010-12-20 12:10:01 -080049 *
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050050 * @param <D> The result returned when the load is complete
Ian Lake0a1feb82017-11-13 10:26:46 -080051 *
Ian Lake1f4e67b2017-12-18 10:36:18 -080052 * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">Support Library</a>
53 * {@link android.support.v4.content.Loader}
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050054 */
Ian Lake0a1feb82017-11-13 10:26:46 -080055@Deprecated
Dianne Hackborna2ea7472010-12-20 12:10:01 -080056public class Loader<D> {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050057 int mId;
58 OnLoadCompleteListener<D> mListener;
Jeff Brownb19a71a2012-01-31 11:48:39 -080059 OnLoadCanceledListener<D> mOnLoadCanceledListener;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050060 Context mContext;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080061 boolean mStarted = false;
Dianne Hackborn260c3c72011-01-30 16:55:55 -080062 boolean mAbandoned = false;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080063 boolean mReset = true;
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -080064 boolean mContentChanged = false;
Dianne Hackbornca614f72013-03-14 19:10:04 -070065 boolean mProcessingChange = false;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050066
Dianne Hackborn7871bad2011-12-12 15:19:26 -080067 /**
68 * An implementation of a ContentObserver that takes care of connecting
69 * it to the Loader to have the loader re-load its data when the observer
70 * is told it has changed. You do not normally need to use this yourself;
71 * it is used for you by {@link CursorLoader} to take care of executing
72 * an update when the cursor's backing data changes.
Ian Lake0a1feb82017-11-13 10:26:46 -080073 *
74 * @deprecated Use {@link android.support.v4.content.Loader.ForceLoadContentObserver}
Dianne Hackborn7871bad2011-12-12 15:19:26 -080075 */
Ian Lake0a1feb82017-11-13 10:26:46 -080076 @Deprecated
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050077 public final class ForceLoadContentObserver extends ContentObserver {
78 public ForceLoadContentObserver() {
79 super(new Handler());
80 }
81
82 @Override
83 public boolean deliverSelfNotifications() {
84 return true;
85 }
86
87 @Override
88 public void onChange(boolean selfChange) {
Makoto Onukiec37c882010-08-02 16:57:01 -070089 onContentChanged();
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050090 }
91 }
92
Dianne Hackborn7871bad2011-12-12 15:19:26 -080093 /**
94 * Interface that is implemented to discover when a Loader has finished
95 * loading its data. You do not normally need to implement this yourself;
96 * it is used in the implementation of {@link android.app.LoaderManager}
97 * to find out when a Loader it is managing has completed so that this can
98 * be reported to its client. This interface should only be used if a
99 * Loader is not being used in conjunction with LoaderManager.
Ian Lake0a1feb82017-11-13 10:26:46 -0800100 *
101 * @deprecated Use {@link android.support.v4.content.Loader.OnLoadCompleteListener}
Dianne Hackborn7871bad2011-12-12 15:19:26 -0800102 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800103 @Deprecated
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500104 public interface OnLoadCompleteListener<D> {
105 /**
106 * Called on the thread that created the Loader when the load is complete.
107 *
108 * @param loader the loader that completed the load
109 * @param data the result of the load
110 */
111 public void onLoadComplete(Loader<D> loader, D data);
112 }
113
114 /**
Jeff Brownb19a71a2012-01-31 11:48:39 -0800115 * Interface that is implemented to discover when a Loader has been canceled
116 * before it finished loading its data. You do not normally need to implement
117 * this yourself; it is used in the implementation of {@link android.app.LoaderManager}
118 * to find out when a Loader it is managing has been canceled so that it
119 * can schedule the next Loader. This interface should only be used if a
120 * Loader is not being used in conjunction with LoaderManager.
Ian Lake0a1feb82017-11-13 10:26:46 -0800121 *
122 * @deprecated Use {@link android.support.v4.content.Loader.OnLoadCanceledListener}
Jeff Brownb19a71a2012-01-31 11:48:39 -0800123 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800124 @Deprecated
Jeff Brownb19a71a2012-01-31 11:48:39 -0800125 public interface OnLoadCanceledListener<D> {
126 /**
127 * Called on the thread that created the Loader when the load is canceled.
128 *
129 * @param loader the loader that canceled the load
130 */
131 public void onLoadCanceled(Loader<D> loader);
132 }
133
134 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -0700135 * Stores away the application context associated with context.
136 * Since Loaders can be used across multiple activities it's dangerous to
137 * store the context directly; always use {@link #getContext()} to retrieve
138 * the Loader's Context, don't use the constructor argument directly.
139 * The Context returned by {@link #getContext} is safe to use across
140 * Activity instances.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500141 *
142 * @param context used to retrieve the application context.
143 */
144 public Loader(Context context) {
145 mContext = context.getApplicationContext();
146 }
147
148 /**
149 * Sends the result of the load to the registered listener. Should only be called by subclasses.
150 *
Dianne Hackborn247fe742011-01-08 17:25:57 -0800151 * Must be called from the process's main thread.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500152 *
153 * @param data the result of the load
154 */
155 public void deliverResult(D data) {
156 if (mListener != null) {
157 mListener.onLoadComplete(this, data);
158 }
159 }
160
161 /**
Jeff Brownb19a71a2012-01-31 11:48:39 -0800162 * Informs the registered {@link OnLoadCanceledListener} that the load has been canceled.
163 * Should only be called by subclasses.
164 *
165 * Must be called from the process's main thread.
166 */
167 public void deliverCancellation() {
168 if (mOnLoadCanceledListener != null) {
169 mOnLoadCanceledListener.onLoadCanceled(this);
170 }
171 }
172
173 /**
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500174 * @return an application context retrieved from the Context passed to the constructor.
175 */
176 public Context getContext() {
177 return mContext;
178 }
179
180 /**
181 * @return the ID of this loader
182 */
183 public int getId() {
184 return mId;
185 }
186
187 /**
Dianne Hackborn247fe742011-01-08 17:25:57 -0800188 * Registers a class that will receive callbacks when a load is complete.
189 * The callback will be called on the process's main thread so it's safe to
190 * pass the results to widgets.
Makoto Onukiec37c882010-08-02 16:57:01 -0700191 *
Dianne Hackborn247fe742011-01-08 17:25:57 -0800192 * <p>Must be called from the process's main thread.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500193 */
194 public void registerListener(int id, OnLoadCompleteListener<D> listener) {
195 if (mListener != null) {
196 throw new IllegalStateException("There is already a listener registered");
197 }
198 mListener = listener;
199 mId = id;
200 }
201
202 /**
Dianne Hackborn247fe742011-01-08 17:25:57 -0800203 * Remove a listener that was previously added with {@link #registerListener}.
204 *
205 * Must be called from the process's main thread.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500206 */
207 public void unregisterListener(OnLoadCompleteListener<D> listener) {
208 if (mListener == null) {
209 throw new IllegalStateException("No listener register");
210 }
211 if (mListener != listener) {
212 throw new IllegalArgumentException("Attempting to unregister the wrong listener");
213 }
214 mListener = null;
215 }
216
217 /**
Jeff Brownb19a71a2012-01-31 11:48:39 -0800218 * Registers a listener that will receive callbacks when a load is canceled.
219 * The callback will be called on the process's main thread so it's safe to
220 * pass the results to widgets.
221 *
222 * Must be called from the process's main thread.
223 *
224 * @param listener The listener to register.
225 */
226 public void registerOnLoadCanceledListener(OnLoadCanceledListener<D> listener) {
227 if (mOnLoadCanceledListener != null) {
228 throw new IllegalStateException("There is already a listener registered");
229 }
230 mOnLoadCanceledListener = listener;
231 }
232
233 /**
234 * Unregisters a listener that was previously added with
235 * {@link #registerOnLoadCanceledListener}.
236 *
237 * Must be called from the process's main thread.
238 *
239 * @param listener The listener to unregister.
240 */
241 public void unregisterOnLoadCanceledListener(OnLoadCanceledListener<D> listener) {
242 if (mOnLoadCanceledListener == null) {
243 throw new IllegalStateException("No listener register");
244 }
245 if (mOnLoadCanceledListener != listener) {
246 throw new IllegalArgumentException("Attempting to unregister the wrong listener");
247 }
248 mOnLoadCanceledListener = null;
249 }
250
251 /**
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800252 * Return whether this load has been started. That is, its {@link #startLoading()}
253 * has been called and no calls to {@link #stopLoading()} or
254 * {@link #reset()} have yet been made.
255 */
256 public boolean isStarted() {
257 return mStarted;
258 }
259
260 /**
Dianne Hackborn260c3c72011-01-30 16:55:55 -0800261 * Return whether this loader has been abandoned. In this state, the
262 * loader <em>must not</em> report any new data, and <em>must</em> keep
263 * its last reported data valid until it is finally reset.
264 */
265 public boolean isAbandoned() {
266 return mAbandoned;
267 }
268
269 /**
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800270 * Return whether this load has been reset. That is, either the loader
271 * has not yet been started for the first time, or its {@link #reset()}
272 * has been called.
273 */
274 public boolean isReset() {
275 return mReset;
276 }
277
278 /**
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700279 * This function will normally be called for you automatically by
280 * {@link android.app.LoaderManager} when the associated fragment/activity
281 * is being started. When using a Loader with {@link android.app.LoaderManager},
282 * you <em>must not</em> call this method yourself, or you will conflict
283 * with its management of the Loader.
284 *
Dianne Hackborn247fe742011-01-08 17:25:57 -0800285 * Starts an asynchronous load of the Loader's data. When the result
286 * is ready the callbacks will be called on the process's main thread.
287 * If a previous load has been completed and is still valid
288 * the result may be passed to the callbacks immediately.
289 * The loader will monitor the source of
290 * the data set and may deliver future callbacks if the source changes.
291 * Calling {@link #stopLoading} will stop the delivery of callbacks.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500292 *
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800293 * <p>This updates the Loader's internal state so that
294 * {@link #isStarted()} and {@link #isReset()} will return the correct
295 * values, and then calls the implementation's {@link #onStartLoading()}.
296 *
Dianne Hackborn247fe742011-01-08 17:25:57 -0800297 * <p>Must be called from the process's main thread.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500298 */
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800299 public final void startLoading() {
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800300 mStarted = true;
301 mReset = false;
Dianne Hackborn260c3c72011-01-30 16:55:55 -0800302 mAbandoned = false;
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800303 onStartLoading();
304 }
305
306 /**
307 * Subclasses must implement this to take care of loading their data,
308 * as per {@link #startLoading()}. This is not called by clients directly,
309 * but as a result of a call to {@link #startLoading()}.
310 */
311 protected void onStartLoading() {
312 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500313
314 /**
Jeff Brownb19a71a2012-01-31 11:48:39 -0800315 * Attempt to cancel the current load task.
316 * Must be called on the main thread of the process.
317 *
318 * <p>Cancellation is not an immediate operation, since the load is performed
319 * in a background thread. If there is currently a load in progress, this
320 * method requests that the load be canceled, and notes this is the case;
321 * once the background thread has completed its work its remaining state
322 * will be cleared. If another load request comes in during this time,
323 * it will be held until the canceled load is complete.
324 *
325 * @return Returns <tt>false</tt> if the task could not be canceled,
326 * typically because it has already completed normally, or
327 * because {@link #startLoading()} hasn't been called; returns
328 * <tt>true</tt> otherwise. When <tt>true</tt> is returned, the task
329 * is still running and the {@link OnLoadCanceledListener} will be called
330 * when the task completes.
331 */
332 public boolean cancelLoad() {
333 return onCancelLoad();
334 }
335
336 /**
337 * Subclasses must implement this to take care of requests to {@link #cancelLoad()}.
338 * This will always be called from the process's main thread.
339 *
340 * @return Returns <tt>false</tt> if the task could not be canceled,
341 * typically because it has already completed normally, or
342 * because {@link #startLoading()} hasn't been called; returns
343 * <tt>true</tt> otherwise. When <tt>true</tt> is returned, the task
344 * is still running and the {@link OnLoadCanceledListener} will be called
345 * when the task completes.
346 */
347 protected boolean onCancelLoad() {
348 return false;
349 }
350
351 /**
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500352 * Force an asynchronous load. Unlike {@link #startLoading()} this will ignore a previously
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800353 * loaded data set and load a new one. This simply calls through to the
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800354 * implementation's {@link #onForceLoad()}. You generally should only call this
355 * when the loader is started -- that is, {@link #isStarted()} returns true.
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800356 *
Dianne Hackborn247fe742011-01-08 17:25:57 -0800357 * <p>Must be called from the process's main thread.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500358 */
Dianne Hackborn247fe742011-01-08 17:25:57 -0800359 public void forceLoad() {
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800360 onForceLoad();
361 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500362
363 /**
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800364 * Subclasses must implement this to take care of requests to {@link #forceLoad()}.
Dianne Hackborn247fe742011-01-08 17:25:57 -0800365 * This will always be called from the process's main thread.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500366 */
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800367 protected void onForceLoad() {
368 }
369
370 /**
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700371 * This function will normally be called for you automatically by
372 * {@link android.app.LoaderManager} when the associated fragment/activity
373 * is being stopped. When using a Loader with {@link android.app.LoaderManager},
374 * you <em>must not</em> call this method yourself, or you will conflict
375 * with its management of the Loader.
376 *
377 * <p>Stops delivery of updates until the next time {@link #startLoading()} is called.
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800378 * Implementations should <em>not</em> invalidate their data at this point --
379 * clients are still free to use the last data the loader reported. They will,
380 * however, typically stop reporting new data if the data changes; they can
381 * still monitor for changes, but must not report them to the client until and
382 * if {@link #startLoading()} is later called.
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800383 *
384 * <p>This updates the Loader's internal state so that
385 * {@link #isStarted()} will return the correct
386 * value, and then calls the implementation's {@link #onStopLoading()}.
387 *
Dianne Hackborn247fe742011-01-08 17:25:57 -0800388 * <p>Must be called from the process's main thread.
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800389 */
Dianne Hackborn247fe742011-01-08 17:25:57 -0800390 public void stopLoading() {
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800391 mStarted = false;
392 onStopLoading();
393 }
394
395 /**
396 * Subclasses must implement this to take care of stopping their loader,
397 * as per {@link #stopLoading()}. This is not called by clients directly,
398 * but as a result of a call to {@link #stopLoading()}.
Dianne Hackborn247fe742011-01-08 17:25:57 -0800399 * This will always be called from the process's main thread.
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800400 */
401 protected void onStopLoading() {
402 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500403
404 /**
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700405 * This function will normally be called for you automatically by
406 * {@link android.app.LoaderManager} when restarting a Loader. When using
407 * a Loader with {@link android.app.LoaderManager},
408 * you <em>must not</em> call this method yourself, or you will conflict
409 * with its management of the Loader.
410 *
Dianne Hackborn260c3c72011-01-30 16:55:55 -0800411 * Tell the Loader that it is being abandoned. This is called prior
412 * to {@link #reset} to have it retain its current data but not report
413 * any new data.
414 */
415 public void abandon() {
416 mAbandoned = true;
417 onAbandon();
418 }
419
420 /**
421 * Subclasses implement this to take care of being abandoned. This is
422 * an optional intermediate state prior to {@link #onReset()} -- it means that
423 * the client is no longer interested in any new data from the loader,
424 * so the loader must not report any further updates. However, the
425 * loader <em>must</em> keep its last reported data valid until the final
426 * {@link #onReset()} happens. You can retrieve the current abandoned
427 * state with {@link #isAbandoned}.
428 */
Jeff Brownc64ff372013-10-09 18:50:56 -0700429 protected void onAbandon() {
Dianne Hackborn260c3c72011-01-30 16:55:55 -0800430 }
431
432 /**
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700433 * This function will normally be called for you automatically by
434 * {@link android.app.LoaderManager} when destroying a Loader. When using
435 * a Loader with {@link android.app.LoaderManager},
436 * you <em>must not</em> call this method yourself, or you will conflict
437 * with its management of the Loader.
438 *
Dianne Hackbornc9189352010-12-15 14:57:25 -0800439 * Resets the state of the Loader. The Loader should at this point free
440 * all of its resources, since it may never be called again; however, its
441 * {@link #startLoading()} may later be called at which point it must be
442 * able to start running again.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500443 *
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800444 * <p>This updates the Loader's internal state so that
445 * {@link #isStarted()} and {@link #isReset()} will return the correct
446 * values, and then calls the implementation's {@link #onReset()}.
447 *
Dianne Hackborn247fe742011-01-08 17:25:57 -0800448 * <p>Must be called from the process's main thread.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500449 */
Dianne Hackborn247fe742011-01-08 17:25:57 -0800450 public void reset() {
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800451 onReset();
452 mReset = true;
453 mStarted = false;
Dianne Hackborn260c3c72011-01-30 16:55:55 -0800454 mAbandoned = false;
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800455 mContentChanged = false;
Dianne Hackbornca614f72013-03-14 19:10:04 -0700456 mProcessingChange = false;
Dianne Hackbornc9189352010-12-15 14:57:25 -0800457 }
458
459 /**
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800460 * Subclasses must implement this to take care of resetting their loader,
461 * as per {@link #reset()}. This is not called by clients directly,
462 * but as a result of a call to {@link #reset()}.
Dianne Hackborn247fe742011-01-08 17:25:57 -0800463 * This will always be called from the process's main thread.
Dianne Hackbornc9189352010-12-15 14:57:25 -0800464 */
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800465 protected void onReset() {
Dianne Hackbornc9189352010-12-15 14:57:25 -0800466 }
Makoto Onukiec37c882010-08-02 16:57:01 -0700467
468 /**
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800469 * Take the current flag indicating whether the loader's content had
470 * changed while it was stopped. If it had, true is returned and the
471 * flag is cleared.
472 */
473 public boolean takeContentChanged() {
474 boolean res = mContentChanged;
475 mContentChanged = false;
Dianne Hackbornca614f72013-03-14 19:10:04 -0700476 mProcessingChange |= res;
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800477 return res;
478 }
Dianne Hackbornca614f72013-03-14 19:10:04 -0700479
480 /**
481 * Commit that you have actually fully processed a content change that
482 * was returned by {@link #takeContentChanged}. This is for use with
483 * {@link #rollbackContentChanged()} to handle situations where a load
484 * is cancelled. Call this when you have completely processed a load
485 * without it being cancelled.
486 */
487 public void commitContentChanged() {
488 mProcessingChange = false;
489 }
490
491 /**
492 * Report that you have abandoned the processing of a content change that
493 * was returned by {@link #takeContentChanged()} and would like to rollback
494 * to the state where there is again a pending content change. This is
495 * to handle the case where a data load due to a content change has been
496 * canceled before its data was delivered back to the loader.
497 */
498 public void rollbackContentChanged() {
499 if (mProcessingChange) {
Adam Powell4be84bb2016-04-27 13:45:38 -0700500 onContentChanged();
Dianne Hackbornca614f72013-03-14 19:10:04 -0700501 }
502 }
503
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800504 /**
505 * Called when {@link ForceLoadContentObserver} detects a change. The
506 * default implementation checks to see if the loader is currently started;
507 * if so, it simply calls {@link #forceLoad()}; otherwise, it sets a flag
508 * so that {@link #takeContentChanged()} returns true.
Makoto Onukiec37c882010-08-02 16:57:01 -0700509 *
Dianne Hackborn247fe742011-01-08 17:25:57 -0800510 * <p>Must be called from the process's main thread.
Makoto Onukiec37c882010-08-02 16:57:01 -0700511 */
512 public void onContentChanged() {
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800513 if (mStarted) {
514 forceLoad();
515 } else {
516 // This loader has been stopped, so we don't want to load
517 // new data right now... but keep track of it changing to
518 // refresh later if we start again.
519 mContentChanged = true;
520 }
Makoto Onukiec37c882010-08-02 16:57:01 -0700521 }
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800522
523 /**
524 * For debugging, converts an instance of the Loader's data class to
525 * a string that can be printed. Must handle a null data.
526 */
527 public String dataToString(D data) {
528 StringBuilder sb = new StringBuilder(64);
529 DebugUtils.buildShortClassTag(data, sb);
530 sb.append("}");
531 return sb.toString();
532 }
533
534 @Override
535 public String toString() {
536 StringBuilder sb = new StringBuilder(64);
537 DebugUtils.buildShortClassTag(this, sb);
538 sb.append(" id=");
539 sb.append(mId);
540 sb.append("}");
541 return sb.toString();
542 }
543
544 /**
545 * Print the Loader's state into the given stream.
546 *
547 * @param prefix Text to print at the front of each line.
548 * @param fd The raw file descriptor that the dump is being sent to.
549 * @param writer A PrintWriter to which the dump is to be set.
550 * @param args Additional arguments to the dump request.
551 */
552 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
553 writer.print(prefix); writer.print("mId="); writer.print(mId);
554 writer.print(" mListener="); writer.println(mListener);
Dianne Hackbornca614f72013-03-14 19:10:04 -0700555 if (mStarted || mContentChanged || mProcessingChange) {
556 writer.print(prefix); writer.print("mStarted="); writer.print(mStarted);
557 writer.print(" mContentChanged="); writer.print(mContentChanged);
558 writer.print(" mProcessingChange="); writer.println(mProcessingChange);
559 }
560 if (mAbandoned || mReset) {
561 writer.print(prefix); writer.print("mAbandoned="); writer.print(mAbandoned);
562 writer.print(" mReset="); writer.println(mReset);
563 }
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800564 }
Ian Lake1f4e67b2017-12-18 10:36:18 -0800565}