From 5849e340c96d349606ed7a600f25754957ed85bd Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Sun, 27 Dec 2020 09:41:11 -0600 Subject: [PATCH] Add initial Android stubs. --- Cargo.toml | 3 ++ src/backends/android.rs | 114 ++++++++++++++++++++++++++++++++++++++++ src/backends/mod.rs | 6 +++ src/lib.rs | 8 +++ 4 files changed, 131 insertions(+) create mode 100644 src/backends/android.rs diff --git a/Cargo.toml b/Cargo.toml index fd7424c..16cfdc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,3 +39,6 @@ objc = "0.2" [target.wasm32-unknown-unknown.dependencies] wasm-bindgen = "0.2" web-sys = { version = "0.3", features = ["EventTarget", "SpeechSynthesis", "SpeechSynthesisErrorCode", "SpeechSynthesisErrorEvent", "SpeechSynthesisEvent", "SpeechSynthesisUtterance", "Window", ] } + +[target.'cfg(target_os="android")'.dependencies] +jni = "0.18" \ No newline at end of file diff --git a/src/backends/android.rs b/src/backends/android.rs new file mode 100644 index 0000000..a2d1fc4 --- /dev/null +++ b/src/backends/android.rs @@ -0,0 +1,114 @@ +#[cfg(target_os = "android")] +use std::sync::Mutex; + +use lazy_static::lazy_static; +use log::info; + +use crate::{Backend, BackendId, Error, Features, UtteranceId, CALLBACKS}; + +lazy_static! { + static ref NEXT_BACKEND_ID: Mutex = Mutex::new(0); +} + +#[derive(Clone, Debug)] +pub(crate) struct Android(BackendId); + +impl Android { + pub(crate) fn new() -> Self { + info!("Initializing Android backend"); + let mut backend_id = NEXT_BACKEND_ID.lock().unwrap(); + let bid = BackendId::Android(*backend_id); + *backend_id += 1; + Self(bid) + } +} + +impl Backend for Android { + fn id(&self) -> Option { + Some(self.0) + } + + fn supported_features(&self) -> Features { + Features { + stop: false, + rate: false, + pitch: false, + volume: false, + is_speaking: false, + utterance_callbacks: false, + } + } + + fn speak(&mut self, text: &str, interrupt: bool) -> Result, Error> { + println!("Speaking {}, {:?}", text, interrupt); + Ok(None) + } + + fn stop(&mut self) -> Result<(), Error> { + todo!() + } + + fn min_rate(&self) -> f32 { + todo!() + } + + fn max_rate(&self) -> f32 { + todo!() + } + + fn normal_rate(&self) -> f32 { + todo!() + } + + fn get_rate(&self) -> Result { + todo!() + } + + fn set_rate(&mut self, rate: f32) -> Result<(), Error> { + todo!() + } + + fn min_pitch(&self) -> f32 { + todo!() + } + + fn max_pitch(&self) -> f32 { + todo!() + } + + fn normal_pitch(&self) -> f32 { + todo!() + } + + fn get_pitch(&self) -> Result { + todo!() + } + + fn set_pitch(&mut self, pitch: f32) -> Result<(), Error> { + todo!() + } + + fn min_volume(&self) -> f32 { + todo!() + } + + fn max_volume(&self) -> f32 { + todo!() + } + + fn normal_volume(&self) -> f32 { + todo!() + } + + fn get_volume(&self) -> Result { + todo!() + } + + fn set_volume(&mut self, volume: f32) -> Result<(), Error> { + todo!() + } + + fn is_speaking(&self) -> Result { + todo!() + } +} diff --git a/src/backends/mod.rs b/src/backends/mod.rs index 408e5bf..4e61063 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -16,6 +16,9 @@ mod appkit; #[cfg(any(target_os = "macos", target_os = "ios"))] mod av_foundation; +#[cfg(target_os = "android")] +mod android; + #[cfg(target_os = "linux")] pub(crate) use self::speech_dispatcher::*; @@ -30,3 +33,6 @@ pub(crate) use self::appkit::*; #[cfg(any(target_os = "macos", target_os = "ios"))] pub(crate) use self::av_foundation::*; + +#[cfg(target_os = "android")] +pub(crate) use self::android::*; diff --git a/src/lib.rs b/src/lib.rs index 820ccd3..7bebbeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,8 @@ pub enum Backends { AppKit, #[cfg(any(target_os = "macos", target_os = "ios"))] AvFoundation, + #[cfg(target_os = "android")] + Android, } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -55,6 +57,8 @@ pub enum BackendId { WinRT(u64), #[cfg(any(target_os = "macos", target_os = "ios"))] AvFoundation(u64), + #[cfg(target_os = "android")] + Android(u64), } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -193,6 +197,8 @@ impl TTS { Backends::AppKit => Ok(TTS(Box::new(backends::AppKit::new()))), #[cfg(any(target_os = "macos", target_os = "ios"))] Backends::AvFoundation => Ok(TTS(Box::new(backends::AvFoundation::new()))), + #[cfg(target_os = "android")] + Backends::Android => Ok(TTS(Box::new(backends::Android::new()))), }; if let Ok(backend) = backend { if let Some(id) = backend.0.id() { @@ -239,6 +245,8 @@ impl TTS { }; #[cfg(target_os = "ios")] let tts = TTS::new(Backends::AvFoundation); + #[cfg(target_os = "android")] + let tts = TTS::new(Backends::Android); tts }