securitydialogs/Autolock/src/AutolockAppUi.cpp
changeset 0 164170e6151a
child 5 3b17fc5c9564
child 14 b75757c81051
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002 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 #include <apgtask.h>
       
    19 #include <etelmm.h>
       
    20 #include <AknEcs.h>
       
    21 #include <aknnotedialog.h>
       
    22 #include <aknkeylock.h>
       
    23 #include <AknLayoutDef.h>
       
    24 #include <AknLayout.lag>
       
    25 #include <bldvariant.hrh>
       
    26 #include <aknappui.h> 
       
    27 #include <centralrepository.h>
       
    28 #include <settingsinternalcrkeys.h>
       
    29 #include <secuisecurityhandler.h>
       
    30 #include <secui.h>
       
    31 #include <featmgr.h>
       
    32 #include <Autolock.rsg>
       
    33 #include <mmtsy_names.h>
       
    34 #include <e32property.h>
       
    35 #include <PSVariables.h>   // Property values
       
    36 #include <coreapplicationuisdomainpskeys.h>
       
    37 #include <startupdomainpskeys.h>
       
    38 #include <ctsydomainpskeys.h>
       
    39 #include <securityuisprivatepskeys.h>
       
    40 #include <AknSgcc.h>
       
    41 
       
    42 #include "AutolockAppUiPS.h"
       
    43 #include "AutoLockValueObserverPS.h"
       
    44 #include "AutoLockModelPS.h"
       
    45 #include "AutolockApp.h"
       
    46 #include "autolock.hrh"
       
    47 #include "AutolockView.h"
       
    48 
       
    49 #include <SCPClient.h>
       
    50 #include <AknSoftNotifier.h>
       
    51 
       
    52 // sysap uid and message enums defined in eikon.hrh
       
    53 // for sending messages to sysap
       
    54 #include <eikon.hrh>
       
    55 #include <apgwgnam.h>
       
    56 #include <aknlayoutscalable_avkon.cdl.h>
       
    57 
       
    58 //  LOCAL CONSTANTS AND MACROS  
       
    59 #define KSysApUid TUid::Uid(0x100058F3)
       
    60 
       
    61 const TInt KTriesToConnectServer( 2 );
       
    62 const TInt KTimeBeforeRetryingServerConnection( 50000 );
       
    63 const TInt PhoneIndex( 0 );
       
    64 
       
    65 // ================= MEMBER FUNCTIONS =======================
       
    66 //
       
    67 // ----------------------------------------------------------
       
    68 // CAutolockAppUi::ConstructL()
       
    69 // ?implementation_description
       
    70 // ----------------------------------------------------------
       
    71 //
       
    72 void CAutolockAppUi::ConstructL()
       
    73     {
       
    74 	#if defined(_DEBUG)
       
    75     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL"));
       
    76     #endif
       
    77     
       
    78     BaseConstructL( EAknEnableSkin | EAknEnableMSK );
       
    79     
       
    80     //Disable priority control so that Autolock process priority isn't set to "background" by 
       
    81 	//window server when it is not active.
       
    82 	iEikonEnv->WsSession().ComputeMode( RWsSession::EPriorityControlDisabled ); 
       
    83 	RThread().SetProcessPriority( EPriorityHigh );
       
    84 
       
    85     FeatureManager::InitializeLibL();
       
    86 
       
    87 	RTelServer::TPhoneInfo PhoneInfo;
       
    88 	// prevent autolock shutdown
       
    89 	iEikonEnv->SetSystem( ETrue ); 
       
    90 
       
    91 	iSideKey1 = 0;
       
    92 	iSideKey2 = 0;
       
    93 	iAppKey = 0;
       
    94 
       
    95 	aCallButtonRect = TRect (0,0,0,0);
       
    96 	//connect to ETel
       
    97 
       
    98 	TInt err( KErrGeneral );
       
    99     TInt thisTry( 0 );
       
   100     
       
   101 	/*All server connections are tried to be made KTiesToConnectServer times because occasional
       
   102     fails on connections are possible at least on some servers*/
       
   103 	#if defined(_DEBUG)
       
   104     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() connect to etel server"));
       
   105     #endif
       
   106 	// connect etel server
       
   107 	while ( ( err = iServer.Connect() ) != KErrNone && ( thisTry++ ) <= KTriesToConnectServer )
       
   108         {
       
   109         User::After( KTimeBeforeRetryingServerConnection );
       
   110         }
       
   111     User::LeaveIfError( err );
       
   112 
       
   113 	#if defined(_DEBUG)
       
   114     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() load tsy"));
       
   115     #endif
       
   116     // load tsy
       
   117     err = iServer.LoadPhoneModule( KMmTsyModuleName );
       
   118 	#if defined(_DEBUG)
       
   119     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() load tsy ERROR: %d"), err);
       
   120     #endif
       
   121     if ( err != KErrAlreadyExists )
       
   122         {
       
   123         // may return also KErrAlreadyExists if some other
       
   124         // is already loaded the tsy module. And that is
       
   125         // not an error.
       
   126         User::LeaveIfError( err );
       
   127         }
       
   128 	
       
   129 	thisTry = 0;
       
   130 	#if defined(_DEBUG)
       
   131     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() open phone"));
       
   132     #endif
       
   133 	//open phone
       
   134 	User::LeaveIfError(iServer.SetExtendedErrorGranularity(RTelServer::EErrorExtended));
       
   135 	User::LeaveIfError(iServer.GetPhoneInfo(PhoneIndex, PhoneInfo));
       
   136 	User::LeaveIfError(iPhone.Open(iServer,PhoneInfo.iName));
       
   137     User::LeaveIfError(iCustomPhone.Open(iPhone));
       
   138  	#if defined(_DEBUG)
       
   139     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() phone opened"));
       
   140     #endif
       
   141 
       
   142     TBool systemLocked = EFalse;
       
   143 	TBool autolockValue = EAutolockStatusUninitialized;
       
   144 
       
   145     iWait = NULL;
       
   146     iWait = CWait::NewL();
       
   147 
       
   148 	#ifndef __WINS__
       
   149 
       
   150 
       
   151 	/*****************************************************
       
   152 	*	Series 60 Customer / ETEL
       
   153 	*	Series 60 ETEL API
       
   154 	*****************************************************/
       
   155 
       
   156 	// set autolock period to 0, if lock is disabled in DOS side
       
   157 	#if defined(_DEBUG)
       
   158     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() set autolock period to 0"));
       
   159     #endif
       
   160 	RMobilePhone::TMobilePhoneLock lockType = RMobilePhone::ELockPhoneDevice;
       
   161 	RMobilePhone::TMobilePhoneLockInfoV1 lockInfo;
       
   162 	RMobilePhone::TMobilePhoneLockInfoV1Pckg lockInfoPkg(lockInfo);
       
   163     
       
   164 	iWait->SetRequestType(EMobilePhoneGetLockInfo);
       
   165 	iPhone.GetLockInfo(iWait->iStatus, lockType, lockInfoPkg);
       
   166 	TInt res = iWait->WaitForRequestL();
       
   167 	User::LeaveIfError(res);
       
   168     TInt lockValue = 0;
       
   169     CRepository* repository = CRepository::NewL(KCRUidSecuritySettings);
       
   170     TInt cRresult = repository->Get(KSettingsAutolockStatus, lockValue);
       
   171     TBool hiddenReset = HiddenReset();
       
   172     #if defined(_DEBUG)
       
   173     if(hiddenReset)
       
   174         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() Hidden reset"));
       
   175     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() CR get result: %d"), cRresult);
       
   176     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() CR lock value: %d"), lockValue);
       
   177     #endif
       
   178 	if (lockInfo.iSetting == RMobilePhone::ELockSetDisabled)
       
   179 		{
       
   180         repository->Set(KSettingsAutoLockTime, 0);
       
   181         if ( FeatureManager::FeatureSupported( KFeatureIdProtocolCdma ) )
       
   182             {
       
   183             repository->Set(KSettingsLockOnPowerUp, 0);
       
   184             }
       
   185         }
       
   186     // In CDMA, the system can stay locked on after the boot-up sequence.
       
   187     else if ( FeatureManager::FeatureSupported( KFeatureIdProtocolCdma ) || (hiddenReset && (lockValue == 1)))
       
   188         {
       
   189         #if defined(_DEBUG)
       
   190         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() Hidden reset when locked"));
       
   191         #endif
       
   192         systemLocked = ETrue;
       
   193         }
       
   194 
       
   195         
       
   196  		if ( lockInfo.iSetting == RMobilePhone::ELockSetEnabled && lockValue != EAutolockOff && !hiddenReset)
       
   197 		{
       
   198 		    #if defined(_DEBUG)
       
   199         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() EAutolockStatusUninitialized %d"),EAutolockStatusUninitialized);
       
   200         #endif
       
   201         autolockValue = EAutolockStatusUninitialized;					            
       
   202     }
       
   203     else if (lockInfo.iSetting == RMobilePhone::ELockSetDisabled || (hiddenReset && (lockValue == 0)) )
       
   204 		{
       
   205 		    #if defined(_DEBUG)
       
   206         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() EAutolockOff %d"),EAutolockOff);
       
   207         #endif
       
   208         autolockValue = EAutolockOff;
       
   209     }
       
   210      
       
   211     delete repository;
       
   212 	#endif   //__WINS__
       
   213 
       
   214 	#if defined(_DEBUG)
       
   215     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() Enable emergency call support"));
       
   216     #endif
       
   217 	
       
   218 	#if defined(_DEBUG)
       
   219     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() Autolock view"));
       
   220     #endif
       
   221     
       
   222 
       
   223 
       
   224     // -------------------------------------------------------------------------------------------------------------
       
   225     // part of emergency call handling when telephony+devicelock is active
       
   226     // this solution is meant only for 3.1 and 3.2
       
   227 
       
   228     iEcsNote = new (ELeave) CEcsNote();
       
   229     iEcsNote->ConstructSleepingNoteL(R_AVKON_EMERGENCY_CALL_NOTE);
       
   230     iEcsNote->ButtonGroupContainer().ButtonGroup()->AsControl()->DrawableWindow()->SetOrdinalPosition(0,2);
       
   231     
       
   232     if (AknLayoutUtils::PenEnabled())	// on touch devices, if Autolock is activated from IdleScreen in landscape, the buttons need to be drawn.
       
   233 	{
       
   234 	iEcsNote->ButtonGroupContainer().SetCommandL( 0, _L("") );	// as keyboard is locked, these buttons do nothing. Better to hide their labels.
       
   235   	iEcsNote->ButtonGroupContainer().SetCommandL( EAknSoftkeyCancel, _L("") );
       
   236 	iEcsNote->ButtonGroupContainer().ButtonGroup()->AsControl()->MakeVisible(ETrue);
       
   237   	}
       
   238 
       
   239 	// Emergency call support
       
   240     iEcsDetector = CAknEcsDetector::NewL();
       
   241     iEcsDetector->SetObserver( this );
       
   242 	iEmergencySupportReady = ETrue;
       
   243     // -------------------------------------------------------------------------------------------------------------
       
   244         
       
   245 
       
   246 	// Autolock view	
       
   247 	CAutolockView* lockView = new(ELeave) CAutolockView;
       
   248     CleanupStack::PushL(lockView);
       
   249     lockView->ConstructL();
       
   250     CleanupStack::Pop();	// lockView
       
   251     AddViewL(lockView);    // transfer ownership to CAknViewAppUi
       
   252 	SetDefaultViewL(*lockView);
       
   253 
       
   254 	// start autolock timer
       
   255 	iModel = CAutoLockModel::NewL(this, autolockValue);	
       
   256 
       
   257 	// phone event observer
       
   258 	iPhoneObserver = CValueObserver::NewL(this);
       
   259 	//call bubble
       
   260 	iIncallBubble = CAknIncallBubble::NewL();
       
   261 
       
   262 	//Autokeyguard Period observer
       
   263 	#ifdef RD_AUTO_KEYGUARD
       
   264 	iKeyguardObserver = CAutoKeyguardObserver::NewL();
       
   265 	#else //!RD_AUTO_KEYGUARD
       
   266 	iKeyguardObserver = NULL;
       
   267 	#endif //RD_AUTO_KEYGUARD
       
   268     // Create the write policy. Also processes with write device data can write the value.
       
   269     TSecurityPolicy writePolicy( ECapabilityWriteDeviceData ); 
       
   270 	// Create the read policy. Also processes with read device data can read the value.	
       
   271 	TSecurityPolicy readPolicy( ECapabilityReadDeviceData ); 
       
   272 	
       
   273 	TInt tRet = RProperty::Define( KPSUidSecurityUIs, KSecurityUIsSecUIOriginatedQuery, RProperty::EInt, readPolicy, writePolicy );
       
   274         
       
   275     if ( tRet != KErrNone )
       
   276         {
       
   277         #if defined(_DEBUG)
       
   278         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL():\
       
   279             FAILED to define the SECUI query Flag: %d"), tRet);
       
   280         #endif
       
   281         }	
       
   282     
       
   283     tRet = RProperty::Define( KPSUidSecurityUIs, KSecurityUIsQueryRequestCancel, RProperty::EInt, readPolicy, writePolicy );
       
   284     if ( tRet != KErrNone )
       
   285         {
       
   286         #if defined(_DEBUG)
       
   287         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL():\
       
   288             FAILED to define the SECUI query request state Flag: %d"), tRet);
       
   289         #endif
       
   290         }
       
   291 
       
   292 if(FeatureManager::FeatureSupported(KFeatureIdSapTerminalControlFw ))  
       
   293 {
       
   294 
       
   295     // Define the TARM admin flag.
       
   296     
       
   297     tRet = RProperty::Define( KSCPSIDAutolock, SCP_TARM_ADMIN_FLAG_UID, RProperty::EInt,
       
   298         readPolicy, writePolicy );    
       
   299     if ( tRet != KErrNone )
       
   300         {
       
   301         #if defined(_DEBUG)
       
   302         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL():\
       
   303             FAILED to define the TARM Admin Flag"));
       
   304         #endif
       
   305         }
       
   306         
       
   307     // The following sequence is used to validate the configuration on SCP server.
       
   308     // This is needed on the first boot (initial or RFS) or if the C-drive has been formatted
       
   309     // (3-button format) and Autolock is not active.
       
   310 
       
   311     RSCPClient scpClient;
       
   312     if ( scpClient.Connect() == KErrNone )
       
   313         {
       
   314         TInt confStatus = scpClient.CheckConfiguration( KSCPInitial );
       
   315         
       
   316         if ( confStatus == KErrAccessDenied )
       
   317             {
       
   318             #ifndef __WINS__            
       
   319             if ( ( lockInfo.iSetting == RMobilePhone::ELockSetDisabled ) )    
       
   320             #else // __WINS__                    
       
   321             if ( 1 ) // DOS lock is never active in WINS            
       
   322             #endif // __WINS__     
       
   323 		        {					
       
   324 		        // DOS lock is not active. Note that if DOS is locked, checking the code here will
       
   325 		        // mess up the query sequence. On initial startup DOS is not locked.		                
       
   326                 
       
   327                 TInt finalConfStatus = scpClient.CheckConfiguration( KSCPComplete );
       
   328                 
       
   329                 if ( finalConfStatus == KErrAccessDenied )
       
   330                     {                
       
   331                     #ifdef __WINS__   
       
   332                     #if defined(_DEBUG)
       
   333                     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL():\
       
   334                         DOS validation FAILED in WINS, something wrong!"));
       
   335                     #endif                                  
       
   336                     #else // !__WINS__                                            
       
   337 
       
   338                     // The SCP server is out of sync and Autolock is not active. (c-drive formatted)
       
   339                     // We must ask the security code. ( Note that it is very rare that this is executed )
       
   340 	                #if defined(_DEBUG)
       
   341                     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL():\
       
   342                         Lock setting disabled, calling setlocksetting"));
       
   343                     #endif
       
   344                 
       
   345                     // Wait here until the startup is complete
       
   346                 
       
   347                     TInt tarmErr = KErrNone;
       
   348                     while ( tarmErr == KErrNone )
       
   349                         {                              
       
   350                         TInt sysState=0;
       
   351                             tarmErr = RProperty::Get(KPSUidStartup, KPSGlobalSystemState, sysState);
       
   352 
       
   353                             if ((sysState == ESwStateNormalRfOn) || (sysState == ESwStateNormalRfOff) 
       
   354                                  || (sysState == ESwStateNormalBTSap))
       
   355                                 {
       
   356                                 break;
       
   357                                 }                                        
       
   358                         User::After(500000);
       
   359                         }
       
   360                 
       
   361                     // Just change the lock setting again to disabled to request the security code.
       
   362                     // Set the TARM flag so SecUi knows it should display the "login" query.
       
   363 	                TInt tarmFlag=0;
       
   364 	                tRet = RProperty::Get( KSCPSIDAutolock, SCP_TARM_ADMIN_FLAG_UID, tarmFlag );
       
   365 	                if ( tRet == KErrNone )
       
   366     	                {
       
   367 	                    tarmFlag |= KSCPFlagResyncQuery;
       
   368 	                    tRet = RProperty::Set( KSCPSIDAutolock, SCP_TARM_ADMIN_FLAG_UID, tarmFlag );
       
   369 	                    }
       
   370 	            
       
   371 	                if ( tRet != KErrNone )
       
   372                         {
       
   373                         #if defined(_DEBUG)
       
   374                         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL():\
       
   375                             FAILED to set TARM Admin Flag"));
       
   376                         #endif
       
   377                         }
       
   378 	            
       
   379     	            RMobilePhone::TMobilePhoneLockSetting lockChange;
       
   380 	                lockChange = RMobilePhone::ELockSetDisabled;
       
   381 	                iWait->SetRequestType(EMobilePhoneSetLockSetting);
       
   382                     iPhone.SetLockSetting(iWait->iStatus, lockType, lockChange);
       
   383                 
       
   384                     res = iWait->WaitForRequestL();
       
   385                     #endif // __WINS__                                 
       
   386                     }
       
   387                 }                        
       
   388                                    
       
   389             } // if ( confStatus == KErrAccessDenied )
       
   390             
       
   391         scpClient.Close();               
       
   392         }
       
   393       
       
   394 }
       
   395 
       
   396     // Eventhough we might lock the device on boot-up (systemLocked == ETrue), we
       
   397     // want to hide the app until the handshake is done. StartUp application will
       
   398     // active the app when it is finished.
       
   399     if( !systemLocked )
       
   400         {// app to background
       
   401         #if defined(_DEBUG)
       
   402         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() app to background"));
       
   403         #endif
       
   404         TApaTask self(iCoeEnv->WsSession());
       
   405         self.SetWgId(iCoeEnv->RootWin().Identifier());
       
   406         self.SendToBackground();
       
   407         // flush
       
   408         iCoeEnv->WsSession().Flush();      
       
   409         }
       
   410     else
       
   411         {
       
   412         #if defined(_DEBUG)
       
   413         RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL() LOCK SYSTEM"));
       
   414         #endif
       
   415         TInt lockState = 0;
       
   416         
       
   417         #ifdef RD_REMOTELOCK
       
   418 	    lockState = EManualLocked; 	    
       
   419 	    #else //!RD_REMOTELOCK	    
       
   420 	    lockState = EAutolockOn; 	    
       
   421 	    #endif//RD_REMOTELOCK
       
   422         iModel->LockSystemL(lockState);  
       
   423         }
       
   424         
       
   425     iGripStatusObserver = CAutolockGripStatusObserver::NewL( this, iEikonEnv->WsSession() ); 
       
   426     iFpsStatusObserver = CAutolockFpsStatusObserver::NewL( this, iEikonEnv->WsSession() ); 
       
   427     iDeviceLockQueryStatus = EFalse;
       
   428     #if defined(_DEBUG)
       
   429     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::ConstructL()  END"));
       
   430     #endif
       
   431 	}
       
   432 // ----------------------------------------------------
       
   433 // CAutolockAppUi::~CAutolockAppUi()
       
   434 // Destructor
       
   435 // Frees reserved resources
       
   436 // ----------------------------------------------------
       
   437 //
       
   438 CAutolockAppUi::~CAutolockAppUi()
       
   439     {
       
   440     #if defined(_DEBUG)
       
   441     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::~CAutolockAppUi() BEGIN"));
       
   442     #endif
       
   443 
       
   444     if(iWait)
       
   445         {
       
   446         // Cancel active requests
       
   447         if(iWait->IsActive())
       
   448             {
       
   449             #if defined(_DEBUG)
       
   450 	        RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::~CAutolockAppUi() CANCEL REQ"));
       
   451 	        #endif
       
   452             iPhone.CancelAsyncRequest(iWait->GetRequestType());
       
   453             }
       
   454         }
       
   455     //close ETel connections
       
   456     if (iCustomPhone.SubSessionHandle())
       
   457         iCustomPhone.Close();
       
   458     
       
   459     if (iPhone.SubSessionHandle())
       
   460     	iPhone.Close();
       
   461     
       
   462     if (iServer.Handle())
       
   463         {           
       
   464         iServer.UnloadPhoneModule(KMmTsyModuleName);
       
   465         iServer.Close();
       
   466         }
       
   467 	delete iModel;
       
   468 	delete iPhoneObserver;
       
   469 	delete iIncallBubble;
       
   470 #ifdef RD_AUTO_KEYGUARD
       
   471 	delete iKeyguardObserver;
       
   472 #endif
       
   473 
       
   474     delete iEcsDetector;
       
   475     delete iEcsNote; // Ecs change
       
   476     delete iWait;
       
   477     FeatureManager::UnInitializeLib();
       
   478     delete iGripStatusObserver;
       
   479     delete iFpsStatusObserver;
       
   480     #if defined(_DEBUG)
       
   481     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::~CAutolockAppUi() END"));
       
   482     #endif
       
   483 	}
       
   484 // ----------------------------------------------------
       
   485 // CAutolockAppUi::HandleForegroundEventL()
       
   486 // Handles foreground event.
       
   487 // ----------------------------------------------------
       
   488 //
       
   489 void CAutolockAppUi::HandleForegroundEventL(TBool aForeground)
       
   490 	{
       
   491 	#if defined(_DEBUG)
       
   492     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleForegroundEventL()"));
       
   493     #endif
       
   494 	if (aForeground )
       
   495 		{
       
   496 		if (iLocked)
       
   497 			{
       
   498 			// lock voice key 
       
   499 			LockSideKeyL();			
       
   500 			}
       
   501 		else
       
   502 			{
       
   503 			// app back to background
       
   504 			TApaTask self(iCoeEnv->WsSession());
       
   505 			self.SetWgId(iCoeEnv->RootWin().Identifier());
       
   506 			self.SendToBackground();
       
   507 			}
       
   508 		}
       
   509 
       
   510 	if (!aForeground && iLocked)
       
   511 		{
       
   512         TInt callState=0;
       
   513         TInt simStatus=0;
       
   514         RProperty::Get(KPSUidCtsyCallInformation, KCTsyCallState, callState );
       
   515         RProperty::Get(KPSUidStartup, KPSSimStatus, simStatus);
       
   516 
       
   517         if (callState == EPSCTsyCallStateNone && simStatus != ESimNotPresent)
       
   518 			{	
       
   519 			// try put autolock back to foreground
       
   520 			TApaTask self(iCoeEnv->WsSession());
       
   521 			self.SetWgId(iCoeEnv->RootWin().Identifier());
       
   522 			self.BringToForeground();		
       
   523 			}
       
   524 		else
       
   525 			{
       
   526 			// unlock voice key while there is active call
       
   527 			UnLockSideKey();
       
   528 			}
       
   529 		}
       
   530 
       
   531 	CAknAppUi::HandleForegroundEventL(aForeground);
       
   532 	
       
   533 	}
       
   534 	
       
   535 
       
   536 // ----------------------------------------------------
       
   537 // CAutolockAppUi::HandleMessageL
       
   538 //
       
   539 // Handles the TARM command to unlock the phone.
       
   540 // ----------------------------------------------------
       
   541 //	
       
   542 MCoeMessageObserver::TMessageResponse CAutolockAppUi::HandleMessageL( 
       
   543                                 TUint32 aClientHandleOfTargetWindowGroup, 
       
   544                                 TUid aMessageUid, 
       
   545                                 const TDesC8& aMessageParameters 
       
   546                                )
       
   547     {
       
   548     	
       
   549     	if(!FeatureManager::FeatureSupported(KFeatureIdSapTerminalControlFw ))  
       
   550     	{
       
   551     		User::Leave(KErrNotSupported);
       
   552     	}
       
   553     #if defined(_DEBUG)
       
   554     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleMessageL()"));
       
   555     #endif
       
   556     
       
   557     MCoeMessageObserver::TMessageResponse messageResponse( EMessageHandled );
       
   558     
       
   559     if ( aMessageUid.iUid == SCP_CMDUID_UNLOCK )
       
   560         {        
       
   561         // For security reasons we must check from the SCP server did this 
       
   562         // command originate from it.                                
       
   563         RSCPClient scpClient;
       
   564         if ( scpClient.Connect() == KErrNone )
       
   565             {
       
   566             CleanupClosePushL( scpClient );
       
   567         
       
   568             if ( scpClient.QueryAdminCmd( ESCPCommandUnlockPhone ) )
       
   569                 {
       
   570                 // Switch autolock to BG
       
   571                 #if defined(_DEBUG)
       
   572                 RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleMessageL():\
       
   573                     Admin command received, unlocking"));
       
   574                 #endif            
       
   575             
       
   576                 if ( !iLocked )
       
   577                     {
       
   578                     #if defined(_DEBUG)
       
   579                     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleMessageL():\
       
   580                         Ignoring Unlock message - not locked."));
       
   581                     #endif            
       
   582                     }   
       
   583                 else
       
   584                     {
       
   585                     iLocked = EFalse;
       
   586                   DisableWGListChangeEventListening();  
       
   587 	                UnLockKeys();
       
   588 	                iModel->SetLockedL(EFalse);
       
   589 			// Disable keyguard after remote unlock
       
   590 	                RAknKeyLock iKeyLock;
       
   591 	                TInt tempResult = iKeyLock.Connect();
       
   592 	                iKeyLock.DisableKeyLock();
       
   593 	                // end Disable keyguard after remote unlock
       
   594 	                SwitchToPreviousAppL();
       
   595                     }   
       
   596                 }           
       
   597             else
       
   598                 {
       
   599                 #if defined(_DEBUG)
       
   600                 RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleMessageL():\
       
   601                     Unauthorized attempt to unlock"));
       
   602                 #endif
       
   603                 }
       
   604         
       
   605             CleanupStack::PopAndDestroy(); // scpClient                               
       
   606             }
       
   607         else
       
   608             {          
       
   609             #if defined(_DEBUG)
       
   610             RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleMessageL():\
       
   611                 Failed to connect to SCP, ignoring unlock-message."));
       
   612             #endif            
       
   613             
       
   614             }
       
   615         }               
       
   616     else // aMessageUid.iUid != SCP_CMDUID_UNLOCK 
       
   617         {
       
   618         messageResponse = CAknAppUi::HandleMessageL( aClientHandleOfTargetWindowGroup, 
       
   619                                                      aMessageUid,
       
   620                                                      aMessageParameters
       
   621                                                    );
       
   622         }
       
   623     
       
   624     return messageResponse;
       
   625     }
       
   626     	 	
       
   627  	
       
   628 // ------------------------------------------------------------------------------
       
   629 // CAutolockAppUi::::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
       
   630 //  This function is called by the UIKON framework just before it displays
       
   631 //  a menu pane. Its default implementation is empty, and by overriding it,
       
   632 //  the application can set the state of menu items dynamically according
       
   633 //  to the state of application data.
       
   634 // ------------------------------------------------------------------------------
       
   635 //
       
   636 void CAutolockAppUi::DynInitMenuPaneL(
       
   637     TInt /*aResourceId*/,CEikMenuPane* /*aMenuPane*/)
       
   638     {
       
   639     }
       
   640 
       
   641 void CAutolockAppUi::HandlePointerEventL(const TPointerEvent& aPointerEvent)
       
   642     {
       
   643     RDebug::Printf( "%s %s (%u) value=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, 0 );
       
   644 		}
       
   645 // ----------------------------------------------------
       
   646 // CAutolockAppUi::HandleKeyEventL(
       
   647 //     const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
       
   648 // ----------------------------------------------------
       
   649 //
       
   650 TKeyResponse CAutolockAppUi::HandleKeyEventL(
       
   651     const TKeyEvent& aKeyEvent,TEventCode aType)
       
   652     {
       
   653 	if ( aKeyEvent.iCode == EKeyBell)
       
   654  		{
       
   655  		HandleCommandL(ESecUiCmdUnlock);
       
   656  		return EKeyWasConsumed; 
       
   657  		}
       
   658 
       
   659     TBool featureNoPowerkey = FeatureManager::FeatureSupported( KFeatureIdNoPowerkey );
       
   660     // If power key pressed, tell SysAp about if
       
   661     if( (aKeyEvent.iScanCode == EStdKeyDevice2 && aType == EEventKeyDown )
       
   662         || (aType == EEventKey && featureNoPowerkey && aKeyEvent.iCode == EKeyNo))
       
   663         {
       
   664         SendMessageToSysAp(EEikKeyLockPowerKeyPressed);
       
   665         if ( featureNoPowerkey )
       
   666             {
       
   667             SendMessageToSysAp(EEikKeyLockLightsOnRequest);              
       
   668             }
       
   669         }
       
   670     return EKeyWasNotConsumed;
       
   671     }
       
   672 
       
   673 void CAutolockAppUi::SendMessageToSysAp(TInt aMessage)
       
   674     {
       
   675     RWsSession& ws = iEikonEnv->WsSession();
       
   676     TInt wgId=0;
       
   677     CApaWindowGroupName::FindByAppUid(KSysApUid, ws, wgId);
       
   678     if (wgId)
       
   679         {
       
   680         TWsEvent event;
       
   681         event.SetType(aMessage);
       
   682         event.SetTimeNow();
       
   683         ws.SendEventToWindowGroup(wgId, event);
       
   684         }
       
   685     }
       
   686 
       
   687 // ----------------------------------------------------
       
   688 // HideSoftNotification()
       
   689 // dismiss all the pending notes just before asking the unlocking code
       
   690 // ----------------------------------------------------
       
   691 //
       
   692 void HideSoftNotification()
       
   693     {
       
   694     #if defined(_DEBUG)
       
   695     RDebug::Print(_L("(AUTOLOCK)HideSoftNotification"));
       
   696     #endif
       
   697     CAknSoftNotifier *softNotifier = NULL;
       
   698     TRAPD (err, {
       
   699             softNotifier = CAknSoftNotifier::NewL();
       
   700             softNotifier->AddNotificationL(ESetIdleState, 0);
       
   701     };)
       
   702     delete softNotifier;
       
   703     }
       
   704 // CAutolockAppUi::HandleCommandL(TInt aCommand)
       
   705 // ----------------------------------------------------
       
   706 //
       
   707 void CAutolockAppUi::HandleCommandL(TInt aCommand)
       
   708     {
       
   709 	#if defined(_DEBUG)
       
   710     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleCommandL()"));
       
   711     #endif
       
   712 	switch ( aCommand )
       
   713         {
       
   714         case EEikCmdExit:
       
   715 			{
       
   716 			#if defined(_DEBUG)
       
   717 			RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleCommandL() EEikCmdExit"));
       
   718 			#endif
       
   719             Exit();
       
   720             break;
       
   721 			}
       
   722         case ESecUiCmdUnlock:
       
   723 			{
       
   724 			#if defined(_DEBUG)
       
   725 			RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleCommandL() ESecUiCmdUnlock"));
       
   726 			#endif
       
   727 			// stop observing emergency call event
       
   728 			iEmergencySupportReady = EFalse;
       
   729 			iEcsDetector->Reset(); // Ecs queue is cleared; keys up til here are forgotten
       
   730 			// ask secuity code
       
   731 			CSecurityHandler* handler = new (ELeave) CSecurityHandler(iPhone);
       
   732 			CleanupStack::PushL(handler);
       
   733 			TSecUi::InitializeLibL();	
       
   734 	        
       
   735 	        // Put the lights on when security query is shown
       
   736 	        SendMessageToSysAp( EEikSecurityQueryLights );
       
   737 	        HideSoftNotification();	// dismiss all the pending notes just before asking the unlocking code
       
   738 	        
       
   739             TRAPD(err,
       
   740 			{
       
   741 			iDeviceLockQueryStatus = ETrue;
       
   742 			if(handler->AskSecCodeInAutoLockL())
       
   743 				{		
       
   744 				iLocked = EFalse;
       
   745 				DisableWGListChangeEventListening();
       
   746 				iDeviceLockQueryStatus = EFalse;
       
   747 				UnLockKeys();
       
   748 				iModel->SetLockedL(EFalse);
       
   749 				SwitchToPreviousAppL();
       
   750 				}
       
   751             else
       
   752 				{  // make sure that we will be topmost still
       
   753 				    iDeviceLockQueryStatus = EFalse;
       
   754                     TInt callState;
       
   755                     RProperty::Get( KPSUidCtsyCallInformation, KCTsyCallState, callState );
       
   756                 if ( callState == EPSCTsyCallStateNone &&
       
   757                      !FeatureManager::FeatureSupported( KFeatureIdProtocolCdma ) )
       
   758                     {
       
   759 				    TApaTask self(CCoeEnv::Static()->WsSession());
       
   760 				    self.SetWgId(CCoeEnv::Static()->RootWin().Identifier());
       
   761 				    self.BringToForeground();
       
   762                     TBool featureNoPowerkey = FeatureManager::FeatureSupported( KFeatureIdNoPowerkey );
       
   763                     if ( featureNoPowerkey )
       
   764                         {//set lights on in case user pressed "red button". If he pressed cancel the lights are on anyway so it doesn't matter.
       
   765                         SendMessageToSysAp(EEikKeyLockLightsOnRequest);              
       
   766                         }
       
   767 				    // we don't want enable lock if call in progress    
       
   768                     RProperty::Get( KPSUidCtsyCallInformation, KCTsyCallState, callState );
       
   769                     TInt keyguardDisableState(ECoreAppUIsDisableKeyguardUninitialized);
       
   770                     //If there is alarm on the keyguard status is set to disabled. In that case don't enable keyguard as it will be done by SysAp 
       
   771                     //after the alarm has been disabled/snoozed. Otherwise the alarm CBA is left under keyguard CBA.
       
   772                     RProperty::Get( KPSUidCoreApplicationUIs, KCoreAppUIsDisableKeyguard, keyguardDisableState );
       
   773                     if ((callState == EPSCTsyCallStateNone) && (keyguardDisableState != ECoreAppUIsDisableKeyguard))
       
   774                         {   
       
   775 				    	RAknKeyLock keylock;
       
   776 					    if ( keylock.Connect() == KErrNone )
       
   777 						    {
       
   778 						    keylock.EnableAutoLockEmulation();
       
   779 						    keylock.Close();
       
   780 						    }
       
   781 					    }
       
   782                     }
       
   783                 }
       
   784 			};)
       
   785 
       
   786 			// start observing emergency call event
       
   787 			iEmergencySupportReady = ETrue;
       
   788 			CleanupStack::PopAndDestroy(handler); // handler
       
   789 			TSecUi::UnInitializeLib();  // secui 		
       
   790 			
       
   791 			User::LeaveIfError(err);
       
   792 
       
   793 			break;
       
   794 			}
       
   795         default:
       
   796             break;      
       
   797         } 
       
   798     }
       
   799 
       
   800 // ----------------------------------------------------
       
   801 // CAutolockAppUi::SwitchToPreviousAppL()
       
   802 // Activates previous app 
       
   803 //----------------------------------------------------
       
   804 //
       
   805 void CAutolockAppUi::SwitchToPreviousAppL()
       
   806 	{
       
   807 	#if defined(_DEBUG)
       
   808 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::SwitchToPreviousAppL()"));
       
   809 	#endif
       
   810 	iEcsDetector->Reset(); // Ecs queue is cleared; keys up til here are forgotten
       
   811 	// stop observing phone events
       
   812 	iPhoneObserver->Stop();
       
   813 	CAknView* view = View(KAutoLockViewId);
       
   814 	if(view)
       
   815 	  {
       
   816 	  STATIC_CAST(CAutolockView*, view)->MakeVisible(EFalse);
       
   817 	  }
       
   818 	else
       
   819 	  {
       
   820 	  #if defined(_DEBUG)
       
   821 	  RDebug::Printf( "%s %s (%u) no view ????=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, view );
       
   822 	  #endif
       
   823 	  }
       
   824 	// app to background
       
   825 	TApaTask self(iCoeEnv->WsSession());
       
   826 	self.SetWgId(iCoeEnv->RootWin().Identifier());
       
   827 	self.SendToBackground();
       
   828 	// flush
       
   829 	iCoeEnv->WsSession().Flush();
       
   830 	}
       
   831 // ----------------------------------------------------
       
   832 // CAutolockAppUi::BringAppToForegroundL()
       
   833 // Activates autolock app
       
   834 //----------------------------------------------------
       
   835 //
       
   836 void CAutolockAppUi::BringAppToForegroundL()
       
   837 	{
       
   838 	#if defined(_DEBUG)
       
   839 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::BringAppToForegroundL()"));
       
   840 	#endif
       
   841 	TBool tarmAdminFlag(EFalse);
       
   842 	TBool remotelockState(EFalse);
       
   843 if(FeatureManager::FeatureSupported(KFeatureIdSapTerminalControlFw ))
       
   844 {
       
   845     tarmAdminFlag = TarmState();
       
   846 }
       
   847 
       
   848 #ifdef RD_REMOTELOCK
       
   849         TInt autolockState;
       
   850         RProperty::Get(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, autolockState);
       
   851         remotelockState = (autolockState == ERemoteLocked);           
       
   852 #endif//RD_REMOTELOCK
       
   853 	// If TARM admin flag is set, bring Autolock to foreground regardless of call state.
       
   854 	if(!tarmAdminFlag)
       
   855 	{   //If phone has been remote locked bring Autolock to foreground regardless of call state.
       
   856 	    if(!remotelockState)
       
   857 	    {   // check if there is active call
       
   858     	    TInt callState;
       
   859     	    RProperty::Get( KPSUidCtsyCallInformation, KCTsyCallState, callState );
       
   860             if (callState != EPSCTsyCallStateNone )
       
   861                 {
       
   862     		    iModel->SetLockedL(EFalse);
       
   863     		    iModel->ResetInactivityTimeout();
       
   864     		    UnLockKeys();
       
   865     		    return;
       
   866     		    }   
       
   867 	    }
       
   868 	    
       
   869 	}
       
   870          		
       
   871 
       
   872 	#ifndef __WINS__
       
   873 
       
   874 	#ifndef __NO_DOS__
       
   875 
       
   876 		/*****************************************************
       
   877 		*	Series 60 Customer / ETEL
       
   878 		*	Series 60 ETEL API
       
   879 		*****************************************************/
       
   880 
       
   881 	// check that device locked in DOS side too
       
   882 	RMobilePhone::TMobilePhoneLock lockType = RMobilePhone::ELockPhoneDevice;
       
   883 	RMobilePhone::TMobilePhoneLockInfoV1 lockInfo;
       
   884 	RMobilePhone::TMobilePhoneLockInfoV1Pckg lockInfoPkg(lockInfo);
       
   885 
       
   886 	#if defined(_DEBUG)
       
   887 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::BringAppToForegroundL() GetLockInfo"));
       
   888 	#endif
       
   889 	iWait->SetRequestType(EMobilePhoneGetLockInfo);
       
   890 	iPhone.GetLockInfo(iWait->iStatus, lockType, lockInfoPkg);
       
   891 	TInt res = iWait->WaitForRequestL();
       
   892 	User::LeaveIfError(res);
       
   893 	if (iWait->iStatus == KErrNone)
       
   894 		{
       
   895 		// if not don't lock ui either
       
   896 		#if defined(_DEBUG)
       
   897 		RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::BringAppToForegroundL() KErrNone"));
       
   898 		#endif
       
   899 		if (lockInfo.iSetting == RMobilePhone::ELockSetDisabled)
       
   900 			{
       
   901 			iModel->SetLockedL(EFalse);
       
   902 			UnLockKeys();
       
   903 			return;
       
   904 			}
       
   905 		}
       
   906 	#endif
       
   907 
       
   908 	#endif
       
   909 
       
   910 	// in case Telephone-app is topmost, then the user is confused because he sees but can't use it.
       
   911 	// therefore it's required to hide it, by sending to background. ETMA-7M8A2Y 20090105
       
   912 	if ( AknLayoutUtils::PenEnabled() )
       
   913      	{ 		
       
   914    			TApaTaskList apaTaskList(CCoeEnv::Static()->WsSession());
       
   915    			#define KPhoneAppUid TUid::Uid(0x100058B3)
       
   916 
       
   917    			TApaTask apaTask = apaTaskList.FindApp(KPhoneAppUid);
       
   918    			if (apaTask.Exists())
       
   919        	{
       
   920 			 		#if defined(_DEBUG)
       
   921 			 			RDebug::Print(_L("(AUTOLOCK)CAknKeyLockControl::EnableKeylock() Bring phone to background"));
       
   922 			 		#endif
       
   923        		// Bring phone to background
       
   924        		apaTask.SendToBackground();
       
   925        	}
       
   926 			}	
       
   927 	
       
   928 	iPhoneObserver->Start();
       
   929 	iLocked = ETrue;
       
   930 	// app to foreground	
       
   931 	TApaTask self(iCoeEnv->WsSession());
       
   932 	self.SetWgId(iCoeEnv->RootWin().Identifier());
       
   933 	self.BringToForeground();
       
   934 	ActivateLocalViewL(KAutoLockViewId);
       
   935 	CAknView* view = View(KAutoLockViewId);
       
   936 	if(view)
       
   937 	  {
       
   938 	  STATIC_CAST(CAutolockView*, view)->MakeVisible(ETrue);
       
   939 	  }
       
   940 	else
       
   941 	  {
       
   942 	  #if defined(_DEBUG)
       
   943 	  RDebug::Printf( "%s %s (%u) no view ????=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, view );
       
   944 	  #endif
       
   945 	  }
       
   946 	}
       
   947 
       
   948 // ----------------------------------------------------
       
   949 // CAutolockAppUi::LockKeysL()
       
   950 // locks keys 
       
   951 //----------------------------------------------------
       
   952 //
       
   953 void CAutolockAppUi::LockKeysL()
       
   954 	{
       
   955 	#if defined(_DEBUG)
       
   956 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::LockKeysL()"));
       
   957 	#endif
       
   958 	// capture appkey & volume key
       
   959 	if (!iAppKey)
       
   960 		{
       
   961 		RWindowGroup& groupWin=iCoeEnv->RootWin();
       
   962 		iAppKey = groupWin.CaptureKeyUpAndDowns(EStdKeyApplication0, 0, 0); // Capture app key
       
   963 		}
       
   964 	LockSideKeyL();
       
   965 	}
       
   966 // ----------------------------------------------------
       
   967 // CAutolockAppUi::UnLockKeys()
       
   968 // unlocks keys 
       
   969 //----------------------------------------------------
       
   970 //
       
   971 void CAutolockAppUi::UnLockKeys()
       
   972 	{
       
   973 	#if defined(_DEBUG)
       
   974 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::UnLockKeysL()"));
       
   975 	#endif
       
   976 	// uncapture appkey
       
   977 	if (iAppKey)
       
   978 		{
       
   979 		RWindowGroup& groupWin=iCoeEnv->RootWin();
       
   980 		groupWin.CancelCaptureKeyUpAndDowns(iAppKey);
       
   981 		iAppKey = 0;
       
   982 		}
       
   983 	UnLockSideKey();
       
   984 	}
       
   985 // ----------------------------------------------------
       
   986 // CAutolockAppUi::LockSideKeyL()
       
   987 // unlocks side-key 
       
   988 //----------------------------------------------------
       
   989 //
       
   990 void CAutolockAppUi::LockSideKeyL()
       
   991 	{
       
   992 	#if defined(_DEBUG)
       
   993 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::LockSideKeyL()"));
       
   994 	#endif
       
   995 	if (!iSideKey1)
       
   996 		{
       
   997 		RWindowGroup& groupWin=iCoeEnv->RootWin();
       
   998 		iSideKey1 = groupWin.CaptureKey(EKeySide,0,0);
       
   999 		iSideKey2 = groupWin.CaptureKeyUpAndDowns(EStdKeyDevice6, 0, 0);
       
  1000 		}
       
  1001 	}
       
  1002 
       
  1003 // -------------------------------------------------------------------------------------------------------------
       
  1004 // part of emergency call handling when telephony+devicelock is active
       
  1005 // this solution is meant only for 3.1 and 3.2
       
  1006 void CAutolockAppUi::HandleEcsEvent( CAknEcsDetector* aEcsDetector, CAknEcsDetector::TState aState )
       
  1007     {
       
  1008     switch ( aState )
       
  1009         {
       
  1010         case CAknEcsDetector::ECompleteMatchThenSendKey:
       
  1011             // Do nothing since note will be removed on ECallAttempted event
       
  1012             break;
       
  1013         case CAknEcsDetector::ECompleteMatch:
       
  1014             iEcsNote->SetEmergencyNumber( aEcsDetector->CurrentMatch() );
       
  1015             
       
  1016             // Tell sysAp to switch lights on
       
  1017             SendMessageToSysAp( EEikEcsQueryLights );
       
  1018             
       
  1019             iEcsNote->ShowNote();
       
  1020             iEcsNote->iNoteOnScreen =ETrue;
       
  1021             break;
       
  1022         case CAknEcsDetector::EPartialMatch:
       
  1023             iEcsNote->SleepNote();
       
  1024             break;
       
  1025         case CAknEcsDetector::ECallAttempted:
       
  1026             iEcsNote->SleepNote();
       
  1027             break;
       
  1028         case CAknEcsDetector::EEmpty:
       
  1029             iEcsNote->SleepNote();
       
  1030             break;
       
  1031         case CAknEcsDetector::ENoMatch:
       
  1032             iEcsNote->SleepNote();
       
  1033             break;
       
  1034         default:
       
  1035             break;
       
  1036         }
       
  1037     }
       
  1038 
       
  1039 CEcsNote::CEcsNote() : iNoteOnScreen( EFalse )
       
  1040     {}
       
  1041     
       
  1042 void CEcsNote::ConstructSleepingNoteL(TInt aResourceId)
       
  1043     {
       
  1044     CAknNoteDialog::ConstructSleepingDialogL(aResourceId);
       
  1045     }
       
  1046     
       
  1047 TInt CEcsNote::ShowNote()
       
  1048     {
       
  1049     ReportUserActivity();
       
  1050     iTimeoutInMicroseconds = CAknNoteDialog::EUndefinedTimeout;
       
  1051     iTone = CAknNoteDialog::ENoTone;    
       
  1052     if (!iNoteOnScreen)
       
  1053         {  
       
  1054         return RouseSleepingDialog();
       
  1055         }
       
  1056         // return value not used
       
  1057     else
       
  1058         // return value not used
       
  1059         return NULL;
       
  1060     }
       
  1061 
       
  1062 void CEcsNote::SleepNote()
       
  1063     {
       
  1064     if (iNoteOnScreen)
       
  1065         ExitSleepingDialog(); // Causes flicker to other notes if called when note is not on screen
       
  1066     iNoteOnScreen = EFalse;
       
  1067     }
       
  1068 
       
  1069 TKeyResponse CEcsNote::OfferKeyEventL(const TKeyEvent& /*aKeyEvent*/, TEventCode /*aType*/)
       
  1070     {
       
  1071     return EKeyWasConsumed;
       
  1072     }
       
  1073 
       
  1074 void CEcsNote::SetEmergencyNumber( const TDesC& aMatchedNumber )
       
  1075     {
       
  1076     TRect screen(iAvkonAppUi->ApplicationRect());
       
  1077     TAknLayoutRect mainPane;
       
  1078     mainPane.LayoutRect(screen, AKN_LAYOUT_WINDOW_main_pane(screen, 0, 1, 1));
       
  1079     TAknLayoutRect popupNoteWindow;
       
  1080     AknLayoutUtils::TAknCbaLocation cbaLocation( AknLayoutUtils::CbaLocation() );
       
  1081     TInt variety( 0 );
       
  1082     if ( cbaLocation == AknLayoutUtils::EAknCbaLocationRight )
       
  1083         {
       
  1084         variety = 5;
       
  1085         }
       
  1086     else if ( cbaLocation == AknLayoutUtils::EAknCbaLocationLeft )
       
  1087         {
       
  1088         variety = 8;
       
  1089         }
       
  1090     else
       
  1091         {
       
  1092         variety = 2;
       
  1093         }
       
  1094 
       
  1095     popupNoteWindow.LayoutRect(mainPane.Rect(), AknLayoutScalable_Avkon::popup_note_window( variety ));
       
  1096     TAknLayoutText textRect;
       
  1097     textRect.LayoutText(popupNoteWindow.Rect(), AKN_LAYOUT_TEXT_Note_pop_up_window_texts_Line_1(4));
       
  1098 
       
  1099     // Size of a temporary buffer that contains new lines, spaces and 
       
  1100     // emergency number for a note.
       
  1101     TBuf16<KAknEcsMaxMatchingLength+80> number;
       
  1102     number.Append('\n');
       
  1103     number.Append('\n');
       
  1104 
       
  1105     TInt spaceCharWidthInPixels = textRect.Font()->CharWidthInPixels(' ');
       
  1106     if (spaceCharWidthInPixels < 1)
       
  1107         {
       
  1108         // Avoid divide by zero situation even the space char would have zero length.
       
  1109         spaceCharWidthInPixels = 1;
       
  1110         }
       
  1111     
       
  1112     TInt length = (textRect.TextRect().Width() - textRect.Font()->TextWidthInPixels(aMatchedNumber))
       
  1113                     / spaceCharWidthInPixels;
       
  1114 
       
  1115     const TInt matchedNumberLength = aMatchedNumber.Length();
       
  1116     const TInt numberLength = number.Length();
       
  1117     const TInt numberMaxLength = number.MaxLength();
       
  1118     
       
  1119     if ( numberLength + length + matchedNumberLength > numberMaxLength)
       
  1120         {
       
  1121         // To make sure that buffer overflow does not happen.
       
  1122         length = numberMaxLength - numberLength - matchedNumberLength;
       
  1123         }
       
  1124     for (int i = 0; i < length ; i++)
       
  1125         {
       
  1126         number.Append(' ');
       
  1127         }
       
  1128 
       
  1129     number.Append(aMatchedNumber);
       
  1130     TRAP_IGNORE(SetTextL(number));
       
  1131 
       
  1132     }
       
  1133         
       
  1134 // -------------------------------------------------------------------------------------------------------------
       
  1135 	
       
  1136 // ----------------------------------------------------
       
  1137 // CAutolockAppUi::UnLockSideKey()
       
  1138 // unlocks side-key 
       
  1139 //----------------------------------------------------
       
  1140 //
       
  1141 void CAutolockAppUi::UnLockSideKey()
       
  1142 	{
       
  1143 	#if defined(_DEBUG)
       
  1144 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::UnLockSideKeyL()"));
       
  1145 	#endif
       
  1146 	if (iSideKey1)
       
  1147 		{
       
  1148 		RWindowGroup& groupWin=iCoeEnv->RootWin();
       
  1149 		groupWin.CancelCaptureKeyUpAndDowns(iSideKey2);
       
  1150 		groupWin.CancelCaptureKey(iSideKey1);
       
  1151 		iSideKey1 = 0;
       
  1152 		iSideKey2 = 0;
       
  1153 		}
       
  1154 	}
       
  1155 	
       
  1156 
       
  1157 //
       
  1158 // ---------------------------------------------------------
       
  1159 // CAutolockAppUi::HandleScreenDeviceChangedL()
       
  1160 // Handles screen layout changes, called by CCoeAppUi
       
  1161 // ---------------------------------------------------------
       
  1162 //
       
  1163 void CAutolockAppUi::HandleScreenDeviceChangedL()
       
  1164 {
       
  1165   CAknAppUiBase::HandleScreenDeviceChangedL();
       
  1166    //get autolock view from CAknViewAppUi
       
  1167   CAknView* view = View(KAutoLockViewId);
       
  1168   if(view)
       
  1169     {
       
  1170         STATIC_CAST(CAutolockView*, view)->ScreenDeviceChanged();
       
  1171     }
       
  1172   
       
  1173 }
       
  1174 
       
  1175 //
       
  1176 // ----------------------------------------------------------
       
  1177 // CAutolockAppUi::IsPinBlocked()
       
  1178 // Checks whether PIN1/UPIN is blocked
       
  1179 // ----------------------------------------------------------
       
  1180 //
       
  1181 TBool CAutolockAppUi::IsPinBlocked()
       
  1182 {
       
  1183 	RMmCustomAPI::TSecurityCodeType secCodeType;
       
  1184 	TBool wcdmaSupported(FeatureManager::FeatureSupported( KFeatureIdProtocolWcdma ));
       
  1185 	TBool upinSupported(FeatureManager::FeatureSupported( KFeatureIdUpin ));
       
  1186 
       
  1187 	if(wcdmaSupported || upinSupported)
       
  1188 	  {
       
  1189     	RMobilePhone::TMobilePhoneSecurityCode activePin;
       
  1190     	iCustomPhone.GetActivePin(activePin);
       
  1191     	if(activePin == RMobilePhone::ESecurityUniversalPin)
       
  1192     		secCodeType = RMmCustomAPI::ESecurityUniversalPin;
       
  1193     	else
       
  1194     		secCodeType = RMmCustomAPI::ESecurityCodePin1;
       
  1195 	  }
       
  1196 	else 
       
  1197 	    secCodeType = RMmCustomAPI::ESecurityCodePin1;
       
  1198 	
       
  1199 	TBool isBlocked = EFalse;
       
  1200     iCustomPhone.IsBlocked(secCodeType,isBlocked);
       
  1201     #if defined(_DEBUG)
       
  1202     RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::IsPinBlocked result: %d"), isBlocked);
       
  1203     #endif
       
  1204 	return isBlocked;
       
  1205 }
       
  1206 
       
  1207 //
       
  1208 // ----------------------------------------------------------
       
  1209 // CAutolockAppUi::HandleWsEventL()
       
  1210 // 
       
  1211 // ----------------------------------------------------------
       
  1212 //
       
  1213 void CAutolockAppUi::HandleWsEventL( const TWsEvent& aEvent,CCoeControl* aDestination )
       
  1214     {
       
  1215     	const TInt type = aEvent.Type();
       
  1216     	
       
  1217     	switch ( type )
       
  1218     	{
       
  1219     		case KAknFullOrPartialForegroundLost: // partial or full fg lost
       
  1220     			if( iIncallBubble )
       
  1221                     {
       
  1222                     #if defined(_DEBUG)
       
  1223     				RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleWsEventL: DISABLE call bubble"));
       
  1224     				#endif                    
       
  1225                     iIncallBubble->SetIncallBubbleAllowedInIdleL( EFalse );
       
  1226                     }
       
  1227 				break;
       
  1228 		    case KAknFullOrPartialForegroundGained: // partial or full fg gained
       
  1229 		    	if( iIncallBubble )
       
  1230 		           	{
       
  1231 		             	#if defined(_DEBUG)
       
  1232 		    			RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HandleWsEventL: ENABLE call bubble"));
       
  1233 		    			#endif                      
       
  1234 		                iIncallBubble->SetIncallBubbleAllowedInIdleL( ETrue );
       
  1235 		            }
       
  1236 		    	break;	
       
  1237     	    case EEventWindowGroupListChanged:
       
  1238     	        HandleWindowGroupListChange();
       
  1239     	        break;
       
  1240     	    case EEventPointer:
       
  1241     	    case EEventDragDrop:
       
  1242     	    		{
       
  1243     	    		TInt callState = 0;
       
  1244     	    		RProperty::Get( KPSUidCtsyCallInformation, KCTsyCallState, callState );
       
  1245 			        if ( 1==1 || callState == EPSCTsyCallStateNone || callState == EPSCTsyCallStateUninitialized )
       
  1246 			        	{
       
  1247 	    	        TPointerEvent *pointer = aEvent.Pointer();
       
  1248 	    	          CAknView* view = View(KAutoLockViewId);
       
  1249 								  if(view)
       
  1250 								    {	
       
  1251 								        STATIC_CAST(CAutolockView*, view)->ScreenDeviceChanged();
       
  1252 								    	TRect aCallRect;
       
  1253 								        STATIC_CAST(CAutolockView*, view)->HandleCall(1, aCallRect);
       
  1254    								    	if(aCallButtonRect.iBr.iX==0)
       
  1255    								    		aCallButtonRect = TRect (aCallRect);
       
  1256 								    }
       
  1257 	    	        if(pointer->iType==TPointerEvent::EButton1Up)
       
  1258 	    	        	{
       
  1259 		    	        TPoint iPosition = pointer->iPosition;
       
  1260 						if(aCallButtonRect.iBr.iX<iPosition.iX && iPosition.iX<aCallButtonRect.iBr.iX+aCallButtonRect.iTl.iX && iPosition.iY<400 )
       
  1261 	    		        	{
       
  1262 	    		        		#define KPhoneAppUid1 TUid::Uid(0x100058B3)
       
  1263 	    		        		TApaTaskList tasklist( iCoeEnv->WsSession() );
       
  1264                       TApaTask phonetask = tasklist.FindApp( KPhoneAppUid1 );
       
  1265 	    		        		if ( phonetask.Exists() )
       
  1266                         {
       
  1267 										    TRawEvent event;
       
  1268 										    event.Set(TRawEvent::EKeyDown, EStdKeyNo);
       
  1269 										    iEikonEnv->WsSession().SimulateRawEvent(event);
       
  1270                         					User::After(1000);
       
  1271 										    event.Set(TRawEvent::EKeyUp, EStdKeyNo);
       
  1272 										    iEikonEnv->WsSession().SimulateRawEvent(event);
       
  1273                         }
       
  1274 	    		        	}
       
  1275 	    		        }
       
  1276 	    		      }
       
  1277     		      }
       
  1278     	        break;
       
  1279     	    case EEventKeyUp:	// on touch devices, this happens only for the switch-key, which should turn on the lights.
       
  1280     	    case EEventKey:
       
  1281     	    case EEventKeyDown:
       
  1282     		if(iLocked)
       
  1283 	    	        SendMessageToSysAp( EEikSecurityQueryLights );
       
  1284 	    	        break;
       
  1285 		    default:
       
  1286 		    	break;
       
  1287     	}
       
  1288     	
       
  1289     	// All events are sent to base class.
       
  1290     	CAknViewAppUi::HandleWsEventL( aEvent, aDestination );
       
  1291         
       
  1292         // part of emergency call handling when telephony+devicelock is active
       
  1293         // this solution is meant only for 3.1 and 3.2
       
  1294         // Emergency detector only handles key down events
       
  1295         if ( iEmergencySupportReady )
       
  1296         	iEcsDetector->HandleWsEventL( aEvent, aDestination);    	
       
  1297     }
       
  1298 
       
  1299 TBool CAutolockAppUi::DeviceLockQueryStatus()
       
  1300     {
       
  1301     return iDeviceLockQueryStatus;
       
  1302     }
       
  1303 
       
  1304 TBool CAutolockAppUi::DeviceLockStatus()
       
  1305     {
       
  1306     return iLocked;
       
  1307     }
       
  1308 
       
  1309 TBool CAutolockAppUi::DeviceFpsLock(TInt iStatus)
       
  1310     {
       
  1311 		if(iStatus)
       
  1312 			HandleCommandL(ESecUiCmdUnlock);
       
  1313 		else
       
  1314 			iModel->SetLockedL(ETimerLocked);
       
  1315     return ETrue;
       
  1316     }
       
  1317 void CAutolockAppUi::HandleWindowGroupListChange()
       
  1318     {
       
  1319     if ( !iLocked )
       
  1320         {
       
  1321         // System is not locked, make sure app is not on the foreground.
       
  1322         if ( !iCoeEnv->RootWin().OrdinalPosition() )
       
  1323             {
       
  1324             CAknSgcClient::MoveApp(
       
  1325                 iCoeEnv->RootWin().Identifier(), 
       
  1326                 ESgcMoveAppToBackground );
       
  1327             }
       
  1328         }
       
  1329     else
       
  1330         {
       
  1331 		            CAknView* view = View(KAutoLockViewId);
       
  1332 								  if(view)
       
  1333 								    {	
       
  1334 								    	TRect aCallRect;
       
  1335 								        STATIC_CAST(CAutolockView*, view)->HandleCall(10, aCallRect);
       
  1336 												if(aCallButtonRect.iBr.iX==0)
       
  1337    								    		aCallButtonRect = TRect (aCallRect);
       
  1338 								    }
       
  1339         // So now system is locked. When call is not ongoing, autolock should
       
  1340         // be on the foreground.
       
  1341         TInt callState = 0;
       
  1342         RProperty::Get( KPSUidCtsyCallInformation, KCTsyCallState, callState );
       
  1343         if ( callState == EPSCTsyCallStateNone || 
       
  1344              callState == EPSCTsyCallStateUninitialized )
       
  1345             {
       
  1346             // No calls ongoing.
       
  1347             if ( iCoeEnv->RootWin().OrdinalPosition() > 0 ) 
       
  1348                 {
       
  1349                 // Not on foreground
       
  1350                 CAknSgcClient::MoveApp(
       
  1351                     iCoeEnv->RootWin().Identifier(), 
       
  1352                     ESgcMoveAppToForeground );
       
  1353                 }
       
  1354             }
       
  1355         }
       
  1356     }
       
  1357 
       
  1358 TBool CAutolockAppUi::TarmState()
       
  1359 {
       
  1360 TBool ret(EFalse);
       
  1361 if(FeatureManager::FeatureSupported(KFeatureIdSapTerminalControlFw ))
       
  1362 {
       
  1363     // Get the TARM admin flag value
       
  1364     TInt tarmFlag;
       
  1365 	TInt tRet = RProperty::Get( KSCPSIDAutolock, SCP_TARM_ADMIN_FLAG_UID, tarmFlag );
       
  1366     
       
  1367     if ( tRet != KErrNone )
       
  1368         {
       
  1369             #if defined(_DEBUG)
       
  1370             RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::BringAppToForegroundL(): Warning: failed to get TARM Admin Flag state"));
       
  1371             #endif
       
  1372         }
       
  1373     else
       
  1374         {
       
  1375             #if defined(_DEBUG)
       
  1376             RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::BringAppToForegroundL(): TARM flag: %d"),
       
  1377                 tarmFlag );
       
  1378             #endif
       
  1379         }
       
  1380         
       
  1381     // Unset the admin flag if set
       
  1382     if ( tarmFlag & KSCPFlagAdminLock )
       
  1383         {
       
  1384         ret = ETrue;
       
  1385         TInt tRet = RProperty::Get( KSCPSIDAutolock, SCP_TARM_ADMIN_FLAG_UID, tarmFlag );
       
  1386         
       
  1387         if ( tRet == KErrNone )
       
  1388             {
       
  1389             tarmFlag &= ~KSCPFlagAdminLock;
       
  1390             tRet = RProperty::Set( KSCPSIDAutolock, SCP_TARM_ADMIN_FLAG_UID, tarmFlag );
       
  1391             }
       
  1392 
       
  1393         if ( tRet != KErrNone )
       
  1394             {
       
  1395             #if defined(_DEBUG)
       
  1396             RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::BringAppToForegroundL(): FAILED to unset TARM Admin Flag"));
       
  1397             #endif
       
  1398             }                    
       
  1399         }  
       
  1400 
       
  1401 }
       
  1402 return ret;
       
  1403 }
       
  1404 
       
  1405 // ---------------------------------------------------------
       
  1406 // CAutolockAppUi::HiddenReset()
       
  1407 // ---------------------------------------------------------
       
  1408 TBool CAutolockAppUi::HiddenReset()
       
  1409     {
       
  1410     #if defined(_DEBUG)
       
  1411 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HiddenReset() begin"));
       
  1412 	#endif	    				    			
       
  1413 
       
  1414     TBool ret_val ( EFalse );
       
  1415 	TInt startupReason(ENormalStartup);
       
  1416 	TInt errorCode(KErrNone);
       
  1417 	
       
  1418 	RProperty::Get(KPSUidStartup, KPSStartupReason, startupReason);
       
  1419 	
       
  1420 	if ( errorCode != KErrNone )
       
  1421 		{
       
  1422 		    #if defined(_DEBUG)
       
  1423 	        RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HiddenReset() error getting startup reason"));
       
  1424 	        #endif
       
  1425 	    }
       
  1426     ret_val = (startupReason != ENormalStartup);
       
  1427     #if defined(_DEBUG)
       
  1428 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::HiddenReset() END: %d"), ret_val);
       
  1429 	#endif
       
  1430     return ret_val;
       
  1431     }
       
  1432 void CAutolockAppUi::EnableWGListChangeEventListening()
       
  1433 {
       
  1434 	#if defined(_DEBUG)
       
  1435 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::EnableWGListChangeEventListening()"));
       
  1436 	#endif
       
  1437     RWindowGroup& rootWin = iCoeEnv->RootWin();
       
  1438     rootWin.EnableGroupListChangeEvents();
       
  1439     rootWin.EnableScreenChangeEvents();
       
  1440 }
       
  1441 void CAutolockAppUi::DisableWGListChangeEventListening()
       
  1442 {
       
  1443 	#if defined(_DEBUG)
       
  1444 	RDebug::Print(_L("(AUTOLOCK)CAutolockAppUi::DisableWGListChangeEventListening()"));
       
  1445 	#endif
       
  1446     RWindowGroup& rootWin = iCoeEnv->RootWin();
       
  1447     rootWin.DisableGroupListChangeEvents();
       
  1448     rootWin.DisableScreenChangeEvents();
       
  1449 }
       
  1450 // End of File