testexecfw/useremul/src/GlobalCapturer.cpp
changeset 0 3e07fef1e154
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     1 /*------------------------------------------------------------------
       
     2  -
       
     3  * Software Name : UserEmulator
       
     4  * Version       : v4.2.1309
       
     5  * 
       
     6  * Copyright (c) 2009 France Telecom. All rights reserved.
       
     7  * This software is distributed under the License 
       
     8  * "Eclipse Public License - v 1.0" the text of which is available
       
     9  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    10  *
       
    11  * Initial Contributors:
       
    12  * France Telecom 
       
    13  *
       
    14  * Contributors:
       
    15  *------------------------------------------------------------------
       
    16  -
       
    17  * File Name: GlobalCapturer.cpp
       
    18  * 
       
    19  * Created: 13/08/2009
       
    20  * Author(s): Marcell Kiss, Reshma Sandeep Das
       
    21  *   
       
    22  * Description:
       
    23  * Key capturing class
       
    24  *------------------------------------------------------------------
       
    25  -
       
    26  *
       
    27  */
       
    28  
       
    29 // System Includes
       
    30 #include <e32base.h>
       
    31 #include <e32std.h>
       
    32 #include <e32cons.h>
       
    33 #include <aknnotewrappers.h>
       
    34 #include <apgwgnam.h>
       
    35 #include <eikenv.h>
       
    36 #include <apgtask.h>
       
    37 #include <APGCLI.H>
       
    38 #include <W32STD.H>
       
    39 #include <bautils.h>
       
    40 // User Includes
       
    41 #include <UserEmulator_0x2001C3AF.rsg>
       
    42 #include "GlobalCapturer.h"
       
    43 #include "UserEmulatorApplication.h"
       
    44 
       
    45 // Constants
       
    46 const TUid KUidStandBy = { 0x102750f0 };
       
    47 
       
    48 const TUint KKeyCodePowerKey  	= EKeyDevice2;
       
    49 const TUint KCameraKey 			= 0xF887;
       
    50 const TUint KEndKeyScanCode 	= 197;
       
    51 
       
    52 const TInt KCaptureMaxPriority = 11;
       
    53 const TInt KAppInfoLength = 10;
       
    54 const TInt KZeroAscii = 48;
       
    55 const TInt KZedAscii = 122;
       
    56 const TInt KDotAscii = 46;
       
    57 
       
    58 // ============================ MEMBER FUNCTIONS ===============================
       
    59 // -------------------------------------------
       
    60 // CGlobalCapturer::CGlobalCapturer
       
    61 // -------------------------------------------
       
    62 //
       
    63 CGlobalCapturer::CGlobalCapturer(MKeyObserver& aObserver, CSettings& aSettings, TBool& aScriptRun, 
       
    64 		TBool& aRandomTestStarted, RClientCommander& aClientCommander, CEikonEnv* aEikonEnv, RApaLsSession& aSession) :
       
    65 		CActive( EPriorityNormal ),iObserver(aObserver),iSettings(aSettings), iScriptRun(aScriptRun),
       
    66 		iRandomTestStarted(aRandomTestStarted),iClientCommander(aClientCommander), iEEnv(aEikonEnv), iSession(aSession)
       
    67 {
       
    68 }
       
    69 // -------------------------------------------
       
    70 // CGlobalCapturer::NewL
       
    71 // -------------------------------------------
       
    72 //
       
    73 CGlobalCapturer* CGlobalCapturer::NewL(MKeyObserver& aObserver, CSettings& aSettings, TBool& aScriptRun, 
       
    74 		TBool& aRandomTestStarted, RClientCommander& aClientCommander, CEikonEnv* aEikonEnv,RApaLsSession& aSession)
       
    75 	{
       
    76 	CGlobalCapturer* me = new (ELeave) CGlobalCapturer(aObserver,aSettings,aScriptRun, 
       
    77 			aRandomTestStarted,aClientCommander,aEikonEnv,aSession);
       
    78 	CleanupStack::PushL(me);
       
    79 	me->ConstructL();
       
    80 	CleanupStack::Pop(me);
       
    81 	return me;
       
    82 	}
       
    83 
       
    84 // -------------------------------------------
       
    85 // Symbian 2nd phase constructor
       
    86 // -------------------------------------------
       
    87 //
       
    88 void CGlobalCapturer::ConstructL(void)
       
    89 	{
       
    90 	iRecord = EFalse;
       
    91 	iKeyCaptureStart = EFalse;
       
    92 	
       
    93 	iCameraAppTimer = CCameraAppTimer::NewL(*this);
       
    94 	}
       
    95 
       
    96 // -------------------------------------------
       
    97 // Destructor
       
    98 // -------------------------------------------
       
    99 //
       
   100 CGlobalCapturer::~CGlobalCapturer()
       
   101 {
       
   102 	Cancel();			
       
   103 	delete iWindowGroup;
       
   104 	iWsSession.Close();
       
   105 	iFs.Close();
       
   106 	
       
   107 	if(iCameraAppTimer)
       
   108 		{
       
   109 	    delete iCameraAppTimer;
       
   110 	    iCameraAppTimer = NULL;
       
   111 	    }
       
   112 }
       
   113 
       
   114 // -------------------------------------------
       
   115 // CActive cancel function
       
   116 // -------------------------------------------
       
   117 //
       
   118 void CGlobalCapturer::DoCancel()
       
   119 {	
       
   120 	if ( IsActive() )
       
   121 	{
       
   122 		iWsSession.EventReadyCancel();
       
   123 	}
       
   124 }
       
   125 
       
   126 // -------------------------------------------
       
   127 // Initialise key capturing
       
   128 // -------------------------------------------
       
   129 //
       
   130 void CGlobalCapturer::StartCapturingL() 
       
   131 {
       
   132 	// Connect to the window server
       
   133 	User::LeaveIfError( iWsSession.Connect() );
       
   134 	// Create an invisible window group. Well, we'll make it invisible later
       
   135 	iWindowGroup = new (ELeave) RWindowGroup ( iWsSession );
       
   136 	iWindowGroup->Construct( (TUint32)iWindowGroup, EFalse );
       
   137 	
       
   138 	CApaWindowGroupName* wn;
       
   139 	wn=CApaWindowGroupName::NewLC(iWsSession);
       
   140 	wn->SetHidden(ETrue);
       
   141 	wn->SetWindowGroupName(*iWindowGroup);	
       
   142 	CleanupStack::PopAndDestroy(wn);
       
   143 	
       
   144 	CActiveScheduler::Add( this );
       
   145 }
       
   146 
       
   147 // -------------------------------------------
       
   148 // Starts capturing Power button key presses
       
   149 // -------------------------------------------
       
   150 //
       
   151 void CGlobalCapturer::StartL()
       
   152 { 
       
   153     User::LeaveIfError( iCaptureHandle1 = iWindowGroup->CaptureKey( EKeyDevice2, 0, 0, KCaptureMaxPriority ) );	
       
   154   
       
   155 	// Tell window server we are ready to receive events
       
   156 	if ( !IsActive() )
       
   157 	{
       
   158 		iWsSession.EventReady( &iStatus );
       
   159 		SetActive();
       
   160 	}		
       
   161 }
       
   162 
       
   163 // -------------------------------------------
       
   164 // Stops capturing Power button key presses
       
   165 // -------------------------------------------
       
   166 //
       
   167 void CGlobalCapturer::Stop()
       
   168 {
       
   169 		Cancel();
       
   170 		iWindowGroup->CancelCaptureKey( iCaptureHandle1 );
       
   171 }
       
   172 
       
   173 // --------------------------------------------------------------
       
   174 // CActive RunL function. Called when a key press happened
       
   175 // --------------------------------------------------------------
       
   176 //
       
   177 void CGlobalCapturer::RunL()
       
   178 {
       
   179 	if( iStatus == KErrNone ) 	
       
   180 	{
       
   181 		TWsEvent we;
       
   182 		iWsSession.GetEvent( we );
       
   183 		iKeyEvent=we.Key();
       
   184 		CApaWindowGroupName* gn = CApaWindowGroupName::NewLC(iEEnv->WsSession(), iEEnv->WsSession().GetFocusWindowGroup());
       
   185 		TUid foreTaskUid = gn->AppUid();
       
   186 		CleanupStack::PopAndDestroy(gn);
       
   187 		
       
   188 		// Stops key recording
       
   189 		if(iKeyEvent->iCode == EKeyDevice2 && iRecord)
       
   190 			{
       
   191 			StopRecordKeysL();
       
   192 			return;
       
   193 			}
       
   194 		// Stops random tests
       
   195 		else if(iKeyEvent->iCode == EKeyDevice2 && iRandomTestStarted)
       
   196 			{
       
   197 			iObserver.RandomTestEndsL();
       
   198 			return;
       
   199 			}
       
   200 		// Stops running scripts
       
   201 		else if(iKeyEvent->iCode == EKeyDevice2)
       
   202 			{
       
   203 			Stop();
       
   204 			iObserver.TerminationKeyReceived();
       
   205 			return;
       
   206 			}
       
   207 		
       
   208 		// Send event to the application which is in foreground			
       
   209 		TInt appId = iEEnv->WsSession().GetFocusWindowGroup();
       
   210 		iEEnv->WsSession().SendEventToWindowGroup(appId, we);											
       
   211 		
       
   212 		if ( !IsActive() )
       
   213 		{
       
   214 			iWsSession.EventReady( &iStatus );
       
   215 			SetActive();
       
   216 		}		
       
   217 	}
       
   218 	
       
   219 }
       
   220 
       
   221 // --------------------------------------------------------------
       
   222 // Checks if camera app is in memory or not
       
   223 // --------------------------------------------------------------
       
   224 //
       
   225 void CGlobalCapturer::CheckCameraAppL()
       
   226 {
       
   227     _LIT8(KDecimal,"%d");
       
   228     _LIT8(KAsterisk,"*");
       
   229 	// Get foreground app's name
       
   230 	CApaWindowGroupName* gn = CApaWindowGroupName::NewLC(iEEnv->WsSession(),iEEnv->WsSession().GetFocusWindowGroup());
       
   231 	TApaAppInfo appInfo;
       
   232 	RApaLsSession ApaLs;
       
   233 	User::LeaveIfError(ApaLs.Connect());
       
   234 	ApaLs.GetAppInfo(appInfo, gn->AppUid());
       
   235 	// Store length!
       
   236 	iAppName.Format(KDecimal,appInfo.iCaption.Length());
       
   237 	if(appInfo.iCaption.Length()<KAppInfoLength)
       
   238 		iAppName.Append(KAsterisk);
       
   239 	
       
   240 	iAppName.Append(appInfo.iCaption);
       
   241 	CleanupStack::PopAndDestroy(gn);
       
   242 	ApaLs.Close();
       
   243 	
       
   244 	iAppName.ZeroTerminate();
       
   245 	TPtrC8 ptr;
       
   246 	ptr.Set(iAppName);
       
   247 	// Poll. (And send foreground app's name) 
       
   248 	if( iClientCommander.AnimCommand( RClientCommander::EPoll, &ptr) == 1 )
       
   249 	{
       
   250 		TUid CamId;
       
   251 		CamId = KUidCamera;
       
   252 		TApaTaskList taskList(iEEnv->WsSession());
       
   253 		TApaTask task = taskList.FindApp(CamId);
       
   254 		if (task.Exists())
       
   255 			task.EndTask();
       
   256 		else
       
   257 		  iClientCommander.AnimCommand( RClientCommander::ETaskExisted, NULL);
       
   258 	}
       
   259 	// Restart camera app timer
       
   260 	iCameraAppTimer->After(KWait08); // 0.8 sec
       
   261 }
       
   262 
       
   263 // ---------------------------------------------
       
   264 // Filters not appropriate characters
       
   265 // Allows only chars. from 0 to z and '.'
       
   266 // ---------------------------------------------
       
   267 //
       
   268 TBool CGlobalCapturer::StrictFileNameCheck(const TDesC& aFileName)
       
   269 {
       
   270 	TBool res=ETrue;
       
   271 	for(TInt t=0;t<aFileName.Length();t++)
       
   272 	{
       
   273 		const TUint16* a=aFileName.Mid(t,1).Ptr();
       
   274 		if((*a<KZeroAscii || *a>KZedAscii) && *a!=KDotAscii) // 46 -> '.'
       
   275 			res=EFalse;
       
   276 	}
       
   277 	return res;
       
   278 }
       
   279 // ---------------------------------------------
       
   280 // Starts key record with Windows server dll
       
   281 // ---------------------------------------------
       
   282 //
       
   283 TInt CGlobalCapturer::StartRecordKeysL()
       
   284 	{
       
   285 	// Checks if there's another process started
       
   286 	if(!iRecord && !iScriptRun && !iKeyCaptureStart)
       
   287 		{
       
   288 		_LIT16(Ktitle,"Output file name?");
       
   289 		TBuf16<KBuffer256> aBuffer;
       
   290 		
       
   291 		TBool exit = EFalse;
       
   292 		while(!exit)
       
   293         {
       
   294           // Asks for output file name
       
   295 		  CAknTextQueryDialog* Dialog = CAknTextQueryDialog::NewL(aBuffer,CAknQueryDialog::ENoTone); 
       
   296 		  Dialog->PrepareLC(R_FILE_DIALOG); 
       
   297 		  Dialog->SetPromptL(Ktitle); 
       
   298 		
       
   299 		  if(Dialog->RunLD())
       
   300 			{
       
   301 				iBuf8.Copy(iSettings.iScriptsPath);
       
   302 				TInt a = iBuf8.LocateReverse('\\');
       
   303 				if(a!=KErrNotFound)
       
   304 				{
       
   305 				iBuf8 = iBuf8.Left(a+1);
       
   306 				}
       
   307 								
       
   308 				iBuf8.Append(aBuffer);
       
   309 				
       
   310 				if(iBuf8.LocateReverse('.')==KErrNotFound)
       
   311 				{
       
   312 			    _LIT(Kxml,".xml");
       
   313 			    iBuf8.Append(Kxml);
       
   314 			    }
       
   315 							
       
   316 				TBuf<KBuffer512> TempPath;
       
   317 				TempPath.Copy(iBuf8);
       
   318 				// Validate file name
       
   319 				if(iEEnv->FsSession().IsValidName(TempPath) && StrictFileNameCheck(aBuffer))
       
   320 				{
       
   321 					exit = ETrue;
       
   322 				}
       
   323 				else
       
   324 				{
       
   325 				    _LIT(KInvalid,"Invalid file name! Please try again.");
       
   326 					CAknErrorNote* errorNote = new (ELeave) CAknErrorNote(ETrue);
       
   327 					errorNote->ExecuteLD(KInvalid);
       
   328 					continue;
       
   329 				}
       
   330 				
       
   331 				User::LeaveIfError(iFs.Connect());
       
   332 				
       
   333 				// If file exists then overwrite it or not?
       
   334 				TBool overwrite=ETrue;
       
   335 				TBuf16<KBuffer512> TempiBuf8;
       
   336 				TBuf16<KBuffer8> empty;
       
   337 				TempiBuf8.Copy(iBuf8);
       
   338 				if(BaflUtils::FileExists(iFs, TempiBuf8))
       
   339 				{
       
   340 					_LIT8(Kk,"Overwrite existing file?");
       
   341 					_LIT8(Kk1,"");
       
   342 					TempiBuf8.Copy(Kk);
       
   343 					empty.Copy(Kk1);
       
   344 					if(iEEnv->QueryWinL(TempiBuf8,empty))
       
   345 						overwrite = ETrue;
       
   346 					else
       
   347 						overwrite = EFalse;
       
   348 				}
       
   349 				
       
   350 				iFs.Close();
       
   351 			
       
   352 				if(overwrite)
       
   353 				{
       
   354 					TPtrC8 ptr;
       
   355 					ptr.Set(iBuf8);
       
   356 					// Starts all event capture with the Window server object!
       
   357 					TInt res=iClientCommander.AnimCommand( RClientCommander::EStart, &ptr);
       
   358 					
       
   359 					if(res==1)
       
   360 					{
       
   361 					    _LIT(KWritingError,"Script file writing error. Drive might be locked");
       
   362 						CAknErrorNote* errorNote = new (ELeave) CAknErrorNote(ETrue);
       
   363 						errorNote->ExecuteLD(KWritingError);
       
   364 						return 1;
       
   365 					}
       
   366 					else
       
   367 					{
       
   368                        TBuf<KBuffer64> buff;
       
   369                        _LIT(KWait,"Wait. Script recording will start from the Idle screen..");
       
   370                        
       
   371                        CAknConfirmationNote* note = new (ELeave) CAknConfirmationNote(ETrue);
       
   372                        buff.Copy(KWait);
       
   373                        note->ExecuteLD(buff);
       
   374                        User::After(KWait02);
       
   375 					   // Closes apps (there are exceptions) as a reset
       
   376 					   KillUnwantedAppsResetL();
       
   377 					
       
   378 					   // Starts wait timer
       
   379                        iClientCommander.AnimCommand( RClientCommander::EStartTiming, NULL);
       
   380 					
       
   381 					   // Resets menu screen
       
   382 					   if(ScreenReset()!=KErrNone)
       
   383 						  return 1;
       
   384 					   iKeyCaptureStart = ETrue;				
       
   385 					   iRecord = ETrue;
       
   386 	
       
   387 	                   // Starts Power button capturing
       
   388 					   StartL();
       
   389 					   // Starts camera app timer
       
   390 					   iCameraAppTimer->After(KWait05); // 0.5 sec
       
   391 					  
       
   392 					}	
       
   393 					
       
   394 				}
       
   395 			}
       
   396 			else
       
   397 				return 1;
       
   398 		  }// while end
       
   399 		}
       
   400 	else
       
   401 		{
       
   402 		_LIT(KRunning,"A script or recording is already running!");
       
   403 		CAknErrorNote* errorNote = new (ELeave) CAknErrorNote(ETrue);
       
   404 		errorNote->ExecuteLD(KRunning);
       
   405 		return 1;
       
   406 		}
       
   407 	
       
   408 	return 0;
       
   409 	}
       
   410 
       
   411 // --------------------------------------------------------------------
       
   412 // Closes Menu screen and resets idle screen by two No key presses
       
   413 // --------------------------------------------------------------------
       
   414 //
       
   415 TInt CGlobalCapturer::ScreenReset()
       
   416 	{
       
   417 	// Reset Menu app.
       
   418 	TApaTaskList taskList(iEEnv->WsSession());
       
   419 	TApaTask task = taskList.FindApp(KUidMenu);
       
   420         
       
   421 	task.EndTask();
       
   422 	
       
   423 	//Brings 'Menu' to foreground and resets screen selecting 'Exit'
       
   424 	TApaTaskList taskList2(iEEnv->WsSession());
       
   425     TApaTask task2 = taskList2.FindApp(KUidHomeScreen); //? old phones
       
   426     task2.BringToForeground();
       
   427     User::After(KWait1);
       
   428     
       
   429     TRawEvent lEventDown;
       
   430     TRawEvent lEventUp;
       
   431     
       
   432     // No key press (Idle screen reset!) //? old phones (No key opens a menu on old phones)
       
   433     lEventDown.Set(TRawEvent::EKeyDown, KEndKeyScanCode); // No key
       
   434     UserSvr::AddEvent(lEventDown);
       
   435     User::After(KWait01);               
       
   436     lEventUp.Set(TRawEvent::EKeyUp, KEndKeyScanCode);                       
       
   437     UserSvr::AddEvent(lEventUp);
       
   438     User::After(KWait1);
       
   439     
       
   440 	return KErrNone;
       
   441 	}
       
   442 
       
   443 // --------------------------------------------------------------------
       
   444 // Kills applications in the background except apps which has to run
       
   445 // --------------------------------------------------------------------
       
   446 //
       
   447 void CGlobalCapturer::KillUnwantedAppsResetL()
       
   448 	{	
       
   449 	
       
   450     TInt numWindowGroups = iEEnv->WsSession().NumWindowGroups();
       
   451     CArrayFixFlat<TInt>* windowGroupList = new(ELeave) CArrayFixFlat<TInt>(numWindowGroups);
       
   452     CleanupStack::PushL(windowGroupList);
       
   453 
       
   454     // Populate array with current group list ids
       
   455     User::LeaveIfError(iEEnv->WsSession().WindowGroupList(windowGroupList));
       
   456     CApaWindowGroupName* windowGroupName = CApaWindowGroupName::NewLC(iEEnv->WsSession());
       
   457 
       
   458     for (TInt i = 0; i < numWindowGroups; ++i)
       
   459     	{
       
   460         TInt wgId = windowGroupList->At(i);
       
   461         windowGroupName->ConstructFromWgIdL(wgId);
       
   462         TUid Uid = windowGroupName->AppUid();
       
   463         TInt32 appUid = Uid.iUid;
       
   464         
       
   465                        
       
   466         // Exceptions
       
   467         if(appUid != KUidMenu.iUid && appUid != KUidPanicCapturer.iUid && appUid != KUidUserEmulatorApp.iUid &&
       
   468         		appUid != KUidHomeScreen.iUid && appUid != KUidAknCapServer.iUid &&
       
   469                    		appUid != KUidAknnfySrv.iUid && appUid != KUidTelephony.iUid && 
       
   470 appUid != KUidPenInputSrv.iUid )
       
   471         	{
       
   472 			// Only list 'visible' applications
       
   473 			if (!windowGroupName->Hidden() && !windowGroupName->IsBusy() && !windowGroupName->IsSystem())
       
   474 				{
       
   475 				TApaTaskList taskList(iEEnv->WsSession());
       
   476 				TApaTask task = taskList.FindApp(Uid);
       
   477 				if (task.Exists())
       
   478 					{
       
   479         
       
   480 					task.EndTask();
       
   481 					User::After(KWait005);
       
   482 					}
       
   483 				}
       
   484         	}
       
   485            
       
   486     	}
       
   487     CleanupStack::PopAndDestroy(2);
       
   488    
       
   489 	}
       
   490 
       
   491 // ----------------------------------------------------
       
   492 // Notifies Window server object to stop capturing
       
   493 // ----------------------------------------------------
       
   494 //
       
   495 void CGlobalCapturer::StopRecordKeysL()
       
   496 {
       
   497 	iKeyCaptureStart = EFalse;
       
   498 	
       
   499 	// Call client commander which calls server side object
       
   500 	iClientCommander.AnimCommand( RClientCommander::EStop, NULL);
       
   501 	// Cancels camera app timer
       
   502 	iCameraAppTimer->Cancel();
       
   503 	// Stop Power button capturing
       
   504 	Stop();
       
   505 	
       
   506 	if(iRecord)
       
   507 		iRecord = EFalse;
       
   508 	// Bring this app to foreground
       
   509 	iObserver.BringUserEmulatorToFront();
       
   510 	// Display acknowledgement
       
   511 	CAknInformationNote* note = new (ELeave) CAknInformationNote(ETrue);
       
   512 	TBuf<KBuffer64> buff;
       
   513 	_LIT(KCapturingEnded, "Capturing ended");
       
   514 	buff.Format(KCapturingEnded);
       
   515 	note->ExecuteLD(buff);
       
   516 }
       
   517 // End of file