1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package android.service.textservice;
import com.android.internal.textservice.ISpellCheckerService;
import com.android.internal.textservice.ISpellCheckerServiceCallback;
import com.android.internal.textservice.ISpellCheckerSession;
import com.android.internal.textservice.ISpellCheckerSessionListener;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.text.TextUtils;
import android.text.method.WordIterator;
import android.util.Log;
import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
import java.lang.ref.WeakReference;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Locale;
/**
* SpellCheckerService provides an abstract base class for a spell checker.
* This class combines a service to the system with the spell checker service interface that
* spell checker must implement.
*
* <p>In addition to the normal Service lifecycle methods, this class
* introduces a new specific callback that subclasses should override
* {@link #createSession()} to provide a spell checker session that is corresponding
* to requested language and so on. The spell checker session returned by this method
* should extend {@link SpellCheckerService.Session}.
* </p>
*
* <h3>Returning spell check results</h3>
*
* <p>{@link SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
* should return spell check results.
* It receives {@link android.view.textservice.TextInfo} and returns
* {@link android.view.textservice.SuggestionsInfo} for the input.
* You may want to override
* {@link SpellCheckerService.Session#onGetSuggestionsMultiple(TextInfo[], int, boolean)} for
* better performance and quality.
* </p>
*
* <p>Please note that {@link SpellCheckerService.Session#getLocale()} does not return a valid
* locale before {@link SpellCheckerService.Session#onCreate()} </p>
*
*/
public abstract class SpellCheckerService extends Service {
private static final String TAG = SpellCheckerService.class.getSimpleName();
private static final boolean DBG = false;
public static final String SERVICE_INTERFACE =
"android.service.textservice.SpellCheckerService";
private final SpellCheckerServiceBinder mBinder = new SpellCheckerServiceBinder(this);
/**
* Implement to return the implementation of the internal spell checker
* service interface. Subclasses should not override.
*/
@Override
public final IBinder onBind(final Intent intent) {
if (DBG) {
Log.w(TAG, "onBind");
}
return mBinder;
}
/**
* Factory method to create a spell checker session impl
* @return SpellCheckerSessionImpl which should be overridden by a concrete implementation.
*/
public abstract Session createSession();
/**
* This abstract class should be overridden by a concrete implementation of a spell checker.
*/
public static abstract class Session {
private InternalISpellCheckerSession mInternalSession;
private volatile SentenceLevelAdapter mSentenceLevelAdapter;
/**
* @hide
*/
public final void setInternalISpellCheckerSession(InternalISpellCheckerSession session) {
mInternalSession = session;
}
/**
* This is called after the class is initialized, at which point it knows it can call
* getLocale() etc...
*/
public abstract void onCreate();
/**
* Get suggestions for specified text in TextInfo.
* This function will run on the incoming IPC thread.
* So, this is not called on the main thread,
* but will be called in series on another thread.
* @param textInfo the text metadata
* @param suggestionsLimit the maximum number of suggestions to be returned
* @return SuggestionsInfo which contains suggestions for textInfo
*/
public abstract SuggestionsInfo onGetSuggestions(TextInfo textInfo, int suggestionsLimit);
/**
* A batch process of onGetSuggestions.
* This function will run on the incoming IPC thread.
* So, this is not called on the main thread,
* but will be called in series on another thread.
* @param textInfos an array of the text metadata
* @param suggestionsLimit the maximum number of suggestions to be returned
* @param sequentialWords true if textInfos can be treated as sequential words.
* @return an array of {@link SentenceSuggestionsInfo} returned by
* {@link SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
*/
public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos,
int suggestionsLimit, boolean sequentialWords) {
final int length = textInfos.length;
final SuggestionsInfo[] retval = new SuggestionsInfo[length];
for (int i = 0; i < length; ++i) {
retval[i] = onGetSuggestions(textInfos[i], suggestionsLimit);
retval[i].setCookieAndSequence(
textInfos[i].getCookie(), textInfos[i].getSequence());
}
return retval;
}
/**
* Get sentence suggestions for specified texts in an array of TextInfo.
* The default implementation splits the input text to words and returns
* {@link SentenceSuggestionsInfo} which contains suggestions for each word.
* This function will run on the incoming IPC thread.
* So, this is not called on the main thread,
* but will be called in series on another thread.
* When you override this method, make sure that suggestionsLimit is applied to suggestions
* that share the same start position and length.
* @param textInfos an array of the text metadata
* @param suggestionsLimit the maximum number of suggestions to be returned
* @return an array of {@link SentenceSuggestionsInfo} returned by
* {@link SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
*/
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,
int suggestionsLimit) {
if (textInfos == null || textInfos.length == 0) {
return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
}
if (DBG) {
Log.d(TAG, "onGetSentenceSuggestionsMultiple: + " + textInfos.length + ", "
+ suggestionsLimit);
}
if (mSentenceLevelAdapter == null) {
synchronized(this) {
if (mSentenceLevelAdapter == null) {
final String localeStr = getLocale();
if (!TextUtils.isEmpty(localeStr)) {
mSentenceLevelAdapter = new SentenceLevelAdapter(new Locale(localeStr));
}
}
}
}
if (mSentenceLevelAdapter == null) {
return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS;
}
final int infosSize = textInfos.length;
final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize];
for (int i = 0; i < infosSize; ++i) {
final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams =
mSentenceLevelAdapter.getSplitWords(textInfos[i]);
final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems =
textInfoParams.mItems;
final int itemsSize = mItems.size();
final TextInfo[] splitTextInfos = new TextInfo[itemsSize];
for (int j = 0; j < itemsSize; ++j) {
splitTextInfos[j] = mItems.get(j).mTextInfo;
}
retval[i] = SentenceLevelAdapter.reconstructSuggestions(
textInfoParams, onGetSuggestionsMultiple(
splitTextInfos, suggestionsLimit, true));
}
return retval;
}
/**
* Request to abort all tasks executed in SpellChecker.
* This function will run on the incoming IPC thread.
* So, this is not called on the main thread,
* but will be called in series on another thread.
*/
public void onCancel() {}
/**
* Request to close this session.
* This function will run on the incoming IPC thread.
* So, this is not called on the main thread,
* but will be called in series on another thread.
*/
public void onClose() {}
/**
* @return Locale for this session
*/
public String getLocale() {
return mInternalSession.getLocale();
}
/**
* @return Bundle for this session
*/
public Bundle getBundle() {
return mInternalSession.getBundle();
}
}
// Preventing from exposing ISpellCheckerSession.aidl, create an internal class.
private static class InternalISpellCheckerSession extends ISpellCheckerSession.Stub {
private ISpellCheckerSessionListener mListener;
private final Session mSession;
private final String mLocale;
private final Bundle mBundle;
public InternalISpellCheckerSession(String locale, ISpellCheckerSessionListener listener,
Bundle bundle, Session session) {
mListener = listener;
mSession = session;
mLocale = locale;
mBundle = bundle;
session.setInternalISpellCheckerSession(this);
}
@Override
public void onGetSuggestionsMultiple(
TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) {
int pri = Process.getThreadPriority(Process.myTid());
try {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
mListener.onGetSuggestions(
mSession.onGetSuggestionsMultiple(
textInfos, suggestionsLimit, sequentialWords));
} catch (RemoteException e) {
} finally {
Process.setThreadPriority(pri);
}
}
@Override
public void onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit) {
try {
mListener.onGetSentenceSuggestions(
mSession.onGetSentenceSuggestionsMultiple(textInfos, suggestionsLimit));
} catch (RemoteException e) {
}
}
@Override
public void onCancel() {
int pri = Process.getThreadPriority(Process.myTid());
try {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
mSession.onCancel();
} finally {
Process.setThreadPriority(pri);
}
}
@Override
public void onClose() {
int pri = Process.getThreadPriority(Process.myTid());
try {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
mSession.onClose();
} finally {
Process.setThreadPriority(pri);
mListener = null;
}
}
public String getLocale() {
return mLocale;
}
public Bundle getBundle() {
return mBundle;
}
}
private static class SpellCheckerServiceBinder extends ISpellCheckerService.Stub {
private final WeakReference<SpellCheckerService> mInternalServiceRef;
public SpellCheckerServiceBinder(SpellCheckerService service) {
mInternalServiceRef = new WeakReference<SpellCheckerService>(service);
}
/**
* Called from the system when an application is requesting a new spell checker session.
*
* <p>Note: This is an internal protocol used by the system to establish spell checker
* sessions, which is not guaranteed to be stable and is subject to change.</p>
*
* @param locale locale to be returned from {@link Session#getLocale()}
* @param listener IPC channel object to be used to implement
* {@link Session#onGetSuggestionsMultiple(TextInfo[], int, boolean)} and
* {@link Session#onGetSuggestions(TextInfo, int)}
* @param bundle bundle to be returned from {@link Session#getBundle()}
* @param callback IPC channel to return the result to the caller in an asynchronous manner
*/
@Override
public void getISpellCheckerSession(
String locale, ISpellCheckerSessionListener listener, Bundle bundle,
ISpellCheckerServiceCallback callback) {
final SpellCheckerService service = mInternalServiceRef.get();
final InternalISpellCheckerSession internalSession;
if (service == null) {
// If the owner SpellCheckerService object was already destroyed and got GC-ed,
// the weak-reference returns null and we should just ignore this request.
internalSession = null;
} else {
final Session session = service.createSession();
internalSession =
new InternalISpellCheckerSession(locale, listener, bundle, session);
session.onCreate();
}
try {
callback.onSessionCreated(internalSession);
} catch (RemoteException e) {
}
}
}
/**
* Adapter class to accommodate word level spell checking APIs to sentence level spell checking
* APIs used in
* {@link SpellCheckerService.Session#onGetSuggestionsMultiple(TextInfo[], int, boolean)}
*/
private static class SentenceLevelAdapter {
public static final SentenceSuggestionsInfo[] EMPTY_SENTENCE_SUGGESTIONS_INFOS =
new SentenceSuggestionsInfo[] {};
private static final SuggestionsInfo EMPTY_SUGGESTIONS_INFO = new SuggestionsInfo(0, null);
/**
* Container for split TextInfo parameters
*/
public static class SentenceWordItem {
public final TextInfo mTextInfo;
public final int mStart;
public final int mLength;
public SentenceWordItem(TextInfo ti, int start, int end) {
mTextInfo = ti;
mStart = start;
mLength = end - start;
}
}
/**
* Container for originally queried TextInfo and parameters
*/
public static class SentenceTextInfoParams {
final TextInfo mOriginalTextInfo;
final ArrayList<SentenceWordItem> mItems;
final int mSize;
public SentenceTextInfoParams(TextInfo ti, ArrayList<SentenceWordItem> items) {
mOriginalTextInfo = ti;
mItems = items;
mSize = items.size();
}
}
private final WordIterator mWordIterator;
public SentenceLevelAdapter(Locale locale) {
mWordIterator = new WordIterator(locale);
}
private SentenceTextInfoParams getSplitWords(TextInfo originalTextInfo) {
final WordIterator wordIterator = mWordIterator;
final CharSequence originalText = originalTextInfo.getText();
final int cookie = originalTextInfo.getCookie();
final int start = 0;
final int end = originalText.length();
final ArrayList<SentenceWordItem> wordItems = new ArrayList<SentenceWordItem>();
wordIterator.setCharSequence(originalText, 0, originalText.length());
int wordEnd = wordIterator.following(start);
int wordStart = wordIterator.getBeginning(wordEnd);
if (DBG) {
Log.d(TAG, "iterator: break: ---- 1st word start = " + wordStart + ", end = "
+ wordEnd + "\n" + originalText);
}
while (wordStart <= end && wordEnd != BreakIterator.DONE
&& wordStart != BreakIterator.DONE) {
if (wordEnd >= start && wordEnd > wordStart) {
final CharSequence query = originalText.subSequence(wordStart, wordEnd);
final TextInfo ti = new TextInfo(query, 0, query.length(), cookie,
query.hashCode());
wordItems.add(new SentenceWordItem(ti, wordStart, wordEnd));
if (DBG) {
Log.d(TAG, "Adapter: word (" + (wordItems.size() - 1) + ") " + query);
}
}
wordEnd = wordIterator.following(wordEnd);
if (wordEnd == BreakIterator.DONE) {
break;
}
wordStart = wordIterator.getBeginning(wordEnd);
}
return new SentenceTextInfoParams(originalTextInfo, wordItems);
}
public static SentenceSuggestionsInfo reconstructSuggestions(
SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) {
if (results == null || results.length == 0) {
return null;
}
if (DBG) {
Log.w(TAG, "Adapter: onGetSuggestions: got " + results.length);
}
if (originalTextInfoParams == null) {
if (DBG) {
Log.w(TAG, "Adapter: originalTextInfoParams is null.");
}
return null;
}
final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie();
final int originalSequence =
originalTextInfoParams.mOriginalTextInfo.getSequence();
final int querySize = originalTextInfoParams.mSize;
final int[] offsets = new int[querySize];
final int[] lengths = new int[querySize];
final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize];
for (int i = 0; i < querySize; ++i) {
final SentenceWordItem item = originalTextInfoParams.mItems.get(i);
SuggestionsInfo result = null;
for (int j = 0; j < results.length; ++j) {
final SuggestionsInfo cur = results[j];
if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) {
result = cur;
result.setCookieAndSequence(originalCookie, originalSequence);
break;
}
}
offsets[i] = item.mStart;
lengths[i] = item.mLength;
reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO;
if (DBG) {
final int size = reconstructedSuggestions[i].getSuggestionsCount();
Log.w(TAG, "reconstructedSuggestions(" + i + ")" + size + ", first = "
+ (size > 0 ? reconstructedSuggestions[i].getSuggestionAt(0)
: "<none>") + ", offset = " + offsets[i] + ", length = "
+ lengths[i]);
}
}
return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths);
}
}
}
|