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