Skip to content

Commit

Permalink
Apply theme for webview
Browse files Browse the repository at this point in the history
Sets the theme for the web view based on the application theme.
Uses WebKit to check whether the web view supports the
dark mode feature. If yes,
FORCE_DARK_ON - For Dark and True black theme
FORCE_DARK_OFF - Light mode.

ForceDarkStrategy is set as
DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING.
In this mode WebView content will be darkened by a user agent
unless web page supports dark theme.


Former-commit-id: 29eb5b9
  • Loading branch information
hussainmohd-a committed Apr 15, 2021
1 parent 00dc290 commit ef65a8b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ dependencies {

//Work manager
implementation 'android.arch.work:work-runtime-ktx:1.0.1'

//Webkit
implementation 'androidx.webkit:webkit:1.4.0'

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING
import androidx.webkit.WebViewFeature
import by.kirich1409.viewbindingdelegate.viewBinding
import com.celzero.bravedns.R
import com.celzero.bravedns.database.DoHEndpointRepository
Expand Down Expand Up @@ -125,12 +128,47 @@ class DNSConfigureWebViewActivity : AppCompatActivity(R.layout.activity_faq_webv
}
})

setWebViewTheme()
}

private fun Context.isDarkThemeOn(): Boolean {
return resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}

/**
* Sets the theme for the webview based on the application theme.
* FORCE_DARK_ON - For Dark and True black theme
* FORCE_DARK_OFF - Light mode.
*/
private fun setWebViewTheme(){
if (persistentState.theme == 0) {
if (isDarkThemeOn()) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(b.configureWebview.settings, WebSettingsCompat.FORCE_DARK_ON)
}
} else {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(b.configureWebview.settings, WebSettingsCompat.FORCE_DARK_OFF)
}
}
} else if (persistentState.theme == 1) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(b.configureWebview.settings, WebSettingsCompat.FORCE_DARK_OFF)
}
} else if (persistentState.theme == 2) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(b.configureWebview.settings, WebSettingsCompat.FORCE_DARK_ON)
}
} else {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(b.configureWebview.settings, WebSettingsCompat.FORCE_DARK_ON)
}
}
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) {
WebSettingsCompat.setForceDarkStrategy(b.configureWebview.settings, DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING)
}
}

override fun onDestroy() {
if (receivedIntentFrom == LOCAL) {
updateLocalStamp()
Expand Down

0 comments on commit ef65a8b

Please sign in to comment.