Fix #2508283 (Improper wrapping of long text in ics attachments)

* Ignore CR and change LF into backslash + n
* Write unit test for handling of CRLF's
* Rename mLineCount to mColumnCount, which is more appropriate

Bug: 2508283
Change-Id: I8b2081aa474cb07b6cb09383ff6ac58a1dab1bba
diff --git a/src/com/android/exchange/utility/SimpleIcsWriter.java b/src/com/android/exchange/utility/SimpleIcsWriter.java
index fa6eaec..ceb95a7 100644
--- a/src/com/android/exchange/utility/SimpleIcsWriter.java
+++ b/src/com/android/exchange/utility/SimpleIcsWriter.java
@@ -22,7 +22,7 @@
     public static final int MAX_LINE_LENGTH = 75;
     public static final int LINE_BREAK_LENGTH = 3;
     public static final String LINE_BREAK = "\r\n\t";
-    int mLineCount = 0;
+    int mColumnCount = 0;
 
     public SimpleIcsWriter() {
         super();
@@ -31,26 +31,33 @@
     private void newLine() {
         write('\r');
         write('\n');
-        mLineCount = 0;
+        mColumnCount = 0;
     }
 
     @Override
     public void write(String str) throws IOException {
         int len = str.length();
-        // Handle the simple case here to avoid unnecessary looping
-        if (mLineCount + len < MAX_LINE_LENGTH) {
-            mLineCount += len;
-            super.write(str);
-            return;
-        }
-        for (int i = 0; i < len; i++, mLineCount++) {
-            if (mLineCount == MAX_LINE_LENGTH) {
+        for (int i = 0; i < len; i++, mColumnCount++) {
+            if (mColumnCount == MAX_LINE_LENGTH) {
                 write('\r');
                 write('\n');
                 write('\t');
-                mLineCount = 0;
+                // Line count will get immediately incremented to one (the tab)
+                mColumnCount = 0;
             }
-            write(str.charAt(i));
+            char c = str.charAt(i);
+            if (c == '\r') {
+                // Ignore CR
+                mColumnCount--;
+                continue;
+            } else if (c == '\n') {
+                // On LF, set to -1, which will immediately get incremented to zero
+                write("\\");
+                write("n");
+                mColumnCount = -1;
+                continue;
+            }
+            write(c);
         }
     }
 
diff --git a/tests/src/com/android/exchange/utility/SimpleIcsWriterTests.java b/tests/src/com/android/exchange/utility/SimpleIcsWriterTests.java
index 212627d..c57291e 100644
--- a/tests/src/com/android/exchange/utility/SimpleIcsWriterTests.java
+++ b/tests/src/com/android/exchange/utility/SimpleIcsWriterTests.java
@@ -37,6 +37,14 @@
     private final String expectedSecondLineBreak =
         string80Chars.charAt(SimpleIcsWriter.MAX_LINE_LENGTH - 1) + SimpleIcsWriter.LINE_BREAK;
 
+    public void testCrlf() throws IOException {
+        SimpleIcsWriter w = new SimpleIcsWriter();
+        w.writeTag("TAG", "A\r\nB\nC\r\nD");
+        String str = w.toString();
+        // Make sure \r's are stripped and that \n is turned into two chars, \ and n
+        assertEquals("TAG:A\\nB\\nC\\nD\r\n", str);
+    }
+
     public void testWriter() throws IOException {
         // Sanity test on constant strings
         assertEquals(63, string63Chars.length());
@@ -49,7 +57,7 @@
         w.writeTag(tag11Chars, string63Chars + string80Chars);
 
         // We should always end a tag on a new line
-        assertEquals(0, w.mLineCount);
+        assertEquals(0, w.mColumnCount);
 
         // Get the final string
         String str = w.toString();