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