diff options
author | 2011-01-07 15:01:51 +0900 | |
---|---|---|
committer | 2011-01-07 19:51:45 +0900 | |
commit | e90b333017c68e888a5e3d351f07ea29036457d0 (patch) | |
tree | dd51657376f1c571df581b7016be891d09246be5 /java/src | |
parent | f16028b92e15c0fdf3fdc364d7888cf024723b00 (diff) | |
download | latinime-e90b333017c68e888a5e3d351f07ea29036457d0.tar.gz latinime-e90b333017c68e888a5e3d351f07ea29036457d0.tar.xz latinime-e90b333017c68e888a5e3d351f07ea29036457d0.zip |
Load main dic in native
Follow up to Id57dce51
bug: 3219819
Change-Id: I00e11ef21d0252ffa88c12dffb9c55b0f2e19a66
Diffstat (limited to 'java/src')
3 files changed, 54 insertions, 96 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 4bb64ee90..9f934c6ef 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -1,12 +1,12 @@ /* * Copyright (C) 2008 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 @@ -17,13 +17,9 @@ package com.android.inputmethod.latin; import android.content.Context; +import android.content.res.AssetFileDescriptor; import android.util.Log; -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.channels.Channels; import java.util.Arrays; /** @@ -48,15 +44,12 @@ public class BinaryDictionary extends Dictionary { private int mDicTypeId; private int mNativeDict; - private int mDictLength; + private long mDictLength; private final int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_ALTERNATIVES]; private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS]; private final char[] mOutputChars_bigrams = new char[MAX_WORD_LENGTH * MAX_BIGRAMS]; private final int[] mFrequencies = new int[MAX_WORDS]; private final int[] mFrequencies_bigrams = new int[MAX_BIGRAMS]; - // Keep a reference to the native dict direct buffer in Java to avoid - // unexpected deallocation of the direct buffer. - private ByteBuffer mNativeDictDirectBuffer; static { try { @@ -78,30 +71,9 @@ public class BinaryDictionary extends Dictionary { mDicTypeId = dicTypeId; } - /** - * Create a dictionary from a byte buffer. This is used for testing. - * @param context application context for reading resources - * @param byteBuffer a ByteBuffer containing the binary dictionary - */ - public BinaryDictionary(Context context, ByteBuffer byteBuffer, int dicTypeId) { - if (byteBuffer != null) { - if (byteBuffer.isDirect()) { - mNativeDictDirectBuffer = byteBuffer; - } else { - mNativeDictDirectBuffer = ByteBuffer.allocateDirect(byteBuffer.capacity()); - byteBuffer.rewind(); - mNativeDictDirectBuffer.put(byteBuffer); - } - mDictLength = byteBuffer.capacity(); - mNativeDict = openNative(mNativeDictDirectBuffer, - TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER, - MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES); - } - mDicTypeId = dicTypeId; - } - - private native int openNative(ByteBuffer bb, int typedLetterMultiplier, - int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives); + private native int openNative(String apkFileName, long dictOffset, long dictSize, + int typedLetterMultiplier, 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, @@ -112,37 +84,17 @@ public class BinaryDictionary extends Dictionary { int maxWordLength, int maxBigrams, int maxAlternatives); private final void loadDictionary(Context context, int resId) { - InputStream is = null; - try { - is = context.getResources().openRawResource(resId); - final int total = is.available(); - mNativeDictDirectBuffer = - ByteBuffer.allocateDirect(total).order(ByteOrder.nativeOrder()); - final int got = Channels.newChannel(is).read(mNativeDictDirectBuffer); - if (got != total) { - Log.e(TAG, "Read " + got + " bytes, expected " + total); - } else { - mNativeDict = openNative(mNativeDictDirectBuffer, - TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER, - MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES); - mDictLength = total; - } - } catch (IOException e) { - Log.w(TAG, "No available memory for binary dictionary"); - } finally { - try { - if (is != null) is.close(); - } catch (IOException e) { - Log.w(TAG, "Failed to close input stream"); - } - } + final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId); + mNativeDict = openNative(context.getApplicationInfo().sourceDir, + afd.getStartOffset(), afd.getLength(), + TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER, + MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES); + mDictLength = afd.getLength(); } - @Override public void getBigrams(final WordComposer codes, final CharSequence previousWord, final WordCallback callback, int[] nextLettersFrequencies) { - char[] chars = previousWord.toString().toCharArray(); Arrays.fill(mOutputChars_bigrams, (char) 0); Arrays.fill(mFrequencies_bigrams, 0); @@ -212,7 +164,7 @@ public class BinaryDictionary extends Dictionary { return isValidWordNative(mNativeDict, chars, chars.length); } - public int getSize() { + public long getSize() { return mDictLength; // This value is initialized on the call to openNative() } @@ -221,6 +173,7 @@ public class BinaryDictionary extends Dictionary { if (mNativeDict != 0) { closeNative(mNativeDict); mNativeDict = 0; + mDictLength = 0; } } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index be98f4c3e..ad783cac0 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -371,23 +371,19 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen int mainDicResId = getMainDictionaryResourceId(res); mSuggest = new Suggest(this, mainDicResId); loadAndSetAutoCorrectionThreshold(prefs); - if (mUserDictionary != null) mUserDictionary.close(); + mUserDictionary = new UserDictionary(this, locale); - if (mContactsDictionary == null) { - mContactsDictionary = new ContactsDictionary(this, Suggest.DIC_CONTACTS); - } - if (mAutoDictionary != null) { - mAutoDictionary.close(); - } - mAutoDictionary = new AutoDictionary(this, this, locale, Suggest.DIC_AUTO); - if (mUserBigramDictionary != null) { - mUserBigramDictionary.close(); - } - mUserBigramDictionary = new UserBigramDictionary(this, this, locale, Suggest.DIC_USER); - mSuggest.setUserBigramDictionary(mUserBigramDictionary); mSuggest.setUserDictionary(mUserDictionary); + + mContactsDictionary = new ContactsDictionary(this, Suggest.DIC_CONTACTS); mSuggest.setContactsDictionary(mContactsDictionary); + + mAutoDictionary = new AutoDictionary(this, this, locale, Suggest.DIC_AUTO); mSuggest.setAutoDictionary(mAutoDictionary); + + mUserBigramDictionary = new UserBigramDictionary(this, this, locale, Suggest.DIC_USER); + mSuggest.setUserBigramDictionary(mUserBigramDictionary); + updateCorrectionMode(); mWordSeparators = res.getString(R.string.word_separators); mSentenceSeparators = res.getString(R.string.sentence_separators); @@ -397,11 +393,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @Override public void onDestroy() { - if (mUserDictionary != null) { - mUserDictionary.close(); - } - if (mContactsDictionary != null) { - mContactsDictionary.close(); + if (mSuggest != null) { + mSuggest.close(); + mSuggest = null; } unregisterReceiver(mReceiver); mVoiceConnector.destroy(); diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 9f979fffd..9ea9c2f3e 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -1,12 +1,12 @@ /* * Copyright (C) 2008 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 @@ -22,12 +22,11 @@ import android.text.TextUtils; import android.util.Log; import android.view.View; -import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; /** - * This class loads a dictionary and provides a list of suggestions for a given sequence of + * This class loads a dictionary and provides a list of suggestions for a given sequence of * characters. This includes corrections and completions. */ public class Suggest implements Dictionary.WordCallback { @@ -108,11 +107,6 @@ public class Suggest implements Dictionary.WordCallback { initPool(); } - public Suggest(Context context, ByteBuffer byteBuffer) { - mMainDict = new BinaryDictionary(context, byteBuffer, DIC_MAIN); - initPool(); - } - private void initPool() { for (int i = 0; i < mPrefMaxSuggestions; i++) { StringBuilder sb = new StringBuilder(getApproxMaxWordLength()); @@ -154,7 +148,7 @@ public class Suggest implements Dictionary.WordCallback { public void setContactsDictionary(Dictionary userDictionary) { mContactsDictionary = userDictionary; } - + public void setAutoDictionary(Dictionary autoDictionary) { mAutoDictionary = autoDictionary; } @@ -232,7 +226,7 @@ public class Suggest implements Dictionary.WordCallback { if (!TextUtils.isEmpty(prevWordForBigram)) { CharSequence lowerPrevWord = prevWordForBigram.toString().toLowerCase(); - if (mMainDict.isValidWord(lowerPrevWord)) { + if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) { prevWordForBigram = lowerPrevWord; } if (mUserBigramDictionary != null) { @@ -383,7 +377,7 @@ public class Suggest implements Dictionary.WordCallback { return mHaveCorrection; } - private boolean compareCaseInsensitive(final String mLowerOriginalWord, + private boolean compareCaseInsensitive(final String mLowerOriginalWord, final char[] word, final int offset, final int length) { final int originalLength = mLowerOriginalWord.length(); if (originalLength == length && Character.isUpperCase(word[offset])) { @@ -456,7 +450,7 @@ public class Suggest implements Dictionary.WordCallback { System.arraycopy(priorities, pos, priorities, pos + 1, prefMaxSuggestions - pos - 1); priorities[pos] = freq; int poolSize = mStringPool.size(); - StringBuilder sb = poolSize > 0 ? (StringBuilder) mStringPool.remove(poolSize - 1) + StringBuilder sb = poolSize > 0 ? (StringBuilder) mStringPool.remove(poolSize - 1) : new StringBuilder(getApproxMaxWordLength()); sb.setLength(0); if (mIsAllUpperCase) { @@ -510,7 +504,7 @@ public class Suggest implements Dictionary.WordCallback { || (mAutoDictionary != null && mAutoDictionary.isValidWord(word)) || (mContactsDictionary != null && mContactsDictionary.isValidWord(word)); } - + private void collectGarbage(ArrayList<CharSequence> suggestions, int prefMaxSuggestions) { int poolSize = mStringPool.size(); int garbageSize = suggestions.size(); @@ -531,6 +525,23 @@ public class Suggest implements Dictionary.WordCallback { public void close() { if (mMainDict != null) { mMainDict.close(); + mMainDict = null; + } + if (mUserDictionary != null) { + mUserDictionary.close(); + mUserDictionary = null; + } + if (mUserBigramDictionary != null) { + mUserBigramDictionary.close(); + mUserBigramDictionary = null; + } + if (mContactsDictionary != null) { + mContactsDictionary.close(); + mContactsDictionary = null; + } + if (mAutoDictionary != null) { + mAutoDictionary.close(); + mAutoDictionary = null; } } } |