Mercurial > 510ConnectbotMonitor
annotate src/com/five_ten_sg/connectbot/monitor/MonitorService.java @ 4:46a9cdf018a1
allow async tts
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 23 Jun 2014 17:09:52 -0700 |
parents | 2be5bca648ab |
children | d1f095e4f8f0 |
rev | line source |
---|---|
0 | 1 package com.five_ten_sg.connectbot.monitor; |
2 | |
3 import java.io.IOException; | |
4 import java.io.InputStream; | |
5 import java.io.OutputStream; | |
6 import java.net.ServerSocket; | |
7 import java.net.Socket; | |
8 import java.util.concurrent.ArrayBlockingQueue; | |
9 import java.util.concurrent.BlockingQueue; | |
10 import java.util.concurrent.ConcurrentHashMap; | |
11 import java.util.Locale; | |
12 | |
13 import android.app.Activity; | |
14 import android.app.Service; | |
15 import android.content.Context; | |
16 import android.content.Intent; | |
17 import android.content.ServiceConnection; | |
18 import android.net.wifi.WifiManager.WifiLock; | |
19 import android.net.wifi.WifiManager; | |
20 import android.os.Binder; | |
21 import android.os.Bundle; | |
22 import android.os.Handler; | |
23 import android.os.IBinder; | |
24 import android.os.Message; | |
25 import android.os.PowerManager; | |
26 import android.speech.tts.TextToSpeech; | |
27 import android.speech.tts.TextToSpeech.OnInitListener; | |
28 import android.util.Log; | |
29 import android.widget.TextView; | |
30 | |
31 public class MonitorService extends Service implements OnInitListener { | |
32 public final static String TAG = "ConnectBot.MonitorService"; | |
33 | |
34 public static final char MONITOR_CMD_INIT = 0; | |
35 public static final char MONITOR_CMD_ACTIVATE = 1; | |
2 | 36 public static final char MONITOR_CMD_KEYSTATE = 2; |
0 | 37 public static final char MONITOR_CMD_CURSORMOVE = 3; |
38 public static final char MONITOR_CMD_SCREENCHANGE = 4; | |
39 public static final char MONITOR_CMD_FIELDVALUE = 5; | |
40 public static final char MONITOR_CMD_SETFIELD = 5; | |
41 public static final char MONITOR_CMD_GETFIELD = 6; | |
42 public static final char MONITOR_CMD_SCREENWATCH = 7; | |
2 | 43 public static final char MONITOR_CMD_DEPRESS = 8; |
0 | 44 |
45 public static final int MONITORPORT = 6000; | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
46 public static ConcurrentHashMap<Integer,CommunicationThread> clients = new ConcurrentHashMap<Integer,CommunicationThread>(); |
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
47 public static int currentConnection = -1; |
0 | 48 |
49 private boolean speech = false; | |
50 private TextToSpeech talker = null; | |
51 public Handler handler = null; | |
52 private ServerSocket serverSocket; | |
53 private Thread serverThread = null; | |
54 private WifiManager.WifiLock wifiLock; | |
55 private PowerManager.WakeLock wakeLock; | |
56 final private IBinder binder = new MonitorBinder(); | |
57 | |
58 | |
59 public class MonitorBinder extends Binder { | |
60 public MonitorService getService() { | |
61 return MonitorService.this; | |
62 } | |
63 } | |
64 | |
65 @Override | |
66 public void onInit(int status) { | |
67 if (status == TextToSpeech.SUCCESS) { | |
68 talker.setLanguage(Locale.US); | |
69 speech = true; | |
70 } | |
71 } | |
72 | |
73 @Override | |
74 public void onCreate() { | |
75 WifiManager wMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE); | |
76 wifiLock = wMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, "MyWifiLock"); | |
77 wifiLock.acquire(); | |
78 | |
79 PowerManager pMgr = (PowerManager) getSystemService(Context.POWER_SERVICE); | |
80 wakeLock = pMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock"); | |
81 wakeLock.acquire(); | |
82 | |
83 talker = new TextToSpeech(this, this); | |
84 this.serverThread = new Thread(new ServerThread()); | |
85 this.serverThread.start(); | |
86 } | |
87 | |
88 @Override | |
89 public IBinder onBind(Intent intent) { | |
90 startService(new Intent(this, MonitorService.class)); | |
91 return binder; | |
92 } | |
93 | |
94 public void printer(String msg) { | |
95 if (handler != null) handler.sendMessage(handler.obtainMessage(MonitorActivity.MESSAGE_CODE_PRINT, msg)); | |
96 if (speech) talker.speak(msg, TextToSpeech.QUEUE_FLUSH, null); | |
97 } | |
98 | |
99 @Override | |
100 public int onStartCommand(Intent intent, int flags, int startId) { | |
101 Log.i(TAG, "service onStartCommand()"); | |
102 return START_STICKY; | |
103 } | |
104 | |
105 @Override | |
106 public void onDestroy() { | |
107 try { | |
108 Log.i(TAG, "service onDestroy()"); | |
109 talker.stop(); | |
110 talker.shutdown(); | |
111 wifiLock.release(); | |
112 wakeLock.release(); | |
113 serverSocket.close(); | |
114 } catch (IOException e) { | |
115 Log.e(TAG, "exception in onDestroy()", e); | |
116 } | |
117 super.onDestroy(); | |
118 } | |
119 | |
120 class ServerThread extends Thread { | |
121 public void run() { | |
122 Socket socket = null; | |
123 int connection = 0; | |
124 try { | |
125 serverSocket = new ServerSocket(MONITORPORT); | |
126 } catch (IOException e) { | |
127 Log.e(TAG, "exception in ServerThread.run(), cannot create listening socket", e); | |
128 return; | |
129 } | |
130 while (true) { | |
131 try{ | |
132 socket = serverSocket.accept(); | |
133 connection = connection + 1; | |
134 CommunicationThread commThread = new CommunicationThread(connection, socket); | |
135 clients.put(connection, commThread); | |
136 commThread.start(); | |
137 } catch (IOException e) { | |
138 Log.e(TAG, "exception in ServerThread.run(), listening socket closed", e); | |
139 break; | |
140 } | |
141 } | |
142 } | |
143 } | |
144 | |
145 class triple { | |
146 private int l, c; | |
147 private char[] b; | |
148 public triple(int l, int c, char[] b) { | |
149 this.l = l; | |
150 this.c = c; | |
151 this.b = b; | |
152 } | |
153 } | |
154 | |
155 class CommunicationThread extends Thread { | |
156 public int connection; | |
157 private Socket client_socket; | |
158 private InputStream client_in; | |
159 private OutputStream client_out; | |
160 private boolean is_closing = false; | |
161 private BlockingQueue<triple> queue = new ArrayBlockingQueue<triple>(1); | |
162 | |
163 public CommunicationThread(int handle, Socket socket) { | |
164 connection = handle; | |
165 client_socket = socket; | |
166 try { | |
167 client_in = client_socket.getInputStream(); | |
168 client_out = client_socket.getOutputStream(); | |
169 } catch (IOException e) { | |
170 Log.e(TAG, "exception in CommunicationThread() constructor, cannot get socket streams", e); | |
171 } | |
172 } | |
173 | |
4 | 174 public void speak(byte [] msg, boolean flush, boolean synchronous) { |
175 if (speech) { | |
176 String smsg = bytesToString(msg); | |
177 if (synchronous) { | |
178 HashMap<String, String> myHashParms = new HashMap(); | |
179 myHashParms.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.format("connection %d", connection)); | |
180 talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, myHashParms); | |
181 try { | |
182 String x = talkerQueue.take(); // wait for completion | |
183 } catch (InterruptedException e) { | |
184 Log.e(TAG, "exception in cm.speak()", e); | |
185 } | |
186 } | |
187 else { | |
188 talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null); | |
189 } | |
190 } | |
191 } | |
192 | |
193 public String bytesToString(byte[] b) { | |
194 char[] c = new char[b.length]; | |
195 int bp = 0; | |
196 for(int i = 0; i < c.length; i++) { | |
197 byte b1 = 0; | |
198 byte b2 = b[bp++]; | |
199 c[i] = (char) (((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
200 } | |
201 return new String(c); | |
202 } | |
203 | |
0 | 204 public char[] bytesToChars(byte[] b, int len) { |
205 char[] c = new char[len >> 1]; | |
206 int bp = 0; | |
207 for(int i = 0; i < c.length; i++) { | |
208 byte b1 = b[bp++]; | |
209 byte b2 = b[bp++]; | |
210 c[i] = (char) (((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
211 } | |
212 return c; | |
213 } | |
214 | |
215 public byte[] charsToBytes(char[] c) { | |
216 byte[] b = new byte[c.length << 1]; | |
217 int bp = 0; | |
218 for (int i=0; i<c.length; i++) { | |
219 b[bp++] = (byte) ((c[i] & 0xff00) >> 8); | |
220 b[bp++] = (byte) (c[i] & 0x00ff); | |
221 } | |
222 return b; | |
223 } | |
224 | |
225 public synchronized void clientWrite(char cmd, char[] c) { | |
226 try { | |
227 if (client_out != null) { | |
228 c[0] = (char)(c.length - 1); // number of chars following | |
229 c[1] = cmd; | |
230 Log.i(TAG, String.format("sending %d command", (int)cmd)); | |
231 client_out.write(charsToBytes(c)); | |
232 } | |
233 } | |
234 catch (IOException e) { | |
235 Log.e(TAG, "exception in monitorWrite()", e); | |
236 try { | |
237 client_out.close(); | |
238 } | |
239 catch (IOException ee) { | |
240 Log.e(TAG, "exception in monitorWrite() closing socket", ee); | |
241 } | |
242 client_out = null; | |
243 } | |
244 }; | |
245 | |
246 private char[] forceRead(int len) throws IOException { | |
247 int len2 = len*2; | |
248 int off = 0; | |
249 byte[] b = new byte[len2]; | |
250 while (off < len2) { | |
251 int l = client_in.read(b, off, len2-off); | |
252 if (l < 0) { | |
253 is_closing = true; | |
254 throw new IOException("eof"); | |
255 } | |
256 off += l; | |
257 } | |
258 return bytesToChars(b, len2); | |
259 } | |
260 | |
261 public void run() { | |
262 Log.i(TAG, String.format("CommunicationThread.run() client %d connected", connection)); | |
263 while (true) { | |
264 try { | |
265 char[] len = forceRead(1); | |
266 char[] packet = forceRead(len[0]); | |
267 char[] buf; | |
268 char cmd = packet[0]; | |
269 int plen = packet.length; | |
270 //Log.i(TAG, String.format("received %d command length %d", (int)cmd, plen)); | |
271 switch (cmd) { | |
272 case MONITOR_CMD_INIT: | |
273 buf = new char[plen-1]; | |
274 System.arraycopy(packet, 1, buf, 0, plen-1); | |
275 abandonGetField(connection); | |
276 teInit(connection, buf); | |
277 break; | |
278 case MONITOR_CMD_ACTIVATE: | |
279 abandonGetField(connection); | |
280 buf = new char[plen-3]; | |
281 System.arraycopy(packet, 3, buf, 0, plen-3); | |
282 teActivate(connection, packet[1], packet[2], buf); | |
283 break; | |
2 | 284 case MONITOR_CMD_KEYSTATE: |
285 teKeyState(connection, (packet[1] == 1)); | |
0 | 286 break; |
287 case MONITOR_CMD_CURSORMOVE: | |
288 teCursorMove(connection, packet[1], packet[2]); | |
289 break; | |
290 case MONITOR_CMD_SCREENCHANGE: | |
291 buf = new char[plen-3]; | |
292 System.arraycopy(packet, 3, buf, 0, plen-3); | |
293 teScreenChange(connection, packet[1], packet[2], buf); | |
294 break; | |
295 case MONITOR_CMD_FIELDVALUE: | |
296 buf = new char[plen-3]; | |
297 System.arraycopy(packet, 3, buf, 0, plen-3); | |
298 Log.i(TAG, String.format("teFieldValue %d line %d column %d b.len %d", connection, packet[1], packet[2], buf.length)); | |
299 queue.put(new triple(packet[1], packet[2], buf)); | |
300 break; | |
301 default: | |
302 break; | |
303 } | |
304 } catch (IOException e) { | |
305 if (!is_closing) Log.e(TAG, "exception in CommunicationThread.run()", e); | |
306 break; | |
307 } catch (InterruptedException e) { | |
308 Log.e(TAG, "exception in CommunicationThread.run()", e); | |
309 break; | |
310 } | |
311 } | |
312 Log.i(TAG, String.format("shutting down connection %d", connection)); | |
313 try { | |
314 if (client_in != null) client_in.close(); | |
315 if (client_out != null) client_out.close(); | |
316 if (client_socket != null) client_socket.close(); | |
317 } catch (IOException e) { | |
318 Log.e(TAG, "exception in CommunicationThread.run() closing sockets", e); | |
319 } | |
320 client_in = null; | |
321 client_out = null; | |
322 client_socket = null; | |
323 } | |
324 } | |
325 | |
326 private void abandonGetField(int except) { | |
327 for (CommunicationThread cm : clients.values()) { | |
328 if (cm.connection != except) { | |
329 cm.queue.offer(new triple(0, 0, new char[0])); | |
330 } | |
331 } | |
332 } | |
333 | |
334 | |
335 //////////////////////////////////////// | |
336 //// these functions run on the reader thread here and call your monitoring code | |
337 | |
338 public void teInit(int connection, char[] buf) { | |
339 String fn = new String(buf); | |
340 Log.i(TAG, String.format("teInit %d file %s", connection, fn)); | |
2 | 341 //printer(String.format("init %d %s", connection, fn)); |
0 | 342 } |
343 | |
344 public void teActivate(int connection, int lines, int columns, char[] buf) { | |
345 Log.i(TAG, String.format("teActivate %d", connection)); | |
2 | 346 //printer(String.format("activate %d lines %d columns %d b.len %d", connection, lines, columns, buf.length)); |
0 | 347 } |
348 | |
2 | 349 public void teKeyState(int connection, boolean down) { |
350 String d = (down) ? "yes" : "no"; | |
351 Log.i(TAG, String.format("teKeyState %d isdown %s", connection, d)); | |
352 //printer(String.format("keystate %d isdown %s", connection, d)); | |
0 | 353 } |
354 | |
355 public void teCursorMove(int connection, int l, int c) { | |
356 //Log.i(TAG, String.format("teCursorMove %d line %d column %d", connection, l, c)); | |
357 } | |
358 | |
359 public void teScreenChange(int connection, int lines, int columns, char[] buf) { | |
360 Log.i(TAG, String.format("teScreenChange %d lines %d columns %d b.len %d", connection, lines, columns, buf.length)); | |
361 } | |
362 | |
363 | |
364 //////////////////////////////////////// | |
365 //// these functions are called from your monitoring code thread | |
366 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
367 public static void teSetField(int connection, int l, int c, char[] buf) { |
0 | 368 int len = buf.length; |
369 Log.i(TAG, String.format("teSetField %d request line %d column %d len %d", connection, l, c, len)); | |
370 CommunicationThread cm = clients.get(connection); | |
371 if (cm != null) { | |
372 char[] arg2 = new char[4 + len]; | |
373 arg2[2] = (char) (l & 0x0000ffff); | |
374 arg2[3] = (char) (c & 0x0000ffff); | |
375 int base = 4; | |
376 System.arraycopy(buf, 0, arg2, base, len); | |
377 cm.clientWrite(MONITOR_CMD_SETFIELD, arg2); | |
378 } | |
379 } | |
380 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
381 public static char[] teGetField(int connection, int l, int c, int len) { |
0 | 382 Log.i(TAG, String.format("teGetField %d request line %d column %d len %d", connection, l, c, len)); |
383 CommunicationThread cm = clients.get(connection); | |
384 if (cm != null) { | |
385 char[] arg = new char[5]; | |
386 arg[2] = (char) (l & 0x0000ffff); | |
387 arg[3] = (char) (c & 0x0000ffff); | |
388 arg[4] = (char) (len & 0x0000ffff); | |
389 cm.queue.clear(); // we never have more than one outstanding getfield request on the connection | |
390 cm.clientWrite(MONITOR_CMD_GETFIELD, arg); | |
391 try { | |
392 triple t = cm.queue.take(); // wait for response | |
393 Log.i(TAG, String.format("teGetField %d response line %d column %d len %d", connection, t.l, t.c, t.b.length)); | |
394 return t.b; | |
395 } catch (InterruptedException e) { | |
396 Log.e(TAG, "exception in teGetField(), return empty string", e); | |
397 } | |
398 } | |
399 return new char[0]; | |
400 } | |
401 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
402 public static void teScreenWatch(int connection, int l, int c, int len) { |
0 | 403 Log.i(TAG, String.format("teScreenWatch %d request line %d column %d len %d", connection, l, c, len)); |
404 CommunicationThread cm = clients.get(connection); | |
405 if (cm != null) { | |
406 char[] arg = new char[5]; | |
407 arg[2] = (char) (l & 0x0000ffff); | |
408 arg[3] = (char) (c & 0x0000ffff); | |
409 arg[4] = (char) (len & 0x0000ffff); | |
410 cm.clientWrite(MONITOR_CMD_GETFIELD, arg); | |
411 } | |
412 } | |
413 | |
4 | 414 public static void teSpeak(int connection, String msg, boolean flush, boolean synchronous) { |
415 CommunicationThread cm = clients.get(connection); | |
416 if (cm != null) cm.speak(msg, flush, synchronous); | |
0 | 417 } |
2 | 418 |
419 public static void teDepress(int connection, int vk_key) { | |
420 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731 | |
421 Log.i(TAG, String.format("teDepress %d, %d", connection, vk_key)); | |
422 CommunicationThread cm = clients.get(connection); | |
423 if (cm != null) { | |
424 char[] arg = new char[3]; | |
425 arg[2] = (char) (vk_key & 0x0000ffff); | |
426 cm.clientWrite(MONITOR_CMD_DEPRESS, arg); | |
427 } | |
428 } | |
0 | 429 } |