annotate src/com/five_ten_sg/connectbot/monitor/MonitorService.java @ 13:5bf6d84cc5b8 stable-1.0.1

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