Skip to content

Commit 32a1b44

Browse files
BrayanDSOlukstbit
authored andcommitted
fix: Web debugging not working
this fixes: 1. web debugging not working on all the web screens immediately after the setting is changed (the user needed to open the reviewer for that) 2. web debugging not working on release builds on the new study screen
1 parent a7bb7a7 commit 32a1b44

File tree

5 files changed

+19
-24
lines changed

5 files changed

+19
-24
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ import com.ichi2.ui.FixedEditText
163163
import com.ichi2.utils.HandlerUtils.newHandler
164164
import com.ichi2.utils.HashUtil.hashSetInit
165165
import com.ichi2.utils.Stopwatch
166-
import com.ichi2.utils.WebViewDebugging.initializeDebugging
167166
import com.ichi2.utils.message
168167
import com.ichi2.utils.negativeButton
169168
import com.ichi2.utils.positiveButton
@@ -1269,7 +1268,6 @@ abstract class AbstractFlashcardViewer :
12691268
protected open fun recreateWebView() {
12701269
if (webView == null) {
12711270
webView = createWebView()
1272-
initializeDebugging(this.sharedPrefs())
12731271
cardFrame!!.addView(webView)
12741272
gestureDetectorImpl.onWebViewCreated(webView!!)
12751273
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import android.os.Bundle
2828
import android.os.Environment
2929
import android.system.Os
3030
import android.webkit.CookieManager
31+
import android.webkit.WebView
3132
import androidx.annotation.VisibleForTesting
32-
import androidx.core.content.edit
3333
import androidx.core.net.toUri
3434
import androidx.fragment.app.FragmentActivity
3535
import androidx.lifecycle.MutableLiveData
@@ -40,7 +40,6 @@ import com.ichi2.anki.browser.SharedPreferencesLastDeckIdRepository
4040
import com.ichi2.anki.common.annotations.LegacyNotifications
4141
import com.ichi2.anki.common.annotations.NeedsTest
4242
import com.ichi2.anki.common.utils.annotation.KotlinCleanup
43-
import com.ichi2.anki.common.utils.isRunningAsUnitTest
4443
import com.ichi2.anki.contextmenu.AnkiCardContextMenu
4544
import com.ichi2.anki.contextmenu.CardBrowserContextMenu
4645
import com.ichi2.anki.exception.StorageAccessException
@@ -163,10 +162,8 @@ open class AnkiDroidApp :
163162
showThemedToast(this.applicationContext, getString(R.string.user_is_a_robot), false)
164163
}
165164

166-
// make default HTML / JS debugging true for debug build and disable for unit/android tests
167-
if (BuildConfig.DEBUG && !isRunningAsUnitTest) {
168-
preferences.edit { putBoolean("html_javascript_debugging", true) }
169-
}
165+
WebView.setWebContentsDebuggingEnabled(Prefs.isWebDebugEnabled)
166+
170167
CardBrowserContextMenu.ensureConsistentStateWithPreferenceStatus(
171168
this,
172169
preferences.getBoolean(

AnkiDroid/src/main/java/com/ichi2/anki/preferences/DevOptionsFragment.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.ichi2.anki.preferences
1717

18+
import android.webkit.WebView
1819
import androidx.appcompat.app.AlertDialog
1920
import androidx.core.app.ActivityCompat
2021
import androidx.core.content.edit
@@ -190,6 +191,17 @@ class DevOptionsFragment : SettingsFragment() {
190191
ActivityCompat.recreate(requireActivity())
191192
true
192193
}
194+
195+
setupWebDebugPreference()
196+
}
197+
198+
private fun setupWebDebugPreference() {
199+
requirePreference<SwitchPreferenceCompat>(R.string.html_javascript_debugging_key).apply {
200+
isVisible = !BuildConfig.DEBUG
201+
setOnPreferenceChangeListener { isEnabled ->
202+
WebView.setWebContentsDebuggingEnabled(isEnabled)
203+
}
204+
}
193205
}
194206

195207
/**

AnkiDroid/src/main/java/com/ichi2/anki/settings/Prefs.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.ichi2.anki.AnkiDroidApp
2323
import com.ichi2.anki.BuildConfig
2424
import com.ichi2.anki.R
2525
import com.ichi2.anki.cardviewer.TapGestureMode
26+
import com.ichi2.anki.common.utils.isRunningAsUnitTest
2627
import com.ichi2.anki.settings.enums.FrameStyle
2728
import com.ichi2.anki.settings.enums.HideSystemBars
2829
import com.ichi2.anki.settings.enums.PrefEnum
@@ -295,6 +296,9 @@ object Prefs {
295296

296297
val devUsingCardBrowserSearchView: Boolean by booleanPref(R.string.dev_card_browser_search_view, false)
297298

299+
val isWebDebugEnabled: Boolean
300+
get() = (getBoolean(R.string.html_javascript_debugging_key, false) || BuildConfig.DEBUG) && !isRunningAsUnitTest
301+
298302
// **************************************** UI Config *************************************** //
299303

300304
private const val UI_CONFIG_PREFERENCES_NAME = "ui-config"

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,13 @@
1616

1717
package com.ichi2.utils
1818

19-
import android.content.SharedPreferences
2019
import android.os.Build
2120
import android.webkit.WebView
2221
import androidx.annotation.RequiresApi
23-
import androidx.annotation.UiThread
2422

2523
object WebViewDebugging {
2624
private var sHasSetDataDirectory = false
2725

28-
@UiThread
29-
fun initializeDebugging(sharedPrefs: SharedPreferences) {
30-
// DEFECT: We might be able to cache this value: check what happens on WebView Renderer crash
31-
// On your desktop use chrome://inspect to connect to emulator WebViews
32-
// Beware: Crash in AnkiDroidApp.onCreate() with:
33-
//
34-
// java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory
35-
// is not supported. https://crbug.com/558377 : Lock owner com.ichi2.anki:acra at
36-
// org.chromium.android_webview.AwDataDirLock.a(PG:26)
37-
//
38-
val enableDebugging = sharedPrefs.getBoolean("html_javascript_debugging", false)
39-
WebView.setWebContentsDebuggingEnabled(enableDebugging)
40-
}
41-
4226
/** Throws IllegalStateException if a WebView has been initialized */
4327
@RequiresApi(api = Build.VERSION_CODES.P)
4428
fun setDataDirectorySuffix(suffix: String) {

0 commit comments

Comments
 (0)