Use new robius-android-env API. Add optional log feature.
This commit is contained in:
parent
eb522c827e
commit
6bfda365c2
|
@ -136,7 +136,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "robius-android-env"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/project-robius/robius-android-env#2444b9d9b430ae365e012e94b005444c48db8ae0"
|
||||
source = "git+https://github.com/project-robius/robius-android-env#45acdf70ebc507a2ac017add07bec9ecd7bafa51"
|
||||
dependencies = [
|
||||
"jni",
|
||||
]
|
||||
|
@ -148,6 +148,7 @@ dependencies = [
|
|||
"cfg-if",
|
||||
"icrate",
|
||||
"jni",
|
||||
"log",
|
||||
"objc2",
|
||||
"robius-android-env",
|
||||
]
|
||||
|
|
|
@ -3,12 +3,10 @@ name = "robius-open"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = ["android-result"]
|
||||
android-result = []
|
||||
|
||||
[dependencies]
|
||||
cfg-if = "1.0.0"
|
||||
log = { version = "0.4.21", optional = true }
|
||||
|
||||
[target.'cfg(target_os = "android")'.dependencies.jni]
|
||||
version = "0.21.1"
|
||||
|
@ -27,3 +25,8 @@ version = "0.5.0"
|
|||
[target.'cfg(target_os = "macos")'.dependencies.icrate]
|
||||
version = "0.1.0"
|
||||
features = ["AppKit", "AppKit_NSWorkspace", "Foundation", "Foundation_NSString", "Foundation_NSURL"]
|
||||
|
||||
[features]
|
||||
default = ["android-result"]
|
||||
android-result = []
|
||||
log = ["dep:log"]
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
fn main() {
|
||||
robius_open::Uri::new("mailto:test@example.com")
|
||||
.action("ACTION_MAIL")
|
||||
.open();
|
||||
.open()
|
||||
.unwrap();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
fn main() {
|
||||
robius_open::Uri::new("tel:+61 0400 000 000")
|
||||
.action("ACTION_DIAL")
|
||||
.open();
|
||||
.open()
|
||||
.unwrap();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
fn main() {
|
||||
robius_open::Uri::new("https://github.com/project-robius")
|
||||
.action("ACTION_VIEW")
|
||||
.open();
|
||||
.open()
|
||||
.unwrap();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use jni::objects::JValueGen;
|
||||
use robius_android_env::{current_activity, vm};
|
||||
|
||||
pub(crate) struct Uri<'a, 'b> {
|
||||
inner: &'a str,
|
||||
|
@ -19,77 +18,99 @@ impl<'a, 'b> Uri<'a, 'b> {
|
|||
}
|
||||
|
||||
pub(crate) fn open(self) -> Result<(), ()> {
|
||||
let mut env = vm().unwrap().get_env().unwrap();
|
||||
let current_activity = current_activity().unwrap();
|
||||
let res = robius_android_env::with_activity(|env, current_activity| {
|
||||
let action = env
|
||||
.get_static_field("android/content/Intent", self.action, "Ljava/lang/String;")
|
||||
.unwrap()
|
||||
.l()
|
||||
.unwrap();
|
||||
|
||||
let action = env
|
||||
.get_static_field("android/content/Intent", self.action, "Ljava/lang/String;")
|
||||
.unwrap()
|
||||
.l()
|
||||
.unwrap();
|
||||
let string = env.new_string(self.inner).unwrap();
|
||||
let uri = env
|
||||
.call_static_method(
|
||||
"android/net/Uri",
|
||||
"parse",
|
||||
"(Ljava/lang/String;)Landroid/net/Uri;",
|
||||
&[JValueGen::Object(&string)],
|
||||
)
|
||||
.unwrap()
|
||||
.l()
|
||||
.unwrap();
|
||||
|
||||
let string = env.new_string(self.inner).unwrap();
|
||||
let uri = env
|
||||
.call_static_method(
|
||||
"android/net/Uri",
|
||||
"parse",
|
||||
"(Ljava/lang/String;)Landroid/net/Uri;",
|
||||
&[JValueGen::Object(&string)],
|
||||
)
|
||||
.unwrap()
|
||||
.l()
|
||||
.unwrap();
|
||||
let intent = env
|
||||
.new_object(
|
||||
"android/content/Intent",
|
||||
"(Ljava/lang/String;Landroid/net/Uri;)V",
|
||||
&[JValueGen::Object(&action), JValueGen::Object(&uri)],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let intent = env
|
||||
.new_object(
|
||||
"android/content/Intent",
|
||||
"(Ljava/lang/String;Landroid/net/Uri;)V",
|
||||
&[JValueGen::Object(&action), JValueGen::Object(&uri)],
|
||||
)
|
||||
.unwrap();
|
||||
#[cfg(feature = "android-result")]
|
||||
let is_err = {
|
||||
let package_manager = env
|
||||
.call_method(
|
||||
current_activity,
|
||||
"getPackageManager",
|
||||
"()Landroid/content/pm/PackageManager;",
|
||||
&[],
|
||||
)
|
||||
.unwrap()
|
||||
.l()
|
||||
.unwrap();
|
||||
|
||||
#[cfg(feature = "android-result")]
|
||||
let is_err = {
|
||||
let package_manager = env
|
||||
.call_method(
|
||||
let component_name = env
|
||||
.call_method(
|
||||
&intent,
|
||||
"resolveActivity",
|
||||
"(Landroid/content/pm/PackageManager;)Landroid/content/ComponentName;",
|
||||
&[JValueGen::Object(&package_manager)],
|
||||
)
|
||||
.unwrap()
|
||||
.l()
|
||||
.unwrap();
|
||||
|
||||
component_name.as_raw().is_null()
|
||||
};
|
||||
#[cfg(not(feature = "android-result"))]
|
||||
let is_err = false;
|
||||
|
||||
if is_err {
|
||||
// NOTE: If the correct permissions aren't added to the app manifest,
|
||||
// resolveActivity will return null regardless.
|
||||
Err(())
|
||||
} else {
|
||||
env.call_method(
|
||||
current_activity,
|
||||
"getPackageManager",
|
||||
"()Landroid/content/pm/PackageManager;",
|
||||
&[],
|
||||
"startActivity",
|
||||
"(Landroid/content/Intent;)V",
|
||||
&[JValueGen::Object(&intent)],
|
||||
)
|
||||
.unwrap()
|
||||
.l()
|
||||
.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
});
|
||||
|
||||
let component_name = env
|
||||
.call_method(
|
||||
&intent,
|
||||
"resolveActivity",
|
||||
"(Landroid/content/pm/PackageManager;)Landroid/content/ComponentName;",
|
||||
&[JValueGen::Object(&package_manager)],
|
||||
)
|
||||
.unwrap()
|
||||
.l()
|
||||
.unwrap();
|
||||
|
||||
component_name.as_raw().is_null()
|
||||
};
|
||||
#[cfg(not(feature = "android_result"))]
|
||||
let is_err = false;
|
||||
|
||||
if is_err {
|
||||
// NOTE: If the correct permissions aren't added to the app manifest,
|
||||
// resolveActivity will return null regardless.
|
||||
Err(())
|
||||
} else {
|
||||
env.call_method(
|
||||
current_activity,
|
||||
"startActivity",
|
||||
"(Landroid/content/Intent;)V",
|
||||
&[JValueGen::Object(&intent)],
|
||||
)
|
||||
.unwrap();
|
||||
Ok(())
|
||||
match res {
|
||||
Some(Ok(())) => Ok(()),
|
||||
Some(Err(_)) => {
|
||||
#[cfg(feature = "log")]
|
||||
log::error!(
|
||||
"resolveActivity method failed. Is your app manifest missing permissions?"
|
||||
);
|
||||
// TODO: add error enum: the resolveActivity method failed,
|
||||
// which implies the app manifest is missing permissions.
|
||||
Err(())
|
||||
}
|
||||
None => {
|
||||
#[cfg(feature = "log")]
|
||||
log::error!(
|
||||
"couldn't get current activity or JVM/JNI. Did you call \
|
||||
`robius_android_env::set_vm()` and \
|
||||
`robius_android_env::set_activity_getter()`?"
|
||||
);
|
||||
// TODO: add error enum: couldn't get current activity or JVM/JNI
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue