diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
160 files changed, 0 insertions, 31435 deletions
diff --git a/java/src/com/android/inputmethod/latin/AssetFileAddress.java b/java/src/com/android/inputmethod/latin/AssetFileAddress.java deleted file mode 100644 index f8d02d6ea..000000000 --- a/java/src/com/android/inputmethod/latin/AssetFileAddress.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import com.android.inputmethod.latin.common.FileUtils; - -import java.io.File; - -/** - * Immutable class to hold the address of an asset. - * As opposed to a normal file, an asset is usually represented as a contiguous byte array in - * the package file. Open it correctly thus requires the name of the package it is in, but - * also the offset in the file and the length of this data. This class encapsulates these three. - */ -public final class AssetFileAddress { - public final String mFilename; - public final long mOffset; - public final long mLength; - - public AssetFileAddress(final String filename, final long offset, final long length) { - mFilename = filename; - mOffset = offset; - mLength = length; - } - - public static AssetFileAddress makeFromFile(final File file) { - if (!file.isFile()) return null; - return new AssetFileAddress(file.getAbsolutePath(), 0L, file.length()); - } - - public static AssetFileAddress makeFromFileName(final String filename) { - if (null == filename) return null; - return makeFromFile(new File(filename)); - } - - public static AssetFileAddress makeFromFileNameAndOffset(final String filename, - final long offset, final long length) { - if (null == filename) return null; - final File f = new File(filename); - if (!f.isFile()) return null; - return new AssetFileAddress(filename, offset, length); - } - - public boolean pointsToPhysicalFile() { - return 0 == mOffset; - } - - public void deleteUnderlyingFile() { - FileUtils.deleteRecursively(new File(mFilename)); - } - - @Override - public String toString() { - return String.format("%s (offset=%d, length=%d)", mFilename, mOffset, mLength); - } -} diff --git a/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java b/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java deleted file mode 100644 index 5e6e4ab25..000000000 --- a/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.Context; -import android.media.AudioManager; -import android.os.Vibrator; -import android.view.HapticFeedbackConstants; -import android.view.View; - -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.settings.SettingsValues; - -/** - * This class gathers audio feedback and haptic feedback functions. - * - * It offers a consistent and simple interface that allows LatinIME to forget about the - * complexity of settings and the like. - */ -public final class AudioAndHapticFeedbackManager { - private AudioManager mAudioManager; - private Vibrator mVibrator; - - private SettingsValues mSettingsValues; - private boolean mSoundOn; - - private static final AudioAndHapticFeedbackManager sInstance = - new AudioAndHapticFeedbackManager(); - - public static AudioAndHapticFeedbackManager getInstance() { - return sInstance; - } - - private AudioAndHapticFeedbackManager() { - // Intentional empty constructor for singleton. - } - - public static void init(final Context context) { - sInstance.initInternal(context); - } - - private void initInternal(final Context context) { - mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); - } - - public void performHapticAndAudioFeedback(final int code, - final View viewToPerformHapticFeedbackOn) { - performHapticFeedback(viewToPerformHapticFeedbackOn); - performAudioFeedback(code); - } - - public boolean hasVibrator() { - return mVibrator != null && mVibrator.hasVibrator(); - } - - public void vibrate(final long milliseconds) { - if (mVibrator == null) { - return; - } - mVibrator.vibrate(milliseconds); - } - - private boolean reevaluateIfSoundIsOn() { - if (mSettingsValues == null || !mSettingsValues.mSoundOn || mAudioManager == null) { - return false; - } - return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL; - } - - public void performAudioFeedback(final int code) { - // if mAudioManager is null, we can't play a sound anyway, so return - if (mAudioManager == null) { - return; - } - if (!mSoundOn) { - return; - } - final int sound; - switch (code) { - case Constants.CODE_DELETE: - sound = AudioManager.FX_KEYPRESS_DELETE; - break; - case Constants.CODE_ENTER: - sound = AudioManager.FX_KEYPRESS_RETURN; - break; - case Constants.CODE_SPACE: - sound = AudioManager.FX_KEYPRESS_SPACEBAR; - break; - default: - sound = AudioManager.FX_KEYPRESS_STANDARD; - break; - } - mAudioManager.playSoundEffect(sound, mSettingsValues.mKeypressSoundVolume); - } - - public void performHapticFeedback(final View viewToPerformHapticFeedbackOn) { - if (!mSettingsValues.mVibrateOn) { - return; - } - if (mSettingsValues.mKeypressVibrationDuration >= 0) { - vibrate(mSettingsValues.mKeypressVibrationDuration); - return; - } - // Go ahead with the system default - if (viewToPerformHapticFeedbackOn != null) { - viewToPerformHapticFeedbackOn.performHapticFeedback( - HapticFeedbackConstants.KEYBOARD_TAP); - } - } - - public void onSettingsChanged(final SettingsValues settingsValues) { - mSettingsValues = settingsValues; - mSoundOn = reevaluateIfSoundIsOn(); - } - - public void onRingerModeChanged() { - mSoundOn = reevaluateIfSoundIsOn(); - } -} diff --git a/java/src/com/android/inputmethod/latin/BackupAgent.java b/java/src/com/android/inputmethod/latin/BackupAgent.java deleted file mode 100644 index b2d92b30c..000000000 --- a/java/src/com/android/inputmethod/latin/BackupAgent.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2008 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.app.backup.BackupAgentHelper; -import android.app.backup.BackupDataInput; -import android.app.backup.SharedPreferencesBackupHelper; -import android.content.SharedPreferences; -import android.os.ParcelFileDescriptor; - -import com.android.inputmethod.latin.settings.LocalSettingsConstants; - -import java.io.IOException; - -/** - * Backup/restore agent for LatinIME. - * Currently it backs up the default shared preferences. - */ -public final class BackupAgent extends BackupAgentHelper { - private static final String PREF_SUFFIX = "_preferences"; - - @Override - public void onCreate() { - addHelper("shared_pref", new SharedPreferencesBackupHelper(this, - getPackageName() + PREF_SUFFIX)); - } - - @Override - public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) - throws IOException { - // Let the restore operation go through - super.onRestore(data, appVersionCode, newState); - - // Remove the preferences that we don't want restored. - final SharedPreferences.Editor prefEditor = getSharedPreferences( - getPackageName() + PREF_SUFFIX, MODE_PRIVATE).edit(); - for (final String key : LocalSettingsConstants.PREFS_TO_SKIP_RESTORING) { - prefEditor.remove(key); - } - // Flush the changes to disk. - prefEditor.commit(); - } -} diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java deleted file mode 100644 index 9a3ac674e..000000000 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ /dev/null @@ -1,669 +0,0 @@ -/* - * Copyright (C) 2008 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.text.TextUtils; -import android.util.Log; -import android.util.SparseArray; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.FileUtils; -import com.android.inputmethod.latin.common.InputPointers; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.makedict.DictionaryHeader; -import com.android.inputmethod.latin.makedict.FormatSpec; -import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions; -import com.android.inputmethod.latin.makedict.UnsupportedFormatException; -import com.android.inputmethod.latin.makedict.WordProperty; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; -import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; -import com.android.inputmethod.latin.utils.JniUtils; -import com.android.inputmethod.latin.utils.WordInputEventForPersonalization; - -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import javax.annotation.Nonnull; - -/** - * Implements a static, compacted, binary dictionary of standard words. - */ -// TODO: All methods which should be locked need to have a suffix "Locked". -public final class BinaryDictionary extends Dictionary { - private static final String TAG = BinaryDictionary.class.getSimpleName(); - - // The cutoff returned by native for auto-commit confidence. - // Must be equal to CONFIDENCE_TO_AUTO_COMMIT in native/jni/src/defines.h - private static final int CONFIDENCE_TO_AUTO_COMMIT = 1000000; - - public static final int DICTIONARY_MAX_WORD_LENGTH = 48; - public static final int MAX_PREV_WORD_COUNT_FOR_N_GRAM = 3; - - @UsedForTesting - public static final String UNIGRAM_COUNT_QUERY = "UNIGRAM_COUNT"; - @UsedForTesting - public static final String BIGRAM_COUNT_QUERY = "BIGRAM_COUNT"; - @UsedForTesting - public static final String MAX_UNIGRAM_COUNT_QUERY = "MAX_UNIGRAM_COUNT"; - @UsedForTesting - public static final String MAX_BIGRAM_COUNT_QUERY = "MAX_BIGRAM_COUNT"; - - public static final int NOT_A_VALID_TIMESTAMP = -1; - - // Format to get unigram flags from native side via getWordPropertyNative(). - private static final int FORMAT_WORD_PROPERTY_OUTPUT_FLAG_COUNT = 5; - private static final int FORMAT_WORD_PROPERTY_IS_NOT_A_WORD_INDEX = 0; - private static final int FORMAT_WORD_PROPERTY_IS_POSSIBLY_OFFENSIVE_INDEX = 1; - private static final int FORMAT_WORD_PROPERTY_HAS_NGRAMS_INDEX = 2; - private static final int FORMAT_WORD_PROPERTY_HAS_SHORTCUTS_INDEX = 3; // DEPRECATED - private static final int FORMAT_WORD_PROPERTY_IS_BEGINNING_OF_SENTENCE_INDEX = 4; - - // Format to get probability and historical info from native side via getWordPropertyNative(). - public static final int FORMAT_WORD_PROPERTY_OUTPUT_PROBABILITY_INFO_COUNT = 4; - public static final int FORMAT_WORD_PROPERTY_PROBABILITY_INDEX = 0; - public static final int FORMAT_WORD_PROPERTY_TIMESTAMP_INDEX = 1; - public static final int FORMAT_WORD_PROPERTY_LEVEL_INDEX = 2; - public static final int FORMAT_WORD_PROPERTY_COUNT_INDEX = 3; - - public static final String DICT_FILE_NAME_SUFFIX_FOR_MIGRATION = ".migrate"; - public static final String DIR_NAME_SUFFIX_FOR_RECORD_MIGRATION = ".migrating"; - - private long mNativeDict; - private final long mDictSize; - private final String mDictFilePath; - private final boolean mUseFullEditDistance; - private final boolean mIsUpdatable; - private boolean mHasUpdated; - - private final SparseArray<DicTraverseSession> mDicTraverseSessions = new SparseArray<>(); - - // TODO: There should be a way to remove used DicTraverseSession objects from - // {@code mDicTraverseSessions}. - private DicTraverseSession getTraverseSession(final int traverseSessionId) { - synchronized(mDicTraverseSessions) { - DicTraverseSession traverseSession = mDicTraverseSessions.get(traverseSessionId); - if (traverseSession == null) { - traverseSession = new DicTraverseSession(mLocale, mNativeDict, mDictSize); - mDicTraverseSessions.put(traverseSessionId, traverseSession); - } - return traverseSession; - } - } - - /** - * Constructs binary dictionary using existing dictionary file. - * @param filename the name of the file to read through native code. - * @param offset the offset of the dictionary data within the file. - * @param length the length of the binary data. - * @param useFullEditDistance whether to use the full edit distance in suggestions - * @param dictType the dictionary type, as a human-readable string - * @param isUpdatable whether to open the dictionary file in writable mode. - */ - public BinaryDictionary(final String filename, final long offset, final long length, - final boolean useFullEditDistance, final Locale locale, final String dictType, - final boolean isUpdatable) { - super(dictType, locale); - mDictSize = length; - mDictFilePath = filename; - mIsUpdatable = isUpdatable; - mHasUpdated = false; - mUseFullEditDistance = useFullEditDistance; - loadDictionary(filename, offset, length, isUpdatable); - } - - /** - * Constructs binary dictionary on memory. - * @param filename the name of the file used to flush. - * @param useFullEditDistance whether to use the full edit distance in suggestions - * @param dictType the dictionary type, as a human-readable string - * @param formatVersion the format version of the dictionary - * @param attributeMap the attributes of the dictionary - */ - public BinaryDictionary(final String filename, final boolean useFullEditDistance, - final Locale locale, final String dictType, final long formatVersion, - final Map<String, String> attributeMap) { - super(dictType, locale); - mDictSize = 0; - mDictFilePath = filename; - // On memory dictionary is always updatable. - mIsUpdatable = true; - mHasUpdated = false; - mUseFullEditDistance = useFullEditDistance; - final String[] keyArray = new String[attributeMap.size()]; - final String[] valueArray = new String[attributeMap.size()]; - int index = 0; - for (final String key : attributeMap.keySet()) { - keyArray[index] = key; - valueArray[index] = attributeMap.get(key); - index++; - } - mNativeDict = createOnMemoryNative(formatVersion, locale.toString(), keyArray, valueArray); - } - - - static { - JniUtils.loadNativeLibrary(); - } - - private static native long openNative(String sourceDir, long dictOffset, long dictSize, - boolean isUpdatable); - private static native long createOnMemoryNative(long formatVersion, - String locale, String[] attributeKeyStringArray, String[] attributeValueStringArray); - private static native void getHeaderInfoNative(long dict, int[] outHeaderSize, - int[] outFormatVersion, ArrayList<int[]> outAttributeKeys, - ArrayList<int[]> outAttributeValues); - private static native boolean flushNative(long dict, String filePath); - private static native boolean needsToRunGCNative(long dict, boolean mindsBlockByGC); - private static native boolean flushWithGCNative(long dict, String filePath); - private static native void closeNative(long dict); - private static native int getFormatVersionNative(long dict); - private static native int getProbabilityNative(long dict, int[] word); - private static native int getMaxProbabilityOfExactMatchesNative(long dict, int[] word); - private static native int getNgramProbabilityNative(long dict, int[][] prevWordCodePointArrays, - boolean[] isBeginningOfSentenceArray, int[] word); - private static native void getWordPropertyNative(long dict, int[] word, - boolean isBeginningOfSentence, int[] outCodePoints, boolean[] outFlags, - int[] outProbabilityInfo, ArrayList<int[][]> outNgramPrevWordsArray, - ArrayList<boolean[]> outNgramPrevWordIsBeginningOfSentenceArray, - ArrayList<int[]> outNgramTargets, ArrayList<int[]> outNgramProbabilityInfo, - ArrayList<int[]> outShortcutTargets, ArrayList<Integer> outShortcutProbabilities); - private static native int getNextWordNative(long dict, int token, int[] outCodePoints, - boolean[] outIsBeginningOfSentence); - private static native void getSuggestionsNative(long dict, long proximityInfo, - long traverseSession, int[] xCoordinates, int[] yCoordinates, int[] times, - int[] pointerIds, int[] inputCodePoints, int inputSize, int[] suggestOptions, - int[][] prevWordCodePointArrays, boolean[] isBeginningOfSentenceArray, - int prevWordCount, int[] outputSuggestionCount, int[] outputCodePoints, - int[] outputScores, int[] outputIndices, int[] outputTypes, - int[] outputAutoCommitFirstWordConfidence, - float[] inOutWeightOfLangModelVsSpatialModel); - private static native boolean addUnigramEntryNative(long dict, int[] word, int probability, - int[] shortcutTarget, int shortcutProbability, boolean isBeginningOfSentence, - boolean isNotAWord, boolean isPossiblyOffensive, int timestamp); - private static native boolean removeUnigramEntryNative(long dict, int[] word); - private static native boolean addNgramEntryNative(long dict, - int[][] prevWordCodePointArrays, boolean[] isBeginningOfSentenceArray, - int[] word, int probability, int timestamp); - private static native boolean removeNgramEntryNative(long dict, - int[][] prevWordCodePointArrays, boolean[] isBeginningOfSentenceArray, int[] word); - private static native boolean updateEntriesForWordWithNgramContextNative(long dict, - int[][] prevWordCodePointArrays, boolean[] isBeginningOfSentenceArray, - int[] word, boolean isValidWord, int count, int timestamp); - private static native int updateEntriesForInputEventsNative(long dict, - WordInputEventForPersonalization[] inputEvents, int startIndex); - private static native String getPropertyNative(long dict, String query); - private static native boolean isCorruptedNative(long dict); - private static native boolean migrateNative(long dict, String dictFilePath, - long newFormatVersion); - - // TODO: Move native dict into session - private void loadDictionary(final String path, final long startOffset, - final long length, final boolean isUpdatable) { - mHasUpdated = false; - mNativeDict = openNative(path, startOffset, length, isUpdatable); - } - - // TODO: Check isCorrupted() for main dictionaries. - public boolean isCorrupted() { - if (!isValidDictionary()) { - return false; - } - if (!isCorruptedNative(mNativeDict)) { - return false; - } - // TODO: Record the corruption. - Log.e(TAG, "BinaryDictionary (" + mDictFilePath + ") is corrupted."); - Log.e(TAG, "locale: " + mLocale); - Log.e(TAG, "dict size: " + mDictSize); - Log.e(TAG, "updatable: " + mIsUpdatable); - return true; - } - - public DictionaryHeader getHeader() throws UnsupportedFormatException { - if (mNativeDict == 0) { - return null; - } - final int[] outHeaderSize = new int[1]; - final int[] outFormatVersion = new int[1]; - final ArrayList<int[]> outAttributeKeys = new ArrayList<>(); - final ArrayList<int[]> outAttributeValues = new ArrayList<>(); - getHeaderInfoNative(mNativeDict, outHeaderSize, outFormatVersion, outAttributeKeys, - outAttributeValues); - final HashMap<String, String> attributes = new HashMap<>(); - for (int i = 0; i < outAttributeKeys.size(); i++) { - final String attributeKey = StringUtils.getStringFromNullTerminatedCodePointArray( - outAttributeKeys.get(i)); - final String attributeValue = StringUtils.getStringFromNullTerminatedCodePointArray( - outAttributeValues.get(i)); - attributes.put(attributeKey, attributeValue); - } - final boolean hasHistoricalInfo = DictionaryHeader.ATTRIBUTE_VALUE_TRUE.equals( - attributes.get(DictionaryHeader.HAS_HISTORICAL_INFO_KEY)); - return new DictionaryHeader(outHeaderSize[0], new DictionaryOptions(attributes), - new FormatSpec.FormatOptions(outFormatVersion[0], hasHistoricalInfo)); - } - - @Override - public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData, - final NgramContext ngramContext, final long proximityInfoHandle, - final SettingsValuesForSuggestion settingsValuesForSuggestion, - final int sessionId, final float weightForLocale, - final float[] inOutWeightOfLangModelVsSpatialModel) { - if (!isValidDictionary()) { - return null; - } - final DicTraverseSession session = getTraverseSession(sessionId); - Arrays.fill(session.mInputCodePoints, Constants.NOT_A_CODE); - ngramContext.outputToArray(session.mPrevWordCodePointArrays, - session.mIsBeginningOfSentenceArray); - final InputPointers inputPointers = composedData.mInputPointers; - final boolean isGesture = composedData.mIsBatchMode; - final int inputSize; - if (!isGesture) { - inputSize = - composedData.copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( - session.mInputCodePoints); - if (inputSize < 0) { - return null; - } - } else { - inputSize = inputPointers.getPointerSize(); - } - session.mNativeSuggestOptions.setUseFullEditDistance(mUseFullEditDistance); - session.mNativeSuggestOptions.setIsGesture(isGesture); - session.mNativeSuggestOptions.setBlockOffensiveWords( - settingsValuesForSuggestion.mBlockPotentiallyOffensive); - session.mNativeSuggestOptions.setWeightForLocale(weightForLocale); - if (inOutWeightOfLangModelVsSpatialModel != null) { - session.mInputOutputWeightOfLangModelVsSpatialModel[0] = - inOutWeightOfLangModelVsSpatialModel[0]; - } else { - session.mInputOutputWeightOfLangModelVsSpatialModel[0] = - Dictionary.NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL; - } - // TOOD: Pass multiple previous words information for n-gram. - getSuggestionsNative(mNativeDict, proximityInfoHandle, - getTraverseSession(sessionId).getSession(), inputPointers.getXCoordinates(), - inputPointers.getYCoordinates(), inputPointers.getTimes(), - inputPointers.getPointerIds(), session.mInputCodePoints, inputSize, - session.mNativeSuggestOptions.getOptions(), session.mPrevWordCodePointArrays, - session.mIsBeginningOfSentenceArray, ngramContext.getPrevWordCount(), - session.mOutputSuggestionCount, session.mOutputCodePoints, session.mOutputScores, - session.mSpaceIndices, session.mOutputTypes, - session.mOutputAutoCommitFirstWordConfidence, - session.mInputOutputWeightOfLangModelVsSpatialModel); - if (inOutWeightOfLangModelVsSpatialModel != null) { - inOutWeightOfLangModelVsSpatialModel[0] = - session.mInputOutputWeightOfLangModelVsSpatialModel[0]; - } - final int count = session.mOutputSuggestionCount[0]; - final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<>(); - for (int j = 0; j < count; ++j) { - final int start = j * DICTIONARY_MAX_WORD_LENGTH; - int len = 0; - while (len < DICTIONARY_MAX_WORD_LENGTH - && session.mOutputCodePoints[start + len] != 0) { - ++len; - } - if (len > 0) { - suggestions.add(new SuggestedWordInfo( - new String(session.mOutputCodePoints, start, len), - "" /* prevWordsContext */, - (int)(session.mOutputScores[j] * weightForLocale), - session.mOutputTypes[j], - this /* sourceDict */, - session.mSpaceIndices[j] /* indexOfTouchPointOfSecondWord */, - session.mOutputAutoCommitFirstWordConfidence[0])); - } - } - return suggestions; - } - - public boolean isValidDictionary() { - return mNativeDict != 0; - } - - public int getFormatVersion() { - return getFormatVersionNative(mNativeDict); - } - - @Override - public boolean isInDictionary(final String word) { - return getFrequency(word) != NOT_A_PROBABILITY; - } - - @Override - public int getFrequency(final String word) { - if (TextUtils.isEmpty(word)) { - return NOT_A_PROBABILITY; - } - final int[] codePoints = StringUtils.toCodePointArray(word); - return getProbabilityNative(mNativeDict, codePoints); - } - - @Override - public int getMaxFrequencyOfExactMatches(final String word) { - if (TextUtils.isEmpty(word)) { - return NOT_A_PROBABILITY; - } - final int[] codePoints = StringUtils.toCodePointArray(word); - return getMaxProbabilityOfExactMatchesNative(mNativeDict, codePoints); - } - - @UsedForTesting - public boolean isValidNgram(final NgramContext ngramContext, final String word) { - return getNgramProbability(ngramContext, word) != NOT_A_PROBABILITY; - } - - public int getNgramProbability(final NgramContext ngramContext, final String word) { - if (!ngramContext.isValid() || TextUtils.isEmpty(word)) { - return NOT_A_PROBABILITY; - } - final int[][] prevWordCodePointArrays = new int[ngramContext.getPrevWordCount()][]; - final boolean[] isBeginningOfSentenceArray = new boolean[ngramContext.getPrevWordCount()]; - ngramContext.outputToArray(prevWordCodePointArrays, isBeginningOfSentenceArray); - final int[] wordCodePoints = StringUtils.toCodePointArray(word); - return getNgramProbabilityNative(mNativeDict, prevWordCodePointArrays, - isBeginningOfSentenceArray, wordCodePoints); - } - - public WordProperty getWordProperty(final String word, final boolean isBeginningOfSentence) { - if (word == null) { - return null; - } - final int[] codePoints = StringUtils.toCodePointArray(word); - final int[] outCodePoints = new int[DICTIONARY_MAX_WORD_LENGTH]; - final boolean[] outFlags = new boolean[FORMAT_WORD_PROPERTY_OUTPUT_FLAG_COUNT]; - final int[] outProbabilityInfo = - new int[FORMAT_WORD_PROPERTY_OUTPUT_PROBABILITY_INFO_COUNT]; - final ArrayList<int[][]> outNgramPrevWordsArray = new ArrayList<>(); - final ArrayList<boolean[]> outNgramPrevWordIsBeginningOfSentenceArray = - new ArrayList<>(); - final ArrayList<int[]> outNgramTargets = new ArrayList<>(); - final ArrayList<int[]> outNgramProbabilityInfo = new ArrayList<>(); - final ArrayList<int[]> outShortcutTargets = new ArrayList<>(); - final ArrayList<Integer> outShortcutProbabilities = new ArrayList<>(); - getWordPropertyNative(mNativeDict, codePoints, isBeginningOfSentence, outCodePoints, - outFlags, outProbabilityInfo, outNgramPrevWordsArray, - outNgramPrevWordIsBeginningOfSentenceArray, outNgramTargets, - outNgramProbabilityInfo, outShortcutTargets, outShortcutProbabilities); - return new WordProperty(codePoints, - outFlags[FORMAT_WORD_PROPERTY_IS_NOT_A_WORD_INDEX], - outFlags[FORMAT_WORD_PROPERTY_IS_POSSIBLY_OFFENSIVE_INDEX], - outFlags[FORMAT_WORD_PROPERTY_HAS_NGRAMS_INDEX], - outFlags[FORMAT_WORD_PROPERTY_IS_BEGINNING_OF_SENTENCE_INDEX], outProbabilityInfo, - outNgramPrevWordsArray, outNgramPrevWordIsBeginningOfSentenceArray, - outNgramTargets, outNgramProbabilityInfo); - } - - public static class GetNextWordPropertyResult { - public WordProperty mWordProperty; - public int mNextToken; - - public GetNextWordPropertyResult(final WordProperty wordProperty, final int nextToken) { - mWordProperty = wordProperty; - mNextToken = nextToken; - } - } - - /** - * Method to iterate all words in the dictionary for makedict. - * If token is 0, this method newly starts iterating the dictionary. - */ - public GetNextWordPropertyResult getNextWordProperty(final int token) { - final int[] codePoints = new int[DICTIONARY_MAX_WORD_LENGTH]; - final boolean[] isBeginningOfSentence = new boolean[1]; - final int nextToken = getNextWordNative(mNativeDict, token, codePoints, - isBeginningOfSentence); - final String word = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints); - return new GetNextWordPropertyResult( - getWordProperty(word, isBeginningOfSentence[0]), nextToken); - } - - // Add a unigram entry to binary dictionary with unigram attributes in native code. - public boolean addUnigramEntry( - final String word, final int probability, final boolean isBeginningOfSentence, - final boolean isNotAWord, final boolean isPossiblyOffensive, final int timestamp) { - if (word == null || (word.isEmpty() && !isBeginningOfSentence)) { - return false; - } - final int[] codePoints = StringUtils.toCodePointArray(word); - if (!addUnigramEntryNative(mNativeDict, codePoints, probability, - null /* shortcutTargetCodePoints */, 0 /* shortcutProbability */, - isBeginningOfSentence, isNotAWord, isPossiblyOffensive, timestamp)) { - return false; - } - mHasUpdated = true; - return true; - } - - // Remove a unigram entry from the binary dictionary in native code. - public boolean removeUnigramEntry(final String word) { - if (TextUtils.isEmpty(word)) { - return false; - } - final int[] codePoints = StringUtils.toCodePointArray(word); - if (!removeUnigramEntryNative(mNativeDict, codePoints)) { - return false; - } - mHasUpdated = true; - return true; - } - - // Add an n-gram entry to the binary dictionary with timestamp in native code. - public boolean addNgramEntry(final NgramContext ngramContext, final String word, - final int probability, final int timestamp) { - if (!ngramContext.isValid() || TextUtils.isEmpty(word)) { - return false; - } - final int[][] prevWordCodePointArrays = new int[ngramContext.getPrevWordCount()][]; - final boolean[] isBeginningOfSentenceArray = new boolean[ngramContext.getPrevWordCount()]; - ngramContext.outputToArray(prevWordCodePointArrays, isBeginningOfSentenceArray); - final int[] wordCodePoints = StringUtils.toCodePointArray(word); - if (!addNgramEntryNative(mNativeDict, prevWordCodePointArrays, - isBeginningOfSentenceArray, wordCodePoints, probability, timestamp)) { - return false; - } - mHasUpdated = true; - return true; - } - - // Update entries for the word occurrence with the ngramContext. - public boolean updateEntriesForWordWithNgramContext(@Nonnull final NgramContext ngramContext, - final String word, final boolean isValidWord, final int count, final int timestamp) { - if (TextUtils.isEmpty(word)) { - return false; - } - final int[][] prevWordCodePointArrays = new int[ngramContext.getPrevWordCount()][]; - final boolean[] isBeginningOfSentenceArray = new boolean[ngramContext.getPrevWordCount()]; - ngramContext.outputToArray(prevWordCodePointArrays, isBeginningOfSentenceArray); - final int[] wordCodePoints = StringUtils.toCodePointArray(word); - if (!updateEntriesForWordWithNgramContextNative(mNativeDict, prevWordCodePointArrays, - isBeginningOfSentenceArray, wordCodePoints, isValidWord, count, timestamp)) { - return false; - } - mHasUpdated = true; - return true; - } - - @UsedForTesting - public void updateEntriesForInputEvents(final WordInputEventForPersonalization[] inputEvents) { - if (!isValidDictionary()) { - return; - } - int processedEventCount = 0; - while (processedEventCount < inputEvents.length) { - if (needsToRunGC(true /* mindsBlockByGC */)) { - flushWithGC(); - } - processedEventCount = updateEntriesForInputEventsNative(mNativeDict, inputEvents, - processedEventCount); - mHasUpdated = true; - if (processedEventCount <= 0) { - return; - } - } - } - - private void reopen() { - close(); - final File dictFile = new File(mDictFilePath); - // WARNING: Because we pass 0 as the offset and file.length() as the length, this can - // only be called for actual files. Right now it's only called by the flush() family of - // functions, which require an updatable dictionary, so it's okay. But beware. - loadDictionary(dictFile.getAbsolutePath(), 0 /* startOffset */, - dictFile.length(), mIsUpdatable); - } - - // Flush to dict file if the dictionary has been updated. - public boolean flush() { - if (!isValidDictionary()) { - return false; - } - if (mHasUpdated) { - if (!flushNative(mNativeDict, mDictFilePath)) { - return false; - } - reopen(); - } - return true; - } - - // Run GC and flush to dict file if the dictionary has been updated. - public boolean flushWithGCIfHasUpdated() { - if (mHasUpdated) { - return flushWithGC(); - } - return true; - } - - // Run GC and flush to dict file. - public boolean flushWithGC() { - if (!isValidDictionary()) { - return false; - } - if (!flushWithGCNative(mNativeDict, mDictFilePath)) { - return false; - } - reopen(); - return true; - } - - /** - * Checks whether GC is needed to run or not. - * @param mindsBlockByGC Whether to mind operations blocked by GC. We don't need to care about - * the blocking in some situations such as in idle time or just before closing. - * @return whether GC is needed to run or not. - */ - public boolean needsToRunGC(final boolean mindsBlockByGC) { - if (!isValidDictionary()) { - return false; - } - return needsToRunGCNative(mNativeDict, mindsBlockByGC); - } - - public boolean migrateTo(final int newFormatVersion) { - if (!isValidDictionary()) { - return false; - } - final File isMigratingDir = - new File(mDictFilePath + DIR_NAME_SUFFIX_FOR_RECORD_MIGRATION); - if (isMigratingDir.exists()) { - isMigratingDir.delete(); - Log.e(TAG, "Previous migration attempt failed probably due to a crash. " - + "Giving up using the old dictionary (" + mDictFilePath + ")."); - return false; - } - if (!isMigratingDir.mkdir()) { - Log.e(TAG, "Cannot create a dir (" + isMigratingDir.getAbsolutePath() - + ") to record migration."); - return false; - } - try { - final String tmpDictFilePath = mDictFilePath + DICT_FILE_NAME_SUFFIX_FOR_MIGRATION; - if (!migrateNative(mNativeDict, tmpDictFilePath, newFormatVersion)) { - return false; - } - close(); - final File dictFile = new File(mDictFilePath); - final File tmpDictFile = new File(tmpDictFilePath); - if (!FileUtils.deleteRecursively(dictFile)) { - return false; - } - if (!BinaryDictionaryUtils.renameDict(tmpDictFile, dictFile)) { - return false; - } - loadDictionary(dictFile.getAbsolutePath(), 0 /* startOffset */, - dictFile.length(), mIsUpdatable); - return true; - } finally { - isMigratingDir.delete(); - } - } - - @UsedForTesting - public String getPropertyForGettingStats(final String query) { - if (!isValidDictionary()) { - return ""; - } - return getPropertyNative(mNativeDict, query); - } - - @Override - public boolean shouldAutoCommit(final SuggestedWordInfo candidate) { - return candidate.mAutoCommitFirstWordConfidence > CONFIDENCE_TO_AUTO_COMMIT; - } - - @Override - public void close() { - synchronized (mDicTraverseSessions) { - final int sessionsSize = mDicTraverseSessions.size(); - for (int index = 0; index < sessionsSize; ++index) { - final DicTraverseSession traverseSession = mDicTraverseSessions.valueAt(index); - if (traverseSession != null) { - traverseSession.close(); - } - } - mDicTraverseSessions.clear(); - } - closeInternalLocked(); - } - - private synchronized void closeInternalLocked() { - if (mNativeDict != 0) { - closeNative(mNativeDict); - mNativeDict = 0; - } - } - - // TODO: Manage BinaryDictionary instances without using WeakReference or something. - @Override - protected void finalize() throws Throwable { - try { - closeInternalLocked(); - } finally { - super.finalize(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java deleted file mode 100644 index 648610c86..000000000 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ /dev/null @@ -1,569 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import android.content.ContentProviderClient; -import android.content.ContentResolver; -import android.content.ContentValues; -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.database.Cursor; -import android.net.Uri; -import android.os.RemoteException; -import android.text.TextUtils; -import android.util.Log; - -import com.android.inputmethod.dictionarypack.DictionaryPackConstants; -import com.android.inputmethod.dictionarypack.MD5Calculator; -import com.android.inputmethod.dictionarypack.UpdateHandler; -import com.android.inputmethod.latin.common.FileUtils; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; -import com.android.inputmethod.latin.utils.DictionaryInfoUtils; -import com.android.inputmethod.latin.utils.DictionaryInfoUtils.DictionaryInfo; -import com.android.inputmethod.latin.utils.FileTransforms; -import com.android.inputmethod.latin.utils.MetadataFileUriGetter; - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.Closeable; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Locale; - -/** - * Group class for static methods to help with creation and getting of the binary dictionary - * file from the dictionary provider - */ -public final class BinaryDictionaryFileDumper { - private static final String TAG = BinaryDictionaryFileDumper.class.getSimpleName(); - private static final boolean DEBUG = false; - - /** - * The size of the temporary buffer to copy files. - */ - private static final int FILE_READ_BUFFER_SIZE = 8192; - // TODO: make the following data common with the native code - private static final byte[] MAGIC_NUMBER_VERSION_1 = - new byte[] { (byte)0x78, (byte)0xB1, (byte)0x00, (byte)0x00 }; - private static final byte[] MAGIC_NUMBER_VERSION_2 = - new byte[] { (byte)0x9B, (byte)0xC1, (byte)0x3A, (byte)0xFE }; - - private static final boolean SHOULD_VERIFY_MAGIC_NUMBER = - DecoderSpecificConstants.SHOULD_VERIFY_MAGIC_NUMBER; - private static final boolean SHOULD_VERIFY_CHECKSUM = - DecoderSpecificConstants.SHOULD_VERIFY_CHECKSUM; - - private static final String DICTIONARY_PROJECTION[] = { "id" }; - - private static final String QUERY_PARAMETER_MAY_PROMPT_USER = "mayPrompt"; - private static final String QUERY_PARAMETER_TRUE = "true"; - private static final String QUERY_PARAMETER_DELETE_RESULT = "result"; - private static final String QUERY_PARAMETER_SUCCESS = "success"; - private static final String QUERY_PARAMETER_FAILURE = "failure"; - - // Using protocol version 2 to communicate with the dictionary pack - private static final String QUERY_PARAMETER_PROTOCOL = "protocol"; - private static final String QUERY_PARAMETER_PROTOCOL_VALUE = "2"; - - // The path fragment to append after the client ID for dictionary info requests. - private static final String QUERY_PATH_DICT_INFO = "dict"; - // The path fragment to append after the client ID for dictionary datafile requests. - private static final String QUERY_PATH_DATAFILE = "datafile"; - // The path fragment to append after the client ID for updating the metadata URI. - private static final String QUERY_PATH_METADATA = "metadata"; - private static final String INSERT_METADATA_CLIENT_ID_COLUMN = "clientid"; - private static final String INSERT_METADATA_METADATA_URI_COLUMN = "uri"; - private static final String INSERT_METADATA_METADATA_ADDITIONAL_ID_COLUMN = "additionalid"; - - // Prevents this class to be accidentally instantiated. - private BinaryDictionaryFileDumper() { - } - - /** - * Returns a URI builder pointing to the dictionary pack. - * - * This creates a URI builder able to build a URI pointing to the dictionary - * pack content provider for a specific dictionary id. - */ - public static Uri.Builder getProviderUriBuilder(final String path) { - return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) - .authority(DictionaryPackConstants.AUTHORITY).appendPath(path); - } - - /** - * Gets the content URI builder for a specified type. - * - * Supported types include QUERY_PATH_DICT_INFO, which takes the locale as - * the extraPath argument, and QUERY_PATH_DATAFILE, which needs a wordlist ID - * as the extraPath argument. - * - * @param clientId the clientId to use - * @param contentProviderClient the instance of content provider client - * @param queryPathType the path element encoding the type - * @param extraPath optional extra argument for this type (typically word list id) - * @return a builder that can build the URI for the best supported protocol version - * @throws RemoteException if the client can't be contacted - */ - private static Uri.Builder getContentUriBuilderForType(final String clientId, - final ContentProviderClient contentProviderClient, final String queryPathType, - final String extraPath) throws RemoteException { - // Check whether protocol v2 is supported by building a v2 URI and calling getType() - // on it. If this returns null, v2 is not supported. - final Uri.Builder uriV2Builder = getProviderUriBuilder(clientId); - uriV2Builder.appendPath(queryPathType); - uriV2Builder.appendPath(extraPath); - uriV2Builder.appendQueryParameter(QUERY_PARAMETER_PROTOCOL, - QUERY_PARAMETER_PROTOCOL_VALUE); - if (null != contentProviderClient.getType(uriV2Builder.build())) return uriV2Builder; - // Protocol v2 is not supported, so create and return the protocol v1 uri. - return getProviderUriBuilder(extraPath); - } - - /** - * Queries a content provider for the list of word lists for a specific locale - * available to copy into Latin IME. - */ - private static List<WordListInfo> getWordListWordListInfos(final Locale locale, - final Context context, final boolean hasDefaultWordList) { - final String clientId = context.getString(R.string.dictionary_pack_client_id); - final ContentProviderClient client = context.getContentResolver(). - acquireContentProviderClient(getProviderUriBuilder("").build()); - if (null == client) return Collections.<WordListInfo>emptyList(); - Cursor cursor = null; - try { - final Uri.Builder builder = getContentUriBuilderForType(clientId, client, - QUERY_PATH_DICT_INFO, locale.toString()); - if (!hasDefaultWordList) { - builder.appendQueryParameter(QUERY_PARAMETER_MAY_PROMPT_USER, - QUERY_PARAMETER_TRUE); - } - final Uri queryUri = builder.build(); - final boolean isProtocolV2 = (QUERY_PARAMETER_PROTOCOL_VALUE.equals( - queryUri.getQueryParameter(QUERY_PARAMETER_PROTOCOL))); - - cursor = client.query(queryUri, DICTIONARY_PROJECTION, null, null, null); - if (isProtocolV2 && null == cursor) { - reinitializeClientRecordInDictionaryContentProvider(context, client, clientId); - cursor = client.query(queryUri, DICTIONARY_PROJECTION, null, null, null); - } - if (null == cursor) return Collections.<WordListInfo>emptyList(); - if (cursor.getCount() <= 0 || !cursor.moveToFirst()) { - return Collections.<WordListInfo>emptyList(); - } - final ArrayList<WordListInfo> list = new ArrayList<>(); - do { - final String wordListId = cursor.getString(0); - final String wordListLocale = cursor.getString(1); - final String wordListRawChecksum = cursor.getString(2); - if (TextUtils.isEmpty(wordListId)) continue; - list.add(new WordListInfo(wordListId, wordListLocale, wordListRawChecksum)); - } while (cursor.moveToNext()); - return list; - } catch (RemoteException e) { - // The documentation is unclear as to in which cases this may happen, but it probably - // happens when the content provider got suddenly killed because it crashed or because - // the user disabled it through Settings. - Log.e(TAG, "RemoteException: communication with the dictionary pack cut", e); - return Collections.<WordListInfo>emptyList(); - } catch (Exception e) { - // A crash here is dangerous because crashing here would brick any encrypted device - - // we need the keyboard to be up and working to enter the password, so we don't want - // to die no matter what. So let's be as safe as possible. - Log.e(TAG, "Unexpected exception communicating with the dictionary pack", e); - return Collections.<WordListInfo>emptyList(); - } finally { - if (null != cursor) { - cursor.close(); - } - client.release(); - } - } - - - /** - * Helper method to encapsulate exception handling. - */ - private static AssetFileDescriptor openAssetFileDescriptor( - final ContentProviderClient providerClient, final Uri uri) { - try { - return providerClient.openAssetFile(uri, "r"); - } catch (FileNotFoundException e) { - // I don't want to log the word list URI here for security concerns. The exception - // contains the name of the file, so let's not pass it to Log.e here. - Log.e(TAG, "Could not find a word list from the dictionary provider." - /* intentionally don't pass the exception (see comment above) */); - return null; - } catch (RemoteException e) { - Log.e(TAG, "Can't communicate with the dictionary pack", e); - return null; - } - } - - /** - * Stages a word list the id of which is passed as an argument. This will write the file - * to the cache file name designated by its id and locale, overwriting it if already present - * and creating it (and its containing directory) if necessary. - */ - private static void installWordListToStaging(final String wordlistId, final String locale, - final String rawChecksum, final ContentProviderClient providerClient, - final Context context) { - final int COMPRESSED_CRYPTED_COMPRESSED = 0; - final int CRYPTED_COMPRESSED = 1; - final int COMPRESSED_CRYPTED = 2; - final int COMPRESSED_ONLY = 3; - final int CRYPTED_ONLY = 4; - final int NONE = 5; - final int MODE_MIN = COMPRESSED_CRYPTED_COMPRESSED; - final int MODE_MAX = NONE; - - final String clientId = context.getString(R.string.dictionary_pack_client_id); - final Uri.Builder wordListUriBuilder; - try { - wordListUriBuilder = getContentUriBuilderForType(clientId, - providerClient, QUERY_PATH_DATAFILE, wordlistId /* extraPath */); - } catch (RemoteException e) { - Log.e(TAG, "Can't communicate with the dictionary pack", e); - return; - } - final String finalFileName = - DictionaryInfoUtils.getStagingFileName(wordlistId, locale, context); - String tempFileName; - try { - tempFileName = BinaryDictionaryGetter.getTempFileName(wordlistId, context); - } catch (IOException e) { - Log.e(TAG, "Can't open the temporary file", e); - return; - } - - for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) { - final InputStream originalSourceStream; - InputStream inputStream = null; - InputStream uncompressedStream = null; - InputStream decryptedStream = null; - BufferedInputStream bufferedInputStream = null; - File outputFile = null; - BufferedOutputStream bufferedOutputStream = null; - AssetFileDescriptor afd = null; - final Uri wordListUri = wordListUriBuilder.build(); - try { - // Open input. - afd = openAssetFileDescriptor(providerClient, wordListUri); - // If we can't open it at all, don't even try a number of times. - if (null == afd) return; - originalSourceStream = afd.createInputStream(); - // Open output. - 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(); - // Get the appropriate decryption method for this try - switch (mode) { - case COMPRESSED_CRYPTED_COMPRESSED: - uncompressedStream = - FileTransforms.getUncompressedStream(originalSourceStream); - decryptedStream = FileTransforms.getDecryptedStream(uncompressedStream); - inputStream = FileTransforms.getUncompressedStream(decryptedStream); - break; - case CRYPTED_COMPRESSED: - decryptedStream = FileTransforms.getDecryptedStream(originalSourceStream); - inputStream = FileTransforms.getUncompressedStream(decryptedStream); - break; - case COMPRESSED_CRYPTED: - uncompressedStream = - FileTransforms.getUncompressedStream(originalSourceStream); - inputStream = FileTransforms.getDecryptedStream(uncompressedStream); - break; - case COMPRESSED_ONLY: - inputStream = FileTransforms.getUncompressedStream(originalSourceStream); - break; - case CRYPTED_ONLY: - inputStream = FileTransforms.getDecryptedStream(originalSourceStream); - break; - case NONE: - inputStream = originalSourceStream; - break; - } - bufferedInputStream = new BufferedInputStream(inputStream); - bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); - checkMagicAndCopyFileTo(bufferedInputStream, bufferedOutputStream); - bufferedOutputStream.flush(); - bufferedOutputStream.close(); - - if (SHOULD_VERIFY_CHECKSUM) { - final String actualRawChecksum = MD5Calculator.checksum( - new BufferedInputStream(new FileInputStream(outputFile))); - Log.i(TAG, "Computed checksum for downloaded dictionary. Expected = " - + rawChecksum + " ; actual = " + actualRawChecksum); - if (!TextUtils.isEmpty(rawChecksum) && !rawChecksum.equals(actualRawChecksum)) { - throw new IOException( - "Could not decode the file correctly : checksum differs"); - } - } - - // move the output file to the final staging file. - final File finalFile = new File(finalFileName); - if (!FileUtils.renameTo(outputFile, finalFile)) { - Log.e(TAG, String.format("Failed to rename from %s to %s.", - outputFile.getAbsoluteFile(), finalFile.getAbsoluteFile())); - } - - wordListUriBuilder.appendQueryParameter(QUERY_PARAMETER_DELETE_RESULT, - QUERY_PARAMETER_SUCCESS); - if (0 >= providerClient.delete(wordListUriBuilder.build(), null, null)) { - Log.e(TAG, "Could not have the dictionary pack delete a word list"); - } - Log.d(TAG, "Successfully copied file for wordlist ID " + wordlistId); - // Success! Close files (through the finally{} clause) and return. - return; - } catch (Exception e) { - if (DEBUG) { - Log.e(TAG, "Can't open word list in mode " + mode, e); - } - if (null != outputFile) { - // This may or may not fail. The file may not have been created if the - // exception was thrown before it could be. Hence, both failure and - // success are expected outcomes, so we don't check the return value. - outputFile.delete(); - } - // Try the next method. - } finally { - // Ignore exceptions while closing files. - closeAssetFileDescriptorAndReportAnyException(afd); - closeCloseableAndReportAnyException(inputStream); - closeCloseableAndReportAnyException(uncompressedStream); - closeCloseableAndReportAnyException(decryptedStream); - closeCloseableAndReportAnyException(bufferedInputStream); - closeCloseableAndReportAnyException(bufferedOutputStream); - } - } - - // We could not copy the file at all. This is very unexpected. - // I'd rather not print the word list ID to the log out of security concerns - Log.e(TAG, "Could not copy a word list. Will not be able to use it."); - // If we can't copy it we should warn the dictionary provider so that it can mark it - // as invalid. - reportBrokenFileToDictionaryProvider(providerClient, clientId, wordlistId); - } - - public static boolean reportBrokenFileToDictionaryProvider( - final ContentProviderClient providerClient, final String clientId, - final String wordlistId) { - try { - final Uri.Builder wordListUriBuilder = getContentUriBuilderForType(clientId, - providerClient, QUERY_PATH_DATAFILE, wordlistId /* extraPath */); - wordListUriBuilder.appendQueryParameter(QUERY_PARAMETER_DELETE_RESULT, - QUERY_PARAMETER_FAILURE); - if (0 >= providerClient.delete(wordListUriBuilder.build(), null, null)) { - Log.e(TAG, "Unable to delete a word list."); - } - } catch (RemoteException e) { - Log.e(TAG, "Communication with the dictionary provider was cut", e); - return false; - } - return true; - } - - // Ideally the two following methods should be merged, but AssetFileDescriptor does not - // implement Closeable although it does implement #close(), and Java does not have - // structural typing. - private static void closeAssetFileDescriptorAndReportAnyException( - final AssetFileDescriptor file) { - try { - if (null != file) file.close(); - } catch (Exception e) { - Log.e(TAG, "Exception while closing a file", e); - } - } - - private static void closeCloseableAndReportAnyException(final Closeable file) { - try { - if (null != file) file.close(); - } catch (Exception e) { - Log.e(TAG, "Exception while closing a file", e); - } - } - - /** - * Queries a content provider for word list data for some locale and stage the returned files - * - * This will query a content provider for word list data for a given locale, and copy the - * files locally so that they can be mmap'ed. This may overwrite previously cached word lists - * with newer versions if a newer version is made available by the content provider. - * @throw FileNotFoundException if the provider returns non-existent data. - * @throw IOException if the provider-returned data could not be read. - */ - public static void installDictToStagingFromContentProvider(final Locale locale, - final Context context, final boolean hasDefaultWordList) { - final ContentProviderClient providerClient; - try { - providerClient = context.getContentResolver(). - acquireContentProviderClient(getProviderUriBuilder("").build()); - } catch (final SecurityException e) { - Log.e(TAG, "No permission to communicate with the dictionary provider", e); - return; - } - if (null == providerClient) { - Log.e(TAG, "Can't establish communication with the dictionary provider"); - return; - } - try { - final List<WordListInfo> idList = getWordListWordListInfos(locale, context, - hasDefaultWordList); - for (WordListInfo id : idList) { - installWordListToStaging(id.mId, id.mLocale, id.mRawChecksum, providerClient, - context); - } - } finally { - providerClient.release(); - } - } - - /** - * Downloads the dictionary if it was never requested/used. - * - * @param locale locale to download - * @param context the context for resources and providers. - * @param hasDefaultWordList whether the default wordlist exists in the resources. - */ - public static void downloadDictIfNeverRequested(final Locale locale, - final Context context, final boolean hasDefaultWordList) { - getWordListWordListInfos(locale, context, hasDefaultWordList); - } - - /** - * Copies the data in an input stream to a target file if the magic number matches. - * - * If the magic number does not match the expected value, this method throws an - * IOException. Other usual conditions for IOException or FileNotFoundException - * also apply. - * - * @param input the stream to be copied. - * @param output an output stream to copy the data to. - */ - public static void checkMagicAndCopyFileTo(final BufferedInputStream input, - final BufferedOutputStream output) throws FileNotFoundException, IOException { - // Check the magic number - final int length = MAGIC_NUMBER_VERSION_2.length; - final byte[] magicNumberBuffer = new byte[length]; - final int readMagicNumberSize = input.read(magicNumberBuffer, 0, length); - if (readMagicNumberSize < length) { - throw new IOException("Less bytes to read than the magic number length"); - } - if (SHOULD_VERIFY_MAGIC_NUMBER) { - if (!Arrays.equals(MAGIC_NUMBER_VERSION_2, magicNumberBuffer)) { - if (!Arrays.equals(MAGIC_NUMBER_VERSION_1, magicNumberBuffer)) { - throw new IOException("Wrong magic number for downloaded file"); - } - } - } - output.write(magicNumberBuffer); - - // Actually copy the file - final byte[] buffer = new byte[FILE_READ_BUFFER_SIZE]; - for (int readBytes = input.read(buffer); readBytes >= 0; readBytes = input.read(buffer)) { - output.write(buffer, 0, readBytes); - } - input.close(); - } - - private static void reinitializeClientRecordInDictionaryContentProvider(final Context context, - final ContentProviderClient client, final String clientId) throws RemoteException { - final String metadataFileUri = MetadataFileUriGetter.getMetadataUri(context); - Log.i(TAG, "reinitializeClientRecordInDictionaryContentProvider() : MetadataFileUri = " - + metadataFileUri); - final String metadataAdditionalId = MetadataFileUriGetter.getMetadataAdditionalId(context); - // Tell the content provider to reset all information about this client id - final Uri metadataContentUri = getProviderUriBuilder(clientId) - .appendPath(QUERY_PATH_METADATA) - .appendQueryParameter(QUERY_PARAMETER_PROTOCOL, QUERY_PARAMETER_PROTOCOL_VALUE) - .build(); - client.delete(metadataContentUri, null, null); - // Update the metadata URI - final ContentValues metadataValues = new ContentValues(); - metadataValues.put(INSERT_METADATA_CLIENT_ID_COLUMN, clientId); - metadataValues.put(INSERT_METADATA_METADATA_URI_COLUMN, metadataFileUri); - metadataValues.put(INSERT_METADATA_METADATA_ADDITIONAL_ID_COLUMN, metadataAdditionalId); - client.insert(metadataContentUri, metadataValues); - - // Update the dictionary list. - final Uri dictionaryContentUriBase = getProviderUriBuilder(clientId) - .appendPath(QUERY_PATH_DICT_INFO) - .appendQueryParameter(QUERY_PARAMETER_PROTOCOL, QUERY_PARAMETER_PROTOCOL_VALUE) - .build(); - final ArrayList<DictionaryInfo> dictionaryList = - DictionaryInfoUtils.getCurrentDictionaryFileNameAndVersionInfo(context); - final int length = dictionaryList.size(); - for (int i = 0; i < length; ++i) { - final DictionaryInfo info = dictionaryList.get(i); - Log.i(TAG, "reinitializeClientRecordInDictionaryContentProvider() : Insert " + info); - client.insert(Uri.withAppendedPath(dictionaryContentUriBase, info.mId), - info.toContentValues()); - } - - // Read from metadata file in resources to get the baseline dictionary info. - // This ensures we start with a valid list of available dictionaries. - final int metadataResourceId = context.getResources().getIdentifier("metadata", - "raw", DictionaryInfoUtils.RESOURCE_PACKAGE_NAME); - if (metadataResourceId == 0) { - Log.w(TAG, "Missing metadata.json resource"); - return; - } - InputStream inputStream = null; - try { - inputStream = context.getResources().openRawResource(metadataResourceId); - UpdateHandler.handleMetadata(context, inputStream, clientId); - } catch (Exception e) { - Log.w(TAG, "Failed to read metadata.json from resources", e); - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - Log.w(TAG, "Failed to close metadata.json", e); - } - } - } - } - - /** - * Initialize a client record with the dictionary content provider. - * - * This merely acquires the content provider and calls - * #reinitializeClientRecordInDictionaryContentProvider. - * - * @param context the context for resources and providers. - * @param clientId the client ID to use. - */ - public static void initializeClientRecordHelper(final Context context, final String clientId) { - try { - final ContentProviderClient client = context.getContentResolver(). - acquireContentProviderClient(getProviderUriBuilder("").build()); - if (null == client) return; - reinitializeClientRecordInDictionaryContentProvider(context, client, clientId); - } catch (RemoteException e) { - Log.e(TAG, "Cannot contact the dictionary content provider", e); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java deleted file mode 100644 index c13f0e20a..000000000 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ /dev/null @@ -1,291 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.AssetFileDescriptor; -import android.util.Log; - -import com.android.inputmethod.latin.common.LocaleUtils; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; -import com.android.inputmethod.latin.makedict.DictionaryHeader; -import com.android.inputmethod.latin.makedict.UnsupportedFormatException; -import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; -import com.android.inputmethod.latin.utils.DictionaryInfoUtils; - -import java.io.File; -import java.io.IOException; -import java.nio.BufferUnderflowException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Locale; - -/** - * Helper class to get the address of a mmap'able dictionary file. - */ -final public class BinaryDictionaryGetter { - - /** - * Used for Log actions from this class - */ - private static final String TAG = BinaryDictionaryGetter.class.getSimpleName(); - - /** - * Used to return empty lists - */ - private static final File[] EMPTY_FILE_ARRAY = new File[0]; - - /** - * Name of the common preferences name to know which word list are on and which are off. - */ - private static final String COMMON_PREFERENCES_NAME = "LatinImeDictPrefs"; - - private static final boolean SHOULD_USE_DICT_VERSION = - DecoderSpecificConstants.SHOULD_USE_DICT_VERSION; - - // Name of the category for the main dictionary - public static final String MAIN_DICTIONARY_CATEGORY = "main"; - public static final String ID_CATEGORY_SEPARATOR = ":"; - - // The key considered to read the version attribute in a dictionary file. - private static String VERSION_KEY = "version"; - - // Prevents this from being instantiated - private BinaryDictionaryGetter() {} - - /** - * Generates a unique temporary file name in the app cache directory. - */ - public static String getTempFileName(final String id, final Context context) - throws IOException { - final String safeId = DictionaryInfoUtils.replaceFileNameDangerousCharacters(id); - final File directory = new File(DictionaryInfoUtils.getWordListTempDirectory(context)); - if (!directory.exists()) { - if (!directory.mkdirs()) { - Log.e(TAG, "Could not create the temporary directory"); - } - } - // If the first argument is less than three chars, createTempFile throws a - // RuntimeException. We don't really care about what name we get, so just - // put a three-chars prefix makes us safe. - return File.createTempFile("xxx" + safeId, null, directory).getAbsolutePath(); - } - - /** - * Returns a file address from a resource, or null if it cannot be opened. - */ - public static AssetFileAddress loadFallbackResource(final Context context, - final int fallbackResId) { - AssetFileDescriptor afd = null; - try { - afd = context.getResources().openRawResourceFd(fallbackResId); - } catch (RuntimeException e) { - Log.e(TAG, "Resource not found: " + fallbackResId); - return null; - } - if (afd == null) { - Log.e(TAG, "Resource cannot be opened: " + fallbackResId); - return null; - } - try { - return AssetFileAddress.makeFromFileNameAndOffset( - context.getApplicationInfo().sourceDir, afd.getStartOffset(), afd.getLength()); - } finally { - try { - afd.close(); - } catch (IOException ignored) { - } - } - } - - private static final class DictPackSettings { - final SharedPreferences mDictPreferences; - public DictPackSettings(final Context context) { - mDictPreferences = null == context ? null - : context.getSharedPreferences(COMMON_PREFERENCES_NAME, - Context.MODE_MULTI_PROCESS); - } - public boolean isWordListActive(final String dictId) { - if (null == mDictPreferences) { - // If we don't have preferences it basically means we can't find the dictionary - // pack - either it's not installed, or it's disabled, or there is some strange - // bug. Either way, a word list with no settings should be on by default: default - // dictionaries in LatinIME are on if there is no settings at all, and if for some - // reason some dictionaries have been installed BUT the dictionary pack can't be - // found anymore it's safer to actually supply installed dictionaries. - return true; - } - // The default is true here for the same reasons as above. We got the dictionary - // pack but if we don't have any settings for it it means the user has never been - // to the settings yet. So by default, the main dictionaries should be on. - return mDictPreferences.getBoolean(dictId, true); - } - } - - /** - * Utility class for the {@link #getCachedWordLists} method - */ - private static final class FileAndMatchLevel { - final File mFile; - final int mMatchLevel; - public FileAndMatchLevel(final File file, final int matchLevel) { - mFile = file; - mMatchLevel = matchLevel; - } - } - - /** - * Returns the list of cached files for a specific locale, one for each category. - * - * This will return exactly one file for each word list category that matches - * the passed locale. If several files match the locale for any given category, - * this returns the file with the closest match to the locale. For example, if - * the passed word list is en_US, and for a category we have an en and an en_US - * word list available, we'll return only the en_US one. - * Thus, the list will contain as many files as there are categories. - * - * @param locale the locale to find the dictionary files for, as a string. - * @param context the context on which to open the files upon. - * @return an array of binary dictionary files, which may be empty but may not be null. - */ - public static File[] getCachedWordLists(final String locale, final Context context) { - final File[] directoryList = DictionaryInfoUtils.getCachedDirectoryList(context); - if (null == directoryList) return EMPTY_FILE_ARRAY; - final HashMap<String, FileAndMatchLevel> cacheFiles = new HashMap<>(); - for (File directory : directoryList) { - if (!directory.isDirectory()) continue; - final String dirLocale = - DictionaryInfoUtils.getWordListIdFromFileName(directory.getName()); - final int matchLevel = LocaleUtils.getMatchLevel(dirLocale, locale); - if (LocaleUtils.isMatch(matchLevel)) { - final File[] wordLists = directory.listFiles(); - if (null != wordLists) { - for (File wordList : wordLists) { - final String category = - DictionaryInfoUtils.getCategoryFromFileName(wordList.getName()); - final FileAndMatchLevel currentBestMatch = cacheFiles.get(category); - if (null == currentBestMatch || currentBestMatch.mMatchLevel < matchLevel) { - cacheFiles.put(category, new FileAndMatchLevel(wordList, matchLevel)); - } - } - } - } - } - if (cacheFiles.isEmpty()) return EMPTY_FILE_ARRAY; - final File[] result = new File[cacheFiles.size()]; - int index = 0; - for (final FileAndMatchLevel entry : cacheFiles.values()) { - result[index++] = entry.mFile; - } - return result; - } - - // ## HACK ## we prevent usage of a dictionary before version 18. The reason for this is, since - // those do not include allowlist entries, the new code with an old version of the dictionary - // would lose allowlist functionality. - private static boolean hackCanUseDictionaryFile(final File file) { - if (!SHOULD_USE_DICT_VERSION) { - return true; - } - - try { - // Read the version of the file - final DictionaryHeader header = BinaryDictionaryUtils.getHeader(file); - final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY); - if (null == version) { - // No version in the options : the format is unexpected - return false; - } - // Version 18 is the first one to include the allowlist. - // Obviously this is a big ## HACK ## - return Integer.parseInt(version) >= 18; - } catch (java.io.FileNotFoundException e) { - return false; - } catch (java.io.IOException e) { - return false; - } catch (NumberFormatException e) { - return false; - } catch (BufferUnderflowException e) { - return false; - } catch (UnsupportedFormatException e) { - return false; - } - } - - /** - * Returns a list of file addresses for a given locale, trying relevant methods in order. - * - * Tries to get binary dictionaries from various sources, in order: - * - Uses a content provider to get a public dictionary set, as per the protocol described - * in BinaryDictionaryFileDumper. - * If that fails: - * - Gets a file name from the built-in dictionary for this locale, if any. - * If that fails: - * - Returns null. - * @return The list of addresses of valid dictionary files, or null. - */ - public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale, - final Context context, boolean notifyDictionaryPackForUpdates) { - if (notifyDictionaryPackForUpdates) { - final boolean hasDefaultWordList = DictionaryInfoUtils.isDictionaryAvailable( - context, locale); - // It makes sure that the first time keyboard comes up and the dictionaries are reset, - // the DB is populated with the appropriate values for each locale. Helps in downloading - // the dictionaries when the user enables and switches new languages before the - // DictionaryService runs. - BinaryDictionaryFileDumper.downloadDictIfNeverRequested( - locale, context, hasDefaultWordList); - - // Move a staging files to the cache ddirectories if any. - DictionaryInfoUtils.moveStagingFilesIfExists(context); - } - final File[] cachedWordLists = getCachedWordLists(locale.toString(), context); - final String mainDictId = DictionaryInfoUtils.getMainDictId(locale); - final DictPackSettings dictPackSettings = new DictPackSettings(context); - - boolean foundMainDict = false; - final ArrayList<AssetFileAddress> fileList = new ArrayList<>(); - // cachedWordLists may not be null, see doc for getCachedDictionaryList - for (final File f : cachedWordLists) { - final String wordListId = DictionaryInfoUtils.getWordListIdFromFileName(f.getName()); - final boolean canUse = f.canRead() && hackCanUseDictionaryFile(f); - if (canUse && DictionaryInfoUtils.isMainWordListId(wordListId)) { - foundMainDict = true; - } - if (!dictPackSettings.isWordListActive(wordListId)) continue; - if (canUse) { - final AssetFileAddress afa = AssetFileAddress.makeFromFileName(f.getPath()); - if (null != afa) fileList.add(afa); - } else { - Log.e(TAG, "Found a cached dictionary file for " + locale.toString() - + " but cannot read or use it"); - } - } - - if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) { - final int fallbackResId = - DictionaryInfoUtils.getMainDictionaryResourceId(context.getResources(), locale); - final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId); - if (null != fallbackAsset) { - fileList.add(fallbackAsset); - } - } - - return fileList; - } -} diff --git a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java deleted file mode 100644 index dbd639fe8..000000000 --- a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * 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.Manifest; -import android.content.Context; -import android.net.Uri; -import android.provider.ContactsContract; -import android.provider.ContactsContract.Contacts; -import android.util.Log; - -import com.android.inputmethod.annotations.ExternallyReferenced; -import com.android.inputmethod.latin.ContactsManager.ContactsChangedListener; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.permissions.PermissionsUtil; -import com.android.inputmethod.latin.personalization.AccountUtils; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -import javax.annotation.Nullable; - -public class ContactsBinaryDictionary extends ExpandableBinaryDictionary - implements ContactsChangedListener { - private static final String TAG = ContactsBinaryDictionary.class.getSimpleName(); - private static final String NAME = "contacts"; - - private static final boolean DEBUG = false; - private static final boolean DEBUG_DUMP = false; - - /** - * Whether to use "firstname lastname" in bigram predictions. - */ - private final boolean mUseFirstLastBigrams; - private final ContactsManager mContactsManager; - - protected ContactsBinaryDictionary(final Context context, final Locale locale, - final File dictFile, final String name) { - super(context, getDictName(name, locale, dictFile), locale, Dictionary.TYPE_CONTACTS, - dictFile); - mUseFirstLastBigrams = ContactsDictionaryUtils.useFirstLastBigramsForLocale(locale); - mContactsManager = new ContactsManager(context); - mContactsManager.registerForUpdates(this /* listener */); - reloadDictionaryIfRequired(); - } - - // Note: This method is called by {@link DictionaryFacilitator} using Java reflection. - @ExternallyReferenced - public static ContactsBinaryDictionary getDictionary(final Context context, final Locale locale, - final File dictFile, final String dictNamePrefix, @Nullable final String account) { - return new ContactsBinaryDictionary(context, locale, dictFile, dictNamePrefix + NAME); - } - - @Override - public synchronized void close() { - mContactsManager.close(); - super.close(); - } - - /** - * Typically called whenever the dictionary is created for the first time or - * recreated when we think that there are updates to the dictionary. - * This is called asynchronously. - */ - @Override - public void loadInitialContentsLocked() { - loadDeviceAccountsEmailAddressesLocked(); - loadDictionaryForUriLocked(ContactsContract.Profile.CONTENT_URI); - // TODO: Switch this URL to the newer ContactsContract too - loadDictionaryForUriLocked(Contacts.CONTENT_URI); - } - - /** - * Loads device accounts to the dictionary. - */ - private void loadDeviceAccountsEmailAddressesLocked() { - final List<String> accountVocabulary = - AccountUtils.getDeviceAccountsEmailAddresses(mContext); - if (accountVocabulary == null || accountVocabulary.isEmpty()) { - return; - } - for (String word : accountVocabulary) { - if (DEBUG) { - Log.d(TAG, "loadAccountVocabulary: " + word); - } - runGCIfRequiredLocked(true /* mindsBlockByGC */); - addUnigramLocked(word, ContactsDictionaryConstants.FREQUENCY_FOR_CONTACTS, - false /* isNotAWord */, false /* isPossiblyOffensive */, - BinaryDictionary.NOT_A_VALID_TIMESTAMP); - } - } - - /** - * Loads data within content providers to the dictionary. - */ - private void loadDictionaryForUriLocked(final Uri uri) { - if (!PermissionsUtil.checkAllPermissionsGranted( - mContext, Manifest.permission.READ_CONTACTS)) { - Log.i(TAG, "No permission to read contacts. Not loading the Dictionary."); - } - - final ArrayList<String> validNames = mContactsManager.getValidNames(uri); - for (final String name : validNames) { - addNameLocked(name); - } - if (uri.equals(Contacts.CONTENT_URI)) { - // Since we were able to add content successfully, update the local - // state of the manager. - mContactsManager.updateLocalState(validNames); - } - } - - /** - * Adds the words in a name (e.g., firstname/lastname) to the binary dictionary along with their - * bigrams depending on locale. - */ - private void addNameLocked(final String name) { - int len = StringUtils.codePointCount(name); - NgramContext ngramContext = NgramContext.getEmptyPrevWordsContext( - BinaryDictionary.MAX_PREV_WORD_COUNT_FOR_N_GRAM); - // TODO: Better tokenization for non-Latin writing systems - for (int i = 0; i < len; i++) { - if (Character.isLetter(name.codePointAt(i))) { - int end = ContactsDictionaryUtils.getWordEndPosition(name, len, i); - String word = name.substring(i, end); - if (DEBUG_DUMP) { - Log.d(TAG, "addName word = " + word); - } - i = end - 1; - // Don't add single letter words, possibly confuses - // capitalization of i. - final int wordLen = StringUtils.codePointCount(word); - if (wordLen <= MAX_WORD_LENGTH && wordLen > 1) { - if (DEBUG) { - Log.d(TAG, "addName " + name + ", " + word + ", " + ngramContext); - } - runGCIfRequiredLocked(true /* mindsBlockByGC */); - addUnigramLocked(word, - ContactsDictionaryConstants.FREQUENCY_FOR_CONTACTS, false /* isNotAWord */, - false /* isPossiblyOffensive */, - BinaryDictionary.NOT_A_VALID_TIMESTAMP); - if (ngramContext.isValid() && mUseFirstLastBigrams) { - runGCIfRequiredLocked(true /* mindsBlockByGC */); - addNgramEntryLocked(ngramContext, - word, - ContactsDictionaryConstants.FREQUENCY_FOR_CONTACTS_BIGRAM, - BinaryDictionary.NOT_A_VALID_TIMESTAMP); - } - ngramContext = ngramContext.getNextNgramContext( - new NgramContext.WordInfo(word)); - } - } - } - } - - @Override - public void onContactsChange() { - setNeedsToRecreate(); - } -} diff --git a/java/src/com/android/inputmethod/latin/ContactsContentObserver.java b/java/src/com/android/inputmethod/latin/ContactsContentObserver.java deleted file mode 100644 index 6103a8296..000000000 --- a/java/src/com/android/inputmethod/latin/ContactsContentObserver.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2014 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.Manifest; -import android.content.ContentResolver; -import android.content.Context; -import android.database.ContentObserver; -import android.os.SystemClock; -import android.provider.ContactsContract.Contacts; -import android.util.Log; - -import com.android.inputmethod.latin.ContactsManager.ContactsChangedListener; -import com.android.inputmethod.latin.define.DebugFlags; -import com.android.inputmethod.latin.permissions.PermissionsUtil; -import com.android.inputmethod.latin.utils.ExecutorUtils; - -import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * A content observer that listens to updates to content provider {@link Contacts#CONTENT_URI}. - */ -public class ContactsContentObserver implements Runnable { - private static final String TAG = "ContactsContentObserver"; - - private final Context mContext; - private final ContactsManager mManager; - private final AtomicBoolean mRunning = new AtomicBoolean(false); - - private ContentObserver mContentObserver; - private ContactsChangedListener mContactsChangedListener; - - public ContactsContentObserver(final ContactsManager manager, final Context context) { - mManager = manager; - mContext = context; - } - - public void registerObserver(final ContactsChangedListener listener) { - if (!PermissionsUtil.checkAllPermissionsGranted( - mContext, Manifest.permission.READ_CONTACTS)) { - Log.i(TAG, "No permission to read contacts. Not registering the observer."); - // do nothing if we do not have the permission to read contacts. - return; - } - - if (DebugFlags.DEBUG_ENABLED) { - Log.d(TAG, "registerObserver()"); - } - mContactsChangedListener = listener; - mContentObserver = new ContentObserver(null /* handler */) { - @Override - public void onChange(boolean self) { - ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD) - .execute(ContactsContentObserver.this); - } - }; - final ContentResolver contentResolver = mContext.getContentResolver(); - contentResolver.registerContentObserver(Contacts.CONTENT_URI, true, mContentObserver); - } - - @Override - public void run() { - if (!PermissionsUtil.checkAllPermissionsGranted( - mContext, Manifest.permission.READ_CONTACTS)) { - Log.i(TAG, "No permission to read contacts. Not updating the contacts."); - unregister(); - return; - } - - if (!mRunning.compareAndSet(false /* expect */, true /* update */)) { - if (DebugFlags.DEBUG_ENABLED) { - Log.d(TAG, "run() : Already running. Don't waste time checking again."); - } - return; - } - if (haveContentsChanged()) { - if (DebugFlags.DEBUG_ENABLED) { - Log.d(TAG, "run() : Contacts have changed. Notifying listeners."); - } - mContactsChangedListener.onContactsChange(); - } - mRunning.set(false); - } - - boolean haveContentsChanged() { - if (!PermissionsUtil.checkAllPermissionsGranted( - mContext, Manifest.permission.READ_CONTACTS)) { - Log.i(TAG, "No permission to read contacts. Marking contacts as not changed."); - return false; - } - - final long startTime = SystemClock.uptimeMillis(); - final int contactCount = mManager.getContactCount(); - if (contactCount > ContactsDictionaryConstants.MAX_CONTACTS_PROVIDER_QUERY_LIMIT) { - // If there are too many contacts then return false. In this rare case it is impossible - // to include all of them anyways and the cost of rebuilding the dictionary is too high. - // TODO: Sort and check only the most recent contacts? - return false; - } - if (contactCount != mManager.getContactCountAtLastRebuild()) { - if (DebugFlags.DEBUG_ENABLED) { - Log.d(TAG, "haveContentsChanged() : Count changed from " - + mManager.getContactCountAtLastRebuild() + " to " + contactCount); - } - return true; - } - final ArrayList<String> names = mManager.getValidNames(Contacts.CONTENT_URI); - if (names.hashCode() != mManager.getHashCodeAtLastRebuild()) { - return true; - } - if (DebugFlags.DEBUG_ENABLED) { - Log.d(TAG, "haveContentsChanged() : No change detected in " - + (SystemClock.uptimeMillis() - startTime) + " ms)"); - } - return false; - } - - public void unregister() { - mContext.getContentResolver().unregisterContentObserver(mContentObserver); - } -} diff --git a/java/src/com/android/inputmethod/latin/ContactsDictionaryConstants.java b/java/src/com/android/inputmethod/latin/ContactsDictionaryConstants.java deleted file mode 100644 index 022940910..000000000 --- a/java/src/com/android/inputmethod/latin/ContactsDictionaryConstants.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2015 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.provider.BaseColumns; -import android.provider.ContactsContract.Contacts; - -/** - * Constants related to Contacts Content Provider. - */ -public class ContactsDictionaryConstants { - /** - * Projections for {@link Contacts.CONTENT_URI} - */ - public static final String[] PROJECTION = { BaseColumns._ID, Contacts.DISPLAY_NAME, - Contacts.TIMES_CONTACTED, Contacts.LAST_TIME_CONTACTED, Contacts.IN_VISIBLE_GROUP }; - public static final String[] PROJECTION_ID_ONLY = { BaseColumns._ID }; - - /** - * Frequency for contacts information into the dictionary - */ - public static final int FREQUENCY_FOR_CONTACTS = 40; - public static final int FREQUENCY_FOR_CONTACTS_BIGRAM = 90; - - /** - * Do not attempt to query contacts if there are more than this many entries. - */ - public static final int MAX_CONTACTS_PROVIDER_QUERY_LIMIT = 10000; - - /** - * Index of the column for 'name' in content providers: - * Contacts & ContactsContract.Profile. - */ - public static final int NAME_INDEX = 1; - public static final int TIMES_CONTACTED_INDEX = 2; - public static final int LAST_TIME_CONTACTED_INDEX = 3; - public static final int IN_VISIBLE_GROUP_INDEX = 4; -} diff --git a/java/src/com/android/inputmethod/latin/ContactsDictionaryUtils.java b/java/src/com/android/inputmethod/latin/ContactsDictionaryUtils.java deleted file mode 100644 index b77388434..000000000 --- a/java/src/com/android/inputmethod/latin/ContactsDictionaryUtils.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2014 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 com.android.inputmethod.latin.common.Constants; - -import java.util.Locale; - -/** - * Utility methods related contacts dictionary. - */ -public class ContactsDictionaryUtils { - - /** - * Returns the index of the last letter in the word, starting from position startIndex. - */ - public static int getWordEndPosition(final String string, final int len, - final int startIndex) { - int end; - int cp = 0; - for (end = startIndex + 1; end < len; end += Character.charCount(cp)) { - cp = string.codePointAt(end); - if (cp != Constants.CODE_DASH && cp != Constants.CODE_SINGLE_QUOTE - && !Character.isLetter(cp)) { - break; - } - } - return end; - } - - /** - * Returns true if the locale supports using first name and last name as bigrams. - */ - public static boolean useFirstLastBigramsForLocale(final Locale locale) { - // TODO: Add firstname/lastname bigram rules for other languages. - if (locale != null && locale.getLanguage().equals(Locale.ENGLISH.getLanguage())) { - return true; - } - return false; - } -} diff --git a/java/src/com/android/inputmethod/latin/ContactsManager.java b/java/src/com/android/inputmethod/latin/ContactsManager.java deleted file mode 100644 index 13503ff45..000000000 --- a/java/src/com/android/inputmethod/latin/ContactsManager.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Copyright (C) 2014 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.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteException; -import android.net.Uri; -import android.provider.ContactsContract.Contacts; -import android.text.TextUtils; -import android.util.Log; - -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.StringUtils; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Manages all interactions with Contacts DB. - * - * The manager provides an API for listening to meaning full updates by keeping a - * measure of the current state of the content provider. - */ -public class ContactsManager { - private static final String TAG = "ContactsManager"; - - /** - * Use at most this many of the highest affinity contacts. - */ - public static final int MAX_CONTACT_NAMES = 200; - - protected static class RankedContact { - public final String mName; - public final long mLastContactedTime; - public final int mTimesContacted; - public final boolean mInVisibleGroup; - - private float mAffinity = 0.0f; - - RankedContact(final Cursor cursor) { - mName = cursor.getString( - ContactsDictionaryConstants.NAME_INDEX); - mTimesContacted = cursor.getInt( - ContactsDictionaryConstants.TIMES_CONTACTED_INDEX); - mLastContactedTime = cursor.getLong( - ContactsDictionaryConstants.LAST_TIME_CONTACTED_INDEX); - mInVisibleGroup = cursor.getInt( - ContactsDictionaryConstants.IN_VISIBLE_GROUP_INDEX) == 1; - } - - float getAffinity() { - return mAffinity; - } - - /** - * Calculates the affinity with the contact based on: - * - How many times it has been contacted - * - How long since the last contact. - * - Whether the contact is in the visible group (i.e., Contacts list). - * - * Note: This affinity is limited by the fact that some apps currently do not update the - * LAST_TIME_CONTACTED or TIMES_CONTACTED counters. As a result, a frequently messaged - * contact may still have 0 affinity. - */ - void computeAffinity(final int maxTimesContacted, final long currentTime) { - final float timesWeight = ((float) mTimesContacted + 1) / (maxTimesContacted + 1); - final long timeSinceLastContact = Math.min( - Math.max(0, currentTime - mLastContactedTime), - TimeUnit.MILLISECONDS.convert(180, TimeUnit.DAYS)); - final float lastTimeWeight = (float) Math.pow(0.5, - timeSinceLastContact / (TimeUnit.MILLISECONDS.convert(10, TimeUnit.DAYS))); - final float visibleWeight = mInVisibleGroup ? 1.0f : 0.0f; - mAffinity = (timesWeight + lastTimeWeight + visibleWeight) / 3; - } - } - - private static class AffinityComparator implements Comparator<RankedContact> { - @Override - public int compare(RankedContact contact1, RankedContact contact2) { - return Float.compare(contact2.getAffinity(), contact1.getAffinity()); - } - } - - /** - * Interface to implement for classes interested in getting notified for updates - * to Contacts content provider. - */ - public static interface ContactsChangedListener { - public void onContactsChange(); - } - - /** - * The number of contacts observed in the most recent instance of - * contacts content provider. - */ - private AtomicInteger mContactCountAtLastRebuild = new AtomicInteger(0); - - /** - * The hash code of list of valid contacts names in the most recent dictionary - * rebuild. - */ - private AtomicInteger mHashCodeAtLastRebuild = new AtomicInteger(0); - - private final Context mContext; - private final ContactsContentObserver mObserver; - - public ContactsManager(final Context context) { - mContext = context; - mObserver = new ContactsContentObserver(this /* ContactsManager */, context); - } - - // TODO: This was synchronized in previous version. Why? - public void registerForUpdates(final ContactsChangedListener listener) { - mObserver.registerObserver(listener); - } - - public int getContactCountAtLastRebuild() { - return mContactCountAtLastRebuild.get(); - } - - public int getHashCodeAtLastRebuild() { - return mHashCodeAtLastRebuild.get(); - } - - /** - * Returns all the valid names in the Contacts DB. Callers should also - * call {@link #updateLocalState(ArrayList)} after they are done with result - * so that the manager can cache local state for determining updates. - * - * These names are sorted by their affinity to the user, with favorite - * contacts appearing first. - */ - public ArrayList<String> getValidNames(final Uri uri) { - // Check all contacts since it's not possible to find out which names have changed. - // This is needed because it's possible to receive extraneous onChange events even when no - // name has changed. - final Cursor cursor = mContext.getContentResolver().query(uri, - ContactsDictionaryConstants.PROJECTION, null, null, null); - final ArrayList<RankedContact> contacts = new ArrayList<>(); - int maxTimesContacted = 0; - if (cursor != null) { - try { - if (cursor.moveToFirst()) { - while (!cursor.isAfterLast()) { - final String name = cursor.getString( - ContactsDictionaryConstants.NAME_INDEX); - if (isValidName(name)) { - final int timesContacted = cursor.getInt( - ContactsDictionaryConstants.TIMES_CONTACTED_INDEX); - if (timesContacted > maxTimesContacted) { - maxTimesContacted = timesContacted; - } - contacts.add(new RankedContact(cursor)); - } - cursor.moveToNext(); - } - } - } finally { - cursor.close(); - } - } - final long currentTime = System.currentTimeMillis(); - for (RankedContact contact : contacts) { - contact.computeAffinity(maxTimesContacted, currentTime); - } - Collections.sort(contacts, new AffinityComparator()); - final HashSet<String> names = new HashSet<>(); - for (int i = 0; i < contacts.size() && names.size() < MAX_CONTACT_NAMES; ++i) { - names.add(contacts.get(i).mName); - } - return new ArrayList<>(names); - } - - /** - * Returns the number of contacts in contacts content provider. - */ - public int getContactCount() { - // TODO: consider switching to a rawQuery("select count(*)...") on the database if - // performance is a bottleneck. - Cursor cursor = null; - try { - cursor = mContext.getContentResolver().query(Contacts.CONTENT_URI, - ContactsDictionaryConstants.PROJECTION_ID_ONLY, null, null, null); - if (null == cursor) { - return 0; - } - return cursor.getCount(); - } catch (final SQLiteException e) { - Log.e(TAG, "SQLiteException in the remote Contacts process.", e); - } finally { - if (null != cursor) { - cursor.close(); - } - } - return 0; - } - - private static boolean isValidName(final String name) { - if (TextUtils.isEmpty(name) || name.indexOf(Constants.CODE_COMMERCIAL_AT) != -1) { - return false; - } - final boolean hasSpace = name.indexOf(Constants.CODE_SPACE) != -1; - if (!hasSpace) { - // Only allow an isolated word if it does not contain a hyphen. - // This helps to filter out mailing lists. - return name.indexOf(Constants.CODE_DASH) == -1; - } - return true; - } - - /** - * Updates the local state of the manager. This should be called when the callers - * are done with all the updates of the content provider successfully. - */ - public void updateLocalState(final ArrayList<String> names) { - mContactCountAtLastRebuild.set(getContactCount()); - mHashCodeAtLastRebuild.set(names.hashCode()); - } - - /** - * Performs any necessary cleanup. - */ - public void close() { - mObserver.unregister(); - } -} diff --git a/java/src/com/android/inputmethod/latin/DicTraverseSession.java b/java/src/com/android/inputmethod/latin/DicTraverseSession.java deleted file mode 100644 index 6816f129a..000000000 --- a/java/src/com/android/inputmethod/latin/DicTraverseSession.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * 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 com.android.inputmethod.latin.common.NativeSuggestOptions; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; -import com.android.inputmethod.latin.utils.JniUtils; - -import java.util.Locale; - -public final class DicTraverseSession { - static { - JniUtils.loadNativeLibrary(); - } - // Must be equal to MAX_RESULTS in native/jni/src/defines.h - private static final int MAX_RESULTS = 18; - public final int[] mInputCodePoints = - new int[DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH]; - public final int[][] mPrevWordCodePointArrays = - new int[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM][]; - public final boolean[] mIsBeginningOfSentenceArray = - new boolean[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; - public final int[] mOutputSuggestionCount = new int[1]; - public final int[] mOutputCodePoints = - new int[DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH * MAX_RESULTS]; - public final int[] mSpaceIndices = new int[MAX_RESULTS]; - public final int[] mOutputScores = new int[MAX_RESULTS]; - public final int[] mOutputTypes = new int[MAX_RESULTS]; - // Only one result is ever used - public final int[] mOutputAutoCommitFirstWordConfidence = new int[1]; - public final float[] mInputOutputWeightOfLangModelVsSpatialModel = new float[1]; - - public final NativeSuggestOptions mNativeSuggestOptions = new NativeSuggestOptions(); - - private static native long setDicTraverseSessionNative(String locale, long dictSize); - private static native void initDicTraverseSessionNative(long nativeDicTraverseSession, - long dictionary, int[] previousWord, int previousWordLength); - private static native void releaseDicTraverseSessionNative(long nativeDicTraverseSession); - - private long mNativeDicTraverseSession; - - public DicTraverseSession(Locale locale, long dictionary, long dictSize) { - mNativeDicTraverseSession = createNativeDicTraverseSession( - locale != null ? locale.toString() : "", dictSize); - initSession(dictionary); - } - - public long getSession() { - return mNativeDicTraverseSession; - } - - public void initSession(long dictionary) { - initSession(dictionary, null, 0); - } - - public void initSession(long dictionary, int[] previousWord, int previousWordLength) { - initDicTraverseSessionNative( - mNativeDicTraverseSession, dictionary, previousWord, previousWordLength); - } - - private static long createNativeDicTraverseSession(String locale, long dictSize) { - return setDicTraverseSessionNative(locale, dictSize); - } - - private void closeInternal() { - if (mNativeDicTraverseSession != 0) { - releaseDicTraverseSessionNative(mNativeDicTraverseSession); - mNativeDicTraverseSession = 0; - } - } - - public void close() { - closeInternal(); - } - - @Override - protected void finalize() throws Throwable { - try { - closeInternal(); - } finally { - super.finalize(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/Dictionary.java b/java/src/com/android/inputmethod/latin/Dictionary.java deleted file mode 100644 index e00219c98..000000000 --- a/java/src/com/android/inputmethod/latin/Dictionary.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright (C) 2008 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 com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; - -import java.util.ArrayList; -import java.util.Locale; -import java.util.Arrays; -import java.util.HashSet; - -/** - * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key - * strokes. - */ -public abstract class Dictionary { - public static final int NOT_A_PROBABILITY = -1; - public static final float NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL = -1.0f; - - // The following types do not actually come from real dictionary instances, so we create - // corresponding instances. - public static final String TYPE_USER_TYPED = "user_typed"; - public static final PhonyDictionary DICTIONARY_USER_TYPED = new PhonyDictionary(TYPE_USER_TYPED); - - public static final String TYPE_USER_SHORTCUT = "user_shortcut"; - public static final PhonyDictionary DICTIONARY_USER_SHORTCUT = - new PhonyDictionary(TYPE_USER_SHORTCUT); - - public static final String TYPE_APPLICATION_DEFINED = "application_defined"; - public static final PhonyDictionary DICTIONARY_APPLICATION_DEFINED = - new PhonyDictionary(TYPE_APPLICATION_DEFINED); - - public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such - public static final PhonyDictionary DICTIONARY_HARDCODED = - new PhonyDictionary(TYPE_HARDCODED); - - // Spawned by resuming suggestions. Comes from a span that was in the TextView. - public static final String TYPE_RESUMED = "resumed"; - public static final PhonyDictionary DICTIONARY_RESUMED = new PhonyDictionary(TYPE_RESUMED); - - // The following types of dictionary have actual functional instances. We don't need final - // phony dictionary instances for them. - public static final String TYPE_MAIN = "main"; - public static final String TYPE_CONTACTS = "contacts"; - // User dictionary, the system-managed one. - public static final String TYPE_USER = "user"; - // User history dictionary internal to LatinIME. - public static final String TYPE_USER_HISTORY = "history"; - public final String mDictType; - // The locale for this dictionary. May be null if unknown (phony dictionary for example). - public final Locale mLocale; - - /** - * Set out of the dictionary types listed above that are based on data specific to the user, - * e.g., the user's contacts. - */ - private static final HashSet<String> sUserSpecificDictionaryTypes = new HashSet<>(Arrays.asList( - TYPE_USER_TYPED, - TYPE_USER, - TYPE_CONTACTS, - TYPE_USER_HISTORY)); - - public Dictionary(final String dictType, final Locale locale) { - mDictType = dictType; - mLocale = locale; - } - - /** - * Searches for suggestions for a given context. - * @param composedData the key sequence to match with coordinate info - * @param ngramContext the context for n-gram. - * @param proximityInfoHandle the handle for key proximity. Is ignored by some implementations. - * @param settingsValuesForSuggestion the settings values used for the suggestion. - * @param sessionId the session id. - * @param weightForLocale the weight given to this locale, to multiply the output scores for - * multilingual input. - * @param inOutWeightOfLangModelVsSpatialModel the weight of the language model as a ratio of - * the spatial model, used for generating suggestions. inOutWeightOfLangModelVsSpatialModel is - * a float array that has only one element. This can be updated when a different value is used. - * @return the list of suggestions (possibly null if none) - */ - abstract public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData, - final NgramContext ngramContext, final long proximityInfoHandle, - final SettingsValuesForSuggestion settingsValuesForSuggestion, - final int sessionId, final float weightForLocale, - final float[] inOutWeightOfLangModelVsSpatialModel); - - /** - * Checks if the given word has to be treated as a valid word. Please note that some - * dictionaries have entries that should be treated as invalid words. - * @param word the word to search for. The search should be case-insensitive. - * @return true if the word is valid, false otherwise - */ - public boolean isValidWord(final String word) { - return isInDictionary(word); - } - - /** - * Checks if the given word is in the dictionary regardless of it being valid or not. - */ - abstract public boolean isInDictionary(final String word); - - /** - * Get the frequency of the word. - * @param word the word to get the frequency of. - */ - public int getFrequency(final String word) { - return NOT_A_PROBABILITY; - } - - /** - * Get the maximum frequency of the word. - * @param word the word to get the maximum frequency of. - */ - public int getMaxFrequencyOfExactMatches(final String word) { - return NOT_A_PROBABILITY; - } - - /** - * Compares the contents of the character array with the typed word and returns true if they - * are the same. - * @param word the array of characters that make up the word - * @param length the number of valid characters in the character array - * @param typedWord the word to compare with - * @return true if they are the same, false otherwise. - */ - protected boolean same(final char[] word, final int length, final String typedWord) { - if (typedWord.length() != length) { - return false; - } - for (int i = 0; i < length; i++) { - if (word[i] != typedWord.charAt(i)) { - return false; - } - } - return true; - } - - /** - * Override to clean up any resources. - */ - public void close() { - // empty base implementation - } - - /** - * Subclasses may override to indicate that this Dictionary is not yet properly initialized. - */ - public boolean isInitialized() { - return true; - } - - /** - * Whether we think this suggestion should trigger an auto-commit. prevWord is the word - * before the suggestion, so that we can use n-gram frequencies. - * @param candidate The candidate suggestion, in whole (not only the first part). - * @return whether we should auto-commit or not. - */ - public boolean shouldAutoCommit(final SuggestedWordInfo candidate) { - // If we don't have support for auto-commit, or if we don't know, we return false to - // avoid auto-committing stuff. Implementations of the Dictionary class that know to - // determine whether we should auto-commit will override this. - return false; - } - - /** - * Whether this dictionary is based on data specific to the user, e.g., the user's contacts. - * @return Whether this dictionary is specific to the user. - */ - public boolean isUserSpecific() { - return sUserSpecificDictionaryTypes.contains(mDictType); - } - - /** - * Not a true dictionary. A placeholder used to indicate suggestions that don't come from any - * real dictionary. - */ - @UsedForTesting - static class PhonyDictionary extends Dictionary { - @UsedForTesting - PhonyDictionary(final String type) { - super(type, null); - } - - @Override - public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData, - final NgramContext ngramContext, final long proximityInfoHandle, - final SettingsValuesForSuggestion settingsValuesForSuggestion, - final int sessionId, final float weightForLocale, - final float[] inOutWeightOfLangModelVsSpatialModel) { - return null; - } - - @Override - public boolean isInDictionary(String word) { - return false; - } - } -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java deleted file mode 100644 index 96575f629..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import android.util.Log; - -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Locale; -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * Class for a collection of dictionaries that behave like one dictionary. - */ -public final class DictionaryCollection extends Dictionary { - private final String TAG = DictionaryCollection.class.getSimpleName(); - protected final CopyOnWriteArrayList<Dictionary> mDictionaries; - - public DictionaryCollection(final String dictType, final Locale locale) { - super(dictType, locale); - mDictionaries = new CopyOnWriteArrayList<>(); - } - - public DictionaryCollection(final String dictType, final Locale locale, - final Dictionary... dictionaries) { - super(dictType, locale); - if (null == dictionaries) { - mDictionaries = new CopyOnWriteArrayList<>(); - } else { - mDictionaries = new CopyOnWriteArrayList<>(dictionaries); - mDictionaries.removeAll(Collections.singleton(null)); - } - } - - public DictionaryCollection(final String dictType, final Locale locale, - final Collection<Dictionary> dictionaries) { - super(dictType, locale); - mDictionaries = new CopyOnWriteArrayList<>(dictionaries); - mDictionaries.removeAll(Collections.singleton(null)); - } - - @Override - public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData, - final NgramContext ngramContext, final long proximityInfoHandle, - final SettingsValuesForSuggestion settingsValuesForSuggestion, - final int sessionId, final float weightForLocale, - final float[] inOutWeightOfLangModelVsSpatialModel) { - 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).getSuggestions(composedData, - ngramContext, proximityInfoHandle, settingsValuesForSuggestion, sessionId, - weightForLocale, inOutWeightOfLangModelVsSpatialModel); - if (null == suggestions) suggestions = new ArrayList<>(); - final int length = dictionaries.size(); - for (int i = 1; i < length; ++ i) { - final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions( - composedData, ngramContext, proximityInfoHandle, settingsValuesForSuggestion, - sessionId, weightForLocale, inOutWeightOfLangModelVsSpatialModel); - if (null != sugg) suggestions.addAll(sugg); - } - return suggestions; - } - - @Override - public boolean isInDictionary(final String word) { - for (int i = mDictionaries.size() - 1; i >= 0; --i) - if (mDictionaries.get(i).isInDictionary(word)) return true; - return false; - } - - @Override - public int getFrequency(final String word) { - int maxFreq = -1; - for (int i = mDictionaries.size() - 1; i >= 0; --i) { - final int tempFreq = mDictionaries.get(i).getFrequency(word); - maxFreq = Math.max(tempFreq, maxFreq); - } - return maxFreq; - } - - @Override - public int getMaxFrequencyOfExactMatches(final String word) { - int maxFreq = -1; - for (int i = mDictionaries.size() - 1; i >= 0; --i) { - final int tempFreq = mDictionaries.get(i).getMaxFrequencyOfExactMatches(word); - maxFreq = Math.max(tempFreq, maxFreq); - } - return maxFreq; - } - - @Override - public boolean isInitialized() { - return !mDictionaries.isEmpty(); - } - - @Override - public void close() { - for (final Dictionary dict : mDictionaries) - dict.close(); - } - - // Warning: this is not thread-safe. Take necessary precaution when calling. - public void addDictionary(final Dictionary newDict) { - if (null == newDict) return; - if (mDictionaries.contains(newDict)) { - Log.w(TAG, "This collection already contains this dictionary: " + newDict); - } - mDictionaries.add(newDict); - } - - // Warning: this is not thread-safe. Take necessary precaution when calling. - public void removeDictionary(final Dictionary dict) { - if (mDictionaries.contains(dict)) { - mDictionaries.remove(dict); - } else { - Log.w(TAG, "This collection does not contain this dictionary: " + dict); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryDumpBroadcastReceiver.java b/java/src/com/android/inputmethod/latin/DictionaryDumpBroadcastReceiver.java deleted file mode 100644 index ee2fdc6c7..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryDumpBroadcastReceiver.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2014 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.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -public class DictionaryDumpBroadcastReceiver extends BroadcastReceiver { - private static final String TAG = DictionaryDumpBroadcastReceiver.class.getSimpleName(); - - private static final String DOMAIN = "com.android.inputmethod.latin"; - public static final String DICTIONARY_DUMP_INTENT_ACTION = DOMAIN + ".DICT_DUMP"; - public static final String DICTIONARY_NAME_KEY = "dictName"; - - final LatinIME mLatinIme; - - public DictionaryDumpBroadcastReceiver(final LatinIME latinIme) { - mLatinIme = latinIme; - } - - @Override - public void onReceive(Context context, Intent intent) { - final String action = intent.getAction(); - if (action.equals(DICTIONARY_DUMP_INTENT_ACTION)) { - final String dictName = intent.getStringExtra(DICTIONARY_NAME_KEY); - if (dictName == null) { - Log.e(TAG, "Received dictionary dump intent action " + - "but the dictionary name is not set."); - return; - } - mLatinIme.dumpDictionaryForDebug(dictName); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java deleted file mode 100644 index 02015da09..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (C) 2013 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.Context; -import android.util.LruCache; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; -import com.android.inputmethod.latin.utils.SuggestionResults; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Interface that facilitates interaction with different kinds of dictionaries. Provides APIs to - * instantiate and select the correct dictionaries (based on language or account), update entries - * and fetch suggestions. Currently AndroidSpellCheckerService and LatinIME both use - * DictionaryFacilitator as a client for interacting with dictionaries. - */ -public interface DictionaryFacilitator { - - public static final String[] ALL_DICTIONARY_TYPES = new String[] { - Dictionary.TYPE_MAIN, - Dictionary.TYPE_CONTACTS, - Dictionary.TYPE_USER_HISTORY, - Dictionary.TYPE_USER}; - - public static final String[] DYNAMIC_DICTIONARY_TYPES = new String[] { - Dictionary.TYPE_CONTACTS, - Dictionary.TYPE_USER_HISTORY, - Dictionary.TYPE_USER}; - - /** - * The facilitator will put words into the cache whenever it decodes them. - * @param cache - */ - void setValidSpellingWordReadCache(final LruCache<String, Boolean> cache); - - /** - * The facilitator will get words from the cache whenever it needs to check their spelling. - * @param cache - */ - void setValidSpellingWordWriteCache(final LruCache<String, Boolean> cache); - - /** - * Returns whether this facilitator is exactly for this locale. - * - * @param locale the locale to test against - */ - boolean isForLocale(final Locale locale); - - /** - * Returns whether this facilitator is exactly for this account. - * - * @param account the account to test against. - */ - boolean isForAccount(@Nullable final String account); - - interface DictionaryInitializationListener { - void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable); - } - - /** - * Called every time {@link LatinIME} starts on a new text field. - * Dot not affect {@link AndroidSpellCheckerService}. - * - * WARNING: The service methods that call start/finish are very spammy. - */ - void onStartInput(); - - /** - * Called every time the {@link LatinIME} finishes with the current text field. - * May be followed by {@link #onStartInput} again in another text field, - * or it may be done for a while. - * Dot not affect {@link AndroidSpellCheckerService}. - * - * WARNING: The service methods that call start/finish are very spammy. - */ - void onFinishInput(Context context); - - boolean isActive(); - - Locale getLocale(); - - boolean usesContacts(); - - String getAccount(); - - void resetDictionaries( - final Context context, - final Locale newLocale, - final boolean useContactsDict, - final boolean usePersonalizedDicts, - final boolean forceReloadMainDictionary, - @Nullable final String account, - final String dictNamePrefix, - @Nullable final DictionaryInitializationListener listener); - - @UsedForTesting - void resetDictionariesForTesting( - final Context context, - final Locale locale, - final ArrayList<String> dictionaryTypes, - final HashMap<String, File> dictionaryFiles, - final Map<String, Map<String, String>> additionalDictAttributes, - @Nullable final String account); - - void closeDictionaries(); - - @UsedForTesting - ExpandableBinaryDictionary getSubDictForTesting(final String dictName); - - // The main dictionaries are loaded asynchronously. Don't cache the return value - // of these methods. - boolean hasAtLeastOneInitializedMainDictionary(); - - boolean hasAtLeastOneUninitializedMainDictionary(); - - void waitForLoadingMainDictionaries(final long timeout, final TimeUnit unit) - throws InterruptedException; - - @UsedForTesting - void waitForLoadingDictionariesForTesting(final long timeout, final TimeUnit unit) - throws InterruptedException; - - void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized, - @Nonnull final NgramContext ngramContext, final long timeStampInSeconds, - final boolean blockPotentiallyOffensive); - - void unlearnFromUserHistory(final String word, - @Nonnull final NgramContext ngramContext, final long timeStampInSeconds, - final int eventType); - - // TODO: Revise the way to fusion suggestion results. - @Nonnull SuggestionResults getSuggestionResults(final ComposedData composedData, - final NgramContext ngramContext, @Nonnull final Keyboard keyboard, - final SettingsValuesForSuggestion settingsValuesForSuggestion, final int sessionId, - final int inputStyle); - - boolean isValidSpellingWord(final String word); - - boolean isValidSuggestionWord(final String word); - - boolean clearUserHistoryDictionary(final Context context); - - String dump(final Context context); - - void dumpDictionaryForDebug(final String dictName); - - @Nonnull List<DictionaryStats> getDictionaryStats(final Context context); -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorImpl.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorImpl.java deleted file mode 100644 index b435de867..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorImpl.java +++ /dev/null @@ -1,736 +0,0 @@ -/* -7 * Copyright (C) 2013 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.Manifest; -import android.content.Context; -import android.text.TextUtils; -import android.util.Log; -import android.util.LruCache; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.latin.NgramContext.WordInfo; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.permissions.PermissionsUtil; -import com.android.inputmethod.latin.personalization.UserHistoryDictionary; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; -import com.android.inputmethod.latin.utils.ExecutorUtils; -import com.android.inputmethod.latin.utils.SuggestionResults; - -import java.io.File; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Facilitates interaction with different kinds of dictionaries. Provides APIs - * to instantiate and select the correct dictionaries (based on language or account), - * update entries and fetch suggestions. - * - * Currently AndroidSpellCheckerService and LatinIME both use DictionaryFacilitator as - * a client for interacting with dictionaries. - */ -public class DictionaryFacilitatorImpl implements DictionaryFacilitator { - // TODO: Consolidate dictionaries in native code. - public static final String TAG = DictionaryFacilitatorImpl.class.getSimpleName(); - - // HACK: This threshold is being used when adding a capitalized entry in the User History - // dictionary. - private static final int CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT = 140; - - private DictionaryGroup mDictionaryGroup = new DictionaryGroup(); - private volatile CountDownLatch mLatchForWaitingLoadingMainDictionaries = new CountDownLatch(0); - // To synchronize assigning mDictionaryGroup to ensure closing dictionaries. - private final Object mLock = new Object(); - - public static final Map<String, Class<? extends ExpandableBinaryDictionary>> - DICT_TYPE_TO_CLASS = new HashMap<>(); - - static { - DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_USER_HISTORY, UserHistoryDictionary.class); - DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_USER, UserBinaryDictionary.class); - DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_CONTACTS, ContactsBinaryDictionary.class); - } - - private static final String DICT_FACTORY_METHOD_NAME = "getDictionary"; - private static final Class<?>[] DICT_FACTORY_METHOD_ARG_TYPES = - new Class[] { Context.class, Locale.class, File.class, String.class, String.class }; - - private LruCache<String, Boolean> mValidSpellingWordReadCache; - private LruCache<String, Boolean> mValidSpellingWordWriteCache; - - @Override - public void setValidSpellingWordReadCache(final LruCache<String, Boolean> cache) { - mValidSpellingWordReadCache = cache; - } - - @Override - public void setValidSpellingWordWriteCache(final LruCache<String, Boolean> cache) { - mValidSpellingWordWriteCache = cache; - } - - @Override - public boolean isForLocale(final Locale locale) { - return locale != null && locale.equals(mDictionaryGroup.mLocale); - } - - /** - * Returns whether this facilitator is exactly for this account. - * - * @param account the account to test against. - */ - public boolean isForAccount(@Nullable final String account) { - return TextUtils.equals(mDictionaryGroup.mAccount, account); - } - - /** - * A group of dictionaries that work together for a single language. - */ - private static class DictionaryGroup { - // TODO: Add null analysis annotations. - // TODO: Run evaluation to determine a reasonable value for these constants. The current - // values are ad-hoc and chosen without any particular care or methodology. - public static final float WEIGHT_FOR_MOST_PROBABLE_LANGUAGE = 1.0f; - public static final float WEIGHT_FOR_GESTURING_IN_NOT_MOST_PROBABLE_LANGUAGE = 0.95f; - public static final float WEIGHT_FOR_TYPING_IN_NOT_MOST_PROBABLE_LANGUAGE = 0.6f; - - /** - * The locale associated with the dictionary group. - */ - @Nullable public final Locale mLocale; - - /** - * The user account associated with the dictionary group. - */ - @Nullable public final String mAccount; - - @Nullable private Dictionary mMainDict; - // Confidence that the most probable language is actually the language the user is - // typing in. For now, this is simply the number of times a word from this language - // has been committed in a row. - private int mConfidence = 0; - - public float mWeightForTypingInLocale = WEIGHT_FOR_MOST_PROBABLE_LANGUAGE; - public float mWeightForGesturingInLocale = WEIGHT_FOR_MOST_PROBABLE_LANGUAGE; - public final ConcurrentHashMap<String, ExpandableBinaryDictionary> mSubDictMap = - new ConcurrentHashMap<>(); - - public DictionaryGroup() { - this(null /* locale */, null /* mainDict */, null /* account */, - Collections.<String, ExpandableBinaryDictionary>emptyMap() /* subDicts */); - } - - public DictionaryGroup(@Nullable final Locale locale, - @Nullable final Dictionary mainDict, - @Nullable final String account, - final Map<String, ExpandableBinaryDictionary> subDicts) { - mLocale = locale; - mAccount = account; - // The main dictionary can be asynchronously loaded. - setMainDict(mainDict); - for (final Map.Entry<String, ExpandableBinaryDictionary> entry : subDicts.entrySet()) { - setSubDict(entry.getKey(), entry.getValue()); - } - } - - private void setSubDict(final String dictType, final ExpandableBinaryDictionary dict) { - if (dict != null) { - mSubDictMap.put(dictType, dict); - } - } - - public void setMainDict(final Dictionary mainDict) { - // Close old dictionary if exists. Main dictionary can be assigned multiple times. - final Dictionary oldDict = mMainDict; - mMainDict = mainDict; - if (oldDict != null && mainDict != oldDict) { - oldDict.close(); - } - } - - public Dictionary getDict(final String dictType) { - if (Dictionary.TYPE_MAIN.equals(dictType)) { - return mMainDict; - } - return getSubDict(dictType); - } - - public ExpandableBinaryDictionary getSubDict(final String dictType) { - return mSubDictMap.get(dictType); - } - - public boolean hasDict(final String dictType, @Nullable final String account) { - if (Dictionary.TYPE_MAIN.equals(dictType)) { - return mMainDict != null; - } - if (Dictionary.TYPE_USER_HISTORY.equals(dictType) && - !TextUtils.equals(account, mAccount)) { - // If the dictionary type is user history, & if the account doesn't match, - // return immediately. If the account matches, continue looking it up in the - // sub dictionary map. - return false; - } - return mSubDictMap.containsKey(dictType); - } - - public void closeDict(final String dictType) { - final Dictionary dict; - if (Dictionary.TYPE_MAIN.equals(dictType)) { - dict = mMainDict; - } else { - dict = mSubDictMap.remove(dictType); - } - if (dict != null) { - dict.close(); - } - } - } - - public DictionaryFacilitatorImpl() { - } - - @Override - public void onStartInput() { - } - - @Override - public void onFinishInput(Context context) { - } - - @Override - public boolean isActive() { - return mDictionaryGroup.mLocale != null; - } - - @Override - public Locale getLocale() { - return mDictionaryGroup.mLocale; - } - - @Override - public boolean usesContacts() { - return mDictionaryGroup.getSubDict(Dictionary.TYPE_CONTACTS) != null; - } - - @Override - public String getAccount() { - return null; - } - - @Nullable - private static ExpandableBinaryDictionary getSubDict(final String dictType, - final Context context, final Locale locale, final File dictFile, - final String dictNamePrefix, @Nullable final String account) { - final Class<? extends ExpandableBinaryDictionary> dictClass = - DICT_TYPE_TO_CLASS.get(dictType); - if (dictClass == null) { - return null; - } - try { - final Method factoryMethod = dictClass.getMethod(DICT_FACTORY_METHOD_NAME, - DICT_FACTORY_METHOD_ARG_TYPES); - final Object dict = factoryMethod.invoke(null /* obj */, - new Object[] { context, locale, dictFile, dictNamePrefix, account }); - return (ExpandableBinaryDictionary) dict; - } catch (final NoSuchMethodException | SecurityException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException e) { - Log.e(TAG, "Cannot create dictionary: " + dictType, e); - return null; - } - } - - @Nullable - static DictionaryGroup findDictionaryGroupWithLocale(final DictionaryGroup dictionaryGroup, - final Locale locale) { - return locale.equals(dictionaryGroup.mLocale) ? dictionaryGroup : null; - } - - @Override - public void resetDictionaries( - final Context context, - final Locale newLocale, - final boolean useContactsDict, - final boolean usePersonalizedDicts, - final boolean forceReloadMainDictionary, - @Nullable final String account, - final String dictNamePrefix, - @Nullable final DictionaryInitializationListener listener) { - final HashMap<Locale, ArrayList<String>> existingDictionariesToCleanup = new HashMap<>(); - // TODO: Make subDictTypesToUse configurable by resource or a static final list. - final HashSet<String> subDictTypesToUse = new HashSet<>(); - subDictTypesToUse.add(Dictionary.TYPE_USER); - - // Do not use contacts dictionary if we do not have permissions to read contacts. - final boolean contactsPermissionGranted = PermissionsUtil.checkAllPermissionsGranted( - context, Manifest.permission.READ_CONTACTS); - if (useContactsDict && contactsPermissionGranted) { - subDictTypesToUse.add(Dictionary.TYPE_CONTACTS); - } - if (usePersonalizedDicts) { - subDictTypesToUse.add(Dictionary.TYPE_USER_HISTORY); - } - - // Gather all dictionaries. We'll remove them from the list to clean up later. - final ArrayList<String> dictTypeForLocale = new ArrayList<>(); - existingDictionariesToCleanup.put(newLocale, dictTypeForLocale); - final DictionaryGroup currentDictionaryGroupForLocale = - findDictionaryGroupWithLocale(mDictionaryGroup, newLocale); - if (currentDictionaryGroupForLocale != null) { - for (final String dictType : DYNAMIC_DICTIONARY_TYPES) { - if (currentDictionaryGroupForLocale.hasDict(dictType, account)) { - dictTypeForLocale.add(dictType); - } - } - if (currentDictionaryGroupForLocale.hasDict(Dictionary.TYPE_MAIN, account)) { - dictTypeForLocale.add(Dictionary.TYPE_MAIN); - } - } - - final DictionaryGroup dictionaryGroupForLocale = - findDictionaryGroupWithLocale(mDictionaryGroup, newLocale); - final ArrayList<String> dictTypesToCleanupForLocale = - existingDictionariesToCleanup.get(newLocale); - final boolean noExistingDictsForThisLocale = (null == dictionaryGroupForLocale); - - final Dictionary mainDict; - if (forceReloadMainDictionary || noExistingDictsForThisLocale - || !dictionaryGroupForLocale.hasDict(Dictionary.TYPE_MAIN, account)) { - mainDict = null; - } else { - mainDict = dictionaryGroupForLocale.getDict(Dictionary.TYPE_MAIN); - dictTypesToCleanupForLocale.remove(Dictionary.TYPE_MAIN); - } - - final Map<String, ExpandableBinaryDictionary> subDicts = new HashMap<>(); - for (final String subDictType : subDictTypesToUse) { - final ExpandableBinaryDictionary subDict; - if (noExistingDictsForThisLocale - || !dictionaryGroupForLocale.hasDict(subDictType, account)) { - // Create a new dictionary. - subDict = getSubDict(subDictType, context, newLocale, null /* dictFile */, - dictNamePrefix, account); - } else { - // Reuse the existing dictionary, and don't close it at the end - subDict = dictionaryGroupForLocale.getSubDict(subDictType); - dictTypesToCleanupForLocale.remove(subDictType); - } - subDicts.put(subDictType, subDict); - } - DictionaryGroup newDictionaryGroup = - new DictionaryGroup(newLocale, mainDict, account, subDicts); - - // Replace Dictionaries. - final DictionaryGroup oldDictionaryGroup; - synchronized (mLock) { - oldDictionaryGroup = mDictionaryGroup; - mDictionaryGroup = newDictionaryGroup; - if (hasAtLeastOneUninitializedMainDictionary()) { - asyncReloadUninitializedMainDictionaries(context, newLocale, listener); - } - } - if (listener != null) { - listener.onUpdateMainDictionaryAvailability(hasAtLeastOneInitializedMainDictionary()); - } - - // Clean up old dictionaries. - for (final Locale localeToCleanUp : existingDictionariesToCleanup.keySet()) { - final ArrayList<String> dictTypesToCleanUp = - existingDictionariesToCleanup.get(localeToCleanUp); - final DictionaryGroup dictionarySetToCleanup = - findDictionaryGroupWithLocale(oldDictionaryGroup, localeToCleanUp); - for (final String dictType : dictTypesToCleanUp) { - dictionarySetToCleanup.closeDict(dictType); - } - } - - if (mValidSpellingWordWriteCache != null) { - mValidSpellingWordWriteCache.evictAll(); - } - } - - private void asyncReloadUninitializedMainDictionaries(final Context context, - final Locale locale, final DictionaryInitializationListener listener) { - final CountDownLatch latchForWaitingLoadingMainDictionary = new CountDownLatch(1); - mLatchForWaitingLoadingMainDictionaries = latchForWaitingLoadingMainDictionary; - ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD).execute(new Runnable() { - @Override - public void run() { - doReloadUninitializedMainDictionaries( - context, locale, listener, latchForWaitingLoadingMainDictionary); - } - }); - } - - void doReloadUninitializedMainDictionaries(final Context context, final Locale locale, - final DictionaryInitializationListener listener, - final CountDownLatch latchForWaitingLoadingMainDictionary) { - final DictionaryGroup dictionaryGroup = - findDictionaryGroupWithLocale(mDictionaryGroup, locale); - if (null == dictionaryGroup) { - // This should never happen, but better safe than crashy - Log.w(TAG, "Expected a dictionary group for " + locale + " but none found"); - return; - } - final Dictionary mainDict = - DictionaryFactory.createMainDictionaryFromManager(context, locale); - synchronized (mLock) { - if (locale.equals(dictionaryGroup.mLocale)) { - dictionaryGroup.setMainDict(mainDict); - } else { - // Dictionary facilitator has been reset for another locale. - mainDict.close(); - } - } - if (listener != null) { - listener.onUpdateMainDictionaryAvailability(hasAtLeastOneInitializedMainDictionary()); - } - latchForWaitingLoadingMainDictionary.countDown(); - } - - @UsedForTesting - public void resetDictionariesForTesting(final Context context, final Locale locale, - final ArrayList<String> dictionaryTypes, final HashMap<String, File> dictionaryFiles, - final Map<String, Map<String, String>> additionalDictAttributes, - @Nullable final String account) { - Dictionary mainDictionary = null; - final Map<String, ExpandableBinaryDictionary> subDicts = new HashMap<>(); - - for (final String dictType : dictionaryTypes) { - if (dictType.equals(Dictionary.TYPE_MAIN)) { - mainDictionary = DictionaryFactory.createMainDictionaryFromManager(context, - locale); - } else { - final File dictFile = dictionaryFiles.get(dictType); - final ExpandableBinaryDictionary dict = getSubDict( - dictType, context, locale, dictFile, "" /* dictNamePrefix */, account); - if (additionalDictAttributes.containsKey(dictType)) { - dict.clearAndFlushDictionaryWithAdditionalAttributes( - additionalDictAttributes.get(dictType)); - } - if (dict == null) { - throw new RuntimeException("Unknown dictionary type: " + dictType); - } - dict.reloadDictionaryIfRequired(); - dict.waitAllTasksForTests(); - subDicts.put(dictType, dict); - } - } - mDictionaryGroup = new DictionaryGroup(locale, mainDictionary, account, subDicts); - } - - public void closeDictionaries() { - final DictionaryGroup dictionaryGroupToClose; - synchronized (mLock) { - dictionaryGroupToClose = mDictionaryGroup; - mDictionaryGroup = new DictionaryGroup(); - } - for (final String dictType : ALL_DICTIONARY_TYPES) { - dictionaryGroupToClose.closeDict(dictType); - } - } - - @UsedForTesting - public ExpandableBinaryDictionary getSubDictForTesting(final String dictName) { - return mDictionaryGroup.getSubDict(dictName); - } - - // The main dictionaries are loaded asynchronously. Don't cache the return value - // of these methods. - public boolean hasAtLeastOneInitializedMainDictionary() { - final Dictionary mainDict = mDictionaryGroup.getDict(Dictionary.TYPE_MAIN); - if (mainDict != null && mainDict.isInitialized()) { - return true; - } - return false; - } - - public boolean hasAtLeastOneUninitializedMainDictionary() { - final Dictionary mainDict = mDictionaryGroup.getDict(Dictionary.TYPE_MAIN); - if (mainDict == null || !mainDict.isInitialized()) { - return true; - } - return false; - } - - public void waitForLoadingMainDictionaries(final long timeout, final TimeUnit unit) - throws InterruptedException { - mLatchForWaitingLoadingMainDictionaries.await(timeout, unit); - } - - @UsedForTesting - public void waitForLoadingDictionariesForTesting(final long timeout, final TimeUnit unit) - throws InterruptedException { - waitForLoadingMainDictionaries(timeout, unit); - for (final ExpandableBinaryDictionary dict : mDictionaryGroup.mSubDictMap.values()) { - dict.waitAllTasksForTests(); - } - } - - public void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized, - @Nonnull final NgramContext ngramContext, final long timeStampInSeconds, - final boolean blockPotentiallyOffensive) { - // Update the spelling cache before learning. Words that are not yet added to user history - // and appear in no other language model are not considered valid. - putWordIntoValidSpellingWordCache("addToUserHistory", suggestion); - - final String[] words = suggestion.split(Constants.WORD_SEPARATOR); - NgramContext ngramContextForCurrentWord = ngramContext; - for (int i = 0; i < words.length; i++) { - final String currentWord = words[i]; - final boolean wasCurrentWordAutoCapitalized = (i == 0) ? wasAutoCapitalized : false; - addWordToUserHistory(mDictionaryGroup, ngramContextForCurrentWord, currentWord, - wasCurrentWordAutoCapitalized, (int) timeStampInSeconds, - blockPotentiallyOffensive); - ngramContextForCurrentWord = - ngramContextForCurrentWord.getNextNgramContext(new WordInfo(currentWord)); - } - } - - private void putWordIntoValidSpellingWordCache( - @Nonnull final String caller, - @Nonnull final String originalWord) { - if (mValidSpellingWordWriteCache == null) { - return; - } - - final String lowerCaseWord = originalWord.toLowerCase(getLocale()); - final boolean lowerCaseValid = isValidSpellingWord(lowerCaseWord); - mValidSpellingWordWriteCache.put(lowerCaseWord, lowerCaseValid); - - final String capitalWord = - StringUtils.capitalizeFirstAndDowncaseRest(originalWord, getLocale()); - final boolean capitalValid; - if (lowerCaseValid) { - // The lower case form of the word is valid, so the upper case must be valid. - capitalValid = true; - } else { - capitalValid = isValidSpellingWord(capitalWord); - } - mValidSpellingWordWriteCache.put(capitalWord, capitalValid); - } - - private void addWordToUserHistory(final DictionaryGroup dictionaryGroup, - final NgramContext ngramContext, final String word, final boolean wasAutoCapitalized, - final int timeStampInSeconds, final boolean blockPotentiallyOffensive) { - final ExpandableBinaryDictionary userHistoryDictionary = - dictionaryGroup.getSubDict(Dictionary.TYPE_USER_HISTORY); - if (userHistoryDictionary == null || !isForLocale(userHistoryDictionary.mLocale)) { - return; - } - final int maxFreq = getFrequency(word); - if (maxFreq == 0 && blockPotentiallyOffensive) { - return; - } - final String lowerCasedWord = word.toLowerCase(dictionaryGroup.mLocale); - final String secondWord; - if (wasAutoCapitalized) { - if (isValidSuggestionWord(word) && !isValidSuggestionWord(lowerCasedWord)) { - // If the word was auto-capitalized and exists only as a capitalized word in the - // dictionary, then we must not downcase it before registering it. For example, - // the name of the contacts in start-of-sentence position would come here with the - // wasAutoCapitalized flag: if we downcase it, we'd register a lower-case version - // of that contact's name which would end up popping in suggestions. - secondWord = word; - } else { - // If however the word is not in the dictionary, or exists as a lower-case word - // only, then we consider that was a lower-case word that had been auto-capitalized. - secondWord = lowerCasedWord; - } - } else { - // HACK: We'd like to avoid adding the capitalized form of common words to the User - // History dictionary in order to avoid suggesting them until the dictionary - // consolidation is done. - // TODO: Remove this hack when ready. - final int lowerCaseFreqInMainDict = dictionaryGroup.hasDict(Dictionary.TYPE_MAIN, - null /* account */) ? - dictionaryGroup.getDict(Dictionary.TYPE_MAIN).getFrequency(lowerCasedWord) : - Dictionary.NOT_A_PROBABILITY; - if (maxFreq < lowerCaseFreqInMainDict - && lowerCaseFreqInMainDict >= CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT) { - // Use lower cased word as the word can be a distracter of the popular word. - secondWord = lowerCasedWord; - } else { - secondWord = word; - } - } - // We demote unrecognized words (frequency < 0, below) by specifying them as "invalid". - // We don't add words with 0-frequency (assuming they would be profanity etc.). - final boolean isValid = maxFreq > 0; - UserHistoryDictionary.addToDictionary(userHistoryDictionary, ngramContext, secondWord, - isValid, timeStampInSeconds); - } - - private void removeWord(final String dictName, final String word) { - final ExpandableBinaryDictionary dictionary = mDictionaryGroup.getSubDict(dictName); - if (dictionary != null) { - dictionary.removeUnigramEntryDynamically(word); - } - } - - @Override - public void unlearnFromUserHistory(final String word, - @Nonnull final NgramContext ngramContext, final long timeStampInSeconds, - final int eventType) { - // TODO: Decide whether or not to remove the word on EVENT_BACKSPACE. - if (eventType != Constants.EVENT_BACKSPACE) { - removeWord(Dictionary.TYPE_USER_HISTORY, word); - } - - // Update the spelling cache after unlearning. Words that are removed from user history - // and appear in no other language model are not considered valid. - putWordIntoValidSpellingWordCache("unlearnFromUserHistory", word.toLowerCase()); - } - - // TODO: Revise the way to fusion suggestion results. - @Override - @Nonnull public SuggestionResults getSuggestionResults(ComposedData composedData, - NgramContext ngramContext, @Nonnull final Keyboard keyboard, - SettingsValuesForSuggestion settingsValuesForSuggestion, int sessionId, - int inputStyle) { - long proximityInfoHandle = keyboard.getProximityInfo().getNativeProximityInfo(); - final SuggestionResults suggestionResults = new SuggestionResults( - SuggestedWords.MAX_SUGGESTIONS, ngramContext.isBeginningOfSentenceContext(), - false /* firstSuggestionExceedsConfidenceThreshold */); - final float[] weightOfLangModelVsSpatialModel = - new float[] { Dictionary.NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL }; - for (final String dictType : ALL_DICTIONARY_TYPES) { - final Dictionary dictionary = mDictionaryGroup.getDict(dictType); - if (null == dictionary) continue; - final float weightForLocale = composedData.mIsBatchMode - ? mDictionaryGroup.mWeightForGesturingInLocale - : mDictionaryGroup.mWeightForTypingInLocale; - final ArrayList<SuggestedWordInfo> dictionarySuggestions = - dictionary.getSuggestions(composedData, ngramContext, - proximityInfoHandle, settingsValuesForSuggestion, sessionId, - weightForLocale, weightOfLangModelVsSpatialModel); - if (null == dictionarySuggestions) continue; - suggestionResults.addAll(dictionarySuggestions); - if (null != suggestionResults.mRawSuggestions) { - suggestionResults.mRawSuggestions.addAll(dictionarySuggestions); - } - } - return suggestionResults; - } - - public boolean isValidSpellingWord(final String word) { - if (mValidSpellingWordReadCache != null) { - final Boolean cachedValue = mValidSpellingWordReadCache.get(word); - if (cachedValue != null) { - return cachedValue; - } - } - - return isValidWord(word, ALL_DICTIONARY_TYPES); - } - - public boolean isValidSuggestionWord(final String word) { - return isValidWord(word, ALL_DICTIONARY_TYPES); - } - - private boolean isValidWord(final String word, final String[] dictionariesToCheck) { - if (TextUtils.isEmpty(word)) { - return false; - } - if (mDictionaryGroup.mLocale == null) { - return false; - } - for (final String dictType : dictionariesToCheck) { - final Dictionary dictionary = mDictionaryGroup.getDict(dictType); - // Ideally the passed map would come out of a {@link java.util.concurrent.Future} and - // would be immutable once it's finished initializing, but concretely a null test is - // probably good enough for the time being. - if (null == dictionary) continue; - if (dictionary.isValidWord(word)) { - return true; - } - } - return false; - } - - private int getFrequency(final String word) { - if (TextUtils.isEmpty(word)) { - return Dictionary.NOT_A_PROBABILITY; - } - int maxFreq = Dictionary.NOT_A_PROBABILITY; - for (final String dictType : ALL_DICTIONARY_TYPES) { - final Dictionary dictionary = mDictionaryGroup.getDict(dictType); - if (dictionary == null) continue; - final int tempFreq = dictionary.getFrequency(word); - if (tempFreq >= maxFreq) { - maxFreq = tempFreq; - } - } - return maxFreq; - } - - private boolean clearSubDictionary(final String dictName) { - final ExpandableBinaryDictionary dictionary = mDictionaryGroup.getSubDict(dictName); - if (dictionary == null) { - return false; - } - dictionary.clear(); - return true; - } - - @Override - public boolean clearUserHistoryDictionary(final Context context) { - return clearSubDictionary(Dictionary.TYPE_USER_HISTORY); - } - - @Override - public void dumpDictionaryForDebug(final String dictName) { - final ExpandableBinaryDictionary dictToDump = mDictionaryGroup.getSubDict(dictName); - if (dictToDump == null) { - Log.e(TAG, "Cannot dump " + dictName + ". " - + "The dictionary is not being used for suggestion or cannot be dumped."); - return; - } - dictToDump.dumpAllWordsForDebug(); - } - - @Override - @Nonnull public List<DictionaryStats> getDictionaryStats(final Context context) { - final ArrayList<DictionaryStats> statsOfEnabledSubDicts = new ArrayList<>(); - for (final String dictType : DYNAMIC_DICTIONARY_TYPES) { - final ExpandableBinaryDictionary dictionary = mDictionaryGroup.getSubDict(dictType); - if (dictionary == null) continue; - statsOfEnabledSubDicts.add(dictionary.getDictionaryStats()); - } - return statsOfEnabledSubDicts; - } - - @Override - public String dump(final Context context) { - return ""; - } -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorLruCache.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorLruCache.java deleted file mode 100644 index cbaf6ea4e..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorLruCache.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2014 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 java.util.Locale; -import java.util.concurrent.TimeUnit; - -import android.content.Context; -import android.util.Log; - -/** - * Cache for dictionary facilitators of multiple locales. - * This class automatically creates and releases up to 3 facilitator instances using LRU policy. - */ -public class DictionaryFacilitatorLruCache { - private static final String TAG = "DictionaryFacilitatorLruCache"; - private static final int WAIT_FOR_LOADING_MAIN_DICT_IN_MILLISECONDS = 1000; - private static final int MAX_RETRY_COUNT_FOR_WAITING_FOR_LOADING_DICT = 5; - - private final Context mContext; - private final String mDictionaryNamePrefix; - private final Object mLock = new Object(); - private final DictionaryFacilitator mDictionaryFacilitator; - private boolean mUseContactsDictionary; - private Locale mLocale; - - public DictionaryFacilitatorLruCache(final Context context, final String dictionaryNamePrefix) { - mContext = context; - mDictionaryNamePrefix = dictionaryNamePrefix; - mDictionaryFacilitator = DictionaryFacilitatorProvider.getDictionaryFacilitator( - true /* isNeededForSpellChecking */); - } - - private static void waitForLoadingMainDictionary( - final DictionaryFacilitator dictionaryFacilitator) { - for (int i = 0; i < MAX_RETRY_COUNT_FOR_WAITING_FOR_LOADING_DICT; i++) { - try { - dictionaryFacilitator.waitForLoadingMainDictionaries( - WAIT_FOR_LOADING_MAIN_DICT_IN_MILLISECONDS, TimeUnit.MILLISECONDS); - return; - } catch (final InterruptedException e) { - Log.i(TAG, "Interrupted during waiting for loading main dictionary.", e); - if (i < MAX_RETRY_COUNT_FOR_WAITING_FOR_LOADING_DICT - 1) { - Log.i(TAG, "Retry", e); - } else { - Log.w(TAG, "Give up retrying. Retried " - + MAX_RETRY_COUNT_FOR_WAITING_FOR_LOADING_DICT + " times.", e); - } - } - } - } - - private void resetDictionariesForLocaleLocked() { - // Nothing to do if the locale is null. This would be the case before any get() calls. - if (mLocale != null) { - // Note: Given that personalized dictionaries are not used here; we can pass null account. - mDictionaryFacilitator.resetDictionaries(mContext, mLocale, - mUseContactsDictionary, false /* usePersonalizedDicts */, - false /* forceReloadMainDictionary */, null /* account */, - mDictionaryNamePrefix, null /* listener */); - } - } - - public void setUseContactsDictionary(final boolean useContactsDictionary) { - synchronized (mLock) { - if (mUseContactsDictionary == useContactsDictionary) { - // The value has not been changed. - return; - } - mUseContactsDictionary = useContactsDictionary; - resetDictionariesForLocaleLocked(); - waitForLoadingMainDictionary(mDictionaryFacilitator); - } - } - - public DictionaryFacilitator get(final Locale locale) { - synchronized (mLock) { - if (!mDictionaryFacilitator.isForLocale(locale)) { - mLocale = locale; - resetDictionariesForLocaleLocked(); - } - waitForLoadingMainDictionary(mDictionaryFacilitator); - return mDictionaryFacilitator; - } - } - - public void closeDictionaries() { - synchronized (mLock) { - mDictionaryFacilitator.closeDictionaries(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorProvider.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorProvider.java deleted file mode 100644 index a48b41fa7..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorProvider.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (C) 2013 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; - -/** - * Factory for instantiating DictionaryFacilitator objects. - */ -public class DictionaryFacilitatorProvider { - public static DictionaryFacilitator getDictionaryFacilitator(boolean isNeededForSpellChecking) { - return new DictionaryFacilitatorImpl(); - } -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java deleted file mode 100644 index 5dd02bd1c..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import android.content.ContentProviderClient; -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.util.Log; - -import com.android.inputmethod.latin.utils.DictionaryInfoUtils; - -import java.io.File; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.Locale; - -/** - * Factory for dictionary instances. - */ -public final class DictionaryFactory { - private static final String TAG = DictionaryFactory.class.getSimpleName(); - - /** - * Initializes a main dictionary collection from a dictionary pack, with explicit flags. - * - * This searches for a content provider providing a dictionary pack for the specified - * locale. If none is found, it falls back to the built-in dictionary - if any. - * @param context application context for reading resources - * @param locale the locale for which to create the dictionary - * @return an initialized instance of DictionaryCollection - */ - public static DictionaryCollection createMainDictionaryFromManager(final Context context, - final Locale locale) { - if (null == locale) { - Log.e(TAG, "No locale defined for dictionary"); - return new DictionaryCollection(Dictionary.TYPE_MAIN, locale, - createReadOnlyBinaryDictionary(context, locale)); - } - - final LinkedList<Dictionary> dictList = new LinkedList<>(); - final ArrayList<AssetFileAddress> assetFileList = - BinaryDictionaryGetter.getDictionaryFiles(locale, context, true); - if (null != assetFileList) { - for (final AssetFileAddress f : assetFileList) { - final ReadOnlyBinaryDictionary readOnlyBinaryDictionary = - new ReadOnlyBinaryDictionary(f.mFilename, f.mOffset, f.mLength, - false /* useFullEditDistance */, locale, Dictionary.TYPE_MAIN); - if (readOnlyBinaryDictionary.isValidDictionary()) { - dictList.add(readOnlyBinaryDictionary); - } else { - readOnlyBinaryDictionary.close(); - // Prevent this dictionary to do any further harm. - killDictionary(context, f); - } - } - } - - // If the list is empty, that means we should not use any dictionary (for example, the user - // explicitly disabled the main dictionary), so the following is okay. dictList is never - // null, but if for some reason it is, DictionaryCollection handles it gracefully. - return new DictionaryCollection(Dictionary.TYPE_MAIN, locale, dictList); - } - - /** - * Kills a dictionary so that it is never used again, if possible. - * @param context The context to contact the dictionary provider, if possible. - * @param f A file address to the dictionary to kill. - */ - public static void killDictionary(final Context context, final AssetFileAddress f) { - if (f.pointsToPhysicalFile()) { - f.deleteUnderlyingFile(); - // Warn the dictionary provider if the dictionary came from there. - final ContentProviderClient providerClient; - try { - providerClient = context.getContentResolver().acquireContentProviderClient( - BinaryDictionaryFileDumper.getProviderUriBuilder("").build()); - } catch (final SecurityException e) { - Log.e(TAG, "No permission to communicate with the dictionary provider", e); - return; - } - if (null == providerClient) { - Log.e(TAG, "Can't establish communication with the dictionary provider"); - return; - } - final String wordlistId = - DictionaryInfoUtils.getWordListIdFromFileName(new File(f.mFilename).getName()); - // TODO: this is a reasonable last resort, but it is suboptimal. - // The following will remove the entry for this dictionary with the dictionary - // provider. When the metadata is downloaded again, we will try downloading it - // again. - // However, in the practice that will mean the user will find themselves without - // the new dictionary. That's fine for languages where it's included in the APK, - // but for other languages it will leave the user without a dictionary at all until - // the next update, which may be a few days away. - // Ideally, we would trigger a new download right away, and use increasing retry - // delays for this particular id/version combination. - // Then again, this is expected to only ever happen in case of human mistake. If - // the wrong file is on the server, the following is still doing the right thing. - // If it's a file left over from the last version however, it's not great. - BinaryDictionaryFileDumper.reportBrokenFileToDictionaryProvider( - providerClient, - context.getString(R.string.dictionary_pack_client_id), - wordlistId); - } - } - - /** - * Initializes a read-only binary dictionary from a raw resource file - * @param context application context for reading resources - * @param locale the locale to use for the resource - * @return an initialized instance of ReadOnlyBinaryDictionary - */ - private static ReadOnlyBinaryDictionary createReadOnlyBinaryDictionary(final Context context, - final Locale locale) { - AssetFileDescriptor afd = null; - try { - final int resId = DictionaryInfoUtils.getMainDictionaryResourceIdIfAvailableForLocale( - context.getResources(), locale); - if (0 == resId) return null; - afd = context.getResources().openRawResourceFd(resId); - if (afd == null) { - Log.e(TAG, "Found the resource but it is compressed. resId=" + resId); - return null; - } - final String sourceDir = context.getApplicationInfo().sourceDir; - final File packagePath = new File(sourceDir); - // TODO: Come up with a way to handle a directory. - if (!packagePath.isFile()) { - Log.e(TAG, "sourceDir is not a file: " + sourceDir); - return null; - } - return new ReadOnlyBinaryDictionary(sourceDir, afd.getStartOffset(), afd.getLength(), - false /* useFullEditDistance */, locale, Dictionary.TYPE_MAIN); - } catch (android.content.res.Resources.NotFoundException e) { - Log.e(TAG, "Could not find the resource"); - return null; - } finally { - if (null != afd) { - try { - afd.close(); - } catch (java.io.IOException e) { - /* IOException on close ? What am I supposed to do ? */ - } - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryPackInstallBroadcastReceiver.java b/java/src/com/android/inputmethod/latin/DictionaryPackInstallBroadcastReceiver.java deleted file mode 100644 index 2dcfdb0b7..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryPackInstallBroadcastReceiver.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import com.android.inputmethod.dictionarypack.DictionaryPackConstants; -import com.android.inputmethod.latin.utils.TargetPackageInfoGetterTask; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.ProviderInfo; -import android.net.Uri; -import android.util.Log; - -/** - * Receives broadcasts pertaining to dictionary management and takes the appropriate action. - * - * This object receives three types of broadcasts. - * - Package installed/added. When a dictionary provider application is added or removed, we - * need to query the dictionaries. - * - New dictionary broadcast. The dictionary provider broadcasts new dictionary availability. When - * this happens, we need to re-query the dictionaries. - * - Unknown client. If the dictionary provider is in urgent need of data about some client that - * it does not know, it sends this broadcast. When we receive this, we need to tell the dictionary - * provider about ourselves. This happens when the settings for the dictionary pack are accessed, - * but Latin IME never got a chance to register itself. - */ -public final class DictionaryPackInstallBroadcastReceiver extends BroadcastReceiver { - private static final String TAG = DictionaryPackInstallBroadcastReceiver.class.getSimpleName(); - - final LatinIME mService; - - public DictionaryPackInstallBroadcastReceiver() { - // This empty constructor is necessary for the system to instantiate this receiver. - // This happens when the dictionary pack says it can't find a record for our client, - // which happens when the dictionary pack settings are called before the keyboard - // was ever started once. - Log.i(TAG, "Latin IME dictionary broadcast receiver instantiated from the framework."); - mService = null; - } - - public DictionaryPackInstallBroadcastReceiver(final LatinIME service) { - mService = service; - } - - @Override - public void onReceive(Context context, Intent intent) { - final String action = intent.getAction(); - final PackageManager manager = context.getPackageManager(); - - // We need to reread the dictionary if a new dictionary package is installed. - if (action.equals(Intent.ACTION_PACKAGE_ADDED)) { - if (null == mService) { - Log.e(TAG, "Called with intent " + action + " but we don't know the service: this " - + "should never happen"); - return; - } - final Uri packageUri = intent.getData(); - if (null == packageUri) return; // No package name : we can't do anything - final String packageName = packageUri.getSchemeSpecificPart(); - if (null == packageName) return; - // TODO: do this in a more appropriate place - TargetPackageInfoGetterTask.removeCachedPackageInfo(packageName); - final PackageInfo packageInfo; - try { - packageInfo = manager.getPackageInfo(packageName, PackageManager.GET_PROVIDERS); - } catch (android.content.pm.PackageManager.NameNotFoundException e) { - return; // No package info : we can't do anything - } - final ProviderInfo[] providers = packageInfo.providers; - if (null == providers) return; // No providers : it is not a dictionary. - - // Search for some dictionary pack in the just-installed package. If found, reread. - for (ProviderInfo info : providers) { - if (DictionaryPackConstants.AUTHORITY.equals(info.authority)) { - mService.resetSuggestMainDict(); - return; - } - } - // If we come here none of the authorities matched the one we searched for. - // We can exit safely. - return; - } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) - && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { - if (null == mService) { - Log.e(TAG, "Called with intent " + action + " but we don't know the service: this " - + "should never happen"); - return; - } - // When the dictionary package is removed, we need to reread dictionary (to use the - // next-priority one, or stop using a dictionary at all if this was the only one, - // since this is the user request). - // If we are replacing the package, we will receive ADDED right away so no need to - // remove the dictionary at the moment, since we will do it when we receive the - // ADDED broadcast. - - // TODO: Only reload dictionary on REMOVED when the removed package is the one we - // read dictionary from? - mService.resetSuggestMainDict(); - } else if (action.equals(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION)) { - if (null == mService) { - Log.e(TAG, "Called with intent " + action + " but we don't know the service: this " - + "should never happen"); - return; - } - mService.resetSuggestMainDict(); - } else if (action.equals(DictionaryPackConstants.UNKNOWN_DICTIONARY_PROVIDER_CLIENT)) { - if (null != mService) { - // Careful! This is returning if the service is NOT null. This is because we - // should come here instantiated by the framework in reaction to a broadcast of - // the above action, so we should gave gone through the no-args constructor. - Log.e(TAG, "Called with intent " + action + " but we have a reference to the " - + "service: this should never happen"); - return; - } - // The dictionary provider does not know about some client. We check that it's really - // us that it needs to know about, and if it's the case, we register with the provider. - final String wantedClientId = - intent.getStringExtra(DictionaryPackConstants.DICTIONARY_PROVIDER_CLIENT_EXTRA); - final String myClientId = context.getString(R.string.dictionary_pack_client_id); - if (!wantedClientId.equals(myClientId)) return; // Not for us - BinaryDictionaryFileDumper.initializeClientRecordHelper(context, myClientId); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/DictionaryStats.java b/java/src/com/android/inputmethod/latin/DictionaryStats.java deleted file mode 100644 index b65d25bb1..000000000 --- a/java/src/com/android/inputmethod/latin/DictionaryStats.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2014 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 java.io.File; -import java.math.BigDecimal; -import java.util.Locale; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -public class DictionaryStats { - public static final int NOT_AN_ENTRY_COUNT = -1; - - public final Locale mLocale; - public final String mDictType; - public final String mDictFileName; - public final long mDictFileSize; - public final int mContentVersion; - public final int mWordCount; - - public DictionaryStats( - @Nonnull final Locale locale, - @Nonnull final String dictType, - @Nullable final String dictFileName, - @Nullable final File dictFile, - final int contentVersion) { - mLocale = locale; - mDictType = dictType; - mDictFileSize = (dictFile == null || !dictFile.exists()) ? 0 : dictFile.length(); - mDictFileName = dictFileName; - mContentVersion = contentVersion; - mWordCount = -1; - } - - public DictionaryStats( - @Nonnull final Locale locale, - @Nonnull final String dictType, - final int wordCount) { - mLocale = locale; - mDictType = dictType; - mDictFileSize = wordCount; - mDictFileName = null; - mContentVersion = 0; - mWordCount = wordCount; - } - - public String getFileSizeString() { - BigDecimal bytes = new BigDecimal(mDictFileSize); - BigDecimal kb = bytes.divide(new BigDecimal(1024), 2, BigDecimal.ROUND_HALF_UP); - if (kb.longValue() == 0) { - return bytes.toString() + " bytes"; - } - BigDecimal mb = kb.divide(new BigDecimal(1024), 2, BigDecimal.ROUND_HALF_UP); - if (mb.longValue() == 0) { - return kb.toString() + " kb"; - } - return mb.toString() + " Mb"; - } - - @Override - public String toString() { - final StringBuilder builder = new StringBuilder(mDictType); - if (mDictType.equals(Dictionary.TYPE_MAIN)) { - builder.append(" ("); - builder.append(mContentVersion); - builder.append(")"); - } - builder.append(": "); - if (mWordCount > -1) { - builder.append(mWordCount); - builder.append(" words"); - } else { - builder.append(mDictFileName); - builder.append(" / "); - builder.append(getFileSizeString()); - } - return builder.toString(); - } - - public static String toString(final Iterable<DictionaryStats> stats) { - final StringBuilder builder = new StringBuilder("LM Stats"); - for (DictionaryStats stat : stats) { - builder.append("\n "); - builder.append(stat.toString()); - } - return builder.toString(); - } -} diff --git a/java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java b/java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java deleted file mode 100644 index 8924e0a3d..000000000 --- a/java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (C) 2014, 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.res.Resources; -import android.util.Log; -import android.util.Pair; -import android.view.KeyEvent; - -import com.android.inputmethod.keyboard.KeyboardSwitcher; -import com.android.inputmethod.latin.settings.Settings; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.annotation.Nonnull; - -/** - * A class for detecting Emoji-Alt physical key. - */ -final class EmojiAltPhysicalKeyDetector { - private static final String TAG = "EmojiAltPhysicalKeyDetector"; - private static final boolean DEBUG = false; - - private List<EmojiHotKeys> mHotKeysList; - - private static class HotKeySet extends HashSet<Pair<Integer, Integer>> { }; - - private abstract class EmojiHotKeys { - private final String mName; - private final HotKeySet mKeySet; - - boolean mCanFire; - int mMetaState; - - public EmojiHotKeys(final String name, HotKeySet keySet) { - mName = name; - mKeySet = keySet; - mCanFire = false; - } - - public void onKeyDown(@Nonnull final KeyEvent keyEvent) { - if (DEBUG) { - Log.d(TAG, "EmojiHotKeys.onKeyDown() - " + mName + " - considering " + keyEvent); - } - - final Pair<Integer, Integer> key = - Pair.create(keyEvent.getKeyCode(), keyEvent.getMetaState()); - if (mKeySet.contains(key)) { - if (DEBUG) { - Log.d(TAG, "EmojiHotKeys.onKeyDown() - " + mName + " - enabling action"); - } - mCanFire = true; - mMetaState = keyEvent.getMetaState(); - } else if (mCanFire) { - if (DEBUG) { - Log.d(TAG, "EmojiHotKeys.onKeyDown() - " + mName + " - disabling action"); - } - mCanFire = false; - } - } - - public void onKeyUp(@Nonnull final KeyEvent keyEvent) { - if (DEBUG) { - Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - considering " + keyEvent); - } - - final int keyCode = keyEvent.getKeyCode(); - int metaState = keyEvent.getMetaState(); - if (KeyEvent.isModifierKey(keyCode)) { - // Try restoring meta stat in case the released key was a modifier. - // I am sure one can come up with scenarios to break this, but it - // seems to work well in practice. - metaState |= mMetaState; - } - - final Pair<Integer, Integer> key = Pair.create(keyCode, metaState); - if (mKeySet.contains(key)) { - if (mCanFire) { - if (!keyEvent.isCanceled()) { - if (DEBUG) { - Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - firing action"); - } - action(); - } else { - // This key up event was a part of key combinations and - // should be ignored. - if (DEBUG) { - Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - canceled, ignoring action"); - } - } - mCanFire = false; - } - } - - if (mCanFire) { - if (DEBUG) { - Log.d(TAG, "EmojiHotKeys.onKeyUp() - " + mName + " - disabling action"); - } - mCanFire = false; - } - } - - protected abstract void action(); - } - - public EmojiAltPhysicalKeyDetector(@Nonnull final Resources resources) { - mHotKeysList = new ArrayList<EmojiHotKeys>(); - - final HotKeySet emojiSwitchSet = parseHotKeys( - resources, R.array.keyboard_switcher_emoji); - final EmojiHotKeys emojiHotKeys = new EmojiHotKeys("emoji", emojiSwitchSet) { - @Override - protected void action() { - final KeyboardSwitcher switcher = KeyboardSwitcher.getInstance(); - switcher.onToggleKeyboard(KeyboardSwitcher.KeyboardSwitchState.EMOJI); - } - }; - mHotKeysList.add(emojiHotKeys); - - final HotKeySet symbolsSwitchSet = parseHotKeys( - resources, R.array.keyboard_switcher_symbols_shifted); - final EmojiHotKeys symbolsHotKeys = new EmojiHotKeys("symbols", symbolsSwitchSet) { - @Override - protected void action() { - final KeyboardSwitcher switcher = KeyboardSwitcher.getInstance(); - switcher.onToggleKeyboard(KeyboardSwitcher.KeyboardSwitchState.SYMBOLS_SHIFTED); - } - }; - mHotKeysList.add(symbolsHotKeys); - } - - public void onKeyDown(@Nonnull final KeyEvent keyEvent) { - if (DEBUG) { - Log.d(TAG, "onKeyDown(): " + keyEvent); - } - - if (shouldProcessEvent(keyEvent)) { - for (EmojiHotKeys hotKeys : mHotKeysList) { - hotKeys.onKeyDown(keyEvent); - } - } - } - - public void onKeyUp(@Nonnull final KeyEvent keyEvent) { - if (DEBUG) { - Log.d(TAG, "onKeyUp(): " + keyEvent); - } - - if (shouldProcessEvent(keyEvent)) { - for (EmojiHotKeys hotKeys : mHotKeysList) { - hotKeys.onKeyUp(keyEvent); - } - } - } - - private static boolean shouldProcessEvent(@Nonnull final KeyEvent keyEvent) { - if (!Settings.getInstance().getCurrent().mEnableEmojiAltPhysicalKey) { - // The feature is disabled. - if (DEBUG) { - Log.d(TAG, "shouldProcessEvent(): Disabled"); - } - return false; - } - - return true; - } - - private static HotKeySet parseHotKeys( - @Nonnull final Resources resources, final int resourceId) { - final HotKeySet keySet = new HotKeySet(); - final String name = resources.getResourceEntryName(resourceId); - final String[] values = resources.getStringArray(resourceId); - for (int i = 0; values != null && i < values.length; i++) { - String[] valuePair = values[i].split(","); - if (valuePair.length != 2) { - Log.w(TAG, "Expected 2 integers in " + name + "[" + i + "] : " + values[i]); - } - try { - final Integer keyCode = Integer.parseInt(valuePair[0]); - final Integer metaState = Integer.parseInt(valuePair[1]); - final Pair<Integer, Integer> key = Pair.create( - keyCode, KeyEvent.normalizeMetaState(metaState)); - keySet.add(key); - } catch (NumberFormatException e) { - Log.w(TAG, "Failed to parse " + name + "[" + i + "] : " + values[i], e); - } - } - return keySet; - } -} diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java deleted file mode 100644 index 907095746..000000000 --- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +++ /dev/null @@ -1,757 +0,0 @@ -/* - * 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.Context; -import android.util.Log; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.common.FileUtils; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; -import com.android.inputmethod.latin.makedict.DictionaryHeader; -import com.android.inputmethod.latin.makedict.FormatSpec; -import com.android.inputmethod.latin.makedict.UnsupportedFormatException; -import com.android.inputmethod.latin.makedict.WordProperty; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; -import com.android.inputmethod.latin.utils.AsyncResultHolder; -import com.android.inputmethod.latin.utils.CombinedFormatUtils; -import com.android.inputmethod.latin.utils.ExecutorUtils; -import com.android.inputmethod.latin.utils.WordInputEventForPersonalization; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantReadWriteLock; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Abstract base class for an expandable dictionary that can be created and updated dynamically - * during runtime. When updated it automatically generates a new binary dictionary to handle future - * queries in native code. This binary dictionary is written to internal storage. - * - * A class that extends this abstract class must have a static factory method named - * getDictionary(Context context, Locale locale, File dictFile, String dictNamePrefix) - */ -abstract public class ExpandableBinaryDictionary extends Dictionary { - private static final boolean DEBUG = false; - - /** Used for Log actions from this class */ - private static final String TAG = ExpandableBinaryDictionary.class.getSimpleName(); - - /** Whether to print debug output to log */ - private static final boolean DBG_STRESS_TEST = false; - - private static final int TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS = 100; - - /** - * The maximum length of a word in this dictionary. - */ - protected static final int MAX_WORD_LENGTH = - DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH; - - private static final int DICTIONARY_FORMAT_VERSION = FormatSpec.VERSION4; - - private static final WordProperty[] DEFAULT_WORD_PROPERTIES_FOR_SYNC = - new WordProperty[0] /* default */; - - /** The application context. */ - protected final Context mContext; - - /** - * The binary dictionary generated dynamically from the fusion dictionary. This is used to - * answer unigram and bigram queries. - */ - private BinaryDictionary mBinaryDictionary; - - /** - * The name of this dictionary, used as a part of the filename for storing the binary - * dictionary. - */ - private final String mDictName; - - /** Dictionary file */ - private final File mDictFile; - - /** Indicates whether a task for reloading the dictionary has been scheduled. */ - private final AtomicBoolean mIsReloading; - - /** Indicates whether the current dictionary needs to be recreated. */ - private boolean mNeedsToRecreate; - - private final ReentrantReadWriteLock mLock; - - private Map<String, String> mAdditionalAttributeMap = null; - - /* A extension for a binary dictionary file. */ - protected static final String DICT_FILE_EXTENSION = ".dict"; - - /** - * Abstract method for loading initial contents of a given dictionary. - */ - protected abstract void loadInitialContentsLocked(); - - static boolean matchesExpectedBinaryDictFormatVersionForThisType(final int formatVersion) { - return formatVersion == FormatSpec.VERSION4; - } - - private static boolean needsToMigrateDictionary(final int formatVersion) { - // When we bump up the dictionary format version, the old version should be added to here - // for supporting migration. Note that native code has to support reading such formats. - return formatVersion == FormatSpec.VERSION402; - } - - public boolean isValidDictionaryLocked() { - return mBinaryDictionary.isValidDictionary(); - } - - /** - * Creates a new expandable binary dictionary. - * - * @param context The application context of the parent. - * @param dictName The name of the dictionary. Multiple instances with the same - * name is supported. - * @param locale the dictionary locale. - * @param dictType the dictionary type, as a human-readable string - * @param dictFile dictionary file path. if null, use default dictionary path based on - * dictionary type. - */ - public ExpandableBinaryDictionary(final Context context, final String dictName, - final Locale locale, final String dictType, final File dictFile) { - super(dictType, locale); - mDictName = dictName; - mContext = context; - mDictFile = getDictFile(context, dictName, dictFile); - mBinaryDictionary = null; - mIsReloading = new AtomicBoolean(); - mNeedsToRecreate = false; - mLock = new ReentrantReadWriteLock(); - } - - public static File getDictFile(final Context context, final String dictName, - final File dictFile) { - return (dictFile != null) ? dictFile - : new File(context.getFilesDir(), dictName + DICT_FILE_EXTENSION); - } - - public static String getDictName(final String name, final Locale locale, - final File dictFile) { - return dictFile != null ? dictFile.getName() : name + "." + locale.toString(); - } - - private void asyncExecuteTaskWithWriteLock(final Runnable task) { - asyncExecuteTaskWithLock(mLock.writeLock(), task); - } - - private static void asyncExecuteTaskWithLock(final Lock lock, final Runnable task) { - ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD).execute(new Runnable() { - @Override - public void run() { - lock.lock(); - try { - task.run(); - } finally { - lock.unlock(); - } - } - }); - } - - @Nullable - BinaryDictionary getBinaryDictionary() { - return mBinaryDictionary; - } - - void closeBinaryDictionary() { - if (mBinaryDictionary != null) { - mBinaryDictionary.close(); - mBinaryDictionary = null; - } - } - - /** - * Closes and cleans up the binary dictionary. - */ - @Override - public void close() { - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - closeBinaryDictionary(); - } - }); - } - - protected Map<String, String> getHeaderAttributeMap() { - HashMap<String, String> attributeMap = new HashMap<>(); - if (mAdditionalAttributeMap != null) { - attributeMap.putAll(mAdditionalAttributeMap); - } - attributeMap.put(DictionaryHeader.DICTIONARY_ID_KEY, mDictName); - attributeMap.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, mLocale.toString()); - attributeMap.put(DictionaryHeader.DICTIONARY_VERSION_KEY, - String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()))); - return attributeMap; - } - - private void removeBinaryDictionary() { - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - removeBinaryDictionaryLocked(); - } - }); - } - - void removeBinaryDictionaryLocked() { - closeBinaryDictionary(); - if (mDictFile.exists() && !FileUtils.deleteRecursively(mDictFile)) { - Log.e(TAG, "Can't remove a file: " + mDictFile.getName()); - } - } - - private void openBinaryDictionaryLocked() { - mBinaryDictionary = new BinaryDictionary( - mDictFile.getAbsolutePath(), 0 /* offset */, mDictFile.length(), - true /* useFullEditDistance */, mLocale, mDictType, true /* isUpdatable */); - } - - void createOnMemoryBinaryDictionaryLocked() { - mBinaryDictionary = new BinaryDictionary( - mDictFile.getAbsolutePath(), true /* useFullEditDistance */, mLocale, mDictType, - DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap()); - } - - public void clear() { - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - removeBinaryDictionaryLocked(); - createOnMemoryBinaryDictionaryLocked(); - } - }); - } - - /** - * Check whether GC is needed and run GC if required. - */ - public void runGCIfRequired(final boolean mindsBlockByGC) { - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - if (getBinaryDictionary() == null) { - return; - } - runGCIfRequiredLocked(mindsBlockByGC); - } - }); - } - - protected void runGCIfRequiredLocked(final boolean mindsBlockByGC) { - if (mBinaryDictionary.needsToRunGC(mindsBlockByGC)) { - mBinaryDictionary.flushWithGC(); - } - } - - private void updateDictionaryWithWriteLock(@Nonnull final Runnable updateTask) { - reloadDictionaryIfRequired(); - final Runnable task = new Runnable() { - @Override - public void run() { - if (getBinaryDictionary() == null) { - return; - } - runGCIfRequiredLocked(true /* mindsBlockByGC */); - updateTask.run(); - } - }; - asyncExecuteTaskWithWriteLock(task); - } - - /** - * Adds unigram information of a word to the dictionary. May overwrite an existing entry. - */ - public void addUnigramEntry(final String word, final int frequency, - final boolean isNotAWord, final boolean isPossiblyOffensive, final int timestamp) { - updateDictionaryWithWriteLock(new Runnable() { - @Override - public void run() { - addUnigramLocked(word, frequency, isNotAWord, isPossiblyOffensive, timestamp); - } - }); - } - - protected void addUnigramLocked(final String word, final int frequency, - final boolean isNotAWord, final boolean isPossiblyOffensive, final int timestamp) { - if (!mBinaryDictionary.addUnigramEntry(word, frequency, - false /* isBeginningOfSentence */, isNotAWord, isPossiblyOffensive, timestamp)) { - Log.e(TAG, "Cannot add unigram entry. word: " + word); - } - } - - /** - * Dynamically remove the unigram entry from the dictionary. - */ - public void removeUnigramEntryDynamically(final String word) { - reloadDictionaryIfRequired(); - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - final BinaryDictionary binaryDictionary = getBinaryDictionary(); - if (binaryDictionary == null) { - return; - } - runGCIfRequiredLocked(true /* mindsBlockByGC */); - if (!binaryDictionary.removeUnigramEntry(word)) { - if (DEBUG) { - Log.i(TAG, "Cannot remove unigram entry: " + word); - } - } - } - }); - } - - /** - * Adds n-gram information of a word to the dictionary. May overwrite an existing entry. - */ - public void addNgramEntry(@Nonnull final NgramContext ngramContext, final String word, - final int frequency, final int timestamp) { - reloadDictionaryIfRequired(); - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - if (getBinaryDictionary() == null) { - return; - } - runGCIfRequiredLocked(true /* mindsBlockByGC */); - addNgramEntryLocked(ngramContext, word, frequency, timestamp); - } - }); - } - - protected void addNgramEntryLocked(@Nonnull final NgramContext ngramContext, final String word, - final int frequency, final int timestamp) { - if (!mBinaryDictionary.addNgramEntry(ngramContext, word, frequency, timestamp)) { - if (DEBUG) { - Log.i(TAG, "Cannot add n-gram entry."); - Log.i(TAG, " NgramContext: " + ngramContext + ", word: " + word); - } - } - } - - /** - * Update dictionary for the word with the ngramContext. - */ - public void updateEntriesForWord(@Nonnull final NgramContext ngramContext, - final String word, final boolean isValidWord, final int count, final int timestamp) { - updateDictionaryWithWriteLock(new Runnable() { - @Override - public void run() { - final BinaryDictionary binaryDictionary = getBinaryDictionary(); - if (binaryDictionary == null) { - return; - } - if (!binaryDictionary.updateEntriesForWordWithNgramContext(ngramContext, word, - isValidWord, count, timestamp)) { - if (DEBUG) { - Log.e(TAG, "Cannot update counter. word: " + word - + " context: " + ngramContext.toString()); - } - } - } - }); - } - - /** - * Used by Sketch. - * {@see https://cs.corp.google.com/#android/vendor/unbundled_google/packages/LatinIMEGoogle/tools/sketch/ime-simulator/src/com/android/inputmethod/sketch/imesimulator/ImeSimulator.java&q=updateEntriesForInputEventsCallback&l=286} - */ - @UsedForTesting - public interface UpdateEntriesForInputEventsCallback { - public void onFinished(); - } - - /** - * Dynamically update entries according to input events. - * - * Used by Sketch. - * {@see https://cs.corp.google.com/#android/vendor/unbundled_google/packages/LatinIMEGoogle/tools/sketch/ime-simulator/src/com/android/inputmethod/sketch/imesimulator/ImeSimulator.java&q=updateEntriesForInputEventsCallback&l=286} - */ - @UsedForTesting - public void updateEntriesForInputEvents( - @Nonnull final ArrayList<WordInputEventForPersonalization> inputEvents, - final UpdateEntriesForInputEventsCallback callback) { - reloadDictionaryIfRequired(); - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - try { - final BinaryDictionary binaryDictionary = getBinaryDictionary(); - if (binaryDictionary == null) { - return; - } - binaryDictionary.updateEntriesForInputEvents( - inputEvents.toArray( - new WordInputEventForPersonalization[inputEvents.size()])); - } finally { - if (callback != null) { - callback.onFinished(); - } - } - } - }); - } - - @Override - public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData, - final NgramContext ngramContext, final long proximityInfoHandle, - final SettingsValuesForSuggestion settingsValuesForSuggestion, final int sessionId, - final float weightForLocale, final float[] inOutWeightOfLangModelVsSpatialModel) { - reloadDictionaryIfRequired(); - boolean lockAcquired = false; - try { - lockAcquired = mLock.readLock().tryLock( - TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS, TimeUnit.MILLISECONDS); - if (lockAcquired) { - if (mBinaryDictionary == null) { - return null; - } - final ArrayList<SuggestedWordInfo> suggestions = - mBinaryDictionary.getSuggestions(composedData, ngramContext, - proximityInfoHandle, settingsValuesForSuggestion, sessionId, - weightForLocale, inOutWeightOfLangModelVsSpatialModel); - if (mBinaryDictionary.isCorrupted()) { - Log.i(TAG, "Dictionary (" + mDictName +") is corrupted. " - + "Remove and regenerate it."); - removeBinaryDictionary(); - } - return suggestions; - } - } catch (final InterruptedException e) { - Log.e(TAG, "Interrupted tryLock() in getSuggestionsWithSessionId().", e); - } finally { - if (lockAcquired) { - mLock.readLock().unlock(); - } - } - return null; - } - - @Override - public boolean isInDictionary(final String word) { - reloadDictionaryIfRequired(); - boolean lockAcquired = false; - try { - lockAcquired = mLock.readLock().tryLock( - TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS, TimeUnit.MILLISECONDS); - if (lockAcquired) { - if (mBinaryDictionary == null) { - return false; - } - return isInDictionaryLocked(word); - } - } catch (final InterruptedException e) { - Log.e(TAG, "Interrupted tryLock() in isInDictionary().", e); - } finally { - if (lockAcquired) { - mLock.readLock().unlock(); - } - } - return false; - } - - protected boolean isInDictionaryLocked(final String word) { - if (mBinaryDictionary == null) return false; - return mBinaryDictionary.isInDictionary(word); - } - - @Override - public int getMaxFrequencyOfExactMatches(final String word) { - reloadDictionaryIfRequired(); - boolean lockAcquired = false; - try { - lockAcquired = mLock.readLock().tryLock( - TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS, TimeUnit.MILLISECONDS); - if (lockAcquired) { - if (mBinaryDictionary == null) { - return NOT_A_PROBABILITY; - } - return mBinaryDictionary.getMaxFrequencyOfExactMatches(word); - } - } catch (final InterruptedException e) { - Log.e(TAG, "Interrupted tryLock() in getMaxFrequencyOfExactMatches().", e); - } finally { - if (lockAcquired) { - mLock.readLock().unlock(); - } - } - return NOT_A_PROBABILITY; - } - - - /** - * Loads the current binary dictionary from internal storage. Assumes the dictionary file - * exists. - */ - void loadBinaryDictionaryLocked() { - if (DBG_STRESS_TEST) { - // Test if this class does not cause problems when it takes long time to load binary - // dictionary. - try { - Log.w(TAG, "Start stress in loading: " + mDictName); - Thread.sleep(15000); - Log.w(TAG, "End stress in loading"); - } catch (InterruptedException e) { - Log.w("Interrupted while loading: " + mDictName, e); - } - } - final BinaryDictionary oldBinaryDictionary = mBinaryDictionary; - openBinaryDictionaryLocked(); - if (oldBinaryDictionary != null) { - oldBinaryDictionary.close(); - } - if (mBinaryDictionary.isValidDictionary() - && needsToMigrateDictionary(mBinaryDictionary.getFormatVersion())) { - if (!mBinaryDictionary.migrateTo(DICTIONARY_FORMAT_VERSION)) { - Log.e(TAG, "Dictionary migration failed: " + mDictName); - removeBinaryDictionaryLocked(); - } - } - } - - /** - * Create a new binary dictionary and load initial contents. - */ - void createNewDictionaryLocked() { - removeBinaryDictionaryLocked(); - createOnMemoryBinaryDictionaryLocked(); - loadInitialContentsLocked(); - // Run GC and flush to file when initial contents have been loaded. - mBinaryDictionary.flushWithGCIfHasUpdated(); - } - - /** - * Marks that the dictionary needs to be recreated. - * - */ - protected void setNeedsToRecreate() { - mNeedsToRecreate = true; - } - - void clearNeedsToRecreate() { - mNeedsToRecreate = false; - } - - boolean isNeededToRecreate() { - return mNeedsToRecreate; - } - - /** - * Load the current binary dictionary from internal storage. If the dictionary file doesn't - * exists or needs to be regenerated, the new dictionary file will be asynchronously generated. - * However, the dictionary itself is accessible even before the new dictionary file is actually - * generated. It may return a null result for getSuggestions() in that case by design. - */ - public final void reloadDictionaryIfRequired() { - if (!isReloadRequired()) return; - asyncReloadDictionary(); - } - - /** - * Returns whether a dictionary reload is required. - */ - private boolean isReloadRequired() { - return mBinaryDictionary == null || mNeedsToRecreate; - } - - /** - * Reloads the dictionary. Access is controlled on a per dictionary file basis. - */ - private void asyncReloadDictionary() { - final AtomicBoolean isReloading = mIsReloading; - if (!isReloading.compareAndSet(false, true)) { - return; - } - final File dictFile = mDictFile; - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - try { - if (!dictFile.exists() || isNeededToRecreate()) { - // If the dictionary file does not exist or contents have been updated, - // generate a new one. - createNewDictionaryLocked(); - } else if (getBinaryDictionary() == null) { - // Otherwise, load the existing dictionary. - loadBinaryDictionaryLocked(); - final BinaryDictionary binaryDictionary = getBinaryDictionary(); - if (binaryDictionary != null && !(isValidDictionaryLocked() - // TODO: remove the check below - && matchesExpectedBinaryDictFormatVersionForThisType( - binaryDictionary.getFormatVersion()))) { - // Binary dictionary or its format version is not valid. Regenerate - // the dictionary file. createNewDictionaryLocked will remove the - // existing files if appropriate. - createNewDictionaryLocked(); - } - } - clearNeedsToRecreate(); - } finally { - isReloading.set(false); - } - } - }); - } - - /** - * Flush binary dictionary to dictionary file. - */ - public void asyncFlushBinaryDictionary() { - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - final BinaryDictionary binaryDictionary = getBinaryDictionary(); - if (binaryDictionary == null) { - return; - } - if (binaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) { - binaryDictionary.flushWithGC(); - } else { - binaryDictionary.flush(); - } - } - }); - } - - public DictionaryStats getDictionaryStats() { - reloadDictionaryIfRequired(); - final String dictName = mDictName; - final File dictFile = mDictFile; - final AsyncResultHolder<DictionaryStats> result = - new AsyncResultHolder<>("DictionaryStats"); - asyncExecuteTaskWithLock(mLock.readLock(), new Runnable() { - @Override - public void run() { - result.set(new DictionaryStats(mLocale, dictName, dictName, dictFile, 0)); - } - }); - return result.get(null /* defaultValue */, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS); - } - - @UsedForTesting - public void waitAllTasksForTests() { - final CountDownLatch countDownLatch = new CountDownLatch(1); - asyncExecuteTaskWithWriteLock(new Runnable() { - @Override - public void run() { - countDownLatch.countDown(); - } - }); - try { - countDownLatch.await(); - } catch (InterruptedException e) { - Log.e(TAG, "Interrupted while waiting for finishing dictionary operations.", e); - } - } - - @UsedForTesting - public void clearAndFlushDictionaryWithAdditionalAttributes( - final Map<String, String> attributeMap) { - mAdditionalAttributeMap = attributeMap; - clear(); - } - - public void dumpAllWordsForDebug() { - reloadDictionaryIfRequired(); - final String tag = TAG; - final String dictName = mDictName; - asyncExecuteTaskWithLock(mLock.readLock(), new Runnable() { - @Override - public void run() { - Log.d(tag, "Dump dictionary: " + dictName + " for " + mLocale); - final BinaryDictionary binaryDictionary = getBinaryDictionary(); - if (binaryDictionary == null) { - return; - } - try { - final DictionaryHeader header = binaryDictionary.getHeader(); - Log.d(tag, "Format version: " + binaryDictionary.getFormatVersion()); - Log.d(tag, CombinedFormatUtils.formatAttributeMap( - header.mDictionaryOptions.mAttributes)); - } catch (final UnsupportedFormatException e) { - Log.d(tag, "Cannot fetch header information.", e); - } - int token = 0; - do { - final BinaryDictionary.GetNextWordPropertyResult result = - binaryDictionary.getNextWordProperty(token); - final WordProperty wordProperty = result.mWordProperty; - if (wordProperty == null) { - Log.d(tag, " dictionary is empty."); - break; - } - Log.d(tag, wordProperty.toString()); - token = result.mNextToken; - } while (token != 0); - } - }); - } - - /** - * Returns dictionary content required for syncing. - */ - public WordProperty[] getWordPropertiesForSyncing() { - reloadDictionaryIfRequired(); - final AsyncResultHolder<WordProperty[]> result = - new AsyncResultHolder<>("WordPropertiesForSync"); - asyncExecuteTaskWithLock(mLock.readLock(), new Runnable() { - @Override - public void run() { - final ArrayList<WordProperty> wordPropertyList = new ArrayList<>(); - final BinaryDictionary binaryDictionary = getBinaryDictionary(); - if (binaryDictionary == null) { - return; - } - int token = 0; - do { - // TODO: We need a new API that returns *new* un-synced data. - final BinaryDictionary.GetNextWordPropertyResult nextWordPropertyResult = - binaryDictionary.getNextWordProperty(token); - final WordProperty wordProperty = nextWordPropertyResult.mWordProperty; - if (wordProperty == null) { - break; - } - wordPropertyList.add(wordProperty); - token = nextWordPropertyResult.mNextToken; - } while (token != 0); - result.set(wordPropertyList.toArray(new WordProperty[wordPropertyList.size()])); - } - }); - // TODO: Figure out the best timeout duration for this API. - return result.get(DEFAULT_WORD_PROPERTIES_FOR_SYNC, - TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS); - } -} diff --git a/java/src/com/android/inputmethod/latin/InputAttributes.java b/java/src/com/android/inputmethod/latin/InputAttributes.java deleted file mode 100644 index 86c7810f4..000000000 --- a/java/src/com/android/inputmethod/latin/InputAttributes.java +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import static com.android.inputmethod.latin.common.Constants.ImeOption.NO_FLOATING_GESTURE_PREVIEW; -import static com.android.inputmethod.latin.common.Constants.ImeOption.NO_MICROPHONE; -import static com.android.inputmethod.latin.common.Constants.ImeOption.NO_MICROPHONE_COMPAT; - -import android.text.InputType; -import android.util.Log; -import android.view.inputmethod.EditorInfo; - -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.utils.InputTypeUtils; - -import java.util.ArrayList; -import java.util.Arrays; - -/** - * Class to hold attributes of the input field. - */ -public final class InputAttributes { - private final String TAG = InputAttributes.class.getSimpleName(); - - final public String mTargetApplicationPackageName; - final public boolean mInputTypeNoAutoCorrect; - final public boolean mIsPasswordField; - final public boolean mShouldShowSuggestions; - final public boolean mApplicationSpecifiedCompletionOn; - final public boolean mShouldInsertSpacesAutomatically; - final public boolean mShouldShowVoiceInputKey; - /** - * Whether the floating gesture preview should be disabled. If true, this should override the - * corresponding keyboard settings preference, always suppressing the floating preview text. - * {@link com.android.inputmethod.latin.settings.SettingsValues#mGestureFloatingPreviewTextEnabled} - */ - final public boolean mDisableGestureFloatingPreviewText; - final public boolean mIsGeneralTextInput; - final private int mInputType; - final private EditorInfo mEditorInfo; - final private String mPackageNameForPrivateImeOptions; - - public InputAttributes(final EditorInfo editorInfo, final boolean isFullscreenMode, - final String packageNameForPrivateImeOptions) { - mEditorInfo = editorInfo; - mPackageNameForPrivateImeOptions = packageNameForPrivateImeOptions; - mTargetApplicationPackageName = null != editorInfo ? editorInfo.packageName : null; - final int inputType = null != editorInfo ? editorInfo.inputType : 0; - final int inputClass = inputType & InputType.TYPE_MASK_CLASS; - mInputType = inputType; - mIsPasswordField = InputTypeUtils.isPasswordInputType(inputType) - || InputTypeUtils.isVisiblePasswordInputType(inputType); - if (inputClass != InputType.TYPE_CLASS_TEXT) { - // If we are not looking at a TYPE_CLASS_TEXT field, the following strange - // cases may arise, so we do a couple validity checks for them. If it's a - // TYPE_CLASS_TEXT field, these special cases cannot happen, by construction - // of the flags. - if (null == editorInfo) { - Log.w(TAG, "No editor info for this field. Bug?"); - } else if (InputType.TYPE_NULL == inputType) { - // TODO: We should honor TYPE_NULL specification. - Log.i(TAG, "InputType.TYPE_NULL is specified"); - } else if (inputClass == 0) { - // TODO: is this check still necessary? - Log.w(TAG, String.format("Unexpected input class: inputType=0x%08x" - + " imeOptions=0x%08x", inputType, editorInfo.imeOptions)); - } - mShouldShowSuggestions = false; - mInputTypeNoAutoCorrect = false; - mApplicationSpecifiedCompletionOn = false; - mShouldInsertSpacesAutomatically = false; - mShouldShowVoiceInputKey = false; - mDisableGestureFloatingPreviewText = false; - mIsGeneralTextInput = false; - return; - } - // inputClass == InputType.TYPE_CLASS_TEXT - final int variation = inputType & InputType.TYPE_MASK_VARIATION; - final boolean flagNoSuggestions = - 0 != (inputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); - final boolean flagMultiLine = - 0 != (inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE); - final boolean flagAutoCorrect = - 0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); - final boolean flagAutoComplete = - 0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); - - // TODO: Have a helper method in InputTypeUtils - // Make sure that passwords are not displayed in {@link SuggestionStripView}. - final boolean shouldSuppressSuggestions = mIsPasswordField - || InputTypeUtils.isEmailVariation(variation) - || InputType.TYPE_TEXT_VARIATION_URI == variation - || InputType.TYPE_TEXT_VARIATION_FILTER == variation - || flagNoSuggestions - || flagAutoComplete; - mShouldShowSuggestions = !shouldSuppressSuggestions; - - mShouldInsertSpacesAutomatically = InputTypeUtils.isAutoSpaceFriendlyType(inputType); - - final boolean noMicrophone = mIsPasswordField - || InputTypeUtils.isEmailVariation(variation) - || InputType.TYPE_TEXT_VARIATION_URI == variation - || hasNoMicrophoneKeyOption(); - mShouldShowVoiceInputKey = !noMicrophone; - - mDisableGestureFloatingPreviewText = InputAttributes.inPrivateImeOptions( - mPackageNameForPrivateImeOptions, NO_FLOATING_GESTURE_PREVIEW, editorInfo); - - // If it's a browser edit field and auto correct is not ON explicitly, then - // disable auto correction, but keep suggestions on. - // If NO_SUGGESTIONS is set, don't do prediction. - // If it's not multiline and the autoCorrect flag is not set, then don't correct - mInputTypeNoAutoCorrect = - (variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT && !flagAutoCorrect) - || flagNoSuggestions - || (!flagAutoCorrect && !flagMultiLine); - - mApplicationSpecifiedCompletionOn = flagAutoComplete && isFullscreenMode; - - // If we come here, inputClass is always TYPE_CLASS_TEXT - mIsGeneralTextInput = InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS != variation - && InputType.TYPE_TEXT_VARIATION_PASSWORD != variation - && InputType.TYPE_TEXT_VARIATION_PHONETIC != variation - && InputType.TYPE_TEXT_VARIATION_URI != variation - && InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD != variation - && InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS != variation - && InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD != variation; - } - - public boolean isTypeNull() { - return InputType.TYPE_NULL == mInputType; - } - - public boolean isSameInputType(final EditorInfo editorInfo) { - return editorInfo.inputType == mInputType; - } - - private boolean hasNoMicrophoneKeyOption() { - @SuppressWarnings("deprecation") - final boolean deprecatedNoMicrophone = InputAttributes.inPrivateImeOptions( - null, NO_MICROPHONE_COMPAT, mEditorInfo); - final boolean noMicrophone = InputAttributes.inPrivateImeOptions( - mPackageNameForPrivateImeOptions, NO_MICROPHONE, mEditorInfo); - return noMicrophone || deprecatedNoMicrophone; - } - - @SuppressWarnings("unused") - private void dumpFlags(final int inputType) { - final int inputClass = inputType & InputType.TYPE_MASK_CLASS; - final String inputClassString = toInputClassString(inputClass); - final String variationString = toVariationString( - inputClass, inputType & InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); - final String flagsString = toFlagsString(inputType & InputType.TYPE_MASK_FLAGS); - Log.i(TAG, "Input class: " + inputClassString); - Log.i(TAG, "Variation: " + variationString); - Log.i(TAG, "Flags: " + flagsString); - } - - private static String toInputClassString(final int inputClass) { - switch (inputClass) { - case InputType.TYPE_CLASS_TEXT: - return "TYPE_CLASS_TEXT"; - case InputType.TYPE_CLASS_PHONE: - return "TYPE_CLASS_PHONE"; - case InputType.TYPE_CLASS_NUMBER: - return "TYPE_CLASS_NUMBER"; - case InputType.TYPE_CLASS_DATETIME: - return "TYPE_CLASS_DATETIME"; - default: - return String.format("unknownInputClass<0x%08x>", inputClass); - } - } - - private static String toVariationString(final int inputClass, final int variation) { - switch (inputClass) { - case InputType.TYPE_CLASS_TEXT: - return toTextVariationString(variation); - case InputType.TYPE_CLASS_NUMBER: - return toNumberVariationString(variation); - case InputType.TYPE_CLASS_DATETIME: - return toDatetimeVariationString(variation); - default: - return ""; - } - } - - private static String toTextVariationString(final int variation) { - switch (variation) { - case InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS: - return " TYPE_TEXT_VARIATION_EMAIL_ADDRESS"; - case InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT: - return "TYPE_TEXT_VARIATION_EMAIL_SUBJECT"; - case InputType.TYPE_TEXT_VARIATION_FILTER: - return "TYPE_TEXT_VARIATION_FILTER"; - case InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE: - return "TYPE_TEXT_VARIATION_LONG_MESSAGE"; - case InputType.TYPE_TEXT_VARIATION_NORMAL: - return "TYPE_TEXT_VARIATION_NORMAL"; - case InputType.TYPE_TEXT_VARIATION_PASSWORD: - return "TYPE_TEXT_VARIATION_PASSWORD"; - case InputType.TYPE_TEXT_VARIATION_PERSON_NAME: - return "TYPE_TEXT_VARIATION_PERSON_NAME"; - case InputType.TYPE_TEXT_VARIATION_PHONETIC: - return "TYPE_TEXT_VARIATION_PHONETIC"; - case InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS: - return "TYPE_TEXT_VARIATION_POSTAL_ADDRESS"; - case InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE: - return "TYPE_TEXT_VARIATION_SHORT_MESSAGE"; - case InputType.TYPE_TEXT_VARIATION_URI: - return "TYPE_TEXT_VARIATION_URI"; - case InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD: - return "TYPE_TEXT_VARIATION_VISIBLE_PASSWORD"; - case InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT: - return "TYPE_TEXT_VARIATION_WEB_EDIT_TEXT"; - case InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS: - return "TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS"; - case InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD: - return "TYPE_TEXT_VARIATION_WEB_PASSWORD"; - default: - return String.format("unknownVariation<0x%08x>", variation); - } - } - - private static String toNumberVariationString(final int variation) { - switch (variation) { - case InputType.TYPE_NUMBER_VARIATION_NORMAL: - return "TYPE_NUMBER_VARIATION_NORMAL"; - case InputType.TYPE_NUMBER_VARIATION_PASSWORD: - return "TYPE_NUMBER_VARIATION_PASSWORD"; - default: - return String.format("unknownVariation<0x%08x>", variation); - } - } - - private static String toDatetimeVariationString(final int variation) { - switch (variation) { - case InputType.TYPE_DATETIME_VARIATION_NORMAL: - return "TYPE_DATETIME_VARIATION_NORMAL"; - case InputType.TYPE_DATETIME_VARIATION_DATE: - return "TYPE_DATETIME_VARIATION_DATE"; - case InputType.TYPE_DATETIME_VARIATION_TIME: - return "TYPE_DATETIME_VARIATION_TIME"; - default: - return String.format("unknownVariation<0x%08x>", variation); - } - } - - private static String toFlagsString(final int flags) { - final ArrayList<String> flagsArray = new ArrayList<>(); - if (0 != (flags & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)) - flagsArray.add("TYPE_TEXT_FLAG_NO_SUGGESTIONS"); - if (0 != (flags & InputType.TYPE_TEXT_FLAG_MULTI_LINE)) - flagsArray.add("TYPE_TEXT_FLAG_MULTI_LINE"); - if (0 != (flags & InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE)) - flagsArray.add("TYPE_TEXT_FLAG_IME_MULTI_LINE"); - if (0 != (flags & InputType.TYPE_TEXT_FLAG_CAP_WORDS)) - flagsArray.add("TYPE_TEXT_FLAG_CAP_WORDS"); - if (0 != (flags & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)) - flagsArray.add("TYPE_TEXT_FLAG_CAP_SENTENCES"); - if (0 != (flags & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS)) - flagsArray.add("TYPE_TEXT_FLAG_CAP_CHARACTERS"); - if (0 != (flags & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT)) - flagsArray.add("TYPE_TEXT_FLAG_AUTO_CORRECT"); - if (0 != (flags & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE)) - flagsArray.add("TYPE_TEXT_FLAG_AUTO_COMPLETE"); - return flagsArray.isEmpty() ? "" : Arrays.toString(flagsArray.toArray()); - } - - // Pretty print - @Override - public String toString() { - return String.format( - "%s: inputType=0x%08x%s%s%s%s%s targetApp=%s\n", getClass().getSimpleName(), - mInputType, - (mInputTypeNoAutoCorrect ? " noAutoCorrect" : ""), - (mIsPasswordField ? " password" : ""), - (mShouldShowSuggestions ? " shouldShowSuggestions" : ""), - (mApplicationSpecifiedCompletionOn ? " appSpecified" : ""), - (mShouldInsertSpacesAutomatically ? " insertSpaces" : ""), - mTargetApplicationPackageName); - } - - public static boolean inPrivateImeOptions(final String packageName, final String key, - final EditorInfo editorInfo) { - if (editorInfo == null) return false; - final String findingKey = (packageName != null) ? packageName + "." + key : key; - return StringUtils.containsInCommaSplittableText(findingKey, editorInfo.privateImeOptions); - } -} diff --git a/java/src/com/android/inputmethod/latin/InputView.java b/java/src/com/android/inputmethod/latin/InputView.java deleted file mode 100644 index f3a8ca169..000000000 --- a/java/src/com/android/inputmethod/latin/InputView.java +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import android.content.Context; -import android.graphics.Rect; -import android.util.AttributeSet; -import android.view.MotionEvent; -import android.view.View; -import android.widget.FrameLayout; - -import com.android.inputmethod.accessibility.AccessibilityUtils; -import com.android.inputmethod.keyboard.MainKeyboardView; -import com.android.inputmethod.latin.suggestions.MoreSuggestionsView; -import com.android.inputmethod.latin.suggestions.SuggestionStripView; - -public final class InputView extends FrameLayout { - private final Rect mInputViewRect = new Rect(); - private MainKeyboardView mMainKeyboardView; - private KeyboardTopPaddingForwarder mKeyboardTopPaddingForwarder; - private MoreSuggestionsViewCanceler mMoreSuggestionsViewCanceler; - private MotionEventForwarder<?, ?> mActiveForwarder; - - public InputView(final Context context, final AttributeSet attrs) { - super(context, attrs, 0); - } - - @Override - protected void onFinishInflate() { - final SuggestionStripView suggestionStripView = - (SuggestionStripView)findViewById(R.id.suggestion_strip_view); - mMainKeyboardView = (MainKeyboardView)findViewById(R.id.keyboard_view); - mKeyboardTopPaddingForwarder = new KeyboardTopPaddingForwarder( - mMainKeyboardView, suggestionStripView); - mMoreSuggestionsViewCanceler = new MoreSuggestionsViewCanceler( - mMainKeyboardView, suggestionStripView); - } - - public void setKeyboardTopPadding(final int keyboardTopPadding) { - mKeyboardTopPaddingForwarder.setKeyboardTopPadding(keyboardTopPadding); - } - - @Override - protected boolean dispatchHoverEvent(final MotionEvent event) { - if (AccessibilityUtils.getInstance().isTouchExplorationEnabled() - && mMainKeyboardView.isShowingMoreKeysPanel()) { - // With accessibility mode on, discard hover events while a more keys keyboard is shown. - // The {@link MoreKeysKeyboard} receives hover events directly from the platform. - return true; - } - return super.dispatchHoverEvent(event); - } - - @Override - public boolean onInterceptTouchEvent(final MotionEvent me) { - final Rect rect = mInputViewRect; - getGlobalVisibleRect(rect); - final int index = me.getActionIndex(); - final int x = (int)me.getX(index) + rect.left; - final int y = (int)me.getY(index) + rect.top; - - // The touch events that hit the top padding of keyboard should be forwarded to - // {@link SuggestionStripView}. - if (mKeyboardTopPaddingForwarder.onInterceptTouchEvent(x, y, me)) { - mActiveForwarder = mKeyboardTopPaddingForwarder; - return true; - } - - // To cancel {@link MoreSuggestionsView}, we should intercept a touch event to - // {@link MainKeyboardView} and dismiss the {@link MoreSuggestionsView}. - if (mMoreSuggestionsViewCanceler.onInterceptTouchEvent(x, y, me)) { - mActiveForwarder = mMoreSuggestionsViewCanceler; - return true; - } - - mActiveForwarder = null; - return false; - } - - @Override - public boolean onTouchEvent(final MotionEvent me) { - if (mActiveForwarder == null) { - return super.onTouchEvent(me); - } - - final Rect rect = mInputViewRect; - getGlobalVisibleRect(rect); - final int index = me.getActionIndex(); - final int x = (int)me.getX(index) + rect.left; - final int y = (int)me.getY(index) + rect.top; - return mActiveForwarder.onTouchEvent(x, y, me); - } - - /** - * This class forwards series of {@link MotionEvent}s from <code>SenderView</code> to - * <code>ReceiverView</code>. - * - * @param <SenderView> a {@link View} that may send a {@link MotionEvent} to <ReceiverView>. - * @param <ReceiverView> a {@link View} that receives forwarded {@link MotionEvent} from - * <SenderView>. - */ - private static abstract class - MotionEventForwarder<SenderView extends View, ReceiverView extends View> { - protected final SenderView mSenderView; - protected final ReceiverView mReceiverView; - - protected final Rect mEventSendingRect = new Rect(); - protected final Rect mEventReceivingRect = new Rect(); - - public MotionEventForwarder(final SenderView senderView, final ReceiverView receiverView) { - mSenderView = senderView; - mReceiverView = receiverView; - } - - // Return true if a touch event of global coordinate x, y needs to be forwarded. - protected abstract boolean needsToForward(final int x, final int y); - - // Translate global x-coordinate to <code>ReceiverView</code> local coordinate. - protected int translateX(final int x) { - return x - mEventReceivingRect.left; - } - - // Translate global y-coordinate to <code>ReceiverView</code> local coordinate. - protected int translateY(final int y) { - return y - mEventReceivingRect.top; - } - - /** - * Callback when a {@link MotionEvent} is forwarded. - * @param me the motion event to be forwarded. - */ - protected void onForwardingEvent(final MotionEvent me) {} - - // Returns true if a {@link MotionEvent} is needed to be forwarded to - // <code>ReceiverView</code>. Otherwise returns false. - public boolean onInterceptTouchEvent(final int x, final int y, final MotionEvent me) { - // Forwards a {link MotionEvent} only if both <code>SenderView</code> and - // <code>ReceiverView</code> are visible. - if (mSenderView.getVisibility() != View.VISIBLE || - mReceiverView.getVisibility() != View.VISIBLE) { - return false; - } - mSenderView.getGlobalVisibleRect(mEventSendingRect); - if (!mEventSendingRect.contains(x, y)) { - return false; - } - - if (me.getActionMasked() == MotionEvent.ACTION_DOWN) { - // If the down event happens in the forwarding area, successive - // {@link MotionEvent}s should be forwarded to <code>ReceiverView</code>. - if (needsToForward(x, y)) { - return true; - } - } - - return false; - } - - // Returns true if a {@link MotionEvent} is forwarded to <code>ReceiverView</code>. - // Otherwise returns false. - public boolean onTouchEvent(final int x, final int y, final MotionEvent me) { - mReceiverView.getGlobalVisibleRect(mEventReceivingRect); - // Translate global coordinates to <code>ReceiverView</code> local coordinates. - me.setLocation(translateX(x), translateY(y)); - mReceiverView.dispatchTouchEvent(me); - onForwardingEvent(me); - return true; - } - } - - /** - * This class forwards {@link MotionEvent}s happened in the top padding of - * {@link MainKeyboardView} to {@link SuggestionStripView}. - */ - private static class KeyboardTopPaddingForwarder - extends MotionEventForwarder<MainKeyboardView, SuggestionStripView> { - private int mKeyboardTopPadding; - - public KeyboardTopPaddingForwarder(final MainKeyboardView mainKeyboardView, - final SuggestionStripView suggestionStripView) { - super(mainKeyboardView, suggestionStripView); - } - - public void setKeyboardTopPadding(final int keyboardTopPadding) { - mKeyboardTopPadding = keyboardTopPadding; - } - - private boolean isInKeyboardTopPadding(final int y) { - return y < mEventSendingRect.top + mKeyboardTopPadding; - } - - @Override - protected boolean needsToForward(final int x, final int y) { - // Forwarding an event only when {@link MainKeyboardView} is visible. - // Because the visibility of {@link MainKeyboardView} is controlled by its parent - // view in {@link KeyboardSwitcher#setMainKeyboardFrame()}, we should check the - // visibility of the parent view. - final View mainKeyboardFrame = (View)mSenderView.getParent(); - return mainKeyboardFrame.getVisibility() == View.VISIBLE && isInKeyboardTopPadding(y); - } - - @Override - protected int translateY(final int y) { - final int translatedY = super.translateY(y); - if (isInKeyboardTopPadding(y)) { - // The forwarded event should have coordinates that are inside of the target. - return Math.min(translatedY, mEventReceivingRect.height() - 1); - } - return translatedY; - } - } - - /** - * This class forwards {@link MotionEvent}s happened in the {@link MainKeyboardView} to - * {@link SuggestionStripView} when the {@link MoreSuggestionsView} is showing. - * {@link SuggestionStripView} dismisses {@link MoreSuggestionsView} when it receives any event - * outside of it. - */ - private static class MoreSuggestionsViewCanceler - extends MotionEventForwarder<MainKeyboardView, SuggestionStripView> { - public MoreSuggestionsViewCanceler(final MainKeyboardView mainKeyboardView, - final SuggestionStripView suggestionStripView) { - super(mainKeyboardView, suggestionStripView); - } - - @Override - protected boolean needsToForward(final int x, final int y) { - return mReceiverView.isShowingMoreSuggestionPanel() && mEventSendingRect.contains(x, y); - } - - @Override - protected void onForwardingEvent(final MotionEvent me) { - if (me.getActionMasked() == MotionEvent.ACTION_DOWN) { - mReceiverView.dismissMoreSuggestionsPanel(); - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/LastComposedWord.java b/java/src/com/android/inputmethod/latin/LastComposedWord.java deleted file mode 100644 index 426d33e6d..000000000 --- a/java/src/com/android/inputmethod/latin/LastComposedWord.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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.text.TextUtils; - -import com.android.inputmethod.event.Event; -import com.android.inputmethod.latin.common.InputPointers; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; - -import java.util.ArrayList; - -/** - * This class encapsulates data about a word previously composed, but that has been - * committed already. This is used for resuming suggestion, and cancel auto-correction. - */ -public final class LastComposedWord { - // COMMIT_TYPE_USER_TYPED_WORD is used when the word committed is the exact typed word, with - // no hinting from the IME. It happens when some external event happens (rotating the device, - // for example) or when auto-correction is off by settings or editor attributes. - public static final int COMMIT_TYPE_USER_TYPED_WORD = 0; - // COMMIT_TYPE_MANUAL_PICK is used when the user pressed a field in the suggestion strip. - public static final int COMMIT_TYPE_MANUAL_PICK = 1; - // COMMIT_TYPE_DECIDED_WORD is used when the IME commits the word it decided was best - // for the current user input. It may be different from what the user typed (true auto-correct) - // or it may be exactly what the user typed if it's in the dictionary or the IME does not have - // enough confidence in any suggestion to auto-correct (auto-correct to typed word). - public static final int COMMIT_TYPE_DECIDED_WORD = 2; - // COMMIT_TYPE_CANCEL_AUTO_CORRECT is used upon committing back the old word upon cancelling - // an auto-correction. - public static final int COMMIT_TYPE_CANCEL_AUTO_CORRECT = 3; - - public static final String NOT_A_SEPARATOR = ""; - - public final ArrayList<Event> mEvents; - public final String mTypedWord; - public final CharSequence mCommittedWord; - public final String mSeparatorString; - public final NgramContext mNgramContext; - public final int mCapitalizedMode; - public final InputPointers mInputPointers = - new InputPointers(DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH); - - private boolean mActive; - - public static final LastComposedWord NOT_A_COMPOSED_WORD = - new LastComposedWord(new ArrayList<Event>(), null, "", "", - NOT_A_SEPARATOR, null, WordComposer.CAPS_MODE_OFF); - - // Warning: this is using the passed objects as is and fully expects them to be - // immutable. Do not fiddle with their contents after you passed them to this constructor. - public LastComposedWord(final ArrayList<Event> events, - final InputPointers inputPointers, final String typedWord, - final CharSequence committedWord, final String separatorString, - final NgramContext ngramContext, final int capitalizedMode) { - if (inputPointers != null) { - mInputPointers.copy(inputPointers); - } - mTypedWord = typedWord; - mEvents = new ArrayList<>(events); - mCommittedWord = committedWord; - mSeparatorString = separatorString; - mActive = true; - mNgramContext = ngramContext; - mCapitalizedMode = capitalizedMode; - } - - public void deactivate() { - mActive = false; - } - - public boolean canRevertCommit() { - return mActive && !TextUtils.isEmpty(mCommittedWord) && !didCommitTypedWord(); - } - - private boolean didCommitTypedWord() { - return TextUtils.equals(mTypedWord, mCommittedWord); - } -} diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java deleted file mode 100644 index e68b43b39..000000000 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ /dev/null @@ -1,2033 +0,0 @@ -/* - * Copyright (C) 2008 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 static com.android.inputmethod.latin.common.Constants.ImeOption.FORCE_ASCII; -import static com.android.inputmethod.latin.common.Constants.ImeOption.NO_MICROPHONE; -import static com.android.inputmethod.latin.common.Constants.ImeOption.NO_MICROPHONE_COMPAT; - -import android.Manifest.permission; -import android.app.ActivityOptions; -import android.app.AlertDialog; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.res.Configuration; -import android.content.res.Resources; -import android.graphics.Color; -import android.inputmethodservice.InputMethodService; -import android.media.AudioManager; -import android.os.Build; -import android.os.Debug; -import android.os.IBinder; -import android.os.Message; -import android.preference.PreferenceManager; -import android.text.InputType; -import android.util.Log; -import android.util.PrintWriterPrinter; -import android.util.Printer; -import android.util.SparseArray; -import android.view.Display; -import android.view.Gravity; -import android.view.KeyEvent; -import android.view.View; -import android.view.ViewGroup.LayoutParams; -import android.view.Window; -import android.view.WindowManager; -import android.view.inputmethod.CompletionInfo; -import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputMethodSubtype; - -import androidx.annotation.NonNull; - -import com.android.inputmethod.accessibility.AccessibilityUtils; -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.compat.BuildCompatUtils; -import com.android.inputmethod.compat.EditorInfoCompatUtils; -import com.android.inputmethod.compat.InputMethodServiceCompatUtils; -import com.android.inputmethod.compat.ViewOutlineProviderCompatUtils; -import com.android.inputmethod.compat.ViewOutlineProviderCompatUtils.InsetsUpdater; -import com.android.inputmethod.dictionarypack.DictionaryPackConstants; -import com.android.inputmethod.event.Event; -import com.android.inputmethod.event.HardwareEventDecoder; -import com.android.inputmethod.event.HardwareKeyboardEventDecoder; -import com.android.inputmethod.event.InputTransaction; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.KeyboardActionListener; -import com.android.inputmethod.keyboard.KeyboardId; -import com.android.inputmethod.keyboard.KeyboardSwitcher; -import com.android.inputmethod.keyboard.MainKeyboardView; -import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.CoordinateUtils; -import com.android.inputmethod.latin.common.InputPointers; -import com.android.inputmethod.latin.define.DebugFlags; -import com.android.inputmethod.latin.define.ProductionFlags; -import com.android.inputmethod.latin.inputlogic.InputLogic; -import com.android.inputmethod.latin.permissions.PermissionsManager; -import com.android.inputmethod.latin.personalization.PersonalizationHelper; -import com.android.inputmethod.latin.settings.Settings; -import com.android.inputmethod.latin.settings.SettingsActivity; -import com.android.inputmethod.latin.settings.SettingsValues; -import com.android.inputmethod.latin.suggestions.SuggestionStripView; -import com.android.inputmethod.latin.suggestions.SuggestionStripViewAccessor; -import com.android.inputmethod.latin.touchinputconsumer.GestureConsumer; -import com.android.inputmethod.latin.utils.ApplicationUtils; -import com.android.inputmethod.latin.utils.DialogUtils; -import com.android.inputmethod.latin.utils.ImportantNoticeUtils; -import com.android.inputmethod.latin.utils.IntentUtils; -import com.android.inputmethod.latin.utils.JniUtils; -import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper; -import com.android.inputmethod.latin.utils.StatsUtils; -import com.android.inputmethod.latin.utils.StatsUtilsManager; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; -import com.android.inputmethod.latin.utils.ViewLayoutUtils; - -import java.io.FileDescriptor; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.concurrent.TimeUnit; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Input method implementation for Qwerty'ish keyboard. - */ -public class LatinIME extends InputMethodService implements KeyboardActionListener, - SuggestionStripView.Listener, SuggestionStripViewAccessor, - DictionaryFacilitator.DictionaryInitializationListener, - PermissionsManager.PermissionsResultCallback { - static final String TAG = LatinIME.class.getSimpleName(); - private static final boolean TRACE = false; - - private static final int PERIOD_FOR_AUDIO_AND_HAPTIC_FEEDBACK_IN_KEY_REPEAT = 2; - private static final int PENDING_IMS_CALLBACK_DURATION_MILLIS = 800; - static final long DELAY_WAIT_FOR_DICTIONARY_LOAD_MILLIS = TimeUnit.SECONDS.toMillis(2); - static final long DELAY_DEALLOCATE_MEMORY_MILLIS = TimeUnit.SECONDS.toMillis(10); - - /** - * A broadcast intent action to hide the software keyboard. - */ - static final String ACTION_HIDE_SOFT_INPUT = - "com.android.inputmethod.latin.HIDE_SOFT_INPUT"; - - /** - * A custom permission for external apps to send {@link #ACTION_HIDE_SOFT_INPUT}. - */ - static final String PERMISSION_HIDE_SOFT_INPUT = - "com.android.inputmethod.latin.HIDE_SOFT_INPUT"; - - /** - * The name of the scheme used by the Package Manager to warn of a new package installation, - * replacement or removal. - */ - private static final String SCHEME_PACKAGE = "package"; - - final Settings mSettings; - private final DictionaryFacilitator mDictionaryFacilitator = - DictionaryFacilitatorProvider.getDictionaryFacilitator( - false /* isNeededForSpellChecking */); - final InputLogic mInputLogic = new InputLogic(this /* LatinIME */, - this /* SuggestionStripViewAccessor */, mDictionaryFacilitator); - // We expect to have only one decoder in almost all cases, hence the default capacity of 1. - // If it turns out we need several, it will get grown seamlessly. - final SparseArray<HardwareEventDecoder> mHardwareEventDecoders = new SparseArray<>(1); - - // TODO: Move these {@link View}s to {@link KeyboardSwitcher}. - private View mInputView; - private InsetsUpdater mInsetsUpdater; - private SuggestionStripView mSuggestionStripView; - - private RichInputMethodManager mRichImm; - @UsedForTesting final KeyboardSwitcher mKeyboardSwitcher; - private final SubtypeState mSubtypeState = new SubtypeState(); - private EmojiAltPhysicalKeyDetector mEmojiAltPhysicalKeyDetector; - private StatsUtilsManager mStatsUtilsManager; - // Working variable for {@link #startShowingInputView()} and - // {@link #onEvaluateInputViewShown()}. - private boolean mIsExecutingStartShowingInputView; - - // Used for re-initialize keyboard layout after onConfigurationChange. - @Nullable private Context mDisplayContext; - - // Object for reacting to adding/removing a dictionary pack. - private final BroadcastReceiver mDictionaryPackInstallReceiver = - new DictionaryPackInstallBroadcastReceiver(this); - - private final BroadcastReceiver mDictionaryDumpBroadcastReceiver = - new DictionaryDumpBroadcastReceiver(this); - - final static class HideSoftInputReceiver extends BroadcastReceiver { - private final InputMethodService mIms; - - public HideSoftInputReceiver(InputMethodService ims) { - mIms = ims; - } - - @Override - public void onReceive(Context context, Intent intent) { - final String action = intent.getAction(); - if (ACTION_HIDE_SOFT_INPUT.equals(action)) { - mIms.requestHideSelf(0 /* flags */); - } else { - Log.e(TAG, "Unexpected intent " + intent); - } - } - } - final HideSoftInputReceiver mHideSoftInputReceiver = new HideSoftInputReceiver(this); - - private AlertDialog mOptionsDialog; - - private final boolean mIsHardwareAcceleratedDrawingEnabled; - - private GestureConsumer mGestureConsumer = GestureConsumer.NULL_GESTURE_CONSUMER; - - public final UIHandler mHandler = new UIHandler(this); - - public static final class UIHandler extends LeakGuardHandlerWrapper<LatinIME> { - private static final int MSG_UPDATE_SHIFT_STATE = 0; - private static final int MSG_PENDING_IMS_CALLBACK = 1; - private static final int MSG_UPDATE_SUGGESTION_STRIP = 2; - private static final int MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP = 3; - private static final int MSG_RESUME_SUGGESTIONS = 4; - private static final int MSG_REOPEN_DICTIONARIES = 5; - private static final int MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED = 6; - private static final int MSG_RESET_CACHES = 7; - private static final int MSG_WAIT_FOR_DICTIONARY_LOAD = 8; - private static final int MSG_DEALLOCATE_MEMORY = 9; - private static final int MSG_RESUME_SUGGESTIONS_FOR_START_INPUT = 10; - private static final int MSG_SWITCH_LANGUAGE_AUTOMATICALLY = 11; - // Update this when adding new messages - private static final int MSG_LAST = MSG_SWITCH_LANGUAGE_AUTOMATICALLY; - - private static final int ARG1_NOT_GESTURE_INPUT = 0; - private static final int ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT = 1; - private static final int ARG1_SHOW_GESTURE_FLOATING_PREVIEW_TEXT = 2; - private static final int ARG2_UNUSED = 0; - private static final int ARG1_TRUE = 1; - - private int mDelayInMillisecondsToUpdateSuggestions; - private int mDelayInMillisecondsToUpdateShiftState; - - public UIHandler(@Nonnull final LatinIME ownerInstance) { - super(ownerInstance); - } - - public void onCreate() { - final LatinIME latinIme = getOwnerInstance(); - if (latinIme == null) { - return; - } - final Resources res = latinIme.getResources(); - mDelayInMillisecondsToUpdateSuggestions = res.getInteger( - R.integer.config_delay_in_milliseconds_to_update_suggestions); - mDelayInMillisecondsToUpdateShiftState = res.getInteger( - R.integer.config_delay_in_milliseconds_to_update_shift_state); - } - - @Override - public void handleMessage(final Message msg) { - final LatinIME latinIme = getOwnerInstance(); - if (latinIme == null) { - return; - } - final KeyboardSwitcher switcher = latinIme.mKeyboardSwitcher; - switch (msg.what) { - case MSG_UPDATE_SUGGESTION_STRIP: - cancelUpdateSuggestionStrip(); - latinIme.mInputLogic.performUpdateSuggestionStripSync( - latinIme.mSettings.getCurrent(), msg.arg1 /* inputStyle */); - break; - case MSG_UPDATE_SHIFT_STATE: - switcher.requestUpdatingShiftState(latinIme.getCurrentAutoCapsState(), - latinIme.getCurrentRecapitalizeState()); - break; - case MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP: - if (msg.arg1 == ARG1_NOT_GESTURE_INPUT) { - final SuggestedWords suggestedWords = (SuggestedWords) msg.obj; - latinIme.showSuggestionStrip(suggestedWords); - } else { - latinIme.showGesturePreviewAndSuggestionStrip((SuggestedWords) msg.obj, - msg.arg1 == ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT); - } - break; - case MSG_RESUME_SUGGESTIONS: - latinIme.mInputLogic.restartSuggestionsOnWordTouchedByCursor( - latinIme.mSettings.getCurrent(), false /* forStartInput */, - latinIme.mKeyboardSwitcher.getCurrentKeyboardScriptId()); - break; - case MSG_RESUME_SUGGESTIONS_FOR_START_INPUT: - latinIme.mInputLogic.restartSuggestionsOnWordTouchedByCursor( - latinIme.mSettings.getCurrent(), true /* forStartInput */, - latinIme.mKeyboardSwitcher.getCurrentKeyboardScriptId()); - break; - case MSG_REOPEN_DICTIONARIES: - // We need to re-evaluate the currently composing word in case the script has - // changed. - postWaitForDictionaryLoad(); - latinIme.resetDictionaryFacilitatorIfNecessary(); - break; - case MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED: - final SuggestedWords suggestedWords = (SuggestedWords) msg.obj; - latinIme.mInputLogic.onUpdateTailBatchInputCompleted( - latinIme.mSettings.getCurrent(), - suggestedWords, latinIme.mKeyboardSwitcher); - latinIme.onTailBatchInputResultShown(suggestedWords); - break; - case MSG_RESET_CACHES: - final SettingsValues settingsValues = latinIme.mSettings.getCurrent(); - if (latinIme.mInputLogic.retryResetCachesAndReturnSuccess( - msg.arg1 == ARG1_TRUE /* tryResumeSuggestions */, - msg.arg2 /* remainingTries */, this /* handler */)) { - // If we were able to reset the caches, then we can reload the keyboard. - // Otherwise, we'll do it when we can. - latinIme.mKeyboardSwitcher.loadKeyboard(latinIme.getCurrentInputEditorInfo(), - settingsValues, latinIme.getCurrentAutoCapsState(), - latinIme.getCurrentRecapitalizeState()); - } - break; - case MSG_WAIT_FOR_DICTIONARY_LOAD: - Log.i(TAG, "Timeout waiting for dictionary load"); - break; - case MSG_DEALLOCATE_MEMORY: - latinIme.deallocateMemory(); - break; - case MSG_SWITCH_LANGUAGE_AUTOMATICALLY: - latinIme.switchLanguage((InputMethodSubtype)msg.obj); - break; - } - } - - public void postUpdateSuggestionStrip(final int inputStyle) { - sendMessageDelayed(obtainMessage(MSG_UPDATE_SUGGESTION_STRIP, inputStyle, - 0 /* ignored */), mDelayInMillisecondsToUpdateSuggestions); - } - - public void postReopenDictionaries() { - sendMessage(obtainMessage(MSG_REOPEN_DICTIONARIES)); - } - - private void postResumeSuggestionsInternal(final boolean shouldDelay, - final boolean forStartInput) { - final LatinIME latinIme = getOwnerInstance(); - if (latinIme == null) { - return; - } - if (!latinIme.mSettings.getCurrent().isSuggestionsEnabledPerUserSettings()) { - return; - } - removeMessages(MSG_RESUME_SUGGESTIONS); - removeMessages(MSG_RESUME_SUGGESTIONS_FOR_START_INPUT); - final int message = forStartInput ? MSG_RESUME_SUGGESTIONS_FOR_START_INPUT - : MSG_RESUME_SUGGESTIONS; - if (shouldDelay) { - sendMessageDelayed(obtainMessage(message), - mDelayInMillisecondsToUpdateSuggestions); - } else { - sendMessage(obtainMessage(message)); - } - } - - public void postResumeSuggestions(final boolean shouldDelay) { - postResumeSuggestionsInternal(shouldDelay, false /* forStartInput */); - } - - public void postResumeSuggestionsForStartInput(final boolean shouldDelay) { - postResumeSuggestionsInternal(shouldDelay, true /* forStartInput */); - } - - public void postResetCaches(final boolean tryResumeSuggestions, final int remainingTries) { - removeMessages(MSG_RESET_CACHES); - sendMessage(obtainMessage(MSG_RESET_CACHES, tryResumeSuggestions ? 1 : 0, - remainingTries, null)); - } - - public void postWaitForDictionaryLoad() { - sendMessageDelayed(obtainMessage(MSG_WAIT_FOR_DICTIONARY_LOAD), - DELAY_WAIT_FOR_DICTIONARY_LOAD_MILLIS); - } - - public void cancelWaitForDictionaryLoad() { - removeMessages(MSG_WAIT_FOR_DICTIONARY_LOAD); - } - - public boolean hasPendingWaitForDictionaryLoad() { - return hasMessages(MSG_WAIT_FOR_DICTIONARY_LOAD); - } - - public void cancelUpdateSuggestionStrip() { - removeMessages(MSG_UPDATE_SUGGESTION_STRIP); - } - - public boolean hasPendingUpdateSuggestions() { - return hasMessages(MSG_UPDATE_SUGGESTION_STRIP); - } - - public boolean hasPendingReopenDictionaries() { - return hasMessages(MSG_REOPEN_DICTIONARIES); - } - - public void postUpdateShiftState() { - removeMessages(MSG_UPDATE_SHIFT_STATE); - sendMessageDelayed(obtainMessage(MSG_UPDATE_SHIFT_STATE), - mDelayInMillisecondsToUpdateShiftState); - } - - public void postDeallocateMemory() { - sendMessageDelayed(obtainMessage(MSG_DEALLOCATE_MEMORY), - DELAY_DEALLOCATE_MEMORY_MILLIS); - } - - public void cancelDeallocateMemory() { - removeMessages(MSG_DEALLOCATE_MEMORY); - } - - public boolean hasPendingDeallocateMemory() { - return hasMessages(MSG_DEALLOCATE_MEMORY); - } - - @UsedForTesting - public void removeAllMessages() { - for (int i = 0; i <= MSG_LAST; ++i) { - removeMessages(i); - } - } - - public void showGesturePreviewAndSuggestionStrip(final SuggestedWords suggestedWords, - final boolean dismissGestureFloatingPreviewText) { - removeMessages(MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP); - final int arg1 = dismissGestureFloatingPreviewText - ? ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT - : ARG1_SHOW_GESTURE_FLOATING_PREVIEW_TEXT; - obtainMessage(MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP, arg1, - ARG2_UNUSED, suggestedWords).sendToTarget(); - } - - public void showSuggestionStrip(final SuggestedWords suggestedWords) { - removeMessages(MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP); - obtainMessage(MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP, - ARG1_NOT_GESTURE_INPUT, ARG2_UNUSED, suggestedWords).sendToTarget(); - } - - public void showTailBatchInputResult(final SuggestedWords suggestedWords) { - obtainMessage(MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED, suggestedWords).sendToTarget(); - } - - public void postSwitchLanguage(final InputMethodSubtype subtype) { - obtainMessage(MSG_SWITCH_LANGUAGE_AUTOMATICALLY, subtype).sendToTarget(); - } - - // Working variables for the following methods. - private boolean mIsOrientationChanging; - private boolean mPendingSuccessiveImsCallback; - private boolean mHasPendingStartInput; - private boolean mHasPendingFinishInputView; - private boolean mHasPendingFinishInput; - private EditorInfo mAppliedEditorInfo; - - public void startOrientationChanging() { - removeMessages(MSG_PENDING_IMS_CALLBACK); - resetPendingImsCallback(); - mIsOrientationChanging = true; - final LatinIME latinIme = getOwnerInstance(); - if (latinIme == null) { - return; - } - if (latinIme.isInputViewShown()) { - latinIme.mKeyboardSwitcher.saveKeyboardState(); - } - } - - private void resetPendingImsCallback() { - mHasPendingFinishInputView = false; - mHasPendingFinishInput = false; - mHasPendingStartInput = false; - } - - private void executePendingImsCallback(final LatinIME latinIme, final EditorInfo editorInfo, - boolean restarting) { - if (mHasPendingFinishInputView) { - latinIme.onFinishInputViewInternal(mHasPendingFinishInput); - } - if (mHasPendingFinishInput) { - latinIme.onFinishInputInternal(); - } - if (mHasPendingStartInput) { - latinIme.onStartInputInternal(editorInfo, restarting); - } - resetPendingImsCallback(); - } - - public void onStartInput(final EditorInfo editorInfo, final boolean restarting) { - if (hasMessages(MSG_PENDING_IMS_CALLBACK)) { - // Typically this is the second onStartInput after orientation changed. - mHasPendingStartInput = true; - } else { - if (mIsOrientationChanging && restarting) { - // This is the first onStartInput after orientation changed. - mIsOrientationChanging = false; - mPendingSuccessiveImsCallback = true; - } - final LatinIME latinIme = getOwnerInstance(); - if (latinIme != null) { - executePendingImsCallback(latinIme, editorInfo, restarting); - latinIme.onStartInputInternal(editorInfo, restarting); - } - } - } - - public void onStartInputView(final EditorInfo editorInfo, final boolean restarting) { - if (hasMessages(MSG_PENDING_IMS_CALLBACK) - && KeyboardId.equivalentEditorInfoForKeyboard(editorInfo, mAppliedEditorInfo)) { - // Typically this is the second onStartInputView after orientation changed. - resetPendingImsCallback(); - } else { - if (mPendingSuccessiveImsCallback) { - // This is the first onStartInputView after orientation changed. - mPendingSuccessiveImsCallback = false; - resetPendingImsCallback(); - sendMessageDelayed(obtainMessage(MSG_PENDING_IMS_CALLBACK), - PENDING_IMS_CALLBACK_DURATION_MILLIS); - } - final LatinIME latinIme = getOwnerInstance(); - if (latinIme != null) { - executePendingImsCallback(latinIme, editorInfo, restarting); - latinIme.onStartInputViewInternal(editorInfo, restarting); - mAppliedEditorInfo = editorInfo; - } - cancelDeallocateMemory(); - } - } - - public void onFinishInputView(final boolean finishingInput) { - if (hasMessages(MSG_PENDING_IMS_CALLBACK)) { - // Typically this is the first onFinishInputView after orientation changed. - mHasPendingFinishInputView = true; - } else { - final LatinIME latinIme = getOwnerInstance(); - if (latinIme != null) { - latinIme.onFinishInputViewInternal(finishingInput); - mAppliedEditorInfo = null; - } - if (!hasPendingDeallocateMemory()) { - postDeallocateMemory(); - } - } - } - - public void onFinishInput() { - if (hasMessages(MSG_PENDING_IMS_CALLBACK)) { - // Typically this is the first onFinishInput after orientation changed. - mHasPendingFinishInput = true; - } else { - final LatinIME latinIme = getOwnerInstance(); - if (latinIme != null) { - executePendingImsCallback(latinIme, null, false); - latinIme.onFinishInputInternal(); - } - } - } - } - - static final class SubtypeState { - private InputMethodSubtype mLastActiveSubtype; - private boolean mCurrentSubtypeHasBeenUsed; - - public void setCurrentSubtypeHasBeenUsed() { - mCurrentSubtypeHasBeenUsed = true; - } - - public void switchSubtype(final IBinder token, final RichInputMethodManager richImm) { - final InputMethodSubtype currentSubtype = richImm.getInputMethodManager() - .getCurrentInputMethodSubtype(); - final InputMethodSubtype lastActiveSubtype = mLastActiveSubtype; - final boolean currentSubtypeHasBeenUsed = mCurrentSubtypeHasBeenUsed; - if (currentSubtypeHasBeenUsed) { - mLastActiveSubtype = currentSubtype; - mCurrentSubtypeHasBeenUsed = false; - } - if (currentSubtypeHasBeenUsed - && richImm.checkIfSubtypeBelongsToThisImeAndEnabled(lastActiveSubtype) - && !currentSubtype.equals(lastActiveSubtype)) { - richImm.setInputMethodAndSubtype(token, lastActiveSubtype); - return; - } - richImm.switchToNextInputMethod(token, true /* onlyCurrentIme */); - } - } - - // Loading the native library eagerly to avoid unexpected UnsatisfiedLinkError at the initial - // JNI call as much as possible. - static { - JniUtils.loadNativeLibrary(); - } - - public LatinIME() { - super(); - mSettings = Settings.getInstance(); - mKeyboardSwitcher = KeyboardSwitcher.getInstance(); - mStatsUtilsManager = StatsUtilsManager.getInstance(); - mIsHardwareAcceleratedDrawingEnabled = - InputMethodServiceCompatUtils.enableHardwareAcceleration(this); - Log.i(TAG, "Hardware accelerated drawing: " + mIsHardwareAcceleratedDrawingEnabled); - } - - @Override - public void onCreate() { - Settings.init(this); - DebugFlags.init(PreferenceManager.getDefaultSharedPreferences(this)); - RichInputMethodManager.init(this); - mRichImm = RichInputMethodManager.getInstance(); - AudioAndHapticFeedbackManager.init(this); - AccessibilityUtils.init(this); - mStatsUtilsManager.onCreate(this /* context */, mDictionaryFacilitator); - final WindowManager wm = getSystemService(WindowManager.class); - mDisplayContext = getDisplayContext(); - KeyboardSwitcher.init(this); - super.onCreate(); - - mHandler.onCreate(); - - // TODO: Resolve mutual dependencies of {@link #loadSettings()} and - // {@link #resetDictionaryFacilitatorIfNecessary()}. - loadSettings(); - resetDictionaryFacilitatorIfNecessary(); - - // Register to receive ringer mode change. - final IntentFilter filter = new IntentFilter(); - filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION); - registerReceiver(mRingerModeChangeReceiver, filter); - - // Register to receive installation and removal of a dictionary pack. - final IntentFilter packageFilter = new IntentFilter(); - packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED); - packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); - packageFilter.addDataScheme(SCHEME_PACKAGE); - registerReceiver(mDictionaryPackInstallReceiver, packageFilter); - - final IntentFilter newDictFilter = new IntentFilter(); - newDictFilter.addAction(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - registerReceiver(mDictionaryPackInstallReceiver, newDictFilter, - Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(mDictionaryPackInstallReceiver, newDictFilter); - } - - final IntentFilter dictDumpFilter = new IntentFilter(); - dictDumpFilter.addAction(DictionaryDumpBroadcastReceiver.DICTIONARY_DUMP_INTENT_ACTION); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - registerReceiver(mDictionaryDumpBroadcastReceiver, dictDumpFilter, - Context.RECEIVER_NOT_EXPORTED); - } else { - registerReceiver(mDictionaryDumpBroadcastReceiver, dictDumpFilter); - } - - final IntentFilter hideSoftInputFilter = new IntentFilter(); - hideSoftInputFilter.addAction(ACTION_HIDE_SOFT_INPUT); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - registerReceiver(mHideSoftInputReceiver, hideSoftInputFilter, - PERMISSION_HIDE_SOFT_INPUT, null /* scheduler */, Context.RECEIVER_EXPORTED); - } else { - registerReceiver(mHideSoftInputReceiver, hideSoftInputFilter, - PERMISSION_HIDE_SOFT_INPUT, null /* scheduler */); - } - - StatsUtils.onCreate(mSettings.getCurrent(), mRichImm); - } - - // Has to be package-visible for unit tests - @UsedForTesting - void loadSettings() { - final Locale locale = mRichImm.getCurrentSubtypeLocale(); - final EditorInfo editorInfo = getCurrentInputEditorInfo(); - final InputAttributes inputAttributes = new InputAttributes( - editorInfo, isFullscreenMode(), getPackageName()); - mSettings.loadSettings(this, locale, inputAttributes); - final SettingsValues currentSettingsValues = mSettings.getCurrent(); - AudioAndHapticFeedbackManager.getInstance().onSettingsChanged(currentSettingsValues); - // This method is called on startup and language switch, before the new layout has - // been displayed. Opening dictionaries never affects responsivity as dictionaries are - // asynchronously loaded. - if (!mHandler.hasPendingReopenDictionaries()) { - resetDictionaryFacilitator(locale); - } - refreshPersonalizationDictionarySession(currentSettingsValues); - resetDictionaryFacilitatorIfNecessary(); - mStatsUtilsManager.onLoadSettings(this /* context */, currentSettingsValues); - } - - private void refreshPersonalizationDictionarySession( - final SettingsValues currentSettingsValues) { - if (!currentSettingsValues.mUsePersonalizedDicts) { - // Remove user history dictionaries. - PersonalizationHelper.removeAllUserHistoryDictionaries(this); - mDictionaryFacilitator.clearUserHistoryDictionary(this); - } - } - - // Note that this method is called from a non-UI thread. - @Override - public void onUpdateMainDictionaryAvailability(final boolean isMainDictionaryAvailable) { - final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); - if (mainKeyboardView != null) { - mainKeyboardView.setMainDictionaryAvailability(isMainDictionaryAvailable); - } - if (mHandler.hasPendingWaitForDictionaryLoad()) { - mHandler.cancelWaitForDictionaryLoad(); - mHandler.postResumeSuggestions(false /* shouldDelay */); - } - } - - void resetDictionaryFacilitatorIfNecessary() { - final Locale subtypeSwitcherLocale = mRichImm.getCurrentSubtypeLocale(); - final Locale subtypeLocale; - if (subtypeSwitcherLocale == null) { - // This happens in very rare corner cases - for example, immediately after a switch - // to LatinIME has been requested, about a frame later another switch happens. In this - // case, we are about to go down but we still don't know it, however the system tells - // us there is no current subtype. - Log.e(TAG, "System is reporting no current subtype."); - subtypeLocale = getResources().getConfiguration().locale; - } else { - subtypeLocale = subtypeSwitcherLocale; - } - if (mDictionaryFacilitator.isForLocale(subtypeLocale) - && mDictionaryFacilitator.isForAccount(mSettings.getCurrent().mAccount)) { - return; - } - resetDictionaryFacilitator(subtypeLocale); - } - - /** - * Reset the facilitator by loading dictionaries for the given locale and - * the current settings values. - * - * @param locale the locale - */ - // TODO: make sure the current settings always have the right locales, and read from them. - private void resetDictionaryFacilitator(final Locale locale) { - final SettingsValues settingsValues = mSettings.getCurrent(); - mDictionaryFacilitator.resetDictionaries(this /* context */, locale, - settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts, - false /* forceReloadMainDictionary */, - settingsValues.mAccount, "" /* dictNamePrefix */, - this /* DictionaryInitializationListener */); - if (settingsValues.mAutoCorrectionEnabledPerUserSettings) { - mInputLogic.mSuggest.setAutoCorrectionThreshold( - settingsValues.mAutoCorrectionThreshold); - } - mInputLogic.mSuggest.setPlausibilityThreshold(settingsValues.mPlausibilityThreshold); - } - - /** - * Reset suggest by loading the main dictionary of the current locale. - */ - /* package private */ void resetSuggestMainDict() { - final SettingsValues settingsValues = mSettings.getCurrent(); - mDictionaryFacilitator.resetDictionaries(this /* context */, - mDictionaryFacilitator.getLocale(), settingsValues.mUseContactsDict, - settingsValues.mUsePersonalizedDicts, - true /* forceReloadMainDictionary */, - settingsValues.mAccount, "" /* dictNamePrefix */, - this /* DictionaryInitializationListener */); - } - - @Override - public void onDestroy() { - mDictionaryFacilitator.closeDictionaries(); - mSettings.onDestroy(); - unregisterReceiver(mHideSoftInputReceiver); - unregisterReceiver(mRingerModeChangeReceiver); - unregisterReceiver(mDictionaryPackInstallReceiver); - unregisterReceiver(mDictionaryDumpBroadcastReceiver); - mStatsUtilsManager.onDestroy(this /* context */); - super.onDestroy(); - } - - @UsedForTesting - public void recycle() { - unregisterReceiver(mDictionaryPackInstallReceiver); - unregisterReceiver(mDictionaryDumpBroadcastReceiver); - unregisterReceiver(mRingerModeChangeReceiver); - mInputLogic.recycle(); - } - - private boolean isImeSuppressedByHardwareKeyboard() { - final KeyboardSwitcher switcher = KeyboardSwitcher.getInstance(); - return !onEvaluateInputViewShown() && switcher.isImeSuppressedByHardwareKeyboard( - mSettings.getCurrent(), switcher.getKeyboardSwitchState()); - } - - @Override - public void onConfigurationChanged(final Configuration conf) { - SettingsValues settingsValues = mSettings.getCurrent(); - if (settingsValues.mDisplayOrientation != conf.orientation) { - mHandler.startOrientationChanging(); - mInputLogic.onOrientationChange(mSettings.getCurrent()); - } - if (settingsValues.mHasHardwareKeyboard != Settings.readHasHardwareKeyboard(conf)) { - // If the state of having a hardware keyboard changed, then we want to reload the - // settings to adjust for that. - // TODO: we should probably do this unconditionally here, rather than only when we - // have a change in hardware keyboard configuration. - loadSettings(); - settingsValues = mSettings.getCurrent(); - if (isImeSuppressedByHardwareKeyboard()) { - // We call cleanupInternalStateForFinishInput() because it's the right thing to do; - // however, it seems at the moment the framework is passing us a seemingly valid - // but actually non-functional InputConnection object. So if this bug ever gets - // fixed we'll be able to remove the composition, but until it is this code is - // actually not doing much. - cleanupInternalStateForFinishInput(); - } - } - super.onConfigurationChanged(conf); - } - - @Override - public void onInitializeInterface() { - mDisplayContext = getDisplayContext(); - mKeyboardSwitcher.updateKeyboardTheme(mDisplayContext); - } - - /** - * Returns the context object whose resources are adjusted to match the metrics of the display. - * - * Note that before {@link android.os.Build.VERSION_CODES#KITKAT}, there is no way to support - * multi-display scenarios, so the context object will just return the IME context itself. - * - * With initiating multi-display APIs from {@link android.os.Build.VERSION_CODES#KITKAT}, the - * context object has to return with re-creating the display context according the metrics - * of the display in runtime. - * - * Starts from {@link android.os.Build.VERSION_CODES#S_V2}, the returning context object has - * became to IME context self since it ends up capable of updating its resources internally. - * - * @see android.content.Context#createDisplayContext(Display) - */ - private @NonNull Context getDisplayContext() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { - // createDisplayContext is not available. - return this; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) { - // IME context sources is now managed by WindowProviderService from Android 12L. - return this; - } - // An issue in Q that non-activity components Resources / DisplayMetrics in - // Context doesn't well updated when the IME window moving to external display. - // Currently we do a workaround is to create new display context directly and re-init - // keyboard layout with this context. - final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); - return createDisplayContext(wm.getDefaultDisplay()); - } - - @Override - public View onCreateInputView() { - StatsUtils.onCreateInputView(); - return mKeyboardSwitcher.onCreateInputView(mDisplayContext, - mIsHardwareAcceleratedDrawingEnabled); - } - - @Override - public void setInputView(final View view) { - super.setInputView(view); - mInputView = view; - mInsetsUpdater = ViewOutlineProviderCompatUtils.setInsetsOutlineProvider(view); - updateSoftInputWindowLayoutParameters(); - mSuggestionStripView = (SuggestionStripView)view.findViewById(R.id.suggestion_strip_view); - if (hasSuggestionStripView()) { - mSuggestionStripView.setListener(this, view); - } - } - - @Override - public void setCandidatesView(final View view) { - // To ensure that CandidatesView will never be set. - } - - @Override - public void onStartInput(final EditorInfo editorInfo, final boolean restarting) { - mHandler.onStartInput(editorInfo, restarting); - } - - @Override - public void onStartInputView(final EditorInfo editorInfo, final boolean restarting) { - mHandler.onStartInputView(editorInfo, restarting); - mStatsUtilsManager.onStartInputView(); - } - - @Override - public void onFinishInputView(final boolean finishingInput) { - StatsUtils.onFinishInputView(); - mHandler.onFinishInputView(finishingInput); - mStatsUtilsManager.onFinishInputView(); - mGestureConsumer = GestureConsumer.NULL_GESTURE_CONSUMER; - } - - @Override - public void onFinishInput() { - mHandler.onFinishInput(); - } - - @Override - public void onCurrentInputMethodSubtypeChanged(final InputMethodSubtype subtype) { - // Note that the calling sequence of onCreate() and onCurrentInputMethodSubtypeChanged() - // is not guaranteed. It may even be called at the same time on a different thread. - InputMethodSubtype oldSubtype = mRichImm.getCurrentSubtype().getRawSubtype(); - StatsUtils.onSubtypeChanged(oldSubtype, subtype); - mRichImm.onSubtypeChanged(subtype); - mInputLogic.onSubtypeChanged(SubtypeLocaleUtils.getCombiningRulesExtraValue(subtype), - mSettings.getCurrent()); - loadKeyboard(); - } - - void onStartInputInternal(final EditorInfo editorInfo, final boolean restarting) { - super.onStartInput(editorInfo, restarting); - - // If the primary hint language does not match the current subtype language, then try - // to switch to the primary hint language. - // TODO: Support all the locales in EditorInfo#hintLocales. - final Locale primaryHintLocale = EditorInfoCompatUtils.getPrimaryHintLocale(editorInfo); - if (primaryHintLocale == null) { - return; - } - final InputMethodSubtype newSubtype = mRichImm.findSubtypeByLocale(primaryHintLocale); - if (newSubtype == null || newSubtype.equals(mRichImm.getCurrentSubtype().getRawSubtype())) { - return; - } - mHandler.postSwitchLanguage(newSubtype); - } - - @SuppressWarnings("deprecation") - void onStartInputViewInternal(final EditorInfo editorInfo, final boolean restarting) { - super.onStartInputView(editorInfo, restarting); - - mDictionaryFacilitator.onStartInput(); - // Switch to the null consumer to handle cases leading to early exit below, for which we - // also wouldn't be consuming gesture data. - mGestureConsumer = GestureConsumer.NULL_GESTURE_CONSUMER; - mRichImm.refreshSubtypeCaches(); - final KeyboardSwitcher switcher = mKeyboardSwitcher; - switcher.updateKeyboardTheme(mDisplayContext); - final MainKeyboardView mainKeyboardView = switcher.getMainKeyboardView(); - // If we are starting input in a different text field from before, we'll have to reload - // settings, so currentSettingsValues can't be final. - SettingsValues currentSettingsValues = mSettings.getCurrent(); - - if (editorInfo == null) { - Log.e(TAG, "Null EditorInfo in onStartInputView()"); - if (DebugFlags.DEBUG_ENABLED) { - throw new NullPointerException("Null EditorInfo in onStartInputView()"); - } - return; - } - if (DebugFlags.DEBUG_ENABLED) { - Log.d(TAG, "onStartInputView: editorInfo:" - + String.format("inputType=0x%08x imeOptions=0x%08x", - editorInfo.inputType, editorInfo.imeOptions)); - Log.d(TAG, "All caps = " - + ((editorInfo.inputType & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS) != 0) - + ", sentence caps = " - + ((editorInfo.inputType & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) != 0) - + ", word caps = " - + ((editorInfo.inputType & InputType.TYPE_TEXT_FLAG_CAP_WORDS) != 0)); - } - Log.i(TAG, "Starting input. Cursor position = " - + editorInfo.initialSelStart + "," + editorInfo.initialSelEnd); - // TODO: Consolidate these checks with {@link InputAttributes}. - if (InputAttributes.inPrivateImeOptions(null, NO_MICROPHONE_COMPAT, editorInfo)) { - Log.w(TAG, "Deprecated private IME option specified: " + editorInfo.privateImeOptions); - Log.w(TAG, "Use " + getPackageName() + "." + NO_MICROPHONE + " instead"); - } - if (InputAttributes.inPrivateImeOptions(getPackageName(), FORCE_ASCII, editorInfo)) { - Log.w(TAG, "Deprecated private IME option specified: " + editorInfo.privateImeOptions); - Log.w(TAG, "Use EditorInfo.IME_FLAG_FORCE_ASCII flag instead"); - } - - // In landscape mode, this method gets called without the input view being created. - if (mainKeyboardView == null) { - return; - } - - // Update to a gesture consumer with the current editor and IME state. - mGestureConsumer = GestureConsumer.newInstance(editorInfo, - mInputLogic.getPrivateCommandPerformer(), - mRichImm.getCurrentSubtypeLocale(), - switcher.getKeyboard()); - - // Forward this event to the accessibility utilities, if enabled. - final AccessibilityUtils accessUtils = AccessibilityUtils.getInstance(); - if (accessUtils.isTouchExplorationEnabled()) { - accessUtils.onStartInputViewInternal(mainKeyboardView, editorInfo, restarting); - } - - final boolean inputTypeChanged = !currentSettingsValues.isSameInputType(editorInfo); - final boolean isDifferentTextField = !restarting || inputTypeChanged; - - StatsUtils.onStartInputView(editorInfo.inputType, - Settings.getInstance().getCurrent().mDisplayOrientation, - !isDifferentTextField); - - // The EditorInfo might have a flag that affects fullscreen mode. - // Note: This call should be done by InputMethodService? - updateFullscreenMode(); - - // ALERT: settings have not been reloaded and there is a chance they may be stale. - // In the practice, if it is, we should have gotten onConfigurationChanged so it should - // be fine, but this is horribly confusing and must be fixed AS SOON AS POSSIBLE. - - // In some cases the input connection has not been reset yet and we can't access it. In - // this case we will need to call loadKeyboard() later, when it's accessible, so that we - // can go into the correct mode, so we need to do some housekeeping here. - final boolean needToCallLoadKeyboardLater; - final Suggest suggest = mInputLogic.mSuggest; - if (!isImeSuppressedByHardwareKeyboard()) { - // The app calling setText() has the effect of clearing the composing - // span, so we should reset our state unconditionally, even if restarting is true. - // We also tell the input logic about the combining rules for the current subtype, so - // it can adjust its combiners if needed. - mInputLogic.startInput(mRichImm.getCombiningRulesExtraValueOfCurrentSubtype(), - currentSettingsValues); - - resetDictionaryFacilitatorIfNecessary(); - - // TODO[IL]: Can the following be moved to InputLogic#startInput? - if (!mInputLogic.mConnection.resetCachesUponCursorMoveAndReturnSuccess( - editorInfo.initialSelStart, editorInfo.initialSelEnd, - false /* shouldFinishComposition */)) { - // Sometimes, while rotating, for some reason the framework tells the app we are not - // connected to it and that means we can't refresh the cache. In this case, schedule - // a refresh later. - // We try resetting the caches up to 5 times before giving up. - mHandler.postResetCaches(isDifferentTextField, 5 /* remainingTries */); - // mLastSelection{Start,End} are reset later in this method, no need to do it here - needToCallLoadKeyboardLater = true; - } else { - // When rotating, and when input is starting again in a field from where the focus - // didn't move (the keyboard having been closed with the back key), - // initialSelStart and initialSelEnd sometimes are lying. Make a best effort to - // work around this bug. - mInputLogic.mConnection.tryFixLyingCursorPosition(); - mHandler.postResumeSuggestionsForStartInput(true /* shouldDelay */); - needToCallLoadKeyboardLater = false; - } - } else { - // If we have a hardware keyboard we don't need to call loadKeyboard later anyway. - needToCallLoadKeyboardLater = false; - } - - if (isDifferentTextField || - !currentSettingsValues.hasSameOrientation(getResources().getConfiguration())) { - loadSettings(); - } - if (isDifferentTextField) { - mainKeyboardView.closing(); - currentSettingsValues = mSettings.getCurrent(); - - if (currentSettingsValues.mAutoCorrectionEnabledPerUserSettings) { - suggest.setAutoCorrectionThreshold( - currentSettingsValues.mAutoCorrectionThreshold); - } - suggest.setPlausibilityThreshold(currentSettingsValues.mPlausibilityThreshold); - - switcher.loadKeyboard(editorInfo, currentSettingsValues, getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - if (needToCallLoadKeyboardLater) { - // If we need to call loadKeyboard again later, we need to save its state now. The - // later call will be done in #retryResetCaches. - switcher.saveKeyboardState(); - } - } else if (restarting) { - // TODO: Come up with a more comprehensive way to reset the keyboard layout when - // a keyboard layout set doesn't get reloaded in this method. - switcher.resetKeyboardStateToAlphabet(getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - // In apps like Talk, we come here when the text is sent and the field gets emptied and - // we need to re-evaluate the shift state, but not the whole layout which would be - // disruptive. - // Space state must be updated before calling updateShiftState - switcher.requestUpdatingShiftState(getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - } - // This will set the punctuation suggestions if next word suggestion is off; - // otherwise it will clear the suggestion strip. - setNeutralSuggestionStrip(); - - mHandler.cancelUpdateSuggestionStrip(); - - mainKeyboardView.setMainDictionaryAvailability( - mDictionaryFacilitator.hasAtLeastOneInitializedMainDictionary()); - mainKeyboardView.setKeyPreviewPopupEnabled(currentSettingsValues.mKeyPreviewPopupOn, - currentSettingsValues.mKeyPreviewPopupDismissDelay); - mainKeyboardView.setSlidingKeyInputPreviewEnabled( - currentSettingsValues.mSlidingKeyInputPreviewEnabled); - mainKeyboardView.setGestureHandlingEnabledByUser( - currentSettingsValues.mGestureInputEnabled, - currentSettingsValues.mGestureTrailEnabled, - currentSettingsValues.mGestureFloatingPreviewTextEnabled); - - if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); - } - - @Override - public void onWindowShown() { - super.onWindowShown(); - setNavigationBarVisibility(isInputViewShown()); - } - - @Override - public void onWindowHidden() { - super.onWindowHidden(); - final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); - if (mainKeyboardView != null) { - mainKeyboardView.closing(); - } - setNavigationBarVisibility(false); - } - - void onFinishInputInternal() { - super.onFinishInput(); - - mDictionaryFacilitator.onFinishInput(this); - final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); - if (mainKeyboardView != null) { - mainKeyboardView.closing(); - } - } - - void onFinishInputViewInternal(final boolean finishingInput) { - super.onFinishInputView(finishingInput); - cleanupInternalStateForFinishInput(); - } - - private void cleanupInternalStateForFinishInput() { - // Remove pending messages related to update suggestions - mHandler.cancelUpdateSuggestionStrip(); - // Should do the following in onFinishInputInternal but until JB MR2 it's not called :( - mInputLogic.finishInput(); - } - - protected void deallocateMemory() { - mKeyboardSwitcher.deallocateMemory(); - } - - @Override - public void onUpdateSelection(final int oldSelStart, final int oldSelEnd, - final int newSelStart, final int newSelEnd, - final int composingSpanStart, final int composingSpanEnd) { - super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, - composingSpanStart, composingSpanEnd); - if (DebugFlags.DEBUG_ENABLED) { - Log.i(TAG, "onUpdateSelection: oss=" + oldSelStart + ", ose=" + oldSelEnd - + ", nss=" + newSelStart + ", nse=" + newSelEnd - + ", cs=" + composingSpanStart + ", ce=" + composingSpanEnd); - } - - // This call happens whether our view is displayed or not, but if it's not then we should - // not attempt recorrection. This is true even with a hardware keyboard connected: if the - // view is not displayed we have no means of showing suggestions anyway, and if it is then - // we want to show suggestions anyway. - final SettingsValues settingsValues = mSettings.getCurrent(); - if (isInputViewShown() - && mInputLogic.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, - settingsValues)) { - mKeyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - } - } - - /** - * This is called when the user has clicked on the extracted text view, - * when running in fullscreen mode. The default implementation hides - * the suggestions view when this happens, but only if the extracted text - * editor has a vertical scroll bar because its text doesn't fit. - * Here we override the behavior due to the possibility that a re-correction could - * cause the suggestions strip to disappear and re-appear. - */ - @Override - public void onExtractedTextClicked() { - if (mSettings.getCurrent().needsToLookupSuggestions()) { - return; - } - - super.onExtractedTextClicked(); - } - - /** - * This is called when the user has performed a cursor movement in the - * extracted text view, when it is running in fullscreen mode. The default - * implementation hides the suggestions view when a vertical movement - * happens, but only if the extracted text editor has a vertical scroll bar - * because its text doesn't fit. - * Here we override the behavior due to the possibility that a re-correction could - * cause the suggestions strip to disappear and re-appear. - */ - @Override - public void onExtractedCursorMovement(final int dx, final int dy) { - if (mSettings.getCurrent().needsToLookupSuggestions()) { - return; - } - - super.onExtractedCursorMovement(dx, dy); - } - - @Override - public void hideWindow() { - mKeyboardSwitcher.onHideWindow(); - - if (TRACE) Debug.stopMethodTracing(); - if (isShowingOptionDialog()) { - mOptionsDialog.dismiss(); - mOptionsDialog = null; - } - super.hideWindow(); - } - - @Override - public void onDisplayCompletions(final CompletionInfo[] applicationSpecifiedCompletions) { - if (DebugFlags.DEBUG_ENABLED) { - Log.i(TAG, "Received completions:"); - if (applicationSpecifiedCompletions != null) { - for (int i = 0; i < applicationSpecifiedCompletions.length; i++) { - Log.i(TAG, " #" + i + ": " + applicationSpecifiedCompletions[i]); - } - } - } - if (!mSettings.getCurrent().isApplicationSpecifiedCompletionsOn()) { - return; - } - // If we have an update request in flight, we need to cancel it so it does not override - // these completions. - mHandler.cancelUpdateSuggestionStrip(); - if (applicationSpecifiedCompletions == null) { - setNeutralSuggestionStrip(); - return; - } - - final ArrayList<SuggestedWords.SuggestedWordInfo> applicationSuggestedWords = - SuggestedWords.getFromApplicationSpecifiedCompletions( - applicationSpecifiedCompletions); - final SuggestedWords suggestedWords = new SuggestedWords(applicationSuggestedWords, - null /* rawSuggestions */, - null /* typedWord */, - false /* typedWordValid */, - false /* willAutoCorrect */, - false /* isObsoleteSuggestions */, - SuggestedWords.INPUT_STYLE_APPLICATION_SPECIFIED /* inputStyle */, - SuggestedWords.NOT_A_SEQUENCE_NUMBER); - // When in fullscreen mode, show completions generated by the application forcibly - setSuggestedWords(suggestedWords); - } - - @Override - public void onComputeInsets(final InputMethodService.Insets outInsets) { - super.onComputeInsets(outInsets); - // This method may be called before {@link #setInputView(View)}. - if (mInputView == null) { - return; - } - final SettingsValues settingsValues = mSettings.getCurrent(); - final View visibleKeyboardView = mKeyboardSwitcher.getVisibleKeyboardView(); - if (visibleKeyboardView == null || !hasSuggestionStripView()) { - return; - } - final int inputHeight = mInputView.getHeight(); - if (isImeSuppressedByHardwareKeyboard() && !visibleKeyboardView.isShown()) { - // If there is a hardware keyboard and a visible software keyboard view has been hidden, - // no visual element will be shown on the screen. - outInsets.contentTopInsets = inputHeight; - outInsets.visibleTopInsets = inputHeight; - mInsetsUpdater.setInsets(outInsets); - return; - } - final int suggestionsHeight = (!mKeyboardSwitcher.isShowingEmojiPalettes() - && mSuggestionStripView.getVisibility() == View.VISIBLE) - ? mSuggestionStripView.getHeight() : 0; - final int visibleTopY = inputHeight - visibleKeyboardView.getHeight() - suggestionsHeight; - mSuggestionStripView.setMoreSuggestionsHeight(visibleTopY); - // Need to set expanded touchable region only if a keyboard view is being shown. - if (visibleKeyboardView.isShown()) { - final int touchLeft = 0; - final int touchTop = mKeyboardSwitcher.isShowingMoreKeysPanel() ? 0 : visibleTopY; - final int touchRight = visibleKeyboardView.getWidth(); - final int touchBottom = inputHeight; - outInsets.touchableInsets = InputMethodService.Insets.TOUCHABLE_INSETS_REGION; - outInsets.touchableRegion.set(touchLeft, touchTop, touchRight, touchBottom); - } - outInsets.contentTopInsets = visibleTopY; - outInsets.visibleTopInsets = visibleTopY; - mInsetsUpdater.setInsets(outInsets); - } - - public void startShowingInputView(final boolean needsToLoadKeyboard) { - mIsExecutingStartShowingInputView = true; - // This {@link #showWindow(boolean)} will eventually call back - // {@link #onEvaluateInputViewShown()}. - showWindow(true /* showInput */); - mIsExecutingStartShowingInputView = false; - if (needsToLoadKeyboard) { - loadKeyboard(); - } - } - - public void stopShowingInputView() { - showWindow(false /* showInput */); - } - - @Override - public boolean onShowInputRequested(final int flags, final boolean configChange) { - if (isImeSuppressedByHardwareKeyboard()) { - return true; - } - return super.onShowInputRequested(flags, configChange); - } - - @Override - public boolean onEvaluateInputViewShown() { - if (mIsExecutingStartShowingInputView) { - return true; - } - return super.onEvaluateInputViewShown(); - } - - @Override - public boolean onEvaluateFullscreenMode() { - final SettingsValues settingsValues = mSettings.getCurrent(); - if (isImeSuppressedByHardwareKeyboard()) { - // If there is a hardware keyboard, disable full screen mode. - return false; - } - // Reread resource value here, because this method is called by the framework as needed. - final boolean isFullscreenModeAllowed = Settings.readUseFullscreenMode(getResources()); - if (super.onEvaluateFullscreenMode() && isFullscreenModeAllowed) { - // TODO: Remove this hack. Actually we should not really assume NO_EXTRACT_UI - // implies NO_FULLSCREEN. However, the framework mistakenly does. i.e. NO_EXTRACT_UI - // without NO_FULLSCREEN doesn't work as expected. Because of this we need this - // hack for now. Let's get rid of this once the framework gets fixed. - final EditorInfo ei = getCurrentInputEditorInfo(); - return !(ei != null && ((ei.imeOptions & EditorInfo.IME_FLAG_NO_EXTRACT_UI) != 0)); - } - return false; - } - - @Override - public void updateFullscreenMode() { - super.updateFullscreenMode(); - updateSoftInputWindowLayoutParameters(); - } - - private void updateSoftInputWindowLayoutParameters() { - // Override layout parameters to expand {@link SoftInputWindow} to the entire screen. - // See {@link InputMethodService#setinputView(View)} and - // {@link SoftInputWindow#updateWidthHeight(WindowManager.LayoutParams)}. - final Window window = getWindow().getWindow(); - ViewLayoutUtils.updateLayoutHeightOf(window, LayoutParams.MATCH_PARENT); - // This method may be called before {@link #setInputView(View)}. - if (mInputView != null) { - // In non-fullscreen mode, {@link InputView} and its parent inputArea should expand to - // the entire screen and be placed at the bottom of {@link SoftInputWindow}. - // In fullscreen mode, these shouldn't expand to the entire screen and should be - // coexistent with {@link #mExtractedArea} above. - // See {@link InputMethodService#setInputView(View) and - // com.android.internal.R.layout.input_method.xml. - final int layoutHeight = isFullscreenMode() - ? LayoutParams.WRAP_CONTENT : LayoutParams.MATCH_PARENT; - final View inputArea = window.findViewById(android.R.id.inputArea); - ViewLayoutUtils.updateLayoutHeightOf(inputArea, layoutHeight); - ViewLayoutUtils.updateLayoutGravityOf(inputArea, Gravity.BOTTOM); - ViewLayoutUtils.updateLayoutHeightOf(mInputView, layoutHeight); - } - } - - int getCurrentAutoCapsState() { - return mInputLogic.getCurrentAutoCapsState(mSettings.getCurrent()); - } - - int getCurrentRecapitalizeState() { - return mInputLogic.getCurrentRecapitalizeState(); - } - - /** - * @param codePoints code points to get coordinates for. - * @return x,y coordinates for this keyboard, as a flattened array. - */ - public int[] getCoordinatesForCurrentKeyboard(final int[] codePoints) { - final Keyboard keyboard = mKeyboardSwitcher.getKeyboard(); - if (null == keyboard) { - return CoordinateUtils.newCoordinateArray(codePoints.length, - Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); - } - return keyboard.getCoordinates(codePoints); - } - - // Callback for the {@link SuggestionStripView}, to call when the important notice strip is - // pressed. - @Override - public void showImportantNoticeContents() { - PermissionsManager.get(this).requestPermissions( - this /* PermissionsResultCallback */, - null /* activity */, permission.READ_CONTACTS); - } - - @Override - public void onRequestPermissionsResult(boolean allGranted) { - ImportantNoticeUtils.updateContactsNoticeShown(this /* context */); - setNeutralSuggestionStrip(); - } - - public void displaySettingsDialog() { - if (isShowingOptionDialog()) { - return; - } - showSubtypeSelectorAndSettings(); - } - - @Override - public boolean onCustomRequest(final int requestCode) { - if (isShowingOptionDialog()) return false; - switch (requestCode) { - case Constants.CUSTOM_CODE_SHOW_INPUT_METHOD_PICKER: - if (mRichImm.hasMultipleEnabledIMEsOrSubtypes(true /* include aux subtypes */)) { - mRichImm.getInputMethodManager().showInputMethodPicker(); - return true; - } - return false; - } - return false; - } - - private boolean isShowingOptionDialog() { - return mOptionsDialog != null && mOptionsDialog.isShowing(); - } - - public void switchLanguage(final InputMethodSubtype subtype) { - final IBinder token = getWindow().getWindow().getAttributes().token; - mRichImm.setInputMethodAndSubtype(token, subtype); - } - - // TODO: Revise the language switch key behavior to make it much smarter and more reasonable. - public void switchToNextSubtype() { - final IBinder token = getWindow().getWindow().getAttributes().token; - if (shouldSwitchToOtherInputMethods()) { - mRichImm.switchToNextInputMethod(token, false /* onlyCurrentIme */); - return; - } - mSubtypeState.switchSubtype(token, mRichImm); - } - - // TODO: Instead of checking for alphabetic keyboard here, separate keycodes for - // alphabetic shift and shift while in symbol layout and get rid of this method. - private int getCodePointForKeyboard(final int codePoint) { - if (Constants.CODE_SHIFT == codePoint) { - final Keyboard currentKeyboard = mKeyboardSwitcher.getKeyboard(); - if (null != currentKeyboard && currentKeyboard.mId.isAlphabetKeyboard()) { - return codePoint; - } - return Constants.CODE_SYMBOL_SHIFT; - } - return codePoint; - } - - // Implementation of {@link KeyboardActionListener}. - @Override - public void onCodeInput(final int codePoint, final int x, final int y, - final boolean isKeyRepeat) { - // TODO: this processing does not belong inside LatinIME, the caller should be doing this. - final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); - // x and y include some padding, but everything down the line (especially native - // code) needs the coordinates in the keyboard frame. - // TODO: We should reconsider which coordinate system should be used to represent - // keyboard event. Also we should pull this up -- LatinIME has no business doing - // this transformation, it should be done already before calling onEvent. - final int keyX = mainKeyboardView.getKeyX(x); - final int keyY = mainKeyboardView.getKeyY(y); - final Event event = createSoftwareKeypressEvent(getCodePointForKeyboard(codePoint), - keyX, keyY, isKeyRepeat); - onEvent(event); - } - - // This method is public for testability of LatinIME, but also in the future it should - // completely replace #onCodeInput. - public void onEvent(@Nonnull final Event event) { - if (Constants.CODE_SHORTCUT == event.mKeyCode) { - mRichImm.switchToShortcutIme(this); - } - final InputTransaction completeInputTransaction = - mInputLogic.onCodeInput(mSettings.getCurrent(), event, - mKeyboardSwitcher.getKeyboardShiftMode(), - mKeyboardSwitcher.getCurrentKeyboardScriptId(), mHandler); - updateStateAfterInputTransaction(completeInputTransaction); - mKeyboardSwitcher.onEvent(event, getCurrentAutoCapsState(), getCurrentRecapitalizeState()); - } - - // A helper method to split the code point and the key code. Ultimately, they should not be - // squashed into the same variable, and this method should be removed. - // public for testing, as we don't want to copy the same logic into test code - @Nonnull - public static Event createSoftwareKeypressEvent(final int keyCodeOrCodePoint, final int keyX, - final int keyY, final boolean isKeyRepeat) { - final int keyCode; - final int codePoint; - if (keyCodeOrCodePoint <= 0) { - keyCode = keyCodeOrCodePoint; - codePoint = Event.NOT_A_CODE_POINT; - } else { - keyCode = Event.NOT_A_KEY_CODE; - codePoint = keyCodeOrCodePoint; - } - return Event.createSoftwareKeypressEvent(codePoint, keyCode, keyX, keyY, isKeyRepeat); - } - - // Called from PointerTracker through the KeyboardActionListener interface - @Override - public void onTextInput(final String rawText) { - // TODO: have the keyboard pass the correct key code when we need it. - final Event event = Event.createSoftwareTextEvent(rawText, Constants.CODE_OUTPUT_TEXT); - final InputTransaction completeInputTransaction = - mInputLogic.onTextInput(mSettings.getCurrent(), event, - mKeyboardSwitcher.getKeyboardShiftMode(), mHandler); - updateStateAfterInputTransaction(completeInputTransaction); - mKeyboardSwitcher.onEvent(event, getCurrentAutoCapsState(), getCurrentRecapitalizeState()); - } - - @Override - public void onStartBatchInput() { - mInputLogic.onStartBatchInput(mSettings.getCurrent(), mKeyboardSwitcher, mHandler); - mGestureConsumer.onGestureStarted( - mRichImm.getCurrentSubtypeLocale(), - mKeyboardSwitcher.getKeyboard()); - } - - @Override - public void onUpdateBatchInput(final InputPointers batchPointers) { - mInputLogic.onUpdateBatchInput(batchPointers); - } - - @Override - public void onEndBatchInput(final InputPointers batchPointers) { - mInputLogic.onEndBatchInput(batchPointers); - mGestureConsumer.onGestureCompleted(batchPointers); - } - - @Override - public void onCancelBatchInput() { - mInputLogic.onCancelBatchInput(mHandler); - mGestureConsumer.onGestureCanceled(); - } - - /** - * To be called after the InputLogic has gotten a chance to act on the suggested words by the - * IME for the full gesture, possibly updating the TextView to reflect the first suggestion. - * <p> - * This method must be run on the UI Thread. - * @param suggestedWords suggested words by the IME for the full gesture. - */ - public void onTailBatchInputResultShown(final SuggestedWords suggestedWords) { - mGestureConsumer.onImeSuggestionsProcessed(suggestedWords, - mInputLogic.getComposingStart(), mInputLogic.getComposingLength(), - mDictionaryFacilitator); - } - - // This method must run on the UI Thread. - void showGesturePreviewAndSuggestionStrip(@Nonnull final SuggestedWords suggestedWords, - final boolean dismissGestureFloatingPreviewText) { - showSuggestionStrip(suggestedWords); - final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); - mainKeyboardView.showGestureFloatingPreviewText(suggestedWords, - dismissGestureFloatingPreviewText /* dismissDelayed */); - } - - // Called from PointerTracker through the KeyboardActionListener interface - @Override - public void onFinishSlidingInput() { - // User finished sliding input. - mKeyboardSwitcher.onFinishSlidingInput(getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - } - - // Called from PointerTracker through the KeyboardActionListener interface - @Override - public void onCancelInput() { - // User released a finger outside any key - // Nothing to do so far. - } - - public boolean hasSuggestionStripView() { - return null != mSuggestionStripView; - } - - private void setSuggestedWords(final SuggestedWords suggestedWords) { - final SettingsValues currentSettingsValues = mSettings.getCurrent(); - mInputLogic.setSuggestedWords(suggestedWords); - // TODO: Modify this when we support suggestions with hard keyboard - if (!hasSuggestionStripView()) { - return; - } - if (!onEvaluateInputViewShown()) { - return; - } - - final boolean shouldShowImportantNotice = - ImportantNoticeUtils.shouldShowImportantNotice(this, currentSettingsValues); - final boolean shouldShowSuggestionCandidates = - currentSettingsValues.mInputAttributes.mShouldShowSuggestions - && currentSettingsValues.isSuggestionsEnabledPerUserSettings(); - final boolean shouldShowSuggestionsStripUnlessPassword = shouldShowImportantNotice - || currentSettingsValues.mShowsVoiceInputKey - || shouldShowSuggestionCandidates - || currentSettingsValues.isApplicationSpecifiedCompletionsOn(); - final boolean shouldShowSuggestionsStrip = shouldShowSuggestionsStripUnlessPassword - && !currentSettingsValues.mInputAttributes.mIsPasswordField; - mSuggestionStripView.updateVisibility(shouldShowSuggestionsStrip, isFullscreenMode()); - if (!shouldShowSuggestionsStrip) { - return; - } - - final boolean isEmptyApplicationSpecifiedCompletions = - currentSettingsValues.isApplicationSpecifiedCompletionsOn() - && suggestedWords.isEmpty(); - final boolean noSuggestionsFromDictionaries = suggestedWords.isEmpty() - || suggestedWords.isPunctuationSuggestions() - || isEmptyApplicationSpecifiedCompletions; - final boolean isBeginningOfSentencePrediction = (suggestedWords.mInputStyle - == SuggestedWords.INPUT_STYLE_BEGINNING_OF_SENTENCE_PREDICTION); - final boolean noSuggestionsToOverrideImportantNotice = noSuggestionsFromDictionaries - || isBeginningOfSentencePrediction; - if (shouldShowImportantNotice && noSuggestionsToOverrideImportantNotice) { - if (mSuggestionStripView.maybeShowImportantNoticeTitle()) { - return; - } - } - - if (currentSettingsValues.isSuggestionsEnabledPerUserSettings() - || currentSettingsValues.isApplicationSpecifiedCompletionsOn() - // We should clear the contextual strip if there is no suggestion from dictionaries. - || noSuggestionsFromDictionaries) { - mSuggestionStripView.setSuggestions(suggestedWords, - mRichImm.getCurrentSubtype().isRtlSubtype()); - } - } - - // TODO[IL]: Move this out of LatinIME. - public void getSuggestedWords(final int inputStyle, final int sequenceNumber, - final OnGetSuggestedWordsCallback callback) { - final Keyboard keyboard = mKeyboardSwitcher.getKeyboard(); - if (keyboard == null) { - callback.onGetSuggestedWords(SuggestedWords.getEmptyInstance()); - return; - } - mInputLogic.getSuggestedWords(mSettings.getCurrent(), keyboard, - mKeyboardSwitcher.getKeyboardShiftMode(), inputStyle, sequenceNumber, callback); - } - - @Override - public void showSuggestionStrip(final SuggestedWords suggestedWords) { - if (suggestedWords.isEmpty()) { - setNeutralSuggestionStrip(); - } else { - setSuggestedWords(suggestedWords); - } - // Cache the auto-correction in accessibility code so we can speak it if the user - // touches a key that will insert it. - AccessibilityUtils.getInstance().setAutoCorrection(suggestedWords); - } - - // Called from {@link SuggestionStripView} through the {@link SuggestionStripView#Listener} - // interface - @Override - public void pickSuggestionManually(final SuggestedWordInfo suggestionInfo) { - final InputTransaction completeInputTransaction = mInputLogic.onPickSuggestionManually( - mSettings.getCurrent(), suggestionInfo, - mKeyboardSwitcher.getKeyboardShiftMode(), - mKeyboardSwitcher.getCurrentKeyboardScriptId(), - mHandler); - updateStateAfterInputTransaction(completeInputTransaction); - } - - // This will show either an empty suggestion strip (if prediction is enabled) or - // punctuation suggestions (if it's disabled). - @Override - public void setNeutralSuggestionStrip() { - final SettingsValues currentSettings = mSettings.getCurrent(); - final SuggestedWords neutralSuggestions = currentSettings.mBigramPredictionEnabled - ? SuggestedWords.getEmptyInstance() - : currentSettings.mSpacingAndPunctuations.mSuggestPuncList; - setSuggestedWords(neutralSuggestions); - } - - // Outside LatinIME, only used by the {@link InputTestsBase} test suite. - @UsedForTesting - void loadKeyboard() { - // Since we are switching languages, the most urgent thing is to let the keyboard graphics - // update. LoadKeyboard does that, but we need to wait for buffer flip for it to be on - // the screen. Anything we do right now will delay this, so wait until the next frame - // before we do the rest, like reopening dictionaries and updating suggestions. So we - // post a message. - mHandler.postReopenDictionaries(); - loadSettings(); - if (mKeyboardSwitcher.getMainKeyboardView() != null) { - // Reload keyboard because the current language has been changed. - mKeyboardSwitcher.loadKeyboard(getCurrentInputEditorInfo(), mSettings.getCurrent(), - getCurrentAutoCapsState(), getCurrentRecapitalizeState()); - } - } - - /** - * After an input transaction has been executed, some state must be updated. This includes - * the shift state of the keyboard and suggestions. This method looks at the finished - * inputTransaction to find out what is necessary and updates the state accordingly. - * @param inputTransaction The transaction that has been executed. - */ - private void updateStateAfterInputTransaction(final InputTransaction inputTransaction) { - switch (inputTransaction.getRequiredShiftUpdate()) { - case InputTransaction.SHIFT_UPDATE_LATER: - mHandler.postUpdateShiftState(); - break; - case InputTransaction.SHIFT_UPDATE_NOW: - mKeyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - break; - default: // SHIFT_NO_UPDATE - } - if (inputTransaction.requiresUpdateSuggestions()) { - final int inputStyle; - if (inputTransaction.mEvent.isSuggestionStripPress()) { - // Suggestion strip press: no input. - inputStyle = SuggestedWords.INPUT_STYLE_NONE; - } else if (inputTransaction.mEvent.isGesture()) { - inputStyle = SuggestedWords.INPUT_STYLE_TAIL_BATCH; - } else { - inputStyle = SuggestedWords.INPUT_STYLE_TYPING; - } - mHandler.postUpdateSuggestionStrip(inputStyle); - } - if (inputTransaction.didAffectContents()) { - mSubtypeState.setCurrentSubtypeHasBeenUsed(); - } - } - - private void hapticAndAudioFeedback(final int code, final int repeatCount) { - final MainKeyboardView keyboardView = mKeyboardSwitcher.getMainKeyboardView(); - if (keyboardView != null && keyboardView.isInDraggingFinger()) { - // No need to feedback while finger is dragging. - return; - } - if (repeatCount > 0) { - if (code == Constants.CODE_DELETE && !mInputLogic.mConnection.canDeleteCharacters()) { - // No need to feedback when repeat delete key will have no effect. - return; - } - // TODO: Use event time that the last feedback has been generated instead of relying on - // a repeat count to thin out feedback. - if (repeatCount % PERIOD_FOR_AUDIO_AND_HAPTIC_FEEDBACK_IN_KEY_REPEAT == 0) { - return; - } - } - final AudioAndHapticFeedbackManager feedbackManager = - AudioAndHapticFeedbackManager.getInstance(); - if (repeatCount == 0) { - // TODO: Reconsider how to perform haptic feedback when repeating key. - feedbackManager.performHapticFeedback(keyboardView); - } - feedbackManager.performAudioFeedback(code); - } - - // Callback of the {@link KeyboardActionListener}. This is called when a key is depressed; - // release matching call is {@link #onReleaseKey(int,boolean)} below. - @Override - public void onPressKey(final int primaryCode, final int repeatCount, - final boolean isSinglePointer) { - mKeyboardSwitcher.onPressKey(primaryCode, isSinglePointer, getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - hapticAndAudioFeedback(primaryCode, repeatCount); - } - - // Callback of the {@link KeyboardActionListener}. This is called when a key is released; - // press matching call is {@link #onPressKey(int,int,boolean)} above. - @Override - public void onReleaseKey(final int primaryCode, final boolean withSliding) { - mKeyboardSwitcher.onReleaseKey(primaryCode, withSliding, getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - } - - private HardwareEventDecoder getHardwareKeyEventDecoder(final int deviceId) { - final HardwareEventDecoder decoder = mHardwareEventDecoders.get(deviceId); - if (null != decoder) return decoder; - // TODO: create the decoder according to the specification - final HardwareEventDecoder newDecoder = new HardwareKeyboardEventDecoder(deviceId); - mHardwareEventDecoders.put(deviceId, newDecoder); - return newDecoder; - } - - // Hooks for hardware keyboard - @Override - public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) { - if (mEmojiAltPhysicalKeyDetector == null) { - mEmojiAltPhysicalKeyDetector = new EmojiAltPhysicalKeyDetector( - getApplicationContext().getResources()); - } - mEmojiAltPhysicalKeyDetector.onKeyDown(keyEvent); - if (!ProductionFlags.IS_HARDWARE_KEYBOARD_SUPPORTED) { - return super.onKeyDown(keyCode, keyEvent); - } - final Event event = getHardwareKeyEventDecoder( - keyEvent.getDeviceId()).decodeHardwareKey(keyEvent); - // If the event is not handled by LatinIME, we just pass it to the parent implementation. - // If it's handled, we return true because we did handle it. - if (event.isHandled()) { - mInputLogic.onCodeInput(mSettings.getCurrent(), event, - mKeyboardSwitcher.getKeyboardShiftMode(), - // TODO: this is not necessarily correct for a hardware keyboard right now - mKeyboardSwitcher.getCurrentKeyboardScriptId(), - mHandler); - return true; - } - return super.onKeyDown(keyCode, keyEvent); - } - - @Override - public boolean onKeyUp(final int keyCode, final KeyEvent keyEvent) { - if (mEmojiAltPhysicalKeyDetector == null) { - mEmojiAltPhysicalKeyDetector = new EmojiAltPhysicalKeyDetector( - getApplicationContext().getResources()); - } - mEmojiAltPhysicalKeyDetector.onKeyUp(keyEvent); - if (!ProductionFlags.IS_HARDWARE_KEYBOARD_SUPPORTED) { - return super.onKeyUp(keyCode, keyEvent); - } - final long keyIdentifier = keyEvent.getDeviceId() << 32 + keyEvent.getKeyCode(); - if (mInputLogic.mCurrentlyPressedHardwareKeys.remove(keyIdentifier)) { - return true; - } - return super.onKeyUp(keyCode, keyEvent); - } - - // onKeyDown and onKeyUp are the main events we are interested in. There are two more events - // related to handling of hardware key events that we may want to implement in the future: - // boolean onKeyLongPress(final int keyCode, final KeyEvent event); - // boolean onKeyMultiple(final int keyCode, final int count, final KeyEvent event); - - // receive ringer mode change. - private final BroadcastReceiver mRingerModeChangeReceiver = new BroadcastReceiver() { - @Override - public void onReceive(final Context context, final Intent intent) { - final String action = intent.getAction(); - if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { - AudioAndHapticFeedbackManager.getInstance().onRingerModeChanged(); - } - } - }; - - /** - * Starts {@link android.app.Activity} on the same display where the IME is shown. - * - * @param intent {@link Intent} to be used to start {@link android.app.Activity}. - */ - private void startActivityOnTheSameDisplay(Intent intent) { - // Note that WindowManager#getDefaultDisplay() returns the display ID associated with the - // Context from which the WindowManager instance was obtained. Therefore the following code - // returns the display ID for the window where the IME is shown. - final int currentDisplayId = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) - .getDefaultDisplay().getDisplayId(); - - startActivity(intent, - ActivityOptions.makeBasic().setLaunchDisplayId(currentDisplayId).toBundle()); - } - - void launchSettings(final String extraEntryValue) { - mInputLogic.commitTyped(mSettings.getCurrent(), LastComposedWord.NOT_A_SEPARATOR); - requestHideSelf(0); - final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); - if (mainKeyboardView != null) { - mainKeyboardView.closing(); - } - final Intent intent = new Intent(); - intent.setClass(LatinIME.this, SettingsActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK - | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - | Intent.FLAG_ACTIVITY_CLEAR_TOP); - intent.putExtra(SettingsActivity.EXTRA_SHOW_HOME_AS_UP, false); - intent.putExtra(SettingsActivity.EXTRA_ENTRY_KEY, extraEntryValue); - startActivityOnTheSameDisplay(intent); - } - - private void showSubtypeSelectorAndSettings() { - final CharSequence title = getString(R.string.english_ime_input_options); - // TODO: Should use new string "Select active input modes". - final CharSequence languageSelectionTitle = getString(R.string.language_selection_title); - final CharSequence[] items = new CharSequence[] { - languageSelectionTitle, - getString(ApplicationUtils.getActivityTitleResId(this, SettingsActivity.class)) - }; - final String imeId = mRichImm.getInputMethodIdOfThisIme(); - final OnClickListener listener = new OnClickListener() { - @Override - public void onClick(DialogInterface di, int position) { - di.dismiss(); - switch (position) { - case 0: - final Intent intent = IntentUtils.getInputLanguageSelectionIntent( - imeId, - Intent.FLAG_ACTIVITY_NEW_TASK - | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - | Intent.FLAG_ACTIVITY_CLEAR_TOP); - intent.putExtra(Intent.EXTRA_TITLE, languageSelectionTitle); - startActivityOnTheSameDisplay(intent); - break; - case 1: - launchSettings(SettingsActivity.EXTRA_ENTRY_VALUE_LONG_PRESS_COMMA); - break; - } - } - }; - final AlertDialog.Builder builder = new AlertDialog.Builder( - DialogUtils.getPlatformDialogThemeContext(this)); - builder.setItems(items, listener).setTitle(title); - final AlertDialog dialog = builder.create(); - dialog.setCancelable(true /* cancelable */); - dialog.setCanceledOnTouchOutside(true /* cancelable */); - showOptionDialog(dialog); - } - - // TODO: Move this method out of {@link LatinIME}. - private void showOptionDialog(final AlertDialog dialog) { - final IBinder windowToken = mKeyboardSwitcher.getMainKeyboardView().getWindowToken(); - if (windowToken == null) { - return; - } - - final Window window = dialog.getWindow(); - final WindowManager.LayoutParams lp = window.getAttributes(); - lp.token = windowToken; - lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; - window.setAttributes(lp); - window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); - - mOptionsDialog = dialog; - dialog.show(); - } - - @UsedForTesting - SuggestedWords getSuggestedWordsForTest() { - // You may not use this method for anything else than debug - return DebugFlags.DEBUG_ENABLED ? mInputLogic.mSuggestedWords : null; - } - - // DO NOT USE THIS for any other purpose than testing. This is information private to LatinIME. - @UsedForTesting - void waitForLoadingDictionaries(final long timeout, final TimeUnit unit) - throws InterruptedException { - mDictionaryFacilitator.waitForLoadingDictionariesForTesting(timeout, unit); - } - - // DO NOT USE THIS for any other purpose than testing. This can break the keyboard badly. - @UsedForTesting - void replaceDictionariesForTest(final Locale locale) { - final SettingsValues settingsValues = mSettings.getCurrent(); - mDictionaryFacilitator.resetDictionaries(this, locale, - settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts, - false /* forceReloadMainDictionary */, - settingsValues.mAccount, "", /* dictionaryNamePrefix */ - this /* DictionaryInitializationListener */); - } - - // DO NOT USE THIS for any other purpose than testing. - @UsedForTesting - void clearPersonalizedDictionariesForTest() { - mDictionaryFacilitator.clearUserHistoryDictionary(this); - } - - @UsedForTesting - List<InputMethodSubtype> getEnabledSubtypesForTest() { - return (mRichImm != null) ? mRichImm.getMyEnabledInputMethodSubtypeList( - true /* allowsImplicitlySelectedSubtypes */) : new ArrayList<InputMethodSubtype>(); - } - - public void dumpDictionaryForDebug(final String dictName) { - if (!mDictionaryFacilitator.isActive()) { - resetDictionaryFacilitatorIfNecessary(); - } - mDictionaryFacilitator.dumpDictionaryForDebug(dictName); - } - - public void debugDumpStateAndCrashWithException(final String context) { - final SettingsValues settingsValues = mSettings.getCurrent(); - final StringBuilder s = new StringBuilder(settingsValues.toString()); - s.append("\nAttributes : ").append(settingsValues.mInputAttributes) - .append("\nContext : ").append(context); - throw new RuntimeException(s.toString()); - } - - @Override - protected void dump(final FileDescriptor fd, final PrintWriter fout, final String[] args) { - super.dump(fd, fout, args); - - final Printer p = new PrintWriterPrinter(fout); - p.println("LatinIME state :"); - p.println(" VersionCode = " + ApplicationUtils.getVersionCode(this)); - p.println(" VersionName = " + ApplicationUtils.getVersionName(this)); - final Keyboard keyboard = mKeyboardSwitcher.getKeyboard(); - final int keyboardMode = keyboard != null ? keyboard.mId.mMode : -1; - p.println(" Keyboard mode = " + keyboardMode); - final SettingsValues settingsValues = mSettings.getCurrent(); - p.println(settingsValues.dump()); - p.println(mDictionaryFacilitator.dump(this /* context */)); - // TODO: Dump all settings values - } - - public boolean shouldSwitchToOtherInputMethods() { - // TODO: Revisit here to reorganize the settings. Probably we can/should use different - // strategy once the implementation of - // {@link InputMethodManager#shouldOfferSwitchingToNextInputMethod} is defined well. - final boolean fallbackValue = mSettings.getCurrent().mIncludesOtherImesInLanguageSwitchList; - final IBinder token = getWindow().getWindow().getAttributes().token; - if (token == null) { - return fallbackValue; - } - return mRichImm.shouldOfferSwitchingToNextInputMethod(token, fallbackValue); - } - - public boolean shouldShowLanguageSwitchKey() { - // TODO: Revisit here to reorganize the settings. Probably we can/should use different - // strategy once the implementation of - // {@link InputMethodManager#shouldOfferSwitchingToNextInputMethod} is defined well. - final boolean fallbackValue = mSettings.getCurrent().isLanguageSwitchKeyEnabled(); - final IBinder token = getWindow().getWindow().getAttributes().token; - if (token == null) { - return fallbackValue; - } - return mRichImm.shouldOfferSwitchingToNextInputMethod(token, fallbackValue); - } - - private void setNavigationBarVisibility(final boolean visible) { - if (BuildCompatUtils.EFFECTIVE_SDK_INT > Build.VERSION_CODES.M) { - // For N and later, IMEs can specify Color.TRANSPARENT to make the navigation bar - // transparent. For other colors the system uses the default color. - getWindow().getWindow().setNavigationBarColor( - visible ? Color.BLACK : Color.TRANSPARENT); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/NgramContext.java b/java/src/com/android/inputmethod/latin/NgramContext.java deleted file mode 100644 index 9682fb8a4..000000000 --- a/java/src/com/android/inputmethod/latin/NgramContext.java +++ /dev/null @@ -1,291 +0,0 @@ -/* - * Copyright (C) 2014 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.text.TextUtils; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; - -import java.util.ArrayList; -import java.util.Arrays; - -import javax.annotation.Nonnull; - -/** - * Class to represent information of previous words. This class is used to add n-gram entries - * into binary dictionaries, to get predictions, and to get suggestions. - */ -public class NgramContext { - @Nonnull - public static final NgramContext EMPTY_PREV_WORDS_INFO = - new NgramContext(WordInfo.EMPTY_WORD_INFO); - @Nonnull - public static final NgramContext BEGINNING_OF_SENTENCE = - new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO); - - public static final String BEGINNING_OF_SENTENCE_TAG = "<S>"; - - public static final String CONTEXT_SEPARATOR = " "; - - public static NgramContext getEmptyPrevWordsContext(int maxPrevWordCount) { - return new NgramContext(maxPrevWordCount, WordInfo.EMPTY_WORD_INFO); - } - - /** - * Word information used to represent previous words information. - */ - public static class WordInfo { - @Nonnull - public static final WordInfo EMPTY_WORD_INFO = new WordInfo(null); - @Nonnull - public static final WordInfo BEGINNING_OF_SENTENCE_WORD_INFO = new WordInfo(); - - // This is an empty char sequence when mIsBeginningOfSentence is true. - public final CharSequence mWord; - // TODO: Have sentence separator. - // Whether the current context is beginning of sentence or not. This is true when composing - // at the beginning of an input field or composing a word after a sentence separator. - public final boolean mIsBeginningOfSentence; - - // Beginning of sentence. - private WordInfo() { - mWord = ""; - mIsBeginningOfSentence = true; - } - - public WordInfo(final CharSequence word) { - mWord = word; - mIsBeginningOfSentence = false; - } - - public boolean isValid() { - return mWord != null; - } - - @Override - public int hashCode() { - return Arrays.hashCode(new Object[] { mWord, mIsBeginningOfSentence } ); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof WordInfo)) return false; - final WordInfo wordInfo = (WordInfo)o; - if (mWord == null || wordInfo.mWord == null) { - return mWord == wordInfo.mWord - && mIsBeginningOfSentence == wordInfo.mIsBeginningOfSentence; - } - return TextUtils.equals(mWord, wordInfo.mWord) - && mIsBeginningOfSentence == wordInfo.mIsBeginningOfSentence; - } - } - - // The words immediately before the considered word. EMPTY_WORD_INFO element means we don't - // have any context for that previous word including the "beginning of sentence context" - we - // just don't know what to predict using the information. An example of that is after a comma. - // For simplicity of implementation, elements may also be EMPTY_WORD_INFO transiently after the - // WordComposer was reset and before starting a new composing word, but we should never be - // calling getSuggetions* in this situation. - private final WordInfo[] mPrevWordsInfo; - private final int mPrevWordsCount; - - private final int mMaxPrevWordCount; - - // Construct from the previous word information. - public NgramContext(final WordInfo... prevWordsInfo) { - this(DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM, prevWordsInfo); - } - - public NgramContext(final int maxPrevWordCount, final WordInfo... prevWordsInfo) { - mPrevWordsInfo = prevWordsInfo; - mPrevWordsCount = prevWordsInfo.length; - mMaxPrevWordCount = maxPrevWordCount; - } - - /** - * Create next prevWordsInfo using current prevWordsInfo. - */ - @Nonnull - public NgramContext getNextNgramContext(final WordInfo wordInfo) { - final int nextPrevWordCount = Math.min(mMaxPrevWordCount, mPrevWordsCount + 1); - final WordInfo[] prevWordsInfo = new WordInfo[nextPrevWordCount]; - prevWordsInfo[0] = wordInfo; - System.arraycopy(mPrevWordsInfo, 0, prevWordsInfo, 1, nextPrevWordCount - 1); - return new NgramContext(mMaxPrevWordCount, prevWordsInfo); - } - - - /** - * Extracts the previous words context. - * - * @return a String with the previous words separated by white space. - */ - public String extractPrevWordsContext() { - final ArrayList<String> terms = new ArrayList<>(); - for (int i = mPrevWordsInfo.length - 1; i >= 0; --i) { - if (mPrevWordsInfo[i] != null && mPrevWordsInfo[i].isValid()) { - final NgramContext.WordInfo wordInfo = mPrevWordsInfo[i]; - if (wordInfo.mIsBeginningOfSentence) { - terms.add(BEGINNING_OF_SENTENCE_TAG); - } else { - final String term = wordInfo.mWord.toString(); - if (!term.isEmpty()) { - terms.add(term); - } - } - } - } - return TextUtils.join(CONTEXT_SEPARATOR, terms); - } - - /** - * Extracts the previous words context. - * - * @return a String array with the previous words. - */ - public String[] extractPrevWordsContextArray() { - final ArrayList<String> prevTermList = new ArrayList<>(); - for (int i = mPrevWordsInfo.length - 1; i >= 0; --i) { - if (mPrevWordsInfo[i] != null && mPrevWordsInfo[i].isValid()) { - final NgramContext.WordInfo wordInfo = mPrevWordsInfo[i]; - if (wordInfo.mIsBeginningOfSentence) { - prevTermList.add(BEGINNING_OF_SENTENCE_TAG); - } else { - final String term = wordInfo.mWord.toString(); - if (!term.isEmpty()) { - prevTermList.add(term); - } - } - } - } - final String[] contextStringArray = prevTermList.toArray(new String[prevTermList.size()]); - return contextStringArray; - } - - public boolean isValid() { - return mPrevWordsCount > 0 && mPrevWordsInfo[0].isValid(); - } - - public boolean isBeginningOfSentenceContext() { - return mPrevWordsCount > 0 && mPrevWordsInfo[0].mIsBeginningOfSentence; - } - - // n is 1-indexed. - // TODO: Remove - public CharSequence getNthPrevWord(final int n) { - if (n <= 0 || n > mPrevWordsCount) { - return null; - } - return mPrevWordsInfo[n - 1].mWord; - } - - // n is 1-indexed. - @UsedForTesting - public boolean isNthPrevWordBeginningOfSentence(final int n) { - if (n <= 0 || n > mPrevWordsCount) { - return false; - } - return mPrevWordsInfo[n - 1].mIsBeginningOfSentence; - } - - public void outputToArray(final int[][] codePointArrays, - final boolean[] isBeginningOfSentenceArray) { - for (int i = 0; i < mPrevWordsCount; i++) { - final WordInfo wordInfo = mPrevWordsInfo[i]; - if (wordInfo == null || !wordInfo.isValid()) { - codePointArrays[i] = new int[0]; - isBeginningOfSentenceArray[i] = false; - continue; - } - codePointArrays[i] = StringUtils.toCodePointArray(wordInfo.mWord); - isBeginningOfSentenceArray[i] = wordInfo.mIsBeginningOfSentence; - } - } - - public int getPrevWordCount() { - return mPrevWordsCount; - } - - @Override - public int hashCode() { - int hashValue = 0; - for (final WordInfo wordInfo : mPrevWordsInfo) { - if (wordInfo == null || !WordInfo.EMPTY_WORD_INFO.equals(wordInfo)) { - break; - } - hashValue ^= wordInfo.hashCode(); - } - return hashValue; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof NgramContext)) return false; - final NgramContext prevWordsInfo = (NgramContext)o; - - final int minLength = Math.min(mPrevWordsCount, prevWordsInfo.mPrevWordsCount); - for (int i = 0; i < minLength; i++) { - if (!mPrevWordsInfo[i].equals(prevWordsInfo.mPrevWordsInfo[i])) { - return false; - } - } - final WordInfo[] longerWordsInfo; - final int longerWordsInfoCount; - if (mPrevWordsCount > prevWordsInfo.mPrevWordsCount) { - longerWordsInfo = mPrevWordsInfo; - longerWordsInfoCount = mPrevWordsCount; - } else { - longerWordsInfo = prevWordsInfo.mPrevWordsInfo; - longerWordsInfoCount = prevWordsInfo.mPrevWordsCount; - } - for (int i = minLength; i < longerWordsInfoCount; i++) { - if (longerWordsInfo[i] != null - && !WordInfo.EMPTY_WORD_INFO.equals(longerWordsInfo[i])) { - return false; - } - } - return true; - } - - @Override - public String toString() { - final StringBuffer builder = new StringBuffer(); - for (int i = 0; i < mPrevWordsCount; i++) { - final WordInfo wordInfo = mPrevWordsInfo[i]; - builder.append("PrevWord["); - builder.append(i); - builder.append("]: "); - if (wordInfo == null) { - builder.append("null. "); - continue; - } - if (!wordInfo.isValid()) { - builder.append("Empty. "); - continue; - } - builder.append(wordInfo.mWord); - builder.append(", isBeginningOfSentence: "); - builder.append(wordInfo.mIsBeginningOfSentence); - builder.append(". "); - } - return builder.toString(); - } -} diff --git a/java/src/com/android/inputmethod/latin/PunctuationSuggestions.java b/java/src/com/android/inputmethod/latin/PunctuationSuggestions.java deleted file mode 100644 index e2c562174..000000000 --- a/java/src/com/android/inputmethod/latin/PunctuationSuggestions.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (C) 2014 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 com.android.inputmethod.keyboard.internal.KeySpecParser; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.StringUtils; - -import java.util.ArrayList; -import java.util.Arrays; - -import javax.annotation.Nullable; - -/** - * The extended {@link SuggestedWords} class to represent punctuation suggestions. - * - * Each punctuation specification string is the key specification that can be parsed by - * {@link KeySpecParser}. - */ -public final class PunctuationSuggestions extends SuggestedWords { - private PunctuationSuggestions(final ArrayList<SuggestedWordInfo> punctuationsList) { - super(punctuationsList, - null /* rawSuggestions */, - null /* typedWord */, - false /* typedWordValid */, - false /* hasAutoCorrectionCandidate */, - false /* isObsoleteSuggestions */, - INPUT_STYLE_NONE /* inputStyle */, - SuggestedWords.NOT_A_SEQUENCE_NUMBER); - } - - /** - * Create new instance of {@link PunctuationSuggestions} from the array of punctuation key - * specifications. - * - * @param punctuationSpecs The array of punctuation key specifications. - * @return The {@link PunctuationSuggestions} object. - */ - public static PunctuationSuggestions newPunctuationSuggestions( - @Nullable final String[] punctuationSpecs) { - if (punctuationSpecs == null || punctuationSpecs.length == 0) { - return new PunctuationSuggestions(new ArrayList<SuggestedWordInfo>(0)); - } - final ArrayList<SuggestedWordInfo> punctuationList = - new ArrayList<>(punctuationSpecs.length); - for (String spec : punctuationSpecs) { - punctuationList.add(newHardCodedWordInfo(spec)); - } - return new PunctuationSuggestions(punctuationList); - } - - /** - * {@inheritDoc} - * Note that {@link SuggestedWords#getWord(int)} returns a punctuation key specification text. - * The suggested punctuation should be gotten by parsing the key specification. - */ - @Override - public String getWord(final int index) { - final String keySpec = super.getWord(index); - final int code = KeySpecParser.getCode(keySpec); - return (code == Constants.CODE_OUTPUT_TEXT) - ? KeySpecParser.getOutputText(keySpec) - : StringUtils.newSingleCodePointString(code); - } - - /** - * {@inheritDoc} - * Note that {@link SuggestedWords#getWord(int)} returns a punctuation key specification text. - * The displayed text should be gotten by parsing the key specification. - */ - @Override - public String getLabel(final int index) { - final String keySpec = super.getWord(index); - return KeySpecParser.getLabel(keySpec); - } - - /** - * {@inheritDoc} - * Note that {@link #getWord(int)} returns a suggested punctuation. We should create a - * {@link SuggestedWords.SuggestedWordInfo} object that represents a hard coded word. - */ - @Override - public SuggestedWordInfo getInfo(final int index) { - return newHardCodedWordInfo(getWord(index)); - } - - /** - * The predicator to tell whether this object represents punctuation suggestions. - * @return true if this object represents punctuation suggestions. - */ - @Override - public boolean isPunctuationSuggestions() { - return true; - } - - @Override - public String toString() { - return "PunctuationSuggestions: " - + " words=" + Arrays.toString(mSuggestedWordInfoList.toArray()); - } - - private static SuggestedWordInfo newHardCodedWordInfo(final String keySpec) { - return new SuggestedWordInfo(keySpec, "" /* prevWordsContext */, - SuggestedWordInfo.MAX_SCORE, - SuggestedWordInfo.KIND_HARDCODED, - Dictionary.DICTIONARY_HARDCODED, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); - } -} diff --git a/java/src/com/android/inputmethod/latin/ReadOnlyBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ReadOnlyBinaryDictionary.java deleted file mode 100644 index 7b1a53a6e..000000000 --- a/java/src/com/android/inputmethod/latin/ReadOnlyBinaryDictionary.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (C) 2013 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 com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; - -import java.util.ArrayList; -import java.util.Locale; -import java.util.concurrent.locks.ReentrantReadWriteLock; - -/** - * This class provides binary dictionary reading operations with locking. An instance of this class - * can be used by multiple threads. Note that different session IDs must be used when multiple - * threads get suggestions using this class. - */ -public final class ReadOnlyBinaryDictionary extends Dictionary { - /** - * A lock for accessing binary dictionary. Only closing binary dictionary is the operation - * that change the state of dictionary. - */ - private final ReentrantReadWriteLock mLock = new ReentrantReadWriteLock(); - - private final BinaryDictionary mBinaryDictionary; - - public ReadOnlyBinaryDictionary(final String filename, final long offset, final long length, - final boolean useFullEditDistance, final Locale locale, final String dictType) { - super(dictType, locale); - mBinaryDictionary = new BinaryDictionary(filename, offset, length, useFullEditDistance, - locale, dictType, false /* isUpdatable */); - } - - public boolean isValidDictionary() { - return mBinaryDictionary.isValidDictionary(); - } - - @Override - public ArrayList<SuggestedWordInfo> getSuggestions(final ComposedData composedData, - final NgramContext ngramContext, final long proximityInfoHandle, - final SettingsValuesForSuggestion settingsValuesForSuggestion, - final int sessionId, final float weightForLocale, - final float[] inOutWeightOfLangModelVsSpatialModel) { - if (mLock.readLock().tryLock()) { - try { - return mBinaryDictionary.getSuggestions(composedData, ngramContext, - proximityInfoHandle, settingsValuesForSuggestion, sessionId, - weightForLocale, inOutWeightOfLangModelVsSpatialModel); - } finally { - mLock.readLock().unlock(); - } - } - return null; - } - - @Override - public boolean isInDictionary(final String word) { - if (mLock.readLock().tryLock()) { - try { - return mBinaryDictionary.isInDictionary(word); - } finally { - mLock.readLock().unlock(); - } - } - return false; - } - - @Override - public boolean shouldAutoCommit(final SuggestedWordInfo candidate) { - if (mLock.readLock().tryLock()) { - try { - return mBinaryDictionary.shouldAutoCommit(candidate); - } finally { - mLock.readLock().unlock(); - } - } - return false; - } - - @Override - public int getFrequency(final String word) { - if (mLock.readLock().tryLock()) { - try { - return mBinaryDictionary.getFrequency(word); - } finally { - mLock.readLock().unlock(); - } - } - return NOT_A_PROBABILITY; - } - - @Override - public int getMaxFrequencyOfExactMatches(final String word) { - if (mLock.readLock().tryLock()) { - try { - return mBinaryDictionary.getMaxFrequencyOfExactMatches(word); - } finally { - mLock.readLock().unlock(); - } - } - return NOT_A_PROBABILITY; - } - - @Override - public void close() { - mLock.writeLock().lock(); - try { - mBinaryDictionary.close(); - } finally { - mLock.writeLock().unlock(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java deleted file mode 100644 index a10f2bdb0..000000000 --- a/java/src/com/android/inputmethod/latin/RichInputConnection.java +++ /dev/null @@ -1,1033 +0,0 @@ -/* - * 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.inputmethodservice.InputMethodService; -import android.os.Build; -import android.os.Bundle; -import android.os.SystemClock; -import android.text.SpannableStringBuilder; -import android.text.TextUtils; -import android.text.style.CharacterStyle; -import android.util.Log; -import android.view.KeyEvent; -import android.view.inputmethod.CompletionInfo; -import android.view.inputmethod.CorrectionInfo; -import android.view.inputmethod.ExtractedText; -import android.view.inputmethod.ExtractedTextRequest; -import android.view.inputmethod.InputConnection; -import android.view.inputmethod.InputMethodManager; - -import com.android.inputmethod.compat.InputConnectionCompatUtils; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.UnicodeSurrogate; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.inputlogic.PrivateCommandPerformer; -import com.android.inputmethod.latin.settings.SpacingAndPunctuations; -import com.android.inputmethod.latin.utils.CapsModeUtils; -import com.android.inputmethod.latin.utils.DebugLogUtils; -import com.android.inputmethod.latin.utils.NgramContextUtils; -import com.android.inputmethod.latin.utils.ScriptUtils; -import com.android.inputmethod.latin.utils.SpannableStringUtils; -import com.android.inputmethod.latin.utils.StatsUtils; -import com.android.inputmethod.latin.utils.TextRange; - -import java.util.concurrent.TimeUnit; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Enrichment class for InputConnection to simplify interaction and add functionality. - * - * This class serves as a wrapper to be able to simply add hooks to any calls to the underlying - * InputConnection. It also keeps track of a number of things to avoid having to call upon IPC - * all the time to find out what text is in the buffer, when we need it to determine caps mode - * for example. - */ -public final class RichInputConnection implements PrivateCommandPerformer { - private static final String TAG = "RichInputConnection"; - private static final boolean DBG = false; - private static final boolean DEBUG_PREVIOUS_TEXT = false; - private static final boolean DEBUG_BATCH_NESTING = false; - private static final int NUM_CHARS_TO_GET_BEFORE_CURSOR = 40; - private static final int NUM_CHARS_TO_GET_AFTER_CURSOR = 40; - private static final int INVALID_CURSOR_POSITION = -1; - - /** - * The amount of time a {@link #reloadTextCache} call needs to take for the keyboard to enter - * the {@link #hasSlowInputConnection} state. - */ - private static final long SLOW_INPUT_CONNECTION_ON_FULL_RELOAD_MS = 1000; - /** - * The amount of time a {@link #getTextBeforeCursor} or {@link #getTextAfterCursor} call needs - * to take for the keyboard to enter the {@link #hasSlowInputConnection} state. - */ - private static final long SLOW_INPUT_CONNECTION_ON_PARTIAL_RELOAD_MS = 200; - - private static final int OPERATION_GET_TEXT_BEFORE_CURSOR = 0; - private static final int OPERATION_GET_TEXT_AFTER_CURSOR = 1; - private static final int OPERATION_GET_WORD_RANGE_AT_CURSOR = 2; - private static final int OPERATION_RELOAD_TEXT_CACHE = 3; - private static final String[] OPERATION_NAMES = new String[] { - "GET_TEXT_BEFORE_CURSOR", - "GET_TEXT_AFTER_CURSOR", - "GET_WORD_RANGE_AT_CURSOR", - "RELOAD_TEXT_CACHE"}; - - /** - * The amount of time the keyboard will persist in the {@link #hasSlowInputConnection} state - * after observing a slow InputConnection event. - */ - private static final long SLOW_INPUTCONNECTION_PERSIST_MS = TimeUnit.MINUTES.toMillis(10); - - /** - * This variable contains an expected value for the selection start position. This is where the - * cursor or selection start may end up after all the keyboard-triggered updates have passed. We - * keep this to compare it to the actual selection start to guess whether the move was caused by - * a keyboard command or not. - * It's not really the selection start position: the selection start may not be there yet, and - * in some cases, it may never arrive there. - */ - private int mExpectedSelStart = INVALID_CURSOR_POSITION; // in chars, not code points - /** - * The expected selection end. Only differs from mExpectedSelStart if a non-empty selection is - * expected. The same caveats as mExpectedSelStart apply. - */ - private int mExpectedSelEnd = INVALID_CURSOR_POSITION; // in chars, not code points - /** - * This contains the committed text immediately preceding the cursor and the composing - * text, if any. It is refreshed when the cursor moves by calling upon the TextView. - */ - private final StringBuilder mCommittedTextBeforeComposingText = new StringBuilder(); - /** - * This contains the currently composing text, as LatinIME thinks the TextView is seeing it. - */ - private final StringBuilder mComposingText = new StringBuilder(); - - /** - * This variable is a temporary object used in {@link #commitText(CharSequence,int)} - * to avoid object creation. - */ - private SpannableStringBuilder mTempObjectForCommitText = new SpannableStringBuilder(); - - private final InputMethodService mParent; - private InputConnection mIC; - private int mNestLevel; - - /** - * The timestamp of the last slow InputConnection operation - */ - private long mLastSlowInputConnectionTime = -SLOW_INPUTCONNECTION_PERSIST_MS; - - public RichInputConnection(final InputMethodService parent) { - mParent = parent; - mIC = null; - mNestLevel = 0; - } - - public boolean isConnected() { - return mIC != null; - } - - /** - * Returns whether or not the underlying InputConnection is slow. When true, we want to avoid - * calling InputConnection methods that trigger an IPC round-trip (e.g., getTextAfterCursor). - */ - public boolean hasSlowInputConnection() { - return (SystemClock.uptimeMillis() - mLastSlowInputConnectionTime) - <= SLOW_INPUTCONNECTION_PERSIST_MS; - } - - public void onStartInput() { - mLastSlowInputConnectionTime = -SLOW_INPUTCONNECTION_PERSIST_MS; - } - - private void checkConsistencyForDebug() { - final ExtractedTextRequest r = new ExtractedTextRequest(); - r.hintMaxChars = 0; - r.hintMaxLines = 0; - r.token = 1; - r.flags = 0; - final ExtractedText et = mIC.getExtractedText(r, 0); - final CharSequence beforeCursor = getTextBeforeCursor(Constants.EDITOR_CONTENTS_CACHE_SIZE, - 0); - final StringBuilder internal = new StringBuilder(mCommittedTextBeforeComposingText) - .append(mComposingText); - if (null == et || null == beforeCursor) return; - final int actualLength = Math.min(beforeCursor.length(), internal.length()); - if (internal.length() > actualLength) { - internal.delete(0, internal.length() - actualLength); - } - final String reference = (beforeCursor.length() <= actualLength) ? beforeCursor.toString() - : beforeCursor.subSequence(beforeCursor.length() - actualLength, - beforeCursor.length()).toString(); - if (et.selectionStart != mExpectedSelStart - || !(reference.equals(internal.toString()))) { - final String context = "Expected selection start = " + mExpectedSelStart - + "\nActual selection start = " + et.selectionStart - + "\nExpected text = " + internal.length() + " " + internal - + "\nActual text = " + reference.length() + " " + reference; - ((LatinIME)mParent).debugDumpStateAndCrashWithException(context); - } else { - Log.e(TAG, DebugLogUtils.getStackTrace(2)); - Log.e(TAG, "Exp <> Actual : " + mExpectedSelStart + " <> " + et.selectionStart); - } - } - - public void beginBatchEdit() { - if (++mNestLevel == 1) { - mIC = mParent.getCurrentInputConnection(); - if (isConnected()) { - mIC.beginBatchEdit(); - } - } else { - if (DBG) { - throw new RuntimeException("Nest level too deep"); - } - Log.e(TAG, "Nest level too deep : " + mNestLevel); - } - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - } - - public void endBatchEdit() { - if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead - if (--mNestLevel == 0 && isConnected()) { - mIC.endBatchEdit(); - } - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - } - - /** - * Reset the cached text and retrieve it again from the editor. - * - * This should be called when the cursor moved. It's possible that we can't connect to - * the application when doing this; notably, this happens sometimes during rotation, probably - * because of a race condition in the framework. In this case, we just can't retrieve the - * data, so we empty the cache and note that we don't know the new cursor position, and we - * return false so that the caller knows about this and can retry later. - * - * @param newSelStart the new position of the selection start, as received from the system. - * @param newSelEnd the new position of the selection end, as received from the system. - * @param shouldFinishComposition whether we should finish the composition in progress. - * @return true if we were able to connect to the editor successfully, false otherwise. When - * this method returns false, the caches could not be correctly refreshed so they were only - * reset: the caller should try again later to return to normal operation. - */ - public boolean resetCachesUponCursorMoveAndReturnSuccess(final int newSelStart, - final int newSelEnd, final boolean shouldFinishComposition) { - mExpectedSelStart = newSelStart; - mExpectedSelEnd = newSelEnd; - mComposingText.setLength(0); - final boolean didReloadTextSuccessfully = reloadTextCache(); - if (!didReloadTextSuccessfully) { - Log.d(TAG, "Will try to retrieve text later."); - return false; - } - if (isConnected() && shouldFinishComposition) { - mIC.finishComposingText(); - } - return true; - } - - /** - * Reload the cached text from the InputConnection. - * - * @return true if successful - */ - private boolean reloadTextCache() { - mCommittedTextBeforeComposingText.setLength(0); - mIC = mParent.getCurrentInputConnection(); - // Call upon the inputconnection directly since our own method is using the cache, and - // we want to refresh it. - final CharSequence textBeforeCursor = getTextBeforeCursorAndDetectLaggyConnection( - OPERATION_RELOAD_TEXT_CACHE, - SLOW_INPUT_CONNECTION_ON_FULL_RELOAD_MS, - Constants.EDITOR_CONTENTS_CACHE_SIZE, - 0 /* flags */); - if (null == textBeforeCursor) { - // For some reason the app thinks we are not connected to it. This looks like a - // framework bug... Fall back to ground state and return false. - mExpectedSelStart = INVALID_CURSOR_POSITION; - mExpectedSelEnd = INVALID_CURSOR_POSITION; - Log.e(TAG, "Unable to connect to the editor to retrieve text."); - return false; - } - mCommittedTextBeforeComposingText.append(textBeforeCursor); - return true; - } - - private void checkBatchEdit() { - if (mNestLevel != 1) { - // TODO: exception instead - Log.e(TAG, "Batch edit level incorrect : " + mNestLevel); - Log.e(TAG, DebugLogUtils.getStackTrace(4)); - } - } - - public void finishComposingText() { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - // TODO: this is not correct! The cursor is not necessarily after the composing text. - // In the practice right now this is only called when input ends so it will be reset so - // it works, but it's wrong and should be fixed. - mCommittedTextBeforeComposingText.append(mComposingText); - mComposingText.setLength(0); - if (isConnected()) { - mIC.finishComposingText(); - } - } - - /** - * Calls {@link InputConnection#commitText(CharSequence, int)}. - * - * @param text The text to commit. This may include styles. - * @param newCursorPosition The new cursor position around the text. - */ - public void commitText(final CharSequence text, final int newCursorPosition) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - mCommittedTextBeforeComposingText.append(text); - // TODO: the following is exceedingly error-prone. Right now when the cursor is in the - // middle of the composing word mComposingText only holds the part of the composing text - // that is before the cursor, so this actually works, but it's terribly confusing. Fix this. - mExpectedSelStart += text.length() - mComposingText.length(); - mExpectedSelEnd = mExpectedSelStart; - mComposingText.setLength(0); - if (isConnected()) { - mTempObjectForCommitText.clear(); - mTempObjectForCommitText.append(text); - final CharacterStyle[] spans = mTempObjectForCommitText.getSpans( - 0, text.length(), CharacterStyle.class); - for (final CharacterStyle span : spans) { - final int spanStart = mTempObjectForCommitText.getSpanStart(span); - final int spanEnd = mTempObjectForCommitText.getSpanEnd(span); - final int spanFlags = mTempObjectForCommitText.getSpanFlags(span); - // We have to adjust the end of the span to include an additional character. - // This is to avoid splitting a unicode surrogate pair. - // See com.android.inputmethod.latin.common.Constants.UnicodeSurrogate - // See https://b.corp.google.com/issues/19255233 - if (0 < spanEnd && spanEnd < mTempObjectForCommitText.length()) { - final char spanEndChar = mTempObjectForCommitText.charAt(spanEnd - 1); - final char nextChar = mTempObjectForCommitText.charAt(spanEnd); - if (UnicodeSurrogate.isLowSurrogate(spanEndChar) - && UnicodeSurrogate.isHighSurrogate(nextChar)) { - mTempObjectForCommitText.setSpan(span, spanStart, spanEnd + 1, spanFlags); - } - } - } - mIC.commitText(mTempObjectForCommitText, newCursorPosition); - } - } - - @Nullable - public CharSequence getSelectedText(final int flags) { - return isConnected() ? mIC.getSelectedText(flags) : null; - } - - public boolean canDeleteCharacters() { - return mExpectedSelStart > 0; - } - - /** - * Gets the caps modes we should be in after this specific string. - * - * This returns a bit set of TextUtils#CAP_MODE_*, masked by the inputType argument. - * This method also supports faking an additional space after the string passed in argument, - * to support cases where a space will be added automatically, like in phantom space - * state for example. - * Note that for English, we are using American typography rules (which are not specific to - * American English, it's just the most common set of rules for English). - * - * @param inputType a mask of the caps modes to test for. - * @param spacingAndPunctuations the values of the settings to use for locale and separators. - * @param hasSpaceBefore if we should consider there should be a space after the string. - * @return the caps modes that should be on as a set of bits - */ - public int getCursorCapsMode(final int inputType, - final SpacingAndPunctuations spacingAndPunctuations, final boolean hasSpaceBefore) { - mIC = mParent.getCurrentInputConnection(); - if (!isConnected()) { - return Constants.TextUtils.CAP_MODE_OFF; - } - if (!TextUtils.isEmpty(mComposingText)) { - if (hasSpaceBefore) { - // If we have some composing text and a space before, then we should have - // MODE_CHARACTERS and MODE_WORDS on. - return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & inputType; - } - // We have some composing text - we should be in MODE_CHARACTERS only. - return TextUtils.CAP_MODE_CHARACTERS & inputType; - } - // TODO: this will generally work, but there may be cases where the buffer contains SOME - // information but not enough to determine the caps mode accurately. This may happen after - // heavy pressing of delete, for example DEFAULT_TEXT_CACHE_SIZE - 5 times or so. - // getCapsMode should be updated to be able to return a "not enough info" result so that - // we can get more context only when needed. - if (TextUtils.isEmpty(mCommittedTextBeforeComposingText) && 0 != mExpectedSelStart) { - if (!reloadTextCache()) { - Log.w(TAG, "Unable to connect to the editor. " - + "Setting caps mode without knowing text."); - } - } - // This never calls InputConnection#getCapsMode - in fact, it's a static method that - // never blocks or initiates IPC. - // TODO: don't call #toString() here. Instead, all accesses to - // mCommittedTextBeforeComposingText should be done on the main thread. - return CapsModeUtils.getCapsMode(mCommittedTextBeforeComposingText.toString(), inputType, - spacingAndPunctuations, hasSpaceBefore); - } - - public int getCodePointBeforeCursor() { - final int length = mCommittedTextBeforeComposingText.length(); - if (length < 1) return Constants.NOT_A_CODE; - return Character.codePointBefore(mCommittedTextBeforeComposingText, length); - } - - public CharSequence getTextBeforeCursor(final int n, final int flags) { - final int cachedLength = - mCommittedTextBeforeComposingText.length() + mComposingText.length(); - // If we have enough characters to satisfy the request, or if we have all characters in - // the text field, then we can return the cached version right away. - // However, if we don't have an expected cursor position, then we should always - // go fetch the cache again (as it happens, INVALID_CURSOR_POSITION < 0, so we need to - // test for this explicitly) - if (INVALID_CURSOR_POSITION != mExpectedSelStart - && (cachedLength >= n || cachedLength >= mExpectedSelStart)) { - final StringBuilder s = new StringBuilder(mCommittedTextBeforeComposingText); - // We call #toString() here to create a temporary object. - // In some situations, this method is called on a worker thread, and it's possible - // the main thread touches the contents of mComposingText while this worker thread - // is suspended, because mComposingText is a StringBuilder. This may lead to crashes, - // so we call #toString() on it. That will result in the return value being strictly - // speaking wrong, but since this is used for basing bigram probability off, and - // it's only going to matter for one getSuggestions call, it's fine in the practice. - s.append(mComposingText.toString()); - if (s.length() > n) { - s.delete(0, s.length() - n); - } - return s; - } - return getTextBeforeCursorAndDetectLaggyConnection( - OPERATION_GET_TEXT_BEFORE_CURSOR, - SLOW_INPUT_CONNECTION_ON_PARTIAL_RELOAD_MS, - n, flags); - } - - private CharSequence getTextBeforeCursorAndDetectLaggyConnection( - final int operation, final long timeout, final int n, final int flags) { - mIC = mParent.getCurrentInputConnection(); - if (!isConnected()) { - return null; - } - final long startTime = SystemClock.uptimeMillis(); - final CharSequence result = mIC.getTextBeforeCursor(n, flags); - detectLaggyConnection(operation, timeout, startTime); - return result; - } - - public CharSequence getTextAfterCursor(final int n, final int flags) { - return getTextAfterCursorAndDetectLaggyConnection( - OPERATION_GET_TEXT_AFTER_CURSOR, - SLOW_INPUT_CONNECTION_ON_PARTIAL_RELOAD_MS, - n, flags); - } - - private CharSequence getTextAfterCursorAndDetectLaggyConnection( - final int operation, final long timeout, final int n, final int flags) { - mIC = mParent.getCurrentInputConnection(); - if (!isConnected()) { - return null; - } - final long startTime = SystemClock.uptimeMillis(); - final CharSequence result = mIC.getTextAfterCursor(n, flags); - detectLaggyConnection(operation, timeout, startTime); - return result; - } - - private void detectLaggyConnection(final int operation, final long timeout, final long startTime) { - final long duration = SystemClock.uptimeMillis() - startTime; - if (duration >= timeout) { - final String operationName = OPERATION_NAMES[operation]; - Log.w(TAG, "Slow InputConnection: " + operationName + " took " + duration + " ms."); - StatsUtils.onInputConnectionLaggy(operation, duration); - mLastSlowInputConnectionTime = SystemClock.uptimeMillis(); - } - } - - public void deleteTextBeforeCursor(final int beforeLength) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - // TODO: the following is incorrect if the cursor is not immediately after the composition. - // Right now we never come here in this case because we reset the composing state before we - // come here in this case, but we need to fix this. - final int remainingChars = mComposingText.length() - beforeLength; - if (remainingChars >= 0) { - mComposingText.setLength(remainingChars); - } else { - mComposingText.setLength(0); - // Never cut under 0 - final int len = Math.max(mCommittedTextBeforeComposingText.length() - + remainingChars, 0); - mCommittedTextBeforeComposingText.setLength(len); - } - if (mExpectedSelStart > beforeLength) { - mExpectedSelStart -= beforeLength; - mExpectedSelEnd -= beforeLength; - } else { - // There are fewer characters before the cursor in the buffer than we are being asked to - // delete. Only delete what is there, and update the end with the amount deleted. - mExpectedSelEnd -= mExpectedSelStart; - mExpectedSelStart = 0; - } - if (isConnected()) { - mIC.deleteSurroundingText(beforeLength, 0); - } - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - } - - public void performEditorAction(final int actionId) { - mIC = mParent.getCurrentInputConnection(); - if (isConnected()) { - mIC.performEditorAction(actionId); - } - } - - public void sendKeyEvent(final KeyEvent keyEvent) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) { - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - // This method is only called for enter or backspace when speaking to old applications - // (target SDK <= 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)), or for digits. - // When talking to new applications we never use this method because it's inherently - // racy and has unpredictable results, but for backward compatibility we continue - // sending the key events for only Enter and Backspace because some applications - // mistakenly catch them to do some stuff. - switch (keyEvent.getKeyCode()) { - case KeyEvent.KEYCODE_ENTER: - mCommittedTextBeforeComposingText.append("\n"); - mExpectedSelStart += 1; - mExpectedSelEnd = mExpectedSelStart; - break; - case KeyEvent.KEYCODE_DEL: - if (0 == mComposingText.length()) { - if (mCommittedTextBeforeComposingText.length() > 0) { - mCommittedTextBeforeComposingText.delete( - mCommittedTextBeforeComposingText.length() - 1, - mCommittedTextBeforeComposingText.length()); - } - } else { - mComposingText.delete(mComposingText.length() - 1, mComposingText.length()); - } - if (mExpectedSelStart > 0 && mExpectedSelStart == mExpectedSelEnd) { - // TODO: Handle surrogate pairs. - mExpectedSelStart -= 1; - } - mExpectedSelEnd = mExpectedSelStart; - break; - case KeyEvent.KEYCODE_UNKNOWN: - if (null != keyEvent.getCharacters()) { - mCommittedTextBeforeComposingText.append(keyEvent.getCharacters()); - mExpectedSelStart += keyEvent.getCharacters().length(); - mExpectedSelEnd = mExpectedSelStart; - } - break; - default: - final String text = StringUtils.newSingleCodePointString(keyEvent.getUnicodeChar()); - mCommittedTextBeforeComposingText.append(text); - mExpectedSelStart += text.length(); - mExpectedSelEnd = mExpectedSelStart; - break; - } - } - if (isConnected()) { - mIC.sendKeyEvent(keyEvent); - } - } - - public void setComposingRegion(final int start, final int end) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - final CharSequence textBeforeCursor = - getTextBeforeCursor(Constants.EDITOR_CONTENTS_CACHE_SIZE + (end - start), 0); - mCommittedTextBeforeComposingText.setLength(0); - if (!TextUtils.isEmpty(textBeforeCursor)) { - // The cursor is not necessarily at the end of the composing text, but we have its - // position in mExpectedSelStart and mExpectedSelEnd. In this case we want the start - // of the text, so we should use mExpectedSelStart. In other words, the composing - // text starts (mExpectedSelStart - start) characters before the end of textBeforeCursor - final int indexOfStartOfComposingText = - Math.max(textBeforeCursor.length() - (mExpectedSelStart - start), 0); - mComposingText.append(textBeforeCursor.subSequence(indexOfStartOfComposingText, - textBeforeCursor.length())); - mCommittedTextBeforeComposingText.append( - textBeforeCursor.subSequence(0, indexOfStartOfComposingText)); - } - if (isConnected()) { - mIC.setComposingRegion(start, end); - } - } - - public void setComposingText(final CharSequence text, final int newCursorPosition) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - mExpectedSelStart += text.length() - mComposingText.length(); - mExpectedSelEnd = mExpectedSelStart; - mComposingText.setLength(0); - mComposingText.append(text); - // TODO: support values of newCursorPosition != 1. At this time, this is never called with - // newCursorPosition != 1. - if (isConnected()) { - mIC.setComposingText(text, newCursorPosition); - } - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - } - - /** - * Set the selection of the text editor. - * - * Calls through to {@link InputConnection#setSelection(int, int)}. - * - * @param start the character index where the selection should start. - * @param end the character index where the selection should end. - * @return Returns true on success, false on failure: either the input connection is no longer - * valid when setting the selection or when retrieving the text cache at that point, or - * invalid arguments were passed. - */ - public boolean setSelection(final int start, final int end) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - if (start < 0 || end < 0) { - return false; - } - mExpectedSelStart = start; - mExpectedSelEnd = end; - if (isConnected()) { - final boolean isIcValid = mIC.setSelection(start, end); - if (!isIcValid) { - return false; - } - } - return reloadTextCache(); - } - - public void commitCorrection(final CorrectionInfo correctionInfo) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - // This has no effect on the text field and does not change its content. It only makes - // TextView flash the text for a second based on indices contained in the argument. - if (isConnected()) { - mIC.commitCorrection(correctionInfo); - } - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - } - - public void commitCompletion(final CompletionInfo completionInfo) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - CharSequence text = completionInfo.getText(); - // text should never be null, but just in case, it's better to insert nothing than to crash - if (null == text) text = ""; - mCommittedTextBeforeComposingText.append(text); - mExpectedSelStart += text.length() - mComposingText.length(); - mExpectedSelEnd = mExpectedSelStart; - mComposingText.setLength(0); - if (isConnected()) { - mIC.commitCompletion(completionInfo); - } - if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); - } - - @SuppressWarnings("unused") - @Nonnull - public NgramContext getNgramContextFromNthPreviousWord( - final SpacingAndPunctuations spacingAndPunctuations, final int n) { - mIC = mParent.getCurrentInputConnection(); - if (!isConnected()) { - return NgramContext.EMPTY_PREV_WORDS_INFO; - } - final CharSequence prev = getTextBeforeCursor(NUM_CHARS_TO_GET_BEFORE_CURSOR, 0); - if (DEBUG_PREVIOUS_TEXT && null != prev) { - final int checkLength = NUM_CHARS_TO_GET_BEFORE_CURSOR - 1; - final String reference = prev.length() <= checkLength ? prev.toString() - : prev.subSequence(prev.length() - checkLength, prev.length()).toString(); - // TODO: right now the following works because mComposingText holds the part of the - // composing text that is before the cursor, but this is very confusing. We should - // fix it. - final StringBuilder internal = new StringBuilder() - .append(mCommittedTextBeforeComposingText).append(mComposingText); - if (internal.length() > checkLength) { - internal.delete(0, internal.length() - checkLength); - if (!(reference.equals(internal.toString()))) { - final String context = - "Expected text = " + internal + "\nActual text = " + reference; - ((LatinIME)mParent).debugDumpStateAndCrashWithException(context); - } - } - } - return NgramContextUtils.getNgramContextFromNthPreviousWord( - prev, spacingAndPunctuations, n); - } - - private static boolean isPartOfCompositionForScript(final int codePoint, - final SpacingAndPunctuations spacingAndPunctuations, final int scriptId) { - // We always consider word connectors part of compositions. - return spacingAndPunctuations.isWordConnector(codePoint) - // Otherwise, it's part of composition if it's part of script and not a separator. - || (!spacingAndPunctuations.isWordSeparator(codePoint) - && ScriptUtils.isLetterPartOfScript(codePoint, scriptId)); - } - - /** - * Returns the text surrounding the cursor. - * - * @param spacingAndPunctuations the rules for spacing and punctuation - * @param scriptId the script we consider to be writing words, as one of ScriptUtils.SCRIPT_* - * @return a range containing the text surrounding the cursor - */ - public TextRange getWordRangeAtCursor(final SpacingAndPunctuations spacingAndPunctuations, - final int scriptId) { - mIC = mParent.getCurrentInputConnection(); - if (!isConnected()) { - return null; - } - final CharSequence before = getTextBeforeCursorAndDetectLaggyConnection( - OPERATION_GET_WORD_RANGE_AT_CURSOR, - SLOW_INPUT_CONNECTION_ON_PARTIAL_RELOAD_MS, - NUM_CHARS_TO_GET_BEFORE_CURSOR, - InputConnection.GET_TEXT_WITH_STYLES); - final CharSequence after = getTextAfterCursorAndDetectLaggyConnection( - OPERATION_GET_WORD_RANGE_AT_CURSOR, - SLOW_INPUT_CONNECTION_ON_PARTIAL_RELOAD_MS, - NUM_CHARS_TO_GET_AFTER_CURSOR, - InputConnection.GET_TEXT_WITH_STYLES); - if (before == null || after == null) { - return null; - } - - // Going backward, find the first breaking point (separator) - int startIndexInBefore = before.length(); - while (startIndexInBefore > 0) { - final int codePoint = Character.codePointBefore(before, startIndexInBefore); - if (!isPartOfCompositionForScript(codePoint, spacingAndPunctuations, scriptId)) { - break; - } - --startIndexInBefore; - if (Character.isSupplementaryCodePoint(codePoint)) { - --startIndexInBefore; - } - } - - // Find last word separator after the cursor - int endIndexInAfter = -1; - while (++endIndexInAfter < after.length()) { - final int codePoint = Character.codePointAt(after, endIndexInAfter); - if (!isPartOfCompositionForScript(codePoint, spacingAndPunctuations, scriptId)) { - break; - } - if (Character.isSupplementaryCodePoint(codePoint)) { - ++endIndexInAfter; - } - } - - final boolean hasUrlSpans = - SpannableStringUtils.hasUrlSpans(before, startIndexInBefore, before.length()) - || SpannableStringUtils.hasUrlSpans(after, 0, endIndexInAfter); - // We don't use TextUtils#concat because it copies all spans without respect to their - // nature. If the text includes a PARAGRAPH span and it has been split, then - // TextUtils#concat will crash when it tries to concat both sides of it. - return new TextRange( - SpannableStringUtils.concatWithNonParagraphSuggestionSpansOnly(before, after), - startIndexInBefore, before.length() + endIndexInAfter, before.length(), - hasUrlSpans); - } - - public boolean isCursorTouchingWord(final SpacingAndPunctuations spacingAndPunctuations, - boolean checkTextAfter) { - if (checkTextAfter && isCursorFollowedByWordCharacter(spacingAndPunctuations)) { - // If what's after the cursor is a word character, then we're touching a word. - return true; - } - final String textBeforeCursor = mCommittedTextBeforeComposingText.toString(); - int indexOfCodePointInJavaChars = textBeforeCursor.length(); - int consideredCodePoint = 0 == indexOfCodePointInJavaChars ? Constants.NOT_A_CODE - : textBeforeCursor.codePointBefore(indexOfCodePointInJavaChars); - // Search for the first non word-connector char - if (spacingAndPunctuations.isWordConnector(consideredCodePoint)) { - indexOfCodePointInJavaChars -= Character.charCount(consideredCodePoint); - consideredCodePoint = 0 == indexOfCodePointInJavaChars ? Constants.NOT_A_CODE - : textBeforeCursor.codePointBefore(indexOfCodePointInJavaChars); - } - return !(Constants.NOT_A_CODE == consideredCodePoint - || spacingAndPunctuations.isWordSeparator(consideredCodePoint) - || spacingAndPunctuations.isWordConnector(consideredCodePoint)); - } - - public boolean isCursorFollowedByWordCharacter( - final SpacingAndPunctuations spacingAndPunctuations) { - final CharSequence after = getTextAfterCursor(1, 0); - if (TextUtils.isEmpty(after)) { - return false; - } - final int codePointAfterCursor = Character.codePointAt(after, 0); - if (spacingAndPunctuations.isWordSeparator(codePointAfterCursor) - || spacingAndPunctuations.isWordConnector(codePointAfterCursor)) { - return false; - } - return true; - } - - public void removeTrailingSpace() { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - final int codePointBeforeCursor = getCodePointBeforeCursor(); - if (Constants.CODE_SPACE == codePointBeforeCursor) { - deleteTextBeforeCursor(1); - } - } - - public boolean sameAsTextBeforeCursor(final CharSequence text) { - final CharSequence beforeText = getTextBeforeCursor(text.length(), 0); - return TextUtils.equals(text, beforeText); - } - - public boolean revertDoubleSpacePeriod(final SpacingAndPunctuations spacingAndPunctuations) { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - // Here we test whether we indeed have a period and a space before us. This should not - // be needed, but it's there just in case something went wrong. - final CharSequence textBeforeCursor = getTextBeforeCursor(2, 0); - if (!TextUtils.equals(spacingAndPunctuations.mSentenceSeparatorAndSpace, - textBeforeCursor)) { - // Theoretically we should not be coming here if there isn't ". " before the - // cursor, but the application may be changing the text while we are typing, so - // anything goes. We should not crash. - Log.d(TAG, "Tried to revert double-space combo but we didn't find \"" - + spacingAndPunctuations.mSentenceSeparatorAndSpace - + "\" just before the cursor."); - return false; - } - // Double-space results in ". ". A backspace to cancel this should result in a single - // space in the text field, so we replace ". " with a single space. - deleteTextBeforeCursor(2); - final String singleSpace = " "; - commitText(singleSpace, 1); - return true; - } - - public boolean revertSwapPunctuation() { - if (DEBUG_BATCH_NESTING) checkBatchEdit(); - // Here we test whether we indeed have a space and something else before us. This should not - // be needed, but it's there just in case something went wrong. - final CharSequence textBeforeCursor = getTextBeforeCursor(2, 0); - // NOTE: This does not work with surrogate pairs. Hopefully when the keyboard is able to - // enter surrogate pairs this code will have been removed. - if (TextUtils.isEmpty(textBeforeCursor) - || (Constants.CODE_SPACE != textBeforeCursor.charAt(1))) { - // We may only come here if the application is changing the text while we are typing. - // This is quite a broken case, but not logically impossible, so we shouldn't crash, - // but some debugging log may be in order. - Log.d(TAG, "Tried to revert a swap of punctuation but we didn't " - + "find a space just before the cursor."); - return false; - } - deleteTextBeforeCursor(2); - final String text = " " + textBeforeCursor.subSequence(0, 1); - commitText(text, 1); - return true; - } - - /** - * Heuristic to determine if this is an expected update of the cursor. - * - * Sometimes updates to the cursor position are late because of their asynchronous nature. - * This method tries to determine if this update is one, based on the values of the cursor - * position in the update, and the currently expected position of the cursor according to - * LatinIME's internal accounting. If this is not a belated expected update, then it should - * mean that the user moved the cursor explicitly. - * This is quite robust, but of course it's not perfect. In particular, it will fail in the - * case we get an update A, the user types in N characters so as to move the cursor to A+N but - * we don't get those, and then the user places the cursor between A and A+N, and we get only - * this update and not the ones in-between. This is almost impossible to achieve even trying - * very very hard. - * - * @param oldSelStart The value of the old selection in the update. - * @param newSelStart The value of the new selection in the update. - * @param oldSelEnd The value of the old selection end in the update. - * @param newSelEnd The value of the new selection end in the update. - * @return whether this is a belated expected update or not. - */ - public boolean isBelatedExpectedUpdate(final int oldSelStart, final int newSelStart, - final int oldSelEnd, final int newSelEnd) { - // This update is "belated" if we are expecting it. That is, mExpectedSelStart and - // mExpectedSelEnd match the new values that the TextView is updating TO. - if (mExpectedSelStart == newSelStart && mExpectedSelEnd == newSelEnd) return true; - // This update is not belated if mExpectedSelStart and mExpectedSelEnd match the old - // values, and one of newSelStart or newSelEnd is updated to a different value. In this - // case, it is likely that something other than the IME has moved the selection endpoint - // to the new value. - if (mExpectedSelStart == oldSelStart && mExpectedSelEnd == oldSelEnd - && (oldSelStart != newSelStart || oldSelEnd != newSelEnd)) return false; - // If neither of the above two cases hold, then the system may be having trouble keeping up - // with updates. If 1) the selection is a cursor, 2) newSelStart is between oldSelStart - // and mExpectedSelStart, and 3) newSelEnd is between oldSelEnd and mExpectedSelEnd, then - // assume a belated update. - return (newSelStart == newSelEnd) - && (newSelStart - oldSelStart) * (mExpectedSelStart - newSelStart) >= 0 - && (newSelEnd - oldSelEnd) * (mExpectedSelEnd - newSelEnd) >= 0; - } - - /** - * Looks at the text just before the cursor to find out if it looks like a URL. - * - * The weakest point here is, if we don't have enough text bufferized, we may fail to realize - * we are in URL situation, but other places in this class have the same limitation and it - * does not matter too much in the practice. - */ - public boolean textBeforeCursorLooksLikeURL() { - return StringUtils.lastPartLooksLikeURL(mCommittedTextBeforeComposingText); - } - - /** - * Looks at the text just before the cursor to find out if we are inside a double quote. - * - * As with #textBeforeCursorLooksLikeURL, this is dependent on how much text we have cached. - * However this won't be a concrete problem in most situations, as the cache is almost always - * long enough for this use. - */ - public boolean isInsideDoubleQuoteOrAfterDigit() { - return StringUtils.isInsideDoubleQuoteOrAfterDigit(mCommittedTextBeforeComposingText); - } - - /** - * Try to get the text from the editor to expose lies the framework may have been - * telling us. Concretely, when the device rotates and when the keyboard reopens in the same - * text field after having been closed with the back key, the frameworks tells us about where - * the cursor used to be initially in the editor at the time it first received the focus; this - * may be completely different from the place it is upon rotation. Since we don't have any - * means to get the real value, try at least to ask the text view for some characters and - * detect the most damaging cases: when the cursor position is declared to be much smaller - * than it really is. - */ - public void tryFixLyingCursorPosition() { - mIC = mParent.getCurrentInputConnection(); - final CharSequence textBeforeCursor = getTextBeforeCursor( - Constants.EDITOR_CONTENTS_CACHE_SIZE, 0); - final CharSequence selectedText = isConnected() ? mIC.getSelectedText(0 /* flags */) : null; - if (null == textBeforeCursor || - (!TextUtils.isEmpty(selectedText) && mExpectedSelEnd == mExpectedSelStart)) { - // If textBeforeCursor is null, we have no idea what kind of text field we have or if - // thinking about the "cursor position" actually makes any sense. In this case we - // remember a meaningless cursor position. Contrast this with an empty string, which is - // valid and should mean the cursor is at the start of the text. - // Also, if we expect we don't have a selection but we DO have non-empty selected text, - // then the framework lied to us about the cursor position. In this case, we should just - // revert to the most basic behavior possible for the next action (backspace in - // particular comes to mind), so we remember a meaningless cursor position which should - // result in degraded behavior from the next input. - // Interestingly, in either case, chances are any action the user takes next will result - // in a call to onUpdateSelection, which should set things right. - mExpectedSelStart = mExpectedSelEnd = Constants.NOT_A_CURSOR_POSITION; - } else { - final int textLength = textBeforeCursor.length(); - if (textLength < Constants.EDITOR_CONTENTS_CACHE_SIZE - && (textLength > mExpectedSelStart - || mExpectedSelStart < Constants.EDITOR_CONTENTS_CACHE_SIZE)) { - // It should not be possible to have only one of those variables be - // NOT_A_CURSOR_POSITION, so if they are equal, either the selection is zero-sized - // (simple cursor, no selection) or there is no cursor/we don't know its pos - final boolean wasEqual = mExpectedSelStart == mExpectedSelEnd; - mExpectedSelStart = textLength; - // We can't figure out the value of mLastSelectionEnd :( - // But at least if it's smaller than mLastSelectionStart something is wrong, - // and if they used to be equal we also don't want to make it look like there is a - // selection. - if (wasEqual || mExpectedSelStart > mExpectedSelEnd) { - mExpectedSelEnd = mExpectedSelStart; - } - } - } - } - - @Override - public boolean performPrivateCommand(final String action, final Bundle data) { - mIC = mParent.getCurrentInputConnection(); - if (!isConnected()) { - return false; - } - return mIC.performPrivateCommand(action, data); - } - - public int getExpectedSelectionStart() { - return mExpectedSelStart; - } - - public int getExpectedSelectionEnd() { - return mExpectedSelEnd; - } - - /** - * @return whether there is a selection currently active. - */ - public boolean hasSelection() { - return mExpectedSelEnd != mExpectedSelStart; - } - - public boolean isCursorPositionKnown() { - return INVALID_CURSOR_POSITION != mExpectedSelStart; - } - - /** - * Work around a bug that was present before Jelly Bean upon rotation. - * - * Before Jelly Bean, there is a bug where setComposingRegion and other committing - * functions on the input connection get ignored until the cursor moves. This method works - * around the bug by wiggling the cursor first, which reactivates the connection and has - * the subsequent methods work, then restoring it to its original position. - * - * On platforms on which this method is not present, this is a no-op. - */ - public void maybeMoveTheCursorAroundAndRestoreToWorkaroundABug() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { - if (mExpectedSelStart > 0) { - mIC.setSelection(mExpectedSelStart - 1, mExpectedSelStart - 1); - } else { - mIC.setSelection(mExpectedSelStart + 1, mExpectedSelStart + 1); - } - mIC.setSelection(mExpectedSelStart, mExpectedSelEnd); - } - } - - /** - * Requests the editor to call back {@link InputMethodManager#updateCursorAnchorInfo}. - * @param enableMonitor {@code true} to request the editor to call back the method whenever the - * cursor/anchor position is changed. - * @param requestImmediateCallback {@code true} to request the editor to call back the method - * as soon as possible to notify the current cursor/anchor position to the input method. - * @return {@code true} if the request is accepted. Returns {@code false} otherwise, which - * includes "not implemented" or "rejected" or "temporarily unavailable" or whatever which - * prevents the application from fulfilling the request. (TODO: Improve the API when it turns - * out that we actually need more detailed error codes) - */ - public boolean requestCursorUpdates(final boolean enableMonitor, - final boolean requestImmediateCallback) { - mIC = mParent.getCurrentInputConnection(); - if (!isConnected()) { - return false; - } - return InputConnectionCompatUtils.requestCursorUpdates( - mIC, enableMonitor, requestImmediateCallback); - } -} diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java deleted file mode 100644 index 3beb51d68..000000000 --- a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java +++ /dev/null @@ -1,612 +0,0 @@ -/* - * 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 static com.android.inputmethod.latin.common.Constants.Subtype.KEYBOARD_MODE; - -import android.content.Context; -import android.content.SharedPreferences; -import android.inputmethodservice.InputMethodService; -import android.os.AsyncTask; -import android.os.Build; -import android.os.IBinder; -import android.preference.PreferenceManager; -import android.util.Log; -import android.view.inputmethod.InputMethodInfo; -import android.view.inputmethod.InputMethodManager; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; -import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; -import com.android.inputmethod.latin.settings.Settings; -import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; -import com.android.inputmethod.latin.utils.LanguageOnSpacebarUtils; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Enrichment class for InputMethodManager to simplify interaction and add functionality. - */ -// non final for easy mocking. -public class RichInputMethodManager { - private static final String TAG = RichInputMethodManager.class.getSimpleName(); - private static final boolean DEBUG = false; - - private RichInputMethodManager() { - // This utility class is not publicly instantiable. - } - - private static final RichInputMethodManager sInstance = new RichInputMethodManager(); - - private Context mContext; - private InputMethodManagerCompatWrapper mImmWrapper; - private InputMethodInfoCache mInputMethodInfoCache; - private RichInputMethodSubtype mCurrentRichInputMethodSubtype; - private InputMethodInfo mShortcutInputMethodInfo; - private InputMethodSubtype mShortcutSubtype; - - private static final int INDEX_NOT_FOUND = -1; - - public static RichInputMethodManager getInstance() { - sInstance.checkInitialized(); - return sInstance; - } - - public static void init(final Context context) { - sInstance.initInternal(context); - } - - private boolean isInitialized() { - return mImmWrapper != null; - } - - private void checkInitialized() { - if (!isInitialized()) { - throw new RuntimeException(TAG + " is used before initialization"); - } - } - - private void initInternal(final Context context) { - if (isInitialized()) { - return; - } - mImmWrapper = new InputMethodManagerCompatWrapper(context); - mContext = context; - mInputMethodInfoCache = new InputMethodInfoCache( - mImmWrapper.mImm, context.getPackageName()); - - // Initialize additional subtypes. - SubtypeLocaleUtils.init(context); - final InputMethodSubtype[] additionalSubtypes = getAdditionalSubtypes(); - mImmWrapper.mImm.setAdditionalInputMethodSubtypes( - getInputMethodIdOfThisIme(), additionalSubtypes); - - // Initialize the current input method subtype and the shortcut IME. - refreshSubtypeCaches(); - } - - public InputMethodSubtype[] getAdditionalSubtypes() { - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); - final String prefAdditionalSubtypes = Settings.readPrefAdditionalSubtypes( - prefs, mContext.getResources()); - return AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefAdditionalSubtypes); - } - - public InputMethodManager getInputMethodManager() { - checkInitialized(); - return mImmWrapper.mImm; - } - - public List<InputMethodSubtype> getMyEnabledInputMethodSubtypeList( - boolean allowsImplicitlySelectedSubtypes) { - return getEnabledInputMethodSubtypeList( - getInputMethodInfoOfThisIme(), allowsImplicitlySelectedSubtypes); - } - - public boolean switchToNextInputMethod(final IBinder token, final boolean onlyCurrentIme) { - if (mImmWrapper.switchToNextInputMethod(token, onlyCurrentIme)) { - return true; - } - // Was not able to call {@link InputMethodManager#switchToNextInputMethodIBinder,boolean)} - // because the current device is running ICS or previous and lacks the API. - if (switchToNextInputSubtypeInThisIme(token, onlyCurrentIme)) { - return true; - } - return switchToNextInputMethodAndSubtype(token); - } - - private boolean switchToNextInputSubtypeInThisIme(final IBinder token, - final boolean onlyCurrentIme) { - final InputMethodManager imm = mImmWrapper.mImm; - final InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype(); - final List<InputMethodSubtype> enabledSubtypes = getMyEnabledInputMethodSubtypeList( - true /* allowsImplicitlySelectedSubtypes */); - final int currentIndex = getSubtypeIndexInList(currentSubtype, enabledSubtypes); - if (currentIndex == INDEX_NOT_FOUND) { - Log.w(TAG, "Can't find current subtype in enabled subtypes: subtype=" - + SubtypeLocaleUtils.getSubtypeNameForLogging(currentSubtype)); - return false; - } - final int nextIndex = (currentIndex + 1) % enabledSubtypes.size(); - if (nextIndex <= currentIndex && !onlyCurrentIme) { - // The current subtype is the last or only enabled one and it needs to switch to - // next IME. - return false; - } - final InputMethodSubtype nextSubtype = enabledSubtypes.get(nextIndex); - setInputMethodAndSubtype(token, nextSubtype); - return true; - } - - private boolean switchToNextInputMethodAndSubtype(final IBinder token) { - final InputMethodManager imm = mImmWrapper.mImm; - final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); - final int currentIndex = getImiIndexInList(getInputMethodInfoOfThisIme(), enabledImis); - if (currentIndex == INDEX_NOT_FOUND) { - Log.w(TAG, "Can't find current IME in enabled IMEs: IME package=" - + getInputMethodInfoOfThisIme().getPackageName()); - return false; - } - final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis); - final List<InputMethodSubtype> enabledSubtypes = getEnabledInputMethodSubtypeList(nextImi, - true /* allowsImplicitlySelectedSubtypes */); - if (enabledSubtypes.isEmpty()) { - // The next IME has no subtype. - imm.setInputMethod(token, nextImi.getId()); - return true; - } - final InputMethodSubtype firstSubtype = enabledSubtypes.get(0); - imm.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype); - return true; - } - - private static int getImiIndexInList(final InputMethodInfo inputMethodInfo, - final List<InputMethodInfo> imiList) { - final int count = imiList.size(); - for (int index = 0; index < count; index++) { - final InputMethodInfo imi = imiList.get(index); - if (imi.equals(inputMethodInfo)) { - return index; - } - } - return INDEX_NOT_FOUND; - } - - // This method mimics {@link InputMethodManager#switchToNextInputMethod(IBinder,boolean)}. - private static InputMethodInfo getNextNonAuxiliaryIme(final int currentIndex, - final List<InputMethodInfo> imiList) { - final int count = imiList.size(); - for (int i = 1; i < count; i++) { - final int nextIndex = (currentIndex + i) % count; - final InputMethodInfo nextImi = imiList.get(nextIndex); - if (!isAuxiliaryIme(nextImi)) { - return nextImi; - } - } - return imiList.get(currentIndex); - } - - // Copied from {@link InputMethodInfo}. See how auxiliary of IME is determined. - private static boolean isAuxiliaryIme(final InputMethodInfo imi) { - final int count = imi.getSubtypeCount(); - if (count == 0) { - return false; - } - for (int index = 0; index < count; index++) { - final InputMethodSubtype subtype = imi.getSubtypeAt(index); - if (!subtype.isAuxiliary()) { - return false; - } - } - return true; - } - - private static class InputMethodInfoCache { - private final InputMethodManager mImm; - private final String mImePackageName; - - private InputMethodInfo mCachedThisImeInfo; - private final HashMap<InputMethodInfo, List<InputMethodSubtype>> - mCachedSubtypeListWithImplicitlySelected; - private final HashMap<InputMethodInfo, List<InputMethodSubtype>> - mCachedSubtypeListOnlyExplicitlySelected; - - public InputMethodInfoCache(final InputMethodManager imm, final String imePackageName) { - mImm = imm; - mImePackageName = imePackageName; - mCachedSubtypeListWithImplicitlySelected = new HashMap<>(); - mCachedSubtypeListOnlyExplicitlySelected = new HashMap<>(); - } - - public synchronized InputMethodInfo getInputMethodOfThisIme() { - if (mCachedThisImeInfo != null) { - return mCachedThisImeInfo; - } - for (final InputMethodInfo imi : mImm.getInputMethodList()) { - if (imi.getPackageName().equals(mImePackageName)) { - mCachedThisImeInfo = imi; - return imi; - } - } - throw new RuntimeException("Input method id for " + mImePackageName + " not found."); - } - - public synchronized List<InputMethodSubtype> getEnabledInputMethodSubtypeList( - final InputMethodInfo imi, final boolean allowsImplicitlySelectedSubtypes) { - final HashMap<InputMethodInfo, List<InputMethodSubtype>> cache = - allowsImplicitlySelectedSubtypes - ? mCachedSubtypeListWithImplicitlySelected - : mCachedSubtypeListOnlyExplicitlySelected; - final List<InputMethodSubtype> cachedList = cache.get(imi); - if (cachedList != null) { - return cachedList; - } - final List<InputMethodSubtype> result = mImm.getEnabledInputMethodSubtypeList( - imi, allowsImplicitlySelectedSubtypes); - cache.put(imi, result); - return result; - } - - public synchronized void clear() { - mCachedThisImeInfo = null; - mCachedSubtypeListWithImplicitlySelected.clear(); - mCachedSubtypeListOnlyExplicitlySelected.clear(); - } - } - - public InputMethodInfo getInputMethodInfoOfThisIme() { - return mInputMethodInfoCache.getInputMethodOfThisIme(); - } - - public String getInputMethodIdOfThisIme() { - return getInputMethodInfoOfThisIme().getId(); - } - - public boolean checkIfSubtypeBelongsToThisImeAndEnabled(final InputMethodSubtype subtype) { - return checkIfSubtypeBelongsToList(subtype, - getEnabledInputMethodSubtypeList( - getInputMethodInfoOfThisIme(), - true /* allowsImplicitlySelectedSubtypes */)); - } - - public boolean checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled( - final InputMethodSubtype subtype) { - final boolean subtypeEnabled = checkIfSubtypeBelongsToThisImeAndEnabled(subtype); - final boolean subtypeExplicitlyEnabled = checkIfSubtypeBelongsToList(subtype, - getMyEnabledInputMethodSubtypeList(false /* allowsImplicitlySelectedSubtypes */)); - return subtypeEnabled && !subtypeExplicitlyEnabled; - } - - private static boolean checkIfSubtypeBelongsToList(final InputMethodSubtype subtype, - final List<InputMethodSubtype> subtypes) { - return getSubtypeIndexInList(subtype, subtypes) != INDEX_NOT_FOUND; - } - - private static int getSubtypeIndexInList(final InputMethodSubtype subtype, - final List<InputMethodSubtype> subtypes) { - final int count = subtypes.size(); - for (int index = 0; index < count; index++) { - final InputMethodSubtype ims = subtypes.get(index); - if (ims.equals(subtype)) { - return index; - } - } - return INDEX_NOT_FOUND; - } - - public void onSubtypeChanged(@Nonnull final InputMethodSubtype newSubtype) { - updateCurrentSubtype(newSubtype); - updateShortcutIme(); - if (DEBUG) { - Log.w(TAG, "onSubtypeChanged: " + mCurrentRichInputMethodSubtype.getNameForLogging()); - } - } - - private static RichInputMethodSubtype sForcedSubtypeForTesting = null; - - @UsedForTesting - static void forceSubtype(@Nonnull final InputMethodSubtype subtype) { - sForcedSubtypeForTesting = RichInputMethodSubtype.getRichInputMethodSubtype(subtype); - } - - @Nonnull - public Locale getCurrentSubtypeLocale() { - if (null != sForcedSubtypeForTesting) { - return sForcedSubtypeForTesting.getLocale(); - } - return getCurrentSubtype().getLocale(); - } - - @Nonnull - public RichInputMethodSubtype getCurrentSubtype() { - if (null != sForcedSubtypeForTesting) { - return sForcedSubtypeForTesting; - } - return mCurrentRichInputMethodSubtype; - } - - - public String getCombiningRulesExtraValueOfCurrentSubtype() { - return SubtypeLocaleUtils.getCombiningRulesExtraValue(getCurrentSubtype().getRawSubtype()); - } - - public boolean hasMultipleEnabledIMEsOrSubtypes(final boolean shouldIncludeAuxiliarySubtypes) { - final List<InputMethodInfo> enabledImis = mImmWrapper.mImm.getEnabledInputMethodList(); - return hasMultipleEnabledSubtypes(shouldIncludeAuxiliarySubtypes, enabledImis); - } - - public boolean hasMultipleEnabledSubtypesInThisIme( - final boolean shouldIncludeAuxiliarySubtypes) { - final List<InputMethodInfo> imiList = Collections.singletonList( - getInputMethodInfoOfThisIme()); - return hasMultipleEnabledSubtypes(shouldIncludeAuxiliarySubtypes, imiList); - } - - private boolean hasMultipleEnabledSubtypes(final boolean shouldIncludeAuxiliarySubtypes, - final List<InputMethodInfo> imiList) { - // Number of the filtered IMEs - int filteredImisCount = 0; - - for (InputMethodInfo imi : imiList) { - // We can return true immediately after we find two or more filtered IMEs. - if (filteredImisCount > 1) return true; - final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeList(imi, true); - // IMEs that have no subtypes should be counted. - if (subtypes.isEmpty()) { - ++filteredImisCount; - continue; - } - - int auxCount = 0; - for (InputMethodSubtype subtype : subtypes) { - if (subtype.isAuxiliary()) { - ++auxCount; - } - } - final int nonAuxCount = subtypes.size() - auxCount; - - // IMEs that have one or more non-auxiliary subtypes should be counted. - // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary - // subtypes should be counted as well. - if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { - ++filteredImisCount; - } - } - - if (filteredImisCount > 1) { - return true; - } - final List<InputMethodSubtype> subtypes = getMyEnabledInputMethodSubtypeList(true); - int keyboardCount = 0; - // imm.getEnabledInputMethodSubtypeList(null, true) will return the current IME's - // both explicitly and implicitly enabled input method subtype. - // (The current IME should be LatinIME.) - for (InputMethodSubtype subtype : subtypes) { - if (KEYBOARD_MODE.equals(subtype.getMode())) { - ++keyboardCount; - } - } - return keyboardCount > 1; - } - - public InputMethodSubtype findSubtypeByLocaleAndKeyboardLayoutSet(final String localeString, - final String keyboardLayoutSetName) { - final InputMethodInfo myImi = getInputMethodInfoOfThisIme(); - final int count = myImi.getSubtypeCount(); - for (int i = 0; i < count; i++) { - final InputMethodSubtype subtype = myImi.getSubtypeAt(i); - final String layoutName = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype); - if (localeString.equals(subtype.getLocale()) - && keyboardLayoutSetName.equals(layoutName)) { - return subtype; - } - } - return null; - } - - public InputMethodSubtype findSubtypeByLocale(final Locale locale) { - // Find the best subtype based on a straightforward matching algorithm. - // TODO: Use LocaleList#getFirstMatch() instead. - final List<InputMethodSubtype> subtypes = - getMyEnabledInputMethodSubtypeList(true /* allowsImplicitlySelectedSubtypes */); - final int count = subtypes.size(); - for (int i = 0; i < count; ++i) { - final InputMethodSubtype subtype = subtypes.get(i); - final Locale subtypeLocale = InputMethodSubtypeCompatUtils.getLocaleObject(subtype); - if (subtypeLocale.equals(locale)) { - return subtype; - } - } - for (int i = 0; i < count; ++i) { - final InputMethodSubtype subtype = subtypes.get(i); - final Locale subtypeLocale = InputMethodSubtypeCompatUtils.getLocaleObject(subtype); - if (subtypeLocale.getLanguage().equals(locale.getLanguage()) && - subtypeLocale.getCountry().equals(locale.getCountry()) && - subtypeLocale.getVariant().equals(locale.getVariant())) { - return subtype; - } - } - for (int i = 0; i < count; ++i) { - final InputMethodSubtype subtype = subtypes.get(i); - final Locale subtypeLocale = InputMethodSubtypeCompatUtils.getLocaleObject(subtype); - if (subtypeLocale.getLanguage().equals(locale.getLanguage()) && - subtypeLocale.getCountry().equals(locale.getCountry())) { - return subtype; - } - } - for (int i = 0; i < count; ++i) { - final InputMethodSubtype subtype = subtypes.get(i); - final Locale subtypeLocale = InputMethodSubtypeCompatUtils.getLocaleObject(subtype); - if (subtypeLocale.getLanguage().equals(locale.getLanguage())) { - return subtype; - } - } - return null; - } - - public void setInputMethodAndSubtype(final IBinder token, final InputMethodSubtype subtype) { - mImmWrapper.mImm.setInputMethodAndSubtype( - token, getInputMethodIdOfThisIme(), subtype); - } - - public void setAdditionalInputMethodSubtypes(final InputMethodSubtype[] subtypes) { - mImmWrapper.mImm.setAdditionalInputMethodSubtypes( - getInputMethodIdOfThisIme(), subtypes); - // Clear the cache so that we go read the {@link InputMethodInfo} of this IME and list of - // subtypes again next time. - refreshSubtypeCaches(); - } - - private List<InputMethodSubtype> getEnabledInputMethodSubtypeList(final InputMethodInfo imi, - final boolean allowsImplicitlySelectedSubtypes) { - return mInputMethodInfoCache.getEnabledInputMethodSubtypeList( - imi, allowsImplicitlySelectedSubtypes); - } - - public void refreshSubtypeCaches() { - mInputMethodInfoCache.clear(); - updateCurrentSubtype(mImmWrapper.mImm.getCurrentInputMethodSubtype()); - updateShortcutIme(); - } - - public boolean shouldOfferSwitchingToNextInputMethod(final IBinder binder, - boolean defaultValue) { - // Use the default value instead on Jelly Bean MR2 and previous where - // {@link InputMethodManager#shouldOfferSwitchingToNextInputMethod} isn't yet available - // and on KitKat where the API is still just a stub to return true always. - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { - return defaultValue; - } - return mImmWrapper.shouldOfferSwitchingToNextInputMethod(binder); - } - - public boolean isSystemLocaleSameAsLocaleOfAllEnabledSubtypesOfEnabledImes() { - final Locale systemLocale = mContext.getResources().getConfiguration().locale; - final Set<InputMethodSubtype> enabledSubtypesOfEnabledImes = new HashSet<>(); - final InputMethodManager inputMethodManager = getInputMethodManager(); - final List<InputMethodInfo> enabledInputMethodInfoList = - inputMethodManager.getEnabledInputMethodList(); - for (final InputMethodInfo info : enabledInputMethodInfoList) { - final List<InputMethodSubtype> enabledSubtypes = - inputMethodManager.getEnabledInputMethodSubtypeList( - info, true /* allowsImplicitlySelectedSubtypes */); - if (enabledSubtypes.isEmpty()) { - // An IME with no subtypes is found. - return false; - } - enabledSubtypesOfEnabledImes.addAll(enabledSubtypes); - } - for (final InputMethodSubtype subtype : enabledSubtypesOfEnabledImes) { - if (!subtype.isAuxiliary() && !subtype.getLocale().isEmpty() - && !systemLocale.equals(SubtypeLocaleUtils.getSubtypeLocale(subtype))) { - return false; - } - } - return true; - } - - private void updateCurrentSubtype(@Nullable final InputMethodSubtype subtype) { - mCurrentRichInputMethodSubtype = RichInputMethodSubtype.getRichInputMethodSubtype(subtype); - } - - private void updateShortcutIme() { - if (DEBUG) { - Log.d(TAG, "Update shortcut IME from : " - + (mShortcutInputMethodInfo == null - ? "<null>" : mShortcutInputMethodInfo.getId()) + ", " - + (mShortcutSubtype == null ? "<null>" : ( - mShortcutSubtype.getLocale() + ", " + mShortcutSubtype.getMode()))); - } - final RichInputMethodSubtype richSubtype = mCurrentRichInputMethodSubtype; - final boolean implicitlyEnabledSubtype = checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled( - richSubtype.getRawSubtype()); - final Locale systemLocale = mContext.getResources().getConfiguration().locale; - LanguageOnSpacebarUtils.onSubtypeChanged( - richSubtype, implicitlyEnabledSubtype, systemLocale); - LanguageOnSpacebarUtils.setEnabledSubtypes(getMyEnabledInputMethodSubtypeList( - true /* allowsImplicitlySelectedSubtypes */)); - - // TODO: Update an icon for shortcut IME - final Map<InputMethodInfo, List<InputMethodSubtype>> shortcuts = - getInputMethodManager().getShortcutInputMethodsAndSubtypes(); - mShortcutInputMethodInfo = null; - mShortcutSubtype = null; - for (final InputMethodInfo imi : shortcuts.keySet()) { - final List<InputMethodSubtype> subtypes = shortcuts.get(imi); - // TODO: Returns the first found IMI for now. Should handle all shortcuts as - // appropriate. - mShortcutInputMethodInfo = imi; - // TODO: Pick up the first found subtype for now. Should handle all subtypes - // as appropriate. - mShortcutSubtype = subtypes.size() > 0 ? subtypes.get(0) : null; - break; - } - if (DEBUG) { - Log.d(TAG, "Update shortcut IME to : " - + (mShortcutInputMethodInfo == null - ? "<null>" : mShortcutInputMethodInfo.getId()) + ", " - + (mShortcutSubtype == null ? "<null>" : ( - mShortcutSubtype.getLocale() + ", " + mShortcutSubtype.getMode()))); - } - } - - public void switchToShortcutIme(final InputMethodService context) { - if (mShortcutInputMethodInfo == null) { - return; - } - - final String imiId = mShortcutInputMethodInfo.getId(); - switchToTargetIME(imiId, mShortcutSubtype, context); - } - - private void switchToTargetIME(final String imiId, final InputMethodSubtype subtype, - final InputMethodService context) { - final IBinder token = context.getWindow().getWindow().getAttributes().token; - if (token == null) { - return; - } - final InputMethodManager imm = getInputMethodManager(); - new AsyncTask<Void, Void, Void>() { - @Override - protected Void doInBackground(Void... params) { - imm.setInputMethodAndSubtype(token, imiId, subtype); - return null; - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - - public boolean isShortcutImeReady() { - if (mShortcutInputMethodInfo == null) { - return false; - } - if (mShortcutSubtype == null) { - return true; - } - return true; - } -} diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java b/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java deleted file mode 100644 index 941149895..000000000 --- a/java/src/com/android/inputmethod/latin/RichInputMethodSubtype.java +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright (C) 2014 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 static com.android.inputmethod.latin.common.Constants.Subtype.KEYBOARD_MODE; - -import android.os.Build; -import android.util.Log; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.compat.BuildCompatUtils; -import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.LocaleUtils; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; - -import java.util.HashMap; -import java.util.Locale; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Enrichment class for InputMethodSubtype to enable concurrent multi-lingual input. - * - * Right now, this returns the extra value of its primary subtype. - */ -// non final for easy mocking. -public class RichInputMethodSubtype { - private static final String TAG = RichInputMethodSubtype.class.getSimpleName(); - - private static final HashMap<Locale, Locale> sLocaleMap = initializeLocaleMap(); - private static final HashMap<Locale, Locale> initializeLocaleMap() { - final HashMap<Locale, Locale> map = new HashMap<>(); - if (BuildCompatUtils.EFFECTIVE_SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - // Locale#forLanguageTag is available on API Level 21+. - // TODO: Remove this workaround once when we become able to deal with "sr-Latn". - map.put(Locale.forLanguageTag("sr-Latn"), new Locale("sr_ZZ")); - } - return map; - } - - @Nonnull - private final InputMethodSubtype mSubtype; - @Nonnull - private final Locale mLocale; - @Nonnull - private final Locale mOriginalLocale; - - public RichInputMethodSubtype(@Nonnull final InputMethodSubtype subtype) { - mSubtype = subtype; - mOriginalLocale = InputMethodSubtypeCompatUtils.getLocaleObject(mSubtype); - final Locale mappedLocale = sLocaleMap.get(mOriginalLocale); - mLocale = mappedLocale != null ? mappedLocale : mOriginalLocale; - } - - // Extra values are determined by the primary subtype. This is probably right, but - // we may have to revisit this later. - public String getExtraValueOf(@Nonnull final String key) { - return mSubtype.getExtraValueOf(key); - } - - // The mode is also determined by the primary subtype. - public String getMode() { - return mSubtype.getMode(); - } - - public boolean isNoLanguage() { - return SubtypeLocaleUtils.NO_LANGUAGE.equals(mSubtype.getLocale()); - } - - public String getNameForLogging() { - return toString(); - } - - // InputMethodSubtype's display name for spacebar text in its locale. - // isAdditionalSubtype (T=true, F=false) - // locale layout | Middle Full - // ------ ------- - --------- ---------------------- - // en_US qwerty F English English (US) exception - // en_GB qwerty F English English (UK) exception - // es_US spanish F Español Español (EE.UU.) exception - // fr azerty F Français Français - // fr_CA qwerty F Français Français (Canada) - // fr_CH swiss F Français Français (Suisse) - // de qwertz F Deutsch Deutsch - // de_CH swiss T Deutsch Deutsch (Schweiz) - // zz qwerty F QWERTY QWERTY - // fr qwertz T Français Français - // de qwerty T Deutsch Deutsch - // en_US azerty T English English (US) - // zz azerty T AZERTY AZERTY - // Get the RichInputMethodSubtype's full display name in its locale. - @Nonnull - public String getFullDisplayName() { - if (isNoLanguage()) { - return SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(mSubtype); - } - return SubtypeLocaleUtils.getSubtypeLocaleDisplayName(mSubtype.getLocale()); - } - - // Get the RichInputMethodSubtype's middle display name in its locale. - @Nonnull - public String getMiddleDisplayName() { - if (isNoLanguage()) { - return SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(mSubtype); - } - return SubtypeLocaleUtils.getSubtypeLanguageDisplayName(mSubtype.getLocale()); - } - - @Override - public boolean equals(final Object o) { - if (!(o instanceof RichInputMethodSubtype)) { - return false; - } - final RichInputMethodSubtype other = (RichInputMethodSubtype)o; - return mSubtype.equals(other.mSubtype) && mLocale.equals(other.mLocale); - } - - @Override - public int hashCode() { - return mSubtype.hashCode() + mLocale.hashCode(); - } - - @Override - public String toString() { - return "Multi-lingual subtype: " + mSubtype + ", " + mLocale; - } - - @Nonnull - public Locale getLocale() { - return mLocale; - } - - @Nonnull - public Locale getOriginalLocale() { - return mOriginalLocale; - } - - public boolean isRtlSubtype() { - // The subtype is considered RTL if the language of the main subtype is RTL. - return LocaleUtils.isRtlLanguage(mLocale); - } - - // TODO: remove this method - @Nonnull - public InputMethodSubtype getRawSubtype() { return mSubtype; } - - @Nonnull - public String getKeyboardLayoutSetName() { - return SubtypeLocaleUtils.getKeyboardLayoutSetName(mSubtype); - } - - public static RichInputMethodSubtype getRichInputMethodSubtype( - @Nullable final InputMethodSubtype subtype) { - if (subtype == null) { - return getNoLanguageSubtype(); - } else { - return new RichInputMethodSubtype(subtype); - } - } - - // Placeholer for no language QWERTY subtype. See {@link R.xml.method}. - private static final int SUBTYPE_ID_OF_PLACEHOLDER_NO_LANGUAGE_SUBTYPE = 0xdde0bfd3; - private static final String EXTRA_VALUE_OF_PLACEHOLDER_NO_LANGUAGE_SUBTYPE = - "KeyboardLayoutSet=" + SubtypeLocaleUtils.QWERTY - + "," + Constants.Subtype.ExtraValue.ASCII_CAPABLE - + "," + Constants.Subtype.ExtraValue.ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE - + "," + Constants.Subtype.ExtraValue.EMOJI_CAPABLE; - @Nonnull - private static final RichInputMethodSubtype PLACEHOLDER_NO_LANGUAGE_SUBTYPE = - new RichInputMethodSubtype(InputMethodSubtypeCompatUtils.newInputMethodSubtype( - R.string.subtype_no_language_qwerty, R.drawable.ic_ime_switcher_dark, - SubtypeLocaleUtils.NO_LANGUAGE, KEYBOARD_MODE, - EXTRA_VALUE_OF_PLACEHOLDER_NO_LANGUAGE_SUBTYPE, - false /* isAuxiliary */, false /* overridesImplicitlyEnabledSubtype */, - SUBTYPE_ID_OF_PLACEHOLDER_NO_LANGUAGE_SUBTYPE)); - // Caveat: We probably should remove this when we add an Emoji subtype in {@link R.xml.method}. - // Placeholder Emoji subtype. See {@link R.xml.method}. - private static final int SUBTYPE_ID_OF_PLACEHOLDER_EMOJI_SUBTYPE = 0xd78b2ed0; - private static final String EXTRA_VALUE_OF_PLACEHOLDER_EMOJI_SUBTYPE = - "KeyboardLayoutSet=" + SubtypeLocaleUtils.EMOJI - + "," + Constants.Subtype.ExtraValue.EMOJI_CAPABLE; - @Nonnull - private static final RichInputMethodSubtype PLACEHOLDER_EMOJI_SUBTYPE = new RichInputMethodSubtype( - InputMethodSubtypeCompatUtils.newInputMethodSubtype( - R.string.subtype_emoji, R.drawable.ic_ime_switcher_dark, - SubtypeLocaleUtils.NO_LANGUAGE, KEYBOARD_MODE, - EXTRA_VALUE_OF_PLACEHOLDER_EMOJI_SUBTYPE, - false /* isAuxiliary */, false /* overridesImplicitlyEnabledSubtype */, - SUBTYPE_ID_OF_PLACEHOLDER_EMOJI_SUBTYPE)); - private static RichInputMethodSubtype sNoLanguageSubtype; - private static RichInputMethodSubtype sEmojiSubtype; - - @Nonnull - public static RichInputMethodSubtype getNoLanguageSubtype() { - RichInputMethodSubtype noLanguageSubtype = sNoLanguageSubtype; - if (noLanguageSubtype == null) { - final InputMethodSubtype rawNoLanguageSubtype = RichInputMethodManager.getInstance() - .findSubtypeByLocaleAndKeyboardLayoutSet( - SubtypeLocaleUtils.NO_LANGUAGE, SubtypeLocaleUtils.QWERTY); - if (rawNoLanguageSubtype != null) { - noLanguageSubtype = new RichInputMethodSubtype(rawNoLanguageSubtype); - } - } - if (noLanguageSubtype != null) { - sNoLanguageSubtype = noLanguageSubtype; - return noLanguageSubtype; - } - Log.w(TAG, "Can't find any language with QWERTY subtype"); - Log.w(TAG, "No input method subtype found; returning placeholder subtype: " - + PLACEHOLDER_NO_LANGUAGE_SUBTYPE); - return PLACEHOLDER_NO_LANGUAGE_SUBTYPE; - } - - @Nonnull - public static RichInputMethodSubtype getEmojiSubtype() { - RichInputMethodSubtype emojiSubtype = sEmojiSubtype; - if (emojiSubtype == null) { - final InputMethodSubtype rawEmojiSubtype = RichInputMethodManager.getInstance() - .findSubtypeByLocaleAndKeyboardLayoutSet( - SubtypeLocaleUtils.NO_LANGUAGE, SubtypeLocaleUtils.EMOJI); - if (rawEmojiSubtype != null) { - emojiSubtype = new RichInputMethodSubtype(rawEmojiSubtype); - } - } - if (emojiSubtype != null) { - sEmojiSubtype = emojiSubtype; - return emojiSubtype; - } - Log.w(TAG, "Can't find emoji subtype"); - Log.w(TAG, "No input method subtype found; returning placeholder subtype: " - + PLACEHOLDER_EMOJI_SUBTYPE); - return PLACEHOLDER_EMOJI_SUBTYPE; - } -} diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java deleted file mode 100644 index da23617af..000000000 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ /dev/null @@ -1,434 +0,0 @@ -/* - * Copyright (C) 2008 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.text.TextUtils; - -import static com.android.inputmethod.latin.define.DecoderSpecificConstants.SHOULD_AUTO_CORRECT_USING_NON_WHITE_LISTED_SUGGESTION; -import static com.android.inputmethod.latin.define.DecoderSpecificConstants.SHOULD_REMOVE_PREVIOUSLY_REJECTED_SUGGESTION; - -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.define.DebugFlags; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; -import com.android.inputmethod.latin.utils.AutoCorrectionUtils; -import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; -import com.android.inputmethod.latin.utils.SuggestionResults; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Locale; - -import javax.annotation.Nonnull; - -/** - * This class loads a dictionary and provides a list of suggestions for a given sequence of - * characters. This includes corrections and completions. - */ -public final class Suggest { - public static final String TAG = Suggest.class.getSimpleName(); - - // Session id for - // {@link #getSuggestedWords(WordComposer,String,ProximityInfo,boolean,int)}. - // We are sharing the same ID between typing and gesture to save RAM footprint. - public static final int SESSION_ID_TYPING = 0; - public static final int SESSION_ID_GESTURE = 0; - - // Close to -2**31 - private static final int SUPPRESS_SUGGEST_THRESHOLD = -2000000000; - - private static final boolean DBG = DebugFlags.DEBUG_ENABLED; - private final DictionaryFacilitator mDictionaryFacilitator; - - private static final int MAXIMUM_AUTO_CORRECT_LENGTH_FOR_GERMAN = 12; - private static final HashMap<String, Integer> sLanguageToMaximumAutoCorrectionWithSpaceLength = - new HashMap<>(); - static { - // TODO: should we add Finnish here? - // TODO: This should not be hardcoded here but be written in the dictionary header - sLanguageToMaximumAutoCorrectionWithSpaceLength.put(Locale.GERMAN.getLanguage(), - MAXIMUM_AUTO_CORRECT_LENGTH_FOR_GERMAN); - } - - private float mAutoCorrectionThreshold; - private float mPlausibilityThreshold; - - public Suggest(final DictionaryFacilitator dictionaryFacilitator) { - mDictionaryFacilitator = dictionaryFacilitator; - } - - /** - * Set the normalized-score threshold for a suggestion to be considered strong enough that we - * will auto-correct to this. - * @param threshold the threshold - */ - public void setAutoCorrectionThreshold(final float threshold) { - mAutoCorrectionThreshold = threshold; - } - - /** - * Set the normalized-score threshold for what we consider a "plausible" suggestion, in - * the same dimension as the auto-correction threshold. - * @param threshold the threshold - */ - public void setPlausibilityThreshold(final float threshold) { - mPlausibilityThreshold = threshold; - } - - public interface OnGetSuggestedWordsCallback { - public void onGetSuggestedWords(final SuggestedWords suggestedWords); - } - - public void getSuggestedWords(final WordComposer wordComposer, - final NgramContext ngramContext, final Keyboard keyboard, - final SettingsValuesForSuggestion settingsValuesForSuggestion, - final boolean isCorrectionEnabled, final int inputStyle, final int sequenceNumber, - final OnGetSuggestedWordsCallback callback) { - if (wordComposer.isBatchMode()) { - getSuggestedWordsForBatchInput(wordComposer, ngramContext, keyboard, - settingsValuesForSuggestion, inputStyle, sequenceNumber, callback); - } else { - getSuggestedWordsForNonBatchInput(wordComposer, ngramContext, keyboard, - settingsValuesForSuggestion, inputStyle, isCorrectionEnabled, - sequenceNumber, callback); - } - } - - private static ArrayList<SuggestedWordInfo> getTransformedSuggestedWordInfoList( - final WordComposer wordComposer, final SuggestionResults results, - final int trailingSingleQuotesCount, final Locale defaultLocale) { - final boolean shouldMakeSuggestionsAllUpperCase = wordComposer.isAllUpperCase() - && !wordComposer.isResumed(); - final boolean isOnlyFirstCharCapitalized = - wordComposer.isOrWillBeOnlyFirstCharCapitalized(); - - final ArrayList<SuggestedWordInfo> suggestionsContainer = new ArrayList<>(results); - final int suggestionsCount = suggestionsContainer.size(); - if (isOnlyFirstCharCapitalized || shouldMakeSuggestionsAllUpperCase - || 0 != trailingSingleQuotesCount) { - for (int i = 0; i < suggestionsCount; ++i) { - final SuggestedWordInfo wordInfo = suggestionsContainer.get(i); - final Locale wordLocale = wordInfo.mSourceDict.mLocale; - final SuggestedWordInfo transformedWordInfo = getTransformedSuggestedWordInfo( - wordInfo, null == wordLocale ? defaultLocale : wordLocale, - shouldMakeSuggestionsAllUpperCase, isOnlyFirstCharCapitalized, - trailingSingleQuotesCount); - suggestionsContainer.set(i, transformedWordInfo); - } - } - return suggestionsContainer; - } - - private static SuggestedWordInfo getWhitelistedWordInfoOrNull( - @Nonnull final ArrayList<SuggestedWordInfo> suggestions) { - if (suggestions.isEmpty()) { - return null; - } - final SuggestedWordInfo firstSuggestedWordInfo = suggestions.get(0); - if (!firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) { - return null; - } - return firstSuggestedWordInfo; - } - - // Retrieves suggestions for non-batch input (typing, recorrection, predictions...) - // and calls the callback function with the suggestions. - private void getSuggestedWordsForNonBatchInput(final WordComposer wordComposer, - final NgramContext ngramContext, final Keyboard keyboard, - final SettingsValuesForSuggestion settingsValuesForSuggestion, - final int inputStyleIfNotPrediction, final boolean isCorrectionEnabled, - final int sequenceNumber, final OnGetSuggestedWordsCallback callback) { - final String typedWordString = wordComposer.getTypedWord(); - final int trailingSingleQuotesCount = - StringUtils.getTrailingSingleQuotesCount(typedWordString); - final String consideredWord = trailingSingleQuotesCount > 0 - ? typedWordString.substring(0, typedWordString.length() - trailingSingleQuotesCount) - : typedWordString; - - final SuggestionResults suggestionResults = mDictionaryFacilitator.getSuggestionResults( - wordComposer.getComposedDataSnapshot(), ngramContext, keyboard, - settingsValuesForSuggestion, SESSION_ID_TYPING, inputStyleIfNotPrediction); - final Locale locale = mDictionaryFacilitator.getLocale(); - final ArrayList<SuggestedWordInfo> suggestionsContainer = - getTransformedSuggestedWordInfoList(wordComposer, suggestionResults, - trailingSingleQuotesCount, locale); - - boolean foundInDictionary = false; - Dictionary sourceDictionaryOfRemovedWord = null; - for (final SuggestedWordInfo info : suggestionsContainer) { - // Search for the best dictionary, defined as the first one with the highest match - // quality we can find. - if (!foundInDictionary && typedWordString.equals(info.mWord)) { - // Use this source if the old match had lower quality than this match - sourceDictionaryOfRemovedWord = info.mSourceDict; - foundInDictionary = true; - break; - } - } - - final int firstOcurrenceOfTypedWordInSuggestions = - SuggestedWordInfo.removeDups(typedWordString, suggestionsContainer); - - final SuggestedWordInfo whitelistedWordInfo = - getWhitelistedWordInfoOrNull(suggestionsContainer); - final String whitelistedWord = whitelistedWordInfo == null - ? null : whitelistedWordInfo.mWord; - final boolean resultsArePredictions = !wordComposer.isComposingWord(); - - // We allow auto-correction if whitelisting is not required or the word is whitelisted, - // or if the word had more than one char and was not suggested. - final boolean allowsToBeAutoCorrected = - (SHOULD_AUTO_CORRECT_USING_NON_WHITE_LISTED_SUGGESTION || whitelistedWord != null) - || (consideredWord.length() > 1 && (sourceDictionaryOfRemovedWord == null)); - - final boolean hasAutoCorrection; - // If correction is not enabled, we never auto-correct. This is for example for when - // the setting "Auto-correction" is "off": we still suggest, but we don't auto-correct. - if (!isCorrectionEnabled - // If the word does not allow to be auto-corrected, then we don't auto-correct. - || !allowsToBeAutoCorrected - // If we are doing prediction, then we never auto-correct of course - || resultsArePredictions - // If we don't have suggestion results, we can't evaluate the first suggestion - // for auto-correction - || suggestionResults.isEmpty() - // If the word has digits, we never auto-correct because it's likely the word - // was type with a lot of care - || wordComposer.hasDigits() - // If the word is mostly caps, we never auto-correct because this is almost - // certainly intentional (and careful input) - || wordComposer.isMostlyCaps() - // We never auto-correct when suggestions are resumed because it would be unexpected - || wordComposer.isResumed() - // 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 types in English with no dictionary and has a "Will" in their contact - // list, "will" would always auto-correct to "Will" which is unwanted. Hence, no - // main dict => no auto-correct. Also, it would probably get obnoxious quickly. - // TODO: now that we have personalization, we may want to re-evaluate this decision - || !mDictionaryFacilitator.hasAtLeastOneInitializedMainDictionary() - // If the first suggestion is a shortcut we never auto-correct to it, regardless - // of how strong it is (allowlist entries are not KIND_SHORTCUT but KIND_WHITELIST). - // TODO: we may want to have shortcut-only entries auto-correct in the future. - || suggestionResults.first().isKindOf(SuggestedWordInfo.KIND_SHORTCUT)) { - hasAutoCorrection = false; - } else { - final SuggestedWordInfo firstSuggestion = suggestionResults.first(); - if (suggestionResults.mFirstSuggestionExceedsConfidenceThreshold - && firstOcurrenceOfTypedWordInSuggestions != 0) { - hasAutoCorrection = true; - } else if (!AutoCorrectionUtils.suggestionExceedsThreshold( - firstSuggestion, consideredWord, mAutoCorrectionThreshold)) { - // Score is too low for autocorrect - hasAutoCorrection = false; - } else { - // We have a high score, so we need to check if this suggestion is in the correct - // form to allow auto-correcting to it in this language. For details of how this - // is determined, see #isAllowedByAutoCorrectionWithSpaceFilter. - // TODO: this should not have its own logic here but be handled by the dictionary. - hasAutoCorrection = isAllowedByAutoCorrectionWithSpaceFilter(firstSuggestion); - } - } - - final SuggestedWordInfo typedWordInfo = new SuggestedWordInfo(typedWordString, - "" /* prevWordsContext */, SuggestedWordInfo.MAX_SCORE, - SuggestedWordInfo.KIND_TYPED, - null == sourceDictionaryOfRemovedWord ? Dictionary.DICTIONARY_USER_TYPED - : sourceDictionaryOfRemovedWord, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); - if (!TextUtils.isEmpty(typedWordString)) { - suggestionsContainer.add(0, typedWordInfo); - } - - final ArrayList<SuggestedWordInfo> suggestionsList; - if (DBG && !suggestionsContainer.isEmpty()) { - suggestionsList = getSuggestionsInfoListWithDebugInfo(typedWordString, - suggestionsContainer); - } else { - suggestionsList = suggestionsContainer; - } - - final int inputStyle; - if (resultsArePredictions) { - inputStyle = suggestionResults.mIsBeginningOfSentence - ? SuggestedWords.INPUT_STYLE_BEGINNING_OF_SENTENCE_PREDICTION - : SuggestedWords.INPUT_STYLE_PREDICTION; - } else { - inputStyle = inputStyleIfNotPrediction; - } - - final boolean isTypedWordValid = firstOcurrenceOfTypedWordInSuggestions > -1 - || (!resultsArePredictions && !allowsToBeAutoCorrected); - callback.onGetSuggestedWords(new SuggestedWords(suggestionsList, - suggestionResults.mRawSuggestions, typedWordInfo, - isTypedWordValid, - hasAutoCorrection /* willAutoCorrect */, - false /* isObsoleteSuggestions */, inputStyle, sequenceNumber)); - } - - // Retrieves suggestions for the batch input - // and calls the callback function with the suggestions. - private void getSuggestedWordsForBatchInput(final WordComposer wordComposer, - final NgramContext ngramContext, final Keyboard keyboard, - final SettingsValuesForSuggestion settingsValuesForSuggestion, - final int inputStyle, final int sequenceNumber, - final OnGetSuggestedWordsCallback callback) { - final SuggestionResults suggestionResults = mDictionaryFacilitator.getSuggestionResults( - wordComposer.getComposedDataSnapshot(), ngramContext, keyboard, - settingsValuesForSuggestion, SESSION_ID_GESTURE, inputStyle); - // For transforming words that don't come from a dictionary, because it's our best bet - final Locale locale = mDictionaryFacilitator.getLocale(); - final ArrayList<SuggestedWordInfo> suggestionsContainer = - new ArrayList<>(suggestionResults); - final int suggestionsCount = suggestionsContainer.size(); - final boolean isFirstCharCapitalized = wordComposer.wasShiftedNoLock(); - final boolean isAllUpperCase = wordComposer.isAllUpperCase(); - if (isFirstCharCapitalized || isAllUpperCase) { - for (int i = 0; i < suggestionsCount; ++i) { - final SuggestedWordInfo wordInfo = suggestionsContainer.get(i); - final Locale wordlocale = wordInfo.mSourceDict.mLocale; - final SuggestedWordInfo transformedWordInfo = getTransformedSuggestedWordInfo( - wordInfo, null == wordlocale ? locale : wordlocale, isAllUpperCase, - isFirstCharCapitalized, 0 /* trailingSingleQuotesCount */); - suggestionsContainer.set(i, transformedWordInfo); - } - } - - if (SHOULD_REMOVE_PREVIOUSLY_REJECTED_SUGGESTION - && suggestionsContainer.size() > 1 - && TextUtils.equals(suggestionsContainer.get(0).mWord, - wordComposer.getRejectedBatchModeSuggestion())) { - final SuggestedWordInfo rejected = suggestionsContainer.remove(0); - suggestionsContainer.add(1, rejected); - } - SuggestedWordInfo.removeDups(null /* typedWord */, suggestionsContainer); - - // For some reason some suggestions with MIN_VALUE are making their way here. - // TODO: Find a more robust way to detect distracters. - for (int i = suggestionsContainer.size() - 1; i >= 0; --i) { - if (suggestionsContainer.get(i).mScore < SUPPRESS_SUGGEST_THRESHOLD) { - suggestionsContainer.remove(i); - } - } - - // In the batch input mode, the most relevant suggested word should act as a "typed word" - // (typedWordValid=true), not as an "auto correct word" (willAutoCorrect=false). - // Note that because this method is never used to get predictions, there is no need to - // modify inputType such in getSuggestedWordsForNonBatchInput. - final SuggestedWordInfo pseudoTypedWordInfo = suggestionsContainer.isEmpty() ? null - : suggestionsContainer.get(0); - - callback.onGetSuggestedWords(new SuggestedWords(suggestionsContainer, - suggestionResults.mRawSuggestions, - pseudoTypedWordInfo, - true /* typedWordValid */, - false /* willAutoCorrect */, - false /* isObsoleteSuggestions */, - inputStyle, sequenceNumber)); - } - - private static ArrayList<SuggestedWordInfo> getSuggestionsInfoListWithDebugInfo( - final String typedWord, final ArrayList<SuggestedWordInfo> suggestions) { - final SuggestedWordInfo typedWordInfo = suggestions.get(0); - typedWordInfo.setDebugString("+"); - final int suggestionsSize = suggestions.size(); - final ArrayList<SuggestedWordInfo> suggestionsList = new ArrayList<>(suggestionsSize); - suggestionsList.add(typedWordInfo); - // Note: i here is the index in mScores[], but the index in mSuggestions is one more - // than i because we added the typed word to mSuggestions without touching mScores. - for (int i = 0; i < suggestionsSize - 1; ++i) { - final SuggestedWordInfo cur = suggestions.get(i + 1); - final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore( - typedWord, cur.toString(), cur.mScore); - final String scoreInfoString; - if (normalizedScore > 0) { - scoreInfoString = String.format( - Locale.ROOT, "%d (%4.2f), %s", cur.mScore, normalizedScore, - cur.mSourceDict.mDictType); - } else { - scoreInfoString = Integer.toString(cur.mScore); - } - cur.setDebugString(scoreInfoString); - suggestionsList.add(cur); - } - return suggestionsList; - } - - /** - * Computes whether this suggestion should be blocked or not in this language - * - * This function implements a filter that avoids auto-correcting to suggestions that contain - * spaces that are above a certain language-dependent character limit. In languages like German - * where it's possible to concatenate many words, it often happens our dictionary does not - * have the longer words. In this case, we offer a lot of unhelpful suggestions that contain - * one or several spaces. Ideally we should understand what the user wants and display useful - * suggestions by improving the dictionary and possibly having some specific logic. Until - * that's possible we should avoid displaying unhelpful suggestions. But it's hard to tell - * whether a suggestion is useful or not. So at least for the time being we block - * auto-correction when the suggestion is long and contains a space, which should avoid the - * worst damage. - * This function is implementing that filter. If the language enforces no such limit, then it - * always returns true. If the suggestion contains no space, it also returns true. Otherwise, - * it checks the length against the language-specific limit. - * - * @param info the suggestion info - * @return whether it's fine to auto-correct to this. - */ - private static boolean isAllowedByAutoCorrectionWithSpaceFilter(final SuggestedWordInfo info) { - final Locale locale = info.mSourceDict.mLocale; - if (null == locale) { - return true; - } - final Integer maximumLengthForThisLanguage = - sLanguageToMaximumAutoCorrectionWithSpaceLength.get(locale.getLanguage()); - if (null == maximumLengthForThisLanguage) { - // This language does not enforce a maximum length to auto-correction - return true; - } - return info.mWord.length() <= maximumLengthForThisLanguage - || -1 == info.mWord.indexOf(Constants.CODE_SPACE); - } - - /* package for test */ static SuggestedWordInfo getTransformedSuggestedWordInfo( - final SuggestedWordInfo wordInfo, final Locale locale, final boolean isAllUpperCase, - final boolean isOnlyFirstCharCapitalized, final int trailingSingleQuotesCount) { - final StringBuilder sb = new StringBuilder(wordInfo.mWord.length()); - if (isAllUpperCase) { - sb.append(wordInfo.mWord.toUpperCase(locale)); - } else if (isOnlyFirstCharCapitalized) { - sb.append(StringUtils.capitalizeFirstCodePoint(wordInfo.mWord, locale)); - } else { - sb.append(wordInfo.mWord); - } - // Appending quotes is here to help people quote words. However, it's not helpful - // when they type words with quotes toward the end like "it's" or "didn't", where - // it's more likely the user missed the last character (or didn't type it yet). - final int quotesToAppend = trailingSingleQuotesCount - - (-1 == wordInfo.mWord.indexOf(Constants.CODE_SINGLE_QUOTE) ? 0 : 1); - for (int i = quotesToAppend - 1; i >= 0; --i) { - sb.appendCodePoint(Constants.CODE_SINGLE_QUOTE); - } - return new SuggestedWordInfo(sb.toString(), wordInfo.mPrevWordsContext, - wordInfo.mScore, wordInfo.mKindAndFlags, - wordInfo.mSourceDict, wordInfo.mIndexOfTouchPointOfSecondWord, - wordInfo.mAutoCommitFirstWordConfidence); - } -} diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java deleted file mode 100644 index bcd4d5f69..000000000 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ /dev/null @@ -1,448 +0,0 @@ -/* - * Copyright (C) 2010 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.text.TextUtils; -import android.view.inputmethod.CompletionInfo; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.define.DebugFlags; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -public class SuggestedWords { - public static final int INDEX_OF_TYPED_WORD = 0; - public static final int INDEX_OF_AUTO_CORRECTION = 1; - public static final int NOT_A_SEQUENCE_NUMBER = -1; - - public static final int INPUT_STYLE_NONE = 0; - public static final int INPUT_STYLE_TYPING = 1; - public static final int INPUT_STYLE_UPDATE_BATCH = 2; - public static final int INPUT_STYLE_TAIL_BATCH = 3; - public static final int INPUT_STYLE_APPLICATION_SPECIFIED = 4; - public static final int INPUT_STYLE_RECORRECTION = 5; - public static final int INPUT_STYLE_PREDICTION = 6; - public static final int INPUT_STYLE_BEGINNING_OF_SENTENCE_PREDICTION = 7; - - // The maximum number of suggestions available. - public static final int MAX_SUGGESTIONS = 18; - - private static final ArrayList<SuggestedWordInfo> EMPTY_WORD_INFO_LIST = new ArrayList<>(0); - @Nonnull - private static final SuggestedWords EMPTY = new SuggestedWords( - EMPTY_WORD_INFO_LIST, null /* rawSuggestions */, null /* typedWord */, - false /* typedWordValid */, false /* willAutoCorrect */, - false /* isObsoleteSuggestions */, INPUT_STYLE_NONE, NOT_A_SEQUENCE_NUMBER); - - @Nullable - public final SuggestedWordInfo mTypedWordInfo; - public final boolean mTypedWordValid; - // Note: this INCLUDES cases where the word will auto-correct to itself. A good definition - // of what this flag means would be "the top suggestion is strong enough to auto-correct", - // whether this exactly matches the user entry or not. - public final boolean mWillAutoCorrect; - public final boolean mIsObsoleteSuggestions; - // How the input for these suggested words was done by the user. Must be one of the - // INPUT_STYLE_* constants above. - public final int mInputStyle; - public final int mSequenceNumber; // Sequence number for auto-commit. - @Nonnull - protected final ArrayList<SuggestedWordInfo> mSuggestedWordInfoList; - @Nullable - public final ArrayList<SuggestedWordInfo> mRawSuggestions; - - public SuggestedWords(@Nonnull final ArrayList<SuggestedWordInfo> suggestedWordInfoList, - @Nullable final ArrayList<SuggestedWordInfo> rawSuggestions, - @Nullable final SuggestedWordInfo typedWordInfo, - final boolean typedWordValid, - final boolean willAutoCorrect, - final boolean isObsoleteSuggestions, - final int inputStyle, - final int sequenceNumber) { - mSuggestedWordInfoList = suggestedWordInfoList; - mRawSuggestions = rawSuggestions; - mTypedWordValid = typedWordValid; - mWillAutoCorrect = willAutoCorrect; - mIsObsoleteSuggestions = isObsoleteSuggestions; - mInputStyle = inputStyle; - mSequenceNumber = sequenceNumber; - mTypedWordInfo = typedWordInfo; - } - - public boolean isEmpty() { - return mSuggestedWordInfoList.isEmpty(); - } - - public int size() { - return mSuggestedWordInfoList.size(); - } - - /** - * Get suggested word to show as suggestions to UI. - * - * @param shouldShowLxxSuggestionUi true if showing suggestion UI introduced in LXX and later. - * @return the count of suggested word to show as suggestions to UI. - */ - public int getWordCountToShow(final boolean shouldShowLxxSuggestionUi) { - if (isPrediction() || !shouldShowLxxSuggestionUi) { - return size(); - } - return size() - /* typed word */ 1; - } - - /** - * Get {@link SuggestedWordInfo} object for the typed word. - * @return The {@link SuggestedWordInfo} object for the typed word. - */ - public SuggestedWordInfo getTypedWordInfo() { - return mTypedWordInfo; - } - - /** - * Get suggested word at <code>index</code>. - * @param index The index of the suggested word. - * @return The suggested word. - */ - public String getWord(final int index) { - return mSuggestedWordInfoList.get(index).mWord; - } - - /** - * Get displayed text at <code>index</code>. - * In RTL languages, the displayed text on the suggestion strip may be different from the - * suggested word that is returned from {@link #getWord(int)}. For example the displayed text - * of punctuation suggestion "(" should be ")". - * @param index The index of the text to display. - * @return The text to be displayed. - */ - public String getLabel(final int index) { - return mSuggestedWordInfoList.get(index).mWord; - } - - /** - * Get {@link SuggestedWordInfo} object at <code>index</code>. - * @param index The index of the {@link SuggestedWordInfo}. - * @return The {@link SuggestedWordInfo} object. - */ - public SuggestedWordInfo getInfo(final int index) { - return mSuggestedWordInfoList.get(index); - } - - /** - * Gets the suggestion index from the suggestions list. - * @param suggestedWordInfo The {@link SuggestedWordInfo} to find the index. - * @return The position of the suggestion in the suggestion list. - */ - public int indexOf(SuggestedWordInfo suggestedWordInfo) { - return mSuggestedWordInfoList.indexOf(suggestedWordInfo); - } - - public String getDebugString(final int pos) { - if (!DebugFlags.DEBUG_ENABLED) { - return null; - } - final SuggestedWordInfo wordInfo = getInfo(pos); - if (wordInfo == null) { - return null; - } - final String debugString = wordInfo.getDebugString(); - if (TextUtils.isEmpty(debugString)) { - return null; - } - return debugString; - } - - /** - * The predicator to tell whether this object represents punctuation suggestions. - * @return false if this object desn't represent punctuation suggestions. - */ - public boolean isPunctuationSuggestions() { - return false; - } - - @Override - public String toString() { - // Pretty-print method to help debug - return "SuggestedWords:" - + " mTypedWordValid=" + mTypedWordValid - + " mWillAutoCorrect=" + mWillAutoCorrect - + " mInputStyle=" + mInputStyle - + " words=" + Arrays.toString(mSuggestedWordInfoList.toArray()); - } - - public static ArrayList<SuggestedWordInfo> getFromApplicationSpecifiedCompletions( - final CompletionInfo[] infos) { - final ArrayList<SuggestedWordInfo> result = new ArrayList<>(); - for (final CompletionInfo info : infos) { - if (null == info || null == info.getText()) { - continue; - } - result.add(new SuggestedWordInfo(info)); - } - return result; - } - - @Nonnull - public static final SuggestedWords getEmptyInstance() { - return SuggestedWords.EMPTY; - } - - // Should get rid of the first one (what the user typed previously) from suggestions - // and replace it with what the user currently typed. - public static ArrayList<SuggestedWordInfo> getTypedWordAndPreviousSuggestions( - @Nonnull final SuggestedWordInfo typedWordInfo, - @Nonnull final SuggestedWords previousSuggestions) { - final ArrayList<SuggestedWordInfo> suggestionsList = new ArrayList<>(); - final HashSet<String> alreadySeen = new HashSet<>(); - suggestionsList.add(typedWordInfo); - alreadySeen.add(typedWordInfo.mWord); - final int previousSize = previousSuggestions.size(); - for (int index = 1; index < previousSize; index++) { - final SuggestedWordInfo prevWordInfo = previousSuggestions.getInfo(index); - final String prevWord = prevWordInfo.mWord; - // Filter out duplicate suggestions. - if (!alreadySeen.contains(prevWord)) { - suggestionsList.add(prevWordInfo); - alreadySeen.add(prevWord); - } - } - return suggestionsList; - } - - public SuggestedWordInfo getAutoCommitCandidate() { - if (mSuggestedWordInfoList.size() <= 0) return null; - final SuggestedWordInfo candidate = mSuggestedWordInfoList.get(0); - return candidate.isEligibleForAutoCommit() ? candidate : null; - } - - // non-final for testability. - public static class SuggestedWordInfo { - public static final int NOT_AN_INDEX = -1; - public static final int NOT_A_CONFIDENCE = -1; - public static final int MAX_SCORE = Integer.MAX_VALUE; - - private static final int KIND_MASK_KIND = 0xFF; // Mask to get only the kind - public static final int KIND_TYPED = 0; // What user typed - public static final int KIND_CORRECTION = 1; // Simple correction/suggestion - public static final int KIND_COMPLETION = 2; // Completion (suggestion with appended chars) - public static final int KIND_WHITELIST = 3; // Whitelisted word - public static final int KIND_BLACKLIST = 4; // Blacklisted word - 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) - // KIND_RESUMED: A resumed suggestion (comes from a span, currently this type is used only - // in java for re-correction) - public static final int KIND_RESUMED = 9; - public static final int KIND_OOV_CORRECTION = 10; // Most probable string correction - - public static final int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; - public static final int KIND_FLAG_EXACT_MATCH = 0x40000000; - public static final int KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION = 0x20000000; - public static final int KIND_FLAG_APPROPRIATE_FOR_AUTO_CORRECTION = 0x10000000; - - public final String mWord; - public final String mPrevWordsContext; - // The completion info from the application. Null for suggestions that don't come from - // the application (including keyboard-computed ones, so this is almost always null) - public final CompletionInfo mApplicationSpecifiedCompletionInfo; - public final int mScore; - public final int mKindAndFlags; - public final int mCodePointCount; - @Deprecated - public final Dictionary mSourceDict; - // For auto-commit. This keeps track of the index inside the touch coordinates array - // passed to native code to get suggestions for a gesture that corresponds to the first - // letter of the second word. - public final int mIndexOfTouchPointOfSecondWord; - // For auto-commit. This is a measure of how confident we are that we can commit the - // first word of this suggestion. - public final int mAutoCommitFirstWordConfidence; - private String mDebugString = ""; - - /** - * Create a new suggested word info. - * @param word The string to suggest. - * @param prevWordsContext previous words context. - * @param score A measure of how likely this suggestion is. - * @param kindAndFlags The kind of suggestion, as one of the above KIND_* constants with - * flags. - * @param sourceDict What instance of Dictionary produced this suggestion. - * @param indexOfTouchPointOfSecondWord See mIndexOfTouchPointOfSecondWord. - * @param autoCommitFirstWordConfidence See mAutoCommitFirstWordConfidence. - */ - public SuggestedWordInfo(final String word, final String prevWordsContext, - final int score, final int kindAndFlags, - final Dictionary sourceDict, final int indexOfTouchPointOfSecondWord, - final int autoCommitFirstWordConfidence) { - mWord = word; - mPrevWordsContext = prevWordsContext; - mApplicationSpecifiedCompletionInfo = null; - mScore = score; - mKindAndFlags = kindAndFlags; - mSourceDict = sourceDict; - mCodePointCount = StringUtils.codePointCount(mWord); - mIndexOfTouchPointOfSecondWord = indexOfTouchPointOfSecondWord; - mAutoCommitFirstWordConfidence = autoCommitFirstWordConfidence; - } - - /** - * Create a new suggested word info from an application-specified completion. - * If the passed argument or its contained text is null, this throws a NPE. - * @param applicationSpecifiedCompletion The application-specified completion info. - */ - public SuggestedWordInfo(final CompletionInfo applicationSpecifiedCompletion) { - mWord = applicationSpecifiedCompletion.getText().toString(); - mPrevWordsContext = ""; - mApplicationSpecifiedCompletionInfo = applicationSpecifiedCompletion; - mScore = SuggestedWordInfo.MAX_SCORE; - mKindAndFlags = SuggestedWordInfo.KIND_APP_DEFINED; - mSourceDict = Dictionary.DICTIONARY_APPLICATION_DEFINED; - mCodePointCount = StringUtils.codePointCount(mWord); - mIndexOfTouchPointOfSecondWord = SuggestedWordInfo.NOT_AN_INDEX; - mAutoCommitFirstWordConfidence = SuggestedWordInfo.NOT_A_CONFIDENCE; - } - - public boolean isEligibleForAutoCommit() { - return (isKindOf(KIND_CORRECTION) && NOT_AN_INDEX != mIndexOfTouchPointOfSecondWord); - } - - public int getKind() { - return (mKindAndFlags & KIND_MASK_KIND); - } - - public boolean isKindOf(final int kind) { - return getKind() == kind; - } - - public boolean isPossiblyOffensive() { - return (mKindAndFlags & KIND_FLAG_POSSIBLY_OFFENSIVE) != 0; - } - - public boolean isExactMatch() { - return (mKindAndFlags & KIND_FLAG_EXACT_MATCH) != 0; - } - - public boolean isExactMatchWithIntentionalOmission() { - return (mKindAndFlags & KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION) != 0; - } - - public boolean isAprapreateForAutoCorrection() { - return (mKindAndFlags & KIND_FLAG_APPROPRIATE_FOR_AUTO_CORRECTION) != 0; - } - - public void setDebugString(final String str) { - if (null == str) throw new NullPointerException("Debug info is null"); - mDebugString = str; - } - - public String getDebugString() { - return mDebugString; - } - - public String getWord() { - return mWord; - } - - @Deprecated - public Dictionary getSourceDictionary() { - return mSourceDict; - } - - public int codePointAt(int i) { - return mWord.codePointAt(i); - } - - @Override - public String toString() { - if (TextUtils.isEmpty(mDebugString)) { - return mWord; - } - return mWord + " (" + mDebugString + ")"; - } - - /** - * This will always remove the higher index if a duplicate is found. - * - * @return position of typed word in the candidate list - */ - public static int removeDups( - @Nullable final String typedWord, - @Nonnull final ArrayList<SuggestedWordInfo> candidates) { - if (candidates.isEmpty()) { - return -1; - } - int firstOccurrenceOfWord = -1; - if (!TextUtils.isEmpty(typedWord)) { - firstOccurrenceOfWord = removeSuggestedWordInfoFromList( - typedWord, candidates, -1 /* startIndexExclusive */); - } - for (int i = 0; i < candidates.size(); ++i) { - removeSuggestedWordInfoFromList( - candidates.get(i).mWord, candidates, i /* startIndexExclusive */); - } - return firstOccurrenceOfWord; - } - - private static int removeSuggestedWordInfoFromList( - @Nonnull final String word, - @Nonnull final ArrayList<SuggestedWordInfo> candidates, - final int startIndexExclusive) { - int firstOccurrenceOfWord = -1; - for (int i = startIndexExclusive + 1; i < candidates.size(); ++i) { - final SuggestedWordInfo previous = candidates.get(i); - if (word.equals(previous.mWord)) { - if (firstOccurrenceOfWord == -1) { - firstOccurrenceOfWord = i; - } - candidates.remove(i); - --i; - } - } - return firstOccurrenceOfWord; - } - } - - private static boolean isPrediction(final int inputStyle) { - return INPUT_STYLE_PREDICTION == inputStyle - || INPUT_STYLE_BEGINNING_OF_SENTENCE_PREDICTION == inputStyle; - } - - public boolean isPrediction() { - return isPrediction(mInputStyle); - } - - /** - * @return the {@link SuggestedWordInfo} which corresponds to the word that is originally - * typed by the user. Otherwise returns {@code null}. Note that gesture input is not - * considered to be a typed word. - */ - @UsedForTesting - public SuggestedWordInfo getTypedWordInfoOrNull() { - if (SuggestedWords.INDEX_OF_TYPED_WORD >= size()) { - return null; - } - final SuggestedWordInfo info = getInfo(SuggestedWords.INDEX_OF_TYPED_WORD); - return (info.getKind() == SuggestedWordInfo.KIND_TYPED) ? info : null; - } -} diff --git a/java/src/com/android/inputmethod/latin/SystemBroadcastReceiver.java b/java/src/com/android/inputmethod/latin/SystemBroadcastReceiver.java deleted file mode 100644 index 90221512f..000000000 --- a/java/src/com/android/inputmethod/latin/SystemBroadcastReceiver.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (C) 2014 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.app.DownloadManager; -import android.content.BroadcastReceiver; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.database.Cursor; -import android.os.Process; -import android.preference.PreferenceManager; -import android.util.Log; -import android.view.inputmethod.InputMethodManager; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.dictionarypack.DictionaryPackConstants; -import com.android.inputmethod.dictionarypack.DownloadManagerWrapper; -import com.android.inputmethod.keyboard.KeyboardLayoutSet; -import com.android.inputmethod.latin.settings.Settings; -import com.android.inputmethod.latin.setup.SetupActivity; -import com.android.inputmethod.latin.utils.UncachedInputMethodManagerUtils; - -/** - * This class detects the {@link Intent#ACTION_MY_PACKAGE_REPLACED} broadcast intent when this IME - * package has been replaced by a newer version of the same package. This class also detects - * {@link Intent#ACTION_BOOT_COMPLETED} and {@link Intent#ACTION_USER_INITIALIZE} broadcast intent. - * - * If this IME has already been installed in the system image and a new version of this IME has - * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received by this receiver and it - * will hide the setup wizard's icon. - * - * If this IME has already been installed in the data partition and a new version of this IME has - * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received by this receiver but it - * will not hide the setup wizard's icon, and the icon will appear on the launcher. - * - * If this IME hasn't been installed yet and has been newly installed, no - * {@link Intent#ACTION_MY_PACKAGE_REPLACED} will be sent and the setup wizard's icon will appear - * on the launcher. - * - * When the device has been booted, {@link Intent#ACTION_BOOT_COMPLETED} is received by this - * receiver and it checks whether the setup wizard's icon should be appeared or not on the launcher - * depending on which partition this IME is installed. - * - * When the system locale has been changed, {@link Intent#ACTION_LOCALE_CHANGED} is received by - * this receiver and the {@link KeyboardLayoutSet}'s cache is cleared. - */ -public final class SystemBroadcastReceiver extends BroadcastReceiver { - private static final String TAG = SystemBroadcastReceiver.class.getSimpleName(); - - @Override - public void onReceive(final Context context, final Intent intent) { - final String intentAction = intent.getAction(); - if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intentAction)) { - Log.i(TAG, "Package has been replaced: " + context.getPackageName()); - // Need to restore additional subtypes because system always clears additional - // subtypes when the package is replaced. - RichInputMethodManager.init(context); - final RichInputMethodManager richImm = RichInputMethodManager.getInstance(); - final InputMethodSubtype[] additionalSubtypes = richImm.getAdditionalSubtypes(); - richImm.setAdditionalInputMethodSubtypes(additionalSubtypes); - toggleAppIcon(context); - - // Remove all the previously scheduled downloads. This will also makes sure - // that any erroneously stuck downloads will get cleared. (b/21797386) - removeOldDownloads(context); - // b/21797386 - // downloadLatestDictionaries(context); - } else if (Intent.ACTION_BOOT_COMPLETED.equals(intentAction)) { - Log.i(TAG, "Boot has been completed"); - toggleAppIcon(context); - } else if (Intent.ACTION_LOCALE_CHANGED.equals(intentAction)) { - Log.i(TAG, "System locale changed"); - KeyboardLayoutSet.onSystemLocaleChanged(); - } - - // The process that hosts this broadcast receiver is invoked and remains alive even after - // 1) the package has been re-installed, - // 2) the device has just booted, - // 3) a new user has been created. - // There is no good reason to keep the process alive if this IME isn't a current IME. - final InputMethodManager imm = (InputMethodManager) - context.getSystemService(Context.INPUT_METHOD_SERVICE); - // Called to check whether this IME has been triggered by the current user or not - final boolean isInputMethodManagerValidForUserOfThisProcess = - !imm.getInputMethodList().isEmpty(); - final boolean isCurrentImeOfCurrentUser = isInputMethodManagerValidForUserOfThisProcess - && UncachedInputMethodManagerUtils.isThisImeCurrent(context, imm); - if (!isCurrentImeOfCurrentUser) { - final int myPid = Process.myPid(); - Log.i(TAG, "Killing my process: pid=" + myPid); - Process.killProcess(myPid); - } - } - - private void removeOldDownloads(Context context) { - try { - Log.i(TAG, "Removing the old downloads in progress of the previous keyboard version."); - final DownloadManagerWrapper downloadManagerWrapper = new DownloadManagerWrapper( - context); - final DownloadManager.Query q = new DownloadManager.Query(); - // Query all the download statuses except the succeeded ones. - q.setFilterByStatus(DownloadManager.STATUS_FAILED - | DownloadManager.STATUS_PAUSED - | DownloadManager.STATUS_PENDING - | DownloadManager.STATUS_RUNNING); - final Cursor c = downloadManagerWrapper.query(q); - if (c != null) { - for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { - final long downloadId = c - .getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)); - downloadManagerWrapper.remove(downloadId); - Log.i(TAG, "Removed the download with Id: " + downloadId); - } - c.close(); - } - } catch (Exception e) { - Log.e(TAG, "Exception while removing old downloads."); - } - } - - private void downloadLatestDictionaries(Context context) { - final Intent updateIntent = new Intent( - DictionaryPackConstants.INIT_AND_UPDATE_NOW_INTENT_ACTION); - context.sendBroadcast(updateIntent); - } - - public static void toggleAppIcon(final Context context) { - final int appInfoFlags = context.getApplicationInfo().flags; - final boolean isSystemApp = (appInfoFlags & ApplicationInfo.FLAG_SYSTEM) > 0; - if (Log.isLoggable(TAG, Log.INFO)) { - Log.i(TAG, "toggleAppIcon() : FLAG_SYSTEM = " + isSystemApp); - } - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - context.getPackageManager().setComponentEnabledSetting( - new ComponentName(context, SetupActivity.class), - Settings.readShowSetupWizardIcon(prefs, context) - ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED - : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, - PackageManager.DONT_KILL_APP); - } -} diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java deleted file mode 100644 index fe24ccfc2..000000000 --- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * 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.ContentResolver; -import android.content.Context; -import android.database.ContentObserver; -import android.database.Cursor; -import android.database.sqlite.SQLiteException; -import android.net.Uri; -import android.provider.UserDictionary.Words; -import android.text.TextUtils; -import android.util.Log; - -import com.android.inputmethod.annotations.ExternallyReferenced; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; - -import java.io.File; -import java.util.Arrays; -import java.util.Locale; - -import javax.annotation.Nullable; - -/** - * An expandable dictionary that stores the words in the user dictionary provider into a binary - * dictionary file to use it from native code. - */ -public class UserBinaryDictionary extends ExpandableBinaryDictionary { - private static final String TAG = ExpandableBinaryDictionary.class.getSimpleName(); - - // The user dictionary provider uses an empty string to mean "all languages". - private static final String USER_DICTIONARY_ALL_LANGUAGES = ""; - private static final int HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY = 250; - private static final int LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY = 160; - - private static final String[] PROJECTION_QUERY = new String[] {Words.WORD, Words.FREQUENCY}; - - private static final String NAME = "userunigram"; - - private ContentObserver mObserver; - final private String mLocaleString; - final private boolean mAlsoUseMoreRestrictiveLocales; - - protected UserBinaryDictionary(final Context context, final Locale locale, - final boolean alsoUseMoreRestrictiveLocales, - final File dictFile, final String name) { - super(context, getDictName(name, locale, dictFile), locale, Dictionary.TYPE_USER, dictFile); - if (null == locale) throw new NullPointerException(); // Catch the error earlier - final String localeStr = locale.toString(); - if (SubtypeLocaleUtils.NO_LANGUAGE.equals(localeStr)) { - // If we don't have a locale, insert into the "all locales" user dictionary. - mLocaleString = USER_DICTIONARY_ALL_LANGUAGES; - } else { - mLocaleString = localeStr; - } - mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales; - ContentResolver cres = context.getContentResolver(); - - mObserver = new ContentObserver(null) { - @Override - public void onChange(final boolean self) { - // This hook is deprecated as of API level 16 (Build.VERSION_CODES.JELLY_BEAN), - // but should still be supported for cases where the IME is running on an older - // version of the platform. - onChange(self, null); - } - // The following hook is only available as of API level 16 - // (Build.VERSION_CODES.JELLY_BEAN), and as such it will only work on JellyBean+ - // devices. On older versions of the platform, the hook above will be called instead. - @Override - public void onChange(final boolean self, final Uri uri) { - setNeedsToRecreate(); - } - }; - cres.registerContentObserver(Words.CONTENT_URI, true, mObserver); - reloadDictionaryIfRequired(); - } - - // Note: This method is called by {@link DictionaryFacilitator} using Java reflection. - @ExternallyReferenced - public static UserBinaryDictionary getDictionary( - final Context context, final Locale locale, final File dictFile, - final String dictNamePrefix, @Nullable final String account) { - return new UserBinaryDictionary( - context, locale, false /* alsoUseMoreRestrictiveLocales */, - dictFile, dictNamePrefix + NAME); - } - - @Override - public synchronized void close() { - if (mObserver != null) { - mContext.getContentResolver().unregisterContentObserver(mObserver); - mObserver = null; - } - super.close(); - } - - @Override - public void loadInitialContentsLocked() { - // Split the locale. For example "en" => ["en"], "de_DE" => ["de", "DE"], - // "en_US_foo_bar_qux" => ["en", "US", "foo_bar_qux"] because of the limit of 3. - // This is correct for locale processing. - // For this example, we'll look at the "en_US_POSIX" case. - final String[] localeElements = - TextUtils.isEmpty(mLocaleString) ? new String[] {} : mLocaleString.split("_", 3); - final int length = localeElements.length; - - final StringBuilder request = new StringBuilder("(locale is NULL)"); - String localeSoFar = ""; - // At start, localeElements = ["en", "US", "POSIX"] ; localeSoFar = "" ; - // and request = "(locale is NULL)" - for (int i = 0; i < length; ++i) { - // i | localeSoFar | localeElements - // 0 | "" | ["en", "US", "POSIX"] - // 1 | "en_" | ["en", "US", "POSIX"] - // 2 | "en_US_" | ["en", "en_US", "POSIX"] - localeElements[i] = localeSoFar + localeElements[i]; - localeSoFar = localeElements[i] + "_"; - // i | request - // 0 | "(locale is NULL)" - // 1 | "(locale is NULL) or (locale=?)" - // 2 | "(locale is NULL) or (locale=?) or (locale=?)" - request.append(" or (locale=?)"); - } - // At the end, localeElements = ["en", "en_US", "en_US_POSIX"]; localeSoFar = en_US_POSIX_" - // and request = "(locale is NULL) or (locale=?) or (locale=?) or (locale=?)" - - final String[] requestArguments; - // If length == 3, we already have all the arguments we need (common prefix is meaningless - // inside variants - if (mAlsoUseMoreRestrictiveLocales && length < 3) { - request.append(" or (locale like ?)"); - // The following creates an array with one more (null) position - final String[] localeElementsWithMoreRestrictiveLocalesIncluded = - Arrays.copyOf(localeElements, length + 1); - localeElementsWithMoreRestrictiveLocalesIncluded[length] = - localeElements[length - 1] + "_%"; - requestArguments = localeElementsWithMoreRestrictiveLocalesIncluded; - // If for example localeElements = ["en"] - // then requestArguments = ["en", "en_%"] - // and request = (locale is NULL) or (locale=?) or (locale like ?) - // If localeElements = ["en", "en_US"] - // then requestArguments = ["en", "en_US", "en_US_%"] - } else { - requestArguments = localeElements; - } - final String requestString = request.toString(); - addWordsFromProjectionLocked(PROJECTION_QUERY, requestString, requestArguments); - } - - private void addWordsFromProjectionLocked(final String[] query, String request, - final String[] requestArguments) - throws IllegalArgumentException { - Cursor cursor = null; - try { - cursor = mContext.getContentResolver().query( - Words.CONTENT_URI, query, request, requestArguments, null); - addWordsLocked(cursor); - } catch (final SQLiteException e) { - Log.e(TAG, "SQLiteException in the remote User dictionary process.", e); - } finally { - try { - if (null != cursor) cursor.close(); - } catch (final SQLiteException e) { - Log.e(TAG, "SQLiteException in the remote User dictionary process.", e); - } - } - } - - private static int scaleFrequencyFromDefaultToLatinIme(final int defaultFrequency) { - // The default frequency for the user dictionary is 250 for historical reasons. - // Latin IME considers a good value for the default user dictionary frequency - // is about 160 considering the scale we use. So we are scaling down the values. - if (defaultFrequency > Integer.MAX_VALUE / LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY) { - return (defaultFrequency / HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY) - * LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY; - } - return (defaultFrequency * LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY) - / HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY; - } - - private void addWordsLocked(final Cursor cursor) { - if (cursor == null) return; - if (cursor.moveToFirst()) { - final int indexWord = cursor.getColumnIndex(Words.WORD); - final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY); - while (!cursor.isAfterLast()) { - final String word = cursor.getString(indexWord); - final int frequency = cursor.getInt(indexFrequency); - final int adjustedFrequency = scaleFrequencyFromDefaultToLatinIme(frequency); - // Safeguard against adding really long words. - if (word.length() <= MAX_WORD_LENGTH) { - runGCIfRequiredLocked(true /* mindsBlockByGC */); - addUnigramLocked(word, adjustedFrequency, false /* isNotAWord */, - false /* isPossiblyOffensive */, - BinaryDictionary.NOT_A_VALID_TIMESTAMP); - } - cursor.moveToNext(); - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java deleted file mode 100644 index 8803edc88..000000000 --- a/java/src/com/android/inputmethod/latin/WordComposer.java +++ /dev/null @@ -1,481 +0,0 @@ -/* - * Copyright (C) 2008 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 com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.event.CombinerChain; -import com.android.inputmethod.event.Event; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.CoordinateUtils; -import com.android.inputmethod.latin.common.InputPointers; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.define.DebugFlags; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; - -import java.util.ArrayList; -import java.util.Collections; - -import javax.annotation.Nonnull; - -/** - * A place to store the currently composing word with information such as adjacent key codes as well - */ -public final class WordComposer { - private static final int MAX_WORD_LENGTH = DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH; - private static final boolean DBG = DebugFlags.DEBUG_ENABLED; - - public static final int CAPS_MODE_OFF = 0; - // 1 is shift bit, 2 is caps bit, 4 is auto bit but this is just a convention as these bits - // aren't used anywhere in the code - public static final int CAPS_MODE_MANUAL_SHIFTED = 0x1; - public static final int CAPS_MODE_MANUAL_SHIFT_LOCKED = 0x3; - public static final int CAPS_MODE_AUTO_SHIFTED = 0x5; - public static final int CAPS_MODE_AUTO_SHIFT_LOCKED = 0x7; - - private CombinerChain mCombinerChain; - private String mCombiningSpec; // Memory so that we don't uselessly recreate the combiner chain - - // The list of events that served to compose this string. - private final ArrayList<Event> mEvents; - private final InputPointers mInputPointers = new InputPointers(MAX_WORD_LENGTH); - private SuggestedWordInfo mAutoCorrection; - private boolean mIsResumed; - private boolean mIsBatchMode; - // A memory of the last rejected batch mode suggestion, if any. This goes like this: the user - // gestures a word, is displeased with the results and hits backspace, then gestures again. - // At the very least we should avoid re-suggesting the same thing, and to do that we memorize - // the rejected suggestion in this variable. - // TODO: this should be done in a comprehensive way by the User History feature instead of - // as an ad-hockery here. - private String mRejectedBatchModeSuggestion; - - // Cache these values for performance - private CharSequence mTypedWordCache; - private int mCapsCount; - private int mDigitsCount; - private int mCapitalizedMode; - // This is the number of code points entered so far. This is not limited to MAX_WORD_LENGTH. - // In general, this contains the size of mPrimaryKeyCodes, except when this is greater than - // MAX_WORD_LENGTH in which case mPrimaryKeyCodes only contain the first MAX_WORD_LENGTH - // code points. - private int mCodePointSize; - private int mCursorPositionWithinWord; - - /** - * Whether the composing word has the only first char capitalized. - */ - private boolean mIsOnlyFirstCharCapitalized; - - public WordComposer() { - mCombinerChain = new CombinerChain(""); - mEvents = new ArrayList<>(); - mAutoCorrection = null; - mIsResumed = false; - mIsBatchMode = false; - mCursorPositionWithinWord = 0; - mRejectedBatchModeSuggestion = null; - refreshTypedWordCache(); - } - - public ComposedData getComposedDataSnapshot() { - return new ComposedData(getInputPointers(), isBatchMode(), mTypedWordCache.toString()); - } - - /** - * Restart the combiners, possibly with a new spec. - * @param combiningSpec The spec string for combining. This is found in the extra value. - */ - public void restartCombining(final String combiningSpec) { - final String nonNullCombiningSpec = null == combiningSpec ? "" : combiningSpec; - if (!nonNullCombiningSpec.equals(mCombiningSpec)) { - mCombinerChain = new CombinerChain( - mCombinerChain.getComposingWordWithCombiningFeedback().toString()); - mCombiningSpec = nonNullCombiningSpec; - } - } - - /** - * Clear out the keys registered so far. - */ - public void reset() { - mCombinerChain.reset(); - mEvents.clear(); - mAutoCorrection = null; - mCapsCount = 0; - mDigitsCount = 0; - mIsOnlyFirstCharCapitalized = false; - mIsResumed = false; - mIsBatchMode = false; - mCursorPositionWithinWord = 0; - mRejectedBatchModeSuggestion = null; - refreshTypedWordCache(); - } - - private final void refreshTypedWordCache() { - mTypedWordCache = mCombinerChain.getComposingWordWithCombiningFeedback(); - mCodePointSize = Character.codePointCount(mTypedWordCache, 0, mTypedWordCache.length()); - } - - /** - * Number of keystrokes in the composing word. - * @return the number of keystrokes - */ - public int size() { - return mCodePointSize; - } - - public boolean isSingleLetter() { - return size() == 1; - } - - public final boolean isComposingWord() { - return size() > 0; - } - - public InputPointers getInputPointers() { - return mInputPointers; - } - - /** - * Process an event and return an event, and return a processed event to apply. - * @param event the unprocessed event. - * @return the processed event. Never null, but may be marked as consumed. - */ - @Nonnull - public Event processEvent(@Nonnull final Event event) { - final Event processedEvent = mCombinerChain.processEvent(mEvents, event); - // The retained state of the combiner chain may have changed while processing the event, - // so we need to update our cache. - refreshTypedWordCache(); - mEvents.add(event); - return processedEvent; - } - - /** - * Apply a processed input event. - * - * All input events should be supported, including software/hardware events, characters as well - * as deletions, multiple inputs and gestures. - * - * @param event the event to apply. Must not be null. - */ - public void applyProcessedEvent(final Event event) { - mCombinerChain.applyProcessedEvent(event); - final int primaryCode = event.mCodePoint; - final int keyX = event.mX; - final int keyY = event.mY; - final int newIndex = size(); - refreshTypedWordCache(); - mCursorPositionWithinWord = mCodePointSize; - // We may have deleted the last one. - if (0 == mCodePointSize) { - mIsOnlyFirstCharCapitalized = false; - } - if (Constants.CODE_DELETE != event.mKeyCode) { - if (newIndex < MAX_WORD_LENGTH) { - // In the batch input mode, the {@code mInputPointers} holds batch input points and - // shouldn't be overridden by the "typed key" coordinates - // (See {@link #setBatchInputWord}). - if (!mIsBatchMode) { - // TODO: Set correct pointer id and time - mInputPointers.addPointerAt(newIndex, keyX, keyY, 0, 0); - } - } - if (0 == newIndex) { - mIsOnlyFirstCharCapitalized = Character.isUpperCase(primaryCode); - } else { - mIsOnlyFirstCharCapitalized = mIsOnlyFirstCharCapitalized - && !Character.isUpperCase(primaryCode); - } - if (Character.isUpperCase(primaryCode)) mCapsCount++; - if (Character.isDigit(primaryCode)) mDigitsCount++; - } - mAutoCorrection = null; - } - - public void setCursorPositionWithinWord(final int posWithinWord) { - mCursorPositionWithinWord = posWithinWord; - // TODO: compute where that puts us inside the events - } - - public boolean isCursorFrontOrMiddleOfComposingWord() { - if (DBG && mCursorPositionWithinWord > mCodePointSize) { - throw new RuntimeException("Wrong cursor position : " + mCursorPositionWithinWord - + "in a word of size " + mCodePointSize); - } - return mCursorPositionWithinWord != mCodePointSize; - } - - /** - * When the cursor is moved by the user, we need to update its position. - * If it falls inside the currently composing word, we don't reset the composition, and - * only update the cursor position. - * - * @param expectedMoveAmount How many java chars to move the cursor. Negative values move - * the cursor backward, positive values move the cursor forward. - * @return true if the cursor is still inside the composing word, false otherwise. - */ - public boolean moveCursorByAndReturnIfInsideComposingWord(final int expectedMoveAmount) { - int actualMoveAmount = 0; - int cursorPos = mCursorPositionWithinWord; - // TODO: Don't make that copy. We can do this directly from mTypedWordCache. - final int[] codePoints = StringUtils.toCodePointArray(mTypedWordCache); - if (expectedMoveAmount >= 0) { - // Moving the cursor forward for the expected amount or until the end of the word has - // been reached, whichever comes first. - while (actualMoveAmount < expectedMoveAmount && cursorPos < codePoints.length) { - actualMoveAmount += Character.charCount(codePoints[cursorPos]); - ++cursorPos; - } - } else { - // Moving the cursor backward for the expected amount or until the start of the word - // has been reached, whichever comes first. - while (actualMoveAmount > expectedMoveAmount && cursorPos > 0) { - --cursorPos; - actualMoveAmount -= Character.charCount(codePoints[cursorPos]); - } - } - // If the actual and expected amounts differ, we crossed the start or the end of the word - // so the result would not be inside the composing word. - if (actualMoveAmount != expectedMoveAmount) { - return false; - } - mCursorPositionWithinWord = cursorPos; - mCombinerChain.applyProcessedEvent(mCombinerChain.processEvent( - mEvents, Event.createCursorMovedEvent(cursorPos))); - return true; - } - - public void setBatchInputPointers(final InputPointers batchPointers) { - mInputPointers.set(batchPointers); - mIsBatchMode = true; - } - - public void setBatchInputWord(final String word) { - reset(); - mIsBatchMode = true; - final int length = word.length(); - for (int i = 0; i < length; i = Character.offsetByCodePoints(word, i, 1)) { - final int codePoint = Character.codePointAt(word, i); - // We don't want to override the batch input points that are held in mInputPointers - // (See {@link #add(int,int,int)}). - final Event processedEvent = - processEvent(Event.createEventForCodePointFromUnknownSource(codePoint)); - applyProcessedEvent(processedEvent); - } - } - - /** - * Set the currently composing word to the one passed as an argument. - * This will register NOT_A_COORDINATE for X and Ys, and use the passed keyboard for proximity. - * @param codePoints the code points to set as the composing word. - * @param coordinates the x, y coordinates of the key in the CoordinateUtils format - */ - public void setComposingWord(final int[] codePoints, final int[] coordinates) { - reset(); - final int length = codePoints.length; - for (int i = 0; i < length; ++i) { - final Event processedEvent = - processEvent(Event.createEventForCodePointFromAlreadyTypedText(codePoints[i], - CoordinateUtils.xFromArray(coordinates, i), - CoordinateUtils.yFromArray(coordinates, i))); - applyProcessedEvent(processedEvent); - } - mIsResumed = true; - } - - /** - * Returns the word as it was typed, without any correction applied. - * @return the word that was typed so far. Never returns null. - */ - public String getTypedWord() { - return mTypedWordCache.toString(); - } - - /** - * Whether this composer is composing or about to compose a word in which only the first letter - * is a capital. - * - * If we do have a composing word, we just return whether the word has indeed only its first - * character capitalized. If we don't, then we return a value based on the capitalized mode, - * which tell us what is likely to happen for the next composing word. - * - * @return capitalization preference - */ - public boolean isOrWillBeOnlyFirstCharCapitalized() { - return isComposingWord() ? mIsOnlyFirstCharCapitalized - : (CAPS_MODE_OFF != mCapitalizedMode); - } - - /** - * Whether or not all of the user typed chars are upper case - * @return true if all user typed chars are upper case, false otherwise - */ - public boolean isAllUpperCase() { - if (size() <= 1) { - return mCapitalizedMode == CAPS_MODE_AUTO_SHIFT_LOCKED - || mCapitalizedMode == CAPS_MODE_MANUAL_SHIFT_LOCKED; - } - return mCapsCount == size(); - } - - public boolean wasShiftedNoLock() { - return mCapitalizedMode == CAPS_MODE_AUTO_SHIFTED - || mCapitalizedMode == CAPS_MODE_MANUAL_SHIFTED; - } - - /** - * Returns true if more than one character is upper case, otherwise returns false. - */ - public boolean isMostlyCaps() { - return mCapsCount > 1; - } - - /** - * Returns true if we have digits in the composing word. - */ - public boolean hasDigits() { - return mDigitsCount > 0; - } - - /** - * Saves the caps mode at the start of composing. - * - * WordComposer needs to know about the caps mode for several reasons. The first is, we need - * to know after the fact what the reason was, to register the correct form into the user - * history dictionary: if the word was automatically capitalized, we should insert it in - * all-lower case but if it's a manual pressing of shift, then it should be inserted as is. - * Also, batch input needs to know about the current caps mode to display correctly - * capitalized suggestions. - * @param mode the mode at the time of start - */ - public void setCapitalizedModeAtStartComposingTime(final int mode) { - mCapitalizedMode = mode; - } - - /** - * Before fetching suggestions, we don't necessarily know about the capitalized mode yet. - * - * If we don't have a composing word yet, we take a note of this mode so that we can then - * supply this information to the suggestion process. If we have a composing word, then - * the previous mode has priority over this. - * @param mode the mode just before fetching suggestions - */ - public void adviseCapitalizedModeBeforeFetchingSuggestions(final int mode) { - if (!isComposingWord()) { - mCapitalizedMode = mode; - } - } - - /** - * Returns whether the word was automatically capitalized. - * @return whether the word was automatically capitalized - */ - public boolean wasAutoCapitalized() { - return mCapitalizedMode == CAPS_MODE_AUTO_SHIFT_LOCKED - || mCapitalizedMode == CAPS_MODE_AUTO_SHIFTED; - } - - /** - * Sets the auto-correction for this word. - */ - public void setAutoCorrection(final SuggestedWordInfo autoCorrection) { - mAutoCorrection = autoCorrection; - } - - /** - * @return the auto-correction for this word, or null if none. - */ - public SuggestedWordInfo getAutoCorrectionOrNull() { - return mAutoCorrection; - } - - /** - * @return whether we started composing this word by resuming suggestion on an existing string - */ - public boolean isResumed() { - return mIsResumed; - } - - // `type' should be one of the LastComposedWord.COMMIT_TYPE_* constants above. - // committedWord should contain suggestion spans if applicable. - public LastComposedWord commitWord(final int type, final CharSequence committedWord, - final String separatorString, final NgramContext ngramContext) { - // Note: currently, we come here whenever we commit a word. If it's a MANUAL_PICK - // or a DECIDED_WORD we may cancel the commit later; otherwise, we should deactivate - // the last composed word to ensure this does not happen. - final LastComposedWord lastComposedWord = new LastComposedWord(mEvents, - mInputPointers, mTypedWordCache.toString(), committedWord, separatorString, - ngramContext, mCapitalizedMode); - mInputPointers.reset(); - if (type != LastComposedWord.COMMIT_TYPE_DECIDED_WORD - && type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) { - lastComposedWord.deactivate(); - } - mCapsCount = 0; - mDigitsCount = 0; - mIsBatchMode = false; - mCombinerChain.reset(); - mEvents.clear(); - mCodePointSize = 0; - mIsOnlyFirstCharCapitalized = false; - mCapitalizedMode = CAPS_MODE_OFF; - refreshTypedWordCache(); - mAutoCorrection = null; - mCursorPositionWithinWord = 0; - mIsResumed = false; - mRejectedBatchModeSuggestion = null; - return lastComposedWord; - } - - public void resumeSuggestionOnLastComposedWord(final LastComposedWord lastComposedWord) { - mEvents.clear(); - Collections.copy(mEvents, lastComposedWord.mEvents); - mInputPointers.set(lastComposedWord.mInputPointers); - mCombinerChain.reset(); - refreshTypedWordCache(); - mCapitalizedMode = lastComposedWord.mCapitalizedMode; - mAutoCorrection = null; // This will be filled by the next call to updateSuggestion. - mCursorPositionWithinWord = mCodePointSize; - mRejectedBatchModeSuggestion = null; - mIsResumed = true; - } - - public boolean isBatchMode() { - return mIsBatchMode; - } - - public void setRejectedBatchModeSuggestion(final String rejectedSuggestion) { - mRejectedBatchModeSuggestion = rejectedSuggestion; - } - - public String getRejectedBatchModeSuggestion() { - return mRejectedBatchModeSuggestion; - } - - @UsedForTesting - void addInputPointerForTest(int index, int keyX, int keyY) { - mInputPointers.addPointerAt(index, keyX, keyY, 0, 0); - } - - @UsedForTesting - void setTypedWordCacheForTests(String typedWordCacheForTests) { - mTypedWordCache = typedWordCacheForTests; - } -} diff --git a/java/src/com/android/inputmethod/latin/WordListInfo.java b/java/src/com/android/inputmethod/latin/WordListInfo.java deleted file mode 100644 index 268fe9818..000000000 --- a/java/src/com/android/inputmethod/latin/WordListInfo.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -/** - * Information container for a word list. - */ -public final class WordListInfo { - public final String mId; - public final String mLocale; - public final String mRawChecksum; - public WordListInfo(final String id, final String locale, final String rawChecksum) { - mId = id; - mLocale = locale; - mRawChecksum = rawChecksum; - } -} diff --git a/java/src/com/android/inputmethod/latin/about/AboutPreferences.java b/java/src/com/android/inputmethod/latin/about/AboutPreferences.java deleted file mode 100644 index ec7e6def5..000000000 --- a/java/src/com/android/inputmethod/latin/about/AboutPreferences.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2013 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.about; - -import android.app.Fragment; - -/** - * Placeholer class of AboutPreferences. Never use this. - */ -public final class AboutPreferences extends Fragment { - private AboutPreferences() { - // Prevents this from being instantiated - } -} diff --git a/java/src/com/android/inputmethod/latin/accounts/AccountStateChangedListener.java b/java/src/com/android/inputmethod/latin/accounts/AccountStateChangedListener.java deleted file mode 100644 index 3c39c9a16..000000000 --- a/java/src/com/android/inputmethod/latin/accounts/AccountStateChangedListener.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2014 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.accounts; - -import androidx.annotation.NonNull; - -import javax.annotation.Nullable; - -/** - * Handles changes to account used to sign in to the keyboard. - * e.g. account switching/sign-in/sign-out from the keyboard - * user toggling the sync preference. - */ -public class AccountStateChangedListener { - - /** - * Called when the current account being used in keyboard is signed out. - * - * @param oldAccount the account that was signed out of. - */ - public static void onAccountSignedOut(@NonNull String oldAccount) { - } - - /** - * Called when the user signs-in to the keyboard. - * This may be called when the user switches accounts to sign in with a different account. - * - * @param oldAccount the previous account that was being used for sign-in. - * May be null for a fresh sign-in. - * @param newAccount the account being used for sign-in. - */ - public static void onAccountSignedIn(@Nullable String oldAccount, @NonNull String newAccount) { - } - - /** - * Called when the user toggles the sync preference. - * - * @param account the account being used for sync. - * @param syncEnabled indicates whether sync has been enabled or not. - */ - public static void onSyncPreferenceChanged(@Nullable String account, boolean syncEnabled) { - } - - /** - * Forces an immediate sync to happen. - * This should only be used for debugging purposes. - * - * @param account the account to use for sync. - */ - public static void forceSync(@Nullable String account) { - } - - /** - * Forces an immediate deletion of user's data. - * This should only be used for debugging purposes. - * - * @param account the account to use for sync. - */ - public static void forceDelete(@Nullable String account) { - } -} diff --git a/java/src/com/android/inputmethod/latin/accounts/AccountsChangedReceiver.java b/java/src/com/android/inputmethod/latin/accounts/AccountsChangedReceiver.java deleted file mode 100644 index 00bcecf52..000000000 --- a/java/src/com/android/inputmethod/latin/accounts/AccountsChangedReceiver.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2014 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.accounts; - -import android.accounts.AccountManager; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.text.TextUtils; -import android.util.Log; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.settings.LocalSettingsConstants; - -/** - * {@link BroadcastReceiver} for {@link AccountManager#LOGIN_ACCOUNTS_CHANGED_ACTION}. - */ -public class AccountsChangedReceiver extends BroadcastReceiver { - static final String TAG = "AccountsChangedReceiver"; - - @Override - public void onReceive(Context context, Intent intent) { - if (!AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION.equals(intent.getAction())) { - Log.w(TAG, "Received unknown broadcast: " + intent); - return; - } - - // Ideally the account preference could live in a different preferences file - // that wasn't being backed up and restored, however the preference fragments - // currently only deal with the default shared preferences which is why - // separating this out into a different file is not trivial currently. - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - final String currentAccount = prefs.getString( - LocalSettingsConstants.PREF_ACCOUNT_NAME, null); - removeUnknownAccountFromPreference(prefs, getAccountsForLogin(context), currentAccount); - } - - /** - * Helper method to help test this receiver. - */ - @UsedForTesting - protected String[] getAccountsForLogin(Context context) { - return LoginAccountUtils.getAccountsForLogin(context); - } - - /** - * Removes the currentAccount from preferences if it's not found - * in the list of current accounts. - */ - private static void removeUnknownAccountFromPreference(final SharedPreferences prefs, - final String[] accounts, final String currentAccount) { - if (currentAccount == null) { - return; - } - for (final String account : accounts) { - if (TextUtils.equals(currentAccount, account)) { - return; - } - } - Log.i(TAG, "The current account was removed from the system: " + currentAccount); - prefs.edit() - .remove(LocalSettingsConstants.PREF_ACCOUNT_NAME) - .apply(); - } -} diff --git a/java/src/com/android/inputmethod/latin/accounts/AuthUtils.java b/java/src/com/android/inputmethod/latin/accounts/AuthUtils.java deleted file mode 100644 index 31aba3631..000000000 --- a/java/src/com/android/inputmethod/latin/accounts/AuthUtils.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2014 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.accounts; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.accounts.AccountManagerCallback; -import android.accounts.AccountManagerFuture; -import android.accounts.AuthenticatorException; -import android.accounts.OperationCanceledException; -import android.content.Context; -import android.os.Bundle; -import android.os.Handler; - -import java.io.IOException; - -/** - * Utility class that handles generation/invalidation of auth tokens in the app. - */ -public class AuthUtils { - private final AccountManager mAccountManager; - - public AuthUtils(Context context) { - mAccountManager = AccountManager.get(context); - } - - /** - * @see AccountManager#invalidateAuthToken(String, String) - */ - public void invalidateAuthToken(final String accountType, final String authToken) { - mAccountManager.invalidateAuthToken(accountType, authToken); - } - - /** - * @see AccountManager#getAuthToken( - * Account, String, Bundle, boolean, AccountManagerCallback, Handler) - */ - public AccountManagerFuture<Bundle> getAuthToken(final Account account, - final String authTokenType, final Bundle options, final boolean notifyAuthFailure, - final AccountManagerCallback<Bundle> callback, final Handler handler) { - return mAccountManager.getAuthToken(account, authTokenType, options, notifyAuthFailure, - callback, handler); - } - - /** - * @see AccountManager#blockingGetAuthToken(Account, String, boolean) - */ - public String blockingGetAuthToken(final Account account, final String authTokenType, - final boolean notifyAuthFailure) throws OperationCanceledException, - AuthenticatorException, IOException { - return mAccountManager.blockingGetAuthToken(account, authTokenType, notifyAuthFailure); - } -} diff --git a/java/src/com/android/inputmethod/latin/accounts/LoginAccountUtils.java b/java/src/com/android/inputmethod/latin/accounts/LoginAccountUtils.java deleted file mode 100644 index dcc64a223..000000000 --- a/java/src/com/android/inputmethod/latin/accounts/LoginAccountUtils.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2014 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.accounts; - -import android.content.Context; - -import javax.annotation.Nonnull; - -/** - * Utility class for retrieving accounts that may be used for login. - */ -public class LoginAccountUtils { - /** - * This defines the type of account this class deals with. - * This account type is used when listing the accounts available on the device for login. - */ - public static final String ACCOUNT_TYPE = ""; - - private LoginAccountUtils() { - // This utility class is not publicly instantiable. - } - - /** - * Get the accounts available for login. - * - * @return an array of accounts. Empty (never null) if no accounts are available for login. - */ - @Nonnull - @SuppressWarnings("unused") - public static String[] getAccountsForLogin(final Context context) { - return new String[0]; - } -} diff --git a/java/src/com/android/inputmethod/latin/define/DebugFlags.java b/java/src/com/android/inputmethod/latin/define/DebugFlags.java deleted file mode 100644 index c509e8322..000000000 --- a/java/src/com/android/inputmethod/latin/define/DebugFlags.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2014 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.define; - -import android.content.SharedPreferences; - -public final class DebugFlags { - public static final boolean DEBUG_ENABLED = false; - - private DebugFlags() { - // This class is not publicly instantiable. - } - - @SuppressWarnings("unused") - public static void init(final SharedPreferences prefs) { - } -} diff --git a/java/src/com/android/inputmethod/latin/define/DecoderSpecificConstants.java b/java/src/com/android/inputmethod/latin/define/DecoderSpecificConstants.java deleted file mode 100644 index 7f57ce858..000000000 --- a/java/src/com/android/inputmethod/latin/define/DecoderSpecificConstants.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2015 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.define; - -/** - * Decoder specific constants for LatinIme. - */ -public class DecoderSpecificConstants { - - // Must be equal to MAX_WORD_LENGTH in native/jni/src/defines.h - public static final int DICTIONARY_MAX_WORD_LENGTH = 48; - - // (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram is supported in Java side. Needs to modify - // MAX_PREV_WORD_COUNT_FOR_N_GRAM in native/jni/src/defines.h for suggestions. - public static final int MAX_PREV_WORD_COUNT_FOR_N_GRAM = 3; - - public static final String DECODER_DICT_SUFFIX = ""; - - public static final boolean SHOULD_VERIFY_MAGIC_NUMBER = true; - public static final boolean SHOULD_VERIFY_CHECKSUM = true; - public static final boolean SHOULD_USE_DICT_VERSION = true; - public static final boolean SHOULD_AUTO_CORRECT_USING_NON_WHITE_LISTED_SUGGESTION = false; - public static final boolean SHOULD_REMOVE_PREVIOUSLY_REJECTED_SUGGESTION = true; -} diff --git a/java/src/com/android/inputmethod/latin/define/JniLibName.java b/java/src/com/android/inputmethod/latin/define/JniLibName.java deleted file mode 100644 index abfc36d39..000000000 --- a/java/src/com/android/inputmethod/latin/define/JniLibName.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.define; - -public final class JniLibName { - private JniLibName() { - // This class is not publicly instantiable. - } - - public static final String JNI_LIB_NAME = "jni_latinime"; -} diff --git a/java/src/com/android/inputmethod/latin/define/ProductionFlags.java b/java/src/com/android/inputmethod/latin/define/ProductionFlags.java deleted file mode 100644 index f31c20822..000000000 --- a/java/src/com/android/inputmethod/latin/define/ProductionFlags.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.define; - -public final class ProductionFlags { - private ProductionFlags() { - // This class is not publicly instantiable. - } - - public static final boolean IS_HARDWARE_KEYBOARD_SUPPORTED = false; - - /** - * Include all suggestions from all dictionaries in - * {@link com.android.inputmethod.latin.SuggestedWords#mRawSuggestions}. - */ - public static final boolean INCLUDE_RAW_SUGGESTIONS = false; - - /** - * When false, the metrics logging is not yet ready to be enabled. - */ - public static final boolean IS_METRICS_LOGGING_SUPPORTED = false; - - /** - * When {@code false}, the split keyboard is not yet ready to be enabled. - */ - public static final boolean IS_SPLIT_KEYBOARD_SUPPORTED = true; - - /** - * When {@code false}, account sign-in in keyboard is not yet ready to be enabled. - */ - public static final boolean ENABLE_ACCOUNT_SIGN_IN = false; - - /** - * When {@code true}, user history dictionary sync feature is ready to be enabled. - */ - public static final boolean ENABLE_USER_HISTORY_DICTIONARY_SYNC = - ENABLE_ACCOUNT_SIGN_IN && false; - - /** - * When {@code true}, the IME maintains per account {@link UserHistoryDictionary}. - */ - public static final boolean ENABLE_PER_ACCOUNT_USER_HISTORY_DICTIONARY = - ENABLE_ACCOUNT_SIGN_IN && false; -} diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java deleted file mode 100644 index a7804a13f..000000000 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ /dev/null @@ -1,2353 +0,0 @@ -/* - * Copyright (C) 2013 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.inputlogic; - -import android.graphics.Color; -import android.os.SystemClock; -import android.text.SpannableString; -import android.text.Spanned; -import android.text.TextUtils; -import android.text.style.BackgroundColorSpan; -import android.text.style.SuggestionSpan; -import android.util.Log; -import android.view.KeyCharacterMap; -import android.view.KeyEvent; -import android.view.inputmethod.CorrectionInfo; -import android.view.inputmethod.EditorInfo; - -import com.android.inputmethod.compat.SuggestionSpanUtils; -import com.android.inputmethod.event.Event; -import com.android.inputmethod.event.InputTransaction; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.KeyboardSwitcher; -import com.android.inputmethod.latin.Dictionary; -import com.android.inputmethod.latin.DictionaryFacilitator; -import com.android.inputmethod.latin.LastComposedWord; -import com.android.inputmethod.latin.LatinIME; -import com.android.inputmethod.latin.NgramContext; -import com.android.inputmethod.latin.RichInputConnection; -import com.android.inputmethod.latin.Suggest; -import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.WordComposer; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.InputPointers; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.define.DebugFlags; -import com.android.inputmethod.latin.settings.SettingsValues; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; -import com.android.inputmethod.latin.settings.SpacingAndPunctuations; -import com.android.inputmethod.latin.suggestions.SuggestionStripViewAccessor; -import com.android.inputmethod.latin.utils.AsyncResultHolder; -import com.android.inputmethod.latin.utils.InputTypeUtils; -import com.android.inputmethod.latin.utils.RecapitalizeStatus; -import com.android.inputmethod.latin.utils.StatsUtils; -import com.android.inputmethod.latin.utils.TextRange; - -import java.util.ArrayList; -import java.util.Locale; -import java.util.TreeSet; -import java.util.concurrent.TimeUnit; - -import javax.annotation.Nonnull; - -/** - * This class manages the input logic. - */ -public final class InputLogic { - private static final String TAG = InputLogic.class.getSimpleName(); - - // TODO : Remove this member when we can. - final LatinIME mLatinIME; - private final SuggestionStripViewAccessor mSuggestionStripViewAccessor; - - // Never null. - private InputLogicHandler mInputLogicHandler = InputLogicHandler.NULL_HANDLER; - - // TODO : make all these fields private as soon as possible. - // Current space state of the input method. This can be any of the above constants. - private int mSpaceState; - // Never null - public SuggestedWords mSuggestedWords = SuggestedWords.getEmptyInstance(); - public final Suggest mSuggest; - private final DictionaryFacilitator mDictionaryFacilitator; - - public LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; - // This has package visibility so it can be accessed from InputLogicHandler. - /* package */ final WordComposer mWordComposer; - public final RichInputConnection mConnection; - private final RecapitalizeStatus mRecapitalizeStatus = new RecapitalizeStatus(); - - private int mDeleteCount; - private long mLastKeyTime; - public final TreeSet<Long> mCurrentlyPressedHardwareKeys = new TreeSet<>(); - - // Keeps track of most recently inserted text (multi-character key) for reverting - private String mEnteredText; - - // TODO: This boolean is persistent state and causes large side effects at unexpected times. - // Find a way to remove it for readability. - private boolean mIsAutoCorrectionIndicatorOn; - private long mDoubleSpacePeriodCountdownStart; - - // The word being corrected while the cursor is in the middle of the word. - // Note: This does not have a composing span, so it must be handled separately. - private String mWordBeingCorrectedByCursor = null; - - /** - * Create a new instance of the input logic. - * @param latinIME the instance of the parent LatinIME. We should remove this when we can. - * @param suggestionStripViewAccessor an object to access the suggestion strip view. - * @param dictionaryFacilitator facilitator for getting suggestions and updating user history - * dictionary. - */ - public InputLogic(final LatinIME latinIME, - final SuggestionStripViewAccessor suggestionStripViewAccessor, - final DictionaryFacilitator dictionaryFacilitator) { - mLatinIME = latinIME; - mSuggestionStripViewAccessor = suggestionStripViewAccessor; - mWordComposer = new WordComposer(); - mConnection = new RichInputConnection(latinIME); - mInputLogicHandler = InputLogicHandler.NULL_HANDLER; - mSuggest = new Suggest(dictionaryFacilitator); - mDictionaryFacilitator = dictionaryFacilitator; - } - - /** - * Initializes the input logic for input in an editor. - * - * Call this when input starts or restarts in some editor (typically, in onStartInputView). - * - * @param combiningSpec the combining spec string for this subtype - * @param settingsValues the current settings values - */ - public void startInput(final String combiningSpec, final SettingsValues settingsValues) { - mEnteredText = null; - mWordBeingCorrectedByCursor = null; - mConnection.onStartInput(); - if (!mWordComposer.getTypedWord().isEmpty()) { - // For messaging apps that offer send button, the IME does not get the opportunity - // to capture the last word. This block should capture those uncommitted words. - // The timestamp at which it is captured is not accurate but close enough. - StatsUtils.onWordCommitUserTyped( - mWordComposer.getTypedWord(), mWordComposer.isBatchMode()); - } - mWordComposer.restartCombining(combiningSpec); - resetComposingState(true /* alsoResetLastComposedWord */); - mDeleteCount = 0; - mSpaceState = SpaceState.NONE; - mRecapitalizeStatus.disable(); // Do not perform recapitalize until the cursor is moved once - mCurrentlyPressedHardwareKeys.clear(); - mSuggestedWords = SuggestedWords.getEmptyInstance(); - // In some cases (namely, after rotation of the device) editorInfo.initialSelStart is lying - // so we try using some heuristics to find out about these and fix them. - mConnection.tryFixLyingCursorPosition(); - cancelDoubleSpacePeriodCountdown(); - if (InputLogicHandler.NULL_HANDLER == mInputLogicHandler) { - mInputLogicHandler = new InputLogicHandler(mLatinIME, this); - } else { - mInputLogicHandler.reset(); - } - - if (settingsValues.mShouldShowLxxSuggestionUi) { - mConnection.requestCursorUpdates(true /* enableMonitor */, - true /* requestImmediateCallback */); - } - } - - /** - * Call this when the subtype changes. - * @param combiningSpec the spec string for the combining rules - * @param settingsValues the current settings values - */ - public void onSubtypeChanged(final String combiningSpec, final SettingsValues settingsValues) { - finishInput(); - startInput(combiningSpec, settingsValues); - } - - /** - * Call this when the orientation changes. - * @param settingsValues the current values of the settings. - */ - public void onOrientationChange(final SettingsValues settingsValues) { - // If !isComposingWord, #commitTyped() is a no-op, but still, it's better to avoid - // the useless IPC of {begin,end}BatchEdit. - if (mWordComposer.isComposingWord()) { - mConnection.beginBatchEdit(); - // If we had a composition in progress, we need to commit the word so that the - // suggestionsSpan will be added. This will allow resuming on the same suggestions - // after rotation is finished. - commitTyped(settingsValues, LastComposedWord.NOT_A_SEPARATOR); - mConnection.endBatchEdit(); - } - } - - /** - * Clean up the input logic after input is finished. - */ - public void finishInput() { - if (mWordComposer.isComposingWord()) { - mConnection.finishComposingText(); - StatsUtils.onWordCommitUserTyped( - mWordComposer.getTypedWord(), mWordComposer.isBatchMode()); - } - resetComposingState(true /* alsoResetLastComposedWord */); - mInputLogicHandler.reset(); - } - - // Normally this class just gets out of scope after the process ends, but in unit tests, we - // create several instances of LatinIME in the same process, which results in several - // instances of InputLogic. This cleans up the associated handler so that tests don't leak - // handlers. - public void recycle() { - final InputLogicHandler inputLogicHandler = mInputLogicHandler; - mInputLogicHandler = InputLogicHandler.NULL_HANDLER; - inputLogicHandler.destroy(); - mDictionaryFacilitator.closeDictionaries(); - } - - /** - * React to a string input. - * - * This is triggered by keys that input many characters at once, like the ".com" key or - * some additional keys for example. - * - * @param settingsValues the current values of the settings. - * @param event the input event containing the data. - * @return the complete transaction object - */ - public InputTransaction onTextInput(final SettingsValues settingsValues, final Event event, - final int keyboardShiftMode, final LatinIME.UIHandler handler) { - final String rawText = event.getTextToCommit().toString(); - final InputTransaction inputTransaction = new InputTransaction(settingsValues, event, - SystemClock.uptimeMillis(), mSpaceState, - getActualCapsMode(settingsValues, keyboardShiftMode)); - mConnection.beginBatchEdit(); - if (mWordComposer.isComposingWord()) { - commitCurrentAutoCorrection(settingsValues, rawText, handler); - } else { - resetComposingState(true /* alsoResetLastComposedWord */); - } - handler.postUpdateSuggestionStrip(SuggestedWords.INPUT_STYLE_TYPING); - final String text = performSpecificTldProcessingOnTextInput(rawText); - if (SpaceState.PHANTOM == mSpaceState) { - insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues); - } - mConnection.commitText(text, 1); - StatsUtils.onWordCommitUserTyped(mEnteredText, mWordComposer.isBatchMode()); - mConnection.endBatchEdit(); - // Space state must be updated before calling updateShiftState - mSpaceState = SpaceState.NONE; - mEnteredText = text; - mWordBeingCorrectedByCursor = null; - inputTransaction.setDidAffectContents(); - inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); - return inputTransaction; - } - - /** - * A suggestion was picked from the suggestion strip. - * @param settingsValues the current values of the settings. - * @param suggestionInfo the suggestion info. - * @param keyboardShiftState the shift state of the keyboard, as returned by - * {@link com.android.inputmethod.keyboard.KeyboardSwitcher#getKeyboardShiftMode()} - * @return the complete transaction object - */ - // Called from {@link SuggestionStripView} through the {@link SuggestionStripView#Listener} - // interface - public InputTransaction onPickSuggestionManually(final SettingsValues settingsValues, - final SuggestedWordInfo suggestionInfo, final int keyboardShiftState, - final int currentKeyboardScriptId, final LatinIME.UIHandler handler) { - final SuggestedWords suggestedWords = mSuggestedWords; - final String suggestion = suggestionInfo.mWord; - // If this is a punctuation picked from the suggestion strip, pass it to onCodeInput - if (suggestion.length() == 1 && suggestedWords.isPunctuationSuggestions()) { - // We still want to log a suggestion click. - StatsUtils.onPickSuggestionManually( - mSuggestedWords, suggestionInfo, mDictionaryFacilitator); - // Word separators are suggested before the user inputs something. - // Rely on onCodeInput to do the complicated swapping/stripping logic consistently. - final Event event = Event.createPunctuationSuggestionPickedEvent(suggestionInfo); - return onCodeInput(settingsValues, event, keyboardShiftState, - currentKeyboardScriptId, handler); - } - - final Event event = Event.createSuggestionPickedEvent(suggestionInfo); - final InputTransaction inputTransaction = new InputTransaction(settingsValues, - event, SystemClock.uptimeMillis(), mSpaceState, keyboardShiftState); - // Manual pick affects the contents of the editor, so we take note of this. It's important - // for the sequence of language switching. - inputTransaction.setDidAffectContents(); - mConnection.beginBatchEdit(); - if (SpaceState.PHANTOM == mSpaceState && suggestion.length() > 0 - // In the batch input mode, a manually picked suggested word should just replace - // the current batch input text and there is no need for a phantom space. - && !mWordComposer.isBatchMode()) { - final int firstChar = Character.codePointAt(suggestion, 0); - if (!settingsValues.isWordSeparator(firstChar) - || settingsValues.isUsuallyPrecededBySpace(firstChar)) { - insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues); - } - } - - // TODO: We should not need the following branch. We should be able to take the same - // code path as for other kinds, use commitChosenWord, and do everything normally. We will - // however need to reset the suggestion strip right away, because we know we can't take - // the risk of calling commitCompletion twice because we don't know how the app will react. - if (suggestionInfo.isKindOf(SuggestedWordInfo.KIND_APP_DEFINED)) { - mSuggestedWords = SuggestedWords.getEmptyInstance(); - mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); - inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); - resetComposingState(true /* alsoResetLastComposedWord */); - mConnection.commitCompletion(suggestionInfo.mApplicationSpecifiedCompletionInfo); - mConnection.endBatchEdit(); - return inputTransaction; - } - - commitChosenWord(settingsValues, suggestion, LastComposedWord.COMMIT_TYPE_MANUAL_PICK, - LastComposedWord.NOT_A_SEPARATOR); - mConnection.endBatchEdit(); - // Don't allow cancellation of manual pick - mLastComposedWord.deactivate(); - // Space state must be updated before calling updateShiftState - mSpaceState = SpaceState.PHANTOM; - inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); - - // If we're not showing the "Touch again to save", then update the suggestion strip. - // That's going to be predictions (or punctuation suggestions), so INPUT_STYLE_NONE. - handler.postUpdateSuggestionStrip(SuggestedWords.INPUT_STYLE_NONE); - - StatsUtils.onPickSuggestionManually( - mSuggestedWords, suggestionInfo, mDictionaryFacilitator); - StatsUtils.onWordCommitSuggestionPickedManually( - suggestionInfo.mWord, mWordComposer.isBatchMode()); - return inputTransaction; - } - - /** - * Consider an update to the cursor position. Evaluate whether this update has happened as - * part of normal typing or whether it was an explicit cursor move by the user. In any case, - * do the necessary adjustments. - * @param oldSelStart old selection start - * @param oldSelEnd old selection end - * @param newSelStart new selection start - * @param newSelEnd new selection end - * @param settingsValues the current values of the settings. - * @return whether the cursor has moved as a result of user interaction. - */ - public boolean onUpdateSelection(final int oldSelStart, final int oldSelEnd, - final int newSelStart, final int newSelEnd, final SettingsValues settingsValues) { - if (mConnection.isBelatedExpectedUpdate(oldSelStart, newSelStart, oldSelEnd, newSelEnd)) { - return false; - } - // TODO: the following is probably better done in resetEntireInputState(). - // it should only happen when the cursor moved, and the very purpose of the - // test below is to narrow down whether this happened or not. Likewise with - // the call to updateShiftState. - // We set this to NONE because after a cursor move, we don't want the space - // state-related special processing to kick in. - mSpaceState = SpaceState.NONE; - - final boolean selectionChangedOrSafeToReset = - oldSelStart != newSelStart || oldSelEnd != newSelEnd // selection changed - || !mWordComposer.isComposingWord(); // safe to reset - final boolean hasOrHadSelection = (oldSelStart != oldSelEnd || newSelStart != newSelEnd); - final int moveAmount = newSelStart - oldSelStart; - // As an added small gift from the framework, it happens upon rotation when there - // is a selection that we get a wrong cursor position delivered to startInput() that - // does not get reflected in the oldSel{Start,End} parameters to the next call to - // onUpdateSelection. In this case, we may have set a composition, and when we're here - // we realize we shouldn't have. In theory, in this case, selectionChangedOrSafeToReset - // should be true, but that is if the framework had taken that wrong cursor position - // into account, which means we have to reset the entire composing state whenever there - // is or was a selection regardless of whether it changed or not. - if (hasOrHadSelection || !settingsValues.needsToLookupSuggestions() - || (selectionChangedOrSafeToReset - && !mWordComposer.moveCursorByAndReturnIfInsideComposingWord(moveAmount))) { - // If we are composing a word and moving the cursor, we would want to set a - // suggestion span for recorrection to work correctly. Unfortunately, that - // would involve the keyboard committing some new text, which would move the - // cursor back to where it was. Latin IME could then fix the position of the cursor - // again, but the asynchronous nature of the calls results in this wreaking havoc - // with selection on double tap and the like. - // Another option would be to send suggestions each time we set the composing - // text, but that is probably too expensive to do, so we decided to leave things - // as is. - // Also, we're posting a resume suggestions message, and this will update the - // suggestions strip in a few milliseconds, so if we cleared the suggestion strip here - // we'd have the suggestion strip noticeably janky. To avoid that, we don't clear - // it here, which means we'll keep outdated suggestions for a split second but the - // visual result is better. - resetEntireInputState(newSelStart, newSelEnd, false /* clearSuggestionStrip */); - // If the user is in the middle of correcting a word, we should learn it before moving - // the cursor away. - if (!TextUtils.isEmpty(mWordBeingCorrectedByCursor)) { - final int timeStampInSeconds = (int)TimeUnit.MILLISECONDS.toSeconds( - System.currentTimeMillis()); - performAdditionToUserHistoryDictionary(settingsValues, mWordBeingCorrectedByCursor, - NgramContext.EMPTY_PREV_WORDS_INFO); - } - } else { - // resetEntireInputState calls resetCachesUponCursorMove, but forcing the - // composition to end. But in all cases where we don't reset the entire input - // state, we still want to tell the rich input connection about the new cursor - // position so that it can update its caches. - mConnection.resetCachesUponCursorMoveAndReturnSuccess( - newSelStart, newSelEnd, false /* shouldFinishComposition */); - } - - // The cursor has been moved : we now accept to perform recapitalization - mRecapitalizeStatus.enable(); - // We moved the cursor. If we are touching a word, we need to resume suggestion. - mLatinIME.mHandler.postResumeSuggestions(true /* shouldDelay */); - // Stop the last recapitalization, if started. - mRecapitalizeStatus.stop(); - mWordBeingCorrectedByCursor = null; - return true; - } - - /** - * React to a code input. It may be a code point to insert, or a symbolic value that influences - * the keyboard behavior. - * - * Typically, this is called whenever a key is pressed on the software keyboard. This is not - * the entry point for gesture input; see the onBatchInput* family of functions for this. - * - * @param settingsValues the current settings values. - * @param event the event to handle. - * @param keyboardShiftMode the current shift mode of the keyboard, as returned by - * {@link com.android.inputmethod.keyboard.KeyboardSwitcher#getKeyboardShiftMode()} - * @return the complete transaction object - */ - public InputTransaction onCodeInput(final SettingsValues settingsValues, - @Nonnull final Event event, final int keyboardShiftMode, - final int currentKeyboardScriptId, final LatinIME.UIHandler handler) { - mWordBeingCorrectedByCursor = null; - final Event processedEvent = mWordComposer.processEvent(event); - final InputTransaction inputTransaction = new InputTransaction(settingsValues, - processedEvent, SystemClock.uptimeMillis(), mSpaceState, - getActualCapsMode(settingsValues, keyboardShiftMode)); - if (processedEvent.mKeyCode != Constants.CODE_DELETE - || inputTransaction.mTimestamp > mLastKeyTime + Constants.LONG_PRESS_MILLISECONDS) { - mDeleteCount = 0; - } - mLastKeyTime = inputTransaction.mTimestamp; - mConnection.beginBatchEdit(); - if (!mWordComposer.isComposingWord()) { - // TODO: is this useful? It doesn't look like it should be done here, but rather after - // a word is committed. - mIsAutoCorrectionIndicatorOn = false; - } - - // TODO: Consolidate the double-space period timer, mLastKeyTime, and the space state. - if (processedEvent.mCodePoint != Constants.CODE_SPACE) { - cancelDoubleSpacePeriodCountdown(); - } - - Event currentEvent = processedEvent; - while (null != currentEvent) { - if (currentEvent.isConsumed()) { - handleConsumedEvent(currentEvent, inputTransaction); - } else if (currentEvent.isFunctionalKeyEvent()) { - handleFunctionalEvent(currentEvent, inputTransaction, currentKeyboardScriptId, - handler); - } else { - handleNonFunctionalEvent(currentEvent, inputTransaction, handler); - } - currentEvent = currentEvent.mNextEvent; - } - // Try to record the word being corrected when the user enters a word character or - // the backspace key. - if (!mConnection.hasSlowInputConnection() && !mWordComposer.isComposingWord() - && (settingsValues.isWordCodePoint(processedEvent.mCodePoint) || - processedEvent.mKeyCode == Constants.CODE_DELETE)) { - mWordBeingCorrectedByCursor = getWordAtCursor( - settingsValues, currentKeyboardScriptId); - } - if (!inputTransaction.didAutoCorrect() && processedEvent.mKeyCode != Constants.CODE_SHIFT - && processedEvent.mKeyCode != Constants.CODE_CAPSLOCK - && processedEvent.mKeyCode != Constants.CODE_SWITCH_ALPHA_SYMBOL) - mLastComposedWord.deactivate(); - if (Constants.CODE_DELETE != processedEvent.mKeyCode) { - mEnteredText = null; - } - mConnection.endBatchEdit(); - return inputTransaction; - } - - public void onStartBatchInput(final SettingsValues settingsValues, - final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) { - mWordBeingCorrectedByCursor = null; - mInputLogicHandler.onStartBatchInput(); - handler.showGesturePreviewAndSuggestionStrip( - SuggestedWords.getEmptyInstance(), false /* dismissGestureFloatingPreviewText */); - handler.cancelUpdateSuggestionStrip(); - ++mAutoCommitSequenceNumber; - mConnection.beginBatchEdit(); - if (mWordComposer.isComposingWord()) { - if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { - // If we are in the middle of a recorrection, we need to commit the recorrection - // first so that we can insert the batch input at the current cursor position. - // We also need to unlearn the original word that is now being corrected. - unlearnWord(mWordComposer.getTypedWord(), settingsValues, - Constants.EVENT_BACKSPACE); - resetEntireInputState(mConnection.getExpectedSelectionStart(), - mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */); - } else if (mWordComposer.isSingleLetter()) { - // We auto-correct the previous (typed, not gestured) string iff it's one character - // long. The reason for this is, even in the middle of gesture typing, you'll still - // tap one-letter words and you want them auto-corrected (typically, "i" in English - // should become "I"). However for any longer word, we assume that the reason for - // tapping probably is that the word you intend to type is not in the dictionary, - // so we do not attempt to correct, on the assumption that if that was a dictionary - // word, the user would probably have gestured instead. - commitCurrentAutoCorrection(settingsValues, LastComposedWord.NOT_A_SEPARATOR, - handler); - } else { - commitTyped(settingsValues, LastComposedWord.NOT_A_SEPARATOR); - } - } - final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor(); - if (Character.isLetterOrDigit(codePointBeforeCursor) - || settingsValues.isUsuallyFollowedBySpace(codePointBeforeCursor)) { - final boolean autoShiftHasBeenOverriden = keyboardSwitcher.getKeyboardShiftMode() != - getCurrentAutoCapsState(settingsValues); - mSpaceState = SpaceState.PHANTOM; - if (!autoShiftHasBeenOverriden) { - // When we change the space state, we need to update the shift state of the - // keyboard unless it has been overridden manually. This is happening for example - // after typing some letters and a period, then gesturing; the keyboard is not in - // caps mode yet, but since a gesture is starting, it should go in caps mode, - // unless the user explictly said it should not. - keyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(settingsValues), - getCurrentRecapitalizeState()); - } - } - mConnection.endBatchEdit(); - mWordComposer.setCapitalizedModeAtStartComposingTime( - getActualCapsMode(settingsValues, keyboardSwitcher.getKeyboardShiftMode())); - } - - /* The sequence number member is only used in onUpdateBatchInput. It is increased each time - * auto-commit happens. The reason we need this is, when auto-commit happens we trim the - * input pointers that are held in a singleton, and to know how much to trim we rely on the - * results of the suggestion process that is held in mSuggestedWords. - * However, the suggestion process is asynchronous, and sometimes we may enter the - * onUpdateBatchInput method twice without having recomputed suggestions yet, or having - * received new suggestions generated from not-yet-trimmed input pointers. In this case, the - * mIndexOfTouchPointOfSecondWords member will be out of date, and we must not use it lest we - * remove an unrelated number of pointers (possibly even more than are left in the input - * pointers, leading to a crash). - * To avoid that, we increase the sequence number each time we auto-commit and trim the - * input pointers, and we do not use any suggested words that have been generated with an - * earlier sequence number. - */ - private int mAutoCommitSequenceNumber = 1; - public void onUpdateBatchInput(final InputPointers batchPointers) { - mInputLogicHandler.onUpdateBatchInput(batchPointers, mAutoCommitSequenceNumber); - } - - public void onEndBatchInput(final InputPointers batchPointers) { - mInputLogicHandler.updateTailBatchInput(batchPointers, mAutoCommitSequenceNumber); - ++mAutoCommitSequenceNumber; - } - - public void onCancelBatchInput(final LatinIME.UIHandler handler) { - mInputLogicHandler.onCancelBatchInput(); - handler.showGesturePreviewAndSuggestionStrip( - SuggestedWords.getEmptyInstance(), true /* dismissGestureFloatingPreviewText */); - } - - // TODO: on the long term, this method should become private, but it will be difficult. - // Especially, how do we deal with InputMethodService.onDisplayCompletions? - public void setSuggestedWords(final SuggestedWords suggestedWords) { - if (!suggestedWords.isEmpty()) { - final SuggestedWordInfo suggestedWordInfo; - if (suggestedWords.mWillAutoCorrect) { - suggestedWordInfo = suggestedWords.getInfo(SuggestedWords.INDEX_OF_AUTO_CORRECTION); - } else { - // We can't use suggestedWords.getWord(SuggestedWords.INDEX_OF_TYPED_WORD) - // because it may differ from mWordComposer.mTypedWord. - suggestedWordInfo = suggestedWords.mTypedWordInfo; - } - mWordComposer.setAutoCorrection(suggestedWordInfo); - } - mSuggestedWords = suggestedWords; - final boolean newAutoCorrectionIndicator = suggestedWords.mWillAutoCorrect; - - // Put a blue underline to a word in TextView which will be auto-corrected. - if (mIsAutoCorrectionIndicatorOn != newAutoCorrectionIndicator - && mWordComposer.isComposingWord()) { - mIsAutoCorrectionIndicatorOn = newAutoCorrectionIndicator; - final CharSequence textWithUnderline = - getTextWithUnderline(mWordComposer.getTypedWord()); - // TODO: when called from an updateSuggestionStrip() call that results from a posted - // message, this is called outside any batch edit. Potentially, this may result in some - // janky flickering of the screen, although the display speed makes it unlikely in - // the practice. - setComposingTextInternal(textWithUnderline, 1); - } - } - - /** - * Handle a consumed event. - * - * Consumed events represent events that have already been consumed, typically by the - * combining chain. - * - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - */ - private void handleConsumedEvent(final Event event, final InputTransaction inputTransaction) { - // A consumed event may have text to commit and an update to the composing state, so - // we evaluate both. With some combiners, it's possible than an event contains both - // and we enter both of the following if clauses. - final CharSequence textToCommit = event.getTextToCommit(); - if (!TextUtils.isEmpty(textToCommit)) { - mConnection.commitText(textToCommit, 1); - inputTransaction.setDidAffectContents(); - } - if (mWordComposer.isComposingWord()) { - setComposingTextInternal(mWordComposer.getTypedWord(), 1); - inputTransaction.setDidAffectContents(); - inputTransaction.setRequiresUpdateSuggestions(); - } - } - - /** - * Handle a functional key event. - * - * A functional event is a special key, like delete, shift, emoji, or the settings key. - * Non-special keys are those that generate a single code point. - * This includes all letters, digits, punctuation, separators, emoji. It excludes keys that - * manage keyboard-related stuff like shift, language switch, settings, layout switch, or - * any key that results in multiple code points like the ".com" key. - * - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - */ - private void handleFunctionalEvent(final Event event, final InputTransaction inputTransaction, - final int currentKeyboardScriptId, final LatinIME.UIHandler handler) { - switch (event.mKeyCode) { - case Constants.CODE_DELETE: - handleBackspaceEvent(event, inputTransaction, currentKeyboardScriptId); - // Backspace is a functional key, but it affects the contents of the editor. - inputTransaction.setDidAffectContents(); - break; - case Constants.CODE_SHIFT: - performRecapitalization(inputTransaction.mSettingsValues); - inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); - if (mSuggestedWords.isPrediction()) { - inputTransaction.setRequiresUpdateSuggestions(); - } - break; - case Constants.CODE_CAPSLOCK: - // Note: Changing keyboard to shift lock state is handled in - // {@link KeyboardSwitcher#onEvent(Event)}. - break; - case Constants.CODE_SYMBOL_SHIFT: - // Note: Calling back to the keyboard on the symbol Shift key is handled in - // {@link #onPressKey(int,int,boolean)} and {@link #onReleaseKey(int,boolean)}. - break; - case Constants.CODE_SWITCH_ALPHA_SYMBOL: - // Note: Calling back to the keyboard on symbol key is handled in - // {@link #onPressKey(int,int,boolean)} and {@link #onReleaseKey(int,boolean)}. - break; - case Constants.CODE_SETTINGS: - onSettingsKeyPressed(); - break; - case Constants.CODE_SHORTCUT: - // We need to switch to the shortcut IME. This is handled by LatinIME since the - // input logic has no business with IME switching. - break; - case Constants.CODE_ACTION_NEXT: - performEditorAction(EditorInfo.IME_ACTION_NEXT); - break; - case Constants.CODE_ACTION_PREVIOUS: - performEditorAction(EditorInfo.IME_ACTION_PREVIOUS); - break; - case Constants.CODE_LANGUAGE_SWITCH: - handleLanguageSwitchKey(); - break; - case Constants.CODE_EMOJI: - // Note: Switching emoji keyboard is being handled in - // {@link KeyboardState#onEvent(Event,int)}. - break; - case Constants.CODE_ALPHA_FROM_EMOJI: - // Note: Switching back from Emoji keyboard to the main keyboard is being - // handled in {@link KeyboardState#onEvent(Event,int)}. - break; - case Constants.CODE_SHIFT_ENTER: - final Event tmpEvent = Event.createSoftwareKeypressEvent(Constants.CODE_ENTER, - event.mKeyCode, event.mX, event.mY, event.isKeyRepeat()); - handleNonSpecialCharacterEvent(tmpEvent, inputTransaction, handler); - // Shift + Enter is treated as a functional key but it results in adding a new - // line, so that does affect the contents of the editor. - inputTransaction.setDidAffectContents(); - break; - default: - throw new RuntimeException("Unknown key code : " + event.mKeyCode); - } - } - - /** - * Handle an event that is not a functional event. - * - * These events are generally events that cause input, but in some cases they may do other - * things like trigger an editor action. - * - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - */ - private void handleNonFunctionalEvent(final Event event, - final InputTransaction inputTransaction, - final LatinIME.UIHandler handler) { - inputTransaction.setDidAffectContents(); - switch (event.mCodePoint) { - case Constants.CODE_ENTER: - final EditorInfo editorInfo = getCurrentInputEditorInfo(); - final int imeOptionsActionId = - InputTypeUtils.getImeOptionsActionIdFromEditorInfo(editorInfo); - if (InputTypeUtils.IME_ACTION_CUSTOM_LABEL == imeOptionsActionId) { - // Either we have an actionLabel and we should performEditorAction with - // actionId regardless of its value. - performEditorAction(editorInfo.actionId); - } else if (EditorInfo.IME_ACTION_NONE != imeOptionsActionId) { - // We didn't have an actionLabel, but we had another action to execute. - // EditorInfo.IME_ACTION_NONE explicitly means no action. In contrast, - // EditorInfo.IME_ACTION_UNSPECIFIED is the default value for an action, so it - // means there should be an action and the app didn't bother to set a specific - // code for it - presumably it only handles one. It does not have to be treated - // in any specific way: anything that is not IME_ACTION_NONE should be sent to - // performEditorAction. - performEditorAction(imeOptionsActionId); - } else { - // No action label, and the action from imeOptions is NONE: this is a regular - // enter key that should input a carriage return. - handleNonSpecialCharacterEvent(event, inputTransaction, handler); - } - break; - default: - handleNonSpecialCharacterEvent(event, inputTransaction, handler); - break; - } - } - - /** - * Handle inputting a code point to the editor. - * - * Non-special keys are those that generate a single code point. - * This includes all letters, digits, punctuation, separators, emoji. It excludes keys that - * manage keyboard-related stuff like shift, language switch, settings, layout switch, or - * any key that results in multiple code points like the ".com" key. - * - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - */ - private void handleNonSpecialCharacterEvent(final Event event, - final InputTransaction inputTransaction, - final LatinIME.UIHandler handler) { - final int codePoint = event.mCodePoint; - mSpaceState = SpaceState.NONE; - if (inputTransaction.mSettingsValues.isWordSeparator(codePoint) - || Character.getType(codePoint) == Character.OTHER_SYMBOL) { - handleSeparatorEvent(event, inputTransaction, handler); - } else { - if (SpaceState.PHANTOM == inputTransaction.mSpaceState) { - if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { - // If we are in the middle of a recorrection, we need to commit the recorrection - // first so that we can insert the character at the current cursor position. - // We also need to unlearn the original word that is now being corrected. - unlearnWord(mWordComposer.getTypedWord(), inputTransaction.mSettingsValues, - Constants.EVENT_BACKSPACE); - resetEntireInputState(mConnection.getExpectedSelectionStart(), - mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */); - } else { - commitTyped(inputTransaction.mSettingsValues, LastComposedWord.NOT_A_SEPARATOR); - } - } - handleNonSeparatorEvent(event, inputTransaction.mSettingsValues, inputTransaction); - } - } - - /** - * Handle a non-separator. - * @param event The event to handle. - * @param settingsValues The current settings values. - * @param inputTransaction The transaction in progress. - */ - private void handleNonSeparatorEvent(final Event event, final SettingsValues settingsValues, - final InputTransaction inputTransaction) { - final int codePoint = event.mCodePoint; - // TODO: refactor this method to stop flipping isComposingWord around all the time, and - // make it shorter (possibly cut into several pieces). Also factor - // handleNonSpecialCharacterEvent which has the same name as other handle* methods but is - // not the same. - boolean isComposingWord = mWordComposer.isComposingWord(); - - // TODO: remove isWordConnector() and use isUsuallyFollowedBySpace() instead. - // See onStartBatchInput() to see how to do it. - if (SpaceState.PHANTOM == inputTransaction.mSpaceState - && !settingsValues.isWordConnector(codePoint)) { - if (isComposingWord) { - // Validity check - throw new RuntimeException("Should not be composing here"); - } - insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues); - } - - if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { - // If we are in the middle of a recorrection, we need to commit the recorrection - // first so that we can insert the character at the current cursor position. - // We also need to unlearn the original word that is now being corrected. - unlearnWord(mWordComposer.getTypedWord(), inputTransaction.mSettingsValues, - Constants.EVENT_BACKSPACE); - resetEntireInputState(mConnection.getExpectedSelectionStart(), - mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */); - isComposingWord = false; - } - // We want to find out whether to start composing a new word with this character. If so, - // we need to reset the composing state and switch isComposingWord. The order of the - // tests is important for good performance. - // We only start composing if we're not already composing. - if (!isComposingWord - // We only start composing if this is a word code point. Essentially that means it's a - // a letter or a word connector. - && settingsValues.isWordCodePoint(codePoint) - // We never go into composing state if suggestions are not requested. - && settingsValues.needsToLookupSuggestions() && - // In languages with spaces, we only start composing a word when we are not already - // touching a word. In languages without spaces, the above conditions are sufficient. - // NOTE: If the InputConnection is slow, we skip the text-after-cursor check since it - // can incur a very expensive getTextAfterCursor() lookup, potentially making the - // keyboard UI slow and non-responsive. - // TODO: Cache the text after the cursor so we don't need to go to the InputConnection - // each time. We are already doing this for getTextBeforeCursor(). - (!settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces - || !mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations, - !mConnection.hasSlowInputConnection() /* checkTextAfter */))) { - // Reset entirely the composing state anyway, then start composing a new word unless - // the character is a word connector. The idea here is, word connectors are not - // separators and they should be treated as normal characters, except in the first - // position where they should not start composing a word. - isComposingWord = !settingsValues.mSpacingAndPunctuations.isWordConnector(codePoint); - // Here we don't need to reset the last composed word. It will be reset - // when we commit this one, if we ever do; if on the other hand we backspace - // it entirely and resume suggestions on the previous word, we'd like to still - // have touch coordinates for it. - resetComposingState(false /* alsoResetLastComposedWord */); - } - if (isComposingWord) { - mWordComposer.applyProcessedEvent(event); - // If it's the first letter, make note of auto-caps state - if (mWordComposer.isSingleLetter()) { - mWordComposer.setCapitalizedModeAtStartComposingTime(inputTransaction.mShiftState); - } - setComposingTextInternal(getTextWithUnderline(mWordComposer.getTypedWord()), 1); - } else { - final boolean swapWeakSpace = tryStripSpaceAndReturnWhetherShouldSwapInstead(event, - inputTransaction); - - if (swapWeakSpace && trySwapSwapperAndSpace(event, inputTransaction)) { - mSpaceState = SpaceState.WEAK; - } else { - sendKeyCodePoint(settingsValues, codePoint); - } - } - inputTransaction.setRequiresUpdateSuggestions(); - } - - /** - * Handle input of a separator code point. - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - */ - private void handleSeparatorEvent(final Event event, final InputTransaction inputTransaction, - final LatinIME.UIHandler handler) { - final int codePoint = event.mCodePoint; - final SettingsValues settingsValues = inputTransaction.mSettingsValues; - final boolean wasComposingWord = mWordComposer.isComposingWord(); - // We avoid sending spaces in languages without spaces if we were composing. - final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == codePoint - && !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces - && wasComposingWord; - if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { - // If we are in the middle of a recorrection, we need to commit the recorrection - // first so that we can insert the separator at the current cursor position. - // We also need to unlearn the original word that is now being corrected. - unlearnWord(mWordComposer.getTypedWord(), inputTransaction.mSettingsValues, - Constants.EVENT_BACKSPACE); - resetEntireInputState(mConnection.getExpectedSelectionStart(), - mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */); - } - // isComposingWord() may have changed since we stored wasComposing - if (mWordComposer.isComposingWord()) { - if (settingsValues.mAutoCorrectionEnabledPerUserSettings) { - final String separator = shouldAvoidSendingCode ? LastComposedWord.NOT_A_SEPARATOR - : StringUtils.newSingleCodePointString(codePoint); - commitCurrentAutoCorrection(settingsValues, separator, handler); - inputTransaction.setDidAutoCorrect(); - } else { - commitTyped(settingsValues, - StringUtils.newSingleCodePointString(codePoint)); - } - } - - final boolean swapWeakSpace = tryStripSpaceAndReturnWhetherShouldSwapInstead(event, - inputTransaction); - - final boolean isInsideDoubleQuoteOrAfterDigit = Constants.CODE_DOUBLE_QUOTE == codePoint - && mConnection.isInsideDoubleQuoteOrAfterDigit(); - - final boolean needsPrecedingSpace; - if (SpaceState.PHANTOM != inputTransaction.mSpaceState) { - needsPrecedingSpace = false; - } else if (Constants.CODE_DOUBLE_QUOTE == codePoint) { - // Double quotes behave like they are usually preceded by space iff we are - // not inside a double quote or after a digit. - needsPrecedingSpace = !isInsideDoubleQuoteOrAfterDigit; - } else if (settingsValues.mSpacingAndPunctuations.isClusteringSymbol(codePoint) - && settingsValues.mSpacingAndPunctuations.isClusteringSymbol( - mConnection.getCodePointBeforeCursor())) { - needsPrecedingSpace = false; - } else { - needsPrecedingSpace = settingsValues.isUsuallyPrecededBySpace(codePoint); - } - - if (needsPrecedingSpace) { - insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues); - } - - if (tryPerformDoubleSpacePeriod(event, inputTransaction)) { - mSpaceState = SpaceState.DOUBLE; - inputTransaction.setRequiresUpdateSuggestions(); - StatsUtils.onDoubleSpacePeriod(); - } else if (swapWeakSpace && trySwapSwapperAndSpace(event, inputTransaction)) { - mSpaceState = SpaceState.SWAP_PUNCTUATION; - mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); - } else if (Constants.CODE_SPACE == codePoint) { - if (!mSuggestedWords.isPunctuationSuggestions()) { - mSpaceState = SpaceState.WEAK; - } - - startDoubleSpacePeriodCountdown(inputTransaction); - if (wasComposingWord || mSuggestedWords.isEmpty()) { - inputTransaction.setRequiresUpdateSuggestions(); - } - - if (!shouldAvoidSendingCode) { - sendKeyCodePoint(settingsValues, codePoint); - } - } else { - if ((SpaceState.PHANTOM == inputTransaction.mSpaceState - && settingsValues.isUsuallyFollowedBySpace(codePoint)) - || (Constants.CODE_DOUBLE_QUOTE == codePoint - && isInsideDoubleQuoteOrAfterDigit)) { - // If we are in phantom space state, and the user presses a separator, we want to - // stay in phantom space state so that the next keypress has a chance to add the - // space. For example, if I type "Good dat", pick "day" from the suggestion strip - // then insert a comma and go on to typing the next word, I want the space to be - // inserted automatically before the next word, the same way it is when I don't - // input the comma. A double quote behaves like it's usually followed by space if - // we're inside a double quote. - // The case is a little different if the separator is a space stripper. Such a - // separator does not normally need a space on the right (that's the difference - // between swappers and strippers), so we should not stay in phantom space state if - // the separator is a stripper. Hence the additional test above. - mSpaceState = SpaceState.PHANTOM; - } - - sendKeyCodePoint(settingsValues, codePoint); - - // Set punctuation right away. onUpdateSelection will fire but tests whether it is - // already displayed or not, so it's okay. - mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); - } - - inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); - } - - /** - * Handle a press on the backspace key. - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - */ - private void handleBackspaceEvent(final Event event, final InputTransaction inputTransaction, - final int currentKeyboardScriptId) { - mSpaceState = SpaceState.NONE; - mDeleteCount++; - - // In many cases after backspace, we need to update the shift state. Normally we need - // to do this right away to avoid the shift state being out of date in case the user types - // backspace then some other character very fast. However, in the case of backspace key - // repeat, this can lead to flashiness when the cursor flies over positions where the - // shift state should be updated, so if this is a key repeat, we update after a small delay. - // Then again, even in the case of a key repeat, if the cursor is at start of text, it - // can't go any further back, so we can update right away even if it's a key repeat. - final int shiftUpdateKind = - event.isKeyRepeat() && mConnection.getExpectedSelectionStart() > 0 - ? InputTransaction.SHIFT_UPDATE_LATER : InputTransaction.SHIFT_UPDATE_NOW; - inputTransaction.requireShiftUpdate(shiftUpdateKind); - - if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { - // If we are in the middle of a recorrection, we need to commit the recorrection - // first so that we can remove the character at the current cursor position. - // We also need to unlearn the original word that is now being corrected. - unlearnWord(mWordComposer.getTypedWord(), inputTransaction.mSettingsValues, - Constants.EVENT_BACKSPACE); - resetEntireInputState(mConnection.getExpectedSelectionStart(), - mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */); - // When we exit this if-clause, mWordComposer.isComposingWord() will return false. - } - if (mWordComposer.isComposingWord()) { - if (mWordComposer.isBatchMode()) { - final String rejectedSuggestion = mWordComposer.getTypedWord(); - mWordComposer.reset(); - mWordComposer.setRejectedBatchModeSuggestion(rejectedSuggestion); - if (!TextUtils.isEmpty(rejectedSuggestion)) { - unlearnWord(rejectedSuggestion, inputTransaction.mSettingsValues, - Constants.EVENT_REJECTION); - } - StatsUtils.onBackspaceWordDelete(rejectedSuggestion.length()); - } else { - mWordComposer.applyProcessedEvent(event); - StatsUtils.onBackspacePressed(1); - } - if (mWordComposer.isComposingWord()) { - setComposingTextInternal(getTextWithUnderline(mWordComposer.getTypedWord()), 1); - } else { - mConnection.commitText("", 1); - } - inputTransaction.setRequiresUpdateSuggestions(); - } else { - if (mLastComposedWord.canRevertCommit()) { - final String lastComposedWord = mLastComposedWord.mTypedWord; - revertCommit(inputTransaction, inputTransaction.mSettingsValues); - StatsUtils.onRevertAutoCorrect(); - StatsUtils.onWordCommitUserTyped(lastComposedWord, mWordComposer.isBatchMode()); - // Restart suggestions when backspacing into a reverted word. This is required for - // the final corrected word to be learned, as learning only occurs when suggestions - // are active. - // - // Note: restartSuggestionsOnWordTouchedByCursor is already called for normal - // (non-revert) backspace handling. - if (inputTransaction.mSettingsValues.isSuggestionsEnabledPerUserSettings() - && inputTransaction.mSettingsValues.mSpacingAndPunctuations - .mCurrentLanguageHasSpaces - && !mConnection.isCursorFollowedByWordCharacter( - inputTransaction.mSettingsValues.mSpacingAndPunctuations)) { - restartSuggestionsOnWordTouchedByCursor(inputTransaction.mSettingsValues, - false /* forStartInput */, currentKeyboardScriptId); - } - return; - } - if (mEnteredText != null && mConnection.sameAsTextBeforeCursor(mEnteredText)) { - // Cancel multi-character input: remove the text we just entered. - // This is triggered on backspace after a key that inputs multiple characters, - // like the smiley key or the .com key. - mConnection.deleteTextBeforeCursor(mEnteredText.length()); - StatsUtils.onDeleteMultiCharInput(mEnteredText.length()); - mEnteredText = null; - // If we have mEnteredText, then we know that mHasUncommittedTypedChars == false. - // In addition we know that spaceState is false, and that we should not be - // reverting any autocorrect at this point. So we can safely return. - return; - } - if (SpaceState.DOUBLE == inputTransaction.mSpaceState) { - cancelDoubleSpacePeriodCountdown(); - if (mConnection.revertDoubleSpacePeriod( - inputTransaction.mSettingsValues.mSpacingAndPunctuations)) { - // No need to reset mSpaceState, it has already be done (that's why we - // receive it as a parameter) - inputTransaction.setRequiresUpdateSuggestions(); - mWordComposer.setCapitalizedModeAtStartComposingTime( - WordComposer.CAPS_MODE_OFF); - StatsUtils.onRevertDoubleSpacePeriod(); - return; - } - } else if (SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) { - if (mConnection.revertSwapPunctuation()) { - StatsUtils.onRevertSwapPunctuation(); - // Likewise - return; - } - } - - boolean hasUnlearnedWordBeingDeleted = false; - - // No cancelling of commit/double space/swap: we have a regular backspace. - // We should backspace one char and restart suggestion if at the end of a word. - if (mConnection.hasSelection()) { - // If there is a selection, remove it. - // We also need to unlearn the selected text. - final CharSequence selection = mConnection.getSelectedText(0 /* 0 for no styles */); - if (!TextUtils.isEmpty(selection)) { - unlearnWord(selection.toString(), inputTransaction.mSettingsValues, - Constants.EVENT_BACKSPACE); - hasUnlearnedWordBeingDeleted = true; - } - final int numCharsDeleted = mConnection.getExpectedSelectionEnd() - - mConnection.getExpectedSelectionStart(); - mConnection.setSelection(mConnection.getExpectedSelectionEnd(), - mConnection.getExpectedSelectionEnd()); - mConnection.deleteTextBeforeCursor(numCharsDeleted); - StatsUtils.onBackspaceSelectedText(numCharsDeleted); - } else { - // There is no selection, just delete one character. - if (inputTransaction.mSettingsValues.isBeforeJellyBean() - || inputTransaction.mSettingsValues.mInputAttributes.isTypeNull() - || Constants.NOT_A_CURSOR_POSITION - == mConnection.getExpectedSelectionEnd()) { - // There are three possible reasons to send a key event: either the field has - // type TYPE_NULL, in which case the keyboard should send events, or we are - // running in backward compatibility mode, or we don't know the cursor position. - // Before Jelly bean, the keyboard would simulate a hardware keyboard event on - // pressing enter or delete. This is bad for many reasons (there are race - // conditions with commits) but some applications are relying on this behavior - // so we continue to support it for older apps, so we retain this behavior if - // the app has target SDK < JellyBean. - // As for the case where we don't know the cursor position, it can happen - // because of bugs in the framework. But the framework should know, so the next - // best thing is to leave it to whatever it thinks is best. - sendDownUpKeyEvent(KeyEvent.KEYCODE_DEL); - int totalDeletedLength = 1; - if (mDeleteCount > Constants.DELETE_ACCELERATE_AT) { - // If this is an accelerated (i.e., double) deletion, then we need to - // consider unlearning here because we may have already reached - // the previous word, and will lose it after next deletion. - hasUnlearnedWordBeingDeleted |= unlearnWordBeingDeleted( - inputTransaction.mSettingsValues, currentKeyboardScriptId); - sendDownUpKeyEvent(KeyEvent.KEYCODE_DEL); - totalDeletedLength++; - } - StatsUtils.onBackspacePressed(totalDeletedLength); - } else { - final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor(); - if (codePointBeforeCursor == Constants.NOT_A_CODE) { - // HACK for backward compatibility with broken apps that haven't realized - // yet that hardware keyboards are not the only way of inputting text. - // Nothing to delete before the cursor. We should not do anything, but many - // broken apps expect something to happen in this case so that they can - // catch it and have their broken interface react. If you need the keyboard - // to do this, you're doing it wrong -- please fix your app. - mConnection.deleteTextBeforeCursor(1); - // TODO: Add a new StatsUtils method onBackspaceWhenNoText() - return; - } - final int lengthToDelete = - Character.isSupplementaryCodePoint(codePointBeforeCursor) ? 2 : 1; - mConnection.deleteTextBeforeCursor(lengthToDelete); - int totalDeletedLength = lengthToDelete; - if (mDeleteCount > Constants.DELETE_ACCELERATE_AT) { - // If this is an accelerated (i.e., double) deletion, then we need to - // consider unlearning here because we may have already reached - // the previous word, and will lose it after next deletion. - hasUnlearnedWordBeingDeleted |= unlearnWordBeingDeleted( - inputTransaction.mSettingsValues, currentKeyboardScriptId); - final int codePointBeforeCursorToDeleteAgain = - mConnection.getCodePointBeforeCursor(); - if (codePointBeforeCursorToDeleteAgain != Constants.NOT_A_CODE) { - final int lengthToDeleteAgain = Character.isSupplementaryCodePoint( - codePointBeforeCursorToDeleteAgain) ? 2 : 1; - mConnection.deleteTextBeforeCursor(lengthToDeleteAgain); - totalDeletedLength += lengthToDeleteAgain; - } - } - StatsUtils.onBackspacePressed(totalDeletedLength); - } - } - if (!hasUnlearnedWordBeingDeleted) { - // Consider unlearning the word being deleted (if we have not done so already). - unlearnWordBeingDeleted( - inputTransaction.mSettingsValues, currentKeyboardScriptId); - } - if (mConnection.hasSlowInputConnection()) { - mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); - } else if (inputTransaction.mSettingsValues.isSuggestionsEnabledPerUserSettings() - && inputTransaction.mSettingsValues.mSpacingAndPunctuations - .mCurrentLanguageHasSpaces - && !mConnection.isCursorFollowedByWordCharacter( - inputTransaction.mSettingsValues.mSpacingAndPunctuations)) { - restartSuggestionsOnWordTouchedByCursor(inputTransaction.mSettingsValues, - false /* forStartInput */, currentKeyboardScriptId); - } - } - } - - String getWordAtCursor(final SettingsValues settingsValues, final int currentKeyboardScriptId) { - if (!mConnection.hasSelection() - && settingsValues.isSuggestionsEnabledPerUserSettings() - && settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) { - final TextRange range = mConnection.getWordRangeAtCursor( - settingsValues.mSpacingAndPunctuations, - currentKeyboardScriptId); - if (range != null) { - return range.mWord.toString(); - } - } - return ""; - } - - boolean unlearnWordBeingDeleted( - final SettingsValues settingsValues, final int currentKeyboardScriptId) { - if (mConnection.hasSlowInputConnection()) { - // TODO: Refactor unlearning so that it does not incur any extra calls - // to the InputConnection. That way it can still be performed on a slow - // InputConnection. - Log.w(TAG, "Skipping unlearning due to slow InputConnection."); - return false; - } - // If we just started backspacing to delete a previous word (but have not - // entered the composing state yet), unlearn the word. - // TODO: Consider tracking whether or not this word was typed by the user. - if (!mConnection.isCursorFollowedByWordCharacter(settingsValues.mSpacingAndPunctuations)) { - final String wordBeingDeleted = getWordAtCursor( - settingsValues, currentKeyboardScriptId); - if (!TextUtils.isEmpty(wordBeingDeleted)) { - unlearnWord(wordBeingDeleted, settingsValues, Constants.EVENT_BACKSPACE); - return true; - } - } - return false; - } - - void unlearnWord(final String word, final SettingsValues settingsValues, final int eventType) { - final NgramContext ngramContext = mConnection.getNgramContextFromNthPreviousWord( - settingsValues.mSpacingAndPunctuations, 2); - final long timeStampInSeconds = TimeUnit.MILLISECONDS.toSeconds( - System.currentTimeMillis()); - mDictionaryFacilitator.unlearnFromUserHistory( - word, ngramContext, timeStampInSeconds, eventType); - } - - /** - * Handle a press on the language switch key (the "globe key") - */ - private void handleLanguageSwitchKey() { - mLatinIME.switchToNextSubtype(); - } - - /** - * Swap a space with a space-swapping punctuation sign. - * - * This method will check that there are two characters before the cursor and that the first - * one is a space before it does the actual swapping. - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - * @return true if the swap has been performed, false if it was prevented by preliminary checks. - */ - private boolean trySwapSwapperAndSpace(final Event event, - final InputTransaction inputTransaction) { - final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor(); - if (Constants.CODE_SPACE != codePointBeforeCursor) { - return false; - } - mConnection.deleteTextBeforeCursor(1); - final String text = event.getTextToCommit() + " "; - mConnection.commitText(text, 1); - inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); - return true; - } - - /* - * Strip a trailing space if necessary and returns whether it's a swap weak space situation. - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - * @return whether we should swap the space instead of removing it. - */ - private boolean tryStripSpaceAndReturnWhetherShouldSwapInstead(final Event event, - final InputTransaction inputTransaction) { - final int codePoint = event.mCodePoint; - final boolean isFromSuggestionStrip = event.isSuggestionStripPress(); - if (Constants.CODE_ENTER == codePoint && - SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) { - mConnection.removeTrailingSpace(); - return false; - } - if ((SpaceState.WEAK == inputTransaction.mSpaceState - || SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) - && isFromSuggestionStrip) { - if (inputTransaction.mSettingsValues.isUsuallyPrecededBySpace(codePoint)) { - return false; - } - if (inputTransaction.mSettingsValues.isUsuallyFollowedBySpace(codePoint)) { - return true; - } - mConnection.removeTrailingSpace(); - } - return false; - } - - public void startDoubleSpacePeriodCountdown(final InputTransaction inputTransaction) { - mDoubleSpacePeriodCountdownStart = inputTransaction.mTimestamp; - } - - public void cancelDoubleSpacePeriodCountdown() { - mDoubleSpacePeriodCountdownStart = 0; - } - - public boolean isDoubleSpacePeriodCountdownActive(final InputTransaction inputTransaction) { - return inputTransaction.mTimestamp - mDoubleSpacePeriodCountdownStart - < inputTransaction.mSettingsValues.mDoubleSpacePeriodTimeout; - } - - /** - * Apply the double-space-to-period transformation if applicable. - * - * The double-space-to-period transformation means that we replace two spaces with a - * period-space sequence of characters. This typically happens when the user presses space - * twice in a row quickly. - * This method will check that the double-space-to-period is active in settings, that the - * two spaces have been input close enough together, that the typed character is a space - * and that the previous character allows for the transformation to take place. If all of - * these conditions are fulfilled, this method applies the transformation and returns true. - * Otherwise, it does nothing and returns false. - * - * @param event The event to handle. - * @param inputTransaction The transaction in progress. - * @return true if we applied the double-space-to-period transformation, false otherwise. - */ - private boolean tryPerformDoubleSpacePeriod(final Event event, - final InputTransaction inputTransaction) { - // Check the setting, the typed character and the countdown. If any of the conditions is - // not fulfilled, return false. - if (!inputTransaction.mSettingsValues.mUseDoubleSpacePeriod - || Constants.CODE_SPACE != event.mCodePoint - || !isDoubleSpacePeriodCountdownActive(inputTransaction)) { - return false; - } - // We only do this when we see one space and an accepted code point before the cursor. - // The code point may be a surrogate pair but the space may not, so we need 3 chars. - final CharSequence lastTwo = mConnection.getTextBeforeCursor(3, 0); - if (null == lastTwo) return false; - final int length = lastTwo.length(); - if (length < 2) return false; - if (lastTwo.charAt(length - 1) != Constants.CODE_SPACE) { - return false; - } - // We know there is a space in pos -1, and we have at least two chars. If we have only two - // chars, isSurrogatePairs can't return true as charAt(1) is a space, so this is fine. - final int firstCodePoint = - Character.isSurrogatePair(lastTwo.charAt(0), lastTwo.charAt(1)) ? - Character.codePointAt(lastTwo, length - 3) : lastTwo.charAt(length - 2); - if (canBeFollowedByDoubleSpacePeriod(firstCodePoint)) { - cancelDoubleSpacePeriodCountdown(); - mConnection.deleteTextBeforeCursor(1); - final String textToInsert = inputTransaction.mSettingsValues.mSpacingAndPunctuations - .mSentenceSeparatorAndSpace; - mConnection.commitText(textToInsert, 1); - inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); - inputTransaction.setRequiresUpdateSuggestions(); - return true; - } - return false; - } - - /** - * Returns whether this code point can be followed by the double-space-to-period transformation. - * - * See #maybeDoubleSpaceToPeriod for details. - * Generally, most word characters can be followed by the double-space-to-period transformation, - * while most punctuation can't. Some punctuation however does allow for this to take place - * after them, like the closing parenthesis for example. - * - * @param codePoint the code point after which we may want to apply the transformation - * @return whether it's fine to apply the transformation after this code point. - */ - private static boolean canBeFollowedByDoubleSpacePeriod(final int codePoint) { - // TODO: This should probably be a denylist rather than a allowlist. - // TODO: This should probably be language-dependant... - return Character.isLetterOrDigit(codePoint) - || codePoint == Constants.CODE_SINGLE_QUOTE - || codePoint == Constants.CODE_DOUBLE_QUOTE - || codePoint == Constants.CODE_CLOSING_PARENTHESIS - || codePoint == Constants.CODE_CLOSING_SQUARE_BRACKET - || codePoint == Constants.CODE_CLOSING_CURLY_BRACKET - || codePoint == Constants.CODE_CLOSING_ANGLE_BRACKET - || codePoint == Constants.CODE_PLUS - || codePoint == Constants.CODE_PERCENT - || Character.getType(codePoint) == Character.OTHER_SYMBOL; - } - - /** - * Performs a recapitalization event. - * @param settingsValues The current settings values. - */ - private void performRecapitalization(final SettingsValues settingsValues) { - if (!mConnection.hasSelection() || !mRecapitalizeStatus.mIsEnabled()) { - return; // No selection or recapitalize is disabled for now - } - final int selectionStart = mConnection.getExpectedSelectionStart(); - final int selectionEnd = mConnection.getExpectedSelectionEnd(); - final int numCharsSelected = selectionEnd - selectionStart; - if (numCharsSelected > Constants.MAX_CHARACTERS_FOR_RECAPITALIZATION) { - // We bail out if we have too many characters for performance reasons. We don't want - // to suck possibly multiple-megabyte data. - return; - } - // If we have a recapitalize in progress, use it; otherwise, start a new one. - if (!mRecapitalizeStatus.isStarted() - || !mRecapitalizeStatus.isSetAt(selectionStart, selectionEnd)) { - final CharSequence selectedText = - mConnection.getSelectedText(0 /* flags, 0 for no styles */); - if (TextUtils.isEmpty(selectedText)) return; // Race condition with the input connection - mRecapitalizeStatus.start(selectionStart, selectionEnd, selectedText.toString(), - settingsValues.mLocale, - settingsValues.mSpacingAndPunctuations.mSortedWordSeparators); - // We trim leading and trailing whitespace. - mRecapitalizeStatus.trim(); - } - mConnection.finishComposingText(); - mRecapitalizeStatus.rotate(); - mConnection.setSelection(selectionEnd, selectionEnd); - mConnection.deleteTextBeforeCursor(numCharsSelected); - mConnection.commitText(mRecapitalizeStatus.getRecapitalizedString(), 0); - mConnection.setSelection(mRecapitalizeStatus.getNewCursorStart(), - mRecapitalizeStatus.getNewCursorEnd()); - } - - private void performAdditionToUserHistoryDictionary(final SettingsValues settingsValues, - final String suggestion, @Nonnull final NgramContext ngramContext) { - // If correction is not enabled, we don't add words to the user history dictionary. - // That's to avoid unintended additions in some sensitive fields, or fields that - // expect to receive non-words. - if (!settingsValues.mAutoCorrectionEnabledPerUserSettings) return; - if (mConnection.hasSlowInputConnection()) { - // Since we don't unlearn when the user backspaces on a slow InputConnection, - // turn off learning to guard against adding typos that the user later deletes. - Log.w(TAG, "Skipping learning due to slow InputConnection."); - return; - } - - if (TextUtils.isEmpty(suggestion)) return; - final boolean wasAutoCapitalized = - mWordComposer.wasAutoCapitalized() && !mWordComposer.isMostlyCaps(); - final int timeStampInSeconds = (int)TimeUnit.MILLISECONDS.toSeconds( - System.currentTimeMillis()); - mDictionaryFacilitator.addToUserHistory(suggestion, wasAutoCapitalized, - ngramContext, timeStampInSeconds, settingsValues.mBlockPotentiallyOffensive); - } - - public void performUpdateSuggestionStripSync(final SettingsValues settingsValues, - final int inputStyle) { - long startTimeMillis = 0; - if (DebugFlags.DEBUG_ENABLED) { - startTimeMillis = System.currentTimeMillis(); - Log.d(TAG, "performUpdateSuggestionStripSync()"); - } - // Check if we have a suggestion engine attached. - if (!settingsValues.needsToLookupSuggestions()) { - if (mWordComposer.isComposingWord()) { - Log.w(TAG, "Called updateSuggestionsOrPredictions but suggestions were not " - + "requested!"); - } - // Clear the suggestions strip. - mSuggestionStripViewAccessor.showSuggestionStrip(SuggestedWords.getEmptyInstance()); - return; - } - - if (!mWordComposer.isComposingWord() && !settingsValues.mBigramPredictionEnabled) { - mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); - return; - } - - final AsyncResultHolder<SuggestedWords> holder = new AsyncResultHolder<>("Suggest"); - mInputLogicHandler.getSuggestedWords(inputStyle, SuggestedWords.NOT_A_SEQUENCE_NUMBER, - new OnGetSuggestedWordsCallback() { - @Override - public void onGetSuggestedWords(final SuggestedWords suggestedWords) { - final String typedWordString = mWordComposer.getTypedWord(); - final SuggestedWordInfo typedWordInfo = new SuggestedWordInfo( - typedWordString, "" /* prevWordsContext */, - SuggestedWordInfo.MAX_SCORE, - SuggestedWordInfo.KIND_TYPED, Dictionary.DICTIONARY_USER_TYPED, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE); - // Show new suggestions if we have at least one. Otherwise keep the old - // suggestions with the new typed word. Exception: if the length of the - // typed word is <= 1 (after a deletion typically) we clear old suggestions. - if (suggestedWords.size() > 1 || typedWordString.length() <= 1) { - holder.set(suggestedWords); - } else { - holder.set(retrieveOlderSuggestions(typedWordInfo, mSuggestedWords)); - } - } - } - ); - - // This line may cause the current thread to wait. - final SuggestedWords suggestedWords = holder.get(null, - Constants.GET_SUGGESTED_WORDS_TIMEOUT); - if (suggestedWords != null) { - mSuggestionStripViewAccessor.showSuggestionStrip(suggestedWords); - } - if (DebugFlags.DEBUG_ENABLED) { - long runTimeMillis = System.currentTimeMillis() - startTimeMillis; - Log.d(TAG, "performUpdateSuggestionStripSync() : " + runTimeMillis + " ms to finish"); - } - } - - /** - * Check if the cursor is touching a word. If so, restart suggestions on this word, else - * do nothing. - * - * @param settingsValues the current values of the settings. - * @param forStartInput whether we're doing this in answer to starting the input (as opposed - * to a cursor move, for example). In ICS, there is a platform bug that we need to work - * around only when we come here at input start time. - */ - public void restartSuggestionsOnWordTouchedByCursor(final SettingsValues settingsValues, - final boolean forStartInput, - // TODO: remove this argument, put it into settingsValues - final int currentKeyboardScriptId) { - // HACK: We may want to special-case some apps that exhibit bad behavior in case of - // recorrection. This is a temporary, stopgap measure that will be removed later. - // TODO: remove this. - if (settingsValues.isBrokenByRecorrection() - // Recorrection is not supported in languages without spaces because we don't know - // how to segment them yet. - || !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces - // If no suggestions are requested, don't try restarting suggestions. - || !settingsValues.needsToLookupSuggestions() - // If we are currently in a batch input, we must not resume suggestions, or the result - // of the batch input will replace the new composition. This may happen in the corner case - // that the app moves the cursor on its own accord during a batch input. - || mInputLogicHandler.isInBatchInput() - // If the cursor is not touching a word, or if there is a selection, return right away. - || mConnection.hasSelection() - // If we don't know the cursor location, return. - || mConnection.getExpectedSelectionStart() < 0) { - mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); - return; - } - final int expectedCursorPosition = mConnection.getExpectedSelectionStart(); - if (!mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations, - true /* checkTextAfter */)) { - // Show predictions. - mWordComposer.setCapitalizedModeAtStartComposingTime(WordComposer.CAPS_MODE_OFF); - mLatinIME.mHandler.postUpdateSuggestionStrip(SuggestedWords.INPUT_STYLE_RECORRECTION); - return; - } - final TextRange range = mConnection.getWordRangeAtCursor( - settingsValues.mSpacingAndPunctuations, currentKeyboardScriptId); - if (null == range) return; // Happens if we don't have an input connection at all - if (range.length() <= 0) { - // Race condition, or touching a word in a non-supported script. - mLatinIME.setNeutralSuggestionStrip(); - return; - } - // If for some strange reason (editor bug or so) we measure the text before the cursor as - // longer than what the entire text is supposed to be, the safe thing to do is bail out. - if (range.mHasUrlSpans) return; // If there are links, we don't resume suggestions. Making - // edits to a linkified text through batch commands would ruin the URL spans, and unless - // we take very complicated steps to preserve the whole link, we can't do things right so - // we just do not resume because it's safer. - final int numberOfCharsInWordBeforeCursor = range.getNumberOfCharsInWordBeforeCursor(); - if (numberOfCharsInWordBeforeCursor > expectedCursorPosition) return; - final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<>(); - final String typedWordString = range.mWord.toString(); - final SuggestedWordInfo typedWordInfo = new SuggestedWordInfo(typedWordString, - "" /* prevWordsContext */, SuggestedWords.MAX_SUGGESTIONS + 1, - SuggestedWordInfo.KIND_TYPED, Dictionary.DICTIONARY_USER_TYPED, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); - suggestions.add(typedWordInfo); - if (!isResumableWord(settingsValues, typedWordString)) { - mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); - return; - } - int i = 0; - for (final SuggestionSpan span : range.getSuggestionSpansAtWord()) { - for (final String s : span.getSuggestions()) { - ++i; - if (!TextUtils.equals(s, typedWordString)) { - suggestions.add(new SuggestedWordInfo(s, - "" /* prevWordsContext */, SuggestedWords.MAX_SUGGESTIONS - i, - SuggestedWordInfo.KIND_RESUMED, Dictionary.DICTIONARY_RESUMED, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE - /* autoCommitFirstWordConfidence */)); - } - } - } - final int[] codePoints = StringUtils.toCodePointArray(typedWordString); - mWordComposer.setComposingWord(codePoints, - mLatinIME.getCoordinatesForCurrentKeyboard(codePoints)); - mWordComposer.setCursorPositionWithinWord( - typedWordString.codePointCount(0, numberOfCharsInWordBeforeCursor)); - if (forStartInput) { - mConnection.maybeMoveTheCursorAroundAndRestoreToWorkaroundABug(); - } - mConnection.setComposingRegion(expectedCursorPosition - numberOfCharsInWordBeforeCursor, - expectedCursorPosition + range.getNumberOfCharsInWordAfterCursor()); - if (suggestions.size() <= 1) { - // If there weren't any suggestion spans on this word, suggestions#size() will be 1 - // if shouldIncludeResumedWordInSuggestions is true, 0 otherwise. In this case, we - // have no useful suggestions, so we will try to compute some for it instead. - mInputLogicHandler.getSuggestedWords(Suggest.SESSION_ID_TYPING, - SuggestedWords.NOT_A_SEQUENCE_NUMBER, new OnGetSuggestedWordsCallback() { - @Override - public void onGetSuggestedWords(final SuggestedWords suggestedWords) { - doShowSuggestionsAndClearAutoCorrectionIndicator(suggestedWords); - }}); - } else { - // We found suggestion spans in the word. We'll create the SuggestedWords out of - // them, and make willAutoCorrect false. We make typedWordValid false, because the - // color of the word in the suggestion strip changes according to this parameter, - // and false gives the correct color. - final SuggestedWords suggestedWords = new SuggestedWords(suggestions, - null /* rawSuggestions */, typedWordInfo, false /* typedWordValid */, - false /* willAutoCorrect */, false /* isObsoleteSuggestions */, - SuggestedWords.INPUT_STYLE_RECORRECTION, SuggestedWords.NOT_A_SEQUENCE_NUMBER); - doShowSuggestionsAndClearAutoCorrectionIndicator(suggestedWords); - } - } - - void doShowSuggestionsAndClearAutoCorrectionIndicator(final SuggestedWords suggestedWords) { - mIsAutoCorrectionIndicatorOn = false; - mLatinIME.mHandler.showSuggestionStrip(suggestedWords); - } - - /** - * Reverts a previous commit with auto-correction. - * - * This is triggered upon pressing backspace just after a commit with auto-correction. - * - * @param inputTransaction The transaction in progress. - * @param settingsValues the current values of the settings. - */ - private void revertCommit(final InputTransaction inputTransaction, - final SettingsValues settingsValues) { - final CharSequence originallyTypedWord = mLastComposedWord.mTypedWord; - final String originallyTypedWordString = - originallyTypedWord != null ? originallyTypedWord.toString() : ""; - final CharSequence committedWord = mLastComposedWord.mCommittedWord; - final String committedWordString = committedWord.toString(); - final int cancelLength = committedWord.length(); - final String separatorString = mLastComposedWord.mSeparatorString; - // If our separator is a space, we won't actually commit it, - // but set the space state to PHANTOM so that a space will be inserted - // on the next keypress - final boolean usePhantomSpace = separatorString.equals(Constants.STRING_SPACE); - // We want java chars, not codepoints for the following. - final int separatorLength = separatorString.length(); - // TODO: should we check our saved separator against the actual contents of the text view? - final int deleteLength = cancelLength + separatorLength; - if (DebugFlags.DEBUG_ENABLED) { - if (mWordComposer.isComposingWord()) { - throw new RuntimeException("revertCommit, but we are composing a word"); - } - final CharSequence wordBeforeCursor = - mConnection.getTextBeforeCursor(deleteLength, 0).subSequence(0, cancelLength); - if (!TextUtils.equals(committedWord, wordBeforeCursor)) { - throw new RuntimeException("revertCommit check failed: we thought we were " - + "reverting \"" + committedWord - + "\", but before the cursor we found \"" + wordBeforeCursor + "\""); - } - } - mConnection.deleteTextBeforeCursor(deleteLength); - if (!TextUtils.isEmpty(committedWord)) { - unlearnWord(committedWordString, inputTransaction.mSettingsValues, - Constants.EVENT_REVERT); - } - final String stringToCommit = originallyTypedWord + - (usePhantomSpace ? "" : separatorString); - final SpannableString textToCommit = new SpannableString(stringToCommit); - if (committedWord instanceof SpannableString) { - final SpannableString committedWordWithSuggestionSpans = (SpannableString)committedWord; - final Object[] spans = committedWordWithSuggestionSpans.getSpans(0, - committedWord.length(), Object.class); - final int lastCharIndex = textToCommit.length() - 1; - // We will collect all suggestions in the following array. - final ArrayList<String> suggestions = new ArrayList<>(); - // First, add the committed word to the list of suggestions. - suggestions.add(committedWordString); - for (final Object span : spans) { - // If this is a suggestion span, we check that the word is not the committed word. - // That should mostly be the case. - // Given this, we add it to the list of suggestions, otherwise we discard it. - if (span instanceof SuggestionSpan) { - final SuggestionSpan suggestionSpan = (SuggestionSpan)span; - for (final String suggestion : suggestionSpan.getSuggestions()) { - if (!suggestion.equals(committedWordString)) { - suggestions.add(suggestion); - } - } - } else { - // If this is not a suggestion span, we just add it as is. - textToCommit.setSpan(span, 0 /* start */, lastCharIndex /* end */, - committedWordWithSuggestionSpans.getSpanFlags(span)); - } - } - // Add the suggestion list to the list of suggestions. - textToCommit.setSpan(new SuggestionSpan(mLatinIME /* context */, - inputTransaction.mSettingsValues.mLocale, - suggestions.toArray(new String[suggestions.size()]), 0 /* flags */, - null /* notificationTargetClass */), - 0 /* start */, lastCharIndex /* end */, 0 /* flags */); - } - - if (inputTransaction.mSettingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) { - mConnection.commitText(textToCommit, 1); - if (usePhantomSpace) { - mSpaceState = SpaceState.PHANTOM; - } - } else { - // For languages without spaces, we revert the typed string but the cursor is flush - // with the typed word, so we need to resume suggestions right away. - final int[] codePoints = StringUtils.toCodePointArray(stringToCommit); - mWordComposer.setComposingWord(codePoints, - mLatinIME.getCoordinatesForCurrentKeyboard(codePoints)); - setComposingTextInternal(textToCommit, 1); - } - // Don't restart suggestion yet. We'll restart if the user deletes the separator. - mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; - - // We have a separator between the word and the cursor: we should show predictions. - inputTransaction.setRequiresUpdateSuggestions(); - } - - /** - * Factor in auto-caps and manual caps and compute the current caps mode. - * @param settingsValues the current settings values. - * @param keyboardShiftMode the current shift mode of the keyboard. See - * KeyboardSwitcher#getKeyboardShiftMode() for possible values. - * @return the actual caps mode the keyboard is in right now. - */ - private int getActualCapsMode(final SettingsValues settingsValues, - final int keyboardShiftMode) { - if (keyboardShiftMode != WordComposer.CAPS_MODE_AUTO_SHIFTED) { - return keyboardShiftMode; - } - final int auto = getCurrentAutoCapsState(settingsValues); - if (0 != (auto & TextUtils.CAP_MODE_CHARACTERS)) { - return WordComposer.CAPS_MODE_AUTO_SHIFT_LOCKED; - } - if (0 != auto) { - return WordComposer.CAPS_MODE_AUTO_SHIFTED; - } - return WordComposer.CAPS_MODE_OFF; - } - - /** - * Gets the current auto-caps state, factoring in the space state. - * - * This method tries its best to do this in the most efficient possible manner. It avoids - * getting text from the editor if possible at all. - * This is called from the KeyboardSwitcher (through a trampoline in LatinIME) because it - * needs to know auto caps state to display the right layout. - * - * @param settingsValues the relevant settings values - * @return a caps mode from TextUtils.CAP_MODE_* or Constants.TextUtils.CAP_MODE_OFF. - */ - public int getCurrentAutoCapsState(final SettingsValues settingsValues) { - if (!settingsValues.mAutoCap) return Constants.TextUtils.CAP_MODE_OFF; - - final EditorInfo ei = getCurrentInputEditorInfo(); - if (ei == null) return Constants.TextUtils.CAP_MODE_OFF; - final int inputType = ei.inputType; - // Warning: this depends on mSpaceState, which may not be the most current value. If - // mSpaceState gets updated later, whoever called this may need to be told about it. - return mConnection.getCursorCapsMode(inputType, settingsValues.mSpacingAndPunctuations, - SpaceState.PHANTOM == mSpaceState); - } - - public int getCurrentRecapitalizeState() { - if (!mRecapitalizeStatus.isStarted() - || !mRecapitalizeStatus.isSetAt(mConnection.getExpectedSelectionStart(), - mConnection.getExpectedSelectionEnd())) { - // Not recapitalizing at the moment - return RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE; - } - return mRecapitalizeStatus.getCurrentMode(); - } - - /** - * @return the editor info for the current editor - */ - private EditorInfo getCurrentInputEditorInfo() { - return mLatinIME.getCurrentInputEditorInfo(); - } - - /** - * Get n-gram context from the nth previous word before the cursor as context - * for the suggestion process. - * @param spacingAndPunctuations the current spacing and punctuations settings. - * @param nthPreviousWord reverse index of the word to get (1-indexed) - * @return the information of previous words - */ - public NgramContext getNgramContextFromNthPreviousWordForSuggestion( - final SpacingAndPunctuations spacingAndPunctuations, final int nthPreviousWord) { - if (spacingAndPunctuations.mCurrentLanguageHasSpaces) { - // If we are typing in a language with spaces we can just look up the previous - // word information from textview. - return mConnection.getNgramContextFromNthPreviousWord( - spacingAndPunctuations, nthPreviousWord); - } - if (LastComposedWord.NOT_A_COMPOSED_WORD == mLastComposedWord) { - return NgramContext.BEGINNING_OF_SENTENCE; - } - return new NgramContext(new NgramContext.WordInfo( - mLastComposedWord.mCommittedWord.toString())); - } - - /** - * Tests the passed word for resumability. - * - * We can resume suggestions on words whose first code point is a word code point (with some - * nuances: check the code for details). - * - * @param settings the current values of the settings. - * @param word the word to evaluate. - * @return whether it's fine to resume suggestions on this word. - */ - private static boolean isResumableWord(final SettingsValues settings, final String word) { - final int firstCodePoint = word.codePointAt(0); - return settings.isWordCodePoint(firstCodePoint) - && Constants.CODE_SINGLE_QUOTE != firstCodePoint - && Constants.CODE_DASH != firstCodePoint; - } - - /** - * @param actionId the action to perform - */ - private void performEditorAction(final int actionId) { - mConnection.performEditorAction(actionId); - } - - /** - * Perform the processing specific to inputting TLDs. - * - * Some keys input a TLD (specifically, the ".com" key) and this warrants some specific - * processing. First, if this is a TLD, we ignore PHANTOM spaces -- this is done by type - * of character in onCodeInput, but since this gets inputted as a whole string we need to - * do it here specifically. Then, if the last character before the cursor is a period, then - * we cut the dot at the start of ".com". This is because humans tend to type "www.google." - * and then press the ".com" key and instinctively don't expect to get "www.google..com". - * - * @param text the raw text supplied to onTextInput - * @return the text to actually send to the editor - */ - private String performSpecificTldProcessingOnTextInput(final String text) { - if (text.length() <= 1 || text.charAt(0) != Constants.CODE_PERIOD - || !Character.isLetter(text.charAt(1))) { - // Not a tld: do nothing. - return text; - } - // We have a TLD (or something that looks like this): make sure we don't add - // a space even if currently in phantom mode. - mSpaceState = SpaceState.NONE; - final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor(); - // If no code point, #getCodePointBeforeCursor returns NOT_A_CODE_POINT. - if (Constants.CODE_PERIOD == codePointBeforeCursor) { - return text.substring(1); - } - return text; - } - - /** - * Handle a press on the settings key. - */ - private void onSettingsKeyPressed() { - mLatinIME.displaySettingsDialog(); - } - - /** - * Resets the whole input state to the starting state. - * - * This will clear the composing word, reset the last composed word, clear the suggestion - * strip and tell the input connection about it so that it can refresh its caches. - * - * @param newSelStart the new selection start, in java characters. - * @param newSelEnd the new selection end, in java characters. - * @param clearSuggestionStrip whether this method should clear the suggestion strip. - */ - // TODO: how is this different from startInput ?! - private void resetEntireInputState(final int newSelStart, final int newSelEnd, - final boolean clearSuggestionStrip) { - final boolean shouldFinishComposition = mWordComposer.isComposingWord(); - resetComposingState(true /* alsoResetLastComposedWord */); - if (clearSuggestionStrip) { - mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); - } - mConnection.resetCachesUponCursorMoveAndReturnSuccess(newSelStart, newSelEnd, - shouldFinishComposition); - } - - /** - * Resets only the composing state. - * - * Compare #resetEntireInputState, which also clears the suggestion strip and resets the - * input connection caches. This only deals with the composing state. - * - * @param alsoResetLastComposedWord whether to also reset the last composed word. - */ - private void resetComposingState(final boolean alsoResetLastComposedWord) { - mWordComposer.reset(); - if (alsoResetLastComposedWord) { - mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; - } - } - - /** - * Make a {@link com.android.inputmethod.latin.SuggestedWords} object containing a typed word - * and obsolete suggestions. - * See {@link com.android.inputmethod.latin.SuggestedWords#getTypedWordAndPreviousSuggestions( - * SuggestedWordInfo, com.android.inputmethod.latin.SuggestedWords)}. - * @param typedWordInfo The typed word as a SuggestedWordInfo. - * @param previousSuggestedWords The previously suggested words. - * @return Obsolete suggestions with the newly typed word. - */ - static SuggestedWords retrieveOlderSuggestions(final SuggestedWordInfo typedWordInfo, - final SuggestedWords previousSuggestedWords) { - final SuggestedWords oldSuggestedWords = previousSuggestedWords.isPunctuationSuggestions() - ? SuggestedWords.getEmptyInstance() : previousSuggestedWords; - final ArrayList<SuggestedWords.SuggestedWordInfo> typedWordAndPreviousSuggestions = - SuggestedWords.getTypedWordAndPreviousSuggestions(typedWordInfo, oldSuggestedWords); - return new SuggestedWords(typedWordAndPreviousSuggestions, null /* rawSuggestions */, - typedWordInfo, false /* typedWordValid */, false /* hasAutoCorrectionCandidate */, - true /* isObsoleteSuggestions */, oldSuggestedWords.mInputStyle, - SuggestedWords.NOT_A_SEQUENCE_NUMBER); - } - - /** - * @return the {@link Locale} of the {@link #mDictionaryFacilitator} if available. Otherwise - * {@link Locale#ROOT}. - */ - @Nonnull - private Locale getDictionaryFacilitatorLocale() { - return mDictionaryFacilitator != null ? mDictionaryFacilitator.getLocale() : Locale.ROOT; - } - - /** - * Gets a chunk of text with or the auto-correction indicator underline span as appropriate. - * - * This method looks at the old state of the auto-correction indicator to put or not put - * the underline span as appropriate. It is important to note that this does not correspond - * exactly to whether this word will be auto-corrected to or not: what's important here is - * to keep the same indication as before. - * When we add a new code point to a composing word, we don't know yet if we are going to - * auto-correct it until the suggestions are computed. But in the mean time, we still need - * to display the character and to extend the previous underline. To avoid any flickering, - * the underline should keep the same color it used to have, even if that's not ultimately - * the correct color for this new word. When the suggestions are finished evaluating, we - * will call this method again to fix the color of the underline. - * - * @param text the text on which to maybe apply the span. - * @return the same text, with the auto-correction underline span if that's appropriate. - */ - // TODO: Shouldn't this go in some *Utils class instead? - private CharSequence getTextWithUnderline(final String text) { - // TODO: Locale should be determined based on context and the text given. - return mIsAutoCorrectionIndicatorOn - ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline( - mLatinIME, text, getDictionaryFacilitatorLocale()) - : text; - } - - /** - * Sends a DOWN key event followed by an UP key event to the editor. - * - * If possible at all, avoid using this method. It causes all sorts of race conditions with - * the text view because it goes through a different, asynchronous binder. Also, batch edits - * are ignored for key events. Use the normal software input methods instead. - * - * @param keyCode the key code to send inside the key event. - */ - private void sendDownUpKeyEvent(final int keyCode) { - final long eventTime = SystemClock.uptimeMillis(); - mConnection.sendKeyEvent(new KeyEvent(eventTime, eventTime, - KeyEvent.ACTION_DOWN, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, - KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE)); - mConnection.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime, - KeyEvent.ACTION_UP, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, - KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE)); - } - - /** - * Sends a code point to the editor, using the most appropriate method. - * - * Normally we send code points with commitText, but there are some cases (where backward - * compatibility is a concern for example) where we want to use deprecated methods. - * - * @param settingsValues the current values of the settings. - * @param codePoint the code point to send. - */ - // TODO: replace these two parameters with an InputTransaction - private void sendKeyCodePoint(final SettingsValues settingsValues, final int codePoint) { - // TODO: Remove this special handling of digit letters. - // For backward compatibility. See {@link InputMethodService#sendKeyChar(char)}. - if (codePoint >= '0' && codePoint <= '9') { - sendDownUpKeyEvent(codePoint - '0' + KeyEvent.KEYCODE_0); - return; - } - - // TODO: we should do this also when the editor has TYPE_NULL - if (Constants.CODE_ENTER == codePoint && settingsValues.isBeforeJellyBean()) { - // Backward compatibility mode. Before Jelly bean, the keyboard would simulate - // a hardware keyboard event on pressing enter or delete. This is bad for many - // reasons (there are race conditions with commits) but some applications are - // relying on this behavior so we continue to support it for older apps. - sendDownUpKeyEvent(KeyEvent.KEYCODE_ENTER); - } else { - mConnection.commitText(StringUtils.newSingleCodePointString(codePoint), 1); - } - } - - /** - * Insert an automatic space, if the options allow it. - * - * This checks the options and the text before the cursor are appropriate before inserting - * an automatic space. - * - * @param settingsValues the current values of the settings. - */ - private void insertAutomaticSpaceIfOptionsAndTextAllow(final SettingsValues settingsValues) { - if (settingsValues.shouldInsertSpacesAutomatically() - && settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces - && !mConnection.textBeforeCursorLooksLikeURL()) { - sendKeyCodePoint(settingsValues, Constants.CODE_SPACE); - } - } - - /** - * Do the final processing after a batch input has ended. This commits the word to the editor. - * @param settingsValues the current values of the settings. - * @param suggestedWords suggestedWords to use. - */ - public void onUpdateTailBatchInputCompleted(final SettingsValues settingsValues, - final SuggestedWords suggestedWords, final KeyboardSwitcher keyboardSwitcher) { - final String batchInputText = suggestedWords.isEmpty() ? null : suggestedWords.getWord(0); - if (TextUtils.isEmpty(batchInputText)) { - return; - } - mConnection.beginBatchEdit(); - if (SpaceState.PHANTOM == mSpaceState) { - insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues); - } - mWordComposer.setBatchInputWord(batchInputText); - setComposingTextInternal(batchInputText, 1); - mConnection.endBatchEdit(); - // Space state must be updated before calling updateShiftState - mSpaceState = SpaceState.PHANTOM; - keyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(settingsValues), - getCurrentRecapitalizeState()); - } - - /** - * Commit the typed string to the editor. - * - * This is typically called when we should commit the currently composing word without applying - * auto-correction to it. Typically, we come here upon pressing a separator when the keyboard - * is configured to not do auto-correction at all (because of the settings or the properties of - * the editor). In this case, `separatorString' is set to the separator that was pressed. - * We also come here in a variety of cases with external user action. For example, when the - * cursor is moved while there is a composition, or when the keyboard is closed, or when the - * user presses the Send button for an SMS, we don't auto-correct as that would be unexpected. - * In this case, `separatorString' is set to NOT_A_SEPARATOR. - * - * @param settingsValues the current values of the settings. - * @param separatorString the separator that's causing the commit, or NOT_A_SEPARATOR if none. - */ - public void commitTyped(final SettingsValues settingsValues, final String separatorString) { - if (!mWordComposer.isComposingWord()) return; - final String typedWord = mWordComposer.getTypedWord(); - if (typedWord.length() > 0) { - final boolean isBatchMode = mWordComposer.isBatchMode(); - commitChosenWord(settingsValues, typedWord, - LastComposedWord.COMMIT_TYPE_USER_TYPED_WORD, separatorString); - StatsUtils.onWordCommitUserTyped(typedWord, isBatchMode); - } - } - - /** - * Commit the current auto-correction. - * - * This will commit the best guess of the keyboard regarding what the user meant by typing - * the currently composing word. The IME computes suggestions and assigns a confidence score - * to each of them; when it's confident enough in one suggestion, it replaces the typed string - * by this suggestion at commit time. When it's not confident enough, or when it has no - * suggestions, or when the settings or environment does not allow for auto-correction, then - * this method just commits the typed string. - * Note that if suggestions are currently being computed in the background, this method will - * block until the computation returns. This is necessary for consistency (it would be very - * strange if pressing space would commit a different word depending on how fast you press). - * - * @param settingsValues the current value of the settings. - * @param separator the separator that's causing the commit to happen. - */ - private void commitCurrentAutoCorrection(final SettingsValues settingsValues, - final String separator, final LatinIME.UIHandler handler) { - // Complete any pending suggestions query first - if (handler.hasPendingUpdateSuggestions()) { - handler.cancelUpdateSuggestionStrip(); - // To know the input style here, we should retrieve the in-flight "update suggestions" - // message and read its arg1 member here. However, the Handler class does not let - // us retrieve this message, so we can't do that. But in fact, we notice that - // we only ever come here when the input style was typing. In the case of batch - // input, we update the suggestions synchronously when the tail batch comes. Likewise - // for application-specified completions. As for recorrections, we never auto-correct, - // so we don't come here either. Hence, the input style is necessarily - // INPUT_STYLE_TYPING. - performUpdateSuggestionStripSync(settingsValues, SuggestedWords.INPUT_STYLE_TYPING); - } - final SuggestedWordInfo autoCorrectionOrNull = mWordComposer.getAutoCorrectionOrNull(); - final String typedWord = mWordComposer.getTypedWord(); - final String stringToCommit = (autoCorrectionOrNull != null) - ? autoCorrectionOrNull.mWord : typedWord; - if (stringToCommit != null) { - if (TextUtils.isEmpty(typedWord)) { - throw new RuntimeException("We have an auto-correction but the typed word " - + "is empty? Impossible! I must commit suicide."); - } - final boolean isBatchMode = mWordComposer.isBatchMode(); - commitChosenWord(settingsValues, stringToCommit, - LastComposedWord.COMMIT_TYPE_DECIDED_WORD, separator); - if (!typedWord.equals(stringToCommit)) { - // This will make the correction flash for a short while as a visual clue - // to the user that auto-correction happened. It has no other effect; in particular - // note that this won't affect the text inside the text field AT ALL: it only makes - // the segment of text starting at the supplied index and running for the length - // of the auto-correction flash. At this moment, the "typedWord" argument is - // ignored by TextView. - mConnection.commitCorrection(new CorrectionInfo( - mConnection.getExpectedSelectionEnd() - stringToCommit.length(), - typedWord, stringToCommit)); - String prevWordsContext = (autoCorrectionOrNull != null) - ? autoCorrectionOrNull.mPrevWordsContext - : ""; - StatsUtils.onAutoCorrection(typedWord, stringToCommit, isBatchMode, - mDictionaryFacilitator, prevWordsContext); - StatsUtils.onWordCommitAutoCorrect(stringToCommit, isBatchMode); - } else { - StatsUtils.onWordCommitUserTyped(stringToCommit, isBatchMode); - } - } - } - - /** - * Commits the chosen word to the text field and saves it for later retrieval. - * - * @param settingsValues the current values of the settings. - * @param chosenWord the word we want to commit. - * @param commitType the type of the commit, as one of LastComposedWord.COMMIT_TYPE_* - * @param separatorString the separator that's causing the commit, or NOT_A_SEPARATOR if none. - */ - private void commitChosenWord(final SettingsValues settingsValues, final String chosenWord, - final int commitType, final String separatorString) { - long startTimeMillis = 0; - if (DebugFlags.DEBUG_ENABLED) { - startTimeMillis = System.currentTimeMillis(); - Log.d(TAG, "commitChosenWord() : [" + chosenWord + "]"); - } - final SuggestedWords suggestedWords = mSuggestedWords; - // TODO: Locale should be determined based on context and the text given. - final Locale locale = getDictionaryFacilitatorLocale(); - final CharSequence chosenWordWithSuggestions = chosenWord; - // b/21926256 - // SuggestionSpanUtils.getTextWithSuggestionSpan(mLatinIME, chosenWord, - // suggestedWords, locale); - if (DebugFlags.DEBUG_ENABLED) { - long runTimeMillis = System.currentTimeMillis() - startTimeMillis; - Log.d(TAG, "commitChosenWord() : " + runTimeMillis + " ms to run " - + "SuggestionSpanUtils.getTextWithSuggestionSpan()"); - startTimeMillis = System.currentTimeMillis(); - } - // When we are composing word, get n-gram context from the 2nd previous word because the - // 1st previous word is the word to be committed. Otherwise get n-gram context from the 1st - // previous word. - final NgramContext ngramContext = mConnection.getNgramContextFromNthPreviousWord( - settingsValues.mSpacingAndPunctuations, mWordComposer.isComposingWord() ? 2 : 1); - if (DebugFlags.DEBUG_ENABLED) { - long runTimeMillis = System.currentTimeMillis() - startTimeMillis; - Log.d(TAG, "commitChosenWord() : " + runTimeMillis + " ms to run " - + "Connection.getNgramContextFromNthPreviousWord()"); - Log.d(TAG, "commitChosenWord() : NgramContext = " + ngramContext); - startTimeMillis = System.currentTimeMillis(); - } - mConnection.commitText(chosenWordWithSuggestions, 1); - if (DebugFlags.DEBUG_ENABLED) { - long runTimeMillis = System.currentTimeMillis() - startTimeMillis; - Log.d(TAG, "commitChosenWord() : " + runTimeMillis + " ms to run " - + "Connection.commitText"); - startTimeMillis = System.currentTimeMillis(); - } - // Add the word to the user history dictionary - performAdditionToUserHistoryDictionary(settingsValues, chosenWord, ngramContext); - if (DebugFlags.DEBUG_ENABLED) { - long runTimeMillis = System.currentTimeMillis() - startTimeMillis; - Log.d(TAG, "commitChosenWord() : " + runTimeMillis + " ms to run " - + "performAdditionToUserHistoryDictionary()"); - startTimeMillis = System.currentTimeMillis(); - } - // TODO: figure out here if this is an auto-correct or if the best word is actually - // what user typed. Note: currently this is done much later in - // LastComposedWord#didCommitTypedWord by string equality of the remembered - // strings. - mLastComposedWord = mWordComposer.commitWord(commitType, - chosenWordWithSuggestions, separatorString, ngramContext); - if (DebugFlags.DEBUG_ENABLED) { - long runTimeMillis = System.currentTimeMillis() - startTimeMillis; - Log.d(TAG, "commitChosenWord() : " + runTimeMillis + " ms to run " - + "WordComposer.commitWord()"); - startTimeMillis = System.currentTimeMillis(); - } - } - - /** - * Retry resetting caches in the rich input connection. - * - * When the editor can't be accessed we can't reset the caches, so we schedule a retry. - * This method handles the retry, and re-schedules a new retry if we still can't access. - * We only retry up to 5 times before giving up. - * - * @param tryResumeSuggestions Whether we should resume suggestions or not. - * @param remainingTries How many times we may try again before giving up. - * @return whether true if the caches were successfully reset, false otherwise. - */ - public boolean retryResetCachesAndReturnSuccess(final boolean tryResumeSuggestions, - final int remainingTries, final LatinIME.UIHandler handler) { - final boolean shouldFinishComposition = mConnection.hasSelection() - || !mConnection.isCursorPositionKnown(); - if (!mConnection.resetCachesUponCursorMoveAndReturnSuccess( - mConnection.getExpectedSelectionStart(), mConnection.getExpectedSelectionEnd(), - shouldFinishComposition)) { - if (0 < remainingTries) { - handler.postResetCaches(tryResumeSuggestions, remainingTries - 1); - return false; - } - // If remainingTries is 0, we should stop waiting for new tries, however we'll still - // return true as we need to perform other tasks (for example, loading the keyboard). - } - mConnection.tryFixLyingCursorPosition(); - if (tryResumeSuggestions) { - handler.postResumeSuggestions(true /* shouldDelay */); - } - return true; - } - - public void getSuggestedWords(final SettingsValues settingsValues, - final Keyboard keyboard, final int keyboardShiftMode, final int inputStyle, - final int sequenceNumber, final OnGetSuggestedWordsCallback callback) { - mWordComposer.adviseCapitalizedModeBeforeFetchingSuggestions( - getActualCapsMode(settingsValues, keyboardShiftMode)); - mSuggest.getSuggestedWords(mWordComposer, - getNgramContextFromNthPreviousWordForSuggestion( - settingsValues.mSpacingAndPunctuations, - // 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. - mWordComposer.isComposingWord() ? 2 : 1), - keyboard, - new SettingsValuesForSuggestion(settingsValues.mBlockPotentiallyOffensive), - settingsValues.mAutoCorrectionEnabledPerUserSettings, - inputStyle, sequenceNumber, callback); - } - - /** - * Used as an injection point for each call of - * {@link RichInputConnection#setComposingText(CharSequence, int)}. - * - * <p>Currently using this method is optional and you can still directly call - * {@link RichInputConnection#setComposingText(CharSequence, int)}, but it is recommended to - * use this method whenever possible.<p> - * <p>TODO: Should we move this mechanism to {@link RichInputConnection}?</p> - * - * @param newComposingText the composing text to be set - * @param newCursorPosition the new cursor position - */ - private void setComposingTextInternal(final CharSequence newComposingText, - final int newCursorPosition) { - setComposingTextInternalWithBackgroundColor(newComposingText, newCursorPosition, - Color.TRANSPARENT, newComposingText.length()); - } - - /** - * Equivalent to {@link #setComposingTextInternal(CharSequence, int)} except that this method - * allows to set {@link BackgroundColorSpan} to the composing text with the given color. - * - * <p>TODO: Currently the background color is exclusive with the black underline, which is - * automatically added by the framework. We need to change the framework if we need to have both - * of them at the same time.</p> - * <p>TODO: Should we move this method to {@link RichInputConnection}?</p> - * - * @param newComposingText the composing text to be set - * @param newCursorPosition the new cursor position - * @param backgroundColor the background color to be set to the composing text. Set - * {@link Color#TRANSPARENT} to disable the background color. - * @param coloredTextLength the length of text, in Java chars, which should be rendered with - * the given background color. - */ - private void setComposingTextInternalWithBackgroundColor(final CharSequence newComposingText, - final int newCursorPosition, final int backgroundColor, final int coloredTextLength) { - final CharSequence composingTextToBeSet; - if (backgroundColor == Color.TRANSPARENT) { - composingTextToBeSet = newComposingText; - } else { - final SpannableString spannable = new SpannableString(newComposingText); - final BackgroundColorSpan backgroundColorSpan = - new BackgroundColorSpan(backgroundColor); - final int spanLength = Math.min(coloredTextLength, spannable.length()); - spannable.setSpan(backgroundColorSpan, 0, spanLength, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); - composingTextToBeSet = spannable; - } - mConnection.setComposingText(composingTextToBeSet, newCursorPosition); - } - - /** - * Gets an object allowing private IME commands to be sent to the - * underlying editor. - * @return An object for sending private commands to the underlying editor. - */ - public PrivateCommandPerformer getPrivateCommandPerformer() { - return mConnection; - } - - /** - * Gets the expected index of the first char of the composing span within the editor's text. - * Returns a negative value in case there appears to be no valid composing span. - * - * @see #getComposingLength() - * @see RichInputConnection#hasSelection() - * @see RichInputConnection#isCursorPositionKnown() - * @see RichInputConnection#getExpectedSelectionStart() - * @see RichInputConnection#getExpectedSelectionEnd() - * @return The expected index in Java chars of the first char of the composing span. - */ - // TODO: try and see if we can get rid of this method. Ideally the users of this class should - // never need to know this. - public int getComposingStart() { - if (!mConnection.isCursorPositionKnown() || mConnection.hasSelection()) { - return -1; - } - return mConnection.getExpectedSelectionStart() - mWordComposer.size(); - } - - /** - * Gets the expected length in Java chars of the composing span. - * May be 0 if there is no valid composing span. - * @see #getComposingStart() - * @return The expected length of the composing span. - */ - // TODO: try and see if we can get rid of this method. Ideally the users of this class should - // never need to know this. - public int getComposingLength() { - return mWordComposer.size(); - } -} diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java deleted file mode 100644 index ddc4ad99c..000000000 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright (C) 2013 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.inputlogic; - -import android.os.Handler; -import android.os.HandlerThread; -import android.os.Message; - -import com.android.inputmethod.compat.LooperCompatUtils; -import com.android.inputmethod.latin.LatinIME; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback; -import com.android.inputmethod.latin.common.InputPointers; - -/** - * A helper to manage deferred tasks for the input logic. - */ -class InputLogicHandler implements Handler.Callback { - final Handler mNonUIThreadHandler; - // TODO: remove this reference. - final LatinIME mLatinIME; - final InputLogic mInputLogic; - private final Object mLock = new Object(); - private boolean mInBatchInput; // synchronized using {@link #mLock}. - - private static final int MSG_GET_SUGGESTED_WORDS = 1; - - // A handler that never does anything. This is used for cases where events come before anything - // is initialized, though probably only the monkey can actually do this. - public static final InputLogicHandler NULL_HANDLER = new InputLogicHandler() { - @Override - public void reset() {} - @Override - public boolean handleMessage(final Message msg) { return true; } - @Override - public void onStartBatchInput() {} - @Override - public void onUpdateBatchInput(final InputPointers batchPointers, - final int sequenceNumber) {} - @Override - public void onCancelBatchInput() {} - @Override - public void updateTailBatchInput(final InputPointers batchPointers, - final int sequenceNumber) {} - @Override - public void getSuggestedWords(final int sessionId, final int sequenceNumber, - final OnGetSuggestedWordsCallback callback) {} - }; - - InputLogicHandler() { - mNonUIThreadHandler = null; - mLatinIME = null; - mInputLogic = null; - } - - public InputLogicHandler(final LatinIME latinIME, final InputLogic inputLogic) { - final HandlerThread handlerThread = new HandlerThread( - InputLogicHandler.class.getSimpleName()); - handlerThread.start(); - mNonUIThreadHandler = new Handler(handlerThread.getLooper(), this); - mLatinIME = latinIME; - mInputLogic = inputLogic; - } - - public void reset() { - mNonUIThreadHandler.removeCallbacksAndMessages(null); - } - - // In unit tests, we create several instances of LatinIME, which results in several instances - // of InputLogicHandler. To avoid these handlers lingering, we call this. - public void destroy() { - LooperCompatUtils.quitSafely(mNonUIThreadHandler.getLooper()); - } - - /** - * Handle a message. - * @see android.os.Handler.Callback#handleMessage(android.os.Message) - */ - // Called on the Non-UI handler thread by the Handler code. - @Override - public boolean handleMessage(final Message msg) { - switch (msg.what) { - case MSG_GET_SUGGESTED_WORDS: - mLatinIME.getSuggestedWords(msg.arg1 /* inputStyle */, - msg.arg2 /* sequenceNumber */, (OnGetSuggestedWordsCallback) msg.obj); - break; - } - return true; - } - - // Called on the UI thread by InputLogic. - public void onStartBatchInput() { - synchronized (mLock) { - mInBatchInput = true; - } - } - - public boolean isInBatchInput() { - return mInBatchInput; - } - - /** - * Fetch suggestions corresponding to an update of a batch input. - * @param batchPointers the updated pointers, including the part that was passed last time. - * @param sequenceNumber the sequence number associated with this batch input. - * @param isTailBatchInput true if this is the end of a batch input, false if it's an update. - */ - // This method can be called from any thread and will see to it that the correct threads - // are used for parts that require it. This method will send a message to the Non-UI handler - // thread to pull suggestions, and get the inlined callback to get called on the Non-UI - // handler thread. If this is the end of a batch input, the callback will then proceed to - // send a message to the UI handler in LatinIME so that showing suggestions can be done on - // the UI thread. - private void updateBatchInput(final InputPointers batchPointers, - final int sequenceNumber, final boolean isTailBatchInput) { - synchronized (mLock) { - if (!mInBatchInput) { - // Batch input has ended or canceled while the message was being delivered. - return; - } - mInputLogic.mWordComposer.setBatchInputPointers(batchPointers); - final OnGetSuggestedWordsCallback callback = new OnGetSuggestedWordsCallback() { - @Override - public void onGetSuggestedWords(final SuggestedWords suggestedWords) { - showGestureSuggestionsWithPreviewVisuals(suggestedWords, isTailBatchInput); - } - }; - getSuggestedWords(isTailBatchInput ? SuggestedWords.INPUT_STYLE_TAIL_BATCH - : SuggestedWords.INPUT_STYLE_UPDATE_BATCH, sequenceNumber, callback); - } - } - - void showGestureSuggestionsWithPreviewVisuals(final SuggestedWords suggestedWordsForBatchInput, - final boolean isTailBatchInput) { - final SuggestedWords suggestedWordsToShowSuggestions; - // We're now inside the callback. This always runs on the Non-UI thread, - // no matter what thread updateBatchInput was originally called on. - if (suggestedWordsForBatchInput.isEmpty()) { - // Use old suggestions if we don't have any new ones. - // Previous suggestions are found in InputLogic#mSuggestedWords. - // Since these are the most recent ones and we just recomputed - // new ones to update them, then the previous ones are there. - suggestedWordsToShowSuggestions = mInputLogic.mSuggestedWords; - } else { - suggestedWordsToShowSuggestions = suggestedWordsForBatchInput; - } - mLatinIME.mHandler.showGesturePreviewAndSuggestionStrip(suggestedWordsToShowSuggestions, - isTailBatchInput /* dismissGestureFloatingPreviewText */); - if (isTailBatchInput) { - mInBatchInput = false; - // The following call schedules onEndBatchInputInternal - // to be called on the UI thread. - mLatinIME.mHandler.showTailBatchInputResult(suggestedWordsToShowSuggestions); - } - } - - /** - * Update a batch input. - * - * This fetches suggestions and updates the suggestion strip and the floating text preview. - * - * @param batchPointers the updated batch pointers. - * @param sequenceNumber the sequence number associated with this batch input. - */ - // Called on the UI thread by InputLogic. - public void onUpdateBatchInput(final InputPointers batchPointers, - final int sequenceNumber) { - updateBatchInput(batchPointers, sequenceNumber, false /* isTailBatchInput */); - } - - /** - * Cancel a batch input. - * - * Note that as opposed to updateTailBatchInput, we do the UI side of this immediately on the - * same thread, rather than get this to call a method in LatinIME. This is because - * canceling a batch input does not necessitate the long operation of pulling suggestions. - */ - // Called on the UI thread by InputLogic. - public void onCancelBatchInput() { - synchronized (mLock) { - mInBatchInput = false; - } - } - - /** - * Trigger an update for a tail batch input. - * - * A tail batch input is the last update for a gesture, the one that is triggered after the - * user lifts their finger. This method schedules fetching suggestions on the non-UI thread, - * then when the suggestions are computed it comes back on the UI thread to update the - * suggestion strip, commit the first suggestion, and dismiss the floating text preview. - * - * @param batchPointers the updated batch pointers. - * @param sequenceNumber the sequence number associated with this batch input. - */ - // Called on the UI thread by InputLogic. - public void updateTailBatchInput(final InputPointers batchPointers, - final int sequenceNumber) { - updateBatchInput(batchPointers, sequenceNumber, true /* isTailBatchInput */); - } - - public void getSuggestedWords(final int inputStyle, final int sequenceNumber, - final OnGetSuggestedWordsCallback callback) { - mNonUIThreadHandler.obtainMessage( - MSG_GET_SUGGESTED_WORDS, inputStyle, sequenceNumber, callback).sendToTarget(); - } -} diff --git a/java/src/com/android/inputmethod/latin/inputlogic/PrivateCommandPerformer.java b/java/src/com/android/inputmethod/latin/inputlogic/PrivateCommandPerformer.java deleted file mode 100644 index 42eaa9c82..000000000 --- a/java/src/com/android/inputmethod/latin/inputlogic/PrivateCommandPerformer.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2014 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.inputlogic; - -import android.os.Bundle; - -/** - * Provides an interface matching - * {@link android.view.inputmethod.InputConnection#performPrivateCommand(String,Bundle)}. - */ -public interface PrivateCommandPerformer { - /** - * API to send private commands from an input method to its connected - * editor. This can be used to provide domain-specific features that are - * only known between certain input methods and their clients. - * - * @param action Name of the command to be performed. This must be a scoped - * name, i.e. prefixed with a package name you own, so that - * different developers will not create conflicting commands. - * @param data Any data to include with the command. - * @return true if the command was sent (regardless of whether the - * associated editor understood it), false if the input connection is no - * longer valid. - */ - boolean performPrivateCommand(String action, Bundle data); -} diff --git a/java/src/com/android/inputmethod/latin/inputlogic/SpaceState.java b/java/src/com/android/inputmethod/latin/inputlogic/SpaceState.java deleted file mode 100644 index ce80c0016..000000000 --- a/java/src/com/android/inputmethod/latin/inputlogic/SpaceState.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2013 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.inputlogic; - -/** - * Class for managing space states. - * - * At any given time, the input logic is in one of five possible space states. Depending on the - * current space state, some behavior will change; the prime example of this is the PHANTOM state, - * in which any subsequent letter input will input a space before the letter. Read on the - * description inside this class for each of the space states. - */ -public class SpaceState { - // None: the state where all the keyboard behavior is the most "standard" and no automatic - // input is added or removed. In this state, all self-inserting keys only insert themselves, - // and backspace removes one character. - public static final int NONE = 0; - // Double space: the state where the user pressed space twice quickly, which LatinIME - // resolved as period-space. In this state, pressing backspace will undo the - // double-space-to-period insertion: it will replace ". " with " ". - public static final int DOUBLE = 1; - // Swap punctuation: the state where a weak space and a punctuation from the suggestion strip - // have just been swapped. In this state, pressing backspace will undo the swap: the - // characters will be swapped back back, and the space state will go to WEAK. - public static final int SWAP_PUNCTUATION = 2; - // Weak space: a space that should be swapped only by suggestion strip punctuation. Weak - // spaces happen when the user presses space, accepting the current suggestion (whether - // it's an auto-correction or not). In this state, pressing a punctuation from the suggestion - // strip inserts it before the space (while it inserts it after the space in the NONE state). - public static final int WEAK = 3; - // Phantom space: a not-yet-inserted space that should get inserted on the next input, - // character provided it's not a separator. If it's a separator, the phantom space is dropped. - // Phantom spaces happen when a user chooses a word from the suggestion strip. In this state, - // non-separators insert a space before they get inserted. - public static final int PHANTOM = 4; - - private SpaceState() { - // This class is not publicly instantiable. - } -} diff --git a/java/src/com/android/inputmethod/latin/makedict/DictionaryHeader.java b/java/src/com/android/inputmethod/latin/makedict/DictionaryHeader.java deleted file mode 100644 index 4d253b0cb..000000000 --- a/java/src/com/android/inputmethod/latin/makedict/DictionaryHeader.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2014 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.makedict; - -import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions; -import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Class representing dictionary header. - */ -public final class DictionaryHeader { - public final int mBodyOffset; - @Nonnull - public final DictionaryOptions mDictionaryOptions; - @Nonnull - public final FormatOptions mFormatOptions; - @Nonnull - public final String mLocaleString; - @Nonnull - public final String mVersionString; - @Nonnull - public final String mIdString; - - // Note that these are corresponding definitions in native code in latinime::HeaderPolicy - // and latinime::HeaderReadWriteUtils. - // TODO: Standardize the key names and bump up the format version, taking care not to - // break format version 2 dictionaries. - public static final String DICTIONARY_VERSION_KEY = "version"; - public static final String DICTIONARY_LOCALE_KEY = "locale"; - public static final String DICTIONARY_ID_KEY = "dictionary"; - public static final String DICTIONARY_DESCRIPTION_KEY = "description"; - public static final String DICTIONARY_DATE_KEY = "date"; - public static final String HAS_HISTORICAL_INFO_KEY = "HAS_HISTORICAL_INFO"; - public static final String USES_FORGETTING_CURVE_KEY = "USES_FORGETTING_CURVE"; - public static final String FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY = - "FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID"; - public static final String MAX_UNIGRAM_COUNT_KEY = "MAX_UNIGRAM_ENTRY_COUNT"; - public static final String MAX_BIGRAM_COUNT_KEY = "MAX_BIGRAM_ENTRY_COUNT"; - public static final String MAX_TRIGRAM_COUNT_KEY = "MAX_TRIGRAM_ENTRY_COUNT"; - public static final String ATTRIBUTE_VALUE_TRUE = "1"; - public static final String CODE_POINT_TABLE_KEY = "codePointTable"; - - public DictionaryHeader(final int headerSize, - @Nonnull final DictionaryOptions dictionaryOptions, - @Nonnull final FormatOptions formatOptions) throws UnsupportedFormatException { - mDictionaryOptions = dictionaryOptions; - mFormatOptions = formatOptions; - mBodyOffset = formatOptions.mVersion < FormatSpec.VERSION4 ? headerSize : 0; - final String localeString = dictionaryOptions.mAttributes.get(DICTIONARY_LOCALE_KEY); - if (null == localeString) { - throw new UnsupportedFormatException("Cannot create a FileHeader without a locale"); - } - final String versionString = dictionaryOptions.mAttributes.get(DICTIONARY_VERSION_KEY); - if (null == versionString) { - throw new UnsupportedFormatException( - "Cannot create a FileHeader without a version"); - } - final String idString = dictionaryOptions.mAttributes.get(DICTIONARY_ID_KEY); - if (null == idString) { - throw new UnsupportedFormatException("Cannot create a FileHeader without an ID"); - } - mLocaleString = localeString; - mVersionString = versionString; - mIdString = idString; - } - - // Helper method to get the description - @Nullable - public String getDescription() { - // TODO: Right now each dictionary file comes with a description in its own language. - // It will display as is no matter the device's locale. It should be internationalized. - return mDictionaryOptions.mAttributes.get(DICTIONARY_DESCRIPTION_KEY); - } -}
\ No newline at end of file diff --git a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java deleted file mode 100644 index 288261bf0..000000000 --- a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java +++ /dev/null @@ -1,310 +0,0 @@ -/* - * 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.makedict; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; - -import java.util.Date; -import java.util.HashMap; - -/** - * Dictionary File Format Specification. - */ -public final class FormatSpec { - - /* - * File header layout is as follows: - * - * v | - * e | MAGIC_NUMBER + version of the file format, 2 bytes. - * r | - * sion - * - * o | - * p | not used, 2 bytes. - * o | - * nflags - * - * h | - * e | size of the file header, 4bytes - * a | including the size of the magic number, the option flags and the header size - * d | - * ersize - * - * attributes list - * - * attributes list is: - * <key> = | string of characters at the char format described below, with the terminator used - * | to signal the end of the string. - * <value> = | string of characters at the char format described below, with the terminator used - * | to signal the end of the string. - * if the size of already read < headersize, goto key. - * - */ - - /* - * Node array (FusionDictionary.PtNodeArray) layout is as follows: - * - * n | - * o | the number of PtNodes, 1 or 2 bytes. - * d | 1 byte = bbbbbbbb match - * e | case 1xxxxxxx => xxxxxxx << 8 + next byte - * c | otherwise => bbbbbbbb - * o | - * unt - * - * n | - * o | sequence of PtNodes, - * d | the layout of each PtNode is described below. - * e | - * s - * - * f | - * o | forward link address, 3byte - * r | 1 byte = bbbbbbbb match - * w | case 1xxxxxxx => -((xxxxxxx << 16) + (next byte << 8) + next byte) - * a | otherwise => (xxxxxxx << 16) + (next byte << 8) + next byte - * r | - * dlinkaddress - */ - - /* Node (FusionDictionary.PtNode) layout is as follows: - * | CHILDREN_ADDRESS_TYPE 2 bits, 11 : FLAG_CHILDREN_ADDRESS_TYPE_THREEBYTES - * | 10 : FLAG_CHILDREN_ADDRESS_TYPE_TWOBYTES - * f | 01 : FLAG_CHILDREN_ADDRESS_TYPE_ONEBYTE - * l | 00 : FLAG_CHILDREN_ADDRESS_TYPE_NOADDRESS - * a | has several chars ? 1 bit, 1 = yes, 0 = no : FLAG_HAS_MULTIPLE_CHARS - * g | has a terminal ? 1 bit, 1 = yes, 0 = no : FLAG_IS_TERMINAL - * s | has shortcut targets ? 1 bit, 1 = yes, 0 = no : FLAG_HAS_SHORTCUT_TARGETS - * | has bigrams ? 1 bit, 1 = yes, 0 = no : FLAG_HAS_BIGRAMS - * | is not a word ? 1 bit, 1 = yes, 0 = no : FLAG_IS_NOT_A_WORD - * | is possibly offensive ? 1 bit, 1 = yes, 0 = no : FLAG_IS_POSSIBLY_OFFENSIVE - * - * c | IF FLAG_HAS_MULTIPLE_CHARS - * h | char, char, char, char n * (1 or 3 bytes) : use PtNodeInfo for i/o helpers - * a | end 1 byte, = 0 - * r | ELSE - * s | char 1 or 3 bytes - * | END - * - * f | - * r | IF FLAG_IS_TERMINAL - * e | frequency 1 byte - * q | - * - * c | - * h | children address, CHILDREN_ADDRESS_TYPE bytes - * i | This address is relative to the position of this field. - * l | - * drenaddress - * - * | IF FLAG_IS_TERMINAL && FLAG_HAS_SHORTCUT_TARGETS - * | shortcut string list - * | IF FLAG_IS_TERMINAL && FLAG_HAS_BIGRAMS - * | bigrams address list - * - * Char format is: - * 1 byte = bbbbbbbb match - * case 000xxxxx: xxxxx << 16 + next byte << 8 + next byte - * else: if 00011111 (= 0x1F) : this is the terminator. This is a relevant choice because - * unicode code points range from 0 to 0x10FFFF, so any 3-byte value starting with - * 00011111 would be outside unicode. - * else: iso-latin-1 code - * This allows for the whole unicode range to be encoded, including chars outside of - * the BMP. Also everything in the iso-latin-1 charset is only 1 byte, except control - * characters which should never happen anyway (and still work, but take 3 bytes). - * - * bigram address list is: - * <flags> = | hasNext = 1 bit, 1 = yes, 0 = no : FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT - * | addressSign = 1 bit, : FLAG_BIGRAM_ATTR_OFFSET_NEGATIVE - * | 1 = must take -address, 0 = must take +address - * | xx : mask with MASK_BIGRAM_ATTR_ADDRESS_TYPE - * | addressFormat = 2 bits, 00 = unused : FLAG_BIGRAM_ATTR_ADDRESS_TYPE_ONEBYTE - * | 01 = 1 byte : FLAG_BIGRAM_ATTR_ADDRESS_TYPE_ONEBYTE - * | 10 = 2 bytes : FLAG_BIGRAM_ATTR_ADDRESS_TYPE_TWOBYTES - * | 11 = 3 bytes : FLAG_BIGRAM_ATTR_ADDRESS_TYPE_THREEBYTES - * | 4 bits : frequency : mask with FLAG_BIGRAM_SHORTCUT_ATTR_FREQUENCY - * <address> | IF (01 == FLAG_BIGRAM_ATTR_ADDRESS_TYPE_ONEBYTE == addressFormat) - * | read 1 byte, add top 4 bits - * | ELSIF (10 == FLAG_BIGRAM_ATTR_ADDRESS_TYPE_TWOBYTES == addressFormat) - * | read 2 bytes, add top 4 bits - * | ELSE // 11 == FLAG_BIGRAM_ATTR_ADDRESS_TYPE_THREEBYTES == addressFormat - * | read 3 bytes, add top 4 bits - * | END - * | if (FLAG_BIGRAM_ATTR_OFFSET_NEGATIVE) then address = -address - * if (FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT) goto bigram_and_shortcut_address_list_is - * - * shortcut string list is: - * <byte size> = PTNODE_SHORTCUT_LIST_SIZE_SIZE bytes, big-endian: size of the list, in bytes. - * <flags> = | hasNext = 1 bit, 1 = yes, 0 = no : FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT - * | reserved = 3 bits, must be 0 - * | 4 bits : frequency : mask with FLAG_BIGRAM_SHORTCUT_ATTR_FREQUENCY - * <shortcut> = | string of characters at the char format described above, with the terminator - * | used to signal the end of the string. - * if (FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT goto flags - */ - - public static final int MAGIC_NUMBER = 0x9BC13AFE; - static final int NOT_A_VERSION_NUMBER = -1; - - // These MUST have the same values as the relevant constants in format_utils.h. - // From version 2.01 on, we use version * 100 + revision as a version number. That allows - // us to change the format during development while having testing devices remove - // older files with each upgrade, while still having a readable versioning scheme. - // When we bump up the dictionary format version, we should update - // ExpandableDictionary.needsToMigrateDictionary() and - // ExpandableDictionary.matchesExpectedBinaryDictFormatVersionForThisType(). - public static final int VERSION2 = 2; - public static final int VERSION201 = 201; - public static final int VERSION202 = 202; - // format version for Fava Dictionaries. - public static final int VERSION_DELIGHT3 = 86736212; - public static final int MINIMUM_SUPPORTED_VERSION_OF_CODE_POINT_TABLE = VERSION201; - // Dictionary version used for testing. - public static final int VERSION4_ONLY_FOR_TESTING = 399; - public static final int VERSION402 = 402; - public static final int VERSION403 = 403; - public static final int VERSION4 = VERSION403; - public static final int MINIMUM_SUPPORTED_STATIC_VERSION = VERSION202; - public static final int MAXIMUM_SUPPORTED_STATIC_VERSION = VERSION_DELIGHT3; - static final int MINIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION4; - static final int MAXIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION403; - - // TODO: Make this value adaptative to content data, store it in the header, and - // use it in the reading code. - static final int MAX_WORD_LENGTH = DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH; - - // These flags are used only in the static dictionary. - static final int MASK_CHILDREN_ADDRESS_TYPE = 0xC0; - static final int FLAG_CHILDREN_ADDRESS_TYPE_NOADDRESS = 0x00; - static final int FLAG_CHILDREN_ADDRESS_TYPE_ONEBYTE = 0x40; - static final int FLAG_CHILDREN_ADDRESS_TYPE_TWOBYTES = 0x80; - static final int FLAG_CHILDREN_ADDRESS_TYPE_THREEBYTES = 0xC0; - - static final int FLAG_HAS_MULTIPLE_CHARS = 0x20; - - static final int FLAG_IS_TERMINAL = 0x10; - static final int FLAG_HAS_SHORTCUT_TARGETS = 0x08; - static final int FLAG_HAS_BIGRAMS = 0x04; - static final int FLAG_IS_NOT_A_WORD = 0x02; - static final int FLAG_IS_POSSIBLY_OFFENSIVE = 0x01; - - static final int FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT = 0x80; - static final int FLAG_BIGRAM_ATTR_OFFSET_NEGATIVE = 0x40; - static final int MASK_BIGRAM_ATTR_ADDRESS_TYPE = 0x30; - static final int FLAG_BIGRAM_ATTR_ADDRESS_TYPE_ONEBYTE = 0x10; - static final int FLAG_BIGRAM_ATTR_ADDRESS_TYPE_TWOBYTES = 0x20; - static final int FLAG_BIGRAM_ATTR_ADDRESS_TYPE_THREEBYTES = 0x30; - static final int FLAG_BIGRAM_SHORTCUT_ATTR_FREQUENCY = 0x0F; - - static final int PTNODE_CHARACTERS_TERMINATOR = 0x1F; - - static final int PTNODE_TERMINATOR_SIZE = 1; - static final int PTNODE_FLAGS_SIZE = 1; - static final int PTNODE_FREQUENCY_SIZE = 1; - static final int PTNODE_MAX_ADDRESS_SIZE = 3; - static final int PTNODE_ATTRIBUTE_FLAGS_SIZE = 1; - static final int PTNODE_ATTRIBUTE_MAX_ADDRESS_SIZE = 3; - static final int PTNODE_SHORTCUT_LIST_SIZE_SIZE = 2; - - static final int NO_CHILDREN_ADDRESS = Integer.MIN_VALUE; - static final int INVALID_CHARACTER = -1; - - static final int MAX_PTNODES_FOR_ONE_BYTE_PTNODE_COUNT = 0x7F; // 127 - // Large PtNode array size field size is 2 bytes. - static final int LARGE_PTNODE_ARRAY_SIZE_FIELD_SIZE_FLAG = 0x8000; - static final int MAX_PTNODES_IN_A_PT_NODE_ARRAY = 0x7FFF; // 32767 - static final int MAX_BIGRAMS_IN_A_PTNODE = 10000; - static final int MAX_SHORTCUT_LIST_SIZE_IN_A_PTNODE = 0xFFFF; - - static final int MAX_TERMINAL_FREQUENCY = 255; - static final int MAX_BIGRAM_FREQUENCY = 15; - - public static final int SHORTCUT_WHITELIST_FREQUENCY = 15; - - // This option needs to be the same numeric value as the one in binary_format.h. - static final int NOT_VALID_WORD = -99; - - static final int UINT8_MAX = 0xFF; - static final int UINT16_MAX = 0xFFFF; - static final int UINT24_MAX = 0xFFFFFF; - static final int MSB8 = 0x80; - static final int MINIMAL_ONE_BYTE_CHARACTER_VALUE = 0x20; - static final int MAXIMAL_ONE_BYTE_CHARACTER_VALUE = 0xFF; - - /** - * Options about file format. - */ - public static final class FormatOptions { - public final int mVersion; - public final boolean mHasTimestamp; - - @UsedForTesting - public FormatOptions(final int version) { - this(version, false /* hasTimestamp */); - } - - public FormatOptions(final int version, final boolean hasTimestamp) { - mVersion = version; - mHasTimestamp = hasTimestamp; - } - } - - /** - * Options global to the dictionary. - */ - public static final class DictionaryOptions { - public final HashMap<String, String> mAttributes; - public DictionaryOptions(final HashMap<String, String> attributes) { - mAttributes = attributes; - } - @Override - public String toString() { // Convenience method - return toString(0, false); - } - public String toString(final int indentCount, final boolean plumbing) { - final StringBuilder indent = new StringBuilder(); - if (plumbing) { - indent.append("H:"); - } else { - for (int i = 0; i < indentCount; ++i) { - indent.append(" "); - } - } - final StringBuilder s = new StringBuilder(); - for (final String optionKey : mAttributes.keySet()) { - s.append(indent); - s.append(optionKey); - s.append(" = "); - if ("date".equals(optionKey) && !plumbing) { - // Date needs a number of milliseconds, but the dictionary contains seconds - s.append(new Date( - 1000 * Long.parseLong(mAttributes.get(optionKey))).toString()); - } else { - s.append(mAttributes.get(optionKey)); - } - s.append("\n"); - } - return s.toString(); - } - } - - private FormatSpec() { - // This utility class is not publicly instantiable. - } -} diff --git a/java/src/com/android/inputmethod/latin/makedict/NgramProperty.java b/java/src/com/android/inputmethod/latin/makedict/NgramProperty.java deleted file mode 100644 index b1d19dc3c..000000000 --- a/java/src/com/android/inputmethod/latin/makedict/NgramProperty.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2014 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.makedict; - -import com.android.inputmethod.latin.NgramContext; - -public class NgramProperty { - public final WeightedString mTargetWord; - public final NgramContext mNgramContext; - - public NgramProperty(final WeightedString targetWord, final NgramContext ngramContext) { - mTargetWord = targetWord; - mNgramContext = ngramContext; - } - - @Override - public int hashCode() { - return mTargetWord.hashCode() ^ mNgramContext.hashCode(); - } - - @Override - public boolean equals(Object o) { - if (o == this) return true; - if (!(o instanceof NgramProperty)) return false; - final NgramProperty n = (NgramProperty)o; - return mTargetWord.equals(n.mTargetWord) && mNgramContext.equals(n.mNgramContext); - } -} diff --git a/java/src/com/android/inputmethod/latin/makedict/ProbabilityInfo.java b/java/src/com/android/inputmethod/latin/makedict/ProbabilityInfo.java deleted file mode 100644 index 03c2ece1d..000000000 --- a/java/src/com/android/inputmethod/latin/makedict/ProbabilityInfo.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2014 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.makedict; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.BinaryDictionary; -import com.android.inputmethod.latin.utils.CombinedFormatUtils; - -import java.util.Arrays; - -public final class ProbabilityInfo { - public final int mProbability; - // mTimestamp, mLevel and mCount are historical info. These values are depend on the - // implementation in native code; thus, we must not use them and have any assumptions about - // them except for tests. - public final int mTimestamp; - public final int mLevel; - public final int mCount; - - @UsedForTesting - public static ProbabilityInfo max(final ProbabilityInfo probabilityInfo1, - final ProbabilityInfo probabilityInfo2) { - if (probabilityInfo1 == null) { - return probabilityInfo2; - } - if (probabilityInfo2 == null) { - return probabilityInfo1; - } - return (probabilityInfo1.mProbability > probabilityInfo2.mProbability) ? probabilityInfo1 - : probabilityInfo2; - } - - public ProbabilityInfo(final int probability) { - this(probability, BinaryDictionary.NOT_A_VALID_TIMESTAMP, 0, 0); - } - - public ProbabilityInfo(final int probability, final int timestamp, final int level, - final int count) { - mProbability = probability; - mTimestamp = timestamp; - mLevel = level; - mCount = count; - } - - public boolean hasHistoricalInfo() { - return mTimestamp != BinaryDictionary.NOT_A_VALID_TIMESTAMP; - } - - @Override - public int hashCode() { - if (hasHistoricalInfo()) { - return Arrays.hashCode(new Object[] { mProbability, mTimestamp, mLevel, mCount }); - } - return Arrays.hashCode(new Object[] { mProbability }); - } - - @Override - public String toString() { - return CombinedFormatUtils.formatProbabilityInfo(this); - } - - @Override - public boolean equals(Object o) { - if (o == this) return true; - if (!(o instanceof ProbabilityInfo)) return false; - final ProbabilityInfo p = (ProbabilityInfo)o; - if (!hasHistoricalInfo() && !p.hasHistoricalInfo()) { - return mProbability == p.mProbability; - } - return mProbability == p.mProbability && mTimestamp == p.mTimestamp && mLevel == p.mLevel - && mCount == p.mCount; - } -}
\ No newline at end of file diff --git a/java/src/com/android/inputmethod/latin/makedict/UnsupportedFormatException.java b/java/src/com/android/inputmethod/latin/makedict/UnsupportedFormatException.java deleted file mode 100644 index 4f3861526..000000000 --- a/java/src/com/android/inputmethod/latin/makedict/UnsupportedFormatException.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.makedict; - -/** - * Simple exception thrown when a file format is not recognized. - */ -public final class UnsupportedFormatException extends Exception { - public UnsupportedFormatException(String description) { - super(description); - } -} diff --git a/java/src/com/android/inputmethod/latin/makedict/WeightedString.java b/java/src/com/android/inputmethod/latin/makedict/WeightedString.java deleted file mode 100644 index f6782df9e..000000000 --- a/java/src/com/android/inputmethod/latin/makedict/WeightedString.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2014 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.makedict; - -import com.android.inputmethod.annotations.UsedForTesting; - -import java.util.Arrays; - -/** - * A string with a probability. - * - * This represents an "attribute", that is either a bigram or a shortcut. - */ -public final class WeightedString { - public final String mWord; - public ProbabilityInfo mProbabilityInfo; - - public WeightedString(final String word, final int probability) { - this(word, new ProbabilityInfo(probability)); - } - - public WeightedString(final String word, final ProbabilityInfo probabilityInfo) { - mWord = word; - mProbabilityInfo = probabilityInfo; - } - - @UsedForTesting - public int getProbability() { - return mProbabilityInfo.mProbability; - } - - public void setProbability(final int probability) { - mProbabilityInfo = new ProbabilityInfo(probability); - } - - @Override - public int hashCode() { - return Arrays.hashCode(new Object[] { mWord, mProbabilityInfo}); - } - - @Override - public boolean equals(Object o) { - if (o == this) return true; - if (!(o instanceof WeightedString)) return false; - final WeightedString w = (WeightedString)o; - return mWord.equals(w.mWord) && mProbabilityInfo.equals(w.mProbabilityInfo); - } -}
\ No newline at end of file diff --git a/java/src/com/android/inputmethod/latin/makedict/WordProperty.java b/java/src/com/android/inputmethod/latin/makedict/WordProperty.java deleted file mode 100644 index 264e75710..000000000 --- a/java/src/com/android/inputmethod/latin/makedict/WordProperty.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.makedict; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.BinaryDictionary; -import com.android.inputmethod.latin.Dictionary; -import com.android.inputmethod.latin.NgramContext; -import com.android.inputmethod.latin.NgramContext.WordInfo; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.utils.CombinedFormatUtils; - -import java.util.ArrayList; -import java.util.Arrays; - -import javax.annotation.Nullable; - -/** - * Utility class for a word with a probability. - * - * This is chiefly used to iterate a dictionary. - */ -public final class WordProperty implements Comparable<WordProperty> { - public final String mWord; - public final ProbabilityInfo mProbabilityInfo; - public final ArrayList<NgramProperty> mNgrams; - // TODO: Support mIsBeginningOfSentence. - public final boolean mIsBeginningOfSentence; - public final boolean mIsNotAWord; - public final boolean mIsPossiblyOffensive; - public final boolean mHasNgrams; - - private int mHashCode = 0; - - // TODO: Support n-gram. - @UsedForTesting - public WordProperty(final String word, final ProbabilityInfo probabilityInfo, - @Nullable final ArrayList<WeightedString> bigrams, - final boolean isNotAWord, final boolean isPossiblyOffensive) { - mWord = word; - mProbabilityInfo = probabilityInfo; - if (null == bigrams) { - mNgrams = null; - } else { - mNgrams = new ArrayList<>(); - final NgramContext ngramContext = new NgramContext(new WordInfo(mWord)); - for (final WeightedString bigramTarget : bigrams) { - mNgrams.add(new NgramProperty(bigramTarget, ngramContext)); - } - } - mIsBeginningOfSentence = false; - mIsNotAWord = isNotAWord; - mIsPossiblyOffensive = isPossiblyOffensive; - mHasNgrams = bigrams != null && !bigrams.isEmpty(); - } - - private static ProbabilityInfo createProbabilityInfoFromArray(final int[] probabilityInfo) { - return new ProbabilityInfo( - probabilityInfo[BinaryDictionary.FORMAT_WORD_PROPERTY_PROBABILITY_INDEX], - probabilityInfo[BinaryDictionary.FORMAT_WORD_PROPERTY_TIMESTAMP_INDEX], - probabilityInfo[BinaryDictionary.FORMAT_WORD_PROPERTY_LEVEL_INDEX], - probabilityInfo[BinaryDictionary.FORMAT_WORD_PROPERTY_COUNT_INDEX]); - } - - // Construct word property using information from native code. - // This represents invalid word when the probability is BinaryDictionary.NOT_A_PROBABILITY. - public WordProperty(final int[] codePoints, final boolean isNotAWord, - final boolean isPossiblyOffensive, final boolean hasBigram, - final boolean isBeginningOfSentence, final int[] probabilityInfo, - final ArrayList<int[][]> ngramPrevWordsArray, - final ArrayList<boolean[]> ngramPrevWordIsBeginningOfSentenceArray, - final ArrayList<int[]> ngramTargets, final ArrayList<int[]> ngramProbabilityInfo) { - mWord = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints); - mProbabilityInfo = createProbabilityInfoFromArray(probabilityInfo); - final ArrayList<NgramProperty> ngrams = new ArrayList<>(); - mIsBeginningOfSentence = isBeginningOfSentence; - mIsNotAWord = isNotAWord; - mIsPossiblyOffensive = isPossiblyOffensive; - mHasNgrams = hasBigram; - - final int relatedNgramCount = ngramTargets.size(); - for (int i = 0; i < relatedNgramCount; i++) { - final String ngramTargetString = - StringUtils.getStringFromNullTerminatedCodePointArray(ngramTargets.get(i)); - final WeightedString ngramTarget = new WeightedString(ngramTargetString, - createProbabilityInfoFromArray(ngramProbabilityInfo.get(i))); - final int[][] prevWords = ngramPrevWordsArray.get(i); - final boolean[] isBeginningOfSentenceArray = - ngramPrevWordIsBeginningOfSentenceArray.get(i); - final WordInfo[] wordInfoArray = new WordInfo[prevWords.length]; - for (int j = 0; j < prevWords.length; j++) { - wordInfoArray[j] = isBeginningOfSentenceArray[j] - ? WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO - : new WordInfo(StringUtils.getStringFromNullTerminatedCodePointArray( - prevWords[j])); - } - final NgramContext ngramContext = new NgramContext(wordInfoArray); - ngrams.add(new NgramProperty(ngramTarget, ngramContext)); - } - mNgrams = ngrams.isEmpty() ? null : ngrams; - } - - // TODO: Remove - @UsedForTesting - public ArrayList<WeightedString> getBigrams() { - if (null == mNgrams) { - return null; - } - final ArrayList<WeightedString> bigrams = new ArrayList<>(); - for (final NgramProperty ngram : mNgrams) { - if (ngram.mNgramContext.getPrevWordCount() == 1) { - bigrams.add(ngram.mTargetWord); - } - } - return bigrams; - } - - public int getProbability() { - return mProbabilityInfo.mProbability; - } - - private static int computeHashCode(WordProperty word) { - return Arrays.hashCode(new Object[] { - word.mWord, - word.mProbabilityInfo, - word.mNgrams, - word.mIsNotAWord, - word.mIsPossiblyOffensive - }); - } - - /** - * Three-way comparison. - * - * A Word x is greater than a word y if x has a higher frequency. If they have the same - * frequency, they are sorted in lexicographic order. - */ - @Override - public int compareTo(final WordProperty w) { - if (getProbability() < w.getProbability()) return 1; - if (getProbability() > w.getProbability()) return -1; - return mWord.compareTo(w.mWord); - } - - /** - * Equality test. - * - * Words are equal if they have the same frequency, the same spellings, and the same - * attributes. - */ - @Override - public boolean equals(Object o) { - if (o == this) return true; - if (!(o instanceof WordProperty)) return false; - WordProperty w = (WordProperty)o; - return mProbabilityInfo.equals(w.mProbabilityInfo) - && mWord.equals(w.mWord) && equals(mNgrams, w.mNgrams) - && mIsNotAWord == w.mIsNotAWord && mIsPossiblyOffensive == w.mIsPossiblyOffensive - && mHasNgrams == w.mHasNgrams; - } - - // TDOO: Have a utility method like java.util.Objects.equals. - private static <T> boolean equals(final ArrayList<T> a, final ArrayList<T> b) { - if (null == a) { - return null == b; - } - return a.equals(b); - } - - @Override - public int hashCode() { - if (mHashCode == 0) { - mHashCode = computeHashCode(this); - } - return mHashCode; - } - - @UsedForTesting - public boolean isValid() { - return getProbability() != Dictionary.NOT_A_PROBABILITY; - } - - @Override - public String toString() { - return CombinedFormatUtils.formatWordProperty(this); - } -} diff --git a/java/src/com/android/inputmethod/latin/network/AuthException.java b/java/src/com/android/inputmethod/latin/network/AuthException.java deleted file mode 100644 index 1bce4c156..000000000 --- a/java/src/com/android/inputmethod/latin/network/AuthException.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2014 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.network; - -/** - * Authentication exception. When this exception is thrown, the client may - * try to refresh the authentication token and try again. - */ -public class AuthException extends Exception { - public AuthException() { - super(); - } - - public AuthException(Throwable throwable) { - super(throwable); - } - - public AuthException(String detailMessage) { - super(detailMessage); - } -}
\ No newline at end of file diff --git a/java/src/com/android/inputmethod/latin/network/BlockingHttpClient.java b/java/src/com/android/inputmethod/latin/network/BlockingHttpClient.java deleted file mode 100644 index 079d07eac..000000000 --- a/java/src/com/android/inputmethod/latin/network/BlockingHttpClient.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2014 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.network; - -import android.util.Log; - -import java.io.BufferedOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.HttpURLConnection; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * A client for executing HTTP requests synchronously. - * This must never be called from the main thread. - */ -public class BlockingHttpClient { - private static final boolean DEBUG = false; - private static final String TAG = BlockingHttpClient.class.getSimpleName(); - - private final HttpURLConnection mConnection; - - /** - * Interface that handles processing the response for a request. - */ - public interface ResponseProcessor<T> { - /** - * Called when the HTTP request finishes successfully. - * The {@link InputStream} is closed by the client after the method finishes, - * so any processing must be done in this method itself. - * - * @param response An input stream that can be used to read the HTTP response. - */ - T onSuccess(InputStream response) throws IOException; - } - - public BlockingHttpClient(HttpURLConnection connection) { - mConnection = connection; - } - - /** - * Executes the request on the underlying {@link HttpURLConnection}. - * - * @param request The request payload, if any, or null. - * @param responseProcessor A processor for the HTTP response. - */ - public <T> T execute(@Nullable byte[] request, @Nonnull ResponseProcessor<T> responseProcessor) - throws IOException, AuthException, HttpException { - if (DEBUG) { - Log.d(TAG, "execute: " + mConnection.getURL()); - } - try { - if (request != null) { - if (DEBUG) { - Log.d(TAG, "request size: " + request.length); - } - OutputStream out = new BufferedOutputStream(mConnection.getOutputStream()); - out.write(request); - out.flush(); - out.close(); - } - - final int responseCode = mConnection.getResponseCode(); - if (responseCode != HttpURLConnection.HTTP_OK) { - Log.w(TAG, "Response error: " + responseCode + ", Message: " - + mConnection.getResponseMessage()); - if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) { - throw new AuthException(mConnection.getResponseMessage()); - } - throw new HttpException(responseCode); - } - if (DEBUG) { - Log.d(TAG, "request executed successfully"); - } - return responseProcessor.onSuccess(mConnection.getInputStream()); - } finally { - mConnection.disconnect(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/network/HttpException.java b/java/src/com/android/inputmethod/latin/network/HttpException.java deleted file mode 100644 index b9d8b6372..000000000 --- a/java/src/com/android/inputmethod/latin/network/HttpException.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2014 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.network; - -import com.android.inputmethod.annotations.UsedForTesting; - -/** - * The HttpException exception represents a XML/HTTP fault with a HTTP status code. - */ -public class HttpException extends Exception { - - /** - * The HTTP status code. - */ - private final int mStatusCode; - - /** - * @param statusCode int HTTP status code. - */ - public HttpException(int statusCode) { - super("Response Code: " + statusCode); - mStatusCode = statusCode; - } - - /** - * @return the HTTP status code related to this exception. - */ - @UsedForTesting - public int getHttpStatusCode() { - return mStatusCode; - } -}
\ No newline at end of file diff --git a/java/src/com/android/inputmethod/latin/network/HttpUrlConnectionBuilder.java b/java/src/com/android/inputmethod/latin/network/HttpUrlConnectionBuilder.java deleted file mode 100644 index df54bf464..000000000 --- a/java/src/com/android/inputmethod/latin/network/HttpUrlConnectionBuilder.java +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Copyright (C) 2014 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.network; - -import android.text.TextUtils; - -import com.android.inputmethod.annotations.UsedForTesting; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.HashMap; -import java.util.Map.Entry; - -/** - * Builder for {@link HttpURLConnection}s. - * - * TODO: Remove @UsedForTesting after this is actually used. - */ -@UsedForTesting -public class HttpUrlConnectionBuilder { - private static final int DEFAULT_TIMEOUT_MILLIS = 5 * 1000; - - /** - * Request header key for authentication. - */ - public static final String HTTP_HEADER_AUTHORIZATION = "Authorization"; - - /** - * Request header key for cache control. - */ - public static final String KEY_CACHE_CONTROL = "Cache-Control"; - /** - * Request header value for cache control indicating no caching. - * @see #KEY_CACHE_CONTROL - */ - public static final String VALUE_NO_CACHE = "no-cache"; - - /** - * Indicates that the request is unidirectional - upload-only. - * TODO: Remove @UsedForTesting after this is actually used. - */ - @UsedForTesting - public static final int MODE_UPLOAD_ONLY = 1; - /** - * Indicates that the request is unidirectional - download only. - * TODO: Remove @UsedForTesting after this is actually used. - */ - @UsedForTesting - public static final int MODE_DOWNLOAD_ONLY = 2; - /** - * Indicates that the request is bi-directional. - * TODO: Remove @UsedForTesting after this is actually used. - */ - @UsedForTesting - public static final int MODE_BI_DIRECTIONAL = 3; - - private final HashMap<String, String> mHeaderMap = new HashMap<>(); - - private URL mUrl; - private int mConnectTimeoutMillis = DEFAULT_TIMEOUT_MILLIS; - private int mReadTimeoutMillis = DEFAULT_TIMEOUT_MILLIS; - private int mContentLength = -1; - private boolean mUseCache; - private int mMode; - - /** - * Sets the URL that'll be used for the request. - * This *must* be set before calling {@link #build()} - * - * TODO: Remove @UsedForTesting after this method is actually used. - */ - @UsedForTesting - public HttpUrlConnectionBuilder setUrl(String url) throws MalformedURLException { - if (TextUtils.isEmpty(url)) { - throw new IllegalArgumentException("URL must not be empty"); - } - mUrl = new URL(url); - return this; - } - - /** - * Sets the connect timeout. Defaults to {@value #DEFAULT_TIMEOUT_MILLIS} milliseconds. - * - * TODO: Remove @UsedForTesting after this method is actually used. - */ - @UsedForTesting - public HttpUrlConnectionBuilder setConnectTimeout(int timeoutMillis) { - if (timeoutMillis < 0) { - throw new IllegalArgumentException("connect-timeout must be >= 0, but was " - + timeoutMillis); - } - mConnectTimeoutMillis = timeoutMillis; - return this; - } - - /** - * Sets the read timeout. Defaults to {@value #DEFAULT_TIMEOUT_MILLIS} milliseconds. - * - * TODO: Remove @UsedForTesting after this method is actually used. - */ - @UsedForTesting - public HttpUrlConnectionBuilder setReadTimeout(int timeoutMillis) { - if (timeoutMillis < 0) { - throw new IllegalArgumentException("read-timeout must be >= 0, but was " - + timeoutMillis); - } - mReadTimeoutMillis = timeoutMillis; - return this; - } - - /** - * Adds an entry to the request header. - * - * TODO: Remove @UsedForTesting after this method is actually used. - */ - @UsedForTesting - public HttpUrlConnectionBuilder addHeader(String key, String value) { - mHeaderMap.put(key, value); - return this; - } - - /** - * Sets an authentication token. - * - * TODO: Remove @UsedForTesting after this method is actually used. - */ - @UsedForTesting - public HttpUrlConnectionBuilder setAuthToken(String value) { - mHeaderMap.put(HTTP_HEADER_AUTHORIZATION, value); - return this; - } - - /** - * Sets the request to be executed such that the input is not buffered. - * This may be set when the request size is known beforehand. - * - * TODO: Remove @UsedForTesting after this method is actually used. - */ - @UsedForTesting - public HttpUrlConnectionBuilder setFixedLengthForStreaming(int length) { - mContentLength = length; - return this; - } - - /** - * Indicates if the request can use cached responses or not. - * - * TODO: Remove @UsedForTesting after this method is actually used. - */ - @UsedForTesting - public HttpUrlConnectionBuilder setUseCache(boolean useCache) { - mUseCache = useCache; - return this; - } - - /** - * The request mode. - * Sets the request mode to be one of: upload-only, download-only or bidirectional. - * - * @see #MODE_UPLOAD_ONLY - * @see #MODE_DOWNLOAD_ONLY - * @see #MODE_BI_DIRECTIONAL - * - * TODO: Remove @UsedForTesting after this method is actually used - */ - @UsedForTesting - public HttpUrlConnectionBuilder setMode(int mode) { - if (mode != MODE_UPLOAD_ONLY - && mode != MODE_DOWNLOAD_ONLY - && mode != MODE_BI_DIRECTIONAL) { - throw new IllegalArgumentException("Invalid mode specified:" + mode); - } - mMode = mode; - return this; - } - - /** - * Builds the {@link HttpURLConnection} instance that can be used to execute the request. - * - * TODO: Remove @UsedForTesting after this method is actually used. - */ - @UsedForTesting - public HttpURLConnection build() throws IOException { - if (mUrl == null) { - throw new IllegalArgumentException("A URL must be specified!"); - } - final HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); - connection.setConnectTimeout(mConnectTimeoutMillis); - connection.setReadTimeout(mReadTimeoutMillis); - connection.setUseCaches(mUseCache); - switch (mMode) { - case MODE_UPLOAD_ONLY: - connection.setDoInput(true); - connection.setDoOutput(false); - break; - case MODE_DOWNLOAD_ONLY: - connection.setDoInput(false); - connection.setDoOutput(true); - break; - case MODE_BI_DIRECTIONAL: - connection.setDoInput(true); - connection.setDoOutput(true); - break; - } - for (final Entry<String, String> entry : mHeaderMap.entrySet()) { - connection.addRequestProperty(entry.getKey(), entry.getValue()); - } - if (mContentLength >= 0) { - connection.setFixedLengthStreamingMode(mContentLength); - } - return connection; - } -}
\ No newline at end of file diff --git a/java/src/com/android/inputmethod/latin/permissions/PermissionsActivity.java b/java/src/com/android/inputmethod/latin/permissions/PermissionsActivity.java deleted file mode 100644 index 36d8ed943..000000000 --- a/java/src/com/android/inputmethod/latin/permissions/PermissionsActivity.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2015 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.permissions; - - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.core.app.ActivityCompat; - -/** - * An activity to help request permissions. It's used when no other activity is available, e.g. in - * InputMethodService. This activity assumes that all permissions are not granted yet. - */ -public final class PermissionsActivity - extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback { - - /** - * Key to retrieve requested permissions from the intent. - */ - public static final String EXTRA_PERMISSION_REQUESTED_PERMISSIONS = "requested_permissions"; - - /** - * Key to retrieve request code from the intent. - */ - public static final String EXTRA_PERMISSION_REQUEST_CODE = "request_code"; - - private static final int INVALID_REQUEST_CODE = -1; - - private int mPendingRequestCode = INVALID_REQUEST_CODE; - - /** - * Starts a PermissionsActivity and checks/requests supplied permissions. - */ - public static void run( - @NonNull Context context, int requestCode, @NonNull String... permissionStrings) { - Intent intent = new Intent(context.getApplicationContext(), PermissionsActivity.class); - intent.putExtra(EXTRA_PERMISSION_REQUESTED_PERMISSIONS, permissionStrings); - intent.putExtra(EXTRA_PERMISSION_REQUEST_CODE, requestCode); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); - context.startActivity(intent); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mPendingRequestCode = (savedInstanceState != null) - ? savedInstanceState.getInt(EXTRA_PERMISSION_REQUEST_CODE, INVALID_REQUEST_CODE) - : INVALID_REQUEST_CODE; - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putInt(EXTRA_PERMISSION_REQUEST_CODE, mPendingRequestCode); - } - - @Override - protected void onResume() { - super.onResume(); - // Only do request when there is no pending request to avoid duplicated requests. - if (mPendingRequestCode == INVALID_REQUEST_CODE) { - final Bundle extras = getIntent().getExtras(); - final String[] permissionsToRequest = - extras.getStringArray(EXTRA_PERMISSION_REQUESTED_PERMISSIONS); - mPendingRequestCode = extras.getInt(EXTRA_PERMISSION_REQUEST_CODE); - // Assuming that all supplied permissions are not granted yet, so that we don't need to - // check them again. - PermissionsUtil.requestPermissions(this, mPendingRequestCode, permissionsToRequest); - } - } - - @Override - public void onRequestPermissionsResult( - int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - mPendingRequestCode = INVALID_REQUEST_CODE; - PermissionsManager.get(this).onRequestPermissionsResult( - requestCode, permissions, grantResults); - finish(); - } -} diff --git a/java/src/com/android/inputmethod/latin/permissions/PermissionsManager.java b/java/src/com/android/inputmethod/latin/permissions/PermissionsManager.java deleted file mode 100644 index 08c623ab5..000000000 --- a/java/src/com/android/inputmethod/latin/permissions/PermissionsManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2015 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.permissions; - -import android.app.Activity; -import android.content.Context; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Manager to perform permission related tasks. Always call on the UI thread. - */ -public class PermissionsManager { - - public interface PermissionsResultCallback { - void onRequestPermissionsResult(boolean allGranted); - } - - private int mRequestCodeId; - - private final Context mContext; - private final Map<Integer, PermissionsResultCallback> mRequestIdToCallback = new HashMap<>(); - - private static PermissionsManager sInstance; - - public PermissionsManager(Context context) { - mContext = context; - } - - @Nonnull - public static synchronized PermissionsManager get(@Nonnull Context context) { - if (sInstance == null) { - sInstance = new PermissionsManager(context); - } - return sInstance; - } - - private synchronized int getNextRequestId() { - return ++mRequestCodeId; - } - - - public synchronized void requestPermissions(@Nonnull PermissionsResultCallback callback, - @Nullable Activity activity, - String... permissionsToRequest) { - List<String> deniedPermissions = PermissionsUtil.getDeniedPermissions( - mContext, permissionsToRequest); - if (deniedPermissions.isEmpty()) { - return; - } - // otherwise request the permissions. - int requestId = getNextRequestId(); - String[] permissionsArray = deniedPermissions.toArray( - new String[deniedPermissions.size()]); - - mRequestIdToCallback.put(requestId, callback); - if (activity != null) { - PermissionsUtil.requestPermissions(activity, requestId, permissionsArray); - } else { - PermissionsActivity.run(mContext, requestId, permissionsArray); - } - } - - public synchronized void onRequestPermissionsResult( - int requestCode, String[] permissions, int[] grantResults) { - PermissionsResultCallback permissionsResultCallback = mRequestIdToCallback.get(requestCode); - mRequestIdToCallback.remove(requestCode); - - boolean allGranted = PermissionsUtil.allGranted(grantResults); - permissionsResultCallback.onRequestPermissionsResult(allGranted); - } -} diff --git a/java/src/com/android/inputmethod/latin/permissions/PermissionsUtil.java b/java/src/com/android/inputmethod/latin/permissions/PermissionsUtil.java deleted file mode 100644 index 9a618a755..000000000 --- a/java/src/com/android/inputmethod/latin/permissions/PermissionsUtil.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2015 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.permissions; - -import android.app.Activity; -import android.content.Context; -import android.content.pm.PackageManager; -import android.os.Build; -import androidx.annotation.NonNull; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; - -import java.util.ArrayList; -import java.util.List; - -/** - * Utility class for permissions. - */ -public class PermissionsUtil { - - /** - * Returns the list of permissions not granted from the given list of permissions. - * @param context Context - * @param permissions list of permissions to check. - * @return the list of permissions that do not have permission to use. - */ - public static List<String> getDeniedPermissions(Context context, - String... permissions) { - final List<String> deniedPermissions = new ArrayList<>(); - for (String permission : permissions) { - if (ContextCompat.checkSelfPermission(context, permission) - != PackageManager.PERMISSION_GRANTED) { - deniedPermissions.add(permission); - } - } - return deniedPermissions; - } - - /** - * Uses the given activity and requests the user for permissions. - * @param activity activity to use. - * @param requestCode request code/id to use. - * @param permissions String array of permissions that needs to be requested. - */ - public static void requestPermissions(Activity activity, int requestCode, - String[] permissions) { - ActivityCompat.requestPermissions(activity, permissions, requestCode); - } - - /** - * Checks if all the permissions are granted. - */ - public static boolean allGranted(@NonNull int[] grantResults) { - for (int result : grantResults) { - if (result != PackageManager.PERMISSION_GRANTED) { - return false; - } - } - return true; - } - - /** - * Queries if al the permissions are granted for the given permission strings. - */ - public static boolean checkAllPermissionsGranted(Context context, String... permissions) { - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) { - // For all pre-M devices, we should have all the premissions granted on install. - return true; - } - - for (String permission : permissions) { - if (ContextCompat.checkSelfPermission(context, permission) - != PackageManager.PERMISSION_GRANTED) { - return false; - } - } - return true; - } -} diff --git a/java/src/com/android/inputmethod/latin/personalization/AccountUtils.java b/java/src/com/android/inputmethod/latin/personalization/AccountUtils.java deleted file mode 100644 index ab3ef964e..000000000 --- a/java/src/com/android/inputmethod/latin/personalization/AccountUtils.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2013 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.personalization; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.content.Context; -import android.util.Patterns; - -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -public class AccountUtils { - private AccountUtils() { - // This utility class is not publicly instantiable. - } - - private static Account[] getAccounts(final Context context) { - return AccountManager.get(context).getAccounts(); - } - - public static List<String> getDeviceAccountsEmailAddresses(final Context context) { - final ArrayList<String> retval = new ArrayList<>(); - for (final Account account : getAccounts(context)) { - final String name = account.name; - if (Patterns.EMAIL_ADDRESS.matcher(name).matches()) { - retval.add(name); - retval.add(name.split("@")[0]); - } - } - return retval; - } - - /** - * Get all device accounts having specified domain name. - * @param context application context - * @param domain domain name used for filtering - * @return List of account names that contain the specified domain name - */ - public static List<String> getDeviceAccountsWithDomain( - final Context context, final String domain) { - final ArrayList<String> retval = new ArrayList<>(); - final String atDomain = "@" + domain.toLowerCase(Locale.ROOT); - for (final Account account : getAccounts(context)) { - if (account.name.toLowerCase(Locale.ROOT).endsWith(atDomain)) { - retval.add(account.name); - } - } - return retval; - } -} diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java deleted file mode 100644 index 298e46c0a..000000000 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) 2013 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.personalization; - -import android.content.Context; -import android.util.Log; - -import com.android.inputmethod.latin.common.FileUtils; - -import java.io.File; -import java.io.FilenameFilter; -import java.lang.ref.SoftReference; -import java.util.Locale; -import java.util.concurrent.ConcurrentHashMap; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Helps handle and manage personalized dictionaries such as {@link UserHistoryDictionary}. - */ -public class PersonalizationHelper { - private static final String TAG = PersonalizationHelper.class.getSimpleName(); - private static final boolean DEBUG = false; - - private static final ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>> - sLangUserHistoryDictCache = new ConcurrentHashMap<>(); - - @Nonnull - public static UserHistoryDictionary getUserHistoryDictionary( - final Context context, final Locale locale, @Nullable final String accountName) { - String lookupStr = locale.toString(); - if (accountName != null) { - lookupStr += "." + accountName; - } - synchronized (sLangUserHistoryDictCache) { - if (sLangUserHistoryDictCache.containsKey(lookupStr)) { - final SoftReference<UserHistoryDictionary> ref = - sLangUserHistoryDictCache.get(lookupStr); - final UserHistoryDictionary dict = ref == null ? null : ref.get(); - if (dict != null) { - if (DEBUG) { - Log.d(TAG, "Use cached UserHistoryDictionary with lookup: " + lookupStr); - } - dict.reloadDictionaryIfRequired(); - return dict; - } - } - final UserHistoryDictionary dict = new UserHistoryDictionary( - context, locale, accountName); - sLangUserHistoryDictCache.put(lookupStr, new SoftReference<>(dict)); - return dict; - } - } - - public static void removeAllUserHistoryDictionaries(final Context context) { - synchronized (sLangUserHistoryDictCache) { - for (final ConcurrentHashMap.Entry<String, SoftReference<UserHistoryDictionary>> entry - : sLangUserHistoryDictCache.entrySet()) { - if (entry.getValue() != null) { - final UserHistoryDictionary dict = entry.getValue().get(); - if (dict != null) { - dict.clear(); - } - } - } - sLangUserHistoryDictCache.clear(); - final File filesDir = context.getFilesDir(); - if (filesDir == null) { - Log.e(TAG, "context.getFilesDir() returned null."); - return; - } - final boolean filesDeleted = FileUtils.deleteFilteredFiles( - filesDir, new DictFilter(UserHistoryDictionary.NAME)); - if (!filesDeleted) { - Log.e(TAG, "Cannot remove dictionary files. filesDir: " + filesDir.getAbsolutePath() - + ", dictNamePrefix: " + UserHistoryDictionary.NAME); - } - } - } - - private static class DictFilter implements FilenameFilter { - private final String mName; - - DictFilter(final String name) { - mName = name; - } - - @Override - public boolean accept(final File dir, final String name) { - return name.startsWith(mName); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java deleted file mode 100644 index cbf0829b5..000000000 --- a/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2013 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.personalization; - -import android.content.Context; - -import com.android.inputmethod.annotations.ExternallyReferenced; -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.BinaryDictionary; -import com.android.inputmethod.latin.Dictionary; -import com.android.inputmethod.latin.ExpandableBinaryDictionary; -import com.android.inputmethod.latin.NgramContext; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; -import com.android.inputmethod.latin.define.ProductionFlags; -import com.android.inputmethod.latin.makedict.DictionaryHeader; - -import java.io.File; -import java.util.Locale; -import java.util.Map; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Locally gathers statistics about the words user types and various other signals like - * auto-correction cancellation or manual picks. This allows the keyboard to adapt to the - * typist over time. - */ -public class UserHistoryDictionary extends ExpandableBinaryDictionary { - static final String NAME = UserHistoryDictionary.class.getSimpleName(); - - // TODO: Make this constructor private - UserHistoryDictionary(final Context context, final Locale locale, - @Nullable final String account) { - super(context, getUserHistoryDictName(NAME, locale, null /* dictFile */, account), locale, Dictionary.TYPE_USER_HISTORY, null); - if (mLocale != null && mLocale.toString().length() > 1) { - reloadDictionaryIfRequired(); - } - } - - /** - * @returns the name of the {@link UserHistoryDictionary}. - */ - @UsedForTesting - static String getUserHistoryDictName(final String name, final Locale locale, - @Nullable final File dictFile, @Nullable final String account) { - if (!ProductionFlags.ENABLE_PER_ACCOUNT_USER_HISTORY_DICTIONARY) { - return getDictName(name, locale, dictFile); - } - return getUserHistoryDictNamePerAccount(name, locale, dictFile, account); - } - - /** - * Uses the currently signed in account to determine the dictionary name. - */ - private static String getUserHistoryDictNamePerAccount(final String name, final Locale locale, - @Nullable final File dictFile, @Nullable final String account) { - if (dictFile != null) { - return dictFile.getName(); - } - String dictName = name + "." + locale.toString(); - if (account != null) { - dictName += "." + account; - } - return dictName; - } - - // Note: This method is called by {@link DictionaryFacilitator} using Java reflection. - @SuppressWarnings("unused") - @ExternallyReferenced - public static UserHistoryDictionary getDictionary(final Context context, final Locale locale, - final File dictFile, final String dictNamePrefix, @Nullable final String account) { - return PersonalizationHelper.getUserHistoryDictionary(context, locale, account); - } - - /** - * Add a word to the user history dictionary. - * - * @param userHistoryDictionary the user history dictionary - * @param ngramContext the n-gram context - * @param word the word the user inputted - * @param isValid whether the word is valid or not - * @param timestamp the timestamp when the word has been inputted - */ - public static void addToDictionary(final ExpandableBinaryDictionary userHistoryDictionary, - @Nonnull final NgramContext ngramContext, final String word, final boolean isValid, - final int timestamp) { - if (word.length() > BinaryDictionary.DICTIONARY_MAX_WORD_LENGTH) { - return; - } - userHistoryDictionary.updateEntriesForWord(ngramContext, word, - isValid, 1 /* count */, timestamp); - } - - @Override - public void close() { - // Flush pending writes. - asyncFlushBinaryDictionary(); - super.close(); - } - - @Override - protected Map<String, String> getHeaderAttributeMap() { - final Map<String, String> attributeMap = super.getHeaderAttributeMap(); - attributeMap.put(DictionaryHeader.USES_FORGETTING_CURVE_KEY, - DictionaryHeader.ATTRIBUTE_VALUE_TRUE); - attributeMap.put(DictionaryHeader.HAS_HISTORICAL_INFO_KEY, - DictionaryHeader.ATTRIBUTE_VALUE_TRUE); - return attributeMap; - } - - @Override - protected void loadInitialContentsLocked() { - // No initial contents. - } - - @Override - public boolean isValidWord(final String word) { - // Strings out of this dictionary should not be considered existing words. - return false; - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java deleted file mode 100644 index b39e6b477..000000000 --- a/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java +++ /dev/null @@ -1,508 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import static com.android.inputmethod.latin.settings.LocalSettingsConstants.PREF_ACCOUNT_NAME; -import static com.android.inputmethod.latin.settings.LocalSettingsConstants.PREF_ENABLE_CLOUD_SYNC; - -import android.Manifest; -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.DialogInterface.OnShowListener; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.os.AsyncTask; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.Preference.OnPreferenceClickListener; -import android.preference.TwoStatePreference; -import android.text.TextUtils; -import android.text.method.LinkMovementMethod; -import android.widget.ListView; -import android.widget.TextView; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.accounts.AccountStateChangedListener; -import com.android.inputmethod.latin.accounts.LoginAccountUtils; -import com.android.inputmethod.latin.define.ProductionFlags; -import com.android.inputmethod.latin.permissions.PermissionsUtil; -import com.android.inputmethod.latin.utils.ManagedProfileUtils; - -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.annotation.Nullable; - -/** - * "Accounts & Privacy" settings sub screen. - * - * This settings sub screen handles the following preferences: - * <li> Account selection/management for IME </li> - * <li> Sync preferences </li> - * <li> Privacy preferences </li> - */ -public final class AccountsSettingsFragment extends SubScreenFragment { - private static final String PREF_ENABLE_SYNC_NOW = "pref_enable_cloud_sync"; - private static final String PREF_SYNC_NOW = "pref_sync_now"; - private static final String PREF_CLEAR_SYNC_DATA = "pref_clear_sync_data"; - - static final String PREF_ACCCOUNT_SWITCHER = "account_switcher"; - - /** - * Onclick listener for sync now pref. - */ - private final Preference.OnPreferenceClickListener mSyncNowListener = - new SyncNowListener(); - /** - * Onclick listener for delete sync pref. - */ - private final Preference.OnPreferenceClickListener mDeleteSyncDataListener = - new DeleteSyncDataListener(); - - /** - * Onclick listener for enable sync pref. - */ - private final Preference.OnPreferenceClickListener mEnableSyncClickListener = - new EnableSyncClickListener(); - - /** - * Enable sync checkbox pref. - */ - private TwoStatePreference mEnableSyncPreference; - - /** - * Enable sync checkbox pref. - */ - private Preference mSyncNowPreference; - - /** - * Clear sync data pref. - */ - private Preference mClearSyncDataPreference; - - /** - * Account switcher preference. - */ - private Preference mAccountSwitcher; - - /** - * Stores if we are currently detecting a managed profile. - */ - private AtomicBoolean mManagedProfileBeingDetected = new AtomicBoolean(true); - - /** - * Stores if we have successfully detected if the device has a managed profile. - */ - private AtomicBoolean mHasManagedProfile = new AtomicBoolean(false); - - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_accounts); - - mAccountSwitcher = findPreference(PREF_ACCCOUNT_SWITCHER); - mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW); - mSyncNowPreference = findPreference(PREF_SYNC_NOW); - mClearSyncDataPreference = findPreference(PREF_CLEAR_SYNC_DATA); - - if (ProductionFlags.IS_METRICS_LOGGING_SUPPORTED) { - final Preference enableMetricsLogging = - findPreference(Settings.PREF_ENABLE_METRICS_LOGGING); - final Resources res = getResources(); - if (enableMetricsLogging != null) { - final String enableMetricsLoggingTitle = res.getString( - R.string.enable_metrics_logging, getApplicationName()); - enableMetricsLogging.setTitle(enableMetricsLoggingTitle); - } - } else { - removePreference(Settings.PREF_ENABLE_METRICS_LOGGING); - } - - if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) { - removeSyncPreferences(); - } else { - // Disable by default till we are sure we can enable this. - disableSyncPreferences(); - new ManagedProfileCheckerTask(this).execute(); - } - } - - /** - * Task to check work profile. If found, it removes the sync prefs. If not, - * it enables them. - */ - private static class ManagedProfileCheckerTask extends AsyncTask<Void, Void, Boolean> { - private final AccountsSettingsFragment mFragment; - - private ManagedProfileCheckerTask(final AccountsSettingsFragment fragment) { - mFragment = fragment; - } - - @Override - protected void onPreExecute() { - mFragment.mManagedProfileBeingDetected.set(true); - } - @Override - protected Boolean doInBackground(Void... params) { - return ManagedProfileUtils.getInstance().hasWorkProfile(mFragment.getActivity()); - } - - @Override - protected void onPostExecute(final Boolean hasWorkProfile) { - mFragment.mHasManagedProfile.set(hasWorkProfile); - mFragment.mManagedProfileBeingDetected.set(false); - mFragment.refreshSyncSettingsUI(); - } - } - - private void enableSyncPreferences(final String[] accountsForLogin, - final String currentAccountName) { - if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) { - return; - } - mAccountSwitcher.setEnabled(true); - - mEnableSyncPreference.setEnabled(true); - mEnableSyncPreference.setOnPreferenceClickListener(mEnableSyncClickListener); - - mSyncNowPreference.setEnabled(true); - mSyncNowPreference.setOnPreferenceClickListener(mSyncNowListener); - - mClearSyncDataPreference.setEnabled(true); - mClearSyncDataPreference.setOnPreferenceClickListener(mDeleteSyncDataListener); - - if (currentAccountName != null) { - mAccountSwitcher.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(final Preference preference) { - if (accountsForLogin.length > 0) { - // TODO: Add addition of account. - createAccountPicker(accountsForLogin, getSignedInAccountName(), - new AccountChangedListener(null)).show(); - } - return true; - } - }); - } - } - - /** - * Two reasons for disable - work profile or no accounts on device. - */ - private void disableSyncPreferences() { - if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) { - return; - } - - mAccountSwitcher.setEnabled(false); - mEnableSyncPreference.setEnabled(false); - mSyncNowPreference.setEnabled(false); - mClearSyncDataPreference.setEnabled(false); - } - - /** - * Called only when ProductionFlag is turned off. - */ - private void removeSyncPreferences() { - removePreference(PREF_ACCCOUNT_SWITCHER); - removePreference(PREF_ENABLE_CLOUD_SYNC); - removePreference(PREF_SYNC_NOW); - removePreference(PREF_CLEAR_SYNC_DATA); - } - - @Override - public void onResume() { - super.onResume(); - refreshSyncSettingsUI(); - } - - @Override - public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { - if (TextUtils.equals(key, PREF_ACCOUNT_NAME)) { - refreshSyncSettingsUI(); - } else if (TextUtils.equals(key, PREF_ENABLE_CLOUD_SYNC)) { - mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW); - final boolean syncEnabled = prefs.getBoolean(PREF_ENABLE_CLOUD_SYNC, false); - if (isSyncEnabled()) { - mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary)); - } else { - mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary_disabled)); - } - AccountStateChangedListener.onSyncPreferenceChanged(getSignedInAccountName(), - syncEnabled); - } - } - - /** - * Checks different states like whether account is present or managed profile is present - * and sets the sync settings accordingly. - */ - private void refreshSyncSettingsUI() { - if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) { - return; - } - boolean hasAccountsPermission = PermissionsUtil.checkAllPermissionsGranted( - getActivity(), Manifest.permission.READ_CONTACTS); - - final String[] accountsForLogin = hasAccountsPermission ? - LoginAccountUtils.getAccountsForLogin(getActivity()) : new String[0]; - final String currentAccount = hasAccountsPermission ? getSignedInAccountName() : null; - - if (hasAccountsPermission && !mManagedProfileBeingDetected.get() && - !mHasManagedProfile.get() && accountsForLogin.length > 0) { - // Sync can be used by user; enable all preferences. - enableSyncPreferences(accountsForLogin, currentAccount); - } else { - // Sync cannot be used by user; disable all preferences. - disableSyncPreferences(); - } - refreshSyncSettingsMessaging(hasAccountsPermission, mManagedProfileBeingDetected.get(), - mHasManagedProfile.get(), accountsForLogin.length > 0, - currentAccount); - } - - /** - * @param hasAccountsPermission whether the app has the permission to read accounts. - * @param managedProfileBeingDetected whether we are in process of determining work profile. - * @param hasManagedProfile whether the device has work profile. - * @param hasAccountsForLogin whether the device has enough accounts for login. - * @param currentAccount the account currently selected in the application. - */ - private void refreshSyncSettingsMessaging(boolean hasAccountsPermission, - boolean managedProfileBeingDetected, - boolean hasManagedProfile, - boolean hasAccountsForLogin, - String currentAccount) { - if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) { - return; - } - - if (!hasAccountsPermission) { - mEnableSyncPreference.setChecked(false); - mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary_disabled)); - mAccountSwitcher.setSummary(""); - return; - } else if (managedProfileBeingDetected) { - // If we are determining eligiblity, we show empty summaries. - // Once we have some deterministic result, we set summaries based on different results. - mEnableSyncPreference.setSummary(""); - mAccountSwitcher.setSummary(""); - } else if (hasManagedProfile) { - mEnableSyncPreference.setSummary( - getString(R.string.cloud_sync_summary_disabled_work_profile)); - } else if (!hasAccountsForLogin) { - mEnableSyncPreference.setSummary(getString(R.string.add_account_to_enable_sync)); - } else if (isSyncEnabled()) { - mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary)); - } else { - mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary_disabled)); - } - - // Set some interdependent settings. - // No account automatically turns off sync. - if (!managedProfileBeingDetected && !hasManagedProfile) { - if (currentAccount != null) { - mAccountSwitcher.setSummary(getString(R.string.account_selected, currentAccount)); - } else { - mEnableSyncPreference.setChecked(false); - mAccountSwitcher.setSummary(getString(R.string.no_accounts_selected)); - } - } - } - - @Nullable - String getSignedInAccountName() { - return getSharedPreferences().getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null); - } - - boolean isSyncEnabled() { - return getSharedPreferences().getBoolean(PREF_ENABLE_CLOUD_SYNC, false); - } - - /** - * Creates an account picker dialog showing the given accounts in a list and selecting - * the selected account by default. The list of accounts must not be null/empty. - * - * Package-private for testing. - * - * @param accounts list of accounts on the device. - * @param selectedAccount currently selected account - * @param positiveButtonClickListener listener that gets called when positive button is - * clicked - */ - @UsedForTesting - AlertDialog createAccountPicker(final String[] accounts, - final String selectedAccount, - final DialogInterface.OnClickListener positiveButtonClickListener) { - if (accounts == null || accounts.length == 0) { - throw new IllegalArgumentException("List of accounts must not be empty"); - } - - // See if the currently selected account is in the list. - // If it is, the entry is selected, and a sign-out button is provided. - // If it isn't, select the 0th account by default which will get picked up - // if the user presses OK. - int index = 0; - boolean isSignedIn = false; - for (int i = 0; i < accounts.length; i++) { - if (TextUtils.equals(accounts[i], selectedAccount)) { - index = i; - isSignedIn = true; - break; - } - } - final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) - .setTitle(R.string.account_select_title) - .setSingleChoiceItems(accounts, index, null) - .setPositiveButton(R.string.account_select_ok, positiveButtonClickListener) - .setNegativeButton(R.string.account_select_cancel, null); - if (isSignedIn) { - builder.setNeutralButton(R.string.account_select_sign_out, positiveButtonClickListener); - } - return builder.create(); - } - - /** - * Listener for a account selection changes from the picker. - * Persists/removes the account to/from shared preferences and sets up sync if required. - */ - class AccountChangedListener implements DialogInterface.OnClickListener { - /** - * Represents preference that should be changed based on account chosen. - */ - private TwoStatePreference mDependentPreference; - - AccountChangedListener(final TwoStatePreference dependentPreference) { - mDependentPreference = dependentPreference; - } - - @Override - public void onClick(final DialogInterface dialog, final int which) { - final String oldAccount = getSignedInAccountName(); - switch (which) { - case DialogInterface.BUTTON_POSITIVE: // Signed in - final ListView lv = ((AlertDialog)dialog).getListView(); - final String newAccount = - (String) lv.getItemAtPosition(lv.getCheckedItemPosition()); - getSharedPreferences() - .edit() - .putString(PREF_ACCOUNT_NAME, newAccount) - .apply(); - AccountStateChangedListener.onAccountSignedIn(oldAccount, newAccount); - if (mDependentPreference != null) { - mDependentPreference.setChecked(true); - } - break; - case DialogInterface.BUTTON_NEUTRAL: // Signed out - AccountStateChangedListener.onAccountSignedOut(oldAccount); - getSharedPreferences() - .edit() - .remove(PREF_ACCOUNT_NAME) - .apply(); - break; - } - } - } - - /** - * Listener that initiates the process of sync in the background. - */ - class SyncNowListener implements Preference.OnPreferenceClickListener { - @Override - public boolean onPreferenceClick(final Preference preference) { - AccountStateChangedListener.forceSync(getSignedInAccountName()); - return true; - } - } - - /** - * Listener that initiates the process of deleting user's data from the cloud. - */ - class DeleteSyncDataListener implements Preference.OnPreferenceClickListener { - @Override - public boolean onPreferenceClick(final Preference preference) { - final AlertDialog confirmationDialog = new AlertDialog.Builder(getActivity()) - .setTitle(R.string.clear_sync_data_title) - .setMessage(R.string.clear_sync_data_confirmation) - .setPositiveButton(R.string.clear_sync_data_ok, - new DialogInterface.OnClickListener() { - @Override - public void onClick(final DialogInterface dialog, final int which) { - if (which == DialogInterface.BUTTON_POSITIVE) { - AccountStateChangedListener.forceDelete( - getSignedInAccountName()); - } - } - }) - .setNegativeButton(R.string.cloud_sync_cancel, null /* OnClickListener */) - .create(); - confirmationDialog.show(); - return true; - } - } - - /** - * Listens to events when user clicks on "Enable sync" feature. - */ - class EnableSyncClickListener implements OnShowListener, Preference.OnPreferenceClickListener { - // TODO(cvnguyen): Write tests. - @Override - public boolean onPreferenceClick(final Preference preference) { - final TwoStatePreference syncPreference = (TwoStatePreference) preference; - if (syncPreference.isChecked()) { - // Uncheck for now. - syncPreference.setChecked(false); - - // Show opt-in. - final AlertDialog optInDialog = new AlertDialog.Builder(getActivity()) - .setTitle(R.string.cloud_sync_title) - .setMessage(R.string.cloud_sync_opt_in_text) - .setPositiveButton(R.string.account_select_ok, - new DialogInterface.OnClickListener() { - @Override - public void onClick(final DialogInterface dialog, - final int which) { - if (which == DialogInterface.BUTTON_POSITIVE) { - final Context context = getActivity(); - final String[] accountsForLogin = - LoginAccountUtils.getAccountsForLogin(context); - createAccountPicker(accountsForLogin, - getSignedInAccountName(), - new AccountChangedListener(syncPreference)) - .show(); - } - } - }) - .setNegativeButton(R.string.cloud_sync_cancel, null) - .create(); - optInDialog.setOnShowListener(this); - optInDialog.show(); - } - return true; - } - - @Override - public void onShow(DialogInterface dialog) { - TextView messageView = (TextView) ((AlertDialog) dialog).findViewById( - android.R.id.message); - if (messageView != null) { - messageView.setMovementMethod(LinkMovementMethod.getInstance()); - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/AdditionalFeaturesSettingUtils.java b/java/src/com/android/inputmethod/latin/settings/AdditionalFeaturesSettingUtils.java deleted file mode 100644 index 4e8a10b1f..000000000 --- a/java/src/com/android/inputmethod/latin/settings/AdditionalFeaturesSettingUtils.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2013 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.settings; - -import android.content.Context; -import android.content.SharedPreferences; -import android.preference.PreferenceFragment; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.latin.RichInputMethodSubtype; -import com.android.inputmethod.latin.RichInputMethodManager; - -import javax.annotation.Nonnull; - -/** - * Utility class for managing additional features settings. - */ -@SuppressWarnings("unused") -public class AdditionalFeaturesSettingUtils { - public static final int ADDITIONAL_FEATURES_SETTINGS_SIZE = 0; - - private AdditionalFeaturesSettingUtils() { - // This utility class is not publicly instantiable. - } - - public static void addAdditionalFeaturesPreferences( - final Context context, final PreferenceFragment settingsFragment) { - // do nothing. - } - - public static void readAdditionalFeaturesPreferencesIntoArray(final Context context, - final SharedPreferences prefs, final int[] additionalFeaturesPreferences) { - // do nothing. - } - - @Nonnull - public static RichInputMethodSubtype createRichInputMethodSubtype( - @Nonnull final RichInputMethodManager imm, - @Nonnull final InputMethodSubtype subtype, - final Context context) { - return new RichInputMethodSubtype(subtype); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/AdvancedSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/AdvancedSettingsFragment.java deleted file mode 100644 index a6fb7f1f1..000000000 --- a/java/src/com/android/inputmethod/latin/settings/AdvancedSettingsFragment.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.media.AudioManager; -import android.os.Bundle; -import android.preference.ListPreference; -import android.preference.Preference; - -import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SystemBroadcastReceiver; -import com.android.inputmethod.latin.define.ProductionFlags; - -/** - * "Advanced" settings sub screen. - * - * This settings sub screen handles the following advanced preferences. - * - Key popup dismiss delay - * - Keypress vibration duration - * - Keypress sound volume - * - Show app icon - * - Improve keyboard - * - Debug settings - */ -public final class AdvancedSettingsFragment extends SubScreenFragment { - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_advanced); - - final Resources res = getResources(); - final Context context = getActivity(); - - // When we are called from the Settings application but we are not already running, some - // singleton and utility classes may not have been initialized. We have to call - // initialization method of these classes here. See {@link LatinIME#onCreate()}. - AudioAndHapticFeedbackManager.init(context); - - final SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); - - if (!Settings.isInternal(prefs)) { - removePreference(Settings.SCREEN_DEBUG); - } - - if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) { - removePreference(Settings.PREF_VIBRATION_DURATION_SETTINGS); - } - - // TODO: consolidate key preview dismiss delay with the key preview animation parameters. - if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) { - removePreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY); - } else { - // TODO: Cleanup this setup. - final ListPreference keyPreviewPopupDismissDelay = - (ListPreference) findPreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY); - final String popupDismissDelayDefaultValue = Integer.toString(res.getInteger( - R.integer.config_key_preview_linger_timeout)); - keyPreviewPopupDismissDelay.setEntries(new String[] { - res.getString(R.string.key_preview_popup_dismiss_no_delay), - res.getString(R.string.key_preview_popup_dismiss_default_delay), - }); - keyPreviewPopupDismissDelay.setEntryValues(new String[] { - "0", - popupDismissDelayDefaultValue - }); - if (null == keyPreviewPopupDismissDelay.getValue()) { - keyPreviewPopupDismissDelay.setValue(popupDismissDelayDefaultValue); - } - keyPreviewPopupDismissDelay.setEnabled( - Settings.readKeyPreviewPopupEnabled(prefs, res)); - } - - setupKeypressVibrationDurationSettings(); - setupKeypressSoundVolumeSettings(); - setupKeyLongpressTimeoutSettings(); - refreshEnablingsOfKeypressSoundAndVibrationSettings(); - } - - @Override - public void onResume() { - super.onResume(); - final SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); - updateListPreferenceSummaryToCurrentValue(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY); - } - - @Override - public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { - final Resources res = getResources(); - if (key.equals(Settings.PREF_POPUP_ON)) { - setPreferenceEnabled(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY, - Settings.readKeyPreviewPopupEnabled(prefs, res)); - } else if (key.equals(Settings.PREF_SHOW_SETUP_WIZARD_ICON)) { - SystemBroadcastReceiver.toggleAppIcon(getActivity()); - } - updateListPreferenceSummaryToCurrentValue(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY); - refreshEnablingsOfKeypressSoundAndVibrationSettings(); - } - - private void refreshEnablingsOfKeypressSoundAndVibrationSettings() { - final SharedPreferences prefs = getSharedPreferences(); - final Resources res = getResources(); - setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS, - Settings.readVibrationEnabled(prefs, res)); - setPreferenceEnabled(Settings.PREF_KEYPRESS_SOUND_VOLUME, - Settings.readKeypressSoundEnabled(prefs, res)); - } - - private void setupKeypressVibrationDurationSettings() { - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference( - Settings.PREF_VIBRATION_DURATION_SETTINGS); - if (pref == null) { - return; - } - final SharedPreferences prefs = getSharedPreferences(); - final Resources res = getResources(); - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - @Override - public void writeValue(final int value, final String key) { - prefs.edit().putInt(key, value).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - prefs.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return Settings.readKeypressVibrationDuration(prefs, res); - } - - @Override - public int readDefaultValue(final String key) { - return Settings.readDefaultKeypressVibrationDuration(res); - } - - @Override - public void feedbackValue(final int value) { - AudioAndHapticFeedbackManager.getInstance().vibrate(value); - } - - @Override - public String getValueText(final int value) { - if (value < 0) { - return res.getString(R.string.settings_system_default); - } - return res.getString(R.string.abbreviation_unit_milliseconds, value); - } - }); - } - - private void setupKeypressSoundVolumeSettings() { - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference( - Settings.PREF_KEYPRESS_SOUND_VOLUME); - if (pref == null) { - return; - } - final SharedPreferences prefs = getSharedPreferences(); - final Resources res = getResources(); - final AudioManager am = (AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE); - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - private static final float PERCENTAGE_FLOAT = 100.0f; - - private float getValueFromPercentage(final int percentage) { - return percentage / PERCENTAGE_FLOAT; - } - - private int getPercentageFromValue(final float floatValue) { - return (int)(floatValue * PERCENTAGE_FLOAT); - } - - @Override - public void writeValue(final int value, final String key) { - prefs.edit().putFloat(key, getValueFromPercentage(value)).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - prefs.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return getPercentageFromValue(Settings.readKeypressSoundVolume(prefs, res)); - } - - @Override - public int readDefaultValue(final String key) { - return getPercentageFromValue(Settings.readDefaultKeypressSoundVolume(res)); - } - - @Override - public String getValueText(final int value) { - if (value < 0) { - return res.getString(R.string.settings_system_default); - } - return Integer.toString(value); - } - - @Override - public void feedbackValue(final int value) { - am.playSoundEffect( - AudioManager.FX_KEYPRESS_STANDARD, getValueFromPercentage(value)); - } - }); - } - - private void setupKeyLongpressTimeoutSettings() { - final SharedPreferences prefs = getSharedPreferences(); - final Resources res = getResources(); - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference( - Settings.PREF_KEY_LONGPRESS_TIMEOUT); - if (pref == null) { - return; - } - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - @Override - public void writeValue(final int value, final String key) { - prefs.edit().putInt(key, value).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - prefs.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return Settings.readKeyLongpressTimeout(prefs, res); - } - - @Override - public int readDefaultValue(final String key) { - return Settings.readDefaultKeyLongpressTimeout(res); - } - - @Override - public String getValueText(final int value) { - return res.getString(R.string.abbreviation_unit_milliseconds, value); - } - - @Override - public void feedbackValue(final int value) {} - }); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/AppearanceSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/AppearanceSettingsFragment.java deleted file mode 100644 index 554edc85c..000000000 --- a/java/src/com/android/inputmethod/latin/settings/AppearanceSettingsFragment.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.os.Bundle; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.define.ProductionFlags; - -/** - * "Appearance" settings sub screen. - */ -public final class AppearanceSettingsFragment extends SubScreenFragment { - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_appearance); - if (!ProductionFlags.IS_SPLIT_KEYBOARD_SUPPORTED || - Constants.isPhone(Settings.readScreenMetrics(getResources()))) { - removePreference(Settings.PREF_ENABLE_SPLIT_KEYBOARD); - } - } - - @Override - public void onResume() { - super.onResume(); - CustomInputStyleSettingsFragment.updateCustomInputStylesSummary( - findPreference(Settings.PREF_CUSTOM_INPUT_STYLES)); - ThemeSettingsFragment.updateKeyboardThemeSummary(findPreference(Settings.SCREEN_THEME)); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java deleted file mode 100644 index dfe899ece..000000000 --- a/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.Manifest; -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.os.Build; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.SwitchPreference; -import android.text.TextUtils; - -import com.android.inputmethod.dictionarypack.DictionarySettingsActivity; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.permissions.PermissionsManager; -import com.android.inputmethod.latin.permissions.PermissionsUtil; -import com.android.inputmethod.latin.userdictionary.UserDictionaryList; -import com.android.inputmethod.latin.userdictionary.UserDictionarySettings; - -import java.util.TreeSet; - -/** - * "Text correction" settings sub screen. - * - * This settings sub screen handles the following text correction preferences. - * - Personal dictionary - * - Add-on dictionaries - * - Block offensive words - * - Auto-correction - * - Show correction suggestions - * - Personalized suggestions - * - Suggest Contact names - * - Next-word suggestions - */ -public final class CorrectionSettingsFragment extends SubScreenFragment - implements SharedPreferences.OnSharedPreferenceChangeListener, - PermissionsManager.PermissionsResultCallback { - - private static final boolean DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS = false; - private static final boolean USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS = - DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS - || Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2; - - private SwitchPreference mUseContactsPreference; - - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_correction); - - final Context context = getActivity(); - final PackageManager pm = context.getPackageManager(); - - final Preference dictionaryLink = findPreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY); - final Intent intent = dictionaryLink.getIntent(); - intent.setClassName(context.getPackageName(), DictionarySettingsActivity.class.getName()); - final int number = pm.queryIntentActivities(intent, 0).size(); - if (0 >= number) { - removePreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY); - } - - final Preference editPersonalDictionary = - findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY); - final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent(); - final ResolveInfo ri = USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS ? null - : pm.resolveActivity( - editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY); - if (ri == null) { - overwriteUserDictionaryPreference(editPersonalDictionary); - } - - mUseContactsPreference = (SwitchPreference) findPreference(Settings.PREF_KEY_USE_CONTACTS_DICT); - turnOffUseContactsIfNoPermission(); - } - - private void overwriteUserDictionaryPreference(final Preference userDictionaryPreference) { - final Activity activity = getActivity(); - final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity); - if (null == localeList) { - // The locale list is null if and only if the user dictionary service is - // not present or disabled. In this case we need to remove the preference. - getPreferenceScreen().removePreference(userDictionaryPreference); - } else if (localeList.size() <= 1) { - userDictionaryPreference.setFragment(UserDictionarySettings.class.getName()); - // If the size of localeList is 0, we don't set the locale parameter in the - // extras. This will be interpreted by the UserDictionarySettings class as - // meaning "the current locale". - // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet() - // the locale list always has at least one element, since it always includes the current - // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet(). - if (localeList.size() == 1) { - final String locale = (String)localeList.toArray()[0]; - userDictionaryPreference.getExtras().putString("locale", locale); - } - } else { - userDictionaryPreference.setFragment(UserDictionaryList.class.getName()); - } - } - - @Override - public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) { - if (!TextUtils.equals(key, Settings.PREF_KEY_USE_CONTACTS_DICT)) { - return; - } - if (!sharedPreferences.getBoolean(key, false)) { - // don't care if the preference is turned off. - return; - } - - // Check for permissions. - if (PermissionsUtil.checkAllPermissionsGranted( - getActivity() /* context */, Manifest.permission.READ_CONTACTS)) { - return; // all permissions granted, no need to request permissions. - } - - PermissionsManager.get(getActivity() /* context */).requestPermissions( - this /* PermissionsResultCallback */, - getActivity() /* activity */, - Manifest.permission.READ_CONTACTS); - } - - @Override - public void onRequestPermissionsResult(boolean allGranted) { - turnOffUseContactsIfNoPermission(); - } - - private void turnOffUseContactsIfNoPermission() { - if (!PermissionsUtil.checkAllPermissionsGranted( - getActivity(), Manifest.permission.READ_CONTACTS)) { - mUseContactsPreference.setChecked(false); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/CustomInputStylePreference.java b/java/src/com/android/inputmethod/latin/settings/CustomInputStylePreference.java deleted file mode 100644 index 44a98c1be..000000000 --- a/java/src/com/android/inputmethod/latin/settings/CustomInputStylePreference.java +++ /dev/null @@ -1,341 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.Context; -import android.content.DialogInterface; -import android.os.Parcel; -import android.os.Parcelable; -import android.preference.DialogPreference; -import android.preference.Preference; -import android.util.Log; -import android.view.View; -import android.view.inputmethod.InputMethodInfo; -import android.view.inputmethod.InputMethodSubtype; -import android.widget.ArrayAdapter; -import android.widget.Spinner; -import android.widget.SpinnerAdapter; - -import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; -import com.android.inputmethod.compat.ViewCompatUtils; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.RichInputMethodManager; -import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; - -import java.util.TreeSet; - -final class CustomInputStylePreference extends DialogPreference - implements DialogInterface.OnCancelListener { - private static final boolean DEBUG_SUBTYPE_ID = false; - - interface Listener { - public void onRemoveCustomInputStyle(CustomInputStylePreference stylePref); - public void onSaveCustomInputStyle(CustomInputStylePreference stylePref); - public void onAddCustomInputStyle(CustomInputStylePreference stylePref); - public SubtypeLocaleAdapter getSubtypeLocaleAdapter(); - public KeyboardLayoutSetAdapter getKeyboardLayoutSetAdapter(); - } - - private static final String KEY_PREFIX = "subtype_pref_"; - private static final String KEY_NEW_SUBTYPE = KEY_PREFIX + "new"; - - private InputMethodSubtype mSubtype; - private InputMethodSubtype mPreviousSubtype; - - private final Listener mProxy; - private Spinner mSubtypeLocaleSpinner; - private Spinner mKeyboardLayoutSetSpinner; - - public static CustomInputStylePreference newIncompleteSubtypePreference( - final Context context, final Listener proxy) { - return new CustomInputStylePreference(context, null, proxy); - } - - public CustomInputStylePreference(final Context context, final InputMethodSubtype subtype, - final Listener proxy) { - super(context, null); - setDialogLayoutResource(R.layout.additional_subtype_dialog); - setPersistent(false); - mProxy = proxy; - setSubtype(subtype); - } - - public void show() { - showDialog(null); - } - - public final boolean isIncomplete() { - return mSubtype == null; - } - - public InputMethodSubtype getSubtype() { - return mSubtype; - } - - public void setSubtype(final InputMethodSubtype subtype) { - mPreviousSubtype = mSubtype; - mSubtype = subtype; - if (isIncomplete()) { - setTitle(null); - setDialogTitle(R.string.add_style); - setKey(KEY_NEW_SUBTYPE); - } else { - final String displayName = - SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype); - setTitle(displayName); - setDialogTitle(displayName); - setKey(KEY_PREFIX + subtype.getLocale() + "_" - + SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype)); - } - } - - public void revert() { - setSubtype(mPreviousSubtype); - } - - public boolean hasBeenModified() { - return mSubtype != null && !mSubtype.equals(mPreviousSubtype); - } - - @Override - protected View onCreateDialogView() { - final View v = super.onCreateDialogView(); - mSubtypeLocaleSpinner = (Spinner) v.findViewById(R.id.subtype_locale_spinner); - mSubtypeLocaleSpinner.setAdapter(mProxy.getSubtypeLocaleAdapter()); - mKeyboardLayoutSetSpinner = (Spinner) v.findViewById(R.id.keyboard_layout_set_spinner); - mKeyboardLayoutSetSpinner.setAdapter(mProxy.getKeyboardLayoutSetAdapter()); - // All keyboard layout names are in the Latin script and thus left to right. That means - // the view would align them to the left even if the system locale is RTL, but that - // would look strange. To fix this, we align them to the view's start, which will be - // natural for any direction. - ViewCompatUtils.setTextAlignment( - mKeyboardLayoutSetSpinner, ViewCompatUtils.TEXT_ALIGNMENT_VIEW_START); - return v; - } - - @Override - protected void onPrepareDialogBuilder(final AlertDialog.Builder builder) { - builder.setCancelable(true).setOnCancelListener(this); - if (isIncomplete()) { - builder.setPositiveButton(R.string.add, this) - .setNegativeButton(android.R.string.cancel, this); - } else { - builder.setPositiveButton(R.string.save, this) - .setNeutralButton(android.R.string.cancel, this) - .setNegativeButton(R.string.remove, this); - final SubtypeLocaleItem localeItem = new SubtypeLocaleItem(mSubtype); - final KeyboardLayoutSetItem layoutItem = new KeyboardLayoutSetItem(mSubtype); - setSpinnerPosition(mSubtypeLocaleSpinner, localeItem); - setSpinnerPosition(mKeyboardLayoutSetSpinner, layoutItem); - } - } - - private static void setSpinnerPosition(final Spinner spinner, final Object itemToSelect) { - final SpinnerAdapter adapter = spinner.getAdapter(); - final int count = adapter.getCount(); - for (int i = 0; i < count; i++) { - final Object item = spinner.getItemAtPosition(i); - if (item.equals(itemToSelect)) { - spinner.setSelection(i); - return; - } - } - } - - @Override - public void onCancel(final DialogInterface dialog) { - if (isIncomplete()) { - mProxy.onRemoveCustomInputStyle(this); - } - } - - @Override - public void onClick(final DialogInterface dialog, final int which) { - super.onClick(dialog, which); - switch (which) { - case DialogInterface.BUTTON_POSITIVE: - final boolean isEditing = !isIncomplete(); - final SubtypeLocaleItem locale = - (SubtypeLocaleItem) mSubtypeLocaleSpinner.getSelectedItem(); - final KeyboardLayoutSetItem layout = - (KeyboardLayoutSetItem) mKeyboardLayoutSetSpinner.getSelectedItem(); - final InputMethodSubtype subtype = - AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype( - locale.mLocaleString, layout.mLayoutName); - setSubtype(subtype); - notifyChanged(); - if (isEditing) { - mProxy.onSaveCustomInputStyle(this); - } else { - mProxy.onAddCustomInputStyle(this); - } - break; - case DialogInterface.BUTTON_NEUTRAL: - // Nothing to do - break; - case DialogInterface.BUTTON_NEGATIVE: - mProxy.onRemoveCustomInputStyle(this); - break; - } - } - - @Override - protected Parcelable onSaveInstanceState() { - final Parcelable superState = super.onSaveInstanceState(); - final Dialog dialog = getDialog(); - if (dialog == null || !dialog.isShowing()) { - return superState; - } - - final SavedState myState = new SavedState(superState); - myState.mSubtype = mSubtype; - return myState; - } - - @Override - protected void onRestoreInstanceState(final Parcelable state) { - if (!(state instanceof SavedState)) { - super.onRestoreInstanceState(state); - return; - } - - final SavedState myState = (SavedState) state; - super.onRestoreInstanceState(myState.getSuperState()); - setSubtype(myState.mSubtype); - } - - static final class SavedState extends Preference.BaseSavedState { - InputMethodSubtype mSubtype; - - public SavedState(final Parcelable superState) { - super(superState); - } - - @Override - public void writeToParcel(final Parcel dest, final int flags) { - super.writeToParcel(dest, flags); - dest.writeParcelable(mSubtype, 0); - } - - public SavedState(final Parcel source) { - super(source); - mSubtype = (InputMethodSubtype)source.readParcelable(null); - } - - @SuppressWarnings("hiding") - public static final Parcelable.Creator<SavedState> CREATOR = - new Parcelable.Creator<SavedState>() { - @Override - public SavedState createFromParcel(final Parcel source) { - return new SavedState(source); - } - - @Override - public SavedState[] newArray(final int size) { - return new SavedState[size]; - } - }; - } - - static final class SubtypeLocaleItem implements Comparable<SubtypeLocaleItem> { - public final String mLocaleString; - private final String mDisplayName; - - public SubtypeLocaleItem(final InputMethodSubtype subtype) { - mLocaleString = subtype.getLocale(); - mDisplayName = SubtypeLocaleUtils.getSubtypeLocaleDisplayNameInSystemLocale( - mLocaleString); - } - - // {@link ArrayAdapter<T>} that hosts the instance of this class needs {@link #toString()} - // to get display name. - @Override - public String toString() { - return mDisplayName; - } - - @Override - public int compareTo(final SubtypeLocaleItem o) { - return mLocaleString.compareTo(o.mLocaleString); - } - } - - static final class SubtypeLocaleAdapter extends ArrayAdapter<SubtypeLocaleItem> { - private static final String TAG_SUBTYPE = SubtypeLocaleAdapter.class.getSimpleName(); - - public SubtypeLocaleAdapter(final Context context) { - super(context, android.R.layout.simple_spinner_item); - setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - - final TreeSet<SubtypeLocaleItem> items = new TreeSet<>(); - final InputMethodInfo imi = RichInputMethodManager.getInstance() - .getInputMethodInfoOfThisIme(); - final int count = imi.getSubtypeCount(); - for (int i = 0; i < count; i++) { - final InputMethodSubtype subtype = imi.getSubtypeAt(i); - if (DEBUG_SUBTYPE_ID) { - Log.d(TAG_SUBTYPE, String.format("%-6s 0x%08x %11d %s", - subtype.getLocale(), subtype.hashCode(), subtype.hashCode(), - SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype))); - } - if (InputMethodSubtypeCompatUtils.isAsciiCapable(subtype)) { - items.add(new SubtypeLocaleItem(subtype)); - } - } - // TODO: Should filter out already existing combinations of locale and layout. - addAll(items); - } - } - - static final class KeyboardLayoutSetItem { - public final String mLayoutName; - private final String mDisplayName; - - public KeyboardLayoutSetItem(final InputMethodSubtype subtype) { - mLayoutName = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype); - mDisplayName = SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(subtype); - } - - // {@link ArrayAdapter<T>} that hosts the instance of this class needs {@link #toString()} - // to get display name. - @Override - public String toString() { - return mDisplayName; - } - } - - static final class KeyboardLayoutSetAdapter extends ArrayAdapter<KeyboardLayoutSetItem> { - public KeyboardLayoutSetAdapter(final Context context) { - super(context, android.R.layout.simple_spinner_item); - setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - - final String[] predefinedKeyboardLayoutSet = context.getResources().getStringArray( - R.array.predefined_layouts); - // TODO: Should filter out already existing combinations of locale and layout. - for (final String layout : predefinedKeyboardLayoutSet) { - // This is a placeholder for a subtype with NO_LANGUAGE, only for display. - final InputMethodSubtype subtype = - AdditionalSubtypeUtils.createDummyAdditionalSubtype( - SubtypeLocaleUtils.NO_LANGUAGE, layout); - add(new KeyboardLayoutSetItem(subtype)); - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/CustomInputStyleSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/CustomInputStyleSettingsFragment.java deleted file mode 100644 index 56e8f1623..000000000 --- a/java/src/com/android/inputmethod/latin/settings/CustomInputStyleSettingsFragment.java +++ /dev/null @@ -1,318 +0,0 @@ -/* - * 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.settings; - -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.PreferenceFragment; -import android.preference.PreferenceGroup; -import androidx.core.view.ViewCompat; -import android.text.TextUtils; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.view.inputmethod.InputMethodSubtype; -import android.widget.Toast; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.RichInputMethodManager; -import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; -import com.android.inputmethod.latin.utils.DialogUtils; -import com.android.inputmethod.latin.utils.IntentUtils; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; - -import java.util.ArrayList; - -public final class CustomInputStyleSettingsFragment extends PreferenceFragment - implements CustomInputStylePreference.Listener { - private static final String TAG = CustomInputStyleSettingsFragment.class.getSimpleName(); - // Note: We would like to turn this debug flag true in order to see what input styles are - // defined in a bug-report. - private static final boolean DEBUG_CUSTOM_INPUT_STYLES = true; - - private RichInputMethodManager mRichImm; - private SharedPreferences mPrefs; - private CustomInputStylePreference.SubtypeLocaleAdapter mSubtypeLocaleAdapter; - private CustomInputStylePreference.KeyboardLayoutSetAdapter mKeyboardLayoutSetAdapter; - - private boolean mIsAddingNewSubtype; - private AlertDialog mSubtypeEnablerNotificationDialog; - private String mSubtypePreferenceKeyForSubtypeEnabler; - - private static final String KEY_IS_ADDING_NEW_SUBTYPE = "is_adding_new_subtype"; - private static final String KEY_IS_SUBTYPE_ENABLER_NOTIFICATION_DIALOG_OPEN = - "is_subtype_enabler_notification_dialog_open"; - private static final String KEY_SUBTYPE_FOR_SUBTYPE_ENABLER = "subtype_for_subtype_enabler"; - - public CustomInputStyleSettingsFragment() { - // Empty constructor for fragment generation. - } - - static void updateCustomInputStylesSummary(final Preference pref) { - // When we are called from the Settings application but we are not already running, some - // singleton and utility classes may not have been initialized. We have to call - // initialization method of these classes here. See {@link LatinIME#onCreate()}. - SubtypeLocaleUtils.init(pref.getContext()); - - final Resources res = pref.getContext().getResources(); - final SharedPreferences prefs = pref.getSharedPreferences(); - final String prefSubtype = Settings.readPrefAdditionalSubtypes(prefs, res); - final InputMethodSubtype[] subtypes = - AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefSubtype); - final ArrayList<String> subtypeNames = new ArrayList<>(); - for (final InputMethodSubtype subtype : subtypes) { - subtypeNames.add(SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype)); - } - // TODO: A delimiter of custom input styles should be localized. - pref.setSummary(TextUtils.join(", ", subtypeNames)); - } - - @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - mPrefs = getPreferenceManager().getSharedPreferences(); - RichInputMethodManager.init(getActivity()); - mRichImm = RichInputMethodManager.getInstance(); - addPreferencesFromResource(R.xml.additional_subtype_settings); - setHasOptionsMenu(true); - } - - @Override - public View onCreateView(final LayoutInflater inflater, final ViewGroup container, - final Bundle savedInstanceState) { - final View view = super.onCreateView(inflater, container, savedInstanceState); - // For correct display in RTL locales, we need to set the layout direction of the - // fragment's top view. - ViewCompat.setLayoutDirection(view, ViewCompat.LAYOUT_DIRECTION_LOCALE); - return view; - } - - @Override - public void onActivityCreated(final Bundle savedInstanceState) { - final Context context = getActivity(); - mSubtypeLocaleAdapter = new CustomInputStylePreference.SubtypeLocaleAdapter(context); - mKeyboardLayoutSetAdapter = - new CustomInputStylePreference.KeyboardLayoutSetAdapter(context); - - final String prefSubtypes = - Settings.readPrefAdditionalSubtypes(mPrefs, getResources()); - if (DEBUG_CUSTOM_INPUT_STYLES) { - Log.i(TAG, "Load custom input styles: " + prefSubtypes); - } - setPrefSubtypes(prefSubtypes, context); - - mIsAddingNewSubtype = (savedInstanceState != null) - && savedInstanceState.containsKey(KEY_IS_ADDING_NEW_SUBTYPE); - if (mIsAddingNewSubtype) { - getPreferenceScreen().addPreference( - CustomInputStylePreference.newIncompleteSubtypePreference(context, this)); - } - - super.onActivityCreated(savedInstanceState); - - if (savedInstanceState != null && savedInstanceState.containsKey( - KEY_IS_SUBTYPE_ENABLER_NOTIFICATION_DIALOG_OPEN)) { - mSubtypePreferenceKeyForSubtypeEnabler = savedInstanceState.getString( - KEY_SUBTYPE_FOR_SUBTYPE_ENABLER); - mSubtypeEnablerNotificationDialog = createDialog(); - mSubtypeEnablerNotificationDialog.show(); - } - } - - @Override - public void onSaveInstanceState(final Bundle outState) { - super.onSaveInstanceState(outState); - if (mIsAddingNewSubtype) { - outState.putBoolean(KEY_IS_ADDING_NEW_SUBTYPE, true); - } - if (mSubtypeEnablerNotificationDialog != null - && mSubtypeEnablerNotificationDialog.isShowing()) { - outState.putBoolean(KEY_IS_SUBTYPE_ENABLER_NOTIFICATION_DIALOG_OPEN, true); - outState.putString( - KEY_SUBTYPE_FOR_SUBTYPE_ENABLER, mSubtypePreferenceKeyForSubtypeEnabler); - } - } - - @Override - public void onRemoveCustomInputStyle(final CustomInputStylePreference stylePref) { - mIsAddingNewSubtype = false; - final PreferenceGroup group = getPreferenceScreen(); - group.removePreference(stylePref); - mRichImm.setAdditionalInputMethodSubtypes(getSubtypes()); - } - - @Override - public void onSaveCustomInputStyle(final CustomInputStylePreference stylePref) { - final InputMethodSubtype subtype = stylePref.getSubtype(); - if (!stylePref.hasBeenModified()) { - return; - } - if (findDuplicatedSubtype(subtype) == null) { - mRichImm.setAdditionalInputMethodSubtypes(getSubtypes()); - return; - } - - // Saved subtype is duplicated. - final PreferenceGroup group = getPreferenceScreen(); - group.removePreference(stylePref); - stylePref.revert(); - group.addPreference(stylePref); - showSubtypeAlreadyExistsToast(subtype); - } - - @Override - public void onAddCustomInputStyle(final CustomInputStylePreference stylePref) { - mIsAddingNewSubtype = false; - final InputMethodSubtype subtype = stylePref.getSubtype(); - if (findDuplicatedSubtype(subtype) == null) { - mRichImm.setAdditionalInputMethodSubtypes(getSubtypes()); - mSubtypePreferenceKeyForSubtypeEnabler = stylePref.getKey(); - mSubtypeEnablerNotificationDialog = createDialog(); - mSubtypeEnablerNotificationDialog.show(); - return; - } - - // Newly added subtype is duplicated. - final PreferenceGroup group = getPreferenceScreen(); - group.removePreference(stylePref); - showSubtypeAlreadyExistsToast(subtype); - } - - @Override - public CustomInputStylePreference.SubtypeLocaleAdapter getSubtypeLocaleAdapter() { - return mSubtypeLocaleAdapter; - } - - @Override - public CustomInputStylePreference.KeyboardLayoutSetAdapter getKeyboardLayoutSetAdapter() { - return mKeyboardLayoutSetAdapter; - } - - private void showSubtypeAlreadyExistsToast(final InputMethodSubtype subtype) { - final Context context = getActivity(); - final Resources res = context.getResources(); - final String message = res.getString(R.string.custom_input_style_already_exists, - SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype)); - Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); - } - - private InputMethodSubtype findDuplicatedSubtype(final InputMethodSubtype subtype) { - final String localeString = subtype.getLocale(); - final String keyboardLayoutSetName = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype); - return mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet( - localeString, keyboardLayoutSetName); - } - - private AlertDialog createDialog() { - final String imeId = mRichImm.getInputMethodIdOfThisIme(); - final AlertDialog.Builder builder = new AlertDialog.Builder( - DialogUtils.getPlatformDialogThemeContext(getActivity())); - builder.setTitle(R.string.custom_input_styles_title) - .setMessage(R.string.custom_input_style_note_message) - .setNegativeButton(R.string.not_now, null) - .setPositiveButton(R.string.enable, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - final Intent intent = IntentUtils.getInputLanguageSelectionIntent( - imeId, - Intent.FLAG_ACTIVITY_NEW_TASK - | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - | Intent.FLAG_ACTIVITY_CLEAR_TOP); - // TODO: Add newly adding subtype to extra value of the intent as a hint - // for the input language selection activity. - // intent.putExtra("newlyAddedSubtype", subtypePref.getSubtype()); - startActivity(intent); - } - }); - - return builder.create(); - } - - private void setPrefSubtypes(final String prefSubtypes, final Context context) { - final PreferenceGroup group = getPreferenceScreen(); - group.removeAll(); - final InputMethodSubtype[] subtypesArray = - AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefSubtypes); - for (final InputMethodSubtype subtype : subtypesArray) { - final CustomInputStylePreference pref = - new CustomInputStylePreference(context, subtype, this); - group.addPreference(pref); - } - } - - private InputMethodSubtype[] getSubtypes() { - final PreferenceGroup group = getPreferenceScreen(); - final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>(); - final int count = group.getPreferenceCount(); - for (int i = 0; i < count; i++) { - final Preference pref = group.getPreference(i); - if (pref instanceof CustomInputStylePreference) { - final CustomInputStylePreference subtypePref = (CustomInputStylePreference)pref; - // We should not save newly adding subtype to preference because it is incomplete. - if (subtypePref.isIncomplete()) continue; - subtypes.add(subtypePref.getSubtype()); - } - } - return subtypes.toArray(new InputMethodSubtype[subtypes.size()]); - } - - @Override - public void onPause() { - super.onPause(); - final String oldSubtypes = Settings.readPrefAdditionalSubtypes(mPrefs, getResources()); - final InputMethodSubtype[] subtypes = getSubtypes(); - final String prefSubtypes = AdditionalSubtypeUtils.createPrefSubtypes(subtypes); - if (DEBUG_CUSTOM_INPUT_STYLES) { - Log.i(TAG, "Save custom input styles: " + prefSubtypes); - } - if (prefSubtypes.equals(oldSubtypes)) { - return; - } - Settings.writePrefAdditionalSubtypes(mPrefs, prefSubtypes); - mRichImm.setAdditionalInputMethodSubtypes(subtypes); - } - - @Override - public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { - inflater.inflate(R.menu.add_style, menu); - } - - @Override - public boolean onOptionsItemSelected(final MenuItem item) { - final int itemId = item.getItemId(); - if (itemId == R.id.action_add_style) { - final CustomInputStylePreference newSubtype = - CustomInputStylePreference.newIncompleteSubtypePreference(getActivity(), this); - getPreferenceScreen().addPreference(newSubtype); - newSubtype.show(); - mIsAddingNewSubtype = true; - return true; - } - return super.onOptionsItemSelected(item); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/DebugSettings.java b/java/src/com/android/inputmethod/latin/settings/DebugSettings.java deleted file mode 100644 index 6fffb8e9d..000000000 --- a/java/src/com/android/inputmethod/latin/settings/DebugSettings.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2010 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.settings; - -/** - * Debug settings for the application. - * - * Note: Even though these settings are stored in the default shared preferences file, - * they shouldn't be restored across devices. - * If a new key is added here, it should also be blacklisted for restore in - * {@link LocalSettingsConstants}. - */ -public final class DebugSettings { - public static final String PREF_DEBUG_MODE = "debug_mode"; - public static final String PREF_FORCE_NON_DISTINCT_MULTITOUCH = "force_non_distinct_multitouch"; - public static final String PREF_HAS_CUSTOM_KEY_PREVIEW_ANIMATION_PARAMS = - "pref_has_custom_key_preview_animation_params"; - public static final String PREF_RESIZE_KEYBOARD = "pref_resize_keyboard"; - public static final String PREF_KEYBOARD_HEIGHT_SCALE = "pref_keyboard_height_scale"; - public static final String PREF_KEY_PREVIEW_DISMISS_DURATION = - "pref_key_preview_dismiss_duration"; - public static final String PREF_KEY_PREVIEW_DISMISS_END_X_SCALE = - "pref_key_preview_dismiss_end_x_scale"; - public static final String PREF_KEY_PREVIEW_DISMISS_END_Y_SCALE = - "pref_key_preview_dismiss_end_y_scale"; - public static final String PREF_KEY_PREVIEW_SHOW_UP_DURATION = - "pref_key_preview_show_up_duration"; - public static final String PREF_KEY_PREVIEW_SHOW_UP_START_X_SCALE = - "pref_key_preview_show_up_start_x_scale"; - public static final String PREF_KEY_PREVIEW_SHOW_UP_START_Y_SCALE = - "pref_key_preview_show_up_start_y_scale"; - public static final String PREF_SHOULD_SHOW_LXX_SUGGESTION_UI = - "pref_should_show_lxx_suggestion_ui"; - public static final String PREF_SLIDING_KEY_INPUT_PREVIEW = "pref_sliding_key_input_preview"; - - private DebugSettings() { - // This class is not publicly instantiable. - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java deleted file mode 100644 index 37855377d..000000000 --- a/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java +++ /dev/null @@ -1,288 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.os.Bundle; -import android.os.Process; -import android.preference.Preference; -import android.preference.Preference.OnPreferenceClickListener; -import android.preference.PreferenceGroup; -import android.preference.TwoStatePreference; - -import com.android.inputmethod.latin.DictionaryDumpBroadcastReceiver; -import com.android.inputmethod.latin.DictionaryFacilitatorImpl; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.utils.ApplicationUtils; -import com.android.inputmethod.latin.utils.ResourceUtils; - -import java.util.Locale; - -/** - * "Debug mode" settings sub screen. - * - * This settings sub screen handles a several preference options for debugging. - */ -public final class DebugSettingsFragment extends SubScreenFragment - implements OnPreferenceClickListener { - private static final String PREF_KEY_DUMP_DICTS = "pref_key_dump_dictionaries"; - private static final String PREF_KEY_DUMP_DICT_PREFIX = "pref_key_dump_dictionaries"; - - private boolean mServiceNeedsRestart = false; - private TwoStatePreference mDebugMode; - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_debug); - - if (!Settings.SHOULD_SHOW_LXX_SUGGESTION_UI) { - removePreference(DebugSettings.PREF_SHOULD_SHOW_LXX_SUGGESTION_UI); - } - - final PreferenceGroup dictDumpPreferenceGroup = - (PreferenceGroup)findPreference(PREF_KEY_DUMP_DICTS); - for (final String dictName : DictionaryFacilitatorImpl.DICT_TYPE_TO_CLASS.keySet()) { - final Preference pref = new DictDumpPreference(getActivity(), dictName); - pref.setOnPreferenceClickListener(this); - dictDumpPreferenceGroup.addPreference(pref); - } - final Resources res = getResources(); - setupKeyPreviewAnimationDuration(DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_DURATION, - res.getInteger(R.integer.config_key_preview_show_up_duration)); - setupKeyPreviewAnimationDuration(DebugSettings.PREF_KEY_PREVIEW_DISMISS_DURATION, - res.getInteger(R.integer.config_key_preview_dismiss_duration)); - final float defaultKeyPreviewShowUpStartScale = ResourceUtils.getFloatFromFraction( - res, R.fraction.config_key_preview_show_up_start_scale); - final float defaultKeyPreviewDismissEndScale = ResourceUtils.getFloatFromFraction( - res, R.fraction.config_key_preview_dismiss_end_scale); - setupKeyPreviewAnimationScale(DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_START_X_SCALE, - defaultKeyPreviewShowUpStartScale); - setupKeyPreviewAnimationScale(DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_START_Y_SCALE, - defaultKeyPreviewShowUpStartScale); - setupKeyPreviewAnimationScale(DebugSettings.PREF_KEY_PREVIEW_DISMISS_END_X_SCALE, - defaultKeyPreviewDismissEndScale); - setupKeyPreviewAnimationScale(DebugSettings.PREF_KEY_PREVIEW_DISMISS_END_Y_SCALE, - defaultKeyPreviewDismissEndScale); - setupKeyboardHeight( - DebugSettings.PREF_KEYBOARD_HEIGHT_SCALE, SettingsValues.DEFAULT_SIZE_SCALE); - - mServiceNeedsRestart = false; - mDebugMode = (TwoStatePreference) findPreference(DebugSettings.PREF_DEBUG_MODE); - updateDebugMode(); - } - - private static class DictDumpPreference extends Preference { - public final String mDictName; - - public DictDumpPreference(final Context context, final String dictName) { - super(context); - setKey(PREF_KEY_DUMP_DICT_PREFIX + dictName); - setTitle("Dump " + dictName + " dictionary"); - mDictName = dictName; - } - } - - @Override - public boolean onPreferenceClick(final Preference pref) { - final Context context = getActivity(); - if (pref instanceof DictDumpPreference) { - final DictDumpPreference dictDumpPref = (DictDumpPreference)pref; - final String dictName = dictDumpPref.mDictName; - final Intent intent = new Intent( - DictionaryDumpBroadcastReceiver.DICTIONARY_DUMP_INTENT_ACTION); - intent.putExtra(DictionaryDumpBroadcastReceiver.DICTIONARY_NAME_KEY, dictName); - context.sendBroadcast(intent); - return true; - } - return true; - } - - @Override - public void onStop() { - super.onStop(); - if (mServiceNeedsRestart) { - Process.killProcess(Process.myPid()); - } - } - - @Override - public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { - if (key.equals(DebugSettings.PREF_DEBUG_MODE) && mDebugMode != null) { - mDebugMode.setChecked(prefs.getBoolean(DebugSettings.PREF_DEBUG_MODE, false)); - updateDebugMode(); - mServiceNeedsRestart = true; - return; - } - if (key.equals(DebugSettings.PREF_FORCE_NON_DISTINCT_MULTITOUCH)) { - mServiceNeedsRestart = true; - return; - } - } - - private void updateDebugMode() { - boolean isDebugMode = mDebugMode.isChecked(); - final String version = getString( - R.string.version_text, ApplicationUtils.getVersionName(getActivity())); - if (!isDebugMode) { - mDebugMode.setTitle(version); - mDebugMode.setSummary(null); - } else { - mDebugMode.setTitle(getString(R.string.prefs_debug_mode)); - mDebugMode.setSummary(version); - } - } - - private void setupKeyPreviewAnimationScale(final String prefKey, final float defaultValue) { - final SharedPreferences prefs = getSharedPreferences(); - final Resources res = getResources(); - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(prefKey); - if (pref == null) { - return; - } - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - private static final float PERCENTAGE_FLOAT = 100.0f; - - private float getValueFromPercentage(final int percentage) { - return percentage / PERCENTAGE_FLOAT; - } - - private int getPercentageFromValue(final float floatValue) { - return (int)(floatValue * PERCENTAGE_FLOAT); - } - - @Override - public void writeValue(final int value, final String key) { - prefs.edit().putFloat(key, getValueFromPercentage(value)).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - prefs.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return getPercentageFromValue( - Settings.readKeyPreviewAnimationScale(prefs, key, defaultValue)); - } - - @Override - public int readDefaultValue(final String key) { - return getPercentageFromValue(defaultValue); - } - - @Override - public String getValueText(final int value) { - if (value < 0) { - return res.getString(R.string.settings_system_default); - } - return String.format(Locale.ROOT, "%d%%", value); - } - - @Override - public void feedbackValue(final int value) {} - }); - } - - private void setupKeyPreviewAnimationDuration(final String prefKey, final int defaultValue) { - final SharedPreferences prefs = getSharedPreferences(); - final Resources res = getResources(); - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(prefKey); - if (pref == null) { - return; - } - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - @Override - public void writeValue(final int value, final String key) { - prefs.edit().putInt(key, value).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - prefs.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return Settings.readKeyPreviewAnimationDuration(prefs, key, defaultValue); - } - - @Override - public int readDefaultValue(final String key) { - return defaultValue; - } - - @Override - public String getValueText(final int value) { - return res.getString(R.string.abbreviation_unit_milliseconds, value); - } - - @Override - public void feedbackValue(final int value) {} - }); - } - - private void setupKeyboardHeight(final String prefKey, final float defaultValue) { - final SharedPreferences prefs = getSharedPreferences(); - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(prefKey); - if (pref == null) { - return; - } - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - private static final float PERCENTAGE_FLOAT = 100.0f; - private float getValueFromPercentage(final int percentage) { - return percentage / PERCENTAGE_FLOAT; - } - - private int getPercentageFromValue(final float floatValue) { - return (int)(floatValue * PERCENTAGE_FLOAT); - } - - @Override - public void writeValue(final int value, final String key) { - prefs.edit().putFloat(key, getValueFromPercentage(value)).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - prefs.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return getPercentageFromValue(Settings.readKeyboardHeight(prefs, defaultValue)); - } - - @Override - public int readDefaultValue(final String key) { - return getPercentageFromValue(defaultValue); - } - - @Override - public String getValueText(final int value) { - return String.format(Locale.ROOT, "%d%%", value); - } - - @Override - public void feedbackValue(final int value) {} - }); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/GestureSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/GestureSettingsFragment.java deleted file mode 100644 index 22b0655b4..000000000 --- a/java/src/com/android/inputmethod/latin/settings/GestureSettingsFragment.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.os.Bundle; - -import com.android.inputmethod.latin.R; - -/** - * "Gesture typing preferences" settings sub screen. - * - * This settings sub screen handles the following gesture typing preferences. - * - Enable gesture typing - * - Dynamic floating preview - * - Show gesture trail - * - Phrase gesture - */ -public final class GestureSettingsFragment extends SubScreenFragment { - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_gesture); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/LocalSettingsConstants.java b/java/src/com/android/inputmethod/latin/settings/LocalSettingsConstants.java deleted file mode 100644 index 5c416ab18..000000000 --- a/java/src/com/android/inputmethod/latin/settings/LocalSettingsConstants.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -/** - * Collection of device specific preference constants. - */ -public class LocalSettingsConstants { - // Preference file for storing preferences that are tied to a device - // and are not backed up. - public static final String PREFS_FILE = "local_prefs"; - - // Preference key for the current account. - // Do not restore. - public static final String PREF_ACCOUNT_NAME = "pref_account_name"; - // Preference key for enabling cloud sync feature. - // Do not restore. - public static final String PREF_ENABLE_CLOUD_SYNC = "pref_enable_cloud_sync"; - - // List of preference keys to skip from being restored by backup agent. - // These preferences are tied to a device and hence should not be restored. - // e.g. account name. - // Ideally they could have been kept in a separate file that wasn't backed up - // however the preference UI currently only deals with the default - // shared preferences which makes it non-trivial to move these out to - // a different shared preferences file. - public static final String[] PREFS_TO_SKIP_RESTORING = new String[] { - PREF_ACCOUNT_NAME, - PREF_ENABLE_CLOUD_SYNC, - // The debug settings are not restored on a new device. - // If a feature relies on these, it should ensure that the defaults are - // correctly set for it to work on a new device. - DebugSettings.PREF_DEBUG_MODE, - DebugSettings.PREF_FORCE_NON_DISTINCT_MULTITOUCH, - DebugSettings.PREF_HAS_CUSTOM_KEY_PREVIEW_ANIMATION_PARAMS, - DebugSettings.PREF_KEYBOARD_HEIGHT_SCALE, - DebugSettings.PREF_KEY_PREVIEW_DISMISS_DURATION, - DebugSettings.PREF_KEY_PREVIEW_DISMISS_END_X_SCALE, - DebugSettings.PREF_KEY_PREVIEW_DISMISS_END_Y_SCALE, - DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_DURATION, - DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_START_X_SCALE, - DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_START_Y_SCALE, - DebugSettings.PREF_RESIZE_KEYBOARD, - DebugSettings.PREF_SHOULD_SHOW_LXX_SUGGESTION_UI, - DebugSettings.PREF_SLIDING_KEY_INPUT_PREVIEW - }; -} diff --git a/java/src/com/android/inputmethod/latin/settings/PreferencesSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/PreferencesSettingsFragment.java deleted file mode 100644 index d9858e61f..000000000 --- a/java/src/com/android/inputmethod/latin/settings/PreferencesSettingsFragment.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.os.Build; -import android.os.Bundle; -import android.preference.Preference; - -import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.RichInputMethodManager; - -/** - * "Preferences" settings sub screen. - * - * This settings sub screen handles the following input preferences. - * - Auto-capitalization - * - Double-space period - * - Vibrate on keypress - * - Sound on keypress - * - Popup on keypress - * - Voice input key - */ -public final class PreferencesSettingsFragment extends SubScreenFragment { - - private static final boolean VOICE_IME_ENABLED = - Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; - - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_preferences); - - final Resources res = getResources(); - final Context context = getActivity(); - - // When we are called from the Settings application but we are not already running, some - // singleton and utility classes may not have been initialized. We have to call - // initialization method of these classes here. See {@link LatinIME#onCreate()}. - RichInputMethodManager.init(context); - - final boolean showVoiceKeyOption = res.getBoolean( - R.bool.config_enable_show_voice_key_option); - if (!showVoiceKeyOption) { - removePreference(Settings.PREF_VOICE_INPUT_KEY); - } - if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) { - removePreference(Settings.PREF_VIBRATE_ON); - } - if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) { - removePreference(Settings.PREF_POPUP_ON); - } - - refreshEnablingsOfKeypressSoundAndVibrationSettings(); - } - - @Override - public void onResume() { - super.onResume(); - final Preference voiceInputKeyOption = findPreference(Settings.PREF_VOICE_INPUT_KEY); - if (voiceInputKeyOption != null) { - RichInputMethodManager.getInstance().refreshSubtypeCaches(); - voiceInputKeyOption.setEnabled(VOICE_IME_ENABLED); - voiceInputKeyOption.setSummary(VOICE_IME_ENABLED - ? null : getText(R.string.voice_input_disabled_summary)); - } - } - - @Override - public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { - final Resources res = getResources(); - if (key.equals(Settings.PREF_POPUP_ON)) { - setPreferenceEnabled(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY, - Settings.readKeyPreviewPopupEnabled(prefs, res)); - } - refreshEnablingsOfKeypressSoundAndVibrationSettings(); - } - - private void refreshEnablingsOfKeypressSoundAndVibrationSettings() { - final SharedPreferences prefs = getSharedPreferences(); - final Resources res = getResources(); - setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS, - Settings.readVibrationEnabled(prefs, res)); - setPreferenceEnabled(Settings.PREF_KEYPRESS_SOUND_VOLUME, - Settings.readKeypressSoundEnabled(prefs, res)); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/RadioButtonPreference.java b/java/src/com/android/inputmethod/latin/settings/RadioButtonPreference.java deleted file mode 100644 index 91444604d..000000000 --- a/java/src/com/android/inputmethod/latin/settings/RadioButtonPreference.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.content.Context; -import android.preference.Preference; -import android.util.AttributeSet; -import android.view.View; -import android.widget.RadioButton; - -import com.android.inputmethod.latin.R; - -/** - * Radio Button preference - */ -public class RadioButtonPreference extends Preference { - interface OnRadioButtonClickedListener { - /** - * Called when this preference needs to be saved its state. - * - * @param preference This preference. - */ - public void onRadioButtonClicked(RadioButtonPreference preference); - } - - private boolean mIsSelected; - private RadioButton mRadioButton; - private OnRadioButtonClickedListener mListener; - private final View.OnClickListener mClickListener = new View.OnClickListener() { - @Override - public void onClick(final View v) { - callListenerOnRadioButtonClicked(); - } - }; - - public RadioButtonPreference(final Context context) { - this(context, null); - } - - public RadioButtonPreference(final Context context, final AttributeSet attrs) { - this(context, attrs, android.R.attr.preferenceStyle); - } - - public RadioButtonPreference(final Context context, final AttributeSet attrs, - final int defStyleAttr) { - super(context, attrs, defStyleAttr); - setWidgetLayoutResource(R.layout.radio_button_preference_widget); - } - - public void setOnRadioButtonClickedListener(final OnRadioButtonClickedListener listener) { - mListener = listener; - } - - void callListenerOnRadioButtonClicked() { - if (mListener != null) { - mListener.onRadioButtonClicked(this); - } - } - - @Override - protected void onBindView(final View view) { - super.onBindView(view); - mRadioButton = (RadioButton)view.findViewById(R.id.radio_button); - mRadioButton.setChecked(mIsSelected); - mRadioButton.setOnClickListener(mClickListener); - view.setOnClickListener(mClickListener); - } - - public boolean isSelected() { - return mIsSelected; - } - - public void setSelected(final boolean selected) { - if (selected == mIsSelected) { - return; - } - mIsSelected = selected; - if (mRadioButton != null) { - mRadioButton.setChecked(selected); - } - notifyChanged(); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/SeekBarDialogPreference.java b/java/src/com/android/inputmethod/latin/settings/SeekBarDialogPreference.java deleted file mode 100644 index 802574aa8..000000000 --- a/java/src/com/android/inputmethod/latin/settings/SeekBarDialogPreference.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright (C) 2013 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.settings; - -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.res.TypedArray; -import android.preference.DialogPreference; -import android.util.AttributeSet; -import android.view.View; -import android.widget.SeekBar; -import android.widget.TextView; - -import com.android.inputmethod.latin.R; - -public final class SeekBarDialogPreference extends DialogPreference - implements SeekBar.OnSeekBarChangeListener { - public interface ValueProxy { - public int readValue(final String key); - public int readDefaultValue(final String key); - public void writeValue(final int value, final String key); - public void writeDefaultValue(final String key); - public String getValueText(final int value); - public void feedbackValue(final int value); - } - - private final int mMaxValue; - private final int mMinValue; - private final int mStepValue; - - private TextView mValueView; - private SeekBar mSeekBar; - - private ValueProxy mValueProxy; - - public SeekBarDialogPreference(final Context context, final AttributeSet attrs) { - super(context, attrs); - final TypedArray a = context.obtainStyledAttributes( - attrs, R.styleable.SeekBarDialogPreference, 0, 0); - mMaxValue = a.getInt(R.styleable.SeekBarDialogPreference_maxValue, 0); - mMinValue = a.getInt(R.styleable.SeekBarDialogPreference_minValue, 0); - mStepValue = a.getInt(R.styleable.SeekBarDialogPreference_stepValue, 0); - a.recycle(); - setDialogLayoutResource(R.layout.seek_bar_dialog); - } - - public void setInterface(final ValueProxy proxy) { - mValueProxy = proxy; - final int value = mValueProxy.readValue(getKey()); - setSummary(mValueProxy.getValueText(value)); - } - - @Override - protected View onCreateDialogView() { - final View view = super.onCreateDialogView(); - mSeekBar = (SeekBar)view.findViewById(R.id.seek_bar_dialog_bar); - mSeekBar.setMax(mMaxValue - mMinValue); - mSeekBar.setOnSeekBarChangeListener(this); - mValueView = (TextView)view.findViewById(R.id.seek_bar_dialog_value); - return view; - } - - private int getProgressFromValue(final int value) { - return value - mMinValue; - } - - private int getValueFromProgress(final int progress) { - return progress + mMinValue; - } - - private int clipValue(final int value) { - final int clippedValue = Math.min(mMaxValue, Math.max(mMinValue, value)); - if (mStepValue <= 1) { - return clippedValue; - } - return clippedValue - (clippedValue % mStepValue); - } - - private int getClippedValueFromProgress(final int progress) { - return clipValue(getValueFromProgress(progress)); - } - - @Override - protected void onBindDialogView(final View view) { - final int value = mValueProxy.readValue(getKey()); - mValueView.setText(mValueProxy.getValueText(value)); - mSeekBar.setProgress(getProgressFromValue(clipValue(value))); - } - - @Override - protected void onPrepareDialogBuilder(final AlertDialog.Builder builder) { - builder.setPositiveButton(android.R.string.ok, this) - .setNegativeButton(android.R.string.cancel, this) - .setNeutralButton(R.string.button_default, this); - } - - @Override - public void onClick(final DialogInterface dialog, final int which) { - super.onClick(dialog, which); - final String key = getKey(); - if (which == DialogInterface.BUTTON_NEUTRAL) { - final int value = mValueProxy.readDefaultValue(key); - setSummary(mValueProxy.getValueText(value)); - mValueProxy.writeDefaultValue(key); - return; - } - if (which == DialogInterface.BUTTON_POSITIVE) { - final int value = getClippedValueFromProgress(mSeekBar.getProgress()); - setSummary(mValueProxy.getValueText(value)); - mValueProxy.writeValue(value, key); - return; - } - } - - @Override - public void onProgressChanged(final SeekBar seekBar, final int progress, - final boolean fromUser) { - final int value = getClippedValueFromProgress(progress); - mValueView.setText(mValueProxy.getValueText(value)); - if (!fromUser) { - mSeekBar.setProgress(getProgressFromValue(value)); - } - } - - @Override - public void onStartTrackingTouch(final SeekBar seekBar) {} - - @Override - public void onStopTrackingTouch(final SeekBar seekBar) { - mValueProxy.feedbackValue(getClippedValueFromProgress(seekBar.getProgress())); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java deleted file mode 100644 index 940f1bdfc..000000000 --- a/java/src/com/android/inputmethod/latin/settings/Settings.java +++ /dev/null @@ -1,458 +0,0 @@ -/* - * Copyright (C) 2013 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.settings; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.pm.ApplicationInfo; -import android.content.res.Configuration; -import android.content.res.Resources; -import android.os.Build; -import android.preference.PreferenceManager; -import android.util.Log; - -import com.android.inputmethod.compat.BuildCompatUtils; -import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; -import com.android.inputmethod.latin.InputAttributes; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; -import com.android.inputmethod.latin.utils.ResourceUtils; -import com.android.inputmethod.latin.utils.RunInLocale; -import com.android.inputmethod.latin.utils.StatsUtils; - -import java.util.Collections; -import java.util.Locale; -import java.util.Set; -import java.util.concurrent.locks.ReentrantLock; - -import javax.annotation.Nonnull; - -public final class Settings implements SharedPreferences.OnSharedPreferenceChangeListener { - private static final String TAG = Settings.class.getSimpleName(); - // Settings screens - public static final String SCREEN_ACCOUNTS = "screen_accounts"; - public static final String SCREEN_THEME = "screen_theme"; - public static final String SCREEN_DEBUG = "screen_debug"; - // In the same order as xml/prefs.xml - public static final String PREF_AUTO_CAP = "auto_cap"; - public static final String PREF_VIBRATE_ON = "vibrate_on"; - public static final String PREF_SOUND_ON = "sound_on"; - public static final String PREF_POPUP_ON = "popup_on"; - // PREF_VOICE_MODE_OBSOLETE is obsolete. Use PREF_VOICE_INPUT_KEY instead. - public static final String PREF_VOICE_MODE_OBSOLETE = "voice_mode"; - public static final String PREF_VOICE_INPUT_KEY = "pref_voice_input_key"; - public static final String PREF_EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary"; - public static final String PREF_CONFIGURE_DICTIONARIES_KEY = "configure_dictionaries_key"; - // PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE is obsolete. Use PREF_AUTO_CORRECTION instead. - public static final String PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE = - "auto_correction_threshold"; - public static final String PREF_AUTO_CORRECTION = "pref_key_auto_correction"; - // PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE is obsolete. Use PREF_SHOW_SUGGESTIONS instead. - public static final String PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE = "show_suggestions_setting"; - public static final String PREF_SHOW_SUGGESTIONS = "show_suggestions"; - public static final String PREF_KEY_USE_CONTACTS_DICT = "pref_key_use_contacts_dict"; - public static final String PREF_KEY_USE_PERSONALIZED_DICTS = "pref_key_use_personalized_dicts"; - public static final String PREF_KEY_USE_DOUBLE_SPACE_PERIOD = - "pref_key_use_double_space_period"; - public static final String PREF_BLOCK_POTENTIALLY_OFFENSIVE = - "pref_key_block_potentially_offensive"; - public static final boolean ENABLE_SHOW_LANGUAGE_SWITCH_KEY_SETTINGS = - BuildCompatUtils.EFFECTIVE_SDK_INT <= Build.VERSION_CODES.KITKAT; - public static final boolean SHOULD_SHOW_LXX_SUGGESTION_UI = - BuildCompatUtils.EFFECTIVE_SDK_INT >= Build.VERSION_CODES.LOLLIPOP; - public static final String PREF_SHOW_LANGUAGE_SWITCH_KEY = - "pref_show_language_switch_key"; - public static final String PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST = - "pref_include_other_imes_in_language_switch_list"; - public static final String PREF_CUSTOM_INPUT_STYLES = "custom_input_styles"; - public static final String PREF_ENABLE_SPLIT_KEYBOARD = "pref_split_keyboard"; - // TODO: consolidate key preview dismiss delay with the key preview animation parameters. - public static final String PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY = - "pref_key_preview_popup_dismiss_delay"; - public static final String PREF_BIGRAM_PREDICTIONS = "next_word_prediction"; - public static final String PREF_GESTURE_INPUT = "gesture_input"; - public static final String PREF_VIBRATION_DURATION_SETTINGS = - "pref_vibration_duration_settings"; - public static final String PREF_KEYPRESS_SOUND_VOLUME = "pref_keypress_sound_volume"; - public static final String PREF_KEY_LONGPRESS_TIMEOUT = "pref_key_longpress_timeout"; - public static final String PREF_ENABLE_EMOJI_ALT_PHYSICAL_KEY = - "pref_enable_emoji_alt_physical_key"; - public static final String PREF_GESTURE_PREVIEW_TRAIL = "pref_gesture_preview_trail"; - public static final String PREF_GESTURE_FLOATING_PREVIEW_TEXT = - "pref_gesture_floating_preview_text"; - public static final String PREF_SHOW_SETUP_WIZARD_ICON = "pref_show_setup_wizard_icon"; - - public static final String PREF_KEY_IS_INTERNAL = "pref_key_is_internal"; - - public static final String PREF_ENABLE_METRICS_LOGGING = "pref_enable_metrics_logging"; - // This preference key is deprecated. Use {@link #PREF_SHOW_LANGUAGE_SWITCH_KEY} instead. - // This is being used only for the backward compatibility. - private static final String PREF_SUPPRESS_LANGUAGE_SWITCH_KEY = - "pref_suppress_language_switch_key"; - - private static final String PREF_LAST_USED_PERSONALIZATION_TOKEN = - "pref_last_used_personalization_token"; - private static final String PREF_LAST_PERSONALIZATION_DICT_WIPED_TIME = - "pref_last_used_personalization_dict_wiped_time"; - private static final String PREF_CORPUS_HANDLES_FOR_PERSONALIZATION = - "pref_corpus_handles_for_personalization"; - - // Emoji - public static final String PREF_EMOJI_RECENT_KEYS = "emoji_recent_keys"; - public static final String PREF_EMOJI_CATEGORY_LAST_TYPED_ID = "emoji_category_last_typed_id"; - public static final String PREF_LAST_SHOWN_EMOJI_CATEGORY_ID = "last_shown_emoji_category_id"; - - private static final float UNDEFINED_PREFERENCE_VALUE_FLOAT = -1.0f; - private static final int UNDEFINED_PREFERENCE_VALUE_INT = -1; - - private Context mContext; - private Resources mRes; - private SharedPreferences mPrefs; - private SettingsValues mSettingsValues; - private final ReentrantLock mSettingsValuesLock = new ReentrantLock(); - - private static final Settings sInstance = new Settings(); - - public static Settings getInstance() { - return sInstance; - } - - public static void init(final Context context) { - sInstance.onCreate(context); - } - - private Settings() { - // Intentional empty constructor for singleton. - } - - private void onCreate(final Context context) { - mContext = context; - mRes = context.getResources(); - mPrefs = PreferenceManager.getDefaultSharedPreferences(context); - mPrefs.registerOnSharedPreferenceChangeListener(this); - upgradeAutocorrectionSettings(mPrefs, mRes); - } - - public void onDestroy() { - mPrefs.unregisterOnSharedPreferenceChangeListener(this); - } - - @Override - public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { - mSettingsValuesLock.lock(); - try { - if (mSettingsValues == null) { - // TODO: Introduce a static function to register this class and ensure that - // loadSettings must be called before "onSharedPreferenceChanged" is called. - Log.w(TAG, "onSharedPreferenceChanged called before loadSettings."); - return; - } - loadSettings(mContext, mSettingsValues.mLocale, mSettingsValues.mInputAttributes); - StatsUtils.onLoadSettings(mSettingsValues); - } finally { - mSettingsValuesLock.unlock(); - } - } - - public void loadSettings(final Context context, final Locale locale, - @Nonnull final InputAttributes inputAttributes) { - mSettingsValuesLock.lock(); - mContext = context; - try { - final SharedPreferences prefs = mPrefs; - final RunInLocale<SettingsValues> job = new RunInLocale<SettingsValues>() { - @Override - protected SettingsValues job(final Resources res) { - return new SettingsValues(context, prefs, res, inputAttributes); - } - }; - mSettingsValues = job.runInLocale(mRes, locale); - } finally { - mSettingsValuesLock.unlock(); - } - } - - // TODO: Remove this method and add proxy method to SettingsValues. - public SettingsValues getCurrent() { - return mSettingsValues; - } - - public boolean isInternal() { - return mSettingsValues.mIsInternal; - } - - public static int readScreenMetrics(final Resources res) { - return res.getInteger(R.integer.config_screen_metrics); - } - - // Accessed from the settings interface, hence public - public static boolean readKeypressSoundEnabled(final SharedPreferences prefs, - final Resources res) { - return prefs.getBoolean(PREF_SOUND_ON, - res.getBoolean(R.bool.config_default_sound_enabled)); - } - - public static boolean readVibrationEnabled(final SharedPreferences prefs, - final Resources res) { - final boolean hasVibrator = AudioAndHapticFeedbackManager.getInstance().hasVibrator(); - return hasVibrator && prefs.getBoolean(PREF_VIBRATE_ON, - res.getBoolean(R.bool.config_default_vibration_enabled)); - } - - public static boolean readAutoCorrectEnabled(final SharedPreferences prefs, - final Resources res) { - return prefs.getBoolean(PREF_AUTO_CORRECTION, true); - } - - public static float readPlausibilityThreshold(final Resources res) { - return Float.parseFloat(res.getString(R.string.plausibility_threshold)); - } - - public static boolean readBlockPotentiallyOffensive(final SharedPreferences prefs, - final Resources res) { - return prefs.getBoolean(PREF_BLOCK_POTENTIALLY_OFFENSIVE, - res.getBoolean(R.bool.config_block_potentially_offensive)); - } - - public static boolean readFromBuildConfigIfGestureInputEnabled(final Resources res) { - return res.getBoolean(R.bool.config_gesture_input_enabled_by_build_config); - } - - public static boolean readGestureInputEnabled(final SharedPreferences prefs, - final Resources res) { - return readFromBuildConfigIfGestureInputEnabled(res) - && prefs.getBoolean(PREF_GESTURE_INPUT, true); - } - - public static boolean readFromBuildConfigIfToShowKeyPreviewPopupOption(final Resources res) { - return res.getBoolean(R.bool.config_enable_show_key_preview_popup_option); - } - - public static boolean readKeyPreviewPopupEnabled(final SharedPreferences prefs, - final Resources res) { - final boolean defaultKeyPreviewPopup = res.getBoolean( - R.bool.config_default_key_preview_popup); - if (!readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) { - return defaultKeyPreviewPopup; - } - return prefs.getBoolean(PREF_POPUP_ON, defaultKeyPreviewPopup); - } - - public static int readKeyPreviewPopupDismissDelay(final SharedPreferences prefs, - final Resources res) { - return Integer.parseInt(prefs.getString(PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY, - Integer.toString(res.getInteger( - R.integer.config_key_preview_linger_timeout)))); - } - - public static boolean readShowsLanguageSwitchKey(final SharedPreferences prefs) { - if (prefs.contains(PREF_SUPPRESS_LANGUAGE_SWITCH_KEY)) { - final boolean suppressLanguageSwitchKey = prefs.getBoolean( - PREF_SUPPRESS_LANGUAGE_SWITCH_KEY, false); - final SharedPreferences.Editor editor = prefs.edit(); - editor.remove(PREF_SUPPRESS_LANGUAGE_SWITCH_KEY); - editor.putBoolean(PREF_SHOW_LANGUAGE_SWITCH_KEY, !suppressLanguageSwitchKey); - editor.apply(); - } - return prefs.getBoolean(PREF_SHOW_LANGUAGE_SWITCH_KEY, true); - } - - public static String readPrefAdditionalSubtypes(final SharedPreferences prefs, - final Resources res) { - final String predefinedPrefSubtypes = AdditionalSubtypeUtils.createPrefSubtypes( - res.getStringArray(R.array.predefined_subtypes)); - return prefs.getString(PREF_CUSTOM_INPUT_STYLES, predefinedPrefSubtypes); - } - - public static void writePrefAdditionalSubtypes(final SharedPreferences prefs, - final String prefSubtypes) { - prefs.edit().putString(PREF_CUSTOM_INPUT_STYLES, prefSubtypes).apply(); - } - - public static float readKeypressSoundVolume(final SharedPreferences prefs, - final Resources res) { - final float volume = prefs.getFloat( - PREF_KEYPRESS_SOUND_VOLUME, UNDEFINED_PREFERENCE_VALUE_FLOAT); - return (volume != UNDEFINED_PREFERENCE_VALUE_FLOAT) ? volume - : readDefaultKeypressSoundVolume(res); - } - - // Default keypress sound volume for unknown devices. - // The negative value means system default. - private static final String DEFAULT_KEYPRESS_SOUND_VOLUME = Float.toString(-1.0f); - - public static float readDefaultKeypressSoundVolume(final Resources res) { - return Float.parseFloat(ResourceUtils.getDeviceOverrideValue(res, - R.array.keypress_volumes, DEFAULT_KEYPRESS_SOUND_VOLUME)); - } - - public static int readKeyLongpressTimeout(final SharedPreferences prefs, - final Resources res) { - final int milliseconds = prefs.getInt( - PREF_KEY_LONGPRESS_TIMEOUT, UNDEFINED_PREFERENCE_VALUE_INT); - return (milliseconds != UNDEFINED_PREFERENCE_VALUE_INT) ? milliseconds - : readDefaultKeyLongpressTimeout(res); - } - - public static int readDefaultKeyLongpressTimeout(final Resources res) { - return res.getInteger(R.integer.config_default_longpress_key_timeout); - } - - public static int readKeypressVibrationDuration(final SharedPreferences prefs, - final Resources res) { - final int milliseconds = prefs.getInt( - PREF_VIBRATION_DURATION_SETTINGS, UNDEFINED_PREFERENCE_VALUE_INT); - return (milliseconds != UNDEFINED_PREFERENCE_VALUE_INT) ? milliseconds - : readDefaultKeypressVibrationDuration(res); - } - - // Default keypress vibration duration for unknown devices. - // The negative value means system default. - private static final String DEFAULT_KEYPRESS_VIBRATION_DURATION = Integer.toString(-1); - - public static int readDefaultKeypressVibrationDuration(final Resources res) { - return Integer.parseInt(ResourceUtils.getDeviceOverrideValue(res, - R.array.keypress_vibration_durations, DEFAULT_KEYPRESS_VIBRATION_DURATION)); - } - - public static float readKeyPreviewAnimationScale(final SharedPreferences prefs, - final String prefKey, final float defaultValue) { - final float fraction = prefs.getFloat(prefKey, UNDEFINED_PREFERENCE_VALUE_FLOAT); - return (fraction != UNDEFINED_PREFERENCE_VALUE_FLOAT) ? fraction : defaultValue; - } - - public static int readKeyPreviewAnimationDuration(final SharedPreferences prefs, - final String prefKey, final int defaultValue) { - final int milliseconds = prefs.getInt(prefKey, UNDEFINED_PREFERENCE_VALUE_INT); - return (milliseconds != UNDEFINED_PREFERENCE_VALUE_INT) ? milliseconds : defaultValue; - } - - public static float readKeyboardHeight(final SharedPreferences prefs, - final float defaultValue) { - final float percentage = prefs.getFloat( - DebugSettings.PREF_KEYBOARD_HEIGHT_SCALE, UNDEFINED_PREFERENCE_VALUE_FLOAT); - return (percentage != UNDEFINED_PREFERENCE_VALUE_FLOAT) ? percentage : defaultValue; - } - - public static boolean readUseFullscreenMode(final Resources res) { - return res.getBoolean(R.bool.config_use_fullscreen_mode); - } - - public static boolean readShowSetupWizardIcon(final SharedPreferences prefs, - final Context context) { - if (!prefs.contains(PREF_SHOW_SETUP_WIZARD_ICON)) { - final ApplicationInfo appInfo = context.getApplicationInfo(); - final boolean isApplicationInSystemImage = - (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; - // Default value - return !isApplicationInSystemImage; - } - return prefs.getBoolean(PREF_SHOW_SETUP_WIZARD_ICON, false); - } - - public static boolean readHasHardwareKeyboard(final Configuration conf) { - // The standard way of finding out whether we have a hardware keyboard. This code is taken - // from InputMethodService#onEvaluateInputShown, which canonically determines this. - // In a nutshell, we have a keyboard if the configuration says the type of hardware keyboard - // is NOKEYS and if it's not hidden (e.g. folded inside the device). - return conf.keyboard != Configuration.KEYBOARD_NOKEYS - && conf.hardKeyboardHidden != Configuration.HARDKEYBOARDHIDDEN_YES; - } - - public static boolean isInternal(final SharedPreferences prefs) { - return prefs.getBoolean(PREF_KEY_IS_INTERNAL, false); - } - - public void writeLastUsedPersonalizationToken(byte[] token) { - if (token == null) { - mPrefs.edit().remove(PREF_LAST_USED_PERSONALIZATION_TOKEN).apply(); - } else { - final String tokenStr = StringUtils.byteArrayToHexString(token); - mPrefs.edit().putString(PREF_LAST_USED_PERSONALIZATION_TOKEN, tokenStr).apply(); - } - } - - public byte[] readLastUsedPersonalizationToken() { - final String tokenStr = mPrefs.getString(PREF_LAST_USED_PERSONALIZATION_TOKEN, null); - return StringUtils.hexStringToByteArray(tokenStr); - } - - public void writeLastPersonalizationDictWipedTime(final long timestamp) { - mPrefs.edit().putLong(PREF_LAST_PERSONALIZATION_DICT_WIPED_TIME, timestamp).apply(); - } - - public long readLastPersonalizationDictGeneratedTime() { - return mPrefs.getLong(PREF_LAST_PERSONALIZATION_DICT_WIPED_TIME, 0); - } - - public void writeCorpusHandlesForPersonalization(final Set<String> corpusHandles) { - mPrefs.edit().putStringSet(PREF_CORPUS_HANDLES_FOR_PERSONALIZATION, corpusHandles).apply(); - } - - public Set<String> readCorpusHandlesForPersonalization() { - final Set<String> emptySet = Collections.emptySet(); - return mPrefs.getStringSet(PREF_CORPUS_HANDLES_FOR_PERSONALIZATION, emptySet); - } - - public static void writeEmojiRecentKeys(final SharedPreferences prefs, String str) { - prefs.edit().putString(PREF_EMOJI_RECENT_KEYS, str).apply(); - } - - public static String readEmojiRecentKeys(final SharedPreferences prefs) { - return prefs.getString(PREF_EMOJI_RECENT_KEYS, ""); - } - - public static void writeLastTypedEmojiCategoryPageId( - final SharedPreferences prefs, final int categoryId, final int categoryPageId) { - final String key = PREF_EMOJI_CATEGORY_LAST_TYPED_ID + categoryId; - prefs.edit().putInt(key, categoryPageId).apply(); - } - - public static int readLastTypedEmojiCategoryPageId( - final SharedPreferences prefs, final int categoryId) { - final String key = PREF_EMOJI_CATEGORY_LAST_TYPED_ID + categoryId; - return prefs.getInt(key, 0); - } - - public static void writeLastShownEmojiCategoryId( - final SharedPreferences prefs, final int categoryId) { - prefs.edit().putInt(PREF_LAST_SHOWN_EMOJI_CATEGORY_ID, categoryId).apply(); - } - - public static int readLastShownEmojiCategoryId( - final SharedPreferences prefs, final int defValue) { - return prefs.getInt(PREF_LAST_SHOWN_EMOJI_CATEGORY_ID, defValue); - } - - private void upgradeAutocorrectionSettings(final SharedPreferences prefs, final Resources res) { - final String thresholdSetting = - prefs.getString(PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE, null); - if (thresholdSetting != null) { - SharedPreferences.Editor editor = prefs.edit(); - editor.remove(PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE); - final String autoCorrectionOff = - res.getString(R.string.auto_correction_threshold_mode_index_off); - if (thresholdSetting.equals(autoCorrectionOff)) { - editor.putBoolean(PREF_AUTO_CORRECTION, false); - } else { - editor.putBoolean(PREF_AUTO_CORRECTION, true); - } - editor.commit(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java b/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java deleted file mode 100644 index ac1657762..000000000 --- a/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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.settings; - -import com.android.inputmethod.latin.permissions.PermissionsManager; -import com.android.inputmethod.latin.utils.FragmentUtils; -import com.android.inputmethod.latin.utils.StatsUtils; -import com.android.inputmethod.latin.utils.StatsUtilsManager; - -import android.app.ActionBar; -import android.content.Intent; -import android.os.Bundle; -import android.preference.PreferenceActivity; -import androidx.core.app.ActivityCompat; -import android.view.MenuItem; - -public final class SettingsActivity extends PreferenceActivity - implements ActivityCompat.OnRequestPermissionsResultCallback { - private static final String DEFAULT_FRAGMENT = SettingsFragment.class.getName(); - - public static final String EXTRA_SHOW_HOME_AS_UP = "show_home_as_up"; - public static final String EXTRA_ENTRY_KEY = "entry"; - public static final String EXTRA_ENTRY_VALUE_LONG_PRESS_COMMA = "long_press_comma"; - public static final String EXTRA_ENTRY_VALUE_APP_ICON = "app_icon"; - public static final String EXTRA_ENTRY_VALUE_NOTICE_DIALOG = "important_notice"; - public static final String EXTRA_ENTRY_VALUE_SYSTEM_SETTINGS = "system_settings"; - - private boolean mShowHomeAsUp; - - @Override - protected void onCreate(final Bundle savedState) { - super.onCreate(savedState); - final ActionBar actionBar = getActionBar(); - final Intent intent = getIntent(); - if (actionBar != null) { - mShowHomeAsUp = intent.getBooleanExtra(EXTRA_SHOW_HOME_AS_UP, true); - actionBar.setDisplayHomeAsUpEnabled(mShowHomeAsUp); - actionBar.setHomeButtonEnabled(mShowHomeAsUp); - } - StatsUtils.onSettingsActivity( - intent.hasExtra(EXTRA_ENTRY_KEY) ? intent.getStringExtra(EXTRA_ENTRY_KEY) - : EXTRA_ENTRY_VALUE_SYSTEM_SETTINGS); - } - - @Override - public boolean onOptionsItemSelected(final MenuItem item) { - if (mShowHomeAsUp && item.getItemId() == android.R.id.home) { - finish(); - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - public Intent getIntent() { - final Intent intent = super.getIntent(); - final String fragment = intent.getStringExtra(EXTRA_SHOW_FRAGMENT); - if (fragment == null) { - intent.putExtra(EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT); - } - intent.putExtra(EXTRA_NO_HEADERS, true); - return intent; - } - - @Override - public boolean isValidFragment(final String fragmentName) { - return FragmentUtils.isValidFragment(fragmentName); - } - - @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { - PermissionsManager.get(this).onRequestPermissionsResult(requestCode, permissions, grantResults); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java deleted file mode 100644 index 874f221c6..000000000 --- a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2008 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.settings; - -import android.app.Activity; -import android.content.Intent; -import android.os.Build; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.PreferenceScreen; -import android.provider.Settings.Secure; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.define.ProductionFlags; -import com.android.inputmethod.latin.utils.ApplicationUtils; -import com.android.inputmethod.latin.utils.FeedbackUtils; -import com.android.inputmethodcommon.InputMethodSettingsFragment; - -public final class SettingsFragment extends InputMethodSettingsFragment { - // We don't care about menu grouping. - private static final int NO_MENU_GROUP = Menu.NONE; - // The first menu item id and order. - private static final int MENU_ABOUT = Menu.FIRST; - // The second menu item id and order. - private static final int MENU_HELP_AND_FEEDBACK = Menu.FIRST + 1; - - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - setHasOptionsMenu(true); - setInputMethodSettingsCategoryTitle(R.string.language_selection_title); - setSubtypeEnablerTitle(R.string.select_language); - addPreferencesFromResource(R.xml.prefs); - final PreferenceScreen preferenceScreen = getPreferenceScreen(); - preferenceScreen.setTitle( - ApplicationUtils.getActivityTitleResId(getActivity(), SettingsActivity.class)); - if (!ProductionFlags.ENABLE_ACCOUNT_SIGN_IN) { - final Preference accountsPreference = findPreference(Settings.SCREEN_ACCOUNTS); - preferenceScreen.removePreference(accountsPreference); - } - } - - @Override - public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { - if (FeedbackUtils.isHelpAndFeedbackFormSupported()) { - menu.add(NO_MENU_GROUP, MENU_HELP_AND_FEEDBACK /* itemId */, - MENU_HELP_AND_FEEDBACK /* order */, R.string.help_and_feedback); - } - final int aboutResId = FeedbackUtils.getAboutKeyboardTitleResId(); - if (aboutResId != 0) { - menu.add(NO_MENU_GROUP, MENU_ABOUT /* itemId */, MENU_ABOUT /* order */, aboutResId); - } - } - - @Override - public boolean onOptionsItemSelected(final MenuItem item) { - final Activity activity = getActivity(); - if (!isUserSetupComplete(activity)) { - // If setup is not complete, it's not safe to launch Help or other activities - // because they might go to the Play Store. See b/19866981. - return true; - } - final int itemId = item.getItemId(); - if (itemId == MENU_HELP_AND_FEEDBACK) { - FeedbackUtils.showHelpAndFeedbackForm(activity); - return true; - } - if (itemId == MENU_ABOUT) { - final Intent aboutIntent = FeedbackUtils.getAboutKeyboardIntent(activity); - if (aboutIntent != null) { - startActivity(aboutIntent); - return true; - } - } - return super.onOptionsItemSelected(item); - } - - private static boolean isUserSetupComplete(final Activity activity) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - return true; - } - return Secure.getInt(activity.getContentResolver(), "user_setup_complete", 0) != 0; - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java deleted file mode 100644 index 57018244f..000000000 --- a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java +++ /dev/null @@ -1,452 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.settings; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.pm.PackageInfo; -import android.content.res.Configuration; -import android.content.res.Resources; -import android.os.Build; -import android.util.Log; -import android.view.inputmethod.EditorInfo; - -import com.android.inputmethod.compat.AppWorkaroundsUtils; -import com.android.inputmethod.latin.InputAttributes; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.RichInputMethodManager; -import com.android.inputmethod.latin.utils.AsyncResultHolder; -import com.android.inputmethod.latin.utils.ResourceUtils; -import com.android.inputmethod.latin.utils.TargetPackageInfoGetterTask; - -import java.util.Arrays; -import java.util.Locale; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * When you call the constructor of this class, you may want to change the current system locale by - * using {@link com.android.inputmethod.latin.utils.RunInLocale}. - */ -// Non-final for testing via mock library. -public class SettingsValues { - private static final String TAG = SettingsValues.class.getSimpleName(); - // "floatMaxValue" and "floatNegativeInfinity" are special marker strings for - // Float.NEGATIVE_INFINITE and Float.MAX_VALUE. Currently used for auto-correction settings. - private static final String FLOAT_MAX_VALUE_MARKER_STRING = "floatMaxValue"; - private static final String FLOAT_NEGATIVE_INFINITY_MARKER_STRING = "floatNegativeInfinity"; - private static final int TIMEOUT_TO_GET_TARGET_PACKAGE = 5; // seconds - public static final float DEFAULT_SIZE_SCALE = 1.0f; // 100% - - // From resources: - public final SpacingAndPunctuations mSpacingAndPunctuations; - public final int mDelayInMillisecondsToUpdateOldSuggestions; - public final long mDoubleSpacePeriodTimeout; - // From configuration: - public final Locale mLocale; - public final boolean mHasHardwareKeyboard; - public final int mDisplayOrientation; - // From preferences, in the same order as xml/prefs.xml: - public final boolean mAutoCap; - public final boolean mVibrateOn; - public final boolean mSoundOn; - public final boolean mKeyPreviewPopupOn; - public final boolean mShowsVoiceInputKey; - public final boolean mIncludesOtherImesInLanguageSwitchList; - public final boolean mShowsLanguageSwitchKey; - public final boolean mUseContactsDict; - public final boolean mUsePersonalizedDicts; - public final boolean mUseDoubleSpacePeriod; - public final boolean mBlockPotentiallyOffensive; - // Use bigrams to predict the next word when there is no input for it yet - public final boolean mBigramPredictionEnabled; - public final boolean mGestureInputEnabled; - public final boolean mGestureTrailEnabled; - public final boolean mGestureFloatingPreviewTextEnabled; - public final boolean mSlidingKeyInputPreviewEnabled; - public final int mKeyLongpressTimeout; - public final boolean mEnableEmojiAltPhysicalKey; - public final boolean mShowAppIcon; - public final boolean mIsShowAppIconSettingInPreferences; - public final boolean mCloudSyncEnabled; - public final boolean mEnableMetricsLogging; - public final boolean mShouldShowLxxSuggestionUi; - // Use split layout for keyboard. - public final boolean mIsSplitKeyboardEnabled; - public final int mScreenMetrics; - - // From the input box - @Nonnull - public final InputAttributes mInputAttributes; - - // Deduced settings - public final int mKeypressVibrationDuration; - public final float mKeypressSoundVolume; - public final int mKeyPreviewPopupDismissDelay; - private final boolean mAutoCorrectEnabled; - public final float mAutoCorrectionThreshold; - public final float mPlausibilityThreshold; - public final boolean mAutoCorrectionEnabledPerUserSettings; - private final boolean mSuggestionsEnabledPerUserSettings; - private final AsyncResultHolder<AppWorkaroundsUtils> mAppWorkarounds; - - // Debug settings - public final boolean mIsInternal; - public final boolean mHasCustomKeyPreviewAnimationParams; - public final boolean mHasKeyboardResize; - public final float mKeyboardHeightScale; - public final int mKeyPreviewShowUpDuration; - public final int mKeyPreviewDismissDuration; - public final float mKeyPreviewShowUpStartXScale; - public final float mKeyPreviewShowUpStartYScale; - public final float mKeyPreviewDismissEndXScale; - public final float mKeyPreviewDismissEndYScale; - - @Nullable public final String mAccount; - - public SettingsValues(final Context context, final SharedPreferences prefs, final Resources res, - @Nonnull final InputAttributes inputAttributes) { - mLocale = res.getConfiguration().locale; - // Get the resources - mDelayInMillisecondsToUpdateOldSuggestions = - res.getInteger(R.integer.config_delay_in_milliseconds_to_update_old_suggestions); - mSpacingAndPunctuations = new SpacingAndPunctuations(res); - - // Store the input attributes - mInputAttributes = inputAttributes; - - // Get the settings preferences - mAutoCap = prefs.getBoolean(Settings.PREF_AUTO_CAP, true); - mVibrateOn = Settings.readVibrationEnabled(prefs, res); - mSoundOn = Settings.readKeypressSoundEnabled(prefs, res); - mKeyPreviewPopupOn = Settings.readKeyPreviewPopupEnabled(prefs, res); - mSlidingKeyInputPreviewEnabled = prefs.getBoolean( - DebugSettings.PREF_SLIDING_KEY_INPUT_PREVIEW, true); - mShowsVoiceInputKey = needsToShowVoiceInputKey(prefs, res) - && mInputAttributes.mShouldShowVoiceInputKey - && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; - mIncludesOtherImesInLanguageSwitchList = Settings.ENABLE_SHOW_LANGUAGE_SWITCH_KEY_SETTINGS - ? prefs.getBoolean(Settings.PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST, false) - : true /* forcibly */; - mShowsLanguageSwitchKey = Settings.ENABLE_SHOW_LANGUAGE_SWITCH_KEY_SETTINGS - ? Settings.readShowsLanguageSwitchKey(prefs) : true /* forcibly */; - mUseContactsDict = prefs.getBoolean(Settings.PREF_KEY_USE_CONTACTS_DICT, true); - mUsePersonalizedDicts = prefs.getBoolean(Settings.PREF_KEY_USE_PERSONALIZED_DICTS, true); - mUseDoubleSpacePeriod = prefs.getBoolean(Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, true) - && inputAttributes.mIsGeneralTextInput; - mBlockPotentiallyOffensive = Settings.readBlockPotentiallyOffensive(prefs, res); - mAutoCorrectEnabled = Settings.readAutoCorrectEnabled(prefs, res); - final String autoCorrectionThresholdRawValue = mAutoCorrectEnabled - ? res.getString(R.string.auto_correction_threshold_mode_index_modest) - : res.getString(R.string.auto_correction_threshold_mode_index_off); - mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res); - mDoubleSpacePeriodTimeout = res.getInteger(R.integer.config_double_space_period_timeout); - mHasHardwareKeyboard = Settings.readHasHardwareKeyboard(res.getConfiguration()); - mEnableMetricsLogging = prefs.getBoolean(Settings.PREF_ENABLE_METRICS_LOGGING, true); - mIsSplitKeyboardEnabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD, false); - mScreenMetrics = Settings.readScreenMetrics(res); - - mShouldShowLxxSuggestionUi = Settings.SHOULD_SHOW_LXX_SUGGESTION_UI - && prefs.getBoolean(DebugSettings.PREF_SHOULD_SHOW_LXX_SUGGESTION_UI, true); - // Compute other readable settings - mKeyLongpressTimeout = Settings.readKeyLongpressTimeout(prefs, res); - mKeypressVibrationDuration = Settings.readKeypressVibrationDuration(prefs, res); - mKeypressSoundVolume = Settings.readKeypressSoundVolume(prefs, res); - mKeyPreviewPopupDismissDelay = Settings.readKeyPreviewPopupDismissDelay(prefs, res); - mEnableEmojiAltPhysicalKey = prefs.getBoolean( - Settings.PREF_ENABLE_EMOJI_ALT_PHYSICAL_KEY, true); - mShowAppIcon = Settings.readShowSetupWizardIcon(prefs, context); - mIsShowAppIconSettingInPreferences = prefs.contains(Settings.PREF_SHOW_SETUP_WIZARD_ICON); - mAutoCorrectionThreshold = readAutoCorrectionThreshold(res, - autoCorrectionThresholdRawValue); - mPlausibilityThreshold = Settings.readPlausibilityThreshold(res); - mGestureInputEnabled = Settings.readGestureInputEnabled(prefs, res); - mGestureTrailEnabled = prefs.getBoolean(Settings.PREF_GESTURE_PREVIEW_TRAIL, true); - mCloudSyncEnabled = prefs.getBoolean(LocalSettingsConstants.PREF_ENABLE_CLOUD_SYNC, false); - mAccount = prefs.getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, - null /* default */); - mGestureFloatingPreviewTextEnabled = !mInputAttributes.mDisableGestureFloatingPreviewText - && prefs.getBoolean(Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT, true); - mAutoCorrectionEnabledPerUserSettings = mAutoCorrectEnabled - && !mInputAttributes.mInputTypeNoAutoCorrect; - mSuggestionsEnabledPerUserSettings = readSuggestionsEnabled(prefs); - mIsInternal = Settings.isInternal(prefs); - mHasCustomKeyPreviewAnimationParams = prefs.getBoolean( - DebugSettings.PREF_HAS_CUSTOM_KEY_PREVIEW_ANIMATION_PARAMS, false); - mHasKeyboardResize = prefs.getBoolean(DebugSettings.PREF_RESIZE_KEYBOARD, false); - mKeyboardHeightScale = Settings.readKeyboardHeight(prefs, DEFAULT_SIZE_SCALE); - mKeyPreviewShowUpDuration = Settings.readKeyPreviewAnimationDuration( - prefs, DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_DURATION, - res.getInteger(R.integer.config_key_preview_show_up_duration)); - mKeyPreviewDismissDuration = Settings.readKeyPreviewAnimationDuration( - prefs, DebugSettings.PREF_KEY_PREVIEW_DISMISS_DURATION, - res.getInteger(R.integer.config_key_preview_dismiss_duration)); - final float defaultKeyPreviewShowUpStartScale = ResourceUtils.getFloatFromFraction( - res, R.fraction.config_key_preview_show_up_start_scale); - final float defaultKeyPreviewDismissEndScale = ResourceUtils.getFloatFromFraction( - res, R.fraction.config_key_preview_dismiss_end_scale); - mKeyPreviewShowUpStartXScale = Settings.readKeyPreviewAnimationScale( - prefs, DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_START_X_SCALE, - defaultKeyPreviewShowUpStartScale); - mKeyPreviewShowUpStartYScale = Settings.readKeyPreviewAnimationScale( - prefs, DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_START_Y_SCALE, - defaultKeyPreviewShowUpStartScale); - mKeyPreviewDismissEndXScale = Settings.readKeyPreviewAnimationScale( - prefs, DebugSettings.PREF_KEY_PREVIEW_DISMISS_END_X_SCALE, - defaultKeyPreviewDismissEndScale); - mKeyPreviewDismissEndYScale = Settings.readKeyPreviewAnimationScale( - prefs, DebugSettings.PREF_KEY_PREVIEW_DISMISS_END_Y_SCALE, - defaultKeyPreviewDismissEndScale); - mDisplayOrientation = res.getConfiguration().orientation; - mAppWorkarounds = new AsyncResultHolder<>("AppWorkarounds"); - final PackageInfo packageInfo = TargetPackageInfoGetterTask.getCachedPackageInfo( - mInputAttributes.mTargetApplicationPackageName); - if (null != packageInfo) { - mAppWorkarounds.set(new AppWorkaroundsUtils(packageInfo)); - } else { - new TargetPackageInfoGetterTask(context, mAppWorkarounds) - .execute(mInputAttributes.mTargetApplicationPackageName); - } - } - - public boolean isMetricsLoggingEnabled() { - return mEnableMetricsLogging; - } - - public boolean isApplicationSpecifiedCompletionsOn() { - return mInputAttributes.mApplicationSpecifiedCompletionOn; - } - - public boolean needsToLookupSuggestions() { - return mInputAttributes.mShouldShowSuggestions - && (mAutoCorrectionEnabledPerUserSettings || isSuggestionsEnabledPerUserSettings()); - } - - public boolean isSuggestionsEnabledPerUserSettings() { - return mSuggestionsEnabledPerUserSettings; - } - - public boolean isPersonalizationEnabled() { - return mUsePersonalizedDicts; - } - - public boolean isWordSeparator(final int code) { - return mSpacingAndPunctuations.isWordSeparator(code); - } - - public boolean isWordConnector(final int code) { - return mSpacingAndPunctuations.isWordConnector(code); - } - - public boolean isWordCodePoint(final int code) { - return Character.isLetter(code) || isWordConnector(code) - || Character.COMBINING_SPACING_MARK == Character.getType(code); - } - - public boolean isUsuallyPrecededBySpace(final int code) { - return mSpacingAndPunctuations.isUsuallyPrecededBySpace(code); - } - - public boolean isUsuallyFollowedBySpace(final int code) { - return mSpacingAndPunctuations.isUsuallyFollowedBySpace(code); - } - - public boolean shouldInsertSpacesAutomatically() { - return mInputAttributes.mShouldInsertSpacesAutomatically; - } - - public boolean isLanguageSwitchKeyEnabled() { - if (!mShowsLanguageSwitchKey) { - return false; - } - final RichInputMethodManager imm = RichInputMethodManager.getInstance(); - if (mIncludesOtherImesInLanguageSwitchList) { - return imm.hasMultipleEnabledIMEsOrSubtypes(false /* include aux subtypes */); - } - return imm.hasMultipleEnabledSubtypesInThisIme(false /* include aux subtypes */); - } - - public boolean isSameInputType(final EditorInfo editorInfo) { - return mInputAttributes.isSameInputType(editorInfo); - } - - public boolean hasSameOrientation(final Configuration configuration) { - return mDisplayOrientation == configuration.orientation; - } - - public boolean isBeforeJellyBean() { - final AppWorkaroundsUtils appWorkaroundUtils - = mAppWorkarounds.get(null, TIMEOUT_TO_GET_TARGET_PACKAGE); - return null == appWorkaroundUtils ? false : appWorkaroundUtils.isBeforeJellyBean(); - } - - public boolean isBrokenByRecorrection() { - final AppWorkaroundsUtils appWorkaroundUtils - = mAppWorkarounds.get(null, TIMEOUT_TO_GET_TARGET_PACKAGE); - return null == appWorkaroundUtils ? false : appWorkaroundUtils.isBrokenByRecorrection(); - } - - private static final String SUGGESTIONS_VISIBILITY_HIDE_VALUE_OBSOLETE = "2"; - - private static boolean readSuggestionsEnabled(final SharedPreferences prefs) { - if (prefs.contains(Settings.PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE)) { - final boolean alwaysHide = SUGGESTIONS_VISIBILITY_HIDE_VALUE_OBSOLETE.equals( - prefs.getString(Settings.PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE, null)); - prefs.edit() - .remove(Settings.PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE) - .putBoolean(Settings.PREF_SHOW_SUGGESTIONS, !alwaysHide) - .apply(); - } - return prefs.getBoolean(Settings.PREF_SHOW_SUGGESTIONS, true); - } - - private static boolean readBigramPredictionEnabled(final SharedPreferences prefs, - final Resources res) { - return prefs.getBoolean(Settings.PREF_BIGRAM_PREDICTIONS, res.getBoolean( - R.bool.config_default_next_word_prediction)); - } - - private static float readAutoCorrectionThreshold(final Resources res, - final String currentAutoCorrectionSetting) { - final String[] autoCorrectionThresholdValues = res.getStringArray( - R.array.auto_correction_threshold_values); - // When autoCorrectionThreshold is greater than 1.0, it's like auto correction is off. - final float autoCorrectionThreshold; - try { - final int arrayIndex = Integer.parseInt(currentAutoCorrectionSetting); - if (arrayIndex >= 0 && arrayIndex < autoCorrectionThresholdValues.length) { - final String val = autoCorrectionThresholdValues[arrayIndex]; - if (FLOAT_MAX_VALUE_MARKER_STRING.equals(val)) { - autoCorrectionThreshold = Float.MAX_VALUE; - } else if (FLOAT_NEGATIVE_INFINITY_MARKER_STRING.equals(val)) { - autoCorrectionThreshold = Float.NEGATIVE_INFINITY; - } else { - autoCorrectionThreshold = Float.parseFloat(val); - } - } else { - autoCorrectionThreshold = Float.MAX_VALUE; - } - } catch (final NumberFormatException e) { - // Whenever the threshold settings are correct, never come here. - Log.w(TAG, "Cannot load auto correction threshold setting." - + " currentAutoCorrectionSetting: " + currentAutoCorrectionSetting - + ", autoCorrectionThresholdValues: " - + Arrays.toString(autoCorrectionThresholdValues), e); - return Float.MAX_VALUE; - } - return autoCorrectionThreshold; - } - - private static boolean needsToShowVoiceInputKey(final SharedPreferences prefs, - final Resources res) { - // Migrate preference from {@link Settings#PREF_VOICE_MODE_OBSOLETE} to - // {@link Settings#PREF_VOICE_INPUT_KEY}. - if (prefs.contains(Settings.PREF_VOICE_MODE_OBSOLETE)) { - final String voiceModeMain = res.getString(R.string.voice_mode_main); - final String voiceMode = prefs.getString( - Settings.PREF_VOICE_MODE_OBSOLETE, voiceModeMain); - final boolean shouldShowVoiceInputKey = voiceModeMain.equals(voiceMode); - prefs.edit() - .putBoolean(Settings.PREF_VOICE_INPUT_KEY, shouldShowVoiceInputKey) - // Remove the obsolete preference if exists. - .remove(Settings.PREF_VOICE_MODE_OBSOLETE) - .apply(); - } - return prefs.getBoolean(Settings.PREF_VOICE_INPUT_KEY, true); - } - - public String dump() { - final StringBuilder sb = new StringBuilder("Current settings :"); - sb.append("\n mSpacingAndPunctuations = "); - sb.append("" + mSpacingAndPunctuations.dump()); - sb.append("\n mDelayInMillisecondsToUpdateOldSuggestions = "); - sb.append("" + mDelayInMillisecondsToUpdateOldSuggestions); - sb.append("\n mAutoCap = "); - sb.append("" + mAutoCap); - sb.append("\n mVibrateOn = "); - sb.append("" + mVibrateOn); - sb.append("\n mSoundOn = "); - sb.append("" + mSoundOn); - sb.append("\n mKeyPreviewPopupOn = "); - sb.append("" + mKeyPreviewPopupOn); - sb.append("\n mShowsVoiceInputKey = "); - sb.append("" + mShowsVoiceInputKey); - sb.append("\n mIncludesOtherImesInLanguageSwitchList = "); - sb.append("" + mIncludesOtherImesInLanguageSwitchList); - sb.append("\n mShowsLanguageSwitchKey = "); - sb.append("" + mShowsLanguageSwitchKey); - sb.append("\n mUseContactsDict = "); - sb.append("" + mUseContactsDict); - sb.append("\n mUsePersonalizedDicts = "); - sb.append("" + mUsePersonalizedDicts); - sb.append("\n mUseDoubleSpacePeriod = "); - sb.append("" + mUseDoubleSpacePeriod); - sb.append("\n mBlockPotentiallyOffensive = "); - sb.append("" + mBlockPotentiallyOffensive); - sb.append("\n mBigramPredictionEnabled = "); - sb.append("" + mBigramPredictionEnabled); - sb.append("\n mGestureInputEnabled = "); - sb.append("" + mGestureInputEnabled); - sb.append("\n mGestureTrailEnabled = "); - sb.append("" + mGestureTrailEnabled); - sb.append("\n mGestureFloatingPreviewTextEnabled = "); - sb.append("" + mGestureFloatingPreviewTextEnabled); - sb.append("\n mSlidingKeyInputPreviewEnabled = "); - sb.append("" + mSlidingKeyInputPreviewEnabled); - sb.append("\n mKeyLongpressTimeout = "); - sb.append("" + mKeyLongpressTimeout); - sb.append("\n mLocale = "); - sb.append("" + mLocale); - sb.append("\n mInputAttributes = "); - sb.append("" + mInputAttributes); - sb.append("\n mKeypressVibrationDuration = "); - sb.append("" + mKeypressVibrationDuration); - sb.append("\n mKeypressSoundVolume = "); - sb.append("" + mKeypressSoundVolume); - sb.append("\n mKeyPreviewPopupDismissDelay = "); - sb.append("" + mKeyPreviewPopupDismissDelay); - sb.append("\n mAutoCorrectEnabled = "); - sb.append("" + mAutoCorrectEnabled); - sb.append("\n mAutoCorrectionThreshold = "); - sb.append("" + mAutoCorrectionThreshold); - sb.append("\n mAutoCorrectionEnabledPerUserSettings = "); - sb.append("" + mAutoCorrectionEnabledPerUserSettings); - sb.append("\n mSuggestionsEnabledPerUserSettings = "); - sb.append("" + mSuggestionsEnabledPerUserSettings); - sb.append("\n mDisplayOrientation = "); - sb.append("" + mDisplayOrientation); - sb.append("\n mAppWorkarounds = "); - final AppWorkaroundsUtils awu = mAppWorkarounds.get(null, 0); - sb.append("" + (null == awu ? "null" : awu.toString())); - sb.append("\n mIsInternal = "); - sb.append("" + mIsInternal); - sb.append("\n mKeyPreviewShowUpDuration = "); - sb.append("" + mKeyPreviewShowUpDuration); - sb.append("\n mKeyPreviewDismissDuration = "); - sb.append("" + mKeyPreviewDismissDuration); - sb.append("\n mKeyPreviewShowUpStartScaleX = "); - sb.append("" + mKeyPreviewShowUpStartXScale); - sb.append("\n mKeyPreviewShowUpStartScaleY = "); - sb.append("" + mKeyPreviewShowUpStartYScale); - sb.append("\n mKeyPreviewDismissEndScaleX = "); - sb.append("" + mKeyPreviewDismissEndXScale); - sb.append("\n mKeyPreviewDismissEndScaleY = "); - sb.append("" + mKeyPreviewDismissEndYScale); - return sb.toString(); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsValuesForSuggestion.java b/java/src/com/android/inputmethod/latin/settings/SettingsValuesForSuggestion.java deleted file mode 100644 index 5e2e5a5d6..000000000 --- a/java/src/com/android/inputmethod/latin/settings/SettingsValuesForSuggestion.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -public class SettingsValuesForSuggestion { - public final boolean mBlockPotentiallyOffensive; - - public SettingsValuesForSuggestion(final boolean blockPotentiallyOffensive) { - mBlockPotentiallyOffensive = blockPotentiallyOffensive; - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java b/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java deleted file mode 100644 index 70d97a5ba..000000000 --- a/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.content.res.Resources; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.keyboard.internal.MoreKeySpec; -import com.android.inputmethod.latin.PunctuationSuggestions; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.StringUtils; - -import java.util.Arrays; -import java.util.Locale; - -public final class SpacingAndPunctuations { - private final int[] mSortedSymbolsPrecededBySpace; - private final int[] mSortedSymbolsFollowedBySpace; - private final int[] mSortedSymbolsClusteringTogether; - private final int[] mSortedWordConnectors; - public final int[] mSortedWordSeparators; - public final PunctuationSuggestions mSuggestPuncList; - private final int mSentenceSeparator; - private final int mAbbreviationMarker; - private final int[] mSortedSentenceTerminators; - public final String mSentenceSeparatorAndSpace; - public final boolean mCurrentLanguageHasSpaces; - public final boolean mUsesAmericanTypography; - public final boolean mUsesGermanRules; - - public SpacingAndPunctuations(final Resources res) { - // To be able to binary search the code point. See {@link #isUsuallyPrecededBySpace(int)}. - mSortedSymbolsPrecededBySpace = StringUtils.toSortedCodePointArray( - res.getString(R.string.symbols_preceded_by_space)); - // To be able to binary search the code point. See {@link #isUsuallyFollowedBySpace(int)}. - mSortedSymbolsFollowedBySpace = StringUtils.toSortedCodePointArray( - res.getString(R.string.symbols_followed_by_space)); - mSortedSymbolsClusteringTogether = StringUtils.toSortedCodePointArray( - res.getString(R.string.symbols_clustering_together)); - // To be able to binary search the code point. See {@link #isWordConnector(int)}. - mSortedWordConnectors = StringUtils.toSortedCodePointArray( - res.getString(R.string.symbols_word_connectors)); - mSortedWordSeparators = StringUtils.toSortedCodePointArray( - res.getString(R.string.symbols_word_separators)); - mSortedSentenceTerminators = StringUtils.toSortedCodePointArray( - res.getString(R.string.symbols_sentence_terminators)); - mSentenceSeparator = res.getInteger(R.integer.sentence_separator); - mAbbreviationMarker = res.getInteger(R.integer.abbreviation_marker); - mSentenceSeparatorAndSpace = new String(new int[] { - mSentenceSeparator, Constants.CODE_SPACE }, 0, 2); - mCurrentLanguageHasSpaces = res.getBoolean(R.bool.current_language_has_spaces); - final Locale locale = res.getConfiguration().locale; - // Heuristic: we use American Typography rules because it's the most common rules for all - // English variants. German rules (not "German typography") also have small gotchas. - mUsesAmericanTypography = Locale.ENGLISH.getLanguage().equals(locale.getLanguage()); - mUsesGermanRules = Locale.GERMAN.getLanguage().equals(locale.getLanguage()); - final String[] suggestPuncsSpec = MoreKeySpec.splitKeySpecs( - res.getString(R.string.suggested_punctuations)); - mSuggestPuncList = PunctuationSuggestions.newPunctuationSuggestions(suggestPuncsSpec); - } - - @UsedForTesting - public SpacingAndPunctuations(final SpacingAndPunctuations model, - final int[] overrideSortedWordSeparators) { - mSortedSymbolsPrecededBySpace = model.mSortedSymbolsPrecededBySpace; - mSortedSymbolsFollowedBySpace = model.mSortedSymbolsFollowedBySpace; - mSortedSymbolsClusteringTogether = model.mSortedSymbolsClusteringTogether; - mSortedWordConnectors = model.mSortedWordConnectors; - mSortedWordSeparators = overrideSortedWordSeparators; - mSortedSentenceTerminators = model.mSortedSentenceTerminators; - mSuggestPuncList = model.mSuggestPuncList; - mSentenceSeparator = model.mSentenceSeparator; - mAbbreviationMarker = model.mAbbreviationMarker; - mSentenceSeparatorAndSpace = model.mSentenceSeparatorAndSpace; - mCurrentLanguageHasSpaces = model.mCurrentLanguageHasSpaces; - mUsesAmericanTypography = model.mUsesAmericanTypography; - mUsesGermanRules = model.mUsesGermanRules; - } - - public boolean isWordSeparator(final int code) { - return Arrays.binarySearch(mSortedWordSeparators, code) >= 0; - } - - public boolean isWordConnector(final int code) { - return Arrays.binarySearch(mSortedWordConnectors, code) >= 0; - } - - public boolean isWordCodePoint(final int code) { - return Character.isLetter(code) || isWordConnector(code); - } - - public boolean isUsuallyPrecededBySpace(final int code) { - return Arrays.binarySearch(mSortedSymbolsPrecededBySpace, code) >= 0; - } - - public boolean isUsuallyFollowedBySpace(final int code) { - return Arrays.binarySearch(mSortedSymbolsFollowedBySpace, code) >= 0; - } - - public boolean isClusteringSymbol(final int code) { - return Arrays.binarySearch(mSortedSymbolsClusteringTogether, code) >= 0; - } - - public boolean isSentenceTerminator(final int code) { - return Arrays.binarySearch(mSortedSentenceTerminators, code) >= 0; - } - - public boolean isAbbreviationMarker(final int code) { - return code == mAbbreviationMarker; - } - - public boolean isSentenceSeparator(final int code) { - return code == mSentenceSeparator; - } - - public String dump() { - final StringBuilder sb = new StringBuilder(); - sb.append("mSortedSymbolsPrecededBySpace = "); - sb.append("" + Arrays.toString(mSortedSymbolsPrecededBySpace)); - sb.append("\n mSortedSymbolsFollowedBySpace = "); - sb.append("" + Arrays.toString(mSortedSymbolsFollowedBySpace)); - sb.append("\n mSortedWordConnectors = "); - sb.append("" + Arrays.toString(mSortedWordConnectors)); - sb.append("\n mSortedWordSeparators = "); - sb.append("" + Arrays.toString(mSortedWordSeparators)); - sb.append("\n mSuggestPuncList = "); - sb.append("" + mSuggestPuncList); - sb.append("\n mSentenceSeparator = "); - sb.append("" + mSentenceSeparator); - sb.append("\n mSentenceSeparatorAndSpace = "); - sb.append("" + mSentenceSeparatorAndSpace); - sb.append("\n mCurrentLanguageHasSpaces = "); - sb.append("" + mCurrentLanguageHasSpaces); - sb.append("\n mUsesAmericanTypography = "); - sb.append("" + mUsesAmericanTypography); - sb.append("\n mUsesGermanRules = "); - sb.append("" + mUsesGermanRules); - return sb.toString(); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/SubScreenFragment.java b/java/src/com/android/inputmethod/latin/settings/SubScreenFragment.java deleted file mode 100644 index 5994a76df..000000000 --- a/java/src/com/android/inputmethod/latin/settings/SubScreenFragment.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.app.backup.BackupManager; -import android.content.Context; -import android.content.SharedPreferences; -import android.content.SharedPreferences.OnSharedPreferenceChangeListener; -import android.content.res.Resources; -import android.os.Bundle; -import android.preference.ListPreference; -import android.preference.Preference; -import android.preference.PreferenceFragment; -import android.preference.PreferenceScreen; -import android.util.Log; - -/** - * A base abstract class for a {@link PreferenceFragment} that implements a nested - * {@link PreferenceScreen} of the main preference screen. - */ -public abstract class SubScreenFragment extends PreferenceFragment - implements OnSharedPreferenceChangeListener { - private OnSharedPreferenceChangeListener mSharedPreferenceChangeListener; - - static void setPreferenceEnabled(final String prefKey, final boolean enabled, - final PreferenceScreen screen) { - final Preference preference = screen.findPreference(prefKey); - if (preference != null) { - preference.setEnabled(enabled); - } - } - - static void removePreference(final String prefKey, final PreferenceScreen screen) { - final Preference preference = screen.findPreference(prefKey); - if (preference != null) { - screen.removePreference(preference); - } - } - - static void updateListPreferenceSummaryToCurrentValue(final String prefKey, - final PreferenceScreen screen) { - // Because the "%s" summary trick of {@link ListPreference} doesn't work properly before - // KitKat, we need to update the summary programmatically. - final ListPreference listPreference = (ListPreference)screen.findPreference(prefKey); - if (listPreference == null) { - return; - } - final CharSequence entries[] = listPreference.getEntries(); - final int entryIndex = listPreference.findIndexOfValue(listPreference.getValue()); - listPreference.setSummary(entryIndex < 0 ? null : entries[entryIndex]); - } - - final void setPreferenceEnabled(final String prefKey, final boolean enabled) { - setPreferenceEnabled(prefKey, enabled, getPreferenceScreen()); - } - - final void removePreference(final String prefKey) { - removePreference(prefKey, getPreferenceScreen()); - } - - final void updateListPreferenceSummaryToCurrentValue(final String prefKey) { - updateListPreferenceSummaryToCurrentValue(prefKey, getPreferenceScreen()); - } - - final SharedPreferences getSharedPreferences() { - return getPreferenceManager().getSharedPreferences(); - } - - /** - * Gets the application name to display on the UI. - */ - final String getApplicationName() { - final Context context = getActivity(); - final Resources res = getResources(); - final int applicationLabelRes = context.getApplicationInfo().labelRes; - return res.getString(applicationLabelRes); - } - - @Override - public void addPreferencesFromResource(final int preferencesResId) { - super.addPreferencesFromResource(preferencesResId); - TwoStatePreferenceHelper.replaceCheckBoxPreferencesBySwitchPreferences( - getPreferenceScreen()); - } - - @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mSharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() { - @Override - public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { - final SubScreenFragment fragment = SubScreenFragment.this; - final Context context = fragment.getActivity(); - if (context == null || fragment.getPreferenceScreen() == null) { - final String tag = fragment.getClass().getSimpleName(); - // TODO: Introduce a static function to register this class and ensure that - // onCreate must be called before "onSharedPreferenceChanged" is called. - Log.w(tag, "onSharedPreferenceChanged called before activity starts."); - return; - } - new BackupManager(context).dataChanged(); - fragment.onSharedPreferenceChanged(prefs, key); - } - }; - getSharedPreferences().registerOnSharedPreferenceChangeListener( - mSharedPreferenceChangeListener); - } - - @Override - public void onDestroy() { - getSharedPreferences().unregisterOnSharedPreferenceChangeListener( - mSharedPreferenceChangeListener); - super.onDestroy(); - } - - @Override - public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { - // This method may be overridden by an extended class. - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/TestFragmentActivity.java b/java/src/com/android/inputmethod/latin/settings/TestFragmentActivity.java deleted file mode 100644 index 254bc6567..000000000 --- a/java/src/com/android/inputmethod/latin/settings/TestFragmentActivity.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.app.Activity; -import android.app.Fragment; -import android.app.FragmentManager; -import android.content.Intent; -import android.os.Bundle; - -/** - * Test activity to use when testing preference fragments. <br/> - * Usage: <br/> - * Create an ActivityInstrumentationTestCase2 for this activity - * and call setIntent() with an intent that specifies the fragment to load in the activity. - * The fragment can then be obtained from this activity and used for testing/verification. - */ -public final class TestFragmentActivity extends Activity { - /** - * The fragment name that should be loaded when starting this activity. - * This must be specified when starting this activity, as this activity is only - * meant to test fragments from instrumentation tests. - */ - public static final String EXTRA_SHOW_FRAGMENT = "show_fragment"; - - public Fragment mFragment; - - @Override - protected void onCreate(final Bundle savedState) { - super.onCreate(savedState); - final Intent intent = getIntent(); - final String fragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT); - if (fragmentName == null) { - throw new IllegalArgumentException("No fragment name specified for testing"); - } - - mFragment = Fragment.instantiate(this, fragmentName); - FragmentManager fragmentManager = getFragmentManager(); - fragmentManager.beginTransaction().add(mFragment, fragmentName).commit(); - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/ThemeSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/ThemeSettingsFragment.java deleted file mode 100644 index 29289aed2..000000000 --- a/java/src/com/android/inputmethod/latin/settings/ThemeSettingsFragment.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.content.Context; -import android.content.res.Resources; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.PreferenceScreen; - -import com.android.inputmethod.keyboard.KeyboardTheme; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.settings.RadioButtonPreference.OnRadioButtonClickedListener; - -/** - * "Keyboard theme" settings sub screen. - */ -public final class ThemeSettingsFragment extends SubScreenFragment - implements OnRadioButtonClickedListener { - private int mSelectedThemeId; - - static class KeyboardThemePreference extends RadioButtonPreference { - final int mThemeId; - - KeyboardThemePreference(final Context context, final String name, final int id) { - super(context); - setTitle(name); - mThemeId = id; - } - } - - static void updateKeyboardThemeSummary(final Preference pref) { - final Context context = pref.getContext(); - final Resources res = context.getResources(); - final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(context); - final String[] keyboardThemeNames = res.getStringArray(R.array.keyboard_theme_names); - final int[] keyboardThemeIds = res.getIntArray(R.array.keyboard_theme_ids); - for (int index = 0; index < keyboardThemeNames.length; index++) { - if (keyboardTheme.mThemeId == keyboardThemeIds[index]) { - pref.setSummary(keyboardThemeNames[index]); - return; - } - } - } - - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_theme); - final PreferenceScreen screen = getPreferenceScreen(); - final Context context = getActivity(); - final Resources res = getResources(); - final String[] keyboardThemeNames = res.getStringArray(R.array.keyboard_theme_names); - final int[] keyboardThemeIds = res.getIntArray(R.array.keyboard_theme_ids); - for (int index = 0; index < keyboardThemeNames.length; index++) { - final KeyboardThemePreference pref = new KeyboardThemePreference( - context, keyboardThemeNames[index], keyboardThemeIds[index]); - screen.addPreference(pref); - pref.setOnRadioButtonClickedListener(this); - } - final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(context); - mSelectedThemeId = keyboardTheme.mThemeId; - } - - @Override - public void onRadioButtonClicked(final RadioButtonPreference preference) { - if (preference instanceof KeyboardThemePreference) { - final KeyboardThemePreference pref = (KeyboardThemePreference)preference; - mSelectedThemeId = pref.mThemeId; - updateSelected(); - } - } - - @Override - public void onResume() { - super.onResume(); - updateSelected(); - } - - @Override - public void onPause() { - super.onPause(); - KeyboardTheme.saveKeyboardThemeId(mSelectedThemeId, getSharedPreferences()); - } - - private void updateSelected() { - final PreferenceScreen screen = getPreferenceScreen(); - final int count = screen.getPreferenceCount(); - for (int index = 0; index < count; index++) { - final Preference preference = screen.getPreference(index); - if (preference instanceof KeyboardThemePreference) { - final KeyboardThemePreference pref = (KeyboardThemePreference)preference; - final boolean selected = (mSelectedThemeId == pref.mThemeId); - pref.setSelected(selected); - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/settings/TwoStatePreferenceHelper.java b/java/src/com/android/inputmethod/latin/settings/TwoStatePreferenceHelper.java deleted file mode 100644 index 07a871ca0..000000000 --- a/java/src/com/android/inputmethod/latin/settings/TwoStatePreferenceHelper.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2014 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.settings; - -import android.os.Build; -import android.preference.CheckBoxPreference; -import android.preference.Preference; -import android.preference.PreferenceGroup; -import android.preference.SwitchPreference; - -import java.util.ArrayList; - -public class TwoStatePreferenceHelper { - private static final String EMPTY_TEXT = ""; - - private TwoStatePreferenceHelper() { - // This utility class is not publicly instantiable. - } - - public static void replaceCheckBoxPreferencesBySwitchPreferences(final PreferenceGroup group) { - // The keyboard settings keeps using a CheckBoxPreference on KitKat or previous. - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { - return; - } - // The keyboard settings starts using a SwitchPreference without switch on/off text on - // API versions newer than KitKat. - replaceAllCheckBoxPreferencesBySwitchPreferences(group); - } - - private static void replaceAllCheckBoxPreferencesBySwitchPreferences( - final PreferenceGroup group) { - final ArrayList<Preference> preferences = new ArrayList<>(); - final int count = group.getPreferenceCount(); - for (int index = 0; index < count; index++) { - preferences.add(group.getPreference(index)); - } - group.removeAll(); - for (int index = 0; index < count; index++) { - final Preference preference = preferences.get(index); - if (preference instanceof CheckBoxPreference) { - addSwitchPreferenceBasedOnCheckBoxPreference((CheckBoxPreference)preference, group); - } else { - group.addPreference(preference); - if (preference instanceof PreferenceGroup) { - replaceAllCheckBoxPreferencesBySwitchPreferences((PreferenceGroup)preference); - } - } - } - } - - static void addSwitchPreferenceBasedOnCheckBoxPreference(final CheckBoxPreference checkBox, - final PreferenceGroup group) { - final SwitchPreference switchPref = new SwitchPreference(checkBox.getContext()); - switchPref.setTitle(checkBox.getTitle()); - switchPref.setKey(checkBox.getKey()); - switchPref.setOrder(checkBox.getOrder()); - switchPref.setPersistent(checkBox.isPersistent()); - switchPref.setEnabled(checkBox.isEnabled()); - switchPref.setChecked(checkBox.isChecked()); - switchPref.setSummary(checkBox.getSummary()); - switchPref.setSummaryOn(checkBox.getSummaryOn()); - switchPref.setSummaryOff(checkBox.getSummaryOff()); - switchPref.setSwitchTextOn(EMPTY_TEXT); - switchPref.setSwitchTextOff(EMPTY_TEXT); - group.addPreference(switchPref); - switchPref.setDependency(checkBox.getDependency()); - } -} diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java deleted file mode 100644 index 7607429f8..000000000 --- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2013 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.setup; - -import android.app.Activity; -import android.content.Intent; -import android.os.Bundle; - -public final class SetupActivity extends Activity { - @Override - protected void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - final Intent intent = new Intent(); - intent.setClass(this, SetupWizardActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP - | Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); - if (!isFinishing()) { - finish(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java b/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java deleted file mode 100644 index 789694f08..000000000 --- a/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (C) 2013 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.setup; - -import android.content.Context; -import android.content.res.ColorStateList; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Path; -import androidx.core.view.ViewCompat; -import android.util.AttributeSet; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.LinearLayout; -import android.widget.TextView; - -import com.android.inputmethod.latin.R; - -public final class SetupStartIndicatorView extends LinearLayout { - public SetupStartIndicatorView(final Context context, final AttributeSet attrs) { - super(context, attrs); - setOrientation(HORIZONTAL); - LayoutInflater.from(context).inflate(R.layout.setup_start_indicator_label, this); - - final LabelView labelView = (LabelView)findViewById(R.id.setup_start_label); - labelView.setIndicatorView(findViewById(R.id.setup_start_indicator)); - } - - public static final class LabelView extends TextView { - private View mIndicatorView; - - public LabelView(final Context context, final AttributeSet attrs) { - super(context, attrs); - } - - public void setIndicatorView(final View indicatorView) { - mIndicatorView = indicatorView; - } - - // TODO: Once we stop supporting ICS, uncomment {@link #setPressed(boolean)} method and - // remove this method. - @Override - protected void drawableStateChanged() { - super.drawableStateChanged(); - for (final int state : getDrawableState()) { - if (state == android.R.attr.state_pressed) { - updateIndicatorView(true /* pressed */); - return; - } - } - updateIndicatorView(false /* pressed */); - } - - // TODO: Once we stop supporting ICS, uncomment this method and remove - // {@link #drawableStateChanged()} method. -// @Override -// public void setPressed(final boolean pressed) { -// super.setPressed(pressed); -// updateIndicatorView(pressed); -// } - - private void updateIndicatorView(final boolean pressed) { - if (mIndicatorView != null) { - mIndicatorView.setPressed(pressed); - mIndicatorView.invalidate(); - } - } - } - - public static final class IndicatorView extends View { - private final Path mIndicatorPath = new Path(); - private final Paint mIndicatorPaint = new Paint(); - private final ColorStateList mIndicatorColor; - - public IndicatorView(final Context context, final AttributeSet attrs) { - super(context, attrs); - mIndicatorColor = getResources().getColorStateList( - R.color.setup_step_action_background); - mIndicatorPaint.setStyle(Paint.Style.FILL); - } - - @Override - protected void onDraw(final Canvas canvas) { - super.onDraw(canvas); - final int layoutDirection = ViewCompat.getLayoutDirection(this); - final int width = getWidth(); - final int height = getHeight(); - final float halfHeight = height / 2.0f; - final Path path = mIndicatorPath; - path.rewind(); - if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) { - // Left arrow - path.moveTo(width, 0.0f); - path.lineTo(0.0f, halfHeight); - path.lineTo(width, height); - } else { // LAYOUT_DIRECTION_LTR - // Right arrow - path.moveTo(0.0f, 0.0f); - path.lineTo(width, halfHeight); - path.lineTo(0.0f, height); - } - path.close(); - final int[] stateSet = getDrawableState(); - final int color = mIndicatorColor.getColorForState(stateSet, 0); - mIndicatorPaint.setColor(color); - canvas.drawPath(path, mIndicatorPaint); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java b/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java deleted file mode 100644 index 9a39ceace..000000000 --- a/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2013 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.setup; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Path; -import androidx.core.view.ViewCompat; -import android.util.AttributeSet; -import android.view.View; - -import com.android.inputmethod.latin.R; - -public final class SetupStepIndicatorView extends View { - private final Path mIndicatorPath = new Path(); - private final Paint mIndicatorPaint = new Paint(); - private float mXRatio; - - public SetupStepIndicatorView(final Context context, final AttributeSet attrs) { - super(context, attrs); - mIndicatorPaint.setColor(getResources().getColor(R.color.setup_step_background)); - mIndicatorPaint.setStyle(Paint.Style.FILL); - } - - public void setIndicatorPosition(final int stepPos, final int totalStepNum) { - final int layoutDirection = ViewCompat.getLayoutDirection(this); - // The indicator position is the center of the partition that is equally divided into - // the total step number. - final float partionWidth = 1.0f / totalStepNum; - final float pos = stepPos * partionWidth + partionWidth / 2.0f; - mXRatio = (layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) ? 1.0f - pos : pos; - invalidate(); - } - - @Override - protected void onDraw(final Canvas canvas) { - super.onDraw(canvas); - final int xPos = (int)(getWidth() * mXRatio); - final int height = getHeight(); - mIndicatorPath.rewind(); - mIndicatorPath.moveTo(xPos, 0); - mIndicatorPath.lineTo(xPos + height, height); - mIndicatorPath.lineTo(xPos - height, height); - mIndicatorPath.close(); - canvas.drawPath(mIndicatorPath, mIndicatorPaint); - } -} diff --git a/java/src/com/android/inputmethod/latin/setup/SetupWizardActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupWizardActivity.java deleted file mode 100644 index bee22afd5..000000000 --- a/java/src/com/android/inputmethod/latin/setup/SetupWizardActivity.java +++ /dev/null @@ -1,513 +0,0 @@ -/* - * Copyright (C) 2013 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.setup; - -import android.app.Activity; -import android.content.ContentResolver; -import android.content.Intent; -import android.content.res.Resources; -import android.media.MediaPlayer; -import android.net.Uri; -import android.os.Bundle; -import android.os.Message; -import android.provider.Settings; -import android.util.Log; -import android.view.View; -import android.view.inputmethod.InputMethodInfo; -import android.view.inputmethod.InputMethodManager; -import android.widget.ImageView; -import android.widget.TextView; -import android.widget.VideoView; - -import com.android.inputmethod.compat.TextViewCompatUtils; -import com.android.inputmethod.compat.ViewCompatUtils; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.settings.SettingsActivity; -import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper; -import com.android.inputmethod.latin.utils.UncachedInputMethodManagerUtils; - -import java.util.ArrayList; - -import javax.annotation.Nonnull; - -// TODO: Use Fragment to implement welcome screen and setup steps. -public final class SetupWizardActivity extends Activity implements View.OnClickListener { - static final String TAG = SetupWizardActivity.class.getSimpleName(); - - // For debugging purpose. - private static final boolean FORCE_TO_SHOW_WELCOME_SCREEN = false; - private static final boolean ENABLE_WELCOME_VIDEO = true; - - private InputMethodManager mImm; - - private View mSetupWizard; - private View mWelcomeScreen; - private View mSetupScreen; - private Uri mWelcomeVideoUri; - private VideoView mWelcomeVideoView; - private ImageView mWelcomeImageView; - private View mActionStart; - private View mActionNext; - private TextView mStep1Bullet; - private TextView mActionFinish; - private SetupStepGroup mSetupStepGroup; - private static final String STATE_STEP = "step"; - private int mStepNumber; - private boolean mNeedsToAdjustStepNumberToSystemState; - private static final int STEP_WELCOME = 0; - private static final int STEP_1 = 1; - private static final int STEP_2 = 2; - private static final int STEP_3 = 3; - private static final int STEP_LAUNCHING_IME_SETTINGS = 4; - private static final int STEP_BACK_FROM_IME_SETTINGS = 5; - - private SettingsPoolingHandler mHandler; - - private static final class SettingsPoolingHandler - extends LeakGuardHandlerWrapper<SetupWizardActivity> { - private static final int MSG_POLLING_IME_SETTINGS = 0; - private static final long IME_SETTINGS_POLLING_INTERVAL = 200; - - private final InputMethodManager mImmInHandler; - - public SettingsPoolingHandler(@Nonnull final SetupWizardActivity ownerInstance, - final InputMethodManager imm) { - super(ownerInstance); - mImmInHandler = imm; - } - - @Override - public void handleMessage(final Message msg) { - final SetupWizardActivity setupWizardActivity = getOwnerInstance(); - if (setupWizardActivity == null) { - return; - } - switch (msg.what) { - case MSG_POLLING_IME_SETTINGS: - if (UncachedInputMethodManagerUtils.isThisImeEnabled(setupWizardActivity, - mImmInHandler)) { - setupWizardActivity.invokeSetupWizardOfThisIme(); - return; - } - startPollingImeSettings(); - break; - } - } - - public void startPollingImeSettings() { - sendMessageDelayed(obtainMessage(MSG_POLLING_IME_SETTINGS), - IME_SETTINGS_POLLING_INTERVAL); - } - - public void cancelPollingImeSettings() { - removeMessages(MSG_POLLING_IME_SETTINGS); - } - } - - @Override - protected void onCreate(final Bundle savedInstanceState) { - setTheme(android.R.style.Theme_Translucent_NoTitleBar); - super.onCreate(savedInstanceState); - - mImm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); - mHandler = new SettingsPoolingHandler(this, mImm); - - setContentView(R.layout.setup_wizard); - mSetupWizard = findViewById(R.id.setup_wizard); - - if (savedInstanceState == null) { - mStepNumber = determineSetupStepNumberFromLauncher(); - } else { - mStepNumber = savedInstanceState.getInt(STATE_STEP); - } - - final String applicationName = getResources().getString(getApplicationInfo().labelRes); - mWelcomeScreen = findViewById(R.id.setup_welcome_screen); - final TextView welcomeTitle = (TextView)findViewById(R.id.setup_welcome_title); - welcomeTitle.setText(getString(R.string.setup_welcome_title, applicationName)); - - mSetupScreen = findViewById(R.id.setup_steps_screen); - final TextView stepsTitle = (TextView)findViewById(R.id.setup_title); - stepsTitle.setText(getString(R.string.setup_steps_title, applicationName)); - - final SetupStepIndicatorView indicatorView = - (SetupStepIndicatorView)findViewById(R.id.setup_step_indicator); - mSetupStepGroup = new SetupStepGroup(indicatorView); - - mStep1Bullet = (TextView)findViewById(R.id.setup_step1_bullet); - mStep1Bullet.setOnClickListener(this); - final SetupStep step1 = new SetupStep(STEP_1, applicationName, - mStep1Bullet, findViewById(R.id.setup_step1), - R.string.setup_step1_title, R.string.setup_step1_instruction, - R.string.setup_step1_finished_instruction, R.drawable.ic_setup_step1, - R.string.setup_step1_action); - final SettingsPoolingHandler handler = mHandler; - step1.setAction(new Runnable() { - @Override - public void run() { - invokeLanguageAndInputSettings(); - handler.startPollingImeSettings(); - } - }); - mSetupStepGroup.addStep(step1); - - final SetupStep step2 = new SetupStep(STEP_2, applicationName, - (TextView)findViewById(R.id.setup_step2_bullet), findViewById(R.id.setup_step2), - R.string.setup_step2_title, R.string.setup_step2_instruction, - 0 /* finishedInstruction */, R.drawable.ic_setup_step2, - R.string.setup_step2_action); - step2.setAction(new Runnable() { - @Override - public void run() { - invokeInputMethodPicker(); - } - }); - mSetupStepGroup.addStep(step2); - - final SetupStep step3 = new SetupStep(STEP_3, applicationName, - (TextView)findViewById(R.id.setup_step3_bullet), findViewById(R.id.setup_step3), - R.string.setup_step3_title, R.string.setup_step3_instruction, - 0 /* finishedInstruction */, R.drawable.ic_setup_step3, - R.string.setup_step3_action); - step3.setAction(new Runnable() { - @Override - public void run() { - invokeSubtypeEnablerOfThisIme(); - } - }); - mSetupStepGroup.addStep(step3); - - mWelcomeVideoUri = new Uri.Builder() - .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) - .authority(getPackageName()) - .path(Integer.toString(R.raw.setup_welcome_video)) - .build(); - final VideoView welcomeVideoView = (VideoView)findViewById(R.id.setup_welcome_video); - welcomeVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { - @Override - public void onPrepared(final MediaPlayer mp) { - // Now VideoView has been laid-out and ready to play, remove background of it to - // reveal the video. - welcomeVideoView.setBackgroundResource(0); - mp.setLooping(true); - } - }); - welcomeVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { - @Override - public boolean onError(final MediaPlayer mp, final int what, final int extra) { - Log.e(TAG, "Playing welcome video causes error: what=" + what + " extra=" + extra); - hideWelcomeVideoAndShowWelcomeImage(); - return true; - } - }); - mWelcomeVideoView = welcomeVideoView; - mWelcomeImageView = (ImageView)findViewById(R.id.setup_welcome_image); - - mActionStart = findViewById(R.id.setup_start_label); - mActionStart.setOnClickListener(this); - mActionNext = findViewById(R.id.setup_next); - mActionNext.setOnClickListener(this); - mActionFinish = (TextView)findViewById(R.id.setup_finish); - TextViewCompatUtils.setCompoundDrawablesRelativeWithIntrinsicBounds(mActionFinish, - getResources().getDrawable(R.drawable.ic_setup_finish), null, null, null); - mActionFinish.setOnClickListener(this); - } - - @Override - public void onClick(final View v) { - if (v == mActionFinish) { - finish(); - return; - } - final int currentStep = determineSetupStepNumber(); - final int nextStep; - if (v == mActionStart) { - nextStep = STEP_1; - } else if (v == mActionNext) { - nextStep = mStepNumber + 1; - } else if (v == mStep1Bullet && currentStep == STEP_2) { - nextStep = STEP_1; - } else { - nextStep = mStepNumber; - } - if (mStepNumber != nextStep) { - mStepNumber = nextStep; - updateSetupStepView(); - } - } - - void invokeSetupWizardOfThisIme() { - final Intent intent = new Intent(); - intent.setClass(this, SetupWizardActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - | Intent.FLAG_ACTIVITY_SINGLE_TOP - | Intent.FLAG_ACTIVITY_CLEAR_TOP); - startActivity(intent); - mNeedsToAdjustStepNumberToSystemState = true; - } - - private void invokeSettingsOfThisIme() { - final Intent intent = new Intent(); - intent.setClass(this, SettingsActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - | Intent.FLAG_ACTIVITY_CLEAR_TOP); - intent.putExtra(SettingsActivity.EXTRA_ENTRY_KEY, - SettingsActivity.EXTRA_ENTRY_VALUE_APP_ICON); - startActivity(intent); - } - - void invokeLanguageAndInputSettings() { - final Intent intent = new Intent(); - intent.setAction(Settings.ACTION_INPUT_METHOD_SETTINGS); - intent.addCategory(Intent.CATEGORY_DEFAULT); - startActivity(intent); - mNeedsToAdjustStepNumberToSystemState = true; - } - - void invokeInputMethodPicker() { - // Invoke input method picker. - mImm.showInputMethodPicker(); - mNeedsToAdjustStepNumberToSystemState = true; - } - - void invokeSubtypeEnablerOfThisIme() { - final InputMethodInfo imi = - UncachedInputMethodManagerUtils.getInputMethodInfoOf(getPackageName(), mImm); - if (imi == null) { - return; - } - final Intent intent = new Intent(); - intent.setAction(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); - intent.addCategory(Intent.CATEGORY_DEFAULT); - intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, imi.getId()); - startActivity(intent); - } - - private int determineSetupStepNumberFromLauncher() { - final int stepNumber = determineSetupStepNumber(); - if (stepNumber == STEP_1) { - return STEP_WELCOME; - } - if (stepNumber == STEP_3) { - return STEP_LAUNCHING_IME_SETTINGS; - } - return stepNumber; - } - - private int determineSetupStepNumber() { - mHandler.cancelPollingImeSettings(); - if (FORCE_TO_SHOW_WELCOME_SCREEN) { - return STEP_1; - } - if (!UncachedInputMethodManagerUtils.isThisImeEnabled(this, mImm)) { - return STEP_1; - } - if (!UncachedInputMethodManagerUtils.isThisImeCurrent(this, mImm)) { - return STEP_2; - } - return STEP_3; - } - - @Override - protected void onSaveInstanceState(final Bundle outState) { - super.onSaveInstanceState(outState); - outState.putInt(STATE_STEP, mStepNumber); - } - - @Override - protected void onRestoreInstanceState(final Bundle savedInstanceState) { - super.onRestoreInstanceState(savedInstanceState); - mStepNumber = savedInstanceState.getInt(STATE_STEP); - } - - private static boolean isInSetupSteps(final int stepNumber) { - return stepNumber >= STEP_1 && stepNumber <= STEP_3; - } - - @Override - protected void onRestart() { - super.onRestart(); - // Probably the setup wizard has been invoked from "Recent" menu. The setup step number - // needs to be adjusted to system state, because the state (IME is enabled and/or current) - // may have been changed. - if (isInSetupSteps(mStepNumber)) { - mStepNumber = determineSetupStepNumber(); - } - } - - @Override - protected void onResume() { - super.onResume(); - if (mStepNumber == STEP_LAUNCHING_IME_SETTINGS) { - // Prevent white screen flashing while launching settings activity. - mSetupWizard.setVisibility(View.INVISIBLE); - invokeSettingsOfThisIme(); - mStepNumber = STEP_BACK_FROM_IME_SETTINGS; - return; - } - if (mStepNumber == STEP_BACK_FROM_IME_SETTINGS) { - finish(); - return; - } - updateSetupStepView(); - } - - @Override - public void onBackPressed() { - if (mStepNumber == STEP_1) { - mStepNumber = STEP_WELCOME; - updateSetupStepView(); - return; - } - super.onBackPressed(); - } - - void hideWelcomeVideoAndShowWelcomeImage() { - mWelcomeVideoView.setVisibility(View.GONE); - mWelcomeImageView.setImageResource(R.raw.setup_welcome_image); - mWelcomeImageView.setVisibility(View.VISIBLE); - } - - private void showAndStartWelcomeVideo() { - mWelcomeVideoView.setVisibility(View.VISIBLE); - mWelcomeVideoView.setVideoURI(mWelcomeVideoUri); - mWelcomeVideoView.start(); - } - - private void hideAndStopWelcomeVideo() { - mWelcomeVideoView.stopPlayback(); - mWelcomeVideoView.setVisibility(View.GONE); - } - - @Override - protected void onPause() { - hideAndStopWelcomeVideo(); - super.onPause(); - } - - @Override - public void onWindowFocusChanged(final boolean hasFocus) { - super.onWindowFocusChanged(hasFocus); - if (hasFocus && mNeedsToAdjustStepNumberToSystemState) { - mNeedsToAdjustStepNumberToSystemState = false; - mStepNumber = determineSetupStepNumber(); - updateSetupStepView(); - } - } - - private void updateSetupStepView() { - mSetupWizard.setVisibility(View.VISIBLE); - final boolean welcomeScreen = (mStepNumber == STEP_WELCOME); - mWelcomeScreen.setVisibility(welcomeScreen ? View.VISIBLE : View.GONE); - mSetupScreen.setVisibility(welcomeScreen ? View.GONE : View.VISIBLE); - if (welcomeScreen) { - if (ENABLE_WELCOME_VIDEO) { - showAndStartWelcomeVideo(); - } else { - hideWelcomeVideoAndShowWelcomeImage(); - } - return; - } - hideAndStopWelcomeVideo(); - final boolean isStepActionAlreadyDone = mStepNumber < determineSetupStepNumber(); - mSetupStepGroup.enableStep(mStepNumber, isStepActionAlreadyDone); - mActionNext.setVisibility(isStepActionAlreadyDone ? View.VISIBLE : View.GONE); - mActionFinish.setVisibility((mStepNumber == STEP_3) ? View.VISIBLE : View.GONE); - } - - static final class SetupStep implements View.OnClickListener { - public final int mStepNo; - private final View mStepView; - private final TextView mBulletView; - private final int mActivatedColor; - private final int mDeactivatedColor; - private final String mInstruction; - private final String mFinishedInstruction; - private final TextView mActionLabel; - private Runnable mAction; - - public SetupStep(final int stepNo, final String applicationName, final TextView bulletView, - final View stepView, final int title, final int instruction, - final int finishedInstruction, final int actionIcon, final int actionLabel) { - mStepNo = stepNo; - mStepView = stepView; - mBulletView = bulletView; - final Resources res = stepView.getResources(); - mActivatedColor = res.getColor(R.color.setup_text_action); - mDeactivatedColor = res.getColor(R.color.setup_text_dark); - - final TextView titleView = (TextView)mStepView.findViewById(R.id.setup_step_title); - titleView.setText(res.getString(title, applicationName)); - mInstruction = (instruction == 0) ? null - : res.getString(instruction, applicationName); - mFinishedInstruction = (finishedInstruction == 0) ? null - : res.getString(finishedInstruction, applicationName); - - mActionLabel = (TextView)mStepView.findViewById(R.id.setup_step_action_label); - mActionLabel.setText(res.getString(actionLabel)); - if (actionIcon == 0) { - final int paddingEnd = ViewCompatUtils.getPaddingEnd(mActionLabel); - ViewCompatUtils.setPaddingRelative(mActionLabel, paddingEnd, 0, paddingEnd, 0); - } else { - TextViewCompatUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( - mActionLabel, res.getDrawable(actionIcon), null, null, null); - } - } - - public void setEnabled(final boolean enabled, final boolean isStepActionAlreadyDone) { - mStepView.setVisibility(enabled ? View.VISIBLE : View.GONE); - mBulletView.setTextColor(enabled ? mActivatedColor : mDeactivatedColor); - final TextView instructionView = (TextView)mStepView.findViewById( - R.id.setup_step_instruction); - instructionView.setText(isStepActionAlreadyDone ? mFinishedInstruction : mInstruction); - mActionLabel.setVisibility(isStepActionAlreadyDone ? View.GONE : View.VISIBLE); - } - - public void setAction(final Runnable action) { - mActionLabel.setOnClickListener(this); - mAction = action; - } - - @Override - public void onClick(final View v) { - if (v == mActionLabel && mAction != null) { - mAction.run(); - return; - } - } - } - - static final class SetupStepGroup { - private final SetupStepIndicatorView mIndicatorView; - private final ArrayList<SetupStep> mGroup = new ArrayList<>(); - - public SetupStepGroup(final SetupStepIndicatorView indicatorView) { - mIndicatorView = indicatorView; - } - - public void addStep(final SetupStep step) { - mGroup.add(step); - } - - public void enableStep(final int enableStepNo, final boolean isStepActionAlreadyDone) { - for (final SetupStep step : mGroup) { - step.setEnabled(step.mStepNo == enableStepNo, isStepActionAlreadyDone); - } - mIndicatorView.setIndicatorPosition(enableStepNo - STEP_1, mGroup.size()); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java deleted file mode 100644 index 4625e8e8b..000000000 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.spellcheck; - -import android.content.Intent; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.service.textservice.SpellCheckerService; -import android.text.InputType; -import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputMethodSubtype; -import android.view.textservice.SuggestionsInfo; - -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.KeyboardId; -import com.android.inputmethod.keyboard.KeyboardLayoutSet; -import com.android.inputmethod.latin.DictionaryFacilitator; -import com.android.inputmethod.latin.DictionaryFacilitatorLruCache; -import com.android.inputmethod.latin.NgramContext; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.RichInputMethodSubtype; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.common.ComposedData; -import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion; -import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; -import com.android.inputmethod.latin.utils.ScriptUtils; -import com.android.inputmethod.latin.utils.SuggestionResults; - -import java.util.Locale; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.Semaphore; - -import javax.annotation.Nonnull; - -/** - * Service for spell checking, using LatinIME's dictionaries and mechanisms. - */ -public final class AndroidSpellCheckerService extends SpellCheckerService - implements SharedPreferences.OnSharedPreferenceChangeListener { - private static final String TAG = AndroidSpellCheckerService.class.getSimpleName(); - private static final boolean DEBUG = false; - - public static final String PREF_USE_CONTACTS_KEY = "pref_spellcheck_use_contacts"; - - private static final int SPELLCHECKER_DUMMY_KEYBOARD_WIDTH = 480; - private static final int SPELLCHECKER_DUMMY_KEYBOARD_HEIGHT = 301; - - private static final String DICTIONARY_NAME_PREFIX = "spellcheck_"; - - private static final String[] EMPTY_STRING_ARRAY = new String[0]; - - private final int MAX_NUM_OF_THREADS_READ_DICTIONARY = 2; - private final Semaphore mSemaphore = new Semaphore(MAX_NUM_OF_THREADS_READ_DICTIONARY, - true /* fair */); - // TODO: Make each spell checker session has its own session id. - private final ConcurrentLinkedQueue<Integer> mSessionIdPool = new ConcurrentLinkedQueue<>(); - - private final DictionaryFacilitatorLruCache mDictionaryFacilitatorCache = - new DictionaryFacilitatorLruCache(this /* context */, DICTIONARY_NAME_PREFIX); - private final ConcurrentHashMap<Locale, Keyboard> mKeyboardCache = new ConcurrentHashMap<>(); - - // The threshold for a suggestion to be considered "recommended". - private float mRecommendedThreshold; - // TODO: make a spell checker option to block offensive words or not - private final SettingsValuesForSuggestion mSettingsValuesForSuggestion = - new SettingsValuesForSuggestion(true /* blockPotentiallyOffensive */); - - public static final String SINGLE_QUOTE = "\u0027"; - public static final String APOSTROPHE = "\u2019"; - - public AndroidSpellCheckerService() { - super(); - for (int i = 0; i < MAX_NUM_OF_THREADS_READ_DICTIONARY; i++) { - mSessionIdPool.add(i); - } - } - - @Override - public void onCreate() { - super.onCreate(); - mRecommendedThreshold = Float.parseFloat( - getString(R.string.spellchecker_recommended_threshold_value)); - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - prefs.registerOnSharedPreferenceChangeListener(this); - onSharedPreferenceChanged(prefs, PREF_USE_CONTACTS_KEY); - } - - public float getRecommendedThreshold() { - return mRecommendedThreshold; - } - - private static String getKeyboardLayoutNameForLocale(final Locale locale) { - // See b/19963288. - if (locale.getLanguage().equals("sr")) { - return "south_slavic"; - } - final int script = ScriptUtils.getScriptFromSpellCheckerLocale(locale); - switch (script) { - case ScriptUtils.SCRIPT_LATIN: - return "qwerty"; - case ScriptUtils.SCRIPT_CYRILLIC: - return "east_slavic"; - case ScriptUtils.SCRIPT_GREEK: - return "greek"; - case ScriptUtils.SCRIPT_HEBREW: - return "hebrew"; - default: - throw new RuntimeException("Wrong script supplied: " + script); - } - } - - @Override - public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { - if (!PREF_USE_CONTACTS_KEY.equals(key)) return; - final boolean useContactsDictionary = prefs.getBoolean(PREF_USE_CONTACTS_KEY, true); - mDictionaryFacilitatorCache.setUseContactsDictionary(useContactsDictionary); - } - - @Override - public Session createSession() { - // Should not refer to AndroidSpellCheckerSession directly considering - // that AndroidSpellCheckerSession may be overlaid. - return AndroidSpellCheckerSessionFactory.newInstance(this); - } - - /** - * Returns an empty SuggestionsInfo with flags signaling the word is not in the dictionary. - * @param reportAsTypo whether this should include the flag LOOKS_LIKE_TYPO, for red underline. - * @return the empty SuggestionsInfo with the appropriate flags set. - */ - public static SuggestionsInfo getNotInDictEmptySuggestions(final boolean reportAsTypo) { - return new SuggestionsInfo(reportAsTypo ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0, - EMPTY_STRING_ARRAY); - } - - /** - * Returns an empty suggestionInfo with flags signaling the word is in the dictionary. - * @return the empty SuggestionsInfo with the appropriate flags set. - */ - public static SuggestionsInfo getInDictEmptySuggestions() { - return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, - EMPTY_STRING_ARRAY); - } - - public boolean isValidWord(final Locale locale, final String word) { - mSemaphore.acquireUninterruptibly(); - try { - DictionaryFacilitator dictionaryFacilitatorForLocale = - mDictionaryFacilitatorCache.get(locale); - return dictionaryFacilitatorForLocale.isValidSpellingWord(word); - } finally { - mSemaphore.release(); - } - } - - public SuggestionResults getSuggestionResults(final Locale locale, - final ComposedData composedData, final NgramContext ngramContext, - @Nonnull final Keyboard keyboard) { - Integer sessionId = null; - mSemaphore.acquireUninterruptibly(); - try { - sessionId = mSessionIdPool.poll(); - DictionaryFacilitator dictionaryFacilitatorForLocale = - mDictionaryFacilitatorCache.get(locale); - return dictionaryFacilitatorForLocale.getSuggestionResults(composedData, ngramContext, - keyboard, mSettingsValuesForSuggestion, - sessionId, SuggestedWords.INPUT_STYLE_TYPING); - } finally { - if (sessionId != null) { - mSessionIdPool.add(sessionId); - } - mSemaphore.release(); - } - } - - public boolean hasMainDictionaryForLocale(final Locale locale) { - mSemaphore.acquireUninterruptibly(); - try { - final DictionaryFacilitator dictionaryFacilitator = - mDictionaryFacilitatorCache.get(locale); - return dictionaryFacilitator.hasAtLeastOneInitializedMainDictionary(); - } finally { - mSemaphore.release(); - } - } - - @Override - public boolean onUnbind(final Intent intent) { - mSemaphore.acquireUninterruptibly(MAX_NUM_OF_THREADS_READ_DICTIONARY); - try { - mDictionaryFacilitatorCache.closeDictionaries(); - } finally { - mSemaphore.release(MAX_NUM_OF_THREADS_READ_DICTIONARY); - } - mKeyboardCache.clear(); - return false; - } - - public Keyboard getKeyboardForLocale(final Locale locale) { - Keyboard keyboard = mKeyboardCache.get(locale); - if (keyboard == null) { - keyboard = createKeyboardForLocale(locale); - if (keyboard != null) { - mKeyboardCache.put(locale, keyboard); - } - } - return keyboard; - } - - private Keyboard createKeyboardForLocale(final Locale locale) { - final String keyboardLayoutName = getKeyboardLayoutNameForLocale(locale); - final InputMethodSubtype subtype = AdditionalSubtypeUtils.createDummyAdditionalSubtype( - locale.toString(), keyboardLayoutName); - final KeyboardLayoutSet keyboardLayoutSet = createKeyboardSetForSpellChecker(subtype); - return keyboardLayoutSet.getKeyboard(KeyboardId.ELEMENT_ALPHABET); - } - - private KeyboardLayoutSet createKeyboardSetForSpellChecker(final InputMethodSubtype subtype) { - final EditorInfo editorInfo = new EditorInfo(); - editorInfo.inputType = InputType.TYPE_CLASS_TEXT; - final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(this, editorInfo); - builder.setKeyboardGeometry( - SPELLCHECKER_DUMMY_KEYBOARD_WIDTH, SPELLCHECKER_DUMMY_KEYBOARD_HEIGHT); - builder.setSubtype(RichInputMethodSubtype.getRichInputMethodSubtype(subtype)); - builder.setIsSpellChecker(true /* isSpellChecker */); - builder.disableTouchPositionCorrectionData(); - return builder.build(); - } -} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java deleted file mode 100644 index c7622e7a1..000000000 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * 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.annotation.TargetApi; -import android.content.res.Resources; -import android.os.Binder; -import android.os.Build; -import android.text.TextUtils; -import android.util.Log; -import android.view.textservice.SentenceSuggestionsInfo; -import android.view.textservice.SuggestionsInfo; -import android.view.textservice.TextInfo; - -import com.android.inputmethod.compat.TextInfoCompatUtils; -import com.android.inputmethod.latin.NgramContext; -import com.android.inputmethod.latin.utils.SpannableStringUtils; - -import java.util.ArrayList; -import java.util.Locale; - -public final class AndroidSpellCheckerSession extends AndroidWordLevelSpellCheckerSession { - private static final String TAG = AndroidSpellCheckerSession.class.getSimpleName(); - private static final boolean DBG = false; - private final Resources mResources; - private SentenceLevelAdapter mSentenceLevelAdapter; - - public AndroidSpellCheckerSession(AndroidSpellCheckerService service) { - super(service); - mResources = service.getResources(); - } - - @TargetApi(Build.VERSION_CODES.JELLY_BEAN) - private SentenceSuggestionsInfo fixWronglyInvalidatedWordWithSingleQuote(TextInfo ti, - SentenceSuggestionsInfo ssi) { - final CharSequence typedText = TextInfoCompatUtils.getCharSequenceOrString(ti); - if (!typedText.toString().contains(AndroidSpellCheckerService.SINGLE_QUOTE)) { - return null; - } - final int N = ssi.getSuggestionsCount(); - final ArrayList<Integer> additionalOffsets = new ArrayList<>(); - final ArrayList<Integer> additionalLengths = new ArrayList<>(); - final ArrayList<SuggestionsInfo> additionalSuggestionsInfos = new ArrayList<>(); - CharSequence 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 CharSequence subText = typedText.subSequence(offset, offset + length); - final NgramContext ngramContext = - new NgramContext(new NgramContext.WordInfo(currentWord)); - currentWord = subText; - if (!subText.toString().contains(AndroidSpellCheckerService.SINGLE_QUOTE)) { - continue; - } - // Split preserving spans. - final CharSequence[] splitTexts = SpannableStringUtils.split(subText, - AndroidSpellCheckerService.SINGLE_QUOTE, - true /* preserveTrailingEmptySegments */); - if (splitTexts == null || splitTexts.length <= 1) { - continue; - } - final int splitNum = splitTexts.length; - for (int j = 0; j < splitNum; ++j) { - final CharSequence splitText = splitTexts[j]; - if (TextUtils.isEmpty(splitText)) { - continue; - } - if (mSuggestionsCache.getSuggestionsFromCache(splitText.toString()) == 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 = splitAndSuggest(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; - } - - /** - * Get sentence suggestions for specified texts in an array of TextInfo. This is taken from - * SpellCheckerService#onGetSentenceSuggestionsMultiple that we can't use because it's - * using private variables. - * The default implementation splits the input text to words and returns - * {@link SentenceSuggestionsInfo} which contains suggestions for each word. - * This function will run on the incoming IPC thread. - * So, this is not called on the main thread, - * but will be called in series on another thread. - * @param textInfos an array of the text metadata - * @param suggestionsLimit the maximum number of suggestions to be returned - * @return an array of {@link SentenceSuggestionsInfo} returned by - * {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestions(TextInfo, int)} - */ - private SentenceSuggestionsInfo[] splitAndSuggest(TextInfo[] textInfos, int suggestionsLimit) { - if (textInfos == null || textInfos.length == 0) { - return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo(); - } - SentenceLevelAdapter sentenceLevelAdapter; - synchronized(this) { - sentenceLevelAdapter = mSentenceLevelAdapter; - if (sentenceLevelAdapter == null) { - final String localeStr = getLocale(); - if (!TextUtils.isEmpty(localeStr)) { - sentenceLevelAdapter = new SentenceLevelAdapter(mResources, - new Locale(localeStr)); - mSentenceLevelAdapter = sentenceLevelAdapter; - } - } - } - if (sentenceLevelAdapter == null) { - return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo(); - } - final int infosSize = textInfos.length; - final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize]; - for (int i = 0; i < infosSize; ++i) { - final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams = - sentenceLevelAdapter.getSplitWords(textInfos[i]); - final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems = - textInfoParams.mItems; - final int itemsSize = mItems.size(); - final TextInfo[] splitTextInfos = new TextInfo[itemsSize]; - for (int j = 0; j < itemsSize; ++j) { - splitTextInfos[j] = mItems.get(j).mTextInfo; - } - retval[i] = SentenceLevelAdapter.reconstructSuggestions( - textInfoParams, onGetSuggestionsMultiple( - splitTextInfos, suggestionsLimit, true)); - } - return retval; - } - - @Override - public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos, - int suggestionsLimit, boolean sequentialWords) { - long ident = Binder.clearCallingIdentity(); - try { - final int length = textInfos.length; - final SuggestionsInfo[] retval = new SuggestionsInfo[length]; - for (int i = 0; i < length; ++i) { - final CharSequence prevWord; - if (sequentialWords && i > 0) { - final TextInfo prevTextInfo = textInfos[i - 1]; - final CharSequence prevWordCandidate = - TextInfoCompatUtils.getCharSequenceOrString(prevTextInfo); - // 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; - } - final NgramContext ngramContext = - new NgramContext(new NgramContext.WordInfo(prevWord)); - final TextInfo textInfo = textInfos[i]; - retval[i] = onGetSuggestionsInternal(textInfo, ngramContext, suggestionsLimit); - retval[i].setCookieAndSequence(textInfo.getCookie(), textInfo.getSequence()); - } - return retval; - } finally { - Binder.restoreCallingIdentity(ident); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSessionFactory.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSessionFactory.java deleted file mode 100644 index e0418d404..000000000 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSessionFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 deleted file mode 100644 index 9223923a7..000000000 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java +++ /dev/null @@ -1,390 +0,0 @@ -/* - * 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.content.ContentResolver; -import android.database.ContentObserver; -import android.os.Binder; -import android.provider.UserDictionary.Words; -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.keyboard.Keyboard; -import com.android.inputmethod.latin.NgramContext; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.WordComposer; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.LocaleUtils; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.define.DebugFlags; -import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; -import com.android.inputmethod.latin.utils.ScriptUtils; -import com.android.inputmethod.latin.utils.StatsUtils; -import com.android.inputmethod.latin.utils.SuggestionResults; - -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -public abstract class AndroidWordLevelSpellCheckerSession extends Session { - private static final String TAG = AndroidWordLevelSpellCheckerSession.class.getSimpleName(); - - public final static String[] EMPTY_STRING_ARRAY = new String[0]; - - // Immutable, but not available in the constructor. - 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 final ContentObserver mObserver; - - private static final String quotesRegexp = - "(\\u0022|\\u0027|\\u0060|\\u00B4|\\u2018|\\u2018|\\u201C|\\u201D)"; - - private static final class SuggestionsParams { - public final String[] mSuggestions; - public final int mFlags; - public SuggestionsParams(String[] suggestions, int flags) { - mSuggestions = suggestions; - mFlags = flags; - } - } - - protected static final class SuggestionsCache { - private static final int MAX_CACHE_SIZE = 50; - private final LruCache<String, SuggestionsParams> mUnigramSuggestionsInfoCache = - new LruCache<>(MAX_CACHE_SIZE); - - private static String generateKey(final String query) { - return query + ""; - } - - public SuggestionsParams getSuggestionsFromCache(final String query) { - return mUnigramSuggestionsInfoCache.get(query); - } - - public void putSuggestionsToCache( - final String query, final String[] suggestions, final int flags) { - if (suggestions == null || TextUtils.isEmpty(query)) { - return; - } - mUnigramSuggestionsInfoCache.put( - generateKey(query), - new SuggestionsParams(suggestions, flags)); - } - - public void clearCache() { - mUnigramSuggestionsInfoCache.evictAll(); - } - } - - AndroidWordLevelSpellCheckerSession(final AndroidSpellCheckerService service) { - mService = service; - final ContentResolver cres = service.getContentResolver(); - - mObserver = new ContentObserver(null) { - @Override - public void onChange(boolean self) { - mSuggestionsCache.clearCache(); - } - }; - cres.registerContentObserver(Words.CONTENT_URI, true, mObserver); - } - - @Override - public void onCreate() { - final String localeString = getLocale(); - mLocale = (null == localeString) ? null - : LocaleUtils.constructLocaleFromString(localeString); - mScript = ScriptUtils.getScriptFromSpellCheckerLocale(mLocale); - } - - @Override - public void onClose() { - final ContentResolver cres = mService.getContentResolver(); - cres.unregisterContentObserver(mObserver); - } - - private static final int CHECKABILITY_CHECKABLE = 0; - private static final int CHECKABILITY_TOO_MANY_NON_LETTERS = 1; - private static final int CHECKABILITY_CONTAINS_PERIOD = 2; - private static final int CHECKABILITY_EMAIL_OR_URL = 3; - private static final int CHECKABILITY_FIRST_LETTER_UNCHECKABLE = 4; - private static final int CHECKABILITY_TOO_SHORT = 5; - /** - * 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 one of the FILTER_OUT_* constants above. - */ - private static int getCheckabilityInScript(final String text, final int script) { - if (TextUtils.isEmpty(text) || text.length() <= 1) return CHECKABILITY_TOO_SHORT; - - // 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 (!ScriptUtils.isLetterPartOfScript(firstCodePoint, script) - && '\'' != firstCodePoint) return CHECKABILITY_FIRST_LETTER_UNCHECKABLE; - - // 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 COMMERCIAL_AT is probably an e-mail address - // Any word containing a SLASH 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 (Constants.CODE_COMMERCIAL_AT == codePoint || Constants.CODE_SLASH == codePoint) { - return CHECKABILITY_EMAIL_OR_URL; - } - // If the string contains a period, native returns strange suggestions (it seems - // to return suggestions for everything up to the period only and to ignore the - // rest), so we suppress lookup if there is a period. - // TODO: investigate why native returns these suggestions and remove this code. - if (Constants.CODE_PERIOD == codePoint) { - return CHECKABILITY_CONTAINS_PERIOD; - } - if (ScriptUtils.isLetterPartOfScript(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) - ? CHECKABILITY_TOO_MANY_NON_LETTERS : CHECKABILITY_CHECKABLE; - } - - /** - * Helper method to test valid capitalizations of a word. - * - * If the "text" is lower-case, we test only the exact string. - * If the "Text" is capitalized, we test the exact string "Text" and the lower-cased - * version of it "text". - * If the "TEXT" is fully upper case, we test the exact string "TEXT", the lower-cased - * version of it "text" and the capitalized version of it "Text". - */ - private boolean isInDictForAnyCapitalization(final String text, final int capitalizeType) { - // If the word is in there as is, then it's in the dictionary. If not, we'll test lower - // case versions, but only if the word is not already all-lower case or mixed case. - if (mService.isValidWord(mLocale, text)) return true; - if (StringUtils.CAPITALIZE_NONE == capitalizeType) return false; - - // If we come here, we have a capitalized word (either First- or All-). - // Downcase the word and look it up again. If the word is only capitalized, we - // tested all possibilities, so if it's still negative we can return false. - final String lowerCaseText = text.toLowerCase(mLocale); - if (mService.isValidWord(mLocale, lowerCaseText)) return true; - if (StringUtils.CAPITALIZE_FIRST == capitalizeType) return false; - - // If the lower case version is not in the dictionary, it's still possible - // that we have an all-caps version of a word that needs to be capitalized - // according to the dictionary. E.g. "GERMANS" only exists in the dictionary as "Germans". - return mService.isValidWord(mLocale, - StringUtils.capitalizeFirstAndDowncaseRest(lowerCaseText, mLocale)); - } - - // 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. - */ - private SuggestionsInfo onGetSuggestionsInternal(final TextInfo textInfo, - final int suggestionsLimit) { - return onGetSuggestionsInternal(textInfo, null, suggestionsLimit); - } - - protected SuggestionsInfo onGetSuggestionsInternal( - final TextInfo textInfo, final NgramContext ngramContext, final int suggestionsLimit) { - try { - final String text = textInfo.getText(). - replaceAll(AndroidSpellCheckerService.APOSTROPHE, - AndroidSpellCheckerService.SINGLE_QUOTE). - replaceAll("^" + quotesRegexp, ""). - replaceAll(quotesRegexp + "$", ""); - - if (!mService.hasMainDictionaryForLocale(mLocale)) { - return AndroidSpellCheckerService.getNotInDictEmptySuggestions( - false /* reportAsTypo */); - } - - // Handle special patterns like email, URI, telephone number. - final int checkability = getCheckabilityInScript(text, mScript); - if (CHECKABILITY_CHECKABLE != checkability) { - if (CHECKABILITY_CONTAINS_PERIOD == checkability) { - final String[] splitText = text.split(Constants.REGEXP_PERIOD); - boolean allWordsAreValid = true; - for (final String word : splitText) { - if (!mService.isValidWord(mLocale, word)) { - allWordsAreValid = false; - break; - } - } - if (allWordsAreValid) { - return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO - | SuggestionsInfo.RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS, - new String[] { - TextUtils.join(Constants.STRING_SPACE, splitText) }); - } - } - return mService.isValidWord(mLocale, text) ? - AndroidSpellCheckerService.getInDictEmptySuggestions() : - AndroidSpellCheckerService.getNotInDictEmptySuggestions( - CHECKABILITY_CONTAINS_PERIOD == checkability /* reportAsTypo */); - } - - // Handle normal words. - final int capitalizeType = StringUtils.getCapitalizationType(text); - - if (isInDictForAnyCapitalization(text, capitalizeType)) { - if (DebugFlags.DEBUG_ENABLED) { - Log.i(TAG, "onGetSuggestionsInternal() : [" + text + "] is a valid word"); - } - return AndroidSpellCheckerService.getInDictEmptySuggestions(); - } - if (DebugFlags.DEBUG_ENABLED) { - Log.i(TAG, "onGetSuggestionsInternal() : [" + text + "] is NOT a valid word"); - } - - final Keyboard keyboard = mService.getKeyboardForLocale(mLocale); - if (null == keyboard) { - Log.w(TAG, "onGetSuggestionsInternal() : No keyboard for locale: " + mLocale); - // If there is no keyboard for this locale, don't do any spell-checking. - return AndroidSpellCheckerService.getNotInDictEmptySuggestions( - false /* reportAsTypo */); - } - - final WordComposer composer = new WordComposer(); - final int[] codePoints = StringUtils.toCodePointArray(text); - final int[] coordinates; - coordinates = keyboard.getCoordinates(codePoints); - composer.setComposingWord(codePoints, coordinates); - // TODO: Don't gather suggestions if the limit is <= 0 unless necessary - final SuggestionResults suggestionResults = mService.getSuggestionResults( - mLocale, composer.getComposedDataSnapshot(), ngramContext, keyboard); - final Result result = getResult(capitalizeType, mLocale, suggestionsLimit, - mService.getRecommendedThreshold(), text, suggestionResults); - if (DebugFlags.DEBUG_ENABLED) { - if (result.mSuggestions != null && result.mSuggestions.length > 0) { - final StringBuilder builder = new StringBuilder(); - for (String suggestion : result.mSuggestions) { - builder.append(" ["); - builder.append(suggestion); - builder.append("]"); - } - Log.i(TAG, "onGetSuggestionsInternal() : Suggestions =" + builder); - } - } - // Handle word not in dictionary. - // This is called only once per unique word, so entering multiple - // instances of the same word does not result in more than one call - // to this method. - // Also, upon changing the orientation of the device, this is called - // again for every unique invalid word in the text box. - StatsUtils.onInvalidWordIdentification(text); - - final int flags = - 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, result.mSuggestions, flags); - return retval; - } catch (RuntimeException e) { - // Don't kill the keyboard if there is a bug in the spell checker - Log.e(TAG, "Exception while spellchecking", e); - return AndroidSpellCheckerService.getNotInDictEmptySuggestions( - false /* reportAsTypo */); - } - } - - private static final class Result { - public final String[] mSuggestions; - public final boolean mHasRecommendedSuggestions; - public Result(final String[] gatheredSuggestions, final boolean hasRecommendedSuggestions) { - mSuggestions = gatheredSuggestions; - mHasRecommendedSuggestions = hasRecommendedSuggestions; - } - } - - private static Result getResult(final int capitalizeType, final Locale locale, - final int suggestionsLimit, final float recommendedThreshold, final String originalText, - final SuggestionResults suggestionResults) { - if (suggestionResults.isEmpty() || suggestionsLimit <= 0) { - return new Result(null /* gatheredSuggestions */, - false /* hasRecommendedSuggestions */); - } - final ArrayList<String> suggestions = new ArrayList<>(); - for (final SuggestedWordInfo suggestedWordInfo : suggestionResults) { - final String suggestion; - if (StringUtils.CAPITALIZE_ALL == capitalizeType) { - suggestion = suggestedWordInfo.mWord.toUpperCase(locale); - } else if (StringUtils.CAPITALIZE_FIRST == capitalizeType) { - suggestion = StringUtils.capitalizeFirstCodePoint( - suggestedWordInfo.mWord, locale); - } else { - suggestion = suggestedWordInfo.mWord; - } - suggestions.add(suggestion); - } - StringUtils.removeDupes(suggestions); - // This returns a String[], while toArray() returns an Object[] which cannot be cast - // into a String[]. - final List<String> gatheredSuggestionsList = - suggestions.subList(0, Math.min(suggestions.size(), suggestionsLimit)); - final String[] gatheredSuggestions = - gatheredSuggestionsList.toArray(new String[gatheredSuggestionsList.size()]); - - final int bestScore = suggestionResults.first().mScore; - final String bestSuggestion = suggestions.get(0); - final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore( - originalText, bestSuggestion, bestScore); - final boolean hasRecommendedSuggestions = (normalizedScore > recommendedThreshold); - return new Result(gatheredSuggestions, hasRecommendedSuggestions); - } - - /* - * The spell checker acts on its own behalf. That is needed, in particular, to be able to - * access the dictionary files, which the provider restricts to the identity of Latin IME. - * Since it's called externally by the application, the spell checker is using the identity - * of the application by default unless we clearCallingIdentity. - * That's what the following method does. - */ - @Override - public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) { - long ident = Binder.clearCallingIdentity(); - try { - return onGetSuggestionsInternal(textInfo, suggestionsLimit); - } finally { - Binder.restoreCallingIdentity(ident); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SentenceLevelAdapter.java b/java/src/com/android/inputmethod/latin/spellcheck/SentenceLevelAdapter.java deleted file mode 100644 index 10c458c7d..000000000 --- a/java/src/com/android/inputmethod/latin/spellcheck/SentenceLevelAdapter.java +++ /dev/null @@ -1,197 +0,0 @@ -/* - * Copyright (C) 2014 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.annotation.TargetApi; -import android.content.res.Resources; -import android.os.Build; -import android.view.textservice.SentenceSuggestionsInfo; -import android.view.textservice.SuggestionsInfo; -import android.view.textservice.TextInfo; - -import com.android.inputmethod.compat.TextInfoCompatUtils; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.settings.SpacingAndPunctuations; -import com.android.inputmethod.latin.utils.RunInLocale; - -import java.util.ArrayList; -import java.util.Locale; - -/** - * This code is mostly lifted directly from android.service.textservice.SpellCheckerService in - * the framework; maybe that should be protected instead, so that implementers don't have to - * rewrite everything for any small change. - */ -public class SentenceLevelAdapter { - private static class EmptySentenceSuggestionsInfosInitializationHolder { - public static final SentenceSuggestionsInfo[] EMPTY_SENTENCE_SUGGESTIONS_INFOS = - new SentenceSuggestionsInfo[]{}; - } - private static final SuggestionsInfo EMPTY_SUGGESTIONS_INFO = new SuggestionsInfo(0, null); - - public static SentenceSuggestionsInfo[] getEmptySentenceSuggestionsInfo() { - return EmptySentenceSuggestionsInfosInitializationHolder.EMPTY_SENTENCE_SUGGESTIONS_INFOS; - } - - /** - * Container for split TextInfo parameters - */ - public static class SentenceWordItem { - public final TextInfo mTextInfo; - public final int mStart; - public final int mLength; - public SentenceWordItem(TextInfo ti, int start, int end) { - mTextInfo = ti; - mStart = start; - mLength = end - start; - } - } - - /** - * Container for originally queried TextInfo and parameters - */ - public static class SentenceTextInfoParams { - final TextInfo mOriginalTextInfo; - final ArrayList<SentenceWordItem> mItems; - final int mSize; - public SentenceTextInfoParams(TextInfo ti, ArrayList<SentenceWordItem> items) { - mOriginalTextInfo = ti; - mItems = items; - mSize = items.size(); - } - } - - private static class WordIterator { - private final SpacingAndPunctuations mSpacingAndPunctuations; - public WordIterator(final Resources res, final Locale locale) { - final RunInLocale<SpacingAndPunctuations> job = - new RunInLocale<SpacingAndPunctuations>() { - @Override - protected SpacingAndPunctuations job(final Resources r) { - return new SpacingAndPunctuations(r); - } - }; - mSpacingAndPunctuations = job.runInLocale(res, locale); - } - - public int getEndOfWord(final CharSequence sequence, final int fromIndex) { - final int length = sequence.length(); - int index = fromIndex < 0 ? 0 : Character.offsetByCodePoints(sequence, fromIndex, 1); - while (index < length) { - final int codePoint = Character.codePointAt(sequence, index); - if (mSpacingAndPunctuations.isWordSeparator(codePoint)) { - // If it's a period, we want to stop here only if it's followed by another - // word separator. In all other cases we stop here. - if (Constants.CODE_PERIOD == codePoint) { - final int indexOfNextCodePoint = - index + Character.charCount(Constants.CODE_PERIOD); - if (indexOfNextCodePoint < length - && mSpacingAndPunctuations.isWordSeparator( - Character.codePointAt(sequence, indexOfNextCodePoint))) { - return index; - } - } else { - return index; - } - } - index += Character.charCount(codePoint); - } - return index; - } - - public int getBeginningOfNextWord(final CharSequence sequence, final int fromIndex) { - final int length = sequence.length(); - if (fromIndex >= length) { - return -1; - } - int index = fromIndex < 0 ? 0 : Character.offsetByCodePoints(sequence, fromIndex, 1); - while (index < length) { - final int codePoint = Character.codePointAt(sequence, index); - if (!mSpacingAndPunctuations.isWordSeparator(codePoint)) { - return index; - } - index += Character.charCount(codePoint); - } - return -1; - } - } - - private final WordIterator mWordIterator; - public SentenceLevelAdapter(final Resources res, final Locale locale) { - mWordIterator = new WordIterator(res, locale); - } - - public SentenceTextInfoParams getSplitWords(TextInfo originalTextInfo) { - final WordIterator wordIterator = mWordIterator; - final CharSequence originalText = - TextInfoCompatUtils.getCharSequenceOrString(originalTextInfo); - final int cookie = originalTextInfo.getCookie(); - final int start = -1; - final int end = originalText.length(); - final ArrayList<SentenceWordItem> wordItems = new ArrayList<>(); - int wordStart = wordIterator.getBeginningOfNextWord(originalText, start); - int wordEnd = wordIterator.getEndOfWord(originalText, wordStart); - while (wordStart <= end && wordEnd != -1 && wordStart != -1) { - if (wordEnd >= start && wordEnd > wordStart) { - final TextInfo ti = TextInfoCompatUtils.newInstance(originalText, wordStart, - wordEnd, cookie, originalText.subSequence(wordStart, wordEnd).hashCode()); - wordItems.add(new SentenceWordItem(ti, wordStart, wordEnd)); - } - wordStart = wordIterator.getBeginningOfNextWord(originalText, wordEnd); - if (wordStart == -1) { - break; - } - wordEnd = wordIterator.getEndOfWord(originalText, wordStart); - } - return new SentenceTextInfoParams(originalTextInfo, wordItems); - } - - @TargetApi(Build.VERSION_CODES.JELLY_BEAN) - public static SentenceSuggestionsInfo reconstructSuggestions( - SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) { - if (results == null || results.length == 0) { - return null; - } - if (originalTextInfoParams == null) { - return null; - } - final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie(); - final int originalSequence = - originalTextInfoParams.mOriginalTextInfo.getSequence(); - - final int querySize = originalTextInfoParams.mSize; - final int[] offsets = new int[querySize]; - final int[] lengths = new int[querySize]; - final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize]; - for (int i = 0; i < querySize; ++i) { - final SentenceWordItem item = originalTextInfoParams.mItems.get(i); - SuggestionsInfo result = null; - for (int j = 0; j < results.length; ++j) { - final SuggestionsInfo cur = results[j]; - if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) { - result = cur; - result.setCookieAndSequence(originalCookie, originalSequence); - break; - } - } - offsets[i] = item.mStart; - lengths[i] = item.mLength; - reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO; - } - return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths); - } -} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java deleted file mode 100644 index 5f99f9004..000000000 --- a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.spellcheck; - -import com.android.inputmethod.latin.permissions.PermissionsManager; -import com.android.inputmethod.latin.utils.FragmentUtils; - -import android.annotation.TargetApi; -import android.content.Intent; -import android.os.Build; -import android.os.Bundle; -import android.preference.PreferenceActivity; -import androidx.core.app.ActivityCompat; - -/** - * Spell checker preference screen. - */ -public final class SpellCheckerSettingsActivity extends PreferenceActivity - implements ActivityCompat.OnRequestPermissionsResultCallback { - private static final String DEFAULT_FRAGMENT = SpellCheckerSettingsFragment.class.getName(); - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - } - - @Override - public Intent getIntent() { - final Intent modIntent = new Intent(super.getIntent()); - modIntent.putExtra(EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT); - modIntent.putExtra(EXTRA_NO_HEADERS, true); - return modIntent; - } - - @TargetApi(Build.VERSION_CODES.KITKAT) - @Override - public boolean isValidFragment(String fragmentName) { - return FragmentUtils.isValidFragment(fragmentName); - } - - @Override - public void onRequestPermissionsResult( - int requestCode, String[] permissions, int[] grantResults) { - PermissionsManager.get(this).onRequestPermissionsResult( - requestCode, permissions, grantResults); - } -} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsFragment.java b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsFragment.java deleted file mode 100644 index 12005c25e..000000000 --- a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsFragment.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.spellcheck; - -import android.Manifest; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.preference.PreferenceScreen; -import android.preference.SwitchPreference; -import android.text.TextUtils; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.permissions.PermissionsManager; -import com.android.inputmethod.latin.permissions.PermissionsUtil; -import com.android.inputmethod.latin.settings.SubScreenFragment; -import com.android.inputmethod.latin.settings.TwoStatePreferenceHelper; -import com.android.inputmethod.latin.utils.ApplicationUtils; - -import static com.android.inputmethod.latin.permissions.PermissionsManager.get; - -/** - * Preference screen. - */ -public final class SpellCheckerSettingsFragment extends SubScreenFragment - implements SharedPreferences.OnSharedPreferenceChangeListener, - PermissionsManager.PermissionsResultCallback { - - private SwitchPreference mLookupContactsPreference; - - @Override - public void onActivityCreated(final Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - addPreferencesFromResource(R.xml.spell_checker_settings); - final PreferenceScreen preferenceScreen = getPreferenceScreen(); - preferenceScreen.setTitle(ApplicationUtils.getActivityTitleResId( - getActivity(), SpellCheckerSettingsActivity.class)); - TwoStatePreferenceHelper.replaceCheckBoxPreferencesBySwitchPreferences(preferenceScreen); - - mLookupContactsPreference = (SwitchPreference) findPreference( - AndroidSpellCheckerService.PREF_USE_CONTACTS_KEY); - turnOffLookupContactsIfNoPermission(); - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (!TextUtils.equals(key, AndroidSpellCheckerService.PREF_USE_CONTACTS_KEY)) { - return; - } - - if (!sharedPreferences.getBoolean(key, false)) { - // don't care if the preference is turned off. - return; - } - - // Check for permissions. - if (PermissionsUtil.checkAllPermissionsGranted( - getActivity() /* context */, Manifest.permission.READ_CONTACTS)) { - return; // all permissions granted, no need to request permissions. - } - - get(getActivity() /* context */).requestPermissions(this /* PermissionsResultCallback */, - getActivity() /* activity */, Manifest.permission.READ_CONTACTS); - } - - @Override - public void onRequestPermissionsResult(boolean allGranted) { - turnOffLookupContactsIfNoPermission(); - } - - private void turnOffLookupContactsIfNoPermission() { - if (!PermissionsUtil.checkAllPermissionsGranted( - getActivity(), Manifest.permission.READ_CONTACTS)) { - mLookupContactsPreference.setChecked(false); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java deleted file mode 100644 index 37ab2669b..000000000 --- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java +++ /dev/null @@ -1,268 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.suggestions; - -import android.content.Context; -import android.content.res.Resources; -import android.graphics.Paint; -import android.graphics.drawable.Drawable; - -import com.android.inputmethod.keyboard.Key; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.internal.KeyboardBuilder; -import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; -import com.android.inputmethod.keyboard.internal.KeyboardParams; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.utils.TypefaceUtils; - -public final class MoreSuggestions extends Keyboard { - public final SuggestedWords mSuggestedWords; - - MoreSuggestions(final MoreSuggestionsParam params, final SuggestedWords suggestedWords) { - super(params); - mSuggestedWords = suggestedWords; - } - - private static final class MoreSuggestionsParam extends KeyboardParams { - private final int[] mWidths = new int[SuggestedWords.MAX_SUGGESTIONS]; - private final int[] mRowNumbers = new int[SuggestedWords.MAX_SUGGESTIONS]; - private final int[] mColumnOrders = new int[SuggestedWords.MAX_SUGGESTIONS]; - private final int[] mNumColumnsInRow = new int[SuggestedWords.MAX_SUGGESTIONS]; - private static final int MAX_COLUMNS_IN_ROW = 3; - private int mNumRows; - public Drawable mDivider; - public int mDividerWidth; - - public MoreSuggestionsParam() { - super(); - } - - public int layout(final SuggestedWords suggestedWords, final int fromIndex, - final int maxWidth, final int minWidth, final int maxRow, final Paint paint, - final Resources res) { - clearKeys(); - mDivider = res.getDrawable(R.drawable.more_suggestions_divider); - mDividerWidth = mDivider.getIntrinsicWidth(); - final float padding = res.getDimension( - R.dimen.config_more_suggestions_key_horizontal_padding); - - int row = 0; - int index = fromIndex; - int rowStartIndex = fromIndex; - final int size = Math.min(suggestedWords.size(), SuggestedWords.MAX_SUGGESTIONS); - while (index < size) { - final String word; - if (isIndexSubjectToAutoCorrection(suggestedWords, index)) { - // INDEX_OF_AUTO_CORRECTION and INDEX_OF_TYPED_WORD got swapped. - word = suggestedWords.getLabel(SuggestedWords.INDEX_OF_TYPED_WORD); - } else { - word = suggestedWords.getLabel(index); - } - // TODO: Should take care of text x-scaling. - mWidths[index] = (int)(TypefaceUtils.getStringWidth(word, paint) + padding); - final int numColumn = index - rowStartIndex + 1; - final int columnWidth = - (maxWidth - mDividerWidth * (numColumn - 1)) / numColumn; - if (numColumn > MAX_COLUMNS_IN_ROW - || !fitInWidth(rowStartIndex, index + 1, columnWidth)) { - if ((row + 1) >= maxRow) { - break; - } - mNumColumnsInRow[row] = index - rowStartIndex; - rowStartIndex = index; - row++; - } - mColumnOrders[index] = index - rowStartIndex; - mRowNumbers[index] = row; - index++; - } - mNumColumnsInRow[row] = index - rowStartIndex; - mNumRows = row + 1; - mBaseWidth = mOccupiedWidth = Math.max( - minWidth, calcurateMaxRowWidth(fromIndex, index)); - mBaseHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight + mVerticalGap; - return index - fromIndex; - } - - private boolean fitInWidth(final int startIndex, final int endIndex, final int width) { - for (int index = startIndex; index < endIndex; index++) { - if (mWidths[index] > width) - return false; - } - return true; - } - - private int calcurateMaxRowWidth(final int startIndex, final int endIndex) { - int maxRowWidth = 0; - int index = startIndex; - for (int row = 0; row < mNumRows; row++) { - final int numColumnInRow = mNumColumnsInRow[row]; - int maxKeyWidth = 0; - while (index < endIndex && mRowNumbers[index] == row) { - maxKeyWidth = Math.max(maxKeyWidth, mWidths[index]); - index++; - } - maxRowWidth = Math.max(maxRowWidth, - maxKeyWidth * numColumnInRow + mDividerWidth * (numColumnInRow - 1)); - } - return maxRowWidth; - } - - private static final int[][] COLUMN_ORDER_TO_NUMBER = { - { 0 }, // center - { 1, 0 }, // right-left - { 1, 0, 2 }, // center-left-right - }; - - public int getNumColumnInRow(final int index) { - return mNumColumnsInRow[mRowNumbers[index]]; - } - - public int getColumnNumber(final int index) { - final int columnOrder = mColumnOrders[index]; - final int numColumn = getNumColumnInRow(index); - return COLUMN_ORDER_TO_NUMBER[numColumn - 1][columnOrder]; - } - - public int getX(final int index) { - final int columnNumber = getColumnNumber(index); - return columnNumber * (getWidth(index) + mDividerWidth); - } - - public int getY(final int index) { - final int row = mRowNumbers[index]; - return (mNumRows -1 - row) * mDefaultRowHeight + mTopPadding; - } - - public int getWidth(final int index) { - final int numColumnInRow = getNumColumnInRow(index); - return (mOccupiedWidth - mDividerWidth * (numColumnInRow - 1)) / numColumnInRow; - } - - public void markAsEdgeKey(final Key key, final int index) { - final int row = mRowNumbers[index]; - if (row == 0) - key.markAsBottomEdge(this); - if (row == mNumRows - 1) - key.markAsTopEdge(this); - - final int numColumnInRow = mNumColumnsInRow[row]; - final int column = getColumnNumber(index); - if (column == 0) - key.markAsLeftEdge(this); - if (column == numColumnInRow - 1) - key.markAsRightEdge(this); - } - } - - static boolean isIndexSubjectToAutoCorrection(final SuggestedWords suggestedWords, - final int index) { - return suggestedWords.mWillAutoCorrect && index == SuggestedWords.INDEX_OF_AUTO_CORRECTION; - } - - public static final class Builder extends KeyboardBuilder<MoreSuggestionsParam> { - private final MoreSuggestionsView mPaneView; - private SuggestedWords mSuggestedWords; - private int mFromIndex; - private int mToIndex; - - public Builder(final Context context, final MoreSuggestionsView paneView) { - super(context, new MoreSuggestionsParam()); - mPaneView = paneView; - } - - public Builder layout(final SuggestedWords suggestedWords, final int fromIndex, - final int maxWidth, final int minWidth, final int maxRow, - final Keyboard parentKeyboard) { - final int xmlId = R.xml.kbd_suggestions_pane_template; - load(xmlId, parentKeyboard.mId); - mParams.mVerticalGap = mParams.mTopPadding = parentKeyboard.mVerticalGap / 2; - mPaneView.updateKeyboardGeometry(mParams.mDefaultRowHeight); - final int count = mParams.layout(suggestedWords, fromIndex, maxWidth, minWidth, maxRow, - mPaneView.newLabelPaint(null /* key */), mResources); - mFromIndex = fromIndex; - mToIndex = fromIndex + count; - mSuggestedWords = suggestedWords; - return this; - } - - @Override - public MoreSuggestions build() { - final MoreSuggestionsParam params = mParams; - for (int index = mFromIndex; index < mToIndex; index++) { - final int x = params.getX(index); - final int y = params.getY(index); - final int width = params.getWidth(index); - final String word; - final String info; - if (isIndexSubjectToAutoCorrection(mSuggestedWords, index)) { - // INDEX_OF_AUTO_CORRECTION and INDEX_OF_TYPED_WORD got swapped. - word = mSuggestedWords.getLabel(SuggestedWords.INDEX_OF_TYPED_WORD); - info = mSuggestedWords.getDebugString(SuggestedWords.INDEX_OF_TYPED_WORD); - } else { - word = mSuggestedWords.getLabel(index); - info = mSuggestedWords.getDebugString(index); - } - final Key key = new MoreSuggestionKey(word, info, index, params); - params.markAsEdgeKey(key, index); - params.onAddKey(key); - final int columnNumber = params.getColumnNumber(index); - final int numColumnInRow = params.getNumColumnInRow(index); - if (columnNumber < numColumnInRow - 1) { - final Divider divider = new Divider(params, params.mDivider, x + width, y, - params.mDividerWidth, params.mDefaultRowHeight); - params.onAddKey(divider); - } - } - return new MoreSuggestions(params, mSuggestedWords); - } - } - - static final class MoreSuggestionKey extends Key { - public final int mSuggestedWordIndex; - - public MoreSuggestionKey(final String word, final String info, final int index, - final MoreSuggestionsParam params) { - super(word /* label */, KeyboardIconsSet.ICON_UNDEFINED, Constants.CODE_OUTPUT_TEXT, - word /* outputText */, info, 0 /* labelFlags */, Key.BACKGROUND_TYPE_NORMAL, - params.getX(index), params.getY(index), params.getWidth(index), - params.mDefaultRowHeight, params.mHorizontalGap, params.mVerticalGap); - mSuggestedWordIndex = index; - } - } - - private static final class Divider extends Key.Spacer { - private final Drawable mIcon; - - public Divider(final KeyboardParams params, final Drawable icon, final int x, - final int y, final int width, final int height) { - super(params, x, y, width, height); - mIcon = icon; - } - - @Override - public Drawable getIcon(final KeyboardIconsSet iconSet, final int alpha) { - // KeyboardIconsSet and alpha are unused. Use the icon that has been passed to the - // constructor. - // TODO: Drawable itself should have an alpha value. - mIcon.setAlpha(128); - return mIcon; - } - } -} diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java deleted file mode 100644 index 907e3fa42..000000000 --- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.suggestions; - -import android.content.Context; -import android.util.AttributeSet; -import android.util.Log; - -import com.android.inputmethod.keyboard.Key; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.KeyboardActionListener; -import com.android.inputmethod.keyboard.MoreKeysKeyboardView; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionKey; - -/** - * A view that renders a virtual {@link MoreSuggestions}. It handles rendering of keys and detecting - * key presses and touch movements. - */ -public final class MoreSuggestionsView extends MoreKeysKeyboardView { - private static final String TAG = MoreSuggestionsView.class.getSimpleName(); - - public static abstract class MoreSuggestionsListener extends KeyboardActionListener.Adapter { - public abstract void onSuggestionSelected(final SuggestedWordInfo info); - } - - private boolean mIsInModalMode; - - public MoreSuggestionsView(final Context context, final AttributeSet attrs) { - this(context, attrs, R.attr.moreKeysKeyboardViewStyle); - } - - public MoreSuggestionsView(final Context context, final AttributeSet attrs, - final int defStyle) { - super(context, attrs, defStyle); - } - - // TODO: Remove redundant override method. - @Override - public void setKeyboard(final Keyboard keyboard) { - super.setKeyboard(keyboard); - mIsInModalMode = false; - // With accessibility mode off, {@link #mAccessibilityDelegate} is set to null at the - // above {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. - // With accessibility mode on, {@link #mAccessibilityDelegate} is set to a - // {@link MoreKeysKeyboardAccessibilityDelegate} object at the above - // {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. - if (mAccessibilityDelegate != null) { - mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions); - mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions); - } - } - - @Override - protected int getDefaultCoordX() { - final MoreSuggestions pane = (MoreSuggestions)getKeyboard(); - return pane.mOccupiedWidth / 2; - } - - public void updateKeyboardGeometry(final int keyHeight) { - updateKeyDrawParams(keyHeight); - } - - public void setModalMode() { - mIsInModalMode = true; - // Set vertical correction to zero (Reset more keys keyboard sliding allowance - // {@link R#dimen.config_more_keys_keyboard_slide_allowance}). - mKeyDetector.setKeyboard(getKeyboard(), -getPaddingLeft(), -getPaddingTop()); - } - - public boolean isInModalMode() { - return mIsInModalMode; - } - - @Override - protected void onKeyInput(final Key key, final int x, final int y) { - if (!(key instanceof MoreSuggestionKey)) { - Log.e(TAG, "Expected key is MoreSuggestionKey, but found " - + key.getClass().getName()); - return; - } - final Keyboard keyboard = getKeyboard(); - if (!(keyboard instanceof MoreSuggestions)) { - Log.e(TAG, "Expected keyboard is MoreSuggestions, but found " - + keyboard.getClass().getName()); - return; - } - final SuggestedWords suggestedWords = ((MoreSuggestions)keyboard).mSuggestedWords; - final int index = ((MoreSuggestionKey)key).mSuggestedWordIndex; - if (index < 0 || index >= suggestedWords.size()) { - Log.e(TAG, "Selected suggestion has an illegal index: " + index); - return; - } - if (!(mListener instanceof MoreSuggestionsListener)) { - Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found " - + mListener.getClass().getName()); - return; - } - ((MoreSuggestionsListener)mListener).onSuggestionSelected(suggestedWords.getInfo(index)); - } -} diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java deleted file mode 100644 index 9577d0913..000000000 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java +++ /dev/null @@ -1,650 +0,0 @@ -/* - * Copyright (C) 2013 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.suggestions; - -import android.content.Context; -import android.content.res.Resources; -import android.content.res.TypedArray; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Paint.Align; -import android.graphics.Rect; -import android.graphics.Typeface; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.text.Spannable; -import android.text.SpannableString; -import android.text.Spanned; -import android.text.TextPaint; -import android.text.TextUtils; -import android.text.style.CharacterStyle; -import android.text.style.StyleSpan; -import android.text.style.UnderlineSpan; -import android.util.AttributeSet; -import android.view.Gravity; -import android.view.View; -import android.view.ViewGroup; -import android.widget.LinearLayout; -import android.widget.TextView; - -import com.android.inputmethod.accessibility.AccessibilityUtils; -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.PunctuationSuggestions; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.settings.Settings; -import com.android.inputmethod.latin.settings.SettingsValues; -import com.android.inputmethod.latin.utils.ResourceUtils; -import com.android.inputmethod.latin.utils.ViewLayoutUtils; - -import java.util.ArrayList; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -final class SuggestionStripLayoutHelper { - private static final int DEFAULT_SUGGESTIONS_COUNT_IN_STRIP = 3; - private static final float DEFAULT_CENTER_SUGGESTION_PERCENTILE = 0.40f; - private static final int DEFAULT_MAX_MORE_SUGGESTIONS_ROW = 2; - private static final int PUNCTUATIONS_IN_STRIP = 5; - private static final float MIN_TEXT_XSCALE = 0.70f; - - public final int mPadding; - public final int mDividerWidth; - public final int mSuggestionsStripHeight; - private final int mSuggestionsCountInStrip; - public final int mMoreSuggestionsRowHeight; - private int mMaxMoreSuggestionsRow; - public final float mMinMoreSuggestionsWidth; - public final int mMoreSuggestionsBottomGap; - private boolean mMoreSuggestionsAvailable; - - // The index of these {@link ArrayList} is the position in the suggestion strip. The indices - // increase towards the right for LTR scripts and the left for RTL scripts, starting with 0. - // The position of the most important suggestion is in {@link #mCenterPositionInStrip} - private final ArrayList<TextView> mWordViews; - private final ArrayList<View> mDividerViews; - private final ArrayList<TextView> mDebugInfoViews; - - private final int mColorValidTypedWord; - private final int mColorTypedWord; - private final int mColorAutoCorrect; - private final int mColorSuggested; - private final float mAlphaObsoleted; - private final float mCenterSuggestionWeight; - private final int mCenterPositionInStrip; - private final int mTypedWordPositionWhenAutocorrect; - private final Drawable mMoreSuggestionsHint; - private static final String MORE_SUGGESTIONS_HINT = "\u2026"; - - private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD); - private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); - - private final int mSuggestionStripOptions; - // These constants are the flag values of - // {@link R.styleable#SuggestionStripView_suggestionStripOptions} attribute. - private static final int AUTO_CORRECT_BOLD = 0x01; - private static final int AUTO_CORRECT_UNDERLINE = 0x02; - private static final int VALID_TYPED_WORD_BOLD = 0x04; - - public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs, - final int defStyle, final ArrayList<TextView> wordViews, - final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) { - mWordViews = wordViews; - mDividerViews = dividerViews; - mDebugInfoViews = debugInfoViews; - - final TextView wordView = wordViews.get(0); - final View dividerView = dividerViews.get(0); - mPadding = wordView.getCompoundPaddingLeft() + wordView.getCompoundPaddingRight(); - dividerView.measure( - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); - mDividerWidth = dividerView.getMeasuredWidth(); - - final Resources res = wordView.getResources(); - mSuggestionsStripHeight = res.getDimensionPixelSize( - R.dimen.config_suggestions_strip_height); - - final TypedArray a = context.obtainStyledAttributes(attrs, - R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripView); - mSuggestionStripOptions = a.getInt( - R.styleable.SuggestionStripView_suggestionStripOptions, 0); - mAlphaObsoleted = ResourceUtils.getFraction(a, - R.styleable.SuggestionStripView_alphaObsoleted, 1.0f); - mColorValidTypedWord = a.getColor(R.styleable.SuggestionStripView_colorValidTypedWord, 0); - mColorTypedWord = a.getColor(R.styleable.SuggestionStripView_colorTypedWord, 0); - mColorAutoCorrect = a.getColor(R.styleable.SuggestionStripView_colorAutoCorrect, 0); - mColorSuggested = a.getColor(R.styleable.SuggestionStripView_colorSuggested, 0); - mSuggestionsCountInStrip = a.getInt( - R.styleable.SuggestionStripView_suggestionsCountInStrip, - DEFAULT_SUGGESTIONS_COUNT_IN_STRIP); - mCenterSuggestionWeight = ResourceUtils.getFraction(a, - R.styleable.SuggestionStripView_centerSuggestionPercentile, - DEFAULT_CENTER_SUGGESTION_PERCENTILE); - mMaxMoreSuggestionsRow = a.getInt( - R.styleable.SuggestionStripView_maxMoreSuggestionsRow, - DEFAULT_MAX_MORE_SUGGESTIONS_ROW); - mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a, - R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f); - a.recycle(); - - mMoreSuggestionsHint = getMoreSuggestionsHint(res, - res.getDimension(R.dimen.config_more_suggestions_hint_text_size), - mColorAutoCorrect); - mCenterPositionInStrip = mSuggestionsCountInStrip / 2; - // Assuming there are at least three suggestions. Also, note that the suggestions are - // laid out according to script direction, so this is left of the center for LTR scripts - // and right of the center for RTL scripts. - mTypedWordPositionWhenAutocorrect = mCenterPositionInStrip - 1; - mMoreSuggestionsBottomGap = res.getDimensionPixelOffset( - R.dimen.config_more_suggestions_bottom_gap); - mMoreSuggestionsRowHeight = res.getDimensionPixelSize( - R.dimen.config_more_suggestions_row_height); - } - - public int getMaxMoreSuggestionsRow() { - return mMaxMoreSuggestionsRow; - } - - private int getMoreSuggestionsHeight() { - return mMaxMoreSuggestionsRow * mMoreSuggestionsRowHeight + mMoreSuggestionsBottomGap; - } - - public void setMoreSuggestionsHeight(final int remainingHeight) { - final int currentHeight = getMoreSuggestionsHeight(); - if (currentHeight <= remainingHeight) { - return; - } - - mMaxMoreSuggestionsRow = (remainingHeight - mMoreSuggestionsBottomGap) - / mMoreSuggestionsRowHeight; - } - - private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, - final int color) { - final Paint paint = new Paint(); - paint.setAntiAlias(true); - paint.setTextAlign(Align.CENTER); - paint.setTextSize(textSize); - paint.setColor(color); - final Rect bounds = new Rect(); - paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds); - final int width = Math.round(bounds.width() + 0.5f); - final int height = Math.round(bounds.height() + 0.5f); - final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888); - final Canvas canvas = new Canvas(buffer); - canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint); - BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer); - bitmapDrawable.setTargetDensity(canvas); - return bitmapDrawable; - } - - private CharSequence getStyledSuggestedWord(final SuggestedWords suggestedWords, - final int indexInSuggestedWords) { - if (indexInSuggestedWords >= suggestedWords.size()) { - return null; - } - final String word = suggestedWords.getLabel(indexInSuggestedWords); - // TODO: don't use the index to decide whether this is the auto-correction/typed word, as - // this is brittle - final boolean isAutoCorrection = suggestedWords.mWillAutoCorrect - && indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION; - final boolean isTypedWordValid = suggestedWords.mTypedWordValid - && indexInSuggestedWords == SuggestedWords.INDEX_OF_TYPED_WORD; - if (!isAutoCorrection && !isTypedWordValid) { - return word; - } - - final Spannable spannedWord = new SpannableString(word); - final int options = mSuggestionStripOptions; - if ((isAutoCorrection && (options & AUTO_CORRECT_BOLD) != 0) - || (isTypedWordValid && (options & VALID_TYPED_WORD_BOLD) != 0)) { - addStyleSpan(spannedWord, BOLD_SPAN); - } - if (isAutoCorrection && (options & AUTO_CORRECT_UNDERLINE) != 0) { - addStyleSpan(spannedWord, UNDERLINE_SPAN); - } - return spannedWord; - } - - /** - * Convert an index of {@link SuggestedWords} to position in the suggestion strip. - * @param indexInSuggestedWords the index of {@link SuggestedWords}. - * @param suggestedWords the suggested words list - * @return Non-negative integer of the position in the suggestion strip. - * Negative integer if the word of the index shouldn't be shown on the suggestion strip. - */ - private int getPositionInSuggestionStrip(final int indexInSuggestedWords, - final SuggestedWords suggestedWords) { - final SettingsValues settingsValues = Settings.getInstance().getCurrent(); - final boolean shouldOmitTypedWord = shouldOmitTypedWord(suggestedWords.mInputStyle, - settingsValues.mGestureFloatingPreviewTextEnabled, - settingsValues.mShouldShowLxxSuggestionUi); - return getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords.mWillAutoCorrect, - settingsValues.mShouldShowLxxSuggestionUi && shouldOmitTypedWord, - mCenterPositionInStrip, mTypedWordPositionWhenAutocorrect); - } - - @UsedForTesting - static boolean shouldOmitTypedWord(final int inputStyle, - final boolean gestureFloatingPreviewTextEnabled, - final boolean shouldShowUiToAcceptTypedWord) { - final boolean omitTypedWord = (inputStyle == SuggestedWords.INPUT_STYLE_TYPING) - || (inputStyle == SuggestedWords.INPUT_STYLE_TAIL_BATCH) - || (inputStyle == SuggestedWords.INPUT_STYLE_UPDATE_BATCH - && gestureFloatingPreviewTextEnabled); - return shouldShowUiToAcceptTypedWord && omitTypedWord; - } - - @UsedForTesting - static int getPositionInSuggestionStrip(final int indexInSuggestedWords, - final boolean willAutoCorrect, final boolean omitTypedWord, - final int centerPositionInStrip, final int typedWordPositionWhenAutoCorrect) { - if (omitTypedWord) { - if (indexInSuggestedWords == SuggestedWords.INDEX_OF_TYPED_WORD) { - // Ignore. - return -1; - } - if (indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION) { - // Center in the suggestion strip. - return centerPositionInStrip; - } - // If neither of those, the order in the suggestion strip is left of the center first - // then right of the center, to both edges of the suggestion strip. - // For example, center-1, center+1, center-2, center+2, and so on. - final int n = indexInSuggestedWords; - final int offsetFromCenter = (n % 2) == 0 ? -(n / 2) : (n / 2); - final int positionInSuggestionStrip = centerPositionInStrip + offsetFromCenter; - return positionInSuggestionStrip; - } - final int indexToDisplayMostImportantSuggestion; - final int indexToDisplaySecondMostImportantSuggestion; - if (willAutoCorrect) { - indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION; - indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD; - } else { - indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD; - indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION; - } - if (indexInSuggestedWords == indexToDisplayMostImportantSuggestion) { - // Center in the suggestion strip. - return centerPositionInStrip; - } - if (indexInSuggestedWords == indexToDisplaySecondMostImportantSuggestion) { - // Center-1. - return typedWordPositionWhenAutoCorrect; - } - // If neither of those, the order in the suggestion strip is right of the center first - // then left of the center, to both edges of the suggestion strip. - // For example, Center+1, center-2, center+2, center-3, and so on. - final int n = indexInSuggestedWords + 1; - final int offsetFromCenter = (n % 2) == 0 ? -(n / 2) : (n / 2); - final int positionInSuggestionStrip = centerPositionInStrip + offsetFromCenter; - return positionInSuggestionStrip; - } - - private int getSuggestionTextColor(final SuggestedWords suggestedWords, - final int indexInSuggestedWords) { - // Use identity for strings, not #equals : it's the typed word if it's the same object - final boolean isTypedWord = suggestedWords.getInfo(indexInSuggestedWords).isKindOf( - SuggestedWordInfo.KIND_TYPED); - - final int color; - if (indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION - && suggestedWords.mWillAutoCorrect) { - color = mColorAutoCorrect; - } else if (isTypedWord && suggestedWords.mTypedWordValid) { - color = mColorValidTypedWord; - } else if (isTypedWord) { - color = mColorTypedWord; - } else { - color = mColorSuggested; - } - if (suggestedWords.mIsObsoleteSuggestions && !isTypedWord) { - return applyAlpha(color, mAlphaObsoleted); - } - return color; - } - - private static int applyAlpha(final int color, final float alpha) { - final int newAlpha = (int)(Color.alpha(color) * alpha); - return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color)); - } - - private static void addDivider(final ViewGroup stripView, final View dividerView) { - stripView.addView(dividerView); - final LinearLayout.LayoutParams params = - (LinearLayout.LayoutParams)dividerView.getLayoutParams(); - params.gravity = Gravity.CENTER; - } - - /** - * Layout suggestions to the suggestions strip. And returns the start index of more - * suggestions. - * - * @param suggestedWords suggestions to be shown in the suggestions strip. - * @param stripView the suggestions strip view. - * @param placerView the view where the debug info will be placed. - * @return the start index of more suggestions. - */ - public int layoutAndReturnStartIndexOfMoreSuggestions( - final Context context, - final SuggestedWords suggestedWords, - final ViewGroup stripView, - final ViewGroup placerView) { - if (suggestedWords.isPunctuationSuggestions()) { - return layoutPunctuationsAndReturnStartIndexOfMoreSuggestions( - (PunctuationSuggestions)suggestedWords, stripView); - } - - final int wordCountToShow = suggestedWords.getWordCountToShow( - Settings.getInstance().getCurrent().mShouldShowLxxSuggestionUi); - final int startIndexOfMoreSuggestions = setupWordViewsAndReturnStartIndexOfMoreSuggestions( - suggestedWords, mSuggestionsCountInStrip); - final TextView centerWordView = mWordViews.get(mCenterPositionInStrip); - final int stripWidth = stripView.getWidth(); - final int centerWidth = getSuggestionWidth(mCenterPositionInStrip, stripWidth); - if (wordCountToShow == 1 || getTextScaleX(centerWordView.getText(), centerWidth, - centerWordView.getPaint()) < MIN_TEXT_XSCALE) { - // Layout only the most relevant suggested word at the center of the suggestion strip - // by consolidating all slots in the strip. - final int countInStrip = 1; - mMoreSuggestionsAvailable = (wordCountToShow > countInStrip); - layoutWord(context, mCenterPositionInStrip, stripWidth - mPadding); - stripView.addView(centerWordView); - setLayoutWeight(centerWordView, 1.0f, ViewGroup.LayoutParams.MATCH_PARENT); - if (SuggestionStripView.DBG) { - layoutDebugInfo(mCenterPositionInStrip, placerView, stripWidth); - } - final Integer lastIndex = (Integer)centerWordView.getTag(); - return (lastIndex == null ? 0 : lastIndex) + 1; - } - - final int countInStrip = mSuggestionsCountInStrip; - mMoreSuggestionsAvailable = (wordCountToShow > countInStrip); - @SuppressWarnings("unused") - int x = 0; - for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) { - if (positionInStrip != 0) { - final View divider = mDividerViews.get(positionInStrip); - // Add divider if this isn't the left most suggestion in suggestions strip. - addDivider(stripView, divider); - x += divider.getMeasuredWidth(); - } - - final int width = getSuggestionWidth(positionInStrip, stripWidth); - final TextView wordView = layoutWord(context, positionInStrip, width); - stripView.addView(wordView); - setLayoutWeight(wordView, getSuggestionWeight(positionInStrip), - ViewGroup.LayoutParams.MATCH_PARENT); - x += wordView.getMeasuredWidth(); - - if (SuggestionStripView.DBG) { - layoutDebugInfo(positionInStrip, placerView, x); - } - } - return startIndexOfMoreSuggestions; - } - - /** - * Format appropriately the suggested word in {@link #mWordViews} specified by - * <code>positionInStrip</code>. When the suggested word doesn't exist, the corresponding - * {@link TextView} will be disabled and never respond to user interaction. The suggested word - * may be shrunk or ellipsized to fit in the specified width. - * - * The <code>positionInStrip</code> argument is the index in the suggestion strip. The indices - * increase towards the right for LTR scripts and the left for RTL scripts, starting with 0. - * The position of the most important suggestion is in {@link #mCenterPositionInStrip}. This - * usually doesn't match the index in <code>suggedtedWords</code> -- see - * {@link #getPositionInSuggestionStrip(int,SuggestedWords)}. - * - * @param positionInStrip the position in the suggestion strip. - * @param width the maximum width for layout in pixels. - * @return the {@link TextView} containing the suggested word appropriately formatted. - */ - private TextView layoutWord(final Context context, final int positionInStrip, final int width) { - final TextView wordView = mWordViews.get(positionInStrip); - final CharSequence word = wordView.getText(); - if (positionInStrip == mCenterPositionInStrip && mMoreSuggestionsAvailable) { - // TODO: This "more suggestions hint" should have a nicely designed icon. - wordView.setCompoundDrawablesWithIntrinsicBounds( - null, null, null, mMoreSuggestionsHint); - // HACK: Align with other TextViews that have no compound drawables. - wordView.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight()); - } else { - wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); - } - // {@link StyleSpan} in a content description may cause an issue of TTS/TalkBack. - // Use a simple {@link String} to avoid the issue. - wordView.setContentDescription( - TextUtils.isEmpty(word) - ? context.getResources().getString(R.string.spoken_empty_suggestion) - : word.toString()); - final CharSequence text = getEllipsizedTextWithSettingScaleX( - word, width, wordView.getPaint()); - final float scaleX = wordView.getTextScaleX(); - wordView.setText(text); // TextView.setText() resets text scale x to 1.0. - wordView.setTextScaleX(scaleX); - // A <code>wordView</code> should be disabled when <code>word</code> is empty in order to - // make it unclickable. - // With accessibility touch exploration on, <code>wordView</code> should be enabled even - // when it is empty to avoid announcing as "disabled". - wordView.setEnabled(!TextUtils.isEmpty(word) - || AccessibilityUtils.getInstance().isTouchExplorationEnabled()); - return wordView; - } - - private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView, - final int x) { - final TextView debugInfoView = mDebugInfoViews.get(positionInStrip); - final CharSequence debugInfo = debugInfoView.getText(); - if (debugInfo == null) { - return; - } - placerView.addView(debugInfoView); - debugInfoView.measure( - ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - final int infoWidth = debugInfoView.getMeasuredWidth(); - final int y = debugInfoView.getMeasuredHeight(); - ViewLayoutUtils.placeViewAt( - debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight()); - } - - private int getSuggestionWidth(final int positionInStrip, final int maxWidth) { - final int paddings = mPadding * mSuggestionsCountInStrip; - final int dividers = mDividerWidth * (mSuggestionsCountInStrip - 1); - final int availableWidth = maxWidth - paddings - dividers; - return (int)(availableWidth * getSuggestionWeight(positionInStrip)); - } - - private float getSuggestionWeight(final int positionInStrip) { - if (positionInStrip == mCenterPositionInStrip) { - return mCenterSuggestionWeight; - } - // TODO: Revisit this for cases of 5 or more suggestions - return (1.0f - mCenterSuggestionWeight) / (mSuggestionsCountInStrip - 1); - } - - private int setupWordViewsAndReturnStartIndexOfMoreSuggestions( - final SuggestedWords suggestedWords, final int maxSuggestionInStrip) { - // Clear all suggestions first - for (int positionInStrip = 0; positionInStrip < maxSuggestionInStrip; ++positionInStrip) { - final TextView wordView = mWordViews.get(positionInStrip); - wordView.setText(null); - wordView.setTag(null); - // Make this inactive for touches in {@link #layoutWord(int,int)}. - if (SuggestionStripView.DBG) { - mDebugInfoViews.get(positionInStrip).setText(null); - } - } - int count = 0; - int indexInSuggestedWords; - for (indexInSuggestedWords = 0; indexInSuggestedWords < suggestedWords.size() - && count < maxSuggestionInStrip; indexInSuggestedWords++) { - final int positionInStrip = - getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords); - if (positionInStrip < 0) { - continue; - } - final TextView wordView = mWordViews.get(positionInStrip); - // {@link TextView#getTag()} is used to get the index in suggestedWords at - // {@link SuggestionStripView#onClick(View)}. - wordView.setTag(indexInSuggestedWords); - wordView.setText(getStyledSuggestedWord(suggestedWords, indexInSuggestedWords)); - wordView.setTextColor(getSuggestionTextColor(suggestedWords, indexInSuggestedWords)); - if (SuggestionStripView.DBG) { - mDebugInfoViews.get(positionInStrip).setText( - suggestedWords.getDebugString(indexInSuggestedWords)); - } - count++; - } - return indexInSuggestedWords; - } - - private int layoutPunctuationsAndReturnStartIndexOfMoreSuggestions( - final PunctuationSuggestions punctuationSuggestions, final ViewGroup stripView) { - final int countInStrip = Math.min(punctuationSuggestions.size(), PUNCTUATIONS_IN_STRIP); - for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) { - if (positionInStrip != 0) { - // Add divider if this isn't the left most suggestion in suggestions strip. - addDivider(stripView, mDividerViews.get(positionInStrip)); - } - - final TextView wordView = mWordViews.get(positionInStrip); - final String punctuation = punctuationSuggestions.getLabel(positionInStrip); - // {@link TextView#getTag()} is used to get the index in suggestedWords at - // {@link SuggestionStripView#onClick(View)}. - wordView.setTag(positionInStrip); - wordView.setText(punctuation); - wordView.setContentDescription(punctuation); - wordView.setTextScaleX(1.0f); - wordView.setCompoundDrawables(null, null, null, null); - wordView.setTextColor(mColorAutoCorrect); - stripView.addView(wordView); - setLayoutWeight(wordView, 1.0f, mSuggestionsStripHeight); - } - mMoreSuggestionsAvailable = (punctuationSuggestions.size() > countInStrip); - return countInStrip; - } - - public void layoutImportantNotice(final View importantNoticeStrip, - final String importantNoticeTitle) { - final TextView titleView = (TextView)importantNoticeStrip.findViewById( - R.id.important_notice_title); - final int width = titleView.getWidth() - titleView.getPaddingLeft() - - titleView.getPaddingRight(); - titleView.setTextColor(mColorAutoCorrect); - titleView.setText(importantNoticeTitle); // TextView.setText() resets text scale x to 1.0. - final float titleScaleX = getTextScaleX(importantNoticeTitle, width, titleView.getPaint()); - titleView.setTextScaleX(titleScaleX); - } - - static void setLayoutWeight(final View v, final float weight, final int height) { - final ViewGroup.LayoutParams lp = v.getLayoutParams(); - if (lp instanceof LinearLayout.LayoutParams) { - final LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)lp; - llp.weight = weight; - llp.width = 0; - llp.height = height; - } - } - - private static float getTextScaleX(@Nullable final CharSequence text, final int maxWidth, - final TextPaint paint) { - paint.setTextScaleX(1.0f); - final int width = getTextWidth(text, paint); - if (width <= maxWidth || maxWidth <= 0) { - return 1.0f; - } - return maxWidth / (float) width; - } - - @Nullable - private static CharSequence getEllipsizedTextWithSettingScaleX( - @Nullable final CharSequence text, final int maxWidth, @Nonnull final TextPaint paint) { - if (text == null) { - return null; - } - final float scaleX = getTextScaleX(text, maxWidth, paint); - if (scaleX >= MIN_TEXT_XSCALE) { - paint.setTextScaleX(scaleX); - return text; - } - - // <code>text</code> must be ellipsized with minimum text scale x. - paint.setTextScaleX(MIN_TEXT_XSCALE); - final boolean hasBoldStyle = hasStyleSpan(text, BOLD_SPAN); - final boolean hasUnderlineStyle = hasStyleSpan(text, UNDERLINE_SPAN); - // TextUtils.ellipsize erases any span object existed after ellipsized point. - // We have to restore these spans afterward. - final CharSequence ellipsizedText = TextUtils.ellipsize( - text, paint, maxWidth, TextUtils.TruncateAt.MIDDLE); - if (!hasBoldStyle && !hasUnderlineStyle) { - return ellipsizedText; - } - final Spannable spannableText = (ellipsizedText instanceof Spannable) - ? (Spannable)ellipsizedText : new SpannableString(ellipsizedText); - if (hasBoldStyle) { - addStyleSpan(spannableText, BOLD_SPAN); - } - if (hasUnderlineStyle) { - addStyleSpan(spannableText, UNDERLINE_SPAN); - } - return spannableText; - } - - private static boolean hasStyleSpan(@Nullable final CharSequence text, - final CharacterStyle style) { - if (text instanceof Spanned) { - return ((Spanned)text).getSpanStart(style) >= 0; - } - return false; - } - - private static void addStyleSpan(@Nonnull final Spannable text, final CharacterStyle style) { - text.removeSpan(style); - text.setSpan(style, 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - } - - private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) { - if (TextUtils.isEmpty(text)) { - return 0; - } - final int length = text.length(); - final float[] widths = new float[length]; - final int count; - final Typeface savedTypeface = paint.getTypeface(); - try { - paint.setTypeface(getTextTypeface(text)); - count = paint.getTextWidths(text, 0, length, widths); - } finally { - paint.setTypeface(savedTypeface); - } - int width = 0; - for (int i = 0; i < count; i++) { - width += Math.round(widths[i] + 0.5f); - } - return width; - } - - private static Typeface getTextTypeface(@Nullable final CharSequence text) { - return hasStyleSpan(text, BOLD_SPAN) ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT; - } -} diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java deleted file mode 100644 index 5dba4928b..000000000 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java +++ /dev/null @@ -1,491 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.suggestions; - -import android.content.Context; -import android.content.res.Resources; -import android.content.res.TypedArray; -import android.graphics.Color; -import android.graphics.drawable.Drawable; -import androidx.core.view.ViewCompat; -import android.text.TextUtils; -import android.util.AttributeSet; -import android.util.TypedValue; -import android.view.GestureDetector; -import android.view.LayoutInflater; -import android.view.MotionEvent; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.View.OnLongClickListener; -import android.view.ViewGroup; -import android.view.ViewParent; -import android.view.accessibility.AccessibilityEvent; -import android.widget.ImageButton; -import android.widget.RelativeLayout; -import android.widget.TextView; - -import com.android.inputmethod.accessibility.AccessibilityUtils; -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.MainKeyboardView; -import com.android.inputmethod.keyboard.MoreKeysPanel; -import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.define.DebugFlags; -import com.android.inputmethod.latin.settings.Settings; -import com.android.inputmethod.latin.settings.SettingsValues; -import com.android.inputmethod.latin.suggestions.MoreSuggestionsView.MoreSuggestionsListener; -import com.android.inputmethod.latin.utils.ImportantNoticeUtils; - -import java.util.ArrayList; - -public final class SuggestionStripView extends RelativeLayout implements OnClickListener, - OnLongClickListener { - public interface Listener { - public void showImportantNoticeContents(); - public void pickSuggestionManually(SuggestedWordInfo word); - public void onCodeInput(int primaryCode, int x, int y, boolean isKeyRepeat); - } - - static final boolean DBG = DebugFlags.DEBUG_ENABLED; - private static final float DEBUG_INFO_TEXT_SIZE_IN_DIP = 6.0f; - - private final ViewGroup mSuggestionsStrip; - private final ImageButton mVoiceKey; - private final View mImportantNoticeStrip; - MainKeyboardView mMainKeyboardView; - - private final View mMoreSuggestionsContainer; - private final MoreSuggestionsView mMoreSuggestionsView; - private final MoreSuggestions.Builder mMoreSuggestionsBuilder; - - private final ArrayList<TextView> mWordViews = new ArrayList<>(); - private final ArrayList<TextView> mDebugInfoViews = new ArrayList<>(); - private final ArrayList<View> mDividerViews = new ArrayList<>(); - - Listener mListener; - private SuggestedWords mSuggestedWords = SuggestedWords.getEmptyInstance(); - private int mStartIndexOfMoreSuggestions; - - private final SuggestionStripLayoutHelper mLayoutHelper; - private final StripVisibilityGroup mStripVisibilityGroup; - - private static class StripVisibilityGroup { - private final View mSuggestionStripView; - private final View mSuggestionsStrip; - private final View mImportantNoticeStrip; - - public StripVisibilityGroup(final View suggestionStripView, - final ViewGroup suggestionsStrip, final View importantNoticeStrip) { - mSuggestionStripView = suggestionStripView; - mSuggestionsStrip = suggestionsStrip; - mImportantNoticeStrip = importantNoticeStrip; - showSuggestionsStrip(); - } - - public void setLayoutDirection(final boolean isRtlLanguage) { - final int layoutDirection = isRtlLanguage ? ViewCompat.LAYOUT_DIRECTION_RTL - : ViewCompat.LAYOUT_DIRECTION_LTR; - ViewCompat.setLayoutDirection(mSuggestionStripView, layoutDirection); - ViewCompat.setLayoutDirection(mSuggestionsStrip, layoutDirection); - ViewCompat.setLayoutDirection(mImportantNoticeStrip, layoutDirection); - } - - public void showSuggestionsStrip() { - mSuggestionsStrip.setVisibility(VISIBLE); - mImportantNoticeStrip.setVisibility(INVISIBLE); - } - - public void showImportantNoticeStrip() { - mSuggestionsStrip.setVisibility(INVISIBLE); - mImportantNoticeStrip.setVisibility(VISIBLE); - } - - public boolean isShowingImportantNoticeStrip() { - return mImportantNoticeStrip.getVisibility() == VISIBLE; - } - } - - /** - * Construct a {@link SuggestionStripView} for showing suggestions to be picked by the user. - * @param context - * @param attrs - */ - public SuggestionStripView(final Context context, final AttributeSet attrs) { - this(context, attrs, R.attr.suggestionStripViewStyle); - } - - public SuggestionStripView(final Context context, final AttributeSet attrs, - final int defStyle) { - super(context, attrs, defStyle); - - final LayoutInflater inflater = LayoutInflater.from(context); - inflater.inflate(R.layout.suggestions_strip, this); - - mSuggestionsStrip = (ViewGroup)findViewById(R.id.suggestions_strip); - mVoiceKey = (ImageButton)findViewById(R.id.suggestions_strip_voice_key); - mImportantNoticeStrip = findViewById(R.id.important_notice_strip); - mStripVisibilityGroup = new StripVisibilityGroup(this, mSuggestionsStrip, - mImportantNoticeStrip); - - for (int pos = 0; pos < SuggestedWords.MAX_SUGGESTIONS; pos++) { - final TextView word = new TextView(context, null, R.attr.suggestionWordStyle); - word.setContentDescription(getResources().getString(R.string.spoken_empty_suggestion)); - word.setOnClickListener(this); - word.setOnLongClickListener(this); - mWordViews.add(word); - final View divider = inflater.inflate(R.layout.suggestion_divider, null); - mDividerViews.add(divider); - final TextView info = new TextView(context, null, R.attr.suggestionWordStyle); - info.setTextColor(Color.WHITE); - info.setTextSize(TypedValue.COMPLEX_UNIT_DIP, DEBUG_INFO_TEXT_SIZE_IN_DIP); - mDebugInfoViews.add(info); - } - - mLayoutHelper = new SuggestionStripLayoutHelper( - context, attrs, defStyle, mWordViews, mDividerViews, mDebugInfoViews); - - mMoreSuggestionsContainer = inflater.inflate(R.layout.more_suggestions, null); - mMoreSuggestionsView = (MoreSuggestionsView)mMoreSuggestionsContainer - .findViewById(R.id.more_suggestions_view); - mMoreSuggestionsBuilder = new MoreSuggestions.Builder(context, mMoreSuggestionsView); - - final Resources res = context.getResources(); - mMoreSuggestionsModalTolerance = res.getDimensionPixelOffset( - R.dimen.config_more_suggestions_modal_tolerance); - mMoreSuggestionsSlidingDetector = new GestureDetector( - context, mMoreSuggestionsSlidingListener); - - final TypedArray keyboardAttr = context.obtainStyledAttributes(attrs, - R.styleable.Keyboard, defStyle, R.style.SuggestionStripView); - final Drawable iconVoice = keyboardAttr.getDrawable(R.styleable.Keyboard_iconShortcutKey); - keyboardAttr.recycle(); - mVoiceKey.setImageDrawable(iconVoice); - mVoiceKey.setOnClickListener(this); - } - - /** - * A connection back to the input method. - * @param listener - */ - public void setListener(final Listener listener, final View inputView) { - mListener = listener; - mMainKeyboardView = (MainKeyboardView)inputView.findViewById(R.id.keyboard_view); - } - - public void updateVisibility(final boolean shouldBeVisible, final boolean isFullscreenMode) { - final int visibility = shouldBeVisible ? VISIBLE : (isFullscreenMode ? GONE : INVISIBLE); - setVisibility(visibility); - final SettingsValues currentSettingsValues = Settings.getInstance().getCurrent(); - mVoiceKey.setVisibility(currentSettingsValues.mShowsVoiceInputKey ? VISIBLE : INVISIBLE); - } - - public void setSuggestions(final SuggestedWords suggestedWords, final boolean isRtlLanguage) { - clear(); - mStripVisibilityGroup.setLayoutDirection(isRtlLanguage); - mSuggestedWords = suggestedWords; - mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( - getContext(), mSuggestedWords, mSuggestionsStrip, this); - mStripVisibilityGroup.showSuggestionsStrip(); - } - - public void setMoreSuggestionsHeight(final int remainingHeight) { - mLayoutHelper.setMoreSuggestionsHeight(remainingHeight); - } - - // This method checks if we should show the important notice (checks on permanent storage if - // it has been shown once already or not, and if in the setup wizard). If applicable, it shows - // the notice. In all cases, it returns true if it was shown, false otherwise. - public boolean maybeShowImportantNoticeTitle() { - final SettingsValues currentSettingsValues = Settings.getInstance().getCurrent(); - if (!ImportantNoticeUtils.shouldShowImportantNotice(getContext(), currentSettingsValues)) { - return false; - } - if (getWidth() <= 0) { - return false; - } - final String importantNoticeTitle = ImportantNoticeUtils.getSuggestContactsNoticeTitle( - getContext()); - if (TextUtils.isEmpty(importantNoticeTitle)) { - return false; - } - if (isShowingMoreSuggestionPanel()) { - dismissMoreSuggestionsPanel(); - } - mLayoutHelper.layoutImportantNotice(mImportantNoticeStrip, importantNoticeTitle); - mStripVisibilityGroup.showImportantNoticeStrip(); - mImportantNoticeStrip.setOnClickListener(this); - return true; - } - - public void clear() { - mSuggestionsStrip.removeAllViews(); - removeAllDebugInfoViews(); - mStripVisibilityGroup.showSuggestionsStrip(); - dismissMoreSuggestionsPanel(); - } - - private void removeAllDebugInfoViews() { - // The debug info views may be placed as children views of this {@link SuggestionStripView}. - for (final View debugInfoView : mDebugInfoViews) { - final ViewParent parent = debugInfoView.getParent(); - if (parent instanceof ViewGroup) { - ((ViewGroup)parent).removeView(debugInfoView); - } - } - } - - private final MoreSuggestionsListener mMoreSuggestionsListener = new MoreSuggestionsListener() { - @Override - public void onSuggestionSelected(final SuggestedWordInfo wordInfo) { - mListener.pickSuggestionManually(wordInfo); - dismissMoreSuggestionsPanel(); - } - - @Override - public void onCancelInput() { - dismissMoreSuggestionsPanel(); - } - }; - - private final MoreKeysPanel.Controller mMoreSuggestionsController = - new MoreKeysPanel.Controller() { - @Override - public void onDismissMoreKeysPanel() { - mMainKeyboardView.onDismissMoreKeysPanel(); - } - - @Override - public void onShowMoreKeysPanel(final MoreKeysPanel panel) { - mMainKeyboardView.onShowMoreKeysPanel(panel); - } - - @Override - public void onCancelMoreKeysPanel() { - dismissMoreSuggestionsPanel(); - } - }; - - public boolean isShowingMoreSuggestionPanel() { - return mMoreSuggestionsView.isShowingInParent(); - } - - public void dismissMoreSuggestionsPanel() { - mMoreSuggestionsView.dismissMoreKeysPanel(); - } - - @Override - public boolean onLongClick(final View view) { - AudioAndHapticFeedbackManager.getInstance().performHapticAndAudioFeedback( - Constants.NOT_A_CODE, this); - return showMoreSuggestions(); - } - - boolean showMoreSuggestions() { - final Keyboard parentKeyboard = mMainKeyboardView.getKeyboard(); - if (parentKeyboard == null) { - return false; - } - final SuggestionStripLayoutHelper layoutHelper = mLayoutHelper; - if (mSuggestedWords.size() <= mStartIndexOfMoreSuggestions) { - return false; - } - final int stripWidth = getWidth(); - final View container = mMoreSuggestionsContainer; - final int maxWidth = stripWidth - container.getPaddingLeft() - container.getPaddingRight(); - final MoreSuggestions.Builder builder = mMoreSuggestionsBuilder; - builder.layout(mSuggestedWords, mStartIndexOfMoreSuggestions, maxWidth, - (int)(maxWidth * layoutHelper.mMinMoreSuggestionsWidth), - layoutHelper.getMaxMoreSuggestionsRow(), parentKeyboard); - mMoreSuggestionsView.setKeyboard(builder.build()); - container.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - - final MoreKeysPanel moreKeysPanel = mMoreSuggestionsView; - final int pointX = stripWidth / 2; - final int pointY = -layoutHelper.mMoreSuggestionsBottomGap; - moreKeysPanel.showMoreKeysPanel(this, mMoreSuggestionsController, pointX, pointY, - mMoreSuggestionsListener); - mOriginX = mLastX; - mOriginY = mLastY; - for (int i = 0; i < mStartIndexOfMoreSuggestions; i++) { - mWordViews.get(i).setPressed(false); - } - return true; - } - - // Working variables for {@link onInterceptTouchEvent(MotionEvent)} and - // {@link onTouchEvent(MotionEvent)}. - private int mLastX; - private int mLastY; - private int mOriginX; - private int mOriginY; - private final int mMoreSuggestionsModalTolerance; - private boolean mNeedsToTransformTouchEventToHoverEvent; - private boolean mIsDispatchingHoverEventToMoreSuggestions; - private final GestureDetector mMoreSuggestionsSlidingDetector; - private final GestureDetector.OnGestureListener mMoreSuggestionsSlidingListener = - new GestureDetector.SimpleOnGestureListener() { - @Override - public boolean onScroll(MotionEvent down, MotionEvent me, float deltaX, float deltaY) { - if (down == null) { - return false; - } - final float dy = me.getY() - down.getY(); - if (deltaY > 0 && dy < 0) { - return showMoreSuggestions(); - } - return false; - } - }; - - @Override - public boolean onInterceptTouchEvent(final MotionEvent me) { - if (mStripVisibilityGroup.isShowingImportantNoticeStrip()) { - return false; - } - // Detecting sliding up finger to show {@link MoreSuggestionsView}. - if (!mMoreSuggestionsView.isShowingInParent()) { - mLastX = (int)me.getX(); - mLastY = (int)me.getY(); - return mMoreSuggestionsSlidingDetector.onTouchEvent(me); - } - if (mMoreSuggestionsView.isInModalMode()) { - return false; - } - - final int action = me.getAction(); - final int index = me.getActionIndex(); - final int x = (int)me.getX(index); - final int y = (int)me.getY(index); - if (Math.abs(x - mOriginX) >= mMoreSuggestionsModalTolerance - || mOriginY - y >= mMoreSuggestionsModalTolerance) { - // Decided to be in the sliding suggestion mode only when the touch point has been moved - // upward. Further {@link MotionEvent}s will be delivered to - // {@link #onTouchEvent(MotionEvent)}. - mNeedsToTransformTouchEventToHoverEvent = - AccessibilityUtils.getInstance().isTouchExplorationEnabled(); - mIsDispatchingHoverEventToMoreSuggestions = false; - return true; - } - - if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { - // Decided to be in the modal input mode. - mMoreSuggestionsView.setModalMode(); - } - return false; - } - - @Override - public boolean dispatchPopulateAccessibilityEvent(final AccessibilityEvent event) { - // Don't populate accessibility event with suggested words and voice key. - return true; - } - - @Override - public boolean onTouchEvent(final MotionEvent me) { - if (!mMoreSuggestionsView.isShowingInParent()) { - // Ignore any touch event while more suggestions panel hasn't been shown. - // Detecting sliding up is done at {@link #onInterceptTouchEvent}. - return true; - } - // In the sliding input mode. {@link MotionEvent} should be forwarded to - // {@link MoreSuggestionsView}. - final int index = me.getActionIndex(); - final int x = mMoreSuggestionsView.translateX((int)me.getX(index)); - final int y = mMoreSuggestionsView.translateY((int)me.getY(index)); - me.setLocation(x, y); - if (!mNeedsToTransformTouchEventToHoverEvent) { - mMoreSuggestionsView.onTouchEvent(me); - return true; - } - // In sliding suggestion mode with accessibility mode on, a touch event should be - // transformed to a hover event. - final int width = mMoreSuggestionsView.getWidth(); - final int height = mMoreSuggestionsView.getHeight(); - final boolean onMoreSuggestions = (x >= 0 && x < width && y >= 0 && y < height); - if (!onMoreSuggestions && !mIsDispatchingHoverEventToMoreSuggestions) { - // Just drop this touch event because dispatching hover event isn't started yet and - // the touch event isn't on {@link MoreSuggestionsView}. - return true; - } - final int hoverAction; - if (onMoreSuggestions && !mIsDispatchingHoverEventToMoreSuggestions) { - // Transform this touch event to a hover enter event and start dispatching a hover - // event to {@link MoreSuggestionsView}. - mIsDispatchingHoverEventToMoreSuggestions = true; - hoverAction = MotionEvent.ACTION_HOVER_ENTER; - } else if (me.getActionMasked() == MotionEvent.ACTION_UP) { - // Transform this touch event to a hover exit event and stop dispatching a hover event - // after this. - mIsDispatchingHoverEventToMoreSuggestions = false; - mNeedsToTransformTouchEventToHoverEvent = false; - hoverAction = MotionEvent.ACTION_HOVER_EXIT; - } else { - // Transform this touch event to a hover move event. - hoverAction = MotionEvent.ACTION_HOVER_MOVE; - } - me.setAction(hoverAction); - mMoreSuggestionsView.onHoverEvent(me); - return true; - } - - @Override - public void onClick(final View view) { - AudioAndHapticFeedbackManager.getInstance().performHapticAndAudioFeedback( - Constants.CODE_UNSPECIFIED, this); - if (view == mImportantNoticeStrip) { - mListener.showImportantNoticeContents(); - return; - } - if (view == mVoiceKey) { - mListener.onCodeInput(Constants.CODE_SHORTCUT, - Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE, - false /* isKeyRepeat */); - return; - } - - final Object tag = view.getTag(); - // {@link Integer} tag is set at - // {@link SuggestionStripLayoutHelper#setupWordViewsTextAndColor(SuggestedWords,int)} and - // {@link SuggestionStripLayoutHelper#layoutPunctuationSuggestions(SuggestedWords,ViewGroup} - if (tag instanceof Integer) { - final int index = (Integer) tag; - if (index >= mSuggestedWords.size()) { - return; - } - final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(index); - mListener.pickSuggestionManually(wordInfo); - } - } - - @Override - protected void onDetachedFromWindow() { - super.onDetachedFromWindow(); - dismissMoreSuggestionsPanel(); - } - - @Override - protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) { - // Called by the framework when the size is known. Show the important notice if applicable. - // This may be overriden by showing suggestions later, if applicable. - if (oldw <= 0 && w > 0) { - maybeShowImportantNoticeTitle(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripViewAccessor.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripViewAccessor.java deleted file mode 100644 index 68f417e84..000000000 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripViewAccessor.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2014 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.suggestions; - -import com.android.inputmethod.latin.SuggestedWords; - -/** - * An object that gives basic control of a suggestion strip and some info on it. - */ -public interface SuggestionStripViewAccessor { - public void setNeutralSuggestionStrip(); - public void showSuggestionStrip(final SuggestedWords suggestedWords); -} diff --git a/java/src/com/android/inputmethod/latin/touchinputconsumer/GestureConsumer.java b/java/src/com/android/inputmethod/latin/touchinputconsumer/GestureConsumer.java deleted file mode 100644 index 8291b4f72..000000000 --- a/java/src/com/android/inputmethod/latin/touchinputconsumer/GestureConsumer.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2014 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.touchinputconsumer; - -import android.view.inputmethod.EditorInfo; - -import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.latin.DictionaryFacilitator; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.common.InputPointers; -import com.android.inputmethod.latin.inputlogic.PrivateCommandPerformer; - -import java.util.Locale; - -/** - * Stub for GestureConsumer. - * <br> - * The methods of this class should only be called from a single thread, e.g., - * the UI Thread. - */ -@SuppressWarnings("unused") -public class GestureConsumer { - public static final GestureConsumer NULL_GESTURE_CONSUMER = - new GestureConsumer(); - - public static GestureConsumer newInstance( - final EditorInfo editorInfo, final PrivateCommandPerformer commandPerformer, - final Locale locale, final Keyboard keyboard) { - return GestureConsumer.NULL_GESTURE_CONSUMER; - } - - private GestureConsumer() { - } - - public boolean willConsume() { - return false; - } - - public void onInit(final Locale locale, final Keyboard keyboard) { - } - - public void onGestureStarted(final Locale locale, final Keyboard keyboard) { - } - - public void onGestureCanceled() { - } - - public void onGestureCompleted(final InputPointers inputPointers) { - } - - public void onImeSuggestionsProcessed(final SuggestedWords suggestedWords, - final int composingStart, final int composingLength, - final DictionaryFacilitator dictionaryFacilitator) { - } -} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java deleted file mode 100644 index 58e6a25b8..000000000 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java +++ /dev/null @@ -1,286 +0,0 @@ -/* - * Copyright (C) 2013 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.userdictionary; - -import android.app.Activity; -import android.content.ContentResolver; -import android.content.Context; -import android.database.Cursor; -import android.os.Bundle; -import android.provider.UserDictionary; -import android.text.TextUtils; -import android.view.View; -import android.widget.EditText; - -import com.android.inputmethod.compat.UserDictionaryCompatUtils; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.common.LocaleUtils; - -import java.util.ArrayList; -import java.util.Locale; -import java.util.TreeSet; - -import javax.annotation.Nullable; - -// Caveat: This class is basically taken from -// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java -// in order to deal with some devices that have issues with the user dictionary handling - -/** - * A container class to factor common code to UserDictionaryAddWordFragment - * and UserDictionaryAddWordActivity. - */ -public class UserDictionaryAddWordContents { - public static final String EXTRA_MODE = "mode"; - public static final String EXTRA_WORD = "word"; - public static final String EXTRA_SHORTCUT = "shortcut"; - public static final String EXTRA_LOCALE = "locale"; - public static final String EXTRA_ORIGINAL_WORD = "originalWord"; - public static final String EXTRA_ORIGINAL_SHORTCUT = "originalShortcut"; - - public static final int MODE_EDIT = 0; - public static final int MODE_INSERT = 1; - - /* package */ static final int CODE_WORD_ADDED = 0; - /* package */ static final int CODE_CANCEL = 1; - /* package */ static final int CODE_ALREADY_PRESENT = 2; - - private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250; - - private final int mMode; // Either MODE_EDIT or MODE_INSERT - private final EditText mWordEditText; - private final EditText mShortcutEditText; - private String mLocale; - private final String mOldWord; - private final String mOldShortcut; - private String mSavedWord; - private String mSavedShortcut; - - /* package */ UserDictionaryAddWordContents(final View view, final Bundle args) { - mWordEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text); - mShortcutEditText = (EditText)view.findViewById(R.id.user_dictionary_add_shortcut); - if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { - mShortcutEditText.setVisibility(View.GONE); - view.findViewById(R.id.user_dictionary_add_shortcut_label).setVisibility(View.GONE); - } - final String word = args.getString(EXTRA_WORD); - if (null != word) { - mWordEditText.setText(word); - // Use getText in case the edit text modified the text we set. This happens when - // it's too long to be edited. - mWordEditText.setSelection(mWordEditText.getText().length()); - } - final String shortcut; - if (UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { - shortcut = args.getString(EXTRA_SHORTCUT); - if (null != shortcut && null != mShortcutEditText) { - mShortcutEditText.setText(shortcut); - } - mOldShortcut = args.getString(EXTRA_SHORTCUT); - } else { - shortcut = null; - mOldShortcut = null; - } - mMode = args.getInt(EXTRA_MODE); // default return value for #getInt() is 0 = MODE_EDIT - mOldWord = args.getString(EXTRA_WORD); - updateLocale(args.getString(EXTRA_LOCALE)); - } - - /* package */ UserDictionaryAddWordContents(final View view, - final UserDictionaryAddWordContents oldInstanceToBeEdited) { - mWordEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text); - mShortcutEditText = (EditText)view.findViewById(R.id.user_dictionary_add_shortcut); - mMode = MODE_EDIT; - mOldWord = oldInstanceToBeEdited.mSavedWord; - mOldShortcut = oldInstanceToBeEdited.mSavedShortcut; - updateLocale(mLocale); - } - - // locale may be null, this means default locale - // It may also be the empty string, which means "all locales" - /* package */ void updateLocale(final String locale) { - mLocale = null == locale ? Locale.getDefault().toString() : locale; - } - - /* package */ void saveStateIntoBundle(final Bundle outState) { - outState.putString(EXTRA_WORD, mWordEditText.getText().toString()); - outState.putString(EXTRA_ORIGINAL_WORD, mOldWord); - if (null != mShortcutEditText) { - outState.putString(EXTRA_SHORTCUT, mShortcutEditText.getText().toString()); - } - if (null != mOldShortcut) { - outState.putString(EXTRA_ORIGINAL_SHORTCUT, mOldShortcut); - } - outState.putString(EXTRA_LOCALE, mLocale); - } - - /* package */ void delete(final Context context) { - if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { - // Mode edit: remove the old entry. - final ContentResolver resolver = context.getContentResolver(); - UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); - } - // If we are in add mode, nothing was added, so we don't need to do anything. - } - - /* package */ - int apply(final Context context, final Bundle outParameters) { - if (null != outParameters) saveStateIntoBundle(outParameters); - final ContentResolver resolver = context.getContentResolver(); - if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { - // Mode edit: remove the old entry. - UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); - } - final String newWord = mWordEditText.getText().toString(); - final String newShortcut; - if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { - newShortcut = null; - } else if (null == mShortcutEditText) { - newShortcut = null; - } else { - final String tmpShortcut = mShortcutEditText.getText().toString(); - if (TextUtils.isEmpty(tmpShortcut)) { - newShortcut = null; - } else { - newShortcut = tmpShortcut; - } - } - if (TextUtils.isEmpty(newWord)) { - // If the word is somehow empty, don't insert it. - return CODE_CANCEL; - } - mSavedWord = newWord; - mSavedShortcut = newShortcut; - // If there is no shortcut, and the word already exists in the database, then we - // should not insert, because either A. the word exists with no shortcut, in which - // case the exact same thing we want to insert is already there, or B. the word - // exists with at least one shortcut, in which case it has priority on our word. - if (TextUtils.isEmpty(newShortcut) && hasWord(newWord, context)) { - return CODE_ALREADY_PRESENT; - } - - // Disallow duplicates. If the same word with no shortcut is defined, remove it; if - // the same word with the same shortcut is defined, remove it; but we don't mind if - // there is the same word with a different, non-empty shortcut. - UserDictionarySettings.deleteWord(newWord, null, resolver); - if (!TextUtils.isEmpty(newShortcut)) { - // If newShortcut is empty we just deleted this, no need to do it again - UserDictionarySettings.deleteWord(newWord, newShortcut, resolver); - } - - // In this class we use the empty string to represent 'all locales' and mLocale cannot - // be null. However the addWord method takes null to mean 'all locales'. - UserDictionaryCompatUtils.addWord(context, newWord.toString(), - FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut, TextUtils.isEmpty(mLocale) ? - null : LocaleUtils.constructLocaleFromString(mLocale)); - - return CODE_WORD_ADDED; - } - - private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD }; - private static final String HAS_WORD_SELECTION_ONE_LOCALE = UserDictionary.Words.WORD - + "=? AND " + UserDictionary.Words.LOCALE + "=?"; - private static final String HAS_WORD_SELECTION_ALL_LOCALES = UserDictionary.Words.WORD - + "=? AND " + UserDictionary.Words.LOCALE + " is null"; - private boolean hasWord(final String word, final Context context) { - final Cursor cursor; - // mLocale == "" indicates this is an entry for all languages. Here, mLocale can't - // be null at all (it's ensured by the updateLocale method). - if ("".equals(mLocale)) { - cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI, - HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ALL_LOCALES, - new String[] { word }, null /* sort order */); - } else { - cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI, - HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ONE_LOCALE, - new String[] { word, mLocale }, null /* sort order */); - } - try { - if (null == cursor) return false; - return cursor.getCount() > 0; - } finally { - if (null != cursor) cursor.close(); - } - } - - public static class LocaleRenderer { - private final String mLocaleString; - private final String mDescription; - - public LocaleRenderer(final Context context, @Nullable final String localeString) { - mLocaleString = localeString; - if (null == localeString) { - mDescription = context.getString(R.string.user_dict_settings_more_languages); - } else if ("".equals(localeString)) { - mDescription = context.getString(R.string.user_dict_settings_all_languages); - } else { - mDescription = LocaleUtils.constructLocaleFromString(localeString).getDisplayName(); - } - } - @Override - public String toString() { - return mDescription; - } - public String getLocaleString() { - return mLocaleString; - } - // "More languages..." is null ; "All languages" is the empty string. - public boolean isMoreLanguages() { - return null == mLocaleString; - } - } - - private static void addLocaleDisplayNameToList(final Context context, - final ArrayList<LocaleRenderer> list, final String locale) { - if (null != locale) { - list.add(new LocaleRenderer(context, locale)); - } - } - - // Helper method to get the list of locales to display for this word - public ArrayList<LocaleRenderer> getLocalesList(final Activity activity) { - final TreeSet<String> locales = UserDictionaryList.getUserDictionaryLocalesSet(activity); - // Remove our locale if it's in, because we're always gonna put it at the top - locales.remove(mLocale); // mLocale may not be null - final String systemLocale = Locale.getDefault().toString(); - // The system locale should be inside. We want it at the 2nd spot. - locales.remove(systemLocale); // system locale may not be null - locales.remove(""); // Remove the empty string if it's there - final ArrayList<LocaleRenderer> localesList = new ArrayList<>(); - // Add the passed locale, then the system locale at the top of the list. Add an - // "all languages" entry at the bottom of the list. - addLocaleDisplayNameToList(activity, localesList, mLocale); - if (!systemLocale.equals(mLocale)) { - addLocaleDisplayNameToList(activity, localesList, systemLocale); - } - for (final String l : locales) { - // TODO: sort in unicode order - addLocaleDisplayNameToList(activity, localesList, l); - } - if (!"".equals(mLocale)) { - // If mLocale is "", then we already inserted the "all languages" item, so don't do it - addLocaleDisplayNameToList(activity, localesList, ""); // meaning: all languages - } - localesList.add(new LocaleRenderer(activity, null)); // meaning: select another locale - return localesList; - } - - public String getCurrentUserDictionaryLocale() { - return mLocale; - } -} - diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java deleted file mode 100644 index a8781d786..000000000 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (C) 2013 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.userdictionary; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.userdictionary.UserDictionaryAddWordContents.LocaleRenderer; -import com.android.inputmethod.latin.userdictionary.UserDictionaryLocalePicker.LocationChangedListener; - -import android.app.Fragment; -import android.os.Bundle; -import android.preference.PreferenceActivity; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.Spinner; - -import java.util.ArrayList; -import java.util.Locale; - -// Caveat: This class is basically taken from -// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java -// in order to deal with some devices that have issues with the user dictionary handling - -/** - * Fragment to add a word/shortcut to the user dictionary. - * - * As opposed to the UserDictionaryActivity, this is only invoked within Settings - * from the UserDictionarySettings. - */ -public class UserDictionaryAddWordFragment extends Fragment - implements AdapterView.OnItemSelectedListener, LocationChangedListener { - - private static final int OPTIONS_MENU_ADD = Menu.FIRST; - private static final int OPTIONS_MENU_DELETE = Menu.FIRST + 1; - - private UserDictionaryAddWordContents mContents; - private View mRootView; - private boolean mIsDeleting = false; - - @Override - public void onActivityCreated(final Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - setHasOptionsMenu(true); - getActivity().getActionBar().setTitle(R.string.edit_personal_dictionary); - // Keep the instance so that we remember mContents when configuration changes (eg rotation) - setRetainInstance(true); - } - - @Override - public View onCreateView(final LayoutInflater inflater, final ViewGroup container, - final Bundle savedState) { - mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null); - mIsDeleting = false; - // If we have a non-null mContents object, it's the old value before a configuration - // change (eg rotation) so we need to use its values. Otherwise, read from the arguments. - if (null == mContents) { - mContents = new UserDictionaryAddWordContents(mRootView, getArguments()); - } else { - // We create a new mContents object to account for the new situation : a word has - // been added to the user dictionary when we started rotating, and we are now editing - // it. That means in particular if the word undergoes any change, the old version should - // be updated, so the mContents object needs to switch to EDIT mode if it was in - // INSERT mode. - mContents = new UserDictionaryAddWordContents(mRootView, - mContents /* oldInstanceToBeEdited */); - } - getActivity().getActionBar().setSubtitle(UserDictionarySettingsUtils.getLocaleDisplayName( - getActivity(), mContents.getCurrentUserDictionaryLocale())); - return mRootView; - } - - @Override - public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { - final MenuItem actionItemAdd = menu.add(0, OPTIONS_MENU_ADD, 0, - R.string.user_dict_settings_add_menu_title).setIcon(R.drawable.ic_menu_add); - actionItemAdd.setShowAsAction( - MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); - final MenuItem actionItemDelete = menu.add(0, OPTIONS_MENU_DELETE, 0, - R.string.user_dict_settings_delete).setIcon(android.R.drawable.ic_menu_delete); - actionItemDelete.setShowAsAction( - MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); - } - - /** - * Callback for the framework when a menu option is pressed. - * - * @param item the item that was pressed - * @return false to allow normal menu processing to proceed, true to consume it here - */ - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == OPTIONS_MENU_ADD) { - // added the entry in "onPause" - getActivity().onBackPressed(); - return true; - } - if (item.getItemId() == OPTIONS_MENU_DELETE) { - mContents.delete(getActivity()); - mIsDeleting = true; - getActivity().onBackPressed(); - return true; - } - return false; - } - - @Override - public void onResume() { - super.onResume(); - // We are being shown: display the word - updateSpinner(); - } - - private void updateSpinner() { - final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity()); - - final Spinner localeSpinner = - (Spinner)mRootView.findViewById(R.id.user_dictionary_add_locale); - final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<>( - getActivity(), android.R.layout.simple_spinner_item, localesList); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - localeSpinner.setAdapter(adapter); - localeSpinner.setOnItemSelectedListener(this); - } - - @Override - public void onPause() { - super.onPause(); - // We are being hidden: commit changes to the user dictionary, unless we were deleting it - if (!mIsDeleting) { - mContents.apply(getActivity(), null); - } - } - - @Override - public void onItemSelected(final AdapterView<?> parent, final View view, final int pos, - final long id) { - final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos); - if (locale.isMoreLanguages()) { - PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity(); - preferenceActivity.startPreferenceFragment(new UserDictionaryLocalePicker(), true); - } else { - mContents.updateLocale(locale.getLocaleString()); - } - } - - @Override - public void onNothingSelected(final AdapterView<?> parent) { - // I'm not sure we can come here, but if we do, that's the right thing to do. - final Bundle args = getArguments(); - mContents.updateLocale(args.getString(UserDictionaryAddWordContents.EXTRA_LOCALE)); - } - - // Called by the locale picker - @Override - public void onLocaleSelected(final Locale locale) { - mContents.updateLocale(locale.toString()); - getActivity().onBackPressed(); - } -} - diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java deleted file mode 100644 index f7940e3de..000000000 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (C) 2013 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.userdictionary; - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.database.Cursor; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.PreferenceFragment; -import android.preference.PreferenceGroup; -import android.provider.UserDictionary; -import android.text.TextUtils; -import android.view.inputmethod.InputMethodInfo; -import android.view.inputmethod.InputMethodManager; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.common.LocaleUtils; - -import java.util.List; -import java.util.Locale; -import java.util.TreeSet; - -import javax.annotation.Nullable; - -// Caveat: This class is basically taken from -// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryList.java -// in order to deal with some devices that have issues with the user dictionary handling - -public class UserDictionaryList extends PreferenceFragment { - - public static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION = - "android.settings.USER_DICTIONARY_SETTINGS"; - - @Override - public void onCreate(final Bundle icicle) { - super.onCreate(icicle); - setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity())); - } - - public static TreeSet<String> getUserDictionaryLocalesSet(final Activity activity) { - final Cursor cursor = activity.getContentResolver().query(UserDictionary.Words.CONTENT_URI, - new String[] { UserDictionary.Words.LOCALE }, - null, null, null); - final TreeSet<String> localeSet = new TreeSet<>(); - if (null == cursor) { - // The user dictionary service is not present or disabled. Return null. - return null; - } - try { - if (cursor.moveToFirst()) { - final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE); - do { - final String locale = cursor.getString(columnIndex); - localeSet.add(null != locale ? locale : ""); - } while (cursor.moveToNext()); - } - } finally { - cursor.close(); - } - if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { - // For ICS, we need to show "For all languages" in case that the keyboard locale - // is different from the system locale - localeSet.add(""); - } - - final InputMethodManager imm = - (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); - final List<InputMethodInfo> imis = imm.getEnabledInputMethodList(); - for (final InputMethodInfo imi : imis) { - final List<InputMethodSubtype> subtypes = - imm.getEnabledInputMethodSubtypeList( - imi, true /* allowsImplicitlySelectedSubtypes */); - for (InputMethodSubtype subtype : subtypes) { - final String locale = subtype.getLocale(); - if (!TextUtils.isEmpty(locale)) { - localeSet.add(locale); - } - } - } - - // We come here after we have collected locales from existing user dictionary entries and - // enabled subtypes. If we already have the locale-without-country version of the system - // locale, we don't add the system locale to avoid confusion even though it's technically - // correct to add it. - if (!localeSet.contains(Locale.getDefault().getLanguage().toString())) { - localeSet.add(Locale.getDefault().toString()); - } - - return localeSet; - } - - /** - * Creates the entries that allow the user to go into the user dictionary for each locale. - * @param userDictGroup The group to put the settings in. - */ - protected void createUserDictSettings(final PreferenceGroup userDictGroup) { - final Activity activity = getActivity(); - userDictGroup.removeAll(); - final TreeSet<String> localeSet = - UserDictionaryList.getUserDictionaryLocalesSet(activity); - - if (localeSet.size() > 1) { - // Have an "All languages" entry in the languages list if there are two or more active - // languages - localeSet.add(""); - } - - if (localeSet.isEmpty()) { - userDictGroup.addPreference(createUserDictionaryPreference(null)); - } else { - for (String locale : localeSet) { - userDictGroup.addPreference(createUserDictionaryPreference(locale)); - } - } - } - - /** - * Create a single User Dictionary Preference object, with its parameters set. - * @param localeString The locale for which this user dictionary is for. - * @return The corresponding preference. - */ - protected Preference createUserDictionaryPreference(@Nullable final String localeString) { - final Preference newPref = new Preference(getActivity()); - final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION); - if (null == localeString) { - newPref.setTitle(Locale.getDefault().getDisplayName()); - } else { - if (localeString.isEmpty()) { - newPref.setTitle(getString(R.string.user_dict_settings_all_languages)); - } else { - newPref.setTitle( - LocaleUtils.constructLocaleFromString(localeString).getDisplayName()); - } - intent.putExtra("locale", localeString); - newPref.getExtras().putString("locale", localeString); - } - newPref.setIntent(intent); - newPref.setFragment(UserDictionarySettings.class.getName()); - return newPref; - } - - @Override - public void onResume() { - super.onResume(); - createUserDictSettings(getPreferenceScreen()); - } -} - diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java deleted file mode 100644 index 58d3fb91c..000000000 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2013 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.userdictionary; - -import android.app.Fragment; - -import java.util.Locale; - -// Caveat: This class is basically taken from -// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryLocalePicker.java -// in order to deal with some devices that have issues with the user dictionary handling - -public class UserDictionaryLocalePicker extends Fragment { - public UserDictionaryLocalePicker() { - super(); - // TODO: implement - } - - public interface LocationChangedListener { - public void onLocaleSelected(Locale locale); - } -} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java deleted file mode 100644 index d9339d965..000000000 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java +++ /dev/null @@ -1,352 +0,0 @@ -/* - * Copyright (C) 2013 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.userdictionary; - -import com.android.inputmethod.latin.R; - -import android.app.ListFragment; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.database.Cursor; -import android.os.Build; -import android.os.Bundle; -import android.provider.UserDictionary; -import android.text.TextUtils; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AlphabetIndexer; -import android.widget.ListAdapter; -import android.widget.ListView; -import android.widget.SectionIndexer; -import android.widget.SimpleCursorAdapter; -import android.widget.TextView; - -import java.util.Locale; - -// Caveat: This class is basically taken from -// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionarySettings.java -// in order to deal with some devices that have issues with the user dictionary handling - -public class UserDictionarySettings extends ListFragment { - - public static final boolean IS_SHORTCUT_API_SUPPORTED = - Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; - - private static final String[] QUERY_PROJECTION_SHORTCUT_UNSUPPORTED = - { UserDictionary.Words._ID, UserDictionary.Words.WORD}; - private static final String[] QUERY_PROJECTION_SHORTCUT_SUPPORTED = - { UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT}; - private static final String[] QUERY_PROJECTION = - IS_SHORTCUT_API_SUPPORTED ? - QUERY_PROJECTION_SHORTCUT_SUPPORTED : QUERY_PROJECTION_SHORTCUT_UNSUPPORTED; - - // The index of the shortcut in the above array. - private static final int INDEX_SHORTCUT = 2; - - private static final String[] ADAPTER_FROM_SHORTCUT_UNSUPPORTED = { - UserDictionary.Words.WORD, - }; - - private static final String[] ADAPTER_FROM_SHORTCUT_SUPPORTED = { - UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT - }; - - private static final String[] ADAPTER_FROM = IS_SHORTCUT_API_SUPPORTED ? - ADAPTER_FROM_SHORTCUT_SUPPORTED : ADAPTER_FROM_SHORTCUT_UNSUPPORTED; - - private static final int[] ADAPTER_TO_SHORTCUT_UNSUPPORTED = { - android.R.id.text1, - }; - - private static final int[] ADAPTER_TO_SHORTCUT_SUPPORTED = { - android.R.id.text1, android.R.id.text2 - }; - - private static final int[] ADAPTER_TO = IS_SHORTCUT_API_SUPPORTED ? - ADAPTER_TO_SHORTCUT_SUPPORTED : ADAPTER_TO_SHORTCUT_UNSUPPORTED; - - // Either the locale is empty (means the word is applicable to all locales) - // or the word equals our current locale - private static final String QUERY_SELECTION = - UserDictionary.Words.LOCALE + "=?"; - private static final String QUERY_SELECTION_ALL_LOCALES = - UserDictionary.Words.LOCALE + " is null"; - - private static final String DELETE_SELECTION_WITH_SHORTCUT = UserDictionary.Words.WORD - + "=? AND " + UserDictionary.Words.SHORTCUT + "=?"; - private static final String DELETE_SELECTION_WITHOUT_SHORTCUT = UserDictionary.Words.WORD - + "=? AND " + UserDictionary.Words.SHORTCUT + " is null OR " - + UserDictionary.Words.SHORTCUT + "=''"; - private static final String DELETE_SELECTION_SHORTCUT_UNSUPPORTED = - UserDictionary.Words.WORD + "=?"; - - private static final int OPTIONS_MENU_ADD = Menu.FIRST; - - private Cursor mCursor; - - protected String mLocale; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getActivity().getActionBar().setTitle(R.string.edit_personal_dictionary); - } - - @Override - public View onCreateView( - LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate( - R.layout.user_dictionary_preference_list_fragment, container, false); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - - final Intent intent = getActivity().getIntent(); - final String localeFromIntent = - null == intent ? null : intent.getStringExtra("locale"); - - final Bundle arguments = getArguments(); - final String localeFromArguments = - null == arguments ? null : arguments.getString("locale"); - - final String locale; - if (null != localeFromArguments) { - locale = localeFromArguments; - } else if (null != localeFromIntent) { - locale = localeFromIntent; - } else { - locale = null; - } - - mLocale = locale; - // WARNING: The following cursor is never closed! TODO: don't put that in a member, and - // make sure all cursors are correctly closed. Also, this comes from a call to - // Activity#managedQuery, which has been deprecated for a long time (and which FORBIDS - // closing the cursor, so take care when resolving this TODO). We should either use a - // regular query and close the cursor, or switch to a LoaderManager and a CursorLoader. - mCursor = createCursor(locale); - TextView emptyView = (TextView) getView().findViewById(android.R.id.empty); - emptyView.setText(R.string.user_dict_settings_empty_text); - - final ListView listView = getListView(); - listView.setAdapter(createAdapter()); - listView.setFastScrollEnabled(true); - listView.setEmptyView(emptyView); - - setHasOptionsMenu(true); - // Show the language as a subtitle of the action bar - getActivity().getActionBar().setSubtitle( - UserDictionarySettingsUtils.getLocaleDisplayName(getActivity(), mLocale)); - } - - @Override - public void onResume() { - super.onResume(); - ListAdapter adapter = getListView().getAdapter(); - if (adapter != null && adapter instanceof MyAdapter) { - // The list view is forced refreshed here. This allows the changes done - // in UserDictionaryAddWordFragment (update/delete/insert) to be seen when - // user goes back to this view. - MyAdapter listAdapter = (MyAdapter) adapter; - listAdapter.notifyDataSetChanged(); - } - } - - @SuppressWarnings("deprecation") - private Cursor createCursor(final String locale) { - // Locale can be any of: - // - The string representation of a locale, as returned by Locale#toString() - // - The empty string. This means we want a cursor returning words valid for all locales. - // - null. This means we want a cursor for the current locale, whatever this is. - // Note that this contrasts with the data inside the database, where NULL means "all - // locales" and there should never be an empty string. The confusion is called by the - // historical use of null for "all locales". - // TODO: it should be easy to make this more readable by making the special values - // human-readable, like "all_locales" and "current_locales" strings, provided they - // can be guaranteed not to match locales that may exist. - if ("".equals(locale)) { - // Case-insensitive sort - return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, - QUERY_SELECTION_ALL_LOCALES, null, - "UPPER(" + UserDictionary.Words.WORD + ")"); - } - final String queryLocale = null != locale ? locale : Locale.getDefault().toString(); - return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, - QUERY_SELECTION, new String[] { queryLocale }, - "UPPER(" + UserDictionary.Words.WORD + ")"); - } - - private ListAdapter createAdapter() { - return new MyAdapter(getActivity(), R.layout.user_dictionary_item, mCursor, - ADAPTER_FROM, ADAPTER_TO); - } - - @Override - public void onListItemClick(ListView l, View v, int position, long id) { - final String word = getWord(position); - final String shortcut = getShortcut(position); - if (word != null) { - showAddOrEditDialog(word, shortcut); - } - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { - final Locale systemLocale = getResources().getConfiguration().locale; - if (!TextUtils.isEmpty(mLocale) && !mLocale.equals(systemLocale.toString())) { - // Hide the add button for ICS because it doesn't support specifying a locale - // for an entry. This new "locale"-aware API has been added in conjunction - // with the shortcut API. - return; - } - } - MenuItem actionItem = - menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title) - .setIcon(R.drawable.ic_menu_add); - actionItem.setShowAsAction( - MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == OPTIONS_MENU_ADD) { - showAddOrEditDialog(null, null); - return true; - } - return false; - } - - /** - * Add or edit a word. If editingWord is null, it's an add; otherwise, it's an edit. - * @param editingWord the word to edit, or null if it's an add. - * @param editingShortcut the shortcut for this entry, or null if none. - */ - private void showAddOrEditDialog(final String editingWord, final String editingShortcut) { - final Bundle args = new Bundle(); - args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, null == editingWord - ? UserDictionaryAddWordContents.MODE_INSERT - : UserDictionaryAddWordContents.MODE_EDIT); - args.putString(UserDictionaryAddWordContents.EXTRA_WORD, editingWord); - args.putString(UserDictionaryAddWordContents.EXTRA_SHORTCUT, editingShortcut); - args.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mLocale); - android.preference.PreferenceActivity pa = - (android.preference.PreferenceActivity)getActivity(); - pa.startPreferencePanel(UserDictionaryAddWordFragment.class.getName(), - args, R.string.user_dict_settings_add_dialog_title, null, null, 0); - } - - private String getWord(final int position) { - if (null == mCursor) return null; - mCursor.moveToPosition(position); - // Handle a possible race-condition - if (mCursor.isAfterLast()) return null; - - return mCursor.getString( - mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD)); - } - - private String getShortcut(final int position) { - if (!IS_SHORTCUT_API_SUPPORTED) return null; - if (null == mCursor) return null; - mCursor.moveToPosition(position); - // Handle a possible race-condition - if (mCursor.isAfterLast()) return null; - - return mCursor.getString( - mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT)); - } - - public static void deleteWord(final String word, final String shortcut, - final ContentResolver resolver) { - if (!IS_SHORTCUT_API_SUPPORTED) { - resolver.delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_SHORTCUT_UNSUPPORTED, - new String[] { word }); - } else if (TextUtils.isEmpty(shortcut)) { - resolver.delete( - UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT, - new String[] { word }); - } else { - resolver.delete( - UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT, - new String[] { word, shortcut }); - } - } - - private static class MyAdapter extends SimpleCursorAdapter implements SectionIndexer { - private AlphabetIndexer mIndexer; - - private ViewBinder mViewBinder = new ViewBinder() { - - @Override - public boolean setViewValue(final View v, final Cursor c, final int columnIndex) { - if (!IS_SHORTCUT_API_SUPPORTED) { - // just let SimpleCursorAdapter set the view values - return false; - } - if (columnIndex == INDEX_SHORTCUT) { - final String shortcut = c.getString(INDEX_SHORTCUT); - if (TextUtils.isEmpty(shortcut)) { - v.setVisibility(View.GONE); - } else { - ((TextView)v).setText(shortcut); - v.setVisibility(View.VISIBLE); - } - v.invalidate(); - return true; - } - - return false; - } - }; - - public MyAdapter(final Context context, final int layout, final Cursor c, - final String[] from, final int[] to) { - super(context, layout, c, from, to, 0 /* flags */); - - if (null != c) { - final String alphabet = context.getString(R.string.user_dict_fast_scroll_alphabet); - final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD); - mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet); - } - setViewBinder(mViewBinder); - } - - @Override - public int getPositionForSection(final int section) { - return null == mIndexer ? 0 : mIndexer.getPositionForSection(section); - } - - @Override - public int getSectionForPosition(final int position) { - return null == mIndexer ? 0 : mIndexer.getSectionForPosition(position); - } - - @Override - public Object[] getSections() { - return null == mIndexer ? null : mIndexer.getSections(); - } - } -} - diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettingsUtils.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettingsUtils.java deleted file mode 100644 index c0a946e42..000000000 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettingsUtils.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2013 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.userdictionary; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.common.LocaleUtils; - -import android.content.Context; -import android.text.TextUtils; - -import java.util.Locale; - -/** - * Utilities of the user dictionary settings - * TODO: We really want to move these utilities to a static library. - */ -public class UserDictionarySettingsUtils { - public static String getLocaleDisplayName(Context context, String localeStr) { - if (TextUtils.isEmpty(localeStr)) { - // CAVEAT: localeStr should not be null because a null locale stands for the system - // locale in UserDictionary.Words.addWord. - return context.getResources().getString(R.string.user_dict_settings_all_languages); - } - final Locale locale = LocaleUtils.constructLocaleFromString(localeStr); - final Locale systemLocale = context.getResources().getConfiguration().locale; - return locale.getDisplayName(systemLocale); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/AdditionalSubtypeUtils.java b/java/src/com/android/inputmethod/latin/utils/AdditionalSubtypeUtils.java deleted file mode 100644 index 2aac7c57a..000000000 --- a/java/src/com/android/inputmethod/latin/utils/AdditionalSubtypeUtils.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - * 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.utils; - -import static com.android.inputmethod.latin.common.Constants.Subtype.KEYBOARD_MODE; -import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.ASCII_CAPABLE; -import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.EMOJI_CAPABLE; -import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.IS_ADDITIONAL_SUBTYPE; -import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.KEYBOARD_LAYOUT_SET; -import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME; - -import android.os.Build; -import android.text.TextUtils; -import android.util.Log; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.common.StringUtils; - -import java.util.ArrayList; -import java.util.Arrays; - -public final class AdditionalSubtypeUtils { - private static final String TAG = AdditionalSubtypeUtils.class.getSimpleName(); - - private static final InputMethodSubtype[] EMPTY_SUBTYPE_ARRAY = new InputMethodSubtype[0]; - - private AdditionalSubtypeUtils() { - // This utility class is not publicly instantiable. - } - - @UsedForTesting - public static boolean isAdditionalSubtype(final InputMethodSubtype subtype) { - return subtype.containsExtraValueKey(IS_ADDITIONAL_SUBTYPE); - } - - private static final String LOCALE_AND_LAYOUT_SEPARATOR = ":"; - private static final int INDEX_OF_LOCALE = 0; - private static final int INDEX_OF_KEYBOARD_LAYOUT = 1; - private static final int INDEX_OF_EXTRA_VALUE = 2; - private static final int LENGTH_WITHOUT_EXTRA_VALUE = (INDEX_OF_KEYBOARD_LAYOUT + 1); - private static final int LENGTH_WITH_EXTRA_VALUE = (INDEX_OF_EXTRA_VALUE + 1); - private static final String PREF_SUBTYPE_SEPARATOR = ";"; - - private static InputMethodSubtype createAdditionalSubtypeInternal( - final String localeString, final String keyboardLayoutSetName, - final boolean isAsciiCapable, final boolean isEmojiCapable) { - final int nameId = SubtypeLocaleUtils.getSubtypeNameId(localeString, keyboardLayoutSetName); - final String platformVersionDependentExtraValues = getPlatformVersionDependentExtraValue( - localeString, keyboardLayoutSetName, isAsciiCapable, isEmojiCapable); - final int platformVersionIndependentSubtypeId = - getPlatformVersionIndependentSubtypeId(localeString, keyboardLayoutSetName); - // NOTE: In KitKat and later, InputMethodSubtypeBuilder#setIsAsciiCapable is also available. - // TODO: Use InputMethodSubtypeBuilder#setIsAsciiCapable when appropriate. - return InputMethodSubtypeCompatUtils.newInputMethodSubtype(nameId, - R.drawable.ic_ime_switcher_dark, localeString, KEYBOARD_MODE, - platformVersionDependentExtraValues, - false /* isAuxiliary */, false /* overrideImplicitlyEnabledSubtype */, - platformVersionIndependentSubtypeId); - } - - public static InputMethodSubtype createDummyAdditionalSubtype( - final String localeString, final String keyboardLayoutSetName) { - return createAdditionalSubtypeInternal(localeString, keyboardLayoutSetName, - false /* isAsciiCapable */, false /* isEmojiCapable */); - } - - public static InputMethodSubtype createAsciiEmojiCapableAdditionalSubtype( - final String localeString, final String keyboardLayoutSetName) { - return createAdditionalSubtypeInternal(localeString, keyboardLayoutSetName, - true /* isAsciiCapable */, true /* isEmojiCapable */); - } - - public static String getPrefSubtype(final InputMethodSubtype subtype) { - final String localeString = subtype.getLocale(); - final String keyboardLayoutSetName = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype); - final String layoutExtraValue = KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName; - final String extraValue = StringUtils.removeFromCommaSplittableTextIfExists( - layoutExtraValue, StringUtils.removeFromCommaSplittableTextIfExists( - IS_ADDITIONAL_SUBTYPE, subtype.getExtraValue())); - final String basePrefSubtype = localeString + LOCALE_AND_LAYOUT_SEPARATOR - + keyboardLayoutSetName; - return extraValue.isEmpty() ? basePrefSubtype - : basePrefSubtype + LOCALE_AND_LAYOUT_SEPARATOR + extraValue; - } - - public static InputMethodSubtype[] createAdditionalSubtypesArray(final String prefSubtypes) { - if (TextUtils.isEmpty(prefSubtypes)) { - return EMPTY_SUBTYPE_ARRAY; - } - final String[] prefSubtypeArray = prefSubtypes.split(PREF_SUBTYPE_SEPARATOR); - final ArrayList<InputMethodSubtype> subtypesList = new ArrayList<>(prefSubtypeArray.length); - for (final String prefSubtype : prefSubtypeArray) { - final String elems[] = prefSubtype.split(LOCALE_AND_LAYOUT_SEPARATOR); - if (elems.length != LENGTH_WITHOUT_EXTRA_VALUE - && elems.length != LENGTH_WITH_EXTRA_VALUE) { - Log.w(TAG, "Unknown additional subtype specified: " + prefSubtype + " in " - + prefSubtypes); - continue; - } - final String localeString = elems[INDEX_OF_LOCALE]; - final String keyboardLayoutSetName = elems[INDEX_OF_KEYBOARD_LAYOUT]; - // Here we assume that all the additional subtypes have AsciiCapable and EmojiCapable. - // This is actually what the setting dialog for additional subtype is doing. - final InputMethodSubtype subtype = createAsciiEmojiCapableAdditionalSubtype( - localeString, keyboardLayoutSetName); - if (subtype.getNameResId() == SubtypeLocaleUtils.UNKNOWN_KEYBOARD_LAYOUT) { - // Skip unknown keyboard layout subtype. This may happen when predefined keyboard - // layout has been removed. - continue; - } - subtypesList.add(subtype); - } - return subtypesList.toArray(new InputMethodSubtype[subtypesList.size()]); - } - - public static String createPrefSubtypes(final InputMethodSubtype[] subtypes) { - if (subtypes == null || subtypes.length == 0) { - return ""; - } - final StringBuilder sb = new StringBuilder(); - for (final InputMethodSubtype subtype : subtypes) { - if (sb.length() > 0) { - sb.append(PREF_SUBTYPE_SEPARATOR); - } - sb.append(getPrefSubtype(subtype)); - } - return sb.toString(); - } - - public static String createPrefSubtypes(final String[] prefSubtypes) { - if (prefSubtypes == null || prefSubtypes.length == 0) { - return ""; - } - final StringBuilder sb = new StringBuilder(); - for (final String prefSubtype : prefSubtypes) { - if (sb.length() > 0) { - sb.append(PREF_SUBTYPE_SEPARATOR); - } - sb.append(prefSubtype); - } - return sb.toString(); - } - - /** - * Returns the extra value that is optimized for the running OS. - * <p> - * Historically the extra value has been used as the last resort to annotate various kinds of - * attributes. Some of these attributes are valid only on some platform versions. Thus we cannot - * assume that the extra values stored in a persistent storage are always valid. We need to - * regenerate the extra value on the fly instead. - * </p> - * @param localeString the locale string (e.g., "en_US"). - * @param keyboardLayoutSetName the keyboard layout set name (e.g., "dvorak"). - * @param isAsciiCapable true when ASCII characters are supported with this layout. - * @param isEmojiCapable true when Unicode Emoji characters are supported with this layout. - * @return extra value that is optimized for the running OS. - * @see #getPlatformVersionIndependentSubtypeId(String, String) - */ - private static String getPlatformVersionDependentExtraValue(final String localeString, - final String keyboardLayoutSetName, final boolean isAsciiCapable, - final boolean isEmojiCapable) { - final ArrayList<String> extraValueItems = new ArrayList<>(); - extraValueItems.add(KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName); - if (isAsciiCapable) { - extraValueItems.add(ASCII_CAPABLE); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && - SubtypeLocaleUtils.isExceptionalLocale(localeString)) { - extraValueItems.add(UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME + "=" + - SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(keyboardLayoutSetName)); - } - if (isEmojiCapable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - extraValueItems.add(EMOJI_CAPABLE); - } - extraValueItems.add(IS_ADDITIONAL_SUBTYPE); - return TextUtils.join(",", extraValueItems); - } - - /** - * Returns the subtype ID that is supposed to be compatible between different version of OSes. - * <p> - * From the compatibility point of view, it is important to keep subtype id predictable and - * stable between different OSes. For this purpose, the calculation code in this method is - * carefully chosen and then fixed. Treat the following code as no more or less than a - * hash function. Each component to be hashed can be different from the corresponding value - * that is used to instantiate {@link InputMethodSubtype} actually. - * For example, you don't need to update <code>compatibilityExtraValueItems</code> in this - * method even when we need to add some new extra values for the actual instance of - * {@link InputMethodSubtype}. - * </p> - * @param localeString the locale string (e.g., "en_US"). - * @param keyboardLayoutSetName the keyboard layout set name (e.g., "dvorak"). - * @return a platform-version independent subtype ID. - * @see #getPlatformVersionDependentExtraValue(String, String, boolean, boolean) - */ - private static int getPlatformVersionIndependentSubtypeId(final String localeString, - final String keyboardLayoutSetName) { - // For compatibility reasons, we concatenate the extra values in the following order. - // - KeyboardLayoutSet - // - AsciiCapable - // - UntranslatableReplacementStringInSubtypeName - // - EmojiCapable - // - isAdditionalSubtype - final ArrayList<String> compatibilityExtraValueItems = new ArrayList<>(); - compatibilityExtraValueItems.add(KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName); - compatibilityExtraValueItems.add(ASCII_CAPABLE); - if (SubtypeLocaleUtils.isExceptionalLocale(localeString)) { - compatibilityExtraValueItems.add(UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME + "=" + - SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(keyboardLayoutSetName)); - } - compatibilityExtraValueItems.add(EMOJI_CAPABLE); - compatibilityExtraValueItems.add(IS_ADDITIONAL_SUBTYPE); - final String compatibilityExtraValues = TextUtils.join(",", compatibilityExtraValueItems); - return Arrays.hashCode(new Object[] { - localeString, - KEYBOARD_MODE, - compatibilityExtraValues, - false /* isAuxiliary */, - false /* overrideImplicitlyEnabledSubtype */ }); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/ApplicationUtils.java b/java/src/com/android/inputmethod/latin/utils/ApplicationUtils.java deleted file mode 100644 index 7a4150def..000000000 --- a/java/src/com/android/inputmethod/latin/utils/ApplicationUtils.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.app.Activity; -import android.content.ComponentName; -import android.content.Context; -import android.content.pm.ActivityInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager.NameNotFoundException; -import android.util.Log; - -public final class ApplicationUtils { - private static final String TAG = ApplicationUtils.class.getSimpleName(); - - private ApplicationUtils() { - // This utility class is not publicly instantiable. - } - - public static int getActivityTitleResId(final Context context, - final Class<? extends Activity> cls) { - final ComponentName cn = new ComponentName(context, cls); - try { - final ActivityInfo ai = context.getPackageManager().getActivityInfo(cn, 0); - if (ai != null) { - return ai.labelRes; - } - } catch (final NameNotFoundException e) { - Log.e(TAG, "Failed to get settings activity title res id.", e); - } - return 0; - } - - /** - * A utility method to get the application's PackageInfo.versionName - * @return the application's PackageInfo.versionName - */ - public static String getVersionName(final Context context) { - try { - if (context == null) { - return ""; - } - final String packageName = context.getPackageName(); - final PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); - return info.versionName; - } catch (final NameNotFoundException e) { - Log.e(TAG, "Could not find version info.", e); - } - return ""; - } - - /** - * A utility method to get the application's PackageInfo.versionCode - * @return the application's PackageInfo.versionCode - */ - public static int getVersionCode(final Context context) { - try { - if (context == null) { - return 0; - } - final String packageName = context.getPackageName(); - final PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); - return info.versionCode; - } catch (final NameNotFoundException e) { - Log.e(TAG, "Could not find version info.", e); - } - return 0; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/AsyncResultHolder.java b/java/src/com/android/inputmethod/latin/utils/AsyncResultHolder.java deleted file mode 100644 index 1525f2d56..000000000 --- a/java/src/com/android/inputmethod/latin/utils/AsyncResultHolder.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.util.Log; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -/** - * This class is a holder of the result of an asynchronous computation. - * - * @param <E> the type of the result. - */ -public class AsyncResultHolder<E> { - - private final Object mLock = new Object(); - - private E mResult; - private final String mTag; - private final CountDownLatch mLatch; - - public AsyncResultHolder(final String tag) { - mTag = tag; - mLatch = new CountDownLatch(1); - } - - /** - * Sets the result value of this holder. - * - * @param result the value to set. - */ - public void set(final E result) { - synchronized(mLock) { - if (mLatch.getCount() > 0) { - mResult = result; - mLatch.countDown(); - } - } - } - - /** - * Gets the result value held in this holder. - * Causes the current thread to wait unless the value is set or the specified time is elapsed. - * - * @param defaultValue the default value. - * @param timeOut the maximum time to wait. - * @return if the result is set before the time limit then the result, otherwise defaultValue. - */ - public E get(final E defaultValue, final long timeOut) { - try { - return mLatch.await(timeOut, TimeUnit.MILLISECONDS) ? mResult : defaultValue; - } catch (InterruptedException e) { - Log.w(mTag, "get() : Interrupted after " + timeOut + " ms"); - return defaultValue; - } - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java b/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java deleted file mode 100644 index c9ecade91..000000000 --- a/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.utils; - -import android.util.Log; - -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.define.DebugFlags; - -public final class AutoCorrectionUtils { - private static final boolean DBG = DebugFlags.DEBUG_ENABLED; - private static final String TAG = AutoCorrectionUtils.class.getSimpleName(); - - private AutoCorrectionUtils() { - // Purely static class: can't instantiate. - } - - public static boolean suggestionExceedsThreshold(final SuggestedWordInfo suggestion, - final String consideredWord, final float threshold) { - if (null != suggestion) { - // Shortlist a whitelisted word - if (suggestion.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) { - return true; - } - // TODO: return suggestion.isAprapreateForAutoCorrection(); - if (!suggestion.isAprapreateForAutoCorrection()) { - return false; - } - 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. - final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore( - consideredWord, suggestion.mWord, autoCorrectionSuggestionScore); - if (DBG) { - Log.d(TAG, "Normalized " + consideredWord + "," + suggestion + "," - + autoCorrectionSuggestionScore + ", " + normalizedScore - + "(" + threshold + ")"); - } - if (normalizedScore >= threshold) { - if (DBG) { - Log.d(TAG, "Exceeds threshold."); - } - return true; - } - } - return false; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/BinaryDictionaryUtils.java b/java/src/com/android/inputmethod/latin/utils/BinaryDictionaryUtils.java deleted file mode 100644 index 3bf9c6200..000000000 --- a/java/src/com/android/inputmethod/latin/utils/BinaryDictionaryUtils.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.BinaryDictionary; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.makedict.DictionaryHeader; -import com.android.inputmethod.latin.makedict.UnsupportedFormatException; - -import java.io.File; -import java.io.IOException; -import java.util.Locale; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public final class BinaryDictionaryUtils { - private static final String TAG = BinaryDictionaryUtils.class.getSimpleName(); - - private BinaryDictionaryUtils() { - // This utility class is not publicly instantiable. - } - - static { - JniUtils.loadNativeLibrary(); - } - - @UsedForTesting - private static native boolean createEmptyDictFileNative(String filePath, long dictVersion, - String locale, String[] attributeKeyStringArray, String[] attributeValueStringArray); - private static native float calcNormalizedScoreNative(int[] before, int[] after, int score); - private static native int setCurrentTimeForTestNative(int currentTime); - - public static DictionaryHeader getHeader(final File dictFile) - throws IOException, UnsupportedFormatException { - return getHeaderWithOffsetAndLength(dictFile, 0 /* offset */, dictFile.length()); - } - - public static DictionaryHeader getHeaderWithOffsetAndLength(final File dictFile, - final long offset, final long length) throws IOException, UnsupportedFormatException { - // dictType is never used for reading the header. Passing an empty string. - final BinaryDictionary binaryDictionary = new BinaryDictionary( - dictFile.getAbsolutePath(), offset, length, - true /* useFullEditDistance */, null /* locale */, "" /* dictType */, - false /* isUpdatable */); - final DictionaryHeader header = binaryDictionary.getHeader(); - binaryDictionary.close(); - if (header == null) { - throw new IOException(); - } - return header; - } - - public static boolean renameDict(final File dictFile, final File newDictFile) { - if (dictFile.isFile()) { - return dictFile.renameTo(newDictFile); - } else if (dictFile.isDirectory()) { - final String dictName = dictFile.getName(); - final String newDictName = newDictFile.getName(); - if (newDictFile.exists()) { - return false; - } - for (final File file : dictFile.listFiles()) { - if (!file.isFile()) { - continue; - } - final String fileName = file.getName(); - final String newFileName = fileName.replaceFirst( - Pattern.quote(dictName), Matcher.quoteReplacement(newDictName)); - if (!file.renameTo(new File(dictFile, newFileName))) { - return false; - } - } - return dictFile.renameTo(newDictFile); - } - return false; - } - - @UsedForTesting - public static boolean createEmptyDictFile(final String filePath, final long dictVersion, - final Locale locale, final Map<String, String> attributeMap) { - final String[] keyArray = new String[attributeMap.size()]; - final String[] valueArray = new String[attributeMap.size()]; - int index = 0; - for (final String key : attributeMap.keySet()) { - keyArray[index] = key; - valueArray[index] = attributeMap.get(key); - index++; - } - return createEmptyDictFileNative(filePath, dictVersion, locale.toString(), keyArray, - valueArray); - } - - public static float calcNormalizedScore(final String before, final String after, - final int score) { - return calcNormalizedScoreNative(StringUtils.toCodePointArray(before), - StringUtils.toCodePointArray(after), score); - } - - /** - * Control the current time to be used in the native code. If currentTime >= 0, this method sets - * the current time and gets into test mode. - * In test mode, set timestamp is used as the current time in the native code. - * If currentTime < 0, quit the test mode and returns to using time() to get the current time. - * - * @param currentTime seconds since the unix epoch - * @return current time got in the native code. - */ - @UsedForTesting - public static int setCurrentTimeForTest(final int currentTime) { - return setCurrentTimeForTestNative(currentTime); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/CapsModeUtils.java b/java/src/com/android/inputmethod/latin/utils/CapsModeUtils.java deleted file mode 100644 index 47f65499d..000000000 --- a/java/src/com/android/inputmethod/latin/utils/CapsModeUtils.java +++ /dev/null @@ -1,357 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.text.InputType; -import android.text.TextUtils; - -import com.android.inputmethod.latin.WordComposer; -import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.settings.SpacingAndPunctuations; - -import java.util.ArrayList; -import java.util.Locale; - -public final class CapsModeUtils { - private CapsModeUtils() { - // This utility class is not publicly instantiable. - } - - /** - * Apply an auto-caps mode to a string. - * - * This intentionally does NOT apply manual caps mode. It only changes the capitalization if - * the mode is one of the auto-caps modes. - * @param s The string to capitalize. - * @param capitalizeMode The mode in which to capitalize. - * @param locale The locale for capitalizing. - * @return The capitalized string. - */ - public static String applyAutoCapsMode(final String s, final int capitalizeMode, - final Locale locale) { - if (WordComposer.CAPS_MODE_AUTO_SHIFT_LOCKED == capitalizeMode) { - return s.toUpperCase(locale); - } else if (WordComposer.CAPS_MODE_AUTO_SHIFTED == capitalizeMode) { - return StringUtils.capitalizeFirstCodePoint(s, locale); - } else { - return s; - } - } - - /** - * Return whether a constant represents an auto-caps mode (either auto-shift or auto-shift-lock) - * @param mode The mode to test for - * @return true if this represents an auto-caps mode, false otherwise - */ - public static boolean isAutoCapsMode(final int mode) { - return WordComposer.CAPS_MODE_AUTO_SHIFTED == mode - || WordComposer.CAPS_MODE_AUTO_SHIFT_LOCKED == mode; - } - - /** - * Helper method to find out if a code point is starting punctuation. - * - * This include the Unicode START_PUNCTUATION category, but also some other symbols that are - * starting, like the inverted question mark or the double quote. - * - * @param codePoint the code point - * @return true if it's starting punctuation, false otherwise. - */ - private static boolean isStartPunctuation(final int codePoint) { - return (codePoint == Constants.CODE_DOUBLE_QUOTE || codePoint == Constants.CODE_SINGLE_QUOTE - || codePoint == Constants.CODE_INVERTED_QUESTION_MARK - || codePoint == Constants.CODE_INVERTED_EXCLAMATION_MARK - || Character.getType(codePoint) == Character.START_PUNCTUATION); - } - - /** - * Determine what caps mode should be in effect at the current offset in - * the text. Only the mode bits set in <var>reqModes</var> will be - * checked. Note that the caps mode flags here are explicitly defined - * to match those in {@link InputType}. - * - * This code is a straight copy of TextUtils.getCapsMode (modulo namespace and formatting - * issues). This will change in the future as we simplify the code for our use and fix bugs. - * - * @param cs The text that should be checked for caps modes. - * @param reqModes The modes to be checked: may be any combination of - * {@link TextUtils#CAP_MODE_CHARACTERS}, {@link TextUtils#CAP_MODE_WORDS}, and - * {@link TextUtils#CAP_MODE_SENTENCES}. - * @param spacingAndPunctuations The current spacing and punctuations settings. - * @param hasSpaceBefore Whether we should consider there is a space inserted at the end of cs - * - * @return Returns the actual capitalization modes that can be in effect - * at the current position, which is any combination of - * {@link TextUtils#CAP_MODE_CHARACTERS}, {@link TextUtils#CAP_MODE_WORDS}, and - * {@link TextUtils#CAP_MODE_SENTENCES}. - */ - public static int getCapsMode(final CharSequence cs, final int reqModes, - final SpacingAndPunctuations spacingAndPunctuations, final boolean hasSpaceBefore) { - // Quick description of what we want to do: - // CAP_MODE_CHARACTERS is always on. - // CAP_MODE_WORDS is on if there is some whitespace before the cursor. - // CAP_MODE_SENTENCES is on if there is some whitespace before the cursor, and the end - // of a sentence just before that. - // We ignore opening parentheses and the like just before the cursor for purposes of - // finding whitespace for WORDS and SENTENCES modes. - // The end of a sentence ends with a period, question mark or exclamation mark. If it's - // a period, it also needs not to be an abbreviation, which means it also needs to either - // be immediately preceded by punctuation, or by a string of only letters with single - // periods interleaved. - - // Step 1 : check for cap MODE_CHARACTERS. If it's looked for, it's always on. - if ((reqModes & (TextUtils.CAP_MODE_WORDS | TextUtils.CAP_MODE_SENTENCES)) == 0) { - // Here we are not looking for MODE_WORDS or MODE_SENTENCES, so since we already - // evaluated MODE_CHARACTERS, we can return. - return TextUtils.CAP_MODE_CHARACTERS & reqModes; - } - - // Step 2 : Skip (ignore at the end of input) any opening punctuation. This includes - // opening parentheses, brackets, opening quotes, everything that *opens* a span of - // text in the linguistic sense. In RTL languages, this is still an opening sign, although - // it may look like a right parenthesis for example. We also include double quote and - // single quote since they aren't start punctuation in the unicode sense, but should still - // be skipped for English. TODO: does this depend on the language? - int i; - if (hasSpaceBefore) { - i = cs.length() + 1; - } else { - for (i = cs.length(); i > 0; i--) { - final char c = cs.charAt(i - 1); - if (!isStartPunctuation(c)) { - break; - } - } - } - - // We are now on the character that precedes any starting punctuation, so in the most - // frequent case this will be whitespace or a letter, although it may occasionally be a - // start of line, or some symbol. - - // Step 3 : Search for the start of a paragraph. From the starting point computed in step 2, - // we go back over any space or tab char sitting there. We find the start of a paragraph - // if the first char that's not a space or tab is a start of line (as in \n, start of text, - // or some other similar characters). - int j = i; - char prevChar = Constants.CODE_SPACE; - if (hasSpaceBefore) --j; - while (j > 0) { - prevChar = cs.charAt(j - 1); - if (!Character.isSpaceChar(prevChar) && prevChar != Constants.CODE_TAB) break; - j--; - } - if (j <= 0 || Character.isWhitespace(prevChar)) { - if (spacingAndPunctuations.mUsesGermanRules) { - // In German typography rules, there is a specific case that the first character - // of a new line should not be capitalized if the previous line ends in a comma. - boolean hasNewLine = false; - while (--j >= 0 && Character.isWhitespace(prevChar)) { - if (Constants.CODE_ENTER == prevChar) { - hasNewLine = true; - } - prevChar = cs.charAt(j); - } - if (Constants.CODE_COMMA == prevChar && hasNewLine) { - return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; - } - } - // There are only spacing chars between the start of the paragraph and the cursor, - // defined as a isWhitespace() char that is neither a isSpaceChar() nor a tab. Both - // MODE_WORDS and MODE_SENTENCES should be active. - return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS - | TextUtils.CAP_MODE_SENTENCES) & reqModes; - } - if (i == j) { - // If we don't have whitespace before index i, it means neither MODE_WORDS - // nor mode sentences should be on so we can return right away. - return TextUtils.CAP_MODE_CHARACTERS & reqModes; - } - if ((reqModes & TextUtils.CAP_MODE_SENTENCES) == 0) { - // Here we know we have whitespace before the cursor (if not, we returned in the above - // if i == j clause), so we need MODE_WORDS to be on. And we don't need to evaluate - // MODE_SENTENCES so we can return right away. - return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; - } - // Please note that because of the reqModes & CAP_MODE_SENTENCES test a few lines above, - // we know that MODE_SENTENCES is being requested. - - // Step 4 : Search for MODE_SENTENCES. - // English is a special case in that "American typography" rules, which are the most common - // in English, state that a sentence terminator immediately following a quotation mark - // should be swapped with it and de-duplicated (included in the quotation mark), - // e.g. <<Did they say, "let's go home?">> - // No other language has such a rule as far as I know, instead putting inside the quotation - // mark as the exact thing quoted and handling the surrounding punctuation independently, - // e.g. <<Did they say, "let's go home"?>> - if (spacingAndPunctuations.mUsesAmericanTypography) { - for (; j > 0; j--) { - // Here we look to go over any closing punctuation. This is because in dominant - // variants of English, the final period is placed within double quotes and maybe - // other closing punctuation signs. This is generally not true in other languages. - final char c = cs.charAt(j - 1); - if (c != Constants.CODE_DOUBLE_QUOTE && c != Constants.CODE_SINGLE_QUOTE - && Character.getType(c) != Character.END_PUNCTUATION) { - break; - } - } - } - - if (j <= 0) return TextUtils.CAP_MODE_CHARACTERS & reqModes; - char c = cs.charAt(--j); - - // We found the next interesting chunk of text ; next we need to determine if it's the - // end of a sentence. If we have a sentence terminator (typically a question mark or an - // exclamation mark), then it's the end of a sentence; however, we treat the abbreviation - // marker specially because usually is the same char as the sentence separator (the - // period in most languages) and in this case we need to apply a heuristic to determine - // in which of these senses it's used. - if (spacingAndPunctuations.isSentenceTerminator(c) - && !spacingAndPunctuations.isAbbreviationMarker(c)) { - return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS - | TextUtils.CAP_MODE_SENTENCES) & reqModes; - } - // If we reach here, we know we have whitespace before the cursor and before that there - // is something that either does not terminate the sentence, or a symbol preceded by the - // start of the text, or it's the sentence separator AND it happens to be the same code - // point as the abbreviation marker. - // If it's a symbol or something that does not terminate the sentence, then we need to - // return caps for MODE_CHARACTERS and MODE_WORDS, but not for MODE_SENTENCES. - if (!spacingAndPunctuations.isSentenceSeparator(c) || j <= 0) { - return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; - } - - // We found out that we have a period. We need to determine if this is a full stop or - // otherwise sentence-ending period, or an abbreviation like "e.g.". An abbreviation - // looks like (\w\.){2,}. Moreover, in German, you put periods after digits for dates - // and some other things, and in German specifically we need to not go into autocaps after - // a whitespace-digits-period sequence. - // To find out, we will have a simple state machine with the following states : - // START, WORD, PERIOD, ABBREVIATION, NUMBER - // On START : (just before the first period) - // letter => WORD - // digit => NUMBER if German; end with caps otherwise - // whitespace => end with no caps (it was a stand-alone period) - // otherwise => end with caps (several periods/symbols in a row) - // On WORD : (within the word just before the first period) - // letter => WORD - // period => PERIOD - // otherwise => end with caps (it was a word with a full stop at the end) - // On PERIOD : (period within a potential abbreviation) - // letter => LETTER - // otherwise => end with caps (it was not an abbreviation) - // On LETTER : (letter within a potential abbreviation) - // letter => LETTER - // period => PERIOD - // otherwise => end with no caps (it was an abbreviation) - // On NUMBER : (period immediately preceded by one or more digits) - // digit => NUMBER - // letter => LETTER (promote to word) - // otherwise => end with no caps (it was a whitespace-digits-period sequence, - // or a punctuation-digits-period sequence like "11.11.") - // "Not an abbreviation" in the above chart essentially covers cases like "...yes.". This - // should capitalize. - - final int START = 0; - final int WORD = 1; - final int PERIOD = 2; - final int LETTER = 3; - final int NUMBER = 4; - final int caps = (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS - | TextUtils.CAP_MODE_SENTENCES) & reqModes; - final int noCaps = (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; - int state = START; - while (j > 0) { - c = cs.charAt(--j); - switch (state) { - case START: - if (Character.isLetter(c)) { - state = WORD; - } else if (Character.isWhitespace(c)) { - return noCaps; - } else if (Character.isDigit(c) && spacingAndPunctuations.mUsesGermanRules) { - state = NUMBER; - } else { - return caps; - } - break; - case WORD: - if (Character.isLetter(c)) { - state = WORD; - } else if (spacingAndPunctuations.isSentenceSeparator(c)) { - state = PERIOD; - } else { - return caps; - } - break; - case PERIOD: - if (Character.isLetter(c)) { - state = LETTER; - } else { - return caps; - } - break; - case LETTER: - if (Character.isLetter(c)) { - state = LETTER; - } else if (spacingAndPunctuations.isSentenceSeparator(c)) { - state = PERIOD; - } else { - return noCaps; - } - break; - case NUMBER: - if (Character.isLetter(c)) { - state = WORD; - } else if (Character.isDigit(c)) { - state = NUMBER; - } else { - return noCaps; - } - } - } - // Here we arrived at the start of the line. This should behave exactly like whitespace. - return (START == state || LETTER == state) ? noCaps : caps; - } - - /** - * Convert capitalize mode flags into human readable text. - * - * @param capsFlags The modes flags to be converted. It may be any combination of - * {@link TextUtils#CAP_MODE_CHARACTERS}, {@link TextUtils#CAP_MODE_WORDS}, and - * {@link TextUtils#CAP_MODE_SENTENCES}. - * @return the text that describe the <code>capsMode</code>. - */ - public static String flagsToString(final int capsFlags) { - final int capsFlagsMask = TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS - | TextUtils.CAP_MODE_SENTENCES; - if ((capsFlags & ~capsFlagsMask) != 0) { - return "unknown<0x" + Integer.toHexString(capsFlags) + ">"; - } - final ArrayList<String> builder = new ArrayList<>(); - if ((capsFlags & android.text.TextUtils.CAP_MODE_CHARACTERS) != 0) { - builder.add("characters"); - } - if ((capsFlags & android.text.TextUtils.CAP_MODE_WORDS) != 0) { - builder.add("words"); - } - if ((capsFlags & android.text.TextUtils.CAP_MODE_SENTENCES) != 0) { - builder.add("sentences"); - } - return builder.isEmpty() ? "none" : TextUtils.join("|", builder); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/CombinedFormatUtils.java b/java/src/com/android/inputmethod/latin/utils/CombinedFormatUtils.java deleted file mode 100644 index 5c0c4328f..000000000 --- a/java/src/com/android/inputmethod/latin/utils/CombinedFormatUtils.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import com.android.inputmethod.latin.makedict.DictionaryHeader; -import com.android.inputmethod.latin.makedict.NgramProperty; -import com.android.inputmethod.latin.makedict.ProbabilityInfo; -import com.android.inputmethod.latin.makedict.WordProperty; - -import java.util.HashMap; - -public class CombinedFormatUtils { - public static final String DICTIONARY_TAG = "dictionary"; - public static final String BIGRAM_TAG = "bigram"; - public static final String NGRAM_TAG = "ngram"; - public static final String NGRAM_PREV_WORD_TAG = "prev_word"; - public static final String PROBABILITY_TAG = "f"; - public static final String HISTORICAL_INFO_TAG = "historicalInfo"; - public static final String HISTORICAL_INFO_SEPARATOR = ":"; - public static final String WORD_TAG = "word"; - public static final String BEGINNING_OF_SENTENCE_TAG = "beginning_of_sentence"; - public static final String NOT_A_WORD_TAG = "not_a_word"; - public static final String POSSIBLY_OFFENSIVE_TAG = "possibly_offensive"; - public static final String TRUE_VALUE = "true"; - - public static String formatAttributeMap(final HashMap<String, String> attributeMap) { - final StringBuilder builder = new StringBuilder(); - builder.append(DICTIONARY_TAG + "="); - if (attributeMap.containsKey(DictionaryHeader.DICTIONARY_ID_KEY)) { - builder.append(attributeMap.get(DictionaryHeader.DICTIONARY_ID_KEY)); - } - for (final String key : attributeMap.keySet()) { - if (key.equals(DictionaryHeader.DICTIONARY_ID_KEY)) { - continue; - } - final String value = attributeMap.get(key); - builder.append("," + key + "=" + value); - } - builder.append("\n"); - return builder.toString(); - } - - public static String formatWordProperty(final WordProperty wordProperty) { - final StringBuilder builder = new StringBuilder(); - builder.append(" " + WORD_TAG + "=" + wordProperty.mWord); - builder.append(","); - builder.append(formatProbabilityInfo(wordProperty.mProbabilityInfo)); - if (wordProperty.mIsBeginningOfSentence) { - builder.append("," + BEGINNING_OF_SENTENCE_TAG + "=" + TRUE_VALUE); - } - if (wordProperty.mIsNotAWord) { - builder.append("," + NOT_A_WORD_TAG + "=" + TRUE_VALUE); - } - if (wordProperty.mIsPossiblyOffensive) { - builder.append("," + POSSIBLY_OFFENSIVE_TAG + "=" + TRUE_VALUE); - } - builder.append("\n"); - if (wordProperty.mHasNgrams) { - for (final NgramProperty ngramProperty : wordProperty.mNgrams) { - builder.append(" " + NGRAM_TAG + "=" + ngramProperty.mTargetWord.mWord); - builder.append(","); - builder.append(formatProbabilityInfo(ngramProperty.mTargetWord.mProbabilityInfo)); - builder.append("\n"); - for (int i = 0; i < ngramProperty.mNgramContext.getPrevWordCount(); i++) { - builder.append(" " + NGRAM_PREV_WORD_TAG + "[" + i + "]=" - + ngramProperty.mNgramContext.getNthPrevWord(i + 1)); - if (ngramProperty.mNgramContext.isNthPrevWordBeginningOfSentence(i + 1)) { - builder.append("," + BEGINNING_OF_SENTENCE_TAG + "=true"); - } - builder.append("\n"); - } - } - } - return builder.toString(); - } - - public static String formatProbabilityInfo(final ProbabilityInfo probabilityInfo) { - final StringBuilder builder = new StringBuilder(); - builder.append(PROBABILITY_TAG + "=" + probabilityInfo.mProbability); - if (probabilityInfo.hasHistoricalInfo()) { - builder.append(","); - builder.append(HISTORICAL_INFO_TAG + "="); - builder.append(probabilityInfo.mTimestamp); - builder.append(HISTORICAL_INFO_SEPARATOR); - builder.append(probabilityInfo.mLevel); - builder.append(HISTORICAL_INFO_SEPARATOR); - builder.append(probabilityInfo.mCount); - } - return builder.toString(); - } - - public static boolean isLiteralTrue(final String value) { - return TRUE_VALUE.equalsIgnoreCase(value); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/CompletionInfoUtils.java b/java/src/com/android/inputmethod/latin/utils/CompletionInfoUtils.java deleted file mode 100644 index 5ccf0e079..000000000 --- a/java/src/com/android/inputmethod/latin/utils/CompletionInfoUtils.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.text.TextUtils; -import android.view.inputmethod.CompletionInfo; - -import java.util.Arrays; - -/** - * Utilities to do various stuff with CompletionInfo. - */ -public class CompletionInfoUtils { - private CompletionInfoUtils() { - // This utility class is not publicly instantiable. - } - - public static CompletionInfo[] removeNulls(final CompletionInfo[] src) { - int j = 0; - final CompletionInfo[] dst = new CompletionInfo[src.length]; - for (int i = 0; i < src.length; ++i) { - if (null != src[i] && !TextUtils.isEmpty(src[i].getText())) { - dst[j] = src[i]; - ++j; - } - } - return Arrays.copyOfRange(dst, 0, j); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/CursorAnchorInfoUtils.java b/java/src/com/android/inputmethod/latin/utils/CursorAnchorInfoUtils.java deleted file mode 100644 index 41090c054..000000000 --- a/java/src/com/android/inputmethod/latin/utils/CursorAnchorInfoUtils.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.annotation.TargetApi; -import android.graphics.Matrix; -import android.graphics.Rect; -import android.inputmethodservice.ExtractEditText; -import android.inputmethodservice.InputMethodService; -import android.os.Build; -import android.text.Layout; -import android.text.Spannable; -import android.text.Spanned; -import android.view.View; -import android.view.ViewParent; -import android.view.inputmethod.CursorAnchorInfo; -import android.widget.TextView; - -import com.android.inputmethod.compat.BuildCompatUtils; -import com.android.inputmethod.compat.CursorAnchorInfoCompatWrapper; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * This class allows input methods to extract {@link CursorAnchorInfo} directly from the given - * {@link TextView}. This is useful and even necessary to support full-screen mode where the default - * {@link InputMethodService#onUpdateCursorAnchorInfo(CursorAnchorInfo)} event callback must be - * ignored because it reports the character locations of the target application rather than - * characters on {@link ExtractEditText}. - */ -public final class CursorAnchorInfoUtils { - private CursorAnchorInfoUtils() { - // This helper class is not instantiable. - } - - private static boolean isPositionVisible(final View view, final float positionX, - final float positionY) { - final float[] position = new float[] { positionX, positionY }; - View currentView = view; - - while (currentView != null) { - if (currentView != view) { - // Local scroll is already taken into account in positionX/Y - position[0] -= currentView.getScrollX(); - position[1] -= currentView.getScrollY(); - } - - if (position[0] < 0 || position[1] < 0 || - position[0] > currentView.getWidth() || position[1] > currentView.getHeight()) { - return false; - } - - if (!currentView.getMatrix().isIdentity()) { - currentView.getMatrix().mapPoints(position); - } - - position[0] += currentView.getLeft(); - position[1] += currentView.getTop(); - - final ViewParent parent = currentView.getParent(); - if (parent instanceof View) { - currentView = (View) parent; - } else { - // We've reached the ViewRoot, stop iterating - currentView = null; - } - } - - // We've been able to walk up the view hierarchy and the position was never clipped - return true; - } - - /** - * Extracts {@link CursorAnchorInfoCompatWrapper} from the given {@link TextView}. - * @param textView the target text view from which {@link CursorAnchorInfoCompatWrapper} is to - * be extracted. - * @return the {@link CursorAnchorInfoCompatWrapper} object based on the current layout. - * {@code null} if {@code Build.VERSION.SDK_INT} is 20 or prior or {@link TextView} is not - * ready to provide layout information. - */ - @Nullable - public static CursorAnchorInfoCompatWrapper extractFromTextView( - @Nonnull final TextView textView) { - if (BuildCompatUtils.EFFECTIVE_SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - return null; - } - return CursorAnchorInfoCompatWrapper.wrap(extractFromTextViewInternal(textView)); - } - - /** - * Returns {@link CursorAnchorInfo} from the given {@link TextView}. - * @param textView the target text view from which {@link CursorAnchorInfo} is to be extracted. - * @return the {@link CursorAnchorInfo} object based on the current layout. {@code null} if it - * is not feasible. - */ - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - @Nullable - private static CursorAnchorInfo extractFromTextViewInternal(@Nonnull final TextView textView) { - final Layout layout = textView.getLayout(); - if (layout == null) { - return null; - } - - final CursorAnchorInfo.Builder builder = new CursorAnchorInfo.Builder(); - - final int selectionStart = textView.getSelectionStart(); - builder.setSelectionRange(selectionStart, textView.getSelectionEnd()); - - // Construct transformation matrix from view local coordinates to screen coordinates. - final Matrix viewToScreenMatrix = new Matrix(textView.getMatrix()); - final int[] viewOriginInScreen = new int[2]; - textView.getLocationOnScreen(viewOriginInScreen); - viewToScreenMatrix.postTranslate(viewOriginInScreen[0], viewOriginInScreen[1]); - builder.setMatrix(viewToScreenMatrix); - - if (layout.getLineCount() == 0) { - return null; - } - final Rect lineBoundsWithoutOffset = new Rect(); - final Rect lineBoundsWithOffset = new Rect(); - layout.getLineBounds(0, lineBoundsWithoutOffset); - textView.getLineBounds(0, lineBoundsWithOffset); - final float viewportToContentHorizontalOffset = lineBoundsWithOffset.left - - lineBoundsWithoutOffset.left - textView.getScrollX(); - final float viewportToContentVerticalOffset = lineBoundsWithOffset.top - - lineBoundsWithoutOffset.top - textView.getScrollY(); - - final CharSequence text = textView.getText(); - if (text instanceof Spannable) { - // Here we assume that the composing text is marked as SPAN_COMPOSING flag. This is not - // necessarily true, but basically works. - int composingTextStart = text.length(); - int composingTextEnd = 0; - final Spannable spannable = (Spannable) text; - final Object[] spans = spannable.getSpans(0, text.length(), Object.class); - for (Object span : spans) { - final int spanFlag = spannable.getSpanFlags(span); - if ((spanFlag & Spanned.SPAN_COMPOSING) != 0) { - composingTextStart = Math.min(composingTextStart, - spannable.getSpanStart(span)); - composingTextEnd = Math.max(composingTextEnd, spannable.getSpanEnd(span)); - } - } - - final boolean hasComposingText = - (0 <= composingTextStart) && (composingTextStart < composingTextEnd); - if (hasComposingText) { - final CharSequence composingText = text.subSequence(composingTextStart, - composingTextEnd); - builder.setComposingText(composingTextStart, composingText); - - final int minLine = layout.getLineForOffset(composingTextStart); - final int maxLine = layout.getLineForOffset(composingTextEnd - 1); - for (int line = minLine; line <= maxLine; ++line) { - final int lineStart = layout.getLineStart(line); - final int lineEnd = layout.getLineEnd(line); - final int offsetStart = Math.max(lineStart, composingTextStart); - final int offsetEnd = Math.min(lineEnd, composingTextEnd); - final boolean ltrLine = - layout.getParagraphDirection(line) == Layout.DIR_LEFT_TO_RIGHT; - final float[] widths = new float[offsetEnd - offsetStart]; - layout.getPaint().getTextWidths(text, offsetStart, offsetEnd, widths); - final float top = layout.getLineTop(line); - final float bottom = layout.getLineBottom(line); - for (int offset = offsetStart; offset < offsetEnd; ++offset) { - final float charWidth = widths[offset - offsetStart]; - final boolean isRtl = layout.isRtlCharAt(offset); - final float primary = layout.getPrimaryHorizontal(offset); - final float secondary = layout.getSecondaryHorizontal(offset); - // TODO: This doesn't work perfectly for text with custom styles and TAB - // chars. - final float left; - final float right; - if (ltrLine) { - if (isRtl) { - left = secondary - charWidth; - right = secondary; - } else { - left = primary; - right = primary + charWidth; - } - } else { - if (!isRtl) { - left = secondary; - right = secondary + charWidth; - } else { - left = primary - charWidth; - right = primary; - } - } - // TODO: Check top-right and bottom-left as well. - final float localLeft = left + viewportToContentHorizontalOffset; - final float localRight = right + viewportToContentHorizontalOffset; - final float localTop = top + viewportToContentVerticalOffset; - final float localBottom = bottom + viewportToContentVerticalOffset; - final boolean isTopLeftVisible = isPositionVisible(textView, - localLeft, localTop); - final boolean isBottomRightVisible = - isPositionVisible(textView, localRight, localBottom); - int characterBoundsFlags = 0; - if (isTopLeftVisible || isBottomRightVisible) { - characterBoundsFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION; - } - if (!isTopLeftVisible || !isBottomRightVisible) { - characterBoundsFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION; - } - if (isRtl) { - characterBoundsFlags |= CursorAnchorInfo.FLAG_IS_RTL; - } - // Here offset is the index in Java chars. - builder.addCharacterBounds(offset, localLeft, localTop, localRight, - localBottom, characterBoundsFlags); - } - } - } - } - - // Treat selectionStart as the insertion point. - if (0 <= selectionStart) { - final int offset = selectionStart; - final int line = layout.getLineForOffset(offset); - final float insertionMarkerX = layout.getPrimaryHorizontal(offset) - + viewportToContentHorizontalOffset; - final float insertionMarkerTop = layout.getLineTop(line) - + viewportToContentVerticalOffset; - final float insertionMarkerBaseline = layout.getLineBaseline(line) - + viewportToContentVerticalOffset; - final float insertionMarkerBottom = layout.getLineBottom(line) - + viewportToContentVerticalOffset; - final boolean isTopVisible = - isPositionVisible(textView, insertionMarkerX, insertionMarkerTop); - final boolean isBottomVisible = - isPositionVisible(textView, insertionMarkerX, insertionMarkerBottom); - int insertionMarkerFlags = 0; - if (isTopVisible || isBottomVisible) { - insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION; - } - if (!isTopVisible || !isBottomVisible) { - insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION; - } - if (layout.isRtlCharAt(offset)) { - insertionMarkerFlags |= CursorAnchorInfo.FLAG_IS_RTL; - } - builder.setInsertionMarkerLocation(insertionMarkerX, insertionMarkerTop, - insertionMarkerBaseline, insertionMarkerBottom, insertionMarkerFlags); - } - return builder.build(); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/DebugLogUtils.java b/java/src/com/android/inputmethod/latin/utils/DebugLogUtils.java deleted file mode 100644 index 1ab834fc3..000000000 --- a/java/src/com/android/inputmethod/latin/utils/DebugLogUtils.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.android.inputmethod.latin.utils; - -import android.util.Log; - -import com.android.inputmethod.latin.define.DebugFlags; - -/** - * A class for logging and debugging utility methods. - */ -public final class DebugLogUtils { - private final static String TAG = DebugLogUtils.class.getSimpleName(); - private final static boolean sDBG = DebugFlags.DEBUG_ENABLED; - - /** - * Calls .toString() on its non-null argument or returns "null" - * @param o the object to convert to a string - * @return the result of .toString() or null - */ - public static String s(final Object o) { - return null == o ? "null" : o.toString(); - } - - /** - * Get the string representation of the current stack trace, for debugging purposes. - * @return a readable, carriage-return-separated string for the current stack trace. - */ - public static String getStackTrace() { - return getStackTrace(Integer.MAX_VALUE - 1); - } - - /** - * Get the string representation of the current stack trace, for debugging purposes. - * @param limit the maximum number of stack frames to be returned. - * @return a readable, carriage-return-separated string for the current stack trace. - */ - public static String getStackTrace(final int limit) { - final StringBuilder sb = new StringBuilder(); - try { - throw new RuntimeException(); - } catch (final RuntimeException e) { - final StackTraceElement[] frames = e.getStackTrace(); - // Start at 1 because the first frame is here and we don't care about it - for (int j = 1; j < frames.length && j < limit + 1; ++j) { - sb.append(frames[j].toString() + "\n"); - } - } - return sb.toString(); - } - - /** - * Get the stack trace contained in an exception as a human-readable string. - * @param t the throwable - * @return the human-readable stack trace - */ - public static String getStackTrace(final Throwable t) { - final StringBuilder sb = new StringBuilder(); - final StackTraceElement[] frames = t.getStackTrace(); - for (int j = 0; j < frames.length; ++j) { - sb.append(frames[j].toString() + "\n"); - } - return sb.toString(); - } - - /** - * Helper log method to ease null-checks and adding spaces. - * - * This sends all arguments to the log, separated by spaces. Any null argument is converted - * to the "null" string. It uses a very visible tag and log level for debugging purposes. - * - * @param args the stuff to send to the log - */ - public static void l(final Object... args) { - if (!sDBG) return; - final StringBuilder sb = new StringBuilder(); - for (final Object o : args) { - sb.append(s(o).toString()); - sb.append(" "); - } - Log.e(TAG, sb.toString()); - } - - /** - * Helper log method to put stuff in red. - * - * This does the same as #l but prints in red - * - * @param args the stuff to send to the log - */ - public static void r(final Object... args) { - if (!sDBG) return; - final StringBuilder sb = new StringBuilder("\u001B[31m"); - for (final Object o : args) { - sb.append(s(o).toString()); - sb.append(" "); - } - sb.append("\u001B[0m"); - Log.e(TAG, sb.toString()); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/DialogUtils.java b/java/src/com/android/inputmethod/latin/utils/DialogUtils.java deleted file mode 100644 index a05c932d0..000000000 --- a/java/src/com/android/inputmethod/latin/utils/DialogUtils.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.content.Context; -import android.view.ContextThemeWrapper; - -import com.android.inputmethod.latin.R; - -public final class DialogUtils { - private DialogUtils() { - // This utility class is not publicly instantiable. - } - - public static Context getPlatformDialogThemeContext(final Context context) { - // Because {@link AlertDialog.Builder.create()} doesn't honor the specified theme with - // createThemeContextWrapper=false, the result dialog box has unneeded paddings around it. - return new ContextThemeWrapper(context, R.style.platformDialogTheme); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/DictionaryHeaderUtils.java b/java/src/com/android/inputmethod/latin/utils/DictionaryHeaderUtils.java deleted file mode 100644 index 5eb613c9b..000000000 --- a/java/src/com/android/inputmethod/latin/utils/DictionaryHeaderUtils.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2015 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.utils; - -import com.android.inputmethod.latin.AssetFileAddress; -import com.android.inputmethod.latin.makedict.DictionaryHeader; - -import java.io.File; - -public class DictionaryHeaderUtils { - - public static int getContentVersion(AssetFileAddress fileAddress) { - final DictionaryHeader header = DictionaryInfoUtils.getDictionaryFileHeaderOrNull( - new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength); - return Integer.parseInt(header.mVersionString); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java b/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java deleted file mode 100644 index cea2e13b1..000000000 --- a/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java +++ /dev/null @@ -1,613 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.content.ContentValues; -import android.content.Context; -import android.content.res.AssetManager; -import android.content.res.Resources; -import android.text.TextUtils; -import android.util.Log; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.dictionarypack.UpdateHandler; -import com.android.inputmethod.latin.AssetFileAddress; -import com.android.inputmethod.latin.BinaryDictionaryGetter; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.RichInputMethodManager; -import com.android.inputmethod.latin.common.FileUtils; -import com.android.inputmethod.latin.common.LocaleUtils; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; -import com.android.inputmethod.latin.makedict.DictionaryHeader; -import com.android.inputmethod.latin.makedict.UnsupportedFormatException; -import com.android.inputmethod.latin.settings.SpacingAndPunctuations; - -import java.io.File; -import java.io.FilenameFilter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.concurrent.TimeUnit; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * This class encapsulates the logic for the Latin-IME side of dictionary information management. - */ -public class DictionaryInfoUtils { - private static final String TAG = DictionaryInfoUtils.class.getSimpleName(); - public static final String RESOURCE_PACKAGE_NAME = R.class.getPackage().getName(); - private static final String DEFAULT_MAIN_DICT = "main"; - private static final String MAIN_DICT_PREFIX = "main_"; - private static final String DECODER_DICT_SUFFIX = DecoderSpecificConstants.DECODER_DICT_SUFFIX; - // 6 digits - unicode is limited to 21 bits - private static final int MAX_HEX_DIGITS_FOR_CODEPOINT = 6; - - private static final String TEMP_DICT_FILE_SUB = UpdateHandler.TEMP_DICT_FILE_SUB; - - public static class DictionaryInfo { - private static final String LOCALE_COLUMN = "locale"; - private static final String WORDLISTID_COLUMN = "id"; - private static final String LOCAL_FILENAME_COLUMN = "filename"; - private static final String DESCRIPTION_COLUMN = "description"; - private static final String DATE_COLUMN = "date"; - private static final String FILESIZE_COLUMN = "filesize"; - private static final String VERSION_COLUMN = "version"; - - @Nonnull public final String mId; - @Nonnull public final Locale mLocale; - @Nullable public final String mDescription; - @Nullable public final String mFilename; - public final long mFilesize; - public final long mModifiedTimeMillis; - public final int mVersion; - - public DictionaryInfo(@Nonnull String id, @Nonnull Locale locale, - @Nullable String description, @Nullable String filename, - long filesize, long modifiedTimeMillis, int version) { - mId = id; - mLocale = locale; - mDescription = description; - mFilename = filename; - mFilesize = filesize; - mModifiedTimeMillis = modifiedTimeMillis; - mVersion = version; - } - - public ContentValues toContentValues() { - final ContentValues values = new ContentValues(); - values.put(WORDLISTID_COLUMN, mId); - values.put(LOCALE_COLUMN, mLocale.toString()); - values.put(DESCRIPTION_COLUMN, mDescription); - values.put(LOCAL_FILENAME_COLUMN, mFilename != null ? mFilename : ""); - values.put(DATE_COLUMN, TimeUnit.MILLISECONDS.toSeconds(mModifiedTimeMillis)); - values.put(FILESIZE_COLUMN, mFilesize); - values.put(VERSION_COLUMN, mVersion); - return values; - } - - @Override - public String toString() { - return "DictionaryInfo : Id = '" + mId - + "' : Locale=" + mLocale - + " : Version=" + mVersion; - } - } - - private DictionaryInfoUtils() { - // Private constructor to forbid instantation of this helper class. - } - - /** - * Returns whether we may want to use this character as part of a file name. - * - * This basically only accepts ascii letters and numbers, and rejects everything else. - */ - private static boolean isFileNameCharacter(int codePoint) { - if (codePoint >= 0x30 && codePoint <= 0x39) return true; // Digit - if (codePoint >= 0x41 && codePoint <= 0x5A) return true; // Uppercase - if (codePoint >= 0x61 && codePoint <= 0x7A) return true; // Lowercase - return codePoint == '_'; // Underscore - } - - /** - * Escapes a string for any characters that may be suspicious for a file or directory name. - * - * Concretely this does a sort of URL-encoding except it will encode everything that's not - * alphanumeric or underscore. (true URL-encoding leaves alone characters like '*', which - * we cannot allow here) - */ - // TODO: create a unit test for this method - public static String replaceFileNameDangerousCharacters(final String name) { - // This assumes '%' is fully available as a non-separator, normal - // character in a file name. This is probably true for all file systems. - final StringBuilder sb = new StringBuilder(); - final int nameLength = name.length(); - for (int i = 0; i < nameLength; i = name.offsetByCodePoints(i, 1)) { - final int codePoint = name.codePointAt(i); - if (DictionaryInfoUtils.isFileNameCharacter(codePoint)) { - sb.appendCodePoint(codePoint); - } else { - sb.append(String.format((Locale)null, "%%%1$0" + MAX_HEX_DIGITS_FOR_CODEPOINT + "x", - codePoint)); - } - } - return sb.toString(); - } - - /** - * Helper method to get the top level cache directory. - */ - private static String getWordListCacheDirectory(final Context context) { - return context.getFilesDir() + File.separator + "dicts"; - } - - /** - * Helper method to get the top level cache directory. - */ - public static String getWordListStagingDirectory(final Context context) { - return context.getFilesDir() + File.separator + "staging"; - } - - /** - * Helper method to get the top level temp directory. - */ - public static String getWordListTempDirectory(final Context context) { - return context.getFilesDir() + File.separator + "tmp"; - } - - /** - * Reverse escaping done by {@link #replaceFileNameDangerousCharacters(String)}. - */ - @Nonnull - public static String getWordListIdFromFileName(@Nonnull final String fname) { - final StringBuilder sb = new StringBuilder(); - final int fnameLength = fname.length(); - for (int i = 0; i < fnameLength; i = fname.offsetByCodePoints(i, 1)) { - final int codePoint = fname.codePointAt(i); - if ('%' != codePoint) { - sb.appendCodePoint(codePoint); - } else { - // + 1 to pass the % sign - final int encodedCodePoint = Integer.parseInt( - fname.substring(i + 1, i + 1 + MAX_HEX_DIGITS_FOR_CODEPOINT), 16); - i += MAX_HEX_DIGITS_FOR_CODEPOINT; - sb.appendCodePoint(encodedCodePoint); - } - } - return sb.toString(); - } - - /** - * Helper method to the list of cache directories, one for each distinct locale. - */ - public static File[] getCachedDirectoryList(final Context context) { - return new File(DictionaryInfoUtils.getWordListCacheDirectory(context)).listFiles(); - } - - public static File[] getStagingDirectoryList(final Context context) { - return new File(DictionaryInfoUtils.getWordListStagingDirectory(context)).listFiles(); - } - - @Nullable - public static File[] getUnusedDictionaryList(final Context context) { - return context.getFilesDir().listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String filename) { - return !TextUtils.isEmpty(filename) && filename.endsWith(".dict") - && filename.contains(TEMP_DICT_FILE_SUB); - } - }); - } - - /** - * Returns the category for a given file name. - * - * This parses the file name, extracts the category, and returns it. See - * {@link #getMainDictId(Locale)} and {@link #isMainWordListId(String)}. - * @return The category as a string or null if it can't be found in the file name. - */ - @Nullable - public static String getCategoryFromFileName(@Nonnull final String fileName) { - final String id = getWordListIdFromFileName(fileName); - final String[] idArray = id.split(BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR); - // An id is supposed to be in format category:locale, so splitting on the separator - // should yield a 2-elements array - if (2 != idArray.length) { - return null; - } - return idArray[0]; - } - - /** - * Find out the cache directory associated with a specific locale. - */ - public static String getCacheDirectoryForLocale(final String locale, final Context context) { - final String relativeDirectoryName = replaceFileNameDangerousCharacters(locale); - final String absoluteDirectoryName = getWordListCacheDirectory(context) + File.separator - + relativeDirectoryName; - final File directory = new File(absoluteDirectoryName); - if (!directory.exists()) { - if (!directory.mkdirs()) { - Log.e(TAG, "Could not create the directory for locale" + locale); - } - } - return absoluteDirectoryName; - } - - /** - * Generates a file name for the id and locale passed as an argument. - * - * In the current implementation the file name returned will always be unique for - * any id/locale pair, but please do not expect that the id can be the same for - * different dictionaries with different locales. An id should be unique for any - * dictionary. - * The file name is pretty much an URL-encoded version of the id inside a directory - * named like the locale, except it will also escape characters that look dangerous - * to some file systems. - * @param id the id of the dictionary for which to get a file name - * @param locale the locale for which to get the file name as a string - * @param context the context to use for getting the directory - * @return the name of the file to be created - */ - public static String getCacheFileName(String id, String locale, Context context) { - final String fileName = replaceFileNameDangerousCharacters(id); - return getCacheDirectoryForLocale(locale, context) + File.separator + fileName; - } - - public static String getStagingFileName(String id, String locale, Context context) { - final String stagingDirectory = getWordListStagingDirectory(context); - // create the directory if it does not exist. - final File directory = new File(stagingDirectory); - if (!directory.exists()) { - if (!directory.mkdirs()) { - Log.e(TAG, "Could not create the staging directory."); - } - } - // e.g. id="main:en_in", locale ="en_IN" - final String fileName = replaceFileNameDangerousCharacters( - locale + TEMP_DICT_FILE_SUB + id); - return stagingDirectory + File.separator + fileName; - } - - public static void moveStagingFilesIfExists(Context context) { - final File[] stagingFiles = DictionaryInfoUtils.getStagingDirectoryList(context); - if (stagingFiles != null && stagingFiles.length > 0) { - for (final File stagingFile : stagingFiles) { - final String fileName = stagingFile.getName(); - final int index = fileName.indexOf(TEMP_DICT_FILE_SUB); - if (index == -1) { - // This should never happen. - Log.e(TAG, "Staging file does not have ___ substring."); - continue; - } - final String[] localeAndFileId = fileName.split(TEMP_DICT_FILE_SUB); - if (localeAndFileId.length != 2) { - Log.e(TAG, String.format("malformed staging file %s. Deleting.", - stagingFile.getAbsoluteFile())); - stagingFile.delete(); - continue; - } - - final String locale = localeAndFileId[0]; - // already escaped while moving to staging. - final String fileId = localeAndFileId[1]; - final String cacheDirectoryForLocale = getCacheDirectoryForLocale(locale, context); - final String cacheFilename = cacheDirectoryForLocale + File.separator + fileId; - final File cacheFile = new File(cacheFilename); - // move the staging file to cache file. - if (!FileUtils.renameTo(stagingFile, cacheFile)) { - Log.e(TAG, String.format("Failed to rename from %s to %s.", - stagingFile.getAbsoluteFile(), cacheFile.getAbsoluteFile())); - } - } - } - } - - public static boolean isMainWordListId(final String id) { - final String[] idArray = id.split(BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR); - // An id is supposed to be in format category:locale, so splitting on the separator - // should yield a 2-elements array - if (2 != idArray.length) { - return false; - } - return BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY.equals(idArray[0]); - } - - /** - * Find out whether a dictionary is available for this locale. - * @param context the context on which to check resources. - * @param locale the locale to check for. - * @return whether a (non-placeholder) dictionary is available or not. - */ - public static boolean isDictionaryAvailable(final Context context, final Locale locale) { - final Resources res = context.getResources(); - return 0 != getMainDictionaryResourceIdIfAvailableForLocale(res, locale); - } - - /** - * Helper method to return a dictionary res id for a locale, or 0 if none. - * @param res resources for the app - * @param locale dictionary locale - * @return main dictionary resource id - */ - public static int getMainDictionaryResourceIdIfAvailableForLocale(final Resources res, - final Locale locale) { - int resId; - // Try to find main_language_country dictionary. - if (!locale.getCountry().isEmpty()) { - final String dictLanguageCountry = MAIN_DICT_PREFIX - + locale.toString().toLowerCase(Locale.ROOT) + DECODER_DICT_SUFFIX; - if ((resId = res.getIdentifier( - dictLanguageCountry, "raw", RESOURCE_PACKAGE_NAME)) != 0) { - return resId; - } - } - - // Try to find main_language dictionary. - final String dictLanguage = MAIN_DICT_PREFIX + locale.getLanguage() + DECODER_DICT_SUFFIX; - if ((resId = res.getIdentifier(dictLanguage, "raw", RESOURCE_PACKAGE_NAME)) != 0) { - return resId; - } - - // Not found, return 0 - return 0; - } - - /** - * Returns a main dictionary resource id - * @param res resources for the app - * @param locale dictionary locale - * @return main dictionary resource id - */ - public static int getMainDictionaryResourceId(final Resources res, final Locale locale) { - int resourceId = getMainDictionaryResourceIdIfAvailableForLocale(res, locale); - if (0 != resourceId) { - return resourceId; - } - return res.getIdentifier(DEFAULT_MAIN_DICT + DecoderSpecificConstants.DECODER_DICT_SUFFIX, - "raw", RESOURCE_PACKAGE_NAME); - } - - /** - * Returns the id associated with the main word list for a specified locale. - * - * Word lists stored in Android Keyboard's resources are referred to as the "main" - * word lists. Since they can be updated like any other list, we need to assign a - * unique ID to them. This ID is just the name of the language (locale-wise) they - * are for, and this method returns this ID. - */ - public static String getMainDictId(@Nonnull final Locale locale) { - // This works because we don't include by default different dictionaries for - // different countries. This actually needs to return the id that we would - // like to use for word lists included in resources, and the following is okay. - return BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY + - BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR + locale.toString().toLowerCase(); - } - - public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file, - final long offset, final long length) { - try { - final DictionaryHeader header = - BinaryDictionaryUtils.getHeaderWithOffsetAndLength(file, offset, length); - return header; - } catch (UnsupportedFormatException e) { - return null; - } catch (IOException e) { - return null; - } - } - - /** - * Returns information of the dictionary. - * - * @param fileAddress the asset dictionary file address. - * @param locale Locale for this file. - * @return information of the specified dictionary. - */ - private static DictionaryInfo createDictionaryInfoFromFileAddress( - @Nonnull final AssetFileAddress fileAddress, final Locale locale) { - final String id = getMainDictId(locale); - final int version = DictionaryHeaderUtils.getContentVersion(fileAddress); - final String description = SubtypeLocaleUtils - .getSubtypeLocaleDisplayName(locale.toString()); - // Do not store the filename on db as it will try to move the filename from db to the - // cached directory. If the filename is already in cached directory, this is not - // necessary. - final String filenameToStoreOnDb = null; - return new DictionaryInfo(id, locale, description, filenameToStoreOnDb, - fileAddress.mLength, new File(fileAddress.mFilename).lastModified(), version); - } - - /** - * Returns the information of the dictionary for the given {@link AssetFileAddress}. - * If the file is corrupted or a pre-fava file, then the file gets deleted and the null - * value is returned. - */ - @Nullable - private static DictionaryInfo createDictionaryInfoForUnCachedFile( - @Nonnull final AssetFileAddress fileAddress, final Locale locale) { - final String id = getMainDictId(locale); - final int version = DictionaryHeaderUtils.getContentVersion(fileAddress); - - if (version == -1) { - // Purge the pre-fava/corrupted unused dictionaires. - fileAddress.deleteUnderlyingFile(); - return null; - } - - final String description = SubtypeLocaleUtils - .getSubtypeLocaleDisplayName(locale.toString()); - - final File unCachedFile = new File(fileAddress.mFilename); - // Store just the filename and not the full path. - final String filenameToStoreOnDb = unCachedFile.getName(); - return new DictionaryInfo(id, locale, description, filenameToStoreOnDb, fileAddress.mLength, - unCachedFile.lastModified(), version); - } - - /** - * Returns dictionary information for the given locale. - */ - private static DictionaryInfo createDictionaryInfoFromLocale(Locale locale) { - final String id = getMainDictId(locale); - final int version = -1; - final String description = SubtypeLocaleUtils - .getSubtypeLocaleDisplayName(locale.toString()); - return new DictionaryInfo(id, locale, description, null, 0L, 0L, version); - } - - private static void addOrUpdateDictInfo(final ArrayList<DictionaryInfo> dictList, - final DictionaryInfo newElement) { - final Iterator<DictionaryInfo> iter = dictList.iterator(); - while (iter.hasNext()) { - final DictionaryInfo thisDictInfo = iter.next(); - if (thisDictInfo.mLocale.equals(newElement.mLocale)) { - if (newElement.mVersion <= thisDictInfo.mVersion) { - return; - } - iter.remove(); - } - } - dictList.add(newElement); - } - - public static ArrayList<DictionaryInfo> getCurrentDictionaryFileNameAndVersionInfo( - final Context context) { - final ArrayList<DictionaryInfo> dictList = new ArrayList<>(); - - // Retrieve downloaded dictionaries from cached directories - final File[] directoryList = getCachedDirectoryList(context); - if (null != directoryList) { - for (final File directory : directoryList) { - final String localeString = getWordListIdFromFileName(directory.getName()); - final File[] dicts = BinaryDictionaryGetter.getCachedWordLists( - localeString, context); - for (final File dict : dicts) { - final String wordListId = getWordListIdFromFileName(dict.getName()); - if (!DictionaryInfoUtils.isMainWordListId(wordListId)) { - continue; - } - final Locale locale = LocaleUtils.constructLocaleFromString(localeString); - final AssetFileAddress fileAddress = AssetFileAddress.makeFromFile(dict); - final DictionaryInfo dictionaryInfo = - createDictionaryInfoFromFileAddress(fileAddress, locale); - // Protect against cases of a less-specific dictionary being found, like an - // en dictionary being used for an en_US locale. In this case, the en dictionary - // should be used for en_US but discounted for listing purposes. - if (dictionaryInfo == null || !dictionaryInfo.mLocale.equals(locale)) { - continue; - } - addOrUpdateDictInfo(dictList, dictionaryInfo); - } - } - } - - // Retrieve downloaded dictionaries from the unused dictionaries. - File[] unusedDictionaryList = getUnusedDictionaryList(context); - if (unusedDictionaryList != null) { - for (File dictionaryFile : unusedDictionaryList) { - String fileName = dictionaryFile.getName(); - int index = fileName.indexOf(TEMP_DICT_FILE_SUB); - if (index == -1) { - continue; - } - String locale = fileName.substring(0, index); - DictionaryInfo dictionaryInfo = createDictionaryInfoForUnCachedFile( - AssetFileAddress.makeFromFile(dictionaryFile), - LocaleUtils.constructLocaleFromString(locale)); - if (dictionaryInfo != null) { - addOrUpdateDictInfo(dictList, dictionaryInfo); - } - } - } - - // Retrieve files from assets - final Resources resources = context.getResources(); - final AssetManager assets = resources.getAssets(); - for (final String localeString : assets.getLocales()) { - final Locale locale = LocaleUtils.constructLocaleFromString(localeString); - final int resourceId = - DictionaryInfoUtils.getMainDictionaryResourceIdIfAvailableForLocale( - context.getResources(), locale); - if (0 == resourceId) { - continue; - } - final AssetFileAddress fileAddress = - BinaryDictionaryGetter.loadFallbackResource(context, resourceId); - final DictionaryInfo dictionaryInfo = createDictionaryInfoFromFileAddress(fileAddress, - locale); - // Protect against cases of a less-specific dictionary being found, like an - // en dictionary being used for an en_US locale. In this case, the en dictionary - // should be used for en_US but discounted for listing purposes. - // TODO: Remove dictionaryInfo == null when the static LMs have the headers. - if (dictionaryInfo == null || !dictionaryInfo.mLocale.equals(locale)) { - continue; - } - addOrUpdateDictInfo(dictList, dictionaryInfo); - } - - // Generate the dictionary information from the enabled subtypes. This will not - // overwrite the real records. - RichInputMethodManager.init(context); - List<InputMethodSubtype> enabledSubtypes = RichInputMethodManager - .getInstance().getMyEnabledInputMethodSubtypeList(true); - for (InputMethodSubtype subtype : enabledSubtypes) { - Locale locale = LocaleUtils.constructLocaleFromString(subtype.getLocale()); - DictionaryInfo dictionaryInfo = createDictionaryInfoFromLocale(locale); - addOrUpdateDictInfo(dictList, dictionaryInfo); - } - - return dictList; - } - - @UsedForTesting - public static boolean looksValidForDictionaryInsertion(final CharSequence text, - final SpacingAndPunctuations spacingAndPunctuations) { - if (TextUtils.isEmpty(text)) { - return false; - } - final int length = text.length(); - if (length > DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH) { - return false; - } - int i = 0; - int digitCount = 0; - while (i < length) { - final int codePoint = Character.codePointAt(text, i); - final int charCount = Character.charCount(codePoint); - i += charCount; - if (Character.isDigit(codePoint)) { - // Count digits: see below - digitCount += charCount; - continue; - } - if (!spacingAndPunctuations.isWordCodePoint(codePoint)) { - return false; - } - } - // We reject strings entirely comprised of digits to avoid using PIN codes or credit - // card numbers. It would come in handy for word prediction though; a good example is - // when writing one's address where the street number is usually quite discriminative, - // as well as the postal code. - return digitCount < length; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java b/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java deleted file mode 100644 index 8ce6eff92..000000000 --- a/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.util.Log; - -import com.android.inputmethod.annotations.UsedForTesting; - -import java.lang.Thread.UncaughtExceptionHandler; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; - -/** - * Utilities to manage executors. - */ -public class ExecutorUtils { - - private static final String TAG = "ExecutorUtils"; - - public static final String KEYBOARD = "Keyboard"; - public static final String SPELLING = "Spelling"; - - private static ScheduledExecutorService sKeyboardExecutorService = newExecutorService(KEYBOARD); - private static ScheduledExecutorService sSpellingExecutorService = newExecutorService(SPELLING); - - private static ScheduledExecutorService newExecutorService(final String name) { - return Executors.newSingleThreadScheduledExecutor(new ExecutorFactory(name)); - } - - private static class ExecutorFactory implements ThreadFactory { - private final String mName; - - private ExecutorFactory(final String name) { - mName = name; - } - - @Override - public Thread newThread(final Runnable runnable) { - Thread thread = new Thread(runnable, TAG); - thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { - @Override - public void uncaughtException(Thread thread, Throwable ex) { - Log.w(mName + "-" + runnable.getClass().getSimpleName(), ex); - } - }); - return thread; - } - } - - @UsedForTesting - private static ScheduledExecutorService sExecutorServiceForTests; - - @UsedForTesting - public static void setExecutorServiceForTests( - final ScheduledExecutorService executorServiceForTests) { - sExecutorServiceForTests = executorServiceForTests; - } - - // - // Public methods used to schedule a runnable for execution. - // - - /** - * @param name Executor's name. - * @return scheduled executor service used to run background tasks - */ - public static ScheduledExecutorService getBackgroundExecutor(final String name) { - if (sExecutorServiceForTests != null) { - return sExecutorServiceForTests; - } - switch (name) { - case KEYBOARD: - return sKeyboardExecutorService; - case SPELLING: - return sSpellingExecutorService; - default: - throw new IllegalArgumentException("Invalid executor: " + name); - } - } - - public static void killTasks(final String name) { - final ScheduledExecutorService executorService = getBackgroundExecutor(name); - executorService.shutdownNow(); - try { - executorService.awaitTermination(5, TimeUnit.SECONDS); - } catch (InterruptedException e) { - Log.wtf(TAG, "Failed to shut down: " + name); - } - if (executorService == sExecutorServiceForTests) { - // Don't do anything to the test service. - return; - } - switch (name) { - case KEYBOARD: - sKeyboardExecutorService = newExecutorService(KEYBOARD); - break; - case SPELLING: - sSpellingExecutorService = newExecutorService(SPELLING); - break; - default: - throw new IllegalArgumentException("Invalid executor: " + name); - } - } - - @UsedForTesting - public static Runnable chain(final Runnable... runnables) { - return new RunnableChain(runnables); - } - - @UsedForTesting - public static class RunnableChain implements Runnable { - private final Runnable[] mRunnables; - - private RunnableChain(final Runnable... runnables) { - if (runnables == null || runnables.length == 0) { - throw new IllegalArgumentException("Attempting to construct an empty chain"); - } - mRunnables = runnables; - } - - @UsedForTesting - public Runnable[] getRunnables() { - return mRunnables; - } - - @Override - public void run() { - for (Runnable runnable : mRunnables) { - if (Thread.interrupted()) { - return; - } - runnable.run(); - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/FeedbackUtils.java b/java/src/com/android/inputmethod/latin/utils/FeedbackUtils.java deleted file mode 100644 index 67de8ba32..000000000 --- a/java/src/com/android/inputmethod/latin/utils/FeedbackUtils.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.content.Context; -import android.content.Intent; - -@SuppressWarnings("unused") -public class FeedbackUtils { - public static boolean isHelpAndFeedbackFormSupported() { - return false; - } - - public static void showHelpAndFeedbackForm(Context context) { - } - - public static int getAboutKeyboardTitleResId() { - return 0; - } - - public static Intent getAboutKeyboardIntent(Context context) { - return null; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/FileTransforms.java b/java/src/com/android/inputmethod/latin/utils/FileTransforms.java deleted file mode 100644 index 9f4584ec9..000000000 --- a/java/src/com/android/inputmethod/latin/utils/FileTransforms.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.utils; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.zip.GZIPInputStream; - -public final class FileTransforms { - public static OutputStream getCryptedStream(OutputStream out) { - // Crypt the stream. - return out; - } - - public static InputStream getDecryptedStream(InputStream in) { - // Decrypt the stream. - return in; - } - - public static InputStream getUncompressedStream(InputStream in) throws IOException { - return new GZIPInputStream(in); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java b/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java deleted file mode 100644 index 147e57b13..000000000 --- a/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import com.android.inputmethod.dictionarypack.DictionarySettingsFragment; -import com.android.inputmethod.latin.about.AboutPreferences; -import com.android.inputmethod.latin.settings.AccountsSettingsFragment; -import com.android.inputmethod.latin.settings.AdvancedSettingsFragment; -import com.android.inputmethod.latin.settings.AppearanceSettingsFragment; -import com.android.inputmethod.latin.settings.CorrectionSettingsFragment; -import com.android.inputmethod.latin.settings.CustomInputStyleSettingsFragment; -import com.android.inputmethod.latin.settings.DebugSettingsFragment; -import com.android.inputmethod.latin.settings.GestureSettingsFragment; -import com.android.inputmethod.latin.settings.PreferencesSettingsFragment; -import com.android.inputmethod.latin.settings.SettingsFragment; -import com.android.inputmethod.latin.settings.ThemeSettingsFragment; -import com.android.inputmethod.latin.spellcheck.SpellCheckerSettingsFragment; -import com.android.inputmethod.latin.userdictionary.UserDictionaryAddWordFragment; -import com.android.inputmethod.latin.userdictionary.UserDictionaryList; -import com.android.inputmethod.latin.userdictionary.UserDictionaryLocalePicker; -import com.android.inputmethod.latin.userdictionary.UserDictionarySettings; - -import java.util.HashSet; - -public class FragmentUtils { - private static final HashSet<String> sLatinImeFragments = new HashSet<>(); - static { - sLatinImeFragments.add(DictionarySettingsFragment.class.getName()); - sLatinImeFragments.add(AboutPreferences.class.getName()); - sLatinImeFragments.add(PreferencesSettingsFragment.class.getName()); - sLatinImeFragments.add(AccountsSettingsFragment.class.getName()); - sLatinImeFragments.add(AppearanceSettingsFragment.class.getName()); - sLatinImeFragments.add(ThemeSettingsFragment.class.getName()); - sLatinImeFragments.add(CustomInputStyleSettingsFragment.class.getName()); - sLatinImeFragments.add(GestureSettingsFragment.class.getName()); - sLatinImeFragments.add(CorrectionSettingsFragment.class.getName()); - sLatinImeFragments.add(AdvancedSettingsFragment.class.getName()); - sLatinImeFragments.add(DebugSettingsFragment.class.getName()); - sLatinImeFragments.add(SettingsFragment.class.getName()); - sLatinImeFragments.add(SpellCheckerSettingsFragment.class.getName()); - sLatinImeFragments.add(UserDictionaryAddWordFragment.class.getName()); - sLatinImeFragments.add(UserDictionaryList.class.getName()); - sLatinImeFragments.add(UserDictionaryLocalePicker.class.getName()); - sLatinImeFragments.add(UserDictionarySettings.class.getName()); - } - - public static boolean isValidFragment(String fragmentName) { - return sLatinImeFragments.contains(fragmentName); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java b/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java deleted file mode 100644 index cea263b3b..000000000 --- a/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.Manifest; -import android.content.Context; -import android.content.SharedPreferences; -import android.provider.Settings; -import android.provider.Settings.SettingNotFoundException; -import android.text.TextUtils; -import android.util.Log; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.permissions.PermissionsUtil; -import com.android.inputmethod.latin.settings.SettingsValues; - -import java.util.concurrent.TimeUnit; - -public final class ImportantNoticeUtils { - private static final String TAG = ImportantNoticeUtils.class.getSimpleName(); - - // {@link SharedPreferences} name to save the last important notice version that has been - // displayed to users. - private static final String PREFERENCE_NAME = "important_notice_pref"; - - private static final String KEY_SUGGEST_CONTACTS_NOTICE = "important_notice_suggest_contacts"; - - @UsedForTesting - static final String KEY_TIMESTAMP_OF_CONTACTS_NOTICE = "timestamp_of_suggest_contacts_notice"; - - @UsedForTesting - static final long TIMEOUT_OF_IMPORTANT_NOTICE = TimeUnit.HOURS.toMillis(23); - - // Copy of the hidden {@link Settings.Secure#USER_SETUP_COMPLETE} settings key. - // The value is zero until each multiuser completes system setup wizard. - // Caveat: This is a hidden API. - private static final String Settings_Secure_USER_SETUP_COMPLETE = "user_setup_complete"; - private static final int USER_SETUP_IS_NOT_COMPLETE = 0; - - private ImportantNoticeUtils() { - // This utility class is not publicly instantiable. - } - - @UsedForTesting - static boolean isInSystemSetupWizard(final Context context) { - try { - final int userSetupComplete = Settings.Secure.getInt( - context.getContentResolver(), Settings_Secure_USER_SETUP_COMPLETE); - return userSetupComplete == USER_SETUP_IS_NOT_COMPLETE; - } catch (final SettingNotFoundException e) { - Log.w(TAG, "Can't find settings in Settings.Secure: key=" - + Settings_Secure_USER_SETUP_COMPLETE); - return false; - } - } - - @UsedForTesting - static SharedPreferences getImportantNoticePreferences(final Context context) { - return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); - } - - @UsedForTesting - static boolean hasContactsNoticeShown(final Context context) { - return getImportantNoticePreferences(context).getBoolean( - KEY_SUGGEST_CONTACTS_NOTICE, false); - } - - public static boolean shouldShowImportantNotice(final Context context, - final SettingsValues settingsValues) { - // Check to see whether "Use Contacts" is enabled by the user. - if (!settingsValues.mUseContactsDict) { - return false; - } - - if (hasContactsNoticeShown(context)) { - return false; - } - - // Don't show the dialog if we have all the permissions. - if (PermissionsUtil.checkAllPermissionsGranted( - context, Manifest.permission.READ_CONTACTS)) { - return false; - } - - final String importantNoticeTitle = getSuggestContactsNoticeTitle(context); - if (TextUtils.isEmpty(importantNoticeTitle)) { - return false; - } - if (isInSystemSetupWizard(context)) { - return false; - } - if (hasContactsNoticeTimeoutPassed(context, System.currentTimeMillis())) { - updateContactsNoticeShown(context); - return false; - } - return true; - } - - public static String getSuggestContactsNoticeTitle(final Context context) { - return context.getResources().getString(R.string.important_notice_suggest_contact_names); - } - - @UsedForTesting - static boolean hasContactsNoticeTimeoutPassed( - final Context context, final long currentTimeInMillis) { - final SharedPreferences prefs = getImportantNoticePreferences(context); - if (!prefs.contains(KEY_TIMESTAMP_OF_CONTACTS_NOTICE)) { - prefs.edit() - .putLong(KEY_TIMESTAMP_OF_CONTACTS_NOTICE, currentTimeInMillis) - .apply(); - } - final long firstDisplayTimeInMillis = prefs.getLong( - KEY_TIMESTAMP_OF_CONTACTS_NOTICE, currentTimeInMillis); - final long elapsedTime = currentTimeInMillis - firstDisplayTimeInMillis; - return elapsedTime >= TIMEOUT_OF_IMPORTANT_NOTICE; - } - - public static void updateContactsNoticeShown(final Context context) { - getImportantNoticePreferences(context) - .edit() - .putBoolean(KEY_SUGGEST_CONTACTS_NOTICE, true) - .remove(KEY_TIMESTAMP_OF_CONTACTS_NOTICE) - .apply(); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/InputTypeUtils.java b/java/src/com/android/inputmethod/latin/utils/InputTypeUtils.java deleted file mode 100644 index 19cd34011..000000000 --- a/java/src/com/android/inputmethod/latin/utils/InputTypeUtils.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * 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.utils; - -import android.text.InputType; -import android.view.inputmethod.EditorInfo; - -public final class InputTypeUtils implements InputType { - private static final int WEB_TEXT_PASSWORD_INPUT_TYPE = - TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_PASSWORD; - private static final int WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE = - TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS; - private static final int NUMBER_PASSWORD_INPUT_TYPE = - TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD; - private static final int TEXT_PASSWORD_INPUT_TYPE = - TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD; - private static final int TEXT_VISIBLE_PASSWORD_INPUT_TYPE = - TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; - private static final int[] SUPPRESSING_AUTO_SPACES_FIELD_VARIATION = { - InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS, - InputType.TYPE_TEXT_VARIATION_PASSWORD, - InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD, - InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD }; - public static final int IME_ACTION_CUSTOM_LABEL = EditorInfo.IME_MASK_ACTION + 1; - - private InputTypeUtils() { - // This utility class is not publicly instantiable. - } - - private static boolean isWebEditTextInputType(final int inputType) { - return inputType == (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); - } - - private static boolean isWebPasswordInputType(final int inputType) { - return WEB_TEXT_PASSWORD_INPUT_TYPE != 0 - && inputType == WEB_TEXT_PASSWORD_INPUT_TYPE; - } - - private static boolean isWebEmailAddressInputType(final int inputType) { - return WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE != 0 - && inputType == WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE; - } - - private static boolean isNumberPasswordInputType(final int inputType) { - return NUMBER_PASSWORD_INPUT_TYPE != 0 - && inputType == NUMBER_PASSWORD_INPUT_TYPE; - } - - private static boolean isTextPasswordInputType(final int inputType) { - return inputType == TEXT_PASSWORD_INPUT_TYPE; - } - - private static boolean isWebEmailAddressVariation(int variation) { - return variation == TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS; - } - - public static boolean isEmailVariation(final int variation) { - return variation == TYPE_TEXT_VARIATION_EMAIL_ADDRESS - || isWebEmailAddressVariation(variation); - } - - public static boolean isWebInputType(final int inputType) { - final int maskedInputType = - inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); - return isWebEditTextInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) - || isWebEmailAddressInputType(maskedInputType); - } - - // Please refer to TextView.isPasswordInputType - public static boolean isPasswordInputType(final int inputType) { - final int maskedInputType = - inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); - return isTextPasswordInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) - || isNumberPasswordInputType(maskedInputType); - } - - // Please refer to TextView.isVisiblePasswordInputType - public static boolean isVisiblePasswordInputType(final int inputType) { - final int maskedInputType = - inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); - return maskedInputType == TEXT_VISIBLE_PASSWORD_INPUT_TYPE; - } - - public static boolean isAutoSpaceFriendlyType(final int inputType) { - if (TYPE_CLASS_TEXT != (TYPE_MASK_CLASS & inputType)) return false; - final int variation = TYPE_MASK_VARIATION & inputType; - for (final int fieldVariation : SUPPRESSING_AUTO_SPACES_FIELD_VARIATION) { - if (variation == fieldVariation) return false; - } - return true; - } - - public static int getImeOptionsActionIdFromEditorInfo(final EditorInfo editorInfo) { - if ((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) { - return EditorInfo.IME_ACTION_NONE; - } else if (editorInfo.actionLabel != null) { - return IME_ACTION_CUSTOM_LABEL; - } else { - // Note: this is different from editorInfo.actionId, hence "ImeOptionsActionId" - return editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION; - } - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/IntentUtils.java b/java/src/com/android/inputmethod/latin/utils/IntentUtils.java deleted file mode 100644 index ea0168117..000000000 --- a/java/src/com/android/inputmethod/latin/utils/IntentUtils.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.content.Intent; -import android.text.TextUtils; - -public final class IntentUtils { - private static final String EXTRA_INPUT_METHOD_ID = "input_method_id"; - // TODO: Can these be constants instead of literal String constants? - private static final String INPUT_METHOD_SUBTYPE_SETTINGS = - "android.settings.INPUT_METHOD_SUBTYPE_SETTINGS"; - - private IntentUtils() { - // This utility class is not publicly instantiable. - } - - public static Intent getInputLanguageSelectionIntent(final String inputMethodId, - final int flagsForSubtypeSettings) { - // Refer to android.provider.Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS - final String action = INPUT_METHOD_SUBTYPE_SETTINGS; - final Intent intent = new Intent(action); - if (!TextUtils.isEmpty(inputMethodId)) { - intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId); - } - if (flagsForSubtypeSettings > 0) { - intent.setFlags(flagsForSubtypeSettings); - } - return intent; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/JniUtils.java b/java/src/com/android/inputmethod/latin/utils/JniUtils.java deleted file mode 100644 index e7fdafaeb..000000000 --- a/java/src/com/android/inputmethod/latin/utils/JniUtils.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.utils; - -import android.util.Log; - -import com.android.inputmethod.latin.define.JniLibName; - -public final class JniUtils { - private static final String TAG = JniUtils.class.getSimpleName(); - - static { - try { - System.loadLibrary(JniLibName.JNI_LIB_NAME); - } catch (UnsatisfiedLinkError ule) { - Log.e(TAG, "Could not load native library " + JniLibName.JNI_LIB_NAME, ule); - } - } - - private JniUtils() { - // This utility class is not publicly instantiable. - } - - public static void loadNativeLibrary() { - // Ensures the static initializer is called - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/JsonUtils.java b/java/src/com/android/inputmethod/latin/utils/JsonUtils.java deleted file mode 100644 index 6dd8d9711..000000000 --- a/java/src/com/android/inputmethod/latin/utils/JsonUtils.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.util.JsonReader; -import android.util.JsonWriter; -import android.util.Log; - -import java.io.Closeable; -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public final class JsonUtils { - private static final String TAG = JsonUtils.class.getSimpleName(); - - private static final String INTEGER_CLASS_NAME = Integer.class.getSimpleName(); - private static final String STRING_CLASS_NAME = String.class.getSimpleName(); - - private static final String EMPTY_STRING = ""; - - public static List<Object> jsonStrToList(final String s) { - final ArrayList<Object> list = new ArrayList<>(); - final JsonReader reader = new JsonReader(new StringReader(s)); - try { - reader.beginArray(); - while (reader.hasNext()) { - reader.beginObject(); - while (reader.hasNext()) { - final String name = reader.nextName(); - if (name.equals(INTEGER_CLASS_NAME)) { - list.add(reader.nextInt()); - } else if (name.equals(STRING_CLASS_NAME)) { - list.add(reader.nextString()); - } else { - Log.w(TAG, "Invalid name: " + name); - reader.skipValue(); - } - } - reader.endObject(); - } - reader.endArray(); - return list; - } catch (final IOException e) { - } finally { - close(reader); - } - return Collections.<Object>emptyList(); - } - - public static String listToJsonStr(final List<Object> list) { - if (list == null || list.isEmpty()) { - return EMPTY_STRING; - } - final StringWriter sw = new StringWriter(); - final JsonWriter writer = new JsonWriter(sw); - try { - writer.beginArray(); - for (final Object o : list) { - writer.beginObject(); - if (o instanceof Integer) { - writer.name(INTEGER_CLASS_NAME).value((Integer)o); - } else if (o instanceof String) { - writer.name(STRING_CLASS_NAME).value((String)o); - } - writer.endObject(); - } - writer.endArray(); - return sw.toString(); - } catch (final IOException e) { - } finally { - close(writer); - } - return EMPTY_STRING; - } - - private static void close(final Closeable closeable) { - try { - if (closeable != null) { - closeable.close(); - } - } catch (final IOException e) { - // Ignore - } - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java b/java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java deleted file mode 100644 index a5a1ea921..000000000 --- a/java/src/com/android/inputmethod/latin/utils/LanguageOnSpacebarUtils.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.latin.RichInputMethodSubtype; - -import java.util.Collections; -import java.util.List; -import java.util.Locale; - -import javax.annotation.Nonnull; - -/** - * This class determines that the language name on the spacebar should be displayed in what format. - */ -public final class LanguageOnSpacebarUtils { - public static final int FORMAT_TYPE_NONE = 0; - public static final int FORMAT_TYPE_LANGUAGE_ONLY = 1; - public static final int FORMAT_TYPE_FULL_LOCALE = 2; - - private static List<InputMethodSubtype> sEnabledSubtypes = Collections.emptyList(); - private static boolean sIsSystemLanguageSameAsInputLanguage; - - private LanguageOnSpacebarUtils() { - // This utility class is not publicly instantiable. - } - - public static int getLanguageOnSpacebarFormatType( - @Nonnull final RichInputMethodSubtype subtype) { - if (subtype.isNoLanguage()) { - return FORMAT_TYPE_FULL_LOCALE; - } - // Only this subtype is enabled and equals to the system locale. - if (sEnabledSubtypes.size() < 2 && sIsSystemLanguageSameAsInputLanguage) { - return FORMAT_TYPE_NONE; - } - final Locale locale = subtype.getLocale(); - if (locale == null) { - return FORMAT_TYPE_NONE; - } - final String keyboardLanguage = locale.getLanguage(); - final String keyboardLayout = subtype.getKeyboardLayoutSetName(); - int sameLanguageAndLayoutCount = 0; - for (final InputMethodSubtype ims : sEnabledSubtypes) { - final String language = SubtypeLocaleUtils.getSubtypeLocale(ims).getLanguage(); - if (keyboardLanguage.equals(language) && keyboardLayout.equals( - SubtypeLocaleUtils.getKeyboardLayoutSetName(ims))) { - sameLanguageAndLayoutCount++; - } - } - // Display full locale name only when there are multiple subtypes that have the same - // locale and keyboard layout. Otherwise displaying language name is enough. - return sameLanguageAndLayoutCount > 1 ? FORMAT_TYPE_FULL_LOCALE - : FORMAT_TYPE_LANGUAGE_ONLY; - } - - public static void setEnabledSubtypes(@Nonnull final List<InputMethodSubtype> enabledSubtypes) { - sEnabledSubtypes = enabledSubtypes; - } - - public static void onSubtypeChanged(@Nonnull final RichInputMethodSubtype subtype, - final boolean implicitlyEnabledSubtype, @Nonnull final Locale systemLocale) { - final Locale newLocale = subtype.getLocale(); - if (systemLocale.equals(newLocale)) { - sIsSystemLanguageSameAsInputLanguage = true; - return; - } - if (!systemLocale.getLanguage().equals(newLocale.getLanguage())) { - sIsSystemLanguageSameAsInputLanguage = false; - return; - } - // If the subtype is enabled explicitly, the language name should be displayed even when - // the keyboard language and the system language are equal. - sIsSystemLanguageSameAsInputLanguage = implicitlyEnabledSubtype; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/LeakGuardHandlerWrapper.java b/java/src/com/android/inputmethod/latin/utils/LeakGuardHandlerWrapper.java deleted file mode 100644 index 9a5be99b3..000000000 --- a/java/src/com/android/inputmethod/latin/utils/LeakGuardHandlerWrapper.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.utils; - -import android.os.Handler; -import android.os.Looper; - -import java.lang.ref.WeakReference; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -public class LeakGuardHandlerWrapper<T> extends Handler { - private final WeakReference<T> mOwnerInstanceRef; - - public LeakGuardHandlerWrapper(@Nonnull final T ownerInstance) { - this(ownerInstance, Looper.myLooper()); - } - - public LeakGuardHandlerWrapper(@Nonnull final T ownerInstance, final Looper looper) { - super(looper); - mOwnerInstanceRef = new WeakReference<>(ownerInstance); - } - - @Nullable - public T getOwnerInstance() { - return mOwnerInstanceRef.get(); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/ManagedProfileUtils.java b/java/src/com/android/inputmethod/latin/utils/ManagedProfileUtils.java deleted file mode 100644 index ef1872bf4..000000000 --- a/java/src/com/android/inputmethod/latin/utils/ManagedProfileUtils.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.content.Context; - -import com.android.inputmethod.annotations.UsedForTesting; - -public class ManagedProfileUtils { - private static ManagedProfileUtils INSTANCE = new ManagedProfileUtils(); - private static ManagedProfileUtils sTestInstance; - - private ManagedProfileUtils() { - // This utility class is not publicly instantiable. - } - - @UsedForTesting - public static void setTestInstance(final ManagedProfileUtils testInstance) { - sTestInstance = testInstance; - } - - public static ManagedProfileUtils getInstance() { - return sTestInstance == null ? INSTANCE : sTestInstance; - } - - public boolean hasWorkProfile(final Context context) { - return false; - } -}
\ No newline at end of file diff --git a/java/src/com/android/inputmethod/latin/utils/MetadataFileUriGetter.java b/java/src/com/android/inputmethod/latin/utils/MetadataFileUriGetter.java deleted file mode 100644 index 97fb17de3..000000000 --- a/java/src/com/android/inputmethod/latin/utils/MetadataFileUriGetter.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import com.android.inputmethod.latin.R; - -import android.content.Context; - -/** - * Helper class to get the metadata URI and the additional ID. - */ -@SuppressWarnings("unused") -public class MetadataFileUriGetter { - private MetadataFileUriGetter() { - // This helper class is not instantiable. - } - - public static String getMetadataUri(final Context context) { - return context.getString(R.string.dictionary_pack_metadata_uri); - } - - public static String getMetadataAdditionalId(final Context context) { - return ""; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/NgramContextUtils.java b/java/src/com/android/inputmethod/latin/utils/NgramContextUtils.java deleted file mode 100644 index c05ffd693..000000000 --- a/java/src/com/android/inputmethod/latin/utils/NgramContextUtils.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import com.android.inputmethod.latin.NgramContext; -import com.android.inputmethod.latin.NgramContext.WordInfo; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; -import com.android.inputmethod.latin.settings.SpacingAndPunctuations; - -import java.util.Arrays; -import java.util.regex.Pattern; - -import javax.annotation.Nonnull; - -public final class NgramContextUtils { - private NgramContextUtils() { - // Intentional empty constructor for utility class. - } - - private static final Pattern NEWLINE_REGEX = Pattern.compile("[\\r\\n]+"); - private static final Pattern SPACE_REGEX = Pattern.compile("\\s+"); - // Get context information from nth word before the cursor. n = 1 retrieves the words - // immediately before the cursor, n = 2 retrieves the words before that, and so on. This splits - // on whitespace only. - // Also, it won't return words that end in a separator (if the nth word before the cursor - // ends in a separator, it returns information representing beginning-of-sentence). - // Example (when Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM is 2): - // (n = 1) "abc def|" -> abc, def - // (n = 1) "abc def |" -> abc, def - // (n = 1) "abc 'def|" -> empty, 'def - // (n = 1) "abc def. |" -> beginning-of-sentence - // (n = 1) "abc def . |" -> beginning-of-sentence - // (n = 2) "abc def|" -> beginning-of-sentence, abc - // (n = 2) "abc def |" -> beginning-of-sentence, abc - // (n = 2) "abc 'def|" -> empty. The context is different from "abc def", but we cannot - // represent this situation using NgramContext. See TODO in the method. - // TODO: The next example's result should be "abc, def". This have to be fixed before we - // retrieve the prior context of Beginning-of-Sentence. - // (n = 2) "abc def. |" -> beginning-of-sentence, abc - // (n = 2) "abc def . |" -> abc, def - // (n = 2) "abc|" -> beginning-of-sentence - // (n = 2) "abc |" -> beginning-of-sentence - // (n = 2) "abc. def|" -> beginning-of-sentence - @Nonnull - public static NgramContext getNgramContextFromNthPreviousWord(final CharSequence prev, - final SpacingAndPunctuations spacingAndPunctuations, final int n) { - if (prev == null) return NgramContext.EMPTY_PREV_WORDS_INFO; - final String[] lines = NEWLINE_REGEX.split(prev); - if (lines.length == 0) { - return new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO); - } - final String[] w = SPACE_REGEX.split(lines[lines.length - 1]); - final WordInfo[] prevWordsInfo = - new WordInfo[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; - Arrays.fill(prevWordsInfo, WordInfo.EMPTY_WORD_INFO); - for (int i = 0; i < prevWordsInfo.length; i++) { - final int focusedWordIndex = w.length - n - i; - // Referring to the word after the focused word. - if ((focusedWordIndex + 1) >= 0 && (focusedWordIndex + 1) < w.length) { - final String wordFollowingTheNthPrevWord = w[focusedWordIndex + 1]; - if (!wordFollowingTheNthPrevWord.isEmpty()) { - final char firstChar = wordFollowingTheNthPrevWord.charAt(0); - if (spacingAndPunctuations.isWordConnector(firstChar)) { - // The word following the focused word is starting with a word connector. - // TODO: Return meaningful context for this case. - break; - } - } - } - // If we can't find (n + i) words, the context is beginning-of-sentence. - if (focusedWordIndex < 0) { - prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO; - break; - } - - final String focusedWord = w[focusedWordIndex]; - // If the word is empty, the context is beginning-of-sentence. - final int length = focusedWord.length(); - if (length <= 0) { - prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO; - break; - } - // If the word ends in a sentence terminator, the context is beginning-of-sentence. - final char lastChar = focusedWord.charAt(length - 1); - if (spacingAndPunctuations.isSentenceTerminator(lastChar)) { - prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO; - break; - } - // If ends in a word separator or connector, the context is unclear. - // TODO: Return meaningful context for this case. - if (spacingAndPunctuations.isWordSeparator(lastChar) - || spacingAndPunctuations.isWordConnector(lastChar)) { - break; - } - prevWordsInfo[i] = new WordInfo(focusedWord); - } - return new NgramContext(prevWordsInfo); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/RecapitalizeStatus.java b/java/src/com/android/inputmethod/latin/utils/RecapitalizeStatus.java deleted file mode 100644 index 737e33228..000000000 --- a/java/src/com/android/inputmethod/latin/utils/RecapitalizeStatus.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import com.android.inputmethod.latin.common.StringUtils; - -import java.util.Locale; - -/** - * The status of the current recapitalize process. - */ -public class RecapitalizeStatus { - public static final int NOT_A_RECAPITALIZE_MODE = -1; - public static final int CAPS_MODE_ORIGINAL_MIXED_CASE = 0; - public static final int CAPS_MODE_ALL_LOWER = 1; - public static final int CAPS_MODE_FIRST_WORD_UPPER = 2; - public static final int CAPS_MODE_ALL_UPPER = 3; - // When adding a new mode, don't forget to update the CAPS_MODE_LAST constant. - public static final int CAPS_MODE_LAST = CAPS_MODE_ALL_UPPER; - - private static final int[] ROTATION_STYLE = { - CAPS_MODE_ORIGINAL_MIXED_CASE, - CAPS_MODE_ALL_LOWER, - CAPS_MODE_FIRST_WORD_UPPER, - CAPS_MODE_ALL_UPPER - }; - - private static final int getStringMode(final String string, final int[] sortedSeparators) { - if (StringUtils.isIdenticalAfterUpcase(string)) { - return CAPS_MODE_ALL_UPPER; - } else if (StringUtils.isIdenticalAfterDowncase(string)) { - return CAPS_MODE_ALL_LOWER; - } else if (StringUtils.isIdenticalAfterCapitalizeEachWord(string, sortedSeparators)) { - return CAPS_MODE_FIRST_WORD_UPPER; - } else { - return CAPS_MODE_ORIGINAL_MIXED_CASE; - } - } - - public static String modeToString(final int recapitalizeMode) { - switch (recapitalizeMode) { - case NOT_A_RECAPITALIZE_MODE: return "undefined"; - case CAPS_MODE_ORIGINAL_MIXED_CASE: return "mixedCase"; - case CAPS_MODE_ALL_LOWER: return "allLower"; - case CAPS_MODE_FIRST_WORD_UPPER: return "firstWordUpper"; - case CAPS_MODE_ALL_UPPER: return "allUpper"; - default: return "unknown<" + recapitalizeMode + ">"; - } - } - - /** - * We store the location of the cursor and the string that was there before the recapitalize - * action was done, and the location of the cursor and the string that was there after. - */ - private int mCursorStartBefore; - private String mStringBefore; - private int mCursorStartAfter; - private int mCursorEndAfter; - private int mRotationStyleCurrentIndex; - private boolean mSkipOriginalMixedCaseMode; - private Locale mLocale; - private int[] mSortedSeparators; - private String mStringAfter; - private boolean mIsStarted; - private boolean mIsEnabled = true; - - private static final int[] EMPTY_STORTED_SEPARATORS = {}; - - public RecapitalizeStatus() { - // By default, initialize with fake values that won't match any real recapitalize. - start(-1, -1, "", Locale.getDefault(), EMPTY_STORTED_SEPARATORS); - stop(); - } - - public void start(final int cursorStart, final int cursorEnd, final String string, - final Locale locale, final int[] sortedSeparators) { - if (!mIsEnabled) { - return; - } - mCursorStartBefore = cursorStart; - mStringBefore = string; - mCursorStartAfter = cursorStart; - mCursorEndAfter = cursorEnd; - mStringAfter = string; - final int initialMode = getStringMode(mStringBefore, sortedSeparators); - mLocale = locale; - mSortedSeparators = sortedSeparators; - if (CAPS_MODE_ORIGINAL_MIXED_CASE == initialMode) { - mRotationStyleCurrentIndex = 0; - mSkipOriginalMixedCaseMode = false; - } else { - // Find the current mode in the array. - int currentMode; - for (currentMode = ROTATION_STYLE.length - 1; currentMode > 0; --currentMode) { - if (ROTATION_STYLE[currentMode] == initialMode) { - break; - } - } - mRotationStyleCurrentIndex = currentMode; - mSkipOriginalMixedCaseMode = true; - } - mIsStarted = true; - } - - public void stop() { - mIsStarted = false; - } - - public boolean isStarted() { - return mIsStarted; - } - - public void enable() { - mIsEnabled = true; - } - - public void disable() { - mIsEnabled = false; - } - - public boolean mIsEnabled() { - return mIsEnabled; - } - - public boolean isSetAt(final int cursorStart, final int cursorEnd) { - return cursorStart == mCursorStartAfter && cursorEnd == mCursorEndAfter; - } - - /** - * Rotate through the different possible capitalization modes. - */ - public void rotate() { - final String oldResult = mStringAfter; - int count = 0; // Protection against infinite loop. - do { - mRotationStyleCurrentIndex = (mRotationStyleCurrentIndex + 1) % ROTATION_STYLE.length; - if (CAPS_MODE_ORIGINAL_MIXED_CASE == ROTATION_STYLE[mRotationStyleCurrentIndex] - && mSkipOriginalMixedCaseMode) { - mRotationStyleCurrentIndex = - (mRotationStyleCurrentIndex + 1) % ROTATION_STYLE.length; - } - ++count; - switch (ROTATION_STYLE[mRotationStyleCurrentIndex]) { - case CAPS_MODE_ORIGINAL_MIXED_CASE: - mStringAfter = mStringBefore; - break; - case CAPS_MODE_ALL_LOWER: - mStringAfter = mStringBefore.toLowerCase(mLocale); - break; - case CAPS_MODE_FIRST_WORD_UPPER: - mStringAfter = StringUtils.capitalizeEachWord(mStringBefore, mSortedSeparators, - mLocale); - break; - case CAPS_MODE_ALL_UPPER: - mStringAfter = mStringBefore.toUpperCase(mLocale); - break; - default: - mStringAfter = mStringBefore; - } - } while (mStringAfter.equals(oldResult) && count < ROTATION_STYLE.length + 1); - mCursorEndAfter = mCursorStartAfter + mStringAfter.length(); - } - - /** - * Remove leading/trailing whitespace from the considered string. - */ - public void trim() { - final int len = mStringBefore.length(); - int nonWhitespaceStart = 0; - for (; nonWhitespaceStart < len; - nonWhitespaceStart = mStringBefore.offsetByCodePoints(nonWhitespaceStart, 1)) { - final int codePoint = mStringBefore.codePointAt(nonWhitespaceStart); - if (!Character.isWhitespace(codePoint)) break; - } - int nonWhitespaceEnd = len; - for (; nonWhitespaceEnd > 0; - nonWhitespaceEnd = mStringBefore.offsetByCodePoints(nonWhitespaceEnd, -1)) { - final int codePoint = mStringBefore.codePointBefore(nonWhitespaceEnd); - if (!Character.isWhitespace(codePoint)) break; - } - // If nonWhitespaceStart >= nonWhitespaceEnd, that means the selection contained only - // whitespace, so we leave it as is. - if ((0 != nonWhitespaceStart || len != nonWhitespaceEnd) - && nonWhitespaceStart < nonWhitespaceEnd) { - mCursorEndAfter = mCursorStartBefore + nonWhitespaceEnd; - mCursorStartBefore = mCursorStartAfter = mCursorStartBefore + nonWhitespaceStart; - mStringAfter = mStringBefore = - mStringBefore.substring(nonWhitespaceStart, nonWhitespaceEnd); - } - } - - public String getRecapitalizedString() { - return mStringAfter; - } - - public int getNewCursorStart() { - return mCursorStartAfter; - } - - public int getNewCursorEnd() { - return mCursorEndAfter; - } - - public int getCurrentMode() { - return ROTATION_STYLE[mRotationStyleCurrentIndex]; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/ResourceUtils.java b/java/src/com/android/inputmethod/latin/utils/ResourceUtils.java deleted file mode 100644 index 37b34751a..000000000 --- a/java/src/com/android/inputmethod/latin/utils/ResourceUtils.java +++ /dev/null @@ -1,320 +0,0 @@ -/* - * 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.utils; - -import android.content.Context; -import android.content.res.Resources; -import android.content.res.TypedArray; -import android.graphics.Insets; -import android.os.Build; -import android.text.TextUtils; -import android.util.DisplayMetrics; -import android.util.Log; -import android.util.TypedValue; -import android.view.Window; -import android.view.WindowInsets; -import android.view.WindowManager; -import android.view.WindowMetrics; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.settings.SettingsValues; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.regex.PatternSyntaxException; - -public final class ResourceUtils { - private static final String TAG = ResourceUtils.class.getSimpleName(); - - public static final float UNDEFINED_RATIO = -1.0f; - public static final int UNDEFINED_DIMENSION = -1; - - private ResourceUtils() { - // This utility class is not publicly instantiable. - } - - private static final HashMap<String, String> sDeviceOverrideValueMap = new HashMap<>(); - - private static final String[] BUILD_KEYS_AND_VALUES = { - "HARDWARE", Build.HARDWARE, - "MODEL", Build.MODEL, - "BRAND", Build.BRAND, - "MANUFACTURER", Build.MANUFACTURER - }; - private static final HashMap<String, String> sBuildKeyValues; - private static final String sBuildKeyValuesDebugString; - - static { - sBuildKeyValues = new HashMap<>(); - final ArrayList<String> keyValuePairs = new ArrayList<>(); - final int keyCount = BUILD_KEYS_AND_VALUES.length / 2; - for (int i = 0; i < keyCount; i++) { - final int index = i * 2; - final String key = BUILD_KEYS_AND_VALUES[index]; - final String value = BUILD_KEYS_AND_VALUES[index + 1]; - sBuildKeyValues.put(key, value); - keyValuePairs.add(key + '=' + value); - } - sBuildKeyValuesDebugString = "[" + TextUtils.join(" ", keyValuePairs) + "]"; - } - - public static String getDeviceOverrideValue(final Resources res, final int overrideResId, - final String defaultValue) { - final int orientation = res.getConfiguration().orientation; - final String key = overrideResId + "-" + orientation; - if (sDeviceOverrideValueMap.containsKey(key)) { - return sDeviceOverrideValueMap.get(key); - } - - final String[] overrideArray = res.getStringArray(overrideResId); - final String overrideValue = findConstantForKeyValuePairs(sBuildKeyValues, overrideArray); - // The overrideValue might be an empty string. - if (overrideValue != null) { - Log.i(TAG, "Find override value:" - + " resource="+ res.getResourceEntryName(overrideResId) - + " build=" + sBuildKeyValuesDebugString - + " override=" + overrideValue); - sDeviceOverrideValueMap.put(key, overrideValue); - return overrideValue; - } - - sDeviceOverrideValueMap.put(key, defaultValue); - return defaultValue; - } - - @SuppressWarnings("serial") - static class DeviceOverridePatternSyntaxError extends Exception { - public DeviceOverridePatternSyntaxError(final String message, final String expression) { - this(message, expression, null); - } - - public DeviceOverridePatternSyntaxError(final String message, final String expression, - final Throwable throwable) { - super(message + ": " + expression, throwable); - } - } - - /** - * Find the condition that fulfills specified key value pairs from an array of - * "condition,constant", and return the corresponding string constant. A condition is - * "pattern1[:pattern2...] (or an empty string for the default). A pattern is - * "key=regexp_value" string. The condition matches only if all patterns of the condition - * are true for the specified key value pairs. - * - * For example, "condition,constant" has the following format. - * - HARDWARE=mako,constantForNexus4 - * - MODEL=Nexus 4:MANUFACTURER=LGE,constantForNexus4 - * - ,defaultConstant - * - * @param keyValuePairs attributes to be used to look for a matched condition. - * @param conditionConstantArray an array of "condition,constant" elements to be searched. - * @return the constant part of the matched "condition,constant" element. Returns null if no - * condition matches. - * @see com.android.inputmethod.latin.utils.ResourceUtilsTests#testFindConstantForKeyValuePairsRegexp() - */ - @UsedForTesting - static String findConstantForKeyValuePairs(final HashMap<String, String> keyValuePairs, - final String[] conditionConstantArray) { - if (conditionConstantArray == null || keyValuePairs == null) { - return null; - } - String foundValue = null; - for (final String conditionConstant : conditionConstantArray) { - final int posComma = conditionConstant.indexOf(','); - if (posComma < 0) { - Log.w(TAG, "Array element has no comma: " + conditionConstant); - continue; - } - final String condition = conditionConstant.substring(0, posComma); - if (condition.isEmpty()) { - Log.w(TAG, "Array element has no condition: " + conditionConstant); - continue; - } - try { - if (fulfillsCondition(keyValuePairs, condition)) { - // Take first match - if (foundValue == null) { - foundValue = conditionConstant.substring(posComma + 1); - } - // And continue walking through all conditions. - } - } catch (final DeviceOverridePatternSyntaxError e) { - Log.w(TAG, "Syntax error, ignored", e); - } - } - return foundValue; - } - - private static boolean fulfillsCondition(final HashMap<String,String> keyValuePairs, - final String condition) throws DeviceOverridePatternSyntaxError { - final String[] patterns = condition.split(":"); - // Check all patterns in a condition are true - boolean matchedAll = true; - for (final String pattern : patterns) { - final int posEqual = pattern.indexOf('='); - if (posEqual < 0) { - throw new DeviceOverridePatternSyntaxError("Pattern has no '='", condition); - } - final String key = pattern.substring(0, posEqual); - final String value = keyValuePairs.get(key); - if (value == null) { - throw new DeviceOverridePatternSyntaxError("Unknown key", condition); - } - final String patternRegexpValue = pattern.substring(posEqual + 1); - try { - if (!value.matches(patternRegexpValue)) { - matchedAll = false; - // And continue walking through all patterns. - } - } catch (final PatternSyntaxException e) { - throw new DeviceOverridePatternSyntaxError("Syntax error", condition, e); - } - } - return matchedAll; - } - - public static int getDefaultKeyboardWidth(final Context context) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - // Since Android 15’s edge-to-edge enforcement, window insets should be considered. - final WindowManager wm = context.getSystemService(WindowManager.class); - final WindowMetrics windowMetrics = wm.getCurrentWindowMetrics(); - final Insets insets = - windowMetrics - .getWindowInsets() - .getInsetsIgnoringVisibility( - WindowInsets.Type.systemBars() - | WindowInsets.Type.displayCutout()); - return windowMetrics.getBounds().width() - insets.left - insets.right; - } - final DisplayMetrics dm = context.getResources().getDisplayMetrics(); - return dm.widthPixels; - } - - public static int getKeyboardHeight(final Resources res, final SettingsValues settingsValues) { - final int defaultKeyboardHeight = getDefaultKeyboardHeight(res); - if (settingsValues.mHasKeyboardResize) { - // mKeyboardHeightScale Ranges from [.5,1.2], from xml/prefs_screen_debug.xml - return (int)(defaultKeyboardHeight * settingsValues.mKeyboardHeightScale); - } - return defaultKeyboardHeight; - } - - public static int getDefaultKeyboardHeight(final Resources res) { - final DisplayMetrics dm = res.getDisplayMetrics(); - final String keyboardHeightInDp = getDeviceOverrideValue( - res, R.array.keyboard_heights, null /* defaultValue */); - final float keyboardHeight; - if (TextUtils.isEmpty(keyboardHeightInDp)) { - keyboardHeight = res.getDimension(R.dimen.config_default_keyboard_height); - } else { - keyboardHeight = Float.parseFloat(keyboardHeightInDp) * dm.density; - } - final float maxKeyboardHeight = res.getFraction( - R.fraction.config_max_keyboard_height, dm.heightPixels, dm.heightPixels); - float minKeyboardHeight = res.getFraction( - R.fraction.config_min_keyboard_height, dm.heightPixels, dm.heightPixels); - if (minKeyboardHeight < 0.0f) { - // Specified fraction was negative, so it should be calculated against display - // width. - minKeyboardHeight = -res.getFraction( - R.fraction.config_min_keyboard_height, dm.widthPixels, dm.widthPixels); - } - // Keyboard height will not exceed maxKeyboardHeight and will not be less than - // minKeyboardHeight. - return (int)Math.max(Math.min(keyboardHeight, maxKeyboardHeight), minKeyboardHeight); - } - - public static boolean isValidFraction(final float fraction) { - return fraction >= 0.0f; - } - - // {@link Resources#getDimensionPixelSize(int)} returns at least one pixel size. - public static boolean isValidDimensionPixelSize(final int dimension) { - return dimension > 0; - } - - // {@link Resources#getDimensionPixelOffset(int)} may return zero pixel offset. - public static boolean isValidDimensionPixelOffset(final int dimension) { - return dimension >= 0; - } - - public static float getFloatFromFraction(final Resources res, final int fractionResId) { - return res.getFraction(fractionResId, 1, 1); - } - - public static float getFraction(final TypedArray a, final int index, final float defValue) { - final TypedValue value = a.peekValue(index); - if (value == null || !isFractionValue(value)) { - return defValue; - } - return a.getFraction(index, 1, 1, defValue); - } - - public static float getFraction(final TypedArray a, final int index) { - return getFraction(a, index, UNDEFINED_RATIO); - } - - public static int getDimensionPixelSize(final TypedArray a, final int index) { - final TypedValue value = a.peekValue(index); - if (value == null || !isDimensionValue(value)) { - return ResourceUtils.UNDEFINED_DIMENSION; - } - return a.getDimensionPixelSize(index, ResourceUtils.UNDEFINED_DIMENSION); - } - - public static float getDimensionOrFraction(final TypedArray a, final int index, final int base, - final float defValue) { - final TypedValue value = a.peekValue(index); - if (value == null) { - return defValue; - } - if (isFractionValue(value)) { - return a.getFraction(index, base, base, defValue); - } else if (isDimensionValue(value)) { - return a.getDimension(index, defValue); - } - return defValue; - } - - public static int getEnumValue(final TypedArray a, final int index, final int defValue) { - final TypedValue value = a.peekValue(index); - if (value == null) { - return defValue; - } - if (isIntegerValue(value)) { - return a.getInt(index, defValue); - } - return defValue; - } - - public static boolean isFractionValue(final TypedValue v) { - return v.type == TypedValue.TYPE_FRACTION; - } - - public static boolean isDimensionValue(final TypedValue v) { - return v.type == TypedValue.TYPE_DIMENSION; - } - - public static boolean isIntegerValue(final TypedValue v) { - return v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT; - } - - public static boolean isStringValue(final TypedValue v) { - return v.type == TypedValue.TYPE_STRING; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/RunInLocale.java b/java/src/com/android/inputmethod/latin/utils/RunInLocale.java deleted file mode 100644 index 1ea16e6ef..000000000 --- a/java/src/com/android/inputmethod/latin/utils/RunInLocale.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.content.res.Configuration; -import android.content.res.Resources; - -import java.util.Locale; - -public abstract class RunInLocale<T> { - private static final Object sLockForRunInLocale = new Object(); - - protected abstract T job(final Resources res); - - /** - * Execute {@link #job(Resources)} method in specified system locale exclusively. - * - * @param res the resources to use. - * @param newLocale the locale to change to. Run in system locale if null. - * @return the value returned from {@link #job(Resources)}. - */ - public T runInLocale(final Resources res, final Locale newLocale) { - synchronized (sLockForRunInLocale) { - final Configuration conf = res.getConfiguration(); - if (newLocale == null || newLocale.equals(conf.locale)) { - return job(res); - } - final Locale savedLocale = conf.locale; - try { - conf.locale = newLocale; - res.updateConfiguration(conf, null); - return job(res); - } finally { - conf.locale = savedLocale; - res.updateConfiguration(conf, null); - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java b/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java deleted file mode 100644 index 537713091..000000000 --- a/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * 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.utils; - -import java.util.Locale; -import java.util.TreeMap; - -/** - * A class to help with handling different writing scripts. - */ -public class ScriptUtils { - - // Used for hardware keyboards - public static final int SCRIPT_UNKNOWN = -1; - - public static final int SCRIPT_ARABIC = 0; - public static final int SCRIPT_ARMENIAN = 1; - public static final int SCRIPT_BENGALI = 2; - public static final int SCRIPT_CYRILLIC = 3; - public static final int SCRIPT_DEVANAGARI = 4; - public static final int SCRIPT_GEORGIAN = 5; - public static final int SCRIPT_GREEK = 6; - public static final int SCRIPT_HEBREW = 7; - public static final int SCRIPT_KANNADA = 8; - public static final int SCRIPT_KHMER = 9; - public static final int SCRIPT_LAO = 10; - public static final int SCRIPT_LATIN = 11; - public static final int SCRIPT_MALAYALAM = 12; - public static final int SCRIPT_MYANMAR = 13; - public static final int SCRIPT_SINHALA = 14; - public static final int SCRIPT_TAMIL = 15; - public static final int SCRIPT_TELUGU = 16; - public static final int SCRIPT_THAI = 17; - - private static final TreeMap<String, Integer> mLanguageCodeToScriptCode; - - static { - mLanguageCodeToScriptCode = new TreeMap<>(); - mLanguageCodeToScriptCode.put("", SCRIPT_LATIN); // default - mLanguageCodeToScriptCode.put("ar", SCRIPT_ARABIC); - mLanguageCodeToScriptCode.put("hy", SCRIPT_ARMENIAN); - mLanguageCodeToScriptCode.put("bn", SCRIPT_BENGALI); - mLanguageCodeToScriptCode.put("bg", SCRIPT_CYRILLIC); - mLanguageCodeToScriptCode.put("sr", SCRIPT_CYRILLIC); - mLanguageCodeToScriptCode.put("ru", SCRIPT_CYRILLIC); - mLanguageCodeToScriptCode.put("ka", SCRIPT_GEORGIAN); - mLanguageCodeToScriptCode.put("el", SCRIPT_GREEK); - mLanguageCodeToScriptCode.put("iw", SCRIPT_HEBREW); - mLanguageCodeToScriptCode.put("km", SCRIPT_KHMER); - mLanguageCodeToScriptCode.put("lo", SCRIPT_LAO); - mLanguageCodeToScriptCode.put("ml", SCRIPT_MALAYALAM); - mLanguageCodeToScriptCode.put("my", SCRIPT_MYANMAR); - mLanguageCodeToScriptCode.put("si", SCRIPT_SINHALA); - mLanguageCodeToScriptCode.put("ta", SCRIPT_TAMIL); - mLanguageCodeToScriptCode.put("te", SCRIPT_TELUGU); - mLanguageCodeToScriptCode.put("th", SCRIPT_THAI); - } - - /* - * 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. - */ - public static boolean isLetterPartOfScript(final int codePoint, final int scriptId) { - switch (scriptId) { - case SCRIPT_ARABIC: - // Arabic letters can be in any of the following blocks: - // Arabic U+0600..U+06FF - // Arabic Supplement, Thaana U+0750..U+077F, U+0780..U+07BF - // Arabic Extended-A U+08A0..U+08FF - // Arabic Presentation Forms-A U+FB50..U+FDFF - // Arabic Presentation Forms-B U+FE70..U+FEFF - return (codePoint >= 0x600 && codePoint <= 0x6FF) - || (codePoint >= 0x750 && codePoint <= 0x7BF) - || (codePoint >= 0x8A0 && codePoint <= 0x8FF) - || (codePoint >= 0xFB50 && codePoint <= 0xFDFF) - || (codePoint >= 0xFE70 && codePoint <= 0xFEFF); - case SCRIPT_ARMENIAN: - // Armenian letters are in the Armenian unicode block, U+0530..U+058F and - // Alphabetic Presentation Forms block, U+FB00..U+FB4F, but only in the Armenian part - // of that block, which is U+FB13..U+FB17. - return (codePoint >= 0x530 && codePoint <= 0x58F - || codePoint >= 0xFB13 && codePoint <= 0xFB17); - case SCRIPT_BENGALI: - // Bengali unicode block is U+0980..U+09FF - return (codePoint >= 0x980 && codePoint <= 0x9FF); - 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); - case SCRIPT_DEVANAGARI: - // Devanagari unicode block is +0900..U+097F - return (codePoint >= 0x900 && codePoint <= 0x97F); - case SCRIPT_GEORGIAN: - // Georgian letters are in the Georgian unicode block, U+10A0..U+10FF, - // or Georgian supplement block, U+2D00..U+2D2F - return (codePoint >= 0x10A0 && codePoint <= 0x10FF - || codePoint >= 0x2D00 && codePoint <= 0x2D2F); - case SCRIPT_GREEK: - // Greek letters are either in the 370~3FF range (Greek & Coptic), or in the - // 1F00~1FFF range (Greek extended). Our dictionary contains both sort of characters. - // Our dictionary also contains a few words with 0xF2; it would be best to check - // if that's correct, but a web search does return results for these words so - // they are probably okay. - return (codePoint >= 0x370 && codePoint <= 0x3FF) - || (codePoint >= 0x1F00 && codePoint <= 0x1FFF) - || codePoint == 0xF2; - case SCRIPT_HEBREW: - // Hebrew letters are in the Hebrew unicode block, which spans from U+0590 to U+05FF, - // or in the Alphabetic Presentation Forms block, U+FB00..U+FB4F, but only in the - // Hebrew part of that block, which is U+FB1D..U+FB4F. - return (codePoint >= 0x590 && codePoint <= 0x5FF - || codePoint >= 0xFB1D && codePoint <= 0xFB4F); - case SCRIPT_KANNADA: - // Kannada unicode block is U+0C80..U+0CFF - return (codePoint >= 0xC80 && codePoint <= 0xCFF); - case SCRIPT_KHMER: - // Khmer letters are in unicode block U+1780..U+17FF, and the Khmer symbols block - // is U+19E0..U+19FF - return (codePoint >= 0x1780 && codePoint <= 0x17FF - || codePoint >= 0x19E0 && codePoint <= 0x19FF); - case SCRIPT_LAO: - // The Lao block is U+0E80..U+0EFF - return (codePoint >= 0xE80 && codePoint <= 0xEFF); - 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_MALAYALAM: - // Malayalam unicode block is U+0D00..U+0D7F - return (codePoint >= 0xD00 && codePoint <= 0xD7F); - case SCRIPT_MYANMAR: - // Myanmar has three unicode blocks : - // Myanmar U+1000..U+109F - // Myanmar extended-A U+AA60..U+AA7F - // Myanmar extended-B U+A9E0..U+A9FF - return (codePoint >= 0x1000 && codePoint <= 0x109F - || codePoint >= 0xAA60 && codePoint <= 0xAA7F - || codePoint >= 0xA9E0 && codePoint <= 0xA9FF); - case SCRIPT_SINHALA: - // Sinhala unicode block is U+0D80..U+0DFF - return (codePoint >= 0xD80 && codePoint <= 0xDFF); - case SCRIPT_TAMIL: - // Tamil unicode block is U+0B80..U+0BFF - return (codePoint >= 0xB80 && codePoint <= 0xBFF); - case SCRIPT_TELUGU: - // Telugu unicode block is U+0C00..U+0C7F - return (codePoint >= 0xC00 && codePoint <= 0xC7F); - case SCRIPT_THAI: - // Thai unicode block is U+0E00..U+0E7F - return (codePoint >= 0xE00 && codePoint <= 0xE7F); - case SCRIPT_UNKNOWN: - return true; - default: - // Should never come here - throw new RuntimeException("Impossible value of script: " + scriptId); - } - } - - /** - * @param locale spell checker locale - * @return internal Latin IME script code that maps to a language code - * {@see http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes} - */ - public static int getScriptFromSpellCheckerLocale(final Locale locale) { - String language = locale.getLanguage(); - Integer script = mLanguageCodeToScriptCode.get(language); - if (script == null) { - // Default to Latin. - script = mLanguageCodeToScriptCode.get(""); - } - return script; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/SpannableStringUtils.java b/java/src/com/android/inputmethod/latin/utils/SpannableStringUtils.java deleted file mode 100644 index c41817fe6..000000000 --- a/java/src/com/android/inputmethod/latin/utils/SpannableStringUtils.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.text.Spannable; -import android.text.SpannableString; -import android.text.Spanned; -import android.text.SpannedString; -import android.text.TextUtils; -import android.text.style.SuggestionSpan; -import android.text.style.URLSpan; - -import com.android.inputmethod.annotations.UsedForTesting; - -import java.util.ArrayList; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public final class SpannableStringUtils { - /** - * Copies the spans from the region <code>start...end</code> in - * <code>source</code> to the region - * <code>destoff...destoff+end-start</code> in <code>dest</code>. - * Spans in <code>source</code> that begin before <code>start</code> - * or end after <code>end</code> but overlap this range are trimmed - * as if they began at <code>start</code> or ended at <code>end</code>. - * Only SuggestionSpans that don't have the SPAN_PARAGRAPH span are copied. - * - * This code is almost entirely taken from {@link TextUtils#copySpansFrom}, except for the - * kind of span that is copied. - * - * @throws IndexOutOfBoundsException if any of the copied spans - * are out of range in <code>dest</code>. - */ - public static void copyNonParagraphSuggestionSpansFrom(Spanned source, int start, int end, - Spannable dest, int destoff) { - Object[] spans = source.getSpans(start, end, SuggestionSpan.class); - - for (int i = 0; i < spans.length; i++) { - int fl = source.getSpanFlags(spans[i]); - // We don't care about the PARAGRAPH flag in LatinIME code. However, if this flag - // is set, Spannable#setSpan will throw an exception unless the span is on the edge - // of a word. But the spans have been split into two by the getText{Before,After}Cursor - // methods, so after concatenation they may end in the middle of a word. - // Since we don't use them, we can just remove them and avoid crashing. - fl &= ~Spanned.SPAN_PARAGRAPH; - - int st = source.getSpanStart(spans[i]); - int en = source.getSpanEnd(spans[i]); - - if (st < start) - st = start; - if (en > end) - en = end; - - dest.setSpan(spans[i], st - start + destoff, en - start + destoff, - fl); - } - } - - /** - * Returns a CharSequence concatenating the specified CharSequences, retaining their - * SuggestionSpans that don't have the PARAGRAPH flag, but not other spans. - * - * This code is almost entirely taken from {@link TextUtils#concat(CharSequence...)}, except - * it calls copyNonParagraphSuggestionSpansFrom instead of {@link TextUtils#copySpansFrom}. - */ - public static CharSequence concatWithNonParagraphSuggestionSpansOnly(CharSequence... text) { - if (text.length == 0) { - return ""; - } - - if (text.length == 1) { - return text[0]; - } - - boolean spanned = false; - for (int i = 0; i < text.length; i++) { - if (text[i] instanceof Spanned) { - spanned = true; - break; - } - } - - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < text.length; i++) { - sb.append(text[i]); - } - - if (!spanned) { - return sb.toString(); - } - - SpannableString ss = new SpannableString(sb); - int off = 0; - for (int i = 0; i < text.length; i++) { - int len = text[i].length(); - - if (text[i] instanceof Spanned) { - copyNonParagraphSuggestionSpansFrom((Spanned) text[i], 0, len, ss, off); - } - - off += len; - } - - return new SpannedString(ss); - } - - public static boolean hasUrlSpans(final CharSequence text, - final int startIndex, final int endIndex) { - if (!(text instanceof Spanned)) { - return false; // Not spanned, so no link - } - final Spanned spanned = (Spanned)text; - // getSpans(x, y) does not return spans that start on x or end on y. x-1, y+1 does the - // trick, and works in all cases even if startIndex <= 0 or endIndex >= text.length(). - final URLSpan[] spans = spanned.getSpans(startIndex - 1, endIndex + 1, URLSpan.class); - return null != spans && spans.length > 0; - } - - /** - * Splits the given {@code charSequence} with at occurrences of the given {@code regex}. - * <p> - * This is equivalent to - * {@code charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0)} - * except that the spans are preserved in the result array. - * </p> - * @param charSequence the character sequence to be split. - * @param regex the regex pattern to be used as the separator. - * @param preserveTrailingEmptySegments {@code true} to preserve the trailing empty - * segments. Otherwise, trailing empty segments will be removed before being returned. - * @return the array which contains the result. All the spans in the <code>charSequence</code> - * is preserved. - */ - @UsedForTesting - public static CharSequence[] split(final CharSequence charSequence, final String regex, - final boolean preserveTrailingEmptySegments) { - // A short-cut for non-spanned strings. - if (!(charSequence instanceof Spanned)) { - // -1 means that trailing empty segments will be preserved. - return charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0); - } - - // Hereafter, emulate String.split for CharSequence. - final ArrayList<CharSequence> sequences = new ArrayList<>(); - final Matcher matcher = Pattern.compile(regex).matcher(charSequence); - int nextStart = 0; - boolean matched = false; - while (matcher.find()) { - sequences.add(charSequence.subSequence(nextStart, matcher.start())); - nextStart = matcher.end(); - matched = true; - } - if (!matched) { - // never matched. preserveTrailingEmptySegments is ignored in this case. - return new CharSequence[] { charSequence }; - } - sequences.add(charSequence.subSequence(nextStart, charSequence.length())); - if (!preserveTrailingEmptySegments) { - for (int i = sequences.size() - 1; i >= 0; --i) { - if (!TextUtils.isEmpty(sequences.get(i))) { - break; - } - sequences.remove(i); - } - } - return sequences.toArray(new CharSequence[sequences.size()]); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/StatsUtils.java b/java/src/com/android/inputmethod/latin/utils/StatsUtils.java deleted file mode 100644 index 03e58478b..000000000 --- a/java/src/com/android/inputmethod/latin/utils/StatsUtils.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.latin.DictionaryFacilitator; -import com.android.inputmethod.latin.RichInputMethodManager; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.settings.SettingsValues; - -import javax.annotation.Nullable; - -@SuppressWarnings("unused") -public final class StatsUtils { - - private StatsUtils() { - // Intentional empty constructor. - } - - public static void onCreate(final SettingsValues settingsValues, - RichInputMethodManager richImm) { - } - - public static void onPickSuggestionManually(final SuggestedWords suggestedWords, - final SuggestedWords.SuggestedWordInfo suggestionInfo, - final DictionaryFacilitator dictionaryFacilitator) { - } - - public static void onBackspaceWordDelete(int wordLength) { - } - - public static void onBackspacePressed(int lengthToDelete) { - } - - public static void onBackspaceSelectedText(int selectedTextLength) { - } - - public static void onDeleteMultiCharInput(int multiCharLength) { - } - - public static void onRevertAutoCorrect() { - } - - public static void onRevertDoubleSpacePeriod() { - } - - public static void onRevertSwapPunctuation() { - } - - public static void onFinishInputView() { - } - - public static void onCreateInputView() { - } - - public static void onStartInputView(int inputType, int displayOrientation, boolean restarting) { - } - - public static void onAutoCorrection(final String typedWord, final String autoCorrectionWord, - final boolean isBatchInput, final DictionaryFacilitator dictionaryFacilitator, - final String prevWordsContext) { - } - - public static void onWordCommitUserTyped(final String commitWord, final boolean isBatchMode) { - } - - public static void onWordCommitAutoCorrect(final String commitWord, final boolean isBatchMode) { - } - - public static void onWordCommitSuggestionPickedManually( - final String commitWord, final boolean isBatchMode) { - } - - public static void onDoubleSpacePeriod() { - } - - public static void onLoadSettings(SettingsValues settingsValues) { - } - - public static void onInvalidWordIdentification(final String invalidWord) { - } - - public static void onSubtypeChanged(final InputMethodSubtype oldSubtype, - final InputMethodSubtype newSubtype) { - } - - public static void onSettingsActivity(final String entryPoint) { - } - - public static void onInputConnectionLaggy(final int operation, final long duration) { - } - - public static void onDecoderLaggy(final int operation, final long duration) { - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/StatsUtilsManager.java b/java/src/com/android/inputmethod/latin/utils/StatsUtilsManager.java deleted file mode 100644 index cd42f50c7..000000000 --- a/java/src/com/android/inputmethod/latin/utils/StatsUtilsManager.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.content.Context; - -import com.android.inputmethod.latin.DictionaryFacilitator; -import com.android.inputmethod.latin.settings.SettingsValues; - -@SuppressWarnings("unused") -public class StatsUtilsManager { - - private static final StatsUtilsManager sInstance = new StatsUtilsManager(); - private static StatsUtilsManager sTestInstance = null; - - /** - * @return the singleton instance of {@link StatsUtilsManager}. - */ - public static StatsUtilsManager getInstance() { - return sTestInstance != null ? sTestInstance : sInstance; - } - - public static void setTestInstance(final StatsUtilsManager testInstance) { - sTestInstance = testInstance; - } - - public void onCreate(final Context context, final DictionaryFacilitator dictionaryFacilitator) { - } - - public void onLoadSettings(final Context context, final SettingsValues settingsValues) { - } - - public void onStartInputView() { - } - - public void onFinishInputView() { - } - - public void onDestroy(final Context context) { - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java deleted file mode 100644 index 54a3fc39c..000000000 --- a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java +++ /dev/null @@ -1,351 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.utils; - -import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.COMBINING_RULES; -import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.KEYBOARD_LAYOUT_SET; -import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME; - -import android.content.Context; -import android.content.res.Resources; -import android.os.Build; -import android.util.Log; -import android.view.inputmethod.InputMethodSubtype; - -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.common.LocaleUtils; -import com.android.inputmethod.latin.common.StringUtils; - -import java.util.HashMap; -import java.util.Locale; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * A helper class to deal with subtype locales. - */ -// TODO: consolidate this into RichInputMethodSubtype -public final class SubtypeLocaleUtils { - static final String TAG = SubtypeLocaleUtils.class.getSimpleName(); - - // This reference class {@link R} must be located in the same package as LatinIME.java. - private static final String RESOURCE_PACKAGE_NAME = R.class.getPackage().getName(); - - // Special language code to represent "no language". - public static final String NO_LANGUAGE = "zz"; - public static final String QWERTY = "qwerty"; - public static final String EMOJI = "emoji"; - public static final int UNKNOWN_KEYBOARD_LAYOUT = R.string.subtype_generic; - - private static volatile boolean sInitialized = false; - private static final Object sInitializeLock = new Object(); - private static Resources sResources; - // Keyboard layout to its display name map. - private static final HashMap<String, String> sKeyboardLayoutToDisplayNameMap = new HashMap<>(); - // Keyboard layout to subtype name resource id map. - private static final HashMap<String, Integer> sKeyboardLayoutToNameIdsMap = new HashMap<>(); - // Exceptional locale whose name should be displayed in Locale.ROOT. - private static final HashMap<String, Integer> sExceptionalLocaleDisplayedInRootLocale = - new HashMap<>(); - // Exceptional locale to subtype name resource id map. - private static final HashMap<String, Integer> sExceptionalLocaleToNameIdsMap = new HashMap<>(); - // Exceptional locale to subtype name with layout resource id map. - private static final HashMap<String, Integer> sExceptionalLocaleToWithLayoutNameIdsMap = - new HashMap<>(); - private static final String SUBTYPE_NAME_RESOURCE_PREFIX = - "string/subtype_"; - private static final String SUBTYPE_NAME_RESOURCE_GENERIC_PREFIX = - "string/subtype_generic_"; - private static final String SUBTYPE_NAME_RESOURCE_WITH_LAYOUT_PREFIX = - "string/subtype_with_layout_"; - private static final String SUBTYPE_NAME_RESOURCE_NO_LANGUAGE_PREFIX = - "string/subtype_no_language_"; - private static final String SUBTYPE_NAME_RESOURCE_IN_ROOT_LOCALE_PREFIX = - "string/subtype_in_root_locale_"; - // Keyboard layout set name for the subtypes that don't have a keyboardLayoutSet extra value. - // This is for compatibility to keep the same subtype ids as pre-JellyBean. - private static final HashMap<String, String> sLocaleAndExtraValueToKeyboardLayoutSetMap = - new HashMap<>(); - - private SubtypeLocaleUtils() { - // Intentional empty constructor for utility class. - } - - // Note that this initialization method can be called multiple times. - public static void init(final Context context) { - synchronized (sInitializeLock) { - if (sInitialized == false) { - initLocked(context); - sInitialized = true; - } - } - } - - private static void initLocked(final Context context) { - final Resources res = context.getResources(); - sResources = res; - - final String[] predefinedLayoutSet = res.getStringArray(R.array.predefined_layouts); - final String[] layoutDisplayNames = res.getStringArray( - R.array.predefined_layout_display_names); - for (int i = 0; i < predefinedLayoutSet.length; i++) { - final String layoutName = predefinedLayoutSet[i]; - sKeyboardLayoutToDisplayNameMap.put(layoutName, layoutDisplayNames[i]); - final String resourceName = SUBTYPE_NAME_RESOURCE_GENERIC_PREFIX + layoutName; - final int resId = res.getIdentifier(resourceName, null, RESOURCE_PACKAGE_NAME); - sKeyboardLayoutToNameIdsMap.put(layoutName, resId); - // Register subtype name resource id of "No language" with key "zz_<layout>" - final String noLanguageResName = SUBTYPE_NAME_RESOURCE_NO_LANGUAGE_PREFIX + layoutName; - final int noLanguageResId = res.getIdentifier( - noLanguageResName, null, RESOURCE_PACKAGE_NAME); - final String key = getNoLanguageLayoutKey(layoutName); - sKeyboardLayoutToNameIdsMap.put(key, noLanguageResId); - } - - final String[] exceptionalLocaleInRootLocale = res.getStringArray( - R.array.subtype_locale_displayed_in_root_locale); - for (int i = 0; i < exceptionalLocaleInRootLocale.length; i++) { - final String localeString = exceptionalLocaleInRootLocale[i]; - final String resourceName = SUBTYPE_NAME_RESOURCE_IN_ROOT_LOCALE_PREFIX + localeString; - final int resId = res.getIdentifier(resourceName, null, RESOURCE_PACKAGE_NAME); - sExceptionalLocaleDisplayedInRootLocale.put(localeString, resId); - } - - final String[] exceptionalLocales = res.getStringArray( - R.array.subtype_locale_exception_keys); - for (int i = 0; i < exceptionalLocales.length; i++) { - final String localeString = exceptionalLocales[i]; - final String resourceName = SUBTYPE_NAME_RESOURCE_PREFIX + localeString; - final int resId = res.getIdentifier(resourceName, null, RESOURCE_PACKAGE_NAME); - sExceptionalLocaleToNameIdsMap.put(localeString, resId); - final String resourceNameWithLayout = - SUBTYPE_NAME_RESOURCE_WITH_LAYOUT_PREFIX + localeString; - final int resIdWithLayout = res.getIdentifier( - resourceNameWithLayout, null, RESOURCE_PACKAGE_NAME); - sExceptionalLocaleToWithLayoutNameIdsMap.put(localeString, resIdWithLayout); - } - - final String[] keyboardLayoutSetMap = res.getStringArray( - R.array.locale_and_extra_value_to_keyboard_layout_set_map); - for (int i = 0; i + 1 < keyboardLayoutSetMap.length; i += 2) { - final String key = keyboardLayoutSetMap[i]; - final String keyboardLayoutSet = keyboardLayoutSetMap[i + 1]; - sLocaleAndExtraValueToKeyboardLayoutSetMap.put(key, keyboardLayoutSet); - } - } - - public static boolean isExceptionalLocale(final String localeString) { - return sExceptionalLocaleToNameIdsMap.containsKey(localeString); - } - - private static final String getNoLanguageLayoutKey(final String keyboardLayoutName) { - return NO_LANGUAGE + "_" + keyboardLayoutName; - } - - public static int getSubtypeNameId(final String localeString, final String keyboardLayoutName) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN - && isExceptionalLocale(localeString)) { - return sExceptionalLocaleToWithLayoutNameIdsMap.get(localeString); - } - final String key = NO_LANGUAGE.equals(localeString) - ? getNoLanguageLayoutKey(keyboardLayoutName) - : keyboardLayoutName; - final Integer nameId = sKeyboardLayoutToNameIdsMap.get(key); - return nameId == null ? UNKNOWN_KEYBOARD_LAYOUT : nameId; - } - - @Nonnull - public static Locale getDisplayLocaleOfSubtypeLocale(@Nonnull final String localeString) { - if (NO_LANGUAGE.equals(localeString)) { - return sResources.getConfiguration().locale; - } - if (sExceptionalLocaleDisplayedInRootLocale.containsKey(localeString)) { - return Locale.ROOT; - } - return LocaleUtils.constructLocaleFromString(localeString); - } - - public static String getSubtypeLocaleDisplayNameInSystemLocale( - @Nonnull final String localeString) { - final Locale displayLocale = sResources.getConfiguration().locale; - return getSubtypeLocaleDisplayNameInternal(localeString, displayLocale); - } - - @Nonnull - public static String getSubtypeLocaleDisplayName(@Nonnull final String localeString) { - final Locale displayLocale = getDisplayLocaleOfSubtypeLocale(localeString); - return getSubtypeLocaleDisplayNameInternal(localeString, displayLocale); - } - - @Nonnull - public static String getSubtypeLanguageDisplayName(@Nonnull final String localeString) { - final Locale displayLocale = getDisplayLocaleOfSubtypeLocale(localeString); - final String languageString; - if (sExceptionalLocaleDisplayedInRootLocale.containsKey(localeString)) { - languageString = localeString; - } else { - languageString = LocaleUtils.constructLocaleFromString(localeString).getLanguage(); - } - return getSubtypeLocaleDisplayNameInternal(languageString, displayLocale); - } - - @Nonnull - private static String getSubtypeLocaleDisplayNameInternal(@Nonnull final String localeString, - @Nonnull final Locale displayLocale) { - if (NO_LANGUAGE.equals(localeString)) { - // No language subtype should be displayed in system locale. - return sResources.getString(R.string.subtype_no_language); - } - final Integer exceptionalNameResId; - if (displayLocale.equals(Locale.ROOT) - && sExceptionalLocaleDisplayedInRootLocale.containsKey(localeString)) { - exceptionalNameResId = sExceptionalLocaleDisplayedInRootLocale.get(localeString); - } else if (sExceptionalLocaleToNameIdsMap.containsKey(localeString)) { - exceptionalNameResId = sExceptionalLocaleToNameIdsMap.get(localeString); - } else { - exceptionalNameResId = null; - } - - final String displayName; - if (exceptionalNameResId != null) { - final RunInLocale<String> getExceptionalName = new RunInLocale<String>() { - @Override - protected String job(final Resources res) { - return res.getString(exceptionalNameResId); - } - }; - displayName = getExceptionalName.runInLocale(sResources, displayLocale); - } else { - displayName = LocaleUtils.constructLocaleFromString(localeString) - .getDisplayName(displayLocale); - } - return StringUtils.capitalizeFirstCodePoint(displayName, displayLocale); - } - - // InputMethodSubtype's display name in its locale. - // isAdditionalSubtype (T=true, F=false) - // locale layout | display name - // ------ ------- - ---------------------- - // en_US qwerty F English (US) exception - // en_GB qwerty F English (UK) exception - // es_US spanish F Español (EE.UU.) exception - // fr azerty F Français - // fr_CA qwerty F Français (Canada) - // fr_CH swiss F Français (Suisse) - // de qwertz F Deutsch - // de_CH swiss T Deutsch (Schweiz) - // zz qwerty F Alphabet (QWERTY) in system locale - // fr qwertz T Français (QWERTZ) - // de qwerty T Deutsch (QWERTY) - // en_US azerty T English (US) (AZERTY) exception - // zz azerty T Alphabet (AZERTY) in system locale - - @Nonnull - private static String getReplacementString(@Nonnull final InputMethodSubtype subtype, - @Nonnull final Locale displayLocale) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN - && subtype.containsExtraValueKey(UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)) { - return subtype.getExtraValueOf(UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME); - } - return getSubtypeLocaleDisplayNameInternal(subtype.getLocale(), displayLocale); - } - - @Nonnull - public static String getSubtypeDisplayNameInSystemLocale( - @Nonnull final InputMethodSubtype subtype) { - final Locale displayLocale = sResources.getConfiguration().locale; - return getSubtypeDisplayNameInternal(subtype, displayLocale); - } - - @Nonnull - public static String getSubtypeNameForLogging(@Nullable final InputMethodSubtype subtype) { - if (subtype == null) { - return "<null subtype>"; - } - return getSubtypeLocale(subtype) + "/" + getKeyboardLayoutSetName(subtype); - } - - @Nonnull - private static String getSubtypeDisplayNameInternal(@Nonnull final InputMethodSubtype subtype, - @Nonnull final Locale displayLocale) { - final String replacementString = getReplacementString(subtype, displayLocale); - // TODO: rework this for multi-lingual subtypes - final int nameResId = subtype.getNameResId(); - final RunInLocale<String> getSubtypeName = new RunInLocale<String>() { - @Override - protected String job(final Resources res) { - try { - return res.getString(nameResId, replacementString); - } catch (Resources.NotFoundException e) { - // TODO: Remove this catch when InputMethodManager.getCurrentInputMethodSubtype - // is fixed. - Log.w(TAG, "Unknown subtype: mode=" + subtype.getMode() - + " nameResId=" + subtype.getNameResId() - + " locale=" + subtype.getLocale() - + " extra=" + subtype.getExtraValue() - + "\n" + DebugLogUtils.getStackTrace()); - return ""; - } - } - }; - return StringUtils.capitalizeFirstCodePoint( - getSubtypeName.runInLocale(sResources, displayLocale), displayLocale); - } - - @Nonnull - public static Locale getSubtypeLocale(@Nonnull final InputMethodSubtype subtype) { - final String localeString = subtype.getLocale(); - return LocaleUtils.constructLocaleFromString(localeString); - } - - @Nonnull - public static String getKeyboardLayoutSetDisplayName( - @Nonnull final InputMethodSubtype subtype) { - final String layoutName = getKeyboardLayoutSetName(subtype); - return getKeyboardLayoutSetDisplayName(layoutName); - } - - @Nonnull - public static String getKeyboardLayoutSetDisplayName(@Nonnull final String layoutName) { - return sKeyboardLayoutToDisplayNameMap.get(layoutName); - } - - @Nonnull - public static String getKeyboardLayoutSetName(final InputMethodSubtype subtype) { - String keyboardLayoutSet = subtype.getExtraValueOf(KEYBOARD_LAYOUT_SET); - if (keyboardLayoutSet == null) { - // This subtype doesn't have a keyboardLayoutSet extra value, so lookup its keyboard - // layout set in sLocaleAndExtraValueToKeyboardLayoutSetMap to keep it compatible with - // pre-JellyBean. - final String key = subtype.getLocale() + ":" + subtype.getExtraValue(); - keyboardLayoutSet = sLocaleAndExtraValueToKeyboardLayoutSetMap.get(key); - } - // TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is - // fixed. - if (keyboardLayoutSet == null) { - android.util.Log.w(TAG, "KeyboardLayoutSet not found, use QWERTY: " + - "locale=" + subtype.getLocale() + " extraValue=" + subtype.getExtraValue()); - return QWERTY; - } - return keyboardLayoutSet; - } - - public static String getCombiningRulesExtraValue(final InputMethodSubtype subtype) { - return subtype.getExtraValueOf(COMBINING_RULES); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/SuggestionResults.java b/java/src/com/android/inputmethod/latin/utils/SuggestionResults.java deleted file mode 100644 index 981355115..000000000 --- a/java/src/com/android/inputmethod/latin/utils/SuggestionResults.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; -import com.android.inputmethod.latin.define.ProductionFlags; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.TreeSet; - -/** - * A TreeSet of SuggestedWordInfo that is bounded in size and throws everything that's smaller - * than its limit - */ -public final class SuggestionResults extends TreeSet<SuggestedWordInfo> { - public final ArrayList<SuggestedWordInfo> mRawSuggestions; - // TODO: Instead of a boolean , we may want to include the context of this suggestion results, - // such as {@link NgramContext}. - public final boolean mIsBeginningOfSentence; - public final boolean mFirstSuggestionExceedsConfidenceThreshold; - private final int mCapacity; - - public SuggestionResults(final int capacity, final boolean isBeginningOfSentence, - final boolean firstSuggestionExceedsConfidenceThreshold) { - this(sSuggestedWordInfoComparator, capacity, isBeginningOfSentence, - firstSuggestionExceedsConfidenceThreshold); - } - - private SuggestionResults(final Comparator<SuggestedWordInfo> comparator, final int capacity, - final boolean isBeginningOfSentence, - final boolean firstSuggestionExceedsConfidenceThreshold) { - super(comparator); - mCapacity = capacity; - if (ProductionFlags.INCLUDE_RAW_SUGGESTIONS) { - mRawSuggestions = new ArrayList<>(); - } else { - mRawSuggestions = null; - } - mIsBeginningOfSentence = isBeginningOfSentence; - mFirstSuggestionExceedsConfidenceThreshold = firstSuggestionExceedsConfidenceThreshold; - } - - @Override - public boolean add(final SuggestedWordInfo e) { - if (size() < mCapacity) return super.add(e); - if (comparator().compare(e, last()) > 0) return false; - super.add(e); - pollLast(); // removes the last element - return true; - } - - @Override - public boolean addAll(final Collection<? extends SuggestedWordInfo> e) { - if (null == e) return false; - return super.addAll(e); - } - - static final class SuggestedWordInfoComparator implements Comparator<SuggestedWordInfo> { - // This comparator ranks the word info with the higher frequency first. That's because - // that's the order we want our elements in. - @Override - public int compare(final SuggestedWordInfo o1, final SuggestedWordInfo o2) { - if (o1.mScore > o2.mScore) return -1; - if (o1.mScore < o2.mScore) return 1; - if (o1.mCodePointCount < o2.mCodePointCount) return -1; - if (o1.mCodePointCount > o2.mCodePointCount) return 1; - return o1.mWord.compareTo(o2.mWord); - } - } - - private static final SuggestedWordInfoComparator sSuggestedWordInfoComparator = - new SuggestedWordInfoComparator(); -} diff --git a/java/src/com/android/inputmethod/latin/utils/TargetPackageInfoGetterTask.java b/java/src/com/android/inputmethod/latin/utils/TargetPackageInfoGetterTask.java deleted file mode 100644 index ab2b00e36..000000000 --- a/java/src/com/android/inputmethod/latin/utils/TargetPackageInfoGetterTask.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.utils; - -import android.content.Context; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.os.AsyncTask; -import android.util.LruCache; - -import com.android.inputmethod.compat.AppWorkaroundsUtils; - -public final class TargetPackageInfoGetterTask extends - AsyncTask<String, Void, PackageInfo> { - private static final int MAX_CACHE_ENTRIES = 64; // arbitrary - private static final LruCache<String, PackageInfo> sCache = new LruCache<>(MAX_CACHE_ENTRIES); - - public static PackageInfo getCachedPackageInfo(final String packageName) { - if (null == packageName) return null; - return sCache.get(packageName); - } - - public static void removeCachedPackageInfo(final String packageName) { - sCache.remove(packageName); - } - - private Context mContext; - private final AsyncResultHolder<AppWorkaroundsUtils> mResult; - - public TargetPackageInfoGetterTask(final Context context, - final AsyncResultHolder<AppWorkaroundsUtils> result) { - mContext = context; - mResult = result; - } - - @Override - protected PackageInfo doInBackground(final String... packageName) { - final PackageManager pm = mContext.getPackageManager(); - mContext = null; // Bazooka-powered anti-leak device - try { - final PackageInfo packageInfo = pm.getPackageInfo(packageName[0], 0 /* flags */); - sCache.put(packageName[0], packageInfo); - return packageInfo; - } catch (android.content.pm.PackageManager.NameNotFoundException e) { - return null; - } - } - - @Override - protected void onPostExecute(final PackageInfo info) { - mResult.set(new AppWorkaroundsUtils(info)); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/TextRange.java b/java/src/com/android/inputmethod/latin/utils/TextRange.java deleted file mode 100644 index dbf3b5060..000000000 --- a/java/src/com/android/inputmethod/latin/utils/TextRange.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.text.Spanned; -import android.text.style.SuggestionSpan; - -import java.util.Arrays; - -/** - * Represents a range of text, relative to the current cursor position. - */ -public final class TextRange { - private final CharSequence mTextAtCursor; - private final int mWordAtCursorStartIndex; - private final int mWordAtCursorEndIndex; - private final int mCursorIndex; - - public final CharSequence mWord; - public final boolean mHasUrlSpans; - - public int getNumberOfCharsInWordBeforeCursor() { - return mCursorIndex - mWordAtCursorStartIndex; - } - - public int getNumberOfCharsInWordAfterCursor() { - return mWordAtCursorEndIndex - mCursorIndex; - } - - public int length() { - return mWord.length(); - } - - /** - * Gets the suggestion spans that are put squarely on the word, with the exact start - * and end of the span matching the boundaries of the word. - * @return the list of spans. - */ - public SuggestionSpan[] getSuggestionSpansAtWord() { - if (!(mTextAtCursor instanceof Spanned && mWord instanceof Spanned)) { - return new SuggestionSpan[0]; - } - final Spanned text = (Spanned)mTextAtCursor; - // Note: it's fine to pass indices negative or greater than the length of the string - // to the #getSpans() method. The reason we need to get from -1 to +1 is that, the - // spans were cut at the cursor position, and #getSpans(start, end) does not return - // spans that end at `start' or begin at `end'. Consider the following case: - // this| is (The | symbolizes the cursor position - // ---- --- - // In this case, the cursor is in position 4, so the 0~7 span has been split into - // a 0~4 part and a 4~7 part. - // If we called #getSpans(0, 4) in this case, we would only get the part from 0 to 4 - // of the span, and not the part from 4 to 7, so we would not realize the span actually - // extends from 0 to 7. But if we call #getSpans(-1, 5) we'll get both the 0~4 and - // the 4~7 spans and we can merge them accordingly. - // Any span starting more than 1 char away from the word boundaries in any direction - // does not touch the word, so we don't need to consider it. That's why requesting - // -1 ~ +1 is enough. - // Of course this is only relevant if the cursor is at one end of the word. If it's - // in the middle, the -1 and +1 are not necessary, but they are harmless. - final SuggestionSpan[] spans = text.getSpans(mWordAtCursorStartIndex - 1, - mWordAtCursorEndIndex + 1, SuggestionSpan.class); - int readIndex = 0; - int writeIndex = 0; - for (; readIndex < spans.length; ++readIndex) { - final SuggestionSpan span = spans[readIndex]; - // The span may be null, as we null them when we find duplicates. Cf a few lines - // down. - if (null == span) continue; - // Tentative span start and end. This may be modified later if we realize the - // same span is also applied to other parts of the string. - int spanStart = text.getSpanStart(span); - int spanEnd = text.getSpanEnd(span); - for (int i = readIndex + 1; i < spans.length; ++i) { - if (span.equals(spans[i])) { - // We found the same span somewhere else. Read the new extent of this - // span, and adjust our values accordingly. - spanStart = Math.min(spanStart, text.getSpanStart(spans[i])); - spanEnd = Math.max(spanEnd, text.getSpanEnd(spans[i])); - // ...and mark the span as processed. - spans[i] = null; - } - } - if (spanStart == mWordAtCursorStartIndex && spanEnd == mWordAtCursorEndIndex) { - // If the span does not start and stop here, ignore it. It probably extends - // past the start or end of the word, as happens in missing space correction - // or EasyEditSpans put by voice input. - spans[writeIndex++] = spans[readIndex]; - } - } - return writeIndex == readIndex ? spans : Arrays.copyOfRange(spans, 0, writeIndex); - } - - public TextRange(final CharSequence textAtCursor, final int wordAtCursorStartIndex, - final int wordAtCursorEndIndex, final int cursorIndex, final boolean hasUrlSpans) { - if (wordAtCursorStartIndex < 0 || cursorIndex < wordAtCursorStartIndex - || cursorIndex > wordAtCursorEndIndex - || wordAtCursorEndIndex > textAtCursor.length()) { - throw new IndexOutOfBoundsException(); - } - mTextAtCursor = textAtCursor; - mWordAtCursorStartIndex = wordAtCursorStartIndex; - mWordAtCursorEndIndex = wordAtCursorEndIndex; - mCursorIndex = cursorIndex; - mHasUrlSpans = hasUrlSpans; - mWord = mTextAtCursor.subSequence(mWordAtCursorStartIndex, mWordAtCursorEndIndex); - } -}
\ No newline at end of file diff --git a/java/src/com/android/inputmethod/latin/utils/TypefaceUtils.java b/java/src/com/android/inputmethod/latin/utils/TypefaceUtils.java deleted file mode 100644 index fafba79c2..000000000 --- a/java/src/com/android/inputmethod/latin/utils/TypefaceUtils.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) 2013 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.utils; - -import android.graphics.Paint; -import android.graphics.Rect; -import android.graphics.Typeface; -import android.util.SparseArray; - -public final class TypefaceUtils { - private static final char[] KEY_LABEL_REFERENCE_CHAR = { 'M' }; - private static final char[] KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR = { '8' }; - - private TypefaceUtils() { - // This utility class is not publicly instantiable. - } - - // This sparse array caches key label text height in pixel indexed by key label text size. - private static final SparseArray<Float> sTextHeightCache = new SparseArray<>(); - // Working variable for the following method. - private static final Rect sTextHeightBounds = new Rect(); - - private static float getCharHeight(final char[] referenceChar, final Paint paint) { - final int key = getCharGeometryCacheKey(referenceChar[0], paint); - synchronized (sTextHeightCache) { - final Float cachedValue = sTextHeightCache.get(key); - if (cachedValue != null) { - return cachedValue; - } - - paint.getTextBounds(referenceChar, 0, 1, sTextHeightBounds); - final float height = sTextHeightBounds.height(); - sTextHeightCache.put(key, height); - return height; - } - } - - // This sparse array caches key label text width in pixel indexed by key label text size. - private static final SparseArray<Float> sTextWidthCache = new SparseArray<>(); - // Working variable for the following method. - private static final Rect sTextWidthBounds = new Rect(); - - private static float getCharWidth(final char[] referenceChar, final Paint paint) { - final int key = getCharGeometryCacheKey(referenceChar[0], paint); - synchronized (sTextWidthCache) { - final Float cachedValue = sTextWidthCache.get(key); - if (cachedValue != null) { - return cachedValue; - } - - paint.getTextBounds(referenceChar, 0, 1, sTextWidthBounds); - final float width = sTextWidthBounds.width(); - sTextWidthCache.put(key, width); - return width; - } - } - - private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) { - final int labelSize = (int)paint.getTextSize(); - final Typeface face = paint.getTypeface(); - final int codePointOffset = referenceChar << 15; - if (face == Typeface.DEFAULT) { - return codePointOffset + labelSize; - } else if (face == Typeface.DEFAULT_BOLD) { - return codePointOffset + labelSize + 0x1000; - } else if (face == Typeface.MONOSPACE) { - return codePointOffset + labelSize + 0x2000; - } else { - return codePointOffset + labelSize; - } - } - - public static float getReferenceCharHeight(final Paint paint) { - return getCharHeight(KEY_LABEL_REFERENCE_CHAR, paint); - } - - public static float getReferenceCharWidth(final Paint paint) { - return getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint); - } - - public static float getReferenceDigitWidth(final Paint paint) { - return getCharWidth(KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR, paint); - } - - // Working variable for the following method. - private static final Rect sStringWidthBounds = new Rect(); - - public static float getStringWidth(final String string, final Paint paint) { - synchronized (sStringWidthBounds) { - paint.getTextBounds(string, 0, string.length(), sStringWidthBounds); - return sStringWidthBounds.width(); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/UncachedInputMethodManagerUtils.java b/java/src/com/android/inputmethod/latin/utils/UncachedInputMethodManagerUtils.java deleted file mode 100644 index 5df00efb9..000000000 --- a/java/src/com/android/inputmethod/latin/utils/UncachedInputMethodManagerUtils.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.content.Context; -import android.provider.Settings; -import android.view.inputmethod.InputMethodInfo; -import android.view.inputmethod.InputMethodManager; - -/* - * A utility class for {@link InputMethodManager}. Unlike {@link RichInputMethodManager}, this - * class provides synchronous, non-cached access to {@link InputMethodManager}. The setup activity - * is a good example to use this class because {@link InputMethodManagerService} may not be aware of - * this IME immediately after this IME is installed. - */ -public final class UncachedInputMethodManagerUtils { - /** - * Check if the IME specified by the context is enabled. - * CAVEAT: This may cause a round trip IPC. - * - * @param context package context of the IME to be checked. - * @param imm the {@link InputMethodManager}. - * @return true if this IME is enabled. - */ - public static boolean isThisImeEnabled(final Context context, - final InputMethodManager imm) { - final String packageName = context.getPackageName(); - for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) { - if (packageName.equals(imi.getPackageName())) { - return true; - } - } - return false; - } - - /** - * Check if the IME specified by the context is the current IME. - * CAVEAT: This may cause a round trip IPC. - * - * @param context package context of the IME to be checked. - * @param imm the {@link InputMethodManager}. - * @return true if this IME is the current IME. - */ - public static boolean isThisImeCurrent(final Context context, - final InputMethodManager imm) { - final InputMethodInfo imi = getInputMethodInfoOf(context.getPackageName(), imm); - final String currentImeId = Settings.Secure.getString( - context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); - return imi != null && imi.getId().equals(currentImeId); - } - - /** - * Get {@link InputMethodInfo} of the IME specified by the package name. - * CAVEAT: This may cause a round trip IPC. - * - * @param packageName package name of the IME. - * @param imm the {@link InputMethodManager}. - * @return the {@link InputMethodInfo} of the IME specified by the <code>packageName</code>, - * or null if not found. - */ - public static InputMethodInfo getInputMethodInfoOf(final String packageName, - final InputMethodManager imm) { - for (final InputMethodInfo imi : imm.getInputMethodList()) { - if (packageName.equals(imi.getPackageName())) { - return imi; - } - } - return null; - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/ViewLayoutUtils.java b/java/src/com/android/inputmethod/latin/utils/ViewLayoutUtils.java deleted file mode 100644 index 0bcba2754..000000000 --- a/java/src/com/android/inputmethod/latin/utils/ViewLayoutUtils.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.utils; - -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewGroup.MarginLayoutParams; -import android.view.Window; -import android.view.WindowManager; -import android.widget.FrameLayout; -import android.widget.LinearLayout; -import android.widget.RelativeLayout; - -public final class ViewLayoutUtils { - private ViewLayoutUtils() { - // This utility class is not publicly instantiable. - } - - public static MarginLayoutParams newLayoutParam(final ViewGroup placer, final int width, - final int height) { - if (placer instanceof FrameLayout) { - return new FrameLayout.LayoutParams(width, height); - } else if (placer instanceof RelativeLayout) { - return new RelativeLayout.LayoutParams(width, height); - } else if (placer == null) { - throw new NullPointerException("placer is null"); - } else { - throw new IllegalArgumentException("placer is neither FrameLayout nor RelativeLayout: " - + placer.getClass().getName()); - } - } - - public static void placeViewAt(final View view, final int x, final int y, final int w, - final int h) { - final ViewGroup.LayoutParams lp = view.getLayoutParams(); - if (lp instanceof MarginLayoutParams) { - final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp; - marginLayoutParams.width = w; - marginLayoutParams.height = h; - marginLayoutParams.setMargins(x, y, 0, 0); - } - } - - public static void updateLayoutHeightOf(final Window window, final int layoutHeight) { - final WindowManager.LayoutParams params = window.getAttributes(); - if (params != null && params.height != layoutHeight) { - params.height = layoutHeight; - window.setAttributes(params); - } - } - - public static void updateLayoutHeightOf(final View view, final int layoutHeight) { - final ViewGroup.LayoutParams params = view.getLayoutParams(); - if (params != null && params.height != layoutHeight) { - params.height = layoutHeight; - view.setLayoutParams(params); - } - } - - public static void updateLayoutGravityOf(final View view, final int layoutGravity) { - final ViewGroup.LayoutParams lp = view.getLayoutParams(); - if (lp instanceof LinearLayout.LayoutParams) { - final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)lp; - if (params.gravity != layoutGravity) { - params.gravity = layoutGravity; - view.setLayoutParams(params); - } - } else if (lp instanceof FrameLayout.LayoutParams) { - final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)lp; - if (params.gravity != layoutGravity) { - params.gravity = layoutGravity; - view.setLayoutParams(params); - } - } else { - throw new IllegalArgumentException("Layout parameter doesn't have gravity: " - + lp.getClass().getName()); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/WordInputEventForPersonalization.java b/java/src/com/android/inputmethod/latin/utils/WordInputEventForPersonalization.java deleted file mode 100644 index fc0a9cb6c..000000000 --- a/java/src/com/android/inputmethod/latin/utils/WordInputEventForPersonalization.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2014 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.utils; - -import android.util.Log; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.NgramContext; -import com.android.inputmethod.latin.common.StringUtils; -import com.android.inputmethod.latin.define.DecoderSpecificConstants; -import com.android.inputmethod.latin.settings.SpacingAndPunctuations; - -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -// Note: this class is used as a parameter type of a native method. You should be careful when you -// rename this class or field name. See BinaryDictionary#addMultipleDictionaryEntriesNative(). -public final class WordInputEventForPersonalization { - private static final String TAG = WordInputEventForPersonalization.class.getSimpleName(); - private static final boolean DEBUG_TOKEN = false; - - public final int[] mTargetWord; - public final int mPrevWordsCount; - public final int[][] mPrevWordArray = - new int[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM][]; - public final boolean[] mIsPrevWordBeginningOfSentenceArray = - new boolean[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; - // Time stamp in seconds. - public final int mTimestamp; - - @UsedForTesting - public WordInputEventForPersonalization(final CharSequence targetWord, - final NgramContext ngramContext, final int timestamp) { - mTargetWord = StringUtils.toCodePointArray(targetWord); - mPrevWordsCount = ngramContext.getPrevWordCount(); - ngramContext.outputToArray(mPrevWordArray, mIsPrevWordBeginningOfSentenceArray); - mTimestamp = timestamp; - } - - // Process a list of words and return a list of {@link WordInputEventForPersonalization} - // objects. - public static ArrayList<WordInputEventForPersonalization> createInputEventFrom( - final List<String> tokens, final int timestamp, - final SpacingAndPunctuations spacingAndPunctuations, final Locale locale) { - final ArrayList<WordInputEventForPersonalization> inputEvents = new ArrayList<>(); - final int N = tokens.size(); - NgramContext ngramContext = NgramContext.EMPTY_PREV_WORDS_INFO; - for (int i = 0; i < N; ++i) { - final String tempWord = tokens.get(i); - if (StringUtils.isEmptyStringOrWhiteSpaces(tempWord)) { - // just skip this token - if (DEBUG_TOKEN) { - Log.d(TAG, "--- isEmptyStringOrWhiteSpaces: \"" + tempWord + "\""); - } - continue; - } - if (!DictionaryInfoUtils.looksValidForDictionaryInsertion( - tempWord, spacingAndPunctuations)) { - if (DEBUG_TOKEN) { - Log.d(TAG, "--- not looksValidForDictionaryInsertion: \"" - + tempWord + "\""); - } - // Sentence terminator found. Split. - // TODO: Detect whether the context is beginning-of-sentence. - ngramContext = NgramContext.EMPTY_PREV_WORDS_INFO; - continue; - } - if (DEBUG_TOKEN) { - Log.d(TAG, "--- word: \"" + tempWord + "\""); - } - final WordInputEventForPersonalization inputEvent = - detectWhetherVaildWordOrNotAndGetInputEvent( - ngramContext, tempWord, timestamp, locale); - if (inputEvent == null) { - continue; - } - inputEvents.add(inputEvent); - ngramContext = ngramContext.getNextNgramContext(new NgramContext.WordInfo(tempWord)); - } - return inputEvents; - } - - private static WordInputEventForPersonalization detectWhetherVaildWordOrNotAndGetInputEvent( - final NgramContext ngramContext, final String targetWord, final int timestamp, - final Locale locale) { - if (locale == null) { - return null; - } - return new WordInputEventForPersonalization(targetWord, ngramContext, timestamp); - } -} diff --git a/java/src/com/android/inputmethod/latin/utils/XmlParseUtils.java b/java/src/com/android/inputmethod/latin/utils/XmlParseUtils.java deleted file mode 100644 index bdad16652..000000000 --- a/java/src/com/android/inputmethod/latin/utils/XmlParseUtils.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.utils; - -import android.content.res.TypedArray; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; - -import java.io.IOException; - -public final class XmlParseUtils { - private XmlParseUtils() { - // This utility class is not publicly instantiable. - } - - @SuppressWarnings("serial") - public static class ParseException extends XmlPullParserException { - public ParseException(final String msg, final XmlPullParser parser) { - super(msg + " at " + parser.getPositionDescription()); - } - } - - @SuppressWarnings("serial") - public static final class IllegalStartTag extends ParseException { - public IllegalStartTag(final XmlPullParser parser, final String tag, final String parent) { - super("Illegal start tag " + tag + " in " + parent, parser); - } - } - - @SuppressWarnings("serial") - public static final class IllegalEndTag extends ParseException { - public IllegalEndTag(final XmlPullParser parser, final String tag, final String parent) { - super("Illegal end tag " + tag + " in " + parent, parser); - } - } - - @SuppressWarnings("serial") - public static final class IllegalAttribute extends ParseException { - public IllegalAttribute(final XmlPullParser parser, final String tag, - final String attribute) { - super("Tag " + tag + " has illegal attribute " + attribute, parser); - } - } - - @SuppressWarnings("serial") - public static final class NonEmptyTag extends ParseException{ - public NonEmptyTag(final XmlPullParser parser, final String tag) { - super(tag + " must be empty tag", parser); - } - } - - public static void checkEndTag(final String tag, final XmlPullParser parser) - throws XmlPullParserException, IOException { - if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName())) - return; - throw new NonEmptyTag(parser, tag); - } - - public static void checkAttributeExists(final TypedArray attr, final int attrId, - final String attrName, final String tag, final XmlPullParser parser) - throws XmlPullParserException { - if (attr.hasValue(attrId)) { - return; - } - throw new ParseException( - "No " + attrName + " attribute found in <" + tag + "/>", parser); - } -} |