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