Android Open Source - lightbox-android-webservices Background Task






From Project

Back to project page lightbox-android-webservices.

License

The source code is released under:

Apache License

If you think the Android project lightbox-android-webservices 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) 2012 Lightbox/*w w  w  .  jav  a 2  s .c om*/
 *
 * 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.lightbox.android.tasks;

import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;

import android.os.Handler;

import com.lightbox.android.utils.debug.DebugLog;

/** 
 * <p>BackgroundTask is a convenient way to execute tasks on the background, and then being called back on main thread.
 * This is greatly inspired by AsyncTask, but gives us finer control over the parameters of execution (thread pool 
 * size, etc.).
 * <p>Example:
 * <pre>
 *     BackgroundTask&lt;String&gt; task = new BackgroundTask&lt;String&gt;() {
 *      &#064;Override
 *      protected void onProgressPublished(int progress) {
 *        // Show progress
 *      }
 *      &#064;Override
 *      protected void onFailed(Exception e) {
 *        // Display error message
 *      }
 *      &#064;Override
 *      protected void onCompleted(String result) {
 *        // Use the result
 *      }
 *      &#064;Override
 *      protected String doWorkInBackground() throws Exception {
 *        // In this example, we just fake a long computation with several steps
 *        for (int i = 0; i <= 10; i++) {
 *          Thread.sleep(200);
 *          publishProgress(i);
 *        }
 *        return "result";
 *      }
 *    };
 *    
 *    task.execute();
 * </pre>
 * @author Fabien Devos
 */
public abstract class BackgroundTask<TResult> {
  /** Used to tag logs */
  //@SuppressWarnings("unused")
  private static final String TAG = "BackgroundTask";
  
  private static ExecutorService sMainExecutor = new DefaultExecutor();
  private static Handler sHandler = null;
  private static ConcurrentMap<String, BackgroundTask<?>> sRunningTasks = new ConcurrentHashMap<String, BackgroundTask<?>>();

  private ExecutorService mExecutor = null;
  private FutureTask<TResult> mFutureTask;
  private String mId;
  
  //----------------------------------------------
  // Running tasks.
  
  public boolean isRunning() {
    return isRunning(mId);
  }
  
  public static boolean isRunning(String key) {
    if (key == null) { return false; }
    return sRunningTasks.get(key) != null;
  }

  public static BackgroundTask<?> getRunningTask(String key) {
    if (key == null) { return null; }
    return sRunningTasks.get(key);
  }
  
  private static void addRunningTask(BackgroundTask<?> task) {
    if (task.getId() != null) {
      sRunningTasks.put(task.getId(), task);
    }
  }
  
  private static void removeTask(String key) {
    if (key != null) {
      sRunningTasks.remove(key);
    }
  }
  
  //----------------------------------------------
  // Constructors.

  /**
   * Constructor. <strong>Must be called on main thread.</strong>
   */
  public BackgroundTask() {
    this(null);
  }

  /**
   * Constructor. <strong>Must be called on main thread.</strong>
   * @param executor the executor to use for this task execution
   */
  public BackgroundTask(ExecutorService executor) {
    if(sHandler == null) {
      sHandler = new Handler();
    }
    if(executor != null) {
      mExecutor = executor;
    } else {
      mExecutor = sMainExecutor;
    }
    
    // Create the Callable worker that will run in the background
    Callable<TResult> doWorkCallable = new Callable<TResult>() {
      @Override
      public TResult call() throws Exception {
        return doWorkInBackground();
      }
    };
    
    // Wrap it in a FutureTask to be notified of the result
    mFutureTask = new FutureTask<TResult>(doWorkCallable) {
      @Override
      protected void done() {
        if ( ! isCancelled()) {
          try {
            TResult result = get();
            callOnCompletedOnMainThread(result);
          } catch (InterruptedException e) {
            DebugLog.d(TAG, "%s", e);
            callOnFailedOnMainThread(e);
          } catch (ExecutionException e) {
            DebugLog.d(TAG, "%s", e);
            // Try to unwrap exception
            Throwable cause = e.getCause();
            if (cause != null && cause instanceof Exception) {
              callOnFailedOnMainThread((Exception) cause);              
            } else {
              callOnFailedOnMainThread(e);
            }
          } catch (CancellationException e) {
            // Silently ignore cancellation
          } catch (Exception e) {
            DebugLog.d(TAG, "%s", e);
            callOnFailedOnMainThread(e);
          } catch (Throwable t) {
            DebugLog.d(TAG, "%s", t);
            callOnFailedOnMainThread(new RuntimeException(t));
          }
        }
      }
    };
    
  }
  
  //----------------------------------------------
  // Getters and Setters
  
  public String getId() {
    return mId;
  }

  /** Must be called before {@link #execute()} */
  public void setId(String id) {
    mId = id;
  }
  
  //----------------------------------------------
  // Helpers method to run call-backs on main thread

  private void callOnCompletedOnMainThread(final TResult result) {
    sHandler.post(new Runnable() {
      @Override
      public void run() {
        removeTask(mId);
        onCompleted(result);
      }
    });
  }
  
  private void callOnFailedOnMainThread(final Exception e) {
    sHandler.post(new Runnable() {
      @Override
      public void run() {
        removeTask(mId);
        onFailed(e);
      }
    });
  }
  
  private void callOnProgressPublishedOnMainThread(final int progress, final TResult tmpResult) {
    sHandler.post(new Runnable() {
      @Override
      public void run() {
        onProgressPublished(progress, tmpResult);
      }
    });
  }
  
  //----------------------------------------------
  // API for subclasses
  
  /**
   * This method will be called in <strong>a background thread</strong>.
   * Override this method to perform your background computation, and then simply return the result.
   * You'll be called back on the main thread with the {@link #onCompleted(Object)} method.
   */
  protected abstract TResult doWorkInBackground() throws Exception;
  
  /**
   * This method will be called <strong>on the main thread</strong> if the task terminates successfully. 
   * @param result the result of the background computation.
   */
  protected abstract void onCompleted(TResult result);

  /**
   * This method will be called <strong>on the main thread</strong> if the task terminates with an error. 
   * @param e the exception that caused the termination.
   */
  protected abstract void onFailed(Exception e);

  /**
   * This method will be called <strong>on the main thread</strong> each time a progress is published. 
   * Optional. Default implementation does nothing.
   * @param progress the current progress
   * @param tmpResult the temporary result, if any
   */
  protected void onProgressPublished(int progress, TResult tmpResult) {
    // Optional callback. Do nothing by default.
  }
  
  /**
   * Call this method from {@link #doWorkInBackground()} to publish the progress of the currently running task to
   * the main thread. You will be called back in {@link #onProgressPublished(int, Object)}.
   * @param progress
   * @param tmpResult
   */
  public final void publishProgress(int progress, TResult tmpResult) {
    callOnProgressPublishedOnMainThread(progress, tmpResult);
  }
  
  //----------------------------------------------
  // Execution
  
  /**
   * Start background execution of this task, using either the provided ExecutorService, or the default one. 
   */
  public void execute() {
    addRunningTask(this);
    
    mExecutor.execute(mFutureTask);
  }

  /**
   * Cancel this task.
   */
  public void cancel() {
    removeTask(mId);
    
    mFutureTask.cancel(true);
  }
  
  /**
   * @return true if this task completed.
   */
  public boolean isDone() {
    return mFutureTask.isDone();
  }
  
}




Java Source Code List

com.lightbox.android.bitmap.BitmapFileCleanerTask.java
com.lightbox.android.bitmap.BitmapLoaderListener.java
com.lightbox.android.bitmap.BitmapLoaderTask.java
com.lightbox.android.bitmap.BitmapLoader.java
com.lightbox.android.bitmap.BitmapSize.java
com.lightbox.android.bitmap.BitmapSource.java
com.lightbox.android.bitmap.BitmapUtils.java
com.lightbox.android.cache.AbstractCache.java
com.lightbox.android.cache.ApiCache.java
com.lightbox.android.cache.BitmapCache.java
com.lightbox.android.cache.Cache.java
com.lightbox.android.data.ClearAndSaveBatchTask.java
com.lightbox.android.data.Data.java
com.lightbox.android.data.DatabaseCleanerTask.java
com.lightbox.android.data.DatabaseHelper.java
com.lightbox.android.data.DeleteBatchTask.java
com.lightbox.android.data.SaveBatchTask.java
com.lightbox.android.io.RandomAccessFileOutputStream.java
com.lightbox.android.lifecycle.LifeCycleListener.java
com.lightbox.android.lifecycle.ManagedLifeCycleActivity.java
com.lightbox.android.location.LocationHelper.java
com.lightbox.android.location.LocationListener.java
com.lightbox.android.network.HttpHelper.java
com.lightbox.android.network.NetworkUtils.java
com.lightbox.android.operations.AbstractOperation.java
com.lightbox.android.operations.CachedOperation.java
com.lightbox.android.operations.DeleteOperation.java
com.lightbox.android.operations.FailureOperation.java
com.lightbox.android.operations.ModificationNetworkOnlyOperation.java
com.lightbox.android.operations.NetworkOnlyOperation.java
com.lightbox.android.operations.OperationListener.java
com.lightbox.android.operations.OperationTask.java
com.lightbox.android.operations.Operation.java
com.lightbox.android.operations.Retrievable.java
com.lightbox.android.operations.RetrieveOperation.java
com.lightbox.android.operations.SaveOperation.java
com.lightbox.android.operations.Updatable.java
com.lightbox.android.tasks.BackgroundTaskWeak.java
com.lightbox.android.tasks.BackgroundTask.java
com.lightbox.android.tasks.DefaultExecutor.java
com.lightbox.android.utils.AndroidUtils.java
com.lightbox.android.utils.Base64.java
com.lightbox.android.utils.IntentUtils.java
com.lightbox.android.utils.MediaUtils.java
com.lightbox.android.utils.ResUtils.java
com.lightbox.android.utils.debug.DebugLifeCycleListener.java
com.lightbox.android.utils.debug.DebugLog.java
com.lightbox.android.views.RemoteImageView.java
com.lightbox.android.views.RemoteThumbImageView.java
com.lightbox.android.webservices.processors.GenerationException.java
com.lightbox.android.webservices.processors.JacksonProcessor.java
com.lightbox.android.webservices.processors.ParsingException.java
com.lightbox.android.webservices.processors.Processor.java
com.lightbox.android.webservices.requests.ApiRequestFactory.java
com.lightbox.android.webservices.requests.ApiRequestListener.java
com.lightbox.android.webservices.requests.ApiRequestTask.java
com.lightbox.android.webservices.requests.ApiRequest.java
com.lightbox.android.webservices.requests.ApiRequests.java
com.lightbox.android.webservices.requests.JacksonApiRequestFactory.java
com.lightbox.android.webservices.responses.ApiException.java
com.lightbox.android.webservices.responses.ApiResponse.java
com.lightbox.tweetsnearby.TweetsNearbyApplication.java
com.lightbox.tweetsnearby.activities.MainActivity.java
com.lightbox.tweetsnearby.activities.PickPlaceActivity.java
com.lightbox.tweetsnearby.model.Tweet.java
com.lightbox.tweetsnearby.model.Venue.java
com.lightbox.tweetsnearby.responses.foursquare.FoursquareApiResponse.java
com.lightbox.tweetsnearby.responses.foursquare.VenueListResponse.java
com.lightbox.tweetsnearby.responses.twitter.TweetListResponse.java
com.lightbox.tweetsnearby.responses.twitter.TwitterApiResponse.java