radioapp/radioenginewrapper/src/radioenginewrapper_p.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 
       
    20 // User includes
       
    21 #include "radioenginewrapper_p.h"
       
    22 #include "radiosettings.h"
       
    23 #include "radiosettings_p.h"
       
    24 #include "radiologger.h"
       
    25 #include "radio_global.h"
       
    26 #include "cradioenginehandler.h"
       
    27 #include "radiofrequencyscanninghandler.h"
       
    28 #include "radiostationhandlerif.h"
       
    29 #include "cradiopubsub.h"
       
    30 #include "radiocontroleventlistener.h"
       
    31 #include "radiordslistener.h"
       
    32 #include "cradiorepositorymanager.h"
       
    33 #include "radioenginewrapperobserver.h"
       
    34 
       
    35 // Constants
       
    36 
       
    37 /*!
       
    38  *
       
    39  */
       
    40 RadioEngineWrapperPrivate::RadioEngineWrapperPrivate( RadioEngineWrapper* wrapper,
       
    41                                                       RadioStationHandlerIf& stationHandler,
       
    42                                                       RadioEngineWrapperObserver& observer ) :
       
    43     q_ptr( wrapper ),
       
    44     mStationHandler( stationHandler ),
       
    45     mObserver( observer ),
       
    46     mEngineHandler( new CRadioEngineHandler( *this ) ),
       
    47     mControlEventListener( new RadioControlEventListener( *this ) ),
       
    48     mRdsListener ( new RadioRdsListener( mStationHandler, *this ) ),
       
    49     mCommandSender( 0 ),
       
    50     mUseLoudspeaker( false ),
       
    51     mIsSeeking( false )
       
    52 {
       
    53 }
       
    54 
       
    55 /*!
       
    56  *
       
    57  */
       
    58 RadioEngineWrapperPrivate::~RadioEngineWrapperPrivate()
       
    59 {
       
    60     // Destructor needs to be defined because some member variables that are forward declared
       
    61     // in the header are managed by QT's smart pointers and they require that the owning class
       
    62     // has a non-inlined destructor. Compiler generates an inlined destructor if it isn't defined.
       
    63 }
       
    64 
       
    65 /*!
       
    66  * Initializes the private implementation
       
    67  */
       
    68 void RadioEngineWrapperPrivate::init()
       
    69 {
       
    70     TRAPD( err, mEngineHandler->ConstructL() );
       
    71     if ( err != KErrNone ) {
       
    72         LOG_FORMAT( "RadioEngineWrapperPrivate::init, EngineHandler construct failed: %d", err );
       
    73         mEngineHandler.reset();
       
    74         return;
       
    75         //TODO: Error handling?
       
    76     }
       
    77 
       
    78     mEngineHandler->SetRdsObserver( mRdsListener.data() );
       
    79     mEngineHandler->PubSub().SetControlEventObserver( mControlEventListener.data() );
       
    80     mControlEventListener->init();
       
    81 
       
    82     // Start observing profile changes
       
    83     mEngineHandler->Repository().AddEntityL( KCRUidProfileEngine,
       
    84                                              KProEngActiveProfile,
       
    85                                              CRadioRepositoryManager::ERadioEntityInt );
       
    86 
       
    87     mUseLoudspeaker = mEngineHandler->IsAudioRoutedToLoudspeaker();
       
    88     if ( !mUseLoudspeaker ) {
       
    89         mObserver.audioRouteChanged( false );
       
    90     }
       
    91 }
       
    92 
       
    93 /*!
       
    94  * Starts up the radio engine
       
    95  */
       
    96 bool RadioEngineWrapperPrivate::isEngineConstructed()
       
    97 {
       
    98     return mEngineHandler != 0;
       
    99 }
       
   100 
       
   101 /*!
       
   102  * Returns the settings handler owned by the engine
       
   103  */
       
   104 RadioSettings& RadioEngineWrapperPrivate::settings()
       
   105 {
       
   106     if ( !mSettings ) {
       
   107         mSettings.reset( new RadioSettings() );
       
   108         mSettings->d_func()->init( &mEngineHandler->ApplicationSettings() );
       
   109     }
       
   110     return *mSettings;
       
   111 }
       
   112 
       
   113 /*!
       
   114  * Returns the enginehandler owned by the engine
       
   115  */
       
   116 CRadioEngineHandler& RadioEngineWrapperPrivate::RadioEnginehandler()
       
   117 {
       
   118     return *mEngineHandler;
       
   119 }
       
   120 
       
   121 /*!
       
   122  * Tunes to the given frequency
       
   123  */
       
   124 void RadioEngineWrapperPrivate::tuneFrequency( uint frequency, const int sender )
       
   125 {
       
   126     mCommandSender = sender;
       
   127     mEngineHandler->Tune( frequency );
       
   128 }
       
   129 
       
   130 /*!
       
   131  * Tunes to the given frequency after a delay
       
   132  */
       
   133 void RadioEngineWrapperPrivate::tuneWithDelay( uint frequency, const int sender )
       
   134 {
       
   135     mCommandSender = sender;
       
   136     mEngineHandler->TuneWithDelay( frequency );
       
   137 }
       
   138 
       
   139 /*!
       
   140  *
       
   141  */
       
   142 RadioEngineWrapperObserver& RadioEngineWrapperPrivate::observer()
       
   143 {
       
   144     return mObserver;
       
   145 }
       
   146 
       
   147 /*!
       
   148  *
       
   149  */
       
   150 void RadioEngineWrapperPrivate::startSeeking( Seeking::Direction direction )
       
   151 {
       
   152     mEngineHandler->Seek( direction );
       
   153     mObserver.seekingStarted( direction );
       
   154 }
       
   155 
       
   156 /*!
       
   157  * \reimp
       
   158  */
       
   159 void RadioEngineWrapperPrivate::PowerEventL( TBool aPowerState, TInt DEBUGVAR( aError ) )
       
   160 {
       
   161     LOG_FORMAT( "RadioEngineWrapperPrivate::PowerEventL, PowerState: %d, Error: %d", aPowerState, aError );
       
   162     mObserver.radioStatusChanged( aPowerState );
       
   163     mEngineHandler->PubSub().PublishPowerState( aPowerState );
       
   164 }
       
   165 
       
   166 /*!
       
   167  * \reimp
       
   168  */
       
   169 void RadioEngineWrapperPrivate::FrequencyEventL( TUint32 aFrequency,
       
   170                                                  RadioEngine::TRadioFrequencyEventReason aReason,
       
   171                                                  TInt aError )
       
   172 {
       
   173     LOG_FORMAT( "RadioEngineWrapperPrivate::FrequencyEventL - Frequency: %d, Reason: %d, Error: %d", aFrequency, aReason, aError );
       
   174 
       
   175     if ( mFrequencyScanningHandler )
       
   176     {
       
   177         // frequencyevents not handled during scanning //TODO remove
       
   178         return;
       
   179     }
       
   180 
       
   181     if ( !aError ) {
       
   182         const uint frequency = static_cast<uint>( aFrequency );
       
   183 
       
   184         mStationHandler.setCurrentStation( frequency );
       
   185         // Stations found by seeking (autotune) are saved as local stations
       
   186 //        if ( aReason == RadioEngine::ERadioFrequencyEventReasonUp
       
   187 //             || aReason == RadioEngine::ERadioFrequencyEventReasonDown  )
       
   188 //        {
       
   189 //            mStationHandler.addScannedFrequency( frequency );
       
   190 //            mIsSeeking = false;
       
   191 //            mCommandSender = 0;
       
   192 //        }
       
   193 
       
   194         //mEngineHandler->SetMuted( EFalse );
       
   195         LOG_TIMESTAMP( "Channel change finished" );
       
   196 
       
   197         mObserver.tunedToFrequency( frequency, mCommandSender );
       
   198 
       
   199         mStationHandler.startDynamicPsCheck();
       
   200 
       
   201         mEngineHandler->PubSub().PublishFrequency( aFrequency );
       
   202     }
       
   203 }
       
   204 
       
   205 /*!
       
   206  * \reimp
       
   207  */
       
   208 void RadioEngineWrapperPrivate::VolumeEventL( TInt aVolume, TInt aError )
       
   209 {
       
   210     Q_UNUSED( aError );
       
   211     mObserver.volumeChanged( aVolume );
       
   212     mEngineHandler->PubSub().PublishVolume( aVolume );
       
   213 }
       
   214 
       
   215 /*!
       
   216  * \reimp
       
   217  */
       
   218 void RadioEngineWrapperPrivate::MuteEventL( TBool aMuteState, TInt aError )
       
   219 {
       
   220     Q_UNUSED( aError );
       
   221     mObserver.muteChanged( aMuteState );
       
   222     mEngineHandler->PubSub().PublishRadioMuteState( aMuteState );
       
   223 }
       
   224 
       
   225 /*!
       
   226  * \reimp
       
   227  */
       
   228 void RadioEngineWrapperPrivate::AudioModeEventL( TInt DEBUGVAR( aAudioMode ), TInt DEBUGVAR( aError ) )
       
   229 {
       
   230     LOG_FORMAT( "RadioEngineWrapperPrivate::AudioModeEventL, AudioMode: %d, Error: %d", aAudioMode, aError );
       
   231 }
       
   232 
       
   233 /*!
       
   234  * \reimp
       
   235  */
       
   236 void RadioEngineWrapperPrivate::AntennaEventL( TBool aAntennaAttached, TInt aError )
       
   237 {
       
   238     Q_UNUSED( aError );
       
   239     mObserver.headsetStatusChanged( aAntennaAttached );
       
   240 //    doc->PubSubL().PublishHeadsetStatusL( EVRPSHeadsetConnected );
       
   241 }
       
   242 
       
   243 /*!
       
   244  * \reimp
       
   245  */
       
   246 void RadioEngineWrapperPrivate::AudioRoutingEventL( TInt aAudioDestination, TInt aError )
       
   247 {
       
   248     //TODO: Check how this event differs from AudioRoutingChangedL
       
   249     Q_UNUSED( aAudioDestination )
       
   250     Q_UNUSED( aError )
       
   251 //    doc->PubSubL().PublishLoudspeakerStatusL( EVRPSLoudspeakerNotInUse );
       
   252 //    Q_Q( RadioEngineWrapper );
       
   253 //    q->audioRouteChanged( aAudioDestination == RadioEngine::ERadioSpeaker );
       
   254 }
       
   255 
       
   256 /*!
       
   257  * \reimp
       
   258  */
       
   259 void RadioEngineWrapperPrivate::SeekingEventL( TInt aSeekingState, TInt DEBUGVAR( aError ) )
       
   260 {
       
   261     LOG_FORMAT( "RadioEngineWrapperPrivate::SeekingEventL, aSeekingState: %d, Error: %d", aSeekingState, aError );
       
   262     if ( aSeekingState != RadioEngine::ERadioNotSeeking ) {
       
   263         // We only set the flag here. It is reset in the FrequencyEventL
       
   264         mIsSeeking = true;
       
   265     }
       
   266 //    Document()->PubSubL().PublishTuningStateL( EVRPSTuningStarted );
       
   267 }
       
   268 
       
   269 /*!
       
   270  * \reimp
       
   271  */
       
   272 void RadioEngineWrapperPrivate::RegionEventL( TInt DEBUGVAR( aRegion ), TInt DEBUGVAR( aError ) )
       
   273 {
       
   274     LOG_FORMAT( "RadioEngineWrapperPrivate::RegionEventL, aRegion: %d, Error: %d", aRegion, aError );
       
   275 //    Document()->PubSubL().PublishFrequencyDecimalCountL(
       
   276 //        static_cast<TVRPSFrequencyDecimalCount>( Document()->RadioSettings()->DecimalCount() ) );
       
   277 }
       
   278 
       
   279 /*!
       
   280  * \reimp
       
   281  */
       
   282 void RadioEngineWrapperPrivate::AudioRouteChangedL( RadioEngine::TRadioAudioRoute aRoute )
       
   283 {
       
   284     mUseLoudspeaker = aRoute == RadioEngine::ERadioSpeaker;
       
   285     mObserver.audioRouteChanged( mUseLoudspeaker );
       
   286 }
       
   287 
       
   288 /*!
       
   289  * \reimp
       
   290  */
       
   291 void RadioEngineWrapperPrivate::HandleSystemEventL( TRadioSystemEventType DEBUGVAR( aEventType ) )
       
   292 {
       
   293     LOG_FORMAT( "RadioEngineWrapperPrivate::HandleSystemEventL, Event: %d", aEventType );
       
   294 //    ERadioHeadsetConnected,         ///< Headset was connected
       
   295 //    ERadioHeadsetDisconnected,      ///< Headset was disconnected
       
   296 //    ERadioNetworkCoverageUp,        ///< Network coverage detected
       
   297 //    ERadioNetworkCoverageDown,      ///< Network coverage lost
       
   298 //    ERadioCallActivated,            ///< Call activated or ringing
       
   299 //    ERadioCallDeactivated,          ///< Call disconnected
       
   300 //    ERadioEmergencyCallActivated,   ///< Call activated or ringing
       
   301 //    ERadioEmergencyCallDeactivated, ///< Call disconnected
       
   302 //    ERadioLowDiskSpace,             ///< Low disk space
       
   303 //    ERadioAudioRoutingHeadset,      ///< Audio routed through headset
       
   304 //    ERadioAudioRoutingSpeaker,      ///< Audio routed through speaker ( IHF )
       
   305 //    ERadioAudioResourcesAvailable,  ///< Audio resources have become available
       
   306 //    ERadioAudioAutoResumeForbidden  ///< Audio auto resuming is forbidden
       
   307 }
       
   308 
       
   309 /*!
       
   310  * \reimp
       
   311  */
       
   312 void RadioEngineWrapperPrivate::HandleRepositoryValueChangeL( const TUid& aUid, TUint32 aKey, TInt aValue, TInt aError )
       
   313 {
       
   314     if ( aUid == KCRUidProfileEngine && aKey == KProEngActiveProfile && !aError && aValue == KOfflineProfileId ) {
       
   315         LOG( "RadioEngineWrapperPrivate::HandleRepositoryValueChangeL: Offline profile activated" );
       
   316     }
       
   317 }
       
   318 
       
   319 /*!
       
   320  *
       
   321  */
       
   322 void RadioEngineWrapperPrivate::frequencyScannerFinished()
       
   323 {
       
   324     RadioFrequencyScanningHandler* handler = mFrequencyScanningHandler.take(); // Nulls the pointer
       
   325     handler->deleteLater();
       
   326     mObserver.scanAndSaveFinished();
       
   327 }