DALi signals refactor to remove V2 naming
[platform/core/uifw/dali-adaptor.git] / adaptors / ubuntu / accessibility-manager-impl-ubuntu.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
23 #include <dali/public-api/dali-core.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/events/touch-event-integ.h>
26 #include <dali/integration-api/events/hover-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 BaseHandle Create()
49 {
50   BaseHandle handle( AccessibilityManager::Get() );
51
52   if ( !handle )
53   {
54     Dali::SingletonService service( SingletonService::Get() );
55     if ( service )
56     {
57       Dali::AccessibilityManager manager = Dali::AccessibilityManager( new AccessibilityManager() );
58       service.Register( typeid( manager ), manager );
59       handle = manager;
60     }
61   }
62
63   return handle;
64 }
65 TypeRegistration ACCESSIBILITY_MANAGER_TYPE( typeid(Dali::AccessibilityManager), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
66
67 } // unnamed namespace
68
69 Dali::AccessibilityManager AccessibilityManager::Get()
70 {
71   Dali::AccessibilityManager manager;
72
73   Dali::SingletonService service( SingletonService::Get() );
74   if ( service )
75   {
76     // Check whether the singleton is already created
77     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::AccessibilityManager ) );
78     if(handle)
79     {
80       // If so, downcast the handle
81       manager = Dali::AccessibilityManager( dynamic_cast< AccessibilityManager* >( handle.GetObjectPtr() ) );
82     }
83   }
84
85   return manager;
86 }
87
88 Vector2 AccessibilityManager::GetReadPosition() const
89 {
90   return mReadPosition;
91 }
92
93 void AccessibilityManager::SetActionHandler(AccessibilityActionHandler& handler)
94 {
95   mActionHandler = &handler;
96 }
97
98 void AccessibilityManager::SetGestureHandler(AccessibilityGestureHandler& handler)
99 {
100   if( mAccessibilityGestureDetector )
101   {
102     mAccessibilityGestureDetector->SetGestureHandler(handler);
103   }
104 }
105
106 bool AccessibilityManager::HandleActionClearFocusEvent()
107 {
108   bool ret = false;
109
110   Dali::AccessibilityManager handle( this );
111
112   /*
113    * In order to application decide reading action first,
114    * emit ActionClearFocus signal in first, ClearAccessibilityFocus for handler in next
115    */
116   if ( !mIndicatorFocused )
117   {
118     if( !mActionClearFocusSignal.Empty() )
119     {
120       mActionClearFocusSignal.Emit( handle );
121     }
122   }
123
124   if( mActionHandler )
125   {
126     ret = mActionHandler->ClearAccessibilityFocus();
127   }
128
129   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
130
131   return ret;
132 }
133
134 bool AccessibilityManager::HandleActionScrollEvent(const TouchPoint& point, unsigned long timeStamp)
135 {
136   bool ret = false;
137
138   Dali::AccessibilityManager handle( this );
139
140   Dali::TouchEvent event(timeStamp);
141   event.points.push_back(point);
142
143   /*
144    * In order to application decide touch action first,
145    * emit ActionScroll signal in first, AccessibilityActionScroll  for handler in next
146    */
147   if ( !mIndicatorFocused )
148   {
149     if( !mActionScrollSignal.Empty() )
150     {
151       mActionScrollSignal.Emit( handle, event );
152     }
153   }
154
155   Integration::TouchEvent touchEvent;
156   Integration::HoverEvent hoverEvent;
157   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
158   if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth) // hover event is ignored
159   {
160     // Process the touch event in accessibility gesture detector
161     if( mAccessibilityGestureDetector )
162     {
163       mAccessibilityGestureDetector->SendEvent(touchEvent);
164       ret = true;
165     }
166   }
167
168   return ret;
169 }
170
171 bool AccessibilityManager::HandleActionTouchEvent(const TouchPoint& point, unsigned long timeStamp)
172 {
173   bool ret = false;
174
175   Dali::TouchEvent touchEvent(timeStamp);
176   touchEvent.points.push_back(point);
177
178   if( mActionHandler )
179   {
180     ret = mActionHandler->AccessibilityActionTouch(touchEvent);
181   }
182   return ret;
183 }
184
185 bool AccessibilityManager::HandleActionBackEvent()
186 {
187   bool ret = false;
188
189   Dali::AccessibilityManager handle( this );
190
191   /*
192    * In order to application decide reading action first,
193    * emit ActionBack signal in first, AccessibilityActionBack for handler in next
194    */
195   if ( !mIndicatorFocused )
196   {
197     if( !mActionBackSignal.Empty() )
198     {
199       mActionBackSignal.Emit( handle );
200     }
201   }
202
203   if( mActionHandler )
204   {
205     ret = mActionHandler->AccessibilityActionBack();
206   }
207
208   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
209
210   return ret;
211 }
212
213 void AccessibilityManager::HandleActionEnableEvent()
214 {
215   EnableAccessibility();
216 }
217
218 void AccessibilityManager::HandleActionDisableEvent()
219 {
220   DisableAccessibility();
221 }
222
223 void AccessibilityManager::EnableAccessibility()
224 {
225   if(mIsEnabled == false)
226   {
227     mIsEnabled = true;
228
229     if( mActionHandler )
230     {
231       mActionHandler->ChangeAccessibilityStatus();
232     }
233
234     //emit status changed signal
235     Dali::AccessibilityManager handle( this );
236     mStatusChangedSignal.Emit( handle );
237   }
238 }
239
240 void AccessibilityManager::DisableAccessibility()
241 {
242   if(mIsEnabled == true)
243   {
244     mIsEnabled = false;
245
246     if( mActionHandler )
247     {
248       mActionHandler->ChangeAccessibilityStatus();
249     }
250
251     //emit status changed signal
252     Dali::AccessibilityManager handle( this );
253     mStatusChangedSignal.Emit( handle );
254
255     // Destroy the TtsPlayer if exists.
256     if ( Adaptor::IsAvailable() )
257     {
258       Dali::Adaptor& adaptor = Dali::Adaptor::Get();
259       Adaptor::GetImplementation( adaptor ).DestroyTtsPlayer( Dali::TtsPlayer::SCREEN_READER );
260     }
261   }
262 }
263
264 bool AccessibilityManager::IsEnabled() const
265 {
266   return mIsEnabled;
267 }
268
269 void AccessibilityManager::SetIndicator(Indicator* indicator)
270 {
271   mIndicator = indicator;
272 }
273
274 AccessibilityManager::AccessibilityManager()
275 : mIsEnabled(false),
276   mActionHandler(NULL),
277   mIndicator(NULL),
278   mIndicatorFocused(false)
279 {
280   int isEnabled = 0;
281   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, isEnabled?"ENABLED":"DISABLED");
282
283   if(isEnabled == 1)
284   {
285     mIsEnabled = true;
286   }
287   else
288   {
289     mIsEnabled = false;
290   }
291
292   mAccessibilityGestureDetector = new AccessibilityGestureDetector();
293 }
294
295 AccessibilityManager::~AccessibilityManager()
296 {
297 }
298
299 bool AccessibilityManager::HandleActionNextEvent(bool allowEndFeedback)
300 {
301   bool ret = false;
302   Dali::AccessibilityManager handle( this );
303
304   /*
305    * In order to application decide reading action first,
306    * emit ActionNext signal in first, AccessibilityActionNext for handler in next
307    */
308   if( !mIndicatorFocused )
309   {
310     if( !mActionNextSignal.Empty() )
311     {
312       mActionNextSignal.Emit( handle );
313     }
314   }
315
316   if( mActionHandler )
317   {
318     ret = mActionHandler->AccessibilityActionNext(allowEndFeedback);
319   }
320
321   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
322
323   return ret;
324 }
325
326 bool AccessibilityManager::HandleActionPreviousEvent(bool allowEndFeedback)
327 {
328   bool ret = false;
329
330   Dali::AccessibilityManager handle( this );
331
332    /*
333    * In order to application decide reading action first,
334    * emit ActionPrevious signal in first, AccessibilityActionPrevious for handler in next
335    */
336  if ( !mIndicatorFocused )
337   {
338     if( !mActionPreviousSignal.Empty() )
339     {
340       mActionPreviousSignal.Emit( handle );
341     }
342   }
343
344   if( mActionHandler )
345   {
346     ret = mActionHandler->AccessibilityActionPrevious(allowEndFeedback);
347   }
348
349   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
350
351   return ret;
352 }
353
354 bool AccessibilityManager::HandleActionActivateEvent()
355 {
356   bool ret = false;
357
358   Dali::AccessibilityManager handle( this );
359
360   /*
361    * In order to application decide reading action first,
362    * emit ActionActivate signal in first, AccessibilityActionActivate for handler in next
363    */
364   if ( !mIndicatorFocused )
365   {
366     if( !mActionActivateSignal.Empty() )
367     {
368       mActionActivateSignal.Emit( handle );
369     }
370   }
371
372   if( mActionHandler )
373   {
374     ret = mActionHandler->AccessibilityActionActivate();
375   }
376
377   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
378
379   return ret;
380 }
381
382 bool AccessibilityManager::HandleActionReadEvent(unsigned int x, unsigned int y, bool allowReadAgain)
383 {
384   bool ret = false;
385
386   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %d , %d\n", __FUNCTION__, __LINE__, x, y);
387
388   mReadPosition.x = x;
389   mReadPosition.y = y;
390
391   Dali::AccessibilityManager handle( this );
392
393   bool indicatorFocused = false;
394
395   // Check whether the Indicator is focused
396   if( mIndicator && mIndicator->IsConnected() )
397   {
398     // Check the position and size of Indicator actor
399     Dali::Actor indicatorActor = mIndicator->GetActor();
400     Vector3 position = Vector3(0.0f, 0.0f, 0.0f);
401     Vector3 size = indicatorActor.GetCurrentSize();
402
403     if(mReadPosition.x >= position.x &&
404        mReadPosition.x <= position.x + size.width &&
405        mReadPosition.y >= position.y &&
406        mReadPosition.y <= position.y + size.height)
407     {
408       indicatorFocused = true;
409       DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] Indicator area!!!!\n", __FUNCTION__, __LINE__);
410     }
411   }
412
413   if( mIndicator )
414   {
415     if( !mIndicatorFocused && indicatorFocused )
416     {
417       // If Indicator is focused, the focus should be cleared in Dali focus chain.
418       if( mActionHandler )
419       {
420         mActionHandler->ClearAccessibilityFocus();
421       }
422     }
423
424     mIndicatorFocused = indicatorFocused;
425
426     // Send accessibility READ action information to Indicator
427     if( mIndicatorFocused )
428     {
429       DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] Send READ message to indicator!!!!\n", __FUNCTION__, __LINE__);
430     }
431   }
432
433   if(allowReadAgain)
434   {
435     /*
436      * In order to application decide reading action first,
437      * emit ActionRead signal in first, AccessibilityActionRead for handler in next
438      */
439     if( !mIndicatorFocused )
440     {
441       if ( !mActionReadSignal.Empty() )
442       {
443         mActionReadSignal.Emit( handle );
444       }
445     }
446   }
447   else
448   {
449     /*
450      * In order to application decide reading action first,
451      * emit ActionRead signal in first, AccessibilityActionRead for handler in next
452      */
453     if( !mIndicatorFocused )
454     {
455       if ( !mActionOverSignal.Empty() )
456       {
457         mActionOverSignal.Emit( handle );
458       }
459     }
460   }
461
462   if( mActionHandler && !mIndicatorFocused)
463   {
464     // If Indicator is not focused, the accessibility actions should be handled by the registered
465     // accessibility action handler (e.g. focus manager)
466     ret = mActionHandler->AccessibilityActionRead(allowReadAgain);
467     DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
468   }
469
470   return ret;
471 }
472
473 bool AccessibilityManager::HandleActionReadNextEvent(bool allowEndFeedback)
474 {
475   bool ret = false;
476
477   Dali::AccessibilityManager handle( this );
478
479   /*
480    * In order to application decide reading action first,
481    * emit ActionReadNext signal in first, AccessibilityActionReadNext for handler in next
482    */
483   if ( !mIndicatorFocused )
484   {
485     if( !mActionReadNextSignal.Empty() )
486     {
487       mActionReadNextSignal.Emit( handle );
488     }
489   }
490
491   if( mActionHandler )
492   {
493     ret = mActionHandler->AccessibilityActionReadNext(allowEndFeedback);
494   }
495
496   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
497
498   return ret;
499 }
500
501 bool AccessibilityManager::HandleActionReadPreviousEvent(bool allowEndFeedback)
502 {
503   bool ret = false;
504
505   Dali::AccessibilityManager handle( this );
506
507   /*
508    * In order to application decide reading action first,
509    * emit ActionReadPrevious signal in first, AccessibilityActionReadPrevious for handler in next
510    */
511   if ( !mIndicatorFocused )
512   {
513     if( !mActionReadPreviousSignal.Empty() )
514     {
515       mActionReadPreviousSignal.Emit( handle );
516     }
517   }
518
519   if( mActionHandler )
520   {
521     ret = mActionHandler->AccessibilityActionReadPrevious(allowEndFeedback);
522   }
523
524   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
525
526   return ret;
527 }
528
529 bool AccessibilityManager::HandleActionUpEvent()
530 {
531   bool ret = false;
532
533   Dali::AccessibilityManager handle( this );
534
535   /*
536    * In order to application decide reading action first,
537    * emit ActionUp signal in first, AccessibilityActionUp for handler in next
538    */
539   if ( !mIndicatorFocused )
540   {
541     if( !mActionUpSignal.Empty() )
542     {
543       mActionUpSignal.Emit( handle );
544     }
545   }
546
547   if( mActionHandler )
548   {
549     ret = mActionHandler->AccessibilityActionUp();
550   }
551
552   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
553
554   return ret;
555 }
556
557 bool AccessibilityManager::HandleActionDownEvent()
558 {
559   bool ret = false;
560
561   Dali::AccessibilityManager handle( this );
562
563   /*
564    * In order to application decide reading action first,
565    * emit ActionDown signal in first, AccessibilityActionDown for handler in next
566    */
567   if ( !mIndicatorFocused )
568   {
569     if( !mActionDownSignal.Empty() )
570     {
571       mActionDownSignal.Emit( handle );
572     }
573   }
574
575   if( mActionHandler )
576   {
577     ret = mActionHandler->AccessibilityActionDown();
578   }
579
580   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
581
582   return ret;
583 }
584
585 } // namespace Adaptor
586
587 } // namespace Internal
588
589 } // namespace Dali