From 676303c3032da69b486d49dc02f5e7e6d6cb23f3 Mon Sep 17 00:00:00 2001 From: JasperDeSutter Date: Fri, 8 May 2026 12:47:03 +0200 Subject: [PATCH] ndk/asset: Add from_java method to create AssetManager from Java AssetManager --- ndk/src/asset.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ndk/src/asset.rs b/ndk/src/asset.rs index 4ae0c200..19f28aa0 100644 --- a/ndk/src/asset.rs +++ b/ndk/src/asset.rs @@ -11,6 +11,8 @@ use std::{ ptr::NonNull, }; +use jni_sys::{jobject, JNIEnv}; + /// A native [`AAssetManager *`] /// /// [`AAssetManager *`]: https://developer.android.com/ndk/reference/group/asset#aassetmanager @@ -35,6 +37,22 @@ impl AssetManager { Self { ptr } } + /// Create an [`AssetManager`] from a Java [`android.content.res.AssetManager`] object. + /// + /// The Java object must be kept alive for the lifetime of the returned [`AssetManager`]. + /// + /// # Safety + /// `env` must be a valid [`JNIEnv`] pointer for the current thread, and `asset_manager` must + /// be a valid reference to a Java `android.content.res.AssetManager` that has not been + /// garbage collected. + /// + /// [`android.content.res.AssetManager`]: https://developer.android.com/reference/android/content/res/AssetManager + #[doc(alias = "AAssetManager_fromJava")] + pub unsafe fn from_java(env: *mut JNIEnv, asset_manager: jobject) -> Option { + let ptr = unsafe { ffi::AAssetManager_fromJava(env, asset_manager) }; + Some(Self::from_ptr(NonNull::new(ptr)?)) + } + /// Returns the pointer to the native `AAssetManager`. pub fn ptr(&self) -> NonNull { self.ptr