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