Libraryless. Click here for Pure Java version (1210L/8K/33K).
1 | import java.util.*; |
2 | import java.util.zip.*; |
3 | import java.util.List; |
4 | import java.util.regex.*; |
5 | import java.util.concurrent.*; |
6 | import java.util.concurrent.atomic.*; |
7 | import java.util.concurrent.locks.*; |
8 | import javax.swing.*; |
9 | import javax.swing.event.*; |
10 | import javax.swing.text.*; |
11 | import javax.swing.table.*; |
12 | import java.io.*; |
13 | import java.net.*; |
14 | import java.lang.reflect.*; |
15 | import java.lang.ref.*; |
16 | import java.lang.management.*; |
17 | import java.security.*; |
18 | import java.security.spec.*; |
19 | import java.awt.*; |
20 | import java.awt.event.*; |
21 | import java.awt.image.*; |
22 | import javax.imageio.*; |
23 | import java.math.*; |
24 | |
25 | |
26 | import android.app.*; |
27 | import android.view.KeyEvent; |
28 | import android.view.inputmethod.*; |
29 | import android.text.*; |
30 | import android.view.View; |
31 | import android.content.Context; |
32 | import android.app.Activity; |
33 | import android.widget.*; |
34 | import android.graphics.*; |
35 | import android.hardware.Camera; |
36 | import android.view.*; |
37 | class main { |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | public static void main(final String[] args) throws Exception { |
45 | print("Listing cameras..."); |
46 | |
47 | runOnUiThread(new Runnable() { public void run() { try { |
48 | int n = Camera.getNumberOfCameras(); |
49 | System.out.println("Cameras: " + n); |
50 | |
51 | final Camera cam = Camera.open(0/*n-1*/); |
52 | |
53 | Camera.Parameters p = cam.getParameters(); |
54 | p.setPreviewSize(100, 100); |
55 | cam.setParameters(p); |
56 | |
57 | SurfaceView surfaceView = new SurfaceView(androidContext()); |
58 | SurfaceHolder surfaceHolder = surfaceView.getHolder(); |
59 | surfaceHolder.addCallback(new CamCallback()); |
60 | surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
61 | |
62 | androidShow(surfaceView); |
63 | |
64 | cam.setPreviewDisplay(surfaceHolder); |
65 | cam.startPreview(); |
66 | |
67 | print("Preview started."); |
68 | |
69 | surfaceView.post(new Runnable() { public void run() { try { |
70 | cam.takePicture(null, null, null, new JpegCallback()); |
71 | |
72 | } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "cam.takePicture(null, null, null, new JpegCallback());"; }}); |
73 | |
74 | print("Waiting for callback"); |
75 | |
76 | } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "int n = Camera.getNumberOfCameras();\n System.out.println(\"Cameras: \" + n);..."; }}); |
77 | } |
78 | |
79 | static class CamCallback implements SurfaceHolder.Callback { |
80 | public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) { |
81 | print("surfaceChanged"); |
82 | } |
83 | public void surfaceCreated (SurfaceHolder holder) { |
84 | print("surfaceCreated"); |
85 | } |
86 | public void surfaceDestroyed (SurfaceHolder holder) { |
87 | print("surfaceDestroyed"); |
88 | } |
89 | } |
90 | |
91 | static class ShutterCallback implements Camera.ShutterCallback { |
92 | public void onShutter() { |
93 | print "Shutter!" |
94 | } |
95 | } |
96 | |
97 | static class JpegCallback implements Camera.PictureCallback { |
98 | public void onPictureTaken (byte[] data, Camera cam) { |
99 | TextView tv = aTextView(); |
100 | tv.setText("Picture data: " + data.length); |
101 | androidShow(tv); |
102 | |
103 | System.out.println("Picture data: " + data.length); |
104 | cam.release(); |
105 | print "Camera released." |
106 | } |
107 | } |
108 | static void runOnUiThread(final Runnable r) { |
109 | androidActivity().runOnUiThread(r); |
110 | } |
111 | static RuntimeException rethrow(Throwable t) { |
112 | |
113 | if (t instanceof Error) |
114 | _handleError((Error) t); |
115 | |
116 | throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); |
117 | } |
118 | static Context androidContext() { |
119 | return getAndroidContext(); |
120 | } |
121 | static volatile StringBuffer local_log = new StringBuffer(); // not redirected |
122 | static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot |
123 | |
124 | // in bytes - will cut to half that |
125 | static volatile int print_log_max = 1024*1024; |
126 | static volatile int local_log_max = 100*1024; |
127 | //static int print_maxLineLength = 0; // 0 = unset |
128 | |
129 | static boolean print_silent; // total mute if set |
130 | |
131 | static Object print_byThread_lock = new Object(); |
132 | static volatile ThreadLocal<Object> print_byThread; // special handling by thread - prefers F1<S, Bool> |
133 | static volatile Object print_allThreads; |
134 | |
135 | static void print() { |
136 | print(""); |
137 | } |
138 | |
139 | static <A> A print(String s, A o) { |
140 | print(s + o); |
141 | return o; |
142 | } |
143 | |
144 | // slightly overblown signature to return original object... |
145 | static <A> A print(A o) { |
146 | ping(); |
147 | if (print_silent) return o; |
148 | String s = String.valueOf(o) + "\n"; |
149 | print_noNewLine(s); |
150 | return o; |
151 | } |
152 | |
153 | static void print_noNewLine(String s) { |
154 | |
155 | Object f = print_byThread == null ? null : print_byThread.get(); |
156 | if (f == null) f = print_allThreads; |
157 | if (f != null) |
158 | if (isFalse(f instanceof F1 ? ((F1) f).get(s) : callF(f, s))) return; |
159 | |
160 | |
161 | print_raw(s); |
162 | } |
163 | |
164 | static void print_raw(String s) { |
165 | s = fixNewLines(s); |
166 | // TODO if (print_maxLineLength != 0) |
167 | StringBuffer loc = local_log; |
168 | StringBuffer buf = print_log; |
169 | int loc_max = print_log_max; |
170 | if (buf != loc && buf != null) { |
171 | print_append(buf, s, print_log_max); |
172 | loc_max = local_log_max; |
173 | } |
174 | if (loc != null) |
175 | print_append(loc, s, loc_max); |
176 | System.out.print(s); |
177 | } |
178 | |
179 | static void print(long l) { |
180 | print(String.valueOf(l)); |
181 | } |
182 | |
183 | static void print(char c) { |
184 | print(String.valueOf(c)); |
185 | } |
186 | |
187 | static void print_append(StringBuffer buf, String s, int max) { |
188 | synchronized(buf) { |
189 | buf.append(s); |
190 | max /= 2; |
191 | if (buf.length() > max) try { |
192 | int newLength = max/2; |
193 | int ofs = buf.length()-newLength; |
194 | String newString = buf.substring(ofs); |
195 | buf.setLength(0); |
196 | buf.append("[...] ").append(newString); |
197 | } catch (Exception e) { |
198 | buf.setLength(0); |
199 | } |
200 | } |
201 | } |
202 | |
203 | |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | static TextView aTextView(String text) { |
210 | TextView tv = aTextView(); |
211 | tv.setText(text); |
212 | return tv; |
213 | } |
214 | |
215 | static TextView aTextView() { |
216 | return androidUI(new F0<TextView>() { TextView get() { try { return |
217 | new TextView(androidActivity()) |
218 | ; } catch (Exception __e) { throw rethrow(__e); } } |
219 | public String toString() { return "new TextView(androidActivity())"; }}); |
220 | } |
221 | static Map<Thread, Boolean> _registerThread_threads = newWeakHashMap(); |
222 | |
223 | static Thread _registerThread(Thread t) { |
224 | _registerThread_threads.put(t, true); |
225 | return t; |
226 | } |
227 | |
228 | static void _registerThread() { _registerThread(Thread.currentThread()); } |
229 | |
230 | |
231 | static View androidShow_view; |
232 | |
233 | static void androidShow(final View view) { |
234 | androidUI(new Runnable() { public void run() { try { |
235 | androidShow_view = view; |
236 | androidActivity().setContentView(aRemoveFromParent(view)); |
237 | |
238 | } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "androidShow_view = view;\r\n androidActivity().setContentView(aRemoveFromPar..."; }}); |
239 | } |
240 | |
241 | |
242 | static String fixNewLines(String s) { |
243 | return s.replace("\r\n", "\n").replace("\r", "\n"); |
244 | } |
245 | // TODO: send hard errors to core |
246 | |
247 | static AtomicLong _handleError_nonVMErrors = new AtomicLong(); |
248 | static AtomicLong _handleError_vmErrors = new AtomicLong(); |
249 | static AtomicLong _handleError_outOfMemoryErrors = new AtomicLong(); |
250 | static volatile long _handleError_lastOutOfMemoryError; |
251 | static volatile Error _handleError_lastHardError; |
252 | |
253 | static void _handleError(Error e) { |
254 | if (!(e instanceof VirtualMachineError)) { |
255 | incAtomicLong(_handleError_nonVMErrors); |
256 | return; |
257 | } |
258 | |
259 | print("\nHARD ERROR\n"); |
260 | printStackTrace2(e); |
261 | print("\nHARD ERROR\n"); |
262 | _handleError_lastHardError = e; |
263 | |
264 | incAtomicLong(_handleError_vmErrors); |
265 | if (e instanceof OutOfMemoryError) { |
266 | incAtomicLong(_handleError_outOfMemoryErrors); |
267 | _handleError_lastOutOfMemoryError = sysNow(); |
268 | } |
269 | } |
270 | static volatile boolean ping_pauseAll; |
271 | static int ping_sleep = 100; // poll pauseAll flag every 100 |
272 | |
273 | static volatile boolean ping_anyActions; |
274 | static Map<Thread, Object> ping_actions = newWeakHashMap(); |
275 | |
276 | |
277 | // always returns true |
278 | static boolean ping() { |
279 | if (ping_pauseAll || ping_anyActions ) ping_impl(); |
280 | return true; |
281 | } |
282 | |
283 | // returns true when it slept |
284 | static boolean ping_impl() { try { |
285 | if (ping_pauseAll && !isAWTThread()) { |
286 | do |
287 | Thread.sleep(ping_sleep); |
288 | while (ping_pauseAll); |
289 | return true; |
290 | } |
291 | |
292 | |
293 | if (ping_anyActions) { |
294 | Object action; |
295 | synchronized(ping_actions) { |
296 | action = ping_actions.get(currentThread()); |
297 | if (action instanceof Runnable) |
298 | ping_actions.remove(currentThread()); |
299 | if (ping_actions.isEmpty()) ping_anyActions = false; |
300 | } |
301 | |
302 | if (action instanceof Runnable) |
303 | ((Runnable) action).run(); |
304 | else if (eq(action, "cancelled")) |
305 | throw fail("Thread cancelled."); |
306 | } |
307 | |
308 | |
309 | return false; |
310 | } catch (Exception __e) { throw rethrow(__e); } } |
311 | static WeakHashMap<Class, ArrayList<Method>> callF_cache = new WeakHashMap(); |
312 | |
313 | |
314 | static <A> A callF(F0<A> f) { |
315 | return f == null ? null : f.get(); |
316 | } |
317 | |
318 | |
319 | |
320 | static <A, B> B callF(F1<A, B> f, A a) { |
321 | return f == null ? null : f.get(a); |
322 | } |
323 | |
324 | |
325 | static Object callF(Object f, Object... args) { try { |
326 | if (f instanceof String) |
327 | return callMC((String) f, args); |
328 | if (f instanceof Runnable) { |
329 | ((Runnable) f).run(); |
330 | return null; |
331 | } |
332 | if (f == null) return null; |
333 | |
334 | Class c = f.getClass(); |
335 | ArrayList<Method> methods; |
336 | synchronized(callF_cache) { |
337 | methods = callF_cache.get(c); |
338 | if (methods == null) |
339 | methods = callF_makeCache(c); |
340 | } |
341 | |
342 | int n = l(methods); |
343 | if (n == 0) throw fail("No get method in " + getClassName(c)); |
344 | if (n == 1) return invokeMethod(methods.get(0), f, args); |
345 | for (int i = 0; i < n; i++) { |
346 | Method m = methods.get(i); |
347 | if (call_checkArgs(m, args, false)) |
348 | return invokeMethod(m, f, args); |
349 | } |
350 | throw fail("No matching get method in " + getClassName(c)); |
351 | } catch (Exception __e) { throw rethrow(__e); } } |
352 | |
353 | // used internally |
354 | static ArrayList<Method> callF_makeCache(Class c) { |
355 | ArrayList<Method> l = new ArrayList(); |
356 | Class _c = c; |
357 | do { |
358 | for (Method m : _c.getDeclaredMethods()) |
359 | if (m.getName().equals("get")) { |
360 | m.setAccessible(true); |
361 | l.add(m); |
362 | } |
363 | if (!l.isEmpty()) break; |
364 | _c = _c.getSuperclass(); |
365 | } while (_c != null); |
366 | callF_cache.put(c, l); |
367 | return l; |
368 | } |
369 | static boolean isFalse(Object o) { |
370 | return eq(false, o); |
371 | } |
372 | static <A, B> Map<A, B> newWeakHashMap() { |
373 | return _registerWeakMap(synchroMap(new WeakHashMap())); |
374 | } |
375 | |
376 | |
377 | static Context getAndroidContext() { |
378 | return (Context) getAndroidContext_gen(); |
379 | } |
380 | static <A extends View> A aRemoveFromParent(final A view) { |
381 | androidUI(new Runnable() { public void run() { try { |
382 | ViewParent group = view.getParent(); |
383 | if (group instanceof ViewGroup) |
384 | ((ViewGroup) group).removeView(view); |
385 | |
386 | } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ViewParent group = view.getParent();\r\n if (group instanceof ViewGroup)\r\n ..."; }}); |
387 | return view; |
388 | } |
389 | |
390 | static void androidUI(final Object r) { |
391 | androidUI_wait(r); |
392 | } |
393 | |
394 | static <A> A androidUI(F0<A> f) { |
395 | return androidUI_wait(f); |
396 | } |
397 | // get purpose 1: access a list/array/map (safer version of x.get(y)) |
398 | |
399 | static <A> A get(List<A> l, int idx) { |
400 | return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; |
401 | } |
402 | |
403 | // seems to conflict with other signatures |
404 | /*static <A, B> B get(Map<A, B> map, A key) { |
405 | ret map != null ? map.get(key) : null; |
406 | }*/ |
407 | |
408 | static <A> A get(A[] l, int idx) { |
409 | return idx >= 0 && idx < l(l) ? l[idx] : null; |
410 | } |
411 | |
412 | // default to false |
413 | static boolean get(boolean[] l, int idx) { |
414 | return idx >= 0 && idx < l(l) ? l[idx] : false; |
415 | } |
416 | |
417 | // get purpose 2: access a field by reflection or a map |
418 | |
419 | static Object get(Object o, String field) { |
420 | try { |
421 | if (o instanceof Class) return get((Class) o, field); |
422 | |
423 | if (o instanceof Map) |
424 | return ((Map) o).get(field); |
425 | |
426 | Field f = getOpt_findField(o.getClass(), field); |
427 | if (f != null) { |
428 | f.setAccessible(true); |
429 | return f.get(o); |
430 | } |
431 | |
432 | |
433 | } catch (Exception e) { |
434 | throw asRuntimeException(e); |
435 | } |
436 | throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName()); |
437 | } |
438 | |
439 | static Object get_raw(Object o, String field) { |
440 | try { |
441 | Field f = get_findField(o.getClass(), field); |
442 | f.setAccessible(true); |
443 | return f.get(o); |
444 | } catch (Exception e) { |
445 | throw new RuntimeException(e); |
446 | } |
447 | } |
448 | |
449 | static Object get(Class c, String field) { |
450 | try { |
451 | Field f = get_findStaticField(c, field); |
452 | f.setAccessible(true); |
453 | return f.get(null); |
454 | } catch (Exception e) { |
455 | throw new RuntimeException(e); |
456 | } |
457 | } |
458 | |
459 | static Field get_findStaticField(Class<?> c, String field) { |
460 | Class _c = c; |
461 | do { |
462 | for (Field f : _c.getDeclaredFields()) |
463 | if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) |
464 | return f; |
465 | _c = _c.getSuperclass(); |
466 | } while (_c != null); |
467 | throw new RuntimeException("Static field '" + field + "' not found in " + c.getName()); |
468 | } |
469 | |
470 | static Field get_findField(Class<?> c, String field) { |
471 | Class _c = c; |
472 | do { |
473 | for (Field f : _c.getDeclaredFields()) |
474 | if (f.getName().equals(field)) |
475 | return f; |
476 | _c = _c.getSuperclass(); |
477 | } while (_c != null); |
478 | throw new RuntimeException("Field '" + field + "' not found in " + c.getName()); |
479 | } |
480 | |
481 | |
482 | |
483 | static Activity androidActivity() { |
484 | return (Activity) androidContext_gen(); |
485 | } |
486 | |
487 | |
488 | static Map synchroMap() { |
489 | return synchroHashMap(); |
490 | } |
491 | |
492 | static <A, B> Map<A, B> synchroMap(Map<A, B> map) { |
493 | return Collections.synchronizedMap(map); |
494 | } |
495 | static HashMap<String, List<Method>> callMC_cache = new HashMap(); |
496 | static String callMC_key; |
497 | static Method callMC_value; |
498 | |
499 | // varargs assignment fixer for a single string array argument |
500 | static Object callMC(String method, String[] arg) { |
501 | return callMC(method, new Object[] {arg}); |
502 | } |
503 | |
504 | static Object callMC(String method, Object... args) { try { |
505 | Method me; |
506 | if (callMC_cache == null) callMC_cache = new HashMap(); // initializer time workaround |
507 | synchronized(callMC_cache) { |
508 | me = method == callMC_key ? callMC_value : null; |
509 | } |
510 | if (me != null) try { |
511 | return invokeMethod(me, null, args); |
512 | } catch (IllegalArgumentException e) { |
513 | throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e); |
514 | } |
515 | |
516 | List<Method> m; |
517 | synchronized(callMC_cache) { |
518 | m = callMC_cache.get(method); |
519 | } |
520 | if (m == null) { |
521 | if (callMC_cache.isEmpty()) { |
522 | callMC_makeCache(); |
523 | m = callMC_cache.get(method); |
524 | } |
525 | if (m == null) throw fail("Method named " + method + " not found in main"); |
526 | } |
527 | int n = m.size(); |
528 | if (n == 1) { |
529 | me = m.get(0); |
530 | synchronized(callMC_cache) { |
531 | callMC_key = method; |
532 | callMC_value = me; |
533 | } |
534 | try { |
535 | return invokeMethod(me, null, args); |
536 | } catch (IllegalArgumentException e) { |
537 | throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e); |
538 | } |
539 | } |
540 | for (int i = 0; i < n; i++) { |
541 | me = m.get(i); |
542 | if (call_checkArgs(me, args, false)) |
543 | return invokeMethod(me, null, args); |
544 | } |
545 | throw fail("No method called " + method + " with matching arguments found in main"); |
546 | } catch (Exception __e) { throw rethrow(__e); } } |
547 | |
548 | static void callMC_makeCache() { |
549 | synchronized(callMC_cache) { |
550 | callMC_cache.clear(); |
551 | Class _c = (Class) mc(), c = _c; |
552 | while (c != null) { |
553 | for (Method m : c.getDeclaredMethods()) |
554 | if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) { |
555 | m.setAccessible(true); |
556 | multiMapPut(callMC_cache, m.getName(), m); |
557 | } |
558 | c = c.getSuperclass(); |
559 | } |
560 | } |
561 | } |
562 | static long sysNow() { |
563 | return System.nanoTime()/1000000; |
564 | } |
565 | static RuntimeException asRuntimeException(Throwable t) { |
566 | |
567 | if (t instanceof Error) |
568 | _handleError((Error) t); |
569 | |
570 | return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); |
571 | } |
572 | static void androidUI_wait(final Object r) { |
573 | final Flag done = new Flag(); |
574 | androidUI_noWait(new Runnable() { public void run() { try { |
575 | try { |
576 | callF(r); |
577 | } finally { |
578 | done.raise(); |
579 | } |
580 | |
581 | } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "try {\r\n callF(r);\r\n } finally {\r\n done.raise();\r\n }"; }}); |
582 | done.waitUntilUp(); |
583 | } |
584 | |
585 | static <A> A androidUI_wait(F0<A> f) { |
586 | return androidEvalInUIThread(f); |
587 | } |
588 | static void incAtomicLong(AtomicLong l) { |
589 | l.incrementAndGet(); |
590 | } |
591 | // TODO: test if android complains about this |
592 | static boolean isAWTThread() { |
593 | if (isAndroid()) return false; |
594 | if (isHeadless()) return false; |
595 | return isAWTThread_awt(); |
596 | } |
597 | |
598 | static boolean isAWTThread_awt() { |
599 | return SwingUtilities.isEventDispatchThread(); |
600 | } |
601 | static Object androidContext_gen() { |
602 | return getAndroidContext_gen(); |
603 | } |
604 | static Thread currentThread() { |
605 | return Thread.currentThread(); |
606 | } |
607 | static boolean eq(Object a, Object b) { |
608 | return a == null ? b == null : a == b || a.equals(b); |
609 | } |
610 | |
611 | |
612 | static int l(Object[] a) { return a == null ? 0 : a.length; } |
613 | static int l(boolean[] a) { return a == null ? 0 : a.length; } |
614 | static int l(byte[] a) { return a == null ? 0 : a.length; } |
615 | static int l(int[] a) { return a == null ? 0 : a.length; } |
616 | static int l(float[] a) { return a == null ? 0 : a.length; } |
617 | static int l(char[] a) { return a == null ? 0 : a.length; } |
618 | static int l(Collection c) { return c == null ? 0 : c.size(); } |
619 | static int l(Map m) { return m == null ? 0 : m.size(); } |
620 | static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); } |
621 | |
622 | static int l(Object o) { |
623 | return o == null ? 0 |
624 | : o instanceof String ? l((String) o) |
625 | : o instanceof Map ? l((Map) o) |
626 | : o instanceof Collection ? l((Collection) o) |
627 | : (Integer) call(o, "size"); |
628 | } |
629 | |
630 | |
631 | |
632 | |
633 | static Object androidContext; |
634 | |
635 | static Object getAndroidContext_gen() { |
636 | return androidContext; |
637 | } |
638 | static RuntimeException fail() { throw new RuntimeException("fail"); } |
639 | static RuntimeException fail(Throwable e) { throw asRuntimeException(e); } |
640 | static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); } |
641 | static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); } |
642 | static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); } |
643 | |
644 | static Object call(Object o) { |
645 | return callFunction(o); |
646 | } |
647 | |
648 | // varargs assignment fixer for a single string array argument |
649 | static Object call(Object o, String method, String[] arg) { |
650 | return call(o, method, new Object[] {arg}); |
651 | } |
652 | |
653 | static Object call(Object o, String method, Object... args) { |
654 | try { |
655 | if (o instanceof Class) { |
656 | Method m = call_findStaticMethod((Class) o, method, args, false); |
657 | m.setAccessible(true); |
658 | return invokeMethod(m, null, args); |
659 | } else { |
660 | Method m = call_findMethod(o, method, args, false); |
661 | m.setAccessible(true); |
662 | return invokeMethod(m, o, args); |
663 | } |
664 | } catch (Exception e) { |
665 | throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); |
666 | } |
667 | } |
668 | |
669 | static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) { |
670 | Class _c = c; |
671 | while (c != null) { |
672 | for (Method m : c.getDeclaredMethods()) { |
673 | if (debug) |
674 | System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");; |
675 | if (!m.getName().equals(method)) { |
676 | if (debug) System.out.println("Method name mismatch: " + method); |
677 | continue; |
678 | } |
679 | |
680 | if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug)) |
681 | continue; |
682 | |
683 | return m; |
684 | } |
685 | c = c.getSuperclass(); |
686 | } |
687 | throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + _c.getName()); |
688 | } |
689 | |
690 | static Method call_findMethod(Object o, String method, Object[] args, boolean debug) { |
691 | Class c = o.getClass(); |
692 | while (c != null) { |
693 | for (Method m : c.getDeclaredMethods()) { |
694 | if (debug) |
695 | System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");; |
696 | if (m.getName().equals(method) && call_checkArgs(m, args, debug)) |
697 | return m; |
698 | } |
699 | c = c.getSuperclass(); |
700 | } |
701 | throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName()); |
702 | } |
703 | |
704 | private static boolean call_checkArgs(Method m, Object[] args, boolean debug) { |
705 | Class<?>[] types = m.getParameterTypes(); |
706 | if (types.length != args.length) { |
707 | if (debug) |
708 | System.out.println("Bad parameter length: " + args.length + " vs " + types.length); |
709 | return false; |
710 | } |
711 | for (int i = 0; i < types.length; i++) |
712 | if (!(args[i] == null || isInstanceX(types[i], args[i]))) { |
713 | if (debug) |
714 | System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); |
715 | return false; |
716 | } |
717 | return true; |
718 | } |
719 | static Throwable printStackTrace2(Throwable e) { |
720 | // we go to system.out now - system.err is nonsense |
721 | print(getStackTrace2(e)); |
722 | return e; |
723 | } |
724 | |
725 | static void printStackTrace2() { |
726 | printStackTrace2(new Throwable()); |
727 | } |
728 | |
729 | static void printStackTrace2(String msg) { |
730 | printStackTrace2(new Throwable(msg)); |
731 | } |
732 | |
733 | /*static void printStackTrace2(S indent, Throwable e) { |
734 | if (endsWithLetter(indent)) indent += " "; |
735 | printIndent(indent, getStackTrace2(e)); |
736 | }*/ |
737 | static List _registerWeakMap_preList; |
738 | |
739 | static <A> A _registerWeakMap(A map) { |
740 | if (javax() == null) { |
741 | // We're in class init |
742 | if (_registerWeakMap_preList == null) _registerWeakMap_preList = synchroList(); |
743 | _registerWeakMap_preList.add(map); |
744 | return map; |
745 | } |
746 | |
747 | try { |
748 | call(javax(), "_registerWeakMap", map); |
749 | } catch (Throwable e) { |
750 | printException(e); |
751 | print("Upgrade JavaX!!"); |
752 | } |
753 | return map; |
754 | } |
755 | |
756 | static void _onLoad_registerWeakMap() { |
757 | assertNotNull(javax()); |
758 | if (_registerWeakMap_preList == null) return; |
759 | for (Object o : _registerWeakMap_preList) |
760 | _registerWeakMap(o); |
761 | _registerWeakMap_preList = null; |
762 | } |
763 | static Field getOpt_findField(Class<?> c, String field) { |
764 | Class _c = c; |
765 | do { |
766 | for (Field f : _c.getDeclaredFields()) |
767 | if (f.getName().equals(field)) |
768 | return f; |
769 | _c = _c.getSuperclass(); |
770 | } while (_c != null); |
771 | return null; |
772 | } |
773 | static String getClassName(Object o) { |
774 | return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName(); |
775 | } |
776 | static Object invokeMethod(Method m, Object o, Object... args) { try { |
777 | try { |
778 | return m.invoke(o, args); |
779 | } catch (InvocationTargetException e) { |
780 | throw rethrow(getExceptionCause(e)); |
781 | } catch (IllegalArgumentException e) { |
782 | throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args))); |
783 | } |
784 | } catch (Exception __e) { throw rethrow(__e); } } |
785 | |
786 | |
787 | static Boolean isHeadless_cache; |
788 | |
789 | static boolean isHeadless() { |
790 | if (isHeadless_cache != null) return isHeadless_cache; |
791 | if (isAndroid()) return isHeadless_cache = true; |
792 | if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true; |
793 | |
794 | // Also check if AWT actually works. |
795 | // If DISPLAY variable is set but no X server up, this will notice. |
796 | |
797 | try { |
798 | SwingUtilities.isEventDispatchThread(); |
799 | return isHeadless_cache = false; |
800 | } catch (Throwable e) { return isHeadless_cache = true; } |
801 | } |
802 | static Throwable getExceptionCause(Throwable e) { |
803 | Throwable c = e.getCause(); |
804 | return c != null ? c : e; |
805 | } |
806 | static String joinWithSpace(Collection<String> c) { |
807 | return join(" ", c); |
808 | } |
809 | |
810 | static String joinWithSpace(String... c) { |
811 | return join(" ", c); |
812 | } |
813 | |
814 | static Object callFunction(Object f, Object... args) { |
815 | return callF(f, args); |
816 | } |
817 | static List<String> classNames(Collection l) { |
818 | return getClassNames(l); |
819 | } |
820 | |
821 | static List<String> classNames(Object[] l) { |
822 | return getClassNames(Arrays.asList(l)); |
823 | } |
824 | static Class javax() { |
825 | return getJavaX(); |
826 | } |
827 | static Class mc() { |
828 | return main.class; |
829 | } |
830 | static <A> A assertNotNull(A a) { |
831 | assertTrue(a != null); |
832 | return a; |
833 | } |
834 | |
835 | static <A> A assertNotNull(String msg, A a) { |
836 | assertTrue(msg, a != null); |
837 | return a; |
838 | } |
839 | static <A, B> void multiMapPut(Map<A, List<B>> map, A a, B b) { |
840 | List<B> l = map.get(a); |
841 | if (l == null) |
842 | map.put(a, l = new ArrayList()); |
843 | l.add(b); |
844 | } |
845 | static String getStackTrace2(Throwable throwable) { |
846 | return hideCredentials(getStackTrace(throwable) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ", |
847 | hideCredentials(str(getInnerException(throwable)))) + "\n"); |
848 | } |
849 | // extended over Class.isInstance() to handle primitive types |
850 | static boolean isInstanceX(Class type, Object arg) { |
851 | if (type == boolean.class) return arg instanceof Boolean; |
852 | if (type == int.class) return arg instanceof Integer; |
853 | if (type == long.class) return arg instanceof Long; |
854 | if (type == float.class) return arg instanceof Float; |
855 | if (type == short.class) return arg instanceof Short; |
856 | if (type == char.class) return arg instanceof Character; |
857 | if (type == byte.class) return arg instanceof Byte; |
858 | if (type == double.class) return arg instanceof Double; |
859 | return type.isInstance(arg); |
860 | } |
861 | static void printException(Throwable e) { |
862 | printStackTrace(e); |
863 | } |
864 | static String str(Object o) { |
865 | return o == null ? "null" : o.toString(); |
866 | } |
867 | |
868 | static String str(char[] c) { |
869 | return new String(c); |
870 | } |
871 | static void androidUI_noWait(final Object r) { |
872 | androidActivity().runOnUiThread(toRunnable(r)); |
873 | } |
874 | static <A> List<A> synchroList() { |
875 | return Collections.synchronizedList(new ArrayList<A>()); |
876 | } |
877 | |
878 | static <A> List<A> synchroList(List<A> l) { |
879 | return Collections.synchronizedList(l); |
880 | } |
881 | |
882 | static <A> A androidEvalInUIThread(final F0<A> f) { |
883 | final Var<A> v = new Var(); |
884 | final Var<Throwable> error = new Var(); |
885 | androidUI_wait(new Runnable() { public void run() { try { |
886 | try { |
887 | v.set(callF(f)); |
888 | } catch (Throwable e) { |
889 | error.set(e); |
890 | } |
891 | |
892 | } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "try {\r\n v.set(callF(f));\r\n } catch (Throwable e) {\r\n error.set(e..."; }}); |
893 | if (error.has()) throw rethrow(error.get()); else return v.get(); |
894 | } |
895 | static Map synchroHashMap() { |
896 | return Collections.synchronizedMap(new HashMap()); |
897 | } |
898 | |
899 | static int isAndroid_flag; |
900 | |
901 | static boolean isAndroid() { |
902 | if (isAndroid_flag == 0) |
903 | isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1; |
904 | return isAndroid_flag > 0; |
905 | } |
906 | |
907 | |
908 | |
909 | static Runnable toRunnable(final Object o) { |
910 | if (o instanceof Runnable) return (Runnable) o; |
911 | return new Runnable() { public void run() { try { callF(o) ; |
912 | } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(o)"; }}; |
913 | } |
914 | static String getStackTrace(Throwable throwable) { |
915 | lastException(throwable); |
916 | return getStackTrace_noRecord(throwable); |
917 | } |
918 | |
919 | static String getStackTrace_noRecord(Throwable throwable) { |
920 | StringWriter writer = new StringWriter(); |
921 | throwable.printStackTrace(new PrintWriter(writer)); |
922 | return hideCredentials(writer.toString()); |
923 | } |
924 | |
925 | static String getStackTrace() { |
926 | return getStackTrace_noRecord(new Throwable()); |
927 | } |
928 | public static String join(String glue, Iterable<String> strings) { |
929 | if (strings == null) return ""; |
930 | StringBuilder buf = new StringBuilder(); |
931 | Iterator<String> i = strings.iterator(); |
932 | if (i.hasNext()) { |
933 | buf.append(i.next()); |
934 | while (i.hasNext()) |
935 | buf.append(glue).append(i.next()); |
936 | } |
937 | return buf.toString(); |
938 | } |
939 | |
940 | public static String join(String glue, String... strings) { |
941 | return join(glue, Arrays.asList(strings)); |
942 | } |
943 | |
944 | static String join(Iterable<String> strings) { |
945 | return join("", strings); |
946 | } |
947 | |
948 | static String join(Iterable<String> strings, String glue) { |
949 | return join(glue, strings); |
950 | } |
951 | |
952 | public static String join(String[] strings) { |
953 | return join("", strings); |
954 | } |
955 | |
956 | |
957 | static Throwable printStackTrace(Throwable e) { |
958 | // we go to system.out now - system.err is nonsense |
959 | print(getStackTrace(e)); |
960 | return e; |
961 | } |
962 | |
963 | static void printStackTrace() { |
964 | printStackTrace(new Throwable()); |
965 | } |
966 | |
967 | static void printStackTrace(String msg) { |
968 | printStackTrace(new Throwable(msg)); |
969 | } |
970 | |
971 | /*static void printStackTrace(S indent, Throwable e) { |
972 | if (endsWithLetter(indent)) indent += " "; |
973 | printIndent(indent, getStackTrace(e)); |
974 | }*/ |
975 | static String replacePrefix(String prefix, String replacement, String s) { |
976 | if (!startsWith(s, prefix)) return s; |
977 | return replacement + substring(s, l(prefix)); |
978 | } |
979 | static void assertTrue(Object o) { |
980 | if (!(eq(o, true) /*|| isTrue(pcallF(o))*/)) |
981 | throw fail(str(o)); |
982 | } |
983 | |
984 | static boolean assertTrue(String msg, boolean b) { |
985 | if (!b) |
986 | throw fail(msg); |
987 | return b; |
988 | } |
989 | |
990 | static boolean assertTrue(boolean b) { |
991 | if (!b) |
992 | throw fail("oops"); |
993 | return b; |
994 | } |
995 | static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); } |
996 | |
997 | static String hideCredentials(String url) { |
998 | return url.replaceAll("([&?])_pass=[^&\\s\"]*", "$1_pass=<hidden>"); |
999 | } |
1000 | |
1001 | static String hideCredentials(Object o) { |
1002 | return hideCredentials(str(o)); |
1003 | } |
1004 | static List<String> getClassNames(Collection l) { |
1005 | List<String> out = new ArrayList(); |
1006 | if (l != null) for (Object o : l) |
1007 | out.add(o == null ? null : getClassName(o)); |
1008 | return out; |
1009 | } |
1010 | static Class __javax; |
1011 | |
1012 | static Class getJavaX() { |
1013 | return __javax; |
1014 | } |
1015 | static Throwable getInnerException(Throwable e) { |
1016 | while (e.getCause() != null) |
1017 | e = e.getCause(); |
1018 | return e; |
1019 | } |
1020 | |
1021 | |
1022 | static volatile Throwable lastException_lastException; |
1023 | |
1024 | static Throwable lastException() { |
1025 | return lastException_lastException; |
1026 | } |
1027 | |
1028 | static void lastException(Throwable e) { |
1029 | lastException_lastException = e; |
1030 | } |
1031 | static String substring(String s, int x) { |
1032 | return substring(s, x, l(s)); |
1033 | } |
1034 | |
1035 | static String substring(String s, int x, int y) { |
1036 | if (s == null) return null; |
1037 | if (x < 0) x = 0; |
1038 | if (x >= s.length()) return ""; |
1039 | if (y < x) y = x; |
1040 | if (y > s.length()) y = s.length(); |
1041 | return s.substring(x, y); |
1042 | } |
1043 | |
1044 | |
1045 | static boolean startsWith(String a, String b) { |
1046 | return a != null && a.startsWith(b); |
1047 | } |
1048 | |
1049 | static boolean startsWith(String a, char c) { |
1050 | return nempty(a) && a.charAt(0) == c; |
1051 | } |
1052 | |
1053 | |
1054 | |
1055 | static boolean startsWith(List a, List b) { |
1056 | if (a == null || l(b) > l(a)) return false; |
1057 | for (int i = 0; i < l(b); i++) |
1058 | if (neq(a.get(i), b.get(i))) |
1059 | return false; |
1060 | return true; |
1061 | } |
1062 | |
1063 | |
1064 | |
1065 | |
1066 | static boolean neq(Object a, Object b) { |
1067 | return !eq(a, b); |
1068 | } |
1069 | static boolean nempty(Collection c) { |
1070 | return !isEmpty(c); |
1071 | } |
1072 | |
1073 | static boolean nempty(CharSequence s) { |
1074 | return !isEmpty(s); |
1075 | } |
1076 | |
1077 | static boolean nempty(Object[] o) { |
1078 | return !isEmpty(o); |
1079 | } |
1080 | |
1081 | static boolean nempty(Map m) { |
1082 | return !isEmpty(m); |
1083 | } |
1084 | |
1085 | static boolean nempty(Iterator i) { |
1086 | return i != null && i.hasNext(); |
1087 | } |
1088 | |
1089 | static boolean nempty(Object o) { return !empty(o); } |
1090 | |
1091 | |
1092 | static boolean empty(Collection c) { return c == null || c.isEmpty(); } |
1093 | static boolean empty(String s) { return s == null || s.length() == 0; } |
1094 | static boolean empty(Map map) { return map == null || map.isEmpty(); } |
1095 | static boolean empty(Object[] o) { return o == null || o.length == 0; } |
1096 | static boolean empty(Object o) { |
1097 | if (o instanceof Collection) return empty((Collection) o); |
1098 | if (o instanceof String) return empty((String) o); |
1099 | if (o instanceof Map) return empty((Map) o); |
1100 | if (o instanceof Object[]) return empty((Object[]) o); |
1101 | if (o == null) return true; |
1102 | throw fail("unknown type for 'empty': " + getType(o)); |
1103 | } |
1104 | |
1105 | static boolean empty(float[] a) { return a == null || a.length == 0; } |
1106 | static boolean empty(int[] a) { return a == null || a.length == 0; } |
1107 | static boolean empty(long[] a) { return a == null || a.length == 0; } |
1108 | |
1109 | |
1110 | static boolean isEmpty(Collection c) { |
1111 | return c == null || c.isEmpty(); |
1112 | } |
1113 | |
1114 | static boolean isEmpty(CharSequence s) { |
1115 | return s == null || s.length() == 0; |
1116 | } |
1117 | |
1118 | static boolean isEmpty(Object[] a) { |
1119 | return a == null || a.length == 0; |
1120 | } |
1121 | |
1122 | static boolean isEmpty(Map map) { |
1123 | return map == null || map.isEmpty(); |
1124 | } |
1125 | |
1126 | |
1127 | static String getType(Object o) { |
1128 | return getClassName(o); |
1129 | } |
1130 | |
1131 | |
1132 | static class Var<A> implements IVar<A> { |
1133 | A v; // you can access this directly if you use one thread |
1134 | |
1135 | Var() {} |
1136 | Var(A v) { |
1137 | this.v = v;} |
1138 | |
1139 | public synchronized void set(A a) { |
1140 | if (v != a) { |
1141 | v = a; |
1142 | notifyAll(); |
1143 | } |
1144 | } |
1145 | |
1146 | public synchronized A get() { return v; } |
1147 | public synchronized boolean has() { return v != null; } |
1148 | public synchronized void clear() { v = null; } |
1149 | |
1150 | public String toString() { return str(get()); } |
1151 | }static abstract class F0<A> { |
1152 | abstract A get(); |
1153 | }static abstract class F1<A, B> { |
1154 | abstract B get(A a); |
1155 | }/** this class is fully thread-safe */ |
1156 | static class Flag { |
1157 | private boolean up; |
1158 | |
1159 | /** returns true if flag was down before */ |
1160 | public synchronized boolean raise() { |
1161 | if (!up) { |
1162 | up = true; |
1163 | notifyAll(); |
1164 | return true; |
1165 | } else |
1166 | return false; |
1167 | } |
1168 | |
1169 | public synchronized void waitUntilUp() { |
1170 | while (!up) { |
1171 | try { |
1172 | wait(); |
1173 | } catch (InterruptedException e) { |
1174 | e.printStackTrace(); |
1175 | } |
1176 | } |
1177 | } |
1178 | |
1179 | public synchronized void waitUntilUp(long timeout) { |
1180 | if (!up) { |
1181 | try { |
1182 | wait(timeout); |
1183 | } catch (InterruptedException e) { |
1184 | e.printStackTrace(); |
1185 | } |
1186 | } |
1187 | } |
1188 | |
1189 | public synchronized boolean isUp() { |
1190 | return up; |
1191 | } |
1192 | |
1193 | public String toString() { |
1194 | return isUp() ? "up" : "down"; |
1195 | } |
1196 | |
1197 | // currently does a semi-active wait with latency = 50 ms |
1198 | public void waitForThisOr(Flag otherFlag) { try { |
1199 | while (!isUp() && !otherFlag.isUp()) |
1200 | Thread.sleep(50); |
1201 | } catch (Exception __e) { throw rethrow(__e); } } |
1202 | } |
1203 | |
1204 | static interface IVar<A> { |
1205 | void set(A a); |
1206 | A get(); |
1207 | boolean has(); |
1208 | void clear(); |
1209 | } |
1210 | } |
Began life as a copy of #1015069
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1015070 |
Snippet name: | Android Take Picture, old transpilation 2 |
Eternal ID of this version: | #1015070/3 |
Text MD5: | 28874e30ab8c1f7152e4d244a470eb66 |
Transpilation MD5: | 8138765c035e521076d3b9b81844a720 |
Author: | stefan |
Category: | javax android |
Type: | JavaX source code (Android) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-05-07 18:29:52 |
Source code size: | 34352 bytes / 1210 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 276 / 363 |
Version history: | 2 change(s) |
Referenced in: | [show references] |