@@ -34,17 +34,20 @@ import kotlinx.coroutines.Dispatchers
3434import kotlinx.coroutines.withContext
3535import timber.log.Timber
3636
37- internal const val OLDEST_WORKING_WEBVIEW_VERSION_CODE = 443000000L
38- internal const val OLDEST_WORKING_WEBVIEW_VERSION = 90
37+ internal const val OLDEST_WORKING_WEBVIEW_VERSION_CODE = 418306960L
38+ internal const val OLDEST_WORKING_WEBVIEW_VERSION = 85
3939
4040/* *
4141 * Shows a dialog if the current WebView version is older than the last supported version.
4242 */
43- fun checkWebviewVersion (activity : AnkiActivity ): Boolean {
44- val userVisibleCode = getChromeLikeWebViewVersionIfOutdated(activity) ? : return false
43+ fun checkWebviewVersion (
44+ activity : AnkiActivity ,
45+ minimumWebViewVersion : Int = OLDEST_WORKING_WEBVIEW_VERSION ,
46+ ): Boolean {
47+ val userVisibleCode = getChromeLikeWebViewVersionIfOutdated(activity, minimumWebViewVersion) ? : return false
4548
4649 // Provide guidance to the user if the WebView is outdated
47- val webviewPackageInfo = getAndroidSystemWebViewPackageInfo (activity.packageManager )
50+ val webviewPackageInfo = WebViewCompat .getCurrentWebViewPackage (activity)
4851 val legacyWebViewPackageInfo = getLegacyWebViewPackageInfo(activity.packageManager)
4952 // TODO modify the alert dialog text to handle the usage of developer builds for system WebView
5053 if (legacyWebViewPackageInfo != null ) {
@@ -78,16 +81,25 @@ fun getWebviewUserAgent(context: Context): String? {
7881 * Returns a Chrome-like WebView version if it is outdated, otherwise null if
7982 * cannot be determined at all or if okay
8083 */
81- private fun getChromeLikeWebViewVersionIfOutdated (activity : AnkiActivity ): Int? {
84+ private fun getChromeLikeWebViewVersionIfOutdated (
85+ activity : AnkiActivity ,
86+ minimumWebViewVersion : Int ,
87+ ): Int? {
8288 // If we cannot get the package information at all, return null
83- val webviewPackageInfo = getAndroidSystemWebViewPackageInfo (activity.packageManager ) ? : return null
89+ val webviewPackageInfo = WebViewCompat .getCurrentWebViewPackage (activity) ? : return null
8490 val webviewVersion =
8591 webviewPackageInfo.versionName ? : run {
8692 Timber .w(" Failed to obtain WebView version" )
8793 return null
8894 }
8995 val versionCode = PackageInfoCompat .getLongVersionCode(webviewPackageInfo)
90- return checkWebViewVersionComponents(webviewPackageInfo.packageName, webviewVersion, versionCode, getWebviewUserAgent(activity))
96+ return checkWebViewVersionComponents(
97+ webviewPackageInfo.packageName,
98+ webviewVersion,
99+ versionCode,
100+ getWebviewUserAgent(activity),
101+ minimumWebViewVersion,
102+ )
91103}
92104
93105@VisibleForTesting
@@ -96,6 +108,7 @@ fun checkWebViewVersionComponents(
96108 webviewVersion : String ,
97109 versionCode : Long ,
98110 userAgent : String? ,
111+ minimumWebViewVersion : Int = OLDEST_WORKING_WEBVIEW_VERSION ,
99112): Int? {
100113 // Sometimes the webview version code appears too old, and the package name does as well,
101114 // but it's a webview that advertises modern capabilities via User-Agent in "Chrome" section
@@ -104,7 +117,7 @@ fun checkWebViewVersionComponents(
104117 val chromeRegex = """ Chrome/(\d+)""" .toRegex()
105118 val matchResult = chromeRegex.find(userAgent)?.groupValues?.get(1 )
106119 matchResult?.toInt()?.let {
107- if (it >= OLDEST_WORKING_WEBVIEW_VERSION ) {
120+ if (it >= minimumWebViewVersion ) {
108121 // If the User-Agent says we are modern, trust it and skip further checks.
109122 return null
110123 } else {
@@ -173,26 +186,6 @@ private fun getLegacyWebViewPackageInfo(packageManager: PackageManager): Package
173186 null
174187 }
175188
176- /* *
177- * Returns a [PackageInfo] from the current system WebView, or `null` if unavailable
178- */
179- private fun getAndroidSystemWebViewPackageInfo (packageManager : PackageManager ): PackageInfo ? {
180- fun getPackage (packageName : String ): PackageInfo ? =
181- try {
182- packageManager.getPackageInfo(packageName, 0 )
183- } catch (_: PackageManager .NameNotFoundException ) {
184- null
185- }
186-
187- // The WebView is called com.android.webview by default.
188- // Partner devices which ship with Google applications ship the Google-specific version
189- // of the WebView called com.google.android.webview.
190- // https://issues.chromium.org/issues/40419837#comment10
191-
192- return getPackage(" com.google.android.webview" )
193- ? : getPackage(" com.android.webview" ) // com.android.webview is used on API 24
194- }
195-
196189/* *
197190 * Enables debugging of web contents (HTML / CSS / JavaScript)
198191 * loaded into any WebViews of this application. This flag can be enabled
0 commit comments