2011-03-14 18:46:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2011-08-11 13:10:16 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.pm.PackageManager.NameNotFoundException;
|
2011-03-14 18:46:15 +00:00
|
|
|
import android.content.res.AssetFileDescriptor;
|
2011-07-21 08:36:57 +00:00
|
|
|
import android.content.res.Resources;
|
2011-03-14 18:46:15 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2012-04-03 05:28:56 +00:00
|
|
|
import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
|
|
|
|
|
2011-08-11 07:44:36 +00:00
|
|
|
import java.io.File;
|
2011-08-11 07:46:43 +00:00
|
|
|
import java.util.ArrayList;
|
2011-03-14 18:46:15 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class to get the address of a mmap'able dictionary file.
|
|
|
|
*/
|
|
|
|
class BinaryDictionaryGetter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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";
|
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
// Prevents this from being instantiated
|
|
|
|
private BinaryDictionaryGetter() {}
|
|
|
|
|
2011-08-11 13:10:16 +00:00
|
|
|
/**
|
|
|
|
* Returns whether we may want to use this character as part of a file name.
|
|
|
|
*
|
|
|
|
* This basically only accepts ascii letters and numbers, and rejects everything else.
|
|
|
|
*/
|
|
|
|
private static boolean isFileNameCharacter(int codePoint) {
|
|
|
|
if (codePoint >= 0x30 && codePoint <= 0x39) return true; // Digit
|
|
|
|
if (codePoint >= 0x41 && codePoint <= 0x5A) return true; // Uppercase
|
|
|
|
if (codePoint >= 0x61 && codePoint <= 0x7A) return true; // Lowercase
|
|
|
|
return codePoint == '_'; // Underscore
|
|
|
|
}
|
|
|
|
|
2011-08-11 07:44:36 +00:00
|
|
|
/**
|
|
|
|
* Escapes a string for any characters that may be suspicious for a file or directory name.
|
|
|
|
*
|
|
|
|
* Concretely this does a sort of URL-encoding except it will encode everything that's not
|
|
|
|
* alphanumeric or underscore. (true URL-encoding leaves alone characters like '*', which
|
|
|
|
* we cannot allow here)
|
|
|
|
*/
|
|
|
|
// TODO: create a unit test for this method
|
2011-08-11 13:10:16 +00:00
|
|
|
private static String replaceFileNameDangerousCharacters(final String name) {
|
2011-08-11 07:44:36 +00:00
|
|
|
// This assumes '%' is fully available as a non-separator, normal
|
|
|
|
// character in a file name. This is probably true for all file systems.
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
2012-02-03 01:51:34 +00:00
|
|
|
final int nameLength = name.length();
|
|
|
|
for (int i = 0; i < nameLength; i = name.offsetByCodePoints(i, 1)) {
|
2011-08-11 07:44:36 +00:00
|
|
|
final int codePoint = name.codePointAt(i);
|
2011-08-11 13:10:16 +00:00
|
|
|
if (isFileNameCharacter(codePoint)) {
|
2011-08-11 07:44:36 +00:00
|
|
|
sb.appendCodePoint(codePoint);
|
|
|
|
} else {
|
2011-08-11 13:10:16 +00:00
|
|
|
// 6 digits - unicode is limited to 21 bits
|
|
|
|
sb.append(String.format((Locale)null, "%%%1$06x", codePoint));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse escaping done by replaceFileNameDangerousCharacters.
|
|
|
|
*/
|
|
|
|
private static String getWordListIdFromFileName(final String fname) {
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
2012-02-03 01:51:34 +00:00
|
|
|
final int fnameLength = fname.length();
|
|
|
|
for (int i = 0; i < fnameLength; i = fname.offsetByCodePoints(i, 1)) {
|
2011-08-11 13:10:16 +00:00
|
|
|
final int codePoint = fname.codePointAt(i);
|
|
|
|
if ('%' != codePoint) {
|
|
|
|
sb.appendCodePoint(codePoint);
|
|
|
|
} else {
|
|
|
|
final int encodedCodePoint = Integer.parseInt(fname.substring(i + 1, i + 7), 16);
|
|
|
|
i += 6;
|
|
|
|
sb.appendCodePoint(encodedCodePoint);
|
2011-08-11 07:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2011-08-25 09:04:21 +00:00
|
|
|
/**
|
|
|
|
* Helper method to get the top level cache directory.
|
|
|
|
*/
|
|
|
|
private static String getWordListCacheDirectory(final Context context) {
|
|
|
|
return context.getFilesDir() + File.separator + "dicts";
|
|
|
|
}
|
|
|
|
|
2011-08-11 07:44:36 +00:00
|
|
|
/**
|
|
|
|
* Find out the cache directory associated with a specific locale.
|
|
|
|
*/
|
2011-08-25 09:04:21 +00:00
|
|
|
private static String getCacheDirectoryForLocale(final String locale, final Context context) {
|
|
|
|
final String relativeDirectoryName = replaceFileNameDangerousCharacters(locale);
|
|
|
|
final String absoluteDirectoryName = getWordListCacheDirectory(context) + File.separator
|
2011-08-11 07:44:36 +00:00
|
|
|
+ relativeDirectoryName;
|
|
|
|
final File directory = new File(absoluteDirectoryName);
|
|
|
|
if (!directory.exists()) {
|
|
|
|
if (!directory.mkdirs()) {
|
|
|
|
Log.e(TAG, "Could not create the directory for locale" + locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return absoluteDirectoryName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a file name for the id and locale passed as an argument.
|
|
|
|
*
|
|
|
|
* In the current implementation the file name returned will always be unique for
|
|
|
|
* any id/locale pair, but please do not expect that the id can be the same for
|
|
|
|
* different dictionaries with different locales. An id should be unique for any
|
|
|
|
* dictionary.
|
|
|
|
* The file name is pretty much an URL-encoded version of the id inside a directory
|
|
|
|
* named like the locale, except it will also escape characters that look dangerous
|
|
|
|
* to some file systems.
|
|
|
|
* @param id the id of the dictionary for which to get a file name
|
2011-08-25 09:04:21 +00:00
|
|
|
* @param locale the locale for which to get the file name as a string
|
2011-08-11 07:44:36 +00:00
|
|
|
* @param context the context to use for getting the directory
|
|
|
|
* @return the name of the file to be created
|
|
|
|
*/
|
2011-08-25 09:04:21 +00:00
|
|
|
public static String getCacheFileName(String id, String locale, Context context) {
|
2011-08-11 07:44:36 +00:00
|
|
|
final String fileName = replaceFileNameDangerousCharacters(id);
|
|
|
|
return getCacheDirectoryForLocale(locale, context) + File.separator + fileName;
|
|
|
|
}
|
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a file address from a resource, or null if it cannot be opened.
|
|
|
|
*/
|
2011-07-21 08:36:57 +00:00
|
|
|
private static AssetFileAddress loadFallbackResource(final Context context,
|
|
|
|
final int fallbackResId, final Locale locale) {
|
2012-04-03 05:28:56 +00:00
|
|
|
final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() {
|
|
|
|
@Override
|
|
|
|
protected AssetFileDescriptor job(Resources res) {
|
|
|
|
return res.openRawResourceFd(fallbackResId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
final AssetFileDescriptor afd = job.runInLocale(context.getResources(), locale);
|
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;
|
|
|
|
}
|
|
|
|
return AssetFileAddress.makeFromFileNameAndOffset(
|
|
|
|
context.getApplicationInfo().sourceDir, afd.getStartOffset(), afd.getLength());
|
|
|
|
}
|
|
|
|
|
2011-08-16 12:41:12 +00:00
|
|
|
static private class DictPackSettings {
|
|
|
|
final SharedPreferences mDictPreferences;
|
|
|
|
public DictPackSettings(final Context context) {
|
|
|
|
Context dictPackContext = null;
|
|
|
|
try {
|
|
|
|
final String dictPackName =
|
|
|
|
context.getString(R.string.dictionary_pack_package_name);
|
|
|
|
dictPackContext = context.createPackageContext(dictPackName, 0);
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
// The dictionary pack is not installed...
|
|
|
|
// TODO: fallback on the built-in dict, see the TODO above
|
|
|
|
Log.e(TAG, "Could not find a dictionary pack");
|
|
|
|
}
|
|
|
|
mDictPreferences = null == dictPackContext ? null
|
|
|
|
: dictPackContext.getSharedPreferences(COMMON_PREFERENCES_NAME,
|
|
|
|
Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 09:04:21 +00:00
|
|
|
/**
|
|
|
|
* Helper method to the list of cache directories, one for each distinct locale.
|
|
|
|
*/
|
|
|
|
private static File[] getCachedDirectoryList(final Context context) {
|
|
|
|
return new File(getWordListCacheDirectory(context)).listFiles();
|
|
|
|
}
|
|
|
|
|
2011-08-11 07:46:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the list of cached files for a specific locale.
|
|
|
|
*
|
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
|
|
|
*/
|
2011-08-25 09:04:21 +00:00
|
|
|
private static File[] getCachedWordLists(final String locale,
|
2011-08-11 07:46:43 +00:00
|
|
|
final Context context) {
|
2011-08-25 09:04:21 +00:00
|
|
|
final File[] directoryList = getCachedDirectoryList(context);
|
|
|
|
if (null == directoryList) return EMPTY_FILE_ARRAY;
|
|
|
|
final ArrayList<File> cacheFiles = new ArrayList<File>();
|
|
|
|
for (File directory : directoryList) {
|
|
|
|
if (!directory.isDirectory()) continue;
|
|
|
|
final String dirLocale = getWordListIdFromFileName(directory.getName());
|
|
|
|
if (LocaleUtils.isMatch(LocaleUtils.getMatchLevel(dirLocale, locale))) {
|
|
|
|
final File[] wordLists = directory.listFiles();
|
|
|
|
if (null != wordLists) {
|
|
|
|
for (File wordList : wordLists) {
|
|
|
|
cacheFiles.add(wordList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cacheFiles.isEmpty()) return EMPTY_FILE_ARRAY;
|
|
|
|
return cacheFiles.toArray(EMPTY_FILE_ARRAY);
|
2011-08-11 07:46:43 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 11:09:35 +00:00
|
|
|
/**
|
2011-08-25 09:04:21 +00:00
|
|
|
* Returns the id associated with the main word list for a specified locale.
|
|
|
|
*
|
|
|
|
* Word lists stored in Android Keyboard's resources are referred to as the "main"
|
|
|
|
* word lists. Since they can be updated like any other list, we need to assign a
|
|
|
|
* unique ID to them. This ID is just the name of the language (locale-wise) they
|
|
|
|
* are for, and this method returns this ID.
|
2011-08-18 11:09:35 +00:00
|
|
|
*/
|
|
|
|
private static String getMainDictId(final Locale locale) {
|
2011-08-25 09:04:21 +00:00
|
|
|
// This works because we don't include by default different dictionaries for
|
|
|
|
// different countries. This actually needs to return the id that we would
|
|
|
|
// like to use for word lists included in resources, and the following is okay.
|
|
|
|
return locale.getLanguage().toString();
|
2011-08-18 11:09:35 +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:
|
|
|
|
* - Gets a file name from the fallback resource passed as an argument.
|
|
|
|
* 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,
|
2011-08-11 07:46:43 +00:00
|
|
|
final Context context, final int fallbackResId) {
|
2011-08-16 12:35:52 +00:00
|
|
|
|
2011-08-24 03:45:52 +00:00
|
|
|
// cacheWordListsFromContentProvider returns the list of files it copied to local
|
2011-08-16 12:35:52 +00:00
|
|
|
// storage, but we don't really care about what was copied NOW: what we want is the
|
|
|
|
// list of everything we ever cached, so we ignore the return value.
|
2011-08-24 03:45:52 +00:00
|
|
|
BinaryDictionaryFileDumper.cacheWordListsFromContentProvider(locale, context);
|
2011-08-25 09:04:21 +00:00
|
|
|
final File[] cachedWordLists = getCachedWordLists(locale.toString(), context);
|
2011-08-18 06:44:53 +00:00
|
|
|
|
2011-08-18 11:09:35 +00:00
|
|
|
final String mainDictId = 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;
|
2011-08-18 06:44:53 +00:00
|
|
|
final ArrayList<AssetFileAddress> fileList = new ArrayList<AssetFileAddress>();
|
2011-08-24 03:45:52 +00:00
|
|
|
// cachedWordLists may not be null, see doc for getCachedDictionaryList
|
|
|
|
for (final File f : cachedWordLists) {
|
2011-08-18 06:44:53 +00:00
|
|
|
final String wordListId = getWordListIdFromFileName(f.getName());
|
2011-08-18 11:09:35 +00:00
|
|
|
if (wordListId.equals(mainDictId)) {
|
|
|
|
foundMainDict = true;
|
|
|
|
}
|
2011-08-18 06:44:53 +00:00
|
|
|
if (!dictPackSettings.isWordListActive(wordListId)) continue;
|
|
|
|
if (f.canRead()) {
|
|
|
|
fileList.add(AssetFileAddress.makeFromFileName(f.getPath()));
|
|
|
|
} else {
|
|
|
|
Log.e(TAG, "Found a cached dictionary file but cannot read it");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-18 11:09:35 +00:00
|
|
|
if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
|
|
|
|
final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId,
|
|
|
|
locale);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|