radioengine/engine/src/cradioengineimp.cpp
changeset 13 46974bebc798
child 14 63aabac4416d
equal deleted inserted replaced
0:f3d95d9c00ab 13:46974bebc798
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <audiooutput.h>
       
    20 #include <eikdef.h>
       
    21 #include <coemain.h>
       
    22 #include <badesca.h>
       
    23 #include <tzlocalizer.h>
       
    24 
       
    25 // User includes
       
    26 #include "cradioenginelogger.h"
       
    27 #include "radiointernalpskeys.h"
       
    28 #include "radiointernalcrkeys.h"
       
    29 #include "cradioaudiorouter.h"
       
    30 #include "cradiopubsub.h"
       
    31 #include "cradioengineimp.h"
       
    32 #include "mradioengineobserver.h"
       
    33 #include "mradioscanobserver.h"
       
    34 #include "cradioregion.h"
       
    35 #include "cradiosettings.h"
       
    36 #include "mradioenginesettings.h"
       
    37 #include "mradiosettingssetter.h"
       
    38 #include "cradiorepositorymanager.h"
       
    39 #include "cradiordsreceiver.h"
       
    40 #include "cradiosystemeventcollector.h"
       
    41 #include "cradionetworkinfolistener.h"
       
    42 #include "radioengine.hrh"
       
    43 
       
    44 #include "../../group/buildflags.hrh"
       
    45 #ifdef __FEATURE_RDS_SIMULATOR
       
    46 #   include "t_cradiordsreceiversimulator.h"
       
    47 #endif
       
    48 
       
    49 // Constants
       
    50 
       
    51 /** The limit of volume steps that must not be divided */
       
    52 const TInt KRadioVolumeStepsDividinglimit = 20;
       
    53 /** If CMMTunerUtility has 200 volume steps, AknVolume control has 20 steps */
       
    54 const TInt KRadioVolumeStepsDivider = 500;
       
    55 /** amount of volume steps used previously */
       
    56 #if defined __WINS__
       
    57 const TInt KRadioVolumeStepsOld = 10;
       
    58 #endif // defined __WINS__
       
    59 
       
    60 /** KRadioRadioSwitchDelay value must not be too small, otherwise problems with
       
    61  radioserver will occur, when swithing between valid and invalid presets */
       
    62 const TInt KRadioRadioSwitchDelay = 300; // In milliseconds
       
    63 
       
    64 const TInt KRadioThousand = 1000;
       
    65 const TInt KRadioMillion = KRadioThousand * KRadioThousand;
       
    66 
       
    67 // Country order from timezones.rss. Default = 0, Japan = 1, America = 2.
       
    68 const TUint KCityGroupRegions[] =
       
    69     {0,0,0,0,0,0,2,0,0,0,0,2,0,2,0,2,0,2,0,2,2,0,2,0,0,2,2,2,0,2,0,0, //32
       
    70      0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,0,0,0, //64
       
    71      0,0,0,0,0,2,0,0,0,2,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //96
       
    72      0,0,0,2,0,0,0,0,2,2,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //128
       
    73      0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //160
       
    74      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0, //192
       
    75      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0, //224
       
    76      2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
       
    77 
       
    78 
       
    79 
       
    80 // ================= MEMBER FUNCTIONS =======================
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 CRadioEngineImp::CRadioEngineImp( CRadioAudioRouter* aAudioRouter )
       
    87     : CRadioEngine( aAudioRouter )
       
    88     , iAntennaAttached( ETrue )
       
    89     , iFreqEventReason( RadioEngine::ERadioFrequencyEventReasonUnknown )
       
    90     {
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 void CRadioEngineImp::ConstructL()
       
    98     {
       
    99     LOG_METHOD_AUTO;
       
   100 
       
   101     if ( !iAudioRouter || !iSystemEventCollector || !iSettings )
       
   102         {
       
   103         User::Leave( KErrNotReady );
       
   104         }
       
   105 
       
   106     iSystemEventCollector->AddObserverL( this );
       
   107 
       
   108     // Initial default values to be over by init
       
   109     User::LeaveIfError( iSettings->RadioSetter().SetPowerOn( EFalse ) );
       
   110 
       
   111     // The output source needs to be in headset when starting the radio.
       
   112     // But if the headset is not connected, audio cannot be heard if it is routed
       
   113     // to headset
       
   114     RadioEngine::TRadioAudioRoute route = iSystemEventCollector->IsHeadsetConnectedL() ?
       
   115                                 RadioEngine::ERadioHeadset : RadioEngine::ERadioSpeaker;
       
   116     User::LeaveIfError( iSettings->RadioSetter().SetAudioRoute( route ) );
       
   117 
       
   118 #ifdef __FEATURE_RDS_SIMULATOR
       
   119     iRdsReceiver = CRadioRdsReceiverSimulator::NewL( iSettings->EngineSettings() );
       
   120 #else
       
   121     iRdsReceiver = CRadioRdsReceiver::NewL( iSettings->EngineSettings() );
       
   122 #endif
       
   123 
       
   124     iNetworkInfoListener = CRadioNetworkInfoListener::NewL( iSettings->RadioSetter(), NULL );
       
   125 
       
   126     iRdsReceiver->AddObserverL( this );
       
   127 
       
   128     // Create timer that is used when polling for radio restart.
       
   129     iRadioTimer = CPeriodic::NewL( CActive::EPriorityHigh );
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 CRadioEngineImp::~CRadioEngineImp()
       
   137     {
       
   138     LOG( "CRadioEngineImp::~CRadioEngineImp -- Start" );
       
   139 
       
   140     delete iNetworkInfoListener;
       
   141 
       
   142     PowerOff();
       
   143 
       
   144     delete iRadioTimer;
       
   145 
       
   146     DeleteAudioOutput();
       
   147 
       
   148     if ( iTunerUtility )
       
   149         {
       
   150         iTunerUtility->Close();
       
   151         }
       
   152 
       
   153     if ( iPlayerUtility )
       
   154         {
       
   155         iPlayerUtility->Close();
       
   156         }
       
   157 
       
   158     if ( iRdsReceiver )
       
   159         {
       
   160         iRdsReceiver->RemoveObserver( this );
       
   161         }
       
   162 
       
   163     delete iRdsReceiver;
       
   164     delete iRadioUtility;
       
   165 
       
   166     iObservers.Close();
       
   167 
       
   168     if ( iSystemEventCollector )
       
   169         {
       
   170         iSystemEventCollector->RemoveObserver( this );
       
   171         }
       
   172     delete iSystemEventCollector;
       
   173 
       
   174     delete iPubSub;
       
   175 
       
   176     if ( iSettings )
       
   177         {
       
   178         iSettings->RadioSetter().SetObserver( NULL );
       
   179         }
       
   180     delete iSettings;
       
   181 
       
   182     LOG( "CRadioEngineImp::~CRadioEngineImp -- End" );
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 void CRadioEngineImp::SetSystemEventCollector( CRadioSystemEventCollector* aCollector )
       
   190     {
       
   191     iSystemEventCollector = aCollector;
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 void CRadioEngineImp::SetRadioSettings( CRadioSettings* aSettings )
       
   199     {
       
   200     iSettings = aSettings;
       
   201     iSettings->RadioSetter().SetObserver( this );
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CRadioEngineImp::SetRadioPubSub( CRadioPubSub* aPubSub )
       
   209     {
       
   210     iPubSub = aPubSub;
       
   211     }
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 CRadioAudioRouter& CRadioEngineImp::AudioRouter() const
       
   218     {
       
   219     return *iAudioRouter;
       
   220     }
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 CRadioSystemEventCollector& CRadioEngineImp::SystemEventCollector() const
       
   227     {
       
   228     return *iSystemEventCollector;
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 CRadioSettings& CRadioEngineImp::Settings() const
       
   236     {
       
   237     return *iSettings;
       
   238     }
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 CRadioPubSub* CRadioEngineImp::PubSub() const
       
   245     {
       
   246     return iPubSub;
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // Determines radio region
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 TRadioRegion CRadioEngineImp::DetermineRegion()
       
   254     {
       
   255     TRadioRegion region = ERadioRegionNone;
       
   256 
       
   257     MRadioEngineSettings& engineSettings = iSettings->EngineSettings();
       
   258     if ( iSystemEventCollector->IsMobileNetworkCoverage() )
       
   259         {
       
   260         region = RegionFromMobileNetwork();
       
   261         }
       
   262     else
       
   263         {
       
   264         region = RegionFromTimezone();
       
   265         }
       
   266 
       
   267     if ( !iSettings->IsRegionAllowed( region ) )
       
   268         {
       
   269         region = ERadioRegionNone;
       
   270         }
       
   271 
       
   272     // Region not found, try to use the previously set region
       
   273     if ( region == ERadioRegionNone && engineSettings.RegionId() != ERadioRegionNone )
       
   274         {
       
   275         region = engineSettings.RegionId();
       
   276         }
       
   277 
       
   278     // All regions have been searched and no direct match found, use default one
       
   279     if ( region == ERadioRegionNone )
       
   280         {
       
   281         region = engineSettings.DefaultRegion();
       
   282         }
       
   283 
       
   284     return region;
       
   285     }
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // Initializes / reinitializes the radio. If this is not called
       
   289 // the radio is not functional
       
   290 // ---------------------------------------------------------------------------
       
   291 //
       
   292 void CRadioEngineImp::InitRadioL( TInt aRegionId, CRadioPubSub* aPubSub )
       
   293     {
       
   294     LOG_METHOD_AUTO;
       
   295     LOG_FORMAT( "CRadioEngineImp::InitRadioL: Region: %d", aRegionId );
       
   296 
       
   297     iRadioInitializationState = ERadioNotInitialized;
       
   298 
       
   299     iPubSub = aPubSub;
       
   300 
       
   301     iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonUnknown;
       
   302     if ( iSettings->EngineSettings().RegionId() != aRegionId )
       
   303         {
       
   304         // Change only when necessary as it changes also the default tuned frequency
       
   305         User::LeaveIfError( iSettings->RadioSetter().SetRegionId( aRegionId ) );
       
   306         }
       
   307 
       
   308     if ( !iRadioUtility )
       
   309         {
       
   310         iRadioUtility = CRadioUtility::NewL( ETrue );
       
   311         }
       
   312 
       
   313     if ( !iPlayerUtility )
       
   314         {
       
   315         iPlayerUtility = &iRadioUtility->RadioPlayerUtilityL( *this );
       
   316         SetAudioOutput( CAudioOutput::NewL( *iPlayerUtility ) );
       
   317         }
       
   318 
       
   319     if ( !iTunerUtility )
       
   320         {
       
   321         iTunerUtility = &iRadioUtility->RadioFmTunerUtilityL( *this );
       
   322         }
       
   323 
       
   324     // Utilities have been created now
       
   325     iRadioInitializationState = ERadioUtilitiesConstructed;
       
   326 
       
   327     // Before first RequestTunerControl() call it is ok to enable offline mode without checking capabilities
       
   328     iTunerUtility->EnableTunerInOfflineMode( ETrue );
       
   329     iTunerUtility->RequestTunerControl();
       
   330     }
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 TBool CRadioEngineImp::RadioInitialized() const
       
   337     {
       
   338     return iRadioInitializationState == ERadioTunerControlGranted;
       
   339     }
       
   340 
       
   341 // ---------------------------------------------------------------------------
       
   342 // Sets the state for radio audio
       
   343 // ---------------------------------------------------------------------------
       
   344 //
       
   345 void CRadioEngineImp::EnableAudio( TBool aEnable, TBool aDelay )
       
   346     {
       
   347     LOG_FORMAT( "CRadioEngineImp::EnableAudio( %d )", aEnable );
       
   348     iRadioEnabled = aEnable;
       
   349     if ( aDelay )
       
   350         {
       
   351         SwitchPower( iRadioEnabled );
       
   352         }
       
   353     else if ( aEnable )
       
   354         {
       
   355         if ( !RadioInitialized() &&
       
   356              iRadioInitializationState == ERadioUtilitiesConstructed &&
       
   357              iRadioEnabled &&
       
   358              OkToPlay( iSettings->EngineSettings().TunedFrequency() ) )
       
   359             {
       
   360             iTunerUtility->RequestTunerControl();
       
   361             }
       
   362         else
       
   363             {
       
   364             PowerOn();
       
   365             }
       
   366         }
       
   367     else
       
   368         {
       
   369         PowerOff();
       
   370         }
       
   371     }
       
   372 
       
   373 // ---------------------------------------------------------------------------
       
   374 // Gets the state for radio audio.
       
   375 // ---------------------------------------------------------------------------
       
   376 //
       
   377 TBool CRadioEngineImp::RadioAudioEnabled() const
       
   378     {
       
   379     return iRadioEnabled;
       
   380     }
       
   381 
       
   382 // ---------------------------------------------------------------------------
       
   383 // Sets the state for audio overriding
       
   384 // ---------------------------------------------------------------------------
       
   385 //
       
   386 void CRadioEngineImp::SetAudioOverride( TBool aOverride )
       
   387     {
       
   388     LOG_FORMAT( "CRadioEngineImp::SetAudioOverride( %d )", aOverride );
       
   389     iOverrideAudioResources = aOverride;
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // Adds an observer for the radio state changes notifications.
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 void CRadioEngineImp::AddObserverL( MRadioEngineObserver* aObserver )
       
   397     {
       
   398     LOG( "CRadioEngineImp::AddObserver" );
       
   399     TInt index = iObservers.FindInAddressOrder( aObserver );
       
   400     if ( index == KErrNotFound )
       
   401         {
       
   402         iObservers.InsertInAddressOrderL( aObserver );
       
   403         }
       
   404     }
       
   405 
       
   406 // ---------------------------------------------------------------------------
       
   407 // Removes an observer from the list of obsevers
       
   408 // ---------------------------------------------------------------------------
       
   409 //
       
   410 void CRadioEngineImp::RemoveObserver( MRadioEngineObserver* aObserver )
       
   411     {
       
   412     LOG( "CRadioEngineImp::RemoveObserver" );
       
   413     TInt index = iObservers.FindInAddressOrder( aObserver );
       
   414 
       
   415     if ( index >= 0 )
       
   416         {
       
   417         iObservers.Remove( index );
       
   418         }
       
   419     }
       
   420 
       
   421 // ---------------------------------------------------------------------------
       
   422 //
       
   423 // ---------------------------------------------------------------------------
       
   424 //
       
   425 TFmRadioFrequencyRange CRadioEngineImp::TunerFrequencyRangeForRegionId( TInt aRegionId ) const
       
   426     {
       
   427     TFmRadioFrequencyRange result = EFmRangeEuroAmerica;
       
   428     switch ( aRegionId )
       
   429         {
       
   430         case ERadioRegionDefault:
       
   431             {
       
   432             result = EFmRangeEuroAmerica;
       
   433             break;
       
   434             }
       
   435         case ERadioRegionJapan:
       
   436             {
       
   437             result = EFmRangeJapan;
       
   438             break;
       
   439             }
       
   440         case ERadioRegionAmerica:
       
   441             {
       
   442             result = EFmRangeEuroAmerica;
       
   443             break;
       
   444             }
       
   445         default:
       
   446             break;
       
   447         }
       
   448     return result;
       
   449     }
       
   450 
       
   451 // ---------------------------------------------------------------------------
       
   452 // Sets radio mode ERadioStereo or ERadioMono
       
   453 // ---------------------------------------------------------------------------
       
   454 
       
   455 void CRadioEngineImp::SetAudioMode( TInt aAudioMode )
       
   456     {
       
   457     LOG_FORMAT( "CRadioEngineImp::SetAudioMode: aAudioMode: %d", aAudioMode );
       
   458     TInt err = KErrNone;
       
   459     if ( !RadioInitialized() )
       
   460         {
       
   461         TInt err = iSettings->RadioSetter().SetOutputMode( aAudioMode );
       
   462         NotifyRadioEvent( ERadioEventAudioMode, err );
       
   463         }
       
   464     else
       
   465         {
       
   466         err = iTunerUtility->ForceMonoReception( aAudioMode == RadioEngine::ERadioMono );
       
   467 
       
   468         if ( err )
       
   469             {
       
   470             NotifyRadioEvent( ERadioEventAudioMode, err );
       
   471             }
       
   472         }
       
   473     }
       
   474 
       
   475 // ---------------------------------------------------------------------------
       
   476 // Switches power on/off after a delay
       
   477 // ---------------------------------------------------------------------------
       
   478 //
       
   479 void CRadioEngineImp::SwitchPower( TBool aPowerOn )
       
   480     {
       
   481     LOG_FORMAT( "CRadioEngineImp::SwitchPower( %d )", aPowerOn );
       
   482     if ( RadioInitialized() )
       
   483         {
       
   484         if ( !aPowerOn || OkToPlay( iSettings->EngineSettings().TunedFrequency() ) )
       
   485             {
       
   486             iRadioTimer->Cancel();
       
   487             if ( aPowerOn )
       
   488                 {
       
   489                 iRadioTimer->Start( TTimeIntervalMicroSeconds32( KRadioRadioSwitchDelay * KRadioThousand ),
       
   490                         TTimeIntervalMicroSeconds32( 0 ),
       
   491                         TCallBack( StaticPowerOnCallback, this ) );
       
   492                 }
       
   493             else
       
   494                 {
       
   495                 iRadioTimer->Start( TTimeIntervalMicroSeconds32( KRadioRadioSwitchDelay * KRadioThousand ),
       
   496                         TTimeIntervalMicroSeconds32( 0 ),
       
   497                         TCallBack( StaticPowerOffCallback, this ) );
       
   498                 }
       
   499             }
       
   500         }
       
   501     else if ( aPowerOn &&
       
   502             iRadioInitializationState == ERadioUtilitiesConstructed &&
       
   503             iRadioEnabled &&
       
   504             OkToPlay( iSettings->EngineSettings().TunedFrequency() ) )
       
   505         {
       
   506         iTunerUtility->RequestTunerControl();
       
   507         }
       
   508     else
       
   509         {
       
   510         LOG( "CRadioEngineImp::SwitchPower - Unhandled case" );
       
   511         LOG_FORMAT( "PowerOn: %d, InitializationState: %d, Enabled: %d, Frequency: %d",
       
   512                 aPowerOn, iRadioInitializationState, iRadioEnabled, iSettings->EngineSettings().TunedFrequency() );
       
   513         }
       
   514     }
       
   515 
       
   516 // ---------------------------------------------------------------------------
       
   517 // Executes the power switch
       
   518 // ---------------------------------------------------------------------------
       
   519 //
       
   520 TInt CRadioEngineImp::StaticPowerOnCallback( TAny* aSelfPtr )
       
   521     {
       
   522     LOG( "CRadioEngineImp::StaticPowerOnCallback" );
       
   523     CRadioEngineImp* self = reinterpret_cast<CRadioEngineImp*>( aSelfPtr );
       
   524 
       
   525     if ( self )
       
   526         {
       
   527         self->iRadioTimer->Cancel(); // prevents the further calls.
       
   528 
       
   529         if ( !self->iSettings->EngineSettings().IsPowerOn() )
       
   530             {
       
   531             self->PowerOn();
       
   532             }
       
   533         }
       
   534 
       
   535     return KErrNone;
       
   536     }
       
   537 
       
   538 // ---------------------------------------------------------------------------
       
   539 // Executes the power switch
       
   540 // ---------------------------------------------------------------------------
       
   541 //
       
   542 TInt CRadioEngineImp::StaticPowerOffCallback( TAny* aSelfPtr )
       
   543     {
       
   544     LOG( "CRadioEngineImp::StaticPowerOffCallback" );
       
   545     CRadioEngineImp* self = reinterpret_cast<CRadioEngineImp*>( aSelfPtr );
       
   546 
       
   547     if ( self )
       
   548         {
       
   549         self->iRadioTimer->Cancel(); // prevents the further calls.
       
   550 
       
   551         if ( self->iSettings->EngineSettings().IsPowerOn() )
       
   552             {
       
   553             self->PowerOff();
       
   554             }
       
   555         }
       
   556 
       
   557     return KErrNone;
       
   558     }
       
   559 
       
   560 // ---------------------------------------------------------------------------
       
   561 //
       
   562 // ---------------------------------------------------------------------------
       
   563 //
       
   564 void CRadioEngineImp::PowerOn()
       
   565     {
       
   566     LOG_METHOD_AUTO;
       
   567 
       
   568     if ( RadioInitialized() &&
       
   569         iRadioEnabled &&
       
   570         OkToPlay( iSettings->EngineSettings().TunedFrequency() ) )
       
   571         {
       
   572         SetAudioMode( iSettings->EngineSettings().OutputMode() );
       
   573         iPlayerUtility->SetVolumeRamp( TTimeIntervalMicroSeconds( MAKE_TINT64( 0, KRadioMillion ) ) );
       
   574         iPlayerUtility->SetVolume( TunerVolumeForUiVolume( iSettings->EngineSettings().Volume() ) );
       
   575 
       
   576         // If we are about to start scanning, mute the radio and set minimum frequency
       
   577         if ( iScanObserver )
       
   578             {
       
   579             iPlayerUtility->Mute( ETrue );
       
   580             iTunerUtility->SetFrequency( iSettings->EngineSettings().MinFrequency() );
       
   581             }
       
   582         else
       
   583             {
       
   584             iPlayerUtility->Mute( iSettings->EngineSettings().IsVolMuted() );
       
   585             iTunerUtility->SetFrequency( iSettings->EngineSettings().TunedFrequency() );
       
   586             }
       
   587         iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonImplicit;
       
   588 
       
   589         TRAP_IGNORE( iAudioRouter->SetAudioRouteL(
       
   590                 RadioEngine::TRadioAudioRoute( iSettings->EngineSettings().AudioRoute() ) ) )
       
   591 
       
   592         iPlayerUtility->Play();
       
   593         }
       
   594     else
       
   595         {
       
   596         HandlePowerEvent( EFalse, KErrGeneral );
       
   597         }
       
   598     }
       
   599 
       
   600 // ---------------------------------------------------------------------------
       
   601 // Radio power off
       
   602 // ---------------------------------------------------------------------------
       
   603 //
       
   604 void CRadioEngineImp::PowerOff()
       
   605     {
       
   606     LOG( "CRadioEngineImp::PowerOff" );
       
   607 
       
   608     if ( iSettings->EngineSettings().IsPowerOn() )
       
   609         {
       
   610         if ( RadioInitialized() )
       
   611             {
       
   612             iPlayerUtility->Stop();
       
   613             }
       
   614         else // This shouldn't occur normally, just a recovery action
       
   615             {
       
   616             HandlePowerEvent( EFalse, KErrNone );
       
   617             }
       
   618         }
       
   619     }
       
   620 
       
   621 // ---------------------------------------------------------------------------
       
   622 //
       
   623 // ---------------------------------------------------------------------------
       
   624 //
       
   625 TBool CRadioEngineImp::OkToPlay( TUint32 aFrequency ) const
       
   626     {
       
   627     TBool audioResourcesAvailable = iSystemEventCollector->IsAudioResourcesAvailable();
       
   628     TBool okToPlay = iAntennaAttached &&
       
   629                      !iFmTransmitterActive &&
       
   630                      ( audioResourcesAvailable || iOverrideAudioResources ) &&
       
   631 #ifdef COMPILE_IN_IVALO
       
   632                      IsFrequencyValid( aFrequency )
       
   633                      && !iSystemEventCollector->IsCallActive();
       
   634 #else
       
   635                      IsFrequencyValid( aFrequency );
       
   636 #endif //COMPILE_IN_IVALO
       
   637 
       
   638     LOG_FORMAT( "CRadioEngineImp::OkToPlay, returning %d ", okToPlay );
       
   639     return okToPlay;
       
   640     }
       
   641 
       
   642 // ---------------------------------------------------------------------------
       
   643 //
       
   644 // ---------------------------------------------------------------------------
       
   645 //
       
   646 TBool CRadioEngineImp::IsFrequencyValid( TUint32 aFrequency ) const
       
   647     {
       
   648     TBool ret( EFalse );
       
   649     if ( !aFrequency )
       
   650         {
       
   651         aFrequency = iSettings->EngineSettings().TunedFrequency();
       
   652         }
       
   653     if ( aFrequency >= iSettings->EngineSettings().MinFrequency() && aFrequency <= iSettings->EngineSettings().MaxFrequency() )
       
   654         {
       
   655         ret = ETrue;
       
   656         }
       
   657     return ret;
       
   658     }
       
   659 
       
   660 // ---------------------------------------------------------------------------
       
   661 // Radio tuning
       
   662 // ---------------------------------------------------------------------------
       
   663 //
       
   664 void CRadioEngineImp::SetFrequency( TUint32 aFrequency, RadioEngine::TRadioFrequencyEventReason aReason )
       
   665     {
       
   666     LOG_METHOD_AUTO;
       
   667     LOG_FORMAT( "CRadioEngineImp::SetFrequency, freq: %u, Initialized: %d, Enabled: %d",
       
   668             aFrequency, RadioInitialized(), iRadioEnabled );
       
   669 
       
   670     iFrequencySetByRdsAf = EFalse;
       
   671     iFreqEventReason = aReason;
       
   672 
       
   673     TInt frequency = 0;
       
   674     if ( iTunerUtility )
       
   675         {
       
   676         iTunerUtility->GetFrequency( frequency );
       
   677         }
       
   678     CancelSeek();
       
   679 
       
   680     if ( aFrequency == frequency ) //radio has already the frequency to be set.
       
   681         {
       
   682         LOG( "CRadioEngineImp::SetFrequency: Already at the requested frequency" );
       
   683         HandleFrequencyEvent( aFrequency );
       
   684         }
       
   685     else
       
   686         {
       
   687         iRadioTimer->Cancel();
       
   688         if ( RadioInitialized() && iRadioEnabled && OkToPlay( aFrequency ) )
       
   689             {
       
   690             LOG( "CRadioEngineImp::SetFrequency: Tuning to frequency" );
       
   691             iTunerUtility->SetFrequency( aFrequency );
       
   692             }
       
   693         else
       
   694             {
       
   695             HandleFrequencyEvent( aFrequency );
       
   696             }
       
   697         }
       
   698     }
       
   699 
       
   700 // ---------------------------------------------------------------------------
       
   701 // Radio tuning
       
   702 // ---------------------------------------------------------------------------
       
   703 //
       
   704 void CRadioEngineImp::SetFrequencyFast( TUint32 aFrequency,
       
   705                                         RadioEngine::TRadioFrequencyEventReason /*aReason*/ )
       
   706 {
       
   707     if ( iSeekingState != RadioEngine::ERadioNotSeeking )
       
   708     {
       
   709         iSeekingState = RadioEngine::ERadioNotSeeking;
       
   710         iTunerUtility->CancelStationSeek();
       
   711     }
       
   712     iTunerUtility->SetFrequency( aFrequency );
       
   713     iSettings->RadioSetter().SetTunedFrequency( aFrequency );
       
   714 }
       
   715 
       
   716 // ---------------------------------------------------------------------------
       
   717 // Frequency stepping
       
   718 // ---------------------------------------------------------------------------
       
   719 //
       
   720 void CRadioEngineImp::StepToFrequency( RadioEngine::TRadioTuneDirection aDirection )
       
   721     {
       
   722     LOG( "CRadioEngineImp::StepToFrequency" );
       
   723 
       
   724     TUint32 freq = iSettings->EngineSettings().TunedFrequency();
       
   725     RadioEngine::TRadioFrequencyEventReason reason( RadioEngine::ERadioFrequencyEventReasonUnknown );
       
   726     if ( aDirection == RadioEngine::ERadioUp )
       
   727         {
       
   728         freq = freq + iSettings->EngineSettings().FrequencyStepSize();
       
   729         reason = RadioEngine::ERadioFrequencyEventReasonUp;
       
   730         }
       
   731     else
       
   732         {
       
   733         freq = freq - iSettings->EngineSettings().FrequencyStepSize();
       
   734         reason = RadioEngine::ERadioFrequencyEventReasonDown;
       
   735         }
       
   736 
       
   737     // Check overflow or underflow
       
   738     if ( IsFrequencyValid( freq ) )
       
   739         {
       
   740         if ( aDirection == RadioEngine::ERadioUp )
       
   741             {
       
   742             freq = iSettings->EngineSettings().MinFrequency();
       
   743             }
       
   744         else
       
   745             {
       
   746             freq = iSettings->EngineSettings().MaxFrequency();
       
   747             }
       
   748         }
       
   749     SetFrequency( freq, reason );
       
   750     }
       
   751 
       
   752 // ---------------------------------------------------------------------------
       
   753 // Radio seek
       
   754 // ---------------------------------------------------------------------------
       
   755 //
       
   756 void CRadioEngineImp::Seek( RadioEngine::TRadioTuneDirection aDirection )
       
   757     {
       
   758     LOG_FORMAT( "CRadioEngineImp::Seek-- Start direction:%d",aDirection );
       
   759 
       
   760     iFrequencySetByRdsAf = EFalse;
       
   761 
       
   762     // Check if audio playing parameters ( other than frequency ) are OK
       
   763     if ( iRadioEnabled &&
       
   764         OkToPlay( iSettings->EngineSettings().MinFrequency() ) &&
       
   765         iSeekingState == RadioEngine::ERadioNotSeeking )
       
   766         {
       
   767         if ( aDirection == RadioEngine::ERadioDown )
       
   768             {
       
   769             iSeekingState = RadioEngine::ERadioSeekingDown;
       
   770             iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonDown;
       
   771             }
       
   772         else
       
   773             {
       
   774             iSeekingState = RadioEngine::ERadioSeekingUp;
       
   775             iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonUp;
       
   776             }
       
   777 
       
   778         NotifyRadioEvent( ERadioEventSeeking );
       
   779 
       
   780         if ( IsFrequencyValid() )
       
   781             {
       
   782             if ( iSettings->EngineSettings().IsPowerOn() )
       
   783                 {
       
   784                 iTunerUtility->StationSeek( aDirection == RadioEngine::ERadioUp ? ETrue : EFalse );
       
   785                 }
       
   786             else
       
   787                 {
       
   788                 // Try to switch power on ( reinitialization )
       
   789                 SwitchPower( ETrue );
       
   790                 }
       
   791             }
       
   792         else
       
   793             {
       
   794             // Tune first to valid frequency, start seeking after radio power is on
       
   795             SetFrequency( iSettings->EngineSettings().MinFrequency(), RadioEngine::ERadioFrequencyEventReasonImplicit );
       
   796             }
       
   797         }
       
   798     else
       
   799         {
       
   800         NotifyRadioEvent( ERadioEventSeeking, KErrGeneral );
       
   801         StopScan( KErrGeneral );
       
   802         }
       
   803     }
       
   804 
       
   805 // ---------------------------------------------------------------------------
       
   806 // Cancels seek up/down request
       
   807 // ---------------------------------------------------------------------------
       
   808 //
       
   809 void CRadioEngineImp::CancelSeek()
       
   810     {
       
   811     LOG_FORMAT( "CRadioEngineImp::CancelSeek -- seeking state was:%d", iSeekingState );
       
   812 
       
   813     if ( !iScanObserver )
       
   814         {
       
   815         if ( iSeekingState != RadioEngine::ERadioNotSeeking )
       
   816             {
       
   817             iSeekingState = RadioEngine::ERadioNotSeeking;
       
   818             iTunerUtility->CancelStationSeek();
       
   819             iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonImplicit;
       
   820             NotifyRadioEvent( ERadioEventSeeking, KErrCancel );
       
   821             NotifyRadioEvent( ERadioEventFrequency, KErrNone ); // Notify the observers even if the frequency remains the same.
       
   822             }
       
   823         }
       
   824     else
       
   825         {
       
   826         StopScan( KErrCancel );
       
   827         }
       
   828     }
       
   829 
       
   830 // ---------------------------------------------------------------------------
       
   831 //
       
   832 // ---------------------------------------------------------------------------
       
   833 //
       
   834 RadioEngine::TRadioSeeking CRadioEngineImp::Seeking() const
       
   835     {
       
   836     return iSeekingState;
       
   837     }
       
   838 
       
   839 // ---------------------------------------------------------------------------
       
   840 // Starts scanning all available stations from the minimum frequency
       
   841 // ---------------------------------------------------------------------------
       
   842 //
       
   843 void CRadioEngineImp::StartScan( MRadioScanObserver& aObserver )
       
   844     {
       
   845     LOG( "CRadioEngineImp::StartScan" );
       
   846 
       
   847     const TUint32 minFrequency = iSettings->EngineSettings().MinFrequency();
       
   848     if ( !iScanObserver && iRadioEnabled && RadioInitialized() && OkToPlay( minFrequency ) )
       
   849         {
       
   850         CancelSeek();
       
   851         iScanObserver = &aObserver;
       
   852         iPreviousMuteState = iSettings->EngineSettings().IsVolMuted();
       
   853         iPreviousScannedFrequency = 0;
       
   854         iPlayerUtility->Mute( ETrue );
       
   855         iTunerUtility->SetFrequency( minFrequency );
       
   856         Seek( RadioEngine::ERadioUp );
       
   857         }
       
   858     else
       
   859         {
       
   860         TInt error = iScanObserver ? KErrAlreadyExists : KErrNotReady;
       
   861         TRAP_IGNORE( aObserver.ScanCompletedEventL( error ) )
       
   862         }
       
   863     }
       
   864 
       
   865 // ---------------------------------------------------------------------------
       
   866 // Stops any scans currently in progress.
       
   867 // ---------------------------------------------------------------------------
       
   868 //
       
   869 void CRadioEngineImp::StopScan( TInt aError )
       
   870     {
       
   871     LOG_FORMAT( "CRadioEngineImp::StopScan, error: %d", aError );
       
   872     if ( iScanObserver )
       
   873         {
       
   874         if ( iSeekingState != RadioEngine::ERadioNotSeeking )
       
   875             {
       
   876             iSeekingState = RadioEngine::ERadioNotSeeking;
       
   877             if ( RadioInitialized() )
       
   878                 {
       
   879                 iTunerUtility->CancelStationSeek();
       
   880                 }
       
   881             }
       
   882 
       
   883         iPreviousScannedFrequency = 0;
       
   884         MRadioScanObserver& observer = *iScanObserver;
       
   885         iScanObserver = NULL;
       
   886         NotifyRadioScanEvent( ERadioEventScanCompleted, observer, aError );
       
   887 
       
   888         if ( !OkToPlay( iSettings->EngineSettings().MinFrequency() ) )
       
   889             {
       
   890             // Try to reset the frequency as sometimes extra frequency event occurs after seeking
       
   891             iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonImplicit;
       
   892             iTunerUtility->SetFrequency( iSettings->EngineSettings().TunedFrequency() );
       
   893             }
       
   894 
       
   895         if ( iPlayerUtility && OkToPlay( iSettings->EngineSettings().TunedFrequency() ) )
       
   896             {
       
   897             iPlayerUtility->Mute( iPreviousMuteState );
       
   898             }
       
   899         }
       
   900     }
       
   901 
       
   902 // ---------------------------------------------------------------------------
       
   903 // Sets volume level up/down one step.
       
   904 // ---------------------------------------------------------------------------
       
   905 //
       
   906 void CRadioEngineImp::AdjustVolume( RadioEngine::TRadioVolumeSetDirection aDirection )
       
   907     {
       
   908     LOG_FORMAT( "CRadioEngineImp::AdjustVolume( %d )", aDirection );
       
   909 
       
   910     if ( iSettings->EngineSettings().IsPowerOn() )
       
   911         {
       
   912         TInt volume = iSettings->EngineSettings().Volume();
       
   913         LOG_FORMAT( "CRadioEngineImp::AdjustVolume volume = ( %d )", volume );
       
   914 
       
   915         if ( aDirection == RadioEngine::ERadioDecVolume )
       
   916             {
       
   917             TInt min = iSettings->EngineSettings().DefaultMinVolumeLevel();
       
   918             if ( --volume < min )
       
   919                 {
       
   920                 volume = min;
       
   921                 }
       
   922             }
       
   923         else if ( aDirection == RadioEngine::ERadioIncVolume )
       
   924             {
       
   925             TInt max = MaxVolumeLevel();
       
   926 
       
   927             if ( ++volume > max )
       
   928                 {
       
   929                 volume = max;
       
   930                 }
       
   931             }
       
   932         else
       
   933             {
       
   934             LOG( "CRadioEngineImp::AdjustVolume - Unhandled case" );
       
   935             }
       
   936         SetVolume( volume );
       
   937         }
       
   938     }
       
   939 
       
   940 // ---------------------------------------------------------------------------
       
   941 // Sets audio volume level
       
   942 // ---------------------------------------------------------------------------
       
   943 //
       
   944 void CRadioEngineImp::SetVolume( TInt aVolume )
       
   945     {
       
   946     LOG_FORMAT( "CRadioEngineImp::SetVolume vol: %d", aVolume );
       
   947 
       
   948     if ( iSettings->EngineSettings().IsPowerOn() && RadioInitialized() )
       
   949         {
       
   950         LOG( "CRadioEngineImp::SetVolume: Setting volume to player utility" );
       
   951 
       
   952         if ( aVolume == 0 )
       
   953             {
       
   954             SetVolumeMuted( ETrue );
       
   955             }
       
   956         else
       
   957             {
       
   958             if ( iSettings->EngineSettings().IsVolMuted() )
       
   959                 {
       
   960                 SetVolumeMuted( EFalse );
       
   961                 }
       
   962             else if ( iPlayerUtility->SetVolume( TunerVolumeForUiVolume( aVolume ) ) == KErrNone )
       
   963                 {
       
   964                 iSettings->RadioSetter().SetVolume( aVolume );
       
   965                 NotifyRadioEvent( ERadioEventVolume, KErrNone );
       
   966                 }
       
   967             }
       
   968         }
       
   969     }
       
   970 
       
   971 // ---------------------------------------------------------------------------
       
   972 // Set radio audio muted/unmuted
       
   973 // ---------------------------------------------------------------------------
       
   974 //
       
   975 void CRadioEngineImp::SetVolumeMuted( TBool aMuteState )
       
   976     {
       
   977     LOG_METHOD_AUTO;
       
   978     LOG_FORMAT( "MuteState = %d", aMuteState );
       
   979 
       
   980     if ( iSettings->EngineSettings().IsPowerOn() )
       
   981         {
       
   982         TInt err = KErrNone;
       
   983         if ( RadioInitialized() )
       
   984             {
       
   985             err = iPlayerUtility->Mute( aMuteState );
       
   986             iSettings->RadioSetter().SetVolMuted( aMuteState );
       
   987             NotifyRadioEvent( ERadioEventMute, err );
       
   988             }
       
   989         else
       
   990             {
       
   991             err = iSettings->RadioSetter().SetVolMuted( aMuteState );
       
   992             NotifyRadioEvent( ERadioEventMute, err );
       
   993             }
       
   994         }
       
   995     }
       
   996 
       
   997 // ---------------------------------------------------------------------------
       
   998 //
       
   999 // ---------------------------------------------------------------------------
       
  1000 //
       
  1001 TBool CRadioEngineImp::IsAntennaAttached()
       
  1002     {
       
  1003     LOG_FORMAT( "CRadioEngineImp::IsAntennaAttached, returning %d", iAntennaAttached );
       
  1004     return iAntennaAttached;
       
  1005     }
       
  1006 
       
  1007 // ---------------------------------------------------------------------------
       
  1008 //
       
  1009 // ---------------------------------------------------------------------------
       
  1010 //
       
  1011 TBool CRadioEngineImp::IsFmTransmitterActive() const
       
  1012     {
       
  1013     LOG_FORMAT( "CRadioEngineImp::IsFmTransmitterActive, returning %d", iFmTransmitterActive );
       
  1014     return iFmTransmitterActive;
       
  1015     }
       
  1016 
       
  1017 // ---------------------------------------------------------------------------
       
  1018 // This function is usable only in WINS emulator.
       
  1019 // ---------------------------------------------------------------------------
       
  1020 //
       
  1021 void CRadioEngineImp::SetAntennaAttached( TBool aAntennaAttached )
       
  1022     {
       
  1023 #ifdef __WINS__
       
  1024     MrftoAntennaStatusChange( aAntennaAttached );
       
  1025 #endif
       
  1026     }
       
  1027 
       
  1028 // ---------------------------------------------------------------------------
       
  1029 // If CRadioPlayerUtility has 10 steps, CAknVolumeControl has also 10 steps and also
       
  1030 // the maximum volume level of Visual radio is 10. Otherwise CRadioPlayerUtility has
       
  1031 // 200 steps, and CAknVolumeControl has 20 steps, so maximum volume level of Visual radio
       
  1032 // is 20.
       
  1033 // ---------------------------------------------------------------------------
       
  1034 //
       
  1035 TInt CRadioEngineImp::MaxVolumeLevel() const
       
  1036     {
       
  1037     TInt maxLevel = 0;
       
  1038 
       
  1039 #if defined __WINS__
       
  1040     maxLevel = KRadioVolumeStepsOld;
       
  1041 #else
       
  1042     if ( RadioInitialized() )
       
  1043         {
       
  1044         iPlayerUtility->GetMaxVolume( maxLevel );
       
  1045         }
       
  1046 #endif //defined __WINS__
       
  1047 
       
  1048     if ( maxLevel > KRadioVolumeStepsDividinglimit )
       
  1049         {
       
  1050         maxLevel = maxLevel / KRadioVolumeStepsDivider;
       
  1051         }
       
  1052 
       
  1053     return maxLevel;
       
  1054     }
       
  1055 
       
  1056 // ---------------------------------------------------------------------------
       
  1057 //
       
  1058 // ---------------------------------------------------------------------------
       
  1059 //
       
  1060 TBool CRadioEngineImp::FrequencySetByRdsAf() const
       
  1061     {
       
  1062     return iFrequencySetByRdsAf;
       
  1063     }
       
  1064 
       
  1065 // ---------------------------------------------------------------------------
       
  1066 //
       
  1067 // ---------------------------------------------------------------------------
       
  1068 //
       
  1069 MRadioRdsReceiver& CRadioEngineImp::RdsReceiver()
       
  1070     {
       
  1071     return *iRdsReceiver;
       
  1072     }
       
  1073 
       
  1074 // ---------------------------------------------------------------------------
       
  1075 //
       
  1076 // ---------------------------------------------------------------------------
       
  1077 //
       
  1078 TInt CRadioEngineImp::TunerVolumeForUiVolume( TInt aUiVolume )
       
  1079     {
       
  1080     TInt vol = aUiVolume * KRadioVolumeStepsDivider;
       
  1081 
       
  1082     return vol;
       
  1083     }
       
  1084 
       
  1085 // ---------------------------------------------------------------------------
       
  1086 //
       
  1087 // ---------------------------------------------------------------------------
       
  1088 //
       
  1089 void CRadioEngineImp::NotifyRadioEvent( TInt aRadioEvent, TInt aErrorCode )
       
  1090     {
       
  1091     TRAP_IGNORE( DoNotifyRadioEventL( aRadioEvent, aErrorCode ) )
       
  1092     }
       
  1093 
       
  1094 // ---------------------------------------------------------------------------
       
  1095 // Notifies all the registered observers of radio events
       
  1096 // by sending notifications to observers.
       
  1097 // ---------------------------------------------------------------------------
       
  1098 //
       
  1099 void CRadioEngineImp::DoNotifyRadioEventL( TInt aRadioEvent, TInt aErrorCode )
       
  1100     {
       
  1101     TInt count = iObservers.Count();
       
  1102 
       
  1103     for ( TInt i = 0; i<count; i++)
       
  1104         {
       
  1105         MRadioEngineObserver * observer = iObservers[i];
       
  1106 
       
  1107         switch ( aRadioEvent )
       
  1108             {
       
  1109             case ERadioEventPower:
       
  1110                 {
       
  1111                 observer->PowerEventL( iSettings->EngineSettings().IsPowerOn(), aErrorCode );
       
  1112                 break;
       
  1113                 }
       
  1114             case ERadioEventFrequency:
       
  1115                 {
       
  1116                 observer->FrequencyEventL( iSettings->EngineSettings().TunedFrequency(),
       
  1117                                            iFreqEventReason, aErrorCode );
       
  1118                 break;
       
  1119                 }
       
  1120             case ERadioEventVolume:
       
  1121                 {
       
  1122                 observer->VolumeEventL( iSettings->EngineSettings().Volume(), aErrorCode );
       
  1123                 break;
       
  1124                 }
       
  1125             case ERadioEventMute:
       
  1126                 {
       
  1127                 if ( !iScanObserver )
       
  1128                     {
       
  1129                     observer->MuteEventL( iSettings->EngineSettings().IsVolMuted(), aErrorCode );
       
  1130                     }
       
  1131                 break;
       
  1132                 }
       
  1133             case ERadioEventAudioMode:
       
  1134                 {
       
  1135                 observer->AudioModeEventL( iSettings->EngineSettings().OutputMode(), aErrorCode );
       
  1136                 break;
       
  1137                 }
       
  1138             case ERadioEventAntenna:
       
  1139                 {
       
  1140                 observer->AntennaEventL( iAntennaAttached, aErrorCode );
       
  1141                 break;
       
  1142                 }
       
  1143             case ERadioEventAudioRouting:
       
  1144                 {
       
  1145                 observer->AudioRoutingEventL( iSettings->EngineSettings().AudioRoute(), aErrorCode );
       
  1146                 break;
       
  1147                 }
       
  1148             case ERadioEventSeeking:
       
  1149                 {
       
  1150                 observer->SeekingEventL( iSeekingState, aErrorCode );
       
  1151                 break;
       
  1152                 }
       
  1153             case ERadioEventRegion:
       
  1154                 {
       
  1155                 observer->RegionEventL( iSettings->EngineSettings().RegionId(), aErrorCode );
       
  1156                 break;
       
  1157                 }
       
  1158             case ERadioEventFmTransmitter:
       
  1159                 {
       
  1160                 observer->FmTransmitterEventL( iFmTransmitterActive );
       
  1161                 break;
       
  1162                 }
       
  1163             default:
       
  1164                 {
       
  1165                 break;
       
  1166                 }
       
  1167             }
       
  1168         }
       
  1169     }
       
  1170 
       
  1171 // ---------------------------------------------------------------------------
       
  1172 // Notifies the observer of a radio scan event.
       
  1173 // ---------------------------------------------------------------------------
       
  1174 //
       
  1175 void CRadioEngineImp::NotifyRadioScanEvent( TRadioScanEvent aEvent,
       
  1176                                             MRadioScanObserver& aObserver,
       
  1177                                             TInt aError  )
       
  1178     {
       
  1179     TRAP_IGNORE( DoNotifyRadioScanEventL( aEvent, aObserver, aError ) )
       
  1180     }
       
  1181 
       
  1182 // ---------------------------------------------------------------------------
       
  1183 // Notifies the observer of a radio scan event.
       
  1184 // ---------------------------------------------------------------------------
       
  1185 //
       
  1186 void CRadioEngineImp::DoNotifyRadioScanEventL( TRadioScanEvent aEvent,
       
  1187                                                MRadioScanObserver& aObserver,
       
  1188                                                TInt aError )
       
  1189     {
       
  1190     LOG_FORMAT( "CRadioEngineImp::DoNotifyRadioScanEventL, aEvent: %d, aError: %d", aEvent, aError );
       
  1191 
       
  1192     if ( aEvent == ERadioEventFrequencyScanned )
       
  1193         {
       
  1194         if ( aError == KErrNone )
       
  1195             {
       
  1196             aObserver.ScanFrequencyEventL( iPreviousScannedFrequency );
       
  1197             }
       
  1198         }
       
  1199     else if ( aEvent == ERadioEventScanCompleted )
       
  1200         {
       
  1201         // KFmRadioErrTuning error means that no channels can be found anymore
       
  1202         if ( aError == KFmRadioErrTuning )
       
  1203             {
       
  1204             aError = KErrNotFound;
       
  1205             }
       
  1206 
       
  1207         aObserver.ScanCompletedEventL( aError );
       
  1208         }
       
  1209     }
       
  1210 
       
  1211 // ---------------------------------------------------------------------------
       
  1212 // Handles some system events
       
  1213 // ---------------------------------------------------------------------------
       
  1214 //
       
  1215 void CRadioEngineImp::HandleSystemEventL( TRadioSystemEventType aEventType )
       
  1216     {
       
  1217     LOG_FORMAT( "CRadioEngineImp::HandleSystemEventL, aEventType = %d", aEventType );
       
  1218 
       
  1219     switch ( aEventType )
       
  1220         {
       
  1221         case ERadioAudioResourcesAvailable:
       
  1222             {
       
  1223             LOG( "CRadioEngineImp::HandleSystemEventL, Audio resources available" );
       
  1224             SwitchPower( ETrue );
       
  1225             }
       
  1226             break;
       
  1227 
       
  1228         case ERadioAudioAutoResumeForbidden:
       
  1229             {
       
  1230             EnableAudio( EFalse );
       
  1231             }
       
  1232             break;
       
  1233 
       
  1234         case ERadioAudioRouteHeadset:
       
  1235             {
       
  1236             HandleAudioRoutingEvent( RadioEngine::ERadioHeadset );
       
  1237             }
       
  1238             break;
       
  1239 
       
  1240         case ERadioAudioRouteSpeaker:
       
  1241             {
       
  1242             HandleAudioRoutingEvent( RadioEngine::ERadioSpeaker );
       
  1243             }
       
  1244             break;
       
  1245 
       
  1246         case ERadioCallActivated:
       
  1247             {
       
  1248             StopScan( KErrGeneral );
       
  1249             CancelSeek();
       
  1250             }
       
  1251             break;
       
  1252 
       
  1253         case ERadioCallDeactivated:
       
  1254             {
       
  1255             SwitchPower( ETrue );
       
  1256             }
       
  1257             break;
       
  1258 
       
  1259         case ERadioHeadsetConnected:
       
  1260             {
       
  1261             // Explicitly set the audio routing to headset. Because system
       
  1262             // forces the routing to headset anyway, and without our knowledge.
       
  1263             iAudioRouter->SetAudioRouteL( RadioEngine::ERadioHeadset );
       
  1264             }
       
  1265             break;
       
  1266 
       
  1267         case ERadioHeadsetDisconnected:
       
  1268             {
       
  1269             // Explicitly set the audio routing to speaker. Because system
       
  1270             // forces the routing to speaker anyway, if radio stays on.
       
  1271             iAudioRouter->SetAudioRouteL( RadioEngine::ERadioSpeaker );
       
  1272             }
       
  1273             break;
       
  1274         default:
       
  1275             {
       
  1276             break;
       
  1277             }
       
  1278         }
       
  1279     }
       
  1280 
       
  1281 // ---------------------------------------------------------------------------
       
  1282 //
       
  1283 // ---------------------------------------------------------------------------
       
  1284 //
       
  1285 void CRadioEngineImp::MrftoRequestTunerControlComplete( TInt aError )
       
  1286     {
       
  1287     LOG_FORMAT( "CRadioEngineImp::MrftoRequestTunerControlComplete( %d )", aError );
       
  1288 
       
  1289     if ( aError == KErrNone || aError == KErrAlreadyExists ) // Tuner activated now or was already active
       
  1290         {
       
  1291         iRadioInitializationState = ERadioTunerControlGranted;
       
  1292 
       
  1293         TFmTunerCapabilities tunerCaps;
       
  1294         tunerCaps.iTunerBands = 0;
       
  1295         tunerCaps.iTunerFunctions = 0;
       
  1296         tunerCaps.iAdditionalFunctions1 = 0;
       
  1297         tunerCaps.iAdditionalFunctions2 = 0;
       
  1298         iTunerUtility->GetCapabilities( tunerCaps );
       
  1299 
       
  1300 #ifdef LOGGING_ENABLED
       
  1301         TBuf<50> tunerCapsBuf;
       
  1302         if ( tunerCaps.ETunerAvailableInOfflineMode )
       
  1303             {
       
  1304             tunerCapsBuf.Append( _L("OfflineMode ") );
       
  1305             }
       
  1306         if ( tunerCaps.ETunerRdsSupport )
       
  1307             {
       
  1308             tunerCapsBuf.Append( _L("RDS ") );
       
  1309             }
       
  1310         if ( tunerCaps.ETunerDualTunerSupport )
       
  1311             {
       
  1312             tunerCapsBuf.Append( _L("DualTuner ") );
       
  1313             }
       
  1314         LOG_FORMAT( "Radio tuner capabilities: %S", &tunerCapsBuf );
       
  1315 
       
  1316 #endif // LOGGING_ENABLED
       
  1317 
       
  1318         TBool offlineAvailable( tunerCaps.iTunerFunctions &
       
  1319                                 TFmTunerCapabilities::ETunerAvailableInOfflineMode );
       
  1320         iTunerUtility->EnableTunerInOfflineMode( offlineAvailable );
       
  1321 
       
  1322         TBool rdsSupported = EFalse;
       
  1323         TRAP_IGNORE( CRadioRepositoryManager::GetRepositoryValueL( KRadioCRUid, KRadioCRRdsSupport,
       
  1324                                                                    rdsSupported ) )
       
  1325         if ( tunerCaps.iTunerFunctions & TFmTunerCapabilities::ETunerRdsSupport &&
       
  1326                 rdsSupported )
       
  1327             {
       
  1328             TRAP_IGNORE( iRdsReceiver->InitL( *iRadioUtility, iPubSub ) )
       
  1329             }
       
  1330 
       
  1331         iTunerUtility->SetFrequencyRange( TunerFrequencyRangeForRegionId(
       
  1332                                           iSettings->EngineSettings().RegionId() ) );
       
  1333         // To prevent false frequency changes
       
  1334         iTunerUtility->SetFrequency( iSettings->EngineSettings().TunedFrequency() );
       
  1335         SwitchPower( ETrue );
       
  1336         }
       
  1337     }
       
  1338 
       
  1339 // ---------------------------------------------------------------------------
       
  1340 //
       
  1341 // ---------------------------------------------------------------------------
       
  1342 //
       
  1343 void CRadioEngineImp::MrftoSetFrequencyRangeComplete( TInt aError )
       
  1344     {
       
  1345     LOG_FORMAT( "CRadioEngineImp::MrftoSetFrequencyRangeComplete( %d )", aError );
       
  1346     if ( aError )
       
  1347         {
       
  1348         NotifyRadioEvent( ERadioEventRegion, aError );
       
  1349         }
       
  1350     }
       
  1351 
       
  1352 // ---------------------------------------------------------------------------
       
  1353 //
       
  1354 // ---------------------------------------------------------------------------
       
  1355 //
       
  1356 void CRadioEngineImp::MrftoSetFrequencyComplete( TInt aError )
       
  1357     {
       
  1358     LOG_FORMAT( "CRadioEngineImp::MrftoSetFrequencyComplete: Err: %d", aError );
       
  1359 
       
  1360     if ( aError )
       
  1361         {
       
  1362         if ( aError == KErrNotReady )
       
  1363             {
       
  1364             iRadioInitializationState = ERadioUtilitiesConstructed;
       
  1365             }
       
  1366         HandleFrequencyEvent( iSettings->EngineSettings().TunedFrequency(), aError );
       
  1367         }
       
  1368     }
       
  1369 
       
  1370 // ---------------------------------------------------------------------------
       
  1371 //
       
  1372 // ---------------------------------------------------------------------------
       
  1373 //
       
  1374 void CRadioEngineImp::MrftoStationSeekComplete( TInt aError, TInt aFrequency )
       
  1375     {
       
  1376     LOG_FORMAT( "CRadioEngineImp::MrftoStationSeekComplete() -- aError = %d, aFrequency = %d", aError, aFrequency );
       
  1377     // Seeking has ended, error code tells if it was successful
       
  1378 
       
  1379     if ( aError == KFmRadioErrAntennaNotConnected )
       
  1380         {
       
  1381         iAntennaAttached = EFalse;
       
  1382         }
       
  1383     else if ( aError == KErrNotReady )
       
  1384         {
       
  1385         iRadioInitializationState = ERadioUtilitiesConstructed;
       
  1386         }
       
  1387 
       
  1388     iSeekingState = RadioEngine::ERadioNotSeeking;
       
  1389 
       
  1390     NotifyRadioEvent( ERadioEventSeeking, aError );
       
  1391 
       
  1392 //    if ( aError != KErrNone )
       
  1393 //        {
       
  1394 //        iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonImplicit;
       
  1395 //        NotifyRadioEvent( ERadioEventFrequency, KErrNone ); // Frequency change is not otherwise notified when seeking fails.
       
  1396 //        }
       
  1397 //    else
       
  1398 //        {
       
  1399 //        // sometimes frequency change is not reported even if seeking succeeds
       
  1400 //        if ( !iFrequencySetByRdsAf )
       
  1401 //            {
       
  1402 //            NotifyRadioEvent( ERadioEventFrequency, KErrNone );
       
  1403 //            }
       
  1404 //        }
       
  1405 
       
  1406     if ( iScanObserver )
       
  1407         {
       
  1408         if ( aError != KErrNone )
       
  1409             {
       
  1410             StopScan( aError );
       
  1411             }
       
  1412         else
       
  1413             {
       
  1414             if ( aFrequency > iSettings->EngineSettings().MinFrequency() && aFrequency > iPreviousScannedFrequency )
       
  1415                 {
       
  1416                 iPreviousScannedFrequency = aFrequency;
       
  1417                 NotifyRadioScanEvent( ERadioEventFrequencyScanned, *iScanObserver, KErrNone );
       
  1418                 Seek( RadioEngine::ERadioUp ); // Continue scanning.
       
  1419                 }
       
  1420             else
       
  1421                 {
       
  1422                 if ( aFrequency == iSettings->EngineSettings().MinFrequency() )
       
  1423                     {
       
  1424                     iPreviousScannedFrequency = aFrequency;
       
  1425                     NotifyRadioScanEvent( ERadioEventFrequencyScanned, *iScanObserver, KErrNone );
       
  1426                     }
       
  1427                 StopScan( aError );
       
  1428                 }
       
  1429             }
       
  1430         }
       
  1431     }
       
  1432 
       
  1433 // ---------------------------------------------------------------------------
       
  1434 //
       
  1435 // ---------------------------------------------------------------------------
       
  1436 //
       
  1437 void CRadioEngineImp::MrftoFmTransmitterStatusChange( TBool aActive )
       
  1438     {
       
  1439     LOG_FORMAT( "CRadioEngineImp::MrftoFmTransmitterStatusChange( %d )", aActive );
       
  1440     iFmTransmitterActive = aActive;
       
  1441 
       
  1442     if ( !iFmTransmitterActive )
       
  1443         {
       
  1444         SwitchPower( ETrue );
       
  1445         }
       
  1446 
       
  1447     NotifyRadioEvent( ERadioEventFmTransmitter );
       
  1448     }
       
  1449 
       
  1450 // ---------------------------------------------------------------------------
       
  1451 //
       
  1452 // ---------------------------------------------------------------------------
       
  1453 //
       
  1454 void CRadioEngineImp::MrftoAntennaStatusChange( TBool aAttached )
       
  1455     {
       
  1456     LOG_FORMAT( "CRadioEngineImp::MrftoAntennaStatusChange( %d )", aAttached );
       
  1457     iAntennaAttached = aAttached;
       
  1458     if ( iAntennaAttached )
       
  1459         {
       
  1460         SwitchPower( ETrue );
       
  1461         NotifyRadioEvent( ERadioEventAntenna );
       
  1462         iPubSub->PublishAntennaState( ERadioPSRadioAntennaAttached );
       
  1463         }
       
  1464     else
       
  1465         {
       
  1466         NotifyRadioEvent( ERadioEventAntenna, KErrDisconnected );
       
  1467         iPubSub->PublishAntennaState( ERadioPSRadioAntennaDetached );
       
  1468         }
       
  1469     }
       
  1470 
       
  1471 // ---------------------------------------------------------------------------
       
  1472 //
       
  1473 // ---------------------------------------------------------------------------
       
  1474 
       
  1475 //
       
  1476 void CRadioEngineImp::MrftoOfflineModeStatusChange( TBool DEBUGVAR( aOfflineMode ) )
       
  1477     {
       
  1478     LOG_FORMAT( "CRadioEngineImp::MrftoOfflineModeStatusChange( %d )", aOfflineMode );
       
  1479     }
       
  1480 
       
  1481 // ---------------------------------------------------------------------------
       
  1482 //
       
  1483 // ---------------------------------------------------------------------------
       
  1484 //
       
  1485 void CRadioEngineImp::MrftoFrequencyRangeChange( TFmRadioFrequencyRange DEBUGVAR( aBand ) )
       
  1486     {
       
  1487     LOG_FORMAT( "CRadioEngineImp::MrftoFrequencyRangeChange( %d )", aBand );
       
  1488     if ( RadioInitialized() )
       
  1489         {
       
  1490         iTunerUtility->SetFrequency( iSettings->EngineSettings().TunedFrequency() );
       
  1491         iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonImplicit;
       
  1492         }
       
  1493     NotifyRadioEvent( ERadioEventRegion );
       
  1494     }
       
  1495 
       
  1496 // ---------------------------------------------------------------------------
       
  1497 //
       
  1498 // ---------------------------------------------------------------------------
       
  1499 //
       
  1500 void CRadioEngineImp::MrftoFrequencyChange( TInt aNewFrequency )
       
  1501     {
       
  1502     LOG_FORMAT( "CRadioEngineImp::MrftoFrequencyChange aNewFrequency = %u", aNewFrequency );
       
  1503 
       
  1504     // There may be frequency changed events when radio is not initialized ( because
       
  1505     // of SetFrequency or Seek returns with KErrNotReady ).
       
  1506     if ( RadioInitialized() )
       
  1507         {
       
  1508       
       
  1509         HandleFrequencyEvent( aNewFrequency );
       
  1510         }
       
  1511     }
       
  1512 
       
  1513 // ---------------------------------------------------------------------------
       
  1514 //
       
  1515 // ---------------------------------------------------------------------------
       
  1516 //
       
  1517 void CRadioEngineImp::MrftoForcedMonoChange( TBool aForcedMono )
       
  1518     {
       
  1519     LOG_FORMAT( "CRadioEngineImp::MrftoForcedMonoChange -- aForcedMono = %d", aForcedMono );
       
  1520 
       
  1521     iSettings->RadioSetter().SetOutputMode( aForcedMono ? RadioEngine::ERadioMono : RadioEngine::ERadioStereo );
       
  1522     NotifyRadioEvent( ERadioEventAudioMode );
       
  1523     }
       
  1524 
       
  1525 // ---------------------------------------------------------------------------
       
  1526 //
       
  1527 // ---------------------------------------------------------------------------
       
  1528 //
       
  1529 void CRadioEngineImp::MrftoSquelchChange( TBool /*aSquelch*/ )
       
  1530     {
       
  1531     }
       
  1532 
       
  1533 // ---------------------------------------------------------------------------
       
  1534 //
       
  1535 // ---------------------------------------------------------------------------
       
  1536 //
       
  1537 void CRadioEngineImp::MrpoStateChange( TPlayerState aState, TInt aError )
       
  1538     {
       
  1539     LOG_FORMAT( "CRadioEngineImp::MrpoStateChange() -- aState = %d, aError = %d", aState, aError );
       
  1540 
       
  1541     if ( aError == KFmRadioErrAntennaNotConnected )
       
  1542         {
       
  1543         iAntennaAttached = EFalse;
       
  1544         }
       
  1545     HandlePowerEvent( aState == ERadioPlayerPlaying, aError );
       
  1546     }
       
  1547 
       
  1548 // ---------------------------------------------------------------------------
       
  1549 //
       
  1550 // ---------------------------------------------------------------------------
       
  1551 //
       
  1552 void CRadioEngineImp::MrpoVolumeChange( TInt aVolume )
       
  1553     {
       
  1554     aVolume = aVolume / KRadioVolumeStepsDivider;
       
  1555     LOG_FORMAT( "CRadioEngineImp::MrpoVolumeChange() --  volume = %d", aVolume );
       
  1556     if ( aVolume != iSettings->EngineSettings().Volume() )
       
  1557         {
       
  1558         iSettings->RadioSetter().SetVolume( aVolume );
       
  1559         NotifyRadioEvent( ERadioEventVolume );
       
  1560         }
       
  1561     }
       
  1562 
       
  1563 // ---------------------------------------------------------------------------
       
  1564 //
       
  1565 // ---------------------------------------------------------------------------
       
  1566 //
       
  1567 void CRadioEngineImp::MrpoMuteChange( TBool aMute )
       
  1568     {
       
  1569     TBool muted = iSettings->EngineSettings().IsVolMuted();
       
  1570     if ( !iScanObserver && !aMute != !muted )
       
  1571         {
       
  1572         iSettings->RadioSetter().SetVolMuted( aMute );
       
  1573         NotifyRadioEvent( ERadioEventMute );
       
  1574         }
       
  1575     }
       
  1576 
       
  1577 // ---------------------------------------------------------------------------
       
  1578 //
       
  1579 // ---------------------------------------------------------------------------
       
  1580 //
       
  1581 void CRadioEngineImp::MrpoBalanceChange( TInt /*aLeftPercentage*/, TInt /*aRightPercentage*/ )
       
  1582     {
       
  1583     }
       
  1584 
       
  1585 // ---------------------------------------------------------------------------
       
  1586 //
       
  1587 // ---------------------------------------------------------------------------
       
  1588 //
       
  1589 void CRadioEngineImp::RdsAfSearchSettingChangedL( TBool aEnabled )
       
  1590     {
       
  1591     LOG_FORMAT( "CRadioEngineImp::RdsAfSearchSettingChangedL( %d )", aEnabled );
       
  1592     iRdsReceiver->SetAutomaticSwitchingL( aEnabled );
       
  1593     }
       
  1594 
       
  1595 // ---------------------------------------------------------------------------
       
  1596 //
       
  1597 // ---------------------------------------------------------------------------
       
  1598 //
       
  1599 void CRadioEngineImp::RegionSettingChangedL( TInt DEBUGVAR( aRegion ) )
       
  1600     {
       
  1601     LOG_FORMAT( "CRadioEngineImp::RegionSettingChangedL( %d )", aRegion );
       
  1602 
       
  1603     if ( RadioInitialized() )
       
  1604         {
       
  1605         iTunerUtility->SetFrequencyRange( TunerFrequencyRangeForRegionId( iSettings->EngineSettings().RegionId() ) );
       
  1606         iTunerUtility->SetFrequency( iSettings->EngineSettings().TunedFrequency() );
       
  1607         iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonImplicit;
       
  1608         }
       
  1609     }
       
  1610 
       
  1611 // ---------------------------------------------------------------------------
       
  1612 //
       
  1613 // ---------------------------------------------------------------------------
       
  1614 //
       
  1615 void CRadioEngineImp::RdsAfSearchBegin()
       
  1616     {
       
  1617     LOG( "CRadioEngineImp::RdsAfSearchBegin()" );
       
  1618     iFrequencySetByRdsAf = ETrue;
       
  1619     }
       
  1620 
       
  1621 // ---------------------------------------------------------------------------
       
  1622 //
       
  1623 // ---------------------------------------------------------------------------
       
  1624 //
       
  1625 void CRadioEngineImp::RdsAfSearchEnd( TUint32 DEBUGVAR( aFrequency ), TInt aError )
       
  1626     {
       
  1627     LOG_FORMAT( "CRadioEngineImp::RdsAfSearchEnd( %d, %d )", aFrequency, aError );
       
  1628     if ( aError != KErrNone )
       
  1629         {
       
  1630         iFrequencySetByRdsAf = EFalse;
       
  1631         }
       
  1632     }
       
  1633 
       
  1634 // ---------------------------------------------------------------------------
       
  1635 // Tries to figure the region out based on current mobile network id
       
  1636 // ---------------------------------------------------------------------------
       
  1637 //
       
  1638 TRadioRegion CRadioEngineImp::RegionFromMobileNetwork() const
       
  1639     {
       
  1640     TRadioRegion region = ERadioRegionNone;
       
  1641 
       
  1642     // Choose the frequency range according to country code
       
  1643     MRadioEngineSettings& engineSettings = iSettings->EngineSettings();
       
  1644     const TDesC& countryCode = engineSettings.CountryCode();
       
  1645     const TInt regionCount = engineSettings.CountRegions();
       
  1646     TBool matchFound = EFalse;
       
  1647     for ( TInt i = 0; i < regionCount && !matchFound; ++i )
       
  1648         {
       
  1649         const RRadioCountryCodeArray& regionCountryCodes =
       
  1650                 engineSettings.Region( i ).CountryCodes();
       
  1651 
       
  1652         const TInt countryCodeCount = regionCountryCodes.Count();
       
  1653         for ( TInt j = 0; j < countryCodeCount && !matchFound; ++j )
       
  1654             {
       
  1655             if ( countryCode == *regionCountryCodes[j] )
       
  1656                 {
       
  1657                 // We have a match
       
  1658                 matchFound = ETrue;
       
  1659                 region = engineSettings.Region( i ).Id();
       
  1660                 }
       
  1661             }
       
  1662         }
       
  1663 
       
  1664     return region;
       
  1665     }
       
  1666 
       
  1667 // ---------------------------------------------------------------------------
       
  1668 // Tries to figure the region out based on timezone selection
       
  1669 // ---------------------------------------------------------------------------
       
  1670 //
       
  1671 TRadioRegion CRadioEngineImp::RegionFromTimezone() const
       
  1672     {
       
  1673     TRadioRegion region = ERadioRegionNone;
       
  1674     TRAP_IGNORE( region = DoRegionFromTimezoneL() );
       
  1675     return region;
       
  1676     }
       
  1677 
       
  1678 // ---------------------------------------------------------------------------
       
  1679 // Performs the timezone-based check
       
  1680 // ---------------------------------------------------------------------------
       
  1681 //
       
  1682 TRadioRegion CRadioEngineImp::DoRegionFromTimezoneL() const
       
  1683     {
       
  1684     LOG_METHOD_AUTO;
       
  1685     CTzLocalizer* timezoneLocalizer = CTzLocalizer::NewL();
       
  1686     CleanupStack::PushL( timezoneLocalizer );
       
  1687 
       
  1688     CTzLocalizedCityGroupArray* cityGroups = timezoneLocalizer->GetAllCityGroupsL(
       
  1689                                             CTzLocalizer::ETzAlphaNameAscending );
       
  1690     CleanupStack::PushL( cityGroups );
       
  1691 
       
  1692     // We get the ownership so we must delete
       
  1693     CTzLocalizedCity* city = timezoneLocalizer->GetFrequentlyUsedZoneCityL( CTzLocalizedTimeZone::ECurrentZone );
       
  1694     const TUint8 cityId = city->GroupId();
       
  1695     delete city;
       
  1696     city = NULL;
       
  1697     LOG_FORMAT( "CRadioEngineHandler::CurrentTimeZoneToRegionL group id: %d", cityId );
       
  1698 
       
  1699     TRadioRegion region = ERadioRegionNone;
       
  1700     const TInt cityGroupCount = cityGroups->Count();
       
  1701     TBool found = EFalse;
       
  1702     for ( TInt i = 0; i < cityGroupCount && !found; ++i )
       
  1703         {
       
  1704         if ( cityId == cityGroups->At( i ).Id() )
       
  1705             {
       
  1706             region = static_cast<TRadioRegion>( KCityGroupRegions[ cityId - 1 ] );
       
  1707             found = ETrue;
       
  1708             }
       
  1709         }
       
  1710 
       
  1711     CleanupStack::PopAndDestroy( cityGroups );
       
  1712     CleanupStack::PopAndDestroy( timezoneLocalizer );
       
  1713 
       
  1714     LOG_ASSERT( found, LOG_FORMAT( "CRadioEngine::DoMapCurrentTimeZoneToRegionL. City not found: %d", cityId ) );
       
  1715 
       
  1716     return region;
       
  1717     }
       
  1718 
       
  1719 // ---------------------------------------------------------------------------
       
  1720 //
       
  1721 // ---------------------------------------------------------------------------
       
  1722 //
       
  1723 void CRadioEngineImp::HandleAudioRoutingEvent( RadioEngine::TRadioAudioRoute aDestination )
       
  1724     {
       
  1725     LOG_FORMAT( "CRadioEngineImp::HandleAudioRoutingL( %d )", aDestination );
       
  1726 
       
  1727     // Make modifications to volume ONLY if new audio source state
       
  1728     // differs from settings. If they don't differ, this state
       
  1729     // change is a part of the radio initialization.
       
  1730     MRadioSettingsSetter& setter = iSettings->RadioSetter();
       
  1731     if ( aDestination != iSettings->EngineSettings().AudioRoute() )
       
  1732         {
       
  1733         setter.SetAudioRoute( aDestination );
       
  1734 
       
  1735         // If audio muted, change it to minimum volume
       
  1736         MRadioEngineSettings& engineSettings = iSettings->EngineSettings();
       
  1737         TInt vol = engineSettings.IsVolMuted() ? engineSettings.DefaultMinVolumeLevel()
       
  1738                                                : engineSettings.Volume();
       
  1739 
       
  1740         if ( RadioInitialized() )
       
  1741             {
       
  1742             TInt err = iPlayerUtility->Mute( EFalse );
       
  1743             if ( !err )
       
  1744                 {
       
  1745                 setter.SetVolMuted( EFalse );
       
  1746                 err = iPlayerUtility->SetVolume( TunerVolumeForUiVolume( vol ));
       
  1747 
       
  1748                 if ( !err )
       
  1749                     {
       
  1750                     setter.SetVolume( vol );
       
  1751                     }
       
  1752                 }
       
  1753             }
       
  1754         else
       
  1755             {
       
  1756             setter.SetVolMuted( EFalse );
       
  1757             setter.SetVolume( vol );
       
  1758             }
       
  1759         NotifyRadioEvent( ERadioEventAudioRouting );
       
  1760         }
       
  1761     }
       
  1762 
       
  1763 // ---------------------------------------------------------------------------
       
  1764 //
       
  1765 // ---------------------------------------------------------------------------
       
  1766 //
       
  1767 void CRadioEngineImp::HandlePowerEvent( TBool aPowerOn, TInt aErrorCode )
       
  1768     {
       
  1769     LOG_FORMAT( "CRadioEngineImp::HandlePowerEvent( %d, %d )", aPowerOn, aErrorCode );
       
  1770 
       
  1771     const TBool powerState = iSettings->EngineSettings().IsPowerOn();
       
  1772     if ( !powerState != !aPowerOn )
       
  1773         {
       
  1774         iSettings->RadioSetter().SetPowerOn( aPowerOn );
       
  1775 
       
  1776         if ( aPowerOn )
       
  1777             {
       
  1778             iRdsReceiver->StartReceiver();
       
  1779             }
       
  1780         else
       
  1781             {
       
  1782             iRdsReceiver->StopReceiver();
       
  1783             }
       
  1784         }
       
  1785 
       
  1786     if ( !iSettings->EngineSettings().IsPowerOn() )
       
  1787         {
       
  1788         StopScan( aErrorCode );
       
  1789         CancelSeek();
       
  1790         }
       
  1791 
       
  1792     // If we are seeking, power event starts seeking
       
  1793     if ( iSeekingState != RadioEngine::ERadioNotSeeking && iSettings->EngineSettings().IsPowerOn() )
       
  1794         {
       
  1795         // Reset seeking state to enable seeking start
       
  1796         RadioEngine::TRadioSeeking oldSeeking = iSeekingState;
       
  1797         iSeekingState = RadioEngine::ERadioNotSeeking;
       
  1798         Seek( oldSeeking == RadioEngine::ERadioSeekingUp ? RadioEngine::ERadioUp : RadioEngine::ERadioDown );
       
  1799         }
       
  1800 
       
  1801     if ( !powerState != !aPowerOn )
       
  1802         {
       
  1803         NotifyRadioEvent( ERadioEventPower, aErrorCode );
       
  1804         }
       
  1805     }
       
  1806 
       
  1807 // ---------------------------------------------------------------------------
       
  1808 //
       
  1809 // ---------------------------------------------------------------------------
       
  1810 //
       
  1811 void CRadioEngineImp::HandleFrequencyEvent( TUint32 aFrequency, TInt aErrorCode )
       
  1812     {
       
  1813     LOG_FORMAT( "CRadioEngineImp::HandleFrequencyEvent( %d, %d )", aFrequency, aErrorCode );
       
  1814 
       
  1815     if ( iSettings->EngineSettings().TunedFrequency() != aFrequency )
       
  1816         {
       
  1817         if ( iSettings->EngineSettings().IsPowerOn() )
       
  1818             {
       
  1819             if ( !OkToPlay( aFrequency ) )
       
  1820                 {
       
  1821                 // Radio is going to be powered off, stop rds receiver immediately because
       
  1822                 // some rds data from previous channel might come before power off event.
       
  1823                 iRdsReceiver->StopReceiver();
       
  1824                 }
       
  1825             else
       
  1826                 {
       
  1827                 // Normal frequency change, make sure that rds receiver is started
       
  1828                 iRdsReceiver->StartReceiver();
       
  1829                 }
       
  1830             }
       
  1831         if ( !iFrequencySetByRdsAf )
       
  1832             {
       
  1833             iRdsReceiver->ClearRdsInformation();
       
  1834             }
       
  1835         }
       
  1836     else if ( iFrequencySetByRdsAf )
       
  1837         {
       
  1838         // frequency didn't change, so AF search didn't complete successfully
       
  1839         iFrequencySetByRdsAf = EFalse;
       
  1840         }
       
  1841 
       
  1842     iSettings->RadioSetter().SetTunedFrequency( aFrequency );
       
  1843 
       
  1844     NotifyRadioEvent( ERadioEventFrequency, aErrorCode );
       
  1845     iFreqEventReason = RadioEngine::ERadioFrequencyEventReasonUnknown;
       
  1846 
       
  1847     if ( aErrorCode == KErrNone )
       
  1848         {
       
  1849         SwitchPower( iRadioEnabled && OkToPlay( aFrequency ) );
       
  1850         }
       
  1851     }
       
  1852 
       
  1853 // ---------------------------------------------------------------------------
       
  1854 // Routing is not possible when headset is not available, power is off or
       
  1855 // audio routing is not supported.
       
  1856 // ---------------------------------------------------------------------------
       
  1857 //
       
  1858 TBool CRadioEngineImp::IsAudioRoutingPossible() const
       
  1859     {
       
  1860     TBool headsetConnected = EFalse;
       
  1861     TRAP_IGNORE( ( headsetConnected = iSystemEventCollector->IsHeadsetConnectedL() ) )
       
  1862 
       
  1863     TBool powerOn = iSettings->EngineSettings().IsPowerOn();
       
  1864 
       
  1865     TBool isAudioRoutingPossible = EFalse;
       
  1866     if ( headsetConnected && powerOn && AudioRoutingSupported() )
       
  1867         {
       
  1868         isAudioRoutingPossible = ETrue;
       
  1869         }
       
  1870 
       
  1871     return isAudioRoutingPossible;
       
  1872     }
       
  1873