int calculateEncodedLength() {
   if (pluginResult == null) {
     return jsPayloadOrCallbackId.length() + 1;
   }
   int statusLen = String.valueOf(pluginResult.getStatus()).length();
   int ret = 2 + statusLen + 1 + jsPayloadOrCallbackId.length() + 1;
   switch (pluginResult.getMessageType()) {
     case PluginResult.MESSAGE_TYPE_BOOLEAN: // f or t
     case PluginResult.MESSAGE_TYPE_NULL: // N
       ret += 1;
       break;
     case PluginResult.MESSAGE_TYPE_NUMBER: // n
       ret += 1 + pluginResult.getMessage().length();
       break;
     case PluginResult.MESSAGE_TYPE_STRING: // s
       ret += 1 + pluginResult.getStrMessage().length();
       break;
     case PluginResult.MESSAGE_TYPE_BINARYSTRING:
       ret += 1 + pluginResult.getMessage().length();
       break;
     case PluginResult.MESSAGE_TYPE_ARRAYBUFFER:
       ret += 1 + pluginResult.getMessage().length();
       break;
     case PluginResult.MESSAGE_TYPE_JSON:
     default:
       ret += pluginResult.getMessage().length();
   }
   return ret;
 }
    void encodeAsMessage(StringBuilder sb) {
      if (pluginResult == null) {
        sb.append('J').append(jsPayloadOrCallbackId);
        return;
      }
      int status = pluginResult.getStatus();
      boolean noResult = status == PluginResult.Status.NO_RESULT.ordinal();
      boolean resultOk = status == PluginResult.Status.OK.ordinal();
      boolean keepCallback = pluginResult.getKeepCallback();

      sb.append((noResult || resultOk) ? 'S' : 'F')
          .append(keepCallback ? '1' : '0')
          .append(status)
          .append(' ')
          .append(jsPayloadOrCallbackId)
          .append(' ');
      switch (pluginResult.getMessageType()) {
        case PluginResult.MESSAGE_TYPE_BOOLEAN:
          sb.append(pluginResult.getMessage().charAt(0)); // t or f.
          break;
        case PluginResult.MESSAGE_TYPE_NULL: // N
          sb.append('N');
          break;
        case PluginResult.MESSAGE_TYPE_NUMBER: // n
          sb.append('n').append(pluginResult.getMessage());
          break;
        case PluginResult.MESSAGE_TYPE_STRING: // s
          sb.append('s');
          sb.append(pluginResult.getStrMessage());
          break;
        case PluginResult.MESSAGE_TYPE_BINARYSTRING: // S
          sb.append('S');
          sb.append(pluginResult.getMessage());
          break;
        case PluginResult.MESSAGE_TYPE_ARRAYBUFFER: // A
          sb.append('A');
          sb.append(pluginResult.getMessage());
          break;
        case PluginResult.MESSAGE_TYPE_JSON:
        default:
          sb.append(pluginResult.getMessage()); // [ or {
      }
    }
 void encodeAsJsMessage(StringBuilder sb) {
   if (pluginResult == null) {
     sb.append(jsPayloadOrCallbackId);
   } else {
     int status = pluginResult.getStatus();
     boolean success =
         (status == PluginResult.Status.OK.ordinal())
             || (status == PluginResult.Status.NO_RESULT.ordinal());
     sb.append("cordova.callbackFromNative('")
         .append(jsPayloadOrCallbackId)
         .append("',")
         .append(success)
         .append(",")
         .append(status)
         .append(",[")
         .append(pluginResult.getMessage())
         .append("],")
         .append(pluginResult.getKeepCallback())
         .append(");");
   }
 }