|
1 /* |
|
2 * ============================================================================ |
|
3 * Name : btnotificationmanager.cpp |
|
4 * Part of : bluetoothengine / btnotif |
|
5 * Description : Class for managing user notification and query objects, and for serializing access to the notification server. |
|
6 * |
|
7 * Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
8 * All rights reserved. |
|
9 * This component and the accompanying materials are made available |
|
10 * under the terms of "Eclipse Public License v1.0" |
|
11 * which accompanies this distribution, and is available |
|
12 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
13 * |
|
14 * Initial Contributors: |
|
15 * Nokia Corporation - initial contribution. |
|
16 * |
|
17 * Contributors: |
|
18 * Nokia Corporation |
|
19 * ============================================================================ |
|
20 * Template version: 4.1 |
|
21 */ |
|
22 |
|
23 #include "btnotificationmanager.h" |
|
24 #include "btnotifserver.h" |
|
25 |
|
26 #include "bluetoothnotification.h" |
|
27 |
|
28 |
|
29 // ======== MEMBER FUNCTIONS ======== |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // C++ default constructor |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 CBTNotificationManager::CBTNotificationManager( const CBTNotifServer* aServer ) |
|
36 : iServer( aServer ) |
|
37 { |
|
38 } |
|
39 |
|
40 |
|
41 // --------------------------------------------------------------------------- |
|
42 // Symbian 2nd-phase constructor |
|
43 // --------------------------------------------------------------------------- |
|
44 // |
|
45 void CBTNotificationManager::ConstructL() |
|
46 { |
|
47 iAsyncCb = new( ELeave ) CAsyncCallBack( iServer->Priority() ); |
|
48 TCallBack cb( AsyncCallback, this ); |
|
49 iAsyncCb->Set( cb ); |
|
50 } |
|
51 |
|
52 |
|
53 // --------------------------------------------------------------------------- |
|
54 // NewL. |
|
55 // --------------------------------------------------------------------------- |
|
56 // |
|
57 CBTNotificationManager* CBTNotificationManager::NewL( const CBTNotifServer* aServer ) |
|
58 { |
|
59 CBTNotificationManager* self = new( ELeave ) CBTNotificationManager( aServer ); |
|
60 CleanupStack::PushL( self ); |
|
61 self->ConstructL(); |
|
62 CleanupStack::Pop( self ); |
|
63 return self; |
|
64 } |
|
65 |
|
66 |
|
67 // --------------------------------------------------------------------------- |
|
68 // Destructor |
|
69 // --------------------------------------------------------------------------- |
|
70 // |
|
71 CBTNotificationManager::~CBTNotificationManager() |
|
72 { |
|
73 iNotificationQ.ResetAndDestroy(); |
|
74 iNotificationQ.Close(); |
|
75 iUnusedQ.ResetAndDestroy(); |
|
76 iUnusedQ.Close(); |
|
77 delete iAsyncCb; |
|
78 } |
|
79 |
|
80 |
|
81 // --------------------------------------------------------------------------- |
|
82 // Get a new notification |
|
83 // --------------------------------------------------------------------------- |
|
84 // |
|
85 CBluetoothNotification* CBTNotificationManager::GetNotification() |
|
86 { |
|
87 CBluetoothNotification* notification = NULL; |
|
88 if( iUnusedQ.Count() ) |
|
89 { |
|
90 // Re-use the first idle notification. |
|
91 notification = iUnusedQ[0]; |
|
92 iUnusedQ.Remove( 0 ); |
|
93 } |
|
94 else |
|
95 { |
|
96 TRAP_IGNORE( notification = CBluetoothNotification::NewL( this ) ); |
|
97 } |
|
98 if( notification ) |
|
99 { |
|
100 if( iNotificationQ.Append( notification ) ) |
|
101 { |
|
102 // In case the appending fails, we just delete the notification. |
|
103 // Otherwise we cannot keep track of it anymore. |
|
104 delete notification; |
|
105 notification = NULL; |
|
106 } |
|
107 } |
|
108 return notification; |
|
109 } |
|
110 |
|
111 |
|
112 // --------------------------------------------------------------------------- |
|
113 // Release the notification |
|
114 // --------------------------------------------------------------------------- |
|
115 // |
|
116 void CBTNotificationManager::ReleaseNotification( CBluetoothNotification* aNotification ) |
|
117 { |
|
118 __ASSERT_ALWAYS( aNotification, PanicServer( EBTNotifPanicBadArgument ) ); |
|
119 TInt pos = iNotificationQ.Find( aNotification ); |
|
120 __ASSERT_ALWAYS( pos > KErrNotFound, PanicServer( EBTNotifPanicMissing ) ); |
|
121 // ToDo: Cancel outstanding notification! |
|
122 iNotificationQ.Remove( pos ); |
|
123 TInt err = iUnusedQ.Append( aNotification ); |
|
124 aNotification->Reset(); // Clean up notification's resources |
|
125 if( err ) |
|
126 { |
|
127 // Just delete the notification. |
|
128 delete aNotification; |
|
129 } |
|
130 if( !iAsyncCb->IsActive() ) |
|
131 { |
|
132 if( !iNotificationQ.Count() ) |
|
133 { |
|
134 // Set the priority so that this is the last scheduled active object to execute. |
|
135 iAsyncCb->SetPriority( CActive::EPriorityIdle ); |
|
136 } |
|
137 iAsyncCb->CallBack(); |
|
138 } |
|
139 } |
|
140 |
|
141 |
|
142 // --------------------------------------------------------------------------- |
|
143 // Queue the notification with given priority |
|
144 // --------------------------------------------------------------------------- |
|
145 // |
|
146 TInt CBTNotificationManager::QueueNotification( CBluetoothNotification* aNotification, |
|
147 TNotificationPriority aPriority ) |
|
148 { |
|
149 TInt pos = iNotificationQ.Find( aNotification ); |
|
150 __ASSERT_ALWAYS( pos > KErrNotFound, PanicServer( EBTNotifPanicMissing ) ); |
|
151 if( aPriority == EPriorityHigh && pos != 0 ) |
|
152 { |
|
153 // ToDo: Move the note to the front of the queue |
|
154 } |
|
155 if( !iAsyncCb->IsActive() ) |
|
156 { |
|
157 if( iAsyncCb->Priority() != iServer->Priority() ) |
|
158 { |
|
159 // Reset priority back to original value |
|
160 // We first check the current priority, otherwise CActive will do an |
|
161 // unnecessary removal and adding of the callback from the active scheduler. |
|
162 iAsyncCb->SetPriority( iServer->Priority() ); |
|
163 } |
|
164 iAsyncCb->CallBack(); |
|
165 } |
|
166 return KErrNone; |
|
167 } |
|
168 |
|
169 |
|
170 // --------------------------------------------------------------------------- |
|
171 // Process the notification queue and launch the next notification. |
|
172 // --------------------------------------------------------------------------- |
|
173 // |
|
174 void CBTNotificationManager::ProcessNotificationQueueL() |
|
175 { |
|
176 if( iNotificationQ.Count() ) |
|
177 { |
|
178 TInt err = iNotificationQ[0]->Show(); |
|
179 // If the note is already showing, it will return KErrAlreadyExists |
|
180 (void) err; // ToDo: add error handling!! |
|
181 NOTIF_NOTHANDLED( !err || err == KErrAlreadyExists || err == KErrNotFound ) |
|
182 } |
|
183 else |
|
184 { |
|
185 // No outstanding notifications, and unused notifications. |
|
186 // Clean up the unused notifications. |
|
187 iUnusedQ.ResetAndDestroy(); |
|
188 iNotificationQ.Reset(); // the queue is empty, reset it. |
|
189 // Also clean up any resources. |
|
190 } |
|
191 } |
|
192 |
|
193 |
|
194 // --------------------------------------------------------------------------- |
|
195 // Callback for asynchronous processing of queued notification requests. |
|
196 // --------------------------------------------------------------------------- |
|
197 // |
|
198 TInt CBTNotificationManager::AsyncCallback( TAny* aPtr ) |
|
199 { |
|
200 TRAPD( err, ( (CBTNotificationManager*) aPtr )->ProcessNotificationQueueL() ); |
|
201 return err; |
|
202 } |