aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java')
-rw-r--r--java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java67
1 files changed, 41 insertions, 26 deletions
diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
index f86517d12..f53dc3748 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
@@ -27,8 +27,8 @@ import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.utils.AsyncResultHolder;
import com.android.inputmethod.latin.utils.CollectionUtils;
+import com.android.inputmethod.latin.utils.FileUtils;
import com.android.inputmethod.latin.utils.PrioritizedSerialExecutor;
-import com.android.inputmethod.latin.utils.StringUtils;
import java.io.File;
import java.util.ArrayList;
@@ -64,10 +64,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
*/
protected static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
- private static final int DICTIONARY_FORMAT_VERSION = 4;
-
- private static final String SUPPORTS_DYNAMIC_UPDATE =
- FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE;
+ private static final int DICTIONARY_FORMAT_VERSION = FormatSpec.VERSION4;
/**
* A static map of update controllers, each of which records the time of accesses to a single
@@ -135,11 +132,18 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
*/
protected abstract boolean hasContentChanged();
- protected boolean isValidBinaryDictFormatVersion(final int formatVersion) {
- // TODO: Use ver4 format.
+ protected boolean matchesExpectedBinaryDictFormatVersionForThisType(final int formatVersion) {
+ // This class is using format 2 because it's used by the User and Contacts dictionary
+ // only, which right now use format 2 (dicts using format 4 use Decaying*, which overrides
+ // this method).
+ // TODO: Migrate these dicts to ver4 format, and remove this function.
return formatVersion == 2;
}
+ public boolean isValidDictionary() {
+ return mBinaryDictionary.isValidDictionary();
+ }
+
protected String getFileNameExtentionToOpenDict() {
return "";
}
@@ -174,11 +178,11 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
}
private static AbstractDictionaryWriter getDictionaryWriter(final Context context,
- final String dictType, final boolean isDynamicPersonalizationDictionary) {
+ final boolean isDynamicPersonalizationDictionary) {
if (isDynamicPersonalizationDictionary) {
return null;
} else {
- return new DictionaryWriter(context, dictType);
+ return new DictionaryWriter(context);
}
}
@@ -203,7 +207,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mBinaryDictionary = null;
mFilenameDictionaryUpdateController = getDictionaryUpdateController(filename);
// Currently, only dynamic personalization dictionary is updatable.
- mDictionaryWriter = getDictionaryWriter(context, dictType, isUpdatable);
+ mDictionaryWriter = getDictionaryWriter(context, isUpdatable);
}
protected static String getFilenameWithLocale(final String name, final Locale locale) {
@@ -222,9 +226,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mBinaryDictionary.close();
mBinaryDictionary = null;
}
- if (mDictionaryWriter != null) {
- mDictionaryWriter.close();
- }
}
});
}
@@ -563,10 +564,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
loadDictionaryAsync();
mDictionaryWriter.write(mFilename, getHeaderAttributeMap());
} else {
- if (mBinaryDictionary == null || !mBinaryDictionary.isValidDictionary()
- || !isValidBinaryDictFormatVersion(mBinaryDictionary.getFormatVersion())) {
+ if (mBinaryDictionary == null || !isValidDictionary()
+ // TODO: remove the check below
+ || !matchesExpectedBinaryDictFormatVersionForThisType(
+ mBinaryDictionary.getFormatVersion())) {
final File file = new File(mContext.getFilesDir(), mFilename);
- file.delete();
+ if (!FileUtils.deleteRecursively(file)) {
+ Log.e(TAG, "Can't remove a file: " + file.getName());
+ }
BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap());
} else {
@@ -662,16 +667,26 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
// load the shared dictionary.
loadBinaryDictionary();
}
- if (mBinaryDictionary != null && !(mBinaryDictionary.isValidDictionary()
- && isValidBinaryDictFormatVersion(
- mBinaryDictionary.getFormatVersion()))) {
- // Binary dictionary or its format version is not valid. Regenerate the
- // dictionary file.
- mFilenameDictionaryUpdateController.mLastUpdateTime = time;
- writeBinaryDictionary();
- loadBinaryDictionary();
- }
- mPerInstanceDictionaryUpdateController.mLastUpdateTime = time;
+ // If we just loaded the binary dictionary, then mBinaryDictionary is not
+ // up-to-date yet so it's useless to test it right away. Schedule the check
+ // for right after it's loaded instead.
+ getExecutor(mFilename).executePrioritized(new Runnable() {
+ @Override
+ public void run() {
+ if (mBinaryDictionary != null && !(isValidDictionary()
+ // TODO: remove the check below
+ && matchesExpectedBinaryDictFormatVersionForThisType(
+ mBinaryDictionary.getFormatVersion()))) {
+ // Binary dictionary or its format version is not valid. Regenerate
+ // the dictionary file. writeBinaryDictionary will remove the
+ // existing files if appropriate.
+ mFilenameDictionaryUpdateController.mLastUpdateTime = time;
+ writeBinaryDictionary();
+ loadBinaryDictionary();
+ }
+ mPerInstanceDictionaryUpdateController.mLastUpdateTime = time;
+ }
+ });
} finally {
mFilenameDictionaryUpdateController.mProcessingLargeTask.set(false);
}