Overriding multiline field for subject.

Yes, we do want a multiline field.
So I have to override the IME behavior

Fixes b/7060948 gmail crashed while replying to a message (NPE in com.android.mail.browse.MessageHeaderView.onClick)

Change-Id: I66ceb031dbb2b868acd12fed6d41cc8563a731e9
diff --git a/res/layout-sw600dp/compose.xml b/res/layout-sw600dp/compose.xml
index d725750..c09db79 100644
--- a/res/layout-sw600dp/compose.xml
+++ b/res/layout-sw600dp/compose.xml
@@ -112,7 +112,7 @@
                     android:layout_weight="1"
                     android:layout_width="0dip">
                     <!-- Subject: localization cannot control what field pressing tab will bring the user to. This is controlled at runtime.  -->
-                    <EditText android:id="@+id/subject"
+                    <com.android.mail.compose.EnterSubject android:id="@+id/subject"
                         style="@style/ComposeSubjectView" />
                 </RelativeLayout>
                 <ImageView android:id="@+id/add_photo_attachment"
diff --git a/res/layout/compose_subject.xml b/res/layout/compose_subject.xml
index 5a9834e..f1b16ef 100644
--- a/res/layout/compose_subject.xml
+++ b/res/layout/compose_subject.xml
@@ -19,10 +19,6 @@
     style="@style/ComposeFieldLayout"
     android:layout_below="@id/cc_bcc_wrapper">
         <!-- Subject: localization cannot control what field pressing tab will bring the user to. This is controlled at runtime.  -->
-        <EditText android:id="@+id/subject"
-            android:inputType="textEmailSubject|textAutoCorrect|textCapSentences|textImeMultiLine|textMultiLine"
-            android:hint="@string/subject_hint"
-            android:textColorHint="@color/compose_label_text"
-            android:imeOptions="actionDone|flagNoExtractUi"
+        <com.android.mail.compose.EnterSubject android:id="@+id/subject"
             style="@style/ComposeSubjectView" />
 </RelativeLayout>
diff --git a/src/com/android/mail/compose/EnterSubject.java b/src/com/android/mail/compose/EnterSubject.java
new file mode 100644
index 0000000..60d4c92
--- /dev/null
+++ b/src/com/android/mail/compose/EnterSubject.java
@@ -0,0 +1,56 @@
+/**
+ * Copyright (c) 2011, Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mail.compose;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputConnection;
+import android.widget.EditText;
+
+/**
+ * Allows IME behavior for multiline text fields to be overridden so that
+ * pressing next/enter will take the user to the compose field instead of
+ * creating a newline in this field.
+ */
+public class EnterSubject extends EditText {
+    public EnterSubject(Context context) {
+        super(context);
+    }
+    public EnterSubject(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+    public EnterSubject(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    @Override
+    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
+        InputConnection connection = super.onCreateInputConnection(outAttrs);
+        int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
+        if ((imeActions&EditorInfo.IME_ACTION_NEXT) != 0) {
+            // clear the existing action
+            outAttrs.imeOptions ^= imeActions;
+            // set the DONE action
+            outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT;
+        }
+        if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
+            outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
+        }
+        return connection;
+    }
+}