voiceui/voiceuivoicerecognition/src/vuicverificationstate.cpp
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <avkon.hrh>
       
    21 #include <StringLoader.h>
       
    22 
       
    23 #include <vuivoicerecognition.rsg>
       
    24 
       
    25 #include "vuivoicerecognition.hrh"
       
    26 
       
    27 #include "vuicstate.h"
       
    28 #include "vuicverificationstate.h"
       
    29 #include "vuicverificationinitstate.h"
       
    30 #include "vuicplaystate.h"
       
    31 #include "vuicabortstate.h"
       
    32 #include "vuicerrorstate.h"
       
    33 #include "vuicexitstate.h"
       
    34 
       
    35 #include "vuicdatastorage.h"
       
    36 
       
    37 #include "vuicvoicerecogdialogimpl.h"
       
    38 #include "vuicverificationdialog.h"
       
    39 #include "vuicttsplayer.h"
       
    40 #include "vuictoneplayer.h"
       
    41 
       
    42 #include "rubydebug.h"
       
    43     
       
    44 // -----------------------------------------------------------------------------
       
    45 // CVerificationState::NewL
       
    46 // Two-phased constructor.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CVerificationState* CVerificationState::NewL( CDataStorage& aDataStorage, CUiModel& aUiModel )
       
    50     {
       
    51     CVerificationState* self = new (ELeave) CVerificationState( aDataStorage, aUiModel );
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL();
       
    54     CleanupStack::Pop( self );
       
    55     return self;
       
    56     }       
       
    57     
       
    58 // Destructor       
       
    59 CVerificationState::~CVerificationState()
       
    60     {
       
    61     RUBY_DEBUG0( "CVerificationState::~CVerificationState START" );
       
    62     
       
    63     if ( DataStorage().TtsPlayer() )
       
    64         {
       
    65         DataStorage().TtsPlayer()->Stop();
       
    66         }
       
    67     
       
    68     // If recording has not ended we should cancel recording
       
    69     if ( iInternalState != ECompleted &&
       
    70          DataStorage().VerificationRecognitionHandler() )
       
    71         {
       
    72         DataStorage().VerificationRecognitionHandler()->Cancel();
       
    73         }
       
    74         
       
    75     delete iVerificationDialog;
       
    76     
       
    77     RUBY_DEBUG0( "CVerificationState::~CVerificationState EXIT" );
       
    78     }
       
    79     
       
    80 // ---------------------------------------------------------
       
    81 // CVerificationState::HandleEventL
       
    82 // ---------------------------------------------------------
       
    83 //       
       
    84 void CVerificationState::HandleEventL( TInt aEvent )
       
    85     {
       
    86     RUBY_DEBUG_BLOCK( "CVerificationState::HandleEventL" );
       
    87 
       
    88     CState* nextState = NULL;
       
    89 
       
    90     switch( aEvent )
       
    91         {
       
    92         case KErrNone:
       
    93         
       
    94             if ( iInternalState == EStarted )
       
    95                 {
       
    96                 iInternalState = ETtsInitialized;
       
    97                 }
       
    98             else if ( iInternalState == ETtsInitialized )
       
    99                 {
       
   100                 iInternalState = ETtsCompleted;
       
   101                 nextState = this;
       
   102                 }
       
   103             else if ( iInternalState == ETtsCompleted )
       
   104                 {
       
   105                 iInternalState = EStartCompleted;
       
   106                 nextState = this;
       
   107                 }
       
   108             else if ( iInternalState == EStartCompleted )
       
   109                 {
       
   110                 iInternalState = ERecordStarted;
       
   111                 }
       
   112             else if ( iInternalState == ERecordStarted )
       
   113                 {
       
   114                 iInternalState = ERecordEnded;
       
   115                 }
       
   116             else if ( iInternalState == ERecordEnded )
       
   117                 {
       
   118                 iInternalState = ECompleted;
       
   119                 nextState = this;
       
   120                 }
       
   121             break;
       
   122         
       
   123         case EVoiceTagSoftKeySelect:
       
   124             
       
   125             nextState = CPlayState::NewL( DataStorage(), UiModel() );
       
   126             break;
       
   127             
       
   128         case KErrNoMatch:
       
   129         case KErrRecogFailed:
       
   130         case KErrNoSpeech:
       
   131         case KErrTooEarly:
       
   132         case KErrTooLong:
       
   133         case KErrTooShort:
       
   134         
       
   135             DataStorage().SetVerificationTryCount( DataStorage().VerificationTryCount() - 1 );
       
   136             nextState = CVerificationInitState::NewL( DataStorage(), UiModel() );
       
   137             break;
       
   138         
       
   139         case EVoiceTagSoftKeyOther:
       
   140             
       
   141             nextState = CVerificationInitState::NewL( DataStorage(), UiModel() );
       
   142             break;
       
   143         
       
   144         case EVoiceTagSoftKeyCancel:
       
   145         case EEndCallKeypress:
       
   146         case ELongKeypress:
       
   147         case KErrNoResults:
       
   148 
       
   149             nextState = CAbortState::NewL( DataStorage(), UiModel() );
       
   150             break;
       
   151         
       
   152         case EShortKeypress:
       
   153         case ESelectKeypress:
       
   154         
       
   155             iKeypress = aEvent;
       
   156             nextState = this;
       
   157             break;
       
   158             
       
   159         case EUpKeypress:
       
   160         case EDownKeypress:
       
   161         
       
   162             // Do nothing
       
   163             break;
       
   164             
       
   165         default:
       
   166             
       
   167             nextState = CErrorState::NewL( DataStorage(), UiModel(), aEvent );
       
   168             break;
       
   169         }
       
   170 
       
   171     DataStorage().VoiceRecognitionImpl()->ChangeState( nextState );
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------
       
   175 // CVerificationState::ExecuteL
       
   176 // ---------------------------------------------------------
       
   177 //       
       
   178 void CVerificationState::ExecuteL()
       
   179     {
       
   180     RUBY_DEBUG_BLOCK( "CVerificationState::ExecuteL" );
       
   181 
       
   182     if ( HandleKeypressL() )
       
   183         {
       
   184         iKeypress = ENoKeypress;
       
   185         }
       
   186     else if ( iInternalState == ETtsCompleted )
       
   187         {
       
   188         DataStorage().TtsPlayer()->Stop();
       
   189         
       
   190         // Play the starting tone
       
   191         DataStorage().TonePlayer()->PlayTone( EAvkonSIDNameDiallerStartTone ); 
       
   192         }
       
   193     else if ( iInternalState == EStartCompleted )
       
   194         {
       
   195         MNssRecognitionHandler* handler = DataStorage().VerificationRecognitionHandler();
       
   196         User::LeaveIfError( handler->Recognize( DataStorage().VoiceRecognitionImpl() ) );
       
   197         }
       
   198     else if ( iInternalState == ECompleted )
       
   199         {
       
   200         SelectTag();
       
   201         
       
   202         DataStorage().SetVerificationTryCount( KVerificationTries );
       
   203         
       
   204         HandleEventL( SelectCommandById() );
       
   205         }
       
   206     else
       
   207         {
       
   208         TInt returnValue = KErrNoResults;
       
   209         
       
   210         if ( DataStorage().CompleteTagList()->Count() != KErrNone &&
       
   211              DataStorage().VerificationTryCount() > KErrNone )
       
   212             {
       
   213             iInternalState = EStarted;
       
   214 
       
   215             MNssTag* selectedTag = NULL;
       
   216             for ( int i = 0; i < DataStorage().CompleteTagList()->Count(); ++i )
       
   217                 {
       
   218                 MNssTag* tag = DataStorage().CompleteTagList()->At( i );
       
   219                 if ( !DataStorage().DeviceLockMode() ||
       
   220                      ( tag->Context()->ContextName() == KVoiceDialContext &&
       
   221                      tag->RRD()->IntArray()->At( KVasExtensionRrdLocation ) == EDial ) )
       
   222                     {
       
   223                     iTagIndex = i;
       
   224                     selectedTag = tag;
       
   225                     break;
       
   226                     }
       
   227                 }
       
   228                      
       
   229             if ( selectedTag )
       
   230                 {
       
   231                 HBufC* header = selectedTag->SpeechItem()->Text().AllocLC();
       
   232                                 
       
   233                 iVerificationDialog = CVerificationDialog::NewL();
       
   234                 iVerificationDialog->CreateVerificationPopupLC( *header );
       
   235                 
       
   236                 PlayPromptL();
       
   237                 BringToForeground();
       
   238     
       
   239                 iVerificationDialog->RegisterForKeyCallback( DataStorage().VoiceRecognitionImpl() );            
       
   240                 returnValue = iVerificationDialog->ShowVerificationPopupL(); // Pops things pushed in CreateVerificationPopupLC
       
   241                 RUBY_DEBUG1( "CVerificationState::ExecuteL - Verification dialog returns [%d]", returnValue );
       
   242                 
       
   243                 CleanupStack::PopAndDestroy( header );
       
   244                 }
       
   245             }
       
   246         
       
   247         if ( returnValue != KErrNone )
       
   248             {           
       
   249             SelectTag();
       
   250             
       
   251             DataStorage().SetVerificationTryCount( KVerificationTries );
       
   252             
       
   253             if ( returnValue == EVoiceTagSoftKeySelect )
       
   254                 {
       
   255                 HandleEventL( SelectCommandById() );
       
   256                 }
       
   257             else
       
   258                 {
       
   259                 HandleEventL( returnValue );
       
   260                 }            
       
   261             }
       
   262         }
       
   263     }
       
   264 
       
   265 // ---------------------------------------------------------
       
   266 // CVerificationState::CVerificationState
       
   267 // ---------------------------------------------------------
       
   268 //              
       
   269 CVerificationState::CVerificationState( CDataStorage& aDataStorage, CUiModel& aUiModel )
       
   270  : CState( aDataStorage, aUiModel ), iKeypress( ENoKeypress ),
       
   271    iInternalState( ENotStarted )
       
   272     {
       
   273     }
       
   274 
       
   275 // ---------------------------------------------------------
       
   276 // CVerificationState::ConstructL
       
   277 // ---------------------------------------------------------
       
   278 //           
       
   279 void CVerificationState::ConstructL()
       
   280     {
       
   281     RUBY_DEBUG_BLOCK( "CVerificationState::ConstructL" );
       
   282     
       
   283     CState::ConstructL();
       
   284     
       
   285     DataStorage().SetAdaptationEnabled( ETrue );
       
   286     }
       
   287     
       
   288 // ---------------------------------------------------------
       
   289 // CVerificationState::HandleKeypressL
       
   290 // ---------------------------------------------------------
       
   291 //           
       
   292 TBool CVerificationState::HandleKeypressL()
       
   293     {
       
   294     RUBY_DEBUG_BLOCK( "CVerificationState::HandleKeypressL" );
       
   295     
       
   296     TBool handled = ETrue;
       
   297     
       
   298     if ( iKeypress == EShortKeypress )
       
   299         {
       
   300         SelectTag();
       
   301 
       
   302         HandleEventL( EVoiceTagSoftKeySelect );
       
   303         }
       
   304     else if ( iKeypress == ESelectKeypress )
       
   305         {        
       
   306         SelectTag();
       
   307         
       
   308         DataStorage().SetVerificationTryCount( KVerificationTries );
       
   309         
       
   310         HandleEventL( SelectCommandById() );
       
   311         }
       
   312     else
       
   313         {
       
   314         handled = EFalse;
       
   315         }
       
   316     
       
   317     return handled;
       
   318     }
       
   319     
       
   320 // ---------------------------------------------------------------------------
       
   321 // CVerificationState::PlayPromptL
       
   322 // ---------------------------------------------------------------------------
       
   323 //
       
   324 void CVerificationState::PlayPromptL()
       
   325     {
       
   326     RUBY_DEBUG_BLOCK( "CVerificationState::PlayPromptL" );
       
   327 
       
   328     // if TTS is used
       
   329     if ( DataStorage().SynthesizerMode() != KErrNone ||
       
   330          DataStorage().DeviceLockMode() )
       
   331         {
       
   332         HBufC* text = NULL;
       
   333                                   
       
   334         if ( DataStorage().CompleteTagList()->At( iTagIndex )->Context()->ContextName() == KVoiceDialContext )
       
   335             {
       
   336             text = DataStorage().CompleteTagList()->At( iTagIndex )->SpeechItem()->PartialTextL( KNameTrainingIndex );
       
   337             }
       
   338         else
       
   339             {
       
   340             text = HBufC::NewL( 0 );
       
   341             }
       
   342 
       
   343         CleanupStack::PushL( text );
       
   344         
       
   345         HBufC* select = StringLoader::LoadLC( R_QAN_VC_VERIFICATION_SELECT );
       
   346         HBufC* other = StringLoader::LoadLC( R_QAN_VC_VERIFICATION_OTHER );
       
   347         HBufC* cancel = StringLoader::LoadLC( R_QAN_VC_VERIFICATION_CANCEL );
       
   348                 
       
   349         DataStorage().TtsPlayer()->PlayL(
       
   350             DataStorage().CompleteTagList()->At( iTagIndex )->SpeechItem()->Text(), 0,
       
   351             DataStorage().CompleteTagList()->At( iTagIndex )->SpeechItem()->Text().Length(),
       
   352             *text, *select, *other, *cancel );
       
   353 
       
   354         CleanupStack::PopAndDestroy( cancel );
       
   355         CleanupStack::PopAndDestroy( other );
       
   356         CleanupStack::PopAndDestroy( select );
       
   357         CleanupStack::PopAndDestroy( text );
       
   358         }
       
   359     }
       
   360     
       
   361 // ---------------------------------------------------------
       
   362 // CVerificationState::SelectTag
       
   363 // ---------------------------------------------------------
       
   364 //   
       
   365 void CVerificationState::SelectTag()
       
   366     {
       
   367     RUBY_DEBUG0( "CVerificationState::SelectTag START" );
       
   368     
       
   369     if ( DataStorage().CompleteTagList()->Count() > KErrNone )
       
   370         {
       
   371         DataStorage().SetTag( DataStorage().CompleteTagList()->At( iTagIndex ), ETrue );
       
   372     
       
   373         DataStorage().CompleteTagList()->Delete( iTagIndex );
       
   374         }
       
   375         
       
   376     RUBY_DEBUG0( "CVerificationState::SelectTag EXIT" );
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------
       
   380 // CVerificationState::SelectTag
       
   381 // ---------------------------------------------------------
       
   382 //       
       
   383 TInt CVerificationState::SelectCommandById()
       
   384     {
       
   385     RUBY_DEBUG0( "CVerificationState::SelectCommandById START" );
       
   386     
       
   387     TInt index = KErrGeneral;
       
   388     
       
   389     if ( DataStorage().AdditionalTagList() &&
       
   390          DataStorage().AdditionalTagList()->Count() > KErrNone )
       
   391         {
       
   392         index = DataStorage().AdditionalTagList()->At( 0 )->RRD()->IntArray()->At( 0 );
       
   393         DataStorage().SetAdditionalTagList( NULL );
       
   394         }
       
   395         
       
   396     RUBY_DEBUG0( "CVerificationState::SelectCommandById EXIT" );
       
   397     
       
   398     return iVerificationDialog->SelectedCommand( index );
       
   399     }
       
   400     
       
   401 // End of File
       
   402