messagingappbase/ncnlist/src/NcnHandlerAudio.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2004 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:  NcnHandlerAudio implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <DRMCommon.h>
       
    22 #include    "NcnHandlerAudio.h"
       
    23 #include    "NcnModelBase.h"
       
    24 #include    "NcnModel.h"
       
    25 
       
    26 #include    "NcnCRHandler.h"
       
    27 #include    <ProfileEngineDomainCRKeys.h>
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 // EXTERNAL FUNCTION PROTOTYPES  
       
    31 // CONSTANTS
       
    32 namespace
       
    33     {
       
    34     
       
    35     const TInt KNcnMaxProfileVolume( 10 );    
       
    36     
       
    37     // sampled beep sequence
       
    38     _LIT8( KNcnSampledBeepSequence,
       
    39         "\x2\x4a\x3a\x51\x9\x95\x95\xc0\x4\x0\xb\x1c\x41\x8d\x51\xa8\x0\x0" );
       
    40     }
       
    41 
       
    42 // MACROS
       
    43 
       
    44 #define SET_VOLUME( t, v )  t->SetVolume( Max( 0, Min( t->MaxVolume() /KNcnMaxProfileVolume *v, t->MaxVolume() ) ) )
       
    45 
       
    46 // LOCAL CONSTANTS AND MACROS
       
    47 // MODULE DATA STRUCTURES
       
    48 // LOCAL FUNCTION PROTOTYPES
       
    49 // FORWARD DECLARATIONS
       
    50 
       
    51 // ============================= LOCAL FUNCTIONS ===============================
       
    52 
       
    53 // ============================ MEMBER FUNCTIONS ===============================
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CNcnHandlerAudio::CNcnHandlerAudio
       
    57 // C++ default constructor can NOT contain any code, that
       
    58 // might leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 CNcnHandlerAudio::CNcnHandlerAudio( CNcnModelBase* aModel ) :
       
    62     iNcnModel( aModel ), iDefaultTonePlayed( EFalse )
       
    63     {
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CNcnHandlerAudio::ConstructL
       
    68 // Symbian 2nd phase constructor can leave.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 void CNcnHandlerAudio::ConstructL()
       
    72     {    
       
    73     iProfileEngine = CreateProfileEngineL(); 
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CNcnHandlerAudio::NewL
       
    78 // Two-phased constructor.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CNcnHandlerAudio* CNcnHandlerAudio::NewL( CNcnModelBase* aModel )
       
    82     {
       
    83     // Create instance of 
       
    84     CNcnHandlerAudio* self = 
       
    85         new( ELeave ) CNcnHandlerAudio( aModel );
       
    86     
       
    87     // Push into cleanup stack and construct
       
    88     CleanupStack::PushL( self );
       
    89     self->ConstructL();
       
    90     CleanupStack::Pop();
       
    91 
       
    92     // Return the pointer
       
    93     return self;
       
    94     }
       
    95 
       
    96     
       
    97 // Destructor
       
    98 CNcnHandlerAudio::~CNcnHandlerAudio()
       
    99     {
       
   100     if( iProfileEngine )
       
   101         {
       
   102         iProfileEngine->Release();
       
   103         }
       
   104 
       
   105     RemoveAudioPlayer();
       
   106     iToneQueue.Close();
       
   107     iAudioPlayer = NULL;
       
   108     iNcnModel = NULL;
       
   109     iProfileEngine = NULL;
       
   110     }
       
   111     
       
   112 
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CNcnHandlerAudio::MdapcInitComplete
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void CNcnHandlerAudio::StartPlayToneL()
       
   119 {
       
   120 	// Update volume
       
   121 	MProfile* profile = NULL;
       
   122 	TRAPD( err,  profile = iProfileEngine->ActiveProfileL() );
       
   123 	if( err == KErrNone && profile != NULL )
       
   124 	   {
       
   125 	    CleanupReleasePushL( *profile ); // CSI: 42 # Should not leave
       
   126 	    
       
   127 	    // Set volume
       
   128 		NCN_RDEBUG_INT( _L("CNcnHandlerAudio::MapcInitComplete: Audio player's max volume is %d"), iAudioPlayer->MaxVolume() );        
       
   129 		NCN_RDEBUG_INT( _L("CNcnHandlerAudio::MapcInitComplete: Profile volume is %d"), profile->ProfileTones().ToneSettings().iRingingVolume );
       
   130 	 	SET_VOLUME( iAudioPlayer, profile->ProfileTones().ToneSettings().iRingingVolume );     	
       
   131 	 	    
       
   132 	    // Set time
       
   133 	    TTimeIntervalMicroSeconds time = TTimeIntervalMicroSeconds( 0 );
       
   134 
       
   135 	    // Set repeats and start playing
       
   136 	    iAudioPlayer->SetRepeats( 0, time );
       
   137 	    iAudioPlayer->Play();
       
   138 
       
   139 	    // Set playing state and inform shared date
       
   140 	    iAlertTonePlaying = iToneQueue[0];
       
   141 	    iNcnModel->MsgReceivedTonePlaying( iAlertTonePlaying );
       
   142 	    
       
   143 		CleanupStack::PopAndDestroy( profile );    	
       
   144 	   }              
       
   145 }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CNcnHandlerAudio::MdapcInitComplete
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CNcnHandlerAudio::MapcInitComplete(
       
   152     TInt                                aError, 
       
   153     const TTimeIntervalMicroSeconds&    /* aDuration */ )
       
   154     {
       
   155 	NCN_RDEBUG_INT(_L("CNcnHandlerAudio::MapcInitComplete:  %d"), aError );
       
   156     // Audio player is initialised, so if there is no error, 
       
   157     // we can start playing the tone.
       
   158     if( aError == KErrNone )
       
   159         {
       
   160       	 TRAP_IGNORE(StartPlayToneL());
       
   161         
       
   162         }
       
   163     // Otherwise, play default tone and if there is an error in initialisation then
       
   164 	// go to next tone in quenue.
       
   165     else
       
   166         {
       
   167 		if ( iDefaultTonePlayed )
       
   168 			{
       
   169 			// Play next tone from queue
       
   170 			iDefaultTonePlayed = EFalse;
       
   171 			MapcPlayComplete( aError );
       
   172 			}
       
   173 		else
       
   174 			{
       
   175 			// Leave should not cancel the whole operation, 
       
   176             // so the error is trapped.
       
   177 			iDefaultTonePlayed = ETrue;
       
   178             TRAP_IGNORE( PlayDefaultAlertToneL() );
       
   179 			}
       
   180         }
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CNcnHandlerAudio::PlayDefaultAlertToneL
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 TInt CNcnHandlerAudio::PlayDefaultAlertToneL()
       
   188 	{
       
   189 	// This method plays default tone if there was a problem in loading original tone.
       
   190 	TInt err = KErrNone;
       
   191 
       
   192 	// Remove current audioplayer.
       
   193 	delete iAudioPlayer;
       
   194     iAudioPlayer = NULL;
       
   195 
       
   196    // Get soundfile according to received message
       
   197 	if( iToneQueue[0] == CNcnModelBase::EIndexNewEmails )
       
   198         {
       
   199 		// Default Email tone not defined (?), use the default message alert
       
   200 		err = iNcnModel->GetCRString(
       
   201 			KCRUidProfileEngine,
       
   202 			KProEngDefaultMessageTone,
       
   203 			iSelectedTone );
       
   204 		}
       
   205 	else
       
   206 		{
       
   207 		err = iNcnModel->GetCRString(
       
   208 			KCRUidProfileEngine,
       
   209 			KProEngDefaultMessageTone,
       
   210 			iSelectedTone );
       
   211 		}
       
   212 
       
   213 
       
   214 	if( err )
       
   215 		{
       
   216 		return err;
       
   217 		}
       
   218 
       
   219 	iAudioPlayer = CMdaAudioPlayerUtility::NewFilePlayerL( 
       
   220 		iSelectedTone,
       
   221 		*this, 
       
   222 		KAudioPriorityRecvMsg, 
       
   223 		static_cast<TMdaPriorityPreference>( KAudioPrefNewSMS ) );
       
   224 	
       
   225 	return KErrNone;
       
   226 	}
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CNcnHandlerAudio::MdapcPlayComplete
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void CNcnHandlerAudio::MapcPlayComplete(TInt aError)
       
   233     {
       
   234 	NCN_RDEBUG_INT(_L("CNcnHandlerAudio::MapcPlayComplete: %d"), aError );
       
   235 
       
   236 	if ( aError == DRMCommon::ENoRights ||
       
   237          aError == DRMCommon::ERightsDBCorrupted ||
       
   238          aError == DRMCommon::ERightsExpired ||
       
   239          aError == DRMCommon::EInvalidRights ||
       
   240          aError == KErrArgument )
       
   241 		{
       
   242 		// Leave should not cancel the whole operation, 
       
   243         // so the error is trapped.
       
   244 		iDefaultTonePlayed = ETrue;
       
   245         TRAP_IGNORE( PlayDefaultAlertToneL() );
       
   246 		}
       
   247 	else
       
   248 		{
       
   249 		// When playing is completed, audioplayer can be deleted
       
   250 		RemoveAudioPlayer();
       
   251 
       
   252 		// Remove the played tone from queue
       
   253 		iToneQueue.Remove( 0 );
       
   254 
       
   255 		// Check if another sound needs to be played and play it
       
   256 		TRAPD( err, PlayAlertToneFromQueueL() );
       
   257 
       
   258 		// Check if there was error trying to play alert tone and try once again
       
   259 		if( err )
       
   260 		    {
       
   261 			NCN_RDEBUG_INT(_L("CNcnHandlerAudio::MapcPlayComplete: PlayAlertToneFromQueueL err %d"), err );
       
   262 		    TRAP( err, PlayAlertToneFromQueueL() );
       
   263 		    }
       
   264 		}
       
   265     }
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // CNcnHandlerAudio::StopMsgReceivedTonePlaying
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CNcnHandlerAudio::StopMsgReceivedTonePlaying()
       
   272     {
       
   273     // This method is called when the user has pressed some key during the playing...
       
   274     // Called from the model.
       
   275 
       
   276     // So check if the audioplayer is on and it is playing
       
   277     if( iAudioPlayer && iAlertTonePlaying )
       
   278         {
       
   279         // Stop playing and remove sound from queue
       
   280         iAudioPlayer->Stop();
       
   281         iToneQueue.Reset();        
       
   282         RemoveAudioPlayer();
       
   283         }
       
   284     }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CNcnHandlerAudio::RemoveAudioPlayer
       
   288 // -----------------------------------------------------------------------------
       
   289 //
       
   290 void CNcnHandlerAudio::RemoveAudioPlayer()
       
   291     {
       
   292 	NCN_RDEBUG(_L("CNcnHandlerAudio::RemoveAudioPlayer"));
       
   293 
       
   294     // If other sound is waiting in queue, remove it
       
   295     iAlertTonePlaying = 0;
       
   296 
       
   297     // Delete player and remove address
       
   298     delete iAudioPlayer;
       
   299     iAudioPlayer = NULL;
       
   300 
       
   301     // Inform shared data that nothing is going to be played
       
   302     iNcnModel->MsgReceivedTonePlaying( iAlertTonePlaying );
       
   303     }
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CNcnHandlerAudio::PlayAlertToneL
       
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 TInt CNcnHandlerAudio::PlayAlertToneL( const TUint& aMessageType )
       
   310     {
       
   311     // First check if the tone is already playing
       
   312     if ( iToneQueue.Find( aMessageType ) != KErrNotFound )
       
   313         {
       
   314 		NCN_RDEBUG(_L("CNcnHandlerAudio::PlayAlertToneL msg type found"));
       
   315         return KErrAlreadyExists;
       
   316         }
       
   317 
       
   318     // Set alert tone waiting
       
   319     iToneQueue.Append( aMessageType );
       
   320     
       
   321     // If the audioplayer already exists we don't play sound as it
       
   322     // will be played, after the other sound is played
       
   323     if( iAudioPlayer )
       
   324         {
       
   325 		NCN_RDEBUG(_L("CNcnHandlerAudio::PlayAlertToneL iAudioPlayer already exists"));
       
   326         return KErrAlreadyExists;
       
   327         }
       
   328 
       
   329     // Play the alert tone
       
   330     return PlayAlertToneFromQueueL();
       
   331     }
       
   332     
       
   333 // -----------------------------------------------------------------------------
       
   334 // CNcnHandlerAudio::PlayAlertToneFromQueueL
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 TInt CNcnHandlerAudio::PlayAlertToneFromQueueL()
       
   338     {
       
   339     // Check if there is alert tone that needs to be played
       
   340     if( iToneQueue.Count() )
       
   341         {   
       
   342 		NCN_RDEBUG_INT(_L("CNcnHandlerAudio::PlayAlertToneFromQueueL iToneQueue.Count() is %d"), iToneQueue.Count());
       
   343 		
       
   344         // Get active profile
       
   345         MProfile* profile = iProfileEngine->ActiveProfileL();
       
   346         CleanupReleasePushL( *profile );
       
   347     
       
   348 		// if beep-once is set instantiate player for audio sequence
       
   349 		if( IsBeepOnceSetL( *profile ) )
       
   350 		    {
       
   351             //Debug message    
       
   352   		    NCN_RDEBUG(_L("CNcnHandlerAudio::PlayAlertToneFromQueueL : Beep-once set, playing sequence.."));		      	    
       
   353 	        
       
   354 	        // create new descriptor audio player	    
       
   355 		    iAudioPlayer = CMdaAudioPlayerUtility::NewDesPlayerReadOnlyL(
       
   356 		        KNcnSampledBeepSequence(), *this, KAudioPriorityRecvMsg, 
       
   357                 static_cast<TMdaPriorityPreference>( KAudioPrefNewSMS ) );
       
   358             iDefaultTonePlayed = EFalse;
       
   359 		    }
       
   360 		// otherwise load tone from file
       
   361 		else
       
   362 		    {		  		    
       
   363             // Get correct sound and volume
       
   364             TInt err = GetSoundFileL(
       
   365                 *profile, iSelectedTone, iToneQueue[0] );
       
   366  
       
   367             //Debug message    
       
   368          	NCN_RDEBUG_INT(_L("CNcnHandlerAudio::PlayAlertToneFromQueueL : Beep-once not set, playing tone: %S"), &iSelectedTone);
       
   369          	
       
   370             // If fetching went fine, play the files
       
   371             if( !err )
       
   372                 {
       
   373                 // Create the audio player
       
   374                 iAudioPlayer =  CMdaAudioPlayerUtility::NewFilePlayerL( 
       
   375                     iSelectedTone, *this, KAudioPriorityRecvMsg,
       
   376                     static_cast<TMdaPriorityPreference>( KAudioPrefNewSMS ) );
       
   377     			iDefaultTonePlayed = EFalse;
       
   378                 }
       
   379     		else
       
   380     			{
       
   381     			NCN_RDEBUG_INT(_L("CNcnHandlerAudio::PlayAlertToneFromQueueL: GetSoundFileAndVolumeL err %d"), err );
       
   382     			}
       
   383 		    }
       
   384 		    
       
   385 		// Release profile
       
   386         CleanupStack::PopAndDestroy( profile );
       
   387         }
       
   388 	else
       
   389 		{
       
   390 		NCN_RDEBUG(_L("CNcnHandlerAudio::PlayAlertToneFromQueueL iToneQueue is empty"));
       
   391 		}
       
   392     
       
   393     return KErrNone;
       
   394     }
       
   395 
       
   396 // -----------------------------------------------------------------------------
       
   397 // CNcnHandlerAudio::GetSoundFileAndVolumeL
       
   398 // -----------------------------------------------------------------------------
       
   399 //
       
   400 TInt CNcnHandlerAudio::GetSoundFileL(
       
   401     const MProfile& aProfile,
       
   402     TFileName&      aSound, 
       
   403     const TUint&    aMessageType ) const
       
   404     {
       
   405     // Get soundfile according to received message
       
   406     if( aMessageType == CNcnModelBase::EIndexNewEmails )
       
   407         {        
       
   408         // Sound for email
       
   409         aSound = aProfile.ProfileExtraTones().EmailAlertTone();
       
   410         }
       
   411     else
       
   412         {      
       
   413         // Sound for other type of messages
       
   414         aSound = aProfile.ProfileTones().MessageAlertTone();
       
   415         }
       
   416         
       
   417     return KErrNone;
       
   418     }
       
   419     
       
   420 // -----------------------------------------------------------------------------
       
   421 // CNcnHandlerAudio::IsBeepOnceSetL
       
   422 // -----------------------------------------------------------------------------
       
   423 //
       
   424 TBool CNcnHandlerAudio::IsBeepOnceSetL( const MProfile& aProfile ) const
       
   425     {
       
   426     // default to false
       
   427     TBool ret = EFalse;
       
   428     
       
   429     // get tone settings    
       
   430     const TProfileToneSettings& toneSettings = aProfile.ProfileTones().ToneSettings();
       
   431     
       
   432     // if beep-once is set, set return value to ETrue
       
   433     if( toneSettings.iRingingType == EProfileRingingTypeBeepOnce )
       
   434         {
       
   435         ret = ETrue;
       
   436         }    
       
   437     
       
   438     return ret;
       
   439     }
       
   440     
       
   441 
       
   442 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   443 
       
   444 //  End of File