Skip to content

Commit

Permalink
Add a preference for DNS over HTTPS
Browse files Browse the repository at this point in the history
This adds an advanced preference for disabling DNS over HTTPS when
relevant.
  • Loading branch information
ahmedre committed Nov 24, 2024
1 parent 1d5ac37 commit 13867f9
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = preferencesFlow
}
1 change: 1 addition & 0 deletions app/src/main/res/values/preferences_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
<string translatable="false" name="prefs_send_logs">sendLogsKey</string>
<string translatable="false" name="prefs_page_type">pageTypeKey</string>
<string translatable="false" name="prefs_category_dual_screen_key">dualScreenKey</string>
<string translatable="false" name="prefs_prefer_dns_over_https">preferDnsOverHttps</string>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<resources>
<string name="app_name">Quran</string>
<string name="downloadPrompt_title">Download Required Files?</string>
<string name="downloadPrompt">In order for Quran Android to work properly,
Expand Down Expand Up @@ -175,6 +175,9 @@
<string name="prefs_night_mode_text_brightness_summary">Brightness of the text when night mode is active</string>
<string name="prefs_night_mode_background_brightness_title">Background brightness</string>
<string name="prefs_night_mode_background_brightness_summary">Brightness of the page when night mode is active</string>
<string name="prefs_dns_over_https_title">DNS over HTTPS</string>
<string name="prefs_dns_over_https_summary_on">Prefer DNS over HTTPS</string>
<string name="prefs_dns_over_https_summary_off">Do not use DNS over HTTPS</string>
<string name="prefs_overlay_page_info_title">Show page info</string>
<string name="prefs_overlay_page_info_summary">Overlay page number, surah name, and juz\' number while reading</string>
<string name="prefs_display_marker_title">Display marker popups</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/quran_advanced_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
android:title="@string/prefs_app_location_title"
app:iconSpaceReserved="false"/>

<CheckBoxPreference
android:key="@string/prefs_prefer_dns_over_https"
android:persistent="true"
android:title="@string/prefs_dns_over_https_title"
android:summaryOn="@string/prefs_dns_over_https_summary_on"
android:summaryOff="@string/prefs_dns_over_https_summary_off"
app:iconSpaceReserved="false"/>

<Preference
android:key="@string/prefs_send_logs"
android:summary="@string/prefs_send_logs_summary"
Expand Down
2 changes: 2 additions & 0 deletions common/data/src/main/java/com/quran/data/dao/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface Settings {
suspend fun setShouldShowLineDividers(show: Boolean)
suspend fun setAyahTextSize(value: Int)
suspend fun translationTextSize(): Int
suspend fun preferDnsOverHttps(): Boolean
suspend fun setPreferDnsOverHttps(value: Boolean)

fun preferencesFlow(): Flow<String>
}
4 changes: 4 additions & 0 deletions common/networking/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,7 +26,7 @@ class DnsModule {
}

@Provides
fun provideServers(dnsCache: Cache): List<Dns> {
fun provideServers(dnsCache: Cache, settings: Settings): List<Dns> {
val bootstrapClient = OkHttpClient.Builder()
.cache(dnsCache)
.build()
Expand All @@ -35,7 +37,11 @@ class DnsModule {
val cloudflareDns = provideCloudflareDns(bootstrapClient)

val result = mutableListOf<Dns>()
if (cloudflareDns != null) result.add(cloudflareDns)

val preferDnsOverHttps = runBlocking { settings.preferDnsOverHttps() }
if (preferDnsOverHttps) {
if (cloudflareDns != null) result.add(cloudflareDns)
}
result.add(dnsFallback)
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 13867f9

Please sign in to comment.