fmradio/fmradioengine/src/fmradioengine.cpp
branchRCL_3
changeset 20 93c594350b9a
equal deleted inserted replaced
19:cce62ebc198e 20:93c594350b9a
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  The class implements the core functionality of the Radio Engine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <AudioOutput.h>
       
    21 #include <AccMonitor.h>
       
    22 #include <bautils.h>
       
    23 #include <coemain.h>
       
    24 #include <data_caging_path_literals.hrh>
       
    25 
       
    26 #include "fmradioengine.h"
       
    27 #include "fmradioenginestatechangecallback.h"
       
    28 #include "debug.h"
       
    29 #include "fmradioengineradiosettings.h"
       
    30 #include "fmradioengineradiostatehandler.h"
       
    31 #include "fmradioenginecentralrepositoryhandler.h"
       
    32 #include "fmradioenginecrkeys.h"
       
    33 #include "fmradiosystemeventdetector.h"
       
    34 #include "fmradiomobilenetworkinfolistener.h"
       
    35 #include "fmradiopubsub.h"
       
    36 #include "fmradiordsreceiver.h"
       
    37 #include "fmradiordsreceiversimulator.h"
       
    38 #include "fmradioaccessoryconnection.h"
       
    39 
       
    40 #ifndef __ACCESSORY_FW
       
    41 #include "fmradioenginedosserverobserver.h"
       
    42 #endif
       
    43 
       
    44 // CONSTANTS
       
    45 
       
    46 const TInt KFMRadioNumberOfVolumeSteps = 20;
       
    47 
       
    48 // ============================ MEMBER FUNCTIONS ===============================
       
    49 // ----------------------------------------------------
       
    50 // CRadioEngine::CRadioEngine
       
    51 // Default class constructor.
       
    52 // ----------------------------------------------------
       
    53 //
       
    54 CRadioEngine::CRadioEngine(
       
    55     MRadioEngineStateChangeCallback& aCallback)
       
    56     :
       
    57     iCurrentRadioState( EStateRadioOff ),
       
    58     iCallback( aCallback ),
       
    59     iTempFrequency( KDefaultRadioFrequency ),
       
    60     iTunePresetRequested( EFalse ),
       
    61     iInitializeRadioRequestExists( EFalse ),
       
    62     iTunerControl( EStateRadioTunerControlUninitialized )
       
    63     {
       
    64     }
       
    65 
       
    66 // ----------------------------------------------------
       
    67 // CRadioEngine::ConstructL
       
    68 // Second phase class constructor.
       
    69 // ----------------------------------------------------
       
    70 //
       
    71 void CRadioEngine::ConstructL()
       
    72     {
       
    73     FTRACE(FPrint(_L("CRadioEngine::ConstructL()")));
       
    74         
       
    75     InitializeResourceLoadingL(); 
       
    76 
       
    77     iRadioSettings = new ( ELeave ) TRadioSettings;
       
    78     
       
    79     iCentralRepositoryHandler = CCentralRepositoryHandler::NewL( *iRadioSettings );
       
    80         
       
    81 #ifdef __WINS__
       
    82     // NetworkInfoListener takes care of listening to the network id and country code.
       
    83     iNetworkInfoListener = CFMRadioMobileNetworkInfoListener::NewL( *this );
       
    84 #endif
       
    85     
       
    86     // for monitoring network availability
       
    87 	iSystemEventDetector = CFMRadioSystemEventDetector::NewL( *this );
       
    88 	
       
    89     // create an instance of Radio Utility factory and indicate
       
    90     // FM Radio is a primary client
       
    91     iRadioUtility = CRadioUtility::NewL( ETrue );
       
    92 
       
    93     // Get a tuner utility
       
    94     iFmTunerUtility = &iRadioUtility->RadioFmTunerUtilityL( *this );
       
    95     iFmTunerUtility->EnableTunerInOfflineMode( ETrue );
       
    96     
       
    97     // Get a player utility
       
    98     iPlayerUtility = &iRadioUtility->RadioPlayerUtilityL( *this );
       
    99 
       
   100     TInt maxVolume = 0;
       
   101     iPlayerUtility->GetMaxVolume( maxVolume );
       
   102     FTRACE(FPrint(_L("CRadioEngine::ConstructL()  maxVolume %d"), maxVolume ) );
       
   103     // calculate multiplier for scaling the UI volume to the value used by DevSound
       
   104     iUiVolumeFactor = ( static_cast<TReal> ( maxVolume ) / KFMRadioNumberOfVolumeSteps );
       
   105     
       
   106     
       
   107     // Get a preset utility
       
   108     iPresetUtility = CRadioFmPresetUtility::NewL( *this );
       
   109     
       
   110 #ifdef __WINS__
       
   111     iRdsReceiver = CFMRadioRdsReceiverSimulator::NewL( *iRadioSettings );
       
   112     static_cast<CFMRadioRdsReceiverSimulator*> ( iRdsReceiver )->SetRadioEngineForRadioFmTunerSimulation( this );
       
   113 #else
       
   114     iRdsReceiver = CFMRadioRdsReceiver::NewL( *iRadioSettings );
       
   115 #endif
       
   116     
       
   117     iRdsReceiver->AddObserver( this );
       
   118     
       
   119     iStateHandler = CRadioStateHandler::NewL( this );
       
   120 
       
   121 #ifndef __ACCESSORY_FW
       
   122     iDosServerObserver = CDosServerObserver::NewL( this, iRadioSettings );
       
   123 #else
       
   124 #ifndef __WINS
       
   125     TRAP_IGNORE( iAudioOutput = CAudioOutput::NewL( *iPlayerUtility ) );
       
   126 #endif
       
   127 #endif
       
   128     
       
   129     iTopFrequency = 0;
       
   130     iBottomFrequency = 0;
       
   131 
       
   132     if ( iSystemEventDetector->IsCallActive() )
       
   133         {
       
   134         // Pre-empted due to phone call
       
   135         iInCall = ETrue;
       
   136         }
       
   137     // accessory observer
       
   138     iHeadsetObserver = CFMRadioAccessoryConnection::NewL();
       
   139     iHeadsetObserver->SetObserver( this );    
       
   140     // Set audio output to current setting
       
   141     if ( iHeadsetObserver->WiredHeadsetConnected() )
       
   142         {
       
   143         SetAudioOutput( EFMRadioOutputHeadset );
       
   144         }
       
   145     else
       
   146         {
       
   147         SetAudioOutput( EFMRadioOutputIHF );
       
   148         }
       
   149     // Assume headset is connected, we'll be notified later if it's not.
       
   150     iRadioSettings->SetHeadsetConnected();
       
   151     
       
   152 
       
   153     // Create P&S interface.
       
   154     iPubSub = CFMRadioPubSub::NewL();
       
   155     
       
   156     // Publish antenna status as connected, we'll be notified later if it's not.
       
   157     iPubSub->PublishAntennaStatusL( EFMRadioPSHeadsetConnected );
       
   158     
       
   159     iPubSub->PublishFrequencyDecimalCountL( 
       
   160             TFMRadioPSFrequencyDecimalCount( iRadioSettings->DecimalCount() ) );
       
   161 
       
   162     FTRACE(FPrint(_L("CRadioEngine::ConstructL() End ")));
       
   163     }
       
   164 
       
   165 // ----------------------------------------------------
       
   166 // CRadioEngine::NewL
       
   167 // Two-phased class constructor.
       
   168 // ----------------------------------------------------
       
   169 //
       
   170 EXPORT_C CRadioEngine* CRadioEngine::NewL(
       
   171     MRadioEngineStateChangeCallback& aCallback )
       
   172     {
       
   173     CRadioEngine* self = new (ELeave) CRadioEngine( aCallback );
       
   174     CleanupStack::PushL(self);
       
   175     self->ConstructL();
       
   176     CleanupStack::Pop();
       
   177     return self;
       
   178     }
       
   179 
       
   180 // ----------------------------------------------------
       
   181 // CRadioEngine::~CRadioEngine
       
   182 // Destructor of CRadioEngine class.
       
   183 // ----------------------------------------------------
       
   184 ////
       
   185 EXPORT_C CRadioEngine::~CRadioEngine()
       
   186     {
       
   187     FTRACE(FPrint(_L("CRadioEngine::~CRadioEngine()")));
       
   188     if ( iCentralRepositoryHandler) 
       
   189         {
       
   190         iCentralRepositoryHandler->SaveEngineSettings();
       
   191         delete iCentralRepositoryHandler;
       
   192         }
       
   193         
       
   194     delete iPubSub;
       
   195     delete iHeadsetObserver;
       
   196 	
       
   197 	RadioOff();
       
   198 	        
       
   199 	delete iNetworkInfoListener;     
       
   200 	delete iSystemEventDetector;      
       
   201     delete iPresetUtility;
       
   202     delete iRadioSettings;
       
   203     delete iStateHandler;
       
   204 
       
   205     if ( iAudioOutput )
       
   206         {
       
   207         TRAP_IGNORE(iAudioOutput->SetAudioOutputL(CAudioOutput::EPrivate));
       
   208         delete iAudioOutput;
       
   209         }
       
   210 
       
   211     if ( iRdsReceiver )
       
   212         {
       
   213         iRdsReceiver->RemoveObserver( this );
       
   214         }
       
   215 
       
   216     delete iRdsReceiver;
       
   217     
       
   218     if ( iFmTunerUtility )
       
   219         {
       
   220         iFmTunerUtility->Close();
       
   221         }
       
   222 
       
   223     if ( iPlayerUtility )
       
   224         {
       
   225         iPlayerUtility->Close();
       
   226         }
       
   227 
       
   228     delete iRadioUtility;
       
   229 
       
   230     #ifndef __ACCESSORY_FW
       
   231     if (iDosServerObserver)
       
   232         {
       
   233         iDosServerObserver->SetAudioRouting(EFMRadioOutputHeadset);
       
   234         delete iDosServerObserver;
       
   235         }
       
   236     #endif
       
   237         
       
   238 	if ( iFMRadioEngineResourceOffset > 0 )
       
   239         {
       
   240         CCoeEnv::Static()->DeleteResourceFile( iFMRadioEngineResourceOffset );
       
   241         }
       
   242     }
       
   243 
       
   244 // ----------------------------------------------------
       
   245 // CRadioEngine::InitializeRadio
       
   246 // Set radio settings to their default values.
       
   247 // Returns: None
       
   248 // ----------------------------------------------------
       
   249 //
       
   250 EXPORT_C void CRadioEngine::InitializeRadio()
       
   251       {
       
   252       FTRACE(FPrint(_L("CRadioEngine::InitializeRadio()")));
       
   253       iInitializeRadioRequestExists = ETrue;
       
   254       Tune( iRadioSettings->Frequency() );
       
   255 #ifdef __WINS__
       
   256       // MrftoRequestTunerControlComplete never completes in WINS, so initialize RdsReceiver here
       
   257       TRAP_IGNORE( iRdsReceiver->InitL( *iRadioUtility, iPubSub ) );
       
   258 #endif
       
   259       }
       
   260 
       
   261 // ----------------------------------------------------
       
   262 // CRadioEngine::RadioOn
       
   263 // Start the radio.
       
   264 // ----------------------------------------------------
       
   265 //
       
   266 EXPORT_C void CRadioEngine::RadioOn()
       
   267     {
       
   268     FTRACE(FPrint(_L("CRadioEngine::RadioOn()")));
       
   269     if ( !iRadioSettings->IsRadioOn() )
       
   270         {
       
   271         FTRACE(FPrint(_L("CRadioEngine::RadioOn() - Turning Radio on, calling play")));
       
   272         iRadioSettings->SetRadioOn();
       
   273         iPlayerUtility->Play();
       
   274         
       
   275         if ( iPubSub )
       
   276             {
       
   277             TRAP_IGNORE( iPubSub->PublishStateL( ETrue ) );
       
   278             }
       
   279        }
       
   280     }
       
   281 
       
   282 // ----------------------------------------------------
       
   283 // CRadioEngine::RadioOff
       
   284 // Stops the radio.
       
   285 // ----------------------------------------------------
       
   286 //
       
   287 EXPORT_C void CRadioEngine::RadioOff()
       
   288     {
       
   289     FTRACE(FPrint(_L("CRadioEngine::RadioOff()")));
       
   290     if ( iStateHandler )
       
   291         {
       
   292         iStateHandler->Cancel();
       
   293         }
       
   294 
       
   295     iCurrentRadioState = EStateRadioOff;
       
   296 
       
   297     if( iFmTunerUtility &&
       
   298         iRadioSettings &&
       
   299         iRadioSettings->RadioMode() == ERadioTunerMode )
       
   300         {
       
   301         CancelScan();
       
   302         }
       
   303 
       
   304     if ( iPlayerUtility &&
       
   305          iPlayerUtility->PlayerState() == ERadioPlayerPlaying )
       
   306         {
       
   307         iPlayerUtility->Stop();
       
   308         }
       
   309     
       
   310     if ( iPubSub )
       
   311         {
       
   312         TRAP_IGNORE( iPubSub->PublishStateL( EFalse ) );
       
   313         }
       
   314     }
       
   315 
       
   316 // ----------------------------------------------------
       
   317 // CRadioEngine::IsRadioOn
       
   318 // Returns the radio status.
       
   319 // ----------------------------------------------------
       
   320 //
       
   321 EXPORT_C TBool CRadioEngine::IsRadioOn()
       
   322 	{
       
   323 	return iRadioSettings->IsRadioOn();
       
   324 	}
       
   325 
       
   326 // ----------------------------------------------------
       
   327 // CRadioEngine::IsOfflineProfileL
       
   328 // Checks if offine profile is current active profile
       
   329 // Returns: true/false 
       
   330 // ----------------------------------------------------
       
   331 //
       
   332 EXPORT_C TBool CRadioEngine::IsOfflineProfileL()
       
   333 	{
       
   334 	return iCentralRepositoryHandler->IsOfflineProfileActiveL();
       
   335 	}
       
   336 
       
   337 // ----------------------------------------------------
       
   338 // CRadioEngine::Tune
       
   339 // Tune to the specified frequency
       
   340 // ----------------------------------------------------
       
   341 //
       
   342 EXPORT_C void CRadioEngine::Tune( TInt aFrequency, TRadioMode aRadioMode )
       
   343     {
       
   344     FTRACE(FPrint(_L("CRadioEngine::Tune()")));
       
   345     iTempFrequency = aFrequency;
       
   346 
       
   347     iRadioSettings->SetRadioMode( aRadioMode );
       
   348     if ( iTunerControl == EStateRadioTunerControlOn && iRadioSettings->IsHeadsetConnected() )
       
   349         {
       
   350         iFmTunerUtility->SetFrequency( aFrequency );
       
   351         }
       
   352     else
       
   353         {
       
   354         StoreAndPublishFrequency( aFrequency );
       
   355         }
       
   356     if ( iPubSub )
       
   357     	{
       
   358         TRAP_IGNORE
       
   359             (
       
   360             iPubSub->PublishFrequencyL( aFrequency );
       
   361             if( aRadioMode == ERadioTunerMode )
       
   362                 {
       
   363                 iPubSub->PublishChannelNameL( KNullDesC );
       
   364                 iPubSub->PublishChannelL( KErrNotFound );
       
   365                 }
       
   366             )
       
   367     	}
       
   368     }
       
   369 
       
   370 // ----------------------------------------------------
       
   371 // CRadioEngine::SetTunerModeOn
       
   372 // ----------------------------------------------------
       
   373 //
       
   374 EXPORT_C void CRadioEngine::SetTunerModeOn()
       
   375     {
       
   376     FTRACE(FPrint(_L("CRadioEngine::SetTunerModeOn()")));
       
   377     
       
   378     iRadioSettings->SetRadioMode( ERadioTunerMode );
       
   379     
       
   380     if ( iPubSub )
       
   381         {
       
   382         TRAP_IGNORE
       
   383             (
       
   384             iPubSub->PublishChannelNameL( KNullDesC );
       
   385             iPubSub->PublishChannelL( KErrNotFound );
       
   386             )
       
   387         }
       
   388     }
       
   389 
       
   390 // ----------------------------------------------------
       
   391 // CRadioEngine::ScanUp
       
   392 // Scan up to the next available frequency
       
   393 // ----------------------------------------------------
       
   394 //
       
   395 EXPORT_C void CRadioEngine::ScanUp()
       
   396     {
       
   397     iRadioSettings->SetRadioMode(ERadioTunerMode);
       
   398     iFmTunerUtility->StationSeek( ETrue );
       
   399     TRAP_IGNORE
       
   400         (
       
   401         iTuningState = EFMRadioPSTuningStarted;
       
   402         iPubSub->PublishTuningStateL( iTuningState );
       
   403         iPubSub->PublishChannelNameL( KNullDesC );
       
   404         iPubSub->PublishChannelL( KErrNotFound );
       
   405         )
       
   406     }
       
   407 
       
   408 // ----------------------------------------------------
       
   409 // CRadioEngine::ScanDown
       
   410 // Scan down to the last available frequency
       
   411 // ----------------------------------------------------
       
   412 //
       
   413 EXPORT_C void CRadioEngine::ScanDown()
       
   414     {
       
   415     iRadioSettings->SetRadioMode(ERadioTunerMode);
       
   416     iFmTunerUtility->StationSeek( EFalse );
       
   417     TRAP_IGNORE
       
   418         (
       
   419         iTuningState = EFMRadioPSTuningStarted;
       
   420         iPubSub->PublishTuningStateL( iTuningState );
       
   421         iPubSub->PublishChannelNameL( KNullDesC );
       
   422         iPubSub->PublishChannelL( KErrNotFound );
       
   423         )
       
   424     }
       
   425 
       
   426 // ----------------------------------------------------
       
   427 // CRadioEngine::CancelScan
       
   428 // Cancel previously requested scan, and return to the
       
   429 // already tuned frequency
       
   430 // ----------------------------------------------------
       
   431 //
       
   432 EXPORT_C void CRadioEngine::CancelScan()
       
   433     {
       
   434     if( iFmTunerUtility )
       
   435         {
       
   436         iFmTunerUtility->CancelStationSeek();
       
   437         iTuningState = EFMRadioPSTuningUninitialized;
       
   438         TRAP_IGNORE(iPubSub->PublishTuningStateL( iTuningState ));
       
   439         }
       
   440     }
       
   441 
       
   442 // ----------------------------------------------------
       
   443 // CRadioEngine::IsHeadsetConnected
       
   444 // Check whether the headset is currently connected
       
   445 // Returns: true/false
       
   446 // ----------------------------------------------------
       
   447 //
       
   448 EXPORT_C TBool CRadioEngine::IsHeadsetConnected() const
       
   449     {
       
   450     return iRadioSettings->IsHeadsetConnected();
       
   451     }
       
   452 
       
   453 // ----------------------------------------------------
       
   454 // CRadioEngine::IsFlightmodeEnabled
       
   455 // Check whether flight mode is currently enabled
       
   456 // Returns: true/false
       
   457 // ----------------------------------------------------
       
   458 //
       
   459 EXPORT_C TBool CRadioEngine::IsFlightModeEnabled() const
       
   460     {
       
   461     return iRadioSettings->IsFlightModeEnabled();
       
   462     }
       
   463 
       
   464 // ----------------------------------------------------
       
   465 // CRadioEngine::SetMuteOn
       
   466 // Sets the audio mute state
       
   467 // Returns: None
       
   468 // ----------------------------------------------------
       
   469 //
       
   470 EXPORT_C void CRadioEngine::SetMuteOn(
       
   471     TBool aMuteOn)  // a flag indicating if set mute on
       
   472     {
       
   473     FTRACE(FPrint(_L("CRadioEngine::SetMuteOn()")));
       
   474     TInt error = KErrNone;
       
   475     if ( IsMuteOn()  && (!aMuteOn) )
       
   476         {
       
   477         error = iPlayerUtility->Mute( EFalse );
       
   478         if( !error )
       
   479             {
       
   480             FTRACE(FPrint(_L("CRadioEngine::SetMuteOn() setting mute off")));
       
   481             iRadioSettings->SetMuteOff();
       
   482             TRAP_IGNORE(iPubSub->PublishRadioMuteStateL(EFalse));
       
   483             }
       
   484         }
       
   485     else if ( !IsMuteOn() && aMuteOn )
       
   486         {
       
   487         error = iPlayerUtility->Mute( ETrue );
       
   488         if( !error )
       
   489             {
       
   490             FTRACE(FPrint(_L("CRadioEngine::SetMuteOn() setting mute on")));
       
   491             iRadioSettings->SetMuteOn();
       
   492             TRAP_IGNORE(iPubSub->PublishRadioMuteStateL(ETrue));
       
   493             }
       
   494         }
       
   495     iStateHandler->Callback( MRadioEngineStateChangeCallback::EFMRadioEventSetMuteState, error );
       
   496     }
       
   497 
       
   498 
       
   499 // ----------------------------------------------------
       
   500 // CRadioEngine::IsMuteOn
       
   501 // Gets the audio mute state
       
   502 // Returns: true/false
       
   503 // ----------------------------------------------------
       
   504 //
       
   505 EXPORT_C TBool CRadioEngine::IsMuteOn() const
       
   506     {
       
   507     return iRadioSettings->IsMuteOn();
       
   508     }
       
   509 
       
   510 // ----------------------------------------------------
       
   511 // CRadioEngine::GetPresetIndex
       
   512 // Retrieve the currently selected channel
       
   513 // Returns: the preset channel index
       
   514 // ----------------------------------------------------
       
   515 //
       
   516 EXPORT_C TInt CRadioEngine::GetPresetIndex() const
       
   517     {
       
   518     return iRadioSettings->CurrentPreset();
       
   519     }
       
   520 
       
   521 
       
   522 // ----------------------------------------------------
       
   523 // CRadioEngine::SetPresetFrequency
       
   524 // Update the channel located at the index to use the frequency specified.
       
   525 // Returns: None
       
   526 // ----------------------------------------------------
       
   527 //
       
   528 EXPORT_C void CRadioEngine::SetPresetNameFrequencyL(
       
   529     TInt aIndex, // preset channel index
       
   530     const TStationName& aStationName,
       
   531     TInt aFrequency) // preset channel frequency
       
   532     {
       
   533     iPresetUtility->SetPresetL(aIndex, aStationName, aFrequency);
       
   534     if( aIndex == iRadioSettings->CurrentPreset() )
       
   535         {
       
   536         iPubSub->PublishChannelNameL( aStationName );
       
   537         if( aFrequency == 0 )
       
   538             {
       
   539             // Current preset was deleted
       
   540             iPubSub->PublishChannelL( KErrNotFound );
       
   541             }
       
   542         }
       
   543     iPubSub->PublishChannelDataChangedL( aIndex );
       
   544     }
       
   545 
       
   546 // ----------------------------------------------------
       
   547 // CRadioEngine::GetPresetFrequency
       
   548 // Retrieve the channel frequency at the index specified
       
   549 // Returns: tmpFrequency: preset frequency
       
   550 // ----------------------------------------------------
       
   551 //
       
   552 EXPORT_C TInt CRadioEngine::GetPresetFrequencyL(
       
   553     TInt aIndex) const  // preset channel index
       
   554     {
       
   555     TInt tmpFrequency = KDefaultRadioFrequency;
       
   556     TStationName tmpStationName;
       
   557 
       
   558     iPresetUtility->GetPresetL( aIndex, tmpStationName, tmpFrequency );
       
   559 
       
   560     FTRACE(FPrint(_L("CRadioEngine::GetPresetFrequencyL()  aIndex %d"), aIndex));
       
   561     FTRACE(FPrint(_L("CRadioEngine::GetPresetFrequencyL()  Freq %d"), tmpFrequency));
       
   562     FTRACE(FPrint(_L("CRadioEngine::GetPresetFrequencyL()  Name %S"), &tmpStationName));
       
   563     return tmpFrequency;
       
   564     }
       
   565 
       
   566 // ----------------------------------------------------
       
   567 // CRadioEngine::GetPresetName
       
   568 // Retrieve the channel name at the index specified
       
   569 // Returns: tmpStationName: the channel name
       
   570 // ----------------------------------------------------
       
   571 //
       
   572 EXPORT_C CRadioEngine::TStationName& CRadioEngine::GetPresetNameL( TInt aIndex )
       
   573 	{		
       
   574     TInt tmpFrequency;
       
   575 
       
   576     iPresetUtility->GetPresetL(aIndex, iStationName, tmpFrequency);
       
   577     FTRACE(FPrint(_L("CRadioEngine::GetPresetFrequencyL()  Freq %d"),tmpFrequency));
       
   578     FTRACE(FPrint(_L("CRadioEngine::GetPresetFrequencyL()  Name %S"),&iStationName));
       
   579     return iStationName; 
       
   580     }
       
   581 
       
   582 // ----------------------------------------------------
       
   583 // CRadioEngine::GetPresetNameAndFrequencyL
       
   584 // get preset name frequency from the preset utility
       
   585 // ----------------------------------------------------
       
   586 //
       
   587 EXPORT_C void CRadioEngine::GetPresetNameAndFrequencyL( TInt aIndex, TDes& aName, TInt& aFrequency )
       
   588     {
       
   589     TInt tmpFrequency;
       
   590     TStationName tmpStationName;
       
   591     
       
   592     iPresetUtility->GetPresetL( aIndex, tmpStationName, tmpFrequency );
       
   593 
       
   594     aFrequency = tmpFrequency;
       
   595     aName = tmpStationName;
       
   596     }
       
   597 
       
   598 // ----------------------------------------------------
       
   599 // CRadioEngine::GetTunedFrequency
       
   600 // Retrieves the current frequency.
       
   601 // Returns: channel frequency
       
   602 // ----------------------------------------------------
       
   603 //
       
   604 EXPORT_C TInt CRadioEngine::GetTunedFrequency() const
       
   605     {
       
   606     return iRadioSettings->Frequency();
       
   607     }
       
   608 
       
   609 // ----------------------------------------------------
       
   610 // CRadioEngine::SetVolume
       
   611 // Sets the volume level of the FM radio
       
   612 // ----------------------------------------------------
       
   613 //
       
   614 EXPORT_C void CRadioEngine::SetVolume(
       
   615     TInt aVolume )
       
   616     {
       
   617     FTRACE(FPrint(_L("CRadioEngine::SetVolume( %d )"), aVolume ));
       
   618     
       
   619     if ( aVolume < 0 )
       
   620         {
       
   621         aVolume = 0;
       
   622         }
       
   623     else if ( aVolume > KFMRadioNumberOfVolumeSteps )
       
   624         {
       
   625         aVolume = KFMRadioNumberOfVolumeSteps;
       
   626         }
       
   627     
       
   628     TInt playerVolume = ( iUiVolumeFactor * aVolume );
       
   629     
       
   630     FTRACE(FPrint(_L("CRadioEngine::SetVolume() playerVolume %d"), playerVolume ));
       
   631     
       
   632     TInt error = iPlayerUtility->SetVolume( playerVolume );
       
   633     
       
   634     if ( !error )
       
   635         {
       
   636         if ( iRadioSettings->AudioOutput() == EFMRadioOutputHeadset )
       
   637             {
       
   638             iRadioSettings->SetHeadsetVolume( aVolume );
       
   639             iCentralRepositoryHandler->SaveHeadsetVolumeSetting();
       
   640             }
       
   641         else
       
   642             {
       
   643              iRadioSettings->SetSpeakerVolume( aVolume );
       
   644              iCentralRepositoryHandler->SaveSpeakerVolumeSetting();
       
   645             }
       
   646         }
       
   647     else
       
   648         {
       
   649         iStateHandler->Callback( MRadioEngineStateChangeCallback::EFMRadioEventVolumeUpdated, error );
       
   650         }
       
   651     }
       
   652 
       
   653 // ----------------------------------------------------
       
   654 // CRadioEngine::GetRadioMode
       
   655 // Retrieves the current radio mode (tuner or preset).
       
   656 // Returns: radio mode
       
   657 // ----------------------------------------------------
       
   658 //
       
   659 EXPORT_C CRadioEngine::TRadioMode CRadioEngine::GetRadioMode() const
       
   660     {
       
   661     return iRadioSettings->RadioMode();
       
   662     }
       
   663 
       
   664 // ----------------------------------------------------
       
   665 // CRadioEngine::GetVolume
       
   666 // Gets the volumelevel.
       
   667 // Returns: volume
       
   668 // ----------------------------------------------------
       
   669 //
       
   670 EXPORT_C TInt CRadioEngine::GetVolume() const
       
   671     {
       
   672     if ( iRadioSettings->AudioOutput() == EFMRadioOutputHeadset )
       
   673         {
       
   674         return iRadioSettings->HeadsetVolume();
       
   675         }
       
   676     else
       
   677         {
       
   678         return iRadioSettings->SpeakerVolume();
       
   679         }
       
   680     }
       
   681 
       
   682 // ----------------------------------------------------
       
   683 // CRadioEngine::GetMaxVolume
       
   684 // Gets the max volumelevel.
       
   685 // Returns: max volume
       
   686 // ----------------------------------------------------
       
   687 //
       
   688 EXPORT_C TInt CRadioEngine::GetMaxVolume() const
       
   689     {
       
   690     TInt maxVolume;
       
   691     iPlayerUtility->GetMaxVolume( maxVolume );
       
   692     return maxVolume;
       
   693     }
       
   694 
       
   695 // ----------------------------------------------------
       
   696 // CRadioEngine::GetAudioMode
       
   697 // Gets the current audio mode.
       
   698 // ----------------------------------------------------
       
   699 //
       
   700 EXPORT_C TInt CRadioEngine::GetAudioMode() const
       
   701     {
       
   702     return iRadioSettings->AudioMode();
       
   703     }
       
   704 
       
   705 // ----------------------------------------------------
       
   706 // CRadioEngine::SetAudioMode
       
   707 // Set the radio's audio mode (stereo or mono)
       
   708 // ----------------------------------------------------
       
   709 //
       
   710 EXPORT_C void CRadioEngine::SetAudioMode( const TFMRadioAudioMode aAudioMode )
       
   711     {
       
   712     FTRACE(FPrint(_L("CRadioEngine::SetAudioMode")));
       
   713     TBool forcedMono = EFalse;
       
   714     TInt res = KErrNone;
       
   715     TFMRadioAudioMode audioMode = iRadioSettings->AudioMode();
       
   716     if (audioMode != aAudioMode)
       
   717         {
       
   718         if ( aAudioMode == EFMRadioMono )
       
   719             {
       
   720             forcedMono = ETrue;
       
   721             }
       
   722         res = iFmTunerUtility->ForceMonoReception( forcedMono );
       
   723         if ( res )
       
   724             {
       
   725             iRadioSettings->SetAudioMode( aAudioMode );
       
   726             }
       
   727         }
       
   728     iStateHandler->Callback( MRadioEngineStateChangeCallback::EFMRadioEventSetAudioMode, res );
       
   729     }
       
   730 
       
   731 // ----------------------------------------------------
       
   732 // CRadioEngine::GetAudioOutput
       
   733 // Retrieve the current audio output
       
   734 // Returns: audio output
       
   735 // ----------------------------------------------------
       
   736 //
       
   737 EXPORT_C CRadioEngine::TFMRadioAudioOutput CRadioEngine::GetAudioOutput() const
       
   738     {
       
   739     return iRadioSettings->AudioOutput();
       
   740     }
       
   741 
       
   742 // ----------------------------------------------------
       
   743 // CRadioEngine::SetAudioOutput
       
   744 // Sets the audio Output
       
   745 // Returns: None
       
   746 // ----------------------------------------------------
       
   747 //
       
   748 EXPORT_C void CRadioEngine::SetAudioOutput( const TFMRadioAudioOutput aAudioOutput )
       
   749     {
       
   750     FTRACE(FPrint(_L("CRadioEngine::SetAudioOutput() Output = %d"), aAudioOutput));
       
   751     TInt tempError = KErrNone;
       
   752     // use mute here to avoid any audio peaks during output change
       
   753     SetMuteOn( ETrue );
       
   754 
       
   755 #ifndef __ACCESSORY_FW
       
   756 
       
   757     tempError = iDosServerObserver->SetAudioRouting(aAudioOutput);
       
   758     FTRACE(FPrint(_L("CRadioEngine::SetAudioOutput() tempError = %d"), tempError));
       
   759 
       
   760 #else
       
   761 
       
   762     CAudioOutput::TAudioOutputPreference outputPreference = CAudioOutput::EPrivate;
       
   763 
       
   764     if ( EFMRadioOutputHeadset == aAudioOutput )
       
   765         {
       
   766         FTRACE(FPrint(_L("CRadioEngine::SetAudioOutput() Headset is set to Output")));
       
   767         outputPreference = CAudioOutput::EPrivate;
       
   768         }
       
   769     else if ( EFMRadioOutputIHF == aAudioOutput )
       
   770         {
       
   771         FTRACE(FPrint(_L("CRadioEngine::SetAudioOutput() IHF is set to Output")));
       
   772         
       
   773         outputPreference = CAudioOutput::EPublic;
       
   774         }
       
   775 
       
   776 #ifndef __WINS
       
   777     if ( iAudioOutput )
       
   778         {
       
   779         TRAP( tempError, iAudioOutput->SetAudioOutputL( outputPreference ) );
       
   780         }
       
   781 #endif
       
   782 
       
   783 #endif
       
   784 
       
   785     if ( KErrNone == tempError )
       
   786         {        
       
   787         if ( !iHeadsetObserver->WiredHeadsetConnected() )
       
   788         	{
       
   789         	iRadioSettings->SetAudioOutput( EFMRadioOutputIHF );
       
   790         	}
       
   791         else
       
   792         	{
       
   793         	iRadioSettings->SetAudioOutput( aAudioOutput );
       
   794         	}
       
   795         }
       
   796 
       
   797     iStateHandler->Callback( MRadioEngineStateChangeCallback::EFMRadioEventSetAudioOutput, tempError );
       
   798     }
       
   799 
       
   800 // ----------------------------------------------------
       
   801 // CRadioEngine::SetRdsAfSearchEnable
       
   802 // Sets the rds af search enabled
       
   803 // Returns: None
       
   804 // ----------------------------------------------------
       
   805 //
       
   806 EXPORT_C void CRadioEngine::SetRdsAfSearchEnable( TBool aEnabled )
       
   807 	{
       
   808 	TRAP_IGNORE( iRdsReceiver->SetAutomaticSwitchingL( aEnabled ) );	
       
   809 	aEnabled ? iRadioSettings->SetRdsAfSearchEnabled() : iRadioSettings->SetRdsAfSearchDisabled();
       
   810 	}
       
   811 
       
   812 // ----------------------------------------------------
       
   813 // CRadioEngine::RadioSettings
       
   814 // ----------------------------------------------------
       
   815 //
       
   816 EXPORT_C TRadioSettings& CRadioEngine::RadioSettings( )
       
   817 	{
       
   818 	return *iRadioSettings;
       
   819 	}
       
   820 
       
   821 // ----------------------------------------------------
       
   822 // CRadioEngine::TunePreset
       
   823 // Tunes to the chosen channel.
       
   824 // Returns: None
       
   825 // ----------------------------------------------------
       
   826 //
       
   827 EXPORT_C void CRadioEngine::TunePresetL( TInt aIndex )
       
   828     {
       
   829     FTRACE(FPrint(_L("CRadioEngine::TunePresetL()")));
       
   830     iRadioSettings->SetCurrentPreset( aIndex );
       
   831     TInt tmpFrequency = 0;// changed back to zero
       
   832     TStationName name;
       
   833 
       
   834     iPresetUtility->GetPresetL( aIndex, name, tmpFrequency );
       
   835     // Should not tune if the radio is not on.
       
   836     if ( iRadioSettings->IsRadioOn())
       
   837         {
       
   838         FTRACE(FPrint(_L("CRadioEngine::TunePresetL() inside if")));
       
   839         iTunePresetRequested = ETrue;
       
   840         iTempFrequency = tmpFrequency;
       
   841         Tune(tmpFrequency, ERadioPresetMode);
       
   842         }
       
   843     else
       
   844         {
       
   845         StoreAndPublishFrequency( tmpFrequency );
       
   846         }
       
   847     iPubSub->PublishChannelNameL( name );
       
   848     iPubSub->PublishChannelL( aIndex );
       
   849     iRadioSettings->SetRadioMode(ERadioPresetMode);
       
   850     FTRACE(FPrint(_L("CRadioEngine::TunePresetL() end %d"),tmpFrequency));
       
   851     }
       
   852 
       
   853 // ----------------------------------------------------
       
   854 // CRadioEngine::5
       
   855 // Return min and max freq for the specified range (band)
       
   856 // ----------------------------------------------------
       
   857 //
       
   858 EXPORT_C TInt CRadioEngine::GetFrequencyBandRange(
       
   859     TInt& aBottomFrequency,
       
   860     TInt& aTopFrequency)
       
   861     {
       
   862     TBool err = KErrNone;
       
   863     
       
   864     aBottomFrequency = iCentralRepositoryHandler->MinFrequency() * KFMRadioFreqMultiplier;
       
   865 	aTopFrequency = iCentralRepositoryHandler->MaxFrequency() * KFMRadioFreqMultiplier;
       
   866 
       
   867     return err;
       
   868     }
       
   869 
       
   870 // ----------------------------------------------------
       
   871 // CRadioEngine::IsInCall
       
   872 // ----------------------------------------------------
       
   873 //
       
   874 EXPORT_C TBool CRadioEngine::IsInCall() const
       
   875     {
       
   876     return iInCall;
       
   877     }
       
   878 
       
   879 // ---------------------------------------------------------
       
   880 // CRadioEngine::FrequencySetByRdsAf
       
   881 // ---------------------------------------------------------
       
   882 //
       
   883 EXPORT_C TBool CRadioEngine::FrequencySetByRdsAf() const
       
   884     {
       
   885     return iFrequencySetByRdsAf;
       
   886     }
       
   887 
       
   888 // ---------------------------------------------------------
       
   889 // CRadioEngine::RdsReceiver
       
   890 // ---------------------------------------------------------
       
   891 //
       
   892 EXPORT_C CFMRadioRdsReceiverBase& CRadioEngine::RdsReceiver()
       
   893     {
       
   894     return *iRdsReceiver;
       
   895     }
       
   896 
       
   897 // ----------------------------------------------------
       
   898 // CRadioEngine::HandleFlightModeEnabled
       
   899 // Switches the engine into flight mode, and notifies the UI of
       
   900 // the change in status.
       
   901 // ----------------------------------------------------
       
   902 //
       
   903 void CRadioEngine::HandleFlightModeEnabled()
       
   904     {
       
   905     FTRACE(FPrint(_L("CRadioEngine::HandleFlightModeEnabled()")));
       
   906     iRadioSettings->SetFlightMode(ETrue);
       
   907     HandleCallback(MRadioEngineStateChangeCallback::EFMRadioEventFlightModeEnabled, KErrNone);
       
   908     }
       
   909 
       
   910 // ----------------------------------------------------
       
   911 // CRadioEngine::HandleFlightModeDisabled
       
   912 // Switches the engine out of flight mode, and notifies the UI of
       
   913 // the change in status
       
   914 // ----------------------------------------------------
       
   915 //
       
   916 void CRadioEngine::HandleFlightModeDisabled()
       
   917     {
       
   918     FTRACE(FPrint(_L("CRadioEngine::HandleFlightModeDisabled()")));
       
   919     iRadioSettings->SetFlightMode(EFalse);
       
   920     HandleCallback(MRadioEngineStateChangeCallback::EFMRadioEventFlightModeDisabled, KErrNone);
       
   921     }
       
   922 
       
   923 
       
   924 // ----------------------------------------------------
       
   925 // CRadioEngine::HandleCallback
       
   926 // Calls the User Interface callback function, indicating the event code and
       
   927 // error code.
       
   928 // ----------------------------------------------------
       
   929 //
       
   930 void CRadioEngine::HandleCallback(TInt aEventCode, TInt aCode )
       
   931     {
       
   932     FTRACE(FPrint(_L("CRadioEngine::HandleCallback(aEventCode %d, aCode %d "), aEventCode, aCode ) );
       
   933     iCallback.HandleRadioEngineCallBack((MRadioEngineStateChangeCallback::TFMRadioNotifyEvent)aEventCode, aCode);
       
   934     }
       
   935 
       
   936 // ----------------------------------------------------
       
   937 // CRadioEngine::HandleHeadsetButtonPress
       
   938 // Notifies UI that the headset button has been pressed
       
   939 // ----------------------------------------------------
       
   940 //
       
   941 void CRadioEngine::HandleHeadsetButtonPress()
       
   942     {
       
   943     FTRACE(FPrint(_L("CRadioEngine::HandleHeadsetButtonPress()")));
       
   944     iStateHandler->Callback(MRadioEngineStateChangeCallback::EFMRadioEventButtonPressed, KErrNone);
       
   945     }
       
   946 
       
   947 // ----------------------------------------------------
       
   948 // CRadioEngine::HeadsetAccessoryConnectedCallbackL
       
   949 // ----------------------------------------------------
       
   950 //
       
   951 void CRadioEngine::HeadsetAccessoryConnectedCallbackL()
       
   952     {
       
   953     FTRACE(FPrint(_L("CRadioEngine::HeadsetAccessoryConnectedCallbackL")));
       
   954     // forward volume changes to headset
       
   955     SetAudioOutput( EFMRadioOutputHeadset );
       
   956     }
       
   957 
       
   958 // ----------------------------------------------------
       
   959 // CRadioEngine::HeadsetAccessoryDisconnectedCallbackL
       
   960 // ----------------------------------------------------
       
   961 //
       
   962 void CRadioEngine::HeadsetAccessoryDisconnectedCallbackL()
       
   963     {
       
   964     FTRACE(FPrint(_L("CRadioEngine::HeadsetAccessoryDisconnectedCallbackL")));
       
   965     // forward volume changes to speakers
       
   966     SetAudioOutput( EFMRadioOutputIHF );
       
   967     }
       
   968 
       
   969 // ----------------------------------------------------
       
   970 // CRadioEngine::MrftoRequestTunerControlComplete
       
   971 // ----------------------------------------------------
       
   972 //
       
   973 void CRadioEngine::MrftoRequestTunerControlComplete( TInt aError )
       
   974     {
       
   975     FTRACE(FPrint(_L("CRadioEngine::MrftoRequestTunerControlComplete() - Error = %d"), aError));
       
   976 
       
   977     if ( aError == KErrNone )
       
   978         {
       
   979         iTunerControl = EStateRadioTunerControlOn;
       
   980         // Headset must be connected
       
   981         iRadioSettings->SetHeadsetConnected();
       
   982         iFmTunerUtility->GetCapabilities( iTunerCaps );
       
   983         if( iTunerCaps.iTunerFunctions & TFmTunerCapabilities::ETunerRdsSupport )
       
   984             {
       
   985             TRAP_IGNORE( iRdsReceiver->InitL( *iRadioUtility, iPubSub ) )
       
   986             }
       
   987         
       
   988         TFmRadioFrequencyRange japanFreqRange = EFmRangeJapan;
       
   989         if ( TunerFrequencyRangeForRegionId( RegionId() ) == japanFreqRange )
       
   990             {
       
   991             // region is Japan so we need to change the freq range from the default
       
   992             iFmTunerUtility->SetFrequencyRange( japanFreqRange );
       
   993             }
       
   994         else
       
   995             {
       
   996             // allready on correct freq range
       
   997             HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventTunerReady, KErrNone );
       
   998             }
       
   999         
       
  1000         }
       
  1001     else if ( aError == KFmRadioErrAntennaNotConnected )
       
  1002         {
       
  1003         FTRACE(FPrint(_L("CRadioEngine::MrftoRequestTunerControlComplete() - KFmRadioErrAntennaNotConnected")));
       
  1004         iTunerControl = EStateRadioTunerControlOff;
       
  1005         iRadioSettings->SetHeadsetDisconnected();
       
  1006         HandleCallback(MRadioEngineStateChangeCallback::EFMRadioEventHeadsetDisconnected, KErrNone );
       
  1007         }
       
  1008     else if ( aError == KErrAlreadyExists )
       
  1009         {
       
  1010         iTunerControl = EStateRadioTunerControlOn;
       
  1011         // Headset must be connected
       
  1012         iRadioSettings->SetHeadsetConnected();
       
  1013         iFmTunerUtility->GetCapabilities( iTunerCaps );
       
  1014         TInt frequency;
       
  1015         iFmTunerUtility->GetFrequency( frequency );
       
  1016         StoreAndPublishFrequency( frequency );
       
  1017         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventTune, KErrNone );
       
  1018         }
       
  1019     else if ( aError == KFmRadioErrFmTransmitterActive )
       
  1020         {
       
  1021         iTunerControl = EStateRadioTunerControlOff;
       
  1022         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventFMTransmitterOn, KErrNone );
       
  1023         }
       
  1024     else if ( aError == KFmRadioErrOfflineMode )
       
  1025         {
       
  1026         iTunerControl = EStateRadioTunerControlOff;
       
  1027         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventFlightModeEnabled, KErrNone );
       
  1028         }
       
  1029     else
       
  1030         {
       
  1031         iTunerControl = EStateRadioTunerControlOff;
       
  1032         }
       
  1033     }
       
  1034 
       
  1035 // ----------------------------------------------------
       
  1036 // CRadioEngine::MrftoSetFrequencyRangeComplete
       
  1037 //
       
  1038 // ----------------------------------------------------
       
  1039 //
       
  1040 void CRadioEngine::MrftoSetFrequencyRangeComplete( TInt aError )
       
  1041     {
       
  1042     if ( aError == KErrNone )
       
  1043         {
       
  1044         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventTunerReady, KErrNone );
       
  1045         }
       
  1046         
       
  1047     FTRACE(FPrint(_L("CRadioEngine::MrftoSetFrequencyRangeComplete() - error = %d"), aError));
       
  1048     }
       
  1049 
       
  1050 // ----------------------------------------------------
       
  1051 // CRadioEngine::MrftoSetFrequencyComplete
       
  1052 // ----------------------------------------------------
       
  1053 //
       
  1054 void CRadioEngine::MrftoSetFrequencyComplete( TInt aError )
       
  1055     {
       
  1056     FTRACE(FPrint(_L("CRadioEngine::MrftoSetFrequencyComplete() - aError = %d"), aError));
       
  1057 
       
  1058     if ( !aError )
       
  1059         {
       
  1060         iRdsReceiver->ClearRdsInformation();
       
  1061         StoreAndPublishFrequency( iTempFrequency );
       
  1062         if ( iTunePresetRequested )
       
  1063             {
       
  1064             iCentralRepositoryHandler->SaveEngineSettings();
       
  1065             iTunePresetRequested = EFalse;
       
  1066             }
       
  1067         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventTune, aError );
       
  1068 
       
  1069         if ( iInitializeRadioRequestExists )
       
  1070             {
       
  1071             iInitializeRadioRequestExists = EFalse;
       
  1072             HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventFMRadioInitialized,  KErrNone );
       
  1073             }
       
  1074         }
       
  1075     else
       
  1076         {
       
  1077         iInitializeRadioRequestExists = EFalse;
       
  1078 
       
  1079         FTRACE(FPrint(_L("CRadioEngine::MrftoSetFrequencyComplete() - Sending event to UI")));
       
  1080         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventTune, aError );
       
  1081         }
       
  1082     }
       
  1083 
       
  1084 // ----------------------------------------------------
       
  1085 // CRadioEngine::MrftoStationSeekComplete
       
  1086 //
       
  1087 // ----------------------------------------------------
       
  1088 //
       
  1089 void CRadioEngine::MrftoStationSeekComplete(
       
  1090     TInt aError,
       
  1091     TInt aFrequency )
       
  1092     {
       
  1093     FTRACE(FPrint(_L("CRadioEngine::MrftoStationSeekComplete() - Frequency = %d"), aFrequency));
       
  1094     FTRACE(FPrint(_L("CRadioEngine::MrftoStationSeekComplete() - error = %d"), aError));
       
  1095 
       
  1096     if ( !aError )
       
  1097         {
       
  1098         StoreAndPublishFrequency( aFrequency );
       
  1099 
       
  1100         iRdsReceiver->ClearRdsInformation();
       
  1101         StoreAndPublishFrequency( aFrequency );
       
  1102         if ( iTunePresetRequested )
       
  1103             {
       
  1104             iCentralRepositoryHandler->SaveEngineSettings();
       
  1105             iTunePresetRequested = EFalse;
       
  1106             }
       
  1107         iTuningState = EFMRadioPSTuningUninitialized;
       
  1108         // Process the callback first because it might change the tuning state and we must avoid switching it back and forth
       
  1109         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventTune, aError );
       
  1110         TRAP_IGNORE(iPubSub->PublishTuningStateL( iTuningState ));
       
  1111 
       
  1112         if ( iInitializeRadioRequestExists )
       
  1113             {
       
  1114             iInitializeRadioRequestExists = EFalse;
       
  1115             HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventFMRadioInitialized,  KErrNone );
       
  1116             }
       
  1117         }
       
  1118     else
       
  1119         {
       
  1120         iInitializeRadioRequestExists = EFalse;
       
  1121         if ( aError == KFmRadioErrAntennaNotConnected )
       
  1122             {            
       
  1123             iRadioSettings->SetHeadsetDisconnected();
       
  1124             }
       
  1125         FTRACE(FPrint(_L("CRadioEngine::MrftoStationSeekComplete() - Sending event to UI")));
       
  1126         iTuningState = EFMRadioPSTuningUninitialized;
       
  1127         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventTune, aError );
       
  1128         TRAP_IGNORE(iPubSub->PublishTuningStateL( iTuningState ));
       
  1129         }
       
  1130     }
       
  1131 
       
  1132 
       
  1133 // ----------------------------------------------------
       
  1134 // CRadioEngine::MrftoFmTransmitterStatusChange
       
  1135 // The FM tuner is turned off when FM transmitter is on.
       
  1136 // ----------------------------------------------------
       
  1137 //
       
  1138 void CRadioEngine::MrftoFmTransmitterStatusChange(
       
  1139     TBool aActive )
       
  1140     {
       
  1141     FTRACE(FPrint(_L("CRadioEngine::MrftoFmTransmitterStatusChange() - Active = %d"), aActive));
       
  1142     if ( aActive )
       
  1143         {
       
  1144         // loses tuner control
       
  1145         iTunerControl = EStateRadioTunerControlOff;
       
  1146         }
       
  1147     }
       
  1148 
       
  1149 
       
  1150 // ----------------------------------------------------
       
  1151 // CRadioEngine::MrftoAntennaStatusChange
       
  1152 // Called when antenna status changes.
       
  1153 // ----------------------------------------------------
       
  1154 //
       
  1155 void CRadioEngine::MrftoAntennaStatusChange(
       
  1156     TBool aAttached )
       
  1157     {
       
  1158     FTRACE(FPrint(_L("CRadioEngine::MrftoAntennaStatusChange() - Antenna Status = %d"), aAttached));
       
  1159     if ( aAttached )
       
  1160         {
       
  1161         if ( iTunerControl == EStateRadioTunerControlOff )
       
  1162             {
       
  1163             iFmTunerUtility->RequestTunerControl();
       
  1164             }
       
  1165         iRadioSettings->SetHeadsetConnected();
       
  1166         TRAP_IGNORE( iPubSub->PublishAntennaStatusL( EFMRadioPSHeadsetConnected ) );
       
  1167         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventHeadsetReconnected, KErrNone );
       
  1168         }
       
  1169     else
       
  1170         {
       
  1171         iRadioSettings->SetHeadsetDisconnected();
       
  1172         TRAP_IGNORE( iPubSub->PublishAntennaStatusL( EFMRadioPSHeadsetDisconnected ) );
       
  1173         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventHeadsetDisconnected, KErrNone );
       
  1174         }
       
  1175     }
       
  1176 
       
  1177 
       
  1178 // ----------------------------------------------------
       
  1179 // CRadioEngine::FlightModeChanged
       
  1180 // Called when offline mode status changes
       
  1181 // ----------------------------------------------------
       
  1182 //
       
  1183 void CRadioEngine::MrftoOfflineModeStatusChange( TBool aOfflineMode )
       
  1184     {
       
  1185     FTRACE(FPrint(_L("CRadioEngine::MrftoOfflineModeStatusChange() - Offline Mode = %d"), aOfflineMode));
       
  1186 
       
  1187     if ( aOfflineMode )
       
  1188         {
       
  1189         HandleFlightModeEnabled();
       
  1190         }
       
  1191     else
       
  1192         {
       
  1193         if ( iTunerControl == EStateRadioTunerControlOff )
       
  1194             {
       
  1195             iFmTunerUtility->RequestTunerControl();
       
  1196             }
       
  1197         HandleFlightModeDisabled();
       
  1198         }
       
  1199     }
       
  1200 
       
  1201 // ----------------------------------------------------
       
  1202 // CRadioEngine::MrftoFrequencyRangeChange
       
  1203 //
       
  1204 // ----------------------------------------------------
       
  1205 //
       
  1206 void CRadioEngine::MrftoFrequencyRangeChange(
       
  1207     TFmRadioFrequencyRange FDEBUGVAR( aNewRange ) )
       
  1208     {
       
  1209     FTRACE( FPrint(_L("CRadioEngine::MrftoFrequencyRangeChange() - Band = %d"), aNewRange ) );    
       
  1210     }
       
  1211 
       
  1212  // ----------------------------------------------------
       
  1213 // CRadioEngine::MrftoFrequencyChange
       
  1214 // Called when the tuned frequency changes
       
  1215 // ----------------------------------------------------
       
  1216 //
       
  1217 void CRadioEngine::MrftoFrequencyChange(
       
  1218     TInt aNewFrequency )
       
  1219     {
       
  1220     FTRACE(FPrint(_L("CRadioEngine::MrftoFrequencyChange() - New Frequency = %d"), aNewFrequency));
       
  1221 	
       
  1222     if ( iRadioSettings->Frequency() != aNewFrequency )
       
  1223         {
       
  1224         iRdsReceiver->ClearRdsInformation();
       
  1225         StoreAndPublishFrequency( aNewFrequency );
       
  1226         HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventTune, KErrNone );
       
  1227         }
       
  1228     }
       
  1229 
       
  1230 // ----------------------------------------------------
       
  1231 // CRadioEngine::MTsoForcedMonoChanged
       
  1232 // Called when a client enables/disabled forced mono reception
       
  1233 // ----------------------------------------------------
       
  1234 //
       
  1235 void CRadioEngine::MrftoForcedMonoChange(
       
  1236     TBool aForcedMono )
       
  1237     {
       
  1238     FTRACE(FPrint(_L("CRadioEngine::MrftoForcedMonoChange() - Forced Mono = %d"), aForcedMono));
       
  1239     if ( aForcedMono )
       
  1240         {
       
  1241         iRadioSettings->SetAudioMode( EFMRadioMono );
       
  1242         }
       
  1243     else
       
  1244         {
       
  1245         iRadioSettings->SetAudioMode( EFMRadioStereo );
       
  1246         }
       
  1247     HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventSetAudioMode, KErrNone );
       
  1248     }
       
  1249 
       
  1250 // ----------------------------------------------------
       
  1251 // CRadioEngine::MrftoSquelchChange
       
  1252 //
       
  1253 // error code.
       
  1254 // ----------------------------------------------------
       
  1255 //
       
  1256 void CRadioEngine::MrftoSquelchChange(
       
  1257     TBool /*aSquelch*/ )
       
  1258     {
       
  1259     }
       
  1260 
       
  1261 
       
  1262 // ----------------------------------------------------
       
  1263 // CRadioEngine::MrpoStateChange This is Tuner State
       
  1264 // Called when the state of the tuner changes
       
  1265 // ----------------------------------------------------
       
  1266 //
       
  1267 void CRadioEngine::MrpoStateChange(
       
  1268     TPlayerState aState,
       
  1269     TInt aError )
       
  1270     {
       
  1271     FTRACE(FPrint(_L("CRadioEngine::MrpoStateChange() - State Change = %d, Error = %d"), aState, aError));
       
  1272 
       
  1273     if ( !aError )
       
  1274         {
       
  1275         if ( aState == ERadioPlayerPlaying )
       
  1276             {
       
  1277             iRadioSettings->SetRadioOn();
       
  1278             iCurrentRadioState = EStateRadioOn;
       
  1279             FTRACE(FPrint(_L("CRadioEngine::MrpoStateChange() Resource Available Error = %d"), aError ));
       
  1280             iRdsReceiver->StartReceiver();
       
  1281             HandleCallback(MRadioEngineStateChangeCallback::EFMRadioEventRadioOn, KErrNone);
       
  1282             }
       
  1283         else
       
  1284             {
       
  1285             iRdsReceiver->StopReceiver();
       
  1286             iRadioSettings->SetRadioOff();
       
  1287             iCurrentRadioState = EStateRadioOff;
       
  1288             HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventRadioOff, KErrNone );
       
  1289             }
       
  1290         }
       
  1291     else
       
  1292         {
       
  1293         if ( aState == ERadioPlayerIdle && iRadioSettings->IsRadioOn() )
       
  1294             {
       
  1295             MRadioEngineStateChangeCallback::TFMRadioNotifyEvent ret = 
       
  1296             	MRadioEngineStateChangeCallback::EFMRadioEventAudioResourcePaused;
       
  1297             iCurrentRadioState = EStateRadioOff;
       
  1298             iRdsReceiver->StopReceiver();
       
  1299             iRadioSettings->SetRadioOff();
       
  1300             if ( ( aError == KErrDied ) ||
       
  1301                     ( aError == KErrAccessDenied ) ||
       
  1302                     ( aError == KErrNotReady ) ||
       
  1303                     ( aError == KErrInUse ) )
       
  1304                 {
       
  1305                 // check call status
       
  1306                 if ( iSystemEventDetector->IsCallActive() )
       
  1307                     {
       
  1308                     FTRACE(FPrint(_L("CRadioEngine::MrpoStateChange() EFMRadioEventCallStarted") ) );
       
  1309                     iInCall = ETrue;
       
  1310                     ret = MRadioEngineStateChangeCallback::EFMRadioEventCallStarted;
       
  1311                     }
       
  1312                 }
       
  1313             else if ( aError == KErrHardwareNotAvailable ) // Radio is interrupted by FM Transmitter activation.
       
  1314                 {
       
  1315                 ret = MRadioEngineStateChangeCallback::EFMRadioEventFMTransmitterOn;
       
  1316                 }
       
  1317             HandleCallback( ret, aError );
       
  1318             }
       
  1319         }
       
  1320     if( iRadioSettings->IsRadioOn() )
       
  1321         {
       
  1322         TRAP_IGNORE( iPubSub->PublishStateL( ETrue ) )
       
  1323         }
       
  1324     else
       
  1325         {
       
  1326         TRAP_IGNORE
       
  1327             (
       
  1328             iTuningState = EFMRadioPSTuningUninitialized;
       
  1329             iPubSub->PublishTuningStateL( iTuningState );
       
  1330             iPubSub->PublishStateL( EFalse );
       
  1331             )
       
  1332         }
       
  1333     }
       
  1334 
       
  1335 // ----------------------------------------------------
       
  1336 // CRadioEngine::MrpoVolumeChange
       
  1337 //
       
  1338 // ----------------------------------------------------
       
  1339 //
       
  1340 void CRadioEngine::MrpoVolumeChange( TInt FDEBUGVAR( aVolume ) )
       
  1341     {
       
  1342     FTRACE(FPrint(_L("CRadioEngine::MrpoVolumeChange() - Volume = %d"), aVolume) );
       
  1343     HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventVolumeUpdated, KErrNone );
       
  1344     }
       
  1345 
       
  1346 // ----------------------------------------------------
       
  1347 // CRadioEngine::MrpoMuteChange
       
  1348 //
       
  1349 // ----------------------------------------------------
       
  1350 //
       
  1351 void CRadioEngine::MrpoMuteChange( TBool aMute )
       
  1352     {
       
  1353     FTRACE(FPrint(_L("CRadioEngine::MrpoMuteChange() - Mute = %d"), aMute));
       
  1354 
       
  1355     if ( aMute && !iRadioSettings->IsMuteOn() )
       
  1356         {
       
  1357         iRadioSettings->SetMuteOn();
       
  1358         HandleCallback(MRadioEngineStateChangeCallback::EFMRadioEventSetMuteState, KErrNone);
       
  1359         }
       
  1360     else if ( !aMute && iRadioSettings->IsMuteOn() )
       
  1361         {
       
  1362         iRadioSettings->SetMuteOff();
       
  1363         HandleCallback(MRadioEngineStateChangeCallback::EFMRadioEventSetMuteState, KErrNone);
       
  1364         }
       
  1365     }
       
  1366 
       
  1367 // ----------------------------------------------------
       
  1368 // CRadioEngine::MrpoBalanceChange
       
  1369 //
       
  1370 // ----------------------------------------------------
       
  1371 //
       
  1372 void CRadioEngine::MrpoBalanceChange(
       
  1373     TInt /*aLeftPercentage*/,
       
  1374     TInt /*aRightPercentage*/ )
       
  1375     {
       
  1376     // Not supported.
       
  1377     }
       
  1378 
       
  1379 // -----------------------------------------------------------------------------
       
  1380 // CRadioEngine::MrpeoPresetChanged
       
  1381 // Observer for Presets
       
  1382 // -----------------------------------------------------------------------------
       
  1383 void CRadioEngine::MrpeoPresetChanged(
       
  1384     TPresetChangeEvent /*aChange*/,
       
  1385     TInt /*aIndex*/ )
       
  1386     {
       
  1387 
       
  1388     }
       
  1389 
       
  1390 // -----------------------------------------------------------------------------
       
  1391 // CRadioEngine::NetworkUpCallbackL
       
  1392 // callback notifies when networks comes up
       
  1393 // -----------------------------------------------------------------------------
       
  1394 //
       
  1395 void CRadioEngine::NetworkUpCallbackL()
       
  1396 	{
       
  1397 #ifndef __WINS__
       
  1398 	
       
  1399 	if ( !iNetworkInfoListener )
       
  1400 		{
       
  1401 		// NetworkInfoListener takes care of listening to the network id and country code.
       
  1402 		iNetworkInfoListener = CFMRadioMobileNetworkInfoListener::NewL( *this );
       
  1403 		}
       
  1404 #endif
       
  1405 	}
       
  1406 
       
  1407 // -----------------------------------------------------------------------------
       
  1408 // CRadioEngine::NetworkDownCallbackL
       
  1409 // callback notifies when network goes down
       
  1410 // -----------------------------------------------------------------------------
       
  1411 //
       
  1412 void CRadioEngine::NetworkDownCallbackL()
       
  1413 	{
       
  1414 	// No implementation needed	
       
  1415 	}
       
  1416 
       
  1417 // -----------------------------------------------------------------------------
       
  1418 // CRadioEngine::NetworkIdChanged
       
  1419 // Used for notifying that the network id has changed
       
  1420 // -----------------------------------------------------------------------------
       
  1421 //
       
  1422 void CRadioEngine::NetworkIdChanged()
       
  1423 	{
       
  1424 	// No implementation needed	
       
  1425 	}
       
  1426 
       
  1427 // -----------------------------------------------------------------------------
       
  1428 // CRadioEngine::AudioResourcesAvailableL
       
  1429 // This callback notifies when audio resources become available.
       
  1430 // -----------------------------------------------------------------------------
       
  1431 //
       
  1432 void CRadioEngine::AudioResourcesAvailableL()
       
  1433     {
       
  1434     FTRACE( FPrint( _L("CRadioEngine::AudioResourcesAvailableL()" ) ) );
       
  1435     // if iPubSub exists, then the ConstructL has been successfully completed
       
  1436     if ( iPubSub && !iInCall && iTunerControl == EStateRadioTunerControlOn ) 
       
  1437         {
       
  1438         HandleCallback(MRadioEngineStateChangeCallback::EFMRadioEventAudioResourceAvailable, KErrNone );
       
  1439         }
       
  1440     }
       
  1441 
       
  1442 // -----------------------------------------------------------------------------
       
  1443 // CRadioEngine::AudioAutoResumeForbiddenL
       
  1444 // This callback notifies when audio auto resuming is forbidden.
       
  1445 // -----------------------------------------------------------------------------
       
  1446 //
       
  1447 void CRadioEngine::AudioAutoResumeForbiddenL()
       
  1448 	{
       
  1449 	FTRACE( FPrint( _L("CRadioEngine::AudioAutoResumeForbiddenL()" ) ) );
       
  1450 	if ( iPubSub ) // if iPubSub exists, then the ConstructL has been successfully completed
       
  1451 		{
       
  1452     	HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventAudioResourceLost, KErrNone );
       
  1453 		}	
       
  1454 	}
       
  1455 
       
  1456 // -----------------------------------------------------------------------------
       
  1457 // CRadioEngine::CallActivatedCallbackL
       
  1458 // This callback notifies when call becomes active.
       
  1459 // -----------------------------------------------------------------------------
       
  1460 //
       
  1461 void CRadioEngine::CallActivatedCallbackL()
       
  1462     {
       
  1463     FTRACE( FPrint( _L("CRadioEngine::CallActivatedCallbackL()" ) ) );
       
  1464     iInCall = ETrue;
       
  1465     }
       
  1466 
       
  1467 // -----------------------------------------------------------------------------
       
  1468 // CRadioEngine::CallDeactivatedCallbackL
       
  1469 // This callback notifies when call becomes deactive.
       
  1470 // -----------------------------------------------------------------------------
       
  1471 //
       
  1472 void CRadioEngine::CallDeactivatedCallbackL()
       
  1473 	{
       
  1474 	FTRACE( FPrint( _L("CRadioEngine::CallDeactivatedCallbackL()" ) ) );	
       
  1475 	iInCall = EFalse;	
       
  1476 	if ( iPubSub ) // if iPubSub exists, then the ConstructL has been successfully completed	
       
  1477 		{
       
  1478 		HandleCallback( MRadioEngineStateChangeCallback::EFMRadioEventCallEnded, KErrNone );	
       
  1479 		}		
       
  1480 	}	
       
  1481 
       
  1482 // ---------------------------------------------------------
       
  1483 // CRadioEngine::RdsDataProgrammeService
       
  1484 // ---------------------------------------------------------
       
  1485 //
       
  1486 void CRadioEngine::RdsDataProgrammeService( const TDesC& /*aProgramService*/ )
       
  1487     {
       
  1488     }
       
  1489 
       
  1490 // ---------------------------------------------------------
       
  1491 // CRadioEngine::RdsDataPsNameIsStatic
       
  1492 // ---------------------------------------------------------
       
  1493 //
       
  1494 void CRadioEngine::RdsDataPsNameIsStatic( TBool /*aStatic*/ )
       
  1495     {
       
  1496     }
       
  1497 
       
  1498 // ---------------------------------------------------------
       
  1499 // CRadioEngine::RdsDataRadioText
       
  1500 // ---------------------------------------------------------
       
  1501 //
       
  1502 void CRadioEngine::RdsDataRadioText( const TDesC& /*aRadioText*/ )
       
  1503     {
       
  1504     }
       
  1505 
       
  1506 // ---------------------------------------------------------
       
  1507 // CRadioEngine::RdsDataRadioTextPlus
       
  1508 // ---------------------------------------------------------
       
  1509 //
       
  1510 void CRadioEngine::RdsDataRadioTextPlus( const TInt /*aRTPlusClass*/, const TDesC& /*aRadioText*/ )
       
  1511     {
       
  1512     }
       
  1513 
       
  1514 // ---------------------------------------------------------
       
  1515 // CRadioEngine::RdsAfSearchBegin
       
  1516 // ---------------------------------------------------------
       
  1517 //
       
  1518 void CRadioEngine::RdsAfSearchBegin()
       
  1519     {
       
  1520     iFrequencySetByRdsAf = ETrue;
       
  1521     }
       
  1522 
       
  1523 // ---------------------------------------------------------
       
  1524 // CRadioEngine::RdsAfSearchEnd
       
  1525 // ---------------------------------------------------------
       
  1526 //
       
  1527 void CRadioEngine::RdsAfSearchEnd( TUint32 /*aFrequency*/, TInt aError )
       
  1528     {
       
  1529    if( aError != KErrNone )
       
  1530         {
       
  1531         iFrequencySetByRdsAf = EFalse;
       
  1532         }
       
  1533     }
       
  1534 
       
  1535 // ---------------------------------------------------------
       
  1536 // CRadioEngine::RdsAfSearchStateChange
       
  1537 // ---------------------------------------------------------
       
  1538 //
       
  1539 void CRadioEngine::RdsAfSearchStateChange( TBool /*aEnabled*/ )
       
  1540     {
       
  1541     }
       
  1542 
       
  1543 // ---------------------------------------------------------
       
  1544 // CRadioEngine::RdsAvailable
       
  1545 // ---------------------------------------------------------
       
  1546 //
       
  1547 void CRadioEngine::RdsAvailable( TBool /*aAvailable*/ )
       
  1548     {
       
  1549     }
       
  1550 
       
  1551 // -----------------------------------------------------------------------------
       
  1552 // CRadioEngine::InitializeResourceLoadingL
       
  1553 // add resource file information
       
  1554 // -----------------------------------------------------------------------------
       
  1555 //
       
  1556 void CRadioEngine::InitializeResourceLoadingL()
       
  1557     {
       
  1558     CCoeEnv* coeEnv = CCoeEnv::Static();
       
  1559     RFs fs( coeEnv->FsSession() );
       
  1560     TFileName fileName; 
       
  1561     TFileName baseResource;
       
  1562     TFindFile finder( fs );
       
  1563     TLanguage language( ELangNone );
       
  1564 
       
  1565     TParsePtrC parse( KFMRadioEngineResourceFile );
       
  1566     _LIT( resourceFileWildExt, ".r*" );
       
  1567 
       
  1568     // Make sure to find all resource files, not only .rsc files as it may be so
       
  1569     // that there is only .r01, .r02, etc. files available
       
  1570     fileName.Copy( parse.Name() );
       
  1571     fileName.Append( resourceFileWildExt );
       
  1572 
       
  1573     // TFindFile applies search order that is from 
       
  1574     // drive Y to A, then Z
       
  1575     CDir* entries = NULL;
       
  1576     TInt err = finder.FindWildByDir( fileName , KDC_RESOURCE_FILES_DIR, entries );
       
  1577     delete entries;
       
  1578     entries = NULL;
       
  1579     TBool found = EFalse;
       
  1580     while ( !found && err == KErrNone )
       
  1581         {
       
  1582         // Found file
       
  1583         fileName.Zero();
       
  1584         TParsePtrC foundPath( finder.File() );
       
  1585         fileName.Copy( foundPath.DriveAndPath() );
       
  1586         fileName.Append( KFMRadioEngineResourceFile );
       
  1587         BaflUtils::NearestLanguageFile( fs, fileName, language );
       
  1588         if ( language != ELangNone && BaflUtils::FileExists( fs, fileName ) )
       
  1589             {
       
  1590             found = ETrue;
       
  1591             iFMRadioEngineResourceOffset = coeEnv->AddResourceFileL( fileName );
       
  1592             }
       
  1593         else
       
  1594             {
       
  1595             if ( language == ELangNone &&
       
  1596                  !baseResource.Compare( KNullDesC ) &&
       
  1597                  BaflUtils::FileExists( fs, fileName ) )
       
  1598                 {
       
  1599                 baseResource.Copy( fileName );
       
  1600                 }
       
  1601             err = finder.FindWild( entries );
       
  1602             delete entries;
       
  1603             entries = NULL;
       
  1604             }
       
  1605         }
       
  1606 
       
  1607     if ( !found && baseResource.Compare( KNullDesC ) )
       
  1608         {
       
  1609         // If we found *.rsc then better to use that than nothing
       
  1610         if ( BaflUtils::FileExists( fs, baseResource ) )
       
  1611             {
       
  1612             iFMRadioEngineResourceOffset = coeEnv->AddResourceFileL( baseResource );
       
  1613             }
       
  1614         }
       
  1615     }
       
  1616 
       
  1617 // -----------------------------------------------------------------------------
       
  1618 // CRadioEngine::TunerFrequencyRangeForRegionId
       
  1619 // convert FMRadio region info to tuner utility frequency range
       
  1620 // -----------------------------------------------------------------------------
       
  1621 //
       
  1622 TFmRadioFrequencyRange CRadioEngine::TunerFrequencyRangeForRegionId( const TInt aRegionId ) const
       
  1623     {
       
  1624     FTRACE( FPrint( _L("CRadioEngine::TunerFrequencyRangeForRegionId(TInt aRegionID = %d)"), aRegionId ) );  
       
  1625     TFmRadioFrequencyRange result = EFmRangeEuroAmerica;
       
  1626     switch( aRegionId )
       
  1627         {
       
  1628         case EFMRadioRegionDefault:
       
  1629             {
       
  1630             result = EFmRangeEuroAmerica;
       
  1631             break;
       
  1632             }
       
  1633         case EFMRadioRegionJapan:
       
  1634             {
       
  1635             result = EFmRangeJapan;
       
  1636             break;
       
  1637             }
       
  1638         case EFMRadioRegionAmerica:
       
  1639             {
       
  1640             result = EFmRangeEuroAmerica;
       
  1641             break;
       
  1642             }
       
  1643         default:
       
  1644             break;
       
  1645         }
       
  1646     return result;
       
  1647     }
       
  1648 
       
  1649 // -----------------------------------------------------------------------------
       
  1650 // CRadioEngine::GetRegionL
       
  1651 // determine current region
       
  1652 // -----------------------------------------------------------------------------
       
  1653 //	
       
  1654 EXPORT_C TFMRadioRegionSetting CRadioEngine::GetRegionL() const
       
  1655     {
       
  1656     FTRACE( FPrint( _L("CRadioEngine::GetRegionL()" ) ) );    
       
  1657     TInt regionCount = iCentralRepositoryHandler->CountRegions();
       
  1658     TFMRadioRegionSetting reg = EFMRadioRegionNone;
       
  1659 
       
  1660     if ( iSystemEventDetector->IsNetworkCoverage() )
       
  1661         {
       
  1662         TBool countryCodeMatch( EFalse );
       
  1663         // Choose the frequency range according to country code
       
  1664         const TDesC& countryCode = 
       
  1665             iNetworkInfoListener->CountryCode();
       
  1666         for ( TInt i = 0 ; i < regionCount ; i++ )
       
  1667             {
       
  1668             const RFMRadioCountryCodeArray& regionCountryCodes = 
       
  1669                 iCentralRepositoryHandler->Region( i ).CountryCodes();
       
  1670                 
       
  1671             for ( TInt j = 0 ; j < regionCountryCodes.Count() ; j++ )
       
  1672                 {
       
  1673                 if ( countryCode == *regionCountryCodes[j] )
       
  1674                     {
       
  1675                     // We have a match
       
  1676                     countryCodeMatch = ETrue;
       
  1677                     reg = iCentralRepositoryHandler->Region(i).Id();
       
  1678                     }
       
  1679                 }
       
  1680             }
       
  1681         if ( !countryCodeMatch )
       
  1682             {  
       
  1683             // All regions have been searched and no direct match found, 
       
  1684             // use default one
       
  1685             reg = EFMRadioRegionDefault;
       
  1686             }
       
  1687         }
       
  1688     else 
       
  1689         {
       
  1690         if ( iCentralRepositoryHandler->RegionId() != EFMRadioRegionNone )
       
  1691             {
       
  1692             reg = static_cast<TFMRadioRegionSetting>( 
       
  1693                 iCentralRepositoryHandler->RegionId() );
       
  1694             }
       
  1695         }
       
  1696     return reg;	
       
  1697     }
       
  1698 
       
  1699 // ----------------------------------------------------
       
  1700 // CRadioEngine::FillListWithRegionDataL
       
  1701 // fill given list with fmradio regions 
       
  1702 // ----------------------------------------------------
       
  1703 //	
       
  1704 EXPORT_C void CRadioEngine::FillListWithRegionDataL( CDesC16Array& aArray ) const
       
  1705 	{
       
  1706 	TInt regionCount = iCentralRepositoryHandler->CountRegions();	
       
  1707 	
       
  1708 	for ( TInt i = 0 ; i < regionCount ; i++ )
       
  1709         {
       
  1710         aArray.AppendL( iCentralRepositoryHandler->Region( i ).Name() );
       
  1711         }	
       
  1712 	}
       
  1713 
       
  1714 // ----------------------------------------------------
       
  1715 // CRadioEngine::RegionIdAtIndex
       
  1716 // return region from the array of supported regions
       
  1717 // ----------------------------------------------------
       
  1718 //	
       
  1719 EXPORT_C TFMRadioRegionSetting CRadioEngine::RegionIdAtIndex( const TInt aIndex ) const
       
  1720 	{
       
  1721  	return iCentralRepositoryHandler->Region( aIndex ).Id();		
       
  1722 	}
       
  1723 
       
  1724 // ----------------------------------------------------
       
  1725 // CRadioEngine::RegionId
       
  1726 // Return current region Id.
       
  1727 // ----------------------------------------------------
       
  1728 //
       
  1729 EXPORT_C TInt CRadioEngine::RegionId() const
       
  1730 	{
       
  1731 	return iCentralRepositoryHandler->RegionId();	
       
  1732 	}
       
  1733 	
       
  1734 // ----------------------------------------------------
       
  1735 // CRadioEngine::SetRegionIdL
       
  1736 // Change the the current region Id.
       
  1737 // ----------------------------------------------------
       
  1738 //	
       
  1739 EXPORT_C void CRadioEngine::SetRegionIdL( TInt aRegionId ) const
       
  1740 	{
       
  1741 	if ( iCentralRepositoryHandler->RegionId() != aRegionId )
       
  1742         {
       
  1743         // Change only when necessary as it changes also the default tuned frequency
       
  1744         //iSetters.SetRegionIdL( aRegionId );
       
  1745         iCentralRepositoryHandler->SetRegionIdL( aRegionId );
       
  1746         iPubSub->PublishFrequencyDecimalCountL( 
       
  1747                 TFMRadioPSFrequencyDecimalCount( iRadioSettings->DecimalCount() ) );
       
  1748         }		
       
  1749 	}
       
  1750 
       
  1751 // ----------------------------------------------------
       
  1752 // CRadioEngine::RequestTunerControl
       
  1753 // request tuner control from RadioFmTunerUtility
       
  1754 // ----------------------------------------------------
       
  1755 //
       
  1756 EXPORT_C void CRadioEngine::RequestTunerControl() const
       
  1757     {
       
  1758     FTRACE( FPrint( _L("CRadioEngine::RequestTunerControl()")) );
       
  1759     if ( iTunerControl == EStateRadioTunerControlUninitialized )
       
  1760         {
       
  1761         // first request for tuner control.
       
  1762         iFmTunerUtility->RequestTunerControl();
       
  1763         }
       
  1764     }
       
  1765 
       
  1766 // ----------------------------------------------------
       
  1767 // CRadioEngine::DecimalCount
       
  1768 // return decimal count for current region MHz information
       
  1769 // ----------------------------------------------------
       
  1770 //
       
  1771 EXPORT_C TInt CRadioEngine::DecimalCount() const
       
  1772 	{
       
  1773 	return iCentralRepositoryHandler->DecimalCount();	
       
  1774 	}
       
  1775 
       
  1776 // ----------------------------------------------------
       
  1777 // CRadioEngine::FrequencyStepSize
       
  1778 // return step size for tuning.
       
  1779 // ----------------------------------------------------
       
  1780 //
       
  1781 EXPORT_C TUint32 CRadioEngine::FrequencyStepSize() const
       
  1782 	{
       
  1783 	return iRadioSettings->FrequencyStepSize();
       
  1784 	}
       
  1785 	
       
  1786 // ----------------------------------------------------
       
  1787 // CRadioEngine::PubSubL
       
  1788 // Returns a reference to P&S interface
       
  1789 // ----------------------------------------------------
       
  1790 //
       
  1791 EXPORT_C CFMRadioPubSub& CRadioEngine::PubSubL() const
       
  1792     {	
       
  1793     User::LeaveIfNull( iPubSub );
       
  1794     return *iPubSub;   
       
  1795     }	
       
  1796 
       
  1797 // ----------------------------------------------------
       
  1798 // CRadioEngine::StartupCount
       
  1799 // increases startup count by one and returns amount of app startups.
       
  1800 // ----------------------------------------------------
       
  1801 //
       
  1802 EXPORT_C TInt CRadioEngine::UpdatedStartupCount() const
       
  1803 	{
       
  1804 	FTRACE( FPrint( _L("CRadioEngine::UpdatedStartupCount()" ) ) );	
       
  1805 	iCentralRepositoryHandler->IncreaseStartupCount();
       
  1806 	return iRadioSettings->StartupCount();
       
  1807 	}
       
  1808 
       
  1809 // ----------------------------------------------------
       
  1810 // CRadioEngine::CancelScanLocalStationsScan
       
  1811 // Cancel seek and notify UI
       
  1812 // ----------------------------------------------------
       
  1813 //
       
  1814 EXPORT_C void CRadioEngine::CancelScanLocalStationsScan()
       
  1815 	{
       
  1816 	FTRACE( FPrint( _L("CRadioEngine::CancelScanLocalStationsScan()" ) ) );
       
  1817 	CancelScan();
       
  1818 	TInt error = KErrNone;
       
  1819 	iStateHandler->Callback( MRadioEngineStateChangeCallback::EFMRadioEventScanLocalStationsCanceled, error ); 
       
  1820 	}
       
  1821 
       
  1822 // ---------------------------------------------------------------------------
       
  1823 // CRadioEngine::IsAudioRoutingPossible
       
  1824 // Routing is not possible when headset is not available or radio is off
       
  1825 // ---------------------------------------------------------------------------
       
  1826 //
       
  1827 EXPORT_C TBool CRadioEngine::IsAudioRoutingPossible() const
       
  1828     {
       
  1829     FTRACE( FPrint( _L("CRadioEngine::IsAudioRoutingPossible()" ) ) );
       
  1830     TBool isAudioRoutingPossible = EFalse;
       
  1831     
       
  1832     TBool headsetConnected = iHeadsetObserver->WiredHeadsetConnected();
       
  1833     TBool radioOn = iRadioSettings->IsRadioOn();
       
  1834         
       
  1835     if ( headsetConnected && radioOn )
       
  1836         {
       
  1837         isAudioRoutingPossible = ETrue;
       
  1838         }
       
  1839         
       
  1840     return isAudioRoutingPossible;
       
  1841     }
       
  1842 	
       
  1843 // ---------------------------------------------------------------------------
       
  1844 // CRadioEngine::MusicStoreNoteDisplayed
       
  1845 // ---------------------------------------------------------------------------
       
  1846 //
       
  1847 EXPORT_C TBool CRadioEngine::MusicStoreNoteDisplayed()
       
  1848     {
       
  1849     return iCentralRepositoryHandler->MusicStoreNoteDisplayed();
       
  1850     }
       
  1851 
       
  1852 // ---------------------------------------------------------------------------
       
  1853 // CRadioEngine::SetMusicStoreNoteDisplayed
       
  1854 // ---------------------------------------------------------------------------
       
  1855 //
       
  1856 EXPORT_C void CRadioEngine::SetMusicStoreNoteDisplayed()
       
  1857     {
       
  1858     iCentralRepositoryHandler->SetMusicStoreNoteDisplayed();
       
  1859     }
       
  1860 
       
  1861 // ---------------------------------------------------------------------------
       
  1862 // CRadioEngine::MusicStoreNoteDisplayed
       
  1863 // ---------------------------------------------------------------------------
       
  1864 //
       
  1865 EXPORT_C TFMRadioRTPlusLevel CRadioEngine::GetRTPlusSupportLevel()
       
  1866     {
       
  1867     TInt value = iCentralRepositoryHandler->GetRTPlusSupportLevel();
       
  1868     
       
  1869     //do not return the value directly - make sure we return a valid value
       
  1870     if ( value == EFMRadioAllInteractions )
       
  1871         {
       
  1872         return EFMRadioAllInteractions;
       
  1873         }
       
  1874     return EFMRadioMusicStoreOnly;
       
  1875     }
       
  1876 
       
  1877 // ---------------------------------------------------------------------------
       
  1878 // CRadioEngine::SetCurrentPresetIndex
       
  1879 // sets current preset index if the preset database changes
       
  1880 // ---------------------------------------------------------------------------
       
  1881 //
       
  1882 EXPORT_C void CRadioEngine::SetCurrentPresetIndex( const TInt aIndex )
       
  1883     {
       
  1884     TInt maxNumberOfPresets;
       
  1885     iPresetUtility->GetMaxNumberOfPresets( maxNumberOfPresets );
       
  1886     
       
  1887     if ( ( aIndex >= KErrNotFound ) && ( aIndex  < maxNumberOfPresets ) )
       
  1888         {
       
  1889         iRadioSettings->SetCurrentPreset( aIndex );
       
  1890         if ( aIndex >= 0 )
       
  1891             {
       
  1892             // publish current preset
       
  1893             TRAP_IGNORE
       
  1894                 (
       
  1895                 TInt frequency = 0;
       
  1896                 TStationName name;
       
  1897                 iPresetUtility->GetPresetL( aIndex, name, frequency );
       
  1898                 iPubSub->PublishChannelNameL( name );
       
  1899                 iPubSub->PublishChannelL( aIndex );
       
  1900                 )
       
  1901             }
       
  1902         }
       
  1903     }
       
  1904 
       
  1905 // ---------------------------------------------------------------------------
       
  1906 // CRadioEngine::StoreAndPublishFrequency
       
  1907 // Updates the frequency into settings and P&S
       
  1908 // ---------------------------------------------------------------------------
       
  1909 //
       
  1910 void CRadioEngine::StoreAndPublishFrequency( TInt aFrequency )
       
  1911     {
       
  1912     iRadioSettings->SetFrequency( aFrequency );
       
  1913     TRAP_IGNORE( iPubSub->PublishFrequencyL( aFrequency ); )
       
  1914     }
       
  1915 
       
  1916 // ---------------------------------------------------------------------------
       
  1917 // CRadioEngine::PresetUrlL
       
  1918 // ---------------------------------------------------------------------------
       
  1919 //
       
  1920 EXPORT_C TInt CRadioEngine::PresetUrlL( TInt aIndex, TDes& aUrl )
       
  1921     {
       
  1922     FTRACE( FPrint( _L("CRadioEngine::PresetUrlL( Index %d, url: %S)"), aIndex, &aUrl ) );
       
  1923     TInt err = KErrNone;
       
  1924     TFmPresetUrl presetUrl;
       
  1925     
       
  1926     if ( presetUrl.MaxLength() <= aUrl.MaxLength() )
       
  1927         {
       
  1928         iPresetUtility->GetPresetUrlL( aIndex, presetUrl );
       
  1929         aUrl.Copy( presetUrl );
       
  1930         if ( !aUrl.Length() )
       
  1931             {
       
  1932             err = KErrNotFound;
       
  1933             }            
       
  1934         }
       
  1935     else
       
  1936         {
       
  1937         err = KErrOverflow;
       
  1938         }
       
  1939     return err;
       
  1940     }
       
  1941 
       
  1942 // ---------------------------------------------------------------------------
       
  1943 // CRadioEngine::DeletePresetL
       
  1944 // Delete preset. With index -1 all preset are reseted
       
  1945 // ---------------------------------------------------------------------------
       
  1946 //
       
  1947 EXPORT_C void CRadioEngine::DeletePresetL( TInt aIndex )
       
  1948     {
       
  1949     FTRACE( FPrint( _L("CRadioEngine::DeletePresetL( i: %d )"), aIndex ) );
       
  1950     iPresetUtility->DeletePresetL( aIndex );
       
  1951     }
       
  1952 
       
  1953 // ---------------------------------------------------------------------------
       
  1954 // CRadioEngine::SaveUrlToCurrentPresetL
       
  1955 // ---------------------------------------------------------------------------
       
  1956 //
       
  1957 EXPORT_C void CRadioEngine::SaveUrlToPresetL( TInt aIndex, const TDesC& aUrl )
       
  1958     {
       
  1959     FTRACE( FPrint( _L("CRadioEngine::SaveUrlToCurrentPresetL( i: %d Url: %S)"), aIndex, &aUrl ) );
       
  1960     TFmPresetUrl presetUrl;
       
  1961     if ( aUrl.Length() <= presetUrl.MaxLength() )
       
  1962         {
       
  1963         presetUrl.Copy( aUrl );
       
  1964         iPresetUtility->SetPresetUrlL( aIndex, presetUrl );
       
  1965         }
       
  1966     }
       
  1967 
       
  1968 // End of file