aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/DictionaryFactory.java
diff options
context:
space:
mode:
authorTadashi G. Takaoka <takaoka@google.com>2012-04-03 14:28:56 +0900
committerTadashi G. Takaoka <takaoka@google.com>2012-04-03 17:43:45 +0900
commit16c6f355700ee5cdaa029f4a25b8b3d40718e6ab (patch)
treeb3863cd867379b6f3a1c15b19c16364d3b44676e /java/src/com/android/inputmethod/latin/DictionaryFactory.java
parent78e333594bbc97e56ad105ce3888192e78771626 (diff)
downloadlatinime-16c6f355700ee5cdaa029f4a25b8b3d40718e6ab.tar.gz
latinime-16c6f355700ee5cdaa029f4a25b8b3d40718e6ab.tar.xz
latinime-16c6f355700ee5cdaa029f4a25b8b3d40718e6ab.zip
Add RunInLocale class to guard locale switching
Bug: 6128216 Change-Id: I8d9c75c773c3de886183b291ada7a3836295839b
Diffstat (limited to 'java/src/com/android/inputmethod/latin/DictionaryFactory.java')
-rw-r--r--java/src/com/android/inputmethod/latin/DictionaryFactory.java78
1 files changed, 40 insertions, 38 deletions
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java
index 77c685c50..7be374db5 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java
@@ -21,6 +21,8 @@ import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.util.Log;
+import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
+
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
@@ -30,7 +32,6 @@ import java.util.Locale;
* Factory for dictionary instances.
*/
public class DictionaryFactory {
-
private static String TAG = DictionaryFactory.class.getSimpleName();
/**
@@ -98,14 +99,13 @@ public class DictionaryFactory {
final int resId, final Locale locale) {
AssetFileDescriptor afd = null;
try {
- final Resources res = context.getResources();
- if (null != locale) {
- final Locale savedLocale = LocaleUtils.setSystemLocale(res, locale);
- afd = res.openRawResourceFd(resId);
- LocaleUtils.setSystemLocale(res, savedLocale);
- } else {
- afd = res.openRawResourceFd(resId);
- }
+ final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() {
+ @Override
+ protected AssetFileDescriptor job(Resources res) {
+ return res.openRawResourceFd(resId);
+ }
+ };
+ afd = job.runInLocale(context.getResources(), locale);
if (afd == null) {
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
return null;
@@ -161,39 +161,41 @@ public class DictionaryFactory {
* @return whether a (non-placeholder) dictionary is available or not.
*/
public static boolean isDictionaryAvailable(Context context, Locale locale) {
- final Resources res = context.getResources();
- final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale);
-
- final int resourceId = getMainDictionaryResourceId(res);
- final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
- final boolean hasDictionary = isFullDictionary(afd);
- try {
- if (null != afd) afd.close();
- } catch (java.io.IOException e) {
- /* Um, what can we do here exactly? */
- }
-
- LocaleUtils.setSystemLocale(res, saveLocale);
- return hasDictionary;
+ final RunInLocale<Boolean> job = new RunInLocale<Boolean>() {
+ @Override
+ protected Boolean job(Resources res) {
+ final int resourceId = getMainDictionaryResourceId(res);
+ final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
+ final boolean hasDictionary = isFullDictionary(afd);
+ try {
+ if (null != afd) afd.close();
+ } catch (java.io.IOException e) {
+ /* Um, what can we do here exactly? */
+ }
+ return hasDictionary;
+ }
+ };
+ return job.runInLocale(context.getResources(), locale);
}
// TODO: Do not use the size of the dictionary as an unique dictionary ID.
public static Long getDictionaryId(final Context context, final Locale locale) {
- final Resources res = context.getResources();
- final Locale saveLocale = LocaleUtils.setSystemLocale(res, locale);
-
- final int resourceId = getMainDictionaryResourceId(res);
- final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
- final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH)
- ? afd.getLength()
- : null;
- try {
- if (null != afd) afd.close();
- } catch (java.io.IOException e) {
- }
-
- LocaleUtils.setSystemLocale(res, saveLocale);
- return size;
+ final RunInLocale<Long> job = new RunInLocale<Long>() {
+ @Override
+ protected Long job(Resources res) {
+ final int resourceId = getMainDictionaryResourceId(res);
+ final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
+ final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH)
+ ? afd.getLength()
+ : null;
+ try {
+ if (null != afd) afd.close();
+ } catch (java.io.IOException e) {
+ }
+ return size;
+ }
+ };
+ return job.runInLocale(context.getResources(), locale);
}
// TODO: Find the Right Way to find out whether the resource is a placeholder or not.