|
1 /* |
|
2 * Copyright (c) 2005 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.microedition.m2g; |
|
19 |
|
20 import java.lang.ref.WeakReference; |
|
21 //import com.nokia.mj.impl.rt.legacy.MIDEventServer; |
|
22 import java.util.Hashtable; |
|
23 //import com.nokia.mj.impl.rt.legacy.MemoryUtil; |
|
24 import java.util.Enumeration; |
|
25 import org.eclipse.swt.events.DisposeListener; |
|
26 import org.eclipse.swt.events.DisposeEvent; |
|
27 import org.eclipse.swt.SWT; |
|
28 import org.eclipse.swt.widgets.*; |
|
29 |
|
30 |
|
31 public final class M2GManager implements Listener |
|
32 { |
|
33 //-------------------------------------------------- |
|
34 // STATIC VARIABLES |
|
35 //-------------------------------------------------- |
|
36 |
|
37 private static WeakReference sWeakManagerProxy = null; |
|
38 static Object sQuard = new Object(); |
|
39 |
|
40 //-------------------------------------------------- |
|
41 // VARIABLES |
|
42 //-------------------------------------------------- |
|
43 private Hashtable iLiveObjects = new Hashtable(); |
|
44 private M2GWeakManager iWeakManager = null; |
|
45 private int iSVGProxyHandle = M2GObject.INVALID_NATIVE_HANDLE; |
|
46 |
|
47 //-------------------------------------------------- |
|
48 // METHODS |
|
49 //-------------------------------------------------- |
|
50 |
|
51 /** |
|
52 * Constructor |
|
53 */ |
|
54 private M2GManager() |
|
55 { |
|
56 super(); |
|
57 |
|
58 |
|
59 //As Display is created in Thread only.. So no need to handle display over here. |
|
60 //scom.nokia.mj.impl.rt.support.Jvm.loadSystemLibrary("javam2g"); |
|
61 /*// setup the finalization via eswt's Display |
|
62 Display display = Display.getCurrent(); |
|
63 if (display == null) |
|
64 { |
|
65 return; // ? |
|
66 } |
|
67 display.addListener(SWT.Dispose, (Listener)this); */ |
|
68 |
|
69 |
|
70 // Execute in UI thread |
|
71 Platform.executeInUIThread( |
|
72 new M2GRunnableQt() { |
|
73 public void doRun() { |
|
74 iSVGProxyHandle = _createSvgProxy(); |
|
75 } |
|
76 }); |
|
77 M2GManager.heuristicGC(); |
|
78 } |
|
79 |
|
80 /** |
|
81 * Impelements 'handleEvent' from Listener |
|
82 */ |
|
83 public void handleEvent(Event e) |
|
84 { |
|
85 if (e.type == SWT.Dispose) |
|
86 { |
|
87 doCleanup(); |
|
88 } |
|
89 } |
|
90 |
|
91 /** |
|
92 * Does the cleanuping |
|
93 */ |
|
94 protected void doCleanup() |
|
95 { |
|
96 finalizeObjects(); |
|
97 synchronized (sQuard) |
|
98 { |
|
99 _deleteSvgProxy(iSVGProxyHandle); |
|
100 iSVGProxyHandle = M2GObject.INVALID_NATIVE_HANDLE; |
|
101 sWeakManagerProxy = null; |
|
102 iLiveObjects = null; |
|
103 } |
|
104 } |
|
105 |
|
106 |
|
107 /** |
|
108 * Find an element from the handle-to-element map. |
|
109 * @param aElementHandle Handle |
|
110 * @return SVGElement. Null if not found |
|
111 */ |
|
112 synchronized private void finalizeObjects() |
|
113 { |
|
114 Enumeration objects = iLiveObjects.elements(); |
|
115 while (objects.hasMoreElements()) |
|
116 { |
|
117 Object weakObject = ((WeakReference)objects.nextElement()).get(); |
|
118 if (weakObject != null) |
|
119 { |
|
120 ((M2GObject)weakObject).registeredFinalize(); |
|
121 } |
|
122 } |
|
123 // Clear table |
|
124 iLiveObjects.clear(); |
|
125 } |
|
126 |
|
127 |
|
128 /** |
|
129 * Gets native SVG proxy handle |
|
130 * @return SVG proxy handle |
|
131 */ |
|
132 public int getSVGProxyHandle() |
|
133 { |
|
134 synchronized (sQuard) |
|
135 { |
|
136 return iSVGProxyHandle; |
|
137 } |
|
138 } |
|
139 |
|
140 /** |
|
141 * Gets native SVG proxy handle |
|
142 * @return SVG proxy handle |
|
143 */ |
|
144 void init(M2GWeakManager aWeakManager) |
|
145 { |
|
146 iWeakManager = aWeakManager; |
|
147 } |
|
148 |
|
149 /** |
|
150 * Register an object |
|
151 * @param aObject Object |
|
152 */ |
|
153 synchronized public void register(M2GObject aObject) |
|
154 { |
|
155 if (aObject == null) |
|
156 { |
|
157 return; |
|
158 } |
|
159 iLiveObjects.put(new Integer(aObject.getHandle()), new WeakReference(aObject)); |
|
160 } |
|
161 |
|
162 /** |
|
163 * Unregister an object |
|
164 * @param aHandle Object's native handle |
|
165 */ |
|
166 synchronized public void unregister(Integer aHandle) |
|
167 { |
|
168 iLiveObjects.remove(aHandle); |
|
169 } |
|
170 |
|
171 //-------------------------------------------------- |
|
172 // STATIC METHODS |
|
173 //-------------------------------------------------- |
|
174 |
|
175 /** |
|
176 * Gets instance of singleton manager object |
|
177 * @return manager object reference |
|
178 */ |
|
179 static public M2GManager getInstance() |
|
180 { |
|
181 M2GWeakManager weakManager = null; |
|
182 synchronized (sQuard) |
|
183 { |
|
184 if (sWeakManagerProxy != null) |
|
185 { |
|
186 |
|
187 weakManager = (M2GWeakManager)sWeakManagerProxy.get(); |
|
188 } |
|
189 // Check if object null |
|
190 if (weakManager == null) |
|
191 { |
|
192 // Create a new object and put it into the static member variable |
|
193 |
|
194 weakManager = new M2GWeakManager(new M2GManager()); |
|
195 sWeakManagerProxy = new WeakReference(weakManager); |
|
196 } |
|
197 return weakManager.getInstance(); |
|
198 } |
|
199 } |
|
200 |
|
201 /** |
|
202 * Trigger GC every N objects |
|
203 */ |
|
204 static final void heuristicGC() |
|
205 { |
|
206 //MemoryUtil.ensureMinFreeRAM(); |
|
207 } |
|
208 |
|
209 //-------------------------------------------------- |
|
210 // NATIVE METHODS |
|
211 //-------------------------------------------------- |
|
212 private static native int _createSvgEngine(int aSvgProxyHandle ); |
|
213 |
|
214 private static native int _createSvgProxy(); |
|
215 |
|
216 private static native void _deleteSvgEngine(int aSvgProxyHandle, int aSvgEngineHandle); |
|
217 |
|
218 private static native void _deleteSvgProxy( int aSvgProxyHandle); |
|
219 |
|
220 |
|
221 } |
|
222 |
|
223 //-------------------------------------------------- |
|
224 // OTHER CLASSES |
|
225 //-------------------------------------------------- |
|
226 |
|
227 /* |
|
228 * M2GWeakManager takes care of carbage colletion of an M2GManager object |
|
229 */ |
|
230 class M2GWeakManager |
|
231 { |
|
232 //-------------------------------------------------- |
|
233 // VARIABLES |
|
234 //-------------------------------------------------- |
|
235 M2GManager iManager; |
|
236 |
|
237 //-------------------------------------------------- |
|
238 // METHODS |
|
239 //-------------------------------------------------- |
|
240 |
|
241 /** |
|
242 * Constructor |
|
243 * @param aManager Manager object |
|
244 */ |
|
245 public M2GWeakManager(M2GManager aManager) |
|
246 { |
|
247 reset(aManager); |
|
248 } |
|
249 |
|
250 /** |
|
251 * Return a reference to the manager |
|
252 * @return a reference to the manager |
|
253 */ |
|
254 M2GManager getInstance() |
|
255 { |
|
256 return iManager; |
|
257 } |
|
258 |
|
259 /** |
|
260 * Reset |
|
261 * @param aManager |
|
262 */ |
|
263 void reset(M2GManager aManager) |
|
264 { |
|
265 iManager = aManager; |
|
266 if (iManager != null) |
|
267 { |
|
268 iManager.init(this); |
|
269 } |
|
270 } |
|
271 } |