Constraints.Builder


class Constraints.Builder


A Builder for a Constraints object.

Summary

Public constructors

Public functions

Constraints.Builder
@RequiresApi(value = 24)
addContentUriTrigger(uri: Uri, triggerForDescendants: Boolean)

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

Constraints

Generates the Constraints from this Builder.

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

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

Constraints.Builder

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

Constraints.Builder
setRequiresBatteryNotLow(requiresBatteryNotLow: Boolean)

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

Constraints.Builder
setRequiresCharging(requiresCharging: Boolean)

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

Constraints.Builder
@RequiresApi(value = 23)
setRequiresDeviceIdle(requiresDeviceIdle: Boolean)

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

Constraints.Builder
setRequiresStorageNotLow(requiresStorageNotLow: Boolean)

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

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.

Constraints.Builder
@RequiresApi(value = 24)
setTriggerContentMaxDelay(duration: Long, 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.

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.

Constraints.Builder
@RequiresApi(value = 24)
setTriggerContentUpdateDelay(duration: Long, 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
Builder()

Public functions

addContentUriTrigger

Added in 1.0.0
@RequiresApi(value = 24)
fun addContentUriTrigger(uri: Uri, triggerForDescendants: Boolean): Constraints.Builder

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
uri: Uri

The local content: Uri to observe

triggerForDescendants: Boolean

true if any changes in descendants cause this WorkRequest to run

Returns
Constraints.Builder

The current Builder

build

Added in 1.0.0
fun build(): Constraints

Generates the Constraints from this Builder.

Returns
Constraints

The Constraints specified by this Builder

setRequiredNetworkRequest

Added in 2.10.0
@RequiresApi(value = 21)
fun setRequiredNetworkRequest(
    networkRequest: NetworkRequest,
    networkType: NetworkType
): Constraints.Builder

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
networkRequest: NetworkRequest
networkType: NetworkType

The type of network required for t

Returns
Constraints.Builder

The current Builder

setRequiredNetworkType

Added in 1.0.0
fun setRequiredNetworkType(networkType: NetworkType): Constraints.Builder

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

Parameters
networkType: NetworkType

The type of network required for the work to run

Returns
Constraints.Builder

The current Builder

setRequiresBatteryNotLow

Added in 1.0.0
fun setRequiresBatteryNotLow(requiresBatteryNotLow: Boolean): Constraints.Builder

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

Parameters
requiresBatteryNotLow: Boolean

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

Returns
Constraints.Builder

The current Builder

setRequiresCharging

Added in 1.0.0
fun setRequiresCharging(requiresCharging: Boolean): Constraints.Builder

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

Parameters
requiresCharging: Boolean

true if device must be charging for the work to run

Returns
Constraints.Builder

The current Builder

setRequiresDeviceIdle

Added in 1.0.0
@RequiresApi(value = 23)
fun setRequiresDeviceIdle(requiresDeviceIdle: Boolean): Constraints.Builder

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

Parameters
requiresDeviceIdle: Boolean

true if device must be idle for the work to run

Returns
Constraints.Builder

The current Builder

setRequiresStorageNotLow

Added in 1.0.0
fun setRequiresStorageNotLow(requiresStorageNotLow: Boolean): Constraints.Builder

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

Parameters
requiresStorageNotLow: Boolean

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

Returns
Constraints.Builder

The current Builder

setTriggerContentMaxDelay

Added in 1.0.0
@RequiresApi(value = 26)
fun setTriggerContentMaxDelay(duration: Duration): 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. This functionality is identical to the one found in JobScheduler and is described in JobInfo.Builder#setTriggerContentMaxDelay(long).

Parameters
duration: Duration

The length of the delay

Returns
Constraints.Builder

The current Builder

setTriggerContentMaxDelay

Added in 1.0.0
@RequiresApi(value = 24)
fun setTriggerContentMaxDelay(duration: Long, timeUnit: TimeUnit): 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. This functionality is identical to the one found in JobScheduler and is described in JobInfo.Builder#setTriggerContentMaxDelay(long).

Parameters
duration: Long

The length of the delay in timeUnit units

timeUnit: TimeUnit

The units of time for duration

Returns
Constraints.Builder

The current Builder

setTriggerContentUpdateDelay

Added in 1.0.0
@RequiresApi(value = 26)
fun setTriggerContentUpdateDelay(duration: Duration): 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. 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
duration: Duration

The length of the delay

Returns
Constraints.Builder

The current Builder

setTriggerContentUpdateDelay

Added in 1.0.0
@RequiresApi(value = 24)
fun setTriggerContentUpdateDelay(duration: Long, timeUnit: TimeUnit): 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. 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
duration: Long

The length of the delay in timeUnit units

timeUnit: TimeUnit

The units of time for duration

Returns
Constraints.Builder

The current Builder