package io.appium.android.bootstrap.utils;

import java.nio.charset.Charset;


public class UnicodeEncoder {
  private static final Charset M_UTF7 = Charset.forName("x-IMAP-mailbox-name");
  private static final Charset ASCII  = Charset.forName("US-ASCII");


  public static String encode(final String text) {
    byte[] encoded = text.getBytes(M_UTF7);
    return new String(encoded, ASCII);
  }

  public static boolean needsEncoding(final String text) {
    char[] chars = text.toCharArray();
    for (int i = 0; i < chars.length; i++) {
      int cp = Character.codePointAt(chars, i);
      if (cp > 0x7F || cp == '&') {
        // Selenium uses a Unicode PUA to cover certain special characters
        // see https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/Keys.java
        // these should juse be passed through as is.
        return !(cp >= 0xE000 && cp <= 0xE040);
      }
    }
    return false;
  }
}

@@ -71,16 +71,60 @@
 arset;%0A%0A
+import io.appium.android.bootstrap.Logger;%0A%0A
 %0Apublic 
@@ -396,22 +396,28 @@
 7);%0A    
-return
+String ret =
  new Str
@@ -437,16 +437,334 @@
 ASCII);%0A
+    if (ret.charAt(ret.length()-1) != text.charAt(text.length()-1) && !ret.endsWith(%22-%22)) %7B%0A      // in some cases there is a problem and the closing tag is not added%0A      // to the encoded text (for instance, with %60%C3%BC%60)%0A      Logger.debug(%22Closing tag missing. Adding.%22);%0A      ret = ret + %22-%22;%0A    %7D%0A    return ret;%0A
   %7D%0A%0A  p

Make sure encoding closes correctly