From 2c80de55d132a91c25036880517c5605c6753f71 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 16 Sep 2023 23:00:30 +0200 Subject: [PATCH] Base: Allow default settings to be provided in meta-data This is useful for apps that include microG as a library, so they can easily modify the default configuration of their embedded microG. --- .../gms/settings/MetaDataPreferences.kt | 45 +++++++++++++++++++ .../microg/gms/settings/SettingsProvider.kt | 13 ++++-- 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 play-services-base/core/src/main/kotlin/org/microg/gms/settings/MetaDataPreferences.kt diff --git a/play-services-base/core/src/main/kotlin/org/microg/gms/settings/MetaDataPreferences.kt b/play-services-base/core/src/main/kotlin/org/microg/gms/settings/MetaDataPreferences.kt new file mode 100644 index 0000000000..1e21f6d9b5 --- /dev/null +++ b/play-services-base/core/src/main/kotlin/org/microg/gms/settings/MetaDataPreferences.kt @@ -0,0 +1,45 @@ +/* + * SPDX-FileCopyrightText: 2023 microG Project Team + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.microg.gms.settings + +import android.content.Context +import android.content.SharedPreferences +import android.content.pm.PackageManager.GET_META_DATA +import android.os.Bundle + +class MetaDataPreferences(private val context: Context, private val prefix: String = "") : SharedPreferences { + private val metaData by lazy { + runCatching { context.packageManager.getApplicationInfo(context.packageName, GET_META_DATA) }.getOrNull()?.metaData ?: Bundle.EMPTY + } + + override fun getAll(): Map = metaData.keySet().filter { it.startsWith(prefix) }.associate { it.substring(prefix.length) to metaData.get(it) } + + override fun getString(key: String, defValue: String?): String? = metaData.getString(prefix + key, defValue) + + override fun getStringSet(key: String, defValues: Set?): Set? = metaData.getStringArray(prefix + key)?.toSet() ?: defValues + + override fun getInt(key: String?, defValue: Int): Int = metaData.getInt(prefix + key, defValue) + + override fun getLong(key: String?, defValue: Long): Long = metaData.getLong(prefix + key, defValue) + + override fun getFloat(key: String?, defValue: Float): Float = metaData.getFloat(prefix + key, defValue) + + override fun getBoolean(key: String?, defValue: Boolean): Boolean = metaData.getBoolean(prefix + key, defValue) + + override fun contains(key: String?): Boolean = metaData.containsKey(prefix + key) + + override fun edit(): SharedPreferences.Editor { + throw UnsupportedOperationException() + } + + override fun registerOnSharedPreferenceChangeListener(listener: SharedPreferences.OnSharedPreferenceChangeListener?) { + throw UnsupportedOperationException() + } + + override fun unregisterOnSharedPreferenceChangeListener(listener: SharedPreferences.OnSharedPreferenceChangeListener?) { + throw UnsupportedOperationException() + } +} \ No newline at end of file diff --git a/play-services-base/core/src/main/kotlin/org/microg/gms/settings/SettingsProvider.kt b/play-services-base/core/src/main/kotlin/org/microg/gms/settings/SettingsProvider.kt index 0ff85092b9..0e2948a90b 100644 --- a/play-services-base/core/src/main/kotlin/org/microg/gms/settings/SettingsProvider.kt +++ b/play-services-base/core/src/main/kotlin/org/microg/gms/settings/SettingsProvider.kt @@ -27,6 +27,8 @@ import org.microg.gms.settings.SettingsContract.SafetyNet import org.microg.gms.settings.SettingsContract.getAuthority import java.io.File +private const val SETTINGS_PREFIX = "org.microg.gms.settings." + /** * All settings access should go through this [ContentProvider], * because it provides safe access from different processes which normal [SharedPreferences] don't. @@ -53,6 +55,9 @@ class SettingsProvider : ContentProvider() { null } } + private val metaDataPreferences: SharedPreferences by lazy { + MetaDataPreferences(context!!, SETTINGS_PREFIX) + } override fun onCreate(): Boolean { return true @@ -355,12 +360,12 @@ class SettingsProvider : ContentProvider() { * @return the current setting as [Int], because [ContentProvider] does not support [Boolean]. */ private fun getSettingsBoolean(key: String, def: Boolean): Int { - return listOf(preferences, systemDefaultPreferences).getBooleanAsInt(key, def) + return listOf(preferences, systemDefaultPreferences, metaDataPreferences).getBooleanAsInt(key, def) } - private fun getSettingsString(key: String, def: String? = null): String? = listOf(preferences, systemDefaultPreferences).getString(key, def) - private fun getSettingsInt(key: String, def: Int): Int = listOf(preferences, systemDefaultPreferences).getInt(key, def) - private fun getSettingsLong(key: String, def: Long): Long = listOf(preferences, systemDefaultPreferences).getLong(key, def) + private fun getSettingsString(key: String, def: String? = null): String? = listOf(preferences, systemDefaultPreferences, metaDataPreferences).getString(key, def) + private fun getSettingsInt(key: String, def: Int): Int = listOf(preferences, systemDefaultPreferences, metaDataPreferences).getInt(key, def) + private fun getSettingsLong(key: String, def: Long): Long = listOf(preferences, systemDefaultPreferences, metaDataPreferences).getLong(key, def) private fun getUnifiedNlpSettingsStringSetCompat(key: String, def: Set): Set = listOf(unifiedNlpPreferences, preferences, systemDefaultPreferences).getStringSetCompat(key, def) private fun SharedPreferences.getStringSetCompat(key: String, def: Set): Set {