diff options
| -rw-r--r-- | core/java/android/content/AsyncTaskLoader.java | 29 | ||||
| -rw-r--r-- | core/java/android/content/CursorLoader.java | 9 | ||||
| -rw-r--r-- | core/java/android/os/AsyncTask.java | 13 | ||||
| -rw-r--r-- | core/java/android/widget/TextView.java | 21 |
4 files changed, 57 insertions, 15 deletions
diff --git a/core/java/android/content/AsyncTaskLoader.java b/core/java/android/content/AsyncTaskLoader.java index f43921faa673..b19c0724b1e5 100644 --- a/core/java/android/content/AsyncTaskLoader.java +++ b/core/java/android/content/AsyncTaskLoader.java @@ -20,15 +20,19 @@ import android.os.AsyncTask; /** * 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> { final class LoadTask extends AsyncTask<Void, Void, D> { + + private D result; + /* Runs on a worker thread */ @Override protected D doInBackground(Void... params) { - return AsyncTaskLoader.this.loadInBackground(); + result = AsyncTaskLoader.this.loadInBackground(); + return result; } /* Runs on the UI thread */ @@ -36,6 +40,11 @@ public abstract class AsyncTaskLoader<D> extends Loader<D> { protected void onPostExecute(D data) { AsyncTaskLoader.this.dispatchOnLoadComplete(data); } + + @Override + protected void onCancelled() { + AsyncTaskLoader.this.onCancelled(result); + } } LoadTask mTask; @@ -50,6 +59,7 @@ public abstract class AsyncTaskLoader<D> extends Loader<D> { */ @Override public void forceLoad() { + cancelLoad(); mTask = new LoadTask(); mTask.execute((Void[]) null); } @@ -65,11 +75,20 @@ public abstract class AsyncTaskLoader<D> extends Loader<D> { */ public boolean cancelLoad() { if (mTask != null) { - return mTask.cancel(false); + boolean cancelled = mTask.cancel(false); + mTask = null; + return cancelled; } return false; } - + + /** + * Called if the task was canceled before it was completed. Gives the class a chance + * to properly dispose of the result. + */ + public void onCancelled(D data) { + } + void dispatchOnLoadComplete(D data) { mTask = null; deliverResult(data); @@ -78,7 +97,7 @@ public abstract class AsyncTaskLoader<D> extends Loader<D> { /** * Called on a worker thread to perform the actual load. Implementations should not deliver the * results directly, but should return them from this method, which will eventually end up - * calling deliverResult on the UI thread. If implementations need to process + * calling deliverResult on the UI thread. If implementations need to process * the results on the UI thread they may override deliverResult and do so * there. * diff --git a/core/java/android/content/CursorLoader.java b/core/java/android/content/CursorLoader.java index e1f9dcab592f..e2303940f5ec 100644 --- a/core/java/android/content/CursorLoader.java +++ b/core/java/android/content/CursorLoader.java @@ -71,7 +71,7 @@ public class CursorLoader extends AsyncTaskLoader<Cursor> { /** * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks * will be called on the UI thread. If a previous load has been completed and is still valid - * the result may be passed to the callbacks immediately. + * the result may be passed to the callbacks immediately. * * Must be called from the UI thread */ @@ -104,6 +104,13 @@ public class CursorLoader extends AsyncTaskLoader<Cursor> { } @Override + public void onCancelled(Cursor cursor) { + if (cursor != null && !cursor.isClosed()) { + cursor.close(); + } + } + + @Override public void destroy() { // Ensure the loader is stopped stopLoading(); diff --git a/core/java/android/os/AsyncTask.java b/core/java/android/os/AsyncTask.java index 823134b5cab9..832ce84f52ef 100644 --- a/core/java/android/os/AsyncTask.java +++ b/core/java/android/os/AsyncTask.java @@ -187,6 +187,17 @@ public abstract class AsyncTask<Params, Progress, Result> { }; mFuture = new FutureTask<Result>(mWorker) { + + @Override + protected void set(Result v) { + super.set(v); + if (isCancelled()) { + Message message = sHandler.obtainMessage(MESSAGE_POST_CANCEL, + new AsyncTaskResult<Result>(AsyncTask.this, (Result[]) null)); + message.sendToTarget(); + } + } + @Override protected void done() { Message message; @@ -401,7 +412,7 @@ public abstract class AsyncTask<Params, Progress, Result> { * publish updates on the UI thread while the background computation is * still running. Each call to this method will trigger the execution of * {@link #onProgressUpdate} on the UI thread. - * + * * {@link #onProgressUpdate} will note be called if the task has been * canceled. * diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index b90ac9dd8674..c186aba1cc8e 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -321,6 +321,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener this(context, attrs, com.android.internal.R.attr.textViewStyle); } + @SuppressWarnings("deprecation") public TextView(Context context, AttributeSet attrs, int defStyle) { @@ -714,7 +715,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } if (inputMethod != null) { - Class c; + Class<?> c; try { c = Class.forName(inputMethod.toString()); @@ -2335,6 +2336,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return str + "}"; } + @SuppressWarnings("hiding") public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { public SavedState createFromParcel(Parcel in) { @@ -2756,6 +2758,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return mChars[off + mStart]; } + @Override public String toString() { return new String(mChars, mStart, mLength); } @@ -3223,7 +3226,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener * * @param create If true, the extras will be created if they don't already * exist. Otherwise, null will be returned if none have been created. - * @see #setInputExtras(int)View + * @see #setInputExtras(int) * @see EditorInfo#extras * @attr ref android.R.styleable#TextView_editorExtras */ @@ -3337,7 +3340,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private static class ErrorPopup extends PopupWindow { private boolean mAbove = false; - private TextView mView; + private final TextView mView; ErrorPopup(TextView v, int width, int height) { super(v, width, height); @@ -5966,7 +5969,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private final WeakReference<TextView> mView; private byte mStatus = MARQUEE_STOPPED; - private float mScrollUnit; + private final float mScrollUnit; private float mMaxScroll; float mMaxFadeScroll; private float mGhostStart; @@ -5978,7 +5981,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener Marquee(TextView v) { final float density = v.getContext().getResources().getDisplayMetrics().density; - mScrollUnit = (MARQUEE_PIXELS_PER_SECOND * density) / (float) MARQUEE_RESOLUTION; + mScrollUnit = (MARQUEE_PIXELS_PER_SECOND * density) / MARQUEE_RESOLUTION; mView = new WeakReference<TextView>(v); } @@ -6570,6 +6573,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener super(getHandler()); } + @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (resultCode != InputMethodManager.RESULT_SHOWN) { final int len = mText.length(); @@ -6688,7 +6692,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } private static class Blink extends Handler implements Runnable { - private WeakReference<TextView> mView; + private final WeakReference<TextView> mView; private boolean mCancelled; public Blink(TextView v) { @@ -7322,6 +7326,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return false; } + @Override public boolean performLongClick() { if (super.performLongClick()) { mEatTouchRelease = true; @@ -7349,9 +7354,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private ArrayList<TextWatcher> mListeners = null; // display attributes - private TextPaint mTextPaint; + private final TextPaint mTextPaint; private boolean mUserSetTextScaleX; - private Paint mHighlightPaint; + private final Paint mHighlightPaint; private int mHighlightColor = 0xFFBBDDFF; private Layout mLayout; |