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

@NonNullStorageTask<ResultT>

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

@NonNullStorageTask<ResultT>
addOnCanceledListener(
    @NonNullActivity activity,
    @NonNullOnCanceledListener listener
)

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

@NonNullStorageTask<ResultT>
addOnCanceledListener(
    @NonNullExecutor executor,
    @NonNullOnCanceledListener listener
)

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

@NonNullStorageTask<ResultT>

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

@NonNullStorageTask<ResultT>
addOnCompleteListener(
    @NonNullActivity activity,
    @NonNullOnCompleteListener<ResultT> listener
)

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

@NonNullStorageTask<ResultT>
addOnCompleteListener(
    @NonNullExecutor executor,
    @NonNullOnCompleteListener<ResultT> listener
)

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

@NonNullStorageTask<ResultT>

Adds a listener that is called if the Task fails.

@NonNullStorageTask<ResultT>
addOnFailureListener(
    @NonNullActivity activity,
    @NonNullOnFailureListener listener
)

Adds a listener that is called if the Task fails.

@NonNullStorageTask<ResultT>
addOnFailureListener(
    @NonNullExecutor executor,
    @NonNullOnFailureListener listener
)

Adds a listener that is called if the Task fails.

@NonNullStorageTask<ResultT>

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

@NonNullStorageTask<ResultT>
addOnPausedListener(
    @NonNullActivity activity,
    @NonNullOnPausedListener<Object> listener
)

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

@NonNullStorageTask<ResultT>
addOnPausedListener(
    @NonNullExecutor executor,
    @NonNullOnPausedListener<Object> listener
)

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

@NonNullStorageTask<ResultT>

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

@NonNullStorageTask<ResultT>

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

@NonNullStorageTask<ResultT>

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

@NonNullStorageTask<ResultT>

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

@NonNullStorageTask<ResultT>

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

@NonNullStorageTask<ResultT>

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

boolean

Attempts to cancel the task.

@NonNullTask<ContinuationResultT>
<ContinuationResultT> continueWith(
    @NonNullContinuation<ResultT, ContinuationResultT> continuation
)

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

@NonNullTask<ContinuationResultT>
<ContinuationResultT> continueWith(
    @NonNullExecutor executor,
    @NonNullContinuation<ResultT, ContinuationResultT> continuation
)

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

@NonNullTask<ContinuationResultT>
<ContinuationResultT> continueWithTask(
    @NonNullContinuation<ResultT, Task<ContinuationResultT>> continuation
)

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

@NonNullTask<ContinuationResultT>
<ContinuationResultT> continueWithTask(
    @NonNullExecutor executor,
    @NonNullContinuation<ResultT, Task<ContinuationResultT>> continuation
)

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

@NullableException

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(@NonNullClass<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.

@NonNullTask<ContinuationResultT>
<ContinuationResultT> onSuccessTask(
    @NonNullSuccessContinuation<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.

@NonNullTask<ContinuationResultT>
<ContinuationResultT> onSuccessTask(
    @NonNullExecutor executor,
    @NonNullSuccessContinuation<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.

@NonNullStorageTask<ResultT>

Removes a listener.

@NonNullStorageTask<ResultT>

Removes a listener.

@NonNullStorageTask<ResultT>

Removes a listener.

@NonNullStorageTask<ResultT>

Removes a listener.

@NonNullStorageTask<ResultT>

Removes a listener.

@NonNullStorageTask<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 @NonNullStorageTask<ResultT> addOnCanceledListener(@NonNullOnCanceledListener 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
@NonNullStorageTask<ResultT>

this Task

addOnCanceledListener

public @NonNullStorageTask<ResultT> addOnCanceledListener(
    @NonNullActivity activity,
    @NonNullOnCanceledListener 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
@NonNullStorageTask<ResultT>

this Task

addOnCanceledListener

public @NonNullStorageTask<ResultT> addOnCanceledListener(
    @NonNullExecutor executor,
    @NonNullOnCanceledListener 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
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullStorageTask<ResultT>

this Task

addOnCompleteListener

public @NonNullStorageTask<ResultT> addOnCompleteListener(@NonNullOnCompleteListener<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
@NonNullStorageTask<ResultT>

this Task

addOnCompleteListener

public @NonNullStorageTask<ResultT> addOnCompleteListener(
    @NonNullActivity activity,
    @NonNullOnCompleteListener<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
@NonNullActivity activity

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

Returns
@NonNullStorageTask<ResultT>

this Task

addOnCompleteListener

public @NonNullStorageTask<ResultT> addOnCompleteListener(
    @NonNullExecutor executor,
    @NonNullOnCompleteListener<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
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullStorageTask<ResultT>

this Task

addOnFailureListener

public @NonNullStorageTask<ResultT> addOnFailureListener(@NonNullOnFailureListener 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
@NonNullStorageTask<ResultT>

this Task

addOnFailureListener

public @NonNullStorageTask<ResultT> addOnFailureListener(
    @NonNullActivity activity,
    @NonNullOnFailureListener 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
@NonNullActivity activity

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

Returns
@NonNullStorageTask<ResultT>

this Task

addOnFailureListener

public @NonNullStorageTask<ResultT> addOnFailureListener(
    @NonNullExecutor executor,
    @NonNullOnFailureListener 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
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullStorageTask<ResultT>

this Task

addOnPausedListener

public @NonNullStorageTask<ResultT> addOnPausedListener(@NonNullOnPausedListener<Object> listener)

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

Returns
@NonNullStorageTask<ResultT>

this Task

addOnPausedListener

public @NonNullStorageTask<ResultT> addOnPausedListener(
    @NonNullActivity activity,
    @NonNullOnPausedListener<Object> listener
)

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

Parameters
@NonNullActivity activity

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

Returns
@NonNullStorageTask<ResultT>

this Task

addOnPausedListener

public @NonNullStorageTask<ResultT> addOnPausedListener(
    @NonNullExecutor executor,
    @NonNullOnPausedListener<Object> listener
)

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

Parameters
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullStorageTask<ResultT>

this Task

addOnProgressListener

public @NonNullStorageTask<ResultT> addOnProgressListener(@NonNullOnProgressListener<Object> listener)

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

Returns
@NonNullStorageTask<ResultT>

this Task

addOnProgressListener

public @NonNullStorageTask<ResultT> addOnProgressListener(
    @NonNullActivity activity,
    @NonNullOnProgressListener<Object> listener
)

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

Parameters
@NonNullActivity activity

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

Returns
@NonNullStorageTask<ResultT>

this Task

addOnProgressListener

public @NonNullStorageTask<ResultT> addOnProgressListener(
    @NonNullExecutor executor,
    @NonNullOnProgressListener<Object> listener
)

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

Parameters
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullStorageTask<ResultT>

this Task

addOnSuccessListener

public @NonNullStorageTask<ResultT> addOnSuccessListener(@NonNullOnSuccessListener<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
@NonNullStorageTask<ResultT>

this Task

addOnSuccessListener

public @NonNullStorageTask<ResultT> addOnSuccessListener(
    @NonNullActivity activity,
    @NonNullOnSuccessListener<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
@NonNullActivity activity

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

Returns
@NonNullStorageTask<ResultT>

this Task

addOnSuccessListener

public @NonNullStorageTask<ResultT> addOnSuccessListener(
    @NonNullExecutor executor,
    @NonNullOnSuccessListener<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
@NonNullExecutor executor

the executor to use to call the listener

Returns
@NonNullStorageTask<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 @NonNullTask<ContinuationResultT> <ContinuationResultT> continueWith(
    @NonNullContinuation<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 @NonNullTask<ContinuationResultT> <ContinuationResultT> continueWith(
    @NonNullExecutor executor,
    @NonNullContinuation<ResultT, ContinuationResultT> continuation
)

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

Parameters
@NonNullExecutor executor

the executor to use to call the Continuation

See also
then

continueWithTask

public @NonNullTask<ContinuationResultT> <ContinuationResultT> continueWithTask(
    @NonNullContinuation<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 @NonNullTask<ContinuationResultT> <ContinuationResultT> continueWithTask(
    @NonNullExecutor executor,
    @NonNullContinuation<ResultT, Task<ContinuationResultT>> continuation
)

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

Parameters
@NonNullExecutor executor

the executor to use to call the Continuation

See also
then

getException

public @NullableException 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(@NonNullClass<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 @NonNullTask<ContinuationResultT> <ContinuationResultT> onSuccessTask(
    @NonNullSuccessContinuation<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 @NonNullTask<ContinuationResultT> <ContinuationResultT> onSuccessTask(
    @NonNullExecutor executor,
    @NonNullSuccessContinuation<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
@NonNullExecutor 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 @NonNullStorageTask<ResultT> removeOnCanceledListener(@NonNullOnCanceledListener listener)

Removes a listener.

removeOnCompleteListener

public @NonNullStorageTask<ResultT> removeOnCompleteListener(@NonNullOnCompleteListener<ResultT> listener)

Removes a listener.

removeOnFailureListener

public @NonNullStorageTask<ResultT> removeOnFailureListener(@NonNullOnFailureListener listener)

Removes a listener.

removeOnPausedListener

public @NonNullStorageTask<ResultT> removeOnPausedListener(@NonNullOnPausedListener<Object> listener)

Removes a listener.

removeOnProgressListener

public @NonNullStorageTask<ResultT> removeOnProgressListener(@NonNullOnProgressListener<Object> listener)

Removes a listener.

removeOnSuccessListener

public @NonNullStorageTask<ResultT> removeOnSuccessListener(@NonNullOnSuccessListener<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 @NonNullFlow<@NonNullTaskState<@NonNull T>> StorageKt.getTaskState(@NonNullStorageTask<@NonNull T> receiver)

StorageKt.getTaskState

public final @NonNullFlow<@NonNullTaskState<@NonNull T>> StorageKt.getTaskState(@NonNullStorageTask<@NonNull T> receiver)