package com.appsimobile.appsii; import android.app.Application; import android.content.Loader; import android.os.Bundle; import android.support.v4.util.DebugUtils; import android.support.v4.util.SparseArrayCompat; import android.util.Log; import java.io.FileDescriptor; import java.io.PrintWriter; import java.lang.reflect.Modifier; /** * Created by nmartens on 08/01/16. */ public class LoaderManagerImpl extends LoaderManager { static final String TAG = "LoaderManager"; static boolean DEBUG = true; // These are the currently active loaders. A loader is here // from the time its load is started until it has been explicitly // stopped or restarted by the application. final SparseArrayCompat<LoaderInfo> mLoaders = new SparseArrayCompat<LoaderInfo>(); // These are previously run loaders. This list is maintained internally // to avoid destroying a loader while an application is still using it. // It allows an application to restart a loader, but continue using its // previously run loader until the new loader's data is available. final SparseArrayCompat<LoaderInfo> mInactiveLoaders = new SparseArrayCompat<LoaderInfo>(); final String mWho; Application mActivity; boolean mStarted; boolean mRetaining; boolean mRetainingStarted; boolean mCreatingLoader; public LoaderManagerImpl(String who, Application activity, boolean started) { mWho = who; mActivity = activity; mStarted = started; } /** * Call to initialize a particular ID with a Loader. If this ID already * has a Loader associated with it, it is left unchanged and any previous * callbacks replaced with the newly provided ones. If there is not currently * a Loader for the ID, a new one is created and started. * <p/> * <p>This function should generally be used when a component is initializing, * to ensure that a Loader it relies on is created. This allows it to re-use * an existing Loader's data if there already is one, so that for example * when an {@link android.app.Activity} is re-created after a configuration change it * does not need to re-create its loaders. * <p/> * <p>Note that in the case where an existing Loader is re-used, the * <var>args</var> given here <em>will be ignored</em> because you will * continue using the previous Loader. * * @param id A unique (to this LoaderManager instance) identifier under * which to manage the new Loader. * @param args Optional arguments that will be propagated to * {@link LoaderCallbacks#onCreateLoader(int, android.os.Bundle) LoaderCallbacks * .onCreateLoader()}. * @param callback Interface implementing management of this Loader. Required. * Its onCreateLoader() method will be called while inside of the function to * instantiate the Loader object. */ @SuppressWarnings("unchecked") public <D> Loader<D> initLoader(int id, Bundle args, LoaderCallbacks<D> callback) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); } LoaderInfo info = mLoaders.get(id); if (DEBUG) Log.v(TAG, "initLoader in " + this + ": args=" + args); if (info == null) { // Loader doesn't already exist; create. info = createAndInstallLoader(id, args, (LoaderCallbacks<Object>) callback); if (DEBUG) Log.v(TAG, " Created new loader " + info); } else { if (DEBUG) Log.v(TAG, " Re-using existing loader " + info); info.mCallbacks = (LoaderCallbacks<Object>) callback; } if (info.mHaveData && mStarted) { // If the loader has already generated its data, report it now. info.callOnLoadFinished(info.mLoader, info.mData); } return (Loader<D>) info.mLoader; } private LoaderInfo createAndInstallLoader(int id, Bundle args, LoaderCallbacks<Object> callback) { try { mCreatingLoader = true; LoaderInfo info = createLoader(id, args, callback); installLoader(info); return info; } finally { mCreatingLoader = false; } } private LoaderInfo createLoader(int id, Bundle args, LoaderCallbacks<Object> callback) { LoaderInfo info = new LoaderInfo(id, args, callback); Loader<Object> loader = callback.onCreateLoader(id, args); info.mLoader = loader; return info; } void installLoader(LoaderInfo info) { mLoaders.put(info.mId, info); if (mStarted) { // The activity will start all existing loaders in it's onStart(), // so only start them here if we're past that point of the activitiy's // life cycle info.start(); } } /** * Call to re-create the Loader associated with a particular ID. If there * is currently a Loader associated with this ID, it will be * canceled/stopped/destroyed as appropriate. A new Loader with the given * arguments will be created and its data delivered to you once available. * <p/> * <p>This function does some throttling of Loaders. If too many Loaders * have been created for the given ID but not yet generated their data, * new calls to this function will create and return a new Loader but not * actually start it until some previous loaders have completed. * <p/> * <p>After calling this function, any previous Loaders associated with * this ID will be considered invalid, and you will receive no further * data updates from them. * * @param id A unique (to this LoaderManager instance) identifier under * which to manage the new Loader. * @param args Optional arguments that will be propagated to * {@link LoaderCallbacks#onCreateLoader(int, Bundle) LoaderCallbacks * .onCreateLoader()}. * @param callback Interface implementing management of this Loader. Required. * Its onCreateLoader() method will be called while inside of the function to * instantiate the Loader object. */ @SuppressWarnings("unchecked") public <D> Loader<D> restartLoader(int id, Bundle args, LoaderCallbacks<D> callback) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); } LoaderInfo info = mLoaders.get(id); if (DEBUG) Log.v(TAG, "restartLoader in " + this + ": args=" + args); if (info != null) { LoaderInfo inactive = mInactiveLoaders.get(id); if (inactive != null) { if (info.mHaveData) { // This loader now has data... we are probably being // called from within onLoadComplete, where we haven't // yet destroyed the last inactive loader. So just do // that now. if (DEBUG) Log.v(TAG, " Removing last inactive loader: " + info); inactive.mDeliveredData = false; inactive.destroy(); info.mLoader.abandon(); mInactiveLoaders.put(id, info); } else { // We already have an inactive loader for this ID that we are // waiting for! What to do, what to do... if (!info.mStarted) { // The current Loader has not been started... we thus // have no reason to keep it around, so bam, slam, // thank-you-ma'am. if (DEBUG) Log.v(TAG, " Current loader is stopped; replacing"); mLoaders.put(id, null); info.destroy(); } else { // Now we have three active loaders... we'll queue // up this request to be processed once one of the other loaders // finishes. if (info.mPendingLoader != null) { if (DEBUG) { Log.v(TAG, " Removing pending loader: " + info.mPendingLoader); } info.mPendingLoader.destroy(); info.mPendingLoader = null; } if (DEBUG) Log.v(TAG, " Enqueuing as new pending loader"); info.mPendingLoader = createLoader(id, args, (LoaderCallbacks<Object>) callback); return (Loader<D>) info.mPendingLoader.mLoader; } } } else { // Keep track of the previous instance of this loader so we can destroy // it when the new one completes. if (DEBUG) Log.v(TAG, " Making last loader inactive: " + info); info.mLoader.abandon(); mInactiveLoaders.put(id, info); } } info = createAndInstallLoader(id, args, (LoaderCallbacks<Object>) callback); return (Loader<D>) info.mLoader; } /** * Rip down, tear apart, shred to pieces a current Loader ID. After returning * from this function, any Loader objects associated with this ID are * destroyed. Any data associated with them is destroyed. You better not * be using it when you do this. * * @param id Identifier of the Loader to be destroyed. */ public void destroyLoader(int id) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); } if (DEBUG) Log.v(TAG, "destroyLoader in " + this + " of " + id); int idx = mLoaders.indexOfKey(id); if (idx >= 0) { LoaderInfo info = mLoaders.valueAt(idx); mLoaders.removeAt(idx); info.destroy(); } idx = mInactiveLoaders.indexOfKey(id); if (idx >= 0) { LoaderInfo info = mInactiveLoaders.valueAt(idx); mInactiveLoaders.removeAt(idx); info.destroy(); } } /** * Return the most recent Loader object associated with the * given ID. */ @SuppressWarnings("unchecked") public <D> Loader<D> getLoader(int id) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); } LoaderInfo loaderInfo = mLoaders.get(id); if (loaderInfo != null) { if (loaderInfo.mPendingLoader != null) { return (Loader<D>) loaderInfo.mPendingLoader.mLoader; } return (Loader<D>) loaderInfo.mLoader; } return null; } @Override public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { if (mLoaders.size() > 0) { writer.print(prefix); writer.println("Active Loaders:"); String innerPrefix = prefix + " "; for (int i = 0; i < mLoaders.size(); i++) { LoaderInfo li = mLoaders.valueAt(i); writer.print(prefix); writer.print(" #"); writer.print(mLoaders.keyAt(i)); writer.print(": "); writer.println(li.toString()); li.dump(innerPrefix, fd, writer, args); } } if (mInactiveLoaders.size() > 0) { writer.print(prefix); writer.println("Inactive Loaders:"); String innerPrefix = prefix + " "; for (int i = 0; i < mInactiveLoaders.size(); i++) { LoaderInfo li = mInactiveLoaders.valueAt(i); writer.print(prefix); writer.print(" #"); writer.print(mInactiveLoaders.keyAt(i)); writer.print(": "); writer.println(li.toString()); li.dump(innerPrefix, fd, writer, args); } } } @Override public boolean hasRunningLoaders() { boolean loadersRunning = false; final int count = mLoaders.size(); for (int i = 0; i < count; i++) { final LoaderInfo li = mLoaders.valueAt(i); loadersRunning |= li.mStarted && !li.mDeliveredData; } return loadersRunning; } public boolean isStarted() { return mStarted; } void doStart() { if (DEBUG) Log.v(TAG, "Starting in " + this); if (mStarted) { RuntimeException e = new RuntimeException("here"); e.fillInStackTrace(); Log.w(TAG, "Called doStart when already started: " + this, e); return; } mStarted = true; // Call out to sub classes so they can start their loaders // Let the existing loaders know that we want to be notified when a load is complete for (int i = mLoaders.size() - 1; i >= 0; i--) { mLoaders.valueAt(i).start(); } // added by me doReportStart(); } void doReportStart() { for (int i = mLoaders.size() - 1; i >= 0; i--) { mLoaders.valueAt(i).reportStart(); } } void doStop() { if (DEBUG) Log.v(TAG, "Stopping in " + this); if (!mStarted) { RuntimeException e = new RuntimeException("here"); e.fillInStackTrace(); Log.w(TAG, "Called doStop when not started: " + this, e); return; } for (int i = mLoaders.size() - 1; i >= 0; i--) { mLoaders.valueAt(i).stop(); } mStarted = false; // added by me doReportNextStart(); } void doReportNextStart() { for (int i = mLoaders.size() - 1; i >= 0; i--) { mLoaders.valueAt(i).mReportNextStart = true; } } void doRetain() { if (DEBUG) Log.v(TAG, "Retaining in " + this); if (!mStarted) { RuntimeException e = new RuntimeException("here"); e.fillInStackTrace(); Log.w(TAG, "Called doRetain when not started: " + this, e); return; } mRetaining = true; mStarted = false; for (int i = mLoaders.size() - 1; i >= 0; i--) { mLoaders.valueAt(i).retain(); } } void finishRetain() { if (mRetaining) { if (DEBUG) Log.v(TAG, "Finished Retaining in " + this); mRetaining = false; for (int i = mLoaders.size() - 1; i >= 0; i--) { mLoaders.valueAt(i).finishRetain(); } } } void doDestroy() { if (!mRetaining) { if (DEBUG) Log.v(TAG, "Destroying Active in " + this); for (int i = mLoaders.size() - 1; i >= 0; i--) { mLoaders.valueAt(i).destroy(); } mLoaders.clear(); } if (DEBUG) Log.v(TAG, "Destroying Inactive in " + this); for (int i = mInactiveLoaders.size() - 1; i >= 0; i--) { mInactiveLoaders.valueAt(i).destroy(); } mInactiveLoaders.clear(); } @Override public String toString() { StringBuilder sb = new StringBuilder(128); sb.append("LoaderManager{"); sb.append(Integer.toHexString(System.identityHashCode(this))); sb.append(" in "); DebugUtils.buildShortClassTag(mActivity, sb); sb.append("}}"); return sb.toString(); } final class LoaderInfo implements Loader.OnLoadCompleteListener<Object> { final int mId; final Bundle mArgs; LoaderCallbacks<Object> mCallbacks; Loader<Object> mLoader; boolean mHaveData; boolean mDeliveredData; Object mData; boolean mStarted; boolean mRetaining; boolean mRetainingStarted; boolean mReportNextStart; boolean mDestroyed; boolean mListenerRegistered; LoaderInfo mPendingLoader; public LoaderInfo(int id, Bundle args, LoaderCallbacks<Object> callbacks) { mId = id; mArgs = args; mCallbacks = callbacks; } void start() { if (mRetaining && mRetainingStarted) { // Our owner is started, but we were being retained from a // previous instance in the started state... so there is really // nothing to do here, since the loaders are still started. mStarted = true; return; } if (mStarted) { // If loader already started, don't restart. return; } mStarted = true; if (DEBUG) Log.v(TAG, " Starting: " + this); if (mLoader == null && mCallbacks != null) { mLoader = mCallbacks.onCreateLoader(mId, mArgs); } if (mLoader != null) { if (mLoader.getClass().isMemberClass() && !Modifier.isStatic(mLoader.getClass().getModifiers())) { throw new IllegalArgumentException( "Object returned from onCreateLoader must not be a non-static inner " + "member class: " + mLoader); } if (!mListenerRegistered) { mLoader.registerListener(mId, this); mListenerRegistered = true; } mLoader.startLoading(); } } void retain() { if (DEBUG) Log.v(TAG, " Retaining: " + this); mRetaining = true; mRetainingStarted = mStarted; mStarted = false; mCallbacks = null; } void finishRetain() { if (mRetaining) { if (DEBUG) Log.v(TAG, " Finished Retaining: " + this); mRetaining = false; if (mStarted != mRetainingStarted) { if (!mStarted) { // This loader was retained in a started state, but // at the end of retaining everything our owner is // no longer started... so make it stop. stop(); } } } if (mStarted && mHaveData && !mReportNextStart) { // This loader has retained its data, either completely across // a configuration change or just whatever the last data set // was after being restarted from a stop, and now at the point of // finishing the retain we find we remain started, have // our data, and the owner has a new callback... so // let's deliver the data now. callOnLoadFinished(mLoader, mData); } } void stop() { if (DEBUG) Log.v(TAG, " Stopping: " + this); mStarted = false; if (!mRetaining) { if (mLoader != null && mListenerRegistered) { // Let the loader know we're done with it mListenerRegistered = false; mLoader.unregisterListener(this); mLoader.stopLoading(); } } } void callOnLoadFinished(Loader<Object> loader, Object data) { if (mCallbacks != null) { if (DEBUG) { Log.v(TAG, " onLoadFinished in " + loader + ": " + loader.dataToString(data)); } mCallbacks.onLoadFinished(loader, data); mDeliveredData = true; } } void reportStart() { if (mStarted) { if (mReportNextStart) { mReportNextStart = false; if (mHaveData) { callOnLoadFinished(mLoader, mData); } } } } void destroy() { if (DEBUG) Log.v(TAG, " Destroying: " + this); mDestroyed = true; boolean needReset = mDeliveredData; mDeliveredData = false; if (mCallbacks != null && mLoader != null && mHaveData && needReset) { if (DEBUG) Log.v(TAG, " Reseting: " + this); mCallbacks.onLoaderReset(mLoader); } mCallbacks = null; mData = null; mHaveData = false; if (mLoader != null) { if (mListenerRegistered) { mListenerRegistered = false; mLoader.unregisterListener(this); } mLoader.reset(); } if (mPendingLoader != null) { mPendingLoader.destroy(); } } @Override public void onLoadComplete(Loader<Object> loader, Object data) { if (DEBUG) Log.v(TAG, "onLoadComplete: " + this); if (mDestroyed) { if (DEBUG) Log.v(TAG, " Ignoring load complete -- destroyed"); return; } if (mLoaders.get(mId) != this) { // This data is not coming from the current active loader. // We don't care about it. if (DEBUG) Log.v(TAG, " Ignoring load complete -- not active"); return; } LoaderInfo pending = mPendingLoader; if (pending != null) { // There is a new request pending and we were just // waiting for the old one to complete before starting // it. So now it is time, switch over to the new loader. if (DEBUG) Log.v(TAG, " Switching to pending loader: " + pending); mPendingLoader = null; mLoaders.put(mId, null); destroy(); installLoader(pending); return; } // Notify of the new data so the app can switch out the old data before // we try to destroy it. if (mData != data || !mHaveData) { mData = data; mHaveData = true; if (mStarted) { callOnLoadFinished(loader, data); } } //if (DEBUG) Log.v(TAG, " onLoadFinished returned: " + this); // We have now given the application the new loader with its // loaded data, so it should have stopped using the previous // loader. If there is a previous loader on the inactive list, // clean it up. LoaderInfo info = mInactiveLoaders.get(mId); if (info != null && info != this) { info.mDeliveredData = false; info.destroy(); mInactiveLoaders.remove(mId); } } public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { writer.print(prefix); writer.print("mId="); writer.print(mId); writer.print(" mArgs="); writer.println(mArgs); writer.print(prefix); writer.print("mCallbacks="); writer.println(mCallbacks); writer.print(prefix); writer.print("mLoader="); writer.println(mLoader); if (mLoader != null) { mLoader.dump(prefix + " ", fd, writer, args); } if (mHaveData || mDeliveredData) { writer.print(prefix); writer.print("mHaveData="); writer.print(mHaveData); writer.print(" mDeliveredData="); writer.println(mDeliveredData); writer.print(prefix); writer.print("mData="); writer.println(mData); } writer.print(prefix); writer.print("mStarted="); writer.print(mStarted); writer.print(" mReportNextStart="); writer.print(mReportNextStart); writer.print(" mDestroyed="); writer.println(mDestroyed); writer.print(prefix); writer.print("mRetaining="); writer.print(mRetaining); writer.print(" mRetainingStarted="); writer.print(mRetainingStarted); writer.print(" mListenerRegistered="); writer.println(mListenerRegistered); if (mPendingLoader != null) { writer.print(prefix); writer.println("Pending Loader "); writer.print(mPendingLoader); writer.println(":"); mPendingLoader.dump(prefix + " ", fd, writer, args); } } @Override public String toString() { StringBuilder sb = new StringBuilder(64); sb.append("LoaderInfo{"); sb.append(Integer.toHexString(System.identityHashCode(this))); sb.append(" #"); sb.append(mId); sb.append(" : "); DebugUtils.buildShortClassTag(mLoader, sb); sb.append("}}"); return sb.toString(); } } }