Constraints.Builder


public final class Constraints.Builder


A Builder for a Constraints object.

Summary

Public constructors

Public methods

final @NonNull Constraints.Builder
@RequiresApi(value = 24)
addContentUriTrigger(@NonNull Uri uri, boolean triggerForDescendants)

Sets whether the WorkRequest should run when a local content: Uri is updated.

final @NonNull Constraints

Generates the Constraints from this Builder.

final @NonNull Constraints.Builder
@RequiresApi(value = 21)
setRequiredNetworkRequest(
    @NonNull NetworkRequest networkRequest,
    @NonNull NetworkType networkType
)

Sets whether device should have a particular NetworkRequest for the WorkRequest to run on the API levels >= 28 (Android P).

final @NonNull Constraints.Builder

Sets whether device should have a particular NetworkType for the WorkRequest to run.

final @NonNull Constraints.Builder
setRequiresBatteryNotLow(boolean requiresBatteryNotLow)

Sets whether device battery should be at an acceptable level for the WorkRequest to run.

final @NonNull Constraints.Builder
setRequiresCharging(boolean requiresCharging)

Sets whether device should be charging for the WorkRequest to run.

final @NonNull Constraints.Builder
@RequiresApi(value = 23)
setRequiresDeviceIdle(boolean requiresDeviceIdle)

Sets whether device should be idle for the WorkRequest to run.

final @NonNull Constraints.Builder
setRequiresStorageNotLow(boolean requiresStorageNotLow)

Sets whether the device's available storage should be at an acceptable level for the WorkRequest to run.

final @NonNull Constraints.Builder

Sets the maximum delay that is allowed from the first time a content: Uri change is detected to the time when the WorkRequest is scheduled.

final @NonNull Constraints.Builder
@RequiresApi(value = 24)
setTriggerContentMaxDelay(long duration, @NonNull TimeUnit timeUnit)

Sets the maximum delay that is allowed from the first time a content: Uri change is detected to the time when the WorkRequest is scheduled.

final @NonNull Constraints.Builder

Sets the delay that is allowed from the time a content: Uri change is detected to the time when the WorkRequest is scheduled.

final @NonNull Constraints.Builder
@RequiresApi(value = 24)
setTriggerContentUpdateDelay(long duration, @NonNull TimeUnit timeUnit)

Sets the delay that is allowed from the time a content: Uri change is detected to the time when the WorkRequest is scheduled.

Public constructors

Builder

Added in 1.0.0
public Builder()

Public methods

addContentUriTrigger

Added in 1.0.0
@RequiresApi(value = 24)
public final @NonNull Constraints.Builder addContentUriTrigger(@NonNull Uri uri, boolean triggerForDescendants)

Sets whether the WorkRequest should run when a local content: Uri is updated. This functionality is identical to the one found in JobScheduler and is described in JobInfo.Builder#addTriggerContentUri(android.app.job.JobInfo.TriggerContentUri).

Parameters
@NonNull Uri uri

The local content: Uri to observe

boolean triggerForDescendants

true if any changes in descendants cause this WorkRequest to run

Returns
@NonNull Constraints.Builder

The current Builder

build

Added in 1.0.0
public final @NonNull Constraints build()

Generates the Constraints from this Builder.

Returns
@NonNull Constraints

The Constraints specified by this Builder

setRequiredNetworkRequest

Added in 2.10.0-rc01
@RequiresApi(value = 21)
public final @NonNull Constraints.Builder setRequiredNetworkRequest(
    @NonNull NetworkRequest networkRequest,
    @NonNull NetworkType networkType
)

Sets whether device should have a particular NetworkRequest for the WorkRequest to run on the API levels >= 28 (Android P). For the older API levels, networkType will be used instead on the older platforms.

NetworkRequest with NetworkSpecifier set aren't supported, as well as NetworkRequest with setIncludeOtherUidNetworks set. IllegalArgumentException will be thrown if such requests are passed.

Parameters
@NonNull NetworkRequest networkRequest
@NonNull NetworkType networkType

The type of network required for t

Returns
@NonNull Constraints.Builder

The current Builder

setRequiredNetworkType

Added in 1.0.0
public final @NonNull Constraints.Builder setRequiredNetworkType(@NonNull NetworkType networkType)

Sets whether device should have a particular NetworkType for the WorkRequest to run. The default value is NetworkType.NOT_REQUIRED.

Parameters
@NonNull NetworkType networkType

The type of network required for the work to run

Returns
@NonNull Constraints.Builder

The current Builder

setRequiresBatteryNotLow

Added in 1.0.0
public final @NonNull Constraints.Builder setRequiresBatteryNotLow(boolean requiresBatteryNotLow)

Sets whether device battery should be at an acceptable level for the WorkRequest to run. The default value is false.

Parameters
boolean requiresBatteryNotLow

true if the battery should be at an acceptable level for the work to run

Returns
@NonNull Constraints.Builder

The current Builder

setRequiresCharging

Added in 1.0.0
public final @NonNull Constraints.Builder setRequiresCharging(boolean requiresCharging)

Sets whether device should be charging for the WorkRequest to run. The default value is false.

Parameters
boolean requiresCharging

true if device must be charging for the work to run

Returns
@NonNull Constraints.Builder

The current Builder

setRequiresDeviceIdle

Added in 1.0.0
@RequiresApi(value = 23)
public final @NonNull Constraints.Builder setRequiresDeviceIdle(boolean requiresDeviceIdle)

Sets whether device should be idle for the WorkRequest to run. The default value is false.

Parameters
boolean requiresDeviceIdle

true if device must be idle for the work to run

Returns
@NonNull Constraints.Builder

The current Builder

setRequiresStorageNotLow

Added in 1.0.0
public final @NonNull Constraints.Builder setRequiresStorageNotLow(boolean requiresStorageNotLow)

Sets whether the device's available storage should be at an acceptable level for the WorkRequest to run. The default value is false.

Parameters
boolean requiresStorageNotLow

true if the available storage should not be below a a critical threshold for the work to run

Returns
@NonNull Constraints.Builder

The current Builder

setTriggerContentMaxDelay

Added in 1.0.0
@RequiresApi(value = 26)
public final @NonNull Constraints.Builder setTriggerContentMaxDelay(@NonNull Duration duration)

Sets the maximum delay that is allowed from the first time a content: Uri change is detected to the time when the WorkRequest is scheduled. This functionality is identical to the one found in JobScheduler and is described in JobInfo.Builder#setTriggerContentMaxDelay(long).

Parameters
@NonNull Duration duration

The length of the delay

Returns
@NonNull Constraints.Builder

The current Builder

setTriggerContentMaxDelay

Added in 1.0.0
@RequiresApi(value = 24)
public final @NonNull Constraints.Builder setTriggerContentMaxDelay(long duration, @NonNull TimeUnit timeUnit)

Sets the maximum delay that is allowed from the first time a content: Uri change is detected to the time when the WorkRequest is scheduled. This functionality is identical to the one found in JobScheduler and is described in JobInfo.Builder#setTriggerContentMaxDelay(long).

Parameters
long duration

The length of the delay in timeUnit units

@NonNull TimeUnit timeUnit

The units of time for duration

Returns
@NonNull Constraints.Builder

The current Builder

setTriggerContentUpdateDelay

Added in 1.0.0
@RequiresApi(value = 26)
public final @NonNull Constraints.Builder setTriggerContentUpdateDelay(@NonNull Duration duration)

Sets the delay that is allowed from the time a content: Uri change is detected to the time when the WorkRequest is scheduled. If there are more changes during this time, the delay will be reset to the start of the most recent change. This functionality is identical to the one found in JobScheduler and is described in JobInfo.Builder#setTriggerContentUpdateDelay(long).

Parameters
@NonNull Duration duration

The length of the delay

Returns
@NonNull Constraints.Builder

The current Builder

setTriggerContentUpdateDelay

Added in 1.0.0
@RequiresApi(value = 24)
public final @NonNull Constraints.Builder setTriggerContentUpdateDelay(long duration, @NonNull TimeUnit timeUnit)

Sets the delay that is allowed from the time a content: Uri change is detected to the time when the WorkRequest is scheduled. If there are more changes during this time, the delay will be reset to the start of the most recent change. This functionality is identical to the one found in JobScheduler and is described in JobInfo.Builder#setTriggerContentUpdateDelay(long).

Parameters
long duration

The length of the delay in timeUnit units

@NonNull TimeUnit timeUnit

The units of time for duration

Returns
@NonNull Constraints.Builder

The current Builder