[NUI] Add Say, PauseResume of Dali-ATSPI (#947)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Accessibility.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #if (NUI_DEBUG_ON)
19
20
21 using global::System;
22 using System.ComponentModel;
23 using System.Runtime.InteropServices;
24 using Tizen.NUI.BaseComponents;
25 #if (NUI_DEBUG_ON)
26 using tlog = Tizen.Log;
27 #endif
28
29 namespace Tizen.NUI
30 {
31     /// <summary>
32     /// Accessibility provides Dali-ATSPI interface which has funtionality of Screen-Reader and general accessibility
33     /// </summary>
34     // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
35     [EditorBrowsable(EditorBrowsableState.Never)]
36     public class Accessibility
37     {
38         #region Constructor, Distructor, Dispose
39         private Accessibility()
40         {
41             dummy = new View();
42             dummy.Name = "dali-atspi-singleton";
43         }
44         ~Accessibility()
45         {
46
47         }
48         #endregion Constructor, Distructor, Dispose
49
50
51         #region Property
52         /// <summary>
53         /// Instance for singleton
54         /// </summary>
55         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
56         [EditorBrowsable(EditorBrowsableState.Never)]
57         public static Accessibility Instance
58         {
59             get => _accessibility;
60         }
61         #endregion Property
62
63
64         #region Method
65         /// <summary>
66         /// Get the current status
67         /// </summary>
68         /// <returns>Current enabled status</returns>
69         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
70         [EditorBrowsable(EditorBrowsableState.Never)]
71         static public bool GetStatus()
72         {
73             return true;
74         }
75
76         /// <summary>
77         /// Start to speak
78         /// </summary>
79         /// <param name="sentence">Content to be spoken</param>
80         /// <param name="discardable">true to be stopped and discarded when other Say is triggered</param>
81         /// <returns></returns>
82         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
83         [EditorBrowsable(EditorBrowsableState.Never)]
84         public bool Say(string sentence, bool discardable)
85         {
86             IntPtr callbackIntPtr = IntPtr.Zero;
87             if (_sayFinishedEventHandler != null)
88             {
89                 _sayFinishedEventCallbackType callback = _sayFinishedEventCallback;
90                 callbackIntPtr = Marshal.GetFunctionPointerForDelegate<Delegate>(callback);
91             }
92             bool ret = Interop.Accessibility.accessibility_say(View.getCPtr(dummy), sentence, discardable, callbackIntPtr);
93             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
94             return ret;
95         }
96
97         /// <summary>
98         /// To make Say be paused or resumed
99         /// </summary>
100         /// <param name="pause">true to be paused, false to be resumed</param>
101         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
102         [EditorBrowsable(EditorBrowsableState.Never)]
103         public void PauseResume(bool pause)
104         {
105             Interop.Accessibility.accessibility_pause_resume(View.getCPtr(dummy), pause);
106             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
107         }
108         #endregion Method
109
110
111         #region Event, Enum, Struct, ETC
112         /// <summary>
113         ///  Say Finished event arguments
114         /// </summary>
115         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         public class SayFinishedEventArgs : EventArgs
118         {
119             /// <summary>
120             /// The state of Say finished
121             /// </summary>
122             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
123             [EditorBrowsable(EditorBrowsableState.Never)]
124             public SayFinishedStates State
125             {
126                 private set;
127                 get;
128             }
129
130             internal SayFinishedEventArgs(int result)
131             {
132                 State = (SayFinishedStates)(result);
133                 tlog.Fatal(tag, $"SayFinishedEventArgs Constructor! State={State}");
134             }
135         }
136
137         /// <summary>
138         /// Enum of Say finished event argument status
139         /// </summary>
140         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
141         [EditorBrowsable(EditorBrowsableState.Never)]
142         public enum SayFinishedStates
143         {
144             /// <summary>
145             /// Invalid
146             /// </summary>
147             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
148             [EditorBrowsable(EditorBrowsableState.Never)]
149             Invalid = -1,
150             /// <summary>
151             /// Cancelled
152             /// </summary>
153             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
154             [EditorBrowsable(EditorBrowsableState.Never)]
155             Cancelled = 1,
156             /// <summary>
157             /// Stopped
158             /// </summary>
159             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
160             [EditorBrowsable(EditorBrowsableState.Never)]
161             Stopped = 2,
162             /// <summary>
163             /// Skipped
164             /// </summary>
165             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
166             [EditorBrowsable(EditorBrowsableState.Never)]
167             Skipped = 3
168         }
169
170         /// <summary>
171         /// When Say is finished, this event is triggered
172         /// </summary>
173         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
174         [EditorBrowsable(EditorBrowsableState.Never)]
175         public event EventHandler<SayFinishedEventArgs> SayFinished
176         {
177             add => _sayFinishedEventHandler += value;
178             remove => _sayFinishedEventHandler -= value;
179         }
180         #endregion Event, Enum, Struct, ETC
181
182
183         #region Internal
184         internal void PauseResume(View target, bool pause)
185         {
186             Interop.Accessibility.accessibility_pause_resume(View.getCPtr(target), pause);
187             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
188         }
189
190         internal bool Say(View target, string sentence, bool discardable)
191         {
192             IntPtr callbackIntPtr = IntPtr.Zero;
193             if (_sayFinishedEventHandler != null)
194             {
195                 _sayFinishedEventCallbackType callback = _sayFinishedEventCallback;
196                 callbackIntPtr = Marshal.GetFunctionPointerForDelegate<Delegate>(callback);
197             }
198             bool ret = Interop.Accessibility.accessibility_say(View.getCPtr(target), sentence, discardable, callbackIntPtr);
199             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200             return ret;
201         }
202         #endregion Internal
203
204
205         #region Private
206         private static readonly Accessibility _accessibility = new Accessibility();
207
208         private event EventHandler<SayFinishedEventArgs> _sayFinishedEventHandler;
209
210         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
211         private delegate void _sayFinishedEventCallbackType(int result);
212
213         private void _sayFinishedEventCallback(int result)
214         {
215             tlog.Fatal(tag, $"_sayFinishedEventCallback(res={result}) called!");
216             _sayFinishedEventHandler?.Invoke(this, new SayFinishedEventArgs(result));
217         }
218
219         private View dummy;
220
221         private static string tag = "NUITEST";
222         #endregion Private
223     }
224 }
225
226
227 #endif