FileOperationRequest.Builder


public static final class FileOperationRequest.Builder
extends Object

java.lang.Object
   ↳ android.os.storage.operations.FileOperationRequest.Builder


Builder for constructing FileOperationRequest instances.

A builder must be initialized with an ERROR(OperationMode/android.os.storage.operations.FileOperationRequest.OperationMode OperationMode) (Move or Copy). Before calling build(), a source must be specified.

Example Usage:

// Initialize the builder with the desired operation mode
 FileOperationRequest.Builder builder = new FileOperationRequest.Builder(
         FileOperationRequest.OPERATION_MOVE);

 // Define the source files (e.g., from the app's internal data)
 OperationSource source = new AppDataFileSource(new File(getDataDir(), "logs.db"));

 // Define the destination (e.g., a subdirectory in PCC)
 OperationTarget target = new PccTarget("archive/databases");

 // Configure and build the request
 FileOperationRequest request = builder
         .setSource(source)
         .setTarget(target)
         .build();
 

Summary

Public constructors

Builder(int mode)

Creates a new builder for a specific file operation mode.

Public methods

FileOperationRequest build()

Builds the FileOperationRequest instance.

FileOperationRequest.Builder setRegisterCompletionListener(boolean shouldRegister)

Sets whether a completion listener should be automatically registered for this request when it is enqueued.

FileOperationRequest.Builder setSource(OperationSource source)

Sets the source of the file operation.

FileOperationRequest.Builder setTarget(OperationTarget target)

Sets the target of the file operation.

Inherited methods

Public constructors

Builder

Added in API level 37
public Builder (int mode)

Creates a new builder for a specific file operation mode.

Parameters
mode int: The operation to perform; must be FileOperationRequest.OPERATION_MOVE or FileOperationRequest.OPERATION_COPY.
Value is one of the following:

Public methods

build

Added in API level 37
public FileOperationRequest build ()

Builds the FileOperationRequest instance.

Returns
FileOperationRequest A new FileOperationRequest instance.
This value cannot be null.

Throws
IllegalStateException if the source or target (for Move/Copy) is not set.

setRegisterCompletionListener

Added in API level 37
public FileOperationRequest.Builder setRegisterCompletionListener (boolean shouldRegister)

Sets whether a completion listener should be automatically registered for this request when it is enqueued.

If set to true, the calling application will receive a FileManager.ACTION_FILE_OPERATION_COMPLETED broadcast when the operation finishes.

IMPORTANT: If the operation is rejected or fails to be queued, the listener will not be registered. Apps should ensure their requests are enqueued successfully via FileOperationEnqueueResult.isSuccessful

Parameters
shouldRegister boolean: Whether to register a completion listener.

Returns
FileOperationRequest.Builder This builder instance.
This value cannot be null.

setSource

Added in API level 37
public FileOperationRequest.Builder setSource (OperationSource source)

Sets the source of the file operation.

Parameters
source OperationSource: The OperationSource representing the files to be operated on.
This value cannot be null.

Returns
FileOperationRequest.Builder This builder instance.
This value cannot be null.

setTarget

Added in API level 37
public FileOperationRequest.Builder setTarget (OperationTarget target)

Sets the target of the file operation.

Parameters
target OperationTarget: The OperationTarget representing the destination of the operation.
This value cannot be null.

Returns
FileOperationRequest.Builder This builder instance.
This value cannot be null.