breakdeps/eikkeysoundserver.cpp
changeset 60 e17592a1211c
child 61 bdb0226b36a8
equal deleted inserted replaced
59:5c286813b9bb 60:e17592a1211c
       
     1 /*
       
     2 * Copyright (c) 2002-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:  EikSrv keysound server.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32svr.h>
       
    19 #include <coemain.h>
       
    20 #include <barsread.h>
       
    21 #include <eiksrvui.rsg>
       
    22 #include <s32mem.h>
       
    23 #include "eikkeysoundserver.h"
       
    24 #include <aknanimdllstd.h>
       
    25 #include "AknEikAudioToneObserver.h"
       
    26 #include <avkon.hrh>
       
    27 #include <centralrepository.h>
       
    28 #include <ProfileEngineSDKCRKeys.h>     // KProEngActiveKeypadVolume
       
    29 #include <ataudioeventapi.h>
       
    30 #include <e32uid.h>
       
    31 
       
    32 // Declare thread entry point
       
    33 GLDEF_C TInt KeySoundServerThreadStartFunction(TAny* aPtr);
       
    34 
       
    35 _LIT(KKeySoundServerThreadName,"KeySoundServerThread");
       
    36 _LIT(KKeySoundServerSemaphoreName,"KeySoundServerSemaphore");
       
    37 _LIT(KKeySoundServerDll,"AtSoundServerClient.dll");
       
    38 
       
    39 const TInt KKeySoundServerStackSize     = 1024*8; // 8K
       
    40 const TInt KAknSoundInfoMapGranularity  = 16;
       
    41 const TInt KKeyClickPreference          = 0x00140001;
       
    42 const TInt KKeySoundServerBufExpandSize = 1024*1; // 1K
       
    43 const TInt KKeysoundServerDllUid        = 0x10281C86;
       
    44 
       
    45 typedef CATAudioEventAPI* (*PFUNC)(MATEventCompleteObserver& aCient);
       
    46 
       
    47 // =======================================
       
    48 // CAknAnimKeySoundControl implementation.
       
    49 // =======================================
       
    50 
       
    51 CAknAnimKeySoundControl::CAknAnimKeySoundControl()
       
    52     {
       
    53     }
       
    54 
       
    55 void CAknAnimKeySoundControl::ConstructL(RWindowGroup* aParent)
       
    56     {
       
    57     CreateWindowL(aParent);
       
    58     SetExtent(TPoint(0,0),TSize(0,0));
       
    59     Window().SetShadowDisabled(ETrue);
       
    60     Window().Activate();
       
    61     }
       
    62 
       
    63 // ================================
       
    64 // RAknAnimKeySound implementation.
       
    65 // ================================
       
    66 
       
    67 RAknAnimKeySound::RAknAnimKeySound(RAnimDll& aAnimDll)
       
    68 :RAnim(aAnimDll)
       
    69     {
       
    70     }
       
    71 
       
    72 void RAknAnimKeySound::ConstructL(RWindowGroup* aParent)
       
    73     {
       
    74     iKeySoundControl = new(ELeave)CAknAnimKeySoundControl();
       
    75     iKeySoundControl->ConstructL(aParent);
       
    76     RAnim::Construct(*(iKeySoundControl->DrawableWindow()), EAnimKeySound, TPtrC8());
       
    77     }
       
    78 
       
    79 void RAknAnimKeySound::Close()
       
    80     {
       
    81     delete iKeySoundControl;
       
    82     iKeySoundControl = NULL;
       
    83     }
       
    84 
       
    85 // ==================================
       
    86 // CEikKeySoundServer implementation.
       
    87 // ==================================
       
    88 
       
    89 TInt CEikKeySoundServer::LaunchServer(TThreadId& aThreadId)
       
    90     {
       
    91     // First, check that ther server isn't already running.
       
    92     TFindServer findServer(__KEYSOUND_SERVER_NAME);
       
    93     TFullName name;
       
    94     if (findServer.Next(name) == KErrNone)
       
    95         {
       
    96         return KErrAlreadyExists;
       
    97         }
       
    98 
       
    99     // Create a semaphore.
       
   100     RSemaphore globStartSignal;
       
   101     TInt err = globStartSignal.CreateGlobal(KKeySoundServerSemaphoreName, EOwnerProcess);
       
   102     if (err != KErrNone)
       
   103         {
       
   104         err=globStartSignal.OpenGlobal(KKeySoundServerSemaphoreName, EOwnerProcess);
       
   105         if (err != KErrNone)
       
   106             {
       
   107             return err;
       
   108             }
       
   109         }
       
   110     RThread keySoundServerThread;
       
   111 
       
   112     err = keySoundServerThread.Create(
       
   113         KKeySoundServerThreadName,
       
   114         KeySoundServerThreadStartFunction,
       
   115         KKeySoundServerStackSize,
       
   116         NULL, // uses caller thread's heap
       
   117         NULL,
       
   118         EOwnerThread);
       
   119 
       
   120     aThreadId = keySoundServerThread.Id();
       
   121     keySoundServerThread.Resume();
       
   122     keySoundServerThread.Close();
       
   123     globStartSignal.Wait();
       
   124     return err;
       
   125     }
       
   126 
       
   127 // Construct the server object
       
   128 LOCAL_C void DoKeySoundServerThreadStartFunctionL()
       
   129     {
       
   130     CEikKeySoundServer::NewLC();
       
   131 
       
   132     RSemaphore globStartSignal;
       
   133     CleanupClosePushL(globStartSignal);
       
   134     User::LeaveIfError(globStartSignal.OpenGlobal(KKeySoundServerSemaphoreName));
       
   135     globStartSignal.Signal();
       
   136     CleanupStack::PopAndDestroy(); // globStartSignal.Close()
       
   137 
       
   138     CActiveScheduler::Start();
       
   139     CleanupStack::PopAndDestroy(); // keySoundServer
       
   140     }
       
   141 
       
   142 // Entry point into the new thread
       
   143 GLDEF_C TInt KeySoundServerThreadStartFunction(TAny* /*aPtr*/)
       
   144     {
       
   145     __UHEAP_MARK;
       
   146     RThread thread;
       
   147 
       
   148     TInt err = User::RenameThread(KKeySoundServerThreadName);
       
   149     if (err == KErrNone)
       
   150         {
       
   151         thread.SetPriority(EPriorityAbsoluteForeground);
       
   152         thread.Close();
       
   153 
       
   154         // Set up scheduler and cleanup stack for this thread
       
   155         CActiveScheduler* scheduler = new CActiveScheduler;
       
   156         if (!scheduler)
       
   157             {
       
   158             return KErrNoMemory;
       
   159             }
       
   160         CActiveScheduler::Install(scheduler);
       
   161         CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   162         if (!trapCleanup)
       
   163             {
       
   164             return KErrNoMemory;
       
   165             }
       
   166 
       
   167         // Set initial trap harness, and construct server object
       
   168         TRAP(err,DoKeySoundServerThreadStartFunctionL());
       
   169 
       
   170         delete CActiveScheduler::Current();
       
   171         delete trapCleanup;
       
   172         }
       
   173     __UHEAP_MARKEND;
       
   174     return err;
       
   175     }
       
   176 
       
   177 CEikKeySoundServer* CEikKeySoundServer::NewLC()
       
   178     {
       
   179     CEikKeySoundServer* self = new(ELeave)CEikKeySoundServer();
       
   180     CleanupStack::PushL(self);
       
   181     self->ConstructL();
       
   182     self->StartL(__KEYSOUND_SERVER_NAME);
       
   183     return self;
       
   184     }
       
   185 
       
   186 CEikKeySoundServer::CEikKeySoundServer()
       
   187 :CServer2(EActivePriorityDefault),
       
   188 iDisabledScanCode( -1 )
       
   189     {
       
   190     // construct all system SID objects
       
   191     }
       
   192 
       
   193 void CEikKeySoundServer::ConstructL()
       
   194     {
       
   195     iInit = EFalse;
       
   196     iSidList = new(ELeave)CArrayFixFlat<TAknSoundID>(KAknSoundInfoMapGranularity);
       
   197     iSoundList = new(ELeave)CArrayPtrFlat<CEikSoundInfo>(KAknSoundInfoMapGranularity);
       
   198 
       
   199     // Default to loudest volume
       
   200     iKeypadVolume = CEikSoundInfo::EKeypadVolumeLoud;
       
   201 
       
   202     TRAPD(err, iProfilesRepository = CRepository::NewL(KCRUidProfileEngine));
       
   203     if (err == KErrNone)
       
   204         {
       
   205         iProfilesNotifyHandler = CCenRepNotifyHandler::NewL(*this,
       
   206                                                             *iProfilesRepository,
       
   207                                                             CCenRepNotifyHandler::EIntKey,
       
   208                                                             KProEngActiveKeypadVolume);
       
   209 
       
   210         iWarningToneEnableHandler = CCenRepNotifyHandler::NewL( *this,
       
   211                                                                 *iProfilesRepository,
       
   212                                                                 CCenRepNotifyHandler::EIntKey,
       
   213                                                                 KProEngActiveWarningTones);
       
   214 
       
   215         iProfilesNotifyHandler->StartListeningL();
       
   216         iWarningToneEnableHandler->StartListeningL();
       
   217 
       
   218         iProfilesRepository->Get(KProEngActiveKeypadVolume, (TInt&)iKeypadVolume);
       
   219         iProfilesRepository->Get(KProEngActiveWarningTones, iWarningToneEnabled);
       
   220         }
       
   221 
       
   222     iATSoundServerAPI = NULL;
       
   223 
       
   224     RLibrary lib;
       
   225     TUidType uidType( KDynamicLibraryUid, KSharedLibraryUid, TUid::Uid( KKeysoundServerDllUid ) );
       
   226 
       
   227     if(lib.Load(KKeySoundServerDll, uidType) == KErrNone)
       
   228         {
       
   229         PFUNC func = (PFUNC)lib.Lookup(1); /* NewL */
       
   230 
       
   231         TRAPD(error, iATSoundServerAPI = (CATAudioEventAPI*) func(*this));
       
   232         if( error )
       
   233             {
       
   234             iATSoundServerAPI = NULL;
       
   235             }
       
   236         }
       
   237 
       
   238 #ifdef _DEBUG
       
   239     RDebug::Print(_L("cenrep CEikKeySoundServer::ConstructL %d"), (TInt)iKeypadVolume);
       
   240 #endif
       
   241     }
       
   242 
       
   243 CEikKeySoundServer::~CEikKeySoundServer()
       
   244     {
       
   245     if (iProfilesNotifyHandler)
       
   246         {
       
   247         iProfilesNotifyHandler->StopListening();
       
   248         delete iProfilesNotifyHandler;
       
   249         }
       
   250 
       
   251     if (iWarningToneEnableHandler)
       
   252         {
       
   253         iWarningToneEnableHandler->StopListening();
       
   254         delete iWarningToneEnableHandler;
       
   255         }
       
   256 
       
   257     delete iProfilesRepository;
       
   258     delete iDefaultSoundMap;
       
   259     delete iSoundList;
       
   260     delete iSidList;
       
   261     delete iATSoundServerAPI;
       
   262     }
       
   263 
       
   264 CSession2* CEikKeySoundServer::NewSessionL(const TVersion& aVersion,
       
   265     const RMessage2& /*aMessage*/) const
       
   266     {
       
   267     TVersion ver(KKeySoundServMajorVN, KKeySoundServMinorVN, KKeySoundServBuildVN);
       
   268     if (!User::QueryVersionSupported(ver, aVersion))
       
   269         {
       
   270         User::Leave(KErrNotSupported);
       
   271         }
       
   272     return CEikKeySoundSession::NewL(CONST_CAST(CEikKeySoundServer*,this));
       
   273     }
       
   274 
       
   275 void CEikKeySoundServer::InitL(const RMessage2& aMessage)
       
   276     {
       
   277     TPckg<TInt> pckgBuf(iInit);
       
   278     aMessage.WriteL(0, pckgBuf);
       
   279     iInit = ETrue;
       
   280     }
       
   281 
       
   282 void CEikKeySoundServer::Complete(TInt aError, TAudioThemeEvent aEvent)
       
   283 	{
       
   284 	if( aError != KErrNone && aError != ESilencedError 
       
   285 	    && aError != EEventCurrentlyPlaying && aError != KErrUnderflow)
       
   286 		{
       
   287 		PlaySid(aEvent, ETrue);
       
   288 		}
       
   289 	}
       
   290 
       
   291 void CEikKeySoundServer::PlaySid(TInt aSid, TBool aPlaySelf)
       
   292     {
       
   293     if (aSid == EAvkonSIDWarningTone && iWarningToneEnabled == 0)
       
   294         {
       
   295         // Don't play warning tone, when the warning tone is disabled in setting.
       
   296         return;
       
   297         }
       
   298 
       
   299     TInt error(KErrNone);
       
   300 
       
   301 // special cases when the sound is always played in the key sound server
       
   302     if (aSid <= 1000 || aSid == EAvkonSIDStandardKeyClick ||
       
   303         aSid == EAvkonSIDReadialCompleteTone || aSid == EAvkonSIDPowerOffTone ||
       
   304         aSid == EAvkonSIDPowerOnTone || aSid == EAvkonSIDVoiceRecordingTone ||
       
   305         aSid == EAvkonSIDVoiceRecordingStartTone ||
       
   306         aSid == EAvkonSIDVoiceRecordingStopTone || aSid == EAvkonSIDNetBusy ||
       
   307         aSid == EAvkonSIDNetCallWaiting || aSid == EAvkonSIDNetReorder ||
       
   308         aSid == EAvkonSIDNetCongestion || aSid == EAvkonSIDNetSpecialInformation ||
       
   309         aSid == EAvkonSIDNetRadioNotAvailable || aSid == EAvkonSIDIHFActive ||
       
   310         aSid == EAvkonSIDRadioPathAcknowledge || aSid == EAvkonSIDDial ||
       
   311         aSid == EAvkonSIDRingGoing || aSid == EAvkonSIDLocationRequest ||
       
   312         aSid == EAvkonSIDInformationTone || aSid == EAvkonSIDConfirmationTone ) 
       
   313         {
       
   314         aPlaySelf = ETrue;
       
   315         }
       
   316 
       
   317 	if(!iATSoundServerAPI)
       
   318         {
       
   319     	aPlaySelf = ETrue;
       
   320     	}
       
   321 
       
   322     if(!aPlaySelf && iATSoundServerAPI)
       
   323         {
       
   324         TAudioThemeEvent event = static_cast<TAudioThemeEvent>(aSid);
       
   325         TRAP(error, iATSoundServerAPI->SendAudioEventL(event, EFalse));
       
   326         }
       
   327 
       
   328     if (aPlaySelf || error)
       
   329         {
       
   330         TKeyArrayFix sidKey(_FOFF(TAknSoundID, iSid), ECmpTUint);
       
   331         TAknSoundID soundId;
       
   332         soundId.iSid = aSid;
       
   333         TInt index;
       
   334         if (iSidList->FindIsq(soundId, sidKey, index) == 0)
       
   335             {
       
   336             CEikSoundInfo* info = ((*iSidList)[index]).iSoundInfo;
       
   337             if (info && info->Volume() != CEikSoundInfo::EKeypadVolumeOff )
       
   338                 {
       
   339                 TRAP_IGNORE(info->PlayL());
       
   340 #if defined(EIKKSS_DEBUGSOUNDID)
       
   341                 RDebug::Print(_L("Playing SoundID:%d"), aSid & 0xffff);
       
   342 #endif
       
   343                 LOGTEXT2(_L(" PlaySid(): Index:%d, pointer:%d"), index, (TInt)info);
       
   344                 }
       
   345             }
       
   346         }
       
   347     }
       
   348 
       
   349 void CEikKeySoundServer::StopSid(TInt aSid)
       
   350     {
       
   351     TKeyArrayFix sidKey(_FOFF(TAknSoundID, iSid), ECmpTUint);
       
   352     TAknSoundID soundId;
       
   353     soundId.iSid = aSid;
       
   354     TInt index;
       
   355     if (iSidList->FindIsq(soundId, sidKey, index) == 0)
       
   356         {
       
   357         CEikSoundInfo* info = ((*iSidList)[index]).iSoundInfo;
       
   358         if (info)
       
   359             {
       
   360             info->Stop();
       
   361 #if defined(EIKKSS_DEBUGSOUNDID)
       
   362             RDebug::Print(_L("Force Stopping SoundID:%d"), aSid & 0xffff);
       
   363 #endif
       
   364             }
       
   365         }
       
   366     }
       
   367 
       
   368 void CEikKeySoundServer::SetVolumeForPreferenceType(TInt aPreference,
       
   369     CEikSoundInfo::TVolumeSetting aVolume)
       
   370     {
       
   371     TInt count = iSoundList->Count();
       
   372     for (TInt ii = 0; ii < count; ii++)
       
   373         {
       
   374         CEikSoundInfo* info = (*iSoundList)[ii];
       
   375         if (info)
       
   376             {
       
   377             if (info->Preference() == aPreference)
       
   378                 {
       
   379                 info->SetVolume(aVolume);
       
   380                 }
       
   381             }
       
   382         }
       
   383     }
       
   384 
       
   385 void CEikKeySoundServer::SetDisabledScanCode( TInt aScanCode )
       
   386 	{
       
   387 	iDisabledScanCode = aScanCode;
       
   388 	}
       
   389 
       
   390 TInt CEikKeySoundServer::DisabledScanCode()
       
   391 	{
       
   392 	return iDisabledScanCode;
       
   393 	}
       
   394 
       
   395 
       
   396 void CEikKeySoundServer::HandleNotifyInt(TUint32 aId, TInt aNewValue)
       
   397     {
       
   398     if (aId == KProEngActiveKeypadVolume)
       
   399         {
       
   400         iKeypadVolume = (CEikSoundInfo::TVolumeSetting)aNewValue;
       
   401         SetVolumeForPreferenceType( KKeyClickPreference, iKeypadVolume );
       
   402         }
       
   403     else if (aId == KProEngActiveWarningTones)
       
   404         {
       
   405         iWarningToneEnabled = aNewValue;
       
   406         }
       
   407     }
       
   408 
       
   409 // ===================================
       
   410 // CEikKeySoundSession implementation.
       
   411 // ===================================
       
   412 
       
   413 CEikKeySoundSession* CEikKeySoundSession::NewL(CEikKeySoundServer* aServer)
       
   414     {
       
   415     CEikKeySoundSession* self = new(ELeave)CEikKeySoundSession(aServer);
       
   416     CleanupStack::PushL(self);
       
   417     self->ConstructL();
       
   418     CleanupStack::Pop();
       
   419     return self;
       
   420     }
       
   421 
       
   422 
       
   423 CEikKeySoundSession::CEikKeySoundSession(CEikKeySoundServer* aServer)
       
   424 :CSession2(), iServer(aServer)
       
   425     {
       
   426     }
       
   427 
       
   428 CEikKeySoundSession::~CEikKeySoundSession()
       
   429     {
       
   430     RemoveSids(iClientUid);
       
   431     if (iHasLockedContext)
       
   432         {
       
   433         if( iServer )
       
   434             {
       
   435             iServer->SetContextLocked(EFalse);
       
   436             }
       
   437         }
       
   438     if (iOwnsDefaultSounds)
       
   439         {
       
   440         RemoveSids(0);
       
   441         iServer->iInit = EFalse;
       
   442         }
       
   443     if (iSoundStack)
       
   444         {
       
   445         if (iSoundStack->Count() > 0)
       
   446             {
       
   447             // Bottom entry is owned by server, so remove before deleting other sound maps
       
   448             iSoundStack->Delete(0);
       
   449             iSoundStack->ResetAndDestroy();
       
   450             }
       
   451         // Make sure server isn't using this soundstack
       
   452         if (iServer->iCurrentSoundStack == iSoundStack)
       
   453             {
       
   454             iServer->iCurrentSoundStack = NULL;
       
   455             }
       
   456         delete iSoundStack;
       
   457         }
       
   458     }
       
   459 
       
   460 
       
   461 void CEikKeySoundSession::ConstructL()
       
   462     {
       
   463     iSoundStack = CEikKeySoundStack::NewL();
       
   464     if (iServer->iDefaultSoundMap)
       
   465         {
       
   466         iSoundStack->AppendL(iServer->iDefaultSoundMap);
       
   467         }
       
   468     }
       
   469 
       
   470 
       
   471 void CEikKeySoundSession::ServiceL(const RMessage2& aMessage)
       
   472     {
       
   473     if (aMessage.Function() == EKeySoundServerPlayKey)
       
   474         {
       
   475         TInt scancode = aMessage.Int0() & 0xff;
       
   476         TBool repeat = aMessage.Int1();
       
   477         aMessage.Complete(KErrNone);
       
   478         TInt sid;
       
   479         if (iServer->iCurrentSoundStack)
       
   480             {
       
   481             if (iServer->iCurrentSoundStack->Find(scancode, repeat, sid))
       
   482                 {
       
   483                 if ( scancode != iServer->DisabledScanCode() )
       
   484                 	{
       
   485                     iServer->PlaySid(sid, EFalse);
       
   486                 	}
       
   487                 else
       
   488                 	{
       
   489                 	iServer->SetDisabledScanCode( -1 );
       
   490                 	}
       
   491                 }
       
   492             }
       
   493         }
       
   494     else
       
   495         {
       
   496         TRAPD(err, DispatchMessageL(aMessage));
       
   497         aMessage.Complete(err);
       
   498         }
       
   499     }
       
   500 
       
   501 
       
   502 void CEikKeySoundSession::DispatchMessageL(const RMessage2& aMessage)
       
   503     {
       
   504     switch (aMessage.Function())
       
   505         {
       
   506         case EKeySoundServerInit:
       
   507             iClientUid = aMessage.Int1();
       
   508             iServer->InitL(aMessage);
       
   509             break;
       
   510         case EKeySoundServerPlaySID:
       
   511             iServer->PlaySid(aMessage.Int0(), EFalse);
       
   512             break;
       
   513         case EKeySoundServerAddSIDS:
       
   514             AddSoundIdBufferL(aMessage);
       
   515             break;
       
   516         case EKeySoundServerPushContext:
       
   517             PushContextL(aMessage);
       
   518             break;
       
   519         case EKeySoundServerPopContext:
       
   520             PopContext();
       
   521             break;
       
   522         case EKeySoundServerTopContext:
       
   523             {
       
   524             TInt id = 0;
       
   525             if (iServer->iCurrentSoundStack && iServer->iCurrentSoundStack->Count())
       
   526                 {
       
   527                 CEikKeySoundMap* map = iServer->iCurrentSoundStack->At(
       
   528                     iServer->iCurrentSoundStack->Count() - 1);
       
   529 
       
   530                 id = map->ContextResourceId();
       
   531                 }
       
   532             TPckg<TInt> pckg(id);
       
   533             aMessage.WriteL(0, pckg);
       
   534             break;
       
   535             }
       
   536         case EKeySoundServerBringToForeground:
       
   537             if ( !iServer->ContextLocked() )
       
   538                 {
       
   539                 iServer->iCurrentSoundStack = iSoundStack;
       
   540                 }
       
   541             break;
       
   542         case EKeySoundServerStopCurrentTone:
       
   543             iServer->StopSid(aMessage.Int0());
       
   544             break;
       
   545         case EKeySoundServerLockContext:
       
   546             // We do not want lock twice and we do not want lock other's context.
       
   547             if ( !iServer->ContextLocked() && iServer->iCurrentSoundStack == iSoundStack )
       
   548                 {
       
   549                 iServer->SetContextLocked( ETrue );
       
   550                 iHasLockedContext = ETrue;
       
   551                 }
       
   552             break;
       
   553         case EKeySoundServerReleaseContext:
       
   554             iServer->SetContextLocked( EFalse );
       
   555             iHasLockedContext = EFalse;
       
   556             break;
       
   557         case EKeySoundServerCloseServer:
       
   558             {
       
   559             RProcess myProcess;
       
   560             TUid myUid = myProcess.SecureId();
       
   561 
       
   562             // This server command is not allowed to be called outside eiksrvs process.
       
   563             if (aMessage.SecureId() != myProcess.SecureId())
       
   564                 {
       
   565                 User::Leave(KErrPermissionDenied);
       
   566                 }
       
   567 
       
   568             CActiveScheduler::Stop();
       
   569             break;
       
   570             }
       
   571 
       
   572         case EKeySoundServerDisableNextKeySound:
       
   573             iServer->SetDisabledScanCode( aMessage.Int0() );
       
   574             break;
       
   575 
       
   576         default:
       
   577             User::Leave(KErrNotSupported);
       
   578             break;
       
   579         }
       
   580     }
       
   581 
       
   582 void CEikKeySoundSession::AddSoundIdBufferL(const RMessage2& aMessage)
       
   583     {
       
   584     TInt uid = aMessage.Int0();
       
   585     TInt size = aMessage.Int1();
       
   586 
       
   587     // CBufFlat requires that size must be positive and not larger than KMaxTInt / 2.
       
   588     // Without this check the KeySoundServer could panic.
       
   589     if (size <= 0 || size >= ((KMaxTInt / 2) - KKeySoundServerBufExpandSize))
       
   590         {
       
   591         User::Leave(KErrArgument);
       
   592         }
       
   593 
       
   594     if (uid == 0)
       
   595         {
       
   596         // Remember if this session is loading the default sounds,
       
   597         // so they can be restored if this session goes down.
       
   598         iOwnsDefaultSounds = ETrue;
       
   599         }
       
   600 
       
   601     CBufFlat* buffer = CBufFlat::NewL(KKeySoundServerBufExpandSize);
       
   602     CleanupStack::PushL(buffer);
       
   603 
       
   604     buffer->ExpandL(0,size);
       
   605     TPtr8 buf(buffer->Ptr(0));
       
   606     aMessage.ReadL(2,buf);
       
   607 
       
   608     // Internalize the data from the stream
       
   609     RBufReadStream readStream;
       
   610     readStream.Open(*buffer);
       
   611 
       
   612     CleanupClosePushL(readStream);
       
   613 
       
   614     TInt count = readStream.ReadUint16L();
       
   615 
       
   616     for (TInt ii = 0; ii < count; ii++)
       
   617         {
       
   618         TAknSoundID soundId;
       
   619         soundId.iSid = readStream.ReadUint32L();
       
   620         soundId.iAppUid = uid;
       
   621 
       
   622         TInt priority = readStream.ReadUint16L();
       
   623         TInt preference = readStream.ReadUint32L();
       
   624 
       
   625         TInt type = readStream.ReadInt8L();
       
   626         switch (type)
       
   627             {
       
   628             case 0: // File
       
   629                 {
       
   630                 CAknFileSoundInfo* soundInfo = new(ELeave)CAknFileSoundInfo(priority, preference);
       
   631                 CleanupStack::PushL(soundInfo);
       
   632                 soundId.iSoundInfo = soundInfo;
       
   633                 TFileName file;
       
   634                 readStream >> file;
       
   635 
       
   636                 TInt volume = readStream.ReadInt8L();
       
   637 
       
   638                 if ( volume >= CEikSoundInfo::ESoundVolume0
       
   639                     && volume <= CEikSoundInfo::ESoundVolume9 )
       
   640                     {
       
   641                     soundInfo->SetVolume((CEikSoundInfo::TVolumeSetting)volume);
       
   642                     }
       
   643                 // No need to use else, because sound infos default to the loudest.
       
   644 
       
   645                 AddFileSidL(soundId, soundInfo, file);
       
   646                 CleanupStack::Pop();    // soundInfo
       
   647                 break;
       
   648                 }
       
   649             case 1: // tone
       
   650                 {
       
   651                 CAknToneSoundInfo* soundInfo = new(ELeave)CAknToneSoundInfo(priority, preference);
       
   652                 CleanupStack::PushL(soundInfo);
       
   653                 soundId.iSoundInfo = soundInfo;
       
   654                 soundInfo->iFrequency = readStream.ReadUint16L();
       
   655                 TInt64 ms(STATIC_CAST(TUint,readStream.ReadUint32L()));
       
   656                 soundInfo->iMs = TTimeIntervalMicroSeconds(ms);
       
   657 
       
   658                 TInt volume = readStream.ReadInt8L();
       
   659 
       
   660                 if ( volume >= CEikSoundInfo::ESoundVolume0
       
   661                     && volume <= CEikSoundInfo::ESoundVolume9 )
       
   662                     {
       
   663                     soundInfo->SetVolume((CEikSoundInfo::TVolumeSetting)volume);
       
   664                     }
       
   665                 // No need to use else, because sound infos default to the loudest.
       
   666 
       
   667                 AddToneSidL(soundId, soundInfo);
       
   668                 CleanupStack::Pop();    // soundInfo
       
   669                 break;
       
   670                 }
       
   671             case 2: // sequence
       
   672                 {
       
   673                 CAknSequenceSoundInfo* soundInfo = new(ELeave)CAknSequenceSoundInfo(priority,
       
   674                     preference);
       
   675 
       
   676                 CleanupStack::PushL(soundInfo);
       
   677                 soundInfo->ReadSequenceL(readStream);
       
   678                 soundId.iSoundInfo = soundInfo;
       
   679 
       
   680                 TInt volume = readStream.ReadInt8L();
       
   681 
       
   682                 if ( volume >= CEikSoundInfo::ESoundVolume0
       
   683                     && volume <= CEikSoundInfo::ESoundVolume9 )
       
   684                     {
       
   685                     soundInfo->SetVolume((CEikSoundInfo::TVolumeSetting)volume);
       
   686                     }
       
   687                 // No need to use else, because sound infos default to the loudest.
       
   688 
       
   689                 AddSequenceSidL(soundId, soundInfo);
       
   690                 CleanupStack::Pop();    // soundinfo
       
   691                 break;
       
   692                 }
       
   693             } // end of switch
       
   694         }
       
   695 
       
   696     CleanupStack::PopAndDestroy(2); // readstream close, buffer
       
   697     // Set default volumes.
       
   698     iServer->SetVolumeForPreferenceType(KKeyClickPreference, iServer->iKeypadVolume);
       
   699     }
       
   700 
       
   701 void CEikKeySoundSession::RemoveSids(TInt aUid)
       
   702     {
       
   703     TUint uid = aUid << 16;
       
   704     if (!iServer->iSidList)
       
   705         {
       
   706         return;
       
   707         }
       
   708     TInt count = iServer->iSidList->Count();
       
   709     for (TInt ii=0; ii<count; ii++)
       
   710         {
       
   711         TAknSoundID& id = iServer->iSidList->At(ii);
       
   712         if ( (id.iSid & 0xffff0000) == uid)
       
   713             {
       
   714             // Check first for possible duplicate sid. It is possible to have duplicates
       
   715             // in some cases when 2 instances of same application (uid) are running
       
   716             // simultaneusly, e.g. the real application and an embedded instance of
       
   717             // the application.
       
   718             TBool duplicateFound = EFalse;
       
   719             for (TInt jj = ii + 1; jj < count; jj++)
       
   720                 {
       
   721                 TAknSoundID& possibleDuplicateId = iServer->iSidList->At(jj);
       
   722                 if (possibleDuplicateId.iSid == id.iSid)
       
   723                     {
       
   724                     duplicateFound = ETrue;
       
   725                     break;
       
   726                     }
       
   727                 }
       
   728             // If no duplicate found, sid is deleted. Else duplicate will be
       
   729             // deleted when the for-loop reaches it (unless there is even more duplicates).
       
   730             if (!duplicateFound)
       
   731                 {
       
   732                 // Remove sound at this position
       
   733                 delete id.iSoundInfo;
       
   734                 if (iServer->iSoundList)
       
   735                     {
       
   736                     iServer->iSoundList->Delete(ii);
       
   737                     }
       
   738                 iServer->iSidList->Delete(ii);
       
   739                 ii--;
       
   740                 count--;
       
   741                 }
       
   742             }
       
   743         }
       
   744     }
       
   745 
       
   746 void CEikKeySoundSession::PushContextL(const RMessage2& aMessage)
       
   747     {
       
   748     TInt items = aMessage.Int0();
       
   749     TInt uid = aMessage.Int2();
       
   750     TInt resSize = (items * 5);
       
   751     TInt contextResId = aMessage.Int3();
       
   752 
       
   753     // CBufFlat requires that resSize must be positive and not larger than KMaxTInt / 2.
       
   754     // Without this check the KeySoundServer could panic.
       
   755     if (resSize <= 0 || resSize >= ((KMaxTInt / 2) - KKeySoundServerBufExpandSize))
       
   756         {
       
   757         User::Leave(KErrArgument);
       
   758         }
       
   759 
       
   760     CBufFlat* buffer = CBufFlat::NewL(KKeySoundServerBufExpandSize);
       
   761     CleanupStack::PushL(buffer);
       
   762 
       
   763     buffer->ExpandL(0,resSize);
       
   764     TPtr8 buf(buffer->Ptr(0));
       
   765     aMessage.ReadL(1,buf);
       
   766 
       
   767     // Internalize the data from the stream
       
   768     RBufReadStream readStream;
       
   769     readStream.Open(*buffer);
       
   770 
       
   771     CleanupClosePushL(readStream);
       
   772 
       
   773     CEikKeySoundMap* soundMap = CEikKeySoundMap::NewL();
       
   774     CleanupStack::PushL(soundMap);
       
   775     soundMap->SetContextResourceId(contextResId);
       
   776     soundMap->InternalizeL(readStream, items, uid);
       
   777     iSoundStack->AppendL(soundMap);
       
   778     CleanupStack::Pop();    //   soundMap
       
   779 
       
   780     if (iServer->iDefaultSoundMap == NULL)
       
   781         {
       
   782         iServer->iDefaultSoundMap = soundMap;
       
   783         }
       
   784 
       
   785     CleanupStack::PopAndDestroy(2); // readstream close, buffer
       
   786     }
       
   787 
       
   788 void CEikKeySoundSession::PopContext()
       
   789     {
       
   790     if (iSoundStack)
       
   791         {
       
   792         TInt count = iSoundStack->Count();
       
   793         if (count > 1)
       
   794             {
       
   795             delete iSoundStack->At(count-1);
       
   796             iSoundStack->Delete(count-1);
       
   797             }
       
   798         }
       
   799     }
       
   800 
       
   801 void CEikKeySoundSession::AddToneSidL(const TAknSoundID& aSoundID, CAknToneSoundInfo* aSoundInfo)
       
   802     {
       
   803     aSoundInfo->InitL();
       
   804 
       
   805     TKeyArrayFix sidKey(_FOFF(TAknSoundID, iSid), ECmpTUint);
       
   806     if (iServer->iSidList && iServer->iSoundList)
       
   807         {
       
   808         TInt position = iServer->iSidList->InsertIsqAllowDuplicatesL(aSoundID, sidKey);
       
   809         TRAPD(err,
       
   810         iServer->iSoundList->AppendL(aSoundInfo));
       
   811         if (err != KErrNone)
       
   812             {
       
   813             iServer->iSidList->Delete(position);
       
   814             }
       
   815         }
       
   816     }
       
   817 
       
   818 void CEikKeySoundSession::AddSequenceSidL(const TAknSoundID& aSoundID,
       
   819     CAknSequenceSoundInfo* aSoundInfo)
       
   820     {
       
   821     aSoundInfo->InitL();
       
   822 
       
   823     TKeyArrayFix sidKey(_FOFF(TAknSoundID, iSid), ECmpTUint);
       
   824 
       
   825     if (iServer->iSidList && iServer->iSoundList)
       
   826         {
       
   827         TInt position = iServer->iSidList->InsertIsqAllowDuplicatesL(aSoundID, sidKey);
       
   828         TRAPD(err,
       
   829         iServer->iSoundList->AppendL(aSoundInfo));
       
   830         if (err != KErrNone)
       
   831             {
       
   832             iServer->iSidList->Delete(position);
       
   833             }
       
   834         }
       
   835     }
       
   836 
       
   837 void CEikKeySoundSession::AddFileSidL(const TAknSoundID& aSoundID, CAknFileSoundInfo* aSoundInfo,
       
   838     const TDesC& aFileName)
       
   839     {
       
   840     aSoundInfo->InitL(aFileName, NULL);
       
   841 
       
   842     TKeyArrayFix sidKey(_FOFF(TAknSoundID, iSid), ECmpTUint);
       
   843     if (iServer->iSidList && iServer->iSoundList)
       
   844         {
       
   845         TInt position = iServer->iSidList->InsertIsqAllowDuplicatesL(aSoundID, sidKey);
       
   846         TRAPD(err,
       
   847         iServer->iSoundList->AppendL(aSoundInfo));
       
   848         if (err != KErrNone)
       
   849             {
       
   850             iServer->iSidList->Delete(position);
       
   851             }
       
   852         }
       
   853     }
       
   854 
       
   855 // =============================
       
   856 // CEikSoundInfo implementation.
       
   857 // =============================
       
   858 
       
   859 CEikSoundInfo::CEikSoundInfo(TInt aPriority, TInt aPreference)
       
   860     {
       
   861     iPriority = aPriority;
       
   862     iPreference = aPreference;
       
   863     iVolume = ESoundVolume9; // default to loudest
       
   864     }
       
   865 
       
   866 CEikSoundInfo::~CEikSoundInfo()
       
   867     {
       
   868     LOGTEXT(_L("CEikSoundInfo::~CEikSoundInfo(). Destructor."));
       
   869     // Empty implementation.
       
   870     }
       
   871 
       
   872 TInt CEikSoundInfo::Preference()
       
   873     {
       
   874     return iPreference;
       
   875     }
       
   876 
       
   877 CEikSoundInfo::TVolumeSetting CEikSoundInfo::Volume()
       
   878     {
       
   879     return iVolume;
       
   880     }
       
   881 
       
   882 // ==================================
       
   883 // CAknSynthSoundInfo implementation.
       
   884 // ==================================
       
   885 
       
   886 CAknSynthSoundInfo::CAknSynthSoundInfo(TInt aPriority, TInt aPreference)
       
   887 : CEikSoundInfo(aPriority, aPreference), iPlayedStatically(EFalse)
       
   888     {
       
   889     }
       
   890 
       
   891 CAknSynthSoundInfo::~CAknSynthSoundInfo()
       
   892     {
       
   893     LOGTEXT(_L("CAknSynthSoundInfo::~CAknSynthSoundInfo(). Destructor."));
       
   894 
       
   895     // Stops playing and deletes instances if they exist.
       
   896     Stop();
       
   897 
       
   898     delete iToneObserver;
       
   899     delete iTonePlayer;
       
   900     }
       
   901 
       
   902 void CAknSynthSoundInfo::Prepare()
       
   903     {
       
   904     LOGTEXT(_L("CAknSynthSoundInfo::Prepare()."));
       
   905     // This base class method should never be called.
       
   906     }
       
   907 
       
   908 void CAknSynthSoundInfo::InitL()
       
   909     {
       
   910     // Keyclicks are played with a statically reserved CMdaAudioToneUtility.
       
   911     // Same concerns also error tones (this is a hack to avoid an endless error dialog
       
   912     // loop because of audio server crash).
       
   913     if ((iPriority == EAvkonKeyClickPriority && iPreference == KKeyClickPreference) ||
       
   914         (iPriority == EAvkonErrorNotePriority && iPreference == EAvkonErrorNotePreference))
       
   915         {
       
   916         LOGTEXT(_L("CAknSynthSoundInfo::InitL(). Static init."));
       
   917 
       
   918         iPlayedStatically = ETrue;
       
   919         iToneObserver = CAknEikAudioToneObserver::NewL(*this);
       
   920         iTonePlayer = CMdaAudioToneUtility::NewL(*iToneObserver, NULL); // Synchronous
       
   921         }
       
   922     }
       
   923 
       
   924 void CAknSynthSoundInfo::PlayL()
       
   925     {
       
   926     LOGTEXT(_L("CAknSynthSoundInfo::PlayL()."));
       
   927     LOGTEXT3(_L(" This:%d, iPriority:%d, iPreference:%d"), (TInt)this, iPriority, iPreference);
       
   928 
       
   929     // Stops playing and deletes instances if they exist.
       
   930     Stop();
       
   931 
       
   932     if (!iPlayedStatically)
       
   933         {
       
   934         // Create new tone player and observer.
       
   935         iToneObserver = CAknEikAudioToneObserver::NewL(*this);
       
   936         iTonePlayer = CMdaAudioToneUtility::NewL(*iToneObserver, NULL); // Synchronous
       
   937         }
       
   938 
       
   939     // Prepare to play either a tone or a sequence depending on subclass.
       
   940     Prepare();
       
   941     }
       
   942 
       
   943 void CAknSynthSoundInfo::DoPlay()
       
   944     {
       
   945     LOGTEXT(_L("CAknSynthSoundInfo::DoPlay()."));
       
   946     LOGTEXT3(_L(" This:%d, iPriority:%d, iPreference:%d"), (TInt)this, iPriority, iPreference);
       
   947 
       
   948     if (iTonePlayer->State() == EMdaAudioToneUtilityPrepared)
       
   949         {
       
   950         LOGTEXT(_L(" CAknSynthSoundInfo::DoPlay(). Prepare successful, play."));
       
   951 
       
   952         iTonePlayer->SetPriority(iPriority,(TMdaPriorityPreference)iPreference);
       
   953         DoSetVolume(iTonePlayer);
       
   954 
       
   955         LOGTEXT1(_L(" iVolume just before calling play: %d"), iVolume);
       
   956         LOGTEXT1(_L(" Volume from iTonePlayer: %d"), iTonePlayer->Volume());
       
   957 
       
   958         iTonePlayer->Play();
       
   959         }
       
   960     else
       
   961         {
       
   962         LOGTEXT(_L(" CAknSynthSoundInfo::DoPlay(). Prepare failed, delete!"));
       
   963 
       
   964         if (!iPlayedStatically)
       
   965             {
       
   966             LOGTEXT(_L(" CAknSynthSoundInfo::DoPlay(). Deleting iTonePlayer and iToneObserver."));
       
   967 
       
   968             delete iTonePlayer;
       
   969             iTonePlayer = NULL;
       
   970             delete iToneObserver;
       
   971             iToneObserver = NULL;
       
   972             }
       
   973         }
       
   974     }
       
   975 
       
   976 void CAknSynthSoundInfo::Stop()
       
   977     {
       
   978     LOGTEXT(_L("CAknSynthSoundInfo::Stop()."));
       
   979     LOGTEXT3(_L(" This:%d, iPriority:%d, iPreference:%d"), (TInt)this, iPriority, iPreference);
       
   980 
       
   981     // Stop playing and delete tone player if it exists.
       
   982     if (iTonePlayer)
       
   983         {
       
   984         LOGTEXT(_L(" CAknSynthSoundInfo::Stop(). iTonePlayer exists."));
       
   985 
       
   986         if (iTonePlayer->State() == EMdaAudioToneUtilityPlaying)
       
   987             {
       
   988             LOGTEXT(_L(" CAknSynthSoundInfo::Stop(). Playing, call CancelPlay()."));
       
   989 
       
   990             iTonePlayer->CancelPlay();
       
   991             }
       
   992 
       
   993         if (!iPlayedStatically)
       
   994             {
       
   995             LOGTEXT(_L(" CAknSynthSoundInfo::Stop(). Dynamic playing. Deleting iTonePlayer."));
       
   996 
       
   997             delete iTonePlayer;
       
   998             iTonePlayer = NULL;
       
   999             }
       
  1000         }
       
  1001 
       
  1002     // Delete also tone observer if it exists.
       
  1003     if (iToneObserver)
       
  1004         {
       
  1005         LOGTEXT(_L(" CAknSynthSoundInfo::Stop(). iToneObserver exists."));
       
  1006 
       
  1007         if (!iPlayedStatically)
       
  1008             {
       
  1009             LOGTEXT(_L(" CAknSynthSoundInfo::Stop(). Dynamic playing. Deleting iToneObserver."));
       
  1010 
       
  1011             delete iToneObserver;
       
  1012             iToneObserver = NULL;
       
  1013             }
       
  1014         }
       
  1015     }
       
  1016 
       
  1017 void CAknSynthSoundInfo::SetVolume(TVolumeSetting aVolume)
       
  1018     {
       
  1019     iVolume = aVolume;
       
  1020     }
       
  1021 
       
  1022 void CAknSynthSoundInfo::DoSetVolume(CMdaAudioToneUtility* aTonePlayer)
       
  1023     {
       
  1024     TInt max = aTonePlayer->MaxVolume();
       
  1025 
       
  1026     if ( max == 0x0000ffff ) // 16bit -1 i.e. not set
       
  1027         {
       
  1028         max = (TInt)ESoundVolume9; // Set it to our max
       
  1029         }
       
  1030 
       
  1031     TInt volume = 0;
       
  1032 
       
  1033     if ( Preference() != KKeyClickPreference ) // Other sounds than key click
       
  1034         {
       
  1035         aTonePlayer->SetVolume( ((TInt)iVolume * max )/(TInt)ESoundVolume9);
       
  1036         return;
       
  1037         }
       
  1038 
       
  1039     switch (iVolume)
       
  1040         {
       
  1041         case EKeypadVolumeOff:
       
  1042             break;
       
  1043         case EKeypadVolumeQuiet:
       
  1044             volume = max / 3;
       
  1045             break;
       
  1046         case EKeypadVolumeMedium:
       
  1047             volume = (max * 2) / 3;
       
  1048             break;
       
  1049         default: //case EKeypadVolumeLoud:
       
  1050             volume = max;
       
  1051             break;
       
  1052         }
       
  1053     aTonePlayer->SetVolume(volume);
       
  1054     }
       
  1055 
       
  1056 
       
  1057 // =================================
       
  1058 // CAknToneSoundInfo implementation.
       
  1059 // =================================
       
  1060 
       
  1061 CAknToneSoundInfo::CAknToneSoundInfo(TInt aPriority, TInt aPreference)
       
  1062 : CAknSynthSoundInfo(aPriority, aPreference)
       
  1063     {
       
  1064     }
       
  1065 
       
  1066 CAknToneSoundInfo::~CAknToneSoundInfo()
       
  1067     {
       
  1068     LOGTEXT(_L("CAknToneSoundInfo::~CAknToneSoundInfo(). Destructor."));
       
  1069     // Empty implementation.
       
  1070     }
       
  1071 
       
  1072 void CAknToneSoundInfo::Prepare()
       
  1073     {
       
  1074     LOGTEXT(_L("CAknToneSoundInfo::Prepare()."));
       
  1075 
       
  1076     // Prepare
       
  1077     iTonePlayer->PrepareToPlayTone(iFrequency, iMs);
       
  1078     }
       
  1079 
       
  1080 
       
  1081 // =====================================
       
  1082 // CAknSequenceSoundInfo implementation.
       
  1083 // =====================================
       
  1084 
       
  1085 CAknSequenceSoundInfo::CAknSequenceSoundInfo(TInt aPriority, TInt aPreference)
       
  1086 : CAknSynthSoundInfo(aPriority, aPreference)
       
  1087     {
       
  1088     }
       
  1089 
       
  1090 CAknSequenceSoundInfo::~CAknSequenceSoundInfo()
       
  1091     {
       
  1092     LOGTEXT(_L("CAknSequenceSoundInfo::~CAknSequenceSoundInfo(). Destructor."));
       
  1093 
       
  1094     delete iSequence;
       
  1095     }
       
  1096 
       
  1097 void CAknSequenceSoundInfo::Prepare()
       
  1098     {
       
  1099     LOGTEXT(_L("CAknSequenceSoundInfo::Prepare()."));
       
  1100 
       
  1101     // Prepare
       
  1102     iTonePlayer->PrepareToPlayDesSequence(*iSequence);
       
  1103     }
       
  1104 
       
  1105 void CAknSequenceSoundInfo::ReadSequenceL(RReadStream& aStream)
       
  1106     {
       
  1107     delete iSequence;
       
  1108     iSequence = NULL;
       
  1109 
       
  1110     TInt length = aStream.ReadInt16L();
       
  1111     iSequence = HBufC8::NewMaxL(length);
       
  1112     TPtr8 ptr = iSequence->Des();
       
  1113     for (TInt ii = 0; ii < length; ii++)
       
  1114         {
       
  1115         ptr[ii] = aStream.ReadUint8L();
       
  1116         }
       
  1117     }
       
  1118 
       
  1119 // =================================
       
  1120 // CAknFileSoundInfo implementation.
       
  1121 // =================================
       
  1122 
       
  1123 CAknFileSoundInfo::CAknFileSoundInfo(TInt aPriority, TInt aPreference)
       
  1124 : CEikSoundInfo(aPriority, aPreference)
       
  1125     {
       
  1126     }
       
  1127 
       
  1128 CAknFileSoundInfo::~CAknFileSoundInfo()
       
  1129     {
       
  1130     delete iAudioPlayer;
       
  1131     }
       
  1132 
       
  1133 void CAknFileSoundInfo::InitL(const TDesC& aFileName, CMdaServer* aMdaServer)
       
  1134     {
       
  1135     LOGTEXT(_L("CAknFileSoundInfo::InitL() - Filename:"));
       
  1136     LOGTEXT(aFileName);
       
  1137     iFileName = aFileName;
       
  1138     LOGTEXT(_L(" CAknFileSoundInfo::InitL() - Exit"));
       
  1139     }
       
  1140 
       
  1141 void CAknFileSoundInfo::PlayL()
       
  1142     {
       
  1143     LOGTEXT(_L("CAknFileSoundInfo::PlayL()."));
       
  1144     LOGTEXT3(_L(" This:%d, iPriority:%d, iPreference:%d"), (TInt)this, iPriority, iPreference);
       
  1145 
       
  1146     // Stops playing and deletes audio player instance if it exist.
       
  1147     Stop();
       
  1148 
       
  1149     // Create audio player. DoPlay() will be called in all circumstances.
       
  1150     iAudioPlayer = CMdaAudioPlayerUtility::NewFilePlayerL(iFileName, *this, iPriority,(TMdaPriorityPreference)iPreference );
       
  1151     LOGTEXT(_L(" CAknFileSoundInfo::PlayL() - Exit"));
       
  1152     }
       
  1153 
       
  1154 
       
  1155 void CAknFileSoundInfo::DoPlay()
       
  1156     {
       
  1157     LOGTEXT(_L("CAknFileSoundInfo::DoPlay()."));
       
  1158 
       
  1159     if (iPrepared)
       
  1160         {
       
  1161         LOGTEXT(_L(" CAknFileSoundInfo::DoPlay(). Prepared succesfull, play."));
       
  1162 
       
  1163         // No need to set priority. It is already set in NewDesPlayerReadOnlyL().
       
  1164 
       
  1165         // Set volume.
       
  1166         DoSetVolume(iAudioPlayer);
       
  1167 
       
  1168         LOGTEXT1(_L(" iVolume just before calling play: %d"), iVolume);
       
  1169 
       
  1170         iAudioPlayer->Play();
       
  1171         iPlaying = ETrue;
       
  1172         }
       
  1173     else
       
  1174         {
       
  1175         LOGTEXT(_L(" CAknFileSoundInfo::DoPlay(). Prepare failed, delete!"));
       
  1176         LOGTEXT(_L(" CAknFileSoundInfo::DoPlay(). Deleting iAudioPlayer."));
       
  1177 
       
  1178         delete iAudioPlayer;
       
  1179         iAudioPlayer = NULL;
       
  1180         }
       
  1181     }
       
  1182 
       
  1183 
       
  1184 void CAknFileSoundInfo::Stop()
       
  1185     {
       
  1186     LOGTEXT(_L("CAknFileSoundInfo::Stop()."));
       
  1187 
       
  1188     // Stop playing and delete audio player if it exists.
       
  1189     if (iAudioPlayer)
       
  1190         {
       
  1191         LOGTEXT(_L(" CAknFileSoundInfo::Stop(). iAudioPlayer exists."));
       
  1192 
       
  1193         if (iPlaying)
       
  1194             {
       
  1195             LOGTEXT(_L(" CAknFileSoundInfo::Stop(). Playing, call CancelPlay()."));
       
  1196 
       
  1197             iAudioPlayer->Stop();
       
  1198             iPlaying = EFalse;
       
  1199             }
       
  1200 
       
  1201         LOGTEXT(_L(" CAknFileSoundInfo::Stop(). Deleting iAudioPlayer."));
       
  1202 
       
  1203         delete iAudioPlayer;
       
  1204         iAudioPlayer = NULL;
       
  1205         iPrepared = EFalse;
       
  1206         }
       
  1207     }
       
  1208 
       
  1209 void CAknFileSoundInfo::MoscoStateChangeEvent(CBase* /*aObject*/, TInt /*aPreviousState*/,
       
  1210     TInt /*aCurrentState*/, TInt /*aErrorCode*/)
       
  1211     {
       
  1212     }
       
  1213 
       
  1214 void CAknFileSoundInfo::SetVolume(TVolumeSetting aVolume)
       
  1215     {
       
  1216     iVolume = aVolume;
       
  1217     }
       
  1218 
       
  1219 void CAknFileSoundInfo::DoSetVolume(CMdaAudioPlayerUtility* aAudioPlayer)
       
  1220     {
       
  1221     TInt max = aAudioPlayer->MaxVolume();
       
  1222 
       
  1223     if ( max == 0x0000ffff ) // 16bit -1 i.e. not set
       
  1224         {
       
  1225         max = (TInt)ESoundVolume9; // Set it to our max
       
  1226         }
       
  1227 
       
  1228     TInt volume = 0;
       
  1229 
       
  1230     if ( Preference() != KKeyClickPreference ) // Other sounds than key click
       
  1231         {
       
  1232 		//change (TInt)ESoundVolume9 to ((TInt)ESoundVolume9 + 1)) to keep consistent with audiotheme
       
  1233         aAudioPlayer->SetVolume( ((TInt)iVolume * max )/((TInt)ESoundVolume9 + 1));
       
  1234         return;
       
  1235         }
       
  1236 
       
  1237     switch (iVolume)
       
  1238         {
       
  1239         case EKeypadVolumeOff:
       
  1240             break;
       
  1241         case EKeypadVolumeQuiet:
       
  1242             volume = max / 3;
       
  1243             break;
       
  1244         case EKeypadVolumeMedium:
       
  1245             volume = (max * 2) / 3;
       
  1246             break;
       
  1247         default: //case EKeypadVolumeLoud:
       
  1248             volume = max;
       
  1249             break;
       
  1250         }
       
  1251     aAudioPlayer->SetVolume(volume);
       
  1252     }
       
  1253 
       
  1254 void CAknFileSoundInfo::MapcInitComplete(TInt aError,
       
  1255     const TTimeIntervalMicroSeconds& /*aDuration*/)
       
  1256     {
       
  1257     LOGTEXT(_L("CAknFileSoundInfo::MapcInitComplete()."));
       
  1258     LOGTEXT1(_L(" aError:%d"), aError);
       
  1259 
       
  1260     if (aError == KErrNone)
       
  1261         {
       
  1262         iPrepared = ETrue;
       
  1263         SetVolume(iVolume);
       
  1264         }
       
  1265 
       
  1266     DoPlay();
       
  1267     }
       
  1268 
       
  1269 void CAknFileSoundInfo::MapcPlayComplete(TInt /*aError*/)
       
  1270     {
       
  1271     LOGTEXT(_L("CAknFileSoundInfo::MapcPlayComplete()"));
       
  1272 
       
  1273     iPlaying = EFalse;
       
  1274 
       
  1275     delete iAudioPlayer;
       
  1276     iAudioPlayer = NULL;
       
  1277     iPrepared = EFalse;
       
  1278     }
       
  1279 
       
  1280 // End of file