Android Open Source - RateBeer-Mobile Better Async Task






From Project

Back to project page RateBeer-Mobile.

License

The source code is released under:

GNU General Public License

If you think the Android project RateBeer-Mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright (c) 2009 Matthias Kppler
 */* w w w . java 2  s  .c  o m*/
 * 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.github.droidfu.concurrent;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Window;

import com.github.droidfu.DroidFuApplication;
import com.github.droidfu.activities.BetterActivity;

public abstract class BetterAsyncTask<ParameterT, ProgressT, ReturnT> extends
        AsyncTask<ParameterT, ProgressT, ReturnT> {

    private DroidFuApplication appContext;

    private Exception error;

    private boolean contextIsDroidFuActivity, isTitleProgressEnabled,
            isTitleProgressIndeterminateEnabled = true;

    private String callerId;

    private BetterAsyncTaskCallable<ParameterT, ProgressT, ReturnT> callable;

    public BetterAsyncTask(Context context) {

        if (!(context.getApplicationContext() instanceof DroidFuApplication)) {
            throw new IllegalArgumentException(
                    "context bound to this task must be a DroidFu context (DroidFuApplication)");
        }
        this.appContext = (DroidFuApplication) context.getApplicationContext();
        this.callerId = context.getClass().getCanonicalName();
        this.contextIsDroidFuActivity = context instanceof BetterActivity;

        appContext.setActiveContext(callerId, context);

        if (contextIsDroidFuActivity) {
            int windowFeatures = ((BetterActivity) context).getWindowFeatures();
            if (Window.FEATURE_PROGRESS == (Window.FEATURE_PROGRESS & windowFeatures)) {
                this.isTitleProgressEnabled = true;
            } else if (Window.FEATURE_INDETERMINATE_PROGRESS == (Window.FEATURE_INDETERMINATE_PROGRESS & windowFeatures)) {
                this.isTitleProgressIndeterminateEnabled = true;
            }
        }
    }

    protected Context getCallingContext() {
        try {
            Context caller = (Context) appContext.getActiveContext(callerId);
            if (caller == null || !this.callerId.equals(caller.getClass().getCanonicalName())
                    || (caller instanceof Activity && ((Activity) caller).isFinishing())) {
                // the context that started this task has died and/or was
                // replaced with a different one
                return null;
            }
            return caller;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected final void onPreExecute() {
        Context context = getCallingContext();
        if (context == null) {
            Log.d(BetterAsyncTask.class.getSimpleName(), "skipping pre-exec handler for task "
                    + hashCode() + " (context is null)");
            cancel(true);
            return;
        }

        if (contextIsDroidFuActivity) {
            Activity activity = (Activity) context;
            if (isTitleProgressEnabled) {
                activity.setProgressBarVisibility(true);
            } else if (isTitleProgressIndeterminateEnabled) {
                activity.setProgressBarIndeterminateVisibility(true);
            }
        }
        before(context);
    }

    protected void before(Context context) {
    }

    @Override
    protected final ReturnT doInBackground(ParameterT... params) {
        ReturnT result = null;
        Context context = getCallingContext();
        try {
            result = doCheckedInBackground(context, params);
        } catch (Exception e) {
            this.error = e;
        }
        return result;
    }

    protected ReturnT doCheckedInBackground(Context context, ParameterT... params) throws Exception {
        if (callable != null) {
            return callable.call(this);
        }
        return null;
    }

    protected abstract void handleError(Context context, Exception error);

    @Override
    protected final void onPostExecute(ReturnT result) {
        Context context = getCallingContext();
        if (context == null) {
            Log.d(BetterAsyncTask.class.getSimpleName(), "skipping post-exec handler for task "
                    + hashCode() + " (context is null)");
            return;
        }

        if (contextIsDroidFuActivity) {
            Activity activity = (Activity) context;
            if (isTitleProgressEnabled) {
                activity.setProgressBarVisibility(false);
            } else if (isTitleProgressIndeterminateEnabled) {
                activity.setProgressBarIndeterminateVisibility(false);
            }
        }

        if (failed()) {
            handleError(context, error);
        } else {
            after(context, result);
        }
    }

    protected abstract void after(Context context, ReturnT result);

    public boolean failed() {
        return error != null;
    }

    public void setCallable(BetterAsyncTaskCallable<ParameterT, ProgressT, ReturnT> callable) {
        this.callable = callable;
    }

}




Java Source Code List

com.github.droidfu.DroidFuApplication.java
com.github.droidfu.DroidFu.java
com.github.droidfu.activities.BetterActivityHelper.java
com.github.droidfu.activities.BetterActivity.java
com.github.droidfu.activities.BetterDefaultActivity.java
com.github.droidfu.activities.BetterListActivity.java
com.github.droidfu.adapters.ListAdapterWithProgress.java
com.github.droidfu.concurrent.BetterAsyncTaskCallable.java
com.github.droidfu.concurrent.BetterAsyncTask.java
com.github.droidfu.services.BetterService.java
dk.moerks.ratebeermobile.BeerMail.java
dk.moerks.ratebeermobile.BeerView.java
dk.moerks.ratebeermobile.Home.java
dk.moerks.ratebeermobile.MailAction.java
dk.moerks.ratebeermobile.MailView.java
dk.moerks.ratebeermobile.PlaceView.java
dk.moerks.ratebeermobile.Places.java
dk.moerks.ratebeermobile.RateBeer.java
dk.moerks.ratebeermobile.Rate.java
dk.moerks.ratebeermobile.Rating.java
dk.moerks.ratebeermobile.Search.java
dk.moerks.ratebeermobile.Settings.java
dk.moerks.ratebeermobile.activity.BetterRBActivity.java
dk.moerks.ratebeermobile.activity.BetterRBDefaultActivity.java
dk.moerks.ratebeermobile.activity.BetterRBListActivity.java
dk.moerks.ratebeermobile.activity.RBActivity.java
dk.moerks.ratebeermobile.adapters.FeedAdapter.java
dk.moerks.ratebeermobile.adapters.MessageAdapter.java
dk.moerks.ratebeermobile.adapters.PlacesAdapter.java
dk.moerks.ratebeermobile.adapters.ReviewAdapter.java
dk.moerks.ratebeermobile.adapters.SearchAdapter.java
dk.moerks.ratebeermobile.exceptions.LocationException.java
dk.moerks.ratebeermobile.exceptions.LoginException.java
dk.moerks.ratebeermobile.exceptions.NetworkException.java
dk.moerks.ratebeermobile.exceptions.RBException.java
dk.moerks.ratebeermobile.exceptions.RBParserException.java
dk.moerks.ratebeermobile.io.NetBroker.java
dk.moerks.ratebeermobile.io.TwitterPoster.java
dk.moerks.ratebeermobile.overlays.PinOverlayItem.java
dk.moerks.ratebeermobile.receivers.BeerMailServiceReceiver.java
dk.moerks.ratebeermobile.receivers.BootReceiver.java
dk.moerks.ratebeermobile.services.BeerMailService.java
dk.moerks.ratebeermobile.services.BeerSearchHistoryProvider.java
dk.moerks.ratebeermobile.task.BarcodeLookupTask.java
dk.moerks.ratebeermobile.task.BetterRBTask.java
dk.moerks.ratebeermobile.task.DeleteBeermailTask.java
dk.moerks.ratebeermobile.task.PostTwitterStatusTask.java
dk.moerks.ratebeermobile.task.RefreshFriendFeedTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerImageTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerInfoTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerReviewsTask.java
dk.moerks.ratebeermobile.task.RetrieveBeermailTask.java
dk.moerks.ratebeermobile.task.RetrieveBeermailsTask.java
dk.moerks.ratebeermobile.task.RetrievePlacesTask.java
dk.moerks.ratebeermobile.task.RetrieveRatingTask.java
dk.moerks.ratebeermobile.task.RetrieveUserIdTask.java
dk.moerks.ratebeermobile.task.SaveRatingTask.java
dk.moerks.ratebeermobile.task.SearchTask.java
dk.moerks.ratebeermobile.task.SendBeermailTask.java
dk.moerks.ratebeermobile.task.SetDrinkingBeerIdTask.java
dk.moerks.ratebeermobile.task.SetDrinkingStatusTask.java
dk.moerks.ratebeermobile.util.BCPParser.java
dk.moerks.ratebeermobile.util.RBJSONParser.java
dk.moerks.ratebeermobile.util.RBParser.java
dk.moerks.ratebeermobile.util.StringUtils.java
dk.moerks.ratebeermobile.vo.BeerInfo.java
dk.moerks.ratebeermobile.vo.Feed.java
dk.moerks.ratebeermobile.vo.MessageHeader.java
dk.moerks.ratebeermobile.vo.Message.java
dk.moerks.ratebeermobile.vo.PlacesInfo.java
dk.moerks.ratebeermobile.vo.RatingData.java
dk.moerks.ratebeermobile.vo.Review.java
dk.moerks.ratebeermobile.vo.SearchResult.java