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