• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app;
18 
19 import android.content.Loader;
20 import android.os.Bundle;
21 import android.util.DebugUtils;
22 import android.util.Log;
23 import android.util.SparseArray;
24 
25 import java.io.FileDescriptor;
26 import java.io.PrintWriter;
27 import java.lang.reflect.Modifier;
28 
29 /**
30  * Interface associated with an {@link Activity} or {@link Fragment} for managing
31  * one or more {@link android.content.Loader} instances associated with it.  This
32  * helps an application manage longer-running operations in conjunction with the
33  * Activity or Fragment lifecycle; the most common use of this is with a
34  * {@link android.content.CursorLoader}, however applications are free to write
35  * their own loaders for loading other types of data.
36  *
37  * While the LoaderManager API was introduced in
38  * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
39  * at is also available for use on older platforms through
40  * {@link android.support.v4.app.FragmentActivity}.  See the blog post
41  * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
42  * Fragments For All</a> for more details.
43  *
44  * <p>As an example, here is the full implementation of a {@link Fragment}
45  * that displays a {@link android.widget.ListView} containing the results of
46  * a query against the contacts content provider.  It uses a
47  * {@link android.content.CursorLoader} to manage the query on the provider.
48  *
49  * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/LoaderCursor.java
50  *      fragment_cursor}
51  *
52  * <div class="special reference">
53  * <h3>Developer Guides</h3>
54  * <p>For more information about using loaders, read the
55  * <a href="{@docRoot}guide/topics/fundamentals/loaders.html">Loaders</a> developer guide.</p>
56  * </div>
57  *
58  * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">Support Library</a>
59  *      {@link android.support.v4.app.LoaderManager}
60  */
61 @Deprecated
62 public abstract class LoaderManager {
63     /**
64      * Callback interface for a client to interact with the manager.
65      *
66      * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">
67      *      Support Library</a> {@link android.support.v4.app.LoaderManager.LoaderCallbacks}
68      */
69     @Deprecated
70     public interface LoaderCallbacks<D> {
71         /**
72          * Instantiate and return a new Loader for the given ID.
73          *
74          * @param id The ID whose loader is to be created.
75          * @param args Any arguments supplied by the caller.
76          * @return Return a new Loader instance that is ready to start loading.
77          */
onCreateLoader(int id, Bundle args)78         public Loader<D> onCreateLoader(int id, Bundle args);
79 
80         /**
81          * Called when a previously created loader has finished its load.  Note
82          * that normally an application is <em>not</em> allowed to commit fragment
83          * transactions while in this call, since it can happen after an
84          * activity's state is saved.  See {@link FragmentManager#beginTransaction()
85          * FragmentManager.openTransaction()} for further discussion on this.
86          *
87          * <p>This function is guaranteed to be called prior to the release of
88          * the last data that was supplied for this Loader.  At this point
89          * you should remove all use of the old data (since it will be released
90          * soon), but should not do your own release of the data since its Loader
91          * owns it and will take care of that.  The Loader will take care of
92          * management of its data so you don't have to.  In particular:
93          *
94          * <ul>
95          * <li> <p>The Loader will monitor for changes to the data, and report
96          * them to you through new calls here.  You should not monitor the
97          * data yourself.  For example, if the data is a {@link android.database.Cursor}
98          * and you place it in a {@link android.widget.CursorAdapter}, use
99          * the {@link android.widget.CursorAdapter#CursorAdapter(android.content.Context,
100          * android.database.Cursor, int)} constructor <em>without</em> passing
101          * in either {@link android.widget.CursorAdapter#FLAG_AUTO_REQUERY}
102          * or {@link android.widget.CursorAdapter#FLAG_REGISTER_CONTENT_OBSERVER}
103          * (that is, use 0 for the flags argument).  This prevents the CursorAdapter
104          * from doing its own observing of the Cursor, which is not needed since
105          * when a change happens you will get a new Cursor throw another call
106          * here.
107          * <li> The Loader will release the data once it knows the application
108          * is no longer using it.  For example, if the data is
109          * a {@link android.database.Cursor} from a {@link android.content.CursorLoader},
110          * you should not call close() on it yourself.  If the Cursor is being placed in a
111          * {@link android.widget.CursorAdapter}, you should use the
112          * {@link android.widget.CursorAdapter#swapCursor(android.database.Cursor)}
113          * method so that the old Cursor is not closed.
114          * </ul>
115          *
116          * @param loader The Loader that has finished.
117          * @param data The data generated by the Loader.
118          */
onLoadFinished(Loader<D> loader, D data)119         public void onLoadFinished(Loader<D> loader, D data);
120 
121         /**
122          * Called when a previously created loader is being reset, and thus
123          * making its data unavailable.  The application should at this point
124          * remove any references it has to the Loader's data.
125          *
126          * @param loader The Loader that is being reset.
127          */
onLoaderReset(Loader<D> loader)128         public void onLoaderReset(Loader<D> loader);
129     }
130 
131     /**
132      * Ensures a loader is initialized and active.  If the loader doesn't
133      * already exist, one is created and (if the activity/fragment is currently
134      * started) starts the loader.  Otherwise the last created
135      * loader is re-used.
136      *
137      * <p>In either case, the given callback is associated with the loader, and
138      * will be called as the loader state changes.  If at the point of call
139      * the caller is in its started state, and the requested loader
140      * already exists and has generated its data, then
141      * callback {@link LoaderCallbacks#onLoadFinished} will
142      * be called immediately (inside of this function), so you must be prepared
143      * for this to happen.
144      *
145      * @param id A unique identifier for this loader.  Can be whatever you want.
146      * Identifiers are scoped to a particular LoaderManager instance.
147      * @param args Optional arguments to supply to the loader at construction.
148      * If a loader already exists (a new one does not need to be created), this
149      * parameter will be ignored and the last arguments continue to be used.
150      * @param callback Interface the LoaderManager will call to report about
151      * changes in the state of the loader.  Required.
152      */
initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback)153     public abstract <D> Loader<D> initLoader(int id, Bundle args,
154             LoaderManager.LoaderCallbacks<D> callback);
155 
156     /**
157      * Starts a new or restarts an existing {@link android.content.Loader} in
158      * this manager, registers the callbacks to it,
159      * and (if the activity/fragment is currently started) starts loading it.
160      * If a loader with the same id has previously been
161      * started it will automatically be destroyed when the new loader completes
162      * its work. The callback will be delivered before the old loader
163      * is destroyed.
164      *
165      * @param id A unique identifier for this loader.  Can be whatever you want.
166      * Identifiers are scoped to a particular LoaderManager instance.
167      * @param args Optional arguments to supply to the loader at construction.
168      * @param callback Interface the LoaderManager will call to report about
169      * changes in the state of the loader.  Required.
170      */
restartLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback)171     public abstract <D> Loader<D> restartLoader(int id, Bundle args,
172             LoaderManager.LoaderCallbacks<D> callback);
173 
174     /**
175      * Stops and removes the loader with the given ID.  If this loader
176      * had previously reported data to the client through
177      * {@link LoaderCallbacks#onLoadFinished(Loader, Object)}, a call
178      * will be made to {@link LoaderCallbacks#onLoaderReset(Loader)}.
179      */
destroyLoader(int id)180     public abstract void destroyLoader(int id);
181 
182     /**
183      * Return the Loader with the given id or null if no matching Loader
184      * is found.
185      */
getLoader(int id)186     public abstract <D> Loader<D> getLoader(int id);
187 
188     /**
189      * Print the LoaderManager's state into the given stream.
190      *
191      * @param prefix Text to print at the front of each line.
192      * @param fd The raw file descriptor that the dump is being sent to.
193      * @param writer A PrintWriter to which the dump is to be set.
194      * @param args Additional arguments to the dump request.
195      */
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)196     public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
197 
198     /**
199      * Control whether the framework's internal loader manager debugging
200      * logs are turned on.  If enabled, you will see output in logcat as
201      * the framework performs loader operations.
202      */
enableDebugLogging(boolean enabled)203     public static void enableDebugLogging(boolean enabled) {
204         LoaderManagerImpl.DEBUG = enabled;
205     }
206 
207     /** @hide for internal testing only */
getFragmentHostCallback()208     public FragmentHostCallback getFragmentHostCallback() { return null; }
209 }
210 
211 class LoaderManagerImpl extends LoaderManager {
212     static final String TAG = "LoaderManager";
213     static boolean DEBUG = false;
214 
215     // These are the currently active loaders.  A loader is here
216     // from the time its load is started until it has been explicitly
217     // stopped or restarted by the application.
218     final SparseArray<LoaderInfo> mLoaders = new SparseArray<LoaderInfo>(0);
219 
220     // These are previously run loaders.  This list is maintained internally
221     // to avoid destroying a loader while an application is still using it.
222     // It allows an application to restart a loader, but continue using its
223     // previously run loader until the new loader's data is available.
224     final SparseArray<LoaderInfo> mInactiveLoaders = new SparseArray<LoaderInfo>(0);
225 
226     final String mWho;
227 
228     boolean mStarted;
229     boolean mRetaining;
230     boolean mRetainingStarted;
231 
232     boolean mCreatingLoader;
233     private FragmentHostCallback mHost;
234 
235     final class LoaderInfo implements Loader.OnLoadCompleteListener<Object>,
236             Loader.OnLoadCanceledListener<Object> {
237         final int mId;
238         final Bundle mArgs;
239         LoaderManager.LoaderCallbacks<Object> mCallbacks;
240         Loader<Object> mLoader;
241         boolean mHaveData;
242         boolean mDeliveredData;
243         Object mData;
244         boolean mStarted;
245         boolean mRetaining;
246         boolean mRetainingStarted;
247         boolean mReportNextStart;
248         boolean mDestroyed;
249         boolean mListenerRegistered;
250 
251         LoaderInfo mPendingLoader;
252 
LoaderInfo(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callbacks)253         public LoaderInfo(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callbacks) {
254             mId = id;
255             mArgs = args;
256             mCallbacks = callbacks;
257         }
258 
start()259         void start() {
260             if (mRetaining && mRetainingStarted) {
261                 // Our owner is started, but we were being retained from a
262                 // previous instance in the started state...  so there is really
263                 // nothing to do here, since the loaders are still started.
264                 mStarted = true;
265                 return;
266             }
267 
268             if (mStarted) {
269                 // If loader already started, don't restart.
270                 return;
271             }
272 
273             mStarted = true;
274 
275             if (DEBUG) Log.v(TAG, "  Starting: " + this);
276             if (mLoader == null && mCallbacks != null) {
277                mLoader = mCallbacks.onCreateLoader(mId, mArgs);
278             }
279             if (mLoader != null) {
280                 if (mLoader.getClass().isMemberClass()
281                         && !Modifier.isStatic(mLoader.getClass().getModifiers())) {
282                     throw new IllegalArgumentException(
283                             "Object returned from onCreateLoader must not be a non-static inner member class: "
284                             + mLoader);
285                 }
286                 if (!mListenerRegistered) {
287                     mLoader.registerListener(mId, this);
288                     mLoader.registerOnLoadCanceledListener(this);
289                     mListenerRegistered = true;
290                 }
291                 mLoader.startLoading();
292             }
293         }
294 
retain()295         void retain() {
296             if (DEBUG) Log.v(TAG, "  Retaining: " + this);
297             mRetaining = true;
298             mRetainingStarted = mStarted;
299             mStarted = false;
300             mCallbacks = null;
301         }
302 
finishRetain()303         void finishRetain() {
304             if (mRetaining) {
305                 if (DEBUG) Log.v(TAG, "  Finished Retaining: " + this);
306                 mRetaining = false;
307                 if (mStarted != mRetainingStarted) {
308                     if (!mStarted) {
309                         // This loader was retained in a started state, but
310                         // at the end of retaining everything our owner is
311                         // no longer started...  so make it stop.
312                         stop();
313                     }
314                 }
315             }
316 
317             if (mStarted && mHaveData && !mReportNextStart) {
318                 // This loader has retained its data, either completely across
319                 // a configuration change or just whatever the last data set
320                 // was after being restarted from a stop, and now at the point of
321                 // finishing the retain we find we remain started, have
322                 // our data, and the owner has a new callback...  so
323                 // let's deliver the data now.
324                 callOnLoadFinished(mLoader, mData);
325             }
326         }
327 
reportStart()328         void reportStart() {
329             if (mStarted) {
330                 if (mReportNextStart) {
331                     mReportNextStart = false;
332                     if (mHaveData && !mRetaining) {
333                         callOnLoadFinished(mLoader, mData);
334                     }
335                 }
336             }
337         }
338 
stop()339         void stop() {
340             if (DEBUG) Log.v(TAG, "  Stopping: " + this);
341             mStarted = false;
342             if (!mRetaining) {
343                 if (mLoader != null && mListenerRegistered) {
344                     // Let the loader know we're done with it
345                     mListenerRegistered = false;
346                     mLoader.unregisterListener(this);
347                     mLoader.unregisterOnLoadCanceledListener(this);
348                     mLoader.stopLoading();
349                 }
350             }
351         }
352 
cancel()353         boolean cancel() {
354             if (DEBUG) Log.v(TAG, "  Canceling: " + this);
355             if (mStarted && mLoader != null && mListenerRegistered) {
356                 final boolean cancelLoadResult = mLoader.cancelLoad();
357                 if (!cancelLoadResult) {
358                     onLoadCanceled(mLoader);
359                 }
360                 return cancelLoadResult;
361             }
362             return false;
363         }
364 
destroy()365         void destroy() {
366             if (DEBUG) Log.v(TAG, "  Destroying: " + this);
367             mDestroyed = true;
368             boolean needReset = mDeliveredData;
369             mDeliveredData = false;
370             if (mCallbacks != null && mLoader != null && mHaveData && needReset) {
371                 if (DEBUG) Log.v(TAG, "  Reseting: " + this);
372                 String lastBecause = null;
373                 if (mHost != null) {
374                     lastBecause = mHost.mFragmentManager.mNoTransactionsBecause;
375                     mHost.mFragmentManager.mNoTransactionsBecause = "onLoaderReset";
376                 }
377                 try {
378                     mCallbacks.onLoaderReset(mLoader);
379                 } finally {
380                     if (mHost != null) {
381                         mHost.mFragmentManager.mNoTransactionsBecause = lastBecause;
382                     }
383                 }
384             }
385             mCallbacks = null;
386             mData = null;
387             mHaveData = false;
388             if (mLoader != null) {
389                 if (mListenerRegistered) {
390                     mListenerRegistered = false;
391                     mLoader.unregisterListener(this);
392                     mLoader.unregisterOnLoadCanceledListener(this);
393                 }
394                 mLoader.reset();
395             }
396             if (mPendingLoader != null) {
397                 mPendingLoader.destroy();
398             }
399         }
400 
401         @Override
onLoadCanceled(Loader<Object> loader)402         public void onLoadCanceled(Loader<Object> loader) {
403             if (DEBUG) Log.v(TAG, "onLoadCanceled: " + this);
404 
405             if (mDestroyed) {
406                 if (DEBUG) Log.v(TAG, "  Ignoring load canceled -- destroyed");
407                 return;
408             }
409 
410             if (mLoaders.get(mId) != this) {
411                 // This cancellation message is not coming from the current active loader.
412                 // We don't care about it.
413                 if (DEBUG) Log.v(TAG, "  Ignoring load canceled -- not active");
414                 return;
415             }
416 
417             LoaderInfo pending = mPendingLoader;
418             if (pending != null) {
419                 // There is a new request pending and we were just
420                 // waiting for the old one to cancel or complete before starting
421                 // it.  So now it is time, switch over to the new loader.
422                 if (DEBUG) Log.v(TAG, "  Switching to pending loader: " + pending);
423                 mPendingLoader = null;
424                 mLoaders.put(mId, null);
425                 destroy();
426                 installLoader(pending);
427             }
428         }
429 
430         @Override
onLoadComplete(Loader<Object> loader, Object data)431         public void onLoadComplete(Loader<Object> loader, Object data) {
432             if (DEBUG) Log.v(TAG, "onLoadComplete: " + this);
433 
434             if (mDestroyed) {
435                 if (DEBUG) Log.v(TAG, "  Ignoring load complete -- destroyed");
436                 return;
437             }
438 
439             if (mLoaders.get(mId) != this) {
440                 // This data is not coming from the current active loader.
441                 // We don't care about it.
442                 if (DEBUG) Log.v(TAG, "  Ignoring load complete -- not active");
443                 return;
444             }
445 
446             LoaderInfo pending = mPendingLoader;
447             if (pending != null) {
448                 // There is a new request pending and we were just
449                 // waiting for the old one to complete before starting
450                 // it.  So now it is time, switch over to the new loader.
451                 if (DEBUG) Log.v(TAG, "  Switching to pending loader: " + pending);
452                 mPendingLoader = null;
453                 mLoaders.put(mId, null);
454                 destroy();
455                 installLoader(pending);
456                 return;
457             }
458 
459             // Notify of the new data so the app can switch out the old data before
460             // we try to destroy it.
461             if (mData != data || !mHaveData) {
462                 mData = data;
463                 mHaveData = true;
464                 if (mStarted) {
465                     callOnLoadFinished(loader, data);
466                 }
467             }
468 
469             //if (DEBUG) Log.v(TAG, "  onLoadFinished returned: " + this);
470 
471             // We have now given the application the new loader with its
472             // loaded data, so it should have stopped using the previous
473             // loader.  If there is a previous loader on the inactive list,
474             // clean it up.
475             LoaderInfo info = mInactiveLoaders.get(mId);
476             if (info != null && info != this) {
477                 info.mDeliveredData = false;
478                 info.destroy();
479                 mInactiveLoaders.remove(mId);
480             }
481 
482             if (mHost != null && !hasRunningLoaders()) {
483                 mHost.mFragmentManager.startPendingDeferredFragments();
484             }
485         }
486 
callOnLoadFinished(Loader<Object> loader, Object data)487         void callOnLoadFinished(Loader<Object> loader, Object data) {
488             if (mCallbacks != null) {
489                 String lastBecause = null;
490                 if (mHost != null) {
491                     lastBecause = mHost.mFragmentManager.mNoTransactionsBecause;
492                     mHost.mFragmentManager.mNoTransactionsBecause = "onLoadFinished";
493                 }
494                 try {
495                     if (DEBUG) Log.v(TAG, "  onLoadFinished in " + loader + ": "
496                             + loader.dataToString(data));
497                     mCallbacks.onLoadFinished(loader, data);
498                 } finally {
499                     if (mHost != null) {
500                         mHost.mFragmentManager.mNoTransactionsBecause = lastBecause;
501                     }
502                 }
503                 mDeliveredData = true;
504             }
505         }
506 
507         @Override
toString()508         public String toString() {
509             StringBuilder sb = new StringBuilder(64);
510             sb.append("LoaderInfo{");
511             sb.append(Integer.toHexString(System.identityHashCode(this)));
512             sb.append(" #");
513             sb.append(mId);
514             sb.append(" : ");
515             DebugUtils.buildShortClassTag(mLoader, sb);
516             sb.append("}}");
517             return sb.toString();
518         }
519 
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)520         public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
521             writer.print(prefix); writer.print("mId="); writer.print(mId);
522                     writer.print(" mArgs="); writer.println(mArgs);
523             writer.print(prefix); writer.print("mCallbacks="); writer.println(mCallbacks);
524             writer.print(prefix); writer.print("mLoader="); writer.println(mLoader);
525             if (mLoader != null) {
526                 mLoader.dump(prefix + "  ", fd, writer, args);
527             }
528             if (mHaveData || mDeliveredData) {
529                 writer.print(prefix); writer.print("mHaveData="); writer.print(mHaveData);
530                         writer.print("  mDeliveredData="); writer.println(mDeliveredData);
531                 writer.print(prefix); writer.print("mData="); writer.println(mData);
532             }
533             writer.print(prefix); writer.print("mStarted="); writer.print(mStarted);
534                     writer.print(" mReportNextStart="); writer.print(mReportNextStart);
535                     writer.print(" mDestroyed="); writer.println(mDestroyed);
536             writer.print(prefix); writer.print("mRetaining="); writer.print(mRetaining);
537                     writer.print(" mRetainingStarted="); writer.print(mRetainingStarted);
538                     writer.print(" mListenerRegistered="); writer.println(mListenerRegistered);
539             if (mPendingLoader != null) {
540                 writer.print(prefix); writer.println("Pending Loader ");
541                         writer.print(mPendingLoader); writer.println(":");
542                 mPendingLoader.dump(prefix + "  ", fd, writer, args);
543             }
544         }
545     }
546 
LoaderManagerImpl(String who, FragmentHostCallback host, boolean started)547     LoaderManagerImpl(String who, FragmentHostCallback host, boolean started) {
548         mWho = who;
549         mHost = host;
550         mStarted = started;
551     }
552 
updateHostController(FragmentHostCallback host)553     void updateHostController(FragmentHostCallback host) {
554         mHost = host;
555     }
556 
getFragmentHostCallback()557     public FragmentHostCallback getFragmentHostCallback() {
558         return mHost;
559     }
560 
createLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callback)561     private LoaderInfo createLoader(int id, Bundle args,
562             LoaderManager.LoaderCallbacks<Object> callback) {
563         LoaderInfo info = new LoaderInfo(id, args,  (LoaderManager.LoaderCallbacks<Object>)callback);
564         Loader<Object> loader = callback.onCreateLoader(id, args);
565         info.mLoader = (Loader<Object>)loader;
566         return info;
567     }
568 
createAndInstallLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callback)569     private LoaderInfo createAndInstallLoader(int id, Bundle args,
570             LoaderManager.LoaderCallbacks<Object> callback) {
571         try {
572             mCreatingLoader = true;
573             LoaderInfo info = createLoader(id, args, callback);
574             installLoader(info);
575             return info;
576         } finally {
577             mCreatingLoader = false;
578         }
579     }
580 
installLoader(LoaderInfo info)581     void installLoader(LoaderInfo info) {
582         mLoaders.put(info.mId, info);
583         if (mStarted) {
584             // The activity will start all existing loaders in it's onStart(),
585             // so only start them here if we're past that point of the activitiy's
586             // life cycle
587             info.start();
588         }
589     }
590 
591     /**
592      * Call to initialize a particular ID with a Loader.  If this ID already
593      * has a Loader associated with it, it is left unchanged and any previous
594      * callbacks replaced with the newly provided ones.  If there is not currently
595      * a Loader for the ID, a new one is created and started.
596      *
597      * <p>This function should generally be used when a component is initializing,
598      * to ensure that a Loader it relies on is created.  This allows it to re-use
599      * an existing Loader's data if there already is one, so that for example
600      * when an {@link Activity} is re-created after a configuration change it
601      * does not need to re-create its loaders.
602      *
603      * <p>Note that in the case where an existing Loader is re-used, the
604      * <var>args</var> given here <em>will be ignored</em> because you will
605      * continue using the previous Loader.
606      *
607      * @param id A unique (to this LoaderManager instance) identifier under
608      * which to manage the new Loader.
609      * @param args Optional arguments that will be propagated to
610      * {@link LoaderCallbacks#onCreateLoader(int, Bundle) LoaderCallbacks.onCreateLoader()}.
611      * @param callback Interface implementing management of this Loader.  Required.
612      * Its onCreateLoader() method will be called while inside of the function to
613      * instantiate the Loader object.
614      */
615     @SuppressWarnings("unchecked")
initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback)616     public <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) {
617         if (mCreatingLoader) {
618             throw new IllegalStateException("Called while creating a loader");
619         }
620 
621         LoaderInfo info = mLoaders.get(id);
622 
623         if (DEBUG) Log.v(TAG, "initLoader in " + this + ": args=" + args);
624 
625         if (info == null) {
626             // Loader doesn't already exist; create.
627             info = createAndInstallLoader(id, args,  (LoaderManager.LoaderCallbacks<Object>)callback);
628             if (DEBUG) Log.v(TAG, "  Created new loader " + info);
629         } else {
630             if (DEBUG) Log.v(TAG, "  Re-using existing loader " + info);
631             info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback;
632         }
633 
634         if (info.mHaveData && mStarted) {
635             // If the loader has already generated its data, report it now.
636             info.callOnLoadFinished(info.mLoader, info.mData);
637         }
638 
639         return (Loader<D>)info.mLoader;
640     }
641 
642     /**
643      * Call to re-create the Loader associated with a particular ID.  If there
644      * is currently a Loader associated with this ID, it will be
645      * canceled/stopped/destroyed as appropriate.  A new Loader with the given
646      * arguments will be created and its data delivered to you once available.
647      *
648      * <p>This function does some throttling of Loaders.  If too many Loaders
649      * have been created for the given ID but not yet generated their data,
650      * new calls to this function will create and return a new Loader but not
651      * actually start it until some previous loaders have completed.
652      *
653      * <p>After calling this function, any previous Loaders associated with
654      * this ID will be considered invalid, and you will receive no further
655      * data updates from them.
656      *
657      * @param id A unique (to this LoaderManager instance) identifier under
658      * which to manage the new Loader.
659      * @param args Optional arguments that will be propagated to
660      * {@link LoaderCallbacks#onCreateLoader(int, Bundle) LoaderCallbacks.onCreateLoader()}.
661      * @param callback Interface implementing management of this Loader.  Required.
662      * Its onCreateLoader() method will be called while inside of the function to
663      * instantiate the Loader object.
664      */
665     @SuppressWarnings("unchecked")
restartLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback)666     public <D> Loader<D> restartLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) {
667         if (mCreatingLoader) {
668             throw new IllegalStateException("Called while creating a loader");
669         }
670 
671         LoaderInfo info = mLoaders.get(id);
672         if (DEBUG) Log.v(TAG, "restartLoader in " + this + ": args=" + args);
673         if (info != null) {
674             LoaderInfo inactive = mInactiveLoaders.get(id);
675             if (inactive != null) {
676                 if (info.mHaveData) {
677                     // This loader now has data...  we are probably being
678                     // called from within onLoadComplete, where we haven't
679                     // yet destroyed the last inactive loader.  So just do
680                     // that now.
681                     if (DEBUG) Log.v(TAG, "  Removing last inactive loader: " + info);
682                     inactive.mDeliveredData = false;
683                     inactive.destroy();
684                     info.mLoader.abandon();
685                     mInactiveLoaders.put(id, info);
686                 } else {
687                     // We already have an inactive loader for this ID that we are
688                     // waiting for! Try to cancel; if this returns true then the task is still
689                     // running and we have more work to do.
690                     if (!info.cancel()) {
691                         // The current Loader has not been started or was successfully canceled,
692                         // we thus have no reason to keep it around. Remove it and a new
693                         // LoaderInfo will be created below.
694                         if (DEBUG) Log.v(TAG, "  Current loader is stopped; replacing");
695                         mLoaders.put(id, null);
696                         info.destroy();
697                     } else {
698                         // Now we have three active loaders... we'll queue
699                         // up this request to be processed once one of the other loaders
700                         // finishes.
701                         if (DEBUG) Log.v(TAG,
702                                 "  Current loader is running; configuring pending loader");
703                         if (info.mPendingLoader != null) {
704                             if (DEBUG) Log.v(TAG, "  Removing pending loader: " + info.mPendingLoader);
705                             info.mPendingLoader.destroy();
706                             info.mPendingLoader = null;
707                         }
708                         if (DEBUG) Log.v(TAG, "  Enqueuing as new pending loader");
709                         info.mPendingLoader = createLoader(id, args,
710                                 (LoaderManager.LoaderCallbacks<Object>)callback);
711                         return (Loader<D>)info.mPendingLoader.mLoader;
712                     }
713                 }
714             } else {
715                 // Keep track of the previous instance of this loader so we can destroy
716                 // it when the new one completes.
717                 if (DEBUG) Log.v(TAG, "  Making last loader inactive: " + info);
718                 info.mLoader.abandon();
719                 mInactiveLoaders.put(id, info);
720             }
721         }
722 
723         info = createAndInstallLoader(id, args,  (LoaderManager.LoaderCallbacks<Object>)callback);
724         return (Loader<D>)info.mLoader;
725     }
726 
727     /**
728      * Rip down, tear apart, shred to pieces a current Loader ID.  After returning
729      * from this function, any Loader objects associated with this ID are
730      * destroyed.  Any data associated with them is destroyed.  You better not
731      * be using it when you do this.
732      * @param id Identifier of the Loader to be destroyed.
733      */
destroyLoader(int id)734     public void destroyLoader(int id) {
735         if (mCreatingLoader) {
736             throw new IllegalStateException("Called while creating a loader");
737         }
738 
739         if (DEBUG) Log.v(TAG, "destroyLoader in " + this + " of " + id);
740         int idx = mLoaders.indexOfKey(id);
741         if (idx >= 0) {
742             LoaderInfo info = mLoaders.valueAt(idx);
743             mLoaders.removeAt(idx);
744             info.destroy();
745         }
746         idx = mInactiveLoaders.indexOfKey(id);
747         if (idx >= 0) {
748             LoaderInfo info = mInactiveLoaders.valueAt(idx);
749             mInactiveLoaders.removeAt(idx);
750             info.destroy();
751         }
752         if (mHost != null && !hasRunningLoaders()) {
753             mHost.mFragmentManager.startPendingDeferredFragments();
754         }
755     }
756 
757     /**
758      * Return the most recent Loader object associated with the
759      * given ID.
760      */
761     @SuppressWarnings("unchecked")
getLoader(int id)762     public <D> Loader<D> getLoader(int id) {
763         if (mCreatingLoader) {
764             throw new IllegalStateException("Called while creating a loader");
765         }
766 
767         LoaderInfo loaderInfo = mLoaders.get(id);
768         if (loaderInfo != null) {
769             if (loaderInfo.mPendingLoader != null) {
770                 return (Loader<D>)loaderInfo.mPendingLoader.mLoader;
771             }
772             return (Loader<D>)loaderInfo.mLoader;
773         }
774         return null;
775     }
776 
doStart()777     void doStart() {
778         if (DEBUG) Log.v(TAG, "Starting in " + this);
779         if (mStarted) {
780             RuntimeException e = new RuntimeException("here");
781             e.fillInStackTrace();
782             Log.w(TAG, "Called doStart when already started: " + this, e);
783             return;
784         }
785 
786         mStarted = true;
787 
788         // Call out to sub classes so they can start their loaders
789         // Let the existing loaders know that we want to be notified when a load is complete
790         for (int i = mLoaders.size()-1; i >= 0; i--) {
791             mLoaders.valueAt(i).start();
792         }
793     }
794 
doStop()795     void doStop() {
796         if (DEBUG) Log.v(TAG, "Stopping in " + this);
797         if (!mStarted) {
798             RuntimeException e = new RuntimeException("here");
799             e.fillInStackTrace();
800             Log.w(TAG, "Called doStop when not started: " + this, e);
801             return;
802         }
803 
804         for (int i = mLoaders.size()-1; i >= 0; i--) {
805             mLoaders.valueAt(i).stop();
806         }
807         mStarted = false;
808     }
809 
doRetain()810     void doRetain() {
811         if (DEBUG) Log.v(TAG, "Retaining in " + this);
812         if (!mStarted) {
813             RuntimeException e = new RuntimeException("here");
814             e.fillInStackTrace();
815             Log.w(TAG, "Called doRetain when not started: " + this, e);
816             return;
817         }
818 
819         mRetaining = true;
820         mStarted = false;
821         for (int i = mLoaders.size()-1; i >= 0; i--) {
822             mLoaders.valueAt(i).retain();
823         }
824     }
825 
finishRetain()826     void finishRetain() {
827         if (mRetaining) {
828             if (DEBUG) Log.v(TAG, "Finished Retaining in " + this);
829 
830             mRetaining = false;
831             for (int i = mLoaders.size()-1; i >= 0; i--) {
832                 mLoaders.valueAt(i).finishRetain();
833             }
834         }
835     }
836 
doReportNextStart()837     void doReportNextStart() {
838         for (int i = mLoaders.size()-1; i >= 0; i--) {
839             mLoaders.valueAt(i).mReportNextStart = true;
840         }
841     }
842 
doReportStart()843     void doReportStart() {
844         for (int i = mLoaders.size()-1; i >= 0; i--) {
845             mLoaders.valueAt(i).reportStart();
846         }
847     }
848 
doDestroy()849     void doDestroy() {
850         if (!mRetaining) {
851             if (DEBUG) Log.v(TAG, "Destroying Active in " + this);
852             for (int i = mLoaders.size()-1; i >= 0; i--) {
853                 mLoaders.valueAt(i).destroy();
854             }
855             mLoaders.clear();
856         }
857 
858         if (DEBUG) Log.v(TAG, "Destroying Inactive in " + this);
859         for (int i = mInactiveLoaders.size()-1; i >= 0; i--) {
860             mInactiveLoaders.valueAt(i).destroy();
861         }
862         mInactiveLoaders.clear();
863         mHost = null;
864     }
865 
866     @Override
toString()867     public String toString() {
868         StringBuilder sb = new StringBuilder(128);
869         sb.append("LoaderManager{");
870         sb.append(Integer.toHexString(System.identityHashCode(this)));
871         sb.append(" in ");
872         DebugUtils.buildShortClassTag(mHost, sb);
873         sb.append("}}");
874         return sb.toString();
875     }
876 
877     @Override
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)878     public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
879         if (mLoaders.size() > 0) {
880             writer.print(prefix); writer.println("Active Loaders:");
881             String innerPrefix = prefix + "    ";
882             for (int i=0; i < mLoaders.size(); i++) {
883                 LoaderInfo li = mLoaders.valueAt(i);
884                 writer.print(prefix); writer.print("  #"); writer.print(mLoaders.keyAt(i));
885                         writer.print(": "); writer.println(li.toString());
886                 li.dump(innerPrefix, fd, writer, args);
887             }
888         }
889         if (mInactiveLoaders.size() > 0) {
890             writer.print(prefix); writer.println("Inactive Loaders:");
891             String innerPrefix = prefix + "    ";
892             for (int i=0; i < mInactiveLoaders.size(); i++) {
893                 LoaderInfo li = mInactiveLoaders.valueAt(i);
894                 writer.print(prefix); writer.print("  #"); writer.print(mInactiveLoaders.keyAt(i));
895                         writer.print(": "); writer.println(li.toString());
896                 li.dump(innerPrefix, fd, writer, args);
897             }
898         }
899     }
900 
hasRunningLoaders()901     public boolean hasRunningLoaders() {
902         boolean loadersRunning = false;
903         final int count = mLoaders.size();
904         for (int i = 0; i < count; i++) {
905             final LoaderInfo li = mLoaders.valueAt(i);
906             loadersRunning |= li.mStarted && !li.mDeliveredData;
907         }
908         return loadersRunning;
909     }
910 }
911