1 /* |
|
2 * Copyright (c) 2009 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 package com.nokia.mid.ui; |
|
19 |
|
20 import java.io.ByteArrayInputStream; |
|
21 import java.io.IOException; |
|
22 import java.io.OutputStream; |
|
23 |
|
24 import org.eclipse.swt.internal.extension.DisplayExtension; |
|
25 import org.eclipse.swt.widgets.Internal_PackageSupport; |
|
26 |
|
27 import com.nokia.mid.ui.internal.OS; |
|
28 import com.nokia.mid.ui.SoftNotificationException; |
|
29 import com.nokia.mid.ui.SoftNotificationListener; |
|
30 import com.nokia.mj.impl.fileutils.FileUtility; |
|
31 import com.nokia.mj.impl.rt.support.Finalizer; |
|
32 import com.nokia.mj.impl.rt.support.ApplicationInfo; |
|
33 |
|
34 /** |
|
35 * SoftNotificationImpl implements the functionality specified in |
|
36 * com.nokia.mid.ui.SoftNotification class. |
|
37 */ |
|
38 final class SoftNotificationImpl extends com.nokia.mid.ui.SoftNotification { |
|
39 |
|
40 // Native CSoftNotification handle |
|
41 private int iSoftNotificationHandle = 0; |
|
42 |
|
43 // Observer of this class set by the API user. An array is used to allow |
|
44 // listener synchronization. |
|
45 private SoftNotificationListener[] iListener; |
|
46 |
|
47 // Listener event when user accepted notification |
|
48 private final static int EVENT_ACCEPT = 1; |
|
49 |
|
50 // Listener event when user dismissed notification |
|
51 private final static int EVENT_DISMISS = 2; |
|
52 |
|
53 // Error message prefix for exceptions. |
|
54 private final static String ERROR_MESSAGE_PREFIX = "Failed to "; |
|
55 |
|
56 // The class will have native resources so register it for |
|
57 // finalization. |
|
58 private Finalizer mFinalizer; |
|
59 |
|
60 private int err; |
|
61 |
|
62 private int id; |
|
63 |
|
64 private int midletUid; |
|
65 |
|
66 /** |
|
67 * Constructor. New instance with old identifier. |
|
68 * |
|
69 * @param aNotificationId |
|
70 * Identification of previous soft notification. |
|
71 */ |
|
72 public SoftNotificationImpl(int aNotificationId) { |
|
73 initialize(aNotificationId); |
|
74 } |
|
75 |
|
76 /** |
|
77 * Constructor. New instance. |
|
78 */ |
|
79 public SoftNotificationImpl() { |
|
80 initialize(0); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Initializes the instance. |
|
85 * |
|
86 * @param aNotificationId |
|
87 * Id of the soft notification. |
|
88 */ |
|
89 protected void initialize(int aNotificationId) { |
|
90 final int iNotificationId = aNotificationId; |
|
91 iListener = new SoftNotificationListener[1]; |
|
92 |
|
93 Runnable r = new Runnable() { |
|
94 public void run() { |
|
95 String uidString = ApplicationInfo.getInstance().getUid().toString(); |
|
96 // removing "[" and "]" brackets |
|
97 uidString = uidString.substring(1,uidString.length()-1); |
|
98 // converting to decimal |
|
99 midletUid =(int)Long.parseLong(uidString,16); |
|
100 iSoftNotificationHandle = OS.createNativePeer(midletUid, iNotificationId, |
|
101 SoftNotificationImpl.this); |
|
102 } |
|
103 }; |
|
104 // if display is created already, execute it in UI thread no matter |
|
105 // what thread it is called from. Otherwise assume LCDUI application is |
|
106 // called |
|
107 // and create a display. eSWT is not allowed to call before display |
|
108 // creation |
|
109 if (DisplayExtension.getDisplayInstance() != null) { |
|
110 DisplayExtension.getDisplayInstance().syncExec(r); |
|
111 } else { |
|
112 com.nokia.mj.impl.nokialcdui.LCDUIInvoker |
|
113 .eSWTUIThreadRunnerSyncExec(r); |
|
114 } |
|
115 |
|
116 if (iSoftNotificationHandle <= 0) { |
|
117 throw new OutOfMemoryError(); |
|
118 } |
|
119 |
|
120 mFinalizer = ((mFinalizer != null) ? mFinalizer : new Finalizer() { |
|
121 public void finalizeImpl() { |
|
122 close(); |
|
123 } |
|
124 }); |
|
125 } |
|
126 |
|
127 /** |
|
128 * Called when the object is finalized by the garbage collector. |
|
129 */ |
|
130 public void close() { |
|
131 if (DisplayExtension.getDisplayInstance() != null) { |
|
132 Internal_PackageSupport.getDisplayInstance().syncExec(new Runnable() { |
|
133 public void run() { |
|
134 if (mFinalizer != null) { |
|
135 OS.destroy(iSoftNotificationHandle); |
|
136 } |
|
137 mFinalizer = null; |
|
138 } |
|
139 }); |
|
140 } |
|
141 } |
|
142 |
|
143 /** |
|
144 * Notification callback, called from the native side. |
|
145 * |
|
146 * @param aEventArg |
|
147 * Occurred event. |
|
148 */ |
|
149 private void notificationCallback(int aEventArg) { |
|
150 // Synchronize the listener usage since the user may want to set it |
|
151 // to null during execution of this function. |
|
152 synchronized (iListener) { |
|
153 SoftNotificationListener listener = iListener[0]; |
|
154 |
|
155 if (listener != null) { |
|
156 if (aEventArg == EVENT_ACCEPT) { |
|
157 listener.notificationSelected(this); |
|
158 } else if (aEventArg == EVENT_DISMISS) { |
|
159 listener.notificationDismissed(this); |
|
160 } |
|
161 } |
|
162 } |
|
163 } |
|
164 |
|
165 /** |
|
166 * Checks the given error value. Throws SoftNotificationException if the |
|
167 * given error value is other than NativeError.KErrNone. |
|
168 * |
|
169 * @param aError |
|
170 * Error value to be checked. |
|
171 * @param aErrorMessage |
|
172 * Message to be included in the exception. |
|
173 */ |
|
174 private final void checkError(int aError, String aErrorMessage) |
|
175 throws SoftNotificationException { |
|
176 if (aError != 0) { |
|
177 throw new SoftNotificationException(ERROR_MESSAGE_PREFIX |
|
178 + aErrorMessage, aError); |
|
179 } |
|
180 } |
|
181 |
|
182 // Functions from the base class. |
|
183 |
|
184 /** |
|
185 * See class SoftNotification for comments |
|
186 */ |
|
187 public int getId() { |
|
188 if (DisplayExtension.getDisplayInstance() != null) { |
|
189 Internal_PackageSupport.getDisplayInstance().syncExec( |
|
190 new Runnable() { |
|
191 public void run() { |
|
192 id = 0; |
|
193 id = OS.getId(iSoftNotificationHandle); |
|
194 } |
|
195 }); |
|
196 } |
|
197 return id; |
|
198 } |
|
199 |
|
200 /** |
|
201 * See class SoftNotification for comments |
|
202 */ |
|
203 public void post() throws SoftNotificationException { |
|
204 if (DisplayExtension.getDisplayInstance() != null) { |
|
205 Internal_PackageSupport.getDisplayInstance().syncExec( |
|
206 new Runnable() { |
|
207 public void run() { |
|
208 err = 0; |
|
209 err = OS |
|
210 .showSoftNotification(iSoftNotificationHandle); |
|
211 } |
|
212 }); |
|
213 } |
|
214 checkError(err, "add soft notification"); |
|
215 } |
|
216 |
|
217 /** |
|
218 * See class SoftNotification for comments |
|
219 */ |
|
220 public void remove() throws SoftNotificationException { |
|
221 if (DisplayExtension.getDisplayInstance() != null) { |
|
222 Internal_PackageSupport.getDisplayInstance().syncExec( |
|
223 new Runnable() { |
|
224 public void run() { |
|
225 err = 0; |
|
226 err = OS |
|
227 .removeSoftNotification(iSoftNotificationHandle); |
|
228 } |
|
229 }); |
|
230 } |
|
231 checkError(err, "remove notification"); |
|
232 } |
|
233 |
|
234 /** |
|
235 * See class SoftNotification for comments |
|
236 */ |
|
237 public void setListener(SoftNotificationListener aListener) { |
|
238 // Synchronize the listener setting since the user may set it |
|
239 // to null during it is used elsewhere in this class. |
|
240 synchronized (iListener) { |
|
241 iListener[0] = aListener; |
|
242 } |
|
243 } |
|
244 |
|
245 /** |
|
246 * See class SoftNotification for comments |
|
247 */ |
|
248 public void setText(String aPrimaryText, String aSecondaryText) |
|
249 throws SoftNotificationException { |
|
250 final String iPrimaryText = aPrimaryText; |
|
251 final String iSecondaryText = aSecondaryText; |
|
252 |
|
253 if (DisplayExtension.getDisplayInstance() != null) { |
|
254 Internal_PackageSupport.getDisplayInstance().syncExec( |
|
255 new Runnable() { |
|
256 public void run() { |
|
257 err = 0; |
|
258 err = OS.setText(iSoftNotificationHandle, |
|
259 iPrimaryText == null ? "" : iPrimaryText, |
|
260 iSecondaryText == null ? "" |
|
261 : iSecondaryText); |
|
262 } |
|
263 }); |
|
264 } |
|
265 checkError(err, "set note text"); |
|
266 } |
|
267 |
|
268 /** |
|
269 * See class SoftNotification for comments |
|
270 */ |
|
271 public void setSoftkeyLabels(String aSoftkey1Label, String aSoftkey2Label) |
|
272 throws SoftNotificationException { |
|
273 int err = 0; |
|
274 checkError(err, "set softkeys"); |
|
275 } |
|
276 |
|
277 private String stringReplace(String source, String searchStr, |
|
278 String replacementStr) { |
|
279 StringBuffer strBuffer = new StringBuffer(); |
|
280 int pos = source.indexOf(searchStr); |
|
281 |
|
282 while (pos != -1) { |
|
283 strBuffer.append(source.substring(0, pos)).append(replacementStr); |
|
284 source = source.substring(pos + searchStr.length()); |
|
285 pos = source.indexOf(searchStr); |
|
286 } |
|
287 strBuffer.append(source); |
|
288 return strBuffer.toString(); |
|
289 } |
|
290 |
|
291 /** |
|
292 * See class SoftNotification for comments |
|
293 */ |
|
294 public void setImage(byte[] aImageData) throws SoftNotificationException { |
|
295 if (aImageData != null) { |
|
296 final byte[] iImageData = aImageData; |
|
297 if (DisplayExtension.getDisplayInstance() != null) { |
|
298 Internal_PackageSupport.getDisplayInstance().syncExec( |
|
299 new Runnable() { |
|
300 public void run() { |
|
301 err = 0; |
|
302 String imagePath = ""; |
|
303 try { |
|
304 String directoryPath = ApplicationInfo |
|
305 .getInstance().getRootPath(); |
|
306 // Replace "\private\102033e6\apps" with |
|
307 // "resource\apps\java" |
|
308 directoryPath = stringReplace( |
|
309 directoryPath, "private", "public"); |
|
310 directoryPath = directoryPath |
|
311 + "softnotification\\" |
|
312 + midletUid + "_" |
|
313 + getId(); |
|
314 String imageName = midletUid |
|
315 + "_" + getId(); |
|
316 FileUtility target = new FileUtility( |
|
317 directoryPath); |
|
318 if (!target.exists()) { |
|
319 target.mkdirs(); |
|
320 } |
|
321 target = new FileUtility(directoryPath |
|
322 + "\\" + imageName); |
|
323 if (!target.exists()) { |
|
324 target.createNewFile(); |
|
325 } |
|
326 OutputStream fos = target |
|
327 .openOutputStream(); |
|
328 ByteArrayInputStream in = new ByteArrayInputStream( |
|
329 iImageData); |
|
330 byte[] buf = new byte[1024]; |
|
331 int len; |
|
332 while ((len = in.read(buf)) > 0) { |
|
333 fos.write(buf, 0, len); |
|
334 } |
|
335 in.close(); |
|
336 fos.close(); |
|
337 target = null; |
|
338 imagePath = directoryPath + "\\" |
|
339 + imageName; |
|
340 } catch (IOException ex) { |
|
341 ex.printStackTrace(); |
|
342 } catch (Throwable t) { |
|
343 t.printStackTrace(); |
|
344 } |
|
345 err = OS.setImagePath(iSoftNotificationHandle, |
|
346 imagePath); |
|
347 } |
|
348 }); |
|
349 } |
|
350 } |
|
351 checkError(err, "set image"); |
|
352 } |
|
353 } |
|