9cd164fc7d14548fb1f40579cd376d14190eb7ce
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / accessibility-manager-impl-tizen.cpp
1 /*
2  * Copyright (c) 2014 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 // CLASS HEADER
19 #include "accessibility-manager-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <vconf.h>
23
24 #include <dali/public-api/dali-core.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/events/touch-event-integ.h>
27 #include <dali/integration-api/events/gesture-requests.h>
28 #include "system-settings.h"
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace Adaptor
37 {
38
39 namespace
40 {
41 #if defined(DEBUG_ENABLED)
42 Debug::Filter* gAccessibilityManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ACCESSIBILITY_MANAGER");
43 #endif
44
45 void AccessibilityOnOffNotification(keynode_t* node, void* data)
46 {
47   AccessibilityManager* manager = static_cast<AccessibilityManager*>(data);
48   int isEnabled = 0;
49   vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &isEnabled);
50
51   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, isEnabled?"ENABLED":"DISABLED");
52
53   if(isEnabled == 1)
54   {
55     manager->EnableAccessibility();
56   }
57   else
58   {
59     manager->DisableAccessibility();
60   }
61 }
62
63 BaseHandle Create()
64 {
65   BaseHandle handle( AccessibilityManager::Get() );
66
67   if ( !handle && Adaptor::IsAvailable() )
68   {
69     Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
70     Dali::AccessibilityManager manager = Dali::AccessibilityManager( new AccessibilityManager() );
71     adaptorImpl.RegisterSingleton( typeid( manager ), manager );
72     handle = manager;
73   }
74
75   return handle;
76 }
77 TypeRegistration ACCESSIBILITY_MANAGER_TYPE( typeid(Dali::AccessibilityManager), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
78
79 } // unnamed namespace
80
81 Dali::AccessibilityManager AccessibilityManager::Get()
82 {
83   Dali::AccessibilityManager manager;
84
85   if ( Adaptor::IsAvailable() )
86   {
87     // Check whether the singleton is already created
88     Dali::BaseHandle handle = Dali::Adaptor::Get().GetSingleton( typeid( Dali::AccessibilityManager ) );
89     if(handle)
90     {
91       // If so, downcast the handle
92       manager = Dali::AccessibilityManager( dynamic_cast< AccessibilityManager* >( handle.GetObjectPtr() ) );
93     }
94   }
95
96   return manager;
97 }
98
99 Vector2 AccessibilityManager::GetReadPosition() const
100 {
101   return mReadPosition;
102 }
103
104 void AccessibilityManager::SetActionHandler(AccessibilityActionHandler& handler)
105 {
106   mActionHandler = &handler;
107 }
108
109 void AccessibilityManager::SetGestureHandler(AccessibilityGestureHandler& handler)
110 {
111   if( mAccessibilityGestureDetector )
112   {
113     mAccessibilityGestureDetector->SetGestureHandler(handler);
114   }
115 }
116
117 bool AccessibilityManager::HandleActionClearFocusEvent()
118 {
119   bool ret = false;
120
121   Dali::AccessibilityManager handle( this );
122
123   /*
124    * In order to application decide reading action first,
125    * emit ActionClearFocus signal in first, ClearAccessibilityFocus for handler in next
126    */
127   if ( !mIndicatorFocused )
128   {
129     if( !mActionClearFocusSignalV2.Empty() )
130     {
131       mActionClearFocusSignalV2.Emit( handle );
132     }
133   }
134
135   if( mActionHandler )
136   {
137     ret = mActionHandler->ClearAccessibilityFocus();
138   }
139
140   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
141
142   return ret;
143 }
144
145 bool AccessibilityManager::HandleActionScrollEvent(const TouchPoint& point, unsigned long timeStamp)
146 {
147   bool ret = false;
148
149   Dali::AccessibilityManager handle( this );
150
151   Dali::TouchEvent touchEvent(timeStamp);
152   touchEvent.points.push_back(point);
153
154   /*
155    * In order to application decide touch action first,
156    * emit ActionScroll signal in first, AccessibilityActionScroll  for handler in next
157    */
158   if ( !mIndicatorFocused )
159   {
160     if( !mActionScrollSignalV2.Empty() )
161     {
162       mActionScrollSignalV2.Emit( handle, touchEvent );
163     }
164   }
165
166   Integration::TouchEvent event;
167   if (mCombiner.GetNextTouchEvent(point, timeStamp, event))
168   {
169     // Process the touch event in accessibility gesture detector
170     if( mAccessibilityGestureDetector )
171     {
172       mAccessibilityGestureDetector->SendEvent(event);
173       ret = true;
174     }
175   }
176
177   return ret;
178 }
179
180 bool AccessibilityManager::HandleActionTouchEvent(const TouchPoint& point, unsigned long timeStamp)
181 {
182   bool ret = false;
183
184   Dali::TouchEvent touchEvent(timeStamp);
185   touchEvent.points.push_back(point);
186
187   if( mActionHandler )
188   {
189     ret = mActionHandler->AccessibilityActionTouch(touchEvent);
190   }
191   return ret;
192 }
193
194 bool AccessibilityManager::HandleActionBackEvent()
195 {
196   bool ret = false;
197
198   Dali::AccessibilityManager handle( this );
199
200   /*
201    * In order to application decide reading action first,
202    * emit ActionBack signal in first, AccessibilityActionBack for handler in next
203    */
204   if ( !mIndicatorFocused )
205   {
206     if( !mActionBackSignalV2.Empty() )
207     {
208       mActionBackSignalV2.Emit( handle );
209     }
210   }
211
212   if( mActionHandler )
213   {
214     ret = mActionHandler->AccessibilityActionBack();
215   }
216
217   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
218
219   return ret;
220 }
221
222 void AccessibilityManager::HandleActionEnableEvent()
223 {
224   EnableAccessibility();
225 }
226
227 void AccessibilityManager::HandleActionDisableEvent()
228 {
229   DisableAccessibility();
230 }
231
232 void AccessibilityManager::EnableAccessibility()
233 {
234   if(mIsEnabled == false)
235   {
236     mIsEnabled = true;
237
238     if( mActionHandler )
239     {
240       mActionHandler->ChangeAccessibilityStatus();
241     }
242
243     //emit status changed signal
244     Dali::AccessibilityManager handle( this );
245     mStatusChangedSignalV2.Emit( handle );
246   }
247 }
248
249 void AccessibilityManager::DisableAccessibility()
250 {
251   if(mIsEnabled == true)
252   {
253     mIsEnabled = false;
254
255     if( mActionHandler )
256     {
257       mActionHandler->ChangeAccessibilityStatus();
258     }
259
260     //emit status changed signal
261     Dali::AccessibilityManager handle( this );
262     mStatusChangedSignalV2.Emit( handle );
263
264     // Destroy the TtsPlayer if exists.
265     Dali::Adaptor& adaptor = Dali::Adaptor::Get();
266     Adaptor::GetImplementation(adaptor).DestroyTtsPlayer(Dali::TtsPlayer::SCREEN_READER);
267   }
268 }
269
270 bool AccessibilityManager::IsEnabled() const
271 {
272   return mIsEnabled;
273 }
274
275 void AccessibilityManager::SetIndicator(Indicator* indicator)
276 {
277   mIndicator = indicator;
278 }
279
280 AccessibilityManager::AccessibilityManager()
281 : mIsEnabled(false),
282   mActionHandler(NULL),
283   mIndicator(NULL),
284   mIndicatorFocused(false)
285 {
286   int isEnabled = 0;
287   vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &isEnabled);
288   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, isEnabled?"ENABLED":"DISABLED");
289
290   if(isEnabled == 1)
291   {
292     mIsEnabled = true;
293   }
294   else
295   {
296     mIsEnabled = false;
297   }
298
299   vconf_notify_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, AccessibilityOnOffNotification, this );
300
301   mAccessibilityGestureDetector = new AccessibilityGestureDetector();
302 }
303
304 AccessibilityManager::~AccessibilityManager()
305 {
306   vconf_ignore_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, AccessibilityOnOffNotification );
307 }
308
309 } // namespace Adaptor
310
311 } // namespace Internal
312
313 } // namespace Dali