securitydialogs/Autolock/src/AutoKeyguardObserver.cpp
changeset 0 164170e6151a
child 15 318c4eab2439
child 36 2ca12c9f635b
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <aknappui.h>
       
    20 #include <aknkeylock.h>
       
    21 #include <AknUtils.h>
       
    22 #include <activitymanager.h>
       
    23 #include <AknNotifierController.h>
       
    24 #include <centralrepository.h> 
       
    25 #include <avkondomainpskeys.h>
       
    26 #include <e32property.h>
       
    27 #include <PSVariables.h>
       
    28 #include <startupdomainpskeys.h>
       
    29 #include <ctsydomainpskeys.h>
       
    30 #include <activeidle2domainpskeys.h>
       
    31 #include <coreapplicationuisdomainpskeys.h>
       
    32 #include <ScreensaverInternalPSKeys.h>
       
    33 #include <hwrmdomainpskeys.h>
       
    34 #include "AutoKeyguardCenRepI.h"
       
    35 #include "AutoKeyguardObserver.h"
       
    36 #include "AutolockPrivateCRKeys.h"
       
    37 
       
    38 const TInt AutoKeyguardOff(60000);
       
    39 // Screensaver "On" status value
       
    40 const TInt KSsOn = 1;
       
    41 // Screensaver started fron idle status value
       
    42 const TInt KSsStartedFromIdle = 1;
       
    43 //Flip open
       
    44 const TInt KFlipOpen = 1;
       
    45 
       
    46 // ================= MEMBER FUNCTIONS =======================
       
    47 //
       
    48 // ----------------------------------------------------------
       
    49 // CAutoKeyguardObserver::NewL()
       
    50 // ----------------------------------------------------------
       
    51 //
       
    52 
       
    53 CAutoKeyguardObserver* CAutoKeyguardObserver::NewL()
       
    54 	{
       
    55 	#ifdef RD_AUTO_KEYGUARD
       
    56 	#if defined(_DEBUG)
       
    57     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::NewL() BEGIN"));
       
    58     #endif
       
    59 	CAutoKeyguardObserver* self = new (ELeave) CAutoKeyguardObserver();
       
    60 	CleanupStack::PushL(self);
       
    61 	self->ConstructL(self);
       
    62 	CleanupStack::Pop(); //self
       
    63 	#if defined(_DEBUG)
       
    64     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::NewL() END"));
       
    65     #endif
       
    66 	return self;
       
    67 	#else
       
    68 	return NULL;
       
    69 	#endif //RD_AUTO_KEYGUARD
       
    70 	}
       
    71 //
       
    72 // ----------------------------------------------------------
       
    73 // CAutoKeyguardObserver::CAutoKeyguardObserver()
       
    74 // C++ default constructor
       
    75 // ----------------------------------------------------------
       
    76 // 
       
    77 CAutoKeyguardObserver::CAutoKeyguardObserver()	
       
    78 	{
       
    79 	}
       
    80 
       
    81 //
       
    82 // ----------------------------------------------------------
       
    83 // CAutoKeyguardObserver::ConstructL()
       
    84 // Symbian OS default constructor
       
    85 // ----------------------------------------------------------
       
    86 //
       
    87 void CAutoKeyguardObserver::ConstructL(CAutoKeyguardObserver* aObserver)
       
    88 	{
       
    89 	#ifdef RD_AUTO_KEYGUARD
       
    90 	#if defined(_DEBUG)
       
    91     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::ConstructL() BEGIN"));
       
    92     #endif	
       
    93 	//Central Repository handler
       
    94     iCenRepI = CAutoKeyguardCenRepI::NewL(aObserver);
       
    95 	// Activitymanager
       
    96 	iActivityManager = CUserActivityManager::NewL(CActive::EPriorityStandard);
       
    97 	StartActivityMonitoringL();
       
    98 	#if defined(_DEBUG)
       
    99     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::ConstructL() END"));
       
   100     #endif
       
   101     #else // !RD_AUTO_KEYGUARD
       
   102     iCenRepI = NULL;
       
   103 	iActivityManager = NULL;
       
   104     #endif //RD_AUTO_KEYGUARD
       
   105 	}
       
   106 //
       
   107 // ----------------------------------------------------------
       
   108 // CAutoKeyguardObserver::StartActivityMonitoringL()
       
   109 // Start monitoring user activity
       
   110 // ----------------------------------------------------------
       
   111 //
       
   112 void CAutoKeyguardObserver::StartActivityMonitoringL()
       
   113 	{
       
   114 	#ifdef RD_AUTO_KEYGUARD	
       
   115 	SetActivityManagerL();
       
   116 	#endif //RD_AUTO_KEYGUARD
       
   117 	}
       
   118 
       
   119 //
       
   120 // ----------------------------------------------------------
       
   121 // CAutoKeyguardObserver::StopActivityMonitoring()
       
   122 // Stop monitoring user activity
       
   123 // ----------------------------------------------------------
       
   124 //
       
   125 void CAutoKeyguardObserver::StopActivityMonitoring()
       
   126 	{
       
   127 	#ifdef RD_AUTO_KEYGUARD
       
   128 	CancelActivityManager();
       
   129 	#endif // RD_AUTO_KEYGUARD
       
   130 	}
       
   131 
       
   132 //
       
   133 // ----------------------------------------------------------
       
   134 // CAutoKeyguardObserver::SetActivityManagerL()
       
   135 // Initializes activymanager   
       
   136 // ----------------------------------------------------------
       
   137 //
       
   138 void CAutoKeyguardObserver::SetActivityManagerL()
       
   139 	{
       
   140 	#ifdef RD_AUTO_KEYGUARD
       
   141 	#if defined(_DEBUG)
       
   142     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::SetActivityManagerL() BEGIN"));
       
   143     #endif
       
   144 	if (AutoKeyguardTimeout() )
       
   145 		{
       
   146 		#if defined(_DEBUG)
       
   147     	RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::SetActivityManagerL() ON: Start manager"));
       
   148     	#endif
       
   149 		iActivityManager->Start(AutoKeyguardTimeout(), TCallBack(HandleInactiveEventL,this), 
       
   150 								TCallBack(HandleActiveEventL,this));
       
   151 		}
       
   152 	else
       
   153 		{
       
   154 		#if defined(_DEBUG)
       
   155     	RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::SetActivityManagerL() OFF: Start manager"));
       
   156     	#endif
       
   157 		iActivityManager->Start(AutoKeyguardOff, TCallBack(HandleInactiveEventL,this), 
       
   158 								TCallBack(HandleActiveEventL,this));
       
   159 		}
       
   160 
       
   161 	#if defined(_DEBUG)
       
   162     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::SetActivityManagerL() END"));
       
   163     #endif
       
   164     #endif //RD_AUTO_KEYGUARD
       
   165 	}
       
   166 //
       
   167 // ----------------------------------------------------------
       
   168 // CAutoKeyguardObserver::CancelActivityManager()
       
   169 // UnInitializes activymanager   
       
   170 // ----------------------------------------------------------
       
   171 //
       
   172 void CAutoKeyguardObserver::CancelActivityManager()
       
   173 	{
       
   174 	#ifdef RD_AUTO_KEYGUARD
       
   175     if ( iActivityManager )
       
   176         {
       
   177 	    iActivityManager->Cancel();
       
   178         }
       
   179     #endif //RD_AUTO_KEYGUARD
       
   180 	}
       
   181 //
       
   182 // ----------------------------------------------------------
       
   183 // CAutoKeyguardObserver::~CAutoKeyguardObserver()
       
   184 // Destructor   
       
   185 // ----------------------------------------------------------
       
   186 //
       
   187 CAutoKeyguardObserver::~CAutoKeyguardObserver()
       
   188 	{
       
   189 	#ifdef RD_AUTO_KEYGUARD
       
   190 	if(iCenRepI)
       
   191     	delete iCenRepI;
       
   192 	if(iActivityManager)
       
   193 		{
       
   194 		StopActivityMonitoring();
       
   195 		delete iActivityManager;
       
   196 		iActivityManager = NULL;
       
   197 		}
       
   198 	#endif //RD_AUTO_KEYGUARD
       
   199 	}
       
   200 //
       
   201 // ----------------------------------------------------------
       
   202 // CAutoKeyguardObserver::AutoKeyguardTimeout()
       
   203 // Returns current AutoKeyguard period
       
   204 // ----------------------------------------------------------
       
   205 //
       
   206 TInt CAutoKeyguardObserver::AutoKeyguardTimeout()
       
   207 	{
       
   208 	#ifdef RD_AUTO_KEYGUARD
       
   209     return iCenRepI->Timeout();
       
   210     #else
       
   211     return 0;
       
   212     #endif //RD_AUTO_KEYGUARD
       
   213 	}
       
   214 //
       
   215 // ----------------------------------------------------------
       
   216 // CAutoKeyguardObserver::ResetInactivityTimeoutL()
       
   217 // Gets AutoKeyguard period and starts monitoring user activity 
       
   218 // ----------------------------------------------------------
       
   219 //
       
   220 void CAutoKeyguardObserver::ResetInactivityTimeout()
       
   221 	{
       
   222 	#ifdef RD_AUTO_KEYGUARD	
       
   223 	if (AutoKeyguardTimeout() )
       
   224 		{
       
   225 		#if defined(_DEBUG)
       
   226     	RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::ResetInactivityTimeoutL() ON"));
       
   227     	#endif
       
   228 		iActivityManager->SetInactivityTimeout(AutoKeyguardTimeout());
       
   229 		}
       
   230 	else
       
   231 		{
       
   232 		#if defined(_DEBUG)
       
   233     	RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::ResetInactivityTimeoutL() OFF"));
       
   234     	#endif
       
   235 		iActivityManager->SetInactivityTimeout(AutoKeyguardOff);
       
   236 		}
       
   237 	#endif //RD_AUTO_KEYGUARD
       
   238 	}
       
   239 //
       
   240 // ----------------------------------------------------------
       
   241 // CAutoKeyguardObserver::HandleActiveEventL()
       
   242 // Handles Active event. Called by ActivityManager
       
   243 // ----------------------------------------------------------
       
   244 //
       
   245 TInt CAutoKeyguardObserver::HandleActiveEventL(TAny* /*aPtr*/)
       
   246 	{
       
   247 	return KErrNone;
       
   248 	}
       
   249 
       
   250 //
       
   251 // ----------------------------------------------------------
       
   252 // CAutoKeyguardObserver::HandleInactiveEventL()
       
   253 // Handles InActive event. Called by ActivityManager
       
   254 // ----------------------------------------------------------
       
   255 //
       
   256 TInt CAutoKeyguardObserver::HandleInactiveEventL(TAny* aPtr)
       
   257 	{
       
   258 	#ifdef RD_AUTO_KEYGUARD
       
   259 	#if defined(_DEBUG)
       
   260     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::HandleInactiveEventL() BEGIN"));
       
   261     #endif
       
   262 	if ( STATIC_CAST(CAutoKeyguardObserver*, aPtr)->AutoKeyguardTimeout() )
       
   263 		{
       
   264 		#if defined(_DEBUG)
       
   265     	RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::HandleInactiveEventL() Lock"));
       
   266     	#endif
       
   267 		STATIC_CAST(CAutoKeyguardObserver*, aPtr)->LockKeysL();
       
   268 		}
       
   269 	#if defined(_DEBUG)
       
   270     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::HandleInactiveEventL() END"));
       
   271     #endif
       
   272     #endif //RD_AUTO_KEYGUARD
       
   273 	return KErrNone;
       
   274 	}
       
   275 
       
   276 //
       
   277 // ----------------------------------------------------------
       
   278 // CAutoKeyguardObserver::LockKeysL()
       
   279 // Locks system
       
   280 // ----------------------------------------------------------
       
   281 //
       
   282 void CAutoKeyguardObserver::LockKeysL()
       
   283 	{
       
   284 	#ifdef RD_AUTO_KEYGUARD
       
   285 	#if defined(_DEBUG)
       
   286     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() BEGIN"));
       
   287     #endif
       
   288     CRepository* repository = CRepository::NewL(KCRUidAutolockConf);
       
   289     TInt keyguardConf(0);
       
   290     //Check local keyguard variation key 
       
   291     repository->Get(KAutoKeyLockConf, keyguardConf);
       
   292     delete repository;
       
   293     if(keyguardConf & KAutoKeylockFeatureIdFlipOpenDisabled)
       
   294     {
       
   295     	#if defined(_DEBUG)
       
   296     	RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() Flip variation"));
       
   297     	#endif
       
   298     	TInt flipState(KFlipOpen);
       
   299     	RProperty::Get(KPSUidHWRM, KHWRMFlipStatus, flipState);
       
   300 	TInt lightStatus=EForcedLightsUninitialized; 
       
   301 	RProperty::Get(KPSUidCoreApplicationUIs,KLightsVTForcedLightsOn,lightStatus ); 
       
   302 
       
   303 	//If flip is open and feature flag is on. don't lock
       
   304 	if(flipState == KFlipOpen || lightStatus == EForcedLightsOn)
       
   305     	{
       
   306     		#if defined(_DEBUG)
       
   307     		RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() Flip open"));
       
   308     		#endif
       
   309     		return;
       
   310     	}
       
   311     		
       
   312     }
       
   313     
       
   314 	{	// REQ 414-5466 Prevention of device lock in context of Hands Free Voice UI
       
   315 	TInt vuiValue = 0;
       
   316 	TUid KHFVuiModePSUid = { 0x102818E7 };
       
   317 	enum THFVuiModePSKeys
       
   318 	    {
       
   319 	    EHFVuiPSModeId = 1000
       
   320 	    };
       
   321 	TInt tRet = RProperty::Get(KHFVuiModePSUid, EHFVuiPSModeId, vuiValue);	// also 0 if can't get because permissions or because doesn't exists
       
   322 	#if defined(_DEBUG)
       
   323 		RDebug::Printf( "%s %s (%u) gatting KHFVuiModePSUid+EHFVuiPSModeId=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, tRet );
       
   324 		RDebug::Printf( "%s %s (%u) vuiValue=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, vuiValue );
       
   325 	#endif
       
   326 	if(vuiValue)
       
   327 		{
       
   328 		#if defined(_DEBUG)
       
   329 		RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() Voice functions active. No locking possible."));
       
   330 		#endif
       
   331 		return;
       
   332 		}
       
   333 	}
       
   334 
       
   335 	TInt value = 0;
       
   336 	TBool keylockOn = EFalse;
       
   337 	TBool autolockOn = EFalse;
       
   338 	TBool idle = EFalse;
       
   339 	TInt callState = EPSCTsyCallStateNone;
       
   340 	TBool okToLock = EFalse;
       
   341 	TBool screenSaverOn = EFalse;
       
   342 	TBool screenSaverStertedFromIdle = EFalse;
       
   343         TBool startupOver = EFalse;
       
   344     //Get keyguard status
       
   345     RProperty::Get(KPSUidAvkonDomain, KAknKeyguardStatus, value);
       
   346     keylockOn = (value == EKeyguardLocked);
       
   347     //Get call state
       
   348     RProperty::Get(KPSUidCtsyCallInformation, KCTsyCallState, callState);
       
   349     
       
   350     //See whether we are in idle
       
   351  		value = 0;   
       
   352     RProperty::Get(KPSUidAiInformation, KActiveIdleState, value);
       
   353     idle = (value == EPSAiForeground);
       
   354     idle = ETrue; // don't care about idle state. Phone should autolock on any UI, not only HomeSreeen.
       
   355     
       
   356     value = 0;
       
   357     RProperty::Get(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, value);
       
   358     autolockOn = (value > EAutolockOff);
       
   359 	
       
   360 		value = 0;
       
   361 	  RProperty::Get(KPSUidScreenSaver, KScreenSaverOn, value);
       
   362     screenSaverOn = (value == KSsOn);
       
   363     
       
   364     value = 0;
       
   365     RProperty::Get(KPSUidScreenSaver, KScreenSaverActivatedFromIdle, value);
       
   366     screenSaverStertedFromIdle = (value == KSsStartedFromIdle);
       
   367     
       
   368     //See if all the startup related queries and graphics has been displayed
       
   369     value = 0;
       
   370     RProperty::Get(KPSUidStartup, KPSStartupUiPhase, value);
       
   371     startupOver = (value == EStartupUiPhaseAllDone);
       
   372         
       
   373     #if defined(_DEBUG)
       
   374     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() startupOver: %d"), startupOver);
       
   375     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() Startup state: %d"), value);
       
   376     #endif
       
   377     if (startupOver)
       
   378         {
       
   379         // If keylock is already ON, there is a ongoing call, 
       
   380         // autolock is already ON or phone is not in idle, don't lock.
       
   381         if (keylockOn || (callState > EPSCTsyCallStateNone) || autolockOn || !idle)
       
   382             {
       
   383         	    #if defined(_DEBUG)
       
   384     		    RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() Don't lock"));
       
   385     		    RDebug::Print(_L("(AUTOLOCK)keylockstatus: %d"), keylockOn);
       
   386     		    RDebug::Print(_L("(AUTOLOCK)callstate: %d"), callState);
       
   387     		    RDebug::Print(_L("(AUTOLOCK)autolockstate: %d"), autolockOn);
       
   388     		    RDebug::Print(_L("(AUTOLOCK)idlestate: %d"), idle);
       
   389     		    RDebug::Print(_L("(AUTOLOCK)screenSaverOn: %d"), screenSaverOn);
       
   390     		    RDebug::Print(_L("(AUTOLOCK)screenSaverStertedFromIdle: %d"), screenSaverStertedFromIdle);
       
   391     		    #endif
       
   392     		    //If screensaver is on, idle does not have foreground. If Screensaver was started from Idle, lock keys.
       
   393     		    if(!idle && screenSaverOn && screenSaverStertedFromIdle && (callState <= EPSCTsyCallStateNone))
       
   394     			    okToLock = ETrue;
       
   395     		    else
       
   396     		        okToLock = EFalse;
       
   397             }
       
   398          else //Otherwise there's no problem...
       
   399          		okToLock = ETrue;
       
   400         }
       
   401     
       
   402     
       
   403     if (okToLock)
       
   404         {
       
   405             #if defined(_DEBUG)
       
   406             RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() Lock"));
       
   407             RDebug::Print(_L("(AUTOLOCK)keylockstatus: %d"), keylockOn);
       
   408             RDebug::Print(_L("(AUTOLOCK)callstate: %d"), callState);
       
   409             RDebug::Print(_L("(AUTOLOCK)autolockstate: %d"), autolockOn);
       
   410             RDebug::Print(_L("(AUTOLOCK)idlestate: %d"), idle);
       
   411             RDebug::Print(_L("(AUTOLOCK)screenSaverOn: %d"), screenSaverOn);
       
   412             RDebug::Print(_L("(AUTOLOCK)screenSaverStertedFromIdle: %d"), screenSaverStertedFromIdle);
       
   413             #endif
       
   414 		//Tell keylock to lock itself.
       
   415 		RAknKeyLock keylock;
       
   416         if ( keylock.Connect() == KErrNone )
       
   417           {
       
   418           	#if defined(_DEBUG)
       
   419     		RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() Connect OK"));
       
   420     		#endif
       
   421     			 if(AknLayoutUtils::PenEnabled())
       
   422     			     keylock.EnableWithoutNote(); //Enable without note
       
   423     			 else
       
   424     			     keylock.EnableKeyLock();
       
   425            keylock.Close();
       
   426           }
       
   427 		}
       
   428 	#if defined(_DEBUG)
       
   429     RDebug::Print(_L("(AUTOLOCK)CAutoKeyguardObserver::LockKeysL() END"));
       
   430     #endif
       
   431     #endif //RD_AUTO_KEYGUARD
       
   432 } 
       
   433 	
       
   434 
       
   435 // END OF FILE