nandi/jolt-nativepublic Fork 0
2ab40bf193b6a39bc8d1397cc36d17572bbf59cd
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

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