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