aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/DictionaryFactory.java
diff options
context:
space:
mode:
authorTadashi G. Takaoka <takaoka@google.com>2012-04-11 14:58:02 +0900
committerTadashi G. Takaoka <takaoka@google.com>2012-04-11 16:11:34 +0900
commit78ab80844b4f8e0369f4e86b2a02208197f9bd34 (patch)
treea54f3764536ae6e8387faee37d5235a889b57c80 /java/src/com/android/inputmethod/latin/DictionaryFactory.java
parent2be51f4fd0c5cd70c7a2757558ffe45e703700cf (diff)
downloadlatinime-78ab80844b4f8e0369f4e86b2a02208197f9bd34.tar.gz
latinime-78ab80844b4f8e0369f4e86b2a02208197f9bd34.tar.xz
latinime-78ab80844b4f8e0369f4e86b2a02208197f9bd34.zip
Add language suffix to main dictionary
Bug: 6319377 Change-Id: Ie6a887fefa12e33c17bfeb5d22984e7c1a7bdb46
Diffstat (limited to 'java/src/com/android/inputmethod/latin/DictionaryFactory.java')
-rw-r--r--java/src/com/android/inputmethod/latin/DictionaryFactory.java89
1 files changed, 45 insertions, 44 deletions
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java
index fedb45407..490a32794 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java
@@ -21,8 +21,6 @@ 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;
@@ -101,13 +99,7 @@ public class DictionaryFactory {
final int resId, final Locale locale) {
AssetFileDescriptor afd = null;
try {
- final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() {
- @Override
- protected AssetFileDescriptor job(Resources res) {
- return res.openRawResourceFd(resId);
- }
- };
- afd = job.runInLocale(context.getResources(), locale);
+ afd = context.getResources().openRawResourceFd(resId);
if (afd == null) {
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
return null;
@@ -163,41 +155,31 @@ public class DictionaryFactory {
* @return whether a (non-placeholder) dictionary is available or not.
*/
public static boolean isDictionaryAvailable(Context context, Locale locale) {
- 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);
+ final Resources res = context.getResources();
+ final int resourceId = getMainDictionaryResourceId(res, locale);
+ 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;
}
// 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 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);
+ final Resources res = context.getResources();
+ final int resourceId = getMainDictionaryResourceId(res, locale);
+ 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;
}
// TODO: Find the Right Way to find out whether the resource is a placeholder or not.
@@ -214,13 +196,32 @@ public class DictionaryFactory {
return (afd != null && afd.getLength() > PLACEHOLDER_LENGTH);
}
+ private static final String DEFAULT_MAIN_DICT = "main";
+ private static final String MAIN_DICT_PREFIX = "main_";
+
/**
* Returns a main dictionary resource id
+ * @param locale dictionary locale
* @return main dictionary resource id
*/
- public static int getMainDictionaryResourceId(Resources res) {
- final String MAIN_DIC_NAME = "main";
- String packageName = LatinIME.class.getPackage().getName();
- return res.getIdentifier(MAIN_DIC_NAME, "raw", packageName);
+ public static int getMainDictionaryResourceId(Resources res, Locale locale) {
+ final String packageName = LatinIME.class.getPackage().getName();
+ int resId;
+
+ // Try to find main_language_country dictionary.
+ if (!locale.getCountry().isEmpty()) {
+ final String dictLanguageCountry = MAIN_DICT_PREFIX + locale.toString().toLowerCase();
+ if ((resId = res.getIdentifier(dictLanguageCountry, "raw", packageName)) != 0) {
+ return resId;
+ }
+ }
+
+ // Try to find main_language dictionary.
+ final String dictLanguage = MAIN_DICT_PREFIX + locale.getLanguage();
+ if ((resId = res.getIdentifier(dictLanguage, "raw", packageName)) != 0) {
+ return resId;
+ }
+
+ return res.getIdentifier(DEFAULT_MAIN_DICT, "raw", packageName);
}
}