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 | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 215 | Activity mActivity; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 216 | boolean mStarted; |
| 217 | boolean mRetaining; |
| 218 | boolean mRetainingStarted; |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 219 | |
| 220 | boolean mCreatingLoader; |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 221 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 222 | final class LoaderInfo implements Loader.OnLoadCompleteListener<Object> { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 223 | final int mId; |
| 224 | final Bundle mArgs; |
| 225 | LoaderManager.LoaderCallbacks<Object> mCallbacks; |
| 226 | Loader<Object> mLoader; |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 227 | boolean mHaveData; |
| 228 | boolean mDeliveredData; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 229 | Object mData; |
| 230 | boolean mStarted; |
| 231 | boolean mRetaining; |
| 232 | boolean mRetainingStarted; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 233 | boolean mReportNextStart; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 234 | boolean mDestroyed; |
| 235 | boolean mListenerRegistered; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 236 | |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 237 | LoaderInfo mPendingLoader; |
| 238 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 239 | public LoaderInfo(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callbacks) { |
| 240 | mId = id; |
| 241 | mArgs = args; |
| 242 | mCallbacks = callbacks; |
| 243 | } |
| 244 | |
| 245 | void start() { |
| 246 | if (mRetaining && mRetainingStarted) { |
| 247 | // Our owner is started, but we were being retained from a |
| 248 | // previous instance in the started state... so there is really |
| 249 | // nothing to do here, since the loaders are still started. |
| 250 | mStarted = true; |
| 251 | return; |
| 252 | } |
| 253 | |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 254 | if (mStarted) { |
| 255 | // If loader already started, don't restart. |
| 256 | return; |
| 257 | } |
| 258 | |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 259 | mStarted = true; |
| 260 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 261 | if (DEBUG) Log.v(TAG, " Starting: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 262 | if (mLoader == null && mCallbacks != null) { |
| 263 | mLoader = mCallbacks.onCreateLoader(mId, mArgs); |
| 264 | } |
| 265 | if (mLoader != null) { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 266 | if (mLoader.getClass().isMemberClass() |
| 267 | && !Modifier.isStatic(mLoader.getClass().getModifiers())) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 268 | throw new IllegalArgumentException( |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 269 | "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] | 270 | + mLoader); |
| 271 | } |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 272 | if (!mListenerRegistered) { |
| 273 | mLoader.registerListener(mId, this); |
| 274 | mListenerRegistered = true; |
| 275 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 276 | mLoader.startLoading(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | void retain() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 281 | if (DEBUG) Log.v(TAG, " Retaining: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 282 | mRetaining = true; |
| 283 | mRetainingStarted = mStarted; |
| 284 | mStarted = false; |
| 285 | mCallbacks = null; |
| 286 | } |
| 287 | |
| 288 | void finishRetain() { |
| 289 | if (mRetaining) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 290 | if (DEBUG) Log.v(TAG, " Finished Retaining: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 291 | mRetaining = false; |
| 292 | if (mStarted != mRetainingStarted) { |
| 293 | if (!mStarted) { |
| 294 | // This loader was retained in a started state, but |
| 295 | // at the end of retaining everything our owner is |
| 296 | // no longer started... so make it stop. |
| 297 | stop(); |
| 298 | } |
| 299 | } |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 302 | if (mStarted && mHaveData && !mReportNextStart) { |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 303 | // This loader has retained its data, either completely across |
| 304 | // a configuration change or just whatever the last data set |
| 305 | // was after being restarted from a stop, and now at the point of |
| 306 | // finishing the retain we find we remain started, have |
| 307 | // our data, and the owner has a new callback... so |
| 308 | // let's deliver the data now. |
| 309 | callOnLoadFinished(mLoader, mData); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 313 | void reportStart() { |
| 314 | if (mStarted) { |
| 315 | if (mReportNextStart) { |
| 316 | mReportNextStart = false; |
| 317 | if (mHaveData) { |
| 318 | callOnLoadFinished(mLoader, mData); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 324 | void stop() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 325 | if (DEBUG) Log.v(TAG, " Stopping: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 326 | mStarted = false; |
Dianne Hackborn | debb2e2 | 2010-08-09 16:32:52 -0700 | [diff] [blame] | 327 | if (!mRetaining) { |
| 328 | if (mLoader != null && mListenerRegistered) { |
| 329 | // Let the loader know we're done with it |
| 330 | mListenerRegistered = false; |
| 331 | mLoader.unregisterListener(this); |
| 332 | mLoader.stopLoading(); |
| 333 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | void destroy() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 338 | if (DEBUG) Log.v(TAG, " Destroying: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 339 | mDestroyed = true; |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 340 | boolean needReset = mDeliveredData; |
| 341 | mDeliveredData = false; |
| 342 | if (mCallbacks != null && mLoader != null && mHaveData && needReset) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 343 | if (DEBUG) Log.v(TAG, " Reseting: " + this); |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 344 | String lastBecause = null; |
| 345 | if (mActivity != null) { |
| 346 | lastBecause = mActivity.mFragments.mNoTransactionsBecause; |
| 347 | mActivity.mFragments.mNoTransactionsBecause = "onLoaderReset"; |
| 348 | } |
| 349 | try { |
| 350 | mCallbacks.onLoaderReset(mLoader); |
| 351 | } finally { |
| 352 | if (mActivity != null) { |
| 353 | mActivity.mFragments.mNoTransactionsBecause = lastBecause; |
| 354 | } |
| 355 | } |
| 356 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 357 | mCallbacks = null; |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 358 | mData = null; |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 359 | mHaveData = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 360 | if (mLoader != null) { |
| 361 | if (mListenerRegistered) { |
| 362 | mListenerRegistered = false; |
| 363 | mLoader.unregisterListener(this); |
| 364 | } |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 365 | mLoader.reset(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 366 | } |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 367 | if (mPendingLoader != null) { |
| 368 | mPendingLoader.destroy(); |
| 369 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 370 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 371 | |
| 372 | @Override public void onLoadComplete(Loader<Object> loader, Object data) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 373 | if (DEBUG) Log.v(TAG, "onLoadComplete: " + this); |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 374 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 375 | if (mDestroyed) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 376 | if (DEBUG) Log.v(TAG, " Ignoring load complete -- destroyed"); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 377 | return; |
| 378 | } |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 379 | |
| 380 | if (mLoaders.get(mId) != this) { |
| 381 | // This data is not coming from the current active loader. |
| 382 | // We don't care about it. |
| 383 | if (DEBUG) Log.v(TAG, " Ignoring load complete -- not active"); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | LoaderInfo pending = mPendingLoader; |
| 388 | if (pending != null) { |
| 389 | // There is a new request pending and we were just |
| 390 | // waiting for the old one to complete before starting |
| 391 | // it. So now it is time, switch over to the new loader. |
| 392 | if (DEBUG) Log.v(TAG, " Switching to pending loader: " + pending); |
| 393 | mPendingLoader = null; |
| 394 | mLoaders.put(mId, null); |
| 395 | destroy(); |
| 396 | installLoader(pending); |
| 397 | return; |
| 398 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 399 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 400 | // Notify of the new data so the app can switch out the old data before |
| 401 | // we try to destroy it. |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 402 | if (mData != data || !mHaveData) { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 403 | mData = data; |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 404 | mHaveData = true; |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 405 | if (mStarted) { |
| 406 | callOnLoadFinished(loader, data); |
| 407 | } |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 408 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 409 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 410 | //if (DEBUG) Log.v(TAG, " onLoadFinished returned: " + this); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 411 | |
| 412 | // We have now given the application the new loader with its |
| 413 | // loaded data, so it should have stopped using the previous |
| 414 | // loader. If there is a previous loader on the inactive list, |
| 415 | // clean it up. |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 416 | LoaderInfo info = mInactiveLoaders.get(mId); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 417 | if (info != null && info != this) { |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 418 | info.mDeliveredData = false; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 419 | info.destroy(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 420 | mInactiveLoaders.remove(mId); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 421 | } |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 422 | |
Adam Powell | 3751090 | 2011-10-31 11:48:24 -0700 | [diff] [blame] | 423 | if (mActivity != null && !hasRunningLoaders()) { |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 424 | mActivity.mFragments.startPendingDeferredFragments(); |
| 425 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 426 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 427 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 428 | void callOnLoadFinished(Loader<Object> loader, Object data) { |
| 429 | if (mCallbacks != null) { |
| 430 | String lastBecause = null; |
| 431 | if (mActivity != null) { |
| 432 | lastBecause = mActivity.mFragments.mNoTransactionsBecause; |
| 433 | mActivity.mFragments.mNoTransactionsBecause = "onLoadFinished"; |
| 434 | } |
| 435 | try { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 436 | if (DEBUG) Log.v(TAG, " onLoadFinished in " + loader + ": " |
| 437 | + loader.dataToString(data)); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 438 | mCallbacks.onLoadFinished(loader, data); |
| 439 | } finally { |
| 440 | if (mActivity != null) { |
| 441 | mActivity.mFragments.mNoTransactionsBecause = lastBecause; |
| 442 | } |
| 443 | } |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 444 | mDeliveredData = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 448 | @Override |
| 449 | public String toString() { |
| 450 | StringBuilder sb = new StringBuilder(64); |
| 451 | sb.append("LoaderInfo{"); |
| 452 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 453 | sb.append(" #"); |
| 454 | sb.append(mId); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 455 | sb.append(" : "); |
| 456 | DebugUtils.buildShortClassTag(mLoader, sb); |
| 457 | sb.append("}}"); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 458 | return sb.toString(); |
| 459 | } |
| 460 | |
| 461 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 462 | writer.print(prefix); writer.print("mId="); writer.print(mId); |
| 463 | writer.print(" mArgs="); writer.println(mArgs); |
| 464 | writer.print(prefix); writer.print("mCallbacks="); writer.println(mCallbacks); |
| 465 | writer.print(prefix); writer.print("mLoader="); writer.println(mLoader); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 466 | if (mLoader != null) { |
| 467 | mLoader.dump(prefix + " ", fd, writer, args); |
| 468 | } |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 469 | if (mHaveData || mDeliveredData) { |
| 470 | writer.print(prefix); writer.print("mHaveData="); writer.print(mHaveData); |
| 471 | writer.print(" mDeliveredData="); writer.println(mDeliveredData); |
| 472 | writer.print(prefix); writer.print("mData="); writer.println(mData); |
| 473 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 474 | writer.print(prefix); writer.print("mStarted="); writer.print(mStarted); |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 475 | writer.print(" mReportNextStart="); writer.print(mReportNextStart); |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame] | 476 | writer.print(" mDestroyed="); writer.println(mDestroyed); |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 477 | writer.print(prefix); writer.print("mRetaining="); writer.print(mRetaining); |
| 478 | writer.print(" mRetainingStarted="); writer.print(mRetainingStarted); |
| 479 | writer.print(" mListenerRegistered="); writer.println(mListenerRegistered); |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 480 | if (mPendingLoader != null) { |
| 481 | writer.print(prefix); writer.println("Pending Loader "); |
| 482 | writer.print(mPendingLoader); writer.println(":"); |
| 483 | mPendingLoader.dump(prefix + " ", fd, writer, args); |
| 484 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 485 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 486 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 487 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 488 | LoaderManagerImpl(Activity activity, boolean started) { |
| 489 | mActivity = activity; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 490 | mStarted = started; |
| 491 | } |
| 492 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 493 | void updateActivity(Activity activity) { |
| 494 | mActivity = activity; |
| 495 | } |
| 496 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 497 | private LoaderInfo createLoader(int id, Bundle args, |
| 498 | LoaderManager.LoaderCallbacks<Object> callback) { |
| 499 | LoaderInfo info = new LoaderInfo(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 500 | Loader<Object> loader = callback.onCreateLoader(id, args); |
| 501 | info.mLoader = (Loader<Object>)loader; |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 502 | return info; |
| 503 | } |
| 504 | |
| 505 | private LoaderInfo createAndInstallLoader(int id, Bundle args, |
| 506 | LoaderManager.LoaderCallbacks<Object> callback) { |
| 507 | try { |
| 508 | mCreatingLoader = true; |
| 509 | LoaderInfo info = createLoader(id, args, callback); |
| 510 | installLoader(info); |
| 511 | return info; |
| 512 | } finally { |
| 513 | mCreatingLoader = false; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void installLoader(LoaderInfo info) { |
| 518 | mLoaders.put(info.mId, info); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 519 | if (mStarted) { |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 520 | // The activity will start all existing loaders in it's onStart(), |
| 521 | // so only start them here if we're past that point of the activitiy's |
| 522 | // life cycle |
| 523 | info.start(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 524 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 527 | /** |
| 528 | * Call to initialize a particular ID with a Loader. If this ID already |
| 529 | * has a Loader associated with it, it is left unchanged and any previous |
| 530 | * callbacks replaced with the newly provided ones. If there is not currently |
| 531 | * a Loader for the ID, a new one is created and started. |
| 532 | * |
| 533 | * <p>This function should generally be used when a component is initializing, |
| 534 | * to ensure that a Loader it relies on is created. This allows it to re-use |
| 535 | * an existing Loader's data if there already is one, so that for example |
| 536 | * when an {@link Activity} is re-created after a configuration change it |
| 537 | * does not need to re-create its loaders. |
| 538 | * |
| 539 | * <p>Note that in the case where an existing Loader is re-used, the |
| 540 | * <var>args</var> given here <em>will be ignored</em> because you will |
| 541 | * continue using the previous Loader. |
| 542 | * |
| 543 | * @param id A unique (to this LoaderManager instance) identifier under |
| 544 | * which to manage the new Loader. |
| 545 | * @param args Optional arguments that will be propagated to |
| 546 | * {@link LoaderCallbacks#onCreateLoader(int, Bundle) LoaderCallbacks.onCreateLoader()}. |
| 547 | * @param callback Interface implementing management of this Loader. Required. |
| 548 | * Its onCreateLoader() method will be called while inside of the function to |
| 549 | * instantiate the Loader object. |
| 550 | */ |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 551 | @SuppressWarnings("unchecked") |
| 552 | 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] | 553 | if (mCreatingLoader) { |
| 554 | throw new IllegalStateException("Called while creating a loader"); |
| 555 | } |
| 556 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 557 | LoaderInfo info = mLoaders.get(id); |
| 558 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 559 | if (DEBUG) Log.v(TAG, "initLoader in " + this + ": args=" + args); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 560 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 561 | if (info == null) { |
| 562 | // Loader doesn't already exist; create. |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 563 | info = createAndInstallLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 564 | if (DEBUG) Log.v(TAG, " Created new loader " + info); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 565 | } else { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 566 | if (DEBUG) Log.v(TAG, " Re-using existing loader " + info); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 567 | info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback; |
| 568 | } |
| 569 | |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 570 | if (info.mHaveData && mStarted) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 571 | // If the loader has already generated its data, report it now. |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 572 | info.callOnLoadFinished(info.mLoader, info.mData); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | return (Loader<D>)info.mLoader; |
| 576 | } |
| 577 | |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 578 | /** |
| 579 | * Call to re-create the Loader associated with a particular ID. If there |
| 580 | * is currently a Loader associated with this ID, it will be |
| 581 | * canceled/stopped/destroyed as appropriate. A new Loader with the given |
| 582 | * arguments will be created and its data delivered to you once available. |
| 583 | * |
| 584 | * <p>This function does some throttling of Loaders. If too many Loaders |
| 585 | * have been created for the given ID but not yet generated their data, |
| 586 | * new calls to this function will create and return a new Loader but not |
| 587 | * actually start it until some previous loaders have completed. |
| 588 | * |
| 589 | * <p>After calling this function, any previous Loaders associated with |
| 590 | * this ID will be considered invalid, and you will receive no further |
| 591 | * data updates from them. |
| 592 | * |
| 593 | * @param id A unique (to this LoaderManager instance) identifier under |
| 594 | * which to manage the new Loader. |
| 595 | * @param args Optional arguments that will be propagated to |
| 596 | * {@link LoaderCallbacks#onCreateLoader(int, Bundle) LoaderCallbacks.onCreateLoader()}. |
| 597 | * @param callback Interface implementing management of this Loader. Required. |
| 598 | * Its onCreateLoader() method will be called while inside of the function to |
| 599 | * instantiate the Loader object. |
| 600 | */ |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 601 | @SuppressWarnings("unchecked") |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 602 | 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] | 603 | if (mCreatingLoader) { |
| 604 | throw new IllegalStateException("Called while creating a loader"); |
| 605 | } |
| 606 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 607 | LoaderInfo info = mLoaders.get(id); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 608 | if (DEBUG) Log.v(TAG, "restartLoader in " + this + ": args=" + args); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 609 | if (info != null) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 610 | LoaderInfo inactive = mInactiveLoaders.get(id); |
| 611 | if (inactive != null) { |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 612 | if (info.mHaveData) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 613 | // This loader now has data... we are probably being |
| 614 | // called from within onLoadComplete, where we haven't |
| 615 | // yet destroyed the last inactive loader. So just do |
| 616 | // that now. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 617 | if (DEBUG) Log.v(TAG, " Removing last inactive loader: " + info); |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 618 | inactive.mDeliveredData = false; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 619 | inactive.destroy(); |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 620 | info.mLoader.abandon(); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 621 | mInactiveLoaders.put(id, info); |
| 622 | } else { |
| 623 | // We already have an inactive loader for this ID that we are |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 624 | // waiting for! What to do, what to do... |
| 625 | if (!info.mStarted) { |
| 626 | // The current Loader has not been started... we thus |
| 627 | // have no reason to keep it around, so bam, slam, |
| 628 | // thank-you-ma'am. |
| 629 | if (DEBUG) Log.v(TAG, " Current loader is stopped; replacing"); |
| 630 | mLoaders.put(id, null); |
| 631 | info.destroy(); |
| 632 | } else { |
| 633 | // Now we have three active loaders... we'll queue |
| 634 | // up this request to be processed once one of the other loaders |
| 635 | // finishes. |
| 636 | if (info.mPendingLoader != null) { |
| 637 | if (DEBUG) Log.v(TAG, " Removing pending loader: " + info.mPendingLoader); |
| 638 | info.mPendingLoader.destroy(); |
| 639 | info.mPendingLoader = null; |
| 640 | } |
| 641 | if (DEBUG) Log.v(TAG, " Enqueuing as new pending loader"); |
| 642 | info.mPendingLoader = createLoader(id, args, |
| 643 | (LoaderManager.LoaderCallbacks<Object>)callback); |
| 644 | return (Loader<D>)info.mPendingLoader.mLoader; |
| 645 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 646 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 647 | } else { |
| 648 | // Keep track of the previous instance of this loader so we can destroy |
| 649 | // it when the new one completes. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 650 | if (DEBUG) Log.v(TAG, " Making last loader inactive: " + info); |
Dianne Hackborn | 260c3c7 | 2011-01-30 16:55:55 -0800 | [diff] [blame] | 651 | info.mLoader.abandon(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 652 | mInactiveLoaders.put(id, info); |
| 653 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 654 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 655 | |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 656 | info = createAndInstallLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 657 | return (Loader<D>)info.mLoader; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 660 | /** |
| 661 | * Rip down, tear apart, shred to pieces a current Loader ID. After returning |
| 662 | * from this function, any Loader objects associated with this ID are |
| 663 | * destroyed. Any data associated with them is destroyed. You better not |
| 664 | * be using it when you do this. |
| 665 | * @param id Identifier of the Loader to be destroyed. |
| 666 | */ |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 667 | public void destroyLoader(int id) { |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 668 | if (mCreatingLoader) { |
| 669 | throw new IllegalStateException("Called while creating a loader"); |
| 670 | } |
| 671 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 672 | if (DEBUG) Log.v(TAG, "destroyLoader in " + this + " of " + id); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 673 | int idx = mLoaders.indexOfKey(id); |
| 674 | if (idx >= 0) { |
| 675 | LoaderInfo info = mLoaders.valueAt(idx); |
| 676 | mLoaders.removeAt(idx); |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 677 | info.destroy(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 678 | } |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame] | 679 | idx = mInactiveLoaders.indexOfKey(id); |
| 680 | if (idx >= 0) { |
| 681 | LoaderInfo info = mInactiveLoaders.valueAt(idx); |
| 682 | mInactiveLoaders.removeAt(idx); |
| 683 | info.destroy(); |
| 684 | } |
Adam Powell | 3751090 | 2011-10-31 11:48:24 -0700 | [diff] [blame] | 685 | if (mActivity != null && !hasRunningLoaders()) { |
| 686 | mActivity.mFragments.startPendingDeferredFragments(); |
| 687 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 690 | /** |
| 691 | * Return the most recent Loader object associated with the |
| 692 | * given ID. |
| 693 | */ |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 694 | @SuppressWarnings("unchecked") |
| 695 | public <D> Loader<D> getLoader(int id) { |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 696 | if (mCreatingLoader) { |
| 697 | throw new IllegalStateException("Called while creating a loader"); |
| 698 | } |
| 699 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 700 | LoaderInfo loaderInfo = mLoaders.get(id); |
| 701 | if (loaderInfo != null) { |
Dianne Hackborn | 6e1bf6a | 2011-01-20 13:40:19 -0800 | [diff] [blame] | 702 | if (loaderInfo.mPendingLoader != null) { |
| 703 | return (Loader<D>)loaderInfo.mPendingLoader.mLoader; |
| 704 | } |
| 705 | return (Loader<D>)loaderInfo.mLoader; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 706 | } |
| 707 | return null; |
| 708 | } |
| 709 | |
| 710 | void doStart() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 711 | if (DEBUG) Log.v(TAG, "Starting in " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 712 | if (mStarted) { |
| 713 | RuntimeException e = new RuntimeException("here"); |
| 714 | e.fillInStackTrace(); |
| 715 | Log.w(TAG, "Called doStart when already started: " + this, e); |
| 716 | return; |
| 717 | } |
| 718 | |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 719 | mStarted = true; |
| 720 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 721 | // Call out to sub classes so they can start their loaders |
| 722 | // Let the existing loaders know that we want to be notified when a load is complete |
| 723 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 724 | mLoaders.valueAt(i).start(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 725 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | void doStop() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 729 | if (DEBUG) Log.v(TAG, "Stopping in " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 730 | if (!mStarted) { |
| 731 | RuntimeException e = new RuntimeException("here"); |
| 732 | e.fillInStackTrace(); |
| 733 | Log.w(TAG, "Called doStop when not started: " + this, e); |
| 734 | return; |
| 735 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 736 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 737 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 738 | mLoaders.valueAt(i).stop(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 739 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 740 | mStarted = false; |
| 741 | } |
| 742 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 743 | void doRetain() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 744 | if (DEBUG) Log.v(TAG, "Retaining in " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 745 | if (!mStarted) { |
| 746 | RuntimeException e = new RuntimeException("here"); |
| 747 | e.fillInStackTrace(); |
| 748 | Log.w(TAG, "Called doRetain when not started: " + this, e); |
| 749 | return; |
| 750 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 751 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 752 | mRetaining = true; |
| 753 | mStarted = false; |
| 754 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
| 755 | mLoaders.valueAt(i).retain(); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | void finishRetain() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 760 | if (mRetaining) { |
| 761 | if (DEBUG) Log.v(TAG, "Finished Retaining in " + this); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 762 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 763 | mRetaining = false; |
| 764 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
| 765 | mLoaders.valueAt(i).finishRetain(); |
| 766 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 770 | void doReportNextStart() { |
| 771 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
| 772 | mLoaders.valueAt(i).mReportNextStart = true; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | void doReportStart() { |
| 777 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
| 778 | mLoaders.valueAt(i).reportStart(); |
| 779 | } |
| 780 | } |
| 781 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 782 | void doDestroy() { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 783 | if (!mRetaining) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 784 | if (DEBUG) Log.v(TAG, "Destroying Active in " + this); |
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).destroy(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 789 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 790 | if (DEBUG) Log.v(TAG, "Destroying Inactive in " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 791 | for (int i = mInactiveLoaders.size()-1; i >= 0; i--) { |
| 792 | mInactiveLoaders.valueAt(i).destroy(); |
| 793 | } |
| 794 | mInactiveLoaders.clear(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 795 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 796 | |
| 797 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 798 | public String toString() { |
| 799 | StringBuilder sb = new StringBuilder(128); |
| 800 | sb.append("LoaderManager{"); |
| 801 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 802 | sb.append(" in "); |
| 803 | DebugUtils.buildShortClassTag(mActivity, sb); |
| 804 | sb.append("}}"); |
| 805 | return sb.toString(); |
| 806 | } |
| 807 | |
| 808 | @Override |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 809 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 810 | if (mLoaders.size() > 0) { |
| 811 | writer.print(prefix); writer.println("Active Loaders:"); |
| 812 | String innerPrefix = prefix + " "; |
| 813 | for (int i=0; i < mLoaders.size(); i++) { |
| 814 | LoaderInfo li = mLoaders.valueAt(i); |
| 815 | writer.print(prefix); writer.print(" #"); writer.print(mLoaders.keyAt(i)); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 816 | writer.print(": "); writer.println(li.toString()); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 817 | li.dump(innerPrefix, fd, writer, args); |
| 818 | } |
| 819 | } |
| 820 | if (mInactiveLoaders.size() > 0) { |
| 821 | writer.print(prefix); writer.println("Inactive Loaders:"); |
| 822 | String innerPrefix = prefix + " "; |
| 823 | for (int i=0; i < mInactiveLoaders.size(); i++) { |
| 824 | LoaderInfo li = mInactiveLoaders.valueAt(i); |
| 825 | writer.print(prefix); writer.print(" #"); writer.print(mInactiveLoaders.keyAt(i)); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 826 | writer.print(": "); writer.println(li.toString()); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 827 | li.dump(innerPrefix, fd, writer, args); |
| 828 | } |
| 829 | } |
| 830 | } |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 831 | |
| 832 | public boolean hasRunningLoaders() { |
| 833 | boolean loadersRunning = false; |
| 834 | final int count = mLoaders.size(); |
| 835 | for (int i = 0; i < count; i++) { |
| 836 | final LoaderInfo li = mLoaders.valueAt(i); |
| 837 | loadersRunning |= li.mStarted && !li.mDeliveredData; |
| 838 | } |
| 839 | return loadersRunning; |
| 840 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 841 | } |