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 | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 21 | import android.util.Log; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 22 | import android.util.SparseArray; |
| 23 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 24 | import java.io.FileDescriptor; |
| 25 | import java.io.PrintWriter; |
| 26 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 27 | /** |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 28 | * Interface associated with an {@link Activity} or {@link Fragment} for managing |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 29 | * one or more {@link android.content.Loader} instances associated with it. This |
| 30 | * helps an application manage longer-running operations in conjunction with the |
| 31 | * Activity or Fragment lifecycle; the most common use of this is with a |
| 32 | * {@link android.content.CursorLoader}, however applications are free to write |
| 33 | * their own loaders for loading other types of data. |
| 34 | * |
| 35 | * <p>As an example, here is the full implementation of a {@link Fragment} |
| 36 | * that displays a {@link android.widget.ListView} containing the results of |
| 37 | * a query against the contacts content provider. It uses a |
| 38 | * {@link android.content.CursorLoader} to manage the query on the provider. |
| 39 | * |
| 40 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentListCursorLoader.java |
| 41 | * fragment_cursor} |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 42 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 43 | public abstract class LoaderManager { |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Callback interface for a client to interact with the manager. |
| 46 | */ |
| 47 | public interface LoaderCallbacks<D> { |
| 48 | /** |
| 49 | * Instantiate and return a new Loader for the given ID. |
| 50 | * |
| 51 | * @param id The ID whose loader is to be created. |
| 52 | * @param args Any arguments supplied by the caller. |
| 53 | * @return Return a new Loader instance that is ready to start loading. |
| 54 | */ |
| 55 | public Loader<D> onCreateLoader(int id, Bundle args); |
| 56 | |
| 57 | /** |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 58 | * Called when a previously created loader has finished its load. Note |
| 59 | * that normally an application is <em>not</em> allowed to commit fragment |
| 60 | * transactions while in this call, since it can happen after an |
| 61 | * activity's state is saved. See {@link FragmentManager#openTransaction() |
| 62 | * FragmentManager.openTransaction()} for further discussion on this. |
| 63 | * |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 64 | * <p>This function is guaranteed to be called prior to the release of |
| 65 | * the last data that was supplied for this Loader. At this point |
| 66 | * you should remove all use of the old data (since it will be released |
| 67 | * soon), but should not do your own release of the data since its Loader |
| 68 | * owns it and will take care of that. The Loader will take care of |
| 69 | * management of its data so you don't have to. In particular: |
| 70 | * |
| 71 | * <ul> |
| 72 | * <li> <p>The Loader will monitor for changes to the data, and report |
| 73 | * them to you through new calls here. You should not monitor the |
| 74 | * data yourself. For example, if the data is a {@link android.database.Cursor} |
| 75 | * and you place it in a {@link android.widget.CursorAdapter}, use |
| 76 | * the {@link android.widget.CursorAdapter#CursorAdapter(android.content.Context, |
| 77 | * android.database.Cursor, int)} constructor <em>without</em> passing |
| 78 | * in either {@link android.widget.CursorAdapter#FLAG_AUTO_REQUERY} |
| 79 | * or {@link android.widget.CursorAdapter#FLAG_REGISTER_CONTENT_OBSERVER} |
| 80 | * (that is, use 0 for the flags argument). This prevents the CursorAdapter |
| 81 | * from doing its own observing of the Cursor, which is not needed since |
| 82 | * when a change happens you will get a new Cursor throw another call |
| 83 | * here. |
| 84 | * <li> The Loader will release the data once it knows the application |
| 85 | * is no longer using it. For example, if the data is |
| 86 | * a {@link android.database.Cursor} from a {@link android.content.CursorLoader}, |
| 87 | * you should not call close() on it yourself. If the Cursor is being placed in a |
| 88 | * {@link android.widget.CursorAdapter}, you should use the |
| 89 | * {@link android.widget.CursorAdapter#swapCursor(android.database.Cursor)} |
| 90 | * method so that the old Cursor is not closed. |
| 91 | * </ul> |
| 92 | * |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 93 | * @param loader The Loader that has finished. |
| 94 | * @param data The data generated by the Loader. |
| 95 | */ |
| 96 | public void onLoadFinished(Loader<D> loader, D data); |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Called when a previously created loader is being reset, and thus |
| 100 | * making its data unavailable. The application should at this point |
| 101 | * remove any references it has to the Loader's data. |
| 102 | * |
| 103 | * @param loader The Loader that is being reset. |
| 104 | */ |
| 105 | public void onLoaderReset(Loader<D> loader); |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Ensures a loader is initialized and active. If the loader doesn't |
| 110 | * already exist, one is created and (if the activity/fragment is currently |
| 111 | * started) starts the loader. Otherwise the last created |
| 112 | * loader is re-used. |
| 113 | * |
| 114 | * <p>In either case, the given callback is associated with the loader, and |
| 115 | * will be called as the loader state changes. If at the point of call |
| 116 | * the caller is in its started state, and the requested loader |
| 117 | * already exists and has generated its data, then |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 118 | * callback {@link LoaderCallbacks#onLoadFinished} will |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 119 | * be called immediately (inside of this function), so you must be prepared |
| 120 | * for this to happen. |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 121 | * |
| 122 | * @param id A unique identifier for this loader. Can be whatever you want. |
| 123 | * Identifiers are scoped to a particular LoaderManager instance. |
| 124 | * @param args Optional arguments to supply to the loader at construction. |
| 125 | * @param callback Interface the LoaderManager will call to report about |
| 126 | * changes in the state of the loader. Required. |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 127 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 128 | public abstract <D> Loader<D> initLoader(int id, Bundle args, |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 129 | LoaderManager.LoaderCallbacks<D> callback); |
| 130 | |
| 131 | /** |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 132 | * Starts a new or restarts an existing {@link android.content.Loader} in |
| 133 | * this manager, registers the callbacks to it, |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 134 | * and (if the activity/fragment is currently started) starts loading it. |
| 135 | * If a loader with the same id has previously been |
| 136 | * started it will automatically be destroyed when the new loader completes |
| 137 | * its work. The callback will be delivered before the old loader |
| 138 | * is destroyed. |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 139 | * |
| 140 | * @param id A unique identifier for this loader. Can be whatever you want. |
| 141 | * Identifiers are scoped to a particular LoaderManager instance. |
| 142 | * @param args Optional arguments to supply to the loader at construction. |
| 143 | * @param callback Interface the LoaderManager will call to report about |
| 144 | * changes in the state of the loader. Required. |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 145 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 146 | public abstract <D> Loader<D> restartLoader(int id, Bundle args, |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 147 | LoaderManager.LoaderCallbacks<D> callback); |
| 148 | |
| 149 | /** |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame^] | 150 | * Stops and removes the loader with the given ID. If this loader |
| 151 | * had previously reported data to the client through |
| 152 | * {@link LoaderCallbacks#onLoadFinished(Loader, Object)}, a call |
| 153 | * will be made to {@link LoaderCallbacks#onLoaderReset(Loader)}. |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 154 | */ |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 155 | public abstract void destroyLoader(int id); |
| 156 | |
| 157 | /** |
| 158 | * @deprecated Renamed to {@link #destroyLoader}. |
| 159 | */ |
| 160 | @Deprecated |
| 161 | public void stopLoader(int id) { |
| 162 | destroyLoader(id); |
| 163 | } |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Return the Loader with the given id or null if no matching Loader |
| 167 | * is found. |
| 168 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 169 | public abstract <D> Loader<D> getLoader(int id); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * Print the LoaderManager's state into the given stream. |
| 173 | * |
| 174 | * @param prefix Text to print at the front of each line. |
| 175 | * @param fd The raw file descriptor that the dump is being sent to. |
| 176 | * @param writer A PrintWriter to which the dump is to be set. |
| 177 | * @param args Additional arguments to the dump request. |
| 178 | */ |
| 179 | public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 182 | class LoaderManagerImpl extends LoaderManager { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 183 | static final String TAG = "LoaderManagerImpl"; |
| 184 | static final boolean DEBUG = true; |
| 185 | |
| 186 | // These are the currently active loaders. A loader is here |
| 187 | // from the time its load is started until it has been explicitly |
| 188 | // stopped or restarted by the application. |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 189 | final SparseArray<LoaderInfo> mLoaders = new SparseArray<LoaderInfo>(); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 190 | |
| 191 | // These are previously run loaders. This list is maintained internally |
| 192 | // to avoid destroying a loader while an application is still using it. |
| 193 | // It allows an application to restart a loader, but continue using its |
| 194 | // previously run loader until the new loader's data is available. |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 195 | final SparseArray<LoaderInfo> mInactiveLoaders = new SparseArray<LoaderInfo>(); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 196 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 197 | Activity mActivity; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 198 | boolean mStarted; |
| 199 | boolean mRetaining; |
| 200 | boolean mRetainingStarted; |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 201 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 202 | final class LoaderInfo implements Loader.OnLoadCompleteListener<Object> { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 203 | final int mId; |
| 204 | final Bundle mArgs; |
| 205 | LoaderManager.LoaderCallbacks<Object> mCallbacks; |
| 206 | Loader<Object> mLoader; |
| 207 | Object mData; |
| 208 | boolean mStarted; |
Dianne Hackborn | 540d3d2 | 2010-12-16 22:41:30 -0800 | [diff] [blame] | 209 | boolean mNeedReset; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 210 | boolean mRetaining; |
| 211 | boolean mRetainingStarted; |
| 212 | boolean mDestroyed; |
| 213 | boolean mListenerRegistered; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 214 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 215 | public LoaderInfo(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callbacks) { |
| 216 | mId = id; |
| 217 | mArgs = args; |
| 218 | mCallbacks = callbacks; |
| 219 | } |
| 220 | |
| 221 | void start() { |
| 222 | if (mRetaining && mRetainingStarted) { |
| 223 | // Our owner is started, but we were being retained from a |
| 224 | // previous instance in the started state... so there is really |
| 225 | // nothing to do here, since the loaders are still started. |
| 226 | mStarted = true; |
| 227 | return; |
| 228 | } |
| 229 | |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 230 | if (mStarted) { |
| 231 | // If loader already started, don't restart. |
| 232 | return; |
| 233 | } |
| 234 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 235 | if (DEBUG) Log.v(TAG, " Starting: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 236 | if (mLoader == null && mCallbacks != null) { |
| 237 | mLoader = mCallbacks.onCreateLoader(mId, mArgs); |
| 238 | } |
| 239 | if (mLoader != null) { |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 240 | if (!mListenerRegistered) { |
| 241 | mLoader.registerListener(mId, this); |
| 242 | mListenerRegistered = true; |
| 243 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 244 | mLoader.startLoading(); |
| 245 | mStarted = true; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void retain() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 250 | if (DEBUG) Log.v(TAG, " Retaining: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 251 | mRetaining = true; |
| 252 | mRetainingStarted = mStarted; |
| 253 | mStarted = false; |
| 254 | mCallbacks = null; |
| 255 | } |
| 256 | |
| 257 | void finishRetain() { |
| 258 | if (mRetaining) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 259 | if (DEBUG) Log.v(TAG, " Finished Retaining: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 260 | mRetaining = false; |
| 261 | if (mStarted != mRetainingStarted) { |
| 262 | if (!mStarted) { |
| 263 | // This loader was retained in a started state, but |
| 264 | // at the end of retaining everything our owner is |
| 265 | // no longer started... so make it stop. |
| 266 | stop(); |
| 267 | } |
| 268 | } |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if (mStarted && mData != null) { |
| 272 | // This loader has retained its data, either completely across |
| 273 | // a configuration change or just whatever the last data set |
| 274 | // was after being restarted from a stop, and now at the point of |
| 275 | // finishing the retain we find we remain started, have |
| 276 | // our data, and the owner has a new callback... so |
| 277 | // let's deliver the data now. |
| 278 | callOnLoadFinished(mLoader, mData); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | void stop() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 283 | if (DEBUG) Log.v(TAG, " Stopping: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 284 | mStarted = false; |
Dianne Hackborn | debb2e2 | 2010-08-09 16:32:52 -0700 | [diff] [blame] | 285 | if (!mRetaining) { |
| 286 | if (mLoader != null && mListenerRegistered) { |
| 287 | // Let the loader know we're done with it |
| 288 | mListenerRegistered = false; |
| 289 | mLoader.unregisterListener(this); |
| 290 | mLoader.stopLoading(); |
| 291 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | void destroy() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 296 | if (DEBUG) Log.v(TAG, " Destroying: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 297 | mDestroyed = true; |
Dianne Hackborn | 540d3d2 | 2010-12-16 22:41:30 -0800 | [diff] [blame] | 298 | if (mCallbacks != null && mLoader != null && mData != null && mNeedReset) { |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 299 | String lastBecause = null; |
| 300 | if (mActivity != null) { |
| 301 | lastBecause = mActivity.mFragments.mNoTransactionsBecause; |
| 302 | mActivity.mFragments.mNoTransactionsBecause = "onLoaderReset"; |
| 303 | } |
| 304 | try { |
| 305 | mCallbacks.onLoaderReset(mLoader); |
| 306 | } finally { |
| 307 | if (mActivity != null) { |
| 308 | mActivity.mFragments.mNoTransactionsBecause = lastBecause; |
| 309 | } |
| 310 | } |
| 311 | } |
Dianne Hackborn | 540d3d2 | 2010-12-16 22:41:30 -0800 | [diff] [blame] | 312 | mNeedReset = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 313 | mCallbacks = null; |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 314 | mData = null; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 315 | if (mLoader != null) { |
| 316 | if (mListenerRegistered) { |
| 317 | mListenerRegistered = false; |
| 318 | mLoader.unregisterListener(this); |
| 319 | } |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 320 | mLoader.reset(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 323 | |
| 324 | @Override public void onLoadComplete(Loader<Object> loader, Object data) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 325 | if (DEBUG) Log.v(TAG, "onLoadComplete: " + this + " mDestroyed=" + mDestroyed); |
| 326 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 327 | if (mDestroyed) { |
| 328 | return; |
| 329 | } |
| 330 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 331 | // Notify of the new data so the app can switch out the old data before |
| 332 | // we try to destroy it. |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 333 | mData = data; |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 334 | if (mStarted) { |
| 335 | callOnLoadFinished(loader, data); |
| 336 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 337 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 338 | if (DEBUG) Log.v(TAG, "onLoadFinished returned: " + this); |
| 339 | |
| 340 | // We have now given the application the new loader with its |
| 341 | // loaded data, so it should have stopped using the previous |
| 342 | // loader. If there is a previous loader on the inactive list, |
| 343 | // clean it up. |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 344 | LoaderInfo info = mInactiveLoaders.get(mId); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 345 | if (info != null && info != this) { |
Dianne Hackborn | 540d3d2 | 2010-12-16 22:41:30 -0800 | [diff] [blame] | 346 | info.mNeedReset = false; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 347 | info.destroy(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 348 | mInactiveLoaders.remove(mId); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 351 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 352 | void callOnLoadFinished(Loader<Object> loader, Object data) { |
| 353 | if (mCallbacks != null) { |
| 354 | String lastBecause = null; |
| 355 | if (mActivity != null) { |
| 356 | lastBecause = mActivity.mFragments.mNoTransactionsBecause; |
| 357 | mActivity.mFragments.mNoTransactionsBecause = "onLoadFinished"; |
| 358 | } |
| 359 | try { |
| 360 | mCallbacks.onLoadFinished(loader, data); |
| 361 | } finally { |
| 362 | if (mActivity != null) { |
| 363 | mActivity.mFragments.mNoTransactionsBecause = lastBecause; |
| 364 | } |
| 365 | } |
Dianne Hackborn | 540d3d2 | 2010-12-16 22:41:30 -0800 | [diff] [blame] | 366 | mNeedReset = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 370 | @Override |
| 371 | public String toString() { |
| 372 | StringBuilder sb = new StringBuilder(64); |
| 373 | sb.append("LoaderInfo{"); |
| 374 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 375 | sb.append(" #"); |
| 376 | sb.append(mId); |
| 377 | if (mArgs != null) { |
| 378 | sb.append(" "); |
| 379 | sb.append(mArgs.toString()); |
| 380 | } |
| 381 | sb.append("}"); |
| 382 | return sb.toString(); |
| 383 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 384 | |
| 385 | public String toBasicString() { |
| 386 | StringBuilder sb = new StringBuilder(64); |
| 387 | sb.append("LoaderInfo{"); |
| 388 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 389 | sb.append(" #"); |
| 390 | sb.append(mId); |
| 391 | sb.append("}"); |
| 392 | return sb.toString(); |
| 393 | } |
| 394 | |
| 395 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 396 | writer.print(prefix); writer.print("mId="); writer.print(mId); |
| 397 | writer.print(" mArgs="); writer.println(mArgs); |
| 398 | writer.print(prefix); writer.print("mCallbacks="); writer.println(mCallbacks); |
| 399 | writer.print(prefix); writer.print("mLoader="); writer.println(mLoader); |
| 400 | writer.print(prefix); writer.print("mData="); writer.println(mData); |
| 401 | writer.print(prefix); writer.print("mStarted="); writer.print(mStarted); |
| 402 | writer.print(" mRetaining="); writer.print(mRetaining); |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame^] | 403 | writer.print(" mDestroyed="); writer.println(mDestroyed); |
| 404 | writer.print(prefix); writer.print("mNeedReset="); writer.print(mNeedReset); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 405 | writer.print(" mListenerRegistered="); writer.println(mListenerRegistered); |
| 406 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 407 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 408 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 409 | LoaderManagerImpl(Activity activity, boolean started) { |
| 410 | mActivity = activity; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 411 | mStarted = started; |
| 412 | } |
| 413 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 414 | void updateActivity(Activity activity) { |
| 415 | mActivity = activity; |
| 416 | } |
| 417 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 418 | private LoaderInfo createLoader(int id, Bundle args, |
| 419 | LoaderManager.LoaderCallbacks<Object> callback) { |
| 420 | LoaderInfo info = new LoaderInfo(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
| 421 | mLoaders.put(id, info); |
| 422 | Loader<Object> loader = callback.onCreateLoader(id, args); |
| 423 | info.mLoader = (Loader<Object>)loader; |
| 424 | if (mStarted) { |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 425 | // The activity will start all existing loaders in it's onStart(), |
| 426 | // so only start them here if we're past that point of the activitiy's |
| 427 | // life cycle |
| 428 | info.start(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 429 | } |
| 430 | return info; |
| 431 | } |
| 432 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 433 | @SuppressWarnings("unchecked") |
| 434 | public <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) { |
| 435 | LoaderInfo info = mLoaders.get(id); |
| 436 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 437 | if (DEBUG) Log.v(TAG, "initLoader in " + this + ": cur=" + info); |
| 438 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 439 | if (info == null) { |
| 440 | // Loader doesn't already exist; create. |
| 441 | info = createLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
| 442 | } else { |
| 443 | info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback; |
| 444 | } |
| 445 | |
| 446 | if (info.mData != null && mStarted) { |
| 447 | // If the loader has already generated its data, report it now. |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 448 | info.callOnLoadFinished(info.mLoader, info.mData); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | return (Loader<D>)info.mLoader; |
| 452 | } |
| 453 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 454 | @SuppressWarnings("unchecked") |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 455 | public <D> Loader<D> restartLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) { |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 456 | LoaderInfo info = mLoaders.get(id); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 457 | if (DEBUG) Log.v(TAG, "restartLoader in " + this + ": cur=" + info); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 458 | if (info != null) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 459 | LoaderInfo inactive = mInactiveLoaders.get(id); |
| 460 | if (inactive != null) { |
| 461 | if (info.mData != null) { |
| 462 | // This loader now has data... we are probably being |
| 463 | // called from within onLoadComplete, where we haven't |
| 464 | // yet destroyed the last inactive loader. So just do |
| 465 | // that now. |
| 466 | if (DEBUG) Log.v(TAG, " Removing last inactive loader in " + this); |
Dianne Hackborn | 540d3d2 | 2010-12-16 22:41:30 -0800 | [diff] [blame] | 467 | inactive.mNeedReset = false; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 468 | inactive.destroy(); |
| 469 | mInactiveLoaders.put(id, info); |
| 470 | } else { |
| 471 | // We already have an inactive loader for this ID that we are |
| 472 | // waiting for! Now we have three active loaders... let's just |
| 473 | // drop the one in the middle, since we are still waiting for |
| 474 | // its result but that result is already out of date. |
| 475 | if (DEBUG) Log.v(TAG, " Removing intermediate loader in " + this); |
| 476 | info.destroy(); |
| 477 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 478 | } else { |
| 479 | // Keep track of the previous instance of this loader so we can destroy |
| 480 | // it when the new one completes. |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 481 | if (DEBUG) Log.v(TAG, " Making inactive: " + info); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 482 | mInactiveLoaders.put(id, info); |
| 483 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 484 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 485 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 486 | info = createLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
| 487 | return (Loader<D>)info.mLoader; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 490 | public void destroyLoader(int id) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 491 | if (DEBUG) Log.v(TAG, "stopLoader in " + this + " of " + id); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 492 | int idx = mLoaders.indexOfKey(id); |
| 493 | if (idx >= 0) { |
| 494 | LoaderInfo info = mLoaders.valueAt(idx); |
| 495 | mLoaders.removeAt(idx); |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 496 | info.destroy(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 497 | } |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame^] | 498 | idx = mInactiveLoaders.indexOfKey(id); |
| 499 | if (idx >= 0) { |
| 500 | LoaderInfo info = mInactiveLoaders.valueAt(idx); |
| 501 | mInactiveLoaders.removeAt(idx); |
| 502 | info.destroy(); |
| 503 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 506 | @SuppressWarnings("unchecked") |
| 507 | public <D> Loader<D> getLoader(int id) { |
| 508 | LoaderInfo loaderInfo = mLoaders.get(id); |
| 509 | if (loaderInfo != null) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 510 | return (Loader<D>)mLoaders.get(id).mLoader; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 511 | } |
| 512 | return null; |
| 513 | } |
| 514 | |
| 515 | void doStart() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 516 | if (DEBUG) Log.v(TAG, "Starting: " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 517 | if (mStarted) { |
| 518 | RuntimeException e = new RuntimeException("here"); |
| 519 | e.fillInStackTrace(); |
| 520 | Log.w(TAG, "Called doStart when already started: " + this, e); |
| 521 | return; |
| 522 | } |
| 523 | |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 524 | mStarted = true; |
| 525 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 526 | // Call out to sub classes so they can start their loaders |
| 527 | // Let the existing loaders know that we want to be notified when a load is complete |
| 528 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 529 | mLoaders.valueAt(i).start(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 530 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void doStop() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 534 | if (DEBUG) Log.v(TAG, "Stopping: " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 535 | if (!mStarted) { |
| 536 | RuntimeException e = new RuntimeException("here"); |
| 537 | e.fillInStackTrace(); |
| 538 | Log.w(TAG, "Called doStop when not started: " + this, e); |
| 539 | return; |
| 540 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 541 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 542 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 543 | mLoaders.valueAt(i).stop(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 544 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 545 | mStarted = false; |
| 546 | } |
| 547 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 548 | void doRetain() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 549 | if (DEBUG) Log.v(TAG, "Retaining: " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 550 | if (!mStarted) { |
| 551 | RuntimeException e = new RuntimeException("here"); |
| 552 | e.fillInStackTrace(); |
| 553 | Log.w(TAG, "Called doRetain when not started: " + this, e); |
| 554 | return; |
| 555 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 556 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 557 | mRetaining = true; |
| 558 | mStarted = false; |
| 559 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
| 560 | mLoaders.valueAt(i).retain(); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | void finishRetain() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 565 | if (DEBUG) Log.v(TAG, "Finished Retaining: " + this); |
| 566 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 567 | mRetaining = false; |
| 568 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
| 569 | mLoaders.valueAt(i).finishRetain(); |
| 570 | } |
| 571 | } |
| 572 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 573 | void doDestroy() { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 574 | if (!mRetaining) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 575 | if (DEBUG) Log.v(TAG, "Destroying Active: " + this); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 576 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 577 | mLoaders.valueAt(i).destroy(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 580 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 581 | if (DEBUG) Log.v(TAG, "Destroying Inactive: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 582 | for (int i = mInactiveLoaders.size()-1; i >= 0; i--) { |
| 583 | mInactiveLoaders.valueAt(i).destroy(); |
| 584 | } |
| 585 | mInactiveLoaders.clear(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 586 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 587 | |
| 588 | @Override |
| 589 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 590 | if (mLoaders.size() > 0) { |
| 591 | writer.print(prefix); writer.println("Active Loaders:"); |
| 592 | String innerPrefix = prefix + " "; |
| 593 | for (int i=0; i < mLoaders.size(); i++) { |
| 594 | LoaderInfo li = mLoaders.valueAt(i); |
| 595 | writer.print(prefix); writer.print(" #"); writer.print(mLoaders.keyAt(i)); |
| 596 | writer.print(": "); writer.println(li.toBasicString()); |
| 597 | li.dump(innerPrefix, fd, writer, args); |
| 598 | } |
| 599 | } |
| 600 | if (mInactiveLoaders.size() > 0) { |
| 601 | writer.print(prefix); writer.println("Inactive Loaders:"); |
| 602 | String innerPrefix = prefix + " "; |
| 603 | for (int i=0; i < mInactiveLoaders.size(); i++) { |
| 604 | LoaderInfo li = mInactiveLoaders.valueAt(i); |
| 605 | writer.print(prefix); writer.print(" #"); writer.print(mInactiveLoaders.keyAt(i)); |
| 606 | writer.print(": "); writer.println(li.toBasicString()); |
| 607 | li.dump(innerPrefix, fd, writer, args); |
| 608 | } |
| 609 | } |
| 610 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 611 | } |