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