Skip to content

Commit 41bcbbb

Browse files
committed
getIntent
1 parent 1058b29 commit 41bcbbb

File tree

3 files changed

+95
-30
lines changed

3 files changed

+95
-30
lines changed

example/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use android_activity::AndroidApp;
2-
use android_intent::{with_current_env, Action, Extra, Intent};
2+
use android_intent::{with_current_env, Action, Extra, IntentBuilder};
33

44
#[no_mangle]
55
fn android_main(_android_app: AndroidApp) {
66
with_current_env(|env| {
7-
Intent::new(env, Action::Send)
7+
IntentBuilder::new(env, Action::Send)
88
.with_type("text/plain")
99
.with_extra(Extra::Text, "Hello World!")
1010
.into_chooser()

src/intent.rs

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,76 @@
11
use jni::{
2-
errors::Error,
2+
errors::Result,
33
objects::{JObject, JString, JValue},
44
JNIEnv,
55
};
66

7-
struct Inner<'vm, 'env> {
7+
/// A messaging object you can use to request an action from another android app component.
8+
#[must_use]
9+
pub struct Intent<'vm, 'env> {
810
env: &'vm mut JNIEnv<'env>,
911
object: JObject<'env>,
1012
}
1113

12-
/// A messaging object you can use to request an action from another android app component.
14+
impl<'vm, 'env> Intent<'vm, 'env> {
15+
pub fn from_object(env: &'vm mut JNIEnv<'env>, object: JObject<'env>) -> Self {
16+
Self { env, object }
17+
}
18+
19+
// TODO: Could also return a borrowed JavaStr so that the caller decides if they just want to read or also allocate
20+
pub fn action(&mut self) -> Result<String> {
21+
let action = self
22+
.env
23+
.call_method(&self.object, "getAction", "()Ljava/lang/String;", &[])?
24+
.l()?
25+
.into();
26+
27+
let action = self.env.get_string(&action)?;
28+
Ok(action.into())
29+
}
30+
31+
pub fn data_string(&mut self) -> Result<String> {
32+
let data_string = self
33+
.env
34+
.call_method(&self.object, "getDataString", "()Ljava/lang/String;", &[])?
35+
.l()?
36+
.into();
37+
let data_string = self.env.get_string(&data_string)?;
38+
Ok(data_string.into())
39+
}
40+
41+
/// <https://developer.android.com/reference/android/content/Intent#getStringExtra(java.lang.String)>
42+
pub fn string_extra(&mut self, name: &str) -> Result<String> {
43+
let name = self.env.new_string(name)?;
44+
45+
let extra = self
46+
.env
47+
.call_method(
48+
&self.object,
49+
"getStringExtra",
50+
"(Ljava/lang/String;)Ljava/lang/String;",
51+
&[JValue::Object(&name.into())],
52+
)?
53+
.l()?
54+
.into();
55+
let extra = self.env.get_string(&extra)?;
56+
Ok(extra.into())
57+
}
58+
}
59+
60+
/// A messaging object you can use to request an action from another Android app component.
1361
#[must_use]
14-
pub struct Intent<'vm, 'env> {
15-
inner: Result<Inner<'vm, 'env>, Error>,
62+
pub struct IntentBuilder<'vm, 'env> {
63+
inner: Result<Intent<'vm, 'env>>,
1664
}
1765

18-
impl<'vm, 'env> Intent<'vm, 'env> {
66+
impl<'vm, 'env> IntentBuilder<'vm, 'env> {
1967
pub fn from_object(env: &'vm mut JNIEnv<'env>, object: JObject<'env>) -> Self {
2068
Self {
21-
inner: Ok(Inner { env, object }),
69+
inner: Ok(Intent::from_object(env, object)),
2270
}
2371
}
2472

25-
fn from_fn(f: impl FnOnce() -> Result<Inner<'vm, 'env>, Error>) -> Self {
73+
fn from_fn(f: impl FnOnce() -> Result<Intent<'vm, 'env>>) -> Self {
2674
let inner = f();
2775
Self { inner }
2876
}
@@ -39,10 +87,7 @@ impl<'vm, 'env> Intent<'vm, 'env> {
3987
&[action_view.borrow()],
4088
)?;
4189

42-
Ok(Inner {
43-
env,
44-
object: intent,
45-
})
90+
Ok(Intent::from_object(env, intent))
4691
})
4792
}
4893

@@ -72,7 +117,7 @@ impl<'vm, 'env> Intent<'vm, 'env> {
72117
&[JValue::Object(&action_view), uri.borrow()],
73118
)?;
74119

75-
Ok(Inner {
120+
Ok(Intent {
76121
env,
77122
object: intent,
78123
})
@@ -81,10 +126,10 @@ impl<'vm, 'env> Intent<'vm, 'env> {
81126

82127
/// Set the class name for the intent target.
83128
/// ```no_run
84-
/// use android_intent::{Action, Extra, Intent};
129+
/// use android_intent::{Action, Extra, IntentBuilder};
85130
///
86131
/// # android_intent::with_current_env(|env| {
87-
/// let intent = Intent::new(env, Action::Send)
132+
/// let intent = IntentBuilder::new(env, Action::Send)
88133
/// .set_class_name("com.excample", "IntentTarget");
89134
/// # })
90135
/// ```
@@ -110,10 +155,10 @@ impl<'vm, 'env> Intent<'vm, 'env> {
110155

111156
/// Add extended data to the intent.
112157
/// ```no_run
113-
/// use android_intent::{Action, Extra, Intent};
158+
/// use android_intent::{Action, Extra, IntentBuilder};
114159
///
115160
/// # android_intent::with_current_env(|env| {
116-
/// let intent = Intent::new(env, Action::Send)
161+
/// let intent = IntentBuilder::new(env, Action::Send)
117162
/// .with_extra(Extra::Text, "Hello World!");
118163
/// # })
119164
/// ```
@@ -135,10 +180,10 @@ impl<'vm, 'env> Intent<'vm, 'env> {
135180

136181
/// Builds a new [`super::Action::Chooser`] Intent that wraps the given target intent.
137182
/// ```no_run
138-
/// use android_intent::{Action, Intent};
183+
/// use android_intent::{Action, IntentBuilder};
139184
///
140185
/// # android_intent::with_current_env(|env| {
141-
/// let intent = Intent::new(env, Action::Send)
186+
/// let intent = IntentBuilder::new(env, Action::Send)
142187
/// .into_chooser();
143188
/// # })
144189
/// ```
@@ -169,10 +214,10 @@ impl<'vm, 'env> Intent<'vm, 'env> {
169214

170215
/// Set an explicit MIME data type.
171216
/// ```no_run
172-
/// use android_intent::{Action, Intent};
217+
/// use android_intent::{Action, IntentBuilder};
173218
///
174219
/// # android_intent::with_current_env(|env| {
175-
/// let intent = Intent::new(env, Action::Send)
220+
/// let intent = IntentBuilder::new(env, Action::Send)
176221
/// .with_type("text/plain");
177222
/// # })
178223
/// ```
@@ -191,7 +236,7 @@ impl<'vm, 'env> Intent<'vm, 'env> {
191236
})
192237
}
193238

194-
pub fn start_activity(self) -> Result<(), Error> {
239+
pub fn start_activity(self) -> Result<()> {
195240
let cx = ndk_context::android_context();
196241
let activity = unsafe { JObject::from_raw(cx.context() as jni::sys::jobject) };
197242

@@ -207,10 +252,7 @@ impl<'vm, 'env> Intent<'vm, 'env> {
207252
})
208253
}
209254

210-
fn and_then(
211-
mut self,
212-
f: impl FnOnce(Inner<'vm, 'env>) -> Result<Inner<'vm, 'env>, Error>,
213-
) -> Self {
255+
fn and_then(mut self, f: impl FnOnce(Intent<'vm, 'env>) -> Result<Intent<'vm, 'env>>) -> Self {
214256
self.inner = self.inner.and_then(f);
215257
self
216258
}

src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,37 @@ mod extra;
55
pub use extra::Extra;
66

77
mod intent;
8-
pub use intent::Intent;
9-
use jni::{JNIEnv, JavaVM};
8+
pub use intent::{Intent, IntentBuilder};
9+
use jni::{errors::Result, objects::JObject, JNIEnv, JavaVM};
1010

1111
/// Run 'f' with the current [`JNIEnv`] from [`ndk_context`].
1212
pub fn with_current_env(f: impl FnOnce(&mut JNIEnv<'_>)) {
13+
// XXX: Pass AndroidActivity?
1314
let cx = ndk_context::android_context();
1415
let vm = unsafe { JavaVM::from_raw(cx.vm().cast()) }.unwrap();
1516
let mut env = vm.attach_current_thread().unwrap();
1617

18+
// TODO: Pass current activity?
1719
f(&mut env);
1820
}
21+
22+
/// Provides the intent from [`Activity#getIntent()`].
23+
///
24+
/// [`Activity#getIntent()`]: https://developer.android.com/reference/android/app/Activity#getIntent()
25+
pub fn with_current_intent<T>(f: impl FnOnce(Intent<'_, '_>) -> T) -> Result<T> {
26+
// XXX: Pass AndroidActivity?
27+
// XXX: Support onNewIntent() callback with setIntent()?
28+
// https://github.com/rust-mobile/ndk/issues/275
29+
let cx = ndk_context::android_context();
30+
let vm = unsafe { JavaVM::from_raw(cx.vm().cast()) }?;
31+
let mut env = vm.attach_current_thread().unwrap();
32+
let activity = unsafe { JObject::from_raw(cx.context() as jni::sys::jobject) };
33+
34+
let object = env
35+
.call_method(activity, "getIntent", "()Landroid/content/Intent;", &[])?
36+
.l()?;
37+
38+
let intent = Intent::from_object(&mut env, object);
39+
40+
Ok(f(intent))
41+
}

0 commit comments

Comments
 (0)