Skip to content

Commit b4f53ea

Browse files
committed
feat: verify webview version and prevent loading blank screen
1 parent a626706 commit b4f53ea

2 files changed

Lines changed: 29 additions & 20 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/pages/PageFragment.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ import androidx.core.net.toUri
2626
import androidx.fragment.app.Fragment
2727
import com.google.android.material.appbar.MaterialToolbar
2828
import com.google.android.material.progressindicator.CircularProgressIndicator
29+
import com.ichi2.anki.AnkiActivity
2930
import com.ichi2.anki.R
3031
import com.ichi2.anki.workarounds.OnWebViewRecreatedListener
3132
import com.ichi2.anki.workarounds.SafeWebViewLayout
3233
import com.ichi2.themes.Themes
34+
import com.ichi2.utils.checkWebviewVersion
3335
import timber.log.Timber
3436

3537
/**
@@ -100,6 +102,12 @@ abstract class PageFragment(
100102
view: View,
101103
savedInstanceState: Bundle?,
102104
) {
105+
val ankiActivity = requireActivity() as AnkiActivity
106+
if (checkWebviewVersion(ankiActivity)) {
107+
Timber.w("Aborting PageFragement load: WebView is outdated")
108+
ankiActivity.onBackPressedDispatcher.onBackPressed()
109+
return
110+
}
103111
server = AnkiServer(this).also { it.start() }
104112
webViewLayout = view.findViewById(R.id.webview_layout)
105113

AnkiDroid/src/main/java/com/ichi2/utils/WebViewUtils.kt

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ import kotlinx.coroutines.Dispatchers
3434
import kotlinx.coroutines.withContext
3535
import timber.log.Timber
3636

37-
internal const val OLDEST_WORKING_WEBVIEW_VERSION_CODE = 418306960L
38-
internal const val OLDEST_WORKING_WEBVIEW_VERSION = 85
37+
internal const val OLDEST_WORKING_WEBVIEW_VERSION_CODE = 443000000L
38+
internal const val OLDEST_WORKING_WEBVIEW_VERSION = 90
3939

4040
/**
4141
* Shows a dialog if the current WebView version is older than the last supported version.
4242
*/
43-
fun checkWebviewVersion(activity: AnkiActivity) {
44-
val userVisibleCode = getChromeLikeWebViewVersionIfOutdated(activity) ?: return
43+
fun checkWebviewVersion(activity: AnkiActivity): Boolean {
44+
val userVisibleCode = getChromeLikeWebViewVersionIfOutdated(activity) ?: return false
4545

4646
// Provide guidance to the user if the WebView is outdated
4747
val webviewPackageInfo = getAndroidSystemWebViewPackageInfo(activity.packageManager)
@@ -54,6 +54,7 @@ fun checkWebviewVersion(activity: AnkiActivity) {
5454
Timber.w("WebView is outdated. %s: %s", webviewPackageInfo?.packageName, webviewPackageInfo?.versionName)
5555
showOutdatedWebViewDialog(activity, userVisibleCode, R.string.link_webview_update)
5656
}
57+
return true
5758
}
5859

5960
@MainThread
@@ -96,33 +97,33 @@ fun checkWebViewVersionComponents(
9697
versionCode: Long,
9798
userAgent: String?,
9899
): Int? {
99-
// Checking the version code works for most webview packages
100-
if (versionCode >= OLDEST_WORKING_WEBVIEW_VERSION_CODE) {
101-
Timber.d(
102-
"WebView is up to date. %s: %s(%s)",
103-
packageName,
104-
webviewVersion,
105-
versionCode.toString(),
106-
)
107-
return null
108-
}
109-
110100
// Sometimes the webview version code appears too old, and the package name does as well,
111101
// but it's a webview that advertises modern capabilities via User-Agent in "Chrome" section
112102
// Our warning is purely advisory, so, let's let those through if User-Agent looks okay
113103
userAgent?.let {
114104
val chromeRegex = """Chrome/(\d+)""".toRegex()
115105
val matchResult = chromeRegex.find(userAgent)?.groupValues?.get(1)
116106
matchResult?.toInt()?.let {
117-
if (it < OLDEST_WORKING_WEBVIEW_VERSION) {
118-
// If we got here, even the User-Agent says it's incompatible, return something
119-
// potentially useful to the user as a browser version
107+
if (it >= OLDEST_WORKING_WEBVIEW_VERSION) {
108+
// If the User-Agent says we are modern, trust it and skip further checks.
109+
return null
110+
} else {
111+
// If the User-Agent is explicitly below the floor, return it immediately.
120112
return it
121113
}
122114
}
123115
}
124-
125-
return null
116+
// Checking the version code works for most webview packages
117+
if (versionCode >= OLDEST_WORKING_WEBVIEW_VERSION_CODE) {
118+
Timber.d(
119+
"WebView is up to date. %s: %s(%s)",
120+
packageName,
121+
webviewVersion,
122+
versionCode.toString(),
123+
)
124+
return null
125+
}
126+
return webviewVersion.split('.').firstOrNull()?.toIntOrNull()
126127
}
127128

128129
data class WebViewInfo(

0 commit comments

Comments
 (0)