mmsharing/mmshengine/src/musengtelephoneutils.cpp
changeset 0 f0cf47e981f9
child 11 ff8a573c0e2e
equal deleted inserted replaced
-1:000000000000 0:f0cf47e981f9
       
     1 /*
       
     2 * Copyright (c) 2005 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 
       
    19 // USER
       
    20 #include "musengtelephoneutils.h"
       
    21 #include "musengaudioroutingobserver.h"
       
    22 #include "musengmcesession.h"
       
    23 #include "muslogger.h"
       
    24 
       
    25 // SYSTEM
       
    26 #include <centralrepository.h>
       
    27 #include <telephonydomaincrkeys.h>
       
    28 #include <e32property.h>
       
    29 #include <telephonydomainpskeys.h>
       
    30 #include <CPhCltCommandHandler.h> // for CPhCltCommandHandler
       
    31 
       
    32 
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CMusEngTelephoneUtils* CMusEngTelephoneUtils::NewL()
       
    39     {
       
    40     CMusEngTelephoneUtils* self = new( ELeave ) CMusEngTelephoneUtils();
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop( self );
       
    44     return self;
       
    45     }
       
    46 
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CMusEngTelephoneUtils::~CMusEngTelephoneUtils()
       
    53     {
       
    54     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::~CMusEngTelephoneUtils()" )
       
    55     
       
    56     Cancel();
       
    57 
       
    58     delete iPhoneCommandHandler;
       
    59     
       
    60     if( iTelephonyAudioRouting ) // If 2nd phase construction has succeeded 
       
    61         {
       
    62         CTelephonyAudioRouting::TAudioOutput currentMode =
       
    63                                             iTelephonyAudioRouting->Output();
       
    64         if( currentMode != iAudioOutputAtStartup )
       
    65             {
       
    66             // As going down, let audiorouting api to show notification
       
    67             iTelephonyAudioRouting->SetShowNote( ETrue );
       
    68             TRAPD( err, DoSetOutputL( iAudioOutputAtStartup ) );
       
    69             MUS_LOG1( "mus: [ENGINE]    final route change completed: %d", err )
       
    70             err++;
       
    71         	}
       
    72         }
       
    73 
       
    74     delete iRepository;
       
    75     delete iTelephonyAudioRouting;
       
    76     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::~CMusEngTelephoneUtils()" )
       
    77     }
       
    78 
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 TBool CMusEngTelephoneUtils::AudioRoutingCanBeChanged()
       
    85     {
       
    86     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::AudioRoutingCanBeChanged" )
       
    87     
       
    88     TBool retValue = ( iTelephonyAudioRouting->Output() !=
       
    89                        CTelephonyAudioRouting::EWiredAudioAccessory &&
       
    90                        iTelephonyAudioRouting->Output() !=
       
    91                        CTelephonyAudioRouting::EBTAudioAccessory &&
       
    92                        iTelephonyAudioRouting->Output() !=
       
    93                        CTelephonyAudioRouting::ETTY );
       
    94     
       
    95     MUS_LOG1( "mus: [ENGINE]  <- CMusEngTelephoneUtils::AudioRoutingCanBeChanged: %d",
       
    96               retValue )
       
    97               
       
    98     return retValue;
       
    99     }
       
   100 
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 void CMusEngTelephoneUtils::LoudspeakerL( TBool aEnable, TBool aShowDialog )
       
   107     {
       
   108     MUS_LOG1( "mus: [ENGINE]  -> CMusEngTelephoneUtils::LoudspeakerL(%d)",
       
   109               aEnable )
       
   110     
       
   111     if ( aEnable )
       
   112         {
       
   113         if ( iTelephonyAudioRouting->Output() == 
       
   114              CTelephonyAudioRouting::EHandset )
       
   115             {
       
   116             // Disable note shown by audiorouting api as it causes
       
   117             // application going to background for a while. Instead, display
       
   118             // note by ourselves once setting output completes. This mechanism
       
   119             // is needed only for loudspeaker enabling as going to background
       
   120             // causes problems only at beginning of sharing session.
       
   121             if ( aShowDialog )
       
   122                 {   
       
   123                 iTelephonyAudioRouting->SetShowNote( EFalse );
       
   124                 
       
   125                 iShowDialog = aShowDialog;
       
   126                 }
       
   127     
       
   128             DoSetOutputL( CTelephonyAudioRouting::ELoudspeaker );
       
   129             }
       
   130         }
       
   131     else
       
   132         {
       
   133         iTelephonyAudioRouting->SetShowNote( aShowDialog );
       
   134         
       
   135         if ( iAudioOutputAtStartup == CTelephonyAudioRouting::ELoudspeaker )
       
   136             {
       
   137             DoSetOutputL( CTelephonyAudioRouting::EHandset );
       
   138             }
       
   139         else
       
   140             {
       
   141             DoSetOutputL( iAudioOutputAtStartup );
       
   142             }
       
   143         }
       
   144 
       
   145     
       
   146                                 
       
   147     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::LoudspeakerL(...)" )
       
   148     }
       
   149 
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 TBool CMusEngTelephoneUtils::IsLoudSpeakerEnabled() const
       
   156     {
       
   157     return ( iTelephonyAudioRouting->Output() ==
       
   158              CTelephonyAudioRouting::ELoudspeaker );
       
   159     }
       
   160 
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // Gets the CS call volume level.
       
   164 // Leaves if error occurs when accessing central repository.
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 TInt CMusEngTelephoneUtils::GetVolumeL() const
       
   168     {
       
   169     TInt currentVolume = 0;
       
   170     if ( IsLoudSpeakerEnabled() )
       
   171         {
       
   172         User::LeaveIfError( iRepository->Get( KTelIncallLoudspeakerVolume,
       
   173                                               currentVolume ) );
       
   174         }
       
   175     else
       
   176         {
       
   177         User::LeaveIfError( iRepository->Get( KTelIncallEarVolume,
       
   178                                               currentVolume ) );
       
   179         }
       
   180 
       
   181     return ValidateVolume( currentVolume );
       
   182     }
       
   183 
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // Sets the CS call volume level.
       
   187 // Leaves if error occurs when accessing central repository.
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 void CMusEngTelephoneUtils::SetVolumeL( TInt aVolume )
       
   191     {
       
   192     TInt newVolume = ValidateVolume( aVolume );
       
   193 
       
   194     if ( GetVolumeL() != newVolume )
       
   195         {
       
   196         if ( IsLoudSpeakerEnabled() )
       
   197             {
       
   198             User::LeaveIfError( iRepository->Set( KTelIncallLoudspeakerVolume,
       
   199                                                   newVolume ) );
       
   200             }
       
   201         else
       
   202             {
       
   203             User::LeaveIfError( iRepository->Set( KTelIncallEarVolume, 
       
   204                                                   newVolume ) );
       
   205             }
       
   206         }
       
   207     }
       
   208 
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CMusEngTelephoneUtils::MuteMicL( TBool aMute )
       
   215     {
       
   216     MUS_LOG1( "mus: [ENGINE]  -> CMusEngTelephoneUtils::MuteMicL(%d)", aMute )
       
   217     
       
   218     if ( IsActive() )
       
   219         {
       
   220         MUS_LOG( "mus: [ENGINE]     Cancel pending request" )
       
   221         Cancel();
       
   222         }
       
   223     
       
   224     iPhoneCommandHandler->MuteMicrophone( iStatus, aMute );
       
   225     SetActive();
       
   226     
       
   227     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::MuteMicL()" )
       
   228     }
       
   229 
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 TBool CMusEngTelephoneUtils::IsMicMutedL()
       
   236     {
       
   237     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::IsMicMutedL()" )
       
   238     
       
   239     TInt psVal;
       
   240     User::LeaveIfError( RProperty::Get( KPSUidTelMicrophoneMuteStatus,
       
   241                                         KTelMicrophoneMuteState,
       
   242                                         psVal ) );
       
   243                                         
       
   244     MUS_LOG1( "mus: [ENGINE]     Mute status in PS is (%d)", psVal )
       
   245     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::IsMicMutedL()" )
       
   246 
       
   247     return ( psVal == EPSTelMicMuteOn );
       
   248     }
       
   249 
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void CMusEngTelephoneUtils::SetAudioRoutingObserver( 
       
   256                                     MMusEngAudioRoutingObserver* aObserver )
       
   257     {
       
   258     iAudioRoutingObserver = aObserver;
       
   259     }
       
   260     
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // 
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 void CMusEngTelephoneUtils::RunL()
       
   267     {
       
   268     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::RunL()" )
       
   269 
       
   270     // NOP, since we really cannot do anything but log the result
       
   271     MUS_LOG1( "mus: [ENGINE]    Asynchronous call completed with code %d",
       
   272              iStatus.Int() )
       
   273 
       
   274     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::RunL()" )
       
   275     }
       
   276 
       
   277 
       
   278 // -----------------------------------------------------------------------------
       
   279 // 
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 void CMusEngTelephoneUtils::DoCancel()
       
   283     {
       
   284     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::DoCancel()" )
       
   285     
       
   286     if ( iPhoneCommandHandler )
       
   287         {
       
   288         iPhoneCommandHandler->CancelAsyncRequest( EPhCltCommandMuteMic );
       
   289         }
       
   290         
       
   291     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::DoCancel()" )
       
   292     }
       
   293     
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 void CMusEngTelephoneUtils::AvailableOutputsChanged( 
       
   300                         CTelephonyAudioRouting& /*aTelephonyAudioRouting*/ )
       
   301     {
       
   302     // NOP
       
   303     }
       
   304 
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 void CMusEngTelephoneUtils::OutputChanged( 
       
   311                 CTelephonyAudioRouting& aTelephonyAudioRouting )
       
   312     {
       
   313     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::OutputChanged()" )
       
   314 
       
   315     // Some other application has changed audio output routing. We consider
       
   316     // this as a last will of a user and return to this state after sharing
       
   317     
       
   318     iAudioOutputAtStartup = aTelephonyAudioRouting.Output();
       
   319     
       
   320     MUS_LOG1( "mus: [ENGINE]     New audio routing is %d", iAudioOutputAtStartup )
       
   321     
       
   322     if ( iAudioRoutingObserver )
       
   323         {
       
   324         iAudioRoutingObserver->AudioRoutingChanged( EFalse );
       
   325         }
       
   326         
       
   327     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::OutputChanged()" )
       
   328     }
       
   329     
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CMusEngTelephoneUtils::SetOutputComplete( 
       
   336                     CTelephonyAudioRouting& /*aTelephonyAudioRouting*/,
       
   337                     TInt aError )
       
   338     {
       
   339     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::SetOutputComplete()" )
       
   340 
       
   341     if ( aError == KErrNone && iAudioRoutingObserver )
       
   342         {
       
   343         // If audio routing api didn't shown note and show dialog mode is on,
       
   344         // we know that this completion is for such setoutput call for which
       
   345         // we need to show the note. Show note mode is turned off only in that
       
   346         // case.
       
   347         TBool dialogShownByUs( EFalse );
       
   348         TBool dialogShownByAudioRouting( EFalse );     
       
   349         aError = iTelephonyAudioRouting->GetShowNote( dialogShownByAudioRouting );
       
   350         if ( aError == KErrNone && !dialogShownByAudioRouting && iShowDialog )
       
   351             {
       
   352             dialogShownByUs = iShowDialog;
       
   353             iShowDialog = EFalse;
       
   354             }
       
   355 
       
   356         iAudioRoutingObserver->AudioRoutingChanged( dialogShownByUs );
       
   357         }
       
   358     
       
   359     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::SetOutputComplete()" )
       
   360     }
       
   361                             
       
   362 
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 CMusEngTelephoneUtils::CMusEngTelephoneUtils() 
       
   368 	: CActive( CActive::EPriorityStandard )
       
   369     {
       
   370     iAudioOutputAtStartup = CTelephonyAudioRouting::ENotActive;
       
   371     }
       
   372 
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 void CMusEngTelephoneUtils::ConstructL()
       
   379     {
       
   380     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::ConstructL()" )
       
   381 
       
   382     // Volume control
       
   383     iRepository = CRepository::NewL( KCRUidInCallVolume );
       
   384 
       
   385     // Audio routing control
       
   386     iTelephonyAudioRouting = CTelephonyAudioRouting::NewL( *this );
       
   387 
       
   388     iAudioOutputAtStartup = iTelephonyAudioRouting->Output();
       
   389     
       
   390     // Phone
       
   391     MUS_LOG( "mus: [ENGINE]     Use static DLL" )
       
   392     iPhoneCommandHandler = CPhCltCommandHandler::NewL();
       
   393         
       
   394     CActiveScheduler::Add( this );
       
   395 
       
   396     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::ConstructL()" )
       
   397     }
       
   398 
       
   399 
       
   400 // -----------------------------------------------------------------------------
       
   401 // Validates that requested volume level is valid (between 1-10) and if it is
       
   402 // not, modifies requested volume level to be valid.  Returns validated volume.
       
   403 // -----------------------------------------------------------------------------
       
   404 //
       
   405 TInt CMusEngTelephoneUtils::ValidateVolume( const TInt aVolume ) const
       
   406     {
       
   407     if ( aVolume < KMusEngMinVolume )
       
   408         {
       
   409         return KMusEngMinVolume;
       
   410         }
       
   411     else if ( aVolume > KMusEngMaxVolume )
       
   412         {
       
   413         return KMusEngMaxVolume;
       
   414         }
       
   415     else
       
   416         {
       
   417         // NOP, to keep PC-Lint happy
       
   418         }
       
   419         
       
   420     return aVolume;
       
   421     }
       
   422 
       
   423 
       
   424 // -----------------------------------------------------------------------------
       
   425 // Set output if setting is currently allowed.
       
   426 // -----------------------------------------------------------------------------
       
   427 //
       
   428 void CMusEngTelephoneUtils::DoSetOutputL( 
       
   429     CTelephonyAudioRouting::TAudioOutput aAudioOutput )
       
   430     {
       
   431     MUS_LOG( "mus: [ENGINE]  -> CMusEngTelephoneUtils::DoSetOutputL()" )
       
   432     if ( iAudioRoutingObserver && !iAudioRoutingObserver->AudioRouteChangeAllowed() )
       
   433         {
       
   434         MUS_LOG( "mus: [ENGINE]     change not allowed!" )
       
   435         User::Leave( KErrAccessDenied );
       
   436         }
       
   437     iTelephonyAudioRouting->SetOutputL( aAudioOutput );
       
   438     MUS_LOG( "mus: [ENGINE]  <- CMusEngTelephoneUtils::DoSetOutputL()" )
       
   439     }
       
   440 
       
   441