19 |
19 |
|
20 |
20 |
package org.distorted.library.main;
|
21 |
21 |
|
|
22 |
import java.util.ArrayList;
|
22 |
23 |
import java.util.HashMap;
|
23 |
24 |
import java.util.LinkedList;
|
24 |
25 |
|
... | ... | |
47 |
48 |
private static final int JOB_CREATE = 0;
|
48 |
49 |
private static final int JOB_DELETE = 1;
|
49 |
50 |
|
|
51 |
private final static Object mLock = new Object();
|
|
52 |
|
|
53 |
private long mID;
|
|
54 |
private int mType;
|
|
55 |
|
|
56 |
/////////////////////////////////////////////////////////////////
|
|
57 |
|
50 |
58 |
private static class Job
|
51 |
59 |
{
|
52 |
60 |
InternalObject object;
|
... | ... | |
59 |
67 |
}
|
60 |
68 |
}
|
61 |
69 |
|
|
70 |
/////////////////////////////////////////////////////////////////
|
|
71 |
|
62 |
72 |
private static boolean mToDo = false;
|
63 |
|
private static LinkedList<InternalObject> mDoneList = new LinkedList<>();
|
64 |
|
private static HashMap<Long,Job> mToDoMap = new HashMap<>();
|
65 |
|
private static long mNextClientID = 0;
|
66 |
|
private static long mNextSystemID = 0;
|
|
73 |
private static StackFrame mCurrentFrame = null;
|
|
74 |
private static ArrayList<StackFrame> mFrameList = new ArrayList<>();
|
67 |
75 |
|
68 |
|
private final static Object mDoneLock = new Object();
|
69 |
|
private final static Object mToDoLock = new Object();
|
|
76 |
/////////////////////////////////////////////////////////////////
|
70 |
77 |
|
71 |
|
private long mID;
|
72 |
|
private int mType;
|
|
78 |
private static class StackFrame
|
|
79 |
{
|
|
80 |
private LinkedList<InternalObject> mDoneList;
|
|
81 |
private HashMap<Long,Job> mToDoMap;
|
|
82 |
private long mNextClientID;
|
|
83 |
private long mNextSystemID;
|
|
84 |
|
|
85 |
/////////////////////////////////////////////////////////
|
|
86 |
|
|
87 |
StackFrame()
|
|
88 |
{
|
|
89 |
mDoneList = new LinkedList<>();
|
|
90 |
mToDoMap = new HashMap<>();
|
|
91 |
mNextClientID = 0;
|
|
92 |
mNextSystemID = 0;
|
|
93 |
}
|
|
94 |
|
|
95 |
/////////////////////////////////////////////////////////
|
|
96 |
|
|
97 |
void onPause()
|
|
98 |
{
|
|
99 |
int num = mDoneList.size();
|
|
100 |
|
|
101 |
try
|
|
102 |
{
|
|
103 |
for (int i=0; i<num; i++)
|
|
104 |
{
|
|
105 |
InternalObject object = mDoneList.removeFirst();
|
|
106 |
Job job = new Job(object, JOB_CREATE);
|
|
107 |
mToDoMap.put(object.mID,job);
|
|
108 |
object.recreate();
|
|
109 |
}
|
|
110 |
}
|
|
111 |
catch( Exception ignored )
|
|
112 |
{
|
|
113 |
// something else removed an object in the meantime; ignore
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
/////////////////////////////////////////////////////////
|
|
118 |
|
|
119 |
void toDo()
|
|
120 |
{
|
|
121 |
for(Long key: mToDoMap.keySet())
|
|
122 |
{
|
|
123 |
Job job = mToDoMap.get(key);
|
|
124 |
InternalObject object = job.object;
|
|
125 |
|
|
126 |
if( job.action==JOB_CREATE )
|
|
127 |
{
|
|
128 |
object.create();
|
|
129 |
mDoneList.add(object);
|
|
130 |
}
|
|
131 |
else if( job.action==JOB_DELETE )
|
|
132 |
{
|
|
133 |
object.delete();
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
mToDoMap.clear();
|
|
138 |
}
|
|
139 |
|
|
140 |
/////////////////////////////////////////////////////////
|
|
141 |
|
|
142 |
long generateID(int type)
|
|
143 |
{
|
|
144 |
return type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
|
|
145 |
}
|
|
146 |
|
|
147 |
/////////////////////////////////////////////////////////
|
|
148 |
|
|
149 |
void addToDoneList(InternalObject obj)
|
|
150 |
{
|
|
151 |
mDoneList.add(obj);
|
|
152 |
}
|
|
153 |
|
|
154 |
|
|
155 |
/////////////////////////////////////////////////////////
|
|
156 |
|
|
157 |
void markFor(InternalObject obj, long id, int jobType)
|
|
158 |
{
|
|
159 |
mDoneList.remove(obj);
|
|
160 |
mToDoMap.put(id, new Job(obj,jobType) );
|
|
161 |
}
|
|
162 |
|
|
163 |
/////////////////////////////////////////////////////////
|
|
164 |
|
|
165 |
void debugLists(String frameMarker)
|
|
166 |
{
|
|
167 |
android.util.Log.e("Object", frameMarker);
|
|
168 |
android.util.Log.e("Object", " Done list:");
|
|
169 |
|
|
170 |
for(InternalObject object : mDoneList)
|
|
171 |
{
|
|
172 |
object.print(" ");
|
|
173 |
}
|
|
174 |
|
|
175 |
android.util.Log.e("Object", " ToDo list:");
|
|
176 |
|
|
177 |
Job job;
|
|
178 |
|
|
179 |
for(Long key: mToDoMap.keySet())
|
|
180 |
{
|
|
181 |
job = mToDoMap.get(key);
|
|
182 |
job.object.print(job.action==JOB_CREATE ? " create":" delete");
|
|
183 |
}
|
|
184 |
}
|
|
185 |
}
|
73 |
186 |
|
74 |
187 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75 |
188 |
|
... | ... | |
85 |
198 |
{
|
86 |
199 |
if( mToDo )
|
87 |
200 |
{
|
88 |
|
Job job;
|
89 |
|
InternalObject object;
|
|
201 |
mToDo = false;
|
90 |
202 |
|
91 |
|
synchronized(mToDoLock)
|
|
203 |
synchronized(mLock)
|
92 |
204 |
{
|
93 |
|
for(Long key: mToDoMap.keySet())
|
94 |
|
{
|
95 |
|
job = mToDoMap.get(key);
|
96 |
|
object = job.object;
|
97 |
|
|
98 |
|
if( job.action==JOB_CREATE )
|
99 |
|
{
|
100 |
|
object.create();
|
101 |
|
mDoneList.add(object);
|
102 |
|
}
|
103 |
|
else if( job.action==JOB_DELETE )
|
104 |
|
{
|
105 |
|
object.delete();
|
106 |
|
}
|
107 |
|
}
|
108 |
|
|
109 |
|
mToDoMap.clear();
|
110 |
|
mToDo = false;
|
|
205 |
mCurrentFrame.toDo();
|
111 |
206 |
}
|
112 |
|
|
113 |
207 |
return true;
|
114 |
208 |
}
|
115 |
209 |
|
... | ... | |
118 |
212 |
|
119 |
213 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120 |
214 |
|
121 |
|
static void onPause()
|
|
215 |
static void onCreate()
|
122 |
216 |
{
|
123 |
|
InternalObject object;
|
124 |
|
|
125 |
|
synchronized(mDoneLock)
|
|
217 |
synchronized(mLock)
|
126 |
218 |
{
|
127 |
|
synchronized(mToDoLock)
|
128 |
|
{
|
129 |
|
int num = mDoneList.size();
|
130 |
|
|
131 |
|
try
|
132 |
|
{
|
133 |
|
for (int i = 0; i < num; i++)
|
134 |
|
{
|
135 |
|
object = mDoneList.removeFirst();
|
136 |
|
Job job = new Job(object, JOB_CREATE);
|
137 |
|
mToDoMap.put(object.mID,job);
|
138 |
|
object.recreate();
|
139 |
|
}
|
140 |
|
}
|
141 |
|
catch( Exception ex )
|
142 |
|
{
|
143 |
|
// something else removed an object in the meantime; ignore
|
144 |
|
}
|
145 |
|
}
|
|
219 |
mCurrentFrame = new StackFrame();
|
|
220 |
mFrameList.add(mCurrentFrame);
|
|
221 |
mToDo = false;
|
146 |
222 |
}
|
|
223 |
}
|
|
224 |
|
|
225 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147 |
226 |
|
148 |
|
mToDo = true;
|
|
227 |
static void onPause()
|
|
228 |
{
|
|
229 |
synchronized(mLock)
|
|
230 |
{
|
|
231 |
mCurrentFrame.onPause();
|
|
232 |
mToDo = true;
|
|
233 |
}
|
149 |
234 |
}
|
150 |
235 |
|
151 |
236 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152 |
237 |
|
153 |
238 |
static void onDestroy()
|
154 |
239 |
{
|
155 |
|
synchronized(mToDoLock)
|
|
240 |
synchronized(mLock)
|
156 |
241 |
{
|
157 |
|
synchronized(mDoneLock)
|
|
242 |
int num = mFrameList.size();
|
|
243 |
|
|
244 |
if( num>0 )
|
158 |
245 |
{
|
159 |
|
mToDoMap.clear();
|
160 |
|
mDoneList.clear();
|
|
246 |
mFrameList.remove(num-1);
|
161 |
247 |
|
162 |
|
mToDo = true;
|
163 |
|
mNextClientID = 0;
|
|
248 |
if( num>1 )
|
|
249 |
{
|
|
250 |
mCurrentFrame = mFrameList.get(num-2);
|
|
251 |
mToDo = true;
|
|
252 |
}
|
|
253 |
else
|
|
254 |
{
|
|
255 |
mCurrentFrame = null;
|
|
256 |
mToDo = false;
|
|
257 |
}
|
164 |
258 |
}
|
165 |
259 |
}
|
166 |
260 |
}
|
... | ... | |
170 |
264 |
@SuppressWarnings("unused")
|
171 |
265 |
static void debugLists()
|
172 |
266 |
{
|
173 |
|
android.util.Log.e("Object", "Done list:");
|
|
267 |
int num = mFrameList.size();
|
|
268 |
StackFrame frame;
|
174 |
269 |
|
175 |
|
for(InternalObject object : mDoneList)
|
|
270 |
for(int i=0; i<num; i++)
|
176 |
271 |
{
|
177 |
|
object.print("");
|
178 |
|
}
|
179 |
|
|
180 |
|
android.util.Log.e("Object", "ToDo list:");
|
181 |
|
|
182 |
|
Job job;
|
183 |
|
|
184 |
|
for(Long key: mToDoMap.keySet())
|
185 |
|
{
|
186 |
|
job = mToDoMap.get(key);
|
187 |
|
job.object.print(job.action==JOB_CREATE ? " create":" delete");
|
|
272 |
frame = mFrameList.get(i);
|
|
273 |
frame.debugLists("frame "+i);
|
188 |
274 |
}
|
189 |
275 |
}
|
190 |
276 |
|
... | ... | |
209 |
295 |
|
210 |
296 |
InternalObject(int type)
|
211 |
297 |
{
|
212 |
|
mID = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID;
|
|
298 |
if( mCurrentFrame==null ) onCreate();
|
|
299 |
|
|
300 |
mID = mCurrentFrame.generateID(type);
|
213 |
301 |
mType= type;
|
214 |
302 |
}
|
215 |
303 |
|
... | ... | |
217 |
305 |
|
218 |
306 |
void markWasCreatedImmediately()
|
219 |
307 |
{
|
220 |
|
mDoneList.add(this);
|
|
308 |
mCurrentFrame.addToDoneList(this);
|
221 |
309 |
}
|
222 |
310 |
|
223 |
311 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224 |
312 |
|
225 |
313 |
void markForCreation()
|
226 |
314 |
{
|
227 |
|
synchronized(mDoneLock)
|
|
315 |
synchronized(mLock)
|
228 |
316 |
{
|
229 |
|
mDoneList.remove(this);
|
230 |
|
}
|
231 |
|
|
232 |
|
synchronized(mToDoLock)
|
233 |
|
{
|
234 |
|
mToDoMap.put(mID, new Job(this,JOB_CREATE) );
|
|
317 |
mCurrentFrame.markFor(this,mID,JOB_CREATE);
|
235 |
318 |
mToDo = true;
|
236 |
319 |
}
|
237 |
320 |
}
|
... | ... | |
244 |
327 |
*/
|
245 |
328 |
public void markForDeletion()
|
246 |
329 |
{
|
247 |
|
synchronized(mDoneLock)
|
248 |
|
{
|
249 |
|
mDoneList.remove(this);
|
250 |
|
}
|
251 |
|
|
252 |
|
synchronized(mToDoLock)
|
|
330 |
synchronized(mLock)
|
253 |
331 |
{
|
254 |
|
mToDoMap.put(mID, new Job(this,JOB_DELETE) );
|
|
332 |
mCurrentFrame.markFor(this,mID,JOB_DELETE);
|
255 |
333 |
mToDo = true;
|
256 |
334 |
}
|
257 |
335 |
}
|
... | ... | |
264 |
342 |
{
|
265 |
343 |
return mID;
|
266 |
344 |
}
|
267 |
|
|
268 |
345 |
}
|
1) Beginnings of support for using the library from more than one activity
2) rename DistortedLibrary.onCreate() to onSurfaceCreated()
3) (rubik) add Cambodia's flag.