Skip to content

Commit ff5a47d

Browse files
committed
refactor: use WebViewVersion and WebViewVersionCode value classes and use finish() in PageFragement.kt
1 parent 4f484af commit ff5a47d

5 files changed

Lines changed: 27 additions & 14 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ import com.ichi2.anki.R
2626
import com.ichi2.anki.SingleFragmentActivity
2727
import com.ichi2.anki.hideShowButtonCss
2828
import com.ichi2.utils.OLDEST_WORKING_WEBVIEW_VERSION
29+
import com.ichi2.utils.WebViewVersion
2930

3031
class AnkiPackageImporterFragment : PageFragment() {
3132
override val pagePath: String by lazy {
3233
val filePath = requireArguments().getString(KEY_FILE_PATH)
3334
"import-anki-package$filePath"
3435
}
3536

36-
override val minimumWebViewVersion: Int = OLDEST_WORKING_WEBVIEW_VERSION
37+
override val minimumWebViewVersion: WebViewVersion = OLDEST_WORKING_WEBVIEW_VERSION
3738

3839
override fun onCreateWebViewClient(savedInstanceState: Bundle?): PageWebViewClient {
3940
// the back callback is only enabled when import is running and showing progress

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.ichi2.anki.R
2828
import com.ichi2.anki.SingleFragmentActivity
2929
import com.ichi2.anki.hideShowButtonCss
3030
import com.ichi2.utils.OLDEST_WORKING_WEBVIEW_VERSION
31+
import com.ichi2.utils.WebViewVersion
3132

3233
/**
3334
* Anki page used to import text/csv files
@@ -38,7 +39,7 @@ class CsvImporter : PageFragment() {
3839
"import-csv$filePath"
3940
}
4041

41-
override val minimumWebViewVersion: Int = OLDEST_WORKING_WEBVIEW_VERSION
42+
override val minimumWebViewVersion: WebViewVersion = OLDEST_WORKING_WEBVIEW_VERSION
4243

4344
override fun onCreateWebViewClient(savedInstanceState: Bundle?): PageWebViewClient {
4445
// the back callback is only enabled when import is running and showing progress

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import com.ichi2.anki.R
3131
import com.ichi2.anki.workarounds.OnWebViewRecreatedListener
3232
import com.ichi2.anki.workarounds.SafeWebViewLayout
3333
import com.ichi2.themes.Themes
34+
import com.ichi2.utils.WebViewVersion
3435
import com.ichi2.utils.showDialogIfWebViewOutdated
3536
import timber.log.Timber
3637

@@ -66,7 +67,7 @@ abstract class PageFragment(
6667

6768
protected open fun onWebViewCreated() { }
6869

69-
protected open val minimumWebViewVersion: Int? = null
70+
protected open val minimumWebViewVersion: WebViewVersion? = null
7071

7172
/**
7273
* When the webview calls `BridgeCommand("foo")`, the PageFragment execute `bridgeCommands["foo"]`.
@@ -113,7 +114,7 @@ abstract class PageFragment(
113114
if (minVersion != null &&
114115
with(ankiActivity) {
115116
showDialogIfWebViewOutdated(minVersion) {
116-
ankiActivity.onBackPressedDispatcher.onBackPressed()
117+
ankiActivity.finish()
117118
}
118119
}
119120
) {

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

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

37+
@JvmInline
38+
value class WebViewVersion(
39+
val value: Int,
40+
)
41+
42+
@JvmInline
43+
value class WebViewVersionCode(
44+
val value: Long,
45+
)
46+
3747
/** The Android package version code which corresponds to the Webview version. */
38-
internal const val OLDEST_WORKING_WEBVIEW_VERSION_CODE = 418306960L
48+
internal val OLDEST_WORKING_WEBVIEW_VERSION_CODE = WebViewVersionCode(418306960L)
3949

4050
/** The minimum supported Webview version(Human readable). */
41-
internal const val OLDEST_WORKING_WEBVIEW_VERSION = 85
51+
internal val OLDEST_WORKING_WEBVIEW_VERSION = WebViewVersion(85)
4252

4353
/**
4454
* Shows a dialog if the current WebView version is older than the last supported version.
4555
*/
4656

4757
context(context: Context)
4858
fun showDialogIfWebViewOutdated(
49-
minimumWebViewVersion: Int = OLDEST_WORKING_WEBVIEW_VERSION,
59+
minimumWebViewVersion: WebViewVersion = OLDEST_WORKING_WEBVIEW_VERSION,
5060
onOutdated: () -> Unit = {},
5161
): Boolean {
5262
val userVisibleCode = getChromeLikeWebViewVersionIfOutdated(context, minimumWebViewVersion) ?: return false
@@ -88,7 +98,7 @@ fun getWebviewUserAgent(context: Context): String? {
8898
*/
8999
private fun getChromeLikeWebViewVersionIfOutdated(
90100
context: Context,
91-
minimumWebViewVersion: Int,
101+
minimumWebViewVersion: WebViewVersion,
92102
): Int? {
93103
// If we cannot get the package information at all, return null
94104
val webviewPackageInfo = WebViewCompat.getCurrentWebViewPackage(context) ?: return null
@@ -113,7 +123,7 @@ fun checkWebViewVersionComponents(
113123
webviewVersion: String,
114124
versionCode: Long,
115125
userAgent: String?,
116-
minimumWebViewVersion: Int = OLDEST_WORKING_WEBVIEW_VERSION,
126+
minimumWebViewVersion: WebViewVersion = OLDEST_WORKING_WEBVIEW_VERSION,
117127
): Int? {
118128
// Sometimes the webview version code appears too old, and the package name does as well,
119129
// but it's a webview that advertises modern capabilities via User-Agent in "Chrome" section
@@ -122,7 +132,7 @@ fun checkWebViewVersionComponents(
122132
val chromeRegex = """Chrome/(\d+)""".toRegex()
123133
val matchResult = chromeRegex.find(userAgent)?.groupValues?.get(1)
124134
matchResult?.toInt()?.let {
125-
if (it >= minimumWebViewVersion) {
135+
if (it >= minimumWebViewVersion.value) {
126136
// If the User-Agent says we are modern, trust it and skip further checks.
127137
return null
128138
} else {
@@ -132,7 +142,7 @@ fun checkWebViewVersionComponents(
132142
}
133143
}
134144
// Checking the version code works for most webview packages
135-
if (versionCode >= OLDEST_WORKING_WEBVIEW_VERSION_CODE) {
145+
if (versionCode >= OLDEST_WORKING_WEBVIEW_VERSION_CODE.value) {
136146
Timber.d(
137147
"WebView is up to date. %s: %s(%s)",
138148
packageName,
@@ -178,7 +188,7 @@ private fun showOutdatedWebViewDialog(
178188
onDismiss: () -> Unit = {},
179189
) {
180190
AlertDialog.Builder(context).show {
181-
setMessage(context.getString(R.string.webview_update_message, installedVersion, OLDEST_WORKING_WEBVIEW_VERSION))
191+
setMessage(context.getString(R.string.webview_update_message, installedVersion, OLDEST_WORKING_WEBVIEW_VERSION.value))
182192
setPositiveButton(R.string.scoped_storage_learn_more) { _, _ ->
183193
context.openUrl(learnMoreUrl)
184194
}

AnkiDroid/src/test/java/com/ichi2/utils/WebViewUtilsTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class WebViewUtilsTest {
2828
checkWebViewVersionComponents(
2929
"com.google.android.webview",
3030
"53.0.2785.124",
31-
OLDEST_WORKING_WEBVIEW_VERSION_CODE - 1,
31+
OLDEST_WORKING_WEBVIEW_VERSION_CODE.value - 1,
3232
"Mozilla/5.0 (Linux; Android 7.0; Android SDK built for arm64 Build/NYC; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.124 Mobile Safari/537.36",
3333
),
3434
equalTo(53),
@@ -50,7 +50,7 @@ class WebViewUtilsTest {
5050
checkWebViewVersionComponents(
5151
"com.google.android.webview",
5252
"74.0.0.0",
53-
OLDEST_WORKING_WEBVIEW_VERSION_CODE - 1,
53+
OLDEST_WORKING_WEBVIEW_VERSION_CODE.value - 1,
5454
"Mozilla/5.0 (Linux; Android 9; SM-A730F Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/130.0.6723.102 Mobile Safari/537.36",
5555
),
5656
equalTo(null),

0 commit comments

Comments
 (0)