2011-07-25 01:35:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2011-07-25 01:35:51 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-25 01:35:51 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2011-07-25 01:35:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin.spellcheck;
|
|
|
|
|
2011-08-16 10:43:16 +00:00
|
|
|
import android.content.Intent;
|
2011-11-09 23:21:42 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
2011-07-25 01:35:51 +00:00
|
|
|
import android.service.textservice.SpellCheckerService;
|
2013-08-07 07:26:10 +00:00
|
|
|
import android.text.InputType;
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
import android.view.inputmethod.InputMethodSubtype;
|
2011-07-25 01:35:51 +00:00
|
|
|
import android.view.textservice.SuggestionsInfo;
|
|
|
|
|
2014-07-10 03:51:37 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
|
|
|
import com.android.inputmethod.keyboard.KeyboardId;
|
2013-05-07 10:09:30 +00:00
|
|
|
import com.android.inputmethod.keyboard.KeyboardLayoutSet;
|
2014-07-10 03:51:37 +00:00
|
|
|
import com.android.inputmethod.latin.DictionaryFacilitator;
|
2014-08-27 13:00:48 +00:00
|
|
|
import com.android.inputmethod.latin.DictionaryFacilitatorLruCache;
|
2014-09-29 01:52:18 +00:00
|
|
|
import com.android.inputmethod.latin.NgramContext;
|
2011-09-02 05:36:04 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2014-08-25 04:42:15 +00:00
|
|
|
import com.android.inputmethod.latin.RichInputMethodSubtype;
|
2015-02-04 20:50:31 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords;
|
2015-02-10 01:09:15 +00:00
|
|
|
import com.android.inputmethod.latin.common.ComposedData;
|
2014-07-10 03:51:37 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion;
|
2013-08-07 07:26:10 +00:00
|
|
|
import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
|
2014-06-26 04:07:49 +00:00
|
|
|
import com.android.inputmethod.latin.utils.ScriptUtils;
|
2014-07-10 03:51:37 +00:00
|
|
|
import com.android.inputmethod.latin.utils.SuggestionResults;
|
2011-08-04 11:19:12 +00:00
|
|
|
|
|
|
|
import java.util.Locale;
|
2014-07-10 03:51:37 +00:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
|
|
import java.util.concurrent.Semaphore;
|
2011-08-04 11:19:12 +00:00
|
|
|
|
2015-02-25 19:27:48 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2011-07-25 01:35:51 +00:00
|
|
|
/**
|
|
|
|
* Service for spell checking, using LatinIME's dictionaries and mechanisms.
|
|
|
|
*/
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class AndroidSpellCheckerService extends SpellCheckerService
|
2011-11-09 23:21:42 +00:00
|
|
|
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2015-02-05 21:59:16 +00:00
|
|
|
private static final String TAG = AndroidSpellCheckerService.class.getSimpleName();
|
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
|
2011-11-09 23:21:42 +00:00
|
|
|
public static final String PREF_USE_CONTACTS_KEY = "pref_spellcheck_use_contacts";
|
|
|
|
|
2013-08-07 07:26:10 +00:00
|
|
|
private static final int SPELLCHECKER_DUMMY_KEYBOARD_WIDTH = 480;
|
2014-10-24 05:59:52 +00:00
|
|
|
private static final int SPELLCHECKER_DUMMY_KEYBOARD_HEIGHT = 301;
|
2013-08-07 07:26:10 +00:00
|
|
|
|
2014-07-10 03:51:37 +00:00
|
|
|
private static final String DICTIONARY_NAME_PREFIX = "spellcheck_";
|
|
|
|
|
|
|
|
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
|
|
|
|
|
|
|
private final int MAX_NUM_OF_THREADS_READ_DICTIONARY = 2;
|
|
|
|
private final Semaphore mSemaphore = new Semaphore(MAX_NUM_OF_THREADS_READ_DICTIONARY,
|
|
|
|
true /* fair */);
|
|
|
|
// TODO: Make each spell checker session has its own session id.
|
|
|
|
private final ConcurrentLinkedQueue<Integer> mSessionIdPool = new ConcurrentLinkedQueue<>();
|
|
|
|
|
2014-08-27 13:00:48 +00:00
|
|
|
private final DictionaryFacilitatorLruCache mDictionaryFacilitatorCache =
|
2015-02-06 19:18:06 +00:00
|
|
|
new DictionaryFacilitatorLruCache(this /* context */, DICTIONARY_NAME_PREFIX);
|
2014-07-10 03:51:37 +00:00
|
|
|
private final ConcurrentHashMap<Locale, Keyboard> mKeyboardCache = new ConcurrentHashMap<>();
|
2011-08-04 11:19:12 +00:00
|
|
|
|
2011-11-09 17:52:46 +00:00
|
|
|
// The threshold for a suggestion to be considered "recommended".
|
2012-05-16 11:42:12 +00:00
|
|
|
private float mRecommendedThreshold;
|
2014-07-10 03:51:37 +00:00
|
|
|
// TODO: make a spell checker option to block offensive words or not
|
|
|
|
private final SettingsValuesForSuggestion mSettingsValuesForSuggestion =
|
2015-03-12 00:09:44 +00:00
|
|
|
new SettingsValuesForSuggestion(true /* blockPotentiallyOffensive */);
|
2011-09-02 05:36:04 +00:00
|
|
|
|
2012-07-10 06:33:18 +00:00
|
|
|
public static final String SINGLE_QUOTE = "\u0027";
|
|
|
|
public static final String APOSTROPHE = "\u2019";
|
2011-12-08 07:52:08 +00:00
|
|
|
|
2014-07-10 03:51:37 +00:00
|
|
|
public AndroidSpellCheckerService() {
|
|
|
|
super();
|
|
|
|
for (int i = 0; i < MAX_NUM_OF_THREADS_READ_DICTIONARY; i++) {
|
|
|
|
mSessionIdPool.add(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 17:49:29 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2011-09-02 05:36:04 +00:00
|
|
|
super.onCreate();
|
2015-04-02 02:18:52 +00:00
|
|
|
mRecommendedThreshold = Float.parseFloat(
|
|
|
|
getString(R.string.spellchecker_recommended_threshold_value));
|
2011-11-09 23:21:42 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
onSharedPreferenceChanged(prefs, PREF_USE_CONTACTS_KEY);
|
|
|
|
}
|
|
|
|
|
2014-07-10 07:23:27 +00:00
|
|
|
public float getRecommendedThreshold() {
|
|
|
|
return mRecommendedThreshold;
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:19:47 +00:00
|
|
|
private static String getKeyboardLayoutNameForLocale(final Locale locale) {
|
|
|
|
// See b/19963288.
|
|
|
|
if (locale.getLanguage().equals("sr")) {
|
|
|
|
return "south_slavic";
|
|
|
|
}
|
|
|
|
final int script = ScriptUtils.getScriptFromSpellCheckerLocale(locale);
|
2013-05-07 10:09:30 +00:00
|
|
|
switch (script) {
|
2014-06-26 04:07:49 +00:00
|
|
|
case ScriptUtils.SCRIPT_LATIN:
|
2013-05-07 10:09:30 +00:00
|
|
|
return "qwerty";
|
2014-06-26 04:07:49 +00:00
|
|
|
case ScriptUtils.SCRIPT_CYRILLIC:
|
2013-05-07 10:09:30 +00:00
|
|
|
return "east_slavic";
|
2014-06-26 04:07:49 +00:00
|
|
|
case ScriptUtils.SCRIPT_GREEK:
|
2013-05-07 10:09:30 +00:00
|
|
|
return "greek";
|
2015-03-27 21:42:27 +00:00
|
|
|
case ScriptUtils.SCRIPT_HEBREW:
|
|
|
|
return "hebrew";
|
2013-05-07 10:09:30 +00:00
|
|
|
default:
|
|
|
|
throw new RuntimeException("Wrong script supplied: " + script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 23:21:42 +00:00
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
|
|
|
|
if (!PREF_USE_CONTACTS_KEY.equals(key)) return;
|
2015-02-20 01:09:03 +00:00
|
|
|
final boolean useContactsDictionary = prefs.getBoolean(PREF_USE_CONTACTS_KEY, true);
|
|
|
|
mDictionaryFacilitatorCache.setUseContactsDictionary(useContactsDictionary);
|
2011-09-02 05:36:04 +00:00
|
|
|
}
|
|
|
|
|
2011-08-05 10:42:36 +00:00
|
|
|
@Override
|
|
|
|
public Session createSession() {
|
2012-07-10 06:51:23 +00:00
|
|
|
// Should not refer to AndroidSpellCheckerSession directly considering
|
|
|
|
// that AndroidSpellCheckerSession may be overlaid.
|
|
|
|
return AndroidSpellCheckerSessionFactory.newInstance(this);
|
2011-08-05 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 06:48:04 +00:00
|
|
|
/**
|
|
|
|
* Returns an empty SuggestionsInfo with flags signaling the word is not in the dictionary.
|
|
|
|
* @param reportAsTypo whether this should include the flag LOOKS_LIKE_TYPO, for red underline.
|
|
|
|
* @return the empty SuggestionsInfo with the appropriate flags set.
|
|
|
|
*/
|
|
|
|
public static SuggestionsInfo getNotInDictEmptySuggestions(final boolean reportAsTypo) {
|
|
|
|
return new SuggestionsInfo(reportAsTypo ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0,
|
|
|
|
EMPTY_STRING_ARRAY);
|
2011-10-24 13:25:37 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 06:48:04 +00:00
|
|
|
/**
|
|
|
|
* Returns an empty suggestionInfo with flags signaling the word is in the dictionary.
|
|
|
|
* @return the empty SuggestionsInfo with the appropriate flags set.
|
|
|
|
*/
|
2012-07-10 06:33:18 +00:00
|
|
|
public static SuggestionsInfo getInDictEmptySuggestions() {
|
2011-10-24 13:25:37 +00:00
|
|
|
return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY,
|
|
|
|
EMPTY_STRING_ARRAY);
|
|
|
|
}
|
|
|
|
|
2014-07-10 03:51:37 +00:00
|
|
|
public boolean isValidWord(final Locale locale, final String word) {
|
|
|
|
mSemaphore.acquireUninterruptibly();
|
|
|
|
try {
|
|
|
|
DictionaryFacilitator dictionaryFacilitatorForLocale =
|
2014-08-27 13:00:48 +00:00
|
|
|
mDictionaryFacilitatorCache.get(locale);
|
2015-02-05 01:15:04 +00:00
|
|
|
return dictionaryFacilitatorForLocale.isValidSpellingWord(word);
|
2014-07-10 03:51:37 +00:00
|
|
|
} finally {
|
|
|
|
mSemaphore.release();
|
|
|
|
}
|
2012-03-26 07:33:27 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 01:09:15 +00:00
|
|
|
public SuggestionResults getSuggestionResults(final Locale locale,
|
|
|
|
final ComposedData composedData, final NgramContext ngramContext,
|
2015-02-25 19:27:48 +00:00
|
|
|
@Nonnull final Keyboard keyboard) {
|
2014-07-10 03:51:37 +00:00
|
|
|
Integer sessionId = null;
|
|
|
|
mSemaphore.acquireUninterruptibly();
|
|
|
|
try {
|
|
|
|
sessionId = mSessionIdPool.poll();
|
|
|
|
DictionaryFacilitator dictionaryFacilitatorForLocale =
|
2014-08-27 13:00:48 +00:00
|
|
|
mDictionaryFacilitatorCache.get(locale);
|
2015-02-10 01:09:15 +00:00
|
|
|
return dictionaryFacilitatorForLocale.getSuggestionResults(composedData, ngramContext,
|
2015-02-25 19:27:48 +00:00
|
|
|
keyboard, mSettingsValuesForSuggestion,
|
|
|
|
sessionId, SuggestedWords.INPUT_STYLE_TYPING);
|
2014-07-10 03:51:37 +00:00
|
|
|
} finally {
|
|
|
|
if (sessionId != null) {
|
|
|
|
mSessionIdPool.add(sessionId);
|
|
|
|
}
|
|
|
|
mSemaphore.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasMainDictionaryForLocale(final Locale locale) {
|
|
|
|
mSemaphore.acquireUninterruptibly();
|
|
|
|
try {
|
|
|
|
final DictionaryFacilitator dictionaryFacilitator =
|
2014-08-27 13:00:48 +00:00
|
|
|
mDictionaryFacilitatorCache.get(locale);
|
2014-09-11 09:51:31 +00:00
|
|
|
return dictionaryFacilitator.hasAtLeastOneInitializedMainDictionary();
|
2014-07-10 03:51:37 +00:00
|
|
|
} finally {
|
|
|
|
mSemaphore.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onUnbind(final Intent intent) {
|
|
|
|
mSemaphore.acquireUninterruptibly(MAX_NUM_OF_THREADS_READ_DICTIONARY);
|
|
|
|
try {
|
2015-02-20 01:09:03 +00:00
|
|
|
mDictionaryFacilitatorCache.closeDictionaries();
|
2014-07-10 03:51:37 +00:00
|
|
|
} finally {
|
|
|
|
mSemaphore.release(MAX_NUM_OF_THREADS_READ_DICTIONARY);
|
|
|
|
}
|
|
|
|
mKeyboardCache.clear();
|
|
|
|
return false;
|
2011-08-16 10:43:16 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 03:51:37 +00:00
|
|
|
public Keyboard getKeyboardForLocale(final Locale locale) {
|
|
|
|
Keyboard keyboard = mKeyboardCache.get(locale);
|
|
|
|
if (keyboard == null) {
|
|
|
|
keyboard = createKeyboardForLocale(locale);
|
|
|
|
if (keyboard != null) {
|
|
|
|
mKeyboardCache.put(locale, keyboard);
|
|
|
|
}
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
2014-07-10 03:51:37 +00:00
|
|
|
return keyboard;
|
2011-08-16 08:37:18 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 03:51:37 +00:00
|
|
|
private Keyboard createKeyboardForLocale(final Locale locale) {
|
2015-03-27 19:19:47 +00:00
|
|
|
final String keyboardLayoutName = getKeyboardLayoutNameForLocale(locale);
|
2014-07-02 04:29:36 +00:00
|
|
|
final InputMethodSubtype subtype = AdditionalSubtypeUtils.createDummyAdditionalSubtype(
|
|
|
|
locale.toString(), keyboardLayoutName);
|
2013-08-07 07:26:10 +00:00
|
|
|
final KeyboardLayoutSet keyboardLayoutSet = createKeyboardSetForSpellChecker(subtype);
|
2014-07-10 03:51:37 +00:00
|
|
|
return keyboardLayoutSet.getKeyboard(KeyboardId.ELEMENT_ALPHABET);
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
2013-08-07 07:26:10 +00:00
|
|
|
|
|
|
|
private KeyboardLayoutSet createKeyboardSetForSpellChecker(final InputMethodSubtype subtype) {
|
|
|
|
final EditorInfo editorInfo = new EditorInfo();
|
|
|
|
editorInfo.inputType = InputType.TYPE_CLASS_TEXT;
|
|
|
|
final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(this, editorInfo);
|
|
|
|
builder.setKeyboardGeometry(
|
|
|
|
SPELLCHECKER_DUMMY_KEYBOARD_WIDTH, SPELLCHECKER_DUMMY_KEYBOARD_HEIGHT);
|
2015-03-06 22:34:57 +00:00
|
|
|
builder.setSubtype(RichInputMethodSubtype.getRichInputMethodSubtype(subtype));
|
2013-08-07 07:26:10 +00:00
|
|
|
builder.setIsSpellChecker(true /* isSpellChecker */);
|
|
|
|
builder.disableTouchPositionCorrectionData();
|
|
|
|
return builder.build();
|
|
|
|
}
|
2011-07-25 01:35:51 +00:00
|
|
|
}
|