diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Info.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Info.kt index 5e707e6e4ac1..6b663be37109 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/Info.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/Info.kt @@ -27,6 +27,7 @@ import android.webkit.WebChromeClient import android.webkit.WebResourceRequest import android.webkit.WebView import android.webkit.WebViewClient +import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.ThemeUtils import com.google.android.material.button.MaterialButton import com.ichi2.anim.ActivityTransitionAnimation @@ -46,7 +47,7 @@ private const val CHANGE_LOG_URL = "https://docs.ankidroid.org/changelog.html" * Shows an about box, which is a small HTML page. */ class Info : AnkiActivity() { - private var mWebView: WebView? = null + private lateinit var mWebView: WebView @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { @@ -67,7 +68,7 @@ class Info : AnkiActivity() { findViewById(R.id.info_donate).setOnClickListener { openUrl(Uri.parse(getString(R.string.link_opencollective_donate))) } title = "$appName v$pkgVersionName" mWebView = findViewById(R.id.info) - mWebView!!.webChromeClient = object : WebChromeClient() { + mWebView.webChromeClient = object : WebChromeClient() { override fun onProgressChanged(view: WebView, progress: Int) { // Hide the progress indicator when the page has finished loaded if (progress == 100) { @@ -88,7 +89,11 @@ class Info : AnkiActivity() { visibility = View.GONE } } - + val onBackPressedCallback = object : OnBackPressedCallback(false) { + override fun handleOnBackPressed() { + if (mWebView.canGoBack()) mWebView.goBack() + } + } // Apply Theme colors val typedArray = theme.obtainStyledAttributes(intArrayOf(android.R.attr.colorBackground, android.R.attr.textColor)) val backgroundColor = typedArray.getColor(0, -1) @@ -97,9 +102,9 @@ class Info : AnkiActivity() { val anchorTextThemeColor = ThemeUtils.getThemeAttrColor(this, android.R.attr.colorAccent) val anchorTextColor = anchorTextThemeColor.toRGBHex() - mWebView!!.setBackgroundColor(backgroundColor) - mWebView!!.settings.allowFileAccess = true - mWebView!!.settings.allowContentAccess = true + mWebView.setBackgroundColor(backgroundColor) + mWebView.settings.allowFileAccess = true + mWebView.settings.allowContentAccess = true setRenderWorkaround(this) when (type) { TYPE_NEW_VERSION -> { @@ -108,14 +113,14 @@ class Info : AnkiActivity() { setOnClickListener { close() } } val background = backgroundColor.toRGBHex() - mWebView!!.loadUrl("/android_asset/changelog.html") - mWebView!!.settings.javaScriptEnabled = true - mWebView!!.webViewClient = object : WebViewClient() { + mWebView.loadUrl("/android_asset/changelog.html") + mWebView.settings.javaScriptEnabled = true + mWebView.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView, url: String) { /* The order of below javascript code must not change (this order works both in debug and release mode) * or else it will break in any one mode. */ - mWebView!!.loadUrl( + mWebView.loadUrl( "javascript:document.body.style.setProperty(\"color\", \"" + textColor + "\");" + "x=document.getElementsByTagName(\"a\"); for(i=0;i finish() } + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) } private fun close() { @@ -170,15 +185,6 @@ class Info : AnkiActivity() { finishWithAnimation(ActivityTransitionAnimation.Direction.START) } - @Suppress("deprecation") // onBackPressed - override fun onBackPressed() { - if (mWebView!!.canGoBack()) { - mWebView!!.goBack() - } else { - super.onBackPressed() - } - } - companion object { const val TYPE_EXTRA = "infoType" const val TYPE_NEW_VERSION = 2 diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt index ed14f24ed746..792291bfe0c4 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt @@ -454,10 +454,6 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler { } override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) { - android.R.id.home -> { - onBackPressed() - true - } R.id.action_add_new_model -> { addFieldDialog() true @@ -469,12 +465,6 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler { finishWithAnimation(ActivityTransitionAnimation.Direction.END) } - @Suppress("DEPRECATION", "Deprecated in API34+dependencies for predictive back feature") - override fun onBackPressed() { - super.onBackPressed() - closeActivity() - } - fun handleAction(contextMenuAction: ModelEditorContextMenuAction) { when (contextMenuAction) { ModelEditorContextMenuAction.Sort -> sortByField() diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Previewer.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Previewer.kt index d90c6124dbed..e41db66afa36 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/Previewer.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/Previewer.kt @@ -75,6 +75,7 @@ class Previewer : AbstractFlashcardViewer() { disableDrawerSwipe() startLoadingCollection() initPreviewProgress() + setOkResult() } private fun initPreviewProgress() { @@ -161,13 +162,8 @@ class Previewer : AbstractFlashcardViewer() { return super.onOptionsItemSelected(item) } - override fun onBackPressed() { - setResult(RESULT_OK, resultIntent) - super.onBackPressed() - } - override fun onNavigationPressed() { - setResult(RESULT_OK, resultIntent) + setOkResult() super.onNavigationPressed() } @@ -214,6 +210,7 @@ class Previewer : AbstractFlashcardViewer() { override fun performReload() { mReloadRequired = true + setOkResult() val newCardList = getColUnsafe.filterToValidCards(mCardList) if (newCardList.isEmpty()) { finish() @@ -228,6 +225,7 @@ class Previewer : AbstractFlashcardViewer() { override fun onEditedNoteChanged() { super.onEditedNoteChanged() mNoteChanged = true + setOkResult() } @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) @@ -267,13 +265,15 @@ class Previewer : AbstractFlashcardViewer() { mProgressText.text = progress } - private val resultIntent: Intent - get() { - val intent = Intent() - intent.putExtra("reloadRequired", mReloadRequired) - intent.putExtra("noteChanged", mNoteChanged) - return intent - } + private fun setOkResult() { + setResult( + RESULT_OK, + Intent().apply { + putExtra("reloadRequired", mReloadRequired) + putExtra("noteChanged", mNoteChanged) + } + ) + } companion object { @CheckResult