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 < ' 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 > {
8
10
env : & ' vm mut JNIEnv < ' env > ,
9
11
object : JObject < ' env > ,
10
12
}
11
13
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.
13
61
#[ 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 > > ,
16
64
}
17
65
18
- impl < ' vm , ' env > Intent < ' vm , ' env > {
66
+ impl < ' vm , ' env > IntentBuilder < ' vm , ' env > {
19
67
pub fn from_object ( env : & ' vm mut JNIEnv < ' env > , object : JObject < ' env > ) -> Self {
20
68
Self {
21
- inner : Ok ( Inner { env, object } ) ,
69
+ inner : Ok ( Intent :: from_object ( env, object) ) ,
22
70
}
23
71
}
24
72
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 {
26
74
let inner = f ( ) ;
27
75
Self { inner }
28
76
}
@@ -39,10 +87,7 @@ impl<'vm, 'env> Intent<'vm, 'env> {
39
87
& [ action_view. borrow ( ) ] ,
40
88
) ?;
41
89
42
- Ok ( Inner {
43
- env,
44
- object : intent,
45
- } )
90
+ Ok ( Intent :: from_object ( env, intent) )
46
91
} )
47
92
}
48
93
@@ -72,7 +117,7 @@ impl<'vm, 'env> Intent<'vm, 'env> {
72
117
& [ JValue :: Object ( & action_view) , uri. borrow ( ) ] ,
73
118
) ?;
74
119
75
- Ok ( Inner {
120
+ Ok ( Intent {
76
121
env,
77
122
object : intent,
78
123
} )
@@ -81,10 +126,10 @@ impl<'vm, 'env> Intent<'vm, 'env> {
81
126
82
127
/// Set the class name for the intent target.
83
128
/// ```no_run
84
- /// use android_intent::{Action, Extra, Intent };
129
+ /// use android_intent::{Action, Extra, IntentBuilder };
85
130
///
86
131
/// # android_intent::with_current_env(|env| {
87
- /// let intent = Intent ::new(env, Action::Send)
132
+ /// let intent = IntentBuilder ::new(env, Action::Send)
88
133
/// .set_class_name("com.excample", "IntentTarget");
89
134
/// # })
90
135
/// ```
@@ -110,10 +155,10 @@ impl<'vm, 'env> Intent<'vm, 'env> {
110
155
111
156
/// Add extended data to the intent.
112
157
/// ```no_run
113
- /// use android_intent::{Action, Extra, Intent };
158
+ /// use android_intent::{Action, Extra, IntentBuilder };
114
159
///
115
160
/// # android_intent::with_current_env(|env| {
116
- /// let intent = Intent ::new(env, Action::Send)
161
+ /// let intent = IntentBuilder ::new(env, Action::Send)
117
162
/// .with_extra(Extra::Text, "Hello World!");
118
163
/// # })
119
164
/// ```
@@ -135,10 +180,10 @@ impl<'vm, 'env> Intent<'vm, 'env> {
135
180
136
181
/// Builds a new [`super::Action::Chooser`] Intent that wraps the given target intent.
137
182
/// ```no_run
138
- /// use android_intent::{Action, Intent };
183
+ /// use android_intent::{Action, IntentBuilder };
139
184
///
140
185
/// # android_intent::with_current_env(|env| {
141
- /// let intent = Intent ::new(env, Action::Send)
186
+ /// let intent = IntentBuilder ::new(env, Action::Send)
142
187
/// .into_chooser();
143
188
/// # })
144
189
/// ```
@@ -169,10 +214,10 @@ impl<'vm, 'env> Intent<'vm, 'env> {
169
214
170
215
/// Set an explicit MIME data type.
171
216
/// ```no_run
172
- /// use android_intent::{Action, Intent };
217
+ /// use android_intent::{Action, IntentBuilder };
173
218
///
174
219
/// # android_intent::with_current_env(|env| {
175
- /// let intent = Intent ::new(env, Action::Send)
220
+ /// let intent = IntentBuilder ::new(env, Action::Send)
176
221
/// .with_type("text/plain");
177
222
/// # })
178
223
/// ```
@@ -191,7 +236,7 @@ impl<'vm, 'env> Intent<'vm, 'env> {
191
236
} )
192
237
}
193
238
194
- pub fn start_activity ( self ) -> Result < ( ) , Error > {
239
+ pub fn start_activity ( self ) -> Result < ( ) > {
195
240
let cx = ndk_context:: android_context ( ) ;
196
241
let activity = unsafe { JObject :: from_raw ( cx. context ( ) as jni:: sys:: jobject ) } ;
197
242
@@ -207,10 +252,7 @@ impl<'vm, 'env> Intent<'vm, 'env> {
207
252
} )
208
253
}
209
254
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 {
214
256
self . inner = self . inner . and_then ( f) ;
215
257
self
216
258
}
0 commit comments