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
//! Run-time permissions that differ between devices, through
//! `CranposePermissions`, which an application carries only when its build
//! script declares a service that asks for them, such as
//! `Use::hand_tracking()`.
use jni::{jni_sig, jni_str, objects::JValue};
use super::jni::call_cranpose_class;
const PERMISSIONS_CLASS: &str = "dev/cranpose/android/CranposePermissions";
/// What the person has answered for a group of alternative Android permissions.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AndroidPermission {
/// No prompt was answered yet, or the system cannot be asked.
NotAsked,
/// One of the permissions is granted.
Granted,
/// The person refused. Android shows its prompt once more at most.
Refused,
/// The person refused for good: Android shows no prompt any more, and only the application's
/// page in the system settings can allow it.
Blocked,
}
impl AndroidPermission {
// The answer `CranposePermissions.cranposePermissionState` gives: 0 not asked, 1 granted,
// 2 refused, 3 refused for good.
pub(crate) const fn from_state(state: i32) -> Self {
match state {
1 => Self::Granted,
2 => Self::Refused,
3 => Self::Blocked,
_ => Self::NotAsked,
}
}
}
/// The answer for a group of `alternatives`, permissions of which any one satisfies the group,
/// such as the same permission under the names of two headsets' systems. Returns
/// [`AndroidPermission::NotAsked`] when the application's build carries no `CranposePermissions`,
/// which only a declared service brings.
pub fn android_permission(
app: &android_activity::AndroidApp,
alternatives: &[&str],
) -> AndroidPermission {
let names = alternatives.join("\n");
let state = call_cranpose_class(app, PERMISSIONS_CLASS, |env, activity, class| {
let names = env.new_string(&names)?;
let names: &jni::objects::JObject = names.as_ref();
env.call_static_method(
class,
jni_str!("cranposePermissionState"),
jni_sig!("(Landroid/app/Activity;Ljava/lang/String;)I"),
&[JValue::Object(activity), JValue::Object(names)],
)
.and_then(jni::JValueOwned::i)
});
match state {
Ok(state) => AndroidPermission::from_state(state),
Err(error) => {
log::warn!("cranpose: the permission {alternatives:?} could not be read: {error}");
AndroidPermission::NotAsked
}
}
}
/// Shows the system's prompt for those of `permissions` that the application does not hold. A
/// permission that the device does not define is refused with no prompt, so a list may name the
/// permissions of several headsets. The answer reaches [`android_permission`].
pub fn request_android_permissions(app: &android_activity::AndroidApp, permissions: &[&str]) {
let names = permissions.join("\n");
let shown = call_cranpose_class(app, PERMISSIONS_CLASS, |env, activity, class| {
let names = env.new_string(&names)?;
let names: &jni::objects::JObject = names.as_ref();
env.call_static_method(
class,
jni_str!("cranposeRequestPermissions"),
jni_sig!("(Landroid/app/Activity;Ljava/lang/String;)V"),
&[JValue::Object(activity), JValue::Object(names)],
)
.and_then(jni::JValueOwned::v)
});
if let Err(error) = shown {
log::warn!("cranpose: the permissions {permissions:?} were not requested: {error}");
}
}