Fix focus issue after calling ClearFocus api
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyboard-focus-manager-impl.cpp
1 /*
2  * Copyright (c) 2017 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 "keyboard-focus-manager-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/devel-api/adaptor-framework/accessibility-adaptor.h>
25 #include <dali/devel-api/adaptor-framework/singleton-service.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/common/stage.h>
28 #include <dali/public-api/events/key-event.h>
29 #include <dali/public-api/events/touch-data.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/public-api/object/type-registry-helper.h>
32 #include <dali/public-api/object/property-map.h>
33 #include <dali/public-api/images/resource-image.h>
34 #include <dali/integration-api/debug.h>
35
36 // INTERNAL INCLUDES
37 #include <dali-toolkit/public-api/controls/control.h>
38 #include <dali-toolkit/public-api/controls/control-impl.h>
39 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
40 #include <dali-toolkit/public-api/accessibility-manager/accessibility-manager.h>
41 #include <dali-toolkit/devel-api/controls/control-devel.h>
42 #include <dali-toolkit/public-api/styling/style-manager.h>
43 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
44
45 namespace Dali
46 {
47
48 namespace Toolkit
49 {
50
51 namespace Internal
52 {
53
54 namespace // Unnamed namespace
55 {
56
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
59 #endif
60
61 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
62
63 const char* const FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.9.png";
64
65 BaseHandle Create()
66 {
67   BaseHandle handle = KeyboardFocusManager::Get();
68
69   if ( !handle )
70   {
71     SingletonService singletonService( SingletonService::Get() );
72     if ( singletonService )
73     {
74       Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
75       singletonService.Register( typeid( manager ), manager );
76       handle = manager;
77     }
78   }
79
80   return handle;
81 }
82
83 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
84
85 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange",           SIGNAL_PRE_FOCUS_CHANGE )
86 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged",             SIGNAL_FOCUS_CHANGED )
87 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged",        SIGNAL_FOCUS_GROUP_CHANGED )
88 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey",     SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
89
90 DALI_TYPE_REGISTRATION_END()
91
92 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
93
94 } // unnamed namespace
95
96 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
97 {
98   Toolkit::KeyboardFocusManager manager;
99
100   SingletonService singletonService( SingletonService::Get() );
101   if ( singletonService )
102   {
103     // Check whether the keyboard focus manager is already created
104     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
105     if(handle)
106     {
107       // If so, downcast the handle of singleton to keyboard focus manager
108       manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
109     }
110   }
111
112   return manager;
113 }
114
115 KeyboardFocusManager::KeyboardFocusManager()
116 : mPreFocusChangeSignal(),
117   mFocusChangedSignal(),
118   mFocusGroupChangedSignal(),
119   mFocusedActorEnterKeySignal(),
120   mCurrentFocusActor(),
121   mFocusIndicatorActor(),
122   mIsFocusIndicatorEnabled( -1 ),
123   mFocusGroupLoopEnabled( false ),
124   mIsWaitingKeyboardFocusChangeCommit( false ),
125   mClearFocusOnTouch( true ),
126   mFocusHistory(),
127   mSlotDelegate( this ),
128   mCustomAlgorithmInterface(NULL)
129 {
130   // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorEnabled.
131   Stage::GetCurrent().KeyEventSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
132   Stage::GetCurrent().TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
133 }
134
135 KeyboardFocusManager::~KeyboardFocusManager()
136 {
137 }
138
139 void KeyboardFocusManager::GetConfigurationFromStyleManger()
140 {
141     Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
142     if( styleManager )
143     {
144       Property::Map config = Toolkit::DevelStyleManager::GetConfigurations( styleManager );
145       mIsFocusIndicatorEnabled = static_cast<int>(config["alwaysShowFocus"].Get<bool>());
146       mClearFocusOnTouch = mIsFocusIndicatorEnabled ? false : true;
147     }
148 }
149
150 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
151 {
152   DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
153
154   if( mIsFocusIndicatorEnabled == -1 )
155   {
156     GetConfigurationFromStyleManger();
157   }
158
159   return DoSetCurrentFocusActor( actor );
160 }
161
162 bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
163 {
164   bool success = false;
165
166   Actor currentFocusedActor = GetCurrentFocusActor();
167
168   // If developer set focus on same actor, doing nothing
169   if( actor == currentFocusedActor )
170   {
171     if( !actor )
172     {
173       return false;
174     }
175     return true;
176   }
177
178   // Check whether the actor is in the stage and is keyboard focusable.
179   if( actor && actor.IsKeyboardFocusable() && actor.OnStage() )
180   {
181     if( mIsFocusIndicatorEnabled )
182     {
183       actor.Add( GetFocusIndicatorActor() );
184     }
185
186     // Send notification for the change of focus actor
187     if( !mFocusChangedSignal.Empty() )
188     {
189       mFocusChangedSignal.Emit(currentFocusedActor, actor);
190     }
191
192     Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
193     if( currentlyFocusedControl )
194     {
195       // Do we need it to remember if it was previously DISABLED?
196       currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
197       currentlyFocusedControl.ClearKeyInputFocus();
198     }
199
200     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
201
202     // Save the current focused actor
203     mCurrentFocusActor = actor;
204
205     Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
206     if( newlyFocusedControl )
207     {
208       newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
209       newlyFocusedControl.SetKeyInputFocus();
210     }
211
212     // Push Current Focused Actor to FocusHistory
213     mFocusHistory.push_back( actor );
214
215     // Delete first element before add new element when Stack is full.
216     if( mFocusHistory.size() > MAX_HISTORY_AMOUNT )
217     {
218        FocusStackIterator beginPos = mFocusHistory.begin();
219        mFocusHistory.erase( beginPos );
220     }
221
222     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
223     success = true;
224   }
225   else
226   {
227     DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
228   }
229
230   return success;
231 }
232
233 Actor KeyboardFocusManager::GetCurrentFocusActor()
234 {
235   Actor actor = mCurrentFocusActor.GetHandle();
236   if( actor && ! actor.OnStage() )
237   {
238     // If the actor has been removed from the stage, then it should not be focused
239
240     actor.Reset();
241     mCurrentFocusActor.Reset();
242   }
243   return actor;
244 }
245
246 Actor KeyboardFocusManager::GetCurrentFocusGroup()
247 {
248   return GetFocusGroup(GetCurrentFocusActor());
249 }
250
251 void KeyboardFocusManager::MoveFocusBackward()
252 {
253   // Find Pre Focused Actor when the list size is more than 1
254   if( mFocusHistory.size() > 1 )
255   {
256     // Delete current focused actor in history
257     mFocusHistory.pop_back();
258
259     // If pre-focused actors are not on stage or deleted, remove them in stack
260     while( mFocusHistory.size() > 0 )
261     {
262       // Get pre focused actor
263       Actor target = mFocusHistory[ mFocusHistory.size() -1 ].GetHandle();
264
265       // Impl of Actor is not null
266       if( target && target.OnStage() )
267       {
268         // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
269         mFocusHistory.pop_back();
270         SetCurrentFocusActor( target );
271         break;
272       }
273       else
274       {
275         // Target is empty handle or off stage. Erase from queue
276         mFocusHistory.pop_back();
277       }
278     }
279
280     // if there is no actor which can get focus, then push current focus actor in stack again
281     if( mFocusHistory.size() == 0 )
282     {
283       Actor currentFocusedActor = GetCurrentFocusActor();
284       mFocusHistory.push_back( currentFocusedActor );
285     }
286   }
287 }
288
289 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
290 {
291   Toolkit::Control control = Toolkit::Control::DownCast(actor);
292   return control && GetImplementation( control ).IsKeyboardNavigationSupported();
293 }
294
295 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
296 {
297   // Get the actor's parent layout control that supports two dimensional keyboard navigation
298   Actor rootActor = Stage::GetCurrent().GetRootLayer();
299   Actor parent;
300   if(actor)
301   {
302     parent = actor.GetParent();
303   }
304
305   while( parent && !IsLayoutControl(parent) && parent != rootActor )
306   {
307     parent = parent.GetParent();
308   }
309
310   return Toolkit::Control::DownCast(parent);
311 }
312
313 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
314 {
315   Actor currentFocusActor = GetCurrentFocusActor();
316
317   bool succeed = false;
318
319   // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
320   Toolkit::Control parentLayoutControl = GetParentLayoutControl( currentFocusActor );
321   while( parentLayoutControl && !succeed )
322   {
323     succeed = DoMoveFocusWithinLayoutControl( parentLayoutControl, currentFocusActor, direction );
324     parentLayoutControl = GetParentLayoutControl( parentLayoutControl );
325   }
326
327   if( !succeed )
328   {
329     Actor nextFocusableActor;
330
331     Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
332
333     // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
334     if( currentFocusControl )
335     {
336       int actorId = -1;
337       Property::Index index = Property::INVALID_INDEX;
338       Property::Value value;
339
340       // Find property index based upon focus direction
341       switch ( direction )
342       {
343         case Toolkit::Control::KeyboardFocus::LEFT:
344         {
345           index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
346           break;
347         }
348         case Toolkit::Control::KeyboardFocus::RIGHT:
349         {
350           index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
351           break;
352         }
353         case Toolkit::Control::KeyboardFocus::UP:
354         {
355           index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
356           break;
357         }
358         case Toolkit::Control::KeyboardFocus::DOWN:
359         {
360           index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
361           break;
362         }
363         default:
364           break;
365       }
366
367       // If the focusable property is set then determine next focusable actor
368       if( index != Property::INVALID_INDEX)
369       {
370         value = currentFocusActor.GetProperty( index );
371         actorId = value.Get<int>();
372
373         // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
374         if( actorId != -1 )
375         {
376           if( currentFocusActor.GetParent() )
377           {
378             nextFocusableActor = currentFocusActor.GetParent().FindChildById( actorId );
379           }
380
381           if( !nextFocusableActor )
382           {
383             nextFocusableActor = Stage::GetCurrent().GetRootLayer().FindChildById( actorId );
384           }
385         }
386       }
387     }
388
389     if( !nextFocusableActor )
390     {
391       // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
392       if( mCustomAlgorithmInterface )
393       {
394         mIsWaitingKeyboardFocusChangeCommit = true;
395         nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor( currentFocusActor, Actor(), direction );
396         mIsWaitingKeyboardFocusChangeCommit = false;
397       }
398       else if( !mPreFocusChangeSignal.Empty() )
399       {
400         // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
401         mIsWaitingKeyboardFocusChangeCommit = true;
402         nextFocusableActor = mPreFocusChangeSignal.Emit( currentFocusActor, Actor(), direction );
403         mIsWaitingKeyboardFocusChangeCommit = false;
404       }
405     }
406
407     if( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
408     {
409       // Whether the next focusable actor is a layout control
410       if( IsLayoutControl( nextFocusableActor ) )
411       {
412         // If so, move the focus inside it.
413         Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
414         succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
415       }
416       else
417       {
418         // Otherwise, just set focus to the next focusable actor
419         succeed = SetCurrentFocusActor( nextFocusableActor );
420       }
421     }
422   }
423
424   return succeed;
425 }
426
427 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
428 {
429   // Ask the control for the next actor to focus
430   Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
431   if(nextFocusableActor)
432   {
433     if(!nextFocusableActor.IsKeyboardFocusable())
434     {
435       // If the actor is not focusable, ask the same layout control for the next actor to focus
436       return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
437     }
438     else
439     {
440       Actor currentFocusActor = GetCurrentFocusActor();
441       Actor committedFocusActor = nextFocusableActor;
442
443       // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
444       // Signal handler can check the proposed actor and return a different actor if it wishes.
445       if( !mPreFocusChangeSignal.Empty() )
446       {
447         mIsWaitingKeyboardFocusChangeCommit = true;
448         committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
449         mIsWaitingKeyboardFocusChangeCommit = false;
450       }
451
452       if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
453       {
454         // Whether the commited focusable actor is a layout control
455         if(IsLayoutControl(committedFocusActor))
456         {
457           // If so, move the focus inside it.
458           Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
459           return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
460         }
461         else
462         {
463           // Otherwise, just set focus to the next focusable actor
464           if(committedFocusActor == nextFocusableActor)
465           {
466             // If the application hasn't changed our proposed actor, we informs the layout control we will
467             // move the focus to what the control returns. The control might wish to perform some actions
468             // before the focus is actually moved.
469             GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
470           }
471
472           return SetCurrentFocusActor(committedFocusActor);
473         }
474       }
475       else
476       {
477         return false;
478       }
479     }
480   }
481   else
482   {
483     // No more actor can be focused in the given direction within the same layout control.
484     return false;
485   }
486 }
487
488 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
489 {
490   bool succeed = false;
491
492   // Get the parent layout control of the current focus group
493   Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
494
495   while(parentLayoutControl && !succeed)
496   {
497     // If the current focus group has a parent layout control, we can probably automatically
498     // move the focus to the next focus group in the forward or backward direction.
499     Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
500     succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
501     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
502   }
503
504   if(!mFocusGroupChangedSignal.Empty())
505   {
506     // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
507     mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
508   }
509
510   return succeed;
511 }
512
513 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
514 {
515   if( actor )
516   {
517     Toolkit::Control control = Toolkit::Control::DownCast( actor );
518     if( control )
519     {
520       // Notify the control that enter has been pressed on it.
521       GetImplementation( control ).KeyboardEnter();
522     }
523
524     // Send a notification for the actor.
525     if( !mFocusedActorEnterKeySignal.Empty() )
526     {
527       mFocusedActorEnterKeySignal.Emit( actor );
528     }
529   }
530 }
531
532 void KeyboardFocusManager::ClearFocus()
533 {
534   Actor actor = GetCurrentFocusActor();
535   if( actor )
536   {
537     if( mFocusIndicatorActor )
538     {
539       actor.Remove( mFocusIndicatorActor );
540     }
541
542     // Send notification for the change of focus actor
543     if( !mFocusChangedSignal.Empty() )
544     {
545       mFocusChangedSignal.Emit( actor, Actor() );
546     }
547
548     Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
549     if( currentlyFocusedControl )
550     {
551       currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
552       currentlyFocusedControl.ClearKeyInputFocus();
553     }
554   }
555
556   mCurrentFocusActor.Reset();
557   mIsFocusIndicatorEnabled = 0;
558 }
559
560 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
561 {
562   mFocusGroupLoopEnabled = enabled;
563 }
564
565 bool KeyboardFocusManager::GetFocusGroupLoop() const
566 {
567   return mFocusGroupLoopEnabled;
568 }
569
570 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
571 {
572   if(actor)
573   {
574     // Create/Set focus group property.
575     actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
576   }
577 }
578
579 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
580 {
581   // Check whether the actor is a focus group
582   bool isFocusGroup = false;
583
584   if(actor)
585   {
586     Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
587     if(propertyIsFocusGroup != Property::INVALID_INDEX)
588     {
589       isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
590     }
591   }
592
593   return isFocusGroup;
594 }
595
596 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
597 {
598   // Go through the actor's hierarchy to check which focus group the actor belongs to
599   while (actor && !IsFocusGroup(actor))
600   {
601     actor = actor.GetParent();
602   }
603
604   return actor;
605 }
606
607 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
608 {
609   if(mFocusIndicatorActor != indicator)
610   {
611     Actor currentFocusActor = GetCurrentFocusActor();
612     if(currentFocusActor)
613     {
614       // The new focus indicator should be added to the current focused actor immediately
615       if(mFocusIndicatorActor)
616       {
617         currentFocusActor.Remove(mFocusIndicatorActor);
618       }
619
620       if(indicator)
621       {
622         currentFocusActor.Add(indicator);
623       }
624     }
625
626     mFocusIndicatorActor = indicator;
627   }
628 }
629
630 Actor KeyboardFocusManager::GetFocusIndicatorActor()
631 {
632   if( ! mFocusIndicatorActor )
633   {
634     // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
635     mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
636
637     // Apply size constraint to the focus indicator
638     mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
639   }
640
641   mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
642   mFocusIndicatorActor.SetAnchorPoint( AnchorPoint::CENTER );
643   mFocusIndicatorActor.SetPosition(0.0f, 0.0f);
644
645   return mFocusIndicatorActor;
646 }
647
648 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
649 {
650   AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
651   bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
652
653   Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
654
655   std::string keyName = event.keyPressedName;
656
657   if( mIsFocusIndicatorEnabled == -1 )
658   {
659     GetConfigurationFromStyleManger();
660   }
661
662   bool isFocusStartableKey = false;
663
664   if(event.state == KeyEvent::Down)
665   {
666     if (keyName == "Left")
667     {
668       if(!isAccessibilityEnabled)
669       {
670         if(!mIsFocusIndicatorEnabled)
671         {
672           // Show focus indicator
673           mIsFocusIndicatorEnabled = 1;
674         }
675
676         // Move the focus towards left
677         MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
678         isFocusStartableKey = true;
679       }
680       else
681       {
682         // Move the accessibility focus backward
683         accessibilityManager.MoveFocusBackward();
684       }
685     }
686     else if (keyName == "Right")
687     {
688       if(!isAccessibilityEnabled)
689       {
690         if(!mIsFocusIndicatorEnabled)
691         {
692           // Show focus indicator
693           mIsFocusIndicatorEnabled = 1;
694         }
695
696         // Move the focus towards right
697         MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
698       }
699       else
700       {
701         // Move the accessibility focus forward
702         accessibilityManager.MoveFocusForward();
703       }
704
705       isFocusStartableKey = true;
706     }
707     else if (keyName == "Up" && !isAccessibilityEnabled)
708     {
709       if(!mIsFocusIndicatorEnabled)
710       {
711         // Show focus indicator
712         mIsFocusIndicatorEnabled = 1;
713       }
714
715       // Move the focus towards up
716       MoveFocus(Toolkit::Control::KeyboardFocus::UP);
717       isFocusStartableKey = true;
718     }
719     else if (keyName == "Down" && !isAccessibilityEnabled)
720     {
721       if(!mIsFocusIndicatorEnabled)
722       {
723         // Show focus indicator
724         mIsFocusIndicatorEnabled = 1;
725       }
726
727       // Move the focus towards down
728       MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
729       isFocusStartableKey = true;
730     }
731     else if (keyName == "Prior" && !isAccessibilityEnabled)
732     {
733       if(!mIsFocusIndicatorEnabled)
734       {
735         // Show focus indicator
736         mIsFocusIndicatorEnabled = 1;
737       }
738
739       // Move the focus towards the previous page
740       MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
741
742       isFocusStartableKey = true;
743     }
744     else if (keyName == "Next" && !isAccessibilityEnabled)
745     {
746       if(!mIsFocusIndicatorEnabled)
747       {
748         // Show focus indicator
749         mIsFocusIndicatorEnabled = 1;
750       }
751
752       // Move the focus towards the next page
753       MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
754
755       isFocusStartableKey = true;
756     }
757     else if (keyName == "Tab" && !isAccessibilityEnabled)
758     {
759       if(!mIsFocusIndicatorEnabled)
760       {
761         // Show focus indicator
762         mIsFocusIndicatorEnabled = 1;
763       }
764       else
765       {
766         // "Tab" key changes the focus group in the forward direction and
767         // "Shift-Tab" key changes it in the backward direction.
768         DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
769       }
770
771       isFocusStartableKey = true;
772     }
773     else if (keyName == "space" && !isAccessibilityEnabled)
774     {
775       if(!mIsFocusIndicatorEnabled)
776       {
777         // Show focus indicator
778         mIsFocusIndicatorEnabled = 1;
779       }
780
781       isFocusStartableKey = true;
782     }
783     else if (keyName == "" && !isAccessibilityEnabled)
784     {
785       // Check the fake key event for evas-plugin case
786       if(!mIsFocusIndicatorEnabled)
787       {
788         // Show focus indicator
789         mIsFocusIndicatorEnabled = 1;
790       }
791
792       isFocusStartableKey = true;
793     }
794     else if (keyName == "Backspace" && !isAccessibilityEnabled)
795     {
796       // Emit signal to go back to the previous view???
797     }
798     else if (keyName == "Escape" && !isAccessibilityEnabled)
799     {
800     }
801   }
802   else if(event.state == KeyEvent::Up)
803   {
804     if (keyName == "Return")
805     {
806       if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
807       {
808         // Show focus indicator
809         mIsFocusIndicatorEnabled = 1;
810       }
811       else
812       {
813         // The focused actor has enter pressed on it
814         Actor actor;
815         if( !isAccessibilityEnabled )
816         {
817           actor = GetCurrentFocusActor();
818         }
819         else
820         {
821           actor = accessibilityManager.GetCurrentFocusActor();
822         }
823
824         if( actor )
825         {
826           DoKeyboardEnter( actor );
827         }
828       }
829
830       isFocusStartableKey = true;
831     }
832   }
833
834   if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
835   {
836     Actor actor = GetCurrentFocusActor();
837     if( actor )
838     {
839       // Make sure the focused actor is highlighted
840       actor.Add( GetFocusIndicatorActor() );
841     }
842     else
843     {
844       // No actor is focused but keyboard focus is activated by the key press
845       // Let's try to move the initial focus
846       MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
847     }
848   }
849 }
850
851 void KeyboardFocusManager::OnTouch(const TouchData& touch)
852 {
853   // if mIsFocusIndicatorEnabled is -1, it means Configuration is not loaded.
854   // Try to load configuration.
855   if( mIsFocusIndicatorEnabled == -1 )
856   {
857     GetConfigurationFromStyleManger();
858   }
859
860   // Clear the focus when user touch the screen.
861   // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
862   // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
863   if( (( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN )) && mClearFocusOnTouch )
864   {
865     ClearFocus();
866   }
867 }
868
869 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
870 {
871   return mPreFocusChangeSignal;
872 }
873
874 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
875 {
876   return mFocusChangedSignal;
877 }
878
879 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
880 {
881   return mFocusGroupChangedSignal;
882 }
883
884 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
885 {
886   return mFocusedActorEnterKeySignal;
887 }
888
889 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
890 {
891   Dali::BaseHandle handle( object );
892
893   bool connected( true );
894   KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
895
896   if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
897   {
898     manager->PreFocusChangeSignal().Connect( tracker, functor );
899   }
900   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
901   {
902     manager->FocusChangedSignal().Connect( tracker, functor );
903   }
904   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
905   {
906     manager->FocusGroupChangedSignal().Connect( tracker, functor );
907   }
908   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
909   {
910     manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
911   }
912   else
913   {
914     // signalName does not match any signal
915     connected = false;
916   }
917
918   return connected;
919 }
920
921 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
922 {
923   mCustomAlgorithmInterface = &interface;
924 }
925
926 } // namespace Internal
927
928 } // namespace Toolkit
929
930 } // namespace Dali