oboe/java_interface/
audio_features.rs

1use super::{
2    utils::{
3        get_context, get_package_manager, has_system_feature, with_attached, JNIEnv, JObject,
4        JResult,
5    },
6    PackageManager,
7};
8
9/**
10 * The Android audio features
11 */
12#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "java-interface")))]
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub enum AudioFeature {
15    LowLatency,
16    Output,
17    Pro,
18    Microphone,
19    Midi,
20}
21
22impl From<AudioFeature> for &'static str {
23    fn from(feature: AudioFeature) -> Self {
24        use AudioFeature::*;
25        match feature {
26            LowLatency => PackageManager::FEATURE_AUDIO_LOW_LATENCY,
27            Output => PackageManager::FEATURE_AUDIO_OUTPUT,
28            Pro => PackageManager::FEATURE_AUDIO_PRO,
29            Microphone => PackageManager::FEATURE_MICROPHONE,
30            Midi => PackageManager::FEATURE_MIDI,
31        }
32    }
33}
34
35impl AudioFeature {
36    /**
37     * Check availability of an audio feature using Android Java API
38     */
39    pub fn has(&self) -> Result<bool, String> {
40        let context = get_context();
41
42        with_attached(context, |env, activity| {
43            try_check_system_feature(env, &activity, (*self).into())
44        })
45        .map_err(|error| error.to_string())
46    }
47}
48
49fn try_check_system_feature<'j>(
50    env: &mut JNIEnv<'j>,
51    activity: &JObject<'j>,
52    feature: &str,
53) -> JResult<bool> {
54    let package_manager = get_package_manager(env, activity)?;
55
56    has_system_feature(env, &package_manager, feature)
57}