roboguice.util
Class SafeAsyncTask<ResultT>

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

public abstract class SafeAsyncTask<ResultT>
extends Object
implements 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 Executor DEFAULT_EXECUTOR
           
static int DEFAULT_POOL_SIZE
           
protected  Executor executor
           
protected  FutureTask<Void> future
           
protected  android.os.Handler handler
           
protected  StackTraceElement[] launchLocation
           
 
Constructor Summary
SafeAsyncTask()
          Sets executor to Executors.newFixedThreadPool(DEFAULT_POOL_SIZE) and Handler to new Handler()
SafeAsyncTask(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, Executor executor)
           
 
Method Summary
 boolean cancel(boolean mayInterruptIfRunning)
           
 void execute()
           
protected  void execute(StackTraceElement[] launchLocation)
           
 Executor executor()
           
 SafeAsyncTask<ResultT> executor(Executor executor)
           
 FutureTask<Void> future()
           
 android.os.Handler handler()
           
 SafeAsyncTask<ResultT> handler(android.os.Handler handler)
           
protected  SafeAsyncTask.Task<ResultT> newTask()
           
protected  void onException(Exception e)
          Logs the exception as an Error by default, but this method may be overridden by subclasses.
protected  void onFinally()
           
protected  void onInterrupted(Exception e)
          Called when the thread has been interrupted, likely because the task was canceled.
protected  void onPreExecute()
           
protected  void onSuccess(ResultT t)
           
protected  void onThrowable(Throwable 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 Executor DEFAULT_EXECUTOR

handler

protected android.os.Handler handler

executor

protected Executor executor

launchLocation

protected StackTraceElement[] launchLocation

future

protected FutureTask<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(Executor executor)
Sets Handler to new Handler()


SafeAsyncTask

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

future

public FutureTask<Void> future()

executor

public SafeAsyncTask<ResultT> executor(Executor executor)

executor

public 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(StackTraceElement[] launchLocation)

cancel

public boolean cancel(boolean mayInterruptIfRunning)

onPreExecute

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

onSuccess

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

onInterrupted

protected void onInterrupted(Exception 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 - an InterruptedException or InterruptedIOException

onException

protected void onException(Exception e)
                    throws 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
RuntimeException

onThrowable

protected void onThrowable(Throwable t)
                    throws RuntimeException
Throws:
RuntimeException

onFinally

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

newTask

protected SafeAsyncTask.Task<ResultT> newTask()


Copyright © 2012. All Rights Reserved.