diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
4 files changed, 47 insertions, 13 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index 76a230f82..00d80f566 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -19,6 +19,7 @@ package com.android.inputmethod.latin; import android.content.ContentResolver; import android.content.Context; import android.content.res.AssetFileDescriptor; +import android.content.res.Resources; import android.net.Uri; import android.text.TextUtils; @@ -129,8 +130,11 @@ public class BinaryDictionaryFileDumper { */ public static String getDictionaryFileFromResource(int resource, Locale locale, Context context) throws FileNotFoundException, IOException { - return copyFileTo(context.getResources().openRawResource(resource), - getCacheFileNameForLocale(locale, context)); + final Resources res = context.getResources(); + final Locale savedLocale = Utils.setSystemLocale(res, locale); + final InputStream stream = res.openRawResource(resource); + Utils.setSystemLocale(res, savedLocale); + return copyFileTo(stream, getCacheFileNameForLocale(locale, context)); } /** diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index bce787db2..989a0e9a0 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -18,6 +18,7 @@ package com.android.inputmethod.latin; import android.content.Context; import android.content.res.AssetFileDescriptor; +import android.content.res.Resources; import android.util.Log; import java.io.FileNotFoundException; @@ -42,8 +43,13 @@ class BinaryDictionaryGetter { /** * Returns a file address from a resource, or null if it cannot be opened. */ - private static AssetFileAddress loadFallbackResource(Context context, int fallbackResId) { - final AssetFileDescriptor afd = context.getResources().openRawResourceFd(fallbackResId); + private static AssetFileAddress loadFallbackResource(final Context context, + final int fallbackResId, final Locale locale) { + final Resources res = context.getResources(); + final Locale savedLocale = Utils.setSystemLocale(res, locale); + final AssetFileDescriptor afd = res.openRawResourceFd(fallbackResId); + Utils.setSystemLocale(res, savedLocale); + if (afd == null) { Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId=" + fallbackResId); @@ -91,7 +97,8 @@ class BinaryDictionaryGetter { Log.e(TAG, "Unable to read source data for locale " + locale.toString() + ": falling back to internal dictionary"); } - final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId); + final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId, + locale); if (null == fallbackAsset) return null; return Arrays.asList(fallbackAsset); } diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java index a35b0f5b0..39b4f63a5 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java @@ -48,7 +48,7 @@ public class DictionaryFactory { int fallbackResId) { if (null == locale) { Log.e(TAG, "No locale defined for dictionary"); - return new DictionaryCollection(createBinaryDictionary(context, fallbackResId)); + return new DictionaryCollection(createBinaryDictionary(context, fallbackResId, locale)); } final List<Dictionary> dictList = new LinkedList<Dictionary>(); @@ -56,8 +56,11 @@ public class DictionaryFactory { BinaryDictionaryGetter.getDictionaryFiles(locale, context, fallbackResId); if (null != assetFileList) { for (final AssetFileAddress f : assetFileList) { - dictList.add( - new BinaryDictionary(context, f.mFilename, f.mOffset, f.mLength, null)); + final BinaryDictionary binaryDictionary = + new BinaryDictionary(context, f.mFilename, f.mOffset, f.mLength, null); + if (binaryDictionary.isValidDictionary()) { + dictList.add(binaryDictionary); + } } } @@ -67,7 +70,17 @@ public class DictionaryFactory { if (null == dictList) { return new DictionaryCollection(); } else { - return new DictionaryCollection(dictList); + if (dictList.isEmpty()) { + // The list may be empty if no dictionaries have been added. The getter should not + // return an empty list, but if it does we end up here. Likewise, if the files + // we found could not be opened by the native code for any reason (format mismatch, + // file too big to fit in memory, etc) then we could have an empty list. In this + // case we want to fall back on the resource. + return new DictionaryCollection(createBinaryDictionary(context, fallbackResId, + locale)); + } else { + return new DictionaryCollection(dictList); + } } } @@ -75,12 +88,21 @@ public class DictionaryFactory { * Initializes a dictionary from a raw resource file * @param context application context for reading resources * @param resId the resource containing the raw binary dictionary + * @param locale the locale to use for the resource * @return an initialized instance of BinaryDictionary */ - protected static BinaryDictionary createBinaryDictionary(Context context, int resId) { + protected static BinaryDictionary createBinaryDictionary(final Context context, + final int resId, final Locale locale) { AssetFileDescriptor afd = null; try { - afd = context.getResources().openRawResourceFd(resId); + final Resources res = context.getResources(); + if (null != locale) { + final Locale savedLocale = Utils.setSystemLocale(res, locale); + afd = res.openRawResourceFd(resId); + Utils.setSystemLocale(res, savedLocale); + } else { + afd = res.openRawResourceFd(resId); + } if (afd == null) { Log.e(TAG, "Found the resource but it is compressed. resId=" + resId); return null; diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 5d8fd3411..64f7e6011 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -546,8 +546,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar // know now whether this is a password text field, because we need to know now whether we // want to enable the voice button. final VoiceProxy voiceIme = mVoiceProxy; - voiceIme.resetVoiceStates(InputTypeCompatUtils.isPasswordInputType(attribute.inputType) - || InputTypeCompatUtils.isVisiblePasswordInputType(attribute.inputType)); + final int inputType = (attribute != null) ? attribute.inputType : 0; + voiceIme.resetVoiceStates(InputTypeCompatUtils.isPasswordInputType(inputType) + || InputTypeCompatUtils.isVisiblePasswordInputType(inputType)); initializeInputAttributes(attribute); |