android_native_keyring_store/
lib.rs

1#[cfg(feature = "android-log")]
2pub mod android_log;
3pub mod cipher;
4pub mod credential;
5pub mod keystore;
6pub mod methods;
7pub mod shared_preferences;
8#[cfg(feature = "compile_tests")]
9pub mod tests;
10
11use jni::{JNIEnv, objects::JObject};
12use shared_preferences::Context;
13
14pub use credential::AndroidStore;
15
16// package io.crates.keyring
17// import android.content.Context
18// class Keyring {
19//     companion object {
20//         init {
21//             System.loadLibrary("android_keyring")
22//         }
23//         external fun setAndroidKeyringCredentialBuilder(context: Context);
24//     }
25// }
26#[unsafe(no_mangle)]
27pub extern "system" fn Java_io_crates_keyring_Keyring_00024Companion_setAndroidKeyringCredentialBuilder(
28    env: JNIEnv,
29    _class: JObject,
30    context: JObject,
31) {
32    let context = match Context::new(&env, context) {
33        Ok(context) => context,
34        Err(e) => {
35            tracing::error!(%e, "error converting context jobject into Context");
36            tracing::debug!(?e);
37            return;
38        }
39    };
40
41    let builder = match AndroidStore::new(&env, context) {
42        Ok(builder) => builder,
43        Err(e) => {
44            tracing::error!(%e, "error initialized AndroidBuilder credential builder");
45            tracing::debug!(?e);
46            return;
47        }
48    };
49
50    keyring_core::set_default_store(builder);
51}
52
53/// Initializes the android-keyring from pure Rust (no Java call needed).
54/// Requires the `ndk-context` feature.
55#[cfg(feature = "ndk-context")]
56pub fn set_android_keyring_credential_builder() -> Result<(), credential::AndroidKeyringError> {
57    keyring_core::set_default_store(AndroidStore::from_ndk_context()?);
58
59    Ok(())
60}