Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
[bug] Fix crash in Android Navigator: ARK-Builders/ARK-Navigator#412
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Dec 5, 2023
1 parent a14440c commit 0264f55
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 9 deletions.
3 changes: 3 additions & 0 deletions arklib/.cargo/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[build]
target = "aarch64-linux-android"
#RUSTFLAGS="-C debug-assertions -Zsanitizer=hwaddress -g"
RUSTFLAGS="-C debuginfo=2"
RUSTDOCFLAGS="-C -Zsanitizer=address"
24 changes: 22 additions & 2 deletions arklib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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();
Expand All @@ -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(
Expand All @@ -341,6 +350,8 @@ pub mod android {
)
.unwrap();

debug!("Preview PDF 8 Path: {file_path}");

let bitmap = env
.call_static_method_unchecked(
bitmap_cls,
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -397,6 +415,8 @@ pub mod android {
)
.unwrap();

debug!("Preview PDF 13 Path: {file_path}");

bitmap.into_inner()
}

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
debuggable true
jniDebuggable true
}
}
publishing {
Expand All @@ -45,6 +47,8 @@ android {
withSourcesJar()
}
}
packagingOptions.doNotStrip "**/*.so"

ndkVersion "25.2.9519653"
namespace "dev.arkbuilders.arklib"
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,6 +15,8 @@ internal class MetadataStorage(
) : FolderStorage<Metadata>(
"metadata", scope, path, MonoidIsNotUsed()
) {

private val TAG = "tuancoltech"
override fun isNeutral(value: Metadata): Boolean = false

override suspend fun valueToBinary(value: Metadata): ByteArray {
Expand All @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,9 +26,11 @@ object PdfPreviewGenerator : PreviewGenerator {
override suspend fun generate(path: Path, meta: Metadata): Result<Preview> {
// 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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<V>(
private val label: String,
Expand Down Expand Up @@ -78,7 +79,7 @@ abstract class FolderStorage<V>(
val newTimestamps: ConcurrentHashMap<ResourceId, FileTime> =
ConcurrentHashMap()

val jobs = Files.list(storageFolder)
val stream = Files.list(storageFolder)
.filter { !it.isDirectory() }
.map { path ->
Log.v(logPrefix, "reading value from $path")
Expand All @@ -101,7 +102,13 @@ abstract class FolderStorage<V>(
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()

Expand Down

0 comments on commit 0264f55

Please sign in to comment.