[Tts][TCSACR-497] Add new API for service state information (#4488)
authorSuyeon Hwang <35016426+stom-hwang@users.noreply.github.com>
Tue, 23 Aug 2022 07:34:18 +0000 (16:34 +0900)
committerGitHub <noreply@github.com>
Tue, 23 Aug 2022 07:34:18 +0000 (16:34 +0900)
* Add new method for checking TTS service state

Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
* Fix doxygen description more clear

Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
src/Tizen.Uix.Tts/Interop/Interop.Tts.cs
src/Tizen.Uix.Tts/Tizen.Uix.Tts/ServiceStateChangedEventArgs.cs [new file with mode: 0644]
src/Tizen.Uix.Tts/Tizen.Uix.Tts/TtsClient.cs

index 5de83b5..a2afa16 100755 (executable)
@@ -77,6 +77,9 @@ internal static partial class Interop
         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         internal delegate void TtsScreenReaderChangedCB(IntPtr handle, bool is_on, IntPtr userData);
 
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void TtsServiceStateChangedCB(IntPtr handle, ServiceState previous, ServiceState current, IntPtr userData);
+
         [DllImport(Libraries.Tts, EntryPoint = "tts_create", CallingConvention = CallingConvention.Cdecl)]
         internal static extern TtsError TtsCreate(out IntPtr handle);
 
@@ -116,6 +119,9 @@ internal static partial class Interop
         [DllImport(Libraries.Tts, EntryPoint = "tts_get_state", CallingConvention = CallingConvention.Cdecl)]
         internal static extern TtsError TtsGetState(IntPtr handle, out State state);
 
+        [DllImport(Libraries.Tts, EntryPoint = "tts_get_service_state", CallingConvention = CallingConvention.Cdecl)]
+        internal static extern TtsError TtsGetServiceState(IntPtr handle, out ServiceState serviceState);
+
         [DllImport(Libraries.Tts, EntryPoint = "tts_get_speed_range", CallingConvention = CallingConvention.Cdecl)]
         internal static extern TtsError TtsGetSpeedRange(IntPtr handle, out int min, out int normal, out int max);
 
@@ -181,5 +187,11 @@ internal static partial class Interop
 
         [DllImport(Libraries.Tts, EntryPoint = "tts_unset_screen_reader_changed_cb", CallingConvention = CallingConvention.Cdecl)]
         internal static extern TtsError TtsUnsetScreenReaderChangedCB(IntPtr handle);
+
+        [DllImport(Libraries.Tts, EntryPoint = "tts_set_service_state_changed_cb", CallingConvention = CallingConvention.Cdecl)]
+        internal static extern TtsError TtsSetServiceStateChangedCB(IntPtr handle, TtsServiceStateChangedCB callback, IntPtr userData);
+
+        [DllImport(Libraries.Tts, EntryPoint = "tts_unset_service_state_changed_cb", CallingConvention = CallingConvention.Cdecl)]
+        internal static extern TtsError TtsUnsetServiceStateChangedCB(IntPtr handle);
     }
 }
diff --git a/src/Tizen.Uix.Tts/Tizen.Uix.Tts/ServiceStateChangedEventArgs.cs b/src/Tizen.Uix.Tts/Tizen.Uix.Tts/ServiceStateChangedEventArgs.cs
new file mode 100644 (file)
index 0000000..10527e6
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+* Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+*
+* Licensed under the Apache License, Version 2.0 (the License);
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an AS IS BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+
+namespace Tizen.Uix.Tts
+{
+    /// <summary>
+    /// This class holds information related to the TTS ServiceStateChanged event.
+    /// </summary>
+    /// <since_tizen> 10 </since_tizen>
+    public class ServiceStateChangedEventArgs
+    {
+        internal ServiceStateChangedEventArgs(ServiceState previous, ServiceState current)
+        {
+            Previous = previous;
+            Current = current;
+        }
+
+        /// <summary>
+        /// The previous state of TTS service.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public ServiceState Previous
+        {
+            get;
+            internal set;
+        }
+
+        /// <summary>
+        /// The current state of TTS service.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public ServiceState Current
+        {
+            get;
+            internal set;
+        }
+    }
+}
index 1637f37..e5b359b 100644 (file)
@@ -204,6 +204,33 @@ namespace Tizen.Uix.Tts
     };
 
     /// <summary>
+    /// Enumeration for the states of TTS service.
+    /// </summary>
+    /// <since_tizen> 10 </since_tizen>
+    public enum ServiceState
+    {
+        /// <summary>
+        /// Ready state.
+        /// </summary>
+        Ready = 0,
+
+        /// <summary>
+        /// Synthesizing state.
+        /// </summary>
+        Synthesizing = 1,
+
+        /// <summary>
+        /// Playing state.
+        /// </summary>
+        Playing = 2,
+
+        /// <summary>
+        /// Unavailable state.
+        /// </summary>
+        Unavailable
+    };
+
+    /// <summary>
     /// You can use Text-To-Speech (TTS) API's to read sound data transformed by the engine from input texts.
     /// Applications can add input-text to queue for reading continuously and control the player that can play, pause, and stop sound data synthesized from text.
     /// </summary>
@@ -218,6 +245,7 @@ namespace Tizen.Uix.Tts
         private event EventHandler<DefaultVoiceChangedEventArgs> _defaultVoiceChanged;
         private event EventHandler<EngineChangedEventArgs> _engineChanged;
         private event EventHandler<ScreenReaderChangedEventArgs> _screenReaderChanged;
+        private event EventHandler<ServiceStateChangedEventArgs> _serviceStateChanged;
         private bool disposedValue = false;
         private readonly Object _stateChangedLock = new Object();
         private readonly Object _utteranceStartedLock = new Object();
@@ -226,6 +254,7 @@ namespace Tizen.Uix.Tts
         private readonly Object _defaultVoiceChangedLock = new Object();
         private readonly Object _engineChangedLock = new Object();
         private readonly Object _screenReaderChangedLock = new Object();
+        private readonly Object _serviceStateChangedLock = new Object();
         private TtsStateChangedCB _stateDelegate;
         private TtsUtteranceStartedCB _utteranceStartedResultDelegate;
         private TtsUtteranceCompletedCB _utteranceCompletedResultDelegate;
@@ -233,6 +262,7 @@ namespace Tizen.Uix.Tts
         private TtsDefaultVoiceChangedCB _voiceChangedDelegate;
         private TtsEngineChangedCB _engineDelegate;
         private TtsScreenReaderChangedCB _screenReaderDelegate;
+        private TtsServiceStateChangedCB _serviceStateDelegate;
         private TtsSupportedVoiceCB _supportedvoiceDelegate;
 
         /// <summary>
@@ -590,6 +620,51 @@ namespace Tizen.Uix.Tts
         }
 
         /// <summary>
+        /// Event to be invoked when the state of TTS service changes.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public event EventHandler<ServiceStateChangedEventArgs> ServiceStateChanged
+        {
+            add
+            {
+                lock (_serviceStateChangedLock)
+                {
+                    if (_serviceStateChanged == null)
+                    {
+                        _serviceStateDelegate = (IntPtr handle, ServiceState previous, ServiceState current, IntPtr userData) =>
+                        {
+                            ServiceStateChangedEventArgs args = new ServiceStateChangedEventArgs(previous, current);
+                            _serviceStateChanged?.Invoke(this, args);
+                        };
+
+                        TtsError error = TtsSetServiceStateChangedCB(_handle, _serviceStateDelegate, IntPtr.Zero);
+                        if (error != TtsError.None)
+                        {
+                            Log.Error(LogTag, "Add ServiceStateChanged Failed with error " + error);
+                        }
+                    }
+                    _serviceStateChanged += value;
+                }
+            }
+
+            remove
+            {
+                lock (_serviceStateChangedLock)
+                {
+                    _serviceStateChanged -= value;
+                    if (_serviceStateChanged == null)
+                    {
+                        TtsError error = TtsUnsetStateChangedCB(_handle);
+                        if (error != TtsError.None)
+                        {
+                            Log.Error(LogTag, "Remove ServiceStateChanged Failed with error " + error);
+                        }
+                    }
+                }
+            }
+        }
+
+        /// <summary>
         /// Gets the default voice set by the user.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
@@ -674,6 +749,29 @@ namespace Tizen.Uix.Tts
         }
 
         /// <summary>
+        /// Gets the current state of TTS service.
+        /// </summary>
+        /// <value>
+        /// The current state of TTS service.
+        /// </value>
+        /// <since_tizen> 10 </since_tizen>
+        public ServiceState CurrentServiceState
+        {
+            get
+            {
+                ServiceState state;
+                TtsError error = TtsGetServiceState(_handle, out state);
+                if (error != TtsError.None)
+                {
+                    Log.Error(LogTag, "CurrentServiceState Failed with error " + error);
+                    return ServiceState.Unavailable;
+                }
+
+                return state;
+            }
+        }
+
+        /// <summary>
         /// The TTS Mode can be set using this property.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>