26
|
1 |
/*
|
|
2 |
* Copyright (c) 2008-2008 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: Monitors an application exit event.
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include <mpxlog.h> // MPX_DEBUG
|
|
19 |
#include <e32base.h>
|
|
20 |
#include <e32std.h>
|
|
21 |
#include <apacmdln.h>
|
|
22 |
#include <apgtask.h>
|
|
23 |
#include <AknTaskList.h>
|
|
24 |
|
|
25 |
|
|
26 |
#include "applicationmonitor.h"
|
|
27 |
#include "applicationmonitorobserver.h"
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
// ---------------------------------------------------------------------------
|
|
32 |
// Constructor
|
|
33 |
// ---------------------------------------------------------------------------
|
|
34 |
//
|
|
35 |
CApplicationMonitor::CApplicationMonitor( MApplicationMonitorObserver& aObserver)
|
|
36 |
: CActive( EPriorityNormal ),
|
|
37 |
iObserver( aObserver )
|
|
38 |
{
|
|
39 |
}
|
|
40 |
|
|
41 |
// ---------------------------------------------------------------------------
|
|
42 |
// Second Phase Constructor
|
|
43 |
// ---------------------------------------------------------------------------
|
|
44 |
//
|
|
45 |
void CApplicationMonitor::ConstructL()
|
|
46 |
{
|
|
47 |
CActiveScheduler::Add( this );
|
|
48 |
}
|
|
49 |
|
|
50 |
// ---------------------------------------------------------------------------
|
|
51 |
// Two-phased constructor
|
|
52 |
// ---------------------------------------------------------------------------
|
|
53 |
//
|
|
54 |
CApplicationMonitor* CApplicationMonitor::NewL( MApplicationMonitorObserver& aObserver)
|
|
55 |
{
|
|
56 |
MPX_DEBUG1("CApplicationMonitor::NewL <---");
|
|
57 |
CApplicationMonitor* self = new( ELeave ) CApplicationMonitor( aObserver);
|
|
58 |
CleanupStack::PushL( self );
|
|
59 |
self->ConstructL();
|
|
60 |
CleanupStack::Pop( self );
|
|
61 |
MPX_DEBUG1("CApplicationMonitor::NewL --->");
|
|
62 |
return self;
|
|
63 |
}
|
|
64 |
|
|
65 |
|
|
66 |
// ---------------------------------------------------------------------------
|
|
67 |
// Destructor
|
|
68 |
// ---------------------------------------------------------------------------
|
|
69 |
//
|
|
70 |
CApplicationMonitor::~CApplicationMonitor()
|
|
71 |
{
|
|
72 |
MPX_DEBUG1("CApplicationMonitor::~CApplicationMonitor <---");
|
|
73 |
Cancel();
|
|
74 |
iThread.Close();
|
|
75 |
MPX_DEBUG1("CApplicationMonitor::~CApplicationMonitor --->");
|
|
76 |
}
|
|
77 |
|
|
78 |
// ---------------------------------------------------------------------------
|
|
79 |
// Starts monitoring a particular Application
|
|
80 |
// ---------------------------------------------------------------------------
|
|
81 |
//
|
|
82 |
void CApplicationMonitor::StartL( TUid aAppUid , TBool aRootAppIndication )
|
|
83 |
{
|
|
84 |
MPX_DEBUG1("CApplicationMonitor::StartL <---");
|
|
85 |
RWsSession wsSession;
|
|
86 |
User::LeaveIfError( wsSession.Connect() );
|
|
87 |
TUint64 threadId(0);
|
|
88 |
TBool taskExists(EFalse);
|
|
89 |
TInt status = KErrNone;
|
|
90 |
iAppUid = aAppUid;
|
|
91 |
if(aRootAppIndication)
|
|
92 |
{
|
|
93 |
CAknTaskList* taskList = CAknTaskList::NewL( wsSession );
|
|
94 |
TApaTask task = taskList->FindRootApp( aAppUid );
|
|
95 |
delete taskList;
|
|
96 |
taskExists = task.Exists();
|
|
97 |
threadId = task.ThreadId().Id();
|
|
98 |
}
|
|
99 |
else
|
|
100 |
{
|
|
101 |
TApaTaskList taskList( wsSession );
|
|
102 |
TApaTask task = taskList.FindApp( aAppUid );
|
|
103 |
taskExists = task.Exists();
|
|
104 |
threadId = task.ThreadId().Id();
|
|
105 |
}
|
|
106 |
wsSession.Close();
|
|
107 |
|
|
108 |
if (taskExists)
|
|
109 |
{
|
|
110 |
MPX_DEBUG1("CApplicationMonitor::StartL app found");
|
|
111 |
status = iThread.Open(TThreadId(threadId));
|
|
112 |
if ( status == KErrNone )
|
|
113 |
{
|
|
114 |
iThread.Logon(iStatus);
|
|
115 |
SetActive();
|
|
116 |
}
|
|
117 |
}
|
|
118 |
else
|
|
119 |
{
|
|
120 |
MPX_DEBUG1("CApplicationMonitor::StartL not app found");
|
|
121 |
User::Leave(KErrNotFound);
|
|
122 |
}
|
|
123 |
MPX_DEBUG1("CApplicationMonitor::StartL --->");
|
|
124 |
}
|
|
125 |
|
|
126 |
// ---------------------------------------------------------------------------
|
|
127 |
// RunL callback
|
|
128 |
// ---------------------------------------------------------------------------
|
|
129 |
//
|
|
130 |
void CApplicationMonitor::RunL()
|
|
131 |
{
|
|
132 |
MPX_DEBUG1("CApplicationMonitor::RunL <---");
|
|
133 |
switch (iStatus.Int())
|
|
134 |
{
|
|
135 |
case EExitPending:
|
|
136 |
MPX_DEBUG1("CApplicationMonitor::RunL reason = EExitPending");
|
|
137 |
iThread.Close();
|
|
138 |
TRAPD(err, StartL( iAppUid ) );
|
|
139 |
if (KErrNone != err)
|
|
140 |
{
|
|
141 |
MPX_DEBUG1("CApplicationMonitor::RunL reason = EExitPending but thread is really gone");
|
|
142 |
iObserver.HandleApplicationClosedL((TExitType) iStatus.Int());
|
|
143 |
}
|
|
144 |
break;
|
|
145 |
case KErrCancel:
|
|
146 |
MPX_DEBUG1("CApplicationMonitor::RunL cancel");
|
|
147 |
break;
|
|
148 |
default:
|
|
149 |
MPX_DEBUG2("CApplicationMonitor::RunL reason = %d", iStatus.Int());
|
|
150 |
iObserver.HandleApplicationClosedL((TExitType) iStatus.Int());
|
|
151 |
break;
|
|
152 |
}
|
|
153 |
MPX_DEBUG1("CApplicationMonitor::RunL --->");
|
|
154 |
}
|
|
155 |
|
|
156 |
// ---------------------------------------------------------------------------
|
|
157 |
// Handle Cancelling
|
|
158 |
// ---------------------------------------------------------------------------
|
|
159 |
//
|
|
160 |
void CApplicationMonitor::DoCancel()
|
|
161 |
{
|
|
162 |
MPX_DEBUG1("CApplicationMonitor::DoCancel <---");
|
|
163 |
// Stop monitoring
|
|
164 |
iThread.LogonCancel(iStatus);
|
|
165 |
MPX_DEBUG1("CApplicationMonitor::DoCancel --->");
|
|
166 |
}
|
|
167 |
|
|
168 |
// ----------------------------------------------------------------------------
|
|
169 |
// Handles a leave occurring in the request completion event handler RunL()
|
|
170 |
// ----------------------------------------------------------------------------
|
|
171 |
//
|
|
172 |
TInt CApplicationMonitor::RunError(TInt /*aError*/)
|
|
173 |
{
|
|
174 |
MPX_DEBUG1("CApplicationMonitor::RunError <---");
|
|
175 |
MPX_DEBUG1("CApplicationMonitor::RunError --->");
|
|
176 |
return KErrNone;
|
|
177 |
}
|