diff --git a/arklib/.cargo/config b/arklib/.cargo/config index 9bb79f04..658222e4 100644 --- a/arklib/.cargo/config +++ b/arklib/.cargo/config @@ -1,2 +1,5 @@ [build] target = "aarch64-linux-android" +#RUSTFLAGS="-C debug-assertions -Zsanitizer=hwaddress -g" +RUSTFLAGS="-C debuginfo=2" +RUSTDOCFLAGS="-C -Zsanitizer=address" \ No newline at end of file diff --git a/arklib/src/lib.rs b/arklib/src/lib.rs index 641de9ba..d7c292ab 100644 --- a/arklib/src/lib.rs +++ b/arklib/src/lib.rs @@ -295,13 +295,16 @@ pub mod android { .get_string(jni_path) .expect("failed to parse input file name") .into(); - debug!("Preview PDF Path: {file_path}"); - let file = File::open(file_path).expect("failed to open file"); + debug!("Preview PDF 1 Path: {file_path}"); + let file = File::open(file_path.clone()).expect("failed to open file"); + debug!("Preview PDF 2 Path: {file_path}"); let quality: String = env .get_string(jni_quality) .expect("failed to read quality") .into(); + debug!("Preview PDF 3 Path: {file_path}"); + let quality = match quality.as_str() { "HIGH" => PDFQuality::High, "MEDIUM" => PDFQuality::Medium, @@ -311,8 +314,12 @@ pub mod android { } }; + debug!("Preview PDF 4 Path: {file_path}"); + let image = arklib::pdf::render_preview_page(file, quality); + debug!("Preview PDF 5 Path: {file_path}"); + let bitmap_cls = env.find_class("android/graphics/Bitmap").unwrap(); let create_bitmap_fn = env .get_static_method_id( @@ -321,6 +328,7 @@ pub mod android { "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;", ) .unwrap(); + debug!("Preview PDF 6 Path: {file_path}"); let config_name = env.new_string("ARGB_8888").unwrap(); let bitmap_config_cls = env.find_class("android/graphics/Bitmap$Config").unwrap(); @@ -331,6 +339,7 @@ pub mod android { "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;", ) .unwrap(); + debug!("Preview PDF 7 Path: {file_path}"); let bitmap_config = env .call_static_method_unchecked( @@ -341,6 +350,8 @@ pub mod android { ) .unwrap(); + debug!("Preview PDF 8 Path: {file_path}"); + let bitmap = env .call_static_method_unchecked( bitmap_cls, @@ -356,11 +367,15 @@ pub mod android { .l() .unwrap(); + debug!("Preview PDF 9 Path: {file_path}"); + let pixels = env .new_int_array((image.width() * image.height()) as jint) .unwrap(); let rgba8img = image.as_rgba8().unwrap(); + debug!("Preview PDF 10 Path: {file_path}"); + let mut raw_pixel = vec![]; for i in 0..((image.width() * image.height()) as usize) { let rgba8img = rgba8img.as_bytes(); @@ -375,11 +390,14 @@ pub mod android { | (blue)) as i32; raw_pixel.push(current_pixel); } + + debug!("Preview PDF 11 Path: {file_path}"); env.set_int_array_region(pixels, 0, raw_pixel.as_slice()) .unwrap(); let set_pixels_fn = env .get_method_id(bitmap_cls, "setPixels", "([IIIIIII)V") .unwrap(); + debug!("Preview PDF 12 Path: {file_path}"); env.call_method_unchecked( bitmap, @@ -397,6 +415,8 @@ pub mod android { ) .unwrap(); + debug!("Preview PDF 13 Path: {file_path}"); + bitmap.into_inner() } diff --git a/build.gradle b/build.gradle index 603cb597..293d8396 100644 --- a/build.gradle +++ b/build.gradle @@ -5,6 +5,7 @@ buildscript { google() maven { url "https://plugins.gradle.org/m2/" } gradlePluginPortal() + mavenLocal() } dependencies { classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:4.4.1.3373" diff --git a/lib/build.gradle b/lib/build.gradle index 14e2fc1d..a431cca8 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -35,6 +35,8 @@ android { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + debuggable true + jniDebuggable true } } publishing { @@ -45,6 +47,8 @@ android { withSourcesJar() } } + packagingOptions.doNotStrip "**/*.so" + ndkVersion "25.2.9519653" namespace "dev.arkbuilders.arklib" } @@ -58,7 +62,7 @@ cargo { prebuiltToolchains = true // Set to "debug" to enable logging. Or "release" to make a production build. // https://github.com/mozilla/rust-android-gradle/issues/38 - profile = gradle.startParameter.taskNames.any { it.toLowerCase().contains("debug") } ? "debug" : "release" + profile = "debug"/*gradle.startParameter.taskNames.any { it.toLowerCase().contains("debug") } ? "debug" : "release"*/ } dependencies { @@ -93,7 +97,7 @@ tasks.whenTaskAdded { task -> //For local development: //def libVersion = '999' -def libVersion = scmVersion.version +def libVersion = /*scmVersion.version*/"0.3.4-snapshot-crash-28" publishing { publications { diff --git a/lib/src/main/java/dev/arkbuilders/arklib/data/meta/MetadataStorage.kt b/lib/src/main/java/dev/arkbuilders/arklib/data/meta/MetadataStorage.kt index db4e486d..eea5da8d 100644 --- a/lib/src/main/java/dev/arkbuilders/arklib/data/meta/MetadataStorage.kt +++ b/lib/src/main/java/dev/arkbuilders/arklib/data/meta/MetadataStorage.kt @@ -1,5 +1,6 @@ package dev.arkbuilders.arklib.data.meta +import android.util.Log import kotlinx.coroutines.CoroutineScope import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json @@ -14,6 +15,8 @@ internal class MetadataStorage( ) : FolderStorage( "metadata", scope, path, MonoidIsNotUsed() ) { + + private val TAG = "tuancoltech" override fun isNeutral(value: Metadata): Boolean = false override suspend fun valueToBinary(value: Metadata): ByteArray { @@ -32,9 +35,20 @@ internal class MetadataStorage( override suspend fun valueFromBinary(raw: ByteArray): Metadata { val text = String(raw, Charsets.UTF_8) + if (text.isEmpty()) return Metadata.Unknown() + + Log.i(TAG, "valueFromBinary text: " + text) val json = Json.parseToJsonElement(text) - val kind = json.jsonObject[KIND]!!.jsonPrimitive.content + Log.d(TAG, "valueFromBinary json: " + json + "\njsonObj: " + json.jsonObject + + "\njson.jsonObject[KIND]: " + json.jsonObject[KIND]) + +// if (json.jsonObject[KIND] == null) { +// Log.w(TAG, "Parsing unknown kind now...") +// return Metadata.Unknown() +// } + + val kind = json.jsonObject[KIND]?.jsonPrimitive?.content ?: return Metadata.Unknown() val metadata = when (Kind.valueOf(kind)) { Kind.IMAGE -> diff --git a/lib/src/main/java/dev/arkbuilders/arklib/data/preview/generator/PdfPreviewGenerator.kt b/lib/src/main/java/dev/arkbuilders/arklib/data/preview/generator/PdfPreviewGenerator.kt index 982c2290..1ce3be85 100644 --- a/lib/src/main/java/dev/arkbuilders/arklib/data/preview/generator/PdfPreviewGenerator.kt +++ b/lib/src/main/java/dev/arkbuilders/arklib/data/preview/generator/PdfPreviewGenerator.kt @@ -1,5 +1,6 @@ package dev.arkbuilders.arklib.data.preview.generator +import android.util.Log import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import dev.arkbuilders.arklib.PreviewQuality @@ -25,9 +26,11 @@ object PdfPreviewGenerator : PreviewGenerator { override suspend fun generate(path: Path, meta: Metadata): Result { // PDF preview generation must be sequential because Rust pdfium-render isn't thread-safe // See https://github.com/ARK-Builders/ARK-Navigator/pull/271 + Log.d("PdfPreviewGenerator", "START generating preview for path: " + path) val bitmap = mutex.withLock { - pdfPreviewGenerate(path.toString(), PreviewQuality.MEDIUM) + pdfPreviewGenerate(path.toString(), PreviewQuality.LOW) } + Log.d("PdfPreviewGenerator", "END generating preview for path: " + path) return Result.success(Preview(bitmap, onlyThumbnail = false)) } diff --git a/lib/src/main/java/dev/arkbuilders/arklib/data/storage/FolderStorage.kt b/lib/src/main/java/dev/arkbuilders/arklib/data/storage/FolderStorage.kt index 6e5e4068..1b455520 100644 --- a/lib/src/main/java/dev/arkbuilders/arklib/data/storage/FolderStorage.kt +++ b/lib/src/main/java/dev/arkbuilders/arklib/data/storage/FolderStorage.kt @@ -1,5 +1,6 @@ package dev.arkbuilders.arklib.data.storage +import android.os.Build import android.util.Log import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -12,10 +13,10 @@ import java.nio.file.Path import java.nio.file.attribute.FileTime import java.time.Instant import java.util.concurrent.ConcurrentHashMap +import java.util.stream.Collectors import kotlin.io.path.deleteIfExists import kotlin.io.path.isDirectory import kotlin.io.path.relativeTo -import kotlin.streams.toList abstract class FolderStorage( private val label: String, @@ -78,7 +79,7 @@ abstract class FolderStorage( val newTimestamps: ConcurrentHashMap = ConcurrentHashMap() - val jobs = Files.list(storageFolder) + val stream = Files.list(storageFolder) .filter { !it.isDirectory() } .map { path -> Log.v(logPrefix, "reading value from $path") @@ -101,7 +102,13 @@ abstract class FolderStorage( newTimestamps[id] = newTimestamp } } - }.toList() + } + + val jobs = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + stream.toList() + } else { + stream.collect(Collectors.toList()) + } jobs.joinAll()