# StorageDomain.DataStorePreference

Artifact: [androidx.test.backup:backup-host](https://developer.android.com/jetpack/androidx/releases/test-backup) [View Source](https://cs.android.com/search?q=file:androidx/test/backup/host/StorageDomain.kt+class:androidx.test.backup.host.StorageDomain.DataStorePreference) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

*** ** * ** ***


```
public final class StorageDomain.DataStorePreference extends StorageDomain
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.test.backup.host.StorageDomain](https://developer.android.com/reference/androidx/test/backup/host/StorageDomain) ||
|   | ↳ | [androidx.test.backup.host.StorageDomain.DataStorePreference](https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference) |

*** ** * ** ***

Targets a preference of an on-device Preferences DataStore.

The DataStore file is `datastore/<dataStoreName>.preferences_pb` in the files directory of the app, where `preferencesDataStore(dataStoreName)` keeps it. With `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#isDeviceProtectedStorage()`, it's that path in the files directory of device-protected storage, which `deviceProtectedDataStoreFile("<dataStoreName>.preferences_pb")` returns. `preferencesDataStoreFile` always returns the first path, even for a device-protected context.

The Kotlin type of `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#(kotlin.String,kotlin.String,kotlin.Any,kotlin.Boolean)` sets the type of the preference key, such as `1.5f` for `floatPreferencesKey` and `1.5` for `doublePreferencesKey`. Use the type of your app's key: with another type, the verification passes, but your app can't read the value.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#DataStorePreference(kotlin.String,kotlin.String,kotlin.Any,kotlin.Boolean)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html dataStoreName, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html key, https://developer.android.com/reference/java/lang/Object.html value, boolean isDeviceProtectedStorage )` |

| ### Public methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#equals(kotlin.Any)(https://developer.android.com/reference/java/lang/Object.html other)` |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#getDataStoreName()()` The name of the Preferences DataStore, as the app passes it to `preferencesDataStore`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#getKey()()` The preference key to seed or verify. |
| `final https://developer.android.com/reference/java/lang/Object.html` | `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#getValue()()` The value associated with `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#key()`, or `null` if asserting missing state. |
| `int` | `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#hashCode()()` |
| `final boolean` | `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#isDeviceProtectedStorage()()` Whether the Preferences DataStore file is in device-protected storage, which is available before the user first unlocks the device, rather than in the default storage of the app. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#toString()()` |

## Public constructors

### DataStorePreference

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
public DataStorePreference(
    @NonNull String dataStoreName,
    @NonNull String key,
    Object value,
    boolean isDeviceProtectedStorage
)
```

## Public methods

### equals

```
public boolean equals(Object other)
```

### getDataStoreName

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
public final @NonNull String getDataStoreName()
```

The name of the Preferences DataStore, as the app passes it to `preferencesDataStore`.

### getKey

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
public final @NonNull String getKey()
```

The preference key to seed or verify.

### getValue

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
public final Object getValue()
```

The value associated with `https://developer.android.com/reference/androidx/test/backup/host/StorageDomain.DataStorePreference#key()`, or `null` if asserting missing state. Supported types are: String, Int, Long, Float, Double, Boolean, ByteArray, a Set of Strings, or null; other types fail construction with an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. The domain keeps a copy of a set or byte array, and returns a new copy of a byte array on each read. A `null` value is seeded by removing the key and verified by checking that the key is absent. Clearing app data also removes the key, so a `null` value cannot detect a restore that loses data.

### hashCode

```
public int hashCode()
```

### isDeviceProtectedStorage

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
public final boolean isDeviceProtectedStorage()
```

Whether the Preferences DataStore file is in device-protected storage, which is available before the user first unlocks the device, rather than in the default storage of the app. That is credential-encrypted storage, unless the app sets `android:defaultToDeviceProtectedStorage`.

### toString

```
public @NonNull String toString()
```