-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65b76fe
commit edfb29b
Showing
18 changed files
with
228 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/main/java/com/forcetower/uefs/domain/usecase/device/GetDeviceInfoUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.forcetower.uefs.domain.usecase.device | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import dagger.Reusable | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.runBlocking | ||
import java.security.MessageDigest | ||
import java.util.UUID | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
|
||
@Reusable | ||
class GetDeviceInfoUseCase @Inject constructor( | ||
@Named("settings") private val settings: DataStore<Preferences> | ||
) { | ||
private val deviceId = stringPreferencesKey("device_id") | ||
|
||
@Inject | ||
fun generateMachineId(): String { | ||
return runBlocking { | ||
settings.data.first()[deviceId] ?: run { | ||
val generated = UUID.randomUUID().toString() | ||
settings.edit { it[deviceId] = generated } | ||
generated | ||
} | ||
} | ||
} | ||
|
||
fun machineIdDirect(): String { | ||
return runBlocking { | ||
settings.data.first()[deviceId]?.let { str -> | ||
val bytes = MessageDigest.getInstance("MD5").digest(str.toByteArray(Charsets.UTF_8)) | ||
bytes.joinToString(separator = "") { byte -> "%02x".format(byte) } | ||
} ?: UNINITIALIZED | ||
} | ||
} | ||
|
||
fun machineId(): Flow<String> { | ||
return settings.data.map { | ||
it[deviceId]?.let { str -> | ||
val bytes = MessageDigest.getInstance("MD5").digest(str.toByteArray(Charsets.UTF_8)) | ||
bytes.joinToString(separator = "") { byte -> "%02x".format(byte) } | ||
} ?: UNINITIALIZED | ||
} | ||
} | ||
|
||
private companion object { | ||
const val UNINITIALIZED = "unavailable" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.