-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathutils.rs
More file actions
201 lines (179 loc) · 4.72 KB
/
utils.rs
File metadata and controls
201 lines (179 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use jni::sys::jobject;
use ndk_context::AndroidContext;
use std::sync::Arc;
pub use jni::Executor;
pub use jni::{
errors::Result as JResult,
objects::{JIntArray, JObject, JObjectArray, JString},
JNIEnv, JavaVM,
};
pub fn get_context() -> AndroidContext {
ndk_context::android_context()
}
pub fn with_attached<F, R>(context: AndroidContext, closure: F) -> JResult<R>
where
for<'j> F: FnOnce(&mut JNIEnv<'j>, JObject<'j>) -> JResult<R>,
{
let vm = Arc::new(unsafe { JavaVM::from_raw(context.vm().cast())? });
let context = context.context();
let context = unsafe { JObject::from_raw(context as jobject) };
Executor::new(vm).with_attached(|env| closure(env, context))
}
pub fn call_method_no_args_ret_int_array<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
method: &str,
) -> JResult<Vec<i32>> {
let array: JIntArray = env.call_method(subject, method, "()[I", &[])?.l()?.into();
let length = env.get_array_length(&array)?;
let mut values = Vec::with_capacity(length as usize);
env.get_int_array_region(array, 0, values.as_mut())?;
Ok(values)
}
pub fn call_method_no_args_ret_int<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
method: &str,
) -> JResult<i32> {
env.call_method(subject, method, "()I", &[])?.i()
}
pub fn call_method_no_args_ret_bool<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
method: &str,
) -> JResult<bool> {
env.call_method(subject, method, "()Z", &[])?.z()
}
pub fn call_method_no_args_ret_string<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
method: &str,
) -> JResult<JString<'j>> {
Ok(env
.call_method(subject, method, "()Ljava/lang/String;", &[])?
.l()?
.into())
}
pub fn call_method_no_args_ret_char_sequence<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
method: &str,
) -> JResult<JString<'j>> {
let cseq = env
.call_method(subject, method, "()Ljava/lang/CharSequence;", &[])?
.l()?;
Ok(env
.call_method(&cseq, "toString", "()Ljava/lang/String;", &[])?
.l()?
.into())
}
pub fn call_method_string_arg_ret_bool<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
name: &str,
arg: impl AsRef<str>,
) -> JResult<bool> {
env.call_method(
subject,
name,
"(Ljava/lang/String;)Z",
&[(&env.new_string(arg)?).into()],
)?
.z()
}
pub fn call_method_string_arg_ret_string<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
name: &str,
arg: impl AsRef<str>,
) -> JResult<JString<'j>> {
Ok(env
.call_method(
subject,
name,
"(Ljava/lang/String;)Ljava/lang/String;",
&[(&env.new_string(arg)?).into()],
)?
.l()?
.into())
}
pub fn call_method_string_arg_ret_object<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
method: &str,
arg: &str,
) -> JResult<JObject<'j>> {
env.call_method(
subject,
method,
"(Ljava/lang/String;)Ljava/lang/Object;",
&[(&env.new_string(arg)?).into()],
)?
.l()
}
pub fn get_package_manager<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
) -> JResult<JObject<'j>> {
env.call_method(
subject,
"getPackageManager",
"()Landroid/content/pm/PackageManager;",
&[],
)?
.l()
}
pub fn has_system_feature<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
name: &str,
) -> JResult<bool> {
call_method_string_arg_ret_bool(env, subject, "hasSystemFeature", name)
}
pub fn get_system_service<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
name: &str,
) -> JResult<JObject<'j>> {
call_method_string_arg_ret_object(env, subject, "getSystemService", name)
}
pub fn get_property<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
name: &str,
) -> JResult<JString<'j>> {
call_method_string_arg_ret_string(env, subject, "getProperty", name)
}
/// Read an Android system property
pub fn get_system_property<'j>(
env: &mut JNIEnv<'j>,
name: &str,
default_value: &str,
) -> JResult<JString<'j>> {
Ok(env
.call_static_method(
"android/os/SystemProperties",
"get",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
&[
(&env.new_string(name)?).into(),
(&env.new_string(default_value)?).into(),
],
)?
.l()?
.into())
}
pub fn get_devices<'j>(
env: &mut JNIEnv<'j>,
subject: &JObject<'j>,
flags: i32,
) -> JResult<JObjectArray<'j>> {
env.call_method(
subject,
"getDevices",
"(I)[Landroid/media/AudioDeviceInfo;",
&[flags.into()],
)?
.l()
.map(From::from)
}