[RF2] Some refactoring and some cleanup

Bug: 11281748
Change-Id: Iff45fdf6b23f77c08271a169c7d7ca45912c85e8
This commit is contained in:
Jean Chalard 2013-12-05 15:14:29 +09:00
parent f6b479d482
commit b8481456f2
5 changed files with 28 additions and 36 deletions

View file

@ -27,15 +27,13 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
// TODO: Quit extending Dictionary after implementing dynamic binary dictionary. abstract public class AbstractDictionaryWriter {
abstract public class AbstractDictionaryWriter extends Dictionary {
/** Used for Log actions from this class */ /** Used for Log actions from this class */
private static final String TAG = AbstractDictionaryWriter.class.getSimpleName(); private static final String TAG = AbstractDictionaryWriter.class.getSimpleName();
private final Context mContext; private final Context mContext;
public AbstractDictionaryWriter(final Context context, final String dictType) { public AbstractDictionaryWriter(final Context context) {
super(dictType);
mContext = context; mContext = context;
} }

View file

@ -43,8 +43,8 @@ public class DictionaryWriter extends AbstractDictionaryWriter {
private FusionDictionary mFusionDictionary; private FusionDictionary mFusionDictionary;
public DictionaryWriter(final Context context, final String dictType) { public DictionaryWriter(final Context context) {
super(context, dictType); super(context);
clear(); clear();
} }
@ -92,18 +92,4 @@ public class DictionaryWriter extends AbstractDictionaryWriter {
} }
dictEncoder.writeDictionary(mFusionDictionary, FORMAT_OPTIONS); dictEncoder.writeDictionary(mFusionDictionary, FORMAT_OPTIONS);
} }
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
final String prevWord, final ProximityInfo proximityInfo,
boolean blockOffensiveWords, final int[] additionalFeaturesOptions) {
// This class doesn't support suggestion.
return null;
}
@Override
public boolean isValidWord(String word) {
// This class doesn't support dictionary retrieval.
return false;
}
} }

View file

@ -28,7 +28,6 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.utils.AsyncResultHolder; import com.android.inputmethod.latin.utils.AsyncResultHolder;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.PrioritizedSerialExecutor; import com.android.inputmethod.latin.utils.PrioritizedSerialExecutor;
import com.android.inputmethod.latin.utils.StringUtils;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -66,9 +65,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
private static final int DICTIONARY_FORMAT_VERSION = 4; private static final int DICTIONARY_FORMAT_VERSION = 4;
private static final String SUPPORTS_DYNAMIC_UPDATE =
FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE;
/** /**
* A static map of update controllers, each of which records the time of accesses to a single * A static map of update controllers, each of which records the time of accesses to a single
* binary dictionary file and tracks whether the file is regenerating. The key for this map is * binary dictionary file and tracks whether the file is regenerating. The key for this map is
@ -135,11 +131,18 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
*/ */
protected abstract boolean hasContentChanged(); protected abstract boolean hasContentChanged();
protected boolean isValidBinaryDictFormatVersion(final int formatVersion) { protected boolean matchesExpectedBinaryDictFormatVersionForThisType(final int formatVersion) {
// TODO: Use ver4 format. // 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; return formatVersion == 2;
} }
public boolean hasValidContents() {
return mBinaryDictionary.hasValidContents();
}
protected String getFileNameExtentionToOpenDict() { protected String getFileNameExtentionToOpenDict() {
return ""; return "";
} }
@ -174,11 +177,11 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
private static AbstractDictionaryWriter getDictionaryWriter(final Context context, private static AbstractDictionaryWriter getDictionaryWriter(final Context context,
final String dictType, final boolean isDynamicPersonalizationDictionary) { final boolean isDynamicPersonalizationDictionary) {
if (isDynamicPersonalizationDictionary) { if (isDynamicPersonalizationDictionary) {
return null; return null;
} else { } else {
return new DictionaryWriter(context, dictType); return new DictionaryWriter(context);
} }
} }
@ -203,7 +206,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mBinaryDictionary = null; mBinaryDictionary = null;
mFilenameDictionaryUpdateController = getDictionaryUpdateController(filename); mFilenameDictionaryUpdateController = getDictionaryUpdateController(filename);
// Currently, only dynamic personalization dictionary is updatable. // 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) { protected static String getFilenameWithLocale(final String name, final Locale locale) {
@ -222,9 +225,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mBinaryDictionary.close(); mBinaryDictionary.close();
mBinaryDictionary = null; mBinaryDictionary = null;
} }
if (mDictionaryWriter != null) {
mDictionaryWriter.close();
}
} }
}); });
} }
@ -564,7 +564,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mDictionaryWriter.write(mFilename, getHeaderAttributeMap()); mDictionaryWriter.write(mFilename, getHeaderAttributeMap());
} else { } else {
if (mBinaryDictionary == null || !mBinaryDictionary.isValidDictionary() if (mBinaryDictionary == null || !mBinaryDictionary.isValidDictionary()
|| !isValidBinaryDictFormatVersion(mBinaryDictionary.getFormatVersion())) { || !hasValidContents()
// TODO: remove the check below
|| !matchesExpectedBinaryDictFormatVersionForThisType(
mBinaryDictionary.getFormatVersion())) {
final File file = new File(mContext.getFilesDir(), mFilename); final File file = new File(mContext.getFilesDir(), mFilename);
file.delete(); file.delete();
BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(), BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
@ -663,7 +666,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
loadBinaryDictionary(); loadBinaryDictionary();
} }
if (mBinaryDictionary != null && !(mBinaryDictionary.isValidDictionary() if (mBinaryDictionary != null && !(mBinaryDictionary.isValidDictionary()
&& isValidBinaryDictFormatVersion( && hasValidContents()
// TODO: remove the check below
&& matchesExpectedBinaryDictFormatVersionForThisType(
mBinaryDictionary.getFormatVersion()))) { mBinaryDictionary.getFormatVersion()))) {
// Binary dictionary or its format version is not valid. Regenerate the // Binary dictionary or its format version is not valid. Regenerate the
// dictionary file. // dictionary file.

View file

@ -114,8 +114,10 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
} }
@Override @Override
protected boolean isValidBinaryDictFormatVersion(final int formatVersion) { protected boolean matchesExpectedBinaryDictFormatVersionForThisType(final int formatVersion) {
return formatVersion >= REQUIRED_BINARY_DICTIONARY_VERSION; // This class is using format 4 because it's used by all version 4 dictionaries.
// TODO: remove this when all dynamically generated dicts use version 4.
return formatVersion == REQUIRED_BINARY_DICTIONARY_VERSION;
} }
@Override @Override

View file

@ -49,6 +49,7 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndKeyboard> {
final static ArrayList<SuggestedWordInfo> noSuggestions = CollectionUtils.newArrayList(); final static ArrayList<SuggestedWordInfo> noSuggestions = CollectionUtils.newArrayList();
private final static DictAndKeyboard dummyDict = new DictAndKeyboard( private final static DictAndKeyboard dummyDict = new DictAndKeyboard(
new Dictionary(Dictionary.TYPE_MAIN) { new Dictionary(Dictionary.TYPE_MAIN) {
// TODO: this dummy dictionary should be a singleton in the Dictionary class.
@Override @Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
final String prevWord, final ProximityInfo proximityInfo, final String prevWord, final ProximityInfo proximityInfo,