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