Skip to content

Commit

Permalink
apply patch for card info, loading files and i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
krmanik committed Jun 4, 2022
1 parent f1ddd85 commit fb8c12e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
44 changes: 32 additions & 12 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiWebview.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,4 +86,7 @@ interface DroidBackend {

@Throws(BackendNotSupportedException::class)
fun renderCardForTemplateManager(templateRenderContext: TemplateRenderContext): RenderCardOut

@Throws(BackendNotSupportedException::class)
fun i18nResources(): ByteString
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,6 +96,8 @@ open class RustDroidBackend(
throw BackendNotSupportedException()
}

override fun i18nResources(): ByteString = backend.backend.i18nResources().json

companion object {
const val UNUSED_VALUE = 0
}
Expand Down

0 comments on commit fb8c12e

Please sign in to comment.