Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts.common.util;
     18 
     19 import android.os.AsyncTask;
     20 
     21 import java.lang.ref.WeakReference;
     22 
     23 public abstract class WeakAsyncTask<Params, Progress, Result, WeakTarget> extends
     24         AsyncTask<Params, Progress, Result> {
     25     protected WeakReference<WeakTarget> mTarget;
     26 
     27     public WeakAsyncTask(WeakTarget target) {
     28         mTarget = new WeakReference<WeakTarget>(target);
     29     }
     30 
     31     /** {@inheritDoc} */
     32     @Override
     33     protected final void onPreExecute() {
     34         final WeakTarget target = mTarget.get();
     35         if (target != null) {
     36             this.onPreExecute(target);
     37         }
     38     }
     39 
     40     /** {@inheritDoc} */
     41     @Override
     42     protected final Result doInBackground(Params... params) {
     43         final WeakTarget target = mTarget.get();
     44         if (target != null) {
     45             return this.doInBackground(target, params);
     46         } else {
     47             return null;
     48         }
     49     }
     50 
     51     /** {@inheritDoc} */
     52     @Override
     53     protected final void onPostExecute(Result result) {
     54         final WeakTarget target = mTarget.get();
     55         if (target != null) {
     56             this.onPostExecute(target, result);
     57         }
     58     }
     59 
     60     protected void onPreExecute(WeakTarget target) {
     61         // No default action
     62     }
     63 
     64     protected abstract Result doInBackground(WeakTarget target, Params... params);
     65 
     66     protected void onPostExecute(WeakTarget target, Result result) {
     67         // No default action
     68     }
     69 }
     70