Copying (and modifying) some files from packages/experimental
Change-Id: I00ba3c5b05df4320db51cc659fd84f6b37486d6d
diff --git a/src/android/app/patterns/AsyncTaskLoader.java b/src/android/app/patterns/AsyncTaskLoader.java
index 01b3e24..84effaf 100644
--- a/src/android/app/patterns/AsyncTaskLoader.java
+++ b/src/android/app/patterns/AsyncTaskLoader.java
@@ -21,7 +21,7 @@
/**
* Abstract Loader that provides an {@link AsyncTask} to do the work.
- *
+ *
* @param <D> the data type to be loaded.
*/
public abstract class AsyncTaskLoader<D> extends Loader<D> {
@@ -35,15 +35,48 @@
/* Runs on the UI thread */
@Override
protected void onPostExecute(D data) {
- AsyncTaskLoader.this.onLoadComplete(data);
+ AsyncTaskLoader.this.dispatchOnLoadComplete(data);
}
}
+ private LoadListTask mTask;
+
public AsyncTaskLoader(Context context) {
super(context);
}
/**
+ * Force an asynchronous load. Unlike {@link #startLoading()} this will ignore a previously
+ * loaded data set and load a new one.
+ */
+ @Override
+ public void forceLoad() {
+ mTask = new LoadListTask();
+ mTask.execute((Void[]) null);
+ }
+
+ /**
+ * Attempt to cancel the current load task. See {@link AsyncTask#cancel(boolean)}
+ * for more info.
+ *
+ * @return <tt>false</tt> if the task could not be cancelled,
+ * typically because it has already completed normally, or
+ * because {@link startLoading()} hasn't been called, and
+ * <tt>true</tt> otherwise
+ */
+ public boolean cancelLoad() {
+ if (mTask != null) {
+ return mTask.cancel(false);
+ }
+ return false;
+ }
+
+ private void dispatchOnLoadComplete(D data) {
+ mTask = null;
+ onLoadComplete(data);
+ }
+
+ /**
* Called on a worker thread to perform the actual load. Implementions should not deliver the
* results directly, but should return them from this this method and deliver them from
* {@link #onPostExecute()}