11use 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 }
0 commit comments