securitydialogs/Autolock/src/Autolock.cpp
changeset 61 1cc4c46c2963
parent 56 25a3fbb5e4d3
--- a/securitydialogs/Autolock/src/Autolock.cpp	Fri Sep 17 08:35:02 2010 +0300
+++ b/securitydialogs/Autolock/src/Autolock.cpp	Mon Oct 04 01:41:57 2010 +0300
@@ -61,6 +61,8 @@
 #include <hwrmdomainpskeys.h>
 #include <ctsydomainpskeys.h>
 
+#include <startupdomainpskeys.h>
+
 #include <hbdevicedialog.h>
 // handled now directly but screensaver
 // #include <power_save_display_mode.h>
@@ -68,7 +70,8 @@
 const TInt KPhoneIndex(0);
 const TInt KTriesToConnectServer(2);
 const TInt KTimeBeforeRetryingServerConnection(50000);
-#define ESecUiTypeLock                  0x00100000
+#define ESecUiTypeDeviceLock        0x00100000
+#define ESecUiTypeKeyguard          0x00200000
 _LIT( KMmTsyModuleName, "PhoneTsy");
 
 // Constant definitions to control screensaver view types
@@ -85,7 +88,18 @@
 static const char *KSnsrCmdSwitchLowPower = "switch_low_power";
 
 Autolock::Autolock(QWidget *parent, Qt::WFlags f) :
-    QWidget(parent, f), mService(NULL)
+    QWidget(parent, f),
+    mService(NULL),
+    mPowerKeyCaptureHandle(0),
+    mApplicationKeyCaptureHandle(0),
+    mApplicationLongKeyCaptureHandle(0),
+    mEKeyDeviceFCaptureHandle(0),
+    mEKeyBellCaptureHandle(0),
+    mEKeyYesCaptureHandle(0),
+    mEKeyNoCaptureHandle(0),
+    iLockCodeQueryInDisplay(false),
+    mScreensaverModeTimer(0)
+    
     {
     RDEBUG("start autolock", 0);
 
@@ -171,6 +185,7 @@
     TInt cRresult = 0;
     iLockCodeQueryInDisplay = EFalse;
     Q_UNUSED(cRresult);
+    TInt err = 0;
 
 		iProcessingEvent = -1;
     iLockStatusPrev = ELockNotActive;
@@ -198,31 +213,45 @@
 
     // subscribe to settings changes
     subscriberKSettingsAutolockStatus = new QValueSpaceSubscriber("/KCRUidSecuritySettings/KSettingsAutolockStatus", this);
-    connect(subscriberKSettingsAutolockStatus, SIGNAL(contentsChanged()), this, SLOT(subscriberKSettingsAutolockStatusChanged()));
+    err = connect(subscriberKSettingsAutolockStatus, SIGNAL(contentsChanged()), this, SLOT(subscriberKSettingsAutolockStatusChanged()));
+    RDEBUG("err", err);
     subscriberKSettingsAutoLockTime = new QValueSpaceSubscriber("/KCRUidSecuritySettings/KSettingsAutoLockTime", this);
-    connect(subscriberKSettingsAutoLockTime, SIGNAL(contentsChanged()), this, SLOT(subscriberKSettingsAutoLockTimeChanged()));
+    err = connect(subscriberKSettingsAutoLockTime, SIGNAL(contentsChanged()), this, SLOT(subscriberKSettingsAutoLockTimeChanged()));
+    RDEBUG("err", err);
     subscriberKSettingsAutomaticKeyguardTime = new QValueSpaceSubscriber("/KCRUidSecuritySettings/KSettingsAutomaticKeyguardTime", this);
-    connect(subscriberKSettingsAutomaticKeyguardTime, SIGNAL(contentsChanged()), this, SLOT(subscriberKSettingsAutomaticKeyguardTimeChanged()));
+    err = connect(subscriberKSettingsAutomaticKeyguardTime, SIGNAL(contentsChanged()), this, SLOT(subscriberKSettingsAutomaticKeyguardTimeChanged()));
+    RDEBUG("err", err);
     subscriberKDisplayLightsTimeout = new QValueSpaceSubscriber("/KCRUidLightSettings/KDisplayLightsTimeout", this);
-    connect(subscriberKDisplayLightsTimeout, SIGNAL(contentsChanged()), this, SLOT(subscriberKDisplayLightsTimeoutChanged()));
+    err = connect(subscriberKDisplayLightsTimeout, SIGNAL(contentsChanged()), this, SLOT(subscriberKDisplayLightsTimeoutChanged()));
+    RDEBUG("err", err);
     subscriberKProEngActiveProfile = new QValueSpaceSubscriber("/KCRUidProfileEngine/KProEngActiveProfile", this);
-    connect(subscriberKProEngActiveProfile, SIGNAL(contentsChanged()), this, SLOT(subscriberKProEngActiveProfileChanged()));
+    err = connect(subscriberKProEngActiveProfile, SIGNAL(contentsChanged()), this, SLOT(subscriberKProEngActiveProfileChanged()));
+    RDEBUG("err", err);
 
     subscriberKSecurityUIsDismissDialog = new QValueSpaceSubscriber("/KPSUidSecurityUIs/KSecurityUIsDismissDialog", this);
-    connect(subscriberKSecurityUIsDismissDialog, SIGNAL(contentsChanged()), this, SLOT(subscriberKSecurityUIsDismissDialogChanged()));
+    err = connect(subscriberKSecurityUIsDismissDialog, SIGNAL(contentsChanged()), this, SLOT(subscriberKSecurityUIsDismissDialogChanged()));
+    RDEBUG("err", err);
 
     // subscribe to environment changes
-    subscriberKHWRMGripStatus = new QValueSpaceSubscriber("/KPSUidHWRM/KHWRMGripStatus");
-    connect(subscriberKHWRMGripStatus, SIGNAL(contentsChanged()), this, SLOT(subscriberKHWRMGripStatusChanged()));
+    subscriberKHWRMGripStatus = new QValueSpaceSubscriber("/KPSUidHWRM/KHWRMGripStatus", this);
+    err = connect(subscriberKHWRMGripStatus, SIGNAL(contentsChanged()), this, SLOT(subscriberKHWRMGripStatusChanged()));
+    RDEBUG("err", err);
 
-    subscriberKAknKeyguardStatus = new QValueSpaceSubscriber("/KPSUidAvkonDomain/KAknKeyguardStatus");
-    connect(subscriberKAknKeyguardStatus, SIGNAL(contentsChanged()), this, SLOT(subscriberKAknKeyguardStatusChanged()));
+    subscriberKAknKeyguardStatus = new QValueSpaceSubscriber("/KPSUidAvkonDomain/KAknKeyguardStatus", this);
+    err = connect(subscriberKAknKeyguardStatus, SIGNAL(contentsChanged()), this, SLOT(subscriberKAknKeyguardStatusChanged()));
+    RDEBUG("err", err);
 
-    subscriberKCoreAppUIsAutolockStatus = new QValueSpaceSubscriber("/KPSUidCoreApplicationUIs/KCoreAppUIsAutolockStatus");
-    connect(subscriberKCoreAppUIsAutolockStatus, SIGNAL(contentsChanged()), this, SLOT(subscriberKCoreAppUIsAutolockStatusChanged()));
+    subscriberKCoreAppUIsAutolockStatus = new QValueSpaceSubscriber("/KPSUidCoreApplicationUIs/KCoreAppUIsAutolockStatus", this);
+    err = connect(subscriberKCoreAppUIsAutolockStatus, SIGNAL(contentsChanged()), this, SLOT(subscriberKCoreAppUIsAutolockStatusChanged()));
+    RDEBUG("err", err);
 
-    subscriberKCTsyCallState = new QValueSpaceSubscriber("/KPSUidCtsyCallInformation/KCTsyCallState");
-    connect(subscriberKCTsyCallState, SIGNAL(contentsChanged()), this, SLOT(subscriberKCTsyCallStateChanged()));
+    subscriberKCTsyCallState = new QValueSpaceSubscriber("/KPSUidCtsyCallInformation/KCTsyCallState", this);
+    err = connect(subscriberKCTsyCallState, SIGNAL(contentsChanged()), this, SLOT(subscriberKCTsyCallStateChanged()));
+    RDEBUG("err", err);
+
+    subscriberKSecurityUIsTestCode = new QValueSpaceSubscriber("/KPSUidSecurityUIs/KSecurityUIsTestCode", this);
+    err = connect(subscriberKSecurityUIsTestCode, SIGNAL(contentsChanged()), this, SLOT(subscriberKSecurityUIsTestCodeChanged()));
+    RDEBUG("err", err);
 
     // inactivity
     connect(serviceKeyguard, SIGNAL(active()), this, SLOT(activeKeyguard()));
@@ -265,6 +294,7 @@
     // mScreensaverPowerSave = CPowerSaveDisplayMode::NewL();
     // mScreensaverPowerSavePixelBuffer = HBufC16::NewL(360 * 640);
     // mScreensaverPowerSavePixelBuffer->Des().Fill(0);
+    RDEBUG("99", 0x99);
     }
 
 Autolock::~Autolock()
@@ -274,7 +304,7 @@
     // handled now directly but screensaver
     // delete mScreensaverPowerSave;
     // delete mScreensaverPowerSavePixelBuffer;
-    RDEBUG("1", 1);
+    RDEBUG("99", 0x99);
     }
 
 void Autolock::adjustInactivityTimers(int aReason)
@@ -324,6 +354,7 @@
         }
 
     delete repository;
+    RDEBUG("99", 0x99);
     }
 void Autolock::quit()
     {
@@ -337,10 +368,18 @@
     RDEBUG("aMode", aMode);
     RDEBUG("aReason", aReason);
     RDEBUG("aCaller", aCaller);
-    TInt err = RProperty::Set(KPSUidSecurityUIs, KSecurityUIsLights, aMode);
+    TInt err = 0;
+    if(aMode!=ESecurityUIsLightsLockOffRequest)
+    	{
+    	RDEBUG("KLightsSSForcedLightsOn", KLightsSSForcedLightsOn);
+    	err = RProperty::Set(KPSUidCoreApplicationUIs, KLightsSSForcedLightsOn, 30);
+    	}
+    RDEBUG("err", err);
+    err = RProperty::Set(KPSUidSecurityUIs, KSecurityUIsLights, aMode);
     RDEBUG("KSecurityUIsLights err", err);
     return err;
   	}
+
 int Autolock::AskValidSecCode(int aReason)
     {
     RDEBUG("aReason", aReason);
@@ -583,6 +622,10 @@
             RDEBUG("KErrInUse", aReason)
             ;
             break;
+        case KErrLocked: // never happens
+            RDEBUG("KErrLocked", aReason)
+            ;
+            break;
         default:
             RDEBUG("default", aReason)
             ;
@@ -598,6 +641,23 @@
     TInt err = RProperty::Get(KPSUidSecurityUIs, KSecurityUIsSecUIOriginatedQuery, secUiOriginatedQuery);
     RDEBUG("err", err);
     RDEBUG("secUiOriginatedQuery", secUiOriginatedQuery);
+
+    TInt startupValue(EStartupUiPhaseUninitialized);
+		err = RProperty::Get(KPSUidStartup, KPSStartupUiPhase, startupValue);
+    RDEBUG("err", err);
+    RDEBUG("startupValue", startupValue);
+
+    TInt simStatusValue(0);
+		err = RProperty::Get(KPSUidStartup, KPSSimStatus, simStatusValue);
+    RDEBUG("err", err);
+    RDEBUG("simStatusValue", simStatusValue);
+    if(startupValue<EStartupUiPhaseAllDone && aReason!=ELockAppDisableKeyguard && aReason!=ELockAppDisableDevicelock )
+    	{
+    	// before bootup, it can't be manually nor automatically keyguarded/locked
+    	RDEBUG("before bootup, it can't be manually nor automatically keyguarded/locked KErrInUse", KErrInUse);
+    	return KErrInUse;
+    	}
+
     switch (aReason)
         {
         case ELockAppEnableKeyguard:
@@ -747,6 +807,12 @@
     QT_TRAP_THROWING(repositoryKeyguard = CRepository::NewL(KCRUidCoreApplicationUIsSysAp));
     TInt cRresult = KErrNone;
     Q_UNUSED(cRresult);
+
+    TInt autolockState;
+    err = RProperty::Get(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, autolockState);
+    RDEBUG("Get KCoreAppUIsAutolockStatus", err);
+    RDEBUG("autolockState", autolockState);
+
     if (1 == 1) // this is a quick way to disable this functionality, for testing
         {
         RDEBUG("publishing", aReason);
@@ -754,8 +820,15 @@
             {
             err = RProperty::Set(KPSUidAvkonDomain, KAknKeyguardStatus, EKeyguardNotActive);
             RDEBUG("err", err);
-            err = RProperty::Set(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, EAutolockOff);
-            RDEBUG("err", err);
+				    if(autolockState!=EAutolockOff)
+				    	{
+            	err = RProperty::Set(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, EAutolockOff);
+            	RDEBUG("err", err);
+            	}
+            else
+            	{
+            	RDEBUG("not set KCoreAppUIsAutolockStatus because already ", EAutolockOff);
+            	}
             TurnLights(ESecurityUIsLightsLockOnRequest, aReason, 0x14);
 
             // cRresult = repositoryDevicelock->Set(KSettingsAutolockStatus, 0);    // the settings remains. Only ISA changes, as well as the P&S
@@ -768,8 +841,15 @@
                 {
                 err = RProperty::Set(KPSUidAvkonDomain, KAknKeyguardStatus, EKeyguardLocked);
                 RDEBUG("KAknKeyguardStatus err", err);
-                err = RProperty::Set(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, EAutolockOff);
-                RDEBUG("KCoreAppUIsAutolockStatus err", err);
+                if(autolockState!=EAutolockOff)
+                	{
+	                err = RProperty::Set(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, EAutolockOff);
+  	              RDEBUG("KCoreAppUIsAutolockStatus err", err);
+  	            	}
+		            else
+		            	{
+		            	RDEBUG("not set KCoreAppUIsAutolockStatus because already ", EAutolockOff);
+		            	}
                 TurnLights(ESecurityUIsLightsLockOffRequest, aReason, 0x16);	// same for keyguard and devicelock
                 // cRresult = repositoryDevicelock->Set(KSettingsAutolockStatus, 0);
                 cRresult = repositoryKeyguard->Set(KSysApKeyguardActive, 1);
@@ -779,8 +859,15 @@
                 {
                 err = RProperty::Set(KPSUidAvkonDomain, KAknKeyguardStatus, EKeyguardLocked); // previously EKeyguardAutolockEmulation, but then Telephone doesn't understand that it's keyguarded. Other candidates might be: EKeyguardLocked and EKeyguardNotActive
                 RDEBUG("KAknKeyguardStatus err", err);
-                err = RProperty::Set(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, EManualLocked);
-                RDEBUG("KCoreAppUIsAutolockStatus err", err);
+                if(autolockState!=EManualLocked)
+                	{
+	                err = RProperty::Set(KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, EManualLocked);
+  	              RDEBUG("KCoreAppUIsAutolockStatus err", err);
+  	            	}
+		            else
+		            	{
+		            	RDEBUG("not set KCoreAppUIsAutolockStatus because already ", EManualLocked);
+		            	}
                 TurnLights(ESecurityUIsLightsQueryOnRequest, aReason, 0x18);
                 // cRresult = repositoryDevicelock->Set(KSettingsAutolockStatus, 1);
                 cRresult = repositoryKeyguard->Set(KSysApKeyguardActive, 0); // keyguard disabled, so that user can type and DeviceF can be captured
@@ -857,12 +944,14 @@
                 setLabelIcon(EKeyguardActive);
                 updateIndicator(EKeyguardActive);
                 publishStatus(EKeyguardActive);
-                setLockDialog(aReason, 1);
+                setLockDialog(aReason, EDeviceDialogCreated);
                 }
             // Telephone might want to re-enable only-keyguard after the call ends. Allow it. This means re-showing the screensaver, not more.
-						if (errorInProcess == KErrPermissionDenied && iTempDisableOnlyKeyguardBecauseIncomingCall==1)
+            // Note: this is already done in CallStateChanged, so it's useless. Therefore it expects KErrLocked, which never happens
+						if (errorInProcess == KErrLocked && iTempDisableOnlyKeyguardBecauseIncomingCall==1)
                {
-               setLockDialog(aReason, 1);
+							 RDEBUG("allowing telephone to re-enable keyguard . Call setLockDialog EDeviceDialogScreenSaverReDisplay", EDeviceDialogScreenSaverReDisplay);
+               setLockDialog(aReason, EDeviceDialogScreenSaverReDisplay);
                iTempDisableOnlyKeyguardBecauseIncomingCall=0;
              	 }
             }
@@ -877,21 +966,22 @@
                 setLabelIcon(ELockNotActive);
                 updateIndicator(ELockNotActive);
                 publishStatus(ELockNotActive);
-                setLockDialog(aReason, 0);
+                setLockDialog(aReason, EDeviceDialogDestroyed);
                 }
             }
             RDEBUG("errorInProcess", errorInProcess);
-            // Telephone might want to disnable only-keyguard during a call. Allow it. This means hiding the screensaver, not more.
-            if(errorInProcess==KErrPermissionDenied)
+            // Telephone might want to disable only-keyguard during a call. Allow it. This means hiding the screensaver, not more.
+            // Note: this is already done in CallStateChanged, so it's useless. Therefore it expects KErrLocked, which never happens
+            if(errorInProcess==KErrLocked)
             	{
-					    TInt aCallStatus;
+					    TInt aCallStatus=-1;
 					    TInt err = RProperty::Get(KPSUidCtsyCallInformation, KCTsyCallState, aCallStatus);
 					    RDEBUG("err", err);
 					    RDEBUG("aCallStatus", aCallStatus);
 							if (aCallStatus != EPSCTsyCallStateUninitialized && aCallStatus != EPSCTsyCallStateNone)
 								{
-								RDEBUG("allowing telephone to disable keyguard . Call setLockDialog 0", 0);
-								setLockDialog(aReason, 0);
+								RDEBUG("allowing telephone to disable keyguard . Call setLockDialog EDeviceDialogScreenSaverHidden", EDeviceDialogScreenSaverHidden);
+								setLockDialog(aReason, EDeviceDialogScreenSaverHidden);
 								iTempDisableOnlyKeyguardBecauseIncomingCall=1;
 								errorInProcess=KErrNone;
 								}
@@ -912,7 +1002,7 @@
                 updateIndicator( EDevicelockActive);
                 publishStatus(EDevicelockActive);
                 setLabelIcon(EDevicelockActive);
-                setLockDialog(aReason, 1);
+                setLockDialog(aReason, EDeviceDialogCreated);
                 }
             // aParam1 is aReason : EDevicelockManual, EDevicelockRemote
             // this never shows a note
@@ -932,38 +1022,52 @@
                 {
                 RDEBUG("skip setLockDialog", 0);
                 // No need to disable screensaver. Notes now can get on top. Also, this avoids the transition Screensaver->unlockQuery, which produces flicker on HomeScreen.
-                // setLockDialog(aReason, 0); // hide temporarilly because HbDeviceMessageBox doesn't get in top of the Lock-icon. Thus, dismiss it.
+                setLockDialog(aReason, EDeviceDialogScreenSaverHidden); // hide temporarilly because HbDeviceMessageBox doesn't get in top of the Lock-icon. Thus, dismiss it.
                 // do I need to enable touch? it seems to work without it
                 // in any case, lights are needed
                 TurnLights(ESecurityUIsLightsQueryOnRequest, aReason, 0x20);
-                RDEBUG("calling HbDeviceMessageBox::question", 0);
                 bool value = true;
                 // not sure whether this question is really needed. The UI doesn't say anything, so I remove it for now.
-                // HbDeviceMessageBox::question("Disable Lock?");   // this doesn't block other events, so after return everything might be messed up.
+                RDEBUG("not calling HbDeviceMessageBox::question", 0);
+                // value = HbDeviceMessageBox::question("Disable Lock?");   // this doesn't block other events, so after return everything might be messed up.
                 RDEBUG("value", value);
                 if (!value)
                     errorInProcess = KErrCancel;
                 }
             if (errorInProcess == KErrNone)
                 {
-                RDEBUG("skip setLockDialog", 1);
+                RDEBUG("skip setLockDialog", 0);
                 // No need to disable screensaver. Notes now can get on top. Also, this avoids the transition Screensaver->unlockQuery, which produces flicker on HomeScreen.
-                // setLockDialog(aReason, 0); // hide temporarilly because AskValidSecCode doesn't get in top of the Lock-icon. Thus, dismiss it.
+                setLockDialog(aReason, EDeviceDialogScreenSaverHidden); // hide temporarilly because AskValidSecCode doesn't get in top of the Lock-icon. Thus, dismiss it.
                 RDEBUG("calling AskValidSecCode", 0);
                 errorInProcess = AskValidSecCode(ELockAppDisableDevicelock);
                 RDEBUG("errorInProcess", errorInProcess);
                 }
             if (errorInProcess == KErrNone)
                 {
+                // code is correct. Time to destroy the lock-icon
                 setLabelIcon( ELockNotActive);
                 updateIndicator(ELockNotActive);
-                setLockDialog(aReason, 0);
+                setLockDialog(aReason, EDeviceDialogDestroyed);
                 publishStatus(ELockNotActive);
                 }
             if (errorInProcess != KErrNone)
                 { // re-lock. For example, if password is cancelled
+						    TInt aCallStatus=-1;
+						    TInt skipScreenSaver=0;
+						    TInt err = RProperty::Get(KPSUidCtsyCallInformation, KCTsyCallState, aCallStatus);
+						    RDEBUG("err", err);
+						    RDEBUG("aCallStatus", aCallStatus);
+								if (aCallStatus != EPSCTsyCallStateUninitialized && aCallStatus != EPSCTsyCallStateNone)
+									{
+									// if there's an active call and the query is cancelled, screensaver should not show.
+									skipScreenSaver=1;	// if it's skipped, it will be set at CallStateChanged
+									RDEBUG("new skipScreenSaver", skipScreenSaver);
+									}
+								RDEBUG("skipScreenSaver", skipScreenSaver);
                 if (iLockStatus >= EDevicelockActive) // this skips the case "unlocking although it wasn't locked"
-                    setLockDialog(ELockAppEnableDevicelock, 1);
+                    if(!skipScreenSaver)
+                    	setLockDialog(ELockAppEnableDevicelock, EDeviceDialogScreenSaverReDisplay);
                 }
             // this never shows a note
             }
@@ -995,7 +1099,9 @@
             DebugError(errorInProcess);
             if (errorInProcess == KErrNone)
                 {
-                setLockDialog(aReason, 0); // hide temporarilly because AskValidSecCode doesn't get in top of the Lock-icon. Thus, dismiss it.
+                RDEBUG("skip setLockDialog", 0);
+                // No need to disable screensaver. Notes now can get on top. Also, this avoids the transition Screensaver->unlockQuery, which produces flicker on HomeScreen.
+                setLockDialog(aReason, EDeviceDialogScreenSaverHidden); // hide temporarilly because AskValidSecCode doesn't get in top of the Lock-icon. Thus, dismiss it.
                 errorInProcess = AskValidSecCode(ELockAppEnableDevicelock);
                 }
             if (errorInProcess == KErrNone)
@@ -1025,6 +1131,7 @@
             break;
 
         }
+    RDEBUG("errorInProcess", errorInProcess);
     return errorInProcess;
     }
 /**************************/
@@ -1040,7 +1147,7 @@
 
     RWindowGroup& groupWin = CEikonEnv::Static()->RootWin();
 
-    if (status == 0) // hide
+    if (status == EDeviceDialogDestroyed || status == EDeviceDialogScreenSaverHidden) // hide
         {
         // aReason is not important here, but let's check nevertheless
         RDEBUG("ReleaseContext", R_AVKON_DEFAULT_SKEY_LIST);
@@ -1049,36 +1156,43 @@
         static_cast<CAknAppUi*>(CEikonEnv::Static()->EikAppUi())->KeySounds()->PopContext();
         RDEBUG("ReleaseContext done", 0x92);
 
-        if (aReason != ELockAppDisableKeyguard && aReason != ELockAppDisableDevicelock)
+        if (aReason != ELockAppDisableKeyguard && aReason != ELockAppDisableDevicelock && aReason != ELockAppOfferDevicelock)
             {
             RDEBUG("!!!!****!!!!! error. status=0 but aReason", aReason);
             }
         // secUiOriginatedQuery should be ESecurityUIsSystemLockOriginated . If not, this is not correctly setting it
         if (iDeviceDialogCreated >= EDeviceDialogCreated)
             {
-            iDeviceDialogCreated = EDeviceDialogDestroyed;
-            RDEBUG("new iDeviceDialogCreated", iDeviceDialogCreated);
-
-            RDEBUG("calling switchScreensaverMode", ESnsrViewTypeDisabled);
-            switchScreensaverMode( ESnsrViewTypeDisabled);
-
-            disconnect(iDeviceDialog, SIGNAL(dataReceived(QVariantMap)), this, SLOT(handleMessageFromScreensaver(QVariantMap)));
-            disconnect(iDeviceDialog, SIGNAL(deviceDialogClosed()), this, SLOT(handleScreensaverClosed()));
-            RDEBUG("signal disconnected", err);
-            // TODO this crashes for EDeviceDialogScreenSaver
-            err = iDeviceDialog->cancel();
-            RDEBUG("cancel (bool: 1= wellCancelled) err", err);
-            err = iDeviceDialog->error();
-            RDEBUG("err", err);
-            RDEBUG("calling iDeviceDialog->waitForClosed()", 0);
-            err = iDeviceDialog->waitForClosed();
-            RDEBUG("cancel (bool: 1= well waitForClosed) err", err);
-            err = iDeviceDialog->error();
-            RDEBUG("err", err);
+						if(status == EDeviceDialogDestroyed)
+							{
+	            disconnect(iDeviceDialog, SIGNAL(dataReceived(QVariantMap)), this, SLOT(handleMessageFromScreensaver(QVariantMap)));
+	            disconnect(iDeviceDialog, SIGNAL(deviceDialogClosed()), this, SLOT(handleScreensaverClosed()));
+	            RDEBUG("signal disconnected", err);
+	            // TODO this crashes for EDeviceDialogScreenSaver
+	            err = iDeviceDialog->cancel();
+	            RDEBUG("cancel (bool: 1= wellCancelled) err", err);
+	            err = iDeviceDialog->error();
+	            RDEBUG("err", err);
+	            RDEBUG("calling iDeviceDialog->waitForClosed()", 0);
+	            err = iDeviceDialog->waitForClosed();
+	            RDEBUG("cancel (bool: 1= well waitForClosed) err", err);
+	            err = iDeviceDialog->error();
+	            RDEBUG("err", err);
+            	handleScreensaverClosed();
+	            RDEBUG("deleting iDeviceDialog", 0);
+	            delete iDeviceDialog;
+	            RDEBUG("deleted iDeviceDialog", 1);
+	            iDeviceDialog = NULL;
+							}
+						else if(status == EDeviceDialogScreenSaverHidden)
+							{
+	            RDEBUG("calling switchScreensaverMode", ESnsrViewTypeDisabled);
+  	          switchScreensaverMode( ESnsrViewTypeDisabled);
+  	        	}
 
             TInt err = RProperty::Set(KPSUidSecurityUIs, KSecurityUIsSecUIOriginatedQuery, ESecurityUIsSecUIOriginatedUninitialized);
             RDEBUG("reset KSecurityUIsSecUIOriginatedQuery. err", err);
-            handleScreensaverClosed();
+
             TurnLights(ESecurityUIsLightsQueryOnRequest, aReason, 0x22);
             // Cancel power key and application key capturing
             groupWin.CancelCaptureKey(mPowerKeyCaptureHandle);
@@ -1097,14 +1211,11 @@
                 groupWin.CancelCaptureKey(mEKeyNoCaptureHandle);
                 mEKeyNoCaptureHandle = NULL;
                 }
-
-            RDEBUG("deleting iDeviceDialog", 0);
-            delete iDeviceDialog;
-            RDEBUG("deleted iDeviceDialog", 1);
-            iDeviceDialog = NULL;
+            iDeviceDialogCreated = status;
+            RDEBUG("new iDeviceDialogCreated", iDeviceDialogCreated);
             }
         }
-    else if (status == 1) // show
+    else if (status == EDeviceDialogCreated || status == EDeviceDialogScreenSaverReDisplay) // show
         {
         RDEBUG("PushContextL", R_AVKON_DEFAULT_SKEY_LIST);
         static_cast<CAknAppUi*>(CEikonEnv::Static()->EikAppUi())->KeySounds()->PushContextL(R_AVKON_SILENT_SKEY_LIST);
@@ -1117,8 +1228,6 @@
         // secUiOriginatedQuery should be ESecurityUIsSecUIOriginatedUninitialized . If not, the validation is not correctly filtering it
         QVariantMap params;
         TBool err=EFalse;
-#define ESecUiTypeDeviceLock        0x00100000
-#define ESecUiTypeKeyguard          0x00200000
 
         if (aReason == ELockAppEnableKeyguard)
             params.insert("type", ESecUiTypeKeyguard);
@@ -1126,7 +1235,7 @@
             params.insert("type", ESecUiTypeDeviceLock);
         else
             {
-            RDEBUG("error. status=1 but aReason", aReason);
+            RDEBUG("!!!!****!!!!! error. status=1 but aReason", aReason);
             }
         // no need for title. Icon should be explicit enough
         // params.insert("title", "Locked");
@@ -1155,8 +1264,34 @@
             }
         const QString KScreensaverDeviceDialog("com.nokia.screensaver.snsrdevicedialogplugin/1.0");
         RDEBUG("pre show", aReason);
-        bool launchSuccesful = iDeviceDialog->show(KScreensaverDeviceDialog, params); // and continue processing
-        RDEBUG("post show. bool launchSuccesful", launchSuccesful);
+        TInt skipScreenSaver=0;
+        bool launchSuccesful = EFalse;
+
+		    TInt aCallStatus=-1;
+		    err = RProperty::Get(KPSUidCtsyCallInformation, KCTsyCallState, aCallStatus);
+		    RDEBUG("aCallStatus", aCallStatus);
+		    // never create screensaver if active call.
+				if (aCallStatus != EPSCTsyCallStateUninitialized && aCallStatus != EPSCTsyCallStateNone && iLockStatus >= EDevicelockActive )
+					{
+					skipScreenSaver=1;
+					RDEBUG("new skipScreenSaver", skipScreenSaver);
+					}
+				RDEBUG("skipScreenSaver", skipScreenSaver);
+				if(!skipScreenSaver)
+					{
+	        if(iDeviceDialogCreated == EDeviceDialogCreated)
+	        	{
+	        	launchSuccesful = iDeviceDialog->show(KScreensaverDeviceDialog, params); // and continue processing
+	        	RDEBUG("post show. bool launchSuccesful", launchSuccesful);
+	        	}
+	        else if(iDeviceDialogCreated == EDeviceDialogScreenSaverHidden || iDeviceDialogCreated == EDeviceDialogRaised)
+	        	{
+        		RDEBUG("calling switchScreensaverMode ESnsrViewTypeActive", ESnsrViewTypeActive);
+	        	switchScreensaverMode( ESnsrViewTypeActive);
+	        	launchSuccesful = ETrue;
+	        	}
+        	}
+        RDEBUG("bool launchSuccesful", launchSuccesful);
         err = iDeviceDialog->error();
         RDEBUG("iDeviceDialog->error", err);
         if (launchSuccesful) // TODO && !error ???
@@ -1168,7 +1303,7 @@
             // mode but setting screensaver again to standby doesn't hurt.
             mScreensaverModeTimer->start();
             }
-        else // some err. Usually 3 (not existing)
+        else if (!skipScreenSaver)// some err. Usually 3 (not existing)
             {
             // screensaver has failed. Probably because it's not installed. Then, try the standard lock-icon
             iDeviceDialogCreated = EDeviceDialogLockIcon;
@@ -1247,8 +1382,9 @@
 
 void Autolock::activeKeyguard()
     {
+    RDEBUG("0", 0);
     // activity while keyguarded. Nothing to do
-    RDEBUG("0", 0);
+    RDEBUG("99", 0x99);
     }
 
 void Autolock::notActiveKeyguard()
@@ -1287,8 +1423,9 @@
 /* Some activity detected while the deviceLock is enabled */
 void Autolock::activeDevicelock()
     {
+    RDEBUG("0", 0);
     // nothing to do
-    RDEBUG("0", 0);
+    RDEBUG("99", 0x99);
     }
 
 void Autolock::notActiveDevicelock()
@@ -1304,7 +1441,6 @@
         ret = TryChangeStatus(ELockAppEnableDevicelock);
         }
     RDEBUG("ret", ret);
-
     }
 
 void Autolock::switchScreensaverMode(int mode)
@@ -1340,14 +1476,14 @@
             		}
             }
         }
-    RDEBUG("1", 1);
+    RDEBUG("99", 0x99);
     }
 
 void Autolock::switchScreensaverToPowerSaveMode()
     {
     RDEBUG("0", 0);
     switchScreensaverMode( ESnsrViewTypeStandby);
-    RDEBUG("1", 1);
+    RDEBUG("99", 0x99);
     }
 
 void Autolock::handleMessageFromScreensaver(const QVariantMap &data)
@@ -1404,6 +1540,7 @@
             // RDEBUG("err", err);
             }
         }
+    RDEBUG("99", 0x99);
     }
 
 void Autolock::handleScreensaverClosed()
@@ -1505,7 +1642,7 @@
         } // ev
     // Process if not done before. For example, redraw or quit
     TBool widgetEventSuccesful = QWidget::event(ev);
-    RDEBUG("9", 9);
+    RDEBUG("99", 0x99);
     return widgetEventSuccesful;
     }
 
@@ -1514,6 +1651,7 @@
     RDEBUG("0", 0);
     int ret = KErrNone;
    	RDEBUG("iProcessingEvent", iProcessingEvent);
+   	RDEBUG("iLockStatus", iLockStatus);
    	// iProcessingEvent is used to avoid concurrent processing, i.e. 2 side-switch presses; because it needs time to create (and dismiss) the dialog
    	// However this disallows keyguard during unlock-quey.
    	if(iProcessingEvent < 1)
@@ -1525,7 +1663,20 @@
 	        {
 	        iShowKeyguardNote = 0; // no note on enable keyguard
 	        callerHasECapabilityWriteDeviceData = 1;
-	        ret = TryChangeStatus(ELockAppEnableKeyguard); // this should not ask confirmation
+            // TODO ELockAppEnableDevicelock
+	        CRepository *repository = 0;
+	        int deviceLock = 0;
+	        QT_TRAP_THROWING(repository = CRepository::NewL(KCRUidSecuritySettings));
+	        repository->Get(KSettingsAutoLockTime, deviceLock);
+	        delete repository;
+	        if (65535 == deviceLock) {
+				// Setting says "When keys& screen locked". Therefore device is also locked when screen is manually locked.
+				// TODO what about the API for screen lock?
+	            ret = TryChangeStatus(ELockAppEnableDevicelock); // this should not ask confirmation
+	        } else {
+	            ret = TryChangeStatus(ELockAppEnableKeyguard); // this should not ask confirmation
+	        }
+
 	        }
 	    else if (iLockStatus == EKeyguardActive)
 	        {
@@ -1547,6 +1698,9 @@
 	  else
 	  	{
 	  	// user presses side-switch while another is in process, most probably a unlock-query. Thus, re-switch on the lights
+	  	iProcessingEvent++;	// just to know how many processes we missed
+	  	if(iProcessingEvent>=10)
+	  		iProcessingEvent=0;	// if user really insists, then try again
 	  	if (iLockStatus == EDevicelockActive)
 	  		{
         TurnLights(ESecurityUIsLightsQueryOnRequest, ELockAppEnableDevicelock, 0x26);
@@ -1648,7 +1802,11 @@
     RDEBUG("iLockStatus", iLockStatus);
     if (aCallStatus == EPSCTsyCallStateUninitialized || aCallStatus == EPSCTsyCallStateNone)
         {
+        // Telephone might want to re-enable only-keyguard after the call ends. Allow it. This means re-showing the screensaver, not more.
         RDEBUG("call is inactive", aCallStatus);
+        if(iLockStatus == EDevicelockActive)
+            setLockDialog(ELockAppEnableKeyguard, 1);
+        
         if (iLockStatus == EKeyguardActive || iLockStatus == EDevicelockActive)
             {
             if (!mEKeyYesCaptureHandle)
@@ -1660,6 +1818,11 @@
     else
         {
         RDEBUG("call is active", aCallStatus);
+
+        RDEBUG("allowing telephone to disable keyguard . Call setLockDialog 0", 0);
+        if(iLockStatus == EDevicelockActive)
+            setLockDialog(ELockAppDisableKeyguard, 0);
+
         // Telephone should be on top, but nevertheless we give End/Send to them
         // This should be done only if locked, but it doesn't harm
         if (mEKeyYesCaptureHandle)
@@ -1677,7 +1840,7 @@
         // it seems that Telephone doesn't turn-on the lights when an incoming call on deviceLock. It's done here.
         TurnLights(ESecurityUIsLightsQueryOnRequest, ELockAppDisableKeyguard, 0x28);
         }
-    RDEBUG("9", 9);
+    RDEBUG("99", 0x99);
     }
 // SecUiNotificationDialog::subscriberKSecurityUIsDismissDialogChanged()
 // A way for Autolock to dismiss any possible PIN dialog
@@ -1691,6 +1854,15 @@
     RDEBUG("err", err);
     RDEBUG("aDismissDialog", aDismissDialog);
     }
+
+void Autolock::subscriberKSecurityUIsTestCodeChanged()
+    {
+    RDEBUG("0", 0);
+    TInt aTestCode = -1;
+    TInt err = RProperty::Get(KPSUidSecurityUIs, KSecurityUIsTestCode, aTestCode);
+    RDEBUG("err", err);
+    RDEBUG("aTestCode", aTestCode);
+    }
 // ----------AutolockService---------------
 
 AutolockService::AutolockService(Autolock* parent) :
@@ -1783,7 +1955,7 @@
     RDEBUG("0", 0);
     // Just quit service application if client ends
     mAsyncAnswer = false;
-    RDEBUG("0", 0);
+    RDEBUG("99", 0x99);
     // mAutolock->quit();
     }