1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2008 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
package com.zoffcc.applications.zanavi;
|
21 |
|
22 |
import java.lang.Thread;
|
23 |
import android.os.Handler;
|
24 |
import android.os.Message;
|
25 |
import android.util.Log;
|
26 |
|
27 |
public class NavitWatch implements Runnable
|
28 |
{
|
29 |
private Thread thread;
|
30 |
private static Handler handler = new Handler()
|
31 |
{
|
32 |
public void handleMessage(Message m)
|
33 |
{
|
34 |
Log.e("NavitWatch", "Handler received message");
|
35 |
}
|
36 |
};
|
37 |
private boolean removed;
|
38 |
private int watch_fd;
|
39 |
private int watch_cond;
|
40 |
private int watch_callbackid;
|
41 |
private boolean callback_pending;
|
42 |
private Runnable callback_runnable;
|
43 |
|
44 |
public native void poll(int fd, int cond);
|
45 |
|
46 |
public native void WatchCallback(int id);
|
47 |
|
48 |
NavitWatch(int fd, int cond, int callbackid)
|
49 |
{
|
50 |
// Log.e("NavitWatch","Creating new thread for "+fd+" "+cond+" from current thread " + java.lang.Thread.currentThread().getName());
|
51 |
watch_fd = fd;
|
52 |
watch_cond = cond;
|
53 |
watch_callbackid = callbackid;
|
54 |
final NavitWatch navitwatch = this;
|
55 |
callback_runnable = new Runnable()
|
56 |
{
|
57 |
public void run()
|
58 |
{
|
59 |
navitwatch.callback();
|
60 |
}
|
61 |
};
|
62 |
thread = new Thread(this, "poll thread");
|
63 |
thread.start();
|
64 |
}
|
65 |
|
66 |
public void run()
|
67 |
{
|
68 |
for (;;)
|
69 |
{
|
70 |
// Log.e("NavitWatch","Polling "+watch_fd+" "+watch_cond + " from " + java.lang.Thread.currentThread().getName());
|
71 |
poll(watch_fd, watch_cond);
|
72 |
// Log.e("NavitWatch","poll returned");
|
73 |
if (removed) break;
|
74 |
callback_pending = true;
|
75 |
handler.post(callback_runnable);
|
76 |
try
|
77 |
{
|
78 |
// Log.e("NavitWatch","wait");
|
79 |
synchronized (this)
|
80 |
{
|
81 |
if (callback_pending) this.wait();
|
82 |
}
|
83 |
// Log.e("NavitWatch","wait returned");
|
84 |
}
|
85 |
catch (Exception e)
|
86 |
{
|
87 |
Log.e("NavitWatch", "Exception " + e.getMessage());
|
88 |
}
|
89 |
if (removed) break;
|
90 |
}
|
91 |
}
|
92 |
|
93 |
public void callback()
|
94 |
{
|
95 |
// Log.e("NavitWatch","Calling Callback");
|
96 |
if (!removed) WatchCallback(watch_callbackid);
|
97 |
synchronized (this)
|
98 |
{
|
99 |
callback_pending = false;
|
100 |
// Log.e("NavitWatch","Waking up");
|
101 |
this.notify();
|
102 |
}
|
103 |
}
|
104 |
|
105 |
public void remove()
|
106 |
{
|
107 |
removed = true;
|
108 |
thread.interrupt();
|
109 |
}
|
110 |
}
|