diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/BackendImporting.kt b/AnkiDroid/src/main/java/com/ichi2/anki/BackendImporting.kt index 848e3d365b96..66dc8d830928 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/BackendImporting.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/BackendImporting.kt @@ -31,7 +31,7 @@ import com.ichi2.libanki.undoableOp import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -suspend fun importJsonFileRaw(input: ByteArray): ByteArray { +suspend fun importAnkiPackageUndoable(input: ByteArray): ByteArray { return withContext(Dispatchers.Main) { val output = withCol { this.importAnkiPackageRaw(input) } val changes = OpChangesOnly.parseFrom(output) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiPackageImporterFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiPackageImporterFragment.kt index 8ddc50134d68..66040b4d7e04 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiPackageImporterFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiPackageImporterFragment.kt @@ -22,12 +22,14 @@ import androidx.core.os.bundleOf import com.ichi2.anki.CollectionManager import com.ichi2.anki.R import com.ichi2.anki.hideShowButtonCss +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json class AnkiPackageImporterFragment : PageFragment() { override val title: String get() = resources.getString(R.string.menu_import) override val pageName: String - get() = "import-page" + get() = "import-anki-package" override lateinit var webViewClient: PageWebViewClient override var webChromeClient: PageChromeClient = PageChromeClient() private lateinit var backCallback: OnBackPressedCallback @@ -60,9 +62,9 @@ class AnkiPackageImporterFragment : PageFragment() { private var isDone = false override fun onPageFinished(view: WebView?, url: String?) { - val params = """{ type: "json_file", path: "$path"}""" + val params = Json.encodeToString(path) // https://github.com/ankitects/anki/blob/main/ts/import-page/index.ts - view!!.evaluateJavascript("anki.setupImportPage($params);$hideShowButtonCss;") { + view!!.evaluateJavascript("anki.setupImportAnkiPackagePage($params);$hideShowButtonCss;") { super.onPageFinished(view, url) } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiServer.kt b/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiServer.kt index e0d8dd087172..409f200a2c05 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiServer.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/pages/AnkiServer.kt @@ -20,11 +20,12 @@ package com.ichi2.anki.pages import android.app.Activity import androidx.fragment.app.FragmentActivity import anki.collection.OpChanges +import com.ichi2.anki.AnkiDroidApp import com.ichi2.anki.CollectionManager import com.ichi2.anki.CollectionManager.withCol import com.ichi2.anki.NoteEditor +import com.ichi2.anki.importAnkiPackageUndoable import com.ichi2.anki.importCsvRaw -import com.ichi2.anki.importJsonFileRaw import com.ichi2.anki.launchCatchingTask import com.ichi2.anki.searchInBrowser import com.ichi2.libanki.* @@ -38,6 +39,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import timber.log.Timber import java.io.ByteArrayInputStream +import java.io.IOException const val PORT = 0 // const val PORT = 40001 @@ -62,8 +64,12 @@ open class AnkiServer( val mime = getMimeFromUri(uri) if (session.method == Method.GET) { - val resourcePath = "web$uri" - val stream = this.javaClass.classLoader!!.getResourceAsStream(resourcePath) + val resourcePath = "backend/web$uri" + val stream = try { + AnkiDroidApp.instance.assets.open(resourcePath) + } catch (e: IOException) { + null + } Timber.d("GET: Requested %s (%s), found? %b", uri, resourcePath, stream != null) return newChunkedResponse(Response.Status.OK, mime, stream) } @@ -88,8 +94,9 @@ open class AnkiServer( "getDeckNames" -> withCol { getDeckNamesRaw(bytes) } "getCsvMetadata" -> withCol { getCsvMetadataRaw(bytes) } "importCsv" -> activity.importCsvRaw(bytes) - "importJsonFile" -> importJsonFileRaw(bytes) + "importAnkiPackage" -> importAnkiPackageUndoable(bytes) "importDone" -> bytes + "getImportAnkiPackagePresets" -> withCol { getImportAnkiPackagePresetsRaw(bytes) } "searchInBrowser" -> activity.searchInBrowser(bytes) "completeTag" -> withCol { completeTagRaw(bytes) } "getFieldNames" -> withCol { getFieldNamesRaw(bytes) } diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/BackendImportExport.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/BackendImportExport.kt index 07ec516536aa..948ea2f2e19e 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/BackendImportExport.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/BackendImportExport.kt @@ -17,6 +17,7 @@ package com.ichi2.libanki import anki.import_export.ExportLimit +import anki.import_export.exportAnkiPackageOptions import anki.search.SearchNode import net.ankiweb.rsdroid.Backend @@ -95,6 +96,10 @@ fun Collection.importAnkiPackageRaw(input: ByteArray): ByteArray { return backend.importAnkiPackageRaw(input) } +fun Collection.getImportAnkiPackagePresetsRaw(input: ByteArray): ByteArray { + return backend.getImportAnkiPackagePresetsRaw(input) +} + /** * Export the specified deck to an .apkg file. * * If legacy is false, an apkg will be created that can only @@ -107,7 +112,13 @@ fun Collection.exportAnkiPackage( limit: ExportLimit, legacy: Boolean = true ) { - backend.exportAnkiPackage(outPath, withScheduling, withMedia, legacy, limit) + val options = exportAnkiPackageOptions { + this.withScheduling = withScheduling + this.withMedia = withMedia + this.legacy = legacy + this.withDeckConfigs = withScheduling + } + backend.exportAnkiPackage(outPath, options, limit) } fun Collection.getCsvMetadataRaw(input: ByteArray): ByteArray { diff --git a/build.gradle b/build.gradle index aeafc762f847..239fabadb6f0 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { ext.kotlin_version = '1.9.21' ext.lint_version = '31.1.1' ext.acra_version = '5.11.3' - ext.ankidroid_backend_version = '0.1.32-anki23.10.1' + ext.ankidroid_backend_version = '0.1.34-anki23.12.1' ext.hamcrest_version = '2.2' ext.junit_version = '5.10.1' ext.coroutines_version = '1.7.3'