1
1
use jni:: {
2
- errors:: Error ,
2
+ errors:: Result ,
3
3
objects:: { JObject , JString , JValue } ,
4
4
JNIEnv ,
5
5
} ;
6
6
7
- struct Inner < ' env > {
8
- env : JNIEnv < ' env > ,
9
- object : JObject < ' env > ,
7
+ #[ must_use]
8
+ pub struct Intent < ' env > {
9
+ pub ( crate ) env : JNIEnv < ' env > ,
10
+ pub ( crate ) object : JObject < ' env > ,
11
+ }
12
+
13
+ impl Intent < ' _ > {
14
+ // TODO: Could also return a borrowed JavaStr so that the caller decides if they just want to read or also allocate
15
+ pub fn action ( & mut self ) -> Result < String > {
16
+ let action = self
17
+ . env
18
+ . call_method ( & self . object , "getAction" , "()Ljava/lang/String;" , & [ ] ) ?
19
+ . l ( ) ?
20
+ . into ( ) ;
21
+
22
+ let action = self . env . get_string ( & action) ?;
23
+ Ok ( action. into ( ) )
24
+ }
25
+
26
+ pub fn data_string ( & mut self ) -> Result < String > {
27
+ let data_string = self
28
+ . env
29
+ . call_method ( & self . object , "getDataString" , "()Ljava/lang/String;" , & [ ] ) ?
30
+ . l ( ) ?
31
+ . into ( ) ;
32
+ let data_string = self . env . get_string ( & data_string) ?;
33
+ Ok ( data_string. into ( ) )
34
+ }
35
+
36
+ /// <https://developer.android.com/reference/android/content/Intent#getStringExtra(java.lang.String)>
37
+ pub fn string_extra ( & mut self , name : & str ) -> Result < String > {
38
+ let name = self . env . new_string ( name) ?;
39
+
40
+ let extra = self
41
+ . env
42
+ . call_method (
43
+ & self . object ,
44
+ "getStringExtra" ,
45
+ "(Ljava/lang/String;)Ljava/lang/String;" ,
46
+ & [ JValue :: Object ( & name. into ( ) ) ] ,
47
+ ) ?
48
+ . l ( ) ?
49
+ . into ( ) ;
50
+ let extra = self . env . get_string ( & extra) ?;
51
+ Ok ( extra. into ( ) )
52
+ }
10
53
}
11
54
12
- /// A messaging object you can use to request an action from another android app component.
55
+ /// A messaging object you can use to request an action from another Android app component.
13
56
#[ must_use]
14
- pub struct Intent < ' env > {
15
- inner : Result < Inner < ' env > , Error > ,
57
+ pub struct IntentBuilder < ' env > {
58
+ inner : Result < Intent < ' env > > ,
16
59
}
17
60
18
- impl < ' env > Intent < ' env > {
61
+ impl < ' env > IntentBuilder < ' env > {
19
62
pub fn from_object ( env : JNIEnv < ' env > , object : JObject < ' env > ) -> Self {
20
63
Self {
21
- inner : Ok ( Inner { env, object } ) ,
64
+ inner : Ok ( Intent { env, object } ) ,
22
65
}
23
66
}
24
67
25
- fn from_fn ( f : impl FnOnce ( ) -> Result < Inner < ' env > , Error > ) -> Self {
68
+ fn from_fn ( f : impl FnOnce ( ) -> Result < Intent < ' env > > ) -> Self {
26
69
let inner = f ( ) ;
27
70
Self { inner }
28
71
}
@@ -39,7 +82,7 @@ impl<'env> Intent<'env> {
39
82
& [ action_view. borrow ( ) ] ,
40
83
) ?;
41
84
42
- Ok ( Inner {
85
+ Ok ( Intent {
43
86
env,
44
87
object : intent,
45
88
} )
@@ -72,7 +115,7 @@ impl<'env> Intent<'env> {
72
115
& [ JValue :: Object ( & action_view) , uri. borrow ( ) ] ,
73
116
) ?;
74
117
75
- Ok ( Inner {
118
+ Ok ( Intent {
76
119
env,
77
120
object : intent,
78
121
} )
@@ -190,7 +233,7 @@ impl<'env> Intent<'env> {
190
233
} )
191
234
}
192
235
193
- pub fn start_activity ( self ) -> Result < ( ) , Error > {
236
+ pub fn start_activity ( self ) -> Result < ( ) > {
194
237
let cx = ndk_context:: android_context ( ) ;
195
238
let activity = unsafe { JObject :: from_raw ( cx. context ( ) as jni:: sys:: jobject ) } ;
196
239
@@ -206,7 +249,7 @@ impl<'env> Intent<'env> {
206
249
} )
207
250
}
208
251
209
- fn and_then ( mut self , f : impl FnOnce ( Inner ) -> Result < Inner , Error > ) -> Self {
252
+ fn and_then ( mut self , f : impl FnOnce ( Intent < ' _ > ) -> Result < Intent < ' _ > > ) -> Self {
210
253
self . inner = self . inner . and_then ( f) ;
211
254
self
212
255
}
0 commit comments