Skip to content

Commit

Permalink
Base: Allow default settings to be provided in meta-data
Browse files Browse the repository at this point in the history
This is useful for apps that include microG as a library, so they can easily
modify the default configuration of their embedded microG.
  • Loading branch information
mar-v-in committed Sep 16, 2023
1 parent dd77aca commit 2c80de5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, *> = 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<String>?): Set<String>? = 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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -53,6 +55,9 @@ class SettingsProvider : ContentProvider() {
null
}
}
private val metaDataPreferences: SharedPreferences by lazy {
MetaDataPreferences(context!!, SETTINGS_PREFIX)
}

override fun onCreate(): Boolean {
return true
Expand Down Expand Up @@ -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<String>): Set<String> = listOf(unifiedNlpPreferences, preferences, systemDefaultPreferences).getStringSetCompat(key, def)

private fun SharedPreferences.getStringSetCompat(key: String, def: Set<String>): Set<String> {
Expand Down

0 comments on commit 2c80de5

Please sign in to comment.