From 13867f9521dd44568635a12c1fc493e789af6094 Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Sun, 24 Nov 2024 17:28:36 +0400 Subject: [PATCH] Add a preference for DNS over HTTPS This adds an advanced preference for disabling DNS over HTTPS when relevant. --- .../java/com/quran/labs/androidquran/data/Constants.kt | 1 + .../quran/labs/androidquran/util/QuranSettings.java | 8 ++++++++ .../com/quran/labs/androidquran/util/SettingsImpl.kt | 6 ++++++ app/src/main/res/values/preferences_keys.xml | 1 + app/src/main/res/values/strings.xml | 5 ++++- app/src/main/res/xml/quran_advanced_preferences.xml | 8 ++++++++ .../data/src/main/java/com/quran/data/dao/Settings.kt | 2 ++ common/networking/build.gradle.kts | 4 ++++ .../java/com/quran/common/networking/dns/DnsModule.kt | 10 ++++++++-- .../common/madani/upgrade/MadaniPreferencesUpgrade.kt | 1 + 10 files changed, 43 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/quran/labs/androidquran/data/Constants.kt b/app/src/main/java/com/quran/labs/androidquran/data/Constants.kt index 8542db33ee..d0bcce84bf 100644 --- a/app/src/main/java/com/quran/labs/androidquran/data/Constants.kt +++ b/app/src/main/java/com/quran/labs/androidquran/data/Constants.kt @@ -92,4 +92,5 @@ object Constants { const val PREF_SURA_TRANSLATED_NAME = "suraTranslatedName" const val PREF_SHOW_SIDELINES = "showSidelines" const val PREF_SHOW_LINE_DIVIDERS = "showLineDividers" + const val PREFS_PREFER_DNS_OVER_HTTPS = "preferDnsOverHttps" } diff --git a/app/src/main/java/com/quran/labs/androidquran/util/QuranSettings.java b/app/src/main/java/com/quran/labs/androidquran/util/QuranSettings.java index edcf1c0d2c..b0191e7883 100644 --- a/app/src/main/java/com/quran/labs/androidquran/util/QuranSettings.java +++ b/app/src/main/java/com/quran/labs/androidquran/util/QuranSettings.java @@ -150,6 +150,14 @@ public int getTranslationTextSize() { Constants.DEFAULT_TEXT_SIZE); } + public boolean getPreferDnsOverHttps() { + return prefs.getBoolean(Constants.PREFS_PREFER_DNS_OVER_HTTPS, true); + } + + public void setPreferDnsOverHttps(boolean preferDnsOverHttps) { + prefs.edit().putBoolean(Constants.PREFS_PREFER_DNS_OVER_HTTPS, preferDnsOverHttps).apply(); + } + public int getLastPage() { return prefs.getInt(Constants.PREF_LAST_PAGE, Constants.NO_PAGE); } diff --git a/app/src/main/java/com/quran/labs/androidquran/util/SettingsImpl.kt b/app/src/main/java/com/quran/labs/androidquran/util/SettingsImpl.kt index b9a41e02d2..bdb26b5cc3 100644 --- a/app/src/main/java/com/quran/labs/androidquran/util/SettingsImpl.kt +++ b/app/src/main/java/com/quran/labs/androidquran/util/SettingsImpl.kt @@ -98,5 +98,11 @@ class SettingsImpl @Inject constructor(private val quranSettings: QuranSettings) override suspend fun translationTextSize(): Int = quranSettings.translationTextSize + override suspend fun preferDnsOverHttps(): Boolean = quranSettings.preferDnsOverHttps + + override suspend fun setPreferDnsOverHttps(value: Boolean) { + quranSettings.preferDnsOverHttps = value + } + override fun preferencesFlow(): Flow = preferencesFlow } diff --git a/app/src/main/res/values/preferences_keys.xml b/app/src/main/res/values/preferences_keys.xml index ddf743281c..be3b71258c 100644 --- a/app/src/main/res/values/preferences_keys.xml +++ b/app/src/main/res/values/preferences_keys.xml @@ -38,4 +38,5 @@ sendLogsKey pageTypeKey dualScreenKey + preferDnsOverHttps diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 98bc77ef20..5986433a6e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,5 @@ - + Quran Download Required Files? In order for Quran Android to work properly, @@ -175,6 +175,9 @@ Brightness of the text when night mode is active Background brightness Brightness of the page when night mode is active + DNS over HTTPS + Prefer DNS over HTTPS + Do not use DNS over HTTPS Show page info Overlay page number, surah name, and juz\' number while reading Display marker popups diff --git a/app/src/main/res/xml/quran_advanced_preferences.xml b/app/src/main/res/xml/quran_advanced_preferences.xml index 9d92aaf800..5dde4a9f1a 100644 --- a/app/src/main/res/xml/quran_advanced_preferences.xml +++ b/app/src/main/res/xml/quran_advanced_preferences.xml @@ -28,6 +28,14 @@ android:title="@string/prefs_app_location_title" app:iconSpaceReserved="false"/> + + } diff --git a/common/networking/build.gradle.kts b/common/networking/build.gradle.kts index cb05db57d0..cfedecd409 100644 --- a/common/networking/build.gradle.kts +++ b/common/networking/build.gradle.kts @@ -12,8 +12,12 @@ anvil { android.namespace = "com.quran.common.networking" dependencies { + implementation(project(":common:data")) implementation(libs.dagger.runtime) + implementation(libs.kotlinx.coroutines.core) + implementation(libs.kotlinx.coroutines.android) + implementation(libs.dnsjava) implementation(libs.okhttp.dnsoverhttps) api(libs.okhttp.tls) diff --git a/common/networking/src/main/java/com/quran/common/networking/dns/DnsModule.kt b/common/networking/src/main/java/com/quran/common/networking/dns/DnsModule.kt index 1ad8e8c316..23a1da57cd 100644 --- a/common/networking/src/main/java/com/quran/common/networking/dns/DnsModule.kt +++ b/common/networking/src/main/java/com/quran/common/networking/dns/DnsModule.kt @@ -1,7 +1,9 @@ package com.quran.common.networking.dns +import com.quran.data.dao.Settings import dagger.Module import dagger.Provides +import kotlinx.coroutines.runBlocking import okhttp3.Cache import okhttp3.Dns import okhttp3.HttpUrl.Companion.toHttpUrl @@ -24,7 +26,7 @@ class DnsModule { } @Provides - fun provideServers(dnsCache: Cache): List { + fun provideServers(dnsCache: Cache, settings: Settings): List { val bootstrapClient = OkHttpClient.Builder() .cache(dnsCache) .build() @@ -35,7 +37,11 @@ class DnsModule { val cloudflareDns = provideCloudflareDns(bootstrapClient) val result = mutableListOf() - if (cloudflareDns != null) result.add(cloudflareDns) + + val preferDnsOverHttps = runBlocking { settings.preferDnsOverHttps() } + if (preferDnsOverHttps) { + if (cloudflareDns != null) result.add(cloudflareDns) + } result.add(dnsFallback) return result } diff --git a/pages/common/madani/src/main/kotlin/com/quran/labs/androidquran/pages/common/madani/upgrade/MadaniPreferencesUpgrade.kt b/pages/common/madani/src/main/kotlin/com/quran/labs/androidquran/pages/common/madani/upgrade/MadaniPreferencesUpgrade.kt index 9866bcbd5e..1e871b59a7 100644 --- a/pages/common/madani/src/main/kotlin/com/quran/labs/androidquran/pages/common/madani/upgrade/MadaniPreferencesUpgrade.kt +++ b/pages/common/madani/src/main/kotlin/com/quran/labs/androidquran/pages/common/madani/upgrade/MadaniPreferencesUpgrade.kt @@ -16,6 +16,7 @@ class MadaniPreferencesUpgrade @Inject constructor(private val settings: Setting scope.launch(Dispatchers.Main.immediate) { if (from <= 3441) { settings.setAyahTextSize(settings.translationTextSize()) + settings.setPreferDnsOverHttps(true) } } return true