hs_app_idlefw/plugins/devicestatus/src/hs_app_ainetworkinfolistener.cpp
branchv5backport
changeset 43 690b4f151c12
equal deleted inserted replaced
36:3310c3399a08 43:690b4f151c12
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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:  Network info listener.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <NetworkHandlingProxy.h>
       
    20 #include <CNWSession.h>
       
    21 #include "hs_app_ainetworkinfolistener.h"
       
    22 #include "hs_app_ainetworkinfoobserver.h"
       
    23 #include "debug.h"
       
    24 
       
    25 const TInt KAiMessageCacheGranularity = 4;
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 CAiNetworkInfoListener::CAiNetworkInfoListener()
       
    30 : iKeyProperties( 0, ECmpTInt )
       
    31     {
       
    32     }
       
    33 
       
    34 
       
    35 void CAiNetworkInfoListener::ConstructL()
       
    36     {
       
    37 	//Store pointer in TLS
       
    38     User::LeaveIfError( Dll::SetTls( this ) );
       
    39 
       
    40     //Create network handling engine session.
       
    41     iSession = CreateL( *this, iInfo );
       
    42     
       
    43     iShowOpInd = EFalse;
       
    44     //Create message cache
       
    45     iMessageCache = new( ELeave )CArrayFixFlat
       
    46         <MNWMessageObserver::TNWMessages>( KAiMessageCacheGranularity );
       
    47     }
       
    48 
       
    49 
       
    50 CAiNetworkInfoListener* CAiNetworkInfoListener::InstanceL()
       
    51     {
       
    52     CAiNetworkInfoListener* self = static_cast<CAiNetworkInfoListener*>( Dll::Tls() );
       
    53 
       
    54     if( !self )
       
    55         {
       
    56 		//If instance of network listener is not already constructed, create it
       
    57         self = new( ELeave ) CAiNetworkInfoListener;
       
    58         CleanupStack::PushL( self );
       
    59         self->ConstructL();
       
    60         CleanupStack::Pop( self );
       
    61         }
       
    62 
       
    63 	//increase access count
       
    64     self->IncAccessCount();
       
    65     return self;
       
    66     }
       
    67 
       
    68 
       
    69 void CAiNetworkInfoListener::Release()
       
    70     {
       
    71 	//Decrease access count, if it goes to zero, delete object.
       
    72     if( !DecAccessCount() )
       
    73         {
       
    74         delete this;
       
    75         }
       
    76     }
       
    77 
       
    78 
       
    79 CAiNetworkInfoListener::~CAiNetworkInfoListener()
       
    80     {
       
    81 	//Remove object from TLS
       
    82     Dll::SetTls( NULL );
       
    83     iObservers.Reset();
       
    84     delete iSession;
       
    85     delete iMessageCache;
       
    86     }
       
    87 
       
    88 
       
    89 TInt CAiNetworkInfoListener::IncAccessCount()
       
    90     {
       
    91     return iAccessCount++;
       
    92     }
       
    93 
       
    94 TInt CAiNetworkInfoListener::DecAccessCount()
       
    95     {
       
    96     return --iAccessCount;
       
    97     }
       
    98 
       
    99 
       
   100 void CAiNetworkInfoListener::AddObserverL( MAiNetworkInfoObserver& aObserver )
       
   101     {
       
   102 	//Removing observer doesn't remove slots from array, removed observers are only
       
   103 	//set to NULL. Reason for this is found out later on the code. Adding observer
       
   104 	//first tries to find free slot, if it is not found, observer is appended to the
       
   105 	//array.
       
   106     TInt freeSlot = iObservers.Find( NULL );
       
   107 
       
   108     if( freeSlot == KErrNotFound )
       
   109         {
       
   110         User::LeaveIfError( iObservers.Append( &aObserver ) );
       
   111         }
       
   112     else
       
   113         {
       
   114         User::LeaveIfError( iObservers.Insert( &aObserver, freeSlot ) );
       
   115         }
       
   116     }
       
   117     
       
   118     
       
   119 void CAiNetworkInfoListener::RemoveObserver( MAiNetworkInfoObserver& aObserver )
       
   120     {
       
   121 	//Remove observer, removing is done by replacing it with NULL pointer.
       
   122     const TInt count( iObservers.Count() );
       
   123 
       
   124     for( TInt i( 0 ); i < count; i++ )
       
   125         {
       
   126         if( iObservers[i] == &aObserver )
       
   127             {
       
   128             //replace it with NULL
       
   129             iObservers.Remove( i );
       
   130             iObservers.Insert( NULL, i );
       
   131             break;
       
   132             }
       
   133         }
       
   134     }
       
   135 
       
   136 
       
   137 const TNWInfo& CAiNetworkInfoListener::NetworkInfo() const
       
   138     {
       
   139     return iInfo;
       
   140     }
       
   141 
       
   142 TBool CAiNetworkInfoListener::IsOperatorIndicatorAllowed() const
       
   143     {
       
   144     return iShowOpInd;
       
   145     }
       
   146 
       
   147 TBool CAiNetworkInfoListener::MessageReceived( MNWMessageObserver::TNWMessages aMessage )
       
   148     {
       
   149 	//check if the message is in message cache.
       
   150     TInt index( KErrNotFound );
       
   151     TBool found = ( iMessageCache->FindIsq( aMessage, iKeyProperties, index ) == 0 );
       
   152     return found;
       
   153     }
       
   154 
       
   155 
       
   156 void CAiNetworkInfoListener::HandleNetworkMessage( const TNWMessages aMessage )
       
   157     {
       
   158     __PRINTS("XAI: Handle NW message");
       
   159 	//Insert message into the message cache. Only one messsage of one type.
       
   160     TRAPD( err, iMessageCache->InsertIsqL( aMessage, iKeyProperties ) );
       
   161     if( err == KErrAlreadyExists )
       
   162         {
       
   163         err = KErrNone;
       
   164         }
       
   165         
       
   166     if( err != KErrNone )
       
   167         {
       
   168         return;        
       
   169         }
       
   170 
       
   171 	iShowOpInd 		= !NotAllowedToDisplayOperatorIndicator( aMessage );
       
   172 	
       
   173 	
       
   174 	TBool hasNetInfoChanged = HasNetworkInfoChanged( aMessage );
       
   175 	
       
   176 	if ( !hasNetInfoChanged )
       
   177 		{
       
   178 		return;
       
   179 		}
       
   180 	
       
   181 	__PRINT(__DBG_FORMAT("XAI: Show operator indicator %d, info changed %d"), iShowOpInd, hasNetInfoChanged );
       
   182 	
       
   183     const TInt count( iObservers.Count() );
       
   184 
       
   185 
       
   186     for( TInt i( 0 ); i < count; i++ )
       
   187         {
       
   188 		//Observer might be NULL. When HandleNetworkInfoChange is called, it may
       
   189 		//cause deleting of the publisher which means that observer is removed from listener.
       
   190 		//This is the reason why observer pointers are replaced by NULL when it is removed
       
   191 		//from this listener. If the slot would be removed, it would easily cause index overflow
       
   192 		//or missing HandleNetworkInfoChange calls.
       
   193 		//For example lets assume that we have 5 observers, we are going in loop 3, so variable i
       
   194 		//has value 2. When third call is done, it would cause two publishers to be deleted and
       
   195 		//observers removed. Lets assume that these observers are in place 0 and 1. So now we have
       
   196 		//only 3 observers and the count is still 2. So we miss a call to a observers in index 3 and 4.
       
   197 
       
   198         if( iObservers[i] )
       
   199             {
       
   200             iObservers[i]->HandleNetworkInfoChange( aMessage, iInfo, iShowOpInd );
       
   201             }
       
   202         }
       
   203     }
       
   204 
       
   205 
       
   206 void CAiNetworkInfoListener::HandleNetworkError( const TNWOperation aOperation, TInt aErrorCode )
       
   207     {
       
   208     __PRINT(__DBG_FORMAT("XAI: Error code %d"), aErrorCode );
       
   209     switch ( aOperation )
       
   210         {
       
   211         case MNWMessageObserver::ENWGetNetworkProviderName:
       
   212             iReceivedMessageFlags |= ENetworkProviderNameReceived;
       
   213             iReceivedMessageFlags &= ~ENetworkProviderNameOk;
       
   214             iInfo.iNPName.Zero();
       
   215             __PRINTS("XAI: NPN error received");
       
   216             break;
       
   217         case MNWMessageObserver::ENWGetProgrammableOperatorName:
       
   218             iReceivedMessageFlags |= EProgrammableOperatorInfoReceived;
       
   219             iReceivedMessageFlags &= ~EProgrammableOperatorInfoReceivedOk;
       
   220             iInfo.iOperatorNameInfo.iName.Zero();
       
   221             __PRINTS("XAI: PON error received");
       
   222             break;
       
   223         case MNWMessageObserver::ENWGetServiceProviderName:
       
   224             iReceivedMessageFlags |= EServiceProviderNameReceived;
       
   225             iReceivedMessageFlags &= ~EServiceProviderNameOk;
       
   226             iInfo.iServiceProviderNameDisplayReq = RMobilePhone::KDisplaySPNNotRequired;
       
   227             iInfo.iSPName.Zero();
       
   228             iInfo.iPLMNField.Zero();
       
   229             __PRINTS("XAI: SPN error received");
       
   230             break;
       
   231         default:
       
   232             break;
       
   233         }
       
   234     
       
   235     HandleNetworkMessage( TNWMessages( KErrGeneral ) );
       
   236     }
       
   237     
       
   238     
       
   239 TBool CAiNetworkInfoListener::NotAllowedToDisplayOperatorIndicator( const TNWMessages aMessage )
       
   240 	{
       
   241 	// Service provider name must have been fetched.
       
   242     // Network provider name must have been fetched.
       
   243     // Registration status and network information must have been received.
       
   244     // Operator name information must have been received.
       
   245     // Device must be camped to a network.
       
   246 	
       
   247 	switch ( aMessage )
       
   248     	{
       
   249         case MNWMessageObserver::ENWMessageNetworkInfoChange:
       
   250             iReceivedMessageFlags |= ENetworkInfoChangeReceived;
       
   251             break;
       
   252         case MNWMessageObserver::ENWMessageNetworkRegistrationStatusChange:
       
   253             iReceivedMessageFlags |= ERegistrationStatusReceived;
       
   254             break;
       
   255         case MNWMessageObserver::ENWMessageNetworkProviderNameChange:
       
   256             iReceivedMessageFlags |= 
       
   257                 ( ENetworkProviderNameReceived + ENetworkProviderNameOk );
       
   258             break;
       
   259         case MNWMessageObserver::ENWMessageServiceProviderNameChange:
       
   260             iReceivedMessageFlags |= 
       
   261                 ( EServiceProviderNameReceived + EServiceProviderNameOk );
       
   262             break;
       
   263         case MNWMessageObserver::ENWMessageProgrammableOperatorInfoChange:
       
   264             iReceivedMessageFlags |= 
       
   265                 ( EProgrammableOperatorInfoReceived + 
       
   266                   EProgrammableOperatorInfoReceivedOk );
       
   267             break;
       
   268         case MNWMessageObserver::ENWMessageNetworkProviderNameUpdating:
       
   269             iReceivedMessageFlags &= 
       
   270                 ~( ENetworkProviderNameReceived + ENetworkProviderNameOk );
       
   271             break;
       
   272         case MNWMessageObserver::ENWMessageServiceProviderNameUpdating:
       
   273             iReceivedMessageFlags &= 
       
   274                 ~( EServiceProviderNameReceived + EServiceProviderNameOk );
       
   275             break;
       
   276         case MNWMessageObserver::ENWMessageProgrammableOperatorInfoUpdating:
       
   277             iReceivedMessageFlags &= 
       
   278                 ~( EProgrammableOperatorInfoReceived + 
       
   279                    EProgrammableOperatorInfoReceivedOk );
       
   280             break;       
       
   281         default:
       
   282             break;
       
   283         }
       
   284     
       
   285     TBool serviceProviderNameFetched( EServiceProviderNameReceived & iReceivedMessageFlags );
       
   286     TBool networkProviderNameFetched( ENetworkProviderNameReceived & iReceivedMessageFlags );
       
   287     TBool registrationStatusReceived( ERegistrationStatusReceived & iReceivedMessageFlags );
       
   288     TBool networkInformationReceived( ENetworkInfoChangeReceived & iReceivedMessageFlags );
       
   289     TBool operatorNameInformationReceived(
       
   290     	 EProgrammableOperatorInfoReceived & iReceivedMessageFlags );  
       
   291     TBool currentNetworkOk( 
       
   292     	( ENetworkInfoChangeReceived  & iReceivedMessageFlags ) && 
       
   293         ( ERegistrationStatusReceived & iReceivedMessageFlags ) &&
       
   294         ( iInfo.iStatus == ENWStatusCurrent ) );  
       
   295       
       
   296   	return
       
   297         !serviceProviderNameFetched ||
       
   298         !networkProviderNameFetched ||
       
   299         !( registrationStatusReceived && networkInformationReceived 
       
   300         	&& operatorNameInformationReceived ) ||
       
   301         !currentNetworkOk;              
       
   302 	}
       
   303 
       
   304 
       
   305 TBool CAiNetworkInfoListener::HasNetworkInfoChanged( const TNWMessages aMessage )
       
   306     {
       
   307     TBool result = ETrue;
       
   308     
       
   309     // pass through
       
   310    	if ( aMessage == MNWMessageObserver::ENWMessageCurrentHomeZoneMessage 	||
       
   311    	 	 aMessage == MNWMessageObserver::ENWMessageNetworkConnectionFailure	||
       
   312    	 	 aMessage == MNWMessageObserver::ENWMessageCurrentCellInfoMessage )
       
   313    		{
       
   314    		return result;
       
   315    		}
       
   316     
       
   317     result = ( iReceivedMessageFlags != iOldReceivedMessageFlags );
       
   318     
       
   319     if ( !result )
       
   320         {
       
   321         // Check if contents of iInfo has changed. Most probable to the 
       
   322         // beginning of the expression.
       
   323         result = 
       
   324             iInfo.iRegistrationStatus != iOldInfo.iRegistrationStatus ||
       
   325             iInfo.iStatus != iOldInfo.iStatus ||
       
   326             iInfo.iCountryCode != iOldInfo.iCountryCode ||
       
   327             iInfo.iNetworkId != iOldInfo.iNetworkId ||
       
   328             iInfo.iOperatorNameInfo.iType != 
       
   329                 iOldInfo.iOperatorNameInfo.iType ||
       
   330             iInfo.iOperatorNameInfo.iName != 
       
   331                 iOldInfo.iOperatorNameInfo.iName ||
       
   332             iInfo.iDisplayTag != iOldInfo.iDisplayTag ||
       
   333             iInfo.iShortName != iOldInfo.iShortName ||
       
   334             iInfo.iLongName != iOldInfo.iLongName ||
       
   335             iInfo.iSPName != iOldInfo.iSPName ||
       
   336             iInfo.iServiceProviderNameDisplayReq != 
       
   337                 iOldInfo.iServiceProviderNameDisplayReq ||
       
   338             iInfo.iNPName != iOldInfo.iNPName ||
       
   339             iInfo.iPLMNField != iOldInfo.iPLMNField;
       
   340         }
       
   341 
       
   342     iOldReceivedMessageFlags = iReceivedMessageFlags;
       
   343     iOldInfo = iInfo;
       
   344 
       
   345     return result;
       
   346     }
       
   347