Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/android/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ pub fn test() {
let act2 = act.clone();
let runnable = RunnableImpl::from_fn(move || {
act2.set_content_view(&edit);
})
.unwrap();
Ok(())
}).unwrap();
act.run_on_ui_thread(runnable.as_ref());
let wm: WindowManagerImpl = act.get_window_manager();
assert!(wm.to_string().starts_with("android.view.WindowManagerImpl"));
Expand Down
70 changes: 35 additions & 35 deletions src/java/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,41 @@ impl ObjectExt for Object {
}
}

impl JObjRef for String {
fn java_ref(&self) -> Result<GlobalRef> {
let mut env = vm_attach()?;
if self.is_empty() {
return null_value(&mut env);
}

let s = env.new_string(self.as_str())?;
Ok(env.new_global_ref(&s)?)
}
}

impl JObjNew for String {
type Fields = ();

fn _new(this: &GlobalRef, _: Self::Fields) -> Result<Self> {
let mut env = vm_attach()?;
let s = env.get_string(this.as_obj().into())?;
Ok(s.to_str()?.to_string())
}

fn null() -> Result<Self>
where
Self: Sized,
Self::Fields: Default,
{
Ok(Default::default())
}
}

impl JType for String {
const CLASS: &'static str = "java/lang/String";
//noinspection SpellCheckingInspection
const OBJECT_SIG: &'static str = "Ljava/lang/String;";
}
// impl JObjRef for String {
// fn java_ref(&self) -> Result<GlobalRef> {
// let mut env = vm_attach()?;
// if self.is_empty() {
// return null_value(&mut env);
// }
//
// let s = env.new_string(self.as_str())?;
// Ok(env.new_global_ref(&s)?)
// }
// }
//
// impl JObjNew for String {
// type Fields = ();
//
// fn _new(this: &GlobalRef, _: Self::Fields) -> Result<Self> {
// let mut env = vm_attach()?;
// let s = env.get_string(this.as_obj().into())?;
// Ok(s.to_str()?.to_string())
// }
//
// fn null() -> Result<Self>
// where
// Self: Sized,
// Self::Fields: Default,
// {
// Ok(Default::default())
// }
// }
//
// impl JType for String {
// const CLASS: &'static str = "java/lang/String";
// //noinspection SpellCheckingInspection
// const OBJECT_SIG: &'static str = "Ljava/lang/String;";
// }

/**
Boolean 类将原始类型布尔值包装在对象中。布尔类型的对象包含一个类型为布尔的字段。此外,此类还提供了许多将布尔值转换为字符串和将字符串转换为布尔值的方法,以及处理布尔值时有用的其他常量和方法。
Expand Down
29 changes: 29 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,35 @@ macro_rules! impl_array {
const OBJECT_SIG: &'static str = <String as JType>::OBJECT_SIG;
const DIM: u8 = $dim;
}

// 为 String 类型实现必要的 trait
impl JType for String {
const CLASS: &'static str = "java/lang/String";
const OBJECT_SIG: &'static str = "Ljava/lang/String;";
}

impl JObjRef for String {
fn java_ref(&self) -> Result<GlobalRef> {
let mut env = vm_attach()?;
let jstring = env.new_string(self)?;
Ok(env.new_global_ref(&jstring)?)
}
}

impl JObjNew for String {
type Fields = ();

fn _new(this: &GlobalRef, _fields: Self::Fields) -> Result<Self> {
if this.is_null() {
return Ok(String::new());
}

let mut env = vm_attach()?;
let jstring = this.as_obj();
let java_string = env.get_string(jstring.into())?;
Ok(java_string.to_str()?.to_string())
}
}
};

(u8, $dim:expr) => {
Expand Down