Skip to content

Commit 9434498

Browse files
committed
refactor: use WebViewVersion and WebViewVersionCode value classes and use finish() in PageFragement.kt
1 parent 6ebec4b commit 9434498

6 files changed

Lines changed: 35 additions & 22 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,8 @@ open class DeckPicker :
10261026
toolbarSearchView?.maxWidth = Integer.MAX_VALUE
10271027

10281028
menu.findItem(R.id.action_export_collection)?.title = TR.actionsExport()
1029+
menu.findItem(R.id.action_check_database)?.title = TR.sentenceCase.checkDatabase
1030+
menu.findItem(R.id.action_check_media)?.title = TR.sentenceCase.checkMediaAction
10291031
setupMediaSyncMenuItem(menu)
10301032
// redraw menu synchronously to avoid flicker
10311033
updateMenuFromState(menu)

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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ 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
3029
import com.ichi2.anki.R
3130
import com.ichi2.anki.workarounds.OnWebViewRecreatedListener
3231
import com.ichi2.anki.workarounds.SafeWebViewLayout
3332
import com.ichi2.themes.Themes
33+
import com.ichi2.utils.WebViewVersion
3434
import com.ichi2.utils.showDialogIfWebViewOutdated
3535
import timber.log.Timber
3636

@@ -66,7 +66,7 @@ abstract class PageFragment(
6666

6767
protected open fun onWebViewCreated() { }
6868

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

7171
/**
7272
* When the webview calls `BridgeCommand("foo")`, the PageFragment execute `bridgeCommands["foo"]`.
@@ -105,21 +105,21 @@ abstract class PageFragment(
105105
view: View,
106106
savedInstanceState: Bundle?,
107107
) {
108-
val ankiActivity = requireActivity() as AnkiActivity
109108
server = AnkiServer(this).also { it.start() }
110109
webViewLayout = view.findViewById(R.id.webview_layout)
111110

112-
val minVersion = minimumWebViewVersion
113-
if (minVersion != null &&
114-
with(ankiActivity) {
111+
val minVersion = minimumWebViewVersion ?: return
112+
val isOutdated =
113+
with(requireContext()) {
115114
showDialogIfWebViewOutdated(minVersion) {
116-
ankiActivity.onBackPressedDispatcher.onBackPressed()
115+
requireActivity().finish()
117116
}
118117
}
119-
) {
118+
if (isOutdated) {
120119
Timber.w("${this::class.simpleName} requires modern WebView (Chrome 90+), aborting load")
121120
return
122121
}
122+
123123
view.findViewById<MaterialToolbar>(R.id.toolbar)?.setNavigationOnClickListener {
124124
requireActivity().onBackPressedDispatcher.onBackPressed()
125125
}

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,36 @@ import androidx.annotation.VisibleForTesting
2727
import androidx.appcompat.app.AlertDialog
2828
import androidx.core.content.pm.PackageInfoCompat
2929
import androidx.webkit.WebViewCompat
30-
import com.ichi2.anki.AnkiActivity
3130
import com.ichi2.anki.R
32-
import com.ichi2.anki.utils.openUrl
3331
import com.ichi2.anki.common.crashreporting.CrashReportService
32+
import com.ichi2.anki.utils.openUrl
3433
import kotlinx.coroutines.Dispatchers
3534
import kotlinx.coroutines.withContext
3635
import timber.log.Timber
3736

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

4150
/** The minimum supported Webview version(Human readable). */
42-
internal const val OLDEST_WORKING_WEBVIEW_VERSION = 85
51+
internal val OLDEST_WORKING_WEBVIEW_VERSION = WebViewVersion(85)
4352

4453
/**
4554
* Shows a dialog if the current WebView version is older than the last supported version.
4655
*/
4756

4857
context(context: Context)
4958
fun showDialogIfWebViewOutdated(
50-
minimumWebViewVersion: Int = OLDEST_WORKING_WEBVIEW_VERSION,
59+
minimumWebViewVersion: WebViewVersion = OLDEST_WORKING_WEBVIEW_VERSION,
5160
onOutdated: () -> Unit = {},
5261
): Boolean {
5362
val userVisibleCode = getChromeLikeWebViewVersionIfOutdated(context, minimumWebViewVersion) ?: return false
@@ -89,7 +98,7 @@ fun getWebviewUserAgent(context: Context): String? {
8998
*/
9099
private fun getChromeLikeWebViewVersionIfOutdated(
91100
context: Context,
92-
minimumWebViewVersion: Int,
101+
minimumWebViewVersion: WebViewVersion,
93102
): Int? {
94103
// If we cannot get the package information at all, return null
95104
val webviewPackageInfo = WebViewCompat.getCurrentWebViewPackage(context) ?: return null
@@ -114,7 +123,7 @@ fun checkWebViewVersionComponents(
114123
webviewVersion: String,
115124
versionCode: Long,
116125
userAgent: String?,
117-
minimumWebViewVersion: Int = OLDEST_WORKING_WEBVIEW_VERSION,
126+
minimumWebViewVersion: WebViewVersion = OLDEST_WORKING_WEBVIEW_VERSION,
118127
): Int? {
119128
// Sometimes the webview version code appears too old, and the package name does as well,
120129
// but it's a webview that advertises modern capabilities via User-Agent in "Chrome" section
@@ -123,7 +132,7 @@ fun checkWebViewVersionComponents(
123132
val chromeRegex = """Chrome/(\d+)""".toRegex()
124133
val matchResult = chromeRegex.find(userAgent)?.groupValues?.get(1)
125134
matchResult?.toInt()?.let {
126-
if (it >= minimumWebViewVersion) {
135+
if (it >= minimumWebViewVersion.value) {
127136
// If the User-Agent says we are modern, trust it and skip further checks.
128137
return null
129138
} else {
@@ -133,7 +142,7 @@ fun checkWebViewVersionComponents(
133142
}
134143
}
135144
// Checking the version code works for most webview packages
136-
if (versionCode >= OLDEST_WORKING_WEBVIEW_VERSION_CODE) {
145+
if (versionCode >= OLDEST_WORKING_WEBVIEW_VERSION_CODE.value) {
137146
Timber.d(
138147
"WebView is up to date. %s: %s(%s)",
139148
packageName,
@@ -179,7 +188,7 @@ private fun showOutdatedWebViewDialog(
179188
onDismiss: () -> Unit = {},
180189
) {
181190
AlertDialog.Builder(context).show {
182-
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))
183192
setPositiveButton(R.string.scoped_storage_learn_more) { _, _ ->
184193
context.openUrl(learnMoreUrl)
185194
}

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)