Back to project page blink.
The source code is released under:
Apache License
If you think the Android project blink listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.nashlincoln.blink.content; /*w w w .j a v a 2 s. co m*/ import android.content.AsyncTaskLoader; import android.content.Context; import android.util.Log; import com.nashlincoln.blink.event.Event; import java.util.Observable; import java.util.Observer; /** * Created by nash on 8/28/14. */ public abstract class ModelLoader<T> extends AsyncTaskLoader<T> { private static final String TAG = "ModelLoader"; private T mData; public ModelLoader(Context context) { super(context); } public abstract T fetch(); public abstract String getKey(); @Override public T loadInBackground() { // Log.d(TAG, "loadInBackground"); try { // check memory cache // check disk cache mData = fetch(); Event.observe(getKey(), mObserver); } catch (Exception e) { Log.w(TAG, "exception in fetch: ", e); } return mData; } @Override public void deliverResult(T data) { // Log.d(TAG, "deliverResult: " + isReset() + " " + isStarted()); if (isReset()) { if (data != null) { onReleaseResources(data); } return; } T oldData = mData; mData = data; if (isStarted()) { super.deliverResult(data); } if (oldData != null) { // onReleaseResources(oldData); } } /** * Handles a request to start the Loader. */ @Override protected void onStartLoading() { // Log.d(TAG, "onStartLoading"); if (mData != null) { deliverResult(mData); } if (takeContentChanged() || mData == null) { forceLoad(); } } /** * Handles a request to stop the Loader. */ @Override protected void onStopLoading() { // Log.d(TAG, "onStopLoading"); cancelLoad(); } /** * Handles a request to cancel a load. */ @Override public void onCanceled(T stores) { // Log.d(TAG, "onCanceled"); super.onCanceled(stores); onReleaseResources(stores); } /** * Handles a request to completely reset the Loader. */ @Override protected void onReset() { // Log.d(TAG, "onReset"); super.onReset(); onStopLoading(); if (mData != null) { onReleaseResources(mData); mData = null; } } protected void onReleaseResources(T data) { Event.ignore(getKey(), mObserver); // For a simple List<> there is nothing to do. For something // like a Cursor, we would close it here. } private Observer mObserver = new Observer() { @Override public void update(Observable observable, Object data) { Log.d(TAG, "update"); onContentChanged(); } }; }