diff app/src/main/java/com/five_ten_sg/connectbot/monitor/MonitorService.java @ 31:0bc0b4798d9e

fix saystring(12) command for proper unicode and document it
author Carl Byington <carl@five-ten-sg.com>
date Sun, 28 Apr 2019 14:45:56 -0700
parents 807f7e4eaebe
children
line wrap: on
line diff
--- a/app/src/main/java/com/five_ten_sg/connectbot/monitor/MonitorService.java	Thu Nov 08 11:59:30 2018 -0800
+++ b/app/src/main/java/com/five_ten_sg/connectbot/monitor/MonitorService.java	Sun Apr 28 14:45:56 2019 -0700
@@ -45,6 +45,7 @@
     public  static final char MONITOR_CMD_SHOWURL       = 9;
     public  static final char MONITOR_CMD_SWITCHSESSION = 10;
     public  static final char MONITOR_CMD_CURSORREQUEST = 11;
+    public  static final char MONITOR_CMD_SAYSTRING     = 12;
 
     public  static final char CURSOR_REQUESTED      = 0;
     public  static final char CURSOR_SCREEN_CHANGE  = 1;
@@ -199,12 +200,19 @@
         }
 
         public void speak(byte [] msg, boolean flush, boolean synchronous) {
+            speak(utf8BytesToString(msg), flush, synchronous);
+        }
+
+        public void speak(char [] msg, boolean flush, boolean synchronous) {
+            speak(new String(msg), flush, synchronous);
+        }
+
+        public void speak(String msg, boolean flush, boolean synchronous) {
             if (speech) {
-                String smsg = bytesToString(msg);
                 if (synchronous) {
                     HashMap<String, String> myHashParms = new HashMap();
                     myHashParms.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.format("connection %d", connection));
-                	talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, myHashParms);
+                	talker.speak(msg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, myHashParms);
                     try {
                         String x = talkerQueue.take(); // wait for completion
                     } catch (InterruptedException e) {
@@ -212,20 +220,18 @@
                     }
                 }
                 else {
-            	    talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null);
+            	    talker.speak(msg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null);
                 }
             }
         }
 
-        public String bytesToString(byte[] b) {
-            char[] c = new char[b.length];
-            int bp = 0;
-            for(int i = 0; i < c.length; i++) {
-                byte b1 = 0;
-                byte b2 = b[bp++];
-                c[i] = (char) (((b1 & 0x00FF) << 8) + (b2 & 0x00FF));
+        public String utf8BytesToString(byte[] b) {
+            try {
+                return new String(b, "UTF-8");
             }
-            return new String(c);
+            catch (Exception e) {
+                return "invalid utf-8";
+            }
         }
 
         public char[] bytesToChars(byte[] b, int len) {
@@ -287,7 +293,7 @@
                 }
                 client_out = null;
             }
-        };
+        }
 
         private char[] forceRead(int len) throws IOException {
             int len2 = len*2;
@@ -398,6 +404,11 @@
                             value_queue.clear();
                             value_queue.put(reformatValue(packet));
                             break;
+                        case MONITOR_CMD_SAYSTRING:
+                            buf = new char[plen-3];
+                            System.arraycopy(packet, 3, buf, 0, plen-3);
+                            cleanup(buf);
+                            speak(buf, (packet[1] != 0), (packet[2] != 0));
                         default:
                             break;
                     }
@@ -411,6 +422,7 @@
             }
             Log.i(TAG, String.format("shutting down connection %d", connection));
             try {
+                teClose(connection);
                 if (client_in     != null) client_in.close();
                 if (client_out    != null) client_out.close();
                 if (client_socket != null) client_socket.close();
@@ -457,7 +469,7 @@
         boolean sameinit = false;
         CommunicationThread cm = clients.get(currentConnection);
         if (cm != null) {
-            sameinit = (cm.initString == fn);
+            sameinit = (cm.initString.equals(fn));
         }
         setCurrentConnection(connection);
     }
@@ -535,7 +547,7 @@
             arg[2] = (char) (l & 0x0000ffff);
             arg[3] = (char) (c & 0x0000ffff);
             arg[4] = (char) (len & 0x0000ffff);
-            cm.clientWrite(MONITOR_CMD_GETFIELD, arg);
+            cm.clientWrite(MONITOR_CMD_SCREENWATCH, arg);
         }
     }