diff options
Diffstat (limited to 'java/src')
24 files changed, 1349 insertions, 1015 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index 32ef408b4..1ae0020a4 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -610,6 +610,15 @@ public class PointerTracker { onUpEventInternal(); onDownEventInternal(x, y, eventTime); } else { + // HACK: If there are currently multiple touches, register the key even if + // the finger slides off the key. This defends against noise from some + // touch panels when there are close multiple touches. + // Caveat: When in chording input mode with a modifier key, we don't use + // this hack. + if (me != null && me.getPointerCount() > 1 + && !sPointerTrackerQueue.hasModifierKeyOlderThan(this)) { + onUpEventInternal(); + } mKeyAlreadyProcessed = true; setReleasedKeyGraphics(oldKey); } diff --git a/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java index 5db65c660..d3bb85d4b 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java @@ -28,6 +28,7 @@ public class PointerTrackerQueue { private static final String TAG = PointerTrackerQueue.class.getSimpleName(); private static final boolean DEBUG = false; + // TODO: Use ring buffer instead of {@link LinkedList}. private final LinkedList<PointerTracker> mQueue = new LinkedList<PointerTracker>(); public synchronized void add(PointerTracker tracker) { @@ -81,6 +82,20 @@ public class PointerTrackerQueue { } } + public synchronized boolean hasModifierKeyOlderThan(PointerTracker tracker) { + final Iterator<PointerTracker> it = mQueue.iterator(); + while (it.hasNext()) { + final PointerTracker t = it.next(); + if (t == tracker) { + break; + } + if (t.isModifier()) { + return true; + } + } + return false; + } + public synchronized boolean isAnyInSlidingKeyInput() { for (final PointerTracker tracker : mQueue) { if (tracker.isInSlidingKeyInput()) { diff --git a/java/src/com/android/inputmethod/latin/AutoCorrection.java b/java/src/com/android/inputmethod/latin/AutoCorrection.java index c78974dac..a66337404 100644 --- a/java/src/com/android/inputmethod/latin/AutoCorrection.java +++ b/java/src/com/android/inputmethod/latin/AutoCorrection.java @@ -92,7 +92,8 @@ public class AutoCorrection { public static boolean suggestionExceedsAutoCorrectionThreshold(SuggestedWordInfo suggestion, CharSequence consideredWord, float autoCorrectionThreshold) { if (null != suggestion) { - //final int autoCorrectionSuggestionScore = sortedScores[0]; + // Shortlist a whitelisted word + if (suggestion.mKind == SuggestedWordInfo.KIND_WHITELIST) return true; final int autoCorrectionSuggestionScore = suggestion.mScore; // TODO: when the normalized score of the first suggestion is nearly equals to // the normalized score of the second suggestion, behave less aggressive. diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index ae415d0ab..534cffb2d 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -45,17 +45,17 @@ public class BinaryDictionary extends Dictionary { public static final int MAX_SPACES = 16; private static final String TAG = "BinaryDictionary"; - private static final int MAX_BIGRAMS = 60; + private static final int MAX_PREDICTIONS = 60; + private static final int MAX_RESULTS = Math.max(MAX_PREDICTIONS, MAX_WORDS); private static final int TYPED_LETTER_MULTIPLIER = 2; private long mNativeDict; private final int[] mInputCodes = new int[MAX_WORD_LENGTH]; - private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS]; - private final char[] mOutputChars_bigrams = new char[MAX_WORD_LENGTH * MAX_BIGRAMS]; + private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_RESULTS]; private final int[] mSpaceIndices = new int[MAX_SPACES]; - private final int[] mScores = new int[MAX_WORDS]; - private final int[] mBigramScores = new int[MAX_BIGRAMS]; + private final int[] mOutputScores = new int[MAX_RESULTS]; + private final int[] mOutputTypes = new int[MAX_RESULTS]; private final boolean mUseFullEditDistance; @@ -83,7 +83,8 @@ public class BinaryDictionary extends Dictionary { } private native long openNative(String sourceDir, long dictOffset, long dictSize, - int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords); + int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords, + int maxPredictions); private native void closeNative(long dict); private native int getFrequencyNative(long dict, int[] word, int wordLength); private native boolean isValidBigramNative(long dict, int[] word1, int[] word2); @@ -91,78 +92,59 @@ public class BinaryDictionary extends Dictionary { int[] yCoordinates, int[] times, int[] pointerIds, int[] inputCodes, int codesSize, int commitPoint, boolean isGesture, int[] prevWordCodePointArray, boolean useFullEditDistance, char[] outputChars, - int[] scores, int[] outputIndices); - private native int getBigramsNative(long dict, int[] prevWord, int prevWordLength, - int[] inputCodes, int inputCodesLength, char[] outputChars, int[] scores, - int maxWordLength, int maxBigrams); + int[] outputScores, int[] outputIndices, int[] outputTypes); private static native float calcNormalizedScoreNative( char[] before, int beforeLength, char[] after, int afterLength, int score); private static native int editDistanceNative( char[] before, int beforeLength, char[] after, int afterLength); private final void loadDictionary(String path, long startOffset, long length) { - mNativeDict = openNative(path, startOffset, length, - TYPED_LETTER_MULTIPLIER, FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS); + mNativeDict = openNative(path, startOffset, length, TYPED_LETTER_MULTIPLIER, + FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS, MAX_PREDICTIONS); } @Override - public ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes, - final CharSequence previousWord) { - if (mNativeDict == 0) return null; - - int[] codePoints = StringUtils.toCodePointArray(previousWord.toString()); - Arrays.fill(mOutputChars_bigrams, (char) 0); - Arrays.fill(mBigramScores, 0); - - int codesSize = codes.size(); - Arrays.fill(mInputCodes, -1); - if (codesSize > 0) { - mInputCodes[0] = codes.getCodeAt(0); - } - - int count = getBigramsNative(mNativeDict, codePoints, codePoints.length, mInputCodes, - codesSize, mOutputChars_bigrams, mBigramScores, MAX_WORD_LENGTH, MAX_BIGRAMS); - if (count > MAX_BIGRAMS) { - count = MAX_BIGRAMS; - } - - final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<SuggestedWordInfo>(); - for (int j = 0; j < count; ++j) { - if (codesSize > 0 && mBigramScores[j] < 1) break; - final int start = j * MAX_WORD_LENGTH; - int len = 0; - while (len < MAX_WORD_LENGTH && mOutputChars_bigrams[start + len] != 0) { - ++len; - } - if (len > 0) { - suggestions.add(new SuggestedWordInfo( - new String(mOutputChars_bigrams, start, len), - mBigramScores[j], SuggestedWordInfo.KIND_CORRECTION, mDictType)); + public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { + if (!isValidDictionary()) return null; + Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE); + Arrays.fill(mOutputChars, (char) 0); + Arrays.fill(mOutputScores, 0); + // TODO: toLowerCase in the native code + final int[] prevWordCodePointArray = (null == prevWord) + ? null : StringUtils.toCodePointArray(prevWord.toString()); + final int composerSize = composer.size(); + + final boolean isGesture = composer.isBatchMode(); + if (composerSize <= 1 || !isGesture) { + if (composerSize > MAX_WORD_LENGTH - 1) return null; + for (int i = 0; i < composerSize; i++) { + mInputCodes[i] = composer.getCodeAt(i); } } - return suggestions; - } - // proximityInfo and/or prevWordForBigrams may not be null. - @Override - public ArrayList<SuggestedWordInfo> getWords(final WordComposer codes, - final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { - final int count = getSuggestions(codes, prevWordForBigrams, proximityInfo, mOutputChars, - mScores, mSpaceIndices); + final InputPointers ips = composer.getInputPointers(); + final int codesSize = isGesture ? ips.getPointerSize() : composerSize; + // proximityInfo and/or prevWordForBigrams may not be null. + final int tmpCount = getSuggestionsNative(mNativeDict, + proximityInfo.getNativeProximityInfo(), ips.getXCoordinates(), + ips.getYCoordinates(), ips.getTimes(), ips.getPointerIds(), + mInputCodes, codesSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray, + mUseFullEditDistance, mOutputChars, mOutputScores, mSpaceIndices, mOutputTypes); + final int count = Math.min(tmpCount, MAX_PREDICTIONS); final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<SuggestedWordInfo>(); for (int j = 0; j < count; ++j) { - if (mScores[j] < 1) break; + if (composerSize > 0 && mOutputScores[j] < 1) break; final int start = j * MAX_WORD_LENGTH; int len = 0; - while (len < MAX_WORD_LENGTH && mOutputChars[start + len] != 0) { + while (len < MAX_WORD_LENGTH && mOutputChars[start + len] != 0) { ++len; } if (len > 0) { - // TODO: actually get the kind from native code suggestions.add(new SuggestedWordInfo( new String(mOutputChars, start, len), - mScores[j], SuggestedWordInfo.KIND_CORRECTION, mDictType)); + mOutputScores[j], SuggestedWordInfo.KIND_CORRECTION, mDictType)); } } return suggestions; @@ -172,41 +154,6 @@ public class BinaryDictionary extends Dictionary { return mNativeDict != 0; } - // proximityInfo may not be null. - /* package for test */ int getSuggestions(final WordComposer codes, - final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo, - char[] outputChars, int[] scores, int[] spaceIndices) { - if (!isValidDictionary()) return -1; - - final int codesSize = codes.size(); - // Won't deal with really long words. - if (codesSize > MAX_WORD_LENGTH - 1) return -1; - - Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE); - for (int i = 0; i < codesSize; i++) { - mInputCodes[i] = codes.getCodeAt(i); - } - Arrays.fill(outputChars, (char) 0); - Arrays.fill(scores, 0); - - // TODO: toLowerCase in the native code - final int[] prevWordCodePointArray = (null == prevWordForBigrams) - ? null : StringUtils.toCodePointArray(prevWordForBigrams.toString()); - - int[] emptyArray = new int[codesSize]; - Arrays.fill(emptyArray, 0); - - //final int commitPoint = codes.getCommitPoint(); - //codes.clearCommitPoint(); - - final InputPointers ips = codes.getInputPointers(); - - return getSuggestionsNative(mNativeDict, proximityInfo.getNativeProximityInfo(), - ips.getXCoordinates(), ips.getYCoordinates(), ips.getTimes(), ips.getPointerIds(), - mInputCodes, codesSize, 0 /* unused */, false, prevWordCodePointArray, - mUseFullEditDistance, outputChars, scores, spaceIndices); - } - public static float calcNormalizedScore(String before, String after, int score) { return calcNormalizedScoreNative(before.toCharArray(), before.length(), after.toCharArray(), after.length(), score); diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index 37eced5d6..236c198ad 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -149,7 +149,8 @@ public class BinaryDictionaryFileDumper { final int MODE_MAX = NONE; final Uri.Builder wordListUriBuilder = getProviderUriBuilder(id); - final String outputFileName = BinaryDictionaryGetter.getCacheFileName(id, locale, context); + final String finalFileName = BinaryDictionaryGetter.getCacheFileName(id, locale, context); + final String tempFileName = finalFileName + ".tmp"; for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) { InputStream originalSourceStream = null; @@ -165,7 +166,10 @@ public class BinaryDictionaryFileDumper { if (null == afd) return null; originalSourceStream = afd.createInputStream(); // Open output. - outputFile = new File(outputFileName); + outputFile = new File(tempFileName); + // Just to be sure, delete the file. This may fail silently, and return false: this + // is the right thing to do, as we just want to continue anyway. + outputFile.delete(); outputStream = new FileOutputStream(outputFile); // Get the appropriate decryption method for this try switch (mode) { @@ -194,14 +198,20 @@ public class BinaryDictionaryFileDumper { break; } checkMagicAndCopyFileTo(new BufferedInputStream(inputStream), outputStream); + outputStream.flush(); + outputStream.close(); + final File finalFile = new File(finalFileName); + if (!outputFile.renameTo(finalFile)) { + throw new IOException("Can't move the file to its final name"); + } wordListUriBuilder.appendQueryParameter(QUERY_PARAMETER_DELETE_RESULT, QUERY_PARAMETER_SUCCESS); if (0 >= resolver.delete(wordListUriBuilder.build(), null, null)) { Log.e(TAG, "Could not have the dictionary pack delete a word list"); } - BinaryDictionaryGetter.removeFilesWithIdExcept(context, id, outputFile); + BinaryDictionaryGetter.removeFilesWithIdExcept(context, id, finalFile); // Success! Close files (through the finally{} clause) and return. - return AssetFileAddress.makeFromFileName(outputFileName); + return AssetFileAddress.makeFromFileName(finalFileName); } catch (Exception e) { if (DEBUG) { Log.i(TAG, "Can't open word list in mode " + mode + " : " + e); diff --git a/java/src/com/android/inputmethod/latin/Dictionary.java b/java/src/com/android/inputmethod/latin/Dictionary.java index 0835450c1..fd40aa6da 100644 --- a/java/src/com/android/inputmethod/latin/Dictionary.java +++ b/java/src/com/android/inputmethod/latin/Dictionary.java @@ -16,6 +16,8 @@ package com.android.inputmethod.latin; +import android.text.TextUtils; + import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; @@ -50,24 +52,17 @@ public abstract class Dictionary { } /** - * Searches for words in the dictionary that match the characters in the composer. Matched - * words are returned as an ArrayList. + * Searches for suggestions for a given context. For the moment the context is only the + * previous word. * @param composer the key sequence to match with coordinate info, as a WordComposer - * @param prevWordForBigrams the previous word, or null if none + * @param prevWord the previous word, or null if none * @param proximityInfo the object for key proximity. May be ignored by some implementations. - * @return the list of suggestions - */ - abstract public ArrayList<SuggestedWordInfo> getWords(final WordComposer composer, - final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo); - - /** - * Searches for pairs in the bigram dictionary that matches the previous word. - * @param composer the key sequence to match - * @param previousWord the word before - * @return the list of suggestions + * @return the list of suggestions (possibly null if none) */ - public abstract ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer, - final CharSequence previousWord); + // TODO: pass more context than just the previous word, to enable better suggestions (n-gram + // and more) + abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo); /** * Checks if the given word occurs in the dictionary diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java index dcc53c59f..88ac07d7a 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java +++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java @@ -55,38 +55,19 @@ public class DictionaryCollection extends Dictionary { } @Override - public ArrayList<SuggestedWordInfo> getWords(final WordComposer composer, - final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { + public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries; if (dictionaries.isEmpty()) return null; // To avoid creating unnecessary objects, we get the list out of the first // dictionary and add the rest to it if not null, hence the get(0) - ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getWords(composer, - prevWordForBigrams, proximityInfo); + ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composer, + prevWord, proximityInfo); if (null == suggestions) suggestions = new ArrayList<SuggestedWordInfo>(); final int length = dictionaries.size(); for (int i = 0; i < length; ++ i) { - final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getWords(composer, - prevWordForBigrams, proximityInfo); - if (null != sugg) suggestions.addAll(sugg); - } - return suggestions; - } - - @Override - public ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer, - final CharSequence previousWord) { - final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries; - if (dictionaries.isEmpty()) return null; - // To avoid creating unnecessary objects, we get the list out of the first - // dictionary and add the rest to it if not null, hence the get(0) - ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getBigrams(composer, - previousWord); - if (null == suggestions) suggestions = new ArrayList<SuggestedWordInfo>(); - final int length = dictionaries.size(); - for (int i = 0; i < length; ++ i) { - final ArrayList<SuggestedWordInfo> sugg = - dictionaries.get(i).getBigrams(composer, previousWord); + final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer, + prevWord, proximityInfo); if (null != sugg) suggestions.addAll(sugg); } return suggestions; diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java index 1cda9f257..016530abb 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java @@ -192,41 +192,13 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { } @Override - public ArrayList<SuggestedWordInfo> getWords(final WordComposer codes, - final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { + public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { asyncReloadDictionaryIfRequired(); - return getWordsInner(codes, prevWordForBigrams, proximityInfo); - } - - protected final ArrayList<SuggestedWordInfo> getWordsInner(final WordComposer codes, - final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { - // Ensure that there are no concurrent calls to getWords. If there are, do nothing and - // return. - if (mLocalDictionaryController.tryLock()) { - try { - if (mBinaryDictionary != null) { - return mBinaryDictionary.getWords(codes, prevWordForBigrams, proximityInfo); - } - } finally { - mLocalDictionaryController.unlock(); - } - } - return null; - } - - @Override - public ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes, - final CharSequence previousWord) { - asyncReloadDictionaryIfRequired(); - return getBigramsInner(codes, previousWord); - } - - protected ArrayList<SuggestedWordInfo> getBigramsInner(final WordComposer codes, - final CharSequence previousWord) { if (mLocalDictionaryController.tryLock()) { try { if (mBinaryDictionary != null) { - return mBinaryDictionary.getBigrams(codes, previousWord); + return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo); } } finally { mLocalDictionaryController.unlock(); diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java index 76213c0da..5d7995dc2 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java @@ -17,6 +17,7 @@ package com.android.inputmethod.latin; import android.content.Context; +import android.text.TextUtils; import com.android.inputmethod.keyboard.KeyDetector; import com.android.inputmethod.keyboard.Keyboard; @@ -247,23 +248,36 @@ public class ExpandableDictionary extends Dictionary { } @Override - public ArrayList<SuggestedWordInfo> getWords(final WordComposer codes, - final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { + public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { + if (reloadDictionaryIfRequired()) return null; + if (composer.size() <= 1) { + if (composer.size() >= BinaryDictionary.MAX_WORD_LENGTH) { + return null; + } + final ArrayList<SuggestedWordInfo> suggestions = + getWordsInner(composer, prevWord, proximityInfo); + return suggestions; + } else { + if (TextUtils.isEmpty(prevWord)) return null; + final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<SuggestedWordInfo>(); + runBigramReverseLookUp(prevWord, suggestions); + return suggestions; + } + } + + // This reloads the dictionary if required, and returns whether it's currently updating its + // contents or not. + // @VisibleForTesting + boolean reloadDictionaryIfRequired() { synchronized (mUpdatingLock) { // If we need to update, start off a background task if (mRequiresReload) startDictionaryLoadingTaskLocked(); - // Currently updating contacts, don't return any results. - if (mUpdatingDictionary) return null; - } - if (codes.size() >= BinaryDictionary.MAX_WORD_LENGTH) { - return null; + return mUpdatingDictionary; } - final ArrayList<SuggestedWordInfo> suggestions = - getWordsInner(codes, prevWordForBigrams, proximityInfo); - return suggestions; } - protected final ArrayList<SuggestedWordInfo> getWordsInner(final WordComposer codes, + protected ArrayList<SuggestedWordInfo> getWordsInner(final WordComposer codes, final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<SuggestedWordInfo>(); mInputLength = codes.size(); @@ -589,16 +603,6 @@ public class ExpandableDictionary extends Dictionary { return searchWord(childNode.mChildren, word, depth + 1, childNode); } - // @VisibleForTesting - boolean reloadDictionaryIfRequired() { - synchronized (mUpdatingLock) { - // If we need to update, start off a background task - if (mRequiresReload) startDictionaryLoadingTaskLocked(); - // Currently updating contacts, don't return any results. - return mUpdatingDictionary; - } - } - private void runBigramReverseLookUp(final CharSequence previousWord, final ArrayList<SuggestedWordInfo> suggestions) { // Search for the lowercase version of the word only, because that's where bigrams @@ -610,17 +614,6 @@ public class ExpandableDictionary extends Dictionary { } } - @Override - public ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes, - final CharSequence previousWord) { - if (!reloadDictionaryIfRequired()) { - final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<SuggestedWordInfo>(); - runBigramReverseLookUp(previousWord, suggestions); - return suggestions; - } - return null; - } - /** * Used for testing purposes and in the spell checker * This function will wait for loading from database to be done diff --git a/java/src/com/android/inputmethod/latin/InputPointers.java b/java/src/com/android/inputmethod/latin/InputPointers.java index 9d77d4e96..5ad53480f 100644 --- a/java/src/com/android/inputmethod/latin/InputPointers.java +++ b/java/src/com/android/inputmethod/latin/InputPointers.java @@ -18,7 +18,7 @@ package com.android.inputmethod.latin; import java.util.Arrays; -// TODO: Add unit test +// TODO: This class is not thread-safe. public class InputPointers { private final ScalableIntArray mXCoordinates = new ScalableIntArray(); private final ScalableIntArray mYCoordinates = new ScalableIntArray(); @@ -55,21 +55,15 @@ public class InputPointers { /** * Append the pointers in the specified {@link InputPointers} to the end of this. - * @param src the source {@link InputPointers} to append the pointers. + * @param src the source {@link InputPointers} to read the data from. * @param startPos the starting index of the pointers in {@code src}. * @param length the number of pointers to be appended. */ public void append(InputPointers src, int startPos, int length) { - final int currentLength = getPointerSize(); - final int newLength = currentLength + length; - mXCoordinates.ensureCapacity(newLength); - mYCoordinates.ensureCapacity(newLength); - mPointerIds.ensureCapacity(newLength); - mTimes.ensureCapacity(newLength); - System.arraycopy(src.getXCoordinates(), startPos, getXCoordinates(), currentLength, length); - System.arraycopy(src.getYCoordinates(), startPos, getYCoordinates(), currentLength, length); - System.arraycopy(src.getPointerIds(), startPos, getPointerIds(), currentLength, length); - System.arraycopy(src.getTimes(), startPos, getTimes(), currentLength, length); + mXCoordinates.append(src.mXCoordinates, startPos, length); + mYCoordinates.append(src.mYCoordinates, startPos, length); + mPointerIds.append(src.mPointerIds, startPos, length); + mTimes.append(src.mTimes, startPos, length); } public void reset() { @@ -118,24 +112,23 @@ public class InputPointers { } public void add(int val) { - ensureCapacity(mLength); + final int nextLength = mLength + 1; + ensureCapacity(nextLength); mArray[mLength] = val; - ++mLength; + mLength = nextLength; } - public void ensureCapacity(int minimumCapacity) { + private void ensureCapacity(int minimumCapacity) { if (mArray.length < minimumCapacity) { final int nextCapacity = mArray.length * 2; - grow(minimumCapacity > nextCapacity ? minimumCapacity : nextCapacity); + // The following is the same as newLength = Math.max(minimumCapacity, nextCapacity); + final int newLength = minimumCapacity > nextCapacity + ? minimumCapacity + : nextCapacity; + mArray = Arrays.copyOf(mArray, newLength); } } - private void grow(int newCapacity) { - final int[] newArray = new int[newCapacity]; - System.arraycopy(mArray, 0, newArray, 0, mLength); - mArray = newArray; - } - public int getLength() { return mLength; } @@ -149,14 +142,23 @@ public class InputPointers { return mArray; } - public void copy(ScalableIntArray ip) { - mArray = Arrays.copyOf(ip.mArray, ip.mArray.length); + public void set(ScalableIntArray ip) { + mArray = ip.mArray; mLength = ip.mLength; } - public void set(ScalableIntArray ip) { - mArray = ip.mArray; + public void copy(ScalableIntArray ip) { + ensureCapacity(ip.mLength); + System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength); mLength = ip.mLength; } + + public void append(ScalableIntArray src, int startPos, int length) { + final int currentLength = mLength; + final int newLength = currentLength + length; + ensureCapacity(newLength); + System.arraycopy(src.mArray, startPos, mArray, currentLength, length); + mLength = newLength; + } } } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 4be2a1799..f27d32150 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -353,7 +353,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mPrefs = prefs; LatinImeLogger.init(this, prefs); if (ProductionFlag.IS_EXPERIMENTAL) { - ResearchLogger.getInstance().init(this, prefs); + ResearchLogger.getInstance().init(this, prefs, mKeyboardSwitcher); } InputMethodManagerCompatWrapper.init(this); SubtypeSwitcher.init(this); @@ -689,6 +689,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); } + // Callback for the TargetApplicationGetter @Override public void onTargetApplicationKnown(final ApplicationInfo info) { mTargetApplicationInfo = info; @@ -997,7 +998,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; } - public void commitTyped(final int separatorCode) { + private void commitTyped(final int separatorCode) { if (!mWordComposer.isComposingWord()) return; final CharSequence typedWord = mWordComposer.getTypedWord(); if (typedWord.length() > 0) { @@ -1013,6 +1014,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen updateSuggestionsOrPredictions(); } + // Called from the KeyboardSwitcher which needs to know auto caps state to display + // the right layout. public int getCurrentAutoCapsState() { if (!mCurrentSettings.mAutoCap) return Constants.TextUtils.CAP_MODE_OFF; @@ -1088,6 +1091,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen || codePoint == Keyboard.CODE_CLOSING_ANGLE_BRACKET; } + // Callback for the SuggestionsView, to call when the "add to dictionary" hint is pressed. @Override public boolean addWordToUserDictionary(String word) { mUserDictionary.addWordToUserDictionary(word, 128); @@ -1285,6 +1289,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mConnection.endBatchEdit(); } + // Called from PointerTracker through the KeyboardActionListener interface @Override public void onTextInput(CharSequence text) { mConnection.beginBatchEdit(); @@ -1348,6 +1353,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } + // Called from PointerTracker through the KeyboardActionListener interface @Override public void onCancelInput() { // User released a finger outside any key @@ -1505,7 +1511,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // it entirely and resume suggestions on the previous word, we'd like to still // have touch coordinates for it. resetComposingState(false /* alsoResetLastComposedWord */); - clearSuggestions(); } if (isComposingWord) { final int keyX, keyY; @@ -1621,12 +1626,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen inputView.closing(); } - public boolean isShowingPunctuationList() { + // TODO: make this private + // Outside LatinIME, only used by the test suite. + /* package for tests */ boolean isShowingPunctuationList() { if (mSuggestionsView == null) return false; return mCurrentSettings.mSuggestPuncList == mSuggestionsView.getSuggestions(); } - public boolean isSuggestionsStripVisible() { + private boolean isSuggestionsStripVisible() { if (mSuggestionsView == null) return false; if (mSuggestionsView.isShowingAddToDictionaryHint()) @@ -1638,7 +1645,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return mCurrentSettings.isSuggestionsRequested(mDisplayOrientation); } - public void clearSuggestions() { + private void clearSuggestions() { setSuggestions(SuggestedWords.EMPTY, false); setAutoCorrectionIndicator(false); } @@ -1661,7 +1668,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } - public void updateSuggestionsOrPredictions() { + // TODO: rename this method to updateSuggestionStrip or simply updateSuggestions + private void updateSuggestionsOrPredictions() { mHandler.cancelUpdateSuggestionStrip(); // Check if we have a suggestion engine attached. @@ -1675,42 +1683,36 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } final String typedWord = mWordComposer.getTypedWord(); - final SuggestedWords suggestions; if (!mWordComposer.isComposingWord() && !mCurrentSettings.mBigramPredictionEnabled) { setPunctuationSuggestions(); return; } - if (!mWordComposer.isComposingWord()) { - suggestions = updateBigramPredictions(); - } else { - suggestions = updateSuggestions(typedWord); - } - - if (null != suggestions && suggestions.size() > 0) { - showSuggestions(suggestions, typedWord); - } else { - clearSuggestions(); - } - } - - private SuggestedWords updateSuggestions(final CharSequence typedWord) { - // TODO: May need a better way of retrieving previous word + // Get the word on which we should search the bigrams. If we are composing a word, it's + // whatever is *before* the half-committed word in the buffer, hence 2; if we aren't, we + // should just skip whitespace if any, so 1. + // TODO: this is slow (2-way IPC) - we should probably cache this instead. final CharSequence prevWord = - mConnection.getNthPreviousWord(mCurrentSettings.mWordSeparators, 2); - // getSuggestedWords handles gracefully a null value of prevWord - final SuggestedWords suggestedWords = mSuggest.getSuggestedWords(mWordComposer, + mConnection.getNthPreviousWord(mCurrentSettings.mWordSeparators, + mWordComposer.isComposingWord() ? 2 : 1); + SuggestedWords suggestedWords = mSuggest.getSuggestedWords(mWordComposer, prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(), - // !mWordComposer.isComposingWord() is known to be false - mCurrentSettings.mCorrectionEnabled, !mWordComposer.isComposingWord()); - - // Basically, we update the suggestion strip only when suggestion count > 1. However, - // there is an exception: We update the suggestion strip whenever typed word's length - // is 1 or typed word is found in dictionary, regardless of suggestion count. Actually, - // in most cases, suggestion count is 1 when typed word's length is 1, but we do always - // need to clear the previous state when the user starts typing a word (i.e. typed word's - // length == 1). - if (suggestedWords.size() > 1 || typedWord.length() == 1 + mCurrentSettings.mCorrectionEnabled); + suggestedWords = maybeRetrieveOlderSuggestions(typedWord, suggestedWords); + + showSuggestions(suggestedWords, typedWord); + } + + private SuggestedWords maybeRetrieveOlderSuggestions(final CharSequence typedWord, + final SuggestedWords suggestedWords) { + // TODO: consolidate this into getSuggestedWords + // We update the suggestion strip only when we have some suggestions to show, i.e. when + // the suggestion count is > 1; else, we leave the old suggestions, with the typed word + // replaced with the new one. However, when the word is a dictionary word, or when the + // length of the typed word is 1 or 0 (after a deletion typically), we do want to remove the + // old suggestions. Also, if we are showing the "add to dictionary" hint, we need to + // revert to suggestions - although it is unclear how we can come here if it's displayed. + if (suggestedWords.size() > 1 || typedWord.length() <= 1 || !suggestedWords.mTypedWordValid || mSuggestionsView.isShowingAddToDictionaryHint()) { return suggestedWords; @@ -1733,7 +1735,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private void showSuggestions(final SuggestedWords suggestedWords, final CharSequence typedWord) { - // This method is only ever called by updateSuggestions or updateBigramPredictions. + if (null == suggestedWords || suggestedWords.size() <= 0) { + clearSuggestions(); + return; + } final CharSequence autoCorrection; if (suggestedWords.size() > 0) { if (suggestedWords.mWillAutoCorrect) { @@ -1781,6 +1786,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } + // Called from SuggestionsView through the SuggestionsView.Listener interface @Override public void pickSuggestionManually(final int index, final CharSequence suggestion, final int x, final int y) { @@ -1891,16 +1897,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen separatorCode, prevWord); } - private SuggestedWords updateBigramPredictions() { - final CharSequence prevWord = - mConnection.getNthPreviousWord(mCurrentSettings.mWordSeparators, 1); - return mSuggest.getSuggestedWords(mWordComposer, - prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(), - // !mWordComposer.isComposingWord() is known to be true - mCurrentSettings.mCorrectionEnabled, !mWordComposer.isComposingWord()); - } - - public void setPunctuationSuggestions() { + private void setPunctuationSuggestions() { if (mCurrentSettings.mBigramPredictionEnabled) { clearSuggestions(); } else { @@ -2008,16 +2005,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mHandler.postUpdateSuggestionStrip(); } + // Used by the RingCharBuffer public boolean isWordSeparator(int code) { return mCurrentSettings.isWordSeparator(code); } - public boolean preferCapitalization() { - return mWordComposer.isFirstCharCapitalized(); - } - // Notify that language or mode have been changed and toggleLanguage will update KeyboardID - // according to new language or mode. + // according to new language or mode. Called from SubtypeSwitcher. public void onRefreshKeyboard() { // When the device locale is changed in SetupWizard etc., this method may get called via // onConfigurationChanged before SoftInputWindow is shown. @@ -2034,16 +2028,20 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } // TODO: Remove this method from {@link LatinIME} and move {@link FeedbackManager} to - // {@link KeyboardSwitcher}. + // {@link KeyboardSwitcher}. Called from KeyboardSwitcher public void hapticAndAudioFeedback(final int primaryCode) { mFeedbackManager.hapticAndAudioFeedback(primaryCode, mKeyboardSwitcher.getKeyboardView()); } + // Callback called by PointerTracker through the KeyboardActionListener. This is called when a + // key is depressed; release matching call is onReleaseKey below. @Override public void onPressKey(int primaryCode) { mKeyboardSwitcher.onPressKey(primaryCode); } + // Callback by PointerTracker through the KeyboardActionListener. This is called when a key + // is released; press matching call is onPressKey above. @Override public void onReleaseKey(int primaryCode, boolean withSliding) { mKeyboardSwitcher.onReleaseKey(primaryCode, withSliding); @@ -2088,6 +2086,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen launchSettingsClass(SettingsActivity.class); } + // Called from debug code only public void launchDebugSettings() { launchSettingsClass(DebugSettingsActivity.class); } diff --git a/java/src/com/android/inputmethod/latin/ResearchLog.java b/java/src/com/android/inputmethod/latin/ResearchLog.java new file mode 100644 index 000000000..1de5cb36a --- /dev/null +++ b/java/src/com/android/inputmethod/latin/ResearchLog.java @@ -0,0 +1,360 @@ +/* + * Copyright (C) 2012 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 com.android.inputmethod.latin; + +import android.content.SharedPreferences; +import android.os.SystemClock; +import android.util.JsonWriter; +import android.util.Log; +import android.view.inputmethod.CompletionInfo; + +import com.android.inputmethod.keyboard.Key; +import com.android.inputmethod.latin.ResearchLogger.LogUnit; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; +import com.android.inputmethod.latin.define.ProductionFlag; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +/** + * Logs the use of the LatinIME keyboard. + * + * This class logs operations on the IME keyboard, including what the user has typed. + * Data is stored locally in a file in app-specific storage. + * + * This functionality is off by default. See {@link ProductionFlag#IS_EXPERIMENTAL}. + */ +public class ResearchLog { + private static final String TAG = ResearchLog.class.getSimpleName(); + private static final JsonWriter NULL_JSON_WRITER = new JsonWriter( + new OutputStreamWriter(new NullOutputStream())); + + final ScheduledExecutorService mExecutor; + /* package */ final File mFile; + private JsonWriter mJsonWriter = NULL_JSON_WRITER; // should never be null + + private int mLoggingState; + private static final int LOGGING_STATE_UNSTARTED = 0; + private static final int LOGGING_STATE_RUNNING = 1; + private static final int LOGGING_STATE_STOPPING = 2; + private static final int LOGGING_STATE_STOPPED = 3; + private static final long FLUSH_DELAY_IN_MS = 1000 * 5; + + private static class NullOutputStream extends OutputStream { + /** {@inheritDoc} */ + @Override + public void write(byte[] buffer, int offset, int count) { + // nop + } + + /** {@inheritDoc} */ + @Override + public void write(byte[] buffer) { + // nop + } + + @Override + public void write(int oneByte) { + } + } + + public ResearchLog(File outputFile) { + mExecutor = Executors.newSingleThreadScheduledExecutor(); + if (outputFile == null) { + throw new IllegalArgumentException(); + } + mFile = outputFile; + mLoggingState = LOGGING_STATE_UNSTARTED; + } + + public synchronized void start() throws IOException { + switch (mLoggingState) { + case LOGGING_STATE_UNSTARTED: + mJsonWriter = new JsonWriter(new BufferedWriter(new FileWriter(mFile))); + mJsonWriter.setLenient(true); + mJsonWriter.beginArray(); + mLoggingState = LOGGING_STATE_RUNNING; + break; + case LOGGING_STATE_RUNNING: + case LOGGING_STATE_STOPPING: + case LOGGING_STATE_STOPPED: + break; + } + } + + public synchronized void stop() { + switch (mLoggingState) { + case LOGGING_STATE_UNSTARTED: + mLoggingState = LOGGING_STATE_STOPPED; + break; + case LOGGING_STATE_RUNNING: + mExecutor.submit(new Callable<Object>() { + @Override + public Object call() throws Exception { + try { + mJsonWriter.endArray(); + mJsonWriter.flush(); + mJsonWriter.close(); + } finally { + // the contentprovider only exports data if the writable + // bit is cleared. + boolean success = mFile.setWritable(false, false); + mLoggingState = LOGGING_STATE_STOPPED; + } + return null; + } + }); + mExecutor.shutdown(); + mLoggingState = LOGGING_STATE_STOPPING; + break; + case LOGGING_STATE_STOPPING: + case LOGGING_STATE_STOPPED: + } + } + + public boolean isAlive() { + switch (mLoggingState) { + case LOGGING_STATE_UNSTARTED: + case LOGGING_STATE_RUNNING: + return true; + } + return false; + } + + public void waitUntilStopped(int timeoutInMs) throws InterruptedException { + mExecutor.awaitTermination(timeoutInMs, TimeUnit.MILLISECONDS); + } + + private boolean isAbortSuccessful; + public boolean isAbortSuccessful() { + return isAbortSuccessful; + } + + public synchronized void abort() { + switch (mLoggingState) { + case LOGGING_STATE_UNSTARTED: + mLoggingState = LOGGING_STATE_STOPPED; + isAbortSuccessful = true; + break; + case LOGGING_STATE_RUNNING: + mExecutor.submit(new Callable<Object>() { + @Override + public Object call() throws Exception { + try { + mJsonWriter.endArray(); + mJsonWriter.close(); + } finally { + isAbortSuccessful = mFile.delete(); + } + return null; + } + }); + mExecutor.shutdown(); + mLoggingState = LOGGING_STATE_STOPPING; + break; + case LOGGING_STATE_STOPPING: + case LOGGING_STATE_STOPPED: + } + } + + /* package */ synchronized void flush() { + switch (mLoggingState) { + case LOGGING_STATE_UNSTARTED: + break; + case LOGGING_STATE_RUNNING: + removeAnyScheduledFlush(); + mExecutor.submit(mFlushCallable); + break; + case LOGGING_STATE_STOPPING: + case LOGGING_STATE_STOPPED: + } + } + + private Callable<Object> mFlushCallable = new Callable<Object>() { + @Override + public Object call() throws Exception { + mJsonWriter.flush(); + return null; + } + }; + + private ScheduledFuture<Object> mFlushFuture; + + private void removeAnyScheduledFlush() { + if (mFlushFuture != null) { + mFlushFuture.cancel(false); + mFlushFuture = null; + } + } + + private void scheduleFlush() { + removeAnyScheduledFlush(); + mFlushFuture = mExecutor.schedule(mFlushCallable, FLUSH_DELAY_IN_MS, TimeUnit.MILLISECONDS); + } + + public synchronized void publishPublicEvents(final LogUnit logUnit) { + switch (mLoggingState) { + case LOGGING_STATE_UNSTARTED: + break; + case LOGGING_STATE_RUNNING: + mExecutor.submit(new Callable<Object>() { + @Override + public Object call() throws Exception { + logUnit.publishPublicEventsTo(ResearchLog.this); + scheduleFlush(); + return null; + } + }); + break; + case LOGGING_STATE_STOPPING: + case LOGGING_STATE_STOPPED: + } + } + + public synchronized void publishAllEvents(final LogUnit logUnit) { + switch (mLoggingState) { + case LOGGING_STATE_UNSTARTED: + break; + case LOGGING_STATE_RUNNING: + mExecutor.submit(new Callable<Object>() { + @Override + public Object call() throws Exception { + logUnit.publishAllEventsTo(ResearchLog.this); + scheduleFlush(); + return null; + } + }); + break; + case LOGGING_STATE_STOPPING: + case LOGGING_STATE_STOPPED: + } + } + + private static final String CURRENT_TIME_KEY = "_ct"; + private static final String UPTIME_KEY = "_ut"; + private static final String EVENT_TYPE_KEY = "_ty"; + void outputEvent(final String[] keys, final Object[] values) { + // not thread safe. + try { + mJsonWriter.beginObject(); + mJsonWriter.name(CURRENT_TIME_KEY).value(System.currentTimeMillis()); + mJsonWriter.name(UPTIME_KEY).value(SystemClock.uptimeMillis()); + mJsonWriter.name(EVENT_TYPE_KEY).value(keys[0]); + final int length = values.length; + for (int i = 0; i < length; i++) { + mJsonWriter.name(keys[i + 1]); + Object value = values[i]; + if (value instanceof String) { + mJsonWriter.value((String) value); + } else if (value instanceof Number) { + mJsonWriter.value((Number) value); + } else if (value instanceof Boolean) { + mJsonWriter.value((Boolean) value); + } else if (value instanceof CompletionInfo[]) { + CompletionInfo[] ci = (CompletionInfo[]) value; + mJsonWriter.beginArray(); + for (int j = 0; j < ci.length; j++) { + mJsonWriter.value(ci[j].toString()); + } + mJsonWriter.endArray(); + } else if (value instanceof SharedPreferences) { + SharedPreferences prefs = (SharedPreferences) value; + mJsonWriter.beginObject(); + for (Map.Entry<String,?> entry : prefs.getAll().entrySet()) { + mJsonWriter.name(entry.getKey()); + final Object innerValue = entry.getValue(); + if (innerValue == null) { + mJsonWriter.nullValue(); + } else if (innerValue instanceof Boolean) { + mJsonWriter.value((Boolean) innerValue); + } else if (innerValue instanceof Number) { + mJsonWriter.value((Number) innerValue); + } else { + mJsonWriter.value(innerValue.toString()); + } + } + mJsonWriter.endObject(); + } else if (value instanceof Key[]) { + Key[] keyboardKeys = (Key[]) value; + mJsonWriter.beginArray(); + for (Key keyboardKey : keyboardKeys) { + mJsonWriter.beginObject(); + mJsonWriter.name("code").value(keyboardKey.mCode); + mJsonWriter.name("altCode").value(keyboardKey.mAltCode); + mJsonWriter.name("x").value(keyboardKey.mX); + mJsonWriter.name("y").value(keyboardKey.mY); + mJsonWriter.name("w").value(keyboardKey.mWidth); + mJsonWriter.name("h").value(keyboardKey.mHeight); + mJsonWriter.endObject(); + } + mJsonWriter.endArray(); + } else if (value instanceof SuggestedWords) { + SuggestedWords words = (SuggestedWords) value; + mJsonWriter.beginObject(); + mJsonWriter.name("typedWordValid").value(words.mTypedWordValid); + mJsonWriter.name("willAutoCorrect") + .value(words.mWillAutoCorrect); + mJsonWriter.name("isPunctuationSuggestions") + .value(words.mIsPunctuationSuggestions); + mJsonWriter.name("isObsoleteSuggestions") + .value(words.mIsObsoleteSuggestions); + mJsonWriter.name("isPrediction") + .value(words.mIsPrediction); + mJsonWriter.name("words"); + mJsonWriter.beginArray(); + final int size = words.size(); + for (int j = 0; j < size; j++) { + SuggestedWordInfo wordInfo = words.getWordInfo(j); + mJsonWriter.value(wordInfo.toString()); + } + mJsonWriter.endArray(); + mJsonWriter.endObject(); + } else if (value == null) { + mJsonWriter.nullValue(); + } else { + Log.w(TAG, "Unrecognized type to be logged: " + + (value == null ? "<null>" : value.getClass().getName())); + mJsonWriter.nullValue(); + } + } + mJsonWriter.endObject(); + } catch (IOException e) { + e.printStackTrace(); + Log.w(TAG, "Error in JsonWriter; disabling logging"); + try { + mJsonWriter.close(); + } catch (IllegalStateException e1) { + // assume that this is just the json not being terminated properly. + // ignore + } catch (IOException e1) { + e1.printStackTrace(); + } finally { + mJsonWriter = NULL_JSON_WRITER; + } + } + } +} diff --git a/java/src/com/android/inputmethod/latin/ResearchLogger.java b/java/src/com/android/inputmethod/latin/ResearchLogger.java index 2de0194fd..9055d5d32 100644 --- a/java/src/com/android/inputmethod/latin/ResearchLogger.java +++ b/java/src/com/android/inputmethod/latin/ResearchLogger.java @@ -24,12 +24,7 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.inputmethodservice.InputMethodService; import android.os.Build; -import android.os.Handler; -import android.os.HandlerThread; -import android.os.Process; -import android.os.SystemClock; import android.text.TextUtils; -import android.util.JsonWriter; import android.util.Log; import android.view.MotionEvent; import android.view.inputmethod.CompletionInfo; @@ -40,22 +35,17 @@ import android.widget.Toast; import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardId; +import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.latin.RichInputConnection.Range; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.define.ProductionFlag; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; -import java.util.Map; import java.util.UUID; /** @@ -68,36 +58,35 @@ import java.util.UUID; */ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = ResearchLogger.class.getSimpleName(); - private static final boolean DEBUG = false; private static final boolean OUTPUT_ENTIRE_BUFFER = false; // true may disclose private info /* package */ static boolean sIsLogging = false; private static final int OUTPUT_FORMAT_VERSION = 1; private static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode"; private static final String FILENAME_PREFIX = "researchLog"; private static final String FILENAME_SUFFIX = ".txt"; - private static final JsonWriter NULL_JSON_WRITER = new JsonWriter( - new OutputStreamWriter(new NullOutputStream())); private static final SimpleDateFormat TIMESTAMP_DATEFORMAT = - new SimpleDateFormat("yyyyMMddHHmmss", Locale.US); + new SimpleDateFormat("yyyyMMddHHmmssS", Locale.US); // constants related to specific log points private static final String WHITESPACE_SEPARATORS = " \t\n\r"; private static final int MAX_INPUTVIEW_LENGTH_TO_CAPTURE = 8192; // must be >=1 private static final String PREF_RESEARCH_LOGGER_UUID_STRING = "pref_research_logger_uuid"; + private static final int ABORT_TIMEOUT_IN_MS = 10 * 1000; private static final ResearchLogger sInstance = new ResearchLogger(); - private HandlerThread mHandlerThread; - /* package */ Handler mLoggingHandler; // to write to a different filename, e.g., for testing, set mFile before calling start() - private File mFilesDir; - /* package */ File mFile; - private JsonWriter mJsonWriter = NULL_JSON_WRITER; // should never be null - - private int mLoggingState; - private static final int LOGGING_STATE_OFF = 0; - private static final int LOGGING_STATE_ON = 1; - private static final int LOGGING_STATE_STOPPING = 2; + /* package */ File mFilesDir; + /* package */ String mUUIDString; + /* package */ ResearchLog mMainResearchLog; + // The mIntentionalResearchLog records all events for the session, private or not (excepting + // passwords). It is written to permanent storage only if the user explicitly commands + // the system to do so. + /* package */ ResearchLog mIntentionalResearchLog; + // LogUnits are queued here and released only when the user requests the intentional log. + private final List<LogUnit> mIntentionalResearchLogQueue = new ArrayList<LogUnit>(); + private boolean mIsPasswordView = false; + private boolean mIsLoggingSuspended = false; // digits entered by the user are replaced with this codepoint. /* package for test */ static final int DIGIT_REPLACEMENT_CODEPOINT = @@ -111,34 +100,17 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang // used to check whether words are not unique private Suggest mSuggest; private Dictionary mDictionary; - - private static class NullOutputStream extends OutputStream { - /** {@inheritDoc} */ - @Override - public void write(byte[] buffer, int offset, int count) { - // nop - } - - /** {@inheritDoc} */ - @Override - public void write(byte[] buffer) { - // nop - } - - @Override - public void write(int oneByte) { - } - } + private KeyboardSwitcher mKeyboardSwitcher; private ResearchLogger() { - mLoggingState = LOGGING_STATE_OFF; } public static ResearchLogger getInstance() { return sInstance; } - public void init(final InputMethodService ims, final SharedPreferences prefs) { + public void init(final InputMethodService ims, final SharedPreferences prefs, + KeyboardSwitcher keyboardSwitcher) { assert ims != null; if (ims == null) { Log.w(TAG, "IMS is null; logging is off"); @@ -149,129 +121,100 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } } if (prefs != null) { + mUUIDString = getUUID(prefs); sIsLogging = prefs.getBoolean(PREF_USABILITY_STUDY_MODE, false); prefs.registerOnSharedPreferenceChangeListener(this); } + mKeyboardSwitcher = keyboardSwitcher; + } + + private File createLogFile(File filesDir) { + final StringBuilder sb = new StringBuilder(); + sb.append(FILENAME_PREFIX).append('-'); + sb.append(mUUIDString).append('-'); + sb.append(TIMESTAMP_DATEFORMAT.format(new Date())); + sb.append(FILENAME_SUFFIX); + return new File(filesDir, sb.toString()); } - public synchronized void start() { - Log.d(TAG, "start called"); + public void start() { if (!sIsLogging) { // Log.w(TAG, "not in usability mode; not logging"); return; } if (mFilesDir == null || !mFilesDir.exists()) { Log.w(TAG, "IME storage directory does not exist. Cannot start logging."); - } else { - if (mHandlerThread == null || !mHandlerThread.isAlive()) { - mHandlerThread = new HandlerThread("ResearchLogger logging task", - Process.THREAD_PRIORITY_BACKGROUND); - mHandlerThread.start(); - mLoggingHandler = null; - mLoggingState = LOGGING_STATE_OFF; - } - if (mLoggingHandler == null) { - mLoggingHandler = new Handler(mHandlerThread.getLooper()); - mLoggingState = LOGGING_STATE_OFF; - } - if (mFile == null) { - final String timestampString = TIMESTAMP_DATEFORMAT.format(new Date()); - mFile = new File(mFilesDir, FILENAME_PREFIX + timestampString + FILENAME_SUFFIX); - } - if (mLoggingState == LOGGING_STATE_OFF) { - try { - mJsonWriter = new JsonWriter(new BufferedWriter(new FileWriter(mFile))); - mJsonWriter.setLenient(true); - mJsonWriter.beginArray(); - mLoggingState = LOGGING_STATE_ON; - } catch (IOException e) { - Log.w(TAG, "cannot start JsonWriter"); - mJsonWriter = NULL_JSON_WRITER; - e.printStackTrace(); - } + return; + } + if (mMainResearchLog == null || !mMainResearchLog.isAlive()) { + mMainResearchLog = new ResearchLog(createLogFile(mFilesDir)); + } + try { + mMainResearchLog.start(); + if (mIntentionalResearchLog == null || !mIntentionalResearchLog.isAlive()) { + mIntentionalResearchLog = new ResearchLog(createLogFile(mFilesDir)); } + mIntentionalResearchLog.start(); + } catch (IOException e) { + Log.w(TAG, "Could not start ResearchLogger."); } } - public synchronized void stop() { - Log.d(TAG, "stop called"); - if (mLoggingHandler != null && mLoggingState == LOGGING_STATE_ON) { - mLoggingState = LOGGING_STATE_STOPPING; - flushEventQueue(true); - // put this in the Handler queue so pending writes are processed first. - mLoggingHandler.post(new Runnable() { - @Override - public void run() { - try { - Log.d(TAG, "closing jsonwriter"); - mJsonWriter.endArray(); - mJsonWriter.flush(); - mJsonWriter.close(); - } catch (IllegalStateException e1) { - // assume that this is just the json not being terminated properly. - // ignore - e1.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - mJsonWriter = NULL_JSON_WRITER; - mFile = null; - mLoggingState = LOGGING_STATE_OFF; - if (DEBUG) { - Log.d(TAG, "logfile closed"); - } - Log.d(TAG, "finished stop(), notifying"); - synchronized (ResearchLogger.this) { - ResearchLogger.this.notify(); - } - } - } - }); + public void stop() { + if (mMainResearchLog != null) { + mMainResearchLog.stop(); + } + } + + public boolean abort() { + mIsLoggingSuspended = true; + requestIndicatorRedraw(); + boolean didAbortMainLog = false; + if (mMainResearchLog != null) { + mMainResearchLog.abort(); try { - wait(); + mMainResearchLog.waitUntilStopped(ABORT_TIMEOUT_IN_MS); } catch (InterruptedException e) { - e.printStackTrace(); + // interrupted early. carry on. + } + if (mMainResearchLog.isAbortSuccessful()) { + didAbortMainLog = true; } } - } - - public synchronized boolean abort() { - Log.d(TAG, "abort called"); - boolean isLogFileDeleted = false; - if (mLoggingHandler != null && mLoggingState == LOGGING_STATE_ON) { - mLoggingState = LOGGING_STATE_STOPPING; + boolean didAbortIntentionalLog = false; + if (mIntentionalResearchLog != null) { + mIntentionalResearchLog.abort(); try { - Log.d(TAG, "closing jsonwriter"); - mJsonWriter.endArray(); - mJsonWriter.close(); - } catch (IllegalStateException e1) { - // assume that this is just the json not being terminated properly. - // ignore - e1.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - mJsonWriter = NULL_JSON_WRITER; - // delete file - final boolean isDeleted = mFile.delete(); - if (isDeleted) { - isLogFileDeleted = true; - } - mFile = null; - mLoggingState = LOGGING_STATE_OFF; - if (DEBUG) { - Log.d(TAG, "logfile closed"); - } + mIntentionalResearchLog.waitUntilStopped(ABORT_TIMEOUT_IN_MS); + } catch (InterruptedException e) { + // interrupted early. carry on. + } + if (mIntentionalResearchLog.isAbortSuccessful()) { + didAbortIntentionalLog = true; } } - return isLogFileDeleted; + return didAbortMainLog && didAbortIntentionalLog; } - /* package */ synchronized void flush() { + /* package */ void flush() { + if (mMainResearchLog != null) { + mMainResearchLog.flush(); + } + } + + private void logWholeSessionHistory() throws IOException { try { - mJsonWriter.flush(); - } catch (IOException e) { - e.printStackTrace(); + LogUnit headerLogUnit = new LogUnit(); + headerLogUnit.addLogAtom(EVENTKEYS_INTENTIONAL_LOG, EVENTKEYS_NULLVALUES, false); + mIntentionalResearchLog.publishAllEvents(headerLogUnit); + for (LogUnit logUnit : mIntentionalResearchLogQueue) { + mIntentionalResearchLog.publishAllEvents(logUnit); + } + mIntentionalResearchLog.stop(); + mIntentionalResearchLog = new ResearchLog(createLogFile(mFilesDir)); + mIntentionalResearchLog.start(); + } finally { + mIntentionalResearchLogQueue.clear(); } } @@ -290,7 +233,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang final CharSequence title = latinIME.getString(R.string.english_ime_research_log); final CharSequence[] items = new CharSequence[] { latinIME.getString(R.string.note_timestamp_for_researchlog), - latinIME.getString(R.string.do_not_log_this_session), + mIsLoggingSuspended ? latinIME.getString(R.string.enable_session_logging) : + latinIME.getString(R.string.do_not_log_this_session), + latinIME.getString(R.string.log_whole_session_history), }; final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { @Override @@ -298,28 +243,46 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang di.dismiss(); switch (position) { case 0: - ResearchLogger.getInstance().userTimestamp(); + userTimestamp(); Toast.makeText(latinIME, R.string.notify_recorded_timestamp, Toast.LENGTH_LONG).show(); break; case 1: - Toast toast = Toast.makeText(latinIME, - R.string.notify_session_log_deleting, Toast.LENGTH_LONG); - toast.show(); - final ResearchLogger logger = ResearchLogger.getInstance(); - boolean isLogDeleted = logger.abort(); - toast.cancel(); - if (isLogDeleted) { - Toast.makeText(latinIME, R.string.notify_session_log_deleted, - Toast.LENGTH_LONG).show(); + if (mIsLoggingSuspended) { + mIsLoggingSuspended = false; + requestIndicatorRedraw(); + Toast toast = Toast.makeText(latinIME, + R.string.notify_session_logging_enabled, Toast.LENGTH_LONG); } else { - Toast.makeText(latinIME, - R.string.notify_session_log_not_deleted, Toast.LENGTH_LONG) - .show(); + Toast toast = Toast.makeText(latinIME, + R.string.notify_session_log_deleting, Toast.LENGTH_LONG); + toast.show(); + boolean isLogDeleted = abort(); + toast.cancel(); + if (isLogDeleted) { + Toast.makeText(latinIME, R.string.notify_session_log_deleted, + Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(latinIME, + R.string.notify_session_log_not_deleted, Toast.LENGTH_LONG) + .show(); + } + } + break; + case 2: + try { + logWholeSessionHistory(); + Toast.makeText(latinIME, R.string.notify_session_history_logged, + Toast.LENGTH_LONG).show(); + } catch (IOException e) { + Toast.makeText(latinIME, R.string.notify_session_history_not_logged, + Toast.LENGTH_LONG).show(); + e.printStackTrace(); } break; } } + }; final AlertDialog.Builder builder = new AlertDialog.Builder(latinIME) .setItems(items, listener) @@ -336,7 +299,14 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } private boolean isAllowedToLog() { - return mLoggingState == LOGGING_STATE_ON && !mIsPasswordView; + return !mIsPasswordView && !mIsLoggingSuspended; + } + + public void requestIndicatorRedraw() { + // invalidate any existing graphics + if (mKeyboardSwitcher != null) { + mKeyboardSwitcher.getKeyboardView().invalidateAllKeys(); + } } private static final String CURRENT_TIME_KEY = "_ct"; @@ -360,7 +330,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang private synchronized void enqueuePotentiallyPrivateEvent(final String[] keys, final Object[] values) { assert values.length + 1 == keys.length; - mCurrentLogUnit.addLogAtom(keys, values, true); + if (isAllowedToLog()) { + mCurrentLogUnit.addLogAtom(keys, values, true); + } } /** @@ -377,7 +349,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang */ private synchronized void enqueueEvent(final String[] keys, final Object[] values) { assert values.length + 1 == keys.length; - mCurrentLogUnit.addLogAtom(keys, values, false); + if (isAllowedToLog()) { + mCurrentLogUnit.addLogAtom(keys, values, false); + } } // Used to track how often words are logged. Too-frequent logging can leak @@ -453,131 +427,33 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } private void onWordComplete(String word) { - final boolean isPrivacyThreat = isPrivacyThreat(word); - flushEventQueue(isPrivacyThreat); - if (isPrivacyThreat) { + if (isPrivacyThreat(word)) { + publishLogUnit(mCurrentLogUnit, true); mLoggingFrequencyState.onWordNotLogged(); } else { + publishLogUnit(mCurrentLogUnit, false); mLoggingFrequencyState.onWordLogged(); } + mCurrentLogUnit = new LogUnit(); } - /** - * Write out enqueued LogEvents to the log, possibly dropping privacy sensitive events. - */ - /* package for test */ synchronized void flushEventQueue( - boolean removePotentiallyPrivateEvents) { - if (isAllowedToLog()) { - mCurrentLogUnit.setRemovePotentiallyPrivateEvents(removePotentiallyPrivateEvents); - mLoggingHandler.post(mCurrentLogUnit); + private void publishLogUnit(LogUnit logUnit, boolean isPrivacySensitive) { + if (isPrivacySensitive) { + mMainResearchLog.publishPublicEvents(logUnit); + } else { + mMainResearchLog.publishAllEvents(logUnit); } - mCurrentLogUnit = new LogUnit(); + mIntentionalResearchLogQueue.add(logUnit); } - private synchronized void outputEvent(final String[] keys, final Object[] values) { - try { - mJsonWriter.beginObject(); - mJsonWriter.name(CURRENT_TIME_KEY).value(System.currentTimeMillis()); - mJsonWriter.name(UPTIME_KEY).value(SystemClock.uptimeMillis()); - mJsonWriter.name(EVENT_TYPE_KEY).value(keys[0]); - final int length = values.length; - for (int i = 0; i < length; i++) { - mJsonWriter.name(keys[i + 1]); - Object value = values[i]; - if (value instanceof String) { - mJsonWriter.value((String) value); - } else if (value instanceof Number) { - mJsonWriter.value((Number) value); - } else if (value instanceof Boolean) { - mJsonWriter.value((Boolean) value); - } else if (value instanceof CompletionInfo[]) { - CompletionInfo[] ci = (CompletionInfo[]) value; - mJsonWriter.beginArray(); - for (int j = 0; j < ci.length; j++) { - mJsonWriter.value(ci[j].toString()); - } - mJsonWriter.endArray(); - } else if (value instanceof SharedPreferences) { - SharedPreferences prefs = (SharedPreferences) value; - mJsonWriter.beginObject(); - for (Map.Entry<String,?> entry : prefs.getAll().entrySet()) { - mJsonWriter.name(entry.getKey()); - final Object innerValue = entry.getValue(); - if (innerValue == null) { - mJsonWriter.nullValue(); - } else if (innerValue instanceof Boolean) { - mJsonWriter.value((Boolean) innerValue); - } else if (innerValue instanceof Number) { - mJsonWriter.value((Number) innerValue); - } else { - mJsonWriter.value(innerValue.toString()); - } - } - mJsonWriter.endObject(); - } else if (value instanceof Key[]) { - Key[] keyboardKeys = (Key[]) value; - mJsonWriter.beginArray(); - for (Key keyboardKey : keyboardKeys) { - mJsonWriter.beginObject(); - mJsonWriter.name("code").value(keyboardKey.mCode); - mJsonWriter.name("altCode").value(keyboardKey.mAltCode); - mJsonWriter.name("x").value(keyboardKey.mX); - mJsonWriter.name("y").value(keyboardKey.mY); - mJsonWriter.name("w").value(keyboardKey.mWidth); - mJsonWriter.name("h").value(keyboardKey.mHeight); - mJsonWriter.endObject(); - } - mJsonWriter.endArray(); - } else if (value instanceof SuggestedWords) { - SuggestedWords words = (SuggestedWords) value; - mJsonWriter.beginObject(); - mJsonWriter.name("typedWordValid").value(words.mTypedWordValid); - mJsonWriter.name("willAutoCorrect").value(words.mWillAutoCorrect); - mJsonWriter.name("isPunctuationSuggestions") - .value(words.mIsPunctuationSuggestions); - mJsonWriter.name("isObsoleteSuggestions") - .value(words.mIsObsoleteSuggestions); - mJsonWriter.name("isPrediction") - .value(words.mIsPrediction); - mJsonWriter.name("words"); - mJsonWriter.beginArray(); - final int size = words.size(); - for (int j = 0; j < size; j++) { - SuggestedWordInfo wordInfo = words.getWordInfo(j); - mJsonWriter.value(wordInfo.toString()); - } - mJsonWriter.endArray(); - mJsonWriter.endObject(); - } else if (value == null) { - mJsonWriter.nullValue(); - } else { - Log.w(TAG, "Unrecognized type to be logged: " + - (value == null ? "<null>" : value.getClass().getName())); - mJsonWriter.nullValue(); - } - } - mJsonWriter.endObject(); - } catch (IOException e) { - e.printStackTrace(); - Log.w(TAG, "Error in JsonWriter; disabling logging"); - try { - mJsonWriter.close(); - } catch (IllegalStateException e1) { - // assume that this is just the json not being terminated properly. - // ignore - } catch (IOException e1) { - e1.printStackTrace(); - } finally { - mJsonWriter = NULL_JSON_WRITER; - } - } + /* package */ void publishCurrentLogUnit(ResearchLog researchLog, boolean isPrivacySensitive) { + publishLogUnit(mCurrentLogUnit, isPrivacySensitive); } - private static class LogUnit implements Runnable { + static class LogUnit { private final List<String[]> mKeysList = new ArrayList<String[]>(); private final List<Object[]> mValuesList = new ArrayList<Object[]>(); private final List<Boolean> mIsPotentiallyPrivate = new ArrayList<Boolean>(); - private boolean mRemovePotentiallyPrivateEvents = true; private void addLogAtom(final String[] keys, final Object[] values, final Boolean isPotentiallyPrivate) { @@ -586,20 +462,19 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang mIsPotentiallyPrivate.add(isPotentiallyPrivate); } - void setRemovePotentiallyPrivateEvents(boolean removePotentiallyPrivateEvents) { - mRemovePotentiallyPrivateEvents = removePotentiallyPrivateEvents; + public void publishPublicEventsTo(ResearchLog researchLog) { + final int size = mKeysList.size(); + for (int i = 0; i < size; i++) { + if (!mIsPotentiallyPrivate.get(i)) { + researchLog.outputEvent(mKeysList.get(i), mValuesList.get(i)); + } + } } - @Override - public void run() { - final int numAtoms = mKeysList.size(); - for (int atomIndex = 0; atomIndex < numAtoms; atomIndex++) { - if (mRemovePotentiallyPrivateEvents && mIsPotentiallyPrivate.get(atomIndex)) { - continue; - } - final String[] keys = mKeysList.get(atomIndex); - final Object[] values = mValuesList.get(atomIndex); - ResearchLogger.getInstance().outputEvent(keys, values); + public void publishAllEventsTo(ResearchLog researchLog) { + final int size = mKeysList.size(); + for (int i = 0; i < size; i++) { + researchLog.outputEvent(mKeysList.get(i), mValuesList.get(i)); } } } @@ -642,6 +517,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang return WORD_REPLACEMENT_STRING; } + private static final String[] EVENTKEYS_INTENTIONAL_LOG = { + "IntentionalLog" + }; private static final String[] EVENTKEYS_LATINKEYBOARDVIEW_PROCESSMOTIONEVENT = { "LatinKeyboardViewProcessMotionEvent", "action", "eventTime", "id", "x", "y", "size", "pressure" @@ -792,7 +670,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } final ResearchLogger researchLogger = getInstance(); researchLogger.enqueueEvent(EVENTKEYS_LATINIME_ONWINDOWHIDDEN, values); - researchLogger.flushEventQueue(true); // Play it safe. Remove privacy-sensitive events. + // Play it safe. Remove privacy-sensitive events. + researchLogger.publishLogUnit(researchLogger.mCurrentLogUnit, true); + researchLogger.mCurrentLogUnit = new LogUnit(); } } @@ -804,7 +684,8 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang final SharedPreferences prefs) { if (editorInfo != null) { final Object[] values = { - getUUID(prefs), editorInfo.packageName, Integer.toHexString(editorInfo.inputType), + getInstance().mUUIDString, editorInfo.packageName, + Integer.toHexString(editorInfo.inputType), Integer.toHexString(editorInfo.imeOptions), editorInfo.fieldId, Build.DISPLAY, Build.MODEL, prefs, OUTPUT_FORMAT_VERSION }; diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index f810eccf4..31566bf13 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -153,17 +153,22 @@ public class Suggest { mAutoCorrectionThreshold = threshold; } - // TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder public SuggestedWords getSuggestedWords( final WordComposer wordComposer, CharSequence prevWordForBigram, - final ProximityInfo proximityInfo, final boolean isCorrectionEnabled, - // TODO: remove isPrediction parameter. It effectively means the same thing - // as wordComposer.size() <= 1 - final boolean isPrediction) { + final ProximityInfo proximityInfo, final boolean isCorrectionEnabled) { LatinImeLogger.onStartSuggestion(prevWordForBigram); - final boolean isFirstCharCapitalized = - !isPrediction && wordComposer.isFirstCharCapitalized(); - final boolean isAllUpperCase = !isPrediction && wordComposer.isAllUpperCase(); + if (wordComposer.isBatchMode()) { + return getSuggestedWordsForBatchInput(wordComposer, prevWordForBigram, proximityInfo); + } else { + return getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo, + isCorrectionEnabled); + } + } + + // Retrieves suggestions for the typing input. + private SuggestedWords getSuggestedWordsForTypingInput( + final WordComposer wordComposer, CharSequence prevWordForBigram, + final ProximityInfo proximityInfo, final boolean isCorrectionEnabled) { final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount(); final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator, MAX_SUGGESTIONS); @@ -174,34 +179,20 @@ public class Suggest { : typedWord; LatinImeLogger.onAddSuggestedWord(typedWord, Dictionary.TYPE_USER_TYPED); - if (wordComposer.size() <= 1 && isCorrectionEnabled) { - // At first character typed, search only the bigrams - if (!TextUtils.isEmpty(prevWordForBigram)) { - for (final String key : mDictionaries.keySet()) { - final Dictionary dictionary = mDictionaries.get(key); - suggestionsSet.addAll(dictionary.getBigrams(wordComposer, prevWordForBigram)); - } - } - } else if (wordComposer.size() > 1) { - final WordComposer wordComposerForLookup; - if (trailingSingleQuotesCount > 0) { - wordComposerForLookup = new WordComposer(wordComposer); - for (int i = trailingSingleQuotesCount - 1; i >= 0; --i) { - wordComposerForLookup.deleteLast(); - } - } else { - wordComposerForLookup = wordComposer; - } - // At second character typed, search the unigrams (scores being affected by bigrams) - for (final String key : mDictionaries.keySet()) { - // Skip UserUnigramDictionary and WhitelistDictionary to lookup - if (key.equals(Dictionary.TYPE_USER_HISTORY) - || key.equals(Dictionary.TYPE_WHITELIST)) - continue; - final Dictionary dictionary = mDictionaries.get(key); - suggestionsSet.addAll(dictionary.getWords( - wordComposerForLookup, prevWordForBigram, proximityInfo)); + final WordComposer wordComposerForLookup; + if (trailingSingleQuotesCount > 0) { + wordComposerForLookup = new WordComposer(wordComposer); + for (int i = trailingSingleQuotesCount - 1; i >= 0; --i) { + wordComposerForLookup.deleteLast(); } + } else { + wordComposerForLookup = wordComposer; + } + + for (final String key : mDictionaries.keySet()) { + final Dictionary dictionary = mDictionaries.get(key); + suggestionsSet.addAll(dictionary.getSuggestions( + wordComposerForLookup, prevWordForBigram, proximityInfo)); } // TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid" @@ -214,10 +205,23 @@ public class Suggest { final CharSequence whitelistedWord = mWhiteListDictionary.getWhitelistedWord(consideredWord); + if (whitelistedWord != null) { + // MAX_SCORE ensures this will be considered strong enough to be auto-corrected + suggestionsSet.add(new SuggestedWordInfo(whitelistedWord, + SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_WHITELIST, + Dictionary.TYPE_WHITELIST)); + } final boolean hasAutoCorrection; - if (!isCorrectionEnabled || !allowsToBeAutoCorrected || wordComposer.isMostlyCaps() - || wordComposer.isResumed() || !hasMainDictionary()) { + // TODO: using isCorrectionEnabled here is not very good. It's probably useless, because + // any attempt to do auto-correction is already shielded with a test for this flag; at the + // same time, it feels wrong that the SuggestedWord object includes information about + // the current settings. It may also be useful to know, when the setting is off, whether + // the word *would* have been auto-corrected. + if (!isCorrectionEnabled || !allowsToBeAutoCorrected || !wordComposer.isComposingWord() + || suggestionsSet.isEmpty() + || wordComposer.isMostlyCaps() || wordComposer.isResumed() + || !hasMainDictionary()) { // If we don't have a main dictionary, we never want to auto-correct. The reason for // this is, the user may have a contact whose name happens to match a valid word in // their language, and it will unexpectedly auto-correct. For example, if the user @@ -225,26 +229,16 @@ public class Suggest { // would always auto-correct to "Will" which is unwanted. Hence, no main dict => no // auto-correct. hasAutoCorrection = false; - } else if (null != whitelistedWord) { - hasAutoCorrection = true; - } else if (suggestionsSet.isEmpty()) { - hasAutoCorrection = false; - } else if (AutoCorrection.suggestionExceedsAutoCorrectionThreshold(suggestionsSet.first(), - consideredWord, mAutoCorrectionThreshold)) { - hasAutoCorrection = true; } else { - hasAutoCorrection = false; - } - - if (whitelistedWord != null) { - suggestionsSet.add(new SuggestedWordInfo(whitelistedWord, - SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_WHITELIST, - Dictionary.TYPE_WHITELIST)); + hasAutoCorrection = AutoCorrection.suggestionExceedsAutoCorrectionThreshold( + suggestionsSet.first(), consideredWord, mAutoCorrectionThreshold); } final ArrayList<SuggestedWordInfo> suggestionsContainer = new ArrayList<SuggestedWordInfo>(suggestionsSet); final int suggestionsCount = suggestionsContainer.size(); + final boolean isFirstCharCapitalized = wordComposer.isFirstCharCapitalized(); + final boolean isAllUpperCase = wordComposer.isAllUpperCase(); if (isFirstCharCapitalized || isAllUpperCase || 0 != trailingSingleQuotesCount) { for (int i = 0; i < suggestionsCount; ++i) { final SuggestedWordInfo wordInfo = suggestionsContainer.get(i); @@ -278,11 +272,42 @@ public class Suggest { // TODO: this first argument is lying. If this is a whitelisted word which is an // actual word, it says typedWordValid = false, which looks wrong. We should either // rename the attribute or change the value. - !isPrediction && !allowsToBeAutoCorrected /* typedWordValid */, - !isPrediction && hasAutoCorrection, /* willAutoCorrect */ + !allowsToBeAutoCorrected /* typedWordValid */, + hasAutoCorrection, /* willAutoCorrect */ + false /* isPunctuationSuggestions */, + false /* isObsoleteSuggestions */, + !wordComposer.isComposingWord() /* isPrediction */); + } + + // Retrieves suggestions for the batch input. + private SuggestedWords getSuggestedWordsForBatchInput( + final WordComposer wordComposer, CharSequence prevWordForBigram, + final ProximityInfo proximityInfo) { + final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator, + MAX_SUGGESTIONS); + + // At second character typed, search the unigrams (scores being affected by bigrams) + for (final String key : mDictionaries.keySet()) { + // Skip UserUnigramDictionary and WhitelistDictionary to lookup + if (key.equals(Dictionary.TYPE_USER_HISTORY) + || key.equals(Dictionary.TYPE_WHITELIST)) { + continue; + } + final Dictionary dictionary = mDictionaries.get(key); + suggestionsSet.addAll(dictionary.getSuggestions( + wordComposer, prevWordForBigram, proximityInfo)); + } + + final ArrayList<SuggestedWordInfo> suggestionsContainer = + new ArrayList<SuggestedWordInfo>(suggestionsSet); + + SuggestedWordInfo.removeDups(suggestionsContainer); + return new SuggestedWords(suggestionsContainer, + true /* typedWordValid */, + true /* willAutoCorrect */, false /* isPunctuationSuggestions */, false /* isObsoleteSuggestions */, - isPrediction); + false /* isPrediction */); } private static ArrayList<SuggestedWordInfo> getSuggestionsInfoListWithDebugInfo( diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java index f079c2112..88fc006df 100644 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -125,6 +125,7 @@ public class SuggestedWords { public static final int KIND_HARDCODED = 5; // Hardcoded suggestion, e.g. punctuation public static final int KIND_APP_DEFINED = 6; // Suggested by the application public static final int KIND_SHORTCUT = 7; // A shortcut + public static final int KIND_PREDICTION = 8; // A prediction (== a suggestion with no input) public final String mWord; public final int mScore; public final int mKind; // one of the KIND_* constants above diff --git a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsBinaryDictionary.java b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsBinaryDictionary.java index 9b20bd690..bdd988df2 100644 --- a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsBinaryDictionary.java @@ -32,10 +32,10 @@ public class SynchronouslyLoadedContactsBinaryDictionary extends ContactsBinaryD } @Override - public synchronized ArrayList<SuggestedWordInfo> getWords(final WordComposer codes, + public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes, final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { syncReloadDictionaryIfRequired(); - return getWordsInner(codes, prevWordForBigrams, proximityInfo); + return super.getSuggestions(codes, prevWordForBigrams, proximityInfo); } @Override diff --git a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java index 5b2a6edec..b8cfddd4e 100644 --- a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserBinaryDictionary.java @@ -35,10 +35,10 @@ public class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDictionar } @Override - public synchronized ArrayList<SuggestedWordInfo> getWords(final WordComposer codes, + public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes, final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { syncReloadDictionaryIfRequired(); - return getWordsInner(codes, prevWordForBigrams, proximityInfo); + return super.getSuggestions(codes, prevWordForBigrams, proximityInfo); } @Override diff --git a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java index 73fa83f9a..3bb670c9a 100644 --- a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java @@ -27,9 +27,12 @@ import android.os.AsyncTask; import android.provider.BaseColumns; import android.util.Log; +import com.android.inputmethod.keyboard.ProximityInfo; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.UserHistoryForgettingCurveUtils.ForgettingCurveParams; import java.lang.ref.SoftReference; +import java.util.ArrayList; import java.util.HashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock; @@ -157,6 +160,14 @@ public class UserHistoryDictionary extends ExpandableDictionary { // super.close(); } + @Override + protected ArrayList<SuggestedWordInfo> getWordsInner(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { + // Inhibit suggestions (not predictions) for user history for now. Removing this method + // is enough to use it through the standard ExpandableDictionary way. + return null; + } + /** * Return whether the passed charsequence is in the dictionary. */ diff --git a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java index 3af22140e..14476dcf0 100644 --- a/java/src/com/android/inputmethod/latin/WhitelistDictionary.java +++ b/java/src/com/android/inputmethod/latin/WhitelistDictionary.java @@ -22,8 +22,11 @@ import android.text.TextUtils; import android.util.Log; import android.util.Pair; +import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.latin.LocaleUtils.RunInLocale; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; +import java.util.ArrayList; import java.util.HashMap; import java.util.Locale; @@ -88,6 +91,13 @@ public class WhitelistDictionary extends ExpandableDictionary { return null; } + @Override + public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { + // Whitelist does not supply any suggestions or predictions. + return null; + } + // See LatinIME#updateSuggestions. This breaks in the (queer) case that the whitelist // lists that word a should autocorrect to word b, and word c would autocorrect to // an upper-cased version of a. In this case, the way this return value is used would diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java index bfa41c784..25e29008e 100644 --- a/java/src/com/android/inputmethod/latin/WordComposer.java +++ b/java/src/com/android/inputmethod/latin/WordComposer.java @@ -37,6 +37,7 @@ public class WordComposer { private final StringBuilder mTypedWord; private CharSequence mAutoCorrection; private boolean mIsResumed; + private boolean mIsBatchMode; // Cache these values for performance private int mCapsCount; @@ -55,6 +56,7 @@ public class WordComposer { mAutoCorrection = null; mTrailingSingleQuotesCount = 0; mIsResumed = false; + mIsBatchMode = false; refreshSize(); } @@ -67,6 +69,7 @@ public class WordComposer { mAutoCapitalized = source.mAutoCapitalized; mTrailingSingleQuotesCount = source.mTrailingSingleQuotesCount; mIsResumed = source.mIsResumed; + mIsBatchMode = source.mIsBatchMode; refreshSize(); } @@ -80,6 +83,7 @@ public class WordComposer { mIsFirstCharCapitalized = false; mTrailingSingleQuotesCount = 0; mIsResumed = false; + mIsBatchMode = false; refreshSize(); } @@ -140,6 +144,12 @@ public class WordComposer { mAutoCorrection = null; } + // TODO: We may want to have appendBatchInputPointers() as well. + public void setBatchInputPointers(InputPointers batchPointers) { + mInputPointers.copy(batchPointers); + mIsBatchMode = true; + } + /** * Internal method to retrieve reasonable proximity info for a character. */ @@ -295,7 +305,11 @@ public class WordComposer { && type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) { lastComposedWord.deactivate(); } + mCapsCount = 0; + mIsBatchMode = false; mTypedWord.setLength(0); + mTrailingSingleQuotesCount = 0; + mIsFirstCharCapitalized = false; refreshSize(); mAutoCorrection = null; mIsResumed = false; @@ -311,4 +325,8 @@ public class WordComposer { mAutoCorrection = null; // This will be filled by the next call to updateSuggestion. mIsResumed = true; } + + public boolean isBatchMode() { + return mIsBatchMode; + } } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 5332c066a..3bdfe1f27 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -20,14 +20,9 @@ import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.service.textservice.SpellCheckerService; -import android.text.TextUtils; import android.util.Log; -import android.util.LruCache; -import android.view.textservice.SentenceSuggestionsInfo; import android.view.textservice.SuggestionsInfo; -import android.view.textservice.TextInfo; -import com.android.inputmethod.compat.SuggestionsInfoCompatUtils; import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.latin.BinaryDictionary; import com.android.inputmethod.latin.ContactsBinaryDictionary; @@ -37,12 +32,10 @@ import com.android.inputmethod.latin.DictionaryFactory; import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.StringUtils; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SynchronouslyLoadedContactsBinaryDictionary; import com.android.inputmethod.latin.SynchronouslyLoadedUserBinaryDictionary; import com.android.inputmethod.latin.UserBinaryDictionary; import com.android.inputmethod.latin.WhitelistDictionary; -import com.android.inputmethod.latin.WordComposer; import java.lang.ref.WeakReference; import java.util.ArrayList; @@ -65,9 +58,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService public static final String PREF_USE_CONTACTS_KEY = "pref_spellcheck_use_contacts"; - private static final int CAPITALIZE_NONE = 0; // No caps, or mixed case - private static final int CAPITALIZE_FIRST = 1; // First only - private static final int CAPITALIZE_ALL = 2; // All caps + public static final int CAPITALIZE_NONE = 0; // No caps, or mixed case + public static final int CAPITALIZE_FIRST = 1; // First only + public static final int CAPITALIZE_ALL = 2; // All caps private final static String[] EMPTY_STRING_ARRAY = new String[0]; private Map<String, DictionaryPool> mDictionaryPools = @@ -91,8 +84,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService public static final int SCRIPT_LATIN = 0; public static final int SCRIPT_CYRILLIC = 1; - private static final String SINGLE_QUOTE = "\u0027"; - private static final String APOSTROPHE = "\u2019"; + public static final String SINGLE_QUOTE = "\u0027"; + public static final String APOSTROPHE = "\u2019"; private static final TreeMap<String, Integer> mLanguageToScript; static { // List of the supported languages and their associated script. We won't check @@ -129,7 +122,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService onSharedPreferenceChanged(prefs, PREF_USE_CONTACTS_KEY); } - private static int getScriptFromLocale(final Locale locale) { + public static int getScriptFromLocale(final Locale locale) { final Integer script = mLanguageToScript.get(locale.getLanguage()); if (null == script) { throw new RuntimeException("We have been called with an unsupported language: \"" @@ -191,20 +184,27 @@ public class AndroidSpellCheckerService extends SpellCheckerService @Override public Session createSession() { - return new AndroidSpellCheckerSession(this); + // Should not refer to AndroidSpellCheckerSession directly considering + // that AndroidSpellCheckerSession may be overlaid. + return AndroidSpellCheckerSessionFactory.newInstance(this); } - private static SuggestionsInfo getNotInDictEmptySuggestions() { + public static SuggestionsInfo getNotInDictEmptySuggestions() { return new SuggestionsInfo(0, EMPTY_STRING_ARRAY); } - private static SuggestionsInfo getInDictEmptySuggestions() { + public static SuggestionsInfo getInDictEmptySuggestions() { return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, EMPTY_STRING_ARRAY); } + public SuggestionsGatherer newSuggestionsGatherer(final String text, int maxLength) { + return new SuggestionsGatherer( + text, mSuggestionThreshold, mRecommendedThreshold, maxLength); + } + // TODO: remove this class and replace it by storage local to the session. - private static class SuggestionsGatherer { + public static class SuggestionsGatherer { public static class Result { public final String[] mSuggestions; public final boolean mHasRecommendedSuggestions; @@ -396,7 +396,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService }.start(); } - private DictionaryPool getDictionaryPool(final String locale) { + public DictionaryPool getDictionaryPool(final String locale) { DictionaryPool pool = mDictionaryPools.get(locale); if (null == pool) { final Locale localeObject = LocaleUtils.constructLocaleFromString(locale); @@ -447,7 +447,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService } // This method assumes the text is not empty or null. - private static int getCapitalizationType(String text) { + public static int getCapitalizationType(String text) { // If the first char is not uppercase, then the word is either all lower case, // and in either case we return CAPITALIZE_NONE. if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE; @@ -464,379 +464,4 @@ public class AndroidSpellCheckerService extends SpellCheckerService if (1 == capsCount) return CAPITALIZE_FIRST; return (len == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE); } - - private static class AndroidSpellCheckerSession extends Session { - // Immutable, but need the locale which is not available in the constructor yet - private DictionaryPool mDictionaryPool; - // Likewise - private Locale mLocale; - // Cache this for performance - private int mScript; // One of SCRIPT_LATIN or SCRIPT_CYRILLIC for now. - - private final AndroidSpellCheckerService mService; - - private final SuggestionsCache mSuggestionsCache = new SuggestionsCache(); - - private static class SuggestionsParams { - public final String[] mSuggestions; - public final int mFlags; - public SuggestionsParams(String[] suggestions, int flags) { - mSuggestions = suggestions; - mFlags = flags; - } - } - - private static class SuggestionsCache { - private static final char CHAR_DELIMITER = '\uFFFC'; - private static final int MAX_CACHE_SIZE = 50; - private final LruCache<String, SuggestionsParams> mUnigramSuggestionsInfoCache = - new LruCache<String, SuggestionsParams>(MAX_CACHE_SIZE); - - // TODO: Support n-gram input - private static String generateKey(String query, String prevWord) { - if (TextUtils.isEmpty(query) || TextUtils.isEmpty(prevWord)) { - return query; - } - return query + CHAR_DELIMITER + prevWord; - } - - // TODO: Support n-gram input - public SuggestionsParams getSuggestionsFromCache(String query, String prevWord) { - return mUnigramSuggestionsInfoCache.get(generateKey(query, prevWord)); - } - - // TODO: Support n-gram input - public void putSuggestionsToCache( - String query, String prevWord, String[] suggestions, int flags) { - if (suggestions == null || TextUtils.isEmpty(query)) { - return; - } - mUnigramSuggestionsInfoCache.put( - generateKey(query, prevWord), new SuggestionsParams(suggestions, flags)); - } - } - - AndroidSpellCheckerSession(final AndroidSpellCheckerService service) { - mService = service; - } - - @Override - public void onCreate() { - final String localeString = getLocale(); - mDictionaryPool = mService.getDictionaryPool(localeString); - mLocale = LocaleUtils.constructLocaleFromString(localeString); - mScript = getScriptFromLocale(mLocale); - } - - /* - * Returns whether the code point is a letter that makes sense for the specified - * locale for this spell checker. - * The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml - * and is limited to EFIGS languages and Russian. - * Hence at the moment this explicitly tests for Cyrillic characters or Latin characters - * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters. - */ - private static boolean isLetterCheckableByLanguage(final int codePoint, - final int script) { - switch (script) { - case SCRIPT_LATIN: - // Our supported latin script dictionaries (EFIGS) at the moment only include - // characters in the C0, C1, Latin Extended A and B, IPA extensions unicode - // blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF, - // so the below is a very efficient way to test for it. As for the 0-0x3F, it's - // excluded from isLetter anyway. - return codePoint <= 0x2AF && Character.isLetter(codePoint); - case SCRIPT_CYRILLIC: - // All Cyrillic characters are in the 400~52F block. There are some in the upper - // Unicode range, but they are archaic characters that are not used in modern - // russian and are not used by our dictionary. - return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint); - default: - // Should never come here - throw new RuntimeException("Impossible value of script: " + script); - } - } - - /** - * Finds out whether a particular string should be filtered out of spell checking. - * - * This will loosely match URLs, numbers, symbols. To avoid always underlining words that - * we know we will never recognize, this accepts a script identifier that should be one - * of the SCRIPT_* constants defined above, to rule out quickly characters from very - * different languages. - * - * @param text the string to evaluate. - * @param script the identifier for the script this spell checker recognizes - * @return true if we should filter this text out, false otherwise - */ - private static boolean shouldFilterOut(final String text, final int script) { - if (TextUtils.isEmpty(text) || text.length() <= 1) return true; - - // TODO: check if an equivalent processing can't be done more quickly with a - // compiled regexp. - // Filter by first letter - final int firstCodePoint = text.codePointAt(0); - // Filter out words that don't start with a letter or an apostrophe - if (!isLetterCheckableByLanguage(firstCodePoint, script) - && '\'' != firstCodePoint) return true; - - // Filter contents - final int length = text.length(); - int letterCount = 0; - for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) { - final int codePoint = text.codePointAt(i); - // Any word containing a '@' is probably an e-mail address - // Any word containing a '/' is probably either an ad-hoc combination of two - // words or a URI - in either case we don't want to spell check that - if ('@' == codePoint || '/' == codePoint) return true; - if (isLetterCheckableByLanguage(codePoint, script)) ++letterCount; - } - // Guestimate heuristic: perform spell checking if at least 3/4 of the characters - // in this word are letters - return (letterCount * 4 < length * 3); - } - - private SentenceSuggestionsInfo fixWronglyInvalidatedWordWithSingleQuote( - TextInfo ti, SentenceSuggestionsInfo ssi) { - final String typedText = ti.getText(); - if (!typedText.contains(SINGLE_QUOTE)) { - return null; - } - final int N = ssi.getSuggestionsCount(); - final ArrayList<Integer> additionalOffsets = new ArrayList<Integer>(); - final ArrayList<Integer> additionalLengths = new ArrayList<Integer>(); - final ArrayList<SuggestionsInfo> additionalSuggestionsInfos = - new ArrayList<SuggestionsInfo>(); - String currentWord = null; - for (int i = 0; i < N; ++i) { - final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i); - final int flags = si.getSuggestionsAttributes(); - if ((flags & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) == 0) { - continue; - } - final int offset = ssi.getOffsetAt(i); - final int length = ssi.getLengthAt(i); - final String subText = typedText.substring(offset, offset + length); - final String prevWord = currentWord; - currentWord = subText; - if (!subText.contains(SINGLE_QUOTE)) { - continue; - } - final String[] splitTexts = subText.split(SINGLE_QUOTE, -1); - if (splitTexts == null || splitTexts.length <= 1) { - continue; - } - final int splitNum = splitTexts.length; - for (int j = 0; j < splitNum; ++j) { - final String splitText = splitTexts[j]; - if (TextUtils.isEmpty(splitText)) { - continue; - } - if (mSuggestionsCache.getSuggestionsFromCache( - splitText, prevWord) == null) { - continue; - } - final int newLength = splitText.length(); - // Neither RESULT_ATTR_IN_THE_DICTIONARY nor RESULT_ATTR_LOOKS_LIKE_TYPO - final int newFlags = 0; - final SuggestionsInfo newSi = new SuggestionsInfo(newFlags, EMPTY_STRING_ARRAY); - newSi.setCookieAndSequence(si.getCookie(), si.getSequence()); - if (DBG) { - Log.d(TAG, "Override and remove old span over: " - + splitText + ", " + offset + "," + newLength); - } - additionalOffsets.add(offset); - additionalLengths.add(newLength); - additionalSuggestionsInfos.add(newSi); - } - } - final int additionalSize = additionalOffsets.size(); - if (additionalSize <= 0) { - return null; - } - final int suggestionsSize = N + additionalSize; - final int[] newOffsets = new int[suggestionsSize]; - final int[] newLengths = new int[suggestionsSize]; - final SuggestionsInfo[] newSuggestionsInfos = new SuggestionsInfo[suggestionsSize]; - int i; - for (i = 0; i < N; ++i) { - newOffsets[i] = ssi.getOffsetAt(i); - newLengths[i] = ssi.getLengthAt(i); - newSuggestionsInfos[i] = ssi.getSuggestionsInfoAt(i); - } - for (; i < suggestionsSize; ++i) { - newOffsets[i] = additionalOffsets.get(i - N); - newLengths[i] = additionalLengths.get(i - N); - newSuggestionsInfos[i] = additionalSuggestionsInfos.get(i - N); - } - return new SentenceSuggestionsInfo(newSuggestionsInfos, newOffsets, newLengths); - } - - @Override - public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple( - TextInfo[] textInfos, int suggestionsLimit) { - final SentenceSuggestionsInfo[] retval = super.onGetSentenceSuggestionsMultiple( - textInfos, suggestionsLimit); - if (retval == null || retval.length != textInfos.length) { - return retval; - } - for (int i = 0; i < retval.length; ++i) { - final SentenceSuggestionsInfo tempSsi = - fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]); - if (tempSsi != null) { - retval[i] = tempSsi; - } - } - return retval; - } - - @Override - 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) { - final String prevWord; - if (sequentialWords && i > 0) { - final String prevWordCandidate = textInfos[i - 1].getText(); - // Note that an empty string would be used to indicate the initial word - // in the future. - prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate; - } else { - prevWord = null; - } - retval[i] = onGetSuggestions(textInfos[i], prevWord, suggestionsLimit); - retval[i].setCookieAndSequence( - textInfos[i].getCookie(), textInfos[i].getSequence()); - } - return retval; - } - - // Note : this must be reentrant - /** - * Gets a list of suggestions for a specific string. This returns a list of possible - * corrections for the text passed as an argument. It may split or group words, and - * even perform grammatical analysis. - */ - @Override - public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, - final int suggestionsLimit) { - return onGetSuggestions(textInfo, null, suggestionsLimit); - } - - private SuggestionsInfo onGetSuggestions( - final TextInfo textInfo, final String prevWord, final int suggestionsLimit) { - try { - final String inText = textInfo.getText(); - final SuggestionsParams cachedSuggestionsParams = - mSuggestionsCache.getSuggestionsFromCache(inText, prevWord); - if (cachedSuggestionsParams != null) { - if (DBG) { - Log.d(TAG, "Cache hit: " + inText + ", " + cachedSuggestionsParams.mFlags); - } - return new SuggestionsInfo( - cachedSuggestionsParams.mFlags, cachedSuggestionsParams.mSuggestions); - } - - if (shouldFilterOut(inText, mScript)) { - DictAndProximity dictInfo = null; - try { - dictInfo = mDictionaryPool.takeOrGetNull(); - if (null == dictInfo) return getNotInDictEmptySuggestions(); - return dictInfo.mDictionary.isValidWord(inText) ? - getInDictEmptySuggestions() : getNotInDictEmptySuggestions(); - } finally { - if (null != dictInfo) { - if (!mDictionaryPool.offer(dictInfo)) { - Log.e(TAG, "Can't re-insert a dictionary into its pool"); - } - } - } - } - final String text = inText.replaceAll(APOSTROPHE, SINGLE_QUOTE); - - // TODO: Don't gather suggestions if the limit is <= 0 unless necessary - final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(text, - mService.mSuggestionThreshold, mService.mRecommendedThreshold, - suggestionsLimit); - final WordComposer composer = new WordComposer(); - final int length = text.length(); - for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) { - final int codePoint = text.codePointAt(i); - // The getXYForCodePointAndScript method returns (Y << 16) + X - final int xy = SpellCheckerProximityInfo.getXYForCodePointAndScript( - codePoint, mScript); - if (SpellCheckerProximityInfo.NOT_A_COORDINATE_PAIR == xy) { - composer.add(codePoint, WordComposer.NOT_A_COORDINATE, - WordComposer.NOT_A_COORDINATE); - } else { - composer.add(codePoint, xy & 0xFFFF, xy >> 16); - } - } - - final int capitalizeType = getCapitalizationType(text); - boolean isInDict = true; - DictAndProximity dictInfo = null; - try { - dictInfo = mDictionaryPool.takeOrGetNull(); - if (null == dictInfo) return getNotInDictEmptySuggestions(); - final ArrayList<SuggestedWordInfo> suggestions = dictInfo.mDictionary.getWords( - composer, prevWord, dictInfo.mProximityInfo); - for (final SuggestedWordInfo suggestion : suggestions) { - final String suggestionStr = suggestion.mWord.toString(); - suggestionsGatherer.addWord(suggestionStr.toCharArray(), null, 0, - suggestionStr.length(), suggestion.mScore); - } - isInDict = dictInfo.mDictionary.isValidWord(text); - if (!isInDict && CAPITALIZE_NONE != capitalizeType) { - // We want to test the word again if it's all caps or first caps only. - // If it's fully down, we already tested it, if it's mixed case, we don't - // want to test a lowercase version of it. - isInDict = dictInfo.mDictionary.isValidWord(text.toLowerCase(mLocale)); - } - } finally { - if (null != dictInfo) { - if (!mDictionaryPool.offer(dictInfo)) { - Log.e(TAG, "Can't re-insert a dictionary into its pool"); - } - } - } - - final SuggestionsGatherer.Result result = suggestionsGatherer.getResults( - capitalizeType, mLocale); - - if (DBG) { - Log.i(TAG, "Spell checking results for " + text + " with suggestion limit " - + suggestionsLimit); - Log.i(TAG, "IsInDict = " + isInDict); - Log.i(TAG, "LooksLikeTypo = " + (!isInDict)); - Log.i(TAG, "HasRecommendedSuggestions = " + result.mHasRecommendedSuggestions); - if (null != result.mSuggestions) { - for (String suggestion : result.mSuggestions) { - Log.i(TAG, suggestion); - } - } - } - - final int flags = - (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY - : SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) - | (result.mHasRecommendedSuggestions - ? SuggestionsInfoCompatUtils - .getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS() - : 0); - final SuggestionsInfo retval = new SuggestionsInfo(flags, result.mSuggestions); - mSuggestionsCache.putSuggestionsToCache(text, prevWord, result.mSuggestions, flags); - return retval; - } catch (RuntimeException e) { - // Don't kill the keyboard if there is a bug in the spell checker - if (DBG) { - throw e; - } else { - Log.e(TAG, "Exception while spellcheking: " + e); - return getNotInDictEmptySuggestions(); - } - } - } - } } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java new file mode 100644 index 000000000..501a0e221 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2012 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 com.android.inputmethod.latin.spellcheck; + +import android.text.TextUtils; +import android.util.Log; +import android.view.textservice.SentenceSuggestionsInfo; +import android.view.textservice.SuggestionsInfo; +import android.view.textservice.TextInfo; + +import java.util.ArrayList; + +public class AndroidSpellCheckerSession extends AndroidWordLevelSpellCheckerSession { + private static final String TAG = AndroidSpellCheckerSession.class.getSimpleName(); + private static final boolean DBG = false; + private final static String[] EMPTY_STRING_ARRAY = new String[0]; + + public AndroidSpellCheckerSession(AndroidSpellCheckerService service) { + super(service); + } + + private SentenceSuggestionsInfo fixWronglyInvalidatedWordWithSingleQuote(TextInfo ti, + SentenceSuggestionsInfo ssi) { + final String typedText = ti.getText(); + if (!typedText.contains(AndroidSpellCheckerService.SINGLE_QUOTE)) { + return null; + } + final int N = ssi.getSuggestionsCount(); + final ArrayList<Integer> additionalOffsets = new ArrayList<Integer>(); + final ArrayList<Integer> additionalLengths = new ArrayList<Integer>(); + final ArrayList<SuggestionsInfo> additionalSuggestionsInfos = + new ArrayList<SuggestionsInfo>(); + String currentWord = null; + for (int i = 0; i < N; ++i) { + final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i); + final int flags = si.getSuggestionsAttributes(); + if ((flags & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) == 0) { + continue; + } + final int offset = ssi.getOffsetAt(i); + final int length = ssi.getLengthAt(i); + final String subText = typedText.substring(offset, offset + length); + final String prevWord = currentWord; + currentWord = subText; + if (!subText.contains(AndroidSpellCheckerService.SINGLE_QUOTE)) { + continue; + } + final String[] splitTexts = + subText.split(AndroidSpellCheckerService.SINGLE_QUOTE, -1); + if (splitTexts == null || splitTexts.length <= 1) { + continue; + } + final int splitNum = splitTexts.length; + for (int j = 0; j < splitNum; ++j) { + final String splitText = splitTexts[j]; + if (TextUtils.isEmpty(splitText)) { + continue; + } + if (mSuggestionsCache.getSuggestionsFromCache(splitText, prevWord) == null) { + continue; + } + final int newLength = splitText.length(); + // Neither RESULT_ATTR_IN_THE_DICTIONARY nor RESULT_ATTR_LOOKS_LIKE_TYPO + final int newFlags = 0; + final SuggestionsInfo newSi = + new SuggestionsInfo(newFlags, EMPTY_STRING_ARRAY); + newSi.setCookieAndSequence(si.getCookie(), si.getSequence()); + if (DBG) { + Log.d(TAG, "Override and remove old span over: " + splitText + ", " + + offset + "," + newLength); + } + additionalOffsets.add(offset); + additionalLengths.add(newLength); + additionalSuggestionsInfos.add(newSi); + } + } + final int additionalSize = additionalOffsets.size(); + if (additionalSize <= 0) { + return null; + } + final int suggestionsSize = N + additionalSize; + final int[] newOffsets = new int[suggestionsSize]; + final int[] newLengths = new int[suggestionsSize]; + final SuggestionsInfo[] newSuggestionsInfos = new SuggestionsInfo[suggestionsSize]; + int i; + for (i = 0; i < N; ++i) { + newOffsets[i] = ssi.getOffsetAt(i); + newLengths[i] = ssi.getLengthAt(i); + newSuggestionsInfos[i] = ssi.getSuggestionsInfoAt(i); + } + for (; i < suggestionsSize; ++i) { + newOffsets[i] = additionalOffsets.get(i - N); + newLengths[i] = additionalLengths.get(i - N); + newSuggestionsInfos[i] = additionalSuggestionsInfos.get(i - N); + } + return new SentenceSuggestionsInfo(newSuggestionsInfos, newOffsets, newLengths); + } + + @Override + public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos, + int suggestionsLimit) { + final SentenceSuggestionsInfo[] retval = + super.onGetSentenceSuggestionsMultiple(textInfos, suggestionsLimit); + if (retval == null || retval.length != textInfos.length) { + return retval; + } + for (int i = 0; i < retval.length; ++i) { + final SentenceSuggestionsInfo tempSsi = + fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]); + if (tempSsi != null) { + retval[i] = tempSsi; + } + } + return retval; + } + + @Override + 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) { + final String prevWord; + if (sequentialWords && i > 0) { + final String prevWordCandidate = textInfos[i - 1].getText(); + // Note that an empty string would be used to indicate the initial word + // in the future. + prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate; + } else { + prevWord = null; + } + retval[i] = onGetSuggestions(textInfos[i], prevWord, suggestionsLimit); + retval[i].setCookieAndSequence(textInfos[i].getCookie(), + textInfos[i].getSequence()); + } + return retval; + } +} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSessionFactory.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSessionFactory.java new file mode 100644 index 000000000..8eb1eb68e --- /dev/null +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSessionFactory.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2012 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 com.android.inputmethod.latin.spellcheck; + +import android.service.textservice.SpellCheckerService.Session; + +public abstract class AndroidSpellCheckerSessionFactory { + public static Session newInstance(AndroidSpellCheckerService service) { + return new AndroidSpellCheckerSession(service); + } +} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java new file mode 100644 index 000000000..0171dc06d --- /dev/null +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java @@ -0,0 +1,302 @@ +/* + * Copyright (C) 2012 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 com.android.inputmethod.latin.spellcheck; + +import android.service.textservice.SpellCheckerService.Session; +import android.text.TextUtils; +import android.util.Log; +import android.util.LruCache; +import android.view.textservice.SuggestionsInfo; +import android.view.textservice.TextInfo; + +import com.android.inputmethod.compat.SuggestionsInfoCompatUtils; +import com.android.inputmethod.latin.LocaleUtils; +import com.android.inputmethod.latin.WordComposer; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; +import com.android.inputmethod.latin.spellcheck.AndroidSpellCheckerService.SuggestionsGatherer; + +import java.util.ArrayList; +import java.util.Locale; + +public abstract class AndroidWordLevelSpellCheckerSession extends Session { + private static final String TAG = AndroidWordLevelSpellCheckerSession.class.getSimpleName(); + private static final boolean DBG = false; + + // Immutable, but need the locale which is not available in the constructor yet + private DictionaryPool mDictionaryPool; + // Likewise + private Locale mLocale; + // Cache this for performance + private int mScript; // One of SCRIPT_LATIN or SCRIPT_CYRILLIC for now. + private final AndroidSpellCheckerService mService; + protected final SuggestionsCache mSuggestionsCache = new SuggestionsCache(); + + private static class SuggestionsParams { + public final String[] mSuggestions; + public final int mFlags; + public SuggestionsParams(String[] suggestions, int flags) { + mSuggestions = suggestions; + mFlags = flags; + } + } + + protected static class SuggestionsCache { + private static final char CHAR_DELIMITER = '\uFFFC'; + private static final int MAX_CACHE_SIZE = 50; + private final LruCache<String, SuggestionsParams> mUnigramSuggestionsInfoCache = + new LruCache<String, SuggestionsParams>(MAX_CACHE_SIZE); + + // TODO: Support n-gram input + private static String generateKey(String query, String prevWord) { + if (TextUtils.isEmpty(query) || TextUtils.isEmpty(prevWord)) { + return query; + } + return query + CHAR_DELIMITER + prevWord; + } + + // TODO: Support n-gram input + public SuggestionsParams getSuggestionsFromCache(String query, String prevWord) { + return mUnigramSuggestionsInfoCache.get(generateKey(query, prevWord)); + } + + // TODO: Support n-gram input + public void putSuggestionsToCache( + String query, String prevWord, String[] suggestions, int flags) { + if (suggestions == null || TextUtils.isEmpty(query)) { + return; + } + mUnigramSuggestionsInfoCache.put( + generateKey(query, prevWord), new SuggestionsParams(suggestions, flags)); + } + } + + AndroidWordLevelSpellCheckerSession(final AndroidSpellCheckerService service) { + mService = service; + } + + @Override + public void onCreate() { + final String localeString = getLocale(); + mDictionaryPool = mService.getDictionaryPool(localeString); + mLocale = LocaleUtils.constructLocaleFromString(localeString); + mScript = AndroidSpellCheckerService.getScriptFromLocale(mLocale); + } + + /* + * Returns whether the code point is a letter that makes sense for the specified + * locale for this spell checker. + * The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml + * and is limited to EFIGS languages and Russian. + * Hence at the moment this explicitly tests for Cyrillic characters or Latin characters + * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters. + */ + private static boolean isLetterCheckableByLanguage(final int codePoint, + final int script) { + switch (script) { + case AndroidSpellCheckerService.SCRIPT_LATIN: + // Our supported latin script dictionaries (EFIGS) at the moment only include + // characters in the C0, C1, Latin Extended A and B, IPA extensions unicode + // blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF, + // so the below is a very efficient way to test for it. As for the 0-0x3F, it's + // excluded from isLetter anyway. + return codePoint <= 0x2AF && Character.isLetter(codePoint); + case AndroidSpellCheckerService.SCRIPT_CYRILLIC: + // All Cyrillic characters are in the 400~52F block. There are some in the upper + // Unicode range, but they are archaic characters that are not used in modern + // russian and are not used by our dictionary. + return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint); + default: + // Should never come here + throw new RuntimeException("Impossible value of script: " + script); + } + } + + /** + * Finds out whether a particular string should be filtered out of spell checking. + * + * This will loosely match URLs, numbers, symbols. To avoid always underlining words that + * we know we will never recognize, this accepts a script identifier that should be one + * of the SCRIPT_* constants defined above, to rule out quickly characters from very + * different languages. + * + * @param text the string to evaluate. + * @param script the identifier for the script this spell checker recognizes + * @return true if we should filter this text out, false otherwise + */ + private static boolean shouldFilterOut(final String text, final int script) { + if (TextUtils.isEmpty(text) || text.length() <= 1) return true; + + // TODO: check if an equivalent processing can't be done more quickly with a + // compiled regexp. + // Filter by first letter + final int firstCodePoint = text.codePointAt(0); + // Filter out words that don't start with a letter or an apostrophe + if (!isLetterCheckableByLanguage(firstCodePoint, script) + && '\'' != firstCodePoint) return true; + + // Filter contents + final int length = text.length(); + int letterCount = 0; + for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) { + final int codePoint = text.codePointAt(i); + // Any word containing a '@' is probably an e-mail address + // Any word containing a '/' is probably either an ad-hoc combination of two + // words or a URI - in either case we don't want to spell check that + if ('@' == codePoint || '/' == codePoint) return true; + if (isLetterCheckableByLanguage(codePoint, script)) ++letterCount; + } + // Guestimate heuristic: perform spell checking if at least 3/4 of the characters + // in this word are letters + return (letterCount * 4 < length * 3); + } + + // Note : this must be reentrant + /** + * Gets a list of suggestions for a specific string. This returns a list of possible + * corrections for the text passed as an argument. It may split or group words, and + * even perform grammatical analysis. + */ + @Override + public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, + final int suggestionsLimit) { + return onGetSuggestions(textInfo, null, suggestionsLimit); + } + + protected SuggestionsInfo onGetSuggestions( + final TextInfo textInfo, final String prevWord, final int suggestionsLimit) { + try { + final String inText = textInfo.getText(); + final SuggestionsParams cachedSuggestionsParams = + mSuggestionsCache.getSuggestionsFromCache(inText, prevWord); + if (cachedSuggestionsParams != null) { + if (DBG) { + Log.d(TAG, "Cache hit: " + inText + ", " + cachedSuggestionsParams.mFlags); + } + return new SuggestionsInfo( + cachedSuggestionsParams.mFlags, cachedSuggestionsParams.mSuggestions); + } + + if (shouldFilterOut(inText, mScript)) { + DictAndProximity dictInfo = null; + try { + dictInfo = mDictionaryPool.takeOrGetNull(); + if (null == dictInfo) { + return AndroidSpellCheckerService.getNotInDictEmptySuggestions(); + } + return dictInfo.mDictionary.isValidWord(inText) + ? AndroidSpellCheckerService.getInDictEmptySuggestions() + : AndroidSpellCheckerService.getNotInDictEmptySuggestions(); + } finally { + if (null != dictInfo) { + if (!mDictionaryPool.offer(dictInfo)) { + Log.e(TAG, "Can't re-insert a dictionary into its pool"); + } + } + } + } + final String text = inText.replaceAll( + AndroidSpellCheckerService.APOSTROPHE, AndroidSpellCheckerService.SINGLE_QUOTE); + + // TODO: Don't gather suggestions if the limit is <= 0 unless necessary + //final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(text, + //mService.mSuggestionThreshold, mService.mRecommendedThreshold, + //suggestionsLimit); + final SuggestionsGatherer suggestionsGatherer = mService.newSuggestionsGatherer( + text, suggestionsLimit); + final WordComposer composer = new WordComposer(); + final int length = text.length(); + for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) { + final int codePoint = text.codePointAt(i); + // The getXYForCodePointAndScript method returns (Y << 16) + X + final int xy = SpellCheckerProximityInfo.getXYForCodePointAndScript( + codePoint, mScript); + if (SpellCheckerProximityInfo.NOT_A_COORDINATE_PAIR == xy) { + composer.add(codePoint, WordComposer.NOT_A_COORDINATE, + WordComposer.NOT_A_COORDINATE); + } else { + composer.add(codePoint, xy & 0xFFFF, xy >> 16); + } + } + + final int capitalizeType = AndroidSpellCheckerService.getCapitalizationType(text); + boolean isInDict = true; + DictAndProximity dictInfo = null; + try { + dictInfo = mDictionaryPool.takeOrGetNull(); + if (null == dictInfo) { + return AndroidSpellCheckerService.getNotInDictEmptySuggestions(); + } + final ArrayList<SuggestedWordInfo> suggestions = + dictInfo.mDictionary.getSuggestions(composer, prevWord, + dictInfo.mProximityInfo); + for (final SuggestedWordInfo suggestion : suggestions) { + final String suggestionStr = suggestion.mWord.toString(); + suggestionsGatherer.addWord(suggestionStr.toCharArray(), null, 0, + suggestionStr.length(), suggestion.mScore); + } + isInDict = dictInfo.mDictionary.isValidWord(text); + if (!isInDict && AndroidSpellCheckerService.CAPITALIZE_NONE != capitalizeType) { + // We want to test the word again if it's all caps or first caps only. + // If it's fully down, we already tested it, if it's mixed case, we don't + // want to test a lowercase version of it. + isInDict = dictInfo.mDictionary.isValidWord(text.toLowerCase(mLocale)); + } + } finally { + if (null != dictInfo) { + if (!mDictionaryPool.offer(dictInfo)) { + Log.e(TAG, "Can't re-insert a dictionary into its pool"); + } + } + } + + final SuggestionsGatherer.Result result = suggestionsGatherer.getResults( + capitalizeType, mLocale); + + if (DBG) { + Log.i(TAG, "Spell checking results for " + text + " with suggestion limit " + + suggestionsLimit); + Log.i(TAG, "IsInDict = " + isInDict); + Log.i(TAG, "LooksLikeTypo = " + (!isInDict)); + Log.i(TAG, "HasRecommendedSuggestions = " + result.mHasRecommendedSuggestions); + if (null != result.mSuggestions) { + for (String suggestion : result.mSuggestions) { + Log.i(TAG, suggestion); + } + } + } + + final int flags = + (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY + : SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) + | (result.mHasRecommendedSuggestions + ? SuggestionsInfoCompatUtils + .getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS() + : 0); + final SuggestionsInfo retval = new SuggestionsInfo(flags, result.mSuggestions); + mSuggestionsCache.putSuggestionsToCache(text, prevWord, result.mSuggestions, flags); + return retval; + } catch (RuntimeException e) { + // Don't kill the keyboard if there is a bug in the spell checker + if (DBG) { + throw e; + } else { + Log.e(TAG, "Exception while spellcheking: " + e); + return AndroidSpellCheckerService.getNotInDictEmptySuggestions(); + } + } + } +} |