13f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey/*
23f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * Copyright (C) 2009 The Android Open Source Project
33f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *
43f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
53f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * you may not use this file except in compliance with the License.
63f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * You may obtain a copy of the License at
73f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *
83f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
93f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *
103f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
113f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
123f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * See the License for the specific language governing permissions and
143f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * limitations under the License.
153f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey */
163f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
173f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkeypackage com.android.contacts.util;
183f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
193f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkeyimport android.os.AsyncTask;
203f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
213f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkeyimport java.lang.ref.WeakReference;
223f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
233f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkeypublic abstract class WeakAsyncTask<Params, Progress, Result, WeakTarget> extends
243f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        AsyncTask<Params, Progress, Result> {
253f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    protected WeakReference<WeakTarget> mTarget;
263f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
273f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    public WeakAsyncTask(WeakTarget target) {
283f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        mTarget = new WeakReference<WeakTarget>(target);
293f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    }
303f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
313f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    /** {@inheritDoc} */
323f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    @Override
333f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    protected final void onPreExecute() {
343f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        final WeakTarget target = mTarget.get();
353f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        if (target != null) {
363f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey            this.onPreExecute(target);
373f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        }
383f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    }
393f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
403f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    /** {@inheritDoc} */
413f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    @Override
423f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    protected final Result doInBackground(Params... params) {
433f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        final WeakTarget target = mTarget.get();
443f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        if (target != null) {
453f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey            return this.doInBackground(target, params);
463f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        } else {
473f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey            return null;
483f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        }
493f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    }
503f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
513f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    /** {@inheritDoc} */
523f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    @Override
533f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    protected final void onPostExecute(Result result) {
543f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        final WeakTarget target = mTarget.get();
553f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        if (target != null) {
563f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey            this.onPostExecute(target, result);
573f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        }
583f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    }
593f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
603f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    protected void onPreExecute(WeakTarget target) {
613f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        // No default action
623f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    }
633f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
643f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    protected abstract Result doInBackground(WeakTarget target, Params... params);
653f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
663f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    protected void onPostExecute(WeakTarget target, Result result) {
673f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey        // No default action
683f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey    }
693f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey}
70