import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA;
@BA.Events(values={
"Cancelled(Result As Object)",
"DoInBackground(Params() As Object) As Object",
"PostExecute(Result As Object)",
"PreExecute",
"ProgressUpdate(Progress() As Object)"
})
@BA.ShortName("AsyncTask")
public class AsyncTask extends AbsObjectWrapper<AsyncTask.AsyncTaskImpl> {
/**
* Indicates that event 'PostExecute' has been raised and has completed execution.
*/
public static final android.os.AsyncTask.Status STATUS_FINISHED=android.os.AsyncTask.Status.FINISHED;
/**
* Indicates that the task has not been executed yet.
*/
public static final android.os.AsyncTask.Status STATUS_PENDING=android.os.AsyncTask.Status.PENDING;
/**
* Indicates that the task is running.
*/
public static final android.os.AsyncTask.Status STATUS_RUNNING=android.os.AsyncTask.Status.RUNNING;
/**
* Attempts to cancel execution of this task.
* This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason.
* If successful, and this task has not started when cancel is called, this task should never run.
* If the task has already started, then the MayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.
*
* Calling this method will result in the 'Cancelled' event being raised once your 'DoInBackground' callback has completed.
* Your 'DoInBackground' callback can call the IsCancelled method to determine if the task has been cancelled whilst in progress.
* Calling this method ensures that the 'PostExecute' event is never raised.
*
* Cancel returns True if the task was cancelled, otherwise False.
*/
public boolean Cancel(boolean MayInterruptIfRunning){
return getObject().cancel(MayInterruptIfRunning);
}
/**
* Executes the task with the specified parameters.
*/
public void Execute(Object[] Params){
getObject().execute(Params);
}
/**
* Waits if necessary for the computation to complete, and then retrieves its result.
*/
public Object Get() throws ExecutionException, InterruptedException {
return getObject().get();
}
/**
* Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.
*/
public Object Get2(long TimeoutMillis) throws InterruptedException, ExecutionException, TimeoutException {
return getObject().get(TimeoutMillis, TimeUnit.MILLISECONDS);
}
/**
* Returns the current status of this task.
*/
public android.os.AsyncTask.Status GetStatus(){
return getObject().getStatus();
}
/**
* Initialize the AsyncTask.
* A MissingCallbackException will be raised if no callback sub is found for the 'DoInBackground' event.
*/
public void Initialize(BA ba, String EventName) throws MissingCallbackException {
setObject(new AsyncTaskImpl(ba, EventName));
}
/**
* Returns True if the task was cancelled before it completed normally.
*/
public boolean IsCancelled(){
return getObject().isCancelled();
}
/**
* This method can be invoked from the 'DoInBackground' callback to publish updates on the UI thread while the background computation is still running.
* The 'ProgressUpdate' callback will be called on the UI thread and passed the Progress array.
*/
public void PublishProgress(Object[] Progress){
getObject().publishProgress2(Progress);
}
@BA.Hide
public static final class AsyncTaskImpl extends android.os.AsyncTask<Object, Object, Object>{
private static final String EVENT_NAME_CANCELLED="Cancelled";
private static final String EVENT_NAME_DO_IN_BACKGROUND="DoInBackground";
private static final String EVENT_NAME_POST_EXECUTE="PostExecute";
private static final String EVENT_NAME_PRE_EXECUTE="PreExecute";
private static final String EVENT_NAME_PROGRESS_UPDATE="ProgressUpdate";
private static final String EXCEPTION_MESSAGE_MISSING_CALLBACK="DoInBackground callback sub not found: ";
private final BA ba;
private final String eventNameCancelled;
private final String eventNameDoInBackground;
private final String eventNamePostExecute;
private final String eventNamePreExecute;
private final String eventNameProgressUpdate;
private final boolean subExistsCancelled;
private final boolean subExistsPostExecute;
private final boolean subExistsPreExecute;
private final boolean subExistsProgressUpdate;
public AsyncTaskImpl(BA ba, String EventName) throws MissingCallbackException {
super();
this.ba=ba;
eventNameCancelled=getEventName(EventName, EVENT_NAME_CANCELLED);
subExistsCancelled=ba.subExists(eventNameCancelled);
eventNameDoInBackground=getEventName(EventName, EVENT_NAME_DO_IN_BACKGROUND);
eventNamePostExecute=getEventName(EventName, EVENT_NAME_POST_EXECUTE);
subExistsPostExecute=ba.subExists(eventNamePostExecute);
eventNamePreExecute=getEventName(EventName, EVENT_NAME_PRE_EXECUTE);
subExistsPreExecute=ba.subExists(eventNamePreExecute);
eventNameProgressUpdate=getEventName(EventName, EVENT_NAME_PROGRESS_UPDATE);
subExistsProgressUpdate=ba.subExists(eventNameProgressUpdate);
if(ba.subExists(eventNameDoInBackground)==false){
throw new MissingCallbackException(new StringBuilder(EXCEPTION_MESSAGE_MISSING_CALLBACK).append(eventNameDoInBackground).toString());
}
}
private String getEventName(String baseEventName, String event){
return new StringBuilder(baseEventName).append('_').append(event).toString().toLowerCase(BA.cul);
}
@Override
protected Object doInBackground(Object... params) {
return ba.raiseEvent(AsyncTaskImpl.this, eventNameDoInBackground, new Object[]{params});
}
/* @Override
protected void onCancelled() {}*/
@Override
protected void onCancelled(Object result) {
if(subExistsCancelled) {
ba.raiseEvent(AsyncTaskImpl.this, eventNameCancelled, new Object[]{result});
}
}
@Override
protected void onPostExecute(Object result) {
if(subExistsPostExecute) {
ba.raiseEvent(AsyncTaskImpl.this, eventNamePostExecute, new Object[]{result});
}
}
@Override
protected void onPreExecute() {
if(subExistsPreExecute){
ba.raiseEvent(AsyncTaskImpl.this, eventNamePreExecute, new Object[0]);
}
}
@Override
protected void onProgressUpdate(Object... progress) {
if(subExistsProgressUpdate) {
ba.raiseEvent(AsyncTaskImpl.this, eventNameProgressUpdate, new Object[]{progress});
}
}
public void publishProgress2(Object[] values){
publishProgress(values);
}
}
@BA.Hide
public static final class MissingCallbackException extends Exception{
public MissingCallbackException(String message){
super(message);
}
}
}