… | |
… | |
36 | * Boston, MA 02110-1301, USA. |
36 | * Boston, MA 02110-1301, USA. |
37 | */ |
37 | */ |
38 | |
38 | |
39 | package com.zoffcc.applications.zanavi; |
39 | package com.zoffcc.applications.zanavi; |
40 | |
40 | |
41 | import android.os.Handler; |
41 | import java.util.Random; |
42 | import android.os.Message; |
|
|
43 | import android.util.Log; |
|
|
44 | |
42 | |
45 | public class NavitTimeout implements Runnable |
43 | public class NavitTimeout extends Thread |
46 | { |
44 | { |
47 | private static Handler handler = new Handler() |
|
|
48 | { |
|
|
49 | public void handleMessage(Message m) |
|
|
50 | { |
|
|
51 | Log.e("Navit", "Handler received message"); |
|
|
52 | } |
|
|
53 | }; |
|
|
54 | private boolean event_multi; |
45 | boolean event_multi; |
55 | private int event_callbackid; |
46 | private int event_callbackid; |
56 | private int event_timeout; |
47 | private int event_timeout; |
|
|
48 | Boolean running; |
|
|
49 | private int randnum = 0; |
57 | |
50 | |
|
|
51 | final Random myRandom = new Random(); |
|
|
52 | |
58 | public native void TimeoutCallback(int del, int id); |
53 | // public native void TimeoutCallback(int del, int id); |
59 | |
54 | |
60 | NavitTimeout(int timeout, boolean multi, int callbackid) |
55 | NavitTimeout(int timeout, boolean multi, int callbackid) |
61 | { |
56 | { |
|
|
57 | this.randnum = myRandom.nextInt(); |
62 | Log.e("Navit","Create New Event - to=" + timeout + " multi=" + multi + " cid=" + callbackid); |
58 | //Log.e("Navit", "Create New Event #"+randnum+" - to=" + timeout + " multi=" + multi + " cid=" + callbackid); |
63 | event_timeout = timeout; |
59 | event_timeout = timeout; |
64 | event_multi = multi; |
60 | event_multi = multi; |
65 | event_callbackid = callbackid; |
61 | event_callbackid = callbackid; |
66 | handler.postDelayed(this, event_timeout); |
62 | running = true; |
|
|
63 | this.start(); |
|
|
64 | //Log.e("Navit", "Create New Event - READY"); |
67 | } |
65 | } |
68 | |
66 | |
69 | public void run() |
67 | public void run() |
70 | { |
68 | { |
71 | Log.e("Navit","Handle Event - to=" + event_timeout + " multi=" + event_multi + " cid=" + event_callbackid); |
69 | //Log.e("Navit", "Handle Event #"+randnum+" - run"); |
72 | if (event_multi) |
70 | |
|
|
71 | while (running) |
73 | { |
72 | { |
74 | handler.postDelayed(this, event_timeout); |
73 | try |
|
|
74 | { |
|
|
75 | //Log.e("Navit", "Handle Event - sleep " + event_timeout + " millis"); |
|
|
76 | Thread.sleep(event_timeout, 0); |
|
|
77 | } |
|
|
78 | catch (InterruptedException e) |
|
|
79 | { |
|
|
80 | //e.printStackTrace(); |
|
|
81 | } |
|
|
82 | |
|
|
83 | if (running) |
|
|
84 | { |
|
|
85 | if (event_multi) |
|
|
86 | { |
|
|
87 | //Log.e("Navit", "Handle Event #"+randnum+" - to=" + event_timeout + " multi=" + event_multi + " cid=" + event_callbackid); |
75 | TimeoutCallback(0, event_callbackid); |
88 | Navit.cwthr.TimeoutCallback2(this, 0, event_callbackid); |
|
|
89 | } |
|
|
90 | else |
|
|
91 | { |
|
|
92 | //Log.e("Navit", "Handle Event #"+randnum+" - to=" + event_timeout + " multi=" + event_multi + " cid=" + event_callbackid); |
|
|
93 | running = false; |
|
|
94 | Navit.cwthr.TimeoutCallback2(this, 1, event_callbackid); |
|
|
95 | } |
|
|
96 | } |
76 | } |
97 | } |
77 | else |
98 | |
|
|
99 | //Log.e("Navit", "Handle Event #"+randnum+" - end cid=" + event_callbackid); |
|
|
100 | |
|
|
101 | try |
78 | { |
102 | { |
79 | TimeoutCallback(1, event_callbackid); |
103 | Thread.sleep(event_timeout, 1000); // sleep 1 secs. to wait for timeout remove call (in C code) |
80 | } |
104 | } |
|
|
105 | catch (InterruptedException e) |
|
|
106 | { |
|
|
107 | } |
|
|
108 | |
|
|
109 | //Log.e("Navit", "Handle Event #"+randnum+" - finish cid=" + event_callbackid); |
|
|
110 | |
81 | } |
111 | } |
82 | |
112 | |
83 | public void remove() |
113 | public void remove() |
84 | { |
114 | { |
85 | Log.e("Navit","remove Event - to=" + event_timeout + " multi=" + event_multi + " cid=" + event_callbackid); |
115 | //Log.e("Navit", "remove Event #"+randnum+" - to=" + event_timeout + " multi=" + event_multi + " cid=" + event_callbackid); |
86 | handler.removeCallbacks(this); |
116 | running = false; |
|
|
117 | this.interrupt(); |
87 | } |
118 | } |
88 | } |
119 | } |