Class DynamicRunnable<T,P,R>
- java.lang.Object
-
- com.pranavpandey.android.dynamic.util.concurrent.DynamicRunnable<T,P,R>
-
- Type Parameters:
T- The type of the params.P- The type of the progress.R- The type of the result.
- All Implemented Interfaces:
java.lang.Runnable
- Direct Known Subclasses:
DynamicTask
public abstract class DynamicRunnable<T,P,R> extends java.lang.Object implements java.lang.RunnableBase class to receive the callback from an asynchronous work.
-
-
Constructor Summary
Constructors Constructor Description DynamicRunnable()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract booleancancel(boolean mayInterruptIfRunning)Attempts to cancel execution of this task.protected abstract RdoInBackground(T params)This method will be called for doing the background work.abstract voidfinish(DynamicResult<R> result)This method will be called to handle the result returned by the task.abstract booleanisCancelled()Returnstrueif this task was cancelled before it completed normally.protected voidonCancelled()Applications should preferably overrideonCancelled(DynamicResult).protected voidonCancelled(DynamicResult<R> result)Runs on the UI thread aftercancel(boolean)is invoked anddoInBackground(Object)has finished.protected voidonPostExecute(DynamicResult<R> result)This method will be called after completing the work.protected voidonPreExecute()This method will be called before doing the background work.protected voidonProgressUpdate(DynamicResult<P> progress)This method will be called on publishing the progress.abstract DynamicResult<P>publishProgress(DynamicResult<P> progress)This method can be invoked fromdoInBackground(T)to publish updates on the UI thread while the background computation is still running.
-
-
-
Method Detail
-
onPreExecute
@MainThread protected void onPreExecute()
This method will be called before doing the background work.
-
doInBackground
@WorkerThread @Nullable protected abstract R doInBackground(@Nullable T params)
This method will be called for doing the background work.- Parameters:
params- The optional parameters required for the work.- Returns:
- The optional result object.
-
onProgressUpdate
@MainThread protected void onProgressUpdate(@Nullable DynamicResult<P> progress)This method will be called on publishing the progress.- Parameters:
progress- The progress returned by the work.
-
onPostExecute
@MainThread protected void onPostExecute(@Nullable DynamicResult<R> result)This method will be called after completing the work.- Parameters:
result- The result returned by the work.
-
publishProgress
@WorkerThread @Nullable public abstract DynamicResult<P> publishProgress(@Nullable DynamicResult<P> progress)
This method can be invoked fromdoInBackground(T)to publish updates on the UI thread while the background computation is still running. Each call to this method will trigger the execution ofonProgressUpdate(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<P>)on the UI thread.onProgressUpdate(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<P>)will not be called if the task has been canceled.- Parameters:
progress- The progress result to update the UI with.- Returns:
- The published result.
- See Also:
onProgressUpdate(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<P>),doInBackground(T)
-
finish
@MainThread public abstract void finish(@Nullable DynamicResult<R> result)This method will be called to handle the result returned by the task.- Parameters:
result- The result returned by the work.
-
onCancelled
@MainThread protected void onCancelled(DynamicResult<R> result)
Runs on the UI thread aftercancel(boolean)is invoked anddoInBackground(Object)has finished.The default implementation simply invokes
onCancelled()and ignores the result. If you write your own implementation, do not callsuper.onCancelled(result).- Parameters:
result- The result, if any, computed indoInBackground(Object), can be null.- See Also:
cancel(boolean),isCancelled()
-
onCancelled
@MainThread protected void onCancelled()
Applications should preferably overrideonCancelled(DynamicResult). This method is invoked by the default implementation ofonCancelled(DynamicResult).The default version does nothing.
Runs on the UI thread after
cancel(boolean)is invoked anddoInBackground(Object)has finished.- See Also:
onCancelled(DynamicResult),cancel(boolean),isCancelled()
-
isCancelled
public abstract boolean isCancelled()
Returnstrueif this task was cancelled before it completed normally. If you are callingcancel(boolean)on the task, the value returned by this method should be checked periodically fromdoInBackground(Object)to end the task as soon as possible.- Returns:
trueif task was cancelled before it completed- See Also:
cancel(boolean)
-
cancel
public abstract boolean cancel(boolean mayInterruptIfRunning)
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 whencancelis called, this task should never run. If the task has already started, then themayInterruptIfRunningparameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.Calling this method will result in
onCancelled(DynamicResult)being invoked on the UI thread afterdoInBackground(Object)returns. Calling this method guarantees that onPostExecute(Object) is never subsequently invoked, even ifcancelreturns false, butonPostExecute(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<R>)has not yet run. To finish the task as early as possible, checkisCancelled()periodically fromdoInBackground(Object).This only requests cancellation. It never waits for a running background task to terminate, even if
mayInterruptIfRunningis true.- Parameters:
mayInterruptIfRunning-trueif the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.- Returns:
falseif the task could not be cancelled, typically because it has already completed normally;trueotherwise- See Also:
isCancelled(),onCancelled(DynamicResult)
-
-