SDK: fix storing permission grants
This commit is contained in:
parent
a4aa4120de
commit
bba805ccd6
@ -1,25 +1,40 @@
|
||||
package de.mm20.launcher2.sdk.permissions
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.CorruptionException
|
||||
import androidx.datastore.core.Serializer
|
||||
import androidx.datastore.dataStore
|
||||
import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.decodeFromStream
|
||||
import kotlinx.serialization.json.encodeToStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
internal val Context.permissionsDataStore by dataStore(
|
||||
fileName = "plugin_permissions",
|
||||
fileName = "plugin_permissions.json",
|
||||
serializer = PermissionsSerializer,
|
||||
)
|
||||
|
||||
internal object PermissionsSerializer : Serializer<Set<String>> {
|
||||
override val defaultValue: Set<String>
|
||||
get() = emptySet()
|
||||
internal object PermissionsSerializer : Serializer<PermissionData> {
|
||||
|
||||
override suspend fun readFrom(input: InputStream): Set<String> {
|
||||
return input.bufferedReader().readLines().toSet()
|
||||
override val defaultValue: PermissionData
|
||||
get() = PermissionData()
|
||||
|
||||
override suspend fun readFrom(input: InputStream): PermissionData {
|
||||
try {
|
||||
return Json.decodeFromStream(input)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
throw (CorruptionException("Cannot read json.", e))
|
||||
} catch (e: SerializationException) {
|
||||
throw (CorruptionException("Cannot read json.", e))
|
||||
} catch (e: IOException) {
|
||||
throw (CorruptionException("Cannot read json.", e))
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun writeTo(t: Set<String>, output: OutputStream) {
|
||||
output.bufferedWriter().write(t.joinToString("\n"))
|
||||
override suspend fun writeTo(t: PermissionData, output: OutputStream) {
|
||||
Json.encodeToStream(t, output)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
package de.mm20.launcher2.sdk.permissions
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class PermissionData(
|
||||
val granted: Set<String> = emptySet(),
|
||||
)
|
||||
@ -11,13 +11,15 @@ class PluginPermissionManager(
|
||||
private val dataStore = context.applicationContext.permissionsDataStore
|
||||
|
||||
fun hasPermission(pluginPackage: String): Flow<Boolean> {
|
||||
return dataStore.data.map { it.contains(pluginPackage) }
|
||||
return dataStore.data.map { it.granted.contains(pluginPackage) }
|
||||
}
|
||||
|
||||
fun grantPermission(pluginPackage: String) {
|
||||
runBlocking {
|
||||
dataStore.updateData {
|
||||
it + pluginPackage
|
||||
it.copy(
|
||||
granted = it.granted + pluginPackage
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,7 +27,9 @@ class PluginPermissionManager(
|
||||
fun revokePermission(pluginPackage: String) {
|
||||
runBlocking {
|
||||
dataStore.updateData {
|
||||
it - pluginPackage
|
||||
it.copy(
|
||||
granted = it.granted - pluginPackage
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user