|
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 if(aRootAppIndication) |
|
91 { |
|
92 CAknTaskList* taskList = CAknTaskList::NewL( wsSession ); |
|
93 TApaTask task = taskList->FindRootApp( aAppUid ); |
|
94 delete taskList; |
|
95 taskExists = task.Exists(); |
|
96 threadId = task.ThreadId().Id(); |
|
97 } |
|
98 else |
|
99 { |
|
100 TApaTaskList taskList( wsSession ); |
|
101 TApaTask task = taskList.FindApp( aAppUid ); |
|
102 taskExists = task.Exists(); |
|
103 threadId = task.ThreadId().Id(); |
|
104 } |
|
105 wsSession.Close(); |
|
106 |
|
107 if (taskExists) |
|
108 { |
|
109 MPX_DEBUG1("CApplicationMonitor::StartL app found"); |
|
110 status = iThread.Open(TThreadId(threadId)); |
|
111 if ( status == KErrNone ) |
|
112 { |
|
113 iThread.Logon(iStatus); |
|
114 SetActive(); |
|
115 } |
|
116 } |
|
117 else |
|
118 { |
|
119 MPX_DEBUG1("CApplicationMonitor::StartL not app found"); |
|
120 User::Leave(KErrNotFound); |
|
121 } |
|
122 MPX_DEBUG1("CApplicationMonitor::StartL --->"); |
|
123 } |
|
124 |
|
125 // --------------------------------------------------------------------------- |
|
126 // RunL callback |
|
127 // --------------------------------------------------------------------------- |
|
128 // |
|
129 void CApplicationMonitor::RunL() |
|
130 { |
|
131 MPX_DEBUG1("CApplicationMonitor::RunL <---"); |
|
132 switch (iStatus.Int()) |
|
133 { |
|
134 case EExitKill: |
|
135 case EExitTerminate: |
|
136 case EExitPanic: |
|
137 MPX_DEBUG1("CApplicationMonitor::RunL kill\terminate\panic"); |
|
138 iObserver.HandleApplicationClosedL((TExitType) iStatus.Int()); |
|
139 break; |
|
140 case KErrCancel: |
|
141 case KErrNoMemory: |
|
142 MPX_DEBUG1("CApplicationMonitor::RunL cancel\memory"); |
|
143 break; |
|
144 case EExitPending: |
|
145 default: |
|
146 MPX_DEBUG1("CApplicationMonitor::RunL exitpendin\default"); |
|
147 // Listen again |
|
148 iThread.Logon(iStatus); |
|
149 SetActive(); |
|
150 break; |
|
151 } |
|
152 MPX_DEBUG1("CApplicationMonitor::RunL --->"); |
|
153 } |
|
154 |
|
155 // --------------------------------------------------------------------------- |
|
156 // Handle Cancelling |
|
157 // --------------------------------------------------------------------------- |
|
158 // |
|
159 void CApplicationMonitor::DoCancel() |
|
160 { |
|
161 MPX_DEBUG1("CApplicationMonitor::DoCancel <---"); |
|
162 // Stop monitoring |
|
163 iThread.LogonCancel(iStatus); |
|
164 MPX_DEBUG1("CApplicationMonitor::DoCancel --->"); |
|
165 } |
|
166 |
|
167 // ---------------------------------------------------------------------------- |
|
168 // Handles a leave occurring in the request completion event handler RunL() |
|
169 // ---------------------------------------------------------------------------- |
|
170 // |
|
171 TInt CApplicationMonitor::RunError(TInt /*aError*/) |
|
172 { |
|
173 MPX_DEBUG1("CApplicationMonitor::RunError <---"); |
|
174 // Listen again |
|
175 iThread.Logon(iStatus); |
|
176 SetActive(); |
|
177 MPX_DEBUG1("CApplicationMonitor::RunError --->"); |
|
178 return KErrNone; |
|
179 } |