From da8260cba8ecce6626b52088af3c5458d22f641a Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 29 Dec 2020 15:45:56 -0600 Subject: [PATCH] Store the TTS object in the struct. --- src/backends/android.rs | 41 +++++++++++++++++++++++++++-------------- src/lib.rs | 2 +- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/backends/android.rs b/src/backends/android.rs index 1caee9a..6b4e66a 100644 --- a/src/backends/android.rs +++ b/src/backends/android.rs @@ -1,7 +1,9 @@ #[cfg(target_os = "android")] +use std::collections::HashMap; use std::sync::Mutex; -use jni::objects::JValue; +use jni::objects::GlobalRef; +use jni::JavaVM; use lazy_static::lazy_static; use log::info; @@ -9,11 +11,13 @@ use crate::{Backend, BackendId, Error, Features, UtteranceId, CALLBACKS}; lazy_static! { static ref NEXT_BACKEND_ID: Mutex = Mutex::new(0); + static ref VM: Mutex> = Mutex::new(None); } -#[derive(Clone, Debug)] +#[derive(Clone)] pub(crate) struct Android { id: BackendId, + tts: GlobalRef, } impl Android { @@ -23,18 +27,27 @@ impl Android { let id = BackendId::Android(*backend_id); *backend_id += 1; let native_activity = ndk_glue::native_activity(); - let vm_ptr = native_activity.vm(); - let vm = unsafe { jni::JavaVM::from_raw(vm_ptr) }?; - let env = vm.attach_current_thread()?; - let tts = env.new_object( - "android/speech/tts/TextToSpeech", - "(Landroid/content/Context;Landroid/speech/tts/TextToSpeech$OnInitListener;)V", - &[ - native_activity.activity().into(), - native_activity.activity().into(), - ], - )?; - Ok(Self { id }) + let mut vm = VM.lock().unwrap(); + if vm.is_none() { + let vm_ptr = native_activity.vm(); + let new_vm = unsafe { jni::JavaVM::from_raw(vm_ptr) }?; + *vm = Some(new_vm); + } + if let Some(vm) = &*vm { + let env = vm.attach_current_thread()?; + let tts = env.new_object( + "android/speech/tts/TextToSpeech", + "(Landroid/content/Context;Landroid/speech/tts/TextToSpeech$OnInitListener;)V", + &[ + native_activity.activity().into(), + native_activity.activity().into(), + ], + )?; + let tts = env.new_global_ref(tts)?; + Ok(Self { id, tts }) + } else { + Err(Error::NoneError) + } } } diff --git a/src/lib.rs b/src/lib.rs index ed32cf9..ff0a46d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,7 +121,7 @@ pub enum Error { } #[clonable] -trait Backend: Clone + std::fmt::Debug { +trait Backend: Clone { fn id(&self) -> Option; fn supported_features(&self) -> Features; fn speak(&mut self, text: &str, interrupt: bool) -> Result, Error>;