radioapp/radioenginewrapper/src/radioenginehandler.cpp
branchRCL_3
changeset 19 cce62ebc198e
equal deleted inserted replaced
18:1a6714c53019 19:cce62ebc198e
       
     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 // User includes
       
    19 #include "cradioengine.h"
       
    20 #include "cradioaudiorouter.h"
       
    21 #include "cradiosystemeventcollector.h"
       
    22 #include "cradiosettings.h"
       
    23 #include "mradioenginesettings.h"
       
    24 #include "mradiordsreceiver.h"
       
    25 #include "radioenginehandler.h"
       
    26 #include "mradioenginehandlerobserver.h"
       
    27 #include "radio_global.h"
       
    28 #include "radiologger.h"
       
    29 #include "radioenummapper.h"
       
    30 
       
    31 /*!
       
    32  * Map to translate seek direction enum from its definition in the engine to
       
    33  * its definition in the ui and vice versa
       
    34  */
       
    35 BEGIN_ENUM_MAP( KSeekDirectionMap )
       
    36     ENUM_MAP_ITEM( Seek::Down,              RadioEngine::ERadioDown ),
       
    37     ENUM_MAP_ITEM( Seek::Up,                RadioEngine::ERadioUp )
       
    38 END_ENUM_MAP( KSeekDirectionMap )
       
    39 
       
    40 /*!
       
    41  * Convenience macro to do the mapping of seek directions
       
    42  */
       
    43 #define MAP_FROM_UI_DIRECTION(ui_enum) MAP_FROM_UI_ENUM( RadioEngine::TRadioTuneDirection, ui_enum, KSeekDirectionMap )
       
    44 
       
    45 /*!
       
    46  * Map to translate radio region enum from its definition in the engine to
       
    47  * its definition in the ui and vice versa
       
    48  */
       
    49 BEGIN_ENUM_MAP( KRegionMap )
       
    50     ENUM_MAP_ITEM( RadioRegion::None,       ERadioRegionNone ),
       
    51     ENUM_MAP_ITEM( RadioRegion::Default,    ERadioRegionDefault ),
       
    52     ENUM_MAP_ITEM( RadioRegion::Japan,      ERadioRegionJapan ),
       
    53     ENUM_MAP_ITEM( RadioRegion::America,    ERadioRegionAmerica ),
       
    54     ENUM_MAP_ITEM( RadioRegion::Poland,     ERadioRegionPoland ),
       
    55 END_ENUM_MAP( KRegionMap )
       
    56 
       
    57 /*!
       
    58  * Convenience macros to do the mapping of radio regions
       
    59  */
       
    60 #define MAP_FROM_UI_REGION(ui_enum) MAP_FROM_UI_ENUM( TRadioRegion, ui_enum, KRegionMap )
       
    61 #define MAP_TO_UI_REGION(engine_enum) MAP_TO_UI_ENUM( RadioRegion::Region, engine_enum, KRegionMap )
       
    62 
       
    63 /*!
       
    64  * Map to translate seeking state enum from its definition in the engine to
       
    65  * its definition in the ui and vice versa
       
    66  */
       
    67 BEGIN_ENUM_MAP( KSeekingStateMap )
       
    68     ENUM_MAP_ITEM( Seek::NotSeeking,        RadioEngine::ERadioNotSeeking ),
       
    69     ENUM_MAP_ITEM( Seek::SeekingUp,         RadioEngine::ERadioSeekingUp ),
       
    70     ENUM_MAP_ITEM( Seek::SeekingDown,       RadioEngine::ERadioSeekingDown )
       
    71 END_ENUM_MAP( KSeekingStateMap )
       
    72 
       
    73 /*!
       
    74  * Convenience macro to do the mapping of seeking states
       
    75  */
       
    76 #define MAP_TO_UI_SEEKING_STATE(ui_enum) MAP_TO_UI_ENUM( Seek::State, ui_enum, KSeekingStateMap )
       
    77 
       
    78 /*!
       
    79  *
       
    80  */
       
    81 RadioEngineHandler::RadioEngineHandler( MRadioEngineHandlerObserver& observer )
       
    82     : mObserver( observer )
       
    83 {
       
    84 }
       
    85 
       
    86 /*!
       
    87  *
       
    88  */
       
    89 RadioEngineHandler::~RadioEngineHandler()
       
    90 {
       
    91 }
       
    92 
       
    93 /*!
       
    94  * Attempts to construct the radio engine
       
    95  */
       
    96 bool RadioEngineHandler::constructEngine()
       
    97 {
       
    98     LOG_METHOD;
       
    99 
       
   100     CRadioEngine* engine = NULL;
       
   101     TRAPD( err, engine = CRadioEngine::NewL( *this ) );
       
   102     if ( err ) {
       
   103         return false;
       
   104     }
       
   105 
       
   106     mEngine.reset( engine );
       
   107     TRAP( err,
       
   108         mEngine->SystemEventCollector().AddObserverL( &mObserver );
       
   109         mEngine->AddObserverL( &mObserver );
       
   110     );
       
   111     if ( err ) {
       
   112         return false;
       
   113     }
       
   114 
       
   115     mRegion = MAP_TO_UI_REGION( mEngine->Settings().EngineSettings().RegionId() );
       
   116     return true;
       
   117 }
       
   118 
       
   119 /*!
       
   120  * Sets the rds data observer
       
   121  */
       
   122 void RadioEngineHandler::setRdsObserver( MRadioRdsDataObserver* observer )
       
   123 {
       
   124     TRAP_IGNORE( mEngine->RdsReceiver().AddObserverL( observer ) );
       
   125 }
       
   126 
       
   127 /*!
       
   128  * Starts or stops receiving RDS data
       
   129  */
       
   130 void RadioEngineHandler::setRdsEnabled( bool rdsEnabled )
       
   131 {
       
   132     if ( rdsEnabled ) {
       
   133         mEngine->RdsReceiver().StartReceiver();
       
   134     } else {
       
   135         mEngine->RdsReceiver().StopReceiver();
       
   136     }
       
   137 }
       
   138 
       
   139 /*!
       
   140  * Returns the radio status.
       
   141  */
       
   142 bool RadioEngineHandler::isRadioOn()
       
   143 {
       
   144     return mEngine->Settings().EngineSettings().IsPowerOn();
       
   145 }
       
   146 
       
   147 /*!
       
   148  * Sets the manual seek status
       
   149  */
       
   150 void RadioEngineHandler::setManualSeekMode( bool manualSeek )
       
   151 {
       
   152     mEngine->SetManualSeekMode( manualSeek );
       
   153 }
       
   154 
       
   155 /*!
       
   156  * Returns the manual seek status
       
   157  */
       
   158 bool RadioEngineHandler::isInManualSeekMode() const
       
   159 {
       
   160     return mEngine->IsInManualSeekMode();
       
   161 }
       
   162 
       
   163 /*!
       
   164  * Tune to the specified frequency
       
   165  */
       
   166 void RadioEngineHandler::setFrequency( uint frequency )
       
   167 {
       
   168     mEngine->SetFrequency( frequency );
       
   169 }
       
   170 
       
   171 /*!
       
   172  * Sets the audio mute state
       
   173  */
       
   174 void RadioEngineHandler::setMute( const bool muted, const bool updateSettings )
       
   175 {
       
   176     mEngine->SetVolumeMuted( muted, updateSettings );
       
   177 }
       
   178 
       
   179 /*!
       
   180  * Gets the audio mute state
       
   181  */
       
   182 bool RadioEngineHandler::isMuted() const
       
   183 {
       
   184     return mEngine->Settings().EngineSettings().IsVolMuted();
       
   185 }
       
   186 
       
   187 /*!
       
   188  * Sets the volume level of the FM radio
       
   189  */
       
   190 void RadioEngineHandler::setVolume( int newVolume )
       
   191 {
       
   192     if ( volume() != newVolume ) {
       
   193         if ( newVolume > 0 ) {
       
   194             mEngine->SetVolumeMuted( EFalse );
       
   195         }
       
   196 
       
   197         mEngine->SetVolume( newVolume );
       
   198     }
       
   199 }
       
   200 
       
   201 /*!
       
   202  * Gets the volumelevel.
       
   203  */
       
   204 int RadioEngineHandler::volume() const
       
   205 {
       
   206     return mEngine->Settings().EngineSettings().Volume();
       
   207 }
       
   208 
       
   209 /*!
       
   210  * Gets the max volumelevel.
       
   211  */
       
   212 int RadioEngineHandler::maxVolume() const
       
   213 {
       
   214     return mEngine->MaxVolumeLevel();
       
   215 }
       
   216 
       
   217 /*!
       
   218  * Increases the volume by one increment
       
   219  */
       
   220 void RadioEngineHandler::increaseVolume()
       
   221 {
       
   222     mEngine->AdjustVolume( RadioEngine::ERadioIncVolume );
       
   223 }
       
   224 
       
   225 /*!
       
   226  * Decreases the volume by one increment
       
   227  */
       
   228 void RadioEngineHandler::decreaseVolume()
       
   229 {
       
   230     mEngine->AdjustVolume( RadioEngine::ERadioDecVolume );
       
   231 }
       
   232 
       
   233 
       
   234 /*!
       
   235  * Checks if the antenna is attached
       
   236  */
       
   237 bool RadioEngineHandler::isAntennaAttached() const
       
   238 {
       
   239     return mEngine->IsAntennaAttached();
       
   240 }
       
   241 
       
   242 /*!
       
   243  * Retrieves the current frequency.
       
   244  */
       
   245 uint RadioEngineHandler::currentFrequency() const
       
   246 {
       
   247     return mEngine->Settings().EngineSettings().TunedFrequency();
       
   248 }
       
   249 
       
   250 /*!
       
   251  * Returns the minimum allowed frequency in the current region
       
   252  */
       
   253 uint RadioEngineHandler::minFrequency() const
       
   254 {
       
   255     return mEngine->Settings().EngineSettings().MinFrequency();
       
   256 }
       
   257 
       
   258 /*!
       
   259  * Returns the maximum allowed frequency in the current region
       
   260  */
       
   261 uint RadioEngineHandler::maxFrequency() const
       
   262 {
       
   263     return mEngine->Settings().EngineSettings().MaxFrequency();
       
   264 }
       
   265 
       
   266 /*!
       
   267  * Checks if the given frequency is valid in the current region
       
   268  */
       
   269 bool RadioEngineHandler::isFrequencyValid( uint frequency ) const
       
   270 {
       
   271     return mEngine->IsFrequencyValid( frequency );
       
   272 }
       
   273 
       
   274 /*!
       
   275  * Scan up to the next available frequency
       
   276  */
       
   277 void RadioEngineHandler::seek( Seek::Direction direction )
       
   278 {
       
   279     LOG_TIMESTAMP( "Seek" );
       
   280     mEngine->Seek( MAP_FROM_UI_DIRECTION( direction ) );
       
   281 }
       
   282 
       
   283 /*!
       
   284  * Cancel previously requested scan, and return to the already tuned frequency
       
   285  */
       
   286 void RadioEngineHandler::cancelSeek()
       
   287 {
       
   288     mEngine->CancelSeek();
       
   289 }
       
   290 
       
   291 /*!
       
   292  * Returns the engine seeking state
       
   293  */
       
   294 Seek::State RadioEngineHandler::seekingState() const
       
   295 {
       
   296     return MAP_TO_UI_SEEKING_STATE( mEngine->Seeking() );
       
   297 }
       
   298 
       
   299 /*!
       
   300  * return step size for tuning.
       
   301  */
       
   302 uint RadioEngineHandler::frequencyStepSize() const
       
   303 {
       
   304     return mEngine->Settings().EngineSettings().FrequencyStepSize();
       
   305 }
       
   306 
       
   307 /*!
       
   308  * Returns the selected radio region
       
   309  */
       
   310 RadioRegion::Region RadioEngineHandler::region() const
       
   311 {
       
   312     return mRegion;
       
   313 }
       
   314 
       
   315 /*!
       
   316  * Sets whether or not audio should be routed to loudspeaker
       
   317  */
       
   318 void RadioEngineHandler::setAudioRouteToLoudspeaker( bool loudspeaker )
       
   319 {
       
   320     TRAPD( err, mEngine->AudioRouter().SetAudioRouteL( loudspeaker ? RadioEngine::ERadioSpeaker
       
   321                                                        : RadioEngine::ERadioHeadset ) );
       
   322     if ( err ) {
       
   323         LOG_FORMAT( "Failed to set audioroute: UseLoudspeadker: %d", loudspeaker );
       
   324     }
       
   325 }
       
   326 
       
   327 /*!
       
   328  * Checks if audio is routed to loudspeaker
       
   329  */
       
   330 bool RadioEngineHandler::isAudioRoutedToLoudspeaker() const
       
   331 {
       
   332     return mEngine->Settings().EngineSettings().AudioRoute() == RadioEngine::ERadioSpeaker;
       
   333 }
       
   334 
       
   335 /*!
       
   336  * Returns the repository manager.
       
   337  */
       
   338 MRadioApplicationSettings& RadioEngineHandler::applicationSettings() const
       
   339 {
       
   340     return mEngine->Settings().ApplicationSettings();
       
   341 }
       
   342 
       
   343 /*!
       
   344  * \reimp
       
   345  */
       
   346 CRadioAudioRouter* RadioEngineHandler::InitAudioRouterL()
       
   347 {
       
   348     return CRadioAudioRouter::NewL( mObserver );
       
   349 }
       
   350 
       
   351 /*!
       
   352  * \reimp
       
   353  */
       
   354 CRadioSystemEventCollector* RadioEngineHandler::InitSystemEventCollectorL()
       
   355 {
       
   356     return CRadioSystemEventCollector::NewL();
       
   357 }
       
   358 
       
   359 /*!
       
   360  * \reimp
       
   361  */
       
   362 CRadioSettings* RadioEngineHandler::InitSettingsL()
       
   363 {
       
   364     return CRadioSettings::NewL();
       
   365 }