From fb8c12e45ac84164133e1b2fcb48af6cb6b3adb7 Mon Sep 17 00:00:00 2001 From: krmanik <12841290+krmanik@users.noreply.github.com> Date: Sat, 4 Jun 2022 16:52:29 +0800 Subject: [PATCH] apply patch for card info, loading files and i18n --- .../java/com/ichi2/anki/pages/AnkiWebview.kt | 44 ++++++++++++++----- .../com/ichi2/libanki/backend/DroidBackend.kt | 4 ++ .../libanki/backend/JavaDroidBackend.java | 7 +++ .../ichi2/libanki/backend/RustDroidBackend.kt | 3 ++ 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiWebview.kt b/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiWebview.kt index 95e15f1a521f..a2b636be0b67 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiWebview.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiWebview.kt @@ -4,7 +4,10 @@ import android.os.Bundle import android.webkit.* import com.ichi2.anki.AnkiActivity import com.ichi2.anki.R +import com.ichi2.libanki.Collection import timber.log.Timber +import java.io.ByteArrayInputStream +import java.io.InputStream class AnkiWebview : AnkiActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -14,24 +17,41 @@ class AnkiWebview : AnkiActivity() { webview.settings.javaScriptEnabled = true webview.settings.allowFileAccess = true webview.webChromeClient = WebChromeClient() - webview.webViewClient = AnkiWebChromeClient() - webview.loadUrl("file:///android_asset/pages/card-info.html") + // passing col in here is presumably the wrong way to do this + webview.webViewClient = AnkiWebChromeClient(col) + webview.loadUrl("https://127.0.0.1/card-info.html") } - class AnkiWebChromeClient : WebViewClient() { - override fun onLoadResource(view: WebView?, url: String?) { - if (url.equals("file:///_anki/i18nResources")) { - view?.loadUrl("file:///android_asset/i18nResources") - } - super.onLoadResource(view, url) - } - + class AnkiWebChromeClient(val col: Collection) : WebViewClient() { override fun shouldInterceptRequest( view: WebView?, request: WebResourceRequest? ): WebResourceResponse? { - Timber.d("request %d", request?.method) - return super.shouldInterceptRequest(view, request) + val streamResponse = { data: InputStream, mime: String -> + WebResourceResponse( + mime, "utf-8", 200, "OK", + HashMap(), data + ) + } + val fileResponse = { path: String, mime: String -> + view?.context?.assets?.open("pages$path")?.let { streamResponse(it, mime) } + } + return request?.url?.let { url -> + Timber.i("************** request %s", url) + when (val path = url.path) { + "/card-info.html" -> fileResponse(path, "text/html") + "/card-info.css" -> fileResponse(path, "text/css") + "/card-info.js" -> fileResponse(path, "text/javascript") + "/_anki/i18nResources" -> { + val data = ByteArrayInputStream(col.backend.i18nResources().toByteArray()) + streamResponse(data, "application/binary") + } + else -> { + Timber.i("************** ignore %s", url.path) + null + } + } + } ?: super.shouldInterceptRequest(view, request) } } } diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/backend/DroidBackend.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/backend/DroidBackend.kt index df84e952c02e..3d58b6197253 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/backend/DroidBackend.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/backend/DroidBackend.kt @@ -19,6 +19,7 @@ import BackendProto.Backend.ExtractAVTagsOut import BackendProto.Backend.RenderCardOut import android.content.Context import androidx.annotation.VisibleForTesting +import com.google.protobuf.ByteString import com.ichi2.libanki.Collection import com.ichi2.libanki.DB import com.ichi2.libanki.DeckConfig @@ -85,4 +86,7 @@ interface DroidBackend { @Throws(BackendNotSupportedException::class) fun renderCardForTemplateManager(templateRenderContext: TemplateRenderContext): RenderCardOut + + @Throws(BackendNotSupportedException::class) + fun i18nResources(): ByteString } diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/backend/JavaDroidBackend.java b/AnkiDroid/src/main/java/com/ichi2/libanki/backend/JavaDroidBackend.java index 8e9cd11d9324..cdc9104ff6a3 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/backend/JavaDroidBackend.java +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/backend/JavaDroidBackend.java @@ -18,6 +18,7 @@ import android.content.Context; +import com.google.protobuf.ByteString; import com.ichi2.libanki.Collection; import com.ichi2.libanki.DB; import com.ichi2.libanki.TemplateManager; @@ -108,4 +109,10 @@ public void useNewTimezoneCode(Collection col) { public @NonNull Backend.RenderCardOut renderCardForTemplateManager(@NonNull TemplateManager.TemplateRenderContext templateRenderContext) throws BackendNotSupportedException { throw new BackendNotSupportedException(); } + + @NonNull + @Override + public ByteString i18nResources() throws BackendNotSupportedException { + throw new BackendNotSupportedException(); + } } diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/backend/RustDroidBackend.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/backend/RustDroidBackend.kt index 7bf98cd3a5ed..4f16072f713f 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/backend/RustDroidBackend.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/backend/RustDroidBackend.kt @@ -19,6 +19,7 @@ package com.ichi2.libanki.backend import BackendProto.Backend.ExtractAVTagsOut import BackendProto.Backend.RenderCardOut import android.content.Context +import com.google.protobuf.ByteString import com.ichi2.libanki.Collection import com.ichi2.libanki.DB import com.ichi2.libanki.TemplateManager.TemplateRenderContext @@ -95,6 +96,8 @@ open class RustDroidBackend( throw BackendNotSupportedException() } + override fun i18nResources(): ByteString = backend.backend.i18nResources().json + companion object { const val UNUSED_VALUE = 0 }