# ActionAccumulator

Artifact: [androidx.appsearch:appsearch](https://developer.android.com/jetpack/androidx/releases/appsearch) [View Source](https://cs.android.com/search?q=file:androidx/appsearch/usagereporting/ActionAccumulator.java+class:androidx.appsearch.usagereporting.ActionAccumulator) Added in [1.2.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appsearch#1.2.0-alpha02)

*** ** * ** ***


```
@ExperimentalAppSearchApi
public class ActionAccumulator
```

<br />

*** ** * ** ***

Base class that accumulates `https://developer.android.com/reference/androidx/appsearch/usagereporting/SearchAction` and `https://developer.android.com/reference/androidx/appsearch/usagereporting/ClickAction` documents and stores them into AppSearch, either immediately or after a timeout.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/appsearch/usagereporting/ClickAction` |   |
| `https://developer.android.com/reference/androidx/appsearch/usagereporting/SearchAction` |   |
| `https://developer.android.com/reference/androidx/appsearch/usagereporting/TakenAction` |   |

## Summary

| ### Public methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting https://developer.android.com/reference/androidx/appsearch/usagereporting/ActionAccumulator#cancelTimer()()` Cancels timers and so we can test initialization/shutdown. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/appsearch/usagereporting/ActionAccumulator>` | `https://developer.android.com/reference/androidx/appsearch/usagereporting/ActionAccumulator#createAsync(android.content.Context,androidx.appsearch.app.AppSearchSession,java.util.concurrent.Executor)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/appsearch/app/AppSearchSession appSearchSession, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html executor )` Factory method to create an ActionAccumulator. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/File.html` | `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting https://developer.android.com/reference/androidx/appsearch/usagereporting/ActionAccumulator#getCacheFile()()` Returns the cache file to use for on-disk caching. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/appsearch/app/AppSearchBatchResult<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/Void.html>>` | `https://developer.android.com/reference/androidx/appsearch/usagereporting/ActionAccumulator#reportActionAsync(androidx.appsearch.usagereporting.TakenAction)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/appsearch/usagereporting/TakenAction takenAction)` Reports a `https://developer.android.com/reference/androidx/appsearch/usagereporting/TakenAction` and restarts the flush timer. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/appsearch/app/AppSearchBatchResult<https://developer.android.com/reference/java/lang/String.html, https://developer.android.com/reference/java/lang/Void.html>>` | `https://developer.android.com/reference/androidx/appsearch/usagereporting/ActionAccumulator#saveDocumentsToAppSearchAsync()()` Saves the documents in the cache to AppSearch and deletes the cache file. |

## Public methods

### cancelTimer

Added in [1.2.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appsearch#1.2.0-alpha02)

```
@VisibleForTesting
public void cancelTimer()
```

Cancels timers and so we can test initialization/shutdown. This simulates closing the app using the ActionAccumulator

### createAsync

Added in [1.2.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appsearch#1.2.0-alpha02)

```
public static @NonNull ListenableFuture<ActionAccumulator> createAsync(
    @NonNull Context context,
    @NonNull AppSearchSession appSearchSession,
    @NonNull Executor executor
)
```

Factory method to create an ActionAccumulator.

Initializing and using the ActionAccumulator requires that the provided `https://developer.android.com/reference/androidx/appsearch/app/AppSearchSession` has a database where `https://developer.android.com/reference/androidx/appsearch/usagereporting/SearchAction` and `https://developer.android.com/reference/androidx/appsearch/usagereporting/ClickAction` are set.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/ExecutionException.html` | if either `https://developer.android.com/reference/androidx/appsearch/usagereporting/SearchAction` or `https://developer.android.com/reference/androidx/appsearch/usagereporting/ClickAction` are not set in the provided `https://developer.android.com/reference/androidx/appsearch/app/AppSearchSession`. |

### getCacheFile

Added in [1.2.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appsearch#1.2.0-alpha02)

```
@VisibleForTesting
public @NonNull File getCacheFile()
```

Returns the cache file to use for on-disk caching.

### reportActionAsync

Added in [1.2.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appsearch#1.2.0-alpha02)

```
public @NonNull ListenableFuture<AppSearchBatchResult<String, Void>> reportActionAsync(@NonNull TakenAction takenAction)
```

Reports a `https://developer.android.com/reference/androidx/appsearch/usagereporting/TakenAction` and restarts the flush timer.

### saveDocumentsToAppSearchAsync

Added in [1.2.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appsearch#1.2.0-alpha02)

```
public @NonNull ListenableFuture<AppSearchBatchResult<String, Void>> saveDocumentsToAppSearchAsync()
```

Saves the documents in the cache to AppSearch and deletes the cache file.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/appsearch/exceptions/AppSearchException` |   |