Skip to content

Commit

Permalink
Productionise webview ≥ 77 check
Browse files Browse the repository at this point in the history
fixes 15855
  • Loading branch information
RobozinhoD committed Jun 5, 2024
1 parent f2cca93 commit c25fe79
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
26 changes: 2 additions & 24 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,8 @@ open class DeckPicker :
Onboarding.DeckPicker(this, recyclerViewLayoutManager).onCreate()

launchShowingHidingEssentialFileMigrationProgressDialog()
if (BuildConfig.DEBUG) {
checkWebviewVersion()
}

InitialActivity.checkWebviewVersion(packageManager, this)

supportFragmentManager.setFragmentResultListener(DeckPickerContextMenu.REQUEST_KEY_CONTEXT_MENU, this) { requestKey, arguments ->
when (requestKey) {
Expand Down Expand Up @@ -615,27 +614,6 @@ open class DeckPicker :
}
}

/**
* Check if the current WebView version is older than the last supported version and if it is,
* inform the developer with a snackbar.
*/
private fun checkWebviewVersion() {
// Doesn't need to be translated as it's debug only
val webviewPackageInfo = getAndroidSystemWebViewPackageInfo(packageManager)
if (webviewPackageInfo == null) {
val snackbarMessage = "No Android System WebView found"
postSnackbar(snackbarMessage, Snackbar.LENGTH_INDEFINITE)
return
}

val versionCode = webviewPackageInfo.versionName.split(".")[0].toInt()
if (versionCode < OLDEST_WORKING_WEBVIEW_VERSION) {
val snackbarMessage =
"The WebView version $versionCode is outdated (<$OLDEST_WORKING_WEBVIEW_VERSION)."
postSnackbar(snackbarMessage, Snackbar.LENGTH_INDEFINITE)
}
}

/**
* The first call in showing dialogs for startup
*
Expand Down
43 changes: 43 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/InitialActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ package com.ichi2.anki

import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.database.sqlite.SQLiteFullException
import android.os.Build
import android.os.Environment
import android.os.Parcelable
import androidx.annotation.CheckResult
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.core.content.edit
import com.ichi2.anki.servicelayer.PreferenceUpgradeService
import com.ichi2.anki.servicelayer.PreferenceUpgradeService.setPreferencesUpToDate
Expand All @@ -34,6 +37,8 @@ import com.ichi2.anki.ui.windows.permissions.PermissionsUntil29Fragment
import com.ichi2.anki.ui.windows.permissions.TiramisuPermissionsFragment
import com.ichi2.utils.Permissions
import com.ichi2.utils.VersionUtils.pkgVersionName
import com.ichi2.utils.getAndroidSystemWebViewPackageInfo
import com.ichi2.utils.show
import kotlinx.parcelize.Parcelize
import net.ankiweb.rsdroid.BackendException
import timber.log.Timber
Expand Down Expand Up @@ -133,6 +138,44 @@ object InitialActivity {
SD_CARD_NOT_MOUNTED, DIRECTORY_NOT_ACCESSIBLE, FUTURE_ANKIDROID_VERSION,
DB_ERROR, DATABASE_LOCKED, WEBVIEW_FAILED, DISK_FULL
}

/**
* Shows a dialog if the current WebView version is older than the last supported version.
*/
fun checkWebviewVersion(packageManager: PackageManager, activity: AnkiActivity) {
val webviewPackageInfo = getAndroidSystemWebViewPackageInfo(packageManager) ?: return
val versionCode = webviewPackageInfo.versionName.split(".")[0].toInt()
if (versionCode >= OLDEST_WORKING_WEBVIEW_VERSION) {
Timber.d("WebView is up to date. %s: %s", webviewPackageInfo.packageName, webviewPackageInfo.versionName)
return
}

val legacyWebViewPackageInfo = getLegacyWebViewPackageInfo(packageManager)
if (legacyWebViewPackageInfo != null) {
Timber.w("WebView is outdated. %s: %s", legacyWebViewPackageInfo.packageName, legacyWebViewPackageInfo.versionName)
showOutdatedWebViewDialog(activity, versionCode, activity.getString(R.string.link_legacy_webview_update))
} else {
Timber.w("WebView is outdated. %s: %s", webviewPackageInfo.packageName, webviewPackageInfo.versionName)
showOutdatedWebViewDialog(activity, versionCode, activity.getString(R.string.link_webview_update))
}
}

private fun showOutdatedWebViewDialog(activity: AnkiActivity, installedVersion: Int, learnMoreUrl: String) {
AlertDialog.Builder(activity).show {
setMessage(activity.getString(R.string.webview_update_message, installedVersion, OLDEST_WORKING_WEBVIEW_VERSION))
setPositiveButton(R.string.scoped_storage_learn_more) { _, _ ->
activity.openUrl(learnMoreUrl)
}
}
}

private fun getLegacyWebViewPackageInfo(packageManager: PackageManager): PackageInfo? {
return try {
packageManager.getPackageInfo("com.android.webview", 0)
} catch (e: PackageManager.NameNotFoundException) {
null
}
}
}

sealed class AnkiDroidFolder(val permissionSet: PermissionSet) {
Expand Down
2 changes: 2 additions & 0 deletions AnkiDroid/src/main/res/values/03-dialogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,6 @@ also changes the interval of the card"
<string name="open">Open</string>
<string name="tap_to_cloze">Single tap text to cloze</string>

<!-- Outdated Webview dialog -->
<string name="webview_update_message">The system Webview is outdated. Some features won’t work correctly. Please update it.\n\nInstalled version: %1$d\nMinimum required version: %2$d</string>
</resources>
2 changes: 2 additions & 0 deletions AnkiDroid/src/main/res/values/constants.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
<string name="link_ankidroid_privacy_policy">https://github.com/ankidroid/Anki-Android/wiki/Privacy-Policy</string>
<string name="link_ankiweb_privacy_policy">https://ankiweb.net/account/privacy</string>
<string name="link_ankiweb_terms_and_conditions">https://ankiweb.net/account/terms</string>
<string name="link_webview_update">https://github.com/ankidroid/Anki-Android/wiki/WebView-Detection-and-Package-Updates#outdated-webview</string>
<string name="link_legacy_webview_update">https://github.com/ankidroid/Anki-Android/wiki/WebView-Detection-and-Package-Updates#outdated-android-native-webview</string>
<!-- Link is in the name of Glutanimate - server admin -->
<string name="link_discord">https://discord.gg/qjzcRTx</string>
<string name="link_facebook">https://www.facebook.com/AnkiDroid/</string>
Expand Down

0 comments on commit c25fe79

Please sign in to comment.