fmradio/fmradioengine/src/fmradiosystemeventdetector.cpp
changeset 0 f3d95d9c00ab
equal deleted inserted replaced
-1:000000000000 0:f3d95d9c00ab
       
     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: Observer for system events
       
    15 *
       
    16 */
       
    17 
       
    18 #include "fmradiovariant.hrh" // include first for variation
       
    19 #include <e32def.h> // Define before audiopolicy defs
       
    20 #include <e32std.h> // Define before audiopolicy defs
       
    21 #include <eikdef.h>
       
    22 #include <sacls.h>
       
    23 #include <voiceuidomainpskeys.h>
       
    24 #include <ctsydomainpskeys.h>
       
    25 #ifndef __WINS__
       
    26 
       
    27 #ifdef __FMRADIO_ADVANCED_AUTO_RESUME
       
    28 
       
    29 const TInt KFMRadioAudioCategoryArrayGranularity = 3;
       
    30 #include <internal/audiopolicypubsubdata.h>
       
    31 #include <internal/audiosw_pubsubkeys.h>
       
    32 
       
    33 #else
       
    34 
       
    35 #include <AudioClientsListPSData.h>
       
    36 #include <AudioClientsListPSKeys.h>
       
    37 
       
    38 #endif // __FMRADIO_ADVANCED_AUTO_RESUME
       
    39 
       
    40 #endif //__WINS__
       
    41 
       
    42 #include "debug.h"
       
    43 #include "fmradiosystemeventdetector.h"
       
    44 #include "fmradiosystemeventdetectorobserver.h"
       
    45 
       
    46 const TInt KFMRadioAutoResumeDelay = 2000000; // micro seconds -> 2 seconds
       
    47 
       
    48 // ================= MEMBER FUNCTIONS =======================
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CFMRadioSystemEventDetector::CFMRadioSystemEventDetector
       
    52 // C++ default constructor can NOT contain any code, that might leave.
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 CFMRadioSystemEventDetector::CFMRadioSystemEventDetector(MFMRadioSystemEventDetectorObserver&  aObserver) 
       
    56     :   iObserver( aObserver ),
       
    57         iIsNetworkCoverage( EFalse),
       
    58         iIsVoiceUiActive( EFalse )
       
    59     {
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CFMRadioSystemEventDetector::ConstructL
       
    64 // EPOC default constructor can leave.
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 void CFMRadioSystemEventDetector::ConstructL()
       
    68     {
       
    69     CFMRadioPropertyObserver* networkObserver =
       
    70         CFMRadioPropertyObserver::NewLC( *this,
       
    71                                         KUidSystemCategory,
       
    72                                         KUidNetworkStatusValue,
       
    73                                         CFMRadioPropertyObserver::EFMRadioPropertyInt );
       
    74         
       
    75     iPropertyArray.AppendL( networkObserver );
       
    76     CleanupStack::Pop( networkObserver );
       
    77     
       
    78     CFMRadioPropertyObserver* voiceUiObserver =	
       
    79         CFMRadioPropertyObserver::NewLC( *this, 
       
    80                                         KPSUidVoiceUiAccMonitor, 
       
    81                                         KVoiceUiOpenKey, 
       
    82                                         CFMRadioPropertyObserver::EFMRadioPropertyInt );
       
    83                                                 
       
    84     iPropertyArray.AppendL( voiceUiObserver );
       
    85     CleanupStack::Pop( voiceUiObserver );
       
    86     // Initialize call state observer.
       
    87     iCallStatusObserver = CFMRadioPropertyObserver::NewL( *this, 
       
    88                                                       	  KPSUidCtsyCallInformation, 
       
    89                                                       	  KCTsyCallState, 
       
    90                                                       	  CFMRadioPropertyObserver::EFMRadioPropertyInt);
       
    91 										    
       
    92     iCallStatusObserver->ActivateL();
       
    93     iIsCallActive = iCallStatusObserver->ValueInt() != EPSCTsyCallStateNone;
       
    94 
       
    95     iAutoResumeTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
    96 
       
    97 #ifndef __WINS__
       
    98 #ifdef __FMRADIO_ADVANCED_AUTO_RESUME
       
    99     // Define audio types for not resuming.
       
   100     // Audio categories are currently defined by adaptation of each hw platform.
       
   101     // Nokia adaptation uses following definitions, which must be replaced by corresponding
       
   102     // adaptation specific definitions of licensee adaptation.
       
   103     // No general Symbian Foundation definitions for categories exists yet.
       
   104     iNoAutoResumeAudioCategories = RArray<TInt>( KFMRadioAudioCategoryArrayGranularity );
       
   105     iNoAutoResumeAudioCategories.AppendL( ECatMediaPlayer );
       
   106     iNoAutoResumeAudioCategories.AppendL( ECatMobileTv );
       
   107     iNoAutoResumeAudioCategories.AppendL( ECatUnknownPlayer );
       
   108     iNoAutoResumeAudioCategories.Compress();
       
   109 #endif // __FMRADIO_ADVANCED_AUTO_RESUME
       
   110     // Start listening audio client events.
       
   111     CFMRadioPropertyObserver* audioPolicyObserver =
       
   112         CFMRadioPropertyObserver::NewLC( *this,
       
   113                                         KPSUidMMFAudioServer,
       
   114                                         KAudioPolicyAudioClients,
       
   115                                         CFMRadioPropertyObserver::EFMRadioPropertyByteArray );
       
   116                          
       
   117      iPropertyArray.AppendL( audioPolicyObserver );
       
   118      CleanupStack::Pop( audioPolicyObserver );
       
   119 #endif	
       
   120     for ( TInt i = 0; i < iPropertyArray.Count(); i++ )
       
   121         {
       
   122         iPropertyArray[i]->ActivateL();	
       
   123         }
       
   124     
       
   125     TInt networkAvailability = iPropertyArray[EFMRadioNetworkCoverageProperty]->ValueInt();
       
   126             
       
   127     if (networkAvailability == ESANetworkAvailable)
       
   128         {
       
   129         iIsNetworkCoverage = ETrue;
       
   130         }
       
   131     else
       
   132         {
       
   133         iIsNetworkCoverage = EFalse;
       
   134         }
       
   135     }
       
   136 
       
   137 // ---------------------------------------------------------------------------
       
   138 // CFMRadioSystemEventDetector::~CFMRadioSystemEventDetector
       
   139 // Destructor
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 CFMRadioSystemEventDetector::~CFMRadioSystemEventDetector()
       
   143     {
       
   144     delete iCallStatusObserver;
       
   145     iPropertyArray.ResetAndDestroy();
       
   146     iPropertyArray.Close();
       
   147     iNoAutoResumeAudioCategories.Close();
       
   148     delete iAutoResumeTimer;  
       
   149     }
       
   150     
       
   151 // ---------------------------------------------------------------------------
       
   152 // CFMRadioSystemEventDetector::NewL
       
   153 // Two-phased constructor.
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 CFMRadioSystemEventDetector* CFMRadioSystemEventDetector::NewL(
       
   157                                     MFMRadioSystemEventDetectorObserver&  aObserver)
       
   158     {
       
   159     CFMRadioSystemEventDetector* self = new (ELeave) CFMRadioSystemEventDetector(aObserver);
       
   160     
       
   161     CleanupStack::PushL( self );
       
   162     self->ConstructL();
       
   163     CleanupStack::Pop();
       
   164 	
       
   165     return self;
       
   166     }
       
   167     
       
   168 // ---------------------------------------------------------------------------
       
   169 // CFMRadioSystemEventDetector::HandlePropertyChangeL
       
   170 // Handling of the int property changes is done here. 
       
   171 // Observer components are getting notifications in correspondence with what 
       
   172 // has changed
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CFMRadioSystemEventDetector::HandlePropertyChangeL( const TUid& aCategory, 
       
   176                                                     const TUint aKey, 
       
   177                                                     const TInt aValue )
       
   178 	{
       
   179     if (aCategory == KUidSystemCategory && aKey == KUidNetworkStatusValue)
       
   180     	{
       
   181     	switch (aValue)
       
   182     		{
       
   183     		case ESANetworkAvailable:
       
   184     			{
       
   185 				if (!iIsNetworkCoverage)
       
   186 				    {
       
   187 				    FTRACE( FPrint( _L("CFMRadioSystemEventDetector - iObserver NetworkUpCallbackL") ) );
       
   188 				    iIsNetworkCoverage = ETrue;
       
   189 				    iObserver.NetworkUpCallbackL();
       
   190 				    }
       
   191     			break;
       
   192     			}
       
   193     		case ESANetworkUnAvailable:
       
   194     			{
       
   195     			if (iIsNetworkCoverage)
       
   196     			    {
       
   197     			    FTRACE( FPrint( _L("CFMRadioSystemEventDetector - iObserver NetworkDownCallbackL") ) );
       
   198     			    iIsNetworkCoverage = EFalse;
       
   199     			    iObserver.NetworkDownCallbackL();
       
   200     			    }
       
   201     			break;
       
   202     			}
       
   203     		default:
       
   204     			{
       
   205     			break;   			
       
   206     			}
       
   207     		}
       
   208     	}
       
   209 	else if ( aCategory == KPSUidCtsyCallInformation && aKey == KCTsyCallState )
       
   210     	{
       
   211 		if ( ( !iIsCallActive ) && ( aValue > EPSCTsyCallStateNone ) ) 
       
   212 			{
       
   213 			FTRACE( FPrint( _L("CFMRadioSystemEventDetector - iObserver iIsCallActive = ETrue;") ) );
       
   214 			iIsCallActive = ETrue;
       
   215 			iObserver.CallActivatedCallbackL();
       
   216 			}
       
   217 		else if ( ( iIsCallActive ) && ( aValue <= EPSCTsyCallStateNone ) )
       
   218 			{
       
   219 			FTRACE( FPrint( _L("CFMRadioSystemEventDetector - iObserver iIsCallActive = EFalse;") ) );
       
   220 			iIsCallActive = EFalse;
       
   221 			iObserver.CallDeactivatedCallbackL();	
       
   222 			}
       
   223 		else
       
   224 		    {
       
   225 		    // No change
       
   226 		    }
       
   227     	}
       
   228 	else if ( aCategory == KPSUidVoiceUiAccMonitor && aKey == KVoiceUiOpenKey )
       
   229         {
       
   230         switch (aValue)
       
   231     		{
       
   232     		case KVoiceUiIsClose:
       
   233     			{
       
   234 				if (iIsVoiceUiActive)
       
   235 				    {
       
   236 				    iIsVoiceUiActive = EFalse;
       
   237                     FTRACE( FPrint( _L("CFMRadioSystemEventDetector - iObserver KVoiceUiIsClose") ) );
       
   238                     if ( iNumberOfActiveAudioClients == 0 )
       
   239                         {
       
   240                         NotifyAudioResourcesAvailability();
       
   241                         }
       
   242 				    }
       
   243     			break;
       
   244     			}
       
   245     		case KVoiceUiIsOpen:
       
   246     			{
       
   247     			FTRACE( FPrint( _L("CFMRadioSystemEventDetector - iObserver KVoiceUiIsOpen") ) );
       
   248     			if (!iIsVoiceUiActive)
       
   249     			    {
       
   250     			    iIsVoiceUiActive = ETrue;
       
   251     			    
       
   252     			    }
       
   253     			break;
       
   254     			}
       
   255     		default:
       
   256     			{
       
   257     			break;   			
       
   258     			}
       
   259     		}
       
   260         }
       
   261     else // NOP
       
   262     	{
       
   263     	}    	
       
   264 	    	
       
   265 	}
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // CFMRadioSystemEventDetector::HandlePropertyChangeErrorL
       
   269 // This is a callback function which is called when a P&S components returns 
       
   270 // an error
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 void CFMRadioSystemEventDetector::HandlePropertyChangeErrorL( const TUid& /*aCategory*/, 
       
   274                                                          	  const TUint /*aKey*/, 
       
   275                                                          	  TInt /*aError*/ )
       
   276 	{
       
   277 	}
       
   278 
       
   279 #ifndef __WINS__
       
   280 // ---------------------------------------------------------------------------
       
   281 // Handling of the byte array property changes is done here. 
       
   282 // Observer components are getting notifications in correspondence with what 
       
   283 // has changed
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 void CFMRadioSystemEventDetector::HandlePropertyChangeL( const TUid& aCategory, 
       
   287                                                     const TUint aKey, 
       
   288                                                     const TDesC8& aValue)
       
   289 	{
       
   290     if ( aCategory == KPSUidMMFAudioServer )
       
   291         {        
       
   292         if ( aKey == KAudioPolicyAudioClients )
       
   293             {
       
   294             TAudioClientList audioClients;
       
   295             audioClients.Copy( aValue );
       
   296             
       
   297             iNumberOfActiveAudioClients = audioClients().iNumOfProcesses;                                                           
       
   298             FTRACE(FPrint(_L("CFMRadioSystemEventDetector::HandlePropertyChangeL() number of audio clients: = %d"), iNumberOfActiveAudioClients));
       
   299             
       
   300             TBool autoResumePossible = ETrue;
       
   301             
       
   302             if ( iNumberOfActiveAudioClients > 0 )
       
   303                 {
       
   304                 // cancel any previously started audio resume call
       
   305                 iAutoResumeTimer->Cancel();
       
   306                 }
       
   307             
       
   308             // Check all playing audios!            
       
   309             for ( TInt i = 0; i < iNumberOfActiveAudioClients; i++ )
       
   310                 {
       
   311                 TInt cat = audioClients().iClientCategoryList[i];                                                            
       
   312                 FTRACE( FPrint( _L("CFMRadioSystemEventDetector - audio category %d"), cat ) );
       
   313 
       
   314                 if ( iNoAutoResumeAudioCategories.Find(cat) != KErrNotFound )
       
   315                     {
       
   316                     FTRACE( FPrint( _L("CFMRadioSystemEventDetector - Radio audio will not be resumed") ) );
       
   317                     autoResumePossible = EFalse;
       
   318                     }
       
   319                 else
       
   320                     {
       
   321                     FTRACE( FPrint( _L("CFMRadioSystemEventDetector - radio audio might be resumed") ) );                                      
       
   322                     }
       
   323                 }
       
   324             
       
   325                 // Decide audio resource availability from audio category info.
       
   326                 if ( !autoResumePossible )
       
   327             		{
       
   328             		// Auto resume forbidden
       
   329             		FTRACE( FPrint( _L("CFMRadioSystemEventDetector::HandlePropertyChangeL - AudioAutoResumeForbiddenL ") ) );
       
   330             		iObserver.AudioAutoResumeForbiddenL();
       
   331             		}
       
   332             	else if ( iNumberOfActiveAudioClients == 0 )
       
   333             		{
       
   334             		if ( !iIsVoiceUiActive )
       
   335             		    {            		    
       
   336             		    FTRACE( FPrint( _L("CFMRadioSystemEventDetector::HandlePropertyChangeL - NotifyAudioResourcesAvailabilityL") ) );
       
   337             		    NotifyAudioResourcesAvailability();
       
   338             		    }
       
   339             		}
       
   340 				else
       
   341 					{
       
   342 					// NOP. Some auto resume neutral audio is playing
       
   343 					}
       
   344             }
       
   345         }
       
   346  	}
       
   347 
       
   348 
       
   349 #endif //__WINS__
       
   350 
       
   351 #ifdef __WINS__
       
   352 // ---------------------------------------------------------------------------
       
   353 // Dummy version for WINS in order to avoid compiler warnings. 
       
   354 // The real implementation of function is above.
       
   355 // ---------------------------------------------------------------------------
       
   356 //
       
   357 void CFMRadioSystemEventDetector::HandlePropertyChangeL( const TUid& /*aCategory*/, 
       
   358                                                     const TUint /*aKey*/, 
       
   359                                                     const TDesC8& /*aValue*/)
       
   360 	{
       
   361  	}
       
   362 #endif // __WINS__
       
   363 
       
   364 // Handling of the text property changes is done here. 
       
   365 // Observer components are getting notifications in correspondence with what 
       
   366 // has changed
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 void CFMRadioSystemEventDetector::HandlePropertyChangeL( const TUid& /*aCategory*/, 
       
   370                                                     const TUint /*aKey*/, 
       
   371                                                     const TDesC& /*aValue*/)
       
   372 	{
       
   373 	}
       
   374 	
       
   375 // ---------------------------------------------------------------------------
       
   376 // CFMRadioSystemEventDetector::IsNetworkCoverage
       
   377 // ---------------------------------------------------------------------------
       
   378 //
       
   379 TBool CFMRadioSystemEventDetector::IsNetworkCoverage() const
       
   380 	{
       
   381 #ifdef __WINS__
       
   382     return ETrue;
       
   383 #else
       
   384 	return iIsNetworkCoverage;
       
   385 #endif
       
   386 	}
       
   387 
       
   388 // ---------------------------------------------------------------------------
       
   389 // CFMRadioSystemEventDetector::IsCallActive
       
   390 // ---------------------------------------------------------------------------
       
   391 //
       
   392 TBool CFMRadioSystemEventDetector::IsCallActive() const
       
   393 	{
       
   394 	return iIsCallActive;
       
   395 	}
       
   396 
       
   397 // ---------------------------------------------------------------------------
       
   398 // CFMRadioSystemEventDetector::NotifyAudioResourcesAvailability
       
   399 // ---------------------------------------------------------------------------
       
   400 //
       
   401 void CFMRadioSystemEventDetector::NotifyAudioResourcesAvailability()
       
   402     {
       
   403     FTRACE( FPrint( _L("CFMRadioSystemEventDetector::NotifyAudioResourcesAvailability") ) );
       
   404     // call observer interface after a delay
       
   405     iAutoResumeTimer->Cancel();
       
   406     iAutoResumeTimer->Start( TTimeIntervalMicroSeconds32( KFMRadioAutoResumeDelay ),
       
   407                              TTimeIntervalMicroSeconds32( 0 ),
       
   408                              TCallBack( StaticAutoResumeTimerCallback, this ) );    
       
   409     }
       
   410     
       
   411 // ---------------------------------------------------------------------------
       
   412 // CFMRadioSystemEventDetector::StaticAutoResumeTimerCallback
       
   413 // ---------------------------------------------------------------------------
       
   414 //
       
   415 TInt CFMRadioSystemEventDetector::StaticAutoResumeTimerCallback( TAny* aSelfPtr )
       
   416     {
       
   417     FTRACE( FPrint( _L("CFMRadioSystemEventDetector::StaticAutoResumeTimerCallback") ) );
       
   418     CFMRadioSystemEventDetector* self = static_cast<CFMRadioSystemEventDetector*>( aSelfPtr );
       
   419     if ( self )
       
   420         {
       
   421         self->iAutoResumeTimer->Cancel();
       
   422         TRAP_IGNORE( self->iObserver.AudioResourcesAvailableL() )
       
   423         }
       
   424     return KErrNone;
       
   425     }
       
   426 
       
   427 // end of file