idlefw/plugins/wsplugin/src/keylockstates.cpp
branchRCL_3
changeset 9 d0529222e3f0
parent 4 1a2a00e78665
child 10 5ef93ea513cb
child 18 bd874ee5e5e2
equal deleted inserted replaced
4:1a2a00e78665 9:d0529222e3f0
     1 /*
       
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Keylock states implementation for Active Idle WS Plug-in
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "keylockstates.h"
       
    20 #include "keylockcontrol.h"
       
    21 #include "keypadsettings.h"
       
    22 #include "panic.h"
       
    23 #include "activeidle2domainpskeys.h"
       
    24 
       
    25 #include <e32event.h>
       
    26 #include <e32property.h>
       
    27 
       
    28 #include "debug.h"
       
    29 
       
    30 
       
    31 namespace AiWsPlugin {
       
    32 
       
    33 const TInt KTenMilliSecondsInu = 10 * 1000;
       
    34 
       
    35 /**
       
    36  * Helper macro for declaring internal state classes for CKeyLockHandler.
       
    37  */
       
    38 #define AI_DECLARE_KEYLOCK_STATE(StateCls) \
       
    39     class StateCls { \
       
    40     public: \
       
    41         static const TKeylockState KState; \
       
    42         static const TKeylockState& DefaultNextState(); \
       
    43         static void FocusChanged( MKeyLockHandlerControl& aControl, TBool aState ); \
       
    44         static TBool OfferRawEvent( MKeyLockHandlerControl& aControl, const TRawEvent& aRawEvent ); \
       
    45         static void TimerElapsed( MKeyLockHandlerControl& aControl ); \
       
    46         }
       
    47 
       
    48 /**
       
    49  * Inactive state class for CKeyLockHandler.
       
    50  */
       
    51 AI_DECLARE_KEYLOCK_STATE(TKeylockStateInactive);
       
    52 
       
    53 /**
       
    54  * Active state class for CKeyLockHandler.
       
    55  */
       
    56 AI_DECLARE_KEYLOCK_STATE(TKeylockStateWaitForFirstKeyDown);
       
    57 
       
    58 /**
       
    59  * Keylock sequence in progress state class for CKeyLockHandler.
       
    60  */
       
    61 AI_DECLARE_KEYLOCK_STATE(TKeylockStateWaitForSecondKeyDown);
       
    62 
       
    63 /**
       
    64  * Keylock sequence finalizer state class for CKeyLockHandler.
       
    65  */
       
    66 AI_DECLARE_KEYLOCK_STATE(TKeylockStateWaitForSecondKeyUp);
       
    67 
       
    68 /**
       
    69  * Helper macro for defining key lock state function tables.
       
    70  */
       
    71 #define AI_DEFINE_KEYLOCK_STATE_FT(StateCls) \
       
    72     const TKeylockState StateCls::KState = {     \
       
    73         &StateCls::DefaultNextState,             \
       
    74         &StateCls::FocusChanged,                 \
       
    75         &StateCls::OfferRawEvent,                \
       
    76         &StateCls::TimerElapsed }
       
    77 
       
    78 /// Defines function table for key lock inactive state
       
    79 AI_DEFINE_KEYLOCK_STATE_FT(TKeylockStateInactive);
       
    80 
       
    81 /// Defines function table for key lock active state
       
    82 AI_DEFINE_KEYLOCK_STATE_FT(TKeylockStateWaitForFirstKeyDown);
       
    83 
       
    84 /// Defines function table for key lock in progress state
       
    85 AI_DEFINE_KEYLOCK_STATE_FT(TKeylockStateWaitForSecondKeyDown);
       
    86 
       
    87 /// Defines function table for key lock finalizing state
       
    88 AI_DEFINE_KEYLOCK_STATE_FT(TKeylockStateWaitForSecondKeyUp);
       
    89 
       
    90 // TKeyLockState
       
    91 const TKeylockState& TKeylockState::StartupState()
       
    92     {
       
    93     return TKeylockStateInactive::KState;
       
    94     }
       
    95 
       
    96 // TKeylockStateInactive
       
    97 const TKeylockState& TKeylockStateInactive::DefaultNextState()
       
    98     {
       
    99     return TKeylockStateWaitForFirstKeyDown::KState;
       
   100     }
       
   101     
       
   102 void TKeylockStateInactive::FocusChanged
       
   103         ( MKeyLockHandlerControl& aControl, TBool aState )
       
   104     {
       
   105     __PRINT( __DBG_FORMAT("AiWsPlugin: TKeylockStateInactive::FocusChanged(%d)"), aState );
       
   106     if ( aState )
       
   107         {
       
   108         // Gained focus, switch to active state
       
   109         aControl.ActivateNextState();    
       
   110         }
       
   111     }
       
   112     
       
   113 TBool TKeylockStateInactive::OfferRawEvent
       
   114         ( MKeyLockHandlerControl& /*aControl*/, const TRawEvent& /*aRawEvent*/ )
       
   115     {
       
   116     return EFalse;
       
   117     }
       
   118     
       
   119 void TKeylockStateInactive::TimerElapsed
       
   120         ( MKeyLockHandlerControl& aControl )
       
   121     {
       
   122     // Illegal event in this state
       
   123     aControl.KeypadLockTimeout();
       
   124 #ifndef NDEBUG
       
   125     //Panic( EPanicInvalidKeylockEvent );
       
   126 #endif    
       
   127     }
       
   128 
       
   129 // TKeylockStateWaitForFirstLockKeyDown
       
   130 const TKeylockState& TKeylockStateWaitForFirstKeyDown::DefaultNextState()
       
   131     {
       
   132     return TKeylockStateWaitForSecondKeyDown::KState;
       
   133     }
       
   134 
       
   135 void TKeylockStateWaitForFirstKeyDown::FocusChanged
       
   136         ( MKeyLockHandlerControl& aControl, TBool aState )
       
   137     {
       
   138     __PRINT( __DBG_FORMAT("AiWsPlugin: TKeylockStateWaitForFirstKeyDown::FocusChanged(%d)"), aState );
       
   139     if ( !aState )
       
   140         {
       
   141         if( !aControl.TimeoutTimerActive() )
       
   142             {
       
   143             // Lost focus, switch to inactive state
       
   144             aControl.SetNextState( TKeylockStateInactive::KState );
       
   145             aControl.ActivateNextState();
       
   146             }
       
   147         }
       
   148     }
       
   149     
       
   150 TBool TKeylockStateWaitForFirstKeyDown::OfferRawEvent
       
   151         ( MKeyLockHandlerControl& aControl, const TRawEvent& aRawEvent )
       
   152     {
       
   153     switch ( aRawEvent.Type() )
       
   154         {
       
   155         case TRawEvent::EKeyDown:
       
   156             {
       
   157             const TInt scanCode = aRawEvent.ScanCode();
       
   158             __PRINT( __DBG_FORMAT("AiWsPlugin: TKeylockStateWaitForFirstKeyDown::OfferRawEvent(EKeyDown,ScanCode=%d)" ), scanCode );
       
   159             if ( aControl.IsFirstLockKey( scanCode ) && aControl.HasFocus() )
       
   160                 {
       
   161                 // First lock key was pressed down
       
   162                 aControl.SetLastLockKeyScanCode( scanCode );
       
   163                 // (Re)activate the keylock timeout timer
       
   164                 aControl.StartTimeoutTimer(KTenMilliSecondsInu);
       
   165                 }
       
   166             }
       
   167         }
       
   168 
       
   169     // Never consume the event to enable its processing if keylock
       
   170     // is not activated within the timeout
       
   171     return EFalse;
       
   172     }
       
   173     
       
   174 void TKeylockStateWaitForFirstKeyDown::TimerElapsed
       
   175         ( MKeyLockHandlerControl& aControl )
       
   176     {
       
   177                 TInt value = EPSAiNotDisplayingMenuOrDialog;          
       
   178                 TInt err = RProperty::Get(
       
   179                    KPSUidAiInformation, 
       
   180                    KActiveIdlePopupState, 
       
   181                    value );
       
   182                 
       
   183                 if( value == EPSAiNotDisplayingMenuOrDialog && err == KErrNone )
       
   184                     {
       
   185                     // (Re)activate the keylock timeout timer
       
   186                     aControl.StartTimeoutTimer();
       
   187                     // Switch to wait for second lock key down (see constructor of this state)
       
   188                     aControl.ActivateNextState();
       
   189                     }
       
   190                 else
       
   191                     {
       
   192                     if( aControl.HasFocus() )
       
   193                         {
       
   194                         aControl.StopTimeoutTimer();
       
   195                         }
       
   196                     else
       
   197                         {
       
   198                         // Lost focus, switch to inactive state
       
   199                         aControl.SetNextState( TKeylockStateInactive::KState );
       
   200                         aControl.ActivateNextState();
       
   201                         }
       
   202                     }
       
   203     // Illegal event in this state
       
   204 #ifndef NDEBUG
       
   205     //Panic( EPanicInvalidKeylockEvent );
       
   206 #endif    
       
   207     }
       
   208 
       
   209 // TKeylockStateWaitForSecondKeyDown
       
   210 const TKeylockState&  TKeylockStateWaitForSecondKeyDown::DefaultNextState()
       
   211     {
       
   212     // Assume the keylock sequence is cancelled
       
   213     return TKeylockStateWaitForFirstKeyDown::KState;
       
   214     }
       
   215 
       
   216 void TKeylockStateWaitForSecondKeyDown::FocusChanged
       
   217         ( MKeyLockHandlerControl& aControl, TBool aState )
       
   218     {
       
   219     __PRINT( __DBG_FORMAT("AiWsPlugin: TKeylockStateWaitForSecondKeyDown::FocusChanged(%d)"), aState );
       
   220     if ( aState )
       
   221         {
       
   222         // Gained focus: return to active state if keylock sequence is cancelled
       
   223         aControl.SetNextState( TKeylockStateWaitForFirstKeyDown::KState );
       
   224         }
       
   225     else
       
   226         {
       
   227         // Lost focus: return to inactive state if keylock sequence is cancelled
       
   228         aControl.SetNextState( TKeylockStateInactive::KState );
       
   229         }
       
   230     }
       
   231     
       
   232 TBool TKeylockStateWaitForSecondKeyDown::OfferRawEvent
       
   233         ( MKeyLockHandlerControl& aControl, const TRawEvent& aRawEvent )
       
   234     {
       
   235     TBool consumedEvent = EFalse;
       
   236     
       
   237     switch ( aRawEvent.Type() )
       
   238         {
       
   239         case TRawEvent::EKeyDown:
       
   240             {
       
   241             const TInt scanCode = aRawEvent.ScanCode();
       
   242             __PRINT( __DBG_FORMAT("AiWsPlugin: TKeylockStateWaitForSecondKeyDown::OfferRawEvent(EKeyDown,ScanCode=%d)" ), scanCode );
       
   243             if ( aControl.IsSecondLockKey( scanCode ) )
       
   244                 {
       
   245                 // Second lock key was pressed down. Cancel keylock timer
       
   246                 // and switch to wait for key up event.
       
   247                 aControl.StopTimeoutTimer();
       
   248                 aControl.SetLastLockKeyScanCode( scanCode );
       
   249                 aControl.SetNextState( TKeylockStateWaitForSecondKeyUp::KState );
       
   250                 aControl.ActivateNextState();
       
   251                 consumedEvent = ETrue;
       
   252                 }
       
   253             else
       
   254                 {
       
   255                 // Some other key than second lock key was pressed while
       
   256                 // keylock timer was running. Cancel the keylock sequence and
       
   257                 // switch to previous state. (Depending on focus status,
       
   258                 // see TKeylockStateWaitForFirstKeyDown::FocusChanged.)
       
   259                 aControl.CancelKeypadLock();
       
   260                 aControl.ActivateNextState();
       
   261                 consumedEvent = EFalse;
       
   262                 }
       
   263             break;
       
   264             }
       
   265         }
       
   266         
       
   267     return consumedEvent;
       
   268     }
       
   269     
       
   270 void TKeylockStateWaitForSecondKeyDown::TimerElapsed
       
   271         ( MKeyLockHandlerControl& aControl )
       
   272     {
       
   273     __PRINTS( "AiWsPlugin: TKeylockStateWaitForSecondKeyDown::TimerElapsed()" ); 
       
   274     aControl.KeypadLockTimeout();
       
   275     aControl.ActivateNextState();
       
   276     }
       
   277 
       
   278 // TKeylockStateWaitForSecondKeyUp
       
   279 const TKeylockState& TKeylockStateWaitForSecondKeyUp::DefaultNextState()
       
   280     {
       
   281     // Idle must currently have focus so return to active state from this state
       
   282     return TKeylockStateWaitForFirstKeyDown::KState;
       
   283     }
       
   284 
       
   285 void TKeylockStateWaitForSecondKeyUp::FocusChanged
       
   286         ( MKeyLockHandlerControl& aControl, TBool aState )
       
   287     {
       
   288     __PRINT( __DBG_FORMAT("AiWsPlugin: TKeylockStateWaitForSecondKeyUp::FocusChanged(%d)"), aState );
       
   289     if ( aState )
       
   290         {
       
   291         // Gained focus: return to active state if keylock sequence is cancelled
       
   292         aControl.SetNextState(
       
   293             TKeylockStateWaitForFirstKeyDown::KState );
       
   294         }
       
   295     else
       
   296         {
       
   297         // Lost focus: return to inactive state if keylock sequence is cancelled
       
   298         aControl.SetNextState( TKeylockStateInactive::KState );
       
   299         }
       
   300     }
       
   301     
       
   302 TBool TKeylockStateWaitForSecondKeyUp::OfferRawEvent
       
   303         ( MKeyLockHandlerControl& aControl, const TRawEvent& aRawEvent )
       
   304     {
       
   305     TBool consumedEvent = EFalse;
       
   306     
       
   307     switch ( aRawEvent.Type() )
       
   308         {
       
   309         case TRawEvent::EKeyUp:
       
   310             {
       
   311             const TInt scanCode = aRawEvent.ScanCode();
       
   312             __PRINT( __DBG_FORMAT("AiWsPlugin: TKeylockStateWaitForSecondKeyUp::OfferRawEvent(EKeyUp,ScanCode=%d)" ), scanCode );
       
   313             if ( scanCode == aControl.LastLockKeyScanCode() )
       
   314                 {
       
   315                 // Second lock key was released. Activate the keypad lock and
       
   316                 // switch back to initial state. (Depending on focus status,
       
   317                 // see TKeylockStateWaitForSecondKeyUp::FocusChanged.)
       
   318                 aControl.ActivateKeypadLock();
       
   319                 aControl.ActivateNextState();
       
   320                 }
       
   321             // Consume all key events until the keylock sequence is finalized
       
   322             consumedEvent = ETrue;
       
   323             break;
       
   324             }
       
   325 
       
   326         case TRawEvent::EKeyDown:
       
   327             {
       
   328             // Consume all key events until the keylock sequence is finalized
       
   329             consumedEvent = ETrue;
       
   330             break;
       
   331             }
       
   332         }
       
   333         
       
   334     return consumedEvent;
       
   335     }
       
   336     
       
   337 void TKeylockStateWaitForSecondKeyUp::TimerElapsed
       
   338         ( MKeyLockHandlerControl& /*aControl*/ )
       
   339     {
       
   340     // Illegal event in this state
       
   341 #ifndef NDEBUG
       
   342     //Panic( EPanicInvalidKeylockEvent );
       
   343 #endif    
       
   344     }
       
   345 
       
   346 } // namespace AiWsPlugin