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