keyring fixes

This commit is contained in:
John Smith
2022-01-07 11:06:46 -05:00
parent 8d4ef01086
commit 0a7ebcb3be
4 changed files with 62 additions and 30 deletions

View File

@@ -5,38 +5,46 @@ pub fn get_files_dir() -> String {
let aglock = ANDROID_GLOBALS.lock();
let ag = aglock.as_ref().unwrap();
let env = ag.vm.attach_current_thread().unwrap();
// context.getFilesDir().getAbsolutePath()
let file = env
.call_method(ag.ctx.as_obj(), "getFilesDir", "()Ljava/io/File;", &[])
.unwrap()
.l()
.unwrap();
let path = env
.call_method(file, "getAbsolutePath", "()Ljava/lang/String;", &[])
.unwrap()
.l()
.unwrap();
let jstrval = env.get_string(JString::from(path)).unwrap();
String::from(jstrval.to_string_lossy())
with_null_local_frame(env, 64, || {
// context.getFilesDir().getAbsolutePath()
let file = env
.call_method(ag.ctx.as_obj(), "getFilesDir", "()Ljava/io/File;", &[])
.unwrap()
.l()
.unwrap();
let path = env
.call_method(file, "getAbsolutePath", "()Ljava/lang/String;", &[])
.unwrap()
.l()
.unwrap();
let jstrval = env.get_string(JString::from(path)).unwrap();
Ok(String::from(jstrval.to_string_lossy()))
})
.unwrap()
}
pub fn get_cache_dir() -> String {
let aglock = ANDROID_GLOBALS.lock();
let ag = aglock.as_ref().unwrap();
let env = ag.vm.attach_current_thread().unwrap();
// context.getCacheDir().getAbsolutePath()
let file = env
.call_method(ag.ctx.as_obj(), "getCacheDir", "()Ljava/io/File;", &[])
.unwrap()
.l()
.unwrap();
let path = env
.call_method(file, "getAbsolutePath", "()Ljava/lang/String;", &[])
.unwrap()
.l()
.unwrap();
let jstrval = env.get_string(JString::from(path)).unwrap();
String::from(jstrval.to_string_lossy())
with_null_local_frame(env, 64, || {
// context.getCacheDir().getAbsolutePath()
let file = env
.call_method(ag.ctx.as_obj(), "getCacheDir", "()Ljava/io/File;", &[])
.unwrap()
.l()
.unwrap();
let path = env
.call_method(file, "getAbsolutePath", "()Ljava/lang/String;", &[])
.unwrap()
.l()
.unwrap();
let jstrval = env.get_string(JString::from(path)).unwrap();
String::from(jstrval.to_string_lossy())
})
.unwrap()
}

View File

@@ -8,6 +8,7 @@ pub use get_directories::*;
use crate::xx::*;
use android_logger::{Config, FilterBuilder};
use backtrace::Backtrace;
use jni::errors::Result as JniResult;
use jni::{objects::GlobalRef, objects::JObject, objects::JString, JNIEnv, JavaVM};
use lazy_static::*;
use log::*;
@@ -66,3 +67,13 @@ pub fn veilid_core_setup_android<'a>(
ctx: env.new_global_ref(ctx).unwrap(),
});
}
pub fn with_null_local_frame<'b, T, F>(env: JNIEnv<'b>, s: i32, f: F) -> JniResult<T>
where
F: FnOnce() -> JniResult<T>,
{
env.push_local_frame(s)?;
let out = f();
env.pop_local_frame(JObject::null())?;
out
}