roboguice.util
Class SafeAsyncTask<ResultT>

java.lang.Object
  extended by roboguice.util.SafeAsyncTask<ResultT>
Type Parameters:
ResultT -
All Implemented Interfaces:
java.util.concurrent.Callable<ResultT>
Direct Known Subclasses:
RoboAsyncTask

public abstract class SafeAsyncTask<ResultT>
extends java.lang.Object
implements java.util.concurrent.Callable<ResultT>

A class similar but unrelated to android's AsyncTask. Unlike AsyncTask, this class properly propagates exceptions. If you're familiar with AsyncTask and are looking for AsyncTask.doInBackground(Object[]), we've named it Callable.call() here to conform with java 1.5's Callable interface. Current limitations: does not yet handle progress, although it shouldn't be hard to add. If using your own executor, you must call future() to get a runnable you can execute.


Nested Class Summary
static class SafeAsyncTask.Task<ResultT>
           
 
Field Summary
protected static java.util.concurrent.Executor DEFAULT_EXECUTOR
           
static int DEFAULT_POOL_SIZE
           
protected  java.util.concurrent.Executor executor
           
protected  java.util.concurrent.FutureTask<java.lang.Void> future
           
protected  android.os.Handler handler
           
protected  java.lang.StackTraceElement[] launchLocation
           
 
Constructor Summary
SafeAsyncTask()
          Sets executor to Executors.newFixedThreadPool(DEFAULT_POOL_SIZE) and Handler to new Handler()
SafeAsyncTask(java.util.concurrent.Executor executor)
          Sets Handler to new Handler()
SafeAsyncTask(android.os.Handler handler)
          Sets executor to Executors.newFixedThreadPool(DEFAULT_POOL_SIZE)
SafeAsyncTask(android.os.Handler handler, java.util.concurrent.Executor executor)
           
 
Method Summary
 boolean cancel(boolean mayInterruptIfRunning)
           
 void execute()
           
protected  void execute(java.lang.StackTraceElement[] launchLocation)
           
 java.util.concurrent.Executor executor()
           
 SafeAsyncTask<ResultT> executor(java.util.concurrent.Executor executor)
           
 java.util.concurrent.FutureTask<java.lang.Void> future()
           
 android.os.Handler handler()
           
 SafeAsyncTask<ResultT> handler(android.os.Handler handler)
           
protected  SafeAsyncTask.Task<ResultT> newTask()
           
protected  void onException(java.lang.Exception e)
          Logs the exception as an Error by default, but this method may be overridden by subclasses.
protected  void onFinally()
           
protected  void onInterrupted(java.lang.InterruptedException e)
          Called when the thread has been interrupted, likely because the task was canceled.
protected  void onPreExecute()
           
protected  void onSuccess(ResultT t)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.Callable
call
 

Field Detail

DEFAULT_POOL_SIZE

public static final int DEFAULT_POOL_SIZE
See Also:
Constant Field Values

DEFAULT_EXECUTOR

protected static final java.util.concurrent.Executor DEFAULT_EXECUTOR

handler

protected android.os.Handler handler

executor

protected java.util.concurrent.Executor executor

launchLocation

protected java.lang.StackTraceElement[] launchLocation

future

protected java.util.concurrent.FutureTask<java.lang.Void> future
Constructor Detail

SafeAsyncTask

public SafeAsyncTask()
Sets executor to Executors.newFixedThreadPool(DEFAULT_POOL_SIZE) and Handler to new Handler()


SafeAsyncTask

public SafeAsyncTask(android.os.Handler handler)
Sets executor to Executors.newFixedThreadPool(DEFAULT_POOL_SIZE)


SafeAsyncTask

public SafeAsyncTask(java.util.concurrent.Executor executor)
Sets Handler to new Handler()


SafeAsyncTask

public SafeAsyncTask(android.os.Handler handler,
                     java.util.concurrent.Executor executor)
Method Detail

future

public java.util.concurrent.FutureTask<java.lang.Void> future()

executor

public SafeAsyncTask<ResultT> executor(java.util.concurrent.Executor executor)

executor

public java.util.concurrent.Executor executor()

handler

public SafeAsyncTask<ResultT> handler(android.os.Handler handler)

handler

public android.os.Handler handler()

execute

public void execute()

execute

protected void execute(java.lang.StackTraceElement[] launchLocation)

cancel

public boolean cancel(boolean mayInterruptIfRunning)

onPreExecute

protected void onPreExecute()
                     throws java.lang.Exception
Throws:
Exception, - captured on passed to onException() if present.
java.lang.Exception

onSuccess

protected void onSuccess(ResultT t)
                  throws java.lang.Exception
Parameters:
t - the result of Callable.call()
Throws:
Exception, - captured on passed to onException() if present.
java.lang.Exception

onInterrupted

protected void onInterrupted(java.lang.InterruptedException e)
Called when the thread has been interrupted, likely because the task was canceled. By default, calls onException(Exception), but this method may be overridden to handle interruptions differently than other exceptions.

Parameters:
e - the exception thrown from onPreExecute(), Callable.call(), or onSuccess(Object)

onException

protected void onException(java.lang.Exception e)
                    throws java.lang.RuntimeException
Logs the exception as an Error by default, but this method may be overridden by subclasses.

Parameters:
e - the exception thrown from onPreExecute(), Callable.call(), or onSuccess(Object)
Throws:
RuntimeException, - ignored
java.lang.RuntimeException

onFinally

protected void onFinally()
                  throws java.lang.RuntimeException
Throws:
RuntimeException, - ignored
java.lang.RuntimeException

newTask

protected SafeAsyncTask.Task<ResultT> newTask()


Copyright © 2011. All Rights Reserved.