Introduce DictionaryHeaderUtils to get dictionary header values

Change-Id: I86cef6c7f2f68a75428bb2b9c4d5a1dc1876b541
This commit is contained in:
Mohammadinamul Sheik 2015-03-03 16:17:27 -08:00
parent 2281ba7666
commit 6e29d261cb
2 changed files with 42 additions and 16 deletions

View File

@ -0,0 +1,31 @@
/*
* Copyright (C) 2015 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
*/
package com.android.inputmethod.latin.utils;
import com.android.inputmethod.latin.AssetFileAddress;
import com.android.inputmethod.latin.makedict.DictionaryHeader;
import java.io.File;
public class DictionaryHeaderUtils {
public static int getContentVersion(AssetFileAddress fileAddress) {
final DictionaryHeader header = DictionaryInfoUtils.getDictionaryFileHeaderOrNull(
new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength);
return Integer.parseInt(header.mVersionString);
}
}

View File

@ -340,7 +340,7 @@ public class DictionaryInfoUtils {
return getDictionaryFileHeaderOrNull(file, 0, file.length());
}
private static DictionaryHeader getDictionaryFileHeaderOrNull(final File file,
public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file,
final long offset, final long length) {
try {
final DictionaryHeader header =
@ -357,23 +357,17 @@ public class DictionaryInfoUtils {
* Returns information of the dictionary.
*
* @param fileAddress the asset dictionary file address.
* @param locale Locale for this file.
* @return information of the specified dictionary.
*/
@Nullable
private static DictionaryInfo createDictionaryInfoFromFileAddress(
final AssetFileAddress fileAddress) {
// TODO: Read the header and update the version number for the new dictionaries.
// This will make sure that the dictionary version is updated in the database.
final DictionaryHeader header = getDictionaryFileHeaderOrNull(
new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength);
if (header == null) {
return null;
}
final String id = header.mIdString;
final Locale locale = LocaleUtils.constructLocaleFromString(header.mLocaleString);
final String description = header.getDescription();
final String version = header.mVersionString;
return new DictionaryInfo(id, locale, description, fileAddress, Integer.parseInt(version));
final AssetFileAddress fileAddress, Locale locale) {
final String id = getMainDictId(locale);
final int version = DictionaryHeaderUtils.getContentVersion(fileAddress);
final String description = SubtypeLocaleUtils
.getSubtypeLocaleDisplayName(locale.toString());
return new DictionaryInfo(id, locale, description, fileAddress, version);
}
private static void addOrUpdateDictInfo(final ArrayList<DictionaryInfo> dictList,
@ -410,7 +404,7 @@ public class DictionaryInfoUtils {
final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
final AssetFileAddress fileAddress = AssetFileAddress.makeFromFile(dict);
final DictionaryInfo dictionaryInfo =
createDictionaryInfoFromFileAddress(fileAddress);
createDictionaryInfoFromFileAddress(fileAddress, locale);
// Protect against cases of a less-specific dictionary being found, like an
// en dictionary being used for an en_US locale. In this case, the en dictionary
// should be used for en_US but discounted for listing purposes.
@ -435,7 +429,8 @@ public class DictionaryInfoUtils {
}
final AssetFileAddress fileAddress =
BinaryDictionaryGetter.loadFallbackResource(context, resourceId);
final DictionaryInfo dictionaryInfo = createDictionaryInfoFromFileAddress(fileAddress);
final DictionaryInfo dictionaryInfo = createDictionaryInfoFromFileAddress(fileAddress,
locale);
// Protect against cases of a less-specific dictionary being found, like an
// en dictionary being used for an en_US locale. In this case, the en dictionary
// should be used for en_US but discounted for listing purposes.