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