SpellChecker.java revision 5d6b6f2892c90e95ef3bc650a245a5f2ca021d38
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.content.Context;
20import android.text.Editable;
21import android.text.Selection;
22import android.text.Spanned;
23import android.text.TextUtils;
24import android.text.method.WordIterator;
25import android.text.style.SpellCheckSpan;
26import android.text.style.SuggestionSpan;
27import android.util.Log;
28import android.util.LruCache;
29import android.view.textservice.SentenceSuggestionsInfo;
30import android.view.textservice.SpellCheckerSession;
31import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
32import android.view.textservice.SuggestionsInfo;
33import android.view.textservice.TextInfo;
34import android.view.textservice.TextServicesManager;
35
36import com.android.internal.util.ArrayUtils;
37import com.android.internal.util.GrowingArrayUtils;
38
39import java.text.BreakIterator;
40import java.util.Locale;
41
42
43/**
44 * Helper class for TextView. Bridge between the TextView and the Dictionary service.
45 *
46 * @hide
47 */
48public class SpellChecker implements SpellCheckerSessionListener {
49    private static final String TAG = SpellChecker.class.getSimpleName();
50    private static final boolean DBG = false;
51
52    // No more than this number of words will be parsed on each iteration to ensure a minimum
53    // lock of the UI thread
54    public static final int MAX_NUMBER_OF_WORDS = 50;
55
56    // Rough estimate, such that the word iterator interval usually does not need to be shifted
57    public static final int AVERAGE_WORD_LENGTH = 7;
58
59    // When parsing, use a character window of that size. Will be shifted if needed
60    public static final int WORD_ITERATOR_INTERVAL = AVERAGE_WORD_LENGTH * MAX_NUMBER_OF_WORDS;
61
62    // Pause between each spell check to keep the UI smooth
63    private final static int SPELL_PAUSE_DURATION = 400; // milliseconds
64
65    private static final int MIN_SENTENCE_LENGTH = 50;
66
67    private static final int USE_SPAN_RANGE = -1;
68
69    private final TextView mTextView;
70
71    SpellCheckerSession mSpellCheckerSession;
72    // We assume that the sentence level spell check will always provide better results than words.
73    // Although word SC has a sequential option.
74    private boolean mIsSentenceSpellCheckSupported;
75    final int mCookie;
76
77    // Paired arrays for the (id, spellCheckSpan) pair. A negative id means the associated
78    // SpellCheckSpan has been recycled and can be-reused.
79    // Contains null SpellCheckSpans after index mLength.
80    private int[] mIds;
81    private SpellCheckSpan[] mSpellCheckSpans;
82    // The mLength first elements of the above arrays have been initialized
83    private int mLength;
84
85    // Parsers on chunk of text, cutting text into words that will be checked
86    private SpellParser[] mSpellParsers = new SpellParser[0];
87
88    private int mSpanSequenceCounter = 0;
89
90    private Locale mCurrentLocale;
91
92    // Shared by all SpellParsers. Cannot be shared with TextView since it may be used
93    // concurrently due to the asynchronous nature of onGetSuggestions.
94    private WordIterator mWordIterator;
95
96    private TextServicesManager mTextServicesManager;
97
98    private Runnable mSpellRunnable;
99
100    private static final int SUGGESTION_SPAN_CACHE_SIZE = 10;
101    private final LruCache<Long, SuggestionSpan> mSuggestionSpanCache =
102            new LruCache<Long, SuggestionSpan>(SUGGESTION_SPAN_CACHE_SIZE);
103
104    public SpellChecker(TextView textView) {
105        mTextView = textView;
106
107        // Arbitrary: these arrays will automatically double their sizes on demand
108        final int size = 1;
109        mIds = ArrayUtils.newUnpaddedIntArray(size);
110        mSpellCheckSpans = new SpellCheckSpan[mIds.length];
111
112        setLocale(mTextView.getSpellCheckerLocale());
113
114        mCookie = hashCode();
115    }
116
117    private void resetSession() {
118        closeSession();
119
120        mTextServicesManager = (TextServicesManager) mTextView.getContext().
121                getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
122        if (!mTextServicesManager.isSpellCheckerEnabled()
123                || mCurrentLocale == null
124                || mTextServicesManager.getCurrentSpellCheckerSubtype(true) == null) {
125            mSpellCheckerSession = null;
126        } else {
127            mSpellCheckerSession = mTextServicesManager.newSpellCheckerSession(
128                    null /* Bundle not currently used by the textServicesManager */,
129                    mCurrentLocale, this,
130                    false /* means any available languages from current spell checker */);
131            mIsSentenceSpellCheckSupported = true;
132        }
133
134        // Restore SpellCheckSpans in pool
135        for (int i = 0; i < mLength; i++) {
136            mIds[i] = -1;
137        }
138        mLength = 0;
139
140        // Remove existing misspelled SuggestionSpans
141        mTextView.removeMisspelledSpans((Editable) mTextView.getText());
142        mSuggestionSpanCache.evictAll();
143    }
144
145    private void setLocale(Locale locale) {
146        mCurrentLocale = locale;
147
148        resetSession();
149
150        if (locale != null) {
151            // Change SpellParsers' wordIterator locale
152            mWordIterator = new WordIterator(locale);
153        }
154
155        // This class is the listener for locale change: warn other locale-aware objects
156        mTextView.onLocaleChanged();
157    }
158
159    /**
160     * @return true if a spell checker session has successfully been created. Returns false if not,
161     * for instance when spell checking has been disabled in settings.
162     */
163    private boolean isSessionActive() {
164        return mSpellCheckerSession != null;
165    }
166
167    public void closeSession() {
168        if (mSpellCheckerSession != null) {
169            mSpellCheckerSession.close();
170        }
171
172        final int length = mSpellParsers.length;
173        for (int i = 0; i < length; i++) {
174            mSpellParsers[i].stop();
175        }
176
177        if (mSpellRunnable != null) {
178            mTextView.removeCallbacks(mSpellRunnable);
179        }
180    }
181
182    private int nextSpellCheckSpanIndex() {
183        for (int i = 0; i < mLength; i++) {
184            if (mIds[i] < 0) return i;
185        }
186
187        mIds = GrowingArrayUtils.append(mIds, mLength, 0);
188        mSpellCheckSpans = GrowingArrayUtils.append(
189                mSpellCheckSpans, mLength, new SpellCheckSpan());
190        mLength++;
191        return mLength - 1;
192    }
193
194    private void addSpellCheckSpan(Editable editable, int start, int end) {
195        final int index = nextSpellCheckSpanIndex();
196        SpellCheckSpan spellCheckSpan = mSpellCheckSpans[index];
197        editable.setSpan(spellCheckSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
198        spellCheckSpan.setSpellCheckInProgress(false);
199        mIds[index] = mSpanSequenceCounter++;
200    }
201
202    public void onSpellCheckSpanRemoved(SpellCheckSpan spellCheckSpan) {
203        // Recycle any removed SpellCheckSpan (from this code or during text edition)
204        for (int i = 0; i < mLength; i++) {
205            if (mSpellCheckSpans[i] == spellCheckSpan) {
206                mIds[i] = -1;
207                return;
208            }
209        }
210    }
211
212    public void onSelectionChanged() {
213        spellCheck();
214    }
215
216    public void spellCheck(int start, int end) {
217        if (DBG) {
218            Log.d(TAG, "Start spell-checking: " + start + ", " + end);
219        }
220        final Locale locale = mTextView.getSpellCheckerLocale();
221        final boolean isSessionActive = isSessionActive();
222        if (locale == null || mCurrentLocale == null || (!(mCurrentLocale.equals(locale)))) {
223            setLocale(locale);
224            // Re-check the entire text
225            start = 0;
226            end = mTextView.getText().length();
227        } else {
228            final boolean spellCheckerActivated = mTextServicesManager.isSpellCheckerEnabled();
229            if (isSessionActive != spellCheckerActivated) {
230                // Spell checker has been turned of or off since last spellCheck
231                resetSession();
232            }
233        }
234
235        if (!isSessionActive) return;
236
237        // Find first available SpellParser from pool
238        final int length = mSpellParsers.length;
239        for (int i = 0; i < length; i++) {
240            final SpellParser spellParser = mSpellParsers[i];
241            if (spellParser.isFinished()) {
242                spellParser.parse(start, end);
243                return;
244            }
245        }
246
247        if (DBG) {
248            Log.d(TAG, "new spell parser.");
249        }
250        // No available parser found in pool, create a new one
251        SpellParser[] newSpellParsers = new SpellParser[length + 1];
252        System.arraycopy(mSpellParsers, 0, newSpellParsers, 0, length);
253        mSpellParsers = newSpellParsers;
254
255        SpellParser spellParser = new SpellParser();
256        mSpellParsers[length] = spellParser;
257        spellParser.parse(start, end);
258    }
259
260    private void spellCheck() {
261        if (mSpellCheckerSession == null) return;
262
263        Editable editable = (Editable) mTextView.getText();
264        final int selectionStart = Selection.getSelectionStart(editable);
265        final int selectionEnd = Selection.getSelectionEnd(editable);
266
267        TextInfo[] textInfos = new TextInfo[mLength];
268        int textInfosCount = 0;
269
270        for (int i = 0; i < mLength; i++) {
271            final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[i];
272            if (mIds[i] < 0 || spellCheckSpan.isSpellCheckInProgress()) continue;
273
274            final int start = editable.getSpanStart(spellCheckSpan);
275            final int end = editable.getSpanEnd(spellCheckSpan);
276
277            // Do not check this word if the user is currently editing it
278            final boolean isEditing;
279            if (mIsSentenceSpellCheckSupported) {
280                // Allow the overlap of the cursor and the first boundary of the spell check span
281                // no to skip the spell check of the following word because the
282                // following word will never be spell-checked even if the user finishes composing
283                isEditing = selectionEnd <= start || selectionStart > end;
284            } else {
285                isEditing = selectionEnd < start || selectionStart > end;
286            }
287            if (start >= 0 && end > start && isEditing) {
288                spellCheckSpan.setSpellCheckInProgress(true);
289                final TextInfo textInfo = new TextInfo(editable, start, end, mCookie, mIds[i]);
290                textInfos[textInfosCount++] = textInfo;
291                if (DBG) {
292                    Log.d(TAG, "create TextInfo: (" + i + "/" + mLength + ") text = "
293                            + textInfo.getSequence() + ", cookie = " + mCookie + ", seq = "
294                            + mIds[i] + ", sel start = " + selectionStart + ", sel end = "
295                            + selectionEnd + ", start = " + start + ", end = " + end);
296                }
297            }
298        }
299
300        if (textInfosCount > 0) {
301            if (textInfosCount < textInfos.length) {
302                TextInfo[] textInfosCopy = new TextInfo[textInfosCount];
303                System.arraycopy(textInfos, 0, textInfosCopy, 0, textInfosCount);
304                textInfos = textInfosCopy;
305            }
306
307            if (mIsSentenceSpellCheckSupported) {
308                mSpellCheckerSession.getSentenceSuggestions(
309                        textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE);
310            } else {
311                mSpellCheckerSession.getSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE,
312                        false /* TODO Set sequentialWords to true for initial spell check */);
313            }
314        }
315    }
316
317    private SpellCheckSpan onGetSuggestionsInternal(
318            SuggestionsInfo suggestionsInfo, int offset, int length) {
319        if (suggestionsInfo == null || suggestionsInfo.getCookie() != mCookie) {
320            return null;
321        }
322        final Editable editable = (Editable) mTextView.getText();
323        final int sequenceNumber = suggestionsInfo.getSequence();
324        for (int k = 0; k < mLength; ++k) {
325            if (sequenceNumber == mIds[k]) {
326                final int attributes = suggestionsInfo.getSuggestionsAttributes();
327                final boolean isInDictionary =
328                        ((attributes & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) > 0);
329                final boolean looksLikeTypo =
330                        ((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0);
331
332                final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[k];
333                //TODO: we need to change that rule for results from a sentence-level spell
334                // checker that will probably be in dictionary.
335                if (!isInDictionary && looksLikeTypo) {
336                    createMisspelledSuggestionSpan(
337                            editable, suggestionsInfo, spellCheckSpan, offset, length);
338                } else {
339                    // Valid word -- isInDictionary || !looksLikeTypo
340                    if (mIsSentenceSpellCheckSupported) {
341                        // Allow the spell checker to remove existing misspelled span by
342                        // overwriting the span over the same place
343                        final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
344                        final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
345                        final int start;
346                        final int end;
347                        if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
348                            start = spellCheckSpanStart + offset;
349                            end = start + length;
350                        } else {
351                            start = spellCheckSpanStart;
352                            end = spellCheckSpanEnd;
353                        }
354                        if (spellCheckSpanStart >= 0 && spellCheckSpanEnd > spellCheckSpanStart
355                                && end > start) {
356                            final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
357                            final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
358                            if (tempSuggestionSpan != null) {
359                                if (DBG) {
360                                    Log.i(TAG, "Remove existing misspelled span. "
361                                            + editable.subSequence(start, end));
362                                }
363                                editable.removeSpan(tempSuggestionSpan);
364                                mSuggestionSpanCache.remove(key);
365                            }
366                        }
367                    }
368                }
369                return spellCheckSpan;
370            }
371        }
372        return null;
373    }
374
375    @Override
376    public void onGetSuggestions(SuggestionsInfo[] results) {
377        final Editable editable = (Editable) mTextView.getText();
378        for (int i = 0; i < results.length; ++i) {
379            final SpellCheckSpan spellCheckSpan =
380                    onGetSuggestionsInternal(results[i], USE_SPAN_RANGE, USE_SPAN_RANGE);
381            if (spellCheckSpan != null) {
382                // onSpellCheckSpanRemoved will recycle this span in the pool
383                editable.removeSpan(spellCheckSpan);
384            }
385        }
386        scheduleNewSpellCheck();
387    }
388
389    @Override
390    public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
391        final Editable editable = (Editable) mTextView.getText();
392
393        for (int i = 0; i < results.length; ++i) {
394            final SentenceSuggestionsInfo ssi = results[i];
395            if (ssi == null) {
396                continue;
397            }
398            SpellCheckSpan spellCheckSpan = null;
399            for (int j = 0; j < ssi.getSuggestionsCount(); ++j) {
400                final SuggestionsInfo suggestionsInfo = ssi.getSuggestionsInfoAt(j);
401                if (suggestionsInfo == null) {
402                    continue;
403                }
404                final int offset = ssi.getOffsetAt(j);
405                final int length = ssi.getLengthAt(j);
406                final SpellCheckSpan scs = onGetSuggestionsInternal(
407                        suggestionsInfo, offset, length);
408                if (spellCheckSpan == null && scs != null) {
409                    // the spellCheckSpan is shared by all the "SuggestionsInfo"s in the same
410                    // SentenceSuggestionsInfo. Removal is deferred after this loop.
411                    spellCheckSpan = scs;
412                }
413            }
414            if (spellCheckSpan != null) {
415                // onSpellCheckSpanRemoved will recycle this span in the pool
416                editable.removeSpan(spellCheckSpan);
417            }
418        }
419        scheduleNewSpellCheck();
420    }
421
422    private void scheduleNewSpellCheck() {
423        if (DBG) {
424            Log.i(TAG, "schedule new spell check.");
425        }
426        if (mSpellRunnable == null) {
427            mSpellRunnable = new Runnable() {
428                @Override
429                public void run() {
430                    final int length = mSpellParsers.length;
431                    for (int i = 0; i < length; i++) {
432                        final SpellParser spellParser = mSpellParsers[i];
433                        if (!spellParser.isFinished()) {
434                            spellParser.parse();
435                            break; // run one spell parser at a time to bound running time
436                        }
437                    }
438                }
439            };
440        } else {
441            mTextView.removeCallbacks(mSpellRunnable);
442        }
443
444        mTextView.postDelayed(mSpellRunnable, SPELL_PAUSE_DURATION);
445    }
446
447    private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo,
448            SpellCheckSpan spellCheckSpan, int offset, int length) {
449        final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
450        final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
451        if (spellCheckSpanStart < 0 || spellCheckSpanEnd <= spellCheckSpanStart)
452            return; // span was removed in the meantime
453
454        final int start;
455        final int end;
456        if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
457            start = spellCheckSpanStart + offset;
458            end = start + length;
459        } else {
460            start = spellCheckSpanStart;
461            end = spellCheckSpanEnd;
462        }
463
464        final int suggestionsCount = suggestionsInfo.getSuggestionsCount();
465        String[] suggestions;
466        if (suggestionsCount > 0) {
467            suggestions = new String[suggestionsCount];
468            for (int i = 0; i < suggestionsCount; i++) {
469                suggestions[i] = suggestionsInfo.getSuggestionAt(i);
470            }
471        } else {
472            suggestions = ArrayUtils.emptyArray(String.class);
473        }
474
475        SuggestionSpan suggestionSpan = new SuggestionSpan(mTextView.getContext(), suggestions,
476                SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
477        // TODO: Remove mIsSentenceSpellCheckSupported by extracting an interface
478        // to share the logic of word level spell checker and sentence level spell checker
479        if (mIsSentenceSpellCheckSupported) {
480            final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
481            final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
482            if (tempSuggestionSpan != null) {
483                if (DBG) {
484                    Log.i(TAG, "Cached span on the same position is cleard. "
485                            + editable.subSequence(start, end));
486                }
487                editable.removeSpan(tempSuggestionSpan);
488            }
489            mSuggestionSpanCache.put(key, suggestionSpan);
490        }
491        editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
492
493        mTextView.invalidateRegion(start, end, false /* No cursor involved */);
494    }
495
496    private class SpellParser {
497        private Object mRange = new Object();
498
499        public void parse(int start, int end) {
500            final int max = mTextView.length();
501            final int parseEnd;
502            if (end > max) {
503                Log.w(TAG, "Parse invalid region, from " + start + " to " + end);
504                parseEnd = max;
505            } else {
506                parseEnd = end;
507            }
508            if (parseEnd > start) {
509                setRangeSpan((Editable) mTextView.getText(), start, parseEnd);
510                parse();
511            }
512        }
513
514        public boolean isFinished() {
515            return ((Editable) mTextView.getText()).getSpanStart(mRange) < 0;
516        }
517
518        public void stop() {
519            removeRangeSpan((Editable) mTextView.getText());
520        }
521
522        private void setRangeSpan(Editable editable, int start, int end) {
523            if (DBG) {
524                Log.d(TAG, "set next range span: " + start + ", " + end);
525            }
526            editable.setSpan(mRange, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
527        }
528
529        private void removeRangeSpan(Editable editable) {
530            if (DBG) {
531                Log.d(TAG, "Remove range span." + editable.getSpanStart(editable)
532                        + editable.getSpanEnd(editable));
533            }
534            editable.removeSpan(mRange);
535        }
536
537        public void parse() {
538            Editable editable = (Editable) mTextView.getText();
539            // Iterate over the newly added text and schedule new SpellCheckSpans
540            final int start;
541            if (mIsSentenceSpellCheckSupported) {
542                // TODO: Find the start position of the sentence.
543                // Set span with the context
544                start =  Math.max(
545                        0, editable.getSpanStart(mRange) - MIN_SENTENCE_LENGTH);
546            } else {
547                start = editable.getSpanStart(mRange);
548            }
549
550            final int end = editable.getSpanEnd(mRange);
551
552            int wordIteratorWindowEnd = Math.min(end, start + WORD_ITERATOR_INTERVAL);
553            mWordIterator.setCharSequence(editable, start, wordIteratorWindowEnd);
554
555            // Move back to the beginning of the current word, if any
556            int wordStart = mWordIterator.preceding(start);
557            int wordEnd;
558            if (wordStart == BreakIterator.DONE) {
559                wordEnd = mWordIterator.following(start);
560                if (wordEnd != BreakIterator.DONE) {
561                    wordStart = mWordIterator.getBeginning(wordEnd);
562                }
563            } else {
564                wordEnd = mWordIterator.getEnd(wordStart);
565            }
566            if (wordEnd == BreakIterator.DONE) {
567                if (DBG) {
568                    Log.i(TAG, "No more spell check.");
569                }
570                removeRangeSpan(editable);
571                return;
572            }
573
574            // We need to expand by one character because we want to include the spans that
575            // end/start at position start/end respectively.
576            SpellCheckSpan[] spellCheckSpans = editable.getSpans(start - 1, end + 1,
577                    SpellCheckSpan.class);
578            SuggestionSpan[] suggestionSpans = editable.getSpans(start - 1, end + 1,
579                    SuggestionSpan.class);
580
581            int wordCount = 0;
582            boolean scheduleOtherSpellCheck = false;
583
584            if (mIsSentenceSpellCheckSupported) {
585                if (wordIteratorWindowEnd < end) {
586                    if (DBG) {
587                        Log.i(TAG, "schedule other spell check.");
588                    }
589                    // Several batches needed on that region. Cut after last previous word
590                    scheduleOtherSpellCheck = true;
591                }
592                int spellCheckEnd = mWordIterator.preceding(wordIteratorWindowEnd);
593                boolean correct = spellCheckEnd != BreakIterator.DONE;
594                if (correct) {
595                    spellCheckEnd = mWordIterator.getEnd(spellCheckEnd);
596                    correct = spellCheckEnd != BreakIterator.DONE;
597                }
598                if (!correct) {
599                    if (DBG) {
600                        Log.i(TAG, "Incorrect range span.");
601                    }
602                    removeRangeSpan(editable);
603                    return;
604                }
605                do {
606                    // TODO: Find the start position of the sentence.
607                    int spellCheckStart = wordStart;
608                    boolean createSpellCheckSpan = true;
609                    // Cancel or merge overlapped spell check spans
610                    for (int i = 0; i < mLength; ++i) {
611                        final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[i];
612                        if (mIds[i] < 0 || spellCheckSpan.isSpellCheckInProgress()) {
613                            continue;
614                        }
615                        final int spanStart = editable.getSpanStart(spellCheckSpan);
616                        final int spanEnd = editable.getSpanEnd(spellCheckSpan);
617                        if (spanEnd < spellCheckStart || spellCheckEnd < spanStart) {
618                            // No need to merge
619                            continue;
620                        }
621                        if (spanStart <= spellCheckStart && spellCheckEnd <= spanEnd) {
622                            // There is a completely overlapped spell check span
623                            // skip this span
624                            createSpellCheckSpan = false;
625                            if (DBG) {
626                                Log.i(TAG, "The range is overrapped. Skip spell check.");
627                            }
628                            break;
629                        }
630                        // This spellCheckSpan is replaced by the one we are creating
631                        editable.removeSpan(spellCheckSpan);
632                        spellCheckStart = Math.min(spanStart, spellCheckStart);
633                        spellCheckEnd = Math.max(spanEnd, spellCheckEnd);
634                    }
635
636                    if (DBG) {
637                        Log.d(TAG, "addSpellCheckSpan: "
638                                + ", End = " + spellCheckEnd + ", Start = " + spellCheckStart
639                                + ", next = " + scheduleOtherSpellCheck + "\n"
640                                + editable.subSequence(spellCheckStart, spellCheckEnd));
641                    }
642
643                    // Stop spell checking when there are no characters in the range.
644                    if (spellCheckEnd < start) {
645                        break;
646                    }
647                    if (spellCheckEnd <= spellCheckStart) {
648                        Log.w(TAG, "Trying to spellcheck invalid region, from "
649                                + start + " to " + end);
650                        break;
651                    }
652                    if (createSpellCheckSpan) {
653                        addSpellCheckSpan(editable, spellCheckStart, spellCheckEnd);
654                    }
655                } while (false);
656                wordStart = spellCheckEnd;
657            } else {
658                while (wordStart <= end) {
659                    if (wordEnd >= start && wordEnd > wordStart) {
660                        if (wordCount >= MAX_NUMBER_OF_WORDS) {
661                            scheduleOtherSpellCheck = true;
662                            break;
663                        }
664                        // A new word has been created across the interval boundaries with this
665                        // edit. The previous spans (that ended on start / started on end) are
666                        // not valid anymore and must be removed.
667                        if (wordStart < start && wordEnd > start) {
668                            removeSpansAt(editable, start, spellCheckSpans);
669                            removeSpansAt(editable, start, suggestionSpans);
670                        }
671
672                        if (wordStart < end && wordEnd > end) {
673                            removeSpansAt(editable, end, spellCheckSpans);
674                            removeSpansAt(editable, end, suggestionSpans);
675                        }
676
677                        // Do not create new boundary spans if they already exist
678                        boolean createSpellCheckSpan = true;
679                        if (wordEnd == start) {
680                            for (int i = 0; i < spellCheckSpans.length; i++) {
681                                final int spanEnd = editable.getSpanEnd(spellCheckSpans[i]);
682                                if (spanEnd == start) {
683                                    createSpellCheckSpan = false;
684                                    break;
685                                }
686                            }
687                        }
688
689                        if (wordStart == end) {
690                            for (int i = 0; i < spellCheckSpans.length; i++) {
691                                final int spanStart = editable.getSpanStart(spellCheckSpans[i]);
692                                if (spanStart == end) {
693                                    createSpellCheckSpan = false;
694                                    break;
695                                }
696                            }
697                        }
698
699                        if (createSpellCheckSpan) {
700                            addSpellCheckSpan(editable, wordStart, wordEnd);
701                        }
702                        wordCount++;
703                    }
704
705                    // iterate word by word
706                    int originalWordEnd = wordEnd;
707                    wordEnd = mWordIterator.following(wordEnd);
708                    if ((wordIteratorWindowEnd < end) &&
709                            (wordEnd == BreakIterator.DONE || wordEnd >= wordIteratorWindowEnd)) {
710                        wordIteratorWindowEnd =
711                                Math.min(end, originalWordEnd + WORD_ITERATOR_INTERVAL);
712                        mWordIterator.setCharSequence(
713                                editable, originalWordEnd, wordIteratorWindowEnd);
714                        wordEnd = mWordIterator.following(originalWordEnd);
715                    }
716                    if (wordEnd == BreakIterator.DONE) break;
717                    wordStart = mWordIterator.getBeginning(wordEnd);
718                    if (wordStart == BreakIterator.DONE) {
719                        break;
720                    }
721                }
722            }
723
724            if (scheduleOtherSpellCheck && wordStart <= end) {
725                // Update range span: start new spell check from last wordStart
726                setRangeSpan(editable, wordStart, end);
727            } else {
728                if (DBG && scheduleOtherSpellCheck) {
729                    Log.w(TAG, "Trying to schedule spellcheck for invalid region, from "
730                            + wordStart + " to " + end);
731                }
732                removeRangeSpan(editable);
733            }
734
735            spellCheck();
736        }
737
738        private <T> void removeSpansAt(Editable editable, int offset, T[] spans) {
739            final int length = spans.length;
740            for (int i = 0; i < length; i++) {
741                final T span = spans[i];
742                final int start = editable.getSpanStart(span);
743                if (start > offset) continue;
744                final int end = editable.getSpanEnd(span);
745                if (end < offset) continue;
746                editable.removeSpan(span);
747            }
748        }
749    }
750
751    public static boolean haveWordBoundariesChanged(final Editable editable, final int start,
752            final int end, final int spanStart, final int spanEnd) {
753        final boolean haveWordBoundariesChanged;
754        if (spanEnd != start && spanStart != end) {
755            haveWordBoundariesChanged = true;
756            if (DBG) {
757                Log.d(TAG, "(1) Text inside the span has been modified. Remove.");
758            }
759        } else if (spanEnd == start && start < editable.length()) {
760            final int codePoint = Character.codePointAt(editable, start);
761            haveWordBoundariesChanged = Character.isLetterOrDigit(codePoint);
762            if (DBG) {
763                Log.d(TAG, "(2) Characters have been appended to the spanned text. "
764                        + (haveWordBoundariesChanged ? "Remove.<" : "Keep. <") + (char)(codePoint)
765                        + ">, " + editable + ", " + editable.subSequence(spanStart, spanEnd) + ", "
766                        + start);
767            }
768        } else if (spanStart == end && end > 0) {
769            final int codePoint = Character.codePointBefore(editable, end);
770            haveWordBoundariesChanged = Character.isLetterOrDigit(codePoint);
771            if (DBG) {
772                Log.d(TAG, "(3) Characters have been prepended to the spanned text. "
773                        + (haveWordBoundariesChanged ? "Remove.<" : "Keep.<") + (char)(codePoint)
774                        + ">, " + editable + ", " + editable.subSequence(spanStart, spanEnd) + ", "
775                        + end);
776            }
777        } else {
778            if (DBG) {
779                Log.d(TAG, "(4) Characters adjacent to the spanned text were deleted. Keep.");
780            }
781            haveWordBoundariesChanged = false;
782        }
783        return haveWordBoundariesChanged;
784    }
785}
786