use crate::permissions::PermissionState;
use serde::{Deserialize, Serialize};
#[cfg(target_os = "android")]
#[manganis::ffi("src/android/camera_microphone")]
unsafe extern "Kotlin" {
pub type CameraMicrophonePlugin;
}
#[cfg(target_os = "android")]
const CAMERA_MICROPHONE_CLASS: &str =
"dev.dioxus.g3_native_plugins.camera_microphone.CameraMicrophonePlugin";
#[cfg(target_os = "android")]
type CameraMicrophoneHandle = crate::android_bridge::AndroidPlugin;
#[cfg(target_os = "ios")]
type CameraMicrophoneHandle = CameraMicrophonePlugin;
#[cfg(target_os = "ios")]
#[manganis::ffi("src/ios")]
unsafe extern "Swift" {
pub type CameraMicrophonePlugin;
pub fn checkPermissionsFromRust(this: &CameraMicrophonePlugin) -> Option<String>;
pub fn requestCameraFromRust(this: &CameraMicrophonePlugin);
pub fn requestMicrophoneFromRust(this: &CameraMicrophonePlugin);
pub fn openSettingsFromRust(this: &CameraMicrophonePlugin);
pub fn setCapturingFromRust(this: &CameraMicrophonePlugin, capturing: bool);
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CapturePermissions {
pub camera: PermissionState,
pub microphone: PermissionState,
}
#[cfg(any(target_os = "android", target_os = "ios"))]
pub struct CameraMicrophone {
plugin: Option<CameraMicrophoneHandle>,
}
#[cfg(any(target_arch = "wasm32", target_os = "macos"))]
pub struct CameraMicrophone;
#[cfg(any(target_os = "android", target_os = "ios"))]
impl CameraMicrophone {
pub(crate) fn new() -> Self {
Self { plugin: None }
}
fn get_plugin(&mut self) -> Result<&CameraMicrophoneHandle, String> {
if self.plugin.is_none() {
#[cfg(target_os = "android")]
let created = CameraMicrophoneHandle::new(CAMERA_MICROPHONE_CLASS)?;
#[cfg(target_os = "ios")]
let created = CameraMicrophonePlugin::new()
.map_err(|error| format!("Failed to create CameraMicrophonePlugin: {error:?}"))?;
self.plugin = Some(created);
}
Ok(self.plugin.as_ref().unwrap())
}
pub fn prepare(&mut self) -> Result<(), String> {
self.get_plugin()?;
Ok(())
}
pub fn check_permissions(&mut self) -> Result<CapturePermissions, String> {
let plugin = self.get_plugin()?;
#[cfg(target_os = "android")]
let reported = plugin.call_string("checkPermissionsFromRust")?;
#[cfg(target_os = "ios")]
let reported = checkPermissionsFromRust(plugin)?;
let json = reported.ok_or_else(|| "Capture permissions were not reported".to_string())?;
serde_json::from_str(&json)
.map_err(|error| format!("Failed to read capture permissions: {error}"))
}
pub fn request_camera(&mut self) -> Result<(), String> {
let plugin = self.get_plugin()?;
#[cfg(target_os = "android")]
plugin.call_unit("requestCameraFromRust")?;
#[cfg(target_os = "ios")]
requestCameraFromRust(plugin)?;
Ok(())
}
pub fn request_microphone(&mut self) -> Result<(), String> {
let plugin = self.get_plugin()?;
#[cfg(target_os = "android")]
plugin.call_unit("requestMicrophoneFromRust")?;
#[cfg(target_os = "ios")]
requestMicrophoneFromRust(plugin)?;
Ok(())
}
pub fn open_settings(&mut self) -> Result<(), String> {
let plugin = self.get_plugin()?;
#[cfg(target_os = "android")]
plugin.call_unit("openSettingsFromRust")?;
#[cfg(target_os = "ios")]
openSettingsFromRust(plugin)?;
Ok(())
}
pub fn set_capturing(&mut self, capturing: bool) -> Result<(), String> {
let plugin = self.get_plugin()?;
#[cfg(target_os = "android")]
plugin.call_unit_bool("setCapturingFromRust", capturing)?;
#[cfg(target_os = "ios")]
setCapturingFromRust(plugin, capturing)?;
Ok(())
}
}
#[cfg(any(target_arch = "wasm32", target_os = "macos"))]
impl CameraMicrophone {
pub(crate) fn new() -> Self {
Self
}
pub fn prepare(&mut self) -> Result<(), String> {
Ok(())
}
pub fn check_permissions(&mut self) -> Result<CapturePermissions, String> {
Ok(CapturePermissions::default())
}
pub fn request_camera(&mut self) -> Result<(), String> {
Ok(())
}
pub fn request_microphone(&mut self) -> Result<(), String> {
Ok(())
}
pub fn open_settings(&mut self) -> Result<(), String> {
Ok(())
}
pub fn set_capturing(&mut self, _capturing: bool) -> Result<(), String> {
Ok(())
}
}