Set the locale for opening an asset
This is necessary because we don't know any more whether the locale of the process is the expected one when the dictionary is loaded asynchronously. Bug: 5023141 Change-Id: Ia9e4741f3b4a04a9f085f5b65ec122471b0c2dff
This commit is contained in:
parent
728ab86f8c
commit
e150ef9856
3 changed files with 30 additions and 9 deletions
|
@ -19,6 +19,7 @@ package com.android.inputmethod.latin;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.AssetFileDescriptor;
|
import android.content.res.AssetFileDescriptor;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
@ -129,8 +130,11 @@ public class BinaryDictionaryFileDumper {
|
||||||
*/
|
*/
|
||||||
public static String getDictionaryFileFromResource(int resource, Locale locale,
|
public static String getDictionaryFileFromResource(int resource, Locale locale,
|
||||||
Context context) throws FileNotFoundException, IOException {
|
Context context) throws FileNotFoundException, IOException {
|
||||||
return copyFileTo(context.getResources().openRawResource(resource),
|
final Resources res = context.getResources();
|
||||||
getCacheFileNameForLocale(locale, context));
|
final Locale savedLocale = Utils.setSystemLocale(res, locale);
|
||||||
|
final InputStream stream = res.openRawResource(resource);
|
||||||
|
Utils.setSystemLocale(res, savedLocale);
|
||||||
|
return copyFileTo(stream, getCacheFileNameForLocale(locale, context));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -18,6 +18,7 @@ package com.android.inputmethod.latin;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.AssetFileDescriptor;
|
import android.content.res.AssetFileDescriptor;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
@ -42,8 +43,13 @@ class BinaryDictionaryGetter {
|
||||||
/**
|
/**
|
||||||
* Returns a file address from a resource, or null if it cannot be opened.
|
* Returns a file address from a resource, or null if it cannot be opened.
|
||||||
*/
|
*/
|
||||||
private static AssetFileAddress loadFallbackResource(Context context, int fallbackResId) {
|
private static AssetFileAddress loadFallbackResource(final Context context,
|
||||||
final AssetFileDescriptor afd = context.getResources().openRawResourceFd(fallbackResId);
|
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) {
|
if (afd == null) {
|
||||||
Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId="
|
Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId="
|
||||||
+ fallbackResId);
|
+ fallbackResId);
|
||||||
|
@ -91,7 +97,8 @@ class BinaryDictionaryGetter {
|
||||||
Log.e(TAG, "Unable to read source data for locale "
|
Log.e(TAG, "Unable to read source data for locale "
|
||||||
+ locale.toString() + ": falling back to internal dictionary");
|
+ 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;
|
if (null == fallbackAsset) return null;
|
||||||
return Arrays.asList(fallbackAsset);
|
return Arrays.asList(fallbackAsset);
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ public class DictionaryFactory {
|
||||||
int fallbackResId) {
|
int fallbackResId) {
|
||||||
if (null == locale) {
|
if (null == locale) {
|
||||||
Log.e(TAG, "No locale defined for dictionary");
|
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>();
|
final List<Dictionary> dictList = new LinkedList<Dictionary>();
|
||||||
|
@ -76,7 +76,8 @@ public class DictionaryFactory {
|
||||||
// we found could not be opened by the native code for any reason (format mismatch,
|
// 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
|
// 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.
|
// case we want to fall back on the resource.
|
||||||
return new DictionaryCollection(createBinaryDictionary(context, fallbackResId));
|
return new DictionaryCollection(createBinaryDictionary(context, fallbackResId,
|
||||||
|
locale));
|
||||||
} else {
|
} else {
|
||||||
return new DictionaryCollection(dictList);
|
return new DictionaryCollection(dictList);
|
||||||
}
|
}
|
||||||
|
@ -87,12 +88,21 @@ public class DictionaryFactory {
|
||||||
* Initializes a dictionary from a raw resource file
|
* Initializes a dictionary from a raw resource file
|
||||||
* @param context application context for reading resources
|
* @param context application context for reading resources
|
||||||
* @param resId the resource containing the raw binary dictionary
|
* @param resId the resource containing the raw binary dictionary
|
||||||
|
* @param locale the locale to use for the resource
|
||||||
* @return an initialized instance of BinaryDictionary
|
* @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;
|
AssetFileDescriptor afd = null;
|
||||||
try {
|
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) {
|
if (afd == null) {
|
||||||
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
|
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in a new issue