Revert "Bug 5248215: Even though I turned off the Spelling correction, it still shows up"

This reverts commit 6e405f84b8d0c7c0f939bc8e9bec17e65e704a2d.

Change-Id: I9593273d414aee1f9817f84b462cf45360ff19e3
diff --git a/core/java/android/widget/SpellChecker.java b/core/java/android/widget/SpellChecker.java
index 6b2f3e4..14cbf6f 100644
--- a/core/java/android/widget/SpellChecker.java
+++ b/core/java/android/widget/SpellChecker.java
@@ -75,20 +75,6 @@
         mLength = 0;
     }
 
-    /**
-     * @return true if a spell checker session has successfully been created. Returns false if not,
-     * for instance when spell checking has been disabled in settings.
-     */
-    public boolean isSessionActive() {
-        return mSpellCheckerSession != null;
-    }
-
-    public void closeSession() {
-        if (mSpellCheckerSession != null) {
-            mSpellCheckerSession.close();
-        }
-    }
-
     public void addSpellCheckSpan(SpellCheckSpan spellCheckSpan) {
         int length = mIds.length;
         if (mLength >= length) {
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 204c94a..8db6592 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -3250,7 +3250,7 @@
         sendOnTextChanged(text, 0, oldlen, textLength);
         onTextChanged(text, 0, oldlen, textLength);
 
-        if (startSpellCheck && getSpellChecker().isSessionActive()) {
+        if (startSpellCheck) {
             updateSpellCheckSpans(0, textLength);
         }
 
@@ -7595,16 +7595,14 @@
             }
             ims.mChangedDelta += after-before;
         }
-
+        
         sendOnTextChanged(buffer, start, before, after);
         onTextChanged(buffer, start, before, after);
 
-        if (getSpellChecker().isSessionActive()) {
-            // The WordIterator text change listener may be called after this one.
-            // Make sure this changed text is rescanned before the iterator is used on it.
-            getWordIterator().forceUpdate();
-            updateSpellCheckSpans(start, start + after);
-        }
+        // The WordIterator text change listener may be called after this one.
+        // Make sure this changed text is rescanned before the iterator is used on it.
+        getWordIterator().forceUpdate();
+        updateSpellCheckSpans(start, start + after);
 
         // Hide the controllers if the amount of content changed
         if (before != after) {
@@ -8256,10 +8254,6 @@
                 mBlink.uncancel();
                 makeBlink();
             }
-            if (getSpellChecker().isSessionActive() && (mSuggestionsPopupWindow == null ||
-                        !mSuggestionsPopupWindow.mSuggestionPopupWindowVisible)) {
-                updateSpellCheckSpans(0, mText.length());
-            }
         } else {
             if (mBlink != null) {
                 mBlink.cancel();
@@ -8271,16 +8265,6 @@
             }
 
             hideControllers();
-
-            if (mSpellChecker != null && (mSuggestionsPopupWindow == null ||
-                    !mSuggestionsPopupWindow.mSuggestionPopupWindowVisible)) {
-                mSpellChecker.closeSession();
-                removeMisspelledSpans();
-                // Forces the creation of a new SpellChecker next time this window if focused.
-                // Will handle the cases where the service has been enabled or disabled in
-                // settings in the meantime.
-                mSpellChecker = null;
-            }
         }
 
         startStopMarquee(hasWindowFocus);
@@ -8443,31 +8427,13 @@
                 int flags = suggestionSpans[i].getFlags();
                 if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
                         && (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) {
-                    flags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
+                    flags = flags & ~SuggestionSpan.FLAG_EASY_CORRECT;
                     suggestionSpans[i].setFlags(flags);
                 }
             }
         }
     }
 
-    /**
-     * Removes the suggestion spans for misspelled words.
-     */
-    private void removeMisspelledSpans() {
-        if (mText instanceof Spannable) {
-            Spannable spannable = (Spannable) mText;
-            SuggestionSpan[] suggestionSpans = spannable.getSpans(0,
-                    spannable.length(), SuggestionSpan.class);
-            for (int i = 0; i < suggestionSpans.length; i++) {
-                int flags = suggestionSpans[i].getFlags();
-                if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
-                        && (flags & SuggestionSpan.FLAG_MISSPELLED) != 0) {
-                    spannable.removeSpan(suggestionSpans[i]);
-                }
-            }
-        }
-    }
-
     @Override
     public boolean onGenericMotionEvent(MotionEvent event) {
         if (mMovement != null && mText instanceof Spannable && mLayout != null) {
@@ -9603,11 +9569,11 @@
         private SuggestionInfo[] mSuggestionInfos;
         private int mNumberOfSuggestions;
         private boolean mCursorWasVisibleBeforeSuggestions;
-        private boolean mSuggestionPopupWindowVisible;
         private SuggestionAdapter mSuggestionsAdapter;
         private final Comparator<SuggestionSpan> mSuggestionSpanComparator;
         private final HashMap<SuggestionSpan, Integer> mSpansLengths;
 
+
         private class CustomPopupWindow extends PopupWindow {
             public CustomPopupWindow(Context context, int defStyle) {
                 super(context, null, defStyle);
@@ -9615,13 +9581,13 @@
 
             @Override
             public void dismiss() {
-                mSuggestionPopupWindowVisible = false;
                 super.dismiss();
 
                 TextView.this.getPositionListener().removeSubscriber(SuggestionsPopupWindow.this);
 
-                // Safe cast since show() checks that mText is an Editable
-                ((Spannable) mText).removeSpan(mSuggestionRangeSpan);
+                if ((mText instanceof Spannable)) {
+                    ((Spannable) mText).removeSpan(mSuggestionRangeSpan);
+                }
 
                 setCursorVisible(mCursorWasVisibleBeforeSuggestions);
                 if (hasInsertionController()) {
@@ -9671,8 +9637,8 @@
             void removeMisspelledFlag() {
                 int suggestionSpanFlags = suggestionSpan.getFlags();
                 if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) > 0) {
-                    suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED;
-                    suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
+                    suggestionSpanFlags &= ~(SuggestionSpan.FLAG_MISSPELLED);
+                    suggestionSpanFlags &= ~(SuggestionSpan.FLAG_EASY_CORRECT);
                     suggestionSpan.setFlags(suggestionSpanFlags);
                 }
             }
@@ -9759,7 +9725,6 @@
 
             if (updateSuggestions()) {
                 mCursorWasVisibleBeforeSuggestions = mCursorVisible;
-                mSuggestionPopupWindowVisible = true;
                 setCursorVisible(false);
                 super.show();
             }
@@ -10555,7 +10520,9 @@
 
         public abstract int getCurrentCursorOffset();
 
-        protected abstract void updateSelection(int offset);
+        protected void updateSelection(int offset) {
+            updateDrawable();
+        }
 
         public abstract void updatePosition(float x, float y);
 
@@ -10829,8 +10796,8 @@
 
         @Override
         public void updateSelection(int offset) {
+            super.updateSelection(offset);
             Selection.setSelection((Spannable) mText, offset, getSelectionEnd());
-            updateDrawable();
         }
 
         @Override
@@ -10871,8 +10838,8 @@
 
         @Override
         public void updateSelection(int offset) {
+            super.updateSelection(offset);
             Selection.setSelection((Spannable) mText, getSelectionStart(), offset);
-            updateDrawable();
         }
 
         @Override