SpellChecker.java revision f43305fb057e0818db456065fba9698e2163a762
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 resetSession() { 101 closeSession(); 102 103 mTextServicesManager = (TextServicesManager) mTextView.getContext(). 104 getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); 105 if (!mTextServicesManager.isSpellCheckerEnabled() 106 || mTextServicesManager.getCurrentSpellCheckerSubtype(true) == null) { 107 mSpellCheckerSession = null; 108 } else { 109 mSpellCheckerSession = mTextServicesManager.newSpellCheckerSession( 110 null /* Bundle not currently used by the textServicesManager */, 111 mCurrentLocale, this, 112 false /* means any available languages from current spell checker */); 113 } 114 115 // Restore SpellCheckSpans in pool 116 for (int i = 0; i < mLength; i++) { 117 mSpellCheckSpans[i].setSpellCheckInProgress(false); 118 mIds[i] = -1; 119 } 120 mLength = 0; 121 122 // Remove existing misspelled SuggestionSpans 123 mTextView.removeMisspelledSpans((Editable) mTextView.getText()); 124 } 125 126 private void setLocale(Locale locale) { 127 mCurrentLocale = locale; 128 129 resetSession(); 130 131 // Change SpellParsers' wordIterator locale 132 mWordIterator = new WordIterator(locale); 133 134 // This class is the listener for locale change: warn other locale-aware objects 135 mTextView.onLocaleChanged(); 136 } 137 138 /** 139 * @return true if a spell checker session has successfully been created. Returns false if not, 140 * for instance when spell checking has been disabled in settings. 141 */ 142 private boolean isSessionActive() { 143 return mSpellCheckerSession != null; 144 } 145 146 public void closeSession() { 147 if (mSpellCheckerSession != null) { 148 mSpellCheckerSession.close(); 149 } 150 151 final int length = mSpellParsers.length; 152 for (int i = 0; i < length; i++) { 153 mSpellParsers[i].stop(); 154 } 155 156 if (mSpellRunnable != null) { 157 mTextView.removeCallbacks(mSpellRunnable); 158 } 159 } 160 161 private int nextSpellCheckSpanIndex() { 162 for (int i = 0; i < mLength; i++) { 163 if (mIds[i] < 0) return i; 164 } 165 166 if (mLength == mSpellCheckSpans.length) { 167 final int newSize = mLength * 2; 168 int[] newIds = new int[newSize]; 169 SpellCheckSpan[] newSpellCheckSpans = new SpellCheckSpan[newSize]; 170 System.arraycopy(mIds, 0, newIds, 0, mLength); 171 System.arraycopy(mSpellCheckSpans, 0, newSpellCheckSpans, 0, mLength); 172 mIds = newIds; 173 mSpellCheckSpans = newSpellCheckSpans; 174 } 175 176 mSpellCheckSpans[mLength] = new SpellCheckSpan(); 177 mLength++; 178 return mLength - 1; 179 } 180 181 private void addSpellCheckSpan(Editable editable, int start, int end) { 182 final int index = nextSpellCheckSpanIndex(); 183 editable.setSpan(mSpellCheckSpans[index], start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 184 mIds[index] = mSpanSequenceCounter++; 185 } 186 187 public void removeSpellCheckSpan(SpellCheckSpan spellCheckSpan) { 188 for (int i = 0; i < mLength; i++) { 189 if (mSpellCheckSpans[i] == spellCheckSpan) { 190 mSpellCheckSpans[i].setSpellCheckInProgress(false); 191 mIds[i] = -1; 192 return; 193 } 194 } 195 } 196 197 public void onSelectionChanged() { 198 spellCheck(); 199 } 200 201 public void spellCheck(int start, int end) { 202 final Locale locale = mTextView.getTextServicesLocale(); 203 if (mCurrentLocale == null || (!(mCurrentLocale.equals(locale)))) { 204 setLocale(locale); 205 // Re-check the entire text 206 start = 0; 207 end = mTextView.getText().length(); 208 } else { 209 final boolean spellCheckerActivated = mTextServicesManager.isSpellCheckerEnabled(); 210 if (isSessionActive() != spellCheckerActivated) { 211 // Spell checker has been turned of or off since last spellCheck 212 resetSession(); 213 } 214 } 215 216 if (!isSessionActive()) return; 217 218 // Find first available SpellParser from pool 219 final int length = mSpellParsers.length; 220 for (int i = 0; i < length; i++) { 221 final SpellParser spellParser = mSpellParsers[i]; 222 if (spellParser.isFinished()) { 223 spellParser.init(start, end); 224 spellParser.parse(); 225 return; 226 } 227 } 228 229 // No available parser found in pool, create a new one 230 SpellParser[] newSpellParsers = new SpellParser[length + 1]; 231 System.arraycopy(mSpellParsers, 0, newSpellParsers, 0, length); 232 mSpellParsers = newSpellParsers; 233 234 SpellParser spellParser = new SpellParser(); 235 mSpellParsers[length] = spellParser; 236 spellParser.init(start, end); 237 spellParser.parse(); 238 } 239 240 private void spellCheck() { 241 if (mSpellCheckerSession == null) return; 242 243 Editable editable = (Editable) mTextView.getText(); 244 final int selectionStart = Selection.getSelectionStart(editable); 245 final int selectionEnd = Selection.getSelectionEnd(editable); 246 247 TextInfo[] textInfos = new TextInfo[mLength]; 248 int textInfosCount = 0; 249 250 for (int i = 0; i < mLength; i++) { 251 final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[i]; 252 if (spellCheckSpan.isSpellCheckInProgress()) continue; 253 254 final int start = editable.getSpanStart(spellCheckSpan); 255 final int end = editable.getSpanEnd(spellCheckSpan); 256 257 // Do not check this word if the user is currently editing it 258 if (start >= 0 && end > start && (selectionEnd < start || selectionStart > end)) { 259 final String word = (editable instanceof SpannableStringBuilder) ? 260 ((SpannableStringBuilder) editable).substring(start, end) : 261 editable.subSequence(start, end).toString(); 262 spellCheckSpan.setSpellCheckInProgress(true); 263 textInfos[textInfosCount++] = new TextInfo(word, mCookie, mIds[i]); 264 } 265 } 266 267 if (textInfosCount > 0) { 268 if (textInfosCount < textInfos.length) { 269 TextInfo[] textInfosCopy = new TextInfo[textInfosCount]; 270 System.arraycopy(textInfos, 0, textInfosCopy, 0, textInfosCount); 271 textInfos = textInfosCopy; 272 } 273 274 mSpellCheckerSession.getSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE, 275 false /* TODO Set sequentialWords to true for initial spell check */); 276 } 277 } 278 279 @Override 280 public void onGetSuggestionsForSentence(SuggestionsInfo[] results) { 281 // TODO: Handle the position and length for each suggestion 282 onGetSuggestions(results); 283 } 284 285 @Override 286 public void onGetSuggestions(SuggestionsInfo[] results) { 287 Editable editable = (Editable) mTextView.getText(); 288 289 for (int i = 0; i < results.length; i++) { 290 SuggestionsInfo suggestionsInfo = results[i]; 291 if (suggestionsInfo.getCookie() != mCookie) continue; 292 final int sequenceNumber = suggestionsInfo.getSequence(); 293 294 for (int j = 0; j < mLength; j++) { 295 if (sequenceNumber == mIds[j]) { 296 final int attributes = suggestionsInfo.getSuggestionsAttributes(); 297 boolean isInDictionary = 298 ((attributes & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) > 0); 299 boolean looksLikeTypo = 300 ((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0); 301 302 SpellCheckSpan spellCheckSpan = mSpellCheckSpans[j]; 303 304 if (!isInDictionary && looksLikeTypo) { 305 createMisspelledSuggestionSpan(editable, suggestionsInfo, spellCheckSpan); 306 } 307 308 editable.removeSpan(spellCheckSpan); 309 break; 310 } 311 } 312 } 313 314 scheduleNewSpellCheck(); 315 } 316 317 private void scheduleNewSpellCheck() { 318 if (mSpellRunnable == null) { 319 mSpellRunnable = new Runnable() { 320 @Override 321 public void run() { 322 final int length = mSpellParsers.length; 323 for (int i = 0; i < length; i++) { 324 final SpellParser spellParser = mSpellParsers[i]; 325 if (!spellParser.isFinished()) { 326 spellParser.parse(); 327 break; // run one spell parser at a time to bound running time 328 } 329 } 330 } 331 }; 332 } else { 333 mTextView.removeCallbacks(mSpellRunnable); 334 } 335 336 mTextView.postDelayed(mSpellRunnable, SPELL_PAUSE_DURATION); 337 } 338 339 private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo, 340 SpellCheckSpan spellCheckSpan) { 341 final int start = editable.getSpanStart(spellCheckSpan); 342 final int end = editable.getSpanEnd(spellCheckSpan); 343 if (start < 0 || end <= start) return; // span was removed in the meantime 344 345 // Other suggestion spans may exist on that region, with identical suggestions, filter 346 // them out to avoid duplicates. 347 SuggestionSpan[] suggestionSpans = editable.getSpans(start, end, SuggestionSpan.class); 348 final int length = suggestionSpans.length; 349 for (int i = 0; i < length; i++) { 350 final int spanStart = editable.getSpanStart(suggestionSpans[i]); 351 final int spanEnd = editable.getSpanEnd(suggestionSpans[i]); 352 if (spanStart != start || spanEnd != end) { 353 // Nulled (to avoid new array allocation) if not on that exact same region 354 suggestionSpans[i] = null; 355 } 356 } 357 358 final int suggestionsCount = suggestionsInfo.getSuggestionsCount(); 359 String[] suggestions; 360 if (suggestionsCount <= 0) { 361 // A negative suggestion count is possible 362 suggestions = ArrayUtils.emptyArray(String.class); 363 } else { 364 int numberOfSuggestions = 0; 365 suggestions = new String[suggestionsCount]; 366 367 for (int i = 0; i < suggestionsCount; i++) { 368 final String spellSuggestion = suggestionsInfo.getSuggestionAt(i); 369 if (spellSuggestion == null) break; 370 boolean suggestionFound = false; 371 372 for (int j = 0; j < length && !suggestionFound; j++) { 373 if (suggestionSpans[j] == null) break; 374 375 String[] suggests = suggestionSpans[j].getSuggestions(); 376 for (int k = 0; k < suggests.length; k++) { 377 if (spellSuggestion.equals(suggests[k])) { 378 // The suggestion is already provided by an other SuggestionSpan 379 suggestionFound = true; 380 break; 381 } 382 } 383 } 384 385 if (!suggestionFound) { 386 suggestions[numberOfSuggestions++] = spellSuggestion; 387 } 388 } 389 390 if (numberOfSuggestions != suggestionsCount) { 391 String[] newSuggestions = new String[numberOfSuggestions]; 392 System.arraycopy(suggestions, 0, newSuggestions, 0, numberOfSuggestions); 393 suggestions = newSuggestions; 394 } 395 } 396 397 SuggestionSpan suggestionSpan = new SuggestionSpan(mTextView.getContext(), suggestions, 398 SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED); 399 editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 400 401 mTextView.invalidateRegion(start, end, false /* No cursor involved */); 402 } 403 404 private class SpellParser { 405 private Object mRange = new Object(); 406 407 public void init(int start, int end) { 408 setRangeSpan((Editable) mTextView.getText(), start, end); 409 } 410 411 public boolean isFinished() { 412 return ((Editable) mTextView.getText()).getSpanStart(mRange) < 0; 413 } 414 415 public void stop() { 416 removeRangeSpan((Editable) mTextView.getText()); 417 } 418 419 private void setRangeSpan(Editable editable, int start, int end) { 420 editable.setSpan(mRange, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 421 } 422 423 private void removeRangeSpan(Editable editable) { 424 editable.removeSpan(mRange); 425 } 426 427 public void parse() { 428 Editable editable = (Editable) mTextView.getText(); 429 // Iterate over the newly added text and schedule new SpellCheckSpans 430 final int start = editable.getSpanStart(mRange); 431 final int end = editable.getSpanEnd(mRange); 432 433 int wordIteratorWindowEnd = Math.min(end, start + WORD_ITERATOR_INTERVAL); 434 mWordIterator.setCharSequence(editable, start, wordIteratorWindowEnd); 435 436 // Move back to the beginning of the current word, if any 437 int wordStart = mWordIterator.preceding(start); 438 int wordEnd; 439 if (wordStart == BreakIterator.DONE) { 440 wordEnd = mWordIterator.following(start); 441 if (wordEnd != BreakIterator.DONE) { 442 wordStart = mWordIterator.getBeginning(wordEnd); 443 } 444 } else { 445 wordEnd = mWordIterator.getEnd(wordStart); 446 } 447 if (wordEnd == BreakIterator.DONE) { 448 removeRangeSpan(editable); 449 return; 450 } 451 452 // We need to expand by one character because we want to include the spans that 453 // end/start at position start/end respectively. 454 SpellCheckSpan[] spellCheckSpans = editable.getSpans(start - 1, end + 1, 455 SpellCheckSpan.class); 456 SuggestionSpan[] suggestionSpans = editable.getSpans(start - 1, end + 1, 457 SuggestionSpan.class); 458 459 int wordCount = 0; 460 boolean scheduleOtherSpellCheck = false; 461 462 while (wordStart <= end) { 463 if (wordEnd >= start && wordEnd > wordStart) { 464 if (wordCount >= MAX_NUMBER_OF_WORDS) { 465 scheduleOtherSpellCheck = true; 466 break; 467 } 468 469 // A new word has been created across the interval boundaries with this edit. 470 // Previous spans (ended on start / started on end) removed, not valid anymore 471 if (wordStart < start && wordEnd > start) { 472 removeSpansAt(editable, start, spellCheckSpans); 473 removeSpansAt(editable, start, suggestionSpans); 474 } 475 476 if (wordStart < end && wordEnd > end) { 477 removeSpansAt(editable, end, spellCheckSpans); 478 removeSpansAt(editable, end, suggestionSpans); 479 } 480 481 // Do not create new boundary spans if they already exist 482 boolean createSpellCheckSpan = true; 483 if (wordEnd == start) { 484 for (int i = 0; i < spellCheckSpans.length; i++) { 485 final int spanEnd = editable.getSpanEnd(spellCheckSpans[i]); 486 if (spanEnd == start) { 487 createSpellCheckSpan = false; 488 break; 489 } 490 } 491 } 492 493 if (wordStart == end) { 494 for (int i = 0; i < spellCheckSpans.length; i++) { 495 final int spanStart = editable.getSpanStart(spellCheckSpans[i]); 496 if (spanStart == end) { 497 createSpellCheckSpan = false; 498 break; 499 } 500 } 501 } 502 503 if (createSpellCheckSpan) { 504 addSpellCheckSpan(editable, wordStart, wordEnd); 505 } 506 wordCount++; 507 } 508 509 // iterate word by word 510 int originalWordEnd = wordEnd; 511 wordEnd = mWordIterator.following(wordEnd); 512 if ((wordIteratorWindowEnd < end) && 513 (wordEnd == BreakIterator.DONE || wordEnd >= wordIteratorWindowEnd)) { 514 wordIteratorWindowEnd = Math.min(end, originalWordEnd + WORD_ITERATOR_INTERVAL); 515 mWordIterator.setCharSequence(editable, originalWordEnd, wordIteratorWindowEnd); 516 wordEnd = mWordIterator.following(originalWordEnd); 517 } 518 if (wordEnd == BreakIterator.DONE) break; 519 wordStart = mWordIterator.getBeginning(wordEnd); 520 if (wordStart == BreakIterator.DONE) { 521 break; 522 } 523 } 524 525 if (scheduleOtherSpellCheck) { 526 // Update range span: start new spell check from last wordStart 527 setRangeSpan(editable, wordStart, end); 528 } else { 529 removeRangeSpan(editable); 530 } 531 532 spellCheck(); 533 } 534 535 private <T> void removeSpansAt(Editable editable, int offset, T[] spans) { 536 final int length = spans.length; 537 for (int i = 0; i < length; i++) { 538 final T span = spans[i]; 539 final int start = editable.getSpanStart(span); 540 if (start > offset) continue; 541 final int end = editable.getSpanEnd(span); 542 if (end < offset) continue; 543 editable.removeSpan(span); 544 } 545 } 546 } 547} 548