|
1 /* |
|
2 * Copyright (c) 2002 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: observe the idle event of system |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <e32base.h> |
|
22 #include <e32std.h> |
|
23 #include <f32file.h> |
|
24 #include <ActiveIdle2DomainPSKeys.h> |
|
25 #include <e32property.h> //for RProperty |
|
26 #include "IdleObserver.h" |
|
27 #include "DRMHelperServer.h" |
|
28 |
|
29 // EXTERNAL DATA STRUCTURES |
|
30 // EXTERNAL FUNCTION PROTOTYPES |
|
31 // CONSTANTS |
|
32 // MACROS |
|
33 // LOCAL CONSTANTS AND MACROS |
|
34 |
|
35 |
|
36 #ifdef _DRM_TESTING |
|
37 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName ) |
|
38 { |
|
39 RFile file; |
|
40 TInt size; |
|
41 User::LeaveIfError( file.Open( aFs, aName , EFileWrite ) ); |
|
42 CleanupClosePushL( file ); |
|
43 User::LeaveIfError( file.Size( size ) ); |
|
44 User::LeaveIfError( file.Write( size, text ) ); |
|
45 CleanupStack::PopAndDestroy(&file); //file |
|
46 } |
|
47 |
|
48 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs ) |
|
49 { |
|
50 _LIT( KLogFile , "c:\\IOLog.txt" ); |
|
51 WriteFileL( text , aFs , KLogFile ); |
|
52 } |
|
53 |
|
54 LOCAL_C void CreateLogL() |
|
55 { |
|
56 RFs fs; |
|
57 User::LeaveIfError(fs.Connect()); |
|
58 CleanupClosePushL(fs); |
|
59 RFile file; |
|
60 User::LeaveIfError( file.Replace( fs , _L("c:\\IOLog.txt") , EFileWrite ) ); |
|
61 file.Close(); |
|
62 CleanupStack::PopAndDestroy(&fs); //fs |
|
63 } |
|
64 |
|
65 LOCAL_C void WriteL( const TDesC& aText ) |
|
66 { |
|
67 RFs fs; |
|
68 User::LeaveIfError( fs.Connect() ); |
|
69 CleanupClosePushL(fs); |
|
70 HBufC8* text = HBufC8::NewLC(1000); |
|
71 TPtr8 textptr(text->Des() ); |
|
72 textptr.Append( aText ); |
|
73 textptr.Append(_L( "\r\n" )); |
|
74 WriteLogL(textptr , fs); |
|
75 CleanupStack::PopAndDestroy(text); |
|
76 CleanupStack::PopAndDestroy(&fs); //fs |
|
77 } |
|
78 |
|
79 LOCAL_C void WriteCurrentTimeL() |
|
80 { |
|
81 RFs fs; |
|
82 User::LeaveIfError( fs.Connect() ); |
|
83 CleanupClosePushL(fs); |
|
84 HBufC8* text = HBufC8::NewLC(100); |
|
85 TPtr8 textptr(text->Des() ); |
|
86 // Date and Time display |
|
87 TTime time; |
|
88 time.HomeTime(); |
|
89 TBuf<256> dateString; |
|
90 _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3"); |
|
91 time.FormatL(dateString,KDate); |
|
92 textptr.Append(_L( "\r\n\t\tData:\t" ) ); |
|
93 textptr.Append( dateString ); |
|
94 _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B"); |
|
95 time.FormatL(dateString,KTime); |
|
96 textptr.Append(_L( "\r\n\t\tTime:\t" ) ); |
|
97 textptr.Append( dateString ); |
|
98 textptr.Append(_L( "\r\n" ) ); |
|
99 textptr.Append(_L( "\r\n" ) ); |
|
100 WriteLogL(textptr , fs); |
|
101 CleanupStack::PopAndDestroy(text); |
|
102 CleanupStack::PopAndDestroy(&fs); //fs |
|
103 } |
|
104 #endif |
|
105 // MODULE DATA STRUCTURES |
|
106 // LOCAL FUNCTION PROTOTYPES |
|
107 // FORWARD DECLARATIONS |
|
108 |
|
109 // ============================= LOCAL FUNCTIONS =============================== |
|
110 |
|
111 |
|
112 // ============================ MEMBER FUNCTIONS =============================== |
|
113 |
|
114 // ----------------------------------------------------------------------------- |
|
115 // CIdleObserver::CIdleObserver |
|
116 // C++ default constructor can NOT contain any code, that |
|
117 // might leave. |
|
118 // ----------------------------------------------------------------------------- |
|
119 // |
|
120 CIdleObserver::CIdleObserver( CDRMHelperServer& aServer ): |
|
121 CActive(CActive::EPriorityStandard), |
|
122 iServer( aServer ) |
|
123 { |
|
124 CActiveScheduler::Add( this ); |
|
125 } |
|
126 |
|
127 // ----------------------------------------------------------------------------- |
|
128 // CIdleObserver::ConstructL |
|
129 // Symbian 2nd phase constructor can leave. |
|
130 // ----------------------------------------------------------------------------- |
|
131 // |
|
132 void CIdleObserver::ConstructL() |
|
133 { |
|
134 #ifdef _DRM_TESTING |
|
135 CreateLogL(); //test |
|
136 #endif |
|
137 User::LeaveIfError( iProperty.Attach( KPSUidAiInformation, KActiveIdleState ) ); |
|
138 } |
|
139 |
|
140 // ----------------------------------------------------------------------------- |
|
141 // CIdleObserver::NewL |
|
142 // Two-phased constructor. |
|
143 // ----------------------------------------------------------------------------- |
|
144 // |
|
145 CIdleObserver* CIdleObserver::NewL(CDRMHelperServer& aServer) |
|
146 { |
|
147 CIdleObserver* self = new( ELeave ) CIdleObserver(aServer); |
|
148 |
|
149 CleanupStack::PushL( self ); |
|
150 self->ConstructL(); |
|
151 CleanupStack::Pop(self); |
|
152 |
|
153 return self; |
|
154 } |
|
155 |
|
156 |
|
157 // Destructor |
|
158 CIdleObserver::~CIdleObserver() |
|
159 { |
|
160 #ifdef _DRM_TESTING |
|
161 TRAPD( err , WriteL(_L("CIdleObserver-Destruct")) ); |
|
162 TRAP( err , WriteCurrentTimeL() ); |
|
163 #endif |
|
164 Cancel(); |
|
165 iProperty.Close(); |
|
166 } |
|
167 |
|
168 |
|
169 // ----------------------------------------------------------------------------- |
|
170 // CIdleObserver::StartL |
|
171 // Start the system agent to listen to the event |
|
172 // (other items were commented in a header). |
|
173 // ----------------------------------------------------------------------------- |
|
174 // |
|
175 void CIdleObserver::StartL() |
|
176 { |
|
177 #ifdef _DRM_TESTING |
|
178 WriteL(_L("Start")); |
|
179 WriteCurrentTimeL(); |
|
180 #endif |
|
181 Cancel(); |
|
182 iProperty.Subscribe( iStatus ); |
|
183 SetActive(); |
|
184 } |
|
185 |
|
186 |
|
187 // ----------------------------------------------------------------------------- |
|
188 // CIdleObserver::RunL |
|
189 // from CActive |
|
190 // (other items were commented in a header). |
|
191 // ----------------------------------------------------------------------------- |
|
192 // |
|
193 void CIdleObserver::RunL() |
|
194 { |
|
195 #ifdef _DRM_TESTING |
|
196 //test code start |
|
197 WriteL(_L("RunL")); |
|
198 WriteCurrentTimeL(); |
|
199 //test code end |
|
200 #endif |
|
201 |
|
202 // Resubscribe before processing new value to prevent missing updates |
|
203 TInt idleStauts = 0; |
|
204 TInt err( iStatus.Int() ); |
|
205 if (err == KErrNone) |
|
206 { |
|
207 StartL(); |
|
208 User::LeaveIfError( iProperty.Get( |
|
209 KPSUidAiInformation, |
|
210 KActiveIdleState, idleStauts ) ); |
|
211 if ( idleStauts == EPSAiForeground ) |
|
212 { |
|
213 iServer.HandleIdleL(); |
|
214 } |
|
215 } |
|
216 else if ( err != KErrCancel ) |
|
217 { |
|
218 StartL(); |
|
219 } |
|
220 } |
|
221 |
|
222 // ----------------------------------------------------------------------------- |
|
223 // CIdleObserver::DoCancel |
|
224 // From CActive |
|
225 // (other items were commented in a header). |
|
226 // ----------------------------------------------------------------------------- |
|
227 // |
|
228 void CIdleObserver::DoCancel() |
|
229 { |
|
230 #ifdef _DRM_TESTING |
|
231 TRAPD( err , WriteL(_L("DoCancel")) ); |
|
232 TRAP( err , WriteCurrentTimeL() ); |
|
233 #endif |
|
234 iProperty.Cancel(); |
|
235 } |
|
236 |
|
237 // ========================== OTHER EXPORTED FUNCTIONS ========================= |
|
238 // End of File |