| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.textservice; |
| 18 |
|
| 19 |
import android.app.Service; |
| 20 |
import android.content.Intent; |
| 21 |
import android.os.Bundle; |
| 22 |
import android.os.IBinder; |
| 23 |
import android.os.Process; |
| 24 |
import android.os.RemoteException; |
| 25 |
import android.text.TextUtils; |
| 26 |
import android.text.method.WordIterator; |
| 27 |
import android.util.Log; |
| 28 |
import android.view.textservice.SentenceSuggestionsInfo; |
| 29 |
import android.view.textservice.SuggestionsInfo; |
| 30 |
import android.view.textservice.TextInfo; |
| 31 |
|
| 32 |
import com.android.internal.textservice.ISpellCheckerService; |
| 33 |
import com.android.internal.textservice.ISpellCheckerServiceCallback; |
| 34 |
import com.android.internal.textservice.ISpellCheckerSession; |
| 35 |
import com.android.internal.textservice.ISpellCheckerSessionListener; |
| 36 |
|
| 37 |
import java.lang.ref.WeakReference; |
| 38 |
import java.text.BreakIterator; |
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.Locale; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public abstract class SpellCheckerService extends Service { |
| 70 |
private static final String TAG = SpellCheckerService.class.getSimpleName(); |
| 71 |
private static final boolean DBG = false; |
| 72 |
public static final String SERVICE_INTERFACE = |
| 73 |
"android.service.textservice.SpellCheckerService"; |
| 74 |
|
| 75 |
private final SpellCheckerServiceBinder mBinder = new SpellCheckerServiceBinder(this); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
@Override |
| 83 |
public final IBinder onBind(final Intent intent) { |
| 84 |
if (DBG) { |
| 85 |
Log.w(TAG, "onBind"); |
| 86 |
} |
| 87 |
return mBinder; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public abstract Session createSession(); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static abstract class Session { |
| 100 |
private InternalISpellCheckerSession mInternalSession; |
| 101 |
private volatile SentenceLevelAdapter mSentenceLevelAdapter; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public final void setInternalISpellCheckerSession(InternalISpellCheckerSession session) { |
| 107 |
mInternalSession = session; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public abstract void onCreate(); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public abstract SuggestionsInfo onGetSuggestions(TextInfo textInfo, int suggestionsLimit); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos, |
| 139 |
int suggestionsLimit, boolean sequentialWords) { |
| 140 |
final int length = textInfos.length; |
| 141 |
final SuggestionsInfo[] retval = new SuggestionsInfo[length]; |
| 142 |
for (int i = 0; i < length; ++i) { |
| 143 |
retval[i] = onGetSuggestions(textInfos[i], suggestionsLimit); |
| 144 |
retval[i].setCookieAndSequence( |
| 145 |
textInfos[i].getCookie(), textInfos[i].getSequence()); |
| 146 |
} |
| 147 |
return retval; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, |
| 165 |
int suggestionsLimit) { |
| 166 |
if (textInfos == null || textInfos.length == 0) { |
| 167 |
return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS; |
| 168 |
} |
| 169 |
if (DBG) { |
| 170 |
Log.d(TAG, "onGetSentenceSuggestionsMultiple: + " + textInfos.length + ", " |
| 171 |
+ suggestionsLimit); |
| 172 |
} |
| 173 |
if (mSentenceLevelAdapter == null) { |
| 174 |
synchronized(this) { |
| 175 |
if (mSentenceLevelAdapter == null) { |
| 176 |
final String localeStr = getLocale(); |
| 177 |
if (!TextUtils.isEmpty(localeStr)) { |
| 178 |
mSentenceLevelAdapter = new SentenceLevelAdapter(new Locale(localeStr)); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
if (mSentenceLevelAdapter == null) { |
| 184 |
return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS; |
| 185 |
} |
| 186 |
final int infosSize = textInfos.length; |
| 187 |
final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize]; |
| 188 |
for (int i = 0; i < infosSize; ++i) { |
| 189 |
final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams = |
| 190 |
mSentenceLevelAdapter.getSplitWords(textInfos[i]); |
| 191 |
final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems = |
| 192 |
textInfoParams.mItems; |
| 193 |
final int itemsSize = mItems.size(); |
| 194 |
final TextInfo[] splitTextInfos = new TextInfo[itemsSize]; |
| 195 |
for (int j = 0; j < itemsSize; ++j) { |
| 196 |
splitTextInfos[j] = mItems.get(j).mTextInfo; |
| 197 |
} |
| 198 |
retval[i] = SentenceLevelAdapter.reconstructSuggestions( |
| 199 |
textInfoParams, onGetSuggestionsMultiple( |
| 200 |
splitTextInfos, suggestionsLimit, true)); |
| 201 |
} |
| 202 |
return retval; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public void onCancel() {} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public void onClose() {} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public String getLocale() { |
| 225 |
return mInternalSession.getLocale(); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public Bundle getBundle() { |
| 232 |
return mInternalSession.getBundle(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public @SuggestionsInfo.ResultAttrs int getSupportedAttributes() { |
| 244 |
return mInternalSession.getSupportedAttributes(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
private static class InternalISpellCheckerSession extends ISpellCheckerSession.Stub { |
| 250 |
private ISpellCheckerSessionListener mListener; |
| 251 |
private final Session mSession; |
| 252 |
private final String mLocale; |
| 253 |
private final Bundle mBundle; |
| 254 |
private final @SuggestionsInfo.ResultAttrs int mSupportedAttributes; |
| 255 |
|
| 256 |
public InternalISpellCheckerSession(String locale, ISpellCheckerSessionListener listener, |
| 257 |
Bundle bundle, Session session, |
| 258 |
@SuggestionsInfo.ResultAttrs int supportedAttributes) { |
| 259 |
mListener = listener; |
| 260 |
mSession = session; |
| 261 |
mLocale = locale; |
| 262 |
mBundle = bundle; |
| 263 |
mSupportedAttributes = supportedAttributes; |
| 264 |
session.setInternalISpellCheckerSession(this); |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public void onGetSuggestionsMultiple( |
| 269 |
TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) { |
| 270 |
int pri = Process.getThreadPriority(Process.myTid()); |
| 271 |
try { |
| 272 |
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 273 |
mListener.onGetSuggestions( |
| 274 |
mSession.onGetSuggestionsMultiple( |
| 275 |
textInfos, suggestionsLimit, sequentialWords)); |
| 276 |
} catch (RemoteException e) { |
| 277 |
} finally { |
| 278 |
Process.setThreadPriority(pri); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public void onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) { |
| 284 |
try { |
| 285 |
mListener.onGetSentenceSuggestions( |
| 286 |
mSession.onGetSentenceSuggestionsMultiple(textInfos, suggestionsLimit)); |
| 287 |
} catch (RemoteException e) { |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public void onCancel() { |
| 293 |
int pri = Process.getThreadPriority(Process.myTid()); |
| 294 |
try { |
| 295 |
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 296 |
mSession.onCancel(); |
| 297 |
} finally { |
| 298 |
Process.setThreadPriority(pri); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public void onClose() { |
| 304 |
int pri = Process.getThreadPriority(Process.myTid()); |
| 305 |
try { |
| 306 |
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 307 |
mSession.onClose(); |
| 308 |
} finally { |
| 309 |
Process.setThreadPriority(pri); |
| 310 |
mListener = null; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
public String getLocale() { |
| 315 |
return mLocale; |
| 316 |
} |
| 317 |
|
| 318 |
public Bundle getBundle() { |
| 319 |
return mBundle; |
| 320 |
} |
| 321 |
|
| 322 |
public @SuggestionsInfo.ResultAttrs int getSupportedAttributes() { |
| 323 |
return mSupportedAttributes; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
private static class SpellCheckerServiceBinder extends ISpellCheckerService.Stub { |
| 328 |
private final WeakReference<SpellCheckerService> mInternalServiceRef; |
| 329 |
|
| 330 |
public SpellCheckerServiceBinder(SpellCheckerService service) { |
| 331 |
mInternalServiceRef = new WeakReference<SpellCheckerService>(service); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
@Override |
| 350 |
public void getISpellCheckerSession( |
| 351 |
String locale, ISpellCheckerSessionListener listener, Bundle bundle, |
| 352 |
@SuggestionsInfo.ResultAttrs int supportedAttributes, |
| 353 |
ISpellCheckerServiceCallback callback) { |
| 354 |
final SpellCheckerService service = mInternalServiceRef.get(); |
| 355 |
final InternalISpellCheckerSession internalSession; |
| 356 |
if (service == null) { |
| 357 |
|
| 358 |
|
| 359 |
internalSession = null; |
| 360 |
} else { |
| 361 |
final Session session = service.createSession(); |
| 362 |
internalSession = new InternalISpellCheckerSession( |
| 363 |
locale, listener, bundle, session, supportedAttributes); |
| 364 |
session.onCreate(); |
| 365 |
} |
| 366 |
try { |
| 367 |
callback.onSessionCreated(internalSession); |
| 368 |
} catch (RemoteException e) { |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
private static class SentenceLevelAdapter { |
| 379 |
public static final SentenceSuggestionsInfo[] EMPTY_SENTENCE_SUGGESTIONS_INFOS = |
| 380 |
new SentenceSuggestionsInfo[] {}; |
| 381 |
private static final SuggestionsInfo EMPTY_SUGGESTIONS_INFO = new SuggestionsInfo(0, null); |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public static class SentenceWordItem { |
| 386 |
public final TextInfo mTextInfo; |
| 387 |
public final int mStart; |
| 388 |
public final int mLength; |
| 389 |
public SentenceWordItem(TextInfo ti, int start, int end) { |
| 390 |
mTextInfo = ti; |
| 391 |
mStart = start; |
| 392 |
mLength = end - start; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public static class SentenceTextInfoParams { |
| 400 |
final TextInfo mOriginalTextInfo; |
| 401 |
final ArrayList<SentenceWordItem> mItems; |
| 402 |
final int mSize; |
| 403 |
public SentenceTextInfoParams(TextInfo ti, ArrayList<SentenceWordItem> items) { |
| 404 |
mOriginalTextInfo = ti; |
| 405 |
mItems = items; |
| 406 |
mSize = items.size(); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
private final WordIterator mWordIterator; |
| 411 |
public SentenceLevelAdapter(Locale locale) { |
| 412 |
mWordIterator = new WordIterator(locale); |
| 413 |
} |
| 414 |
|
| 415 |
private SentenceTextInfoParams getSplitWords(TextInfo originalTextInfo) { |
| 416 |
final WordIterator wordIterator = mWordIterator; |
| 417 |
final CharSequence originalText = originalTextInfo.getText(); |
| 418 |
final int cookie = originalTextInfo.getCookie(); |
| 419 |
final int start = 0; |
| 420 |
final int end = originalText.length(); |
| 421 |
final ArrayList<SentenceWordItem> wordItems = new ArrayList<SentenceWordItem>(); |
| 422 |
wordIterator.setCharSequence(originalText, 0, originalText.length()); |
| 423 |
int wordEnd = wordIterator.following(start); |
| 424 |
int wordStart = wordEnd == BreakIterator.DONE ? BreakIterator.DONE |
| 425 |
: wordIterator.getBeginning(wordEnd); |
| 426 |
if (DBG) { |
| 427 |
Log.d(TAG, "iterator: break: ---- 1st word start = " + wordStart + ", end = " |
| 428 |
+ wordEnd + "\n" + originalText); |
| 429 |
} |
| 430 |
while (wordStart <= end && wordEnd != BreakIterator.DONE |
| 431 |
&& wordStart != BreakIterator.DONE) { |
| 432 |
if (wordEnd >= start && wordEnd > wordStart) { |
| 433 |
final CharSequence query = originalText.subSequence(wordStart, wordEnd); |
| 434 |
final TextInfo ti = new TextInfo(query, 0, query.length(), cookie, |
| 435 |
query.hashCode()); |
| 436 |
wordItems.add(new SentenceWordItem(ti, wordStart, wordEnd)); |
| 437 |
if (DBG) { |
| 438 |
Log.d(TAG, "Adapter: word (" + (wordItems.size() - 1) + ") " + query); |
| 439 |
} |
| 440 |
} |
| 441 |
wordEnd = wordIterator.following(wordEnd); |
| 442 |
if (wordEnd == BreakIterator.DONE) { |
| 443 |
break; |
| 444 |
} |
| 445 |
wordStart = wordIterator.getBeginning(wordEnd); |
| 446 |
} |
| 447 |
return new SentenceTextInfoParams(originalTextInfo, wordItems); |
| 448 |
} |
| 449 |
|
| 450 |
public static SentenceSuggestionsInfo reconstructSuggestions( |
| 451 |
SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) { |
| 452 |
if (results == null || results.length == 0) { |
| 453 |
return null; |
| 454 |
} |
| 455 |
if (DBG) { |
| 456 |
Log.w(TAG, "Adapter: onGetSuggestions: got " + results.length); |
| 457 |
} |
| 458 |
if (originalTextInfoParams == null) { |
| 459 |
if (DBG) { |
| 460 |
Log.w(TAG, "Adapter: originalTextInfoParams is null."); |
| 461 |
} |
| 462 |
return null; |
| 463 |
} |
| 464 |
final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie(); |
| 465 |
final int originalSequence = |
| 466 |
originalTextInfoParams.mOriginalTextInfo.getSequence(); |
| 467 |
|
| 468 |
final int querySize = originalTextInfoParams.mSize; |
| 469 |
final int[] offsets = new int[querySize]; |
| 470 |
final int[] lengths = new int[querySize]; |
| 471 |
final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize]; |
| 472 |
for (int i = 0; i < querySize; ++i) { |
| 473 |
final SentenceWordItem item = originalTextInfoParams.mItems.get(i); |
| 474 |
SuggestionsInfo result = null; |
| 475 |
for (int j = 0; j < results.length; ++j) { |
| 476 |
final SuggestionsInfo cur = results[j]; |
| 477 |
if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) { |
| 478 |
result = cur; |
| 479 |
result.setCookieAndSequence(originalCookie, originalSequence); |
| 480 |
break; |
| 481 |
} |
| 482 |
} |
| 483 |
offsets[i] = item.mStart; |
| 484 |
lengths[i] = item.mLength; |
| 485 |
reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO; |
| 486 |
if (DBG) { |
| 487 |
final int size = reconstructedSuggestions[i].getSuggestionsCount(); |
| 488 |
Log.w(TAG, "reconstructedSuggestions(" + i + ")" + size + ", first = " |
| 489 |
+ (size > 0 ? reconstructedSuggestions[i].getSuggestionAt(0) |
| 490 |
: "<none>") + ", offset = " + offsets[i] + ", length = " |
| 491 |
+ lengths[i]); |
| 492 |
} |
| 493 |
} |
| 494 |
return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths); |
| 495 |
} |
| 496 |
} |
| 497 |
} |