SpellChecker.java revision 24d146b966c87fd9c3b48027cbfb4238cb892ca5
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.SpannableStringBuilder;
23import android.text.Spanned;
24import android.text.method.WordIterator;
25import android.text.style.SpellCheckSpan;
26import android.text.style.SuggestionSpan;
27import android.util.Log;
28import android.view.textservice.SentenceSuggestionsInfo;
29import android.view.textservice.SpellCheckerSession;
30import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
31import android.view.textservice.SuggestionsInfo;
32import android.view.textservice.TextInfo;
33import android.view.textservice.TextServicesManager;
34
35import com.android.internal.util.ArrayUtils;
36
37import java.text.BreakIterator;
38import java.util.Locale;
39
40
41/**
42 * Helper class for TextView. Bridge between the TextView and the Dictionnary service.
43 *
44 * @hide
45 */
46public class SpellChecker implements SpellCheckerSessionListener {
47    private static final String TAG = SpellChecker.class.getSimpleName();
48    private static final boolean DBG = false;
49
50    // No more than this number of words will be parsed on each iteration to ensure a minimum
51    // lock of the UI thread
52    public static final int MAX_NUMBER_OF_WORDS = 50;
53
54    // Rough estimate, such that the word iterator interval usually does not need to be shifted
55    public static final int AVERAGE_WORD_LENGTH = 7;
56
57    // When parsing, use a character window of that size. Will be shifted if needed
58    public static final int WORD_ITERATOR_INTERVAL = AVERAGE_WORD_LENGTH * MAX_NUMBER_OF_WORDS;
59
60    // Pause between each spell check to keep the UI smooth
61    private final static int SPELL_PAUSE_DURATION = 400; // milliseconds
62
63    private static final int MIN_SENTENCE_LENGTH = 50;
64
65    private static final int USE_SPAN_RANGE = -1;
66
67    private final TextView mTextView;
68
69    SpellCheckerSession mSpellCheckerSession;
70    // We assume that the sentence level spell check will always provide better results than words.
71    // Although word SC has a sequential option.
72    private boolean mIsSentenceSpellCheckSupported;
73    final int mCookie;
74
75    // Paired arrays for the (id, spellCheckSpan) pair. A negative id means the associated
76    // SpellCheckSpan has been recycled and can be-reused.
77    // Contains null SpellCheckSpans after index mLength.
78    private int[] mIds;
79    private SpellCheckSpan[] mSpellCheckSpans;
80    // The mLength first elements of the above arrays have been initialized
81    private int mLength;
82
83    // Parsers on chunck of text, cutting text into words that will be checked
84    private SpellParser[] mSpellParsers = new SpellParser[0];
85
86    private int mSpanSequenceCounter = 0;
87
88    private Locale mCurrentLocale;
89
90    // Shared by all SpellParsers. Cannot be shared with TextView since it may be used
91    // concurrently due to the asynchronous nature of onGetSuggestions.
92    private WordIterator mWordIterator;
93
94    private TextServicesManager mTextServicesManager;
95
96    private Runnable mSpellRunnable;
97
98    public SpellChecker(TextView textView) {
99        mTextView = textView;
100
101        // Arbitrary: these arrays will automatically double their sizes on demand
102        final int size = ArrayUtils.idealObjectArraySize(1);
103        mIds = new int[size];
104        mSpellCheckSpans = new SpellCheckSpan[size];
105
106        setLocale(mTextView.getTextServicesLocale());
107
108        mCookie = hashCode();
109    }
110
111    private void resetSession() {
112        closeSession();
113
114        mTextServicesManager = (TextServicesManager) mTextView.getContext().
115                getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
116        if (!mTextServicesManager.isSpellCheckerEnabled()
117                ||  mTextServicesManager.getCurrentSpellCheckerSubtype(true) == null) {
118            mSpellCheckerSession = null;
119        } else {
120            mSpellCheckerSession = mTextServicesManager.newSpellCheckerSession(
121                    null /* Bundle not currently used by the textServicesManager */,
122                    mCurrentLocale, this,
123                    false /* means any available languages from current spell checker */);
124            mIsSentenceSpellCheckSupported = true;
125        }
126
127        // Restore SpellCheckSpans in pool
128        for (int i = 0; i < mLength; i++) {
129            mSpellCheckSpans[i].setSpellCheckInProgress(false);
130            mIds[i] = -1;
131        }
132        mLength = 0;
133
134        // Remove existing misspelled SuggestionSpans
135        mTextView.removeMisspelledSpans((Editable) mTextView.getText());
136    }
137
138    private void setLocale(Locale locale) {
139        mCurrentLocale = locale;
140
141        resetSession();
142
143        // Change SpellParsers' wordIterator locale
144        mWordIterator = new WordIterator(locale);
145
146        // This class is the listener for locale change: warn other locale-aware objects
147        mTextView.onLocaleChanged();
148    }
149
150    /**
151     * @return true if a spell checker session has successfully been created. Returns false if not,
152     * for instance when spell checking has been disabled in settings.
153     */
154    private boolean isSessionActive() {
155        return mSpellCheckerSession != null;
156    }
157
158    public void closeSession() {
159        if (mSpellCheckerSession != null) {
160            mSpellCheckerSession.close();
161        }
162
163        final int length = mSpellParsers.length;
164        for (int i = 0; i < length; i++) {
165            mSpellParsers[i].stop();
166        }
167
168        if (mSpellRunnable != null) {
169            mTextView.removeCallbacks(mSpellRunnable);
170        }
171    }
172
173    private int nextSpellCheckSpanIndex() {
174        for (int i = 0; i < mLength; i++) {
175            if (mIds[i] < 0) return i;
176        }
177
178        if (mLength == mSpellCheckSpans.length) {
179            final int newSize = mLength * 2;
180            int[] newIds = new int[newSize];
181            SpellCheckSpan[] newSpellCheckSpans = new SpellCheckSpan[newSize];
182            System.arraycopy(mIds, 0, newIds, 0, mLength);
183            System.arraycopy(mSpellCheckSpans, 0, newSpellCheckSpans, 0, mLength);
184            mIds = newIds;
185            mSpellCheckSpans = newSpellCheckSpans;
186        }
187
188        mSpellCheckSpans[mLength] = new SpellCheckSpan();
189        mLength++;
190        return mLength - 1;
191    }
192
193    private void addSpellCheckSpan(Editable editable, int start, int end) {
194        final int index = nextSpellCheckSpanIndex();
195        editable.setSpan(mSpellCheckSpans[index], start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
196        mIds[index] = mSpanSequenceCounter++;
197    }
198
199    public void removeSpellCheckSpan(SpellCheckSpan spellCheckSpan) {
200        for (int i = 0; i < mLength; i++) {
201            if (mSpellCheckSpans[i] == spellCheckSpan) {
202                mSpellCheckSpans[i].setSpellCheckInProgress(false);
203                mIds[i] = -1;
204                return;
205            }
206        }
207    }
208
209    public void onSelectionChanged() {
210        spellCheck();
211    }
212
213    public void spellCheck(int start, int end) {
214        final Locale locale = mTextView.getTextServicesLocale();
215        final boolean isSessionActive = isSessionActive();
216        if (mCurrentLocale == null || (!(mCurrentLocale.equals(locale)))) {
217            setLocale(locale);
218            // Re-check the entire text
219            start = 0;
220            end = mTextView.getText().length();
221        } else {
222            final boolean spellCheckerActivated = mTextServicesManager.isSpellCheckerEnabled();
223            if (isSessionActive != spellCheckerActivated) {
224                // Spell checker has been turned of or off since last spellCheck
225                resetSession();
226            }
227        }
228
229        if (!isSessionActive) return;
230
231        // Find first available SpellParser from pool
232        final int length = mSpellParsers.length;
233        for (int i = 0; i < length; i++) {
234            final SpellParser spellParser = mSpellParsers[i];
235            if (spellParser.isFinished()) {
236                spellParser.parse(start, end);
237                return;
238            }
239        }
240
241        // No available parser found in pool, create a new one
242        SpellParser[] newSpellParsers = new SpellParser[length + 1];
243        System.arraycopy(mSpellParsers, 0, newSpellParsers, 0, length);
244        mSpellParsers = newSpellParsers;
245
246        SpellParser spellParser = new SpellParser();
247        mSpellParsers[length] = spellParser;
248        spellParser.parse(start, end);
249    }
250
251    private void spellCheck() {
252        if (mSpellCheckerSession == null) return;
253
254        Editable editable = (Editable) mTextView.getText();
255        final int selectionStart = Selection.getSelectionStart(editable);
256        final int selectionEnd = Selection.getSelectionEnd(editable);
257
258        TextInfo[] textInfos = new TextInfo[mLength];
259        int textInfosCount = 0;
260
261        for (int i = 0; i < mLength; i++) {
262            final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[i];
263            if (spellCheckSpan.isSpellCheckInProgress()) continue;
264
265            final int start = editable.getSpanStart(spellCheckSpan);
266            final int end = editable.getSpanEnd(spellCheckSpan);
267
268            // Do not check this word if the user is currently editing it
269            if (start >= 0 && end > start && (selectionEnd < start || selectionStart > end)) {
270                final String word = (editable instanceof SpannableStringBuilder) ?
271                        ((SpannableStringBuilder) editable).substring(start, end) :
272                        editable.subSequence(start, end).toString();
273                spellCheckSpan.setSpellCheckInProgress(true);
274                textInfos[textInfosCount++] = new TextInfo(word, mCookie, mIds[i]);
275                if (DBG) {
276                    Log.d(TAG, "create TextInfo: (" + i + "/" + mLength + ")" + word
277                            + ", cookie = " + mCookie + ", seq = "
278                            + mIds[i] + ", sel start = " + selectionStart + ", sel end = "
279                            + selectionEnd + ", start = " + start + ", end = " + end);
280                }
281            }
282        }
283
284        if (textInfosCount > 0) {
285            if (textInfosCount < textInfos.length) {
286                TextInfo[] textInfosCopy = new TextInfo[textInfosCount];
287                System.arraycopy(textInfos, 0, textInfosCopy, 0, textInfosCount);
288                textInfos = textInfosCopy;
289            }
290
291            if (mIsSentenceSpellCheckSupported) {
292                mSpellCheckerSession.getSentenceSuggestions(
293                        textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE);
294            } else {
295                mSpellCheckerSession.getSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE,
296                        false /* TODO Set sequentialWords to true for initial spell check */);
297            }
298        }
299    }
300
301    private SpellCheckSpan onGetSuggestionsInternal(
302            SuggestionsInfo suggestionsInfo, int offset, int length) {
303        if (suggestionsInfo == null || suggestionsInfo.getCookie() != mCookie) {
304            return null;
305        }
306        final Editable editable = (Editable) mTextView.getText();
307        final int sequenceNumber = suggestionsInfo.getSequence();
308        for (int k = 0; k < mLength; ++k) {
309            if (sequenceNumber == mIds[k]) {
310                final int attributes = suggestionsInfo.getSuggestionsAttributes();
311                final boolean isInDictionary =
312                        ((attributes & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) > 0);
313                final boolean looksLikeTypo =
314                        ((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0);
315
316                final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[k];
317                //TODO: we need to change that rule for results from a sentence-level spell
318                // checker that will probably be in dictionary.
319                if (!isInDictionary && looksLikeTypo) {
320                    createMisspelledSuggestionSpan(
321                            editable, suggestionsInfo, spellCheckSpan, offset, length);
322                }
323                return spellCheckSpan;
324            }
325        }
326        return null;
327    }
328
329    @Override
330    public void onGetSuggestions(SuggestionsInfo[] results) {
331        final Editable editable = (Editable) mTextView.getText();
332        for (int i = 0; i < results.length; ++i) {
333            final SpellCheckSpan spellCheckSpan =
334                    onGetSuggestionsInternal(results[i], USE_SPAN_RANGE, USE_SPAN_RANGE);
335            if (spellCheckSpan != null) {
336                editable.removeSpan(spellCheckSpan);
337            }
338        }
339        scheduleNewSpellCheck();
340    }
341
342    @Override
343    public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
344        final Editable editable = (Editable) mTextView.getText();
345
346        for (int i = 0; i < results.length; ++i) {
347            final SentenceSuggestionsInfo ssi = results[i];
348            if (ssi == null) {
349                continue;
350            }
351            SpellCheckSpan spellCheckSpan = null;
352            for (int j = 0; j < ssi.getSuggestionsCount(); ++j) {
353                final SuggestionsInfo suggestionsInfo = ssi.getSuggestionsInfoAt(j);
354                if (suggestionsInfo == null) {
355                    continue;
356                }
357                final int offset = ssi.getOffsetAt(j);
358                final int length = ssi.getLengthAt(j);
359                final SpellCheckSpan scs = onGetSuggestionsInternal(
360                        suggestionsInfo, offset, length);
361                if (spellCheckSpan == null && scs != null) {
362                    // the spellCheckSpan is shared by all the "SuggestionsInfo"s in the same
363                    // SentenceSuggestionsInfo
364                    spellCheckSpan = scs;
365                }
366            }
367            if (spellCheckSpan != null) {
368                editable.removeSpan(spellCheckSpan);
369            }
370        }
371        scheduleNewSpellCheck();
372    }
373
374    private void scheduleNewSpellCheck() {
375        if (mSpellRunnable == null) {
376            mSpellRunnable = new Runnable() {
377                @Override
378                public void run() {
379                    final int length = mSpellParsers.length;
380                    for (int i = 0; i < length; i++) {
381                        final SpellParser spellParser = mSpellParsers[i];
382                        if (!spellParser.isFinished()) {
383                            spellParser.parse();
384                            break; // run one spell parser at a time to bound running time
385                        }
386                    }
387                }
388            };
389        } else {
390            mTextView.removeCallbacks(mSpellRunnable);
391        }
392
393        mTextView.postDelayed(mSpellRunnable, SPELL_PAUSE_DURATION);
394    }
395
396    private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo,
397            SpellCheckSpan spellCheckSpan, int offset, int length) {
398        final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
399        final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
400        if (spellCheckSpanStart < 0 || spellCheckSpanEnd <= spellCheckSpanStart)
401            return; // span was removed in the meantime
402
403        final int suggestionsCount = suggestionsInfo.getSuggestionsCount();
404        if (suggestionsCount <= 0) {
405            // A negative suggestion count is possible
406            return;
407        }
408
409        final int start;
410        final int end;
411        if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
412            start = spellCheckSpanStart + offset;
413            end = start + length;
414        } else {
415            start = spellCheckSpanStart;
416            end = spellCheckSpanEnd;
417        }
418
419        String[] suggestions = new String[suggestionsCount];
420        for (int i = 0; i < suggestionsCount; i++) {
421            suggestions[i] = suggestionsInfo.getSuggestionAt(i);
422        }
423
424        SuggestionSpan suggestionSpan = new SuggestionSpan(mTextView.getContext(), suggestions,
425                SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
426        editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
427
428        mTextView.invalidateRegion(start, end, false /* No cursor involved */);
429    }
430
431    private class SpellParser {
432        private Object mRange = new Object();
433
434        public void parse(int start, int end) {
435            if (end > start) {
436                setRangeSpan((Editable) mTextView.getText(), start, end);
437                parse();
438            }
439        }
440
441        public boolean isFinished() {
442            return ((Editable) mTextView.getText()).getSpanStart(mRange) < 0;
443        }
444
445        public void stop() {
446            removeRangeSpan((Editable) mTextView.getText());
447        }
448
449        private void setRangeSpan(Editable editable, int start, int end) {
450            editable.setSpan(mRange, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
451        }
452
453        private void removeRangeSpan(Editable editable) {
454            editable.removeSpan(mRange);
455        }
456
457        public void parse() {
458            Editable editable = (Editable) mTextView.getText();
459            // Iterate over the newly added text and schedule new SpellCheckSpans
460            final int start;
461            if (mIsSentenceSpellCheckSupported) {
462                // TODO: Find the start position of the sentence.
463                // Set span with the context
464                start =  Math.max(
465                        0, editable.getSpanStart(mRange) - MIN_SENTENCE_LENGTH);
466            } else {
467                start = editable.getSpanStart(mRange);
468            }
469
470            final int end = editable.getSpanEnd(mRange);
471
472            int wordIteratorWindowEnd = Math.min(end, start + WORD_ITERATOR_INTERVAL);
473            mWordIterator.setCharSequence(editable, start, wordIteratorWindowEnd);
474
475            // Move back to the beginning of the current word, if any
476            int wordStart = mWordIterator.preceding(start);
477            int wordEnd;
478            if (wordStart == BreakIterator.DONE) {
479                wordEnd = mWordIterator.following(start);
480                if (wordEnd != BreakIterator.DONE) {
481                    wordStart = mWordIterator.getBeginning(wordEnd);
482                }
483            } else {
484                wordEnd = mWordIterator.getEnd(wordStart);
485            }
486            if (wordEnd == BreakIterator.DONE) {
487                removeRangeSpan(editable);
488                return;
489            }
490
491            // We need to expand by one character because we want to include the spans that
492            // end/start at position start/end respectively.
493            SpellCheckSpan[] spellCheckSpans = editable.getSpans(start - 1, end + 1,
494                    SpellCheckSpan.class);
495            SuggestionSpan[] suggestionSpans = editable.getSpans(start - 1, end + 1,
496                    SuggestionSpan.class);
497
498            int wordCount = 0;
499            boolean scheduleOtherSpellCheck = false;
500
501            if (mIsSentenceSpellCheckSupported) {
502                int regionEnd;
503                if (wordIteratorWindowEnd < end) {
504                    // Several batches needed on that region. Cut after last previous word
505                    regionEnd = mWordIterator.preceding(wordIteratorWindowEnd);
506                    scheduleOtherSpellCheck = true;
507                } else {
508                    regionEnd = mWordIterator.preceding(end);
509                }
510                boolean correct = regionEnd != BreakIterator.DONE;
511                if (correct) {
512                    regionEnd = mWordIterator.getEnd(regionEnd);
513                    correct = regionEnd != BreakIterator.DONE;
514                }
515                if (!correct) {
516                    editable.removeSpan(mRange);
517                    return;
518                }
519                // Stop spell checking when there are no characters in the range.
520                if (wordEnd < start) {
521                    return;
522                }
523                // TODO: Find the start position of the sentence.
524                final int spellCheckStart =  wordStart;
525                if (regionEnd <= spellCheckStart) {
526                    return;
527                }
528                final int selectionStart = Selection.getSelectionStart(editable);
529                final int selectionEnd = Selection.getSelectionEnd(editable);
530                if (DBG) {
531                    Log.d(TAG, "addSpellCheckSpan: "
532                            + editable.subSequence(spellCheckStart, regionEnd)
533                            + ", regionEnd = " + regionEnd + ", spellCheckStart = "
534                            + spellCheckStart + ", sel start = " + selectionStart + ", sel end ="
535                            + selectionEnd);
536                }
537                // Do not check this word if the user is currently editing it
538                if (spellCheckStart >= 0 && regionEnd > spellCheckStart
539                        && (selectionEnd < spellCheckStart || selectionStart > regionEnd)) {
540                    addSpellCheckSpan(editable, spellCheckStart, regionEnd);
541                }
542                wordStart = regionEnd;
543            } else {
544                while (wordStart <= end) {
545                    if (wordEnd >= start && wordEnd > wordStart) {
546                        if (wordCount >= MAX_NUMBER_OF_WORDS) {
547                            scheduleOtherSpellCheck = true;
548                            break;
549                        }
550                        // A new word has been created across the interval boundaries with this
551                        // edit. The previous spans (that ended on start / started on end) are
552                        // not valid anymore and must be removed.
553                        if (wordStart < start && wordEnd > start) {
554                            removeSpansAt(editable, start, spellCheckSpans);
555                            removeSpansAt(editable, start, suggestionSpans);
556                        }
557
558                        if (wordStart < end && wordEnd > end) {
559                            removeSpansAt(editable, end, spellCheckSpans);
560                            removeSpansAt(editable, end, suggestionSpans);
561                        }
562
563                        // Do not create new boundary spans if they already exist
564                        boolean createSpellCheckSpan = true;
565                        if (wordEnd == start) {
566                            for (int i = 0; i < spellCheckSpans.length; i++) {
567                                final int spanEnd = editable.getSpanEnd(spellCheckSpans[i]);
568                                if (spanEnd == start) {
569                                    createSpellCheckSpan = false;
570                                    break;
571                                }
572                            }
573                        }
574
575                        if (wordStart == end) {
576                            for (int i = 0; i < spellCheckSpans.length; i++) {
577                                final int spanStart = editable.getSpanStart(spellCheckSpans[i]);
578                                if (spanStart == end) {
579                                    createSpellCheckSpan = false;
580                                    break;
581                                }
582                            }
583                        }
584
585                        if (createSpellCheckSpan) {
586                            addSpellCheckSpan(editable, wordStart, wordEnd);
587                        }
588                        wordCount++;
589                    }
590
591                    // iterate word by word
592                    int originalWordEnd = wordEnd;
593                    wordEnd = mWordIterator.following(wordEnd);
594                    if ((wordIteratorWindowEnd < end) &&
595                            (wordEnd == BreakIterator.DONE || wordEnd >= wordIteratorWindowEnd)) {
596                        wordIteratorWindowEnd =
597                                Math.min(end, originalWordEnd + WORD_ITERATOR_INTERVAL);
598                        mWordIterator.setCharSequence(
599                                editable, originalWordEnd, wordIteratorWindowEnd);
600                        wordEnd = mWordIterator.following(originalWordEnd);
601                    }
602                    if (wordEnd == BreakIterator.DONE) break;
603                    wordStart = mWordIterator.getBeginning(wordEnd);
604                    if (wordStart == BreakIterator.DONE) {
605                        break;
606                    }
607                }
608            }
609
610            if (scheduleOtherSpellCheck) {
611                // Update range span: start new spell check from last wordStart
612                setRangeSpan(editable, wordStart, end);
613            } else {
614                removeRangeSpan(editable);
615            }
616
617            spellCheck();
618        }
619
620        private <T> void removeSpansAt(Editable editable, int offset, T[] spans) {
621            final int length = spans.length;
622            for (int i = 0; i < length; i++) {
623                final T span = spans[i];
624                final int start = editable.getSpanStart(span);
625                if (start > offset) continue;
626                final int end = editable.getSpanEnd(span);
627                if (end < offset) continue;
628                editable.removeSpan(span);
629            }
630        }
631    }
632}
633