idlefw/plugins/shortcutplugin/src/aiscuttargetshutter.cpp
changeset 0 79c6a41cd166
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2007 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 <apgtask.h>        //TApaTask
       
    20 #include <apgcli.h>         //RApaLsSession
       
    21 
       
    22 #include "aiscuttargetshutter.h"
       
    23 #include "aiscutdefs.h"
       
    24 #include "debug.h"
       
    25 
       
    26 #include <AknSgcc.h>
       
    27 
       
    28 const TUid KMenuUID3 = { 0x101F4CD2 }; 
       
    29 
       
    30 const TInt KTaskExistsDelay = 1500000; //1.5 second
       
    31 const TInt KTaskNotExistsDelay = 500000; //0.5 second
       
    32 const TInt KMaxNumberOfTries = 3;
       
    33 
       
    34 
       
    35 // ======== MEMBER FUNCTIONS ========
       
    36 
       
    37 CAiScutTargetShutter::CAiScutTargetShutter(CCoeEnv* aEnv, TUid aAppUid)
       
    38     : CActive(CActive::EPriorityLow)
       
    39     , iEnv(aEnv)
       
    40     , iAppUid(aAppUid)
       
    41 {
       
    42     CActiveScheduler::Add(this);
       
    43 }
       
    44 
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CAiScutTargetShutter::~CAiScutTargetShutter()
       
    51 {
       
    52     if (iPeriodic)
       
    53     {
       
    54         iPeriodic->Cancel();
       
    55         delete iPeriodic;
       
    56     }
       
    57 
       
    58     iWsSession.Close();
       
    59     Cancel();
       
    60 }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 void CAiScutTargetShutter::ConstructL(TBool aIsRunning, TBool aIsDialog)
       
    67 {
       
    68     iIsRunning = aIsRunning;
       
    69     iIsDialog = aIsDialog;
       
    70     iTaskExists = EFalse;
       
    71     iTaskKilled = EFalse;
       
    72     iCounter = 0;
       
    73     iPeriodic = CPeriodic::NewL(CActive::EPriorityLow);
       
    74     User::LeaveIfError(iWsSession.Connect());
       
    75 }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CAiScutTargetShutter* CAiScutTargetShutter::NewL(CCoeEnv* aEnv, TUid aAppUid, TBool aIsRunning, TBool aIsDialog)
       
    82 {
       
    83     CAiScutTargetShutter* self = new (ELeave) CAiScutTargetShutter(aEnv, aAppUid);
       
    84     CleanupStack::PushL(self);
       
    85     self->ConstructL(aIsRunning, aIsDialog);
       
    86     CleanupStack::Pop(self);
       
    87     return self;
       
    88 }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 void CAiScutTargetShutter::StartL()
       
    95 {
       
    96 	__PRINT( __DBG_FORMAT( "XAI: CAiScutTargetShutter::Start() app uid=0x%x"), iAppUid.iUid);
       
    97 	
       
    98 
       
    99     TApaTaskList taskList(iWsSession);
       
   100     TApaTask idleTask(taskList.FindApp(KScutActiveIdleUid));
       
   101 
       
   102     if ( idleTask.Exists() && iIsRunning)
       
   103     {
       
   104         if ( iIsDialog )
       
   105             {
       
   106             TKeyEvent keyEvent;
       
   107             keyEvent.iCode = EKeyEscape;
       
   108             keyEvent.iModifiers = 0;
       
   109             keyEvent.iRepeats = 0;
       
   110             iEnv->SimulateKeyEventL(keyEvent, EEventKey);            
       
   111             }
       
   112         //idleTask.BringToForeground();
       
   113         CAknSgcClient::MoveApp(idleTask.WgId(), ESgcMoveAppToForeground);
       
   114         iTaskExists = ETrue;               
       
   115 		__PRINTS( "XAI:   exists and running");
       
   116 
       
   117     }
       
   118     else
       
   119     {
       
   120         TKeyEvent keyEvent;
       
   121         keyEvent.iCode = EKeyEscape;
       
   122         keyEvent.iModifiers = 0;
       
   123         keyEvent.iRepeats = 0;
       
   124         iEnv->SimulateKeyEventL(keyEvent, EEventKey);
       
   125 
       
   126         // bring active idle to foreground
       
   127         TApaTask idleTask(taskList.FindApp(KScutActiveIdleUid));
       
   128         if (idleTask.Exists())
       
   129         {
       
   130 			__PRINTS("XAI:   idle to foreground");
       
   131             //idleTask.BringToForeground();
       
   132             CAknSgcClient::MoveApp(idleTask.WgId(), ESgcMoveAppToForeground);
       
   133         }
       
   134         TApaTask task(taskList.FindApp(iAppUid));
       
   135 		if (task.Exists())
       
   136         {
       
   137 			//task.SendToBackground();
       
   138 			CAknSgcClient::MoveApp(task.WgId(), ESgcMoveAppToBackground);
       
   139         }
       
   140     }
       
   141 	if( !iPeriodic->IsActive() )
       
   142 	{
       
   143 	    if (iTaskExists)
       
   144     	{
       
   145             iPeriodic->Start(KTaskExistsDelay, KTaskExistsDelay,
       
   146                 TCallBack(TaskExistsCallback, this));
       
   147     	}
       
   148     	else
       
   149 	    {
       
   150             iPeriodic->Start(KTaskNotExistsDelay, KTaskNotExistsDelay,
       
   151                 TCallBack(TaskNotExistsCallback, this));
       
   152     	}
       
   153 		
       
   154 	}
       
   155 }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // From CActive
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CAiScutTargetShutter::DoCancel()
       
   162 {
       
   163     if (iPeriodic)
       
   164     {
       
   165         iPeriodic->Cancel();
       
   166     }
       
   167 }
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // From CActive.
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 void CAiScutTargetShutter::RunL()
       
   174 {
       
   175 	__PRINT( __DBG_FORMAT( "XAI: CAiScutTargetShutter::RunL() app uid=0x%x"), iAppUid.iUid);	
       
   176 
       
   177 	TUid menuUid = KMenuUID3;
       
   178 	
       
   179     TApaTaskList taskList(iWsSession);
       
   180     TApaTask task(taskList.FindApp(iAppUid));
       
   181 
       
   182     if (iTaskExists)
       
   183     {
       
   184         RWindowGroup windowGroup = iEnv->RootWin();
       
   185 
       
   186         if (windowGroup.OrdinalPosition() != 0)
       
   187         {
       
   188             TApaTask idleTask(taskList.FindApp(KScutActiveIdleUid));
       
   189             if (idleTask.Exists())
       
   190             {
       
   191 			__PRINTS( "XAI:   idle to foreground");
       
   192 	
       
   193                 //idleTask.BringToForeground();
       
   194                 CAknSgcClient::MoveApp(idleTask.WgId(), ESgcMoveAppToForeground);
       
   195             }
       
   196         }
       
   197         iPeriodic->Cancel();
       
   198     }
       
   199     else
       
   200     {
       
   201         if (iCounter >= KMaxNumberOfTries || iTaskKilled)
       
   202         {
       
   203             iPeriodic->Cancel();
       
   204         }
       
   205         else if (task.Exists() && iAppUid != menuUid)        
       
   206         {
       
   207 			__PRINTS("XAI:   shutdown task");
       
   208 			//task.SendToBackground();
       
   209 			CAknSgcClient::MoveApp(task.WgId(), ESgcMoveAppToBackground);
       
   210             task.SendSystemEvent(EApaSystemEventShutdown);
       
   211             iTaskKilled = ETrue;
       
   212         }
       
   213         else
       
   214         {
       
   215             iCounter++;
       
   216         }
       
   217     }
       
   218 }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // From CActive
       
   222 // Handles an error situation.
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 TInt CAiScutTargetShutter::RunError(TInt aError)
       
   226 {
       
   227     return aError;
       
   228 }
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 //
       
   232 //
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 void CAiScutTargetShutter::Run()
       
   236 {
       
   237     SetActive();
       
   238     TRequestStatus *status = &iStatus;
       
   239     User::RequestComplete(status, KErrNone);
       
   240 }
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 TInt CAiScutTargetShutter::TaskExistsCallback(TAny* aPtr)
       
   247 {
       
   248     CAiScutTargetShutter* self = static_cast<CAiScutTargetShutter*>(aPtr);
       
   249 
       
   250     if (self)
       
   251     {
       
   252         self->Run();
       
   253     }
       
   254 
       
   255     return KErrNone;
       
   256 }
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 // ---------------------------------------------------------------------------
       
   261 //
       
   262 TInt CAiScutTargetShutter::TaskNotExistsCallback(TAny* aPtr)
       
   263 {
       
   264     CAiScutTargetShutter* self = static_cast<CAiScutTargetShutter*>(aPtr);
       
   265 
       
   266     if (self)
       
   267     {
       
   268         self->Run();
       
   269     }
       
   270 
       
   271     return KErrNone;
       
   272 }
       
   273 
       
   274 // End of File.