StorageTask

public abstract class StorageTask<ResultT extends StorageTask.ProvideError> extends ControllableTask

Known direct subclasses
FileDownloadTask

A task that downloads bytes of a GCS blob to a specified File.

StreamDownloadTask

A task that downloads bytes of a GCS blob.

UploadTask

An controllable task that uploads and fires events for success, progress and failure.


A controllable Task that has a synchronized state machine.

Summary

Nested types

protected interface StorageTask.ProvideError

An object that returns an exception.

Base class for state.

Protected fields

final Object

Protected constructors

Public methods

@NonNull StorageTask<ResultT>

Adds a listener that is called if the Task is canceled.

@NonNull StorageTask<ResultT>
addOnCanceledListener(
    @NonNull Activity activity,
    @NonNull OnCanceledListener listener
)

Adds an Activity-scoped listener that is called if the Task is canceled.

@NonNull StorageTask<ResultT>
addOnCanceledListener(
    @NonNull Executor executor,
    @NonNull OnCanceledListener listener
)

Adds a listener that is called if the Task is canceled.

@NonNull StorageTask<ResultT>

Adds a listener that is called when the Task succeeds or fails.

@NonNull StorageTask<ResultT>
addOnCompleteListener(
    @NonNull Activity activity,
    @NonNull OnCompleteListener<ResultT> listener
)

Adds a listener that is called when the Task succeeds or fails.

@NonNull StorageTask<ResultT>
addOnCompleteListener(
    @NonNull Executor executor,
    @NonNull OnCompleteListener<ResultT> listener
)

Adds a listener that is called when the Task succeeds or fails.

@NonNull StorageTask<ResultT>

Adds a listener that is called if the Task fails.

@NonNull StorageTask<ResultT>
addOnFailureListener(
    @NonNull Activity activity,
    @NonNull OnFailureListener listener
)

Adds a listener that is called if the Task fails.

@NonNull StorageTask<ResultT>
addOnFailureListener(
    @NonNull Executor executor,
    @NonNull OnFailureListener listener
)

Adds a listener that is called if the Task fails.

@NonNull StorageTask<ResultT>

Adds a listener that is called when the Task becomes paused.

@NonNull StorageTask<ResultT>
addOnPausedListener(
    @NonNull Activity activity,
    @NonNull OnPausedListener<Object> listener
)

Adds a listener that is called when the Task becomes paused.

@NonNull StorageTask<ResultT>
addOnPausedListener(
    @NonNull Executor executor,
    @NonNull OnPausedListener<Object> listener
)

Adds a listener that is called when the Task becomes paused.

@NonNull StorageTask<ResultT>

Adds a listener that is called periodically while the ControllableTask executes.

@NonNull StorageTask<ResultT>
addOnProgressListener(
    @NonNull Activity activity,
    @NonNull OnProgressListener<Object> listener
)

Adds a listener that is called periodically while the ControllableTask executes.

@NonNull StorageTask<ResultT>
addOnProgressListener(
    @NonNull Executor executor,
    @NonNull OnProgressListener<Object> listener
)

Adds a listener that is called periodically while the ControllableTask executes.

@NonNull StorageTask<ResultT>

Adds a listener that is called if the Task completes successfully.

@NonNull StorageTask<ResultT>
addOnSuccessListener(
    @NonNull Activity activity,
    @NonNull OnSuccessListener<Object> listener
)

Adds a listener that is called if the Task completes successfully.

@NonNull StorageTask<ResultT>
addOnSuccessListener(
    @NonNull Executor executor,
    @NonNull OnSuccessListener<Object> listener
)

Adds a listener that is called if the Task completes successfully.

boolean

Attempts to cancel the task.

@NonNull Task<ContinuationResultT>
<ContinuationResultT> continueWith(
    @NonNull Continuation<ResultT, ContinuationResultT> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

@NonNull Task<ContinuationResultT>
<ContinuationResultT> continueWith(
    @NonNull Executor executor,
    @NonNull Continuation<ResultT, ContinuationResultT> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

@NonNull Task<ContinuationResultT>
<ContinuationResultT> continueWithTask(
    @NonNull Continuation<ResultT, Task<ContinuationResultT>> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

@NonNull Task<ContinuationResultT>
<ContinuationResultT> continueWithTask(
    @NonNull Executor executor,
    @NonNull Continuation<ResultT, Task<ContinuationResultT>> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

@Nullable Exception

Returns the exception that caused the Task to fail.

@NonNull ResultT

Gets the result of the Task, if it has already completed.

@NonNull ResultT
<X extends Throwable> getResult(@NonNull Class<X> exceptionType)

Gets the result of the Task, if it has already completed.

@NonNull ResultT

Returns the current state of the task.

boolean

Returns true if the task has been canceled.

boolean

Returns true if the Task is complete; false otherwise.

boolean

Returns true if the task is currently running.

boolean

Returns true if the task has been paused.

boolean

Returns true if the Task has completed successfully; false otherwise.

@NonNull Task<ContinuationResultT>
<ContinuationResultT> onSuccessTask(
    @NonNull SuccessContinuation<ResultT, ContinuationResultT> continuation
)

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully.

@NonNull Task<ContinuationResultT>
<ContinuationResultT> onSuccessTask(
    @NonNull Executor executor,
    @NonNull SuccessContinuation<ResultT, ContinuationResultT> continuation
)

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully.

boolean

Attempts to pause the task.

@NonNull StorageTask<ResultT>

Removes a listener.

@NonNull StorageTask<ResultT>

Removes a listener.

@NonNull StorageTask<ResultT>

Removes a listener.

@NonNull StorageTask<ResultT>

Removes a listener.

@NonNull StorageTask<ResultT>

Removes a listener.

@NonNull StorageTask<ResultT>

Removes a listener.

boolean

Attempts to resume a paused task.

Protected methods

void
void
void
void
void
void

Inherited methods

From com.google.android.gms.tasks.Task
abstract boolean

Protected fields

syncObject

protected final Object syncObject

Protected constructors

StorageTask

protected StorageTask()

Public methods

addOnCanceledListener

public @NonNull StorageTask<ResultT> addOnCanceledListener(@NonNull OnCanceledListener listener)

Adds a listener that is called if the Task is canceled.

The listener will be called on main application thread. If the Task has already been canceled, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnCanceledListener

public @NonNull StorageTask<ResultT> addOnCanceledListener(
    @NonNull Activity activity,
    @NonNull OnCanceledListener listener
)

Adds an Activity-scoped listener that is called if the Task is canceled.

The listener will be called on main application thread. If the Task has already been canceled, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

The listener will be automatically removed during onStop.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnCanceledListener

public @NonNull StorageTask<ResultT> addOnCanceledListener(
    @NonNull Executor executor,
    @NonNull OnCanceledListener listener
)

Adds a listener that is called if the Task is canceled.

If the Task has already been canceled, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull StorageTask<ResultT>

this Task

addOnCompleteListener

public @NonNull StorageTask<ResultT> addOnCompleteListener(@NonNull OnCompleteListener<ResultT> listener)

Adds a listener that is called when the Task succeeds or fails.

The listener will be called on main application thread. If the task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnCompleteListener

public @NonNull StorageTask<ResultT> addOnCompleteListener(
    @NonNull Activity activity,
    @NonNull OnCompleteListener<ResultT> listener
)

Adds a listener that is called when the Task succeeds or fails.

If the task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Parameters
@NonNull Activity activity

When the supplied Activity stops, this listener will automatically be removed.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnCompleteListener

public @NonNull StorageTask<ResultT> addOnCompleteListener(
    @NonNull Executor executor,
    @NonNull OnCompleteListener<ResultT> listener
)

Adds a listener that is called when the Task succeeds or fails.

If the task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull StorageTask<ResultT>

this Task

addOnFailureListener

public @NonNull StorageTask<ResultT> addOnFailureListener(@NonNull OnFailureListener listener)

Adds a listener that is called if the Task fails.

The listener will be called on main application thread. If the task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnFailureListener

public @NonNull StorageTask<ResultT> addOnFailureListener(
    @NonNull Activity activity,
    @NonNull OnFailureListener listener
)

Adds a listener that is called if the Task fails.

If the task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Parameters
@NonNull Activity activity

When the supplied Activity stops, this listener will automatically be removed.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnFailureListener

public @NonNull StorageTask<ResultT> addOnFailureListener(
    @NonNull Executor executor,
    @NonNull OnFailureListener listener
)

Adds a listener that is called if the Task fails.

If the task has already failed, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull StorageTask<ResultT>

this Task

addOnPausedListener

public @NonNull StorageTask<ResultT> addOnPausedListener(@NonNull OnPausedListener<Object> listener)

Adds a listener that is called when the Task becomes paused.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnPausedListener

public @NonNull StorageTask<ResultT> addOnPausedListener(
    @NonNull Activity activity,
    @NonNull OnPausedListener<Object> listener
)

Adds a listener that is called when the Task becomes paused.

Parameters
@NonNull Activity activity

When the supplied Activity stops, this listener will automatically be removed.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnPausedListener

public @NonNull StorageTask<ResultT> addOnPausedListener(
    @NonNull Executor executor,
    @NonNull OnPausedListener<Object> listener
)

Adds a listener that is called when the Task becomes paused.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull StorageTask<ResultT>

this Task

addOnProgressListener

public @NonNull StorageTask<ResultT> addOnProgressListener(@NonNull OnProgressListener<Object> listener)

Adds a listener that is called periodically while the ControllableTask executes.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnProgressListener

public @NonNull StorageTask<ResultT> addOnProgressListener(
    @NonNull Activity activity,
    @NonNull OnProgressListener<Object> listener
)

Adds a listener that is called periodically while the ControllableTask executes.

Parameters
@NonNull Activity activity

When the supplied Activity stops, this listener will automatically be removed.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnProgressListener

public @NonNull StorageTask<ResultT> addOnProgressListener(
    @NonNull Executor executor,
    @NonNull OnProgressListener<Object> listener
)

Adds a listener that is called periodically while the ControllableTask executes.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull StorageTask<ResultT>

this Task

addOnSuccessListener

public @NonNull StorageTask<ResultT> addOnSuccessListener(@NonNull OnSuccessListener<Object> listener)

Adds a listener that is called if the Task completes successfully. The listener will be called on the main application thread. If the task has already completed successfully, a call to the listener will be immediately scheduled. If multiple listeners are added, they will be called in the order in which they were added.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnSuccessListener

public @NonNull StorageTask<ResultT> addOnSuccessListener(
    @NonNull Activity activity,
    @NonNull OnSuccessListener<Object> listener
)

Adds a listener that is called if the Task completes successfully.

If multiple listeners are added, they will be called in the order in which they were added. If the task has already completed successfully, a call to the listener will be immediately scheduled.

Parameters
@NonNull Activity activity

When the supplied Activity stops, this listener will automatically be removed.

Returns
@NonNull StorageTask<ResultT>

this Task

addOnSuccessListener

public @NonNull StorageTask<ResultT> addOnSuccessListener(
    @NonNull Executor executor,
    @NonNull OnSuccessListener<Object> listener
)

Adds a listener that is called if the Task completes successfully.

If multiple listeners are added, they will be called in the order in which they were added. If the task has already completed successfully, a call to the listener will be immediately scheduled.

Parameters
@NonNull Executor executor

the executor to use to call the listener

Returns
@NonNull StorageTask<ResultT>

this Task

cancel

public boolean cancel()

Attempts to cancel the task. A canceled task cannot be resumed later.

Returns
boolean

true if this task is successfully being canceled.

continueWith

public @NonNull Task<ContinuationResultT> <ContinuationResultT> continueWith(
    @NonNull Continuation<ResultT, ContinuationResultT> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

The Continuation will be called on the main application thread.

See also
then

continueWith

public @NonNull Task<ContinuationResultT> <ContinuationResultT> continueWith(
    @NonNull Executor executor,
    @NonNull Continuation<ResultT, ContinuationResultT> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

Parameters
@NonNull Executor executor

the executor to use to call the Continuation

See also
then

continueWithTask

public @NonNull Task<ContinuationResultT> <ContinuationResultT> continueWithTask(
    @NonNull Continuation<ResultT, Task<ContinuationResultT>> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

The Continuation will be called on the main application thread.

See also
then

continueWithTask

public @NonNull Task<ContinuationResultT> <ContinuationResultT> continueWithTask(
    @NonNull Executor executor,
    @NonNull Continuation<ResultT, Task<ContinuationResultT>> continuation
)

Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.

Parameters
@NonNull Executor executor

the executor to use to call the Continuation

See also
then

getException

public @Nullable Exception getException()

Returns the exception that caused the Task to fail. Returns null if the Task is not yet complete, or completed successfully.

getResult

public @NonNull ResultT getResult()

Gets the result of the Task, if it has already completed.

Throws
java.lang.IllegalStateException java.lang.IllegalStateException

if the Task is not yet complete

com.google.android.gms.tasks.RuntimeExecutionException com.google.android.gms.tasks.RuntimeExecutionException

if the Task failed with an exception

getResult

public @NonNull ResultT <X extends Throwable> getResult(@NonNull Class<X> exceptionType)

Gets the result of the Task, if it has already completed.

Throws
java.lang.IllegalStateException java.lang.IllegalStateException

if the Task is not yet complete

X X

if the Task failed with an exception of type X

com.google.android.gms.tasks.RuntimeExecutionException com.google.android.gms.tasks.RuntimeExecutionException

if the Task failed with an exception that was not of type X

getSnapshot

public @NonNull ResultT getSnapshot()

Returns the current state of the task. This method will return state at any point of the tasks execution and may not be the final result.

isCanceled

public boolean isCanceled()

Returns true if the task has been canceled.

isComplete

public boolean isComplete()

Returns true if the Task is complete; false otherwise.

isInProgress

public boolean isInProgress()

Returns true if the task is currently running.

isPaused

public boolean isPaused()

Returns true if the task has been paused.

isSuccessful

public boolean isSuccessful()

Returns true if the Task has completed successfully; false otherwise.

onSuccessTask

public @NonNull Task<ContinuationResultT> <ContinuationResultT> onSuccessTask(
    @NonNull SuccessContinuation<ResultT, ContinuationResultT> continuation
)

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully. If the previous Task fails, the onSuccessTask completion will be skipped and failure listeners will be invoked.

The SuccessContinuation will be called on the main application thread.

If the previous Task is canceled, the returned Task will also be canceled and the SuccessContinuation would not execute.

onSuccessTask

public @NonNull Task<ContinuationResultT> <ContinuationResultT> onSuccessTask(
    @NonNull Executor executor,
    @NonNull SuccessContinuation<ResultT, ContinuationResultT> continuation
)

Returns a new Task that will be completed with the result of applying the specified SuccessContinuation to this Task when this Task completes successfully. If the previous Task fails, the onSuccessTask completion will be skipped and failure listeners will be invoked.

If the previous Task is canceled, the returned Task will also be canceled and the SuccessContinuation would not execute.

Parameters
@NonNull Executor executor

the executor to use to call the SuccessContinuation

pause

public boolean pause()

Attempts to pause the task. A paused task can later be resumed.

Returns
boolean

true if this task is successfully being paused. Note that a task may not be immediately paused if it was executing another action and can still fail or complete.

removeOnCanceledListener

public @NonNull StorageTask<ResultT> removeOnCanceledListener(@NonNull OnCanceledListener listener)

Removes a listener.

removeOnCompleteListener

public @NonNull StorageTask<ResultT> removeOnCompleteListener(@NonNull OnCompleteListener<ResultT> listener)

Removes a listener.

removeOnFailureListener

public @NonNull StorageTask<ResultT> removeOnFailureListener(@NonNull OnFailureListener listener)

Removes a listener.

removeOnPausedListener

public @NonNull StorageTask<ResultT> removeOnPausedListener(@NonNull OnPausedListener<Object> listener)

Removes a listener.

removeOnProgressListener

public @NonNull StorageTask<ResultT> removeOnProgressListener(@NonNull OnProgressListener<Object> listener)

Removes a listener.

removeOnSuccessListener

public @NonNull StorageTask<ResultT> removeOnSuccessListener(@NonNull OnSuccessListener<Object> listener)

Removes a listener.

resume

public boolean resume()

Attempts to resume a paused task.

Returns
boolean

true if the task is successfully resumed. false if the task has an unrecoverable error or has entered another state that precludes resume.

Protected methods

onCanceled

protected void onCanceled()

onFailure

protected void onFailure()

onPaused

protected void onPaused()

onProgress

protected void onProgress()

onQueued

protected void onQueued()

onSuccess

protected void onSuccess()

Extension functions

StorageKt.getTaskState

public final @NonNull Flow<@NonNull TaskState<@NonNull T>> StorageKt.getTaskState(@NonNull StorageTask<@NonNull T> receiver)

StorageKt.getTaskState

public final @NonNull Flow<@NonNull TaskState<@NonNull T>> StorageKt.getTaskState(@NonNull StorageTask<@NonNull T> receiver)