WorkMetricsInfoRepository


@ExperimentalEventsApi
@ExperimentalWorkMetricsApi
public final class WorkMetricsInfoRepository implements ScheduleEventListener, ExecutionEventListener


Repository class that calculates and stores metrics info and analytics about workers.

Summary

Public fields

static final long

The default retention time for finished work metrics (7 days).

static final long

The maximum allowed retention time for finished work metrics (30 days).

Public constructors

Creates an instance of WorkMetricsInfoRepository with a default retention of DEFAULT_RETENTION_TIME_MILLIS.

@RequiresApi(value = 26)
WorkMetricsInfoRepository(
    @NonNull Context context,
    @NonNull Duration retentionDuration,
    Executor dbExecutor
)

Creates an instance of WorkMetricsInfoRepository with a custom retention duration.

WorkMetricsInfoRepository(
    @NonNull Context context,
    long retentionTime,
    @NonNull TimeUnit retentionTimeUnit,
    Executor dbExecutor
)

Creates an instance of WorkMetricsInfoRepository with a custom retention time.

Public methods

final @NonNull Flow<@NonNull WorkMetricsInfo>

A hot Flow that emits a WorkMetricsInfo whenever one finishes.

final @NonNull List<@NonNull WorkMetricsInfo>

Gets a list of WorkMetricsInfo snapshots for a given work id.

final @NonNull List<@NonNull WorkMetricsInfo>

Gets a list of WorkMetricsInfo matching the query criteria.

void

Called when a work request is canceled from the app e.g. WorkManager.cancelWorkById.

void

Called when a work request is enqueued from the app e.g. WorkManager.enqueue.

void
onException(@NonNull Throwable throwable, @NonNull WorkInfo workInfo)

Called when the work request fails from an exception after it has started.

void
onFinished(
    @NonNull ListenableWorker.Result result,
    @NonNull WorkInfo workInfo
)

Called when the worker finishes and returns a value.

void

Called when a work request fails because a prerequisite work fails.

void

Called when a work request starts executing i.e. ListenableWorker.startWork.

void
onStopped(int stopReason, @NonNull WorkInfo workInfo)

Called when a work request is stopped by the system after it has started.

void

Called when a work request is no longer waiting on any of its prerequisite work.

void
onUpdated(@NonNull WorkInfo oldWorkInfo, @NonNull WorkInfo updatedWorkInfo)

Called when a work request is updated from the app e.g. WorkManager.updateWork.

Public fields

DEFAULT_RETENTION_TIME_MILLIS

public static final long DEFAULT_RETENTION_TIME_MILLIS

The default retention time for finished work metrics (7 days).

MAX_RETENTION_TIME_MILLIS

public static final long MAX_RETENTION_TIME_MILLIS

The maximum allowed retention time for finished work metrics (30 days).

Public constructors

WorkMetricsInfoRepository

public WorkMetricsInfoRepository(@NonNull Context context, Executor dbExecutor)

Creates an instance of WorkMetricsInfoRepository with a default retention of DEFAULT_RETENTION_TIME_MILLIS.

It is recommended that the Executor passed here is the same as the one passed into androidx.work.Configuration.Builder.setTaskExecutor. If no executor is provided, Room's default executors will be used instead.

Note that this executor is only used for database operations and not for running the event hooks themselves.

Parameters
@NonNull Context context

The application Context.

Executor dbExecutor

The Executor passed to Room on which database queries and transactions will be run.

WorkMetricsInfoRepository

@RequiresApi(value = 26)
public WorkMetricsInfoRepository(
    @NonNull Context context,
    @NonNull Duration retentionDuration,
    Executor dbExecutor
)

Creates an instance of WorkMetricsInfoRepository with a custom retention duration.

Parameters
@NonNull Context context

The application Context.

@NonNull Duration retentionDuration

The retention duration. Clamped to a maximum of MAX_RETENTION_TIME_MILLIS. Must be positive.

Executor dbExecutor

The Executor passed to Room on which database queries and transactions will be run.

WorkMetricsInfoRepository

public WorkMetricsInfoRepository(
    @NonNull Context context,
    long retentionTime,
    @NonNull TimeUnit retentionTimeUnit,
    Executor dbExecutor
)

Creates an instance of WorkMetricsInfoRepository with a custom retention time.

Parameters
@NonNull Context context

The application Context.

long retentionTime

The retention time. Clamped to a maximum of MAX_RETENTION_TIME_MILLIS. Must be positive.

@NonNull TimeUnit retentionTimeUnit

The TimeUnit for retentionTime.

Executor dbExecutor

The Executor passed to Room on which database queries and transactions will be run.

Public methods

getFinishedWorkMetricsInfoFlow

public final @NonNull Flow<@NonNull WorkMetricsInfogetFinishedWorkMetricsInfoFlow()

A hot Flow that emits a WorkMetricsInfo whenever one finishes.

A WorkMetricsInfo is considered finished when the request period is complete or obsolete, either because the work finished or a new request period started (e.g. if the work request is updated or a periodic request completes a period).

getWorkMetricsInfoById

public final @NonNull List<@NonNull WorkMetricsInfogetWorkMetricsInfoById(@NonNull UUID workId)

Gets a list of WorkMetricsInfo snapshots for a given work id.

Parameters
@NonNull UUID workId

The identifier of the androidx.work.WorkRequest.

Returns
@NonNull List<@NonNull WorkMetricsInfo>

A list of WorkMetricsInfo records associated with the workId.

getWorkMetricsInfos

public final @NonNull List<@NonNull WorkMetricsInfogetWorkMetricsInfos(@NonNull WorkMetricsQuery query)

Gets a list of WorkMetricsInfo matching the query criteria.

Parameters
@NonNull WorkMetricsQuery query

The WorkMetricsQuery containing filter parameters.

Returns
@NonNull List<@NonNull WorkMetricsInfo>

A list of WorkMetricsInfo records matching the query parameters.

onCancelled

public void onCancelled(@NonNull WorkInfo workInfo)

Called when a work request is canceled from the app e.g. WorkManager.cancelWorkById.

Parameters
@NonNull WorkInfo workInfo

Snapshot of the work info

onEnqueued

public void onEnqueued(@NonNull WorkInfo workInfo)

Called when a work request is enqueued from the app e.g. WorkManager.enqueue.

Parameters
@NonNull WorkInfo workInfo

Snapshot of the work info

onException

public void onException(@NonNull Throwable throwable, @NonNull WorkInfo workInfo)

Called when the work request fails from an exception after it has started.

Parameters
@NonNull Throwable throwable

Throwable thrown by worker

@NonNull WorkInfo workInfo

Snapshot of the work info

onFinished

public void onFinished(
    @NonNull ListenableWorker.Result result,
    @NonNull WorkInfo workInfo
)

Called when the worker finishes and returns a value.

Parameters
@NonNull ListenableWorker.Result result

Result from work finishing

@NonNull WorkInfo workInfo

Snapshot of the work info

onPrerequisiteFailed

public void onPrerequisiteFailed(@NonNull WorkInfo workInfo)

Called when a work request fails because a prerequisite work fails.

Parameters
@NonNull WorkInfo workInfo

Snapshot of the work info

onStarted

public void onStarted(@NonNull WorkInfo workInfo)

Called when a work request starts executing i.e. ListenableWorker.startWork.

Parameters
@NonNull WorkInfo workInfo

Snapshot of the work info

onStopped

public void onStopped(int stopReason, @NonNull WorkInfo workInfo)

Called when a work request is stopped by the system after it has started.

Parameters
int stopReason

Reason why work stopped

@NonNull WorkInfo workInfo

Snapshot of the work info

onUnblocked

public void onUnblocked(@NonNull WorkInfo workInfo)

Called when a work request is no longer waiting on any of its prerequisite work. If the work has no prerequisite work, this is called immediately after onEnqueued.

Parameters
@NonNull WorkInfo workInfo

Snapshot of the work info

onUpdated

public void onUpdated(@NonNull WorkInfo oldWorkInfo, @NonNull WorkInfo updatedWorkInfo)

Called when a work request is updated from the app e.g. WorkManager.updateWork.

Parameters
@NonNull WorkInfo oldWorkInfo

Snapshot of the work info before the update

@NonNull WorkInfo updatedWorkInfo

Snapshot of the work info after the update