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