2011-03-14 18:46:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2011-03-14 18:46:15 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-14 18:46:15 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2011-03-14 18:46:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2011-08-11 13:10:16 +00:00
|
|
|
import android.content.SharedPreferences;
|
2011-03-14 18:46:15 +00:00
|
|
|
import android.content.res.AssetFileDescriptor;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-09-12 08:46:09 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.DictDecoder;
|
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec;
|
2013-08-20 04:15:27 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader;
|
|
|
|
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.DictionaryInfoUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.LocaleUtils;
|
|
|
|
|
2011-08-11 07:44:36 +00:00
|
|
|
import java.io.File;
|
2012-08-21 09:30:18 +00:00
|
|
|
import java.io.IOException;
|
2012-09-18 05:06:12 +00:00
|
|
|
import java.nio.BufferUnderflowException;
|
2011-08-11 07:46:43 +00:00
|
|
|
import java.util.ArrayList;
|
2012-04-20 04:20:45 +00:00
|
|
|
import java.util.HashMap;
|
2011-03-14 18:46:15 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class to get the address of a mmap'able dictionary file.
|
|
|
|
*/
|
2013-06-23 16:11:32 +00:00
|
|
|
final public class BinaryDictionaryGetter {
|
2011-03-14 18:46:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for Log actions from this class
|
|
|
|
*/
|
|
|
|
private static final String TAG = BinaryDictionaryGetter.class.getSimpleName();
|
|
|
|
|
2011-08-18 06:44:53 +00:00
|
|
|
/**
|
|
|
|
* Used to return empty lists
|
|
|
|
*/
|
|
|
|
private static final File[] EMPTY_FILE_ARRAY = new File[0];
|
|
|
|
|
2011-08-11 13:10:16 +00:00
|
|
|
/**
|
|
|
|
* Name of the common preferences name to know which word list are on and which are off.
|
|
|
|
*/
|
|
|
|
private static final String COMMON_PREFERENCES_NAME = "LatinImeDictPrefs";
|
|
|
|
|
2012-04-20 04:20:45 +00:00
|
|
|
// Name of the category for the main dictionary
|
2013-01-18 10:21:14 +00:00
|
|
|
public static final String MAIN_DICTIONARY_CATEGORY = "main";
|
2012-04-20 04:20:45 +00:00
|
|
|
public static final String ID_CATEGORY_SEPARATOR = ":";
|
|
|
|
|
2012-08-17 07:19:03 +00:00
|
|
|
// The key considered to read the version attribute in a dictionary file.
|
|
|
|
private static String VERSION_KEY = "version";
|
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
// Prevents this from being instantiated
|
|
|
|
private BinaryDictionaryGetter() {}
|
|
|
|
|
2012-10-11 07:08:25 +00:00
|
|
|
/**
|
|
|
|
* Generates a unique temporary file name in the app cache directory.
|
|
|
|
*/
|
2013-02-23 09:41:44 +00:00
|
|
|
public static String getTempFileName(final String id, final Context context)
|
|
|
|
throws IOException {
|
|
|
|
final String safeId = DictionaryInfoUtils.replaceFileNameDangerousCharacters(id);
|
2013-04-25 09:14:40 +00:00
|
|
|
final File directory = new File(DictionaryInfoUtils.getWordListTempDirectory(context));
|
|
|
|
if (!directory.exists()) {
|
|
|
|
if (!directory.mkdirs()) {
|
|
|
|
Log.e(TAG, "Could not create the temporary directory");
|
|
|
|
}
|
|
|
|
}
|
2013-02-23 09:41:44 +00:00
|
|
|
// If the first argument is less than three chars, createTempFile throws a
|
|
|
|
// RuntimeException. We don't really care about what name we get, so just
|
|
|
|
// put a three-chars prefix makes us safe.
|
2013-04-25 09:14:40 +00:00
|
|
|
return File.createTempFile("xxx" + safeId, null, directory).getAbsolutePath();
|
2012-10-11 07:08:25 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a file address from a resource, or null if it cannot be opened.
|
|
|
|
*/
|
2013-01-29 07:34:14 +00:00
|
|
|
public static AssetFileAddress loadFallbackResource(final Context context,
|
2012-04-11 05:58:02 +00:00
|
|
|
final int fallbackResId) {
|
|
|
|
final AssetFileDescriptor afd = context.getResources().openRawResourceFd(fallbackResId);
|
2011-03-14 18:46:15 +00:00
|
|
|
if (afd == null) {
|
|
|
|
Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId="
|
|
|
|
+ fallbackResId);
|
|
|
|
return null;
|
|
|
|
}
|
2013-04-29 19:26:33 +00:00
|
|
|
try {
|
|
|
|
return AssetFileAddress.makeFromFileNameAndOffset(
|
|
|
|
context.getApplicationInfo().sourceDir, afd.getStartOffset(), afd.getLength());
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
afd.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// Ignored
|
|
|
|
}
|
|
|
|
}
|
2011-03-14 18:46:15 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
private static final class DictPackSettings {
|
2011-08-16 12:41:12 +00:00
|
|
|
final SharedPreferences mDictPreferences;
|
|
|
|
public DictPackSettings(final Context context) {
|
2013-03-15 10:00:51 +00:00
|
|
|
mDictPreferences = null == context ? null
|
|
|
|
: context.getSharedPreferences(COMMON_PREFERENCES_NAME,
|
2014-01-08 07:52:04 +00:00
|
|
|
Context.MODE_MULTI_PROCESS);
|
2011-08-16 12:41:12 +00:00
|
|
|
}
|
|
|
|
public boolean isWordListActive(final String dictId) {
|
|
|
|
if (null == mDictPreferences) {
|
|
|
|
// If we don't have preferences it basically means we can't find the dictionary
|
|
|
|
// pack - either it's not installed, or it's disabled, or there is some strange
|
|
|
|
// bug. Either way, a word list with no settings should be on by default: default
|
|
|
|
// dictionaries in LatinIME are on if there is no settings at all, and if for some
|
|
|
|
// reason some dictionaries have been installed BUT the dictionary pack can't be
|
|
|
|
// found anymore it's safer to actually supply installed dictionaries.
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// The default is true here for the same reasons as above. We got the dictionary
|
|
|
|
// pack but if we don't have any settings for it it means the user has never been
|
|
|
|
// to the settings yet. So by default, the main dictionaries should be on.
|
|
|
|
return mDictPreferences.getBoolean(dictId, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-20 04:20:45 +00:00
|
|
|
/**
|
|
|
|
* Utility class for the {@link #getCachedWordLists} method
|
|
|
|
*/
|
2012-09-27 09:16:16 +00:00
|
|
|
private static final class FileAndMatchLevel {
|
2012-04-20 04:20:45 +00:00
|
|
|
final File mFile;
|
|
|
|
final int mMatchLevel;
|
|
|
|
public FileAndMatchLevel(final File file, final int matchLevel) {
|
|
|
|
mFile = file;
|
|
|
|
mMatchLevel = matchLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of cached files for a specific locale, one for each category.
|
|
|
|
*
|
|
|
|
* This will return exactly one file for each word list category that matches
|
|
|
|
* the passed locale. If several files match the locale for any given category,
|
|
|
|
* this returns the file with the closest match to the locale. For example, if
|
|
|
|
* the passed word list is en_US, and for a category we have an en and an en_US
|
|
|
|
* word list available, we'll return only the en_US one.
|
|
|
|
* Thus, the list will contain as many files as there are categories.
|
2011-08-11 07:46:43 +00:00
|
|
|
*
|
2011-08-25 09:04:21 +00:00
|
|
|
* @param locale the locale to find the dictionary files for, as a string.
|
2011-08-11 07:46:43 +00:00
|
|
|
* @param context the context on which to open the files upon.
|
2011-08-18 06:44:53 +00:00
|
|
|
* @return an array of binary dictionary files, which may be empty but may not be null.
|
2011-08-11 07:46:43 +00:00
|
|
|
*/
|
2013-01-29 07:34:14 +00:00
|
|
|
public static File[] getCachedWordLists(final String locale, final Context context) {
|
2013-01-29 09:05:33 +00:00
|
|
|
final File[] directoryList = DictionaryInfoUtils.getCachedDirectoryList(context);
|
2011-08-25 09:04:21 +00:00
|
|
|
if (null == directoryList) return EMPTY_FILE_ARRAY;
|
2012-08-21 07:34:55 +00:00
|
|
|
final HashMap<String, FileAndMatchLevel> cacheFiles = CollectionUtils.newHashMap();
|
2011-08-25 09:04:21 +00:00
|
|
|
for (File directory : directoryList) {
|
|
|
|
if (!directory.isDirectory()) continue;
|
2013-01-29 09:05:33 +00:00
|
|
|
final String dirLocale =
|
|
|
|
DictionaryInfoUtils.getWordListIdFromFileName(directory.getName());
|
2012-04-20 04:20:45 +00:00
|
|
|
final int matchLevel = LocaleUtils.getMatchLevel(dirLocale, locale);
|
|
|
|
if (LocaleUtils.isMatch(matchLevel)) {
|
2011-08-25 09:04:21 +00:00
|
|
|
final File[] wordLists = directory.listFiles();
|
|
|
|
if (null != wordLists) {
|
|
|
|
for (File wordList : wordLists) {
|
2013-01-29 09:05:33 +00:00
|
|
|
final String category =
|
|
|
|
DictionaryInfoUtils.getCategoryFromFileName(wordList.getName());
|
2012-04-20 04:20:45 +00:00
|
|
|
final FileAndMatchLevel currentBestMatch = cacheFiles.get(category);
|
|
|
|
if (null == currentBestMatch || currentBestMatch.mMatchLevel < matchLevel) {
|
|
|
|
cacheFiles.put(category, new FileAndMatchLevel(wordList, matchLevel));
|
|
|
|
}
|
2011-08-25 09:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cacheFiles.isEmpty()) return EMPTY_FILE_ARRAY;
|
2012-04-20 04:20:45 +00:00
|
|
|
final File[] result = new File[cacheFiles.size()];
|
|
|
|
int index = 0;
|
|
|
|
for (final FileAndMatchLevel entry : cacheFiles.values()) {
|
|
|
|
result[index++] = entry.mFile;
|
|
|
|
}
|
|
|
|
return result;
|
2011-08-11 07:46:43 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 05:41:50 +00:00
|
|
|
/**
|
|
|
|
* Remove all files with the passed id, except the passed file.
|
|
|
|
*
|
|
|
|
* If a dictionary with a given ID has a metadata change that causes it to change
|
|
|
|
* path, we need to remove the old version. The only way to do this is to check all
|
|
|
|
* installed files for a matching ID in a different directory.
|
|
|
|
*/
|
|
|
|
public static void removeFilesWithIdExcept(final Context context, final String id,
|
|
|
|
final File fileToKeep) {
|
|
|
|
try {
|
|
|
|
final File canonicalFileToKeep = fileToKeep.getCanonicalFile();
|
2013-01-29 09:05:33 +00:00
|
|
|
final File[] directoryList = DictionaryInfoUtils.getCachedDirectoryList(context);
|
2012-05-23 05:41:50 +00:00
|
|
|
if (null == directoryList) return;
|
|
|
|
for (File directory : directoryList) {
|
|
|
|
// There is one directory per locale. See #getCachedDirectoryList
|
|
|
|
if (!directory.isDirectory()) continue;
|
|
|
|
final File[] wordLists = directory.listFiles();
|
|
|
|
if (null == wordLists) continue;
|
|
|
|
for (File wordList : wordLists) {
|
2013-01-29 09:05:33 +00:00
|
|
|
final String fileId =
|
|
|
|
DictionaryInfoUtils.getWordListIdFromFileName(wordList.getName());
|
2012-05-23 05:41:50 +00:00
|
|
|
if (fileId.equals(id)) {
|
|
|
|
if (!canonicalFileToKeep.equals(wordList.getCanonicalFile())) {
|
|
|
|
wordList.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (java.io.IOException e) {
|
2013-02-12 05:14:56 +00:00
|
|
|
Log.e(TAG, "IOException trying to cleanup files", e);
|
2012-05-23 05:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 06:25:33 +00:00
|
|
|
// ## HACK ## we prevent usage of a dictionary before version 18. The reason for this is, since
|
|
|
|
// those do not include whitelist entries, the new code with an old version of the dictionary
|
|
|
|
// would lose whitelist functionality.
|
2012-08-17 07:19:03 +00:00
|
|
|
private static boolean hackCanUseDictionaryFile(final Locale locale, final File f) {
|
|
|
|
try {
|
|
|
|
// Read the version of the file
|
2013-09-12 08:46:09 +00:00
|
|
|
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(f);
|
2013-08-20 04:15:27 +00:00
|
|
|
final FileHeader header = dictDecoder.readHeader();
|
2012-08-21 09:30:18 +00:00
|
|
|
|
2013-08-20 04:15:27 +00:00
|
|
|
final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY);
|
2012-08-17 07:19:03 +00:00
|
|
|
if (null == version) {
|
|
|
|
// No version in the options : the format is unexpected
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Version 18 is the first one to include the whitelist
|
|
|
|
// Obviously this is a big ## HACK ##
|
|
|
|
return Integer.parseInt(version) >= 18;
|
|
|
|
} catch (java.io.FileNotFoundException e) {
|
|
|
|
return false;
|
|
|
|
} catch (java.io.IOException e) {
|
|
|
|
return false;
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return false;
|
2012-09-18 05:06:12 +00:00
|
|
|
} catch (BufferUnderflowException e) {
|
|
|
|
return false;
|
2013-08-20 04:15:27 +00:00
|
|
|
} catch (UnsupportedFormatException e) {
|
|
|
|
return false;
|
2012-08-17 07:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
/**
|
2011-04-27 14:13:11 +00:00
|
|
|
* Returns a list of file addresses for a given locale, trying relevant methods in order.
|
2011-03-14 18:46:15 +00:00
|
|
|
*
|
2011-04-27 14:13:11 +00:00
|
|
|
* Tries to get binary dictionaries from various sources, in order:
|
|
|
|
* - Uses a content provider to get a public dictionary set, as per the protocol described
|
2011-03-14 18:46:15 +00:00
|
|
|
* in BinaryDictionaryFileDumper.
|
|
|
|
* If that fails:
|
2012-04-11 12:02:26 +00:00
|
|
|
* - Gets a file name from the built-in dictionary for this locale, if any.
|
2011-03-14 18:46:15 +00:00
|
|
|
* If that fails:
|
|
|
|
* - Returns null.
|
2012-03-16 15:50:51 +00:00
|
|
|
* @return The list of addresses of valid dictionary files, or null.
|
2011-03-14 18:46:15 +00:00
|
|
|
*/
|
2012-03-16 15:50:51 +00:00
|
|
|
public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale,
|
2012-04-11 12:02:26 +00:00
|
|
|
final Context context) {
|
2011-08-16 12:35:52 +00:00
|
|
|
|
2012-04-11 12:29:53 +00:00
|
|
|
final boolean hasDefaultWordList = DictionaryFactory.isDictionaryAvailable(context, locale);
|
2013-06-28 03:48:41 +00:00
|
|
|
BinaryDictionaryFileDumper.cacheWordListsFromContentProvider(locale, context,
|
|
|
|
hasDefaultWordList);
|
2011-08-25 09:04:21 +00:00
|
|
|
final File[] cachedWordLists = getCachedWordLists(locale.toString(), context);
|
2013-01-29 09:05:33 +00:00
|
|
|
final String mainDictId = DictionaryInfoUtils.getMainDictId(locale);
|
2011-08-18 06:44:53 +00:00
|
|
|
final DictPackSettings dictPackSettings = new DictPackSettings(context);
|
|
|
|
|
2011-08-18 11:09:35 +00:00
|
|
|
boolean foundMainDict = false;
|
2012-08-21 07:34:55 +00:00
|
|
|
final ArrayList<AssetFileAddress> fileList = CollectionUtils.newArrayList();
|
2011-08-24 03:45:52 +00:00
|
|
|
// cachedWordLists may not be null, see doc for getCachedDictionaryList
|
|
|
|
for (final File f : cachedWordLists) {
|
2013-01-29 09:05:33 +00:00
|
|
|
final String wordListId = DictionaryInfoUtils.getWordListIdFromFileName(f.getName());
|
2012-08-17 07:19:03 +00:00
|
|
|
final boolean canUse = f.canRead() && hackCanUseDictionaryFile(locale, f);
|
2013-01-29 09:05:33 +00:00
|
|
|
if (canUse && DictionaryInfoUtils.isMainWordListId(wordListId)) {
|
2011-08-18 11:09:35 +00:00
|
|
|
foundMainDict = true;
|
|
|
|
}
|
2011-08-18 06:44:53 +00:00
|
|
|
if (!dictPackSettings.isWordListActive(wordListId)) continue;
|
2012-08-17 07:19:03 +00:00
|
|
|
if (canUse) {
|
2013-09-18 12:39:30 +00:00
|
|
|
final AssetFileAddress afa = AssetFileAddress.makeFromFileName(f.getPath());
|
|
|
|
if (null != afa) fileList.add(afa);
|
2011-08-18 06:44:53 +00:00
|
|
|
} else {
|
2012-08-17 07:19:03 +00:00
|
|
|
Log.e(TAG, "Found a cached dictionary file but cannot read or use it");
|
2011-08-18 06:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-18 11:09:35 +00:00
|
|
|
if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
|
2012-04-11 12:02:26 +00:00
|
|
|
final int fallbackResId =
|
2013-01-29 09:05:33 +00:00
|
|
|
DictionaryInfoUtils.getMainDictionaryResourceId(context.getResources(), locale);
|
2012-04-11 05:58:02 +00:00
|
|
|
final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
|
2011-08-18 11:09:35 +00:00
|
|
|
if (null != fallbackAsset) {
|
|
|
|
fileList.add(fallbackAsset);
|
|
|
|
}
|
2011-03-14 18:46:15 +00:00
|
|
|
}
|
2011-08-16 12:35:52 +00:00
|
|
|
|
2011-08-18 11:09:35 +00:00
|
|
|
return fileList;
|
2011-03-14 18:46:15 +00:00
|
|
|
}
|
|
|
|
}
|