diff options
Diffstat (limited to 'src/com/android/contacts/common/util/WeakAsyncTask.java')
-rw-r--r-- | src/com/android/contacts/common/util/WeakAsyncTask.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/com/android/contacts/common/util/WeakAsyncTask.java b/src/com/android/contacts/common/util/WeakAsyncTask.java new file mode 100644 index 00000000..f46e5142 --- /dev/null +++ b/src/com/android/contacts/common/util/WeakAsyncTask.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.contacts.common.util; + +import android.os.AsyncTask; + +import java.lang.ref.WeakReference; + +public abstract class WeakAsyncTask<Params, Progress, Result, WeakTarget> extends + AsyncTask<Params, Progress, Result> { + protected WeakReference<WeakTarget> mTarget; + + public WeakAsyncTask(WeakTarget target) { + mTarget = new WeakReference<WeakTarget>(target); + } + + /** {@inheritDoc} */ + @Override + protected final void onPreExecute() { + final WeakTarget target = mTarget.get(); + if (target != null) { + this.onPreExecute(target); + } + } + + /** {@inheritDoc} */ + @Override + protected final Result doInBackground(Params... params) { + final WeakTarget target = mTarget.get(); + if (target != null) { + return this.doInBackground(target, params); + } else { + return null; + } + } + + /** {@inheritDoc} */ + @Override + protected final void onPostExecute(Result result) { + final WeakTarget target = mTarget.get(); + if (target != null) { + this.onPostExecute(target, result); + } + } + + protected void onPreExecute(WeakTarget target) { + // No default action + } + + protected abstract Result doInBackground(WeakTarget target, Params... params); + + protected void onPostExecute(WeakTarget target, Result result) { + // No default action + } +} |