Applications crash observed while selecting words from suggestion list

While selecting words from suggestion list, applications are crashing as
textview span are going out of index. For textinput where spaces are not
allowed length difference shall be calculated properly to avoid Index
out of bound situations.

Change-Id: I360318cf9241abbb7f58ad1ddf3f3c8d364f17d1
CRs-Fixed: 642057
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 5c8c1d5..cd419a4 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -2761,7 +2761,13 @@
 
                 // Restore previous SuggestionSpans
                 final int realSuggestionLength = mTextView.getText().toString().length();
-                final int lengthDifference = realSuggestionLength - (spanEnd - spanStart);
+                int lengthDifference = suggestion.length() - (spanEnd - spanStart);
+                final int realSuggestionDiff = realSuggestionLength - (spanEnd - spanStart);
+
+                if (realSuggestionDiff < lengthDifference) {
+                    lengthDifference = realSuggestionDiff;
+                }
+
                 for (int i = 0; i < length; i++) {
                     // Only spans that include the modified region make sense after replacement
                     // Spans partially included in the replaced region are removed, there is no