securitydialogs/lockapp/src/lockappkeypattern.cpp
branchRCL_3
changeset 21 09b1ac925e3f
parent 20 63339781d179
child 22 03674e5abf46
equal deleted inserted replaced
20:63339781d179 21:09b1ac925e3f
     1 /*
       
     2 * Copyright (c) 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:  Key pattern matching component.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "lockapptrace.h"
       
    20 #include "lockappkeypattern.h"
       
    21 
       
    22 const TInt KDefaultTimeout = 2000000;
       
    23 
       
    24 // ---------------------------------------------------------------------------
       
    25 // Standard two-phased construction
       
    26 // ---------------------------------------------------------------------------
       
    27 CLockAppKeyPattern* CLockAppKeyPattern::NewL( )
       
    28     {
       
    29     CLockAppKeyPattern *self = new ( ELeave ) CLockAppKeyPattern();
       
    30     CleanupStack::PushL( self );
       
    31     self->ConstructL( );
       
    32     CleanupStack::Pop( self );
       
    33     return self;
       
    34     }
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // Default C++ constructor
       
    38 // ---------------------------------------------------------------------------
       
    39 CLockAppKeyPattern::CLockAppKeyPattern( ) :
       
    40     iKeyTimeOut( KDefaultTimeout ),
       
    41     iState( EPatternNoMatch ),
       
    42     iEnabled ( ETrue ),
       
    43     iHasPatterns( EFalse )
       
    44     {
       
    45     // no implementation required
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Key pattern construction
       
    50 // ---------------------------------------------------------------------------
       
    51 void CLockAppKeyPattern::ConstructL( )
       
    52     {
       
    53     iKeyTimer = CPeriodic::NewL( CActive::EPriorityUserInput );
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // Destructor
       
    58 // ---------------------------------------------------------------------------
       
    59 CLockAppKeyPattern::~CLockAppKeyPattern( )
       
    60     {
       
    61     ClearPatterns( );
       
    62     // timer between key presses is cancelled
       
    63     if ( iKeyTimer )
       
    64         {
       
    65         iKeyTimer->Cancel( );
       
    66         delete iKeyTimer;
       
    67         }
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // Gets the enabled state.
       
    72 // ---------------------------------------------------------------------------
       
    73 TBool CLockAppKeyPattern::IsEnabled( )
       
    74     {
       
    75     return iEnabled;
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // Sets the enabled state.
       
    80 // ---------------------------------------------------------------------------
       
    81 void CLockAppKeyPattern::SetEnabled(TBool aEnabled )
       
    82     {
       
    83     iEnabled = aEnabled;
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // Gets the primary key timeout.
       
    88 // ---------------------------------------------------------------------------
       
    89 TUint CLockAppKeyPattern::GetKeyTimeOut( )
       
    90     {
       
    91     return iKeyTimeOut;
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // Sets the primary key timeout.
       
    96 // ---------------------------------------------------------------------------
       
    97 void CLockAppKeyPattern::SetKeyTimeOut(TUint aTimeOut )
       
    98     {
       
    99     iKeyTimeOut = aTimeOut;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Add new key combination
       
   104 // ---------------------------------------------------------------------------
       
   105 TInt CLockAppKeyPattern::AddPattern( TUint32 aPrimaryKey, TUint32 aSecondaryKey )
       
   106     {
       
   107     TInt err(KErrNone);
       
   108     iPrimaryKeys.Append( aPrimaryKey );
       
   109     iSecondaryKeys.Append( aSecondaryKey );
       
   110     iHasPatterns = ETrue;
       
   111     return err;
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // Get key combination
       
   116 // ---------------------------------------------------------------------------
       
   117 TInt CLockAppKeyPattern::GetPattern(TInt aIndex, TUint32& aPrimaryKey, TUint32& aSecondaryKey )
       
   118     {
       
   119     TInt err( KErrNone);
       
   120     if ( iHasPatterns && aIndex < iPrimaryKeys.Count( )&& aIndex < iSecondaryKeys.Count( ) )
       
   121         {
       
   122         aPrimaryKey = iPrimaryKeys[aIndex];
       
   123         aSecondaryKey = iSecondaryKeys[aIndex];
       
   124         }
       
   125     else
       
   126         {
       
   127         err = KErrNotFound;
       
   128         }
       
   129     return err;
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // Returns true if the key pattern matcher has been initialized
       
   134 // ---------------------------------------------------------------------------
       
   135 TBool CLockAppKeyPattern::HasPatterns( )
       
   136     {
       
   137     return (iHasPatterns && iPrimaryKeys.Count() > 0 && iSecondaryKeys.Count() > 0);
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // Clear all key combinations
       
   142 // ---------------------------------------------------------------------------
       
   143 TInt CLockAppKeyPattern::ClearPatterns( )
       
   144     {
       
   145     TInt err( KErrNone);
       
   146     if ( iHasPatterns )
       
   147         {
       
   148         iPrimaryKeys.Reset( );
       
   149         iSecondaryKeys.Reset( );
       
   150         iWaitingKeys.Reset( );
       
   151         iHasPatterns = EFalse;
       
   152         // cancel the timer
       
   153         iKeyTimer->Cancel( );
       
   154         }
       
   155     return err;
       
   156     }
       
   157 
       
   158 // ---------------------------------------------------------------------------
       
   159 // Receives keys and checks whether they match any primary+secondary key combination
       
   160 // ---------------------------------------------------------------------------
       
   161 TPatternState CLockAppKeyPattern::HandleKeyEvent( TUint32 aKey )
       
   162     {  
       
   163     if ( iEnabled )
       
   164         {
       
   165         if ( iHasPatterns )
       
   166             {
       
   167             switch (iState)
       
   168                 {
       
   169                 case EPatternNoMatch:
       
   170                 case EPatternSecondaryMatch:
       
   171                     {
       
   172                     if ( iPrimaryKeys.Find( aKey ) != KErrNotFound )
       
   173                         {
       
   174                         HandlePrimaryKeyEvent( aKey );
       
   175                         }
       
   176                     else
       
   177                         {
       
   178                         HandleOtherKeyEvent();
       
   179                         }
       
   180                     }
       
   181                     break;
       
   182                 case EPatternPrimaryMatch:
       
   183                     {
       
   184                     if ( iWaitingKeys.Find( aKey ) != KErrNotFound )
       
   185                         {
       
   186                         HandleSecondaryKeyEvent();
       
   187                         }
       
   188                     else
       
   189                         {
       
   190                         if ( iPrimaryKeys.Find( aKey ) != KErrNotFound )
       
   191                             {
       
   192                             HandlePrimaryKeyEvent( aKey );
       
   193                             }
       
   194                         else
       
   195                             {
       
   196                             HandleOtherKeyEvent();
       
   197                             }
       
   198                         }
       
   199                     }
       
   200                     break;
       
   201                 default:
       
   202                     break;
       
   203                 }
       
   204             }
       
   205         else
       
   206             {
       
   207             return EPatternNotInitialized;
       
   208             }
       
   209         }
       
   210     else
       
   211         {
       
   212         return EPatternNotEnabled;
       
   213         }
       
   214 
       
   215     return iState;
       
   216     }
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // Handles primary key events
       
   220 // ---------------------------------------------------------------------------
       
   221 void CLockAppKeyPattern::HandlePrimaryKeyEvent( TUint32 aKey )
       
   222     {
       
   223     // cancel the timer
       
   224     iKeyTimer->Cancel( );
       
   225     // clear the waiting keys
       
   226     iWaitingKeys.Reset( );
       
   227     // add those secondary keys to the waiting keys, which have the same primary key
       
   228     for (TInt i( 0); i < iPrimaryKeys.Count( ); i++ )
       
   229         {
       
   230         if ( iPrimaryKeys[i] == aKey && i < iSecondaryKeys.Count( ) )
       
   231             {
       
   232             iWaitingKeys.Append( iSecondaryKeys[i] );
       
   233             }
       
   234         }
       
   235 
       
   236     // start timer for primary key timeout
       
   237     iKeyTimer->Start( iKeyTimeOut, iKeyTimeOut, TCallBack( HandleKeyTimerTimeout, this ) );
       
   238 
       
   239     // set the state
       
   240     iState = EPatternPrimaryMatch;
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // Handles secondary key events
       
   245 // ---------------------------------------------------------------------------
       
   246 void CLockAppKeyPattern::HandleSecondaryKeyEvent( )
       
   247     {
       
   248     // cancel the timer
       
   249     iKeyTimer->Cancel( );
       
   250     // clear the waiting keys
       
   251     iWaitingKeys.Reset( );
       
   252     // set the state
       
   253     iState = EPatternSecondaryMatch;
       
   254     }
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // Handles other key events
       
   258 // ---------------------------------------------------------------------------
       
   259 void CLockAppKeyPattern::HandleOtherKeyEvent( )
       
   260     {
       
   261     // cancel the timer
       
   262     iKeyTimer->Cancel( );
       
   263     // clear the waiting keys
       
   264     iWaitingKeys.Reset( );
       
   265     // set the state
       
   266     iState = EPatternNoMatch;
       
   267     }
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // A call back to the key pattern timer, the allowed time window for pressing
       
   271 // the secondary key to get a match has passed.
       
   272 // ---------------------------------------------------------------------------
       
   273 TInt CLockAppKeyPattern::HandleKeyTimerTimeout( TAny* aSelf )
       
   274     {
       
   275     CLockAppKeyPattern* self = reinterpret_cast< CLockAppKeyPattern* >( aSelf );
       
   276     // reset the state
       
   277     self->HandleOtherKeyEvent( );
       
   278     return KErrNone;
       
   279     }
       
   280 
       
   281 // End of file