Consolidate main dictionary files.

This change is a preparation for upcoming optimizations on dictionary file loading.
* We can consolidate dictionary files because we are no longer relying on Asset Manager.
* Stopping compressing dictionary files as planning to use mmap() on the region in the apk file.
* Probably we won't rely on Asset Manager.  Instead we'll probably use offset and size obtained from AssetFileDescriptor.

Change-Id: Id57dce512fd3d2397a58628f8264bd824194da76
main
Ken Wakasa 2011-01-05 00:39:41 +09:00
parent a96574fdd5
commit 458249e703
7 changed files with 46 additions and 101 deletions

View File

@ -13,7 +13,8 @@ LOCAL_JNI_SHARED_LIBRARIES := libjni_latinime
LOCAL_STATIC_JAVA_LIBRARIES := android-common
#LOCAL_AAPT_FLAGS := -0 .dict
# Do not compress dictionary files to mmap dict data runtime
LOCAL_AAPT_FLAGS := -0 .dict
# Include all the resources regardless of system supported locales
LOCAL_AAPT_INCLUDE_ALL_RESOURCES := true

View File

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 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.
*/
-->
<dictionary>
<part name = "main" />
</dictionary>

View File

@ -71,8 +71,8 @@ public class BinaryDictionary extends Dictionary {
* @param context application context for reading resources
* @param resId the resource containing the raw binary dictionary
*/
public BinaryDictionary(Context context, int[] resId, int dicTypeId) {
if (resId != null && resId.length > 0 && resId[0] != 0) {
public BinaryDictionary(Context context, int resId, int dicTypeId) {
if (resId != 0) {
loadDictionary(context, resId);
}
mDicTypeId = dicTypeId;
@ -104,30 +104,21 @@ public class BinaryDictionary extends Dictionary {
int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives);
private native void closeNative(int dict);
private native boolean isValidWordNative(int nativeData, char[] word, int wordLength);
private native int getSuggestionsNative(int dict, int[] inputCodes, int codesSize,
private native int getSuggestionsNative(int dict, int[] inputCodes, int codesSize,
char[] outputChars, int[] frequencies,
int[] nextLettersFrequencies, int nextLettersSize);
private native int getBigramsNative(int dict, char[] prevWord, int prevWordLength,
int[] inputCodes, int inputCodesLength, char[] outputChars, int[] frequencies,
int maxWordLength, int maxBigrams, int maxAlternatives);
private final void loadDictionary(Context context, int[] resId) {
InputStream[] is = null;
private final void loadDictionary(Context context, int resId) {
InputStream is = null;
try {
// merging separated dictionary into one if dictionary is separated
int total = 0;
is = new InputStream[resId.length];
for (int i = 0; i < resId.length; i++) {
is[i] = context.getResources().openRawResource(resId[i]);
total += is[i].available();
}
is = context.getResources().openRawResource(resId);
final int total = is.available();
mNativeDictDirectBuffer =
ByteBuffer.allocateDirect(total).order(ByteOrder.nativeOrder());
int got = 0;
for (int i = 0; i < resId.length; i++) {
got += Channels.newChannel(is[i]).read(mNativeDictDirectBuffer);
}
final int got = Channels.newChannel(is).read(mNativeDictDirectBuffer);
if (got != total) {
Log.e(TAG, "Read " + got + " bytes, expected " + total);
} else {
@ -140,11 +131,7 @@ public class BinaryDictionary extends Dictionary {
Log.w(TAG, "No available memory for binary dictionary");
} finally {
try {
if (is != null) {
for (int i = 0; i < is.length; i++) {
is[i].close();
}
}
if (is != null) is.close();
} catch (IOException e) {
Log.w(TAG, "Failed to close input stream");
}

View File

@ -99,15 +99,15 @@ public class InputLanguageSelection extends PreferenceActivity {
}
private boolean hasDictionary(Locale locale) {
Resources res = getResources();
Configuration conf = res.getConfiguration();
Locale saveLocale = conf.locale;
final Resources res = getResources();
final Configuration conf = res.getConfiguration();
final Locale saveLocale = conf.locale;
boolean haveDictionary = false;
conf.locale = locale;
res.updateConfiguration(conf, res.getDisplayMetrics());
int[] dictionaries = LatinIME.getDictionary(res);
BinaryDictionary bd = new BinaryDictionary(this, dictionaries, Suggest.DIC_MAIN);
int mainDicResId = LatinIME.getMainDictionaryResourceId(res);
BinaryDictionary bd = new BinaryDictionary(this, mainDicResId, Suggest.DIC_MAIN);
// Is the dictionary larger than a placeholder? Arbitrarily chose a lower limit of
// 4000-5000 words, whereas the LARGE_DICTIONARY is about 20000+ words.

View File

@ -347,49 +347,19 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
/**
* Loads a dictionary or multiple separated dictionary
* @return returns array of dictionary resource ids
* Returns a main dictionary resource id
* @return main dictionary resource id
*/
public static int[] getDictionary(Resources res) {
public static int getMainDictionaryResourceId(Resources res) {
final String MAIN_DIC_NAME = "main";
String packageName = LatinIME.class.getPackage().getName();
XmlResourceParser xrp = res.getXml(R.xml.dictionary);
ArrayList<Integer> dictionaries = new ArrayList<Integer>();
try {
int current = xrp.getEventType();
while (current != XmlPullParser.END_DOCUMENT) {
if (current == XmlPullParser.START_TAG) {
String tag = xrp.getName();
if (tag != null) {
if (tag.equals("part")) {
String dictFileName = xrp.getAttributeValue(null, "name");
dictionaries.add(res.getIdentifier(dictFileName, "raw", packageName));
}
}
}
xrp.next();
current = xrp.getEventType();
}
} catch (XmlPullParserException e) {
Log.e(TAG, "Dictionary XML parsing failure");
} catch (IOException e) {
Log.e(TAG, "Dictionary XML IOException");
}
int count = dictionaries.size();
int[] dict = new int[count];
for (int i = 0; i < count; i++) {
dict[i] = dictionaries.get(i);
}
return dict;
return res.getIdentifier(MAIN_DIC_NAME, "raw", packageName);
}
private void initSuggest() {
updateAutoTextEnabled();
String locale = mSubtypeSwitcher.getInputLocaleStr();
Resources orig = getResources();
Locale savedLocale = mSubtypeSwitcher.changeSystemLocale(new Locale(locale));
if (mSuggest != null) {
mSuggest.close();
@ -397,8 +367,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final SharedPreferences prefs = mPrefs;
mQuickFixes = prefs.getBoolean(Settings.PREF_QUICK_FIXES, true);
int[] dictionaries = getDictionary(orig);
mSuggest = new Suggest(this, dictionaries);
final Resources res = mResources;
int mainDicResId = getMainDictionaryResourceId(res);
mSuggest = new Suggest(this, mainDicResId);
loadAndSetAutoCorrectionThreshold(prefs);
if (mUserDictionary != null) mUserDictionary.close();
mUserDictionary = new UserDictionary(this, locale);
@ -418,8 +389,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mSuggest.setContactsDictionary(mContactsDictionary);
mSuggest.setAutoDictionary(mAutoDictionary);
updateCorrectionMode();
mWordSeparators = mResources.getString(R.string.word_separators);
mSentenceSeparators = mResources.getString(R.string.sentence_separators);
mWordSeparators = res.getString(R.string.word_separators);
mSentenceSeparators = res.getString(R.string.sentence_separators);
mSubtypeSwitcher.changeSystemLocale(savedLocale);
}
@ -859,10 +830,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
@Override
public boolean onEvaluateFullscreenMode() {
DisplayMetrics dm = getResources().getDisplayMetrics();
final Resources res = mResources;
DisplayMetrics dm = res.getDisplayMetrics();
float displayHeight = dm.heightPixels;
// If the display is more than X inches high, don't go to fullscreen mode
float dimen = getResources().getDimension(R.dimen.max_height_for_fullscreen);
float dimen = res.getDimension(R.dimen.max_height_for_fullscreen);
if (displayHeight > dimen) {
return false;
} else {
@ -1874,7 +1846,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} else if (Settings.PREF_RECORRECTION_ENABLED.equals(key)) {
mReCorrectionEnabled = sharedPreferences.getBoolean(
Settings.PREF_RECORRECTION_ENABLED,
getResources().getBoolean(R.bool.default_recorrection_enabled));
mResources.getBoolean(R.bool.default_recorrection_enabled));
}
}
@ -2006,11 +1978,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
private void updateSuggestionVisibility(SharedPreferences prefs) {
final Resources res = mResources;
final String suggestionVisiblityStr = prefs.getString(
Settings.PREF_SHOW_SUGGESTIONS_SETTING,
mResources.getString(R.string.prefs_suggestion_visibility_default_value));
res.getString(R.string.prefs_suggestion_visibility_default_value));
for (int visibility : SUGGESTION_VISIBILITY_VALUE_ARRAY) {
if (suggestionVisiblityStr.equals(mResources.getString(visibility))) {
if (suggestionVisiblityStr.equals(res.getString(visibility))) {
mSuggestionVisibility = visibility;
break;
}

View File

@ -103,7 +103,7 @@ public class Suggest implements Dictionary.WordCallback {
private int mCorrectionMode = CORRECTION_BASIC;
public Suggest(Context context, int[] dictionaryResId) {
public Suggest(Context context, int dictionaryResId) {
mMainDict = new BinaryDictionary(context, dictionaryResId, DIC_MAIN);
initPool();
}

View File

@ -12,13 +12,20 @@ LOCAL_SRC_FILES := \
#FLAG_DBG := true
ifneq ($(TARGET_ARCH),x86)
ifneq ($(FLAG_DBG), true)
LOCAL_NDK_VERSION := 4
TARGETING_UNBUNDLED_FROYO := true
ifeq ($(TARGET_ARCH), x86)
TARGETING_UNBUNDLED_FROYO := false
endif
LOCAL_SDK_VERSION := 8
endif #TARGET_ARCH = x86
ifeq ($(FLAG_DBG), true)
TARGETING_UNBUNDLED_FROYO := false
endif
ifeq ($(TARGETING_UNBUNDLED_FROYO), true)
LOCAL_NDK_VERSION := 4
LOCAL_SDK_VERSION := 8
endif
LOCAL_MODULE := libjni_latinime