testconnuis/htiui/HtiServicePlugins/HtiMessagesServicePlugin/src/HtiNotificationHandler.cpp
changeset 2 453d490c84a5
equal deleted inserted replaced
1:753e33780645 2:453d490c84a5
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Implementation of creating notifications.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "HtiMessagesServicePlugin.h"
       
    21 #include "HtiNotificationHandler.h"
       
    22 
       
    23 #include <HtiDispatcherInterface.h>
       
    24 #include <HtiLogging.h>
       
    25 
       
    26 #include <aknSDData.h>          //CAknSDData
       
    27 #include <avkon.rsg>            //R_AVKON_SOFTKEYS_LISTEN_EXIT
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 
       
    31 // EXTERNAL FUNCTION PROTOTYPES
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 
       
    36 // MACROS
       
    37 #define KSpeedDialViewId TVwsViewId( TUid::Uid( 0x1000590A ), TUid::Uid( 0x02 ) )
       
    38 #define R_NCN_VOICE_MAIL_NOTE                     0x283d012
       
    39 #define R_NCN_VOICE_MAILS_UNK_AMOUNT              0x283d01a
       
    40 #define R_NCN_VOICE_MAIL_GROUPED                  0x283d017
       
    41 #define R_NCN_VOICE_MAILS_UNK_AMOUNT_GROUPED      0x283d01b
       
    42 
       
    43 // LOCAL CONSTANTS AND MACROS
       
    44 
       
    45 _LIT8( KErrorInvalidParameters, "Invalid command parameters" );
       
    46 _LIT8( KErrorCreateVoiceMessageNotificationFailed, "Creating voice message notification failed" );
       
    47 _LIT8( KErrorClearNotificationFailed, "Clearing notification failed" );
       
    48 _LIT8( KErrorUnrecognizedCommand, "Unrecognized command" );
       
    49 
       
    50 // MODULE DATA STRUCTURES
       
    51 
       
    52 // LOCAL FUNCTION PROTOTYPES
       
    53 
       
    54 // FORWARD DECLARATIONS
       
    55 
       
    56 // ============================ MEMBER FUNCTIONS ===============================
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CHtiNotificationHandler::NewL
       
    60 // Two-phased constructor.
       
    61 // -----------------------------------------------------------------------------
       
    62 CHtiNotificationHandler* CHtiNotificationHandler::NewL()
       
    63     {
       
    64     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::NewL" );
       
    65     CHtiNotificationHandler* self = new (ELeave) CHtiNotificationHandler();
       
    66     CleanupStack::PushL ( self );
       
    67     self->ConstructL();
       
    68     CleanupStack::Pop();
       
    69     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::NewL" );
       
    70     return self;
       
    71     }
       
    72 
       
    73 
       
    74 // ----------------------------------------------------------------------------
       
    75 // CHtiNotificationHandler::CHtiNotificationHandler
       
    76 // C++ default constructor can NOT contain any code, that
       
    77 // might leave.
       
    78 // ----------------------------------------------------------------------------
       
    79 CHtiNotificationHandler::CHtiNotificationHandler()
       
    80     : CActive( CActive::EPriorityStandard ),
       
    81       iMsgWaiting(),
       
    82       iMsgWaitingPckg( iMsgWaiting ),
       
    83       iVoiceMailsInLine1(0)
       
    84     {
       
    85     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::CHtiNotificationHandler" );
       
    86     CActiveScheduler::Add( this );
       
    87     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::CHtiNotificationHandler" );
       
    88     }
       
    89 
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CHtiNotificationHandler::~CHtiNotificationHandler
       
    93 // Destructor.
       
    94 // -----------------------------------------------------------------------------
       
    95 CHtiNotificationHandler::~CHtiNotificationHandler()
       
    96     {
       
    97     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::~CHtiNotificationHandler" );
       
    98     Cancel();
       
    99     iMobilePhone.Close();
       
   100     iTelServer.Close();
       
   101     iReqQueue.Close();
       
   102     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::~CHtiNotificationHandler" );
       
   103     }
       
   104 
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CHtiNotificationHandler::ConstructL
       
   108 // Symbian 2nd phase constructor can leave.
       
   109 // -----------------------------------------------------------------------------
       
   110 void CHtiNotificationHandler::ConstructL()
       
   111     {
       
   112     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::ConstructL" );
       
   113     
       
   114     TInt err;       
       
   115     
       
   116     err = iTelServer.Connect();
       
   117     HTI_LOG_FORMAT("RTelServer::Connect() returns %d",err);
       
   118     User::LeaveIfError( err );
       
   119 
       
   120     err = iTelServer.GetPhoneInfo( 0, iPhoneInfo );
       
   121     HTI_LOG_FORMAT("RTelServer::GetPhoneInfo() returns %d",err);
       
   122     User::LeaveIfError( err ); 
       
   123         
       
   124     err = iMobilePhone.Open( iTelServer, iPhoneInfo.iName ); 
       
   125     HTI_LOG_FORMAT("RTelServer::Open() returns %d",err);
       
   126     User::LeaveIfError( err );
       
   127 
       
   128     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::ConstructL" );
       
   129     }
       
   130 
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CHtiNotificationHandler::SetDispatcher
       
   134 // Sets the dispatcher pointer.
       
   135 // -----------------------------------------------------------------------------
       
   136 
       
   137 void CHtiNotificationHandler::SetDispatcher( MHtiDispatcher* aDispatcher )
       
   138     {
       
   139     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::SetDispatcher" );
       
   140     iDispatcher = aDispatcher;
       
   141     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::SetDispatcher" );
       
   142     }
       
   143 
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CHtiNotificationHandler::ProcessMessageL
       
   147 // Parses the received message and calls handler functions.
       
   148 // -----------------------------------------------------------------------------
       
   149 void CHtiNotificationHandler::ProcessMessageL( const TDesC8& aMessage,
       
   150                                               THtiMessagePriority /*aPriority*/ )
       
   151     {
       
   152     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::ProcessMessageL" );
       
   153 
       
   154     // Zero length message and command code validity already checked
       
   155     // in HtiMessagesServicePlugin.
       
   156 
       
   157     if ( aMessage[0] == CHtiMessagesServicePlugin::ECreateVoiceMessageNotification )
       
   158         {
       
   159         TRAPD(err, HandleCreateVoiceMessageNotificationL(aMessage));
       
   160         if ( err != KErrNone )
       
   161             {
       
   162             SendErrorMessageL( err, KErrorCreateVoiceMessageNotificationFailed );
       
   163             }
       
   164         }
       
   165 
       
   166     else if ( aMessage[0] == CHtiMessagesServicePlugin::EClearAllNotifications)
       
   167         {
       
   168         TRAPD(err, HandleClearAllNotificationsL(aMessage));
       
   169         if( err!= KErrNone )
       
   170             {
       
   171             SendErrorMessageL( err, KErrorClearNotificationFailed );
       
   172             }
       
   173         }
       
   174     else
       
   175         {
       
   176         SendErrorMessageL( KErrArgument, KErrorUnrecognizedCommand );
       
   177         }
       
   178 
       
   179     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::ProcessMessageL" );
       
   180     }
       
   181 
       
   182 
       
   183 void CHtiNotificationHandler::RunL()
       
   184     {
       
   185     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::RunL" );
       
   186     if(iReqQueue.Count())
       
   187         {
       
   188         TNcnReqQueueElement elem = iReqQueue[0];
       
   189         iReqQueue.Remove(0);
       
   190         if(elem.iCmd == ENcnSetIndicator)
       
   191             {
       
   192             SetIndicator(elem.iIndicator, elem.iMsgCount, elem.iEnable);
       
   193             }
       
   194         else if(elem.iCmd == ENcnResetIndicator)
       
   195             {
       
   196             ResetIndicator();
       
   197             }
       
   198         }
       
   199     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::RunL" );
       
   200     }
       
   201 
       
   202 void CHtiNotificationHandler::DoCancel()
       
   203     {
       
   204     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::DoCancel" );
       
   205     if( IsActive() )
       
   206         {
       
   207         iMobilePhone.CancelAsyncRequest( EMobilePhoneSetIccMessageWaitingIndicators );
       
   208         }
       
   209     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::DoCancel" );
       
   210     }
       
   211 
       
   212 TInt CHtiNotificationHandler::RunError(TInt aError)
       
   213     {
       
   214     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::RunError" );
       
   215     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::RunError" );
       
   216     return aError;
       
   217     }
       
   218 
       
   219 CAknSoftNotificationParameters* CHtiNotificationHandler::CreateNotificationParametersLC(TBool aAmountKnown)
       
   220     {
       
   221         HTI_LOG_FUNC_IN( "CHtiNotificationHandler::CreateNotificationParametersLC" );
       
   222         const TUid KNcnDefaultMessageUid = { 1 };
       
   223         _LIT( KNcnResourceFile, "z:\\resource\\Ncnlist.rSC" );
       
   224         const TInt KNcnNotificationPriority = 2200;
       
   225 
       
   226         TInt noteId = aAmountKnown ? R_NCN_VOICE_MAIL_NOTE : R_NCN_VOICE_MAILS_UNK_AMOUNT;
       
   227         TInt groupId = aAmountKnown ? R_NCN_VOICE_MAIL_GROUPED : R_NCN_VOICE_MAILS_UNK_AMOUNT_GROUPED;
       
   228         TUid customMessageId = KNcnDefaultMessageUid; // default
       
   229         _LIT8( KNcnExternalLaunch, "outside" );
       
   230         CAknSoftNotificationParameters* ret = CAknSoftNotificationParameters::NewL(
       
   231                 KNcnResourceFile,
       
   232                 noteId,
       
   233                 KNcnNotificationPriority,
       
   234                 R_AVKON_SOFTKEYS_LISTEN_EXIT,
       
   235                 CAknNoteDialog::ENoTone,
       
   236                 KSpeedDialViewId,
       
   237                 customMessageId,
       
   238                 EAknSoftkeyListen,
       
   239                 KNcnExternalLaunch );
       
   240         ret->SetGroupedTexts( groupId );
       
   241         CleanupStack::PushL( ret );
       
   242         HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::CreateNotificationParametersLC" );
       
   243         return ret;
       
   244     } 
       
   245 
       
   246 void CHtiNotificationHandler::ShowNewItemsL( CAknSoftNotifier* aNotifier, 
       
   247         const TAknSoftNotificationType aNotificationType, const TInt aAmount )
       
   248     {
       
   249     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::ShowNewItemsL" );
       
   250     TInt KVMMaximumNumber = 0xfe;
       
   251     TBool amountKnown = ( aAmount <= KVMMaximumNumber );
       
   252 
       
   253     // fuzzy voicemail note and normal voice mail note should never exist at the same time. 
       
   254     // Always cancel the other before adding new note.
       
   255     if ( aNotificationType == EVoiceMailNotification
       
   256         || aNotificationType == ESeparateVoiceMailOnLine1Notification
       
   257         || aNotificationType == ESeparateVoiceMailOnLine2Notification )
       
   258         {
       
   259         CAknSoftNotificationParameters* params =
       
   260             CreateNotificationParametersLC(!amountKnown);
       
   261   
       
   262         aNotifier->CancelCustomSoftNotificationL( *params );
       
   263         CleanupStack::PopAndDestroy( params );
       
   264         }
       
   265 
       
   266     // create notification parameters
       
   267     CAknSoftNotificationParameters* params =
       
   268         CreateNotificationParametersLC(amountKnown);
       
   269         
       
   270     if( aAmount <= 0 )
       
   271         {
       
   272         aNotifier->CancelCustomSoftNotificationL( *params );
       
   273         }
       
   274     else
       
   275         {
       
   276         aNotifier->SetCustomNotificationCountL( *params, aAmount );
       
   277         }
       
   278 
       
   279     // destroy parameters
       
   280     CleanupStack::PopAndDestroy( params );
       
   281     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::ShowNewItemsL" );
       
   282     }
       
   283 
       
   284 void CHtiNotificationHandler::SetNotificationL(TUint aAmount)
       
   285     {
       
   286     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::SetNotificationL" );
       
   287     
       
   288     //Convert to avkon type
       
   289     TAknSoftNotificationType aknType = EVoiceMailNotification;
       
   290 
       
   291     //Get the secondary display id for this note
       
   292     TInt dialogID = 5; //SecondaryDisplay::ECmdVoiceMailNotification in NcnSecondaryDisplayAPI.h
       
   293 
       
   294     // The category UID for the messages in this header file.
       
   295     //
       
   296     const TUid KCatNcnList = {0x100058F1};
       
   297     const TUint KNcnExternalizedNumberMaxLength = 8;
       
   298     
       
   299     // Amount needs to be stored to "additional parameter"
       
   300     TBuf8<KNcnExternalizedNumberMaxLength> additionalData;
       
   301 
       
   302     additionalData.Num(aAmount);
       
   303     
       
   304     // Encapsulate the sent data
       
   305     CAknSDData* sdData = NULL;
       
   306     TRAPD( status, sdData = CAknSDData::NewL(
       
   307             KCatNcnList, 
       
   308             dialogID, 
       
   309             additionalData) );
       
   310     
       
   311     // Create notifier instance
       
   312     CAknSoftNotifier* notifier = CAknSoftNotifier::NewL();
       
   313     CleanupStack::PushL(notifier);
       
   314     
       
   315     // Send data to cover ui    
       
   316     if ( status == KErrNone && sdData != NULL)
       
   317         {
       
   318         notifier->SetSecondaryDisplayData( sdData ); //Takes ownership 
       
   319         }
       
   320     
       
   321     // If there are problems with notifier, just catch the error and 
       
   322     // try to finish the other processes.
       
   323     TRAP( status, ShowNewItemsL( notifier, aknType, aAmount ) );
       
   324     
       
   325     CleanupStack::PopAndDestroy(notifier);
       
   326     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::SetNotificationL" );
       
   327     }
       
   328 
       
   329 void CHtiNotificationHandler::SetIndicator(const TNcnIndicator aIndicator, TUint aMsgCount, TBool aEnable)
       
   330     {
       
   331     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::SetIndicator" );
       
   332        
       
   333     TInt aId = RMobilePhone::KDisplayVoicemailActive;
       
   334     if( aEnable )       
       
   335         {
       
   336         iMsgWaiting.iDisplayStatus |= aId;          
       
   337         }
       
   338     else
       
   339         {
       
   340         iMsgWaiting.iDisplayStatus &= ~ aId;                    
       
   341         } 
       
   342     iMsgWaiting.iVoiceMsgs = aMsgCount;
       
   343     if(IsActive())
       
   344         {
       
   345         TNcnReqQueueElement elem(ENcnSetIndicator, aIndicator, aMsgCount, aEnable );
       
   346         iReqQueue.Append( elem );
       
   347         }
       
   348     else
       
   349         {
       
   350         iStatus = KRequestPending;
       
   351         iMobilePhone.SetIccMessageWaitingIndicators( iStatus, iMsgWaitingPckg );
       
   352         HTI_LOG_FORMAT("iStatus: %d", iStatus.Int());
       
   353         SetActive();
       
   354         }
       
   355     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::SetIndicator" );
       
   356     }
       
   357 
       
   358 void CHtiNotificationHandler::ResetIndicator()
       
   359     {
       
   360     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::ResetIndicator" );
       
   361     if( IsActive() )
       
   362         {
       
   363         TNcnReqQueueElement elem(ENcnResetIndicator, ENcnIndicatorVMLine1, 0, EFalse );
       
   364         iReqQueue.Append( elem );
       
   365         return;
       
   366         }
       
   367     iMsgWaiting.iVoiceMsgs = 0;
       
   368     iMsgWaiting.iDisplayStatus = 0;
       
   369     iMobilePhone.SetIccMessageWaitingIndicators( iStatus, iMsgWaitingPckg );
       
   370     SetActive();
       
   371     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::ResetIndicator" );
       
   372     }
       
   373 
       
   374 // ----------------------------------------------------------------------------
       
   375 // CHtiNotificationHandler::HandleCreateVoiceMessageNotificationL
       
   376 // Creates new voice message notification.
       
   377 // ----------------------------------------------------------------------------
       
   378 void CHtiNotificationHandler::HandleCreateVoiceMessageNotificationL( const TDesC8& aData )
       
   379     {
       
   380     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::HandleCreateVoiceMessageNotificationL" );
       
   381     
       
   382     if ( aData.Length() == 1 )
       
   383         {
       
   384         HTI_LOG_TEXT( "no command parameter, using default value 1");
       
   385         iVoiceMailsInLine1 = 1;
       
   386         }    
       
   387     else if(aData.Length() == 2) //1-byte command parameter
       
   388         {
       
   389         iVoiceMailsInLine1 = aData[1];
       
   390         HTI_LOG_FORMAT("%d new voice messages", iVoiceMailsInLine1);
       
   391         }
       
   392     else
       
   393         {
       
   394         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   395         HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::HandleCreateVoiceMessageNotificationL" );
       
   396         return;
       
   397         }
       
   398     
       
   399     ResetIndicator();
       
   400     SetNotificationL(iVoiceMailsInLine1);    
       
   401     SetIndicator(ENcnIndicatorVMLine1, iVoiceMailsInLine1, iVoiceMailsInLine1 ? ETrue : EFalse);    
       
   402     SendOkMsgL( KNullDesC8 );    
       
   403     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::HandleCreateVoiceMessageNotificationL" );
       
   404     }
       
   405 
       
   406 // ----------------------------------------------------------------------------
       
   407 // CHtiNotificationHandler::HandleClearAllNotificationsL
       
   408 // Clears all message notifications.
       
   409 // ----------------------------------------------------------------------------
       
   410 void CHtiNotificationHandler::HandleClearAllNotificationsL( const TDesC8& aData )
       
   411     {
       
   412     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::HandleClearAllNotificationsL" );
       
   413     if ( aData.Length() != 1 )
       
   414         {
       
   415         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   416         HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::HandleClearAllNotificationsL" );
       
   417         return;
       
   418         }
       
   419 
       
   420     CAknSoftNotifier* notifier = CAknSoftNotifier::NewL();
       
   421     CleanupStack::PushL(notifier);
       
   422     CAknSoftNotificationParameters* params = CreateNotificationParametersLC( ETrue );  
       
   423     notifier->CancelCustomSoftNotificationL( *params ); 
       
   424     CleanupStack::PopAndDestroy(2);//param, notifier
       
   425 
       
   426     ResetIndicator();
       
   427     
       
   428     SendOkMsgL( KNullDesC8 ); 
       
   429     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::HandleClearAllNotificationsL" );
       
   430     }
       
   431 
       
   432 // ----------------------------------------------------------------------------
       
   433 // CHtiNotificationHandler::SendOkMsgL
       
   434 // Helper function for sending response messages.
       
   435 // ----------------------------------------------------------------------------
       
   436 void CHtiNotificationHandler::SendOkMsgL( const TDesC8& aData )
       
   437     {
       
   438     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::SendOkMsgL" );
       
   439     User::LeaveIfNull( iDispatcher );
       
   440     HBufC8* temp = HBufC8::NewL( aData.Length() + 1 );
       
   441     TPtr8 response = temp->Des();
       
   442     response.Append( ( TChar ) CHtiMessagesServicePlugin::EResultOk );
       
   443     response.Append( aData );
       
   444     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
   445         temp, KHtiMessagesServiceUid ) );
       
   446     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::SendOkMsgL" );
       
   447     }
       
   448 
       
   449 
       
   450 // ----------------------------------------------------------------------------
       
   451 // CHtiNotificationHandler::SendErrorMessageL
       
   452 // Helper function for sending error response messages.
       
   453 // ----------------------------------------------------------------------------
       
   454 void CHtiNotificationHandler::SendErrorMessageL( TInt aError, const TDesC8& aDescription )
       
   455     {
       
   456     HTI_LOG_FUNC_IN( "CHtiNotificationHandler::SendErrorMessageL" );
       
   457     User::LeaveIfNull( iDispatcher );
       
   458     User::LeaveIfError( iDispatcher->DispatchOutgoingErrorMessage(
       
   459         aError, aDescription, KHtiMessagesServiceUid ) );
       
   460     HTI_LOG_FUNC_OUT( "CHtiNotificationHandler::SendErrorMessageL" );
       
   461     }
       
   462 
       
   463 // End of file