Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 47720085 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

DroidGuard/SafetyNet/Location: Improve configurability and fix defaults

parent 11a8aa7b
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -123,11 +123,13 @@ object SettingsContract {
        const val ENABLED = "droidguard_enabled"
        const val MODE = "droidguard_mode"
        const val NETWORK_SERVER_URL = "droidguard_network_server_url"
        const val FORCE_LOCAL_DISABLED = "droidguard_force_local_disabled"

        val PROJECTION = arrayOf(
            ENABLED,
            MODE,
            NETWORK_SERVER_URL
            NETWORK_SERVER_URL,
            FORCE_LOCAL_DISABLED,
        )
    }

@@ -155,6 +157,7 @@ object SettingsContract {
        const val WIFI_LEARNING = "location_wifi_learning"
        const val CELL_MLS = "location_cell_mls"
        const val CELL_LEARNING = "location_cell_learning"
        const val GEOCODER_NOMINATIM = "location_geocoder_nominatim"

        val PROJECTION = arrayOf(
            WIFI_MLS,
@@ -162,6 +165,7 @@ object SettingsContract {
            WIFI_LEARNING,
            CELL_MLS,
            CELL_LEARNING,
            GEOCODER_NOMINATIM,
        )
    }

+34 −5
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import android.content.SharedPreferences
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import android.os.Build.VERSION.SDK_INT
import android.preference.PreferenceManager
import org.microg.gms.common.PackageUtils.warnIfNotMainProcess
import org.microg.gms.settings.SettingsContract.Auth
@@ -38,6 +39,9 @@ class SettingsProvider : ContentProvider() {
    private val checkInPrefs by lazy {
        context!!.getSharedPreferences(CheckIn.PREFERENCES_NAME, MODE_PRIVATE)
    }
    private val unifiedNlpPreferences by lazy {
        context!!.getSharedPreferences("unified_nlp", MODE_PRIVATE)
    }
    private val systemDefaultPreferences: SharedPreferences? by lazy {
        try {
            Context::class.java.getDeclaredMethod(
@@ -252,6 +256,7 @@ class SettingsProvider : ContentProvider() {
            DroidGuard.ENABLED -> getSettingsBoolean(key, false)
            DroidGuard.MODE -> getSettingsString(key)
            DroidGuard.NETWORK_SERVER_URL -> getSettingsString(key)
            DroidGuard.FORCE_LOCAL_DISABLED -> systemDefaultPreferences?.getBoolean(key, false) ?: false
            else -> throw IllegalArgumentException("Unknown key: $key")
        }
    }
@@ -293,14 +298,18 @@ class SettingsProvider : ContentProvider() {

    private fun queryLocation(p: Array<out String>): Cursor = MatrixCursor(p).addRow(p) { key ->
        when (key) {
            Location.WIFI_MLS -> getSettingsBoolean(key, true)
            Location.WIFI_MOVING -> getSettingsBoolean(key, true)
            Location.WIFI_LEARNING -> getSettingsBoolean(key, true)
            Location.CELL_MLS -> getSettingsBoolean(key, true)
            Location.CELL_LEARNING -> getSettingsBoolean(key, true)
            Location.WIFI_MLS -> getSettingsBoolean(key, hasUnifiedNlpLocationBackend("org.microg.nlp.backend.ichnaea"))
            Location.WIFI_MOVING -> getSettingsBoolean(key, hasUnifiedNlpLocationBackend("de.sorunome.unifiednlp.trains"))
            Location.WIFI_LEARNING -> getSettingsBoolean(key, hasUnifiedNlpLocationBackend("helium314.localbackend", "org.fitchfamily.android.dejavu"))
            Location.CELL_MLS -> getSettingsBoolean(key, hasUnifiedNlpLocationBackend("org.microg.nlp.backend.ichnaea"))
            Location.CELL_LEARNING -> getSettingsBoolean(key, hasUnifiedNlpLocationBackend("helium314.localbackend", "org.fitchfamily.android.dejavu"))
            Location.GEOCODER_NOMINATIM -> getSettingsBoolean(key, hasUnifiedNlpGeocoderBackend("org.microg.nlp.backend.nominatim") )
            else -> throw IllegalArgumentException("Unknown key: $key")
        }
    }
    private fun hasUnifiedNlpPrefixInStringSet(key: String, vararg prefixes: String) = getUnifiedNlpSettingsStringSetCompat(key, emptySet()).any { entry -> prefixes.any { prefix -> entry.startsWith(prefix)}}
    private fun hasUnifiedNlpLocationBackend(vararg packageNames: String) = hasUnifiedNlpPrefixInStringSet("location_backends", *packageNames.map { "$it/" }.toTypedArray())
    private fun hasUnifiedNlpGeocoderBackend(vararg packageNames: String) = hasUnifiedNlpPrefixInStringSet("geocoder_backends", *packageNames.map { "$it/" }.toTypedArray())

    private fun updateLocation(values: ContentValues) {
        if (values.size() == 0) return
@@ -351,7 +360,27 @@ class SettingsProvider : ContentProvider() {
    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 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> {
        if (SDK_INT >= 11) {
            try {
                val res = getStringSet(key, null)
                if (res != null) return res.filter { it.isNotEmpty() }.toSet()
            } catch (ignored: Exception) {
                // Ignore
            }
        }
        try {
            val str = getString(key, null)
            if (str != null) return str.split("\\|".toRegex()).filter { it.isNotEmpty() }.toSet()
        } catch (ignored: Exception) {
            // Ignore
        }
        return def
    }

    private fun List<SharedPreferences?>.getStringSetCompat(key: String, def: Set<String>): Set<String> = foldRight(def) { preferences, defValue -> preferences?.getStringSetCompat(key, defValue) ?: defValue }
    private fun List<SharedPreferences?>.getString(key: String, def: String?): String? = foldRight(def) { preferences, defValue -> preferences?.getString(key, defValue) ?: defValue }
    private fun List<SharedPreferences?>.getInt(key: String, def: Int): Int = foldRight(def) { preferences, defValue -> preferences?.getInt(key, defValue) ?: defValue }
    private fun List<SharedPreferences?>.getLong(key: String, def: Long): Long = foldRight(def) { preferences, defValue -> preferences?.getLong(key, defValue) ?: defValue }
+4 −0
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ class SafetyNetFragment : PreferenceFragmentCompat() {
    private lateinit var apps: PreferenceCategory
    private lateinit var appsAll: Preference
    private lateinit var appsNone: Preference
    private lateinit var droidguardUnsupported: Preference

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.preferences_safetynet)
@@ -62,6 +63,7 @@ class SafetyNetFragment : PreferenceFragmentCompat() {
        apps = preferenceScreen.findPreference("prefcat_safetynet_apps") ?: apps
        appsAll = preferenceScreen.findPreference("pref_safetynet_apps_all") ?: appsAll
        appsNone = preferenceScreen.findPreference("pref_safetynet_apps_none") ?: appsNone
        droidguardUnsupported = preferenceScreen.findPreference("pref_droidguard_unsupported") ?: droidguardUnsupported

        runAttest.isVisible = SAFETYNET_API_KEY != null
        runReCaptcha.isVisible = RECAPTCHA_SITE_KEY != null
@@ -75,6 +77,7 @@ class SafetyNetFragment : PreferenceFragmentCompat() {
            val newStatus = newValue as Boolean
            SafetyNetPreferences.setEnabled(requireContext(), newStatus)
            DroidGuardPreferences.setEnabled(requireContext(), newStatus)
            droidguardUnsupported.isVisible = switchBarPreference.isChecked && !DroidGuardPreferences.isAvailable(requireContext())
            true
        }
    }
@@ -218,6 +221,7 @@ class SafetyNetFragment : PreferenceFragmentCompat() {

        switchBarPreference.isEnabled = CheckinPreferences.isEnabled(requireContext())
        switchBarPreference.isChecked = SafetyNetPreferences.isEnabled(requireContext()) && DroidGuardPreferences.isEnabled(requireContext())
        droidguardUnsupported.isVisible = switchBarPreference.isChecked && !DroidGuardPreferences.isAvailable(requireContext())

        updateContent()
    }
+1 −0
Original line number Diff line number Diff line
@@ -181,6 +181,7 @@ This can take a couple of minutes."</string>
    <string name="pref_test_summary_warn">Warning: %s</string>
    <string name="pref_test_summary_running">Running…</string>
    <string name="pref_droidguard_operation_mode">Operation mode</string>
    <string name="pref_droidguard_unsupported_summary">DroidGuard execution is unsupported on this device. SafetyNet services may misbehave.</string>
    <string name="prefcat_safetynet_apps_title">Apps using SafetyNet</string>
    <string name="menu_clear_recent_requests">Clear recent requests</string>
    <string name="safetynet_last_run_at">Last use: <xliff:g example="Yesterday, 02:20 PM">%1$s</xliff:g></string>
+7 −0
Original line number Diff line number Diff line
@@ -50,5 +50,12 @@
            android:key="pref_safetynet_summary"
            android:selectable="false"
            android:summary="@string/safetynet_intro" />
        <Preference
            app:isPreferenceVisible="false"
            tools:isPreferenceVisible="true"
            android:icon="@drawable/ic_alert"
            android:key="pref_droidguard_unsupported"
            android:selectable="false"
            android:summary="@string/pref_droidguard_unsupported_summary" />
    </PreferenceCategory>
</PreferenceScreen>
Loading