… | |
… | |
22 | import java.lang.Thread; |
22 | import java.lang.Thread; |
23 | import android.os.Handler; |
23 | import android.os.Handler; |
24 | import android.os.Message; |
24 | import android.os.Message; |
25 | import android.util.Log; |
25 | import android.util.Log; |
26 | |
26 | |
27 | public class NavitWatch implements Runnable { |
27 | public class NavitWatch implements Runnable |
|
|
28 | { |
28 | private Thread thread; |
29 | private Thread thread; |
29 | private static Handler handler =new Handler() { |
30 | private static Handler handler = new Handler() |
|
|
31 | { |
30 | public void handleMessage(Message m) { |
32 | public void handleMessage(Message m) |
|
|
33 | { |
31 | Log.e("NavitWatch","Handler received message"); |
34 | Log.e("NavitWatch", "Handler received message"); |
32 | } |
35 | } |
33 | }; |
36 | }; |
34 | private boolean removed; |
37 | private boolean removed; |
35 | private int watch_fd; |
38 | private int watch_fd; |
36 | private int watch_cond; |
39 | private int watch_cond; |
37 | private int watch_callbackid; |
40 | private int watch_callbackid; |
38 | private boolean callback_pending; |
41 | private boolean callback_pending; |
39 | private Runnable callback_runnable; |
42 | private Runnable callback_runnable; |
|
|
43 | |
40 | public native void poll(int fd, int cond); |
44 | public native void poll(int fd, int cond); |
|
|
45 | |
41 | public native void WatchCallback(int id); |
46 | public native void WatchCallback(int id); |
42 | |
47 | |
43 | NavitWatch(int fd, int cond, int callbackid) |
48 | NavitWatch(int fd, int cond, int callbackid) |
44 | { |
49 | { |
45 | // Log.e("NavitWatch","Creating new thread for "+fd+" "+cond+" from current thread " + java.lang.Thread.currentThread().getName()); |
50 | // Log.e("NavitWatch","Creating new thread for "+fd+" "+cond+" from current thread " + java.lang.Thread.currentThread().getName()); |
46 | watch_fd=fd; |
51 | watch_fd = fd; |
47 | watch_cond=cond; |
52 | watch_cond = cond; |
48 | watch_callbackid=callbackid; |
53 | watch_callbackid = callbackid; |
49 | final NavitWatch navitwatch=this; |
54 | final NavitWatch navitwatch = this; |
50 | callback_runnable = new Runnable() { |
55 | callback_runnable = new Runnable() |
|
|
56 | { |
51 | public void run() |
57 | public void run() |
52 | { |
58 | { |
53 | navitwatch.callback(); |
59 | navitwatch.callback(); |
54 | } |
60 | } |
55 | }; |
61 | }; |
56 | thread = new Thread(this, "poll thread"); |
62 | thread = new Thread(this, "poll thread"); |
57 | thread.start(); |
63 | thread.start(); |
58 | } |
64 | } |
|
|
65 | |
59 | public void run() |
66 | public void run() |
60 | { |
67 | { |
61 | for (;;) { |
68 | for (;;) |
|
|
69 | { |
62 | // Log.e("NavitWatch","Polling "+watch_fd+" "+watch_cond + " from " + java.lang.Thread.currentThread().getName()); |
70 | // Log.e("NavitWatch","Polling "+watch_fd+" "+watch_cond + " from " + java.lang.Thread.currentThread().getName()); |
63 | poll(watch_fd, watch_cond); |
71 | poll(watch_fd, watch_cond); |
64 | // Log.e("NavitWatch","poll returned"); |
72 | // Log.e("NavitWatch","poll returned"); |
65 | if (removed) |
73 | if (removed) break; |
66 | break; |
|
|
67 | callback_pending=true; |
74 | callback_pending = true; |
68 | handler.post(callback_runnable); |
75 | handler.post(callback_runnable); |
69 | try { |
76 | try |
|
|
77 | { |
70 | // Log.e("NavitWatch","wait"); |
78 | // Log.e("NavitWatch","wait"); |
71 | synchronized(this) { |
79 | synchronized (this) |
|
|
80 | { |
72 | if (callback_pending) |
81 | if (callback_pending) this.wait(); |
73 | this.wait(); |
|
|
74 | } |
82 | } |
75 | // Log.e("NavitWatch","wait returned"); |
83 | // Log.e("NavitWatch","wait returned"); |
76 | } catch (Exception e) { |
|
|
77 | Log.e("NavitWatch","Exception "+e.getMessage()); |
|
|
78 | } |
84 | } |
|
|
85 | catch (Exception e) |
|
|
86 | { |
|
|
87 | Log.e("NavitWatch", "Exception " + e.getMessage()); |
|
|
88 | } |
79 | if (removed) |
89 | if (removed) break; |
80 | break; |
|
|
81 | } |
90 | } |
82 | } |
91 | } |
|
|
92 | |
83 | public void callback() |
93 | public void callback() |
84 | { |
94 | { |
85 | // Log.e("NavitWatch","Calling Callback"); |
95 | // Log.e("NavitWatch","Calling Callback"); |
86 | if (!removed) |
|
|
87 | WatchCallback(watch_callbackid); |
96 | if (!removed) WatchCallback(watch_callbackid); |
88 | synchronized(this) { |
97 | synchronized (this) |
|
|
98 | { |
89 | callback_pending=false; |
99 | callback_pending = false; |
90 | // Log.e("NavitWatch","Waking up"); |
100 | // Log.e("NavitWatch","Waking up"); |
91 | this.notify(); |
101 | this.notify(); |
92 | } |
102 | } |
93 | } |
103 | } |
|
|
104 | |
94 | public void remove() |
105 | public void remove() |
95 | { |
106 | { |
96 | removed=true; |
107 | removed = true; |
97 | thread.interrupt(); |
108 | thread.interrupt(); |
98 | } |
109 | } |
99 | } |
110 | } |
100 | |
|
|