StorageDomain.DataStorePreference


public final class StorageDomain.DataStorePreference extends StorageDomain


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 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 value 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

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

Public methods

boolean
equals(Object other)
final @NonNull String

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

final @NonNull String

The preference key to seed or verify.

final Object

The value associated with key, or null if asserting missing state.

int
final boolean

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.

@NonNull String

Public constructors

DataStorePreference

Added in 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
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
public final @NonNull String getKey()

The preference key to seed or verify.

getValue

Added in 1.0.0-alpha04
public final Object getValue()

The value associated with 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 IllegalArgumentException. 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
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()