2011-04-26 12:49:09 +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-04-26 12:49:09 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-26 12:49:09 +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-04-26 12:49:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.AssetFileDescriptor;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-05-31 04:51:22 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.DictionaryInfoUtils;
|
2013-05-31 04:51:22 +00:00
|
|
|
|
2011-04-26 12:49:09 +00:00
|
|
|
import java.io.File;
|
2012-03-16 15:50:51 +00:00
|
|
|
import java.util.ArrayList;
|
2011-04-27 14:13:11 +00:00
|
|
|
import java.util.LinkedList;
|
2011-04-26 12:49:09 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory for dictionary instances.
|
|
|
|
*/
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class DictionaryFactory {
|
2012-04-19 10:53:41 +00:00
|
|
|
private static final String TAG = DictionaryFactory.class.getSimpleName();
|
2011-04-26 12:49:09 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-16 06:08:45 +00:00
|
|
|
* Initializes a main dictionary collection from a dictionary pack, with explicit flags.
|
2011-04-26 12:49:09 +00:00
|
|
|
*
|
|
|
|
* This searches for a content provider providing a dictionary pack for the specified
|
2012-04-11 12:02:26 +00:00
|
|
|
* locale. If none is found, it falls back to the built-in dictionary - if any.
|
2011-04-26 12:49:09 +00:00
|
|
|
* @param context application context for reading resources
|
|
|
|
* @param locale the locale for which to create the dictionary
|
2012-04-06 10:12:05 +00:00
|
|
|
* @param useFullEditDistance whether to use the full edit distance in suggestions
|
2011-08-18 10:29:29 +00:00
|
|
|
* @return an initialized instance of DictionaryCollection
|
2011-04-26 12:49:09 +00:00
|
|
|
*/
|
2012-05-16 06:08:45 +00:00
|
|
|
public static DictionaryCollection createMainDictionaryFromManager(final Context context,
|
2012-04-11 12:02:26 +00:00
|
|
|
final Locale locale, final boolean useFullEditDistance) {
|
2011-04-26 12:49:09 +00:00
|
|
|
if (null == locale) {
|
|
|
|
Log.e(TAG, "No locale defined for dictionary");
|
2012-06-27 09:08:08 +00:00
|
|
|
return new DictionaryCollection(Dictionary.TYPE_MAIN,
|
2012-06-27 08:31:09 +00:00
|
|
|
createBinaryDictionary(context, locale));
|
2011-04-26 12:49:09 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 07:34:55 +00:00
|
|
|
final LinkedList<Dictionary> dictList = CollectionUtils.newLinkedList();
|
2012-03-16 15:50:51 +00:00
|
|
|
final ArrayList<AssetFileAddress> assetFileList =
|
2012-04-11 12:02:26 +00:00
|
|
|
BinaryDictionaryGetter.getDictionaryFiles(locale, context);
|
2011-07-19 09:16:34 +00:00
|
|
|
if (null != assetFileList) {
|
|
|
|
for (final AssetFileAddress f : assetFileList) {
|
2013-01-11 09:59:01 +00:00
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(f.mFilename,
|
2013-06-25 08:39:06 +00:00
|
|
|
f.mOffset, f.mLength, useFullEditDistance, locale, Dictionary.TYPE_MAIN,
|
|
|
|
false /* isUpdatable */);
|
2011-07-20 10:02:29 +00:00
|
|
|
if (binaryDictionary.isValidDictionary()) {
|
|
|
|
dictList.add(binaryDictionary);
|
|
|
|
}
|
2011-07-19 09:16:34 +00:00
|
|
|
}
|
2011-04-27 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
2011-08-23 08:55:46 +00:00
|
|
|
// If the list is empty, that means we should not use any dictionary (for example, the user
|
|
|
|
// explicitly disabled the main dictionary), so the following is okay. dictList is never
|
|
|
|
// null, but if for some reason it is, DictionaryCollection handles it gracefully.
|
2012-06-27 09:08:08 +00:00
|
|
|
return new DictionaryCollection(Dictionary.TYPE_MAIN, dictList);
|
2011-04-26 12:49:09 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 12:07:22 +00:00
|
|
|
/**
|
2012-05-16 06:08:45 +00:00
|
|
|
* Initializes a main dictionary collection from a dictionary pack, with default flags.
|
2011-09-29 12:07:22 +00:00
|
|
|
*
|
|
|
|
* This searches for a content provider providing a dictionary pack for the specified
|
2012-04-11 12:02:26 +00:00
|
|
|
* locale. If none is found, it falls back to the built-in dictionary, if any.
|
2011-09-29 12:07:22 +00:00
|
|
|
* @param context application context for reading resources
|
|
|
|
* @param locale the locale for which to create the dictionary
|
|
|
|
* @return an initialized instance of DictionaryCollection
|
|
|
|
*/
|
2012-05-16 06:08:45 +00:00
|
|
|
public static DictionaryCollection createMainDictionaryFromManager(final Context context,
|
2012-04-11 12:02:26 +00:00
|
|
|
final Locale locale) {
|
2012-05-16 06:08:45 +00:00
|
|
|
return createMainDictionaryFromManager(context, locale, false /* useFullEditDistance */);
|
2011-09-29 12:07:22 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 12:49:09 +00:00
|
|
|
/**
|
|
|
|
* Initializes a dictionary from a raw resource file
|
|
|
|
* @param context application context for reading resources
|
2011-07-21 08:36:57 +00:00
|
|
|
* @param locale the locale to use for the resource
|
2011-04-26 12:49:09 +00:00
|
|
|
* @return an initialized instance of BinaryDictionary
|
|
|
|
*/
|
2011-07-21 08:36:57 +00:00
|
|
|
protected static BinaryDictionary createBinaryDictionary(final Context context,
|
2012-04-11 12:02:26 +00:00
|
|
|
final Locale locale) {
|
2011-04-26 12:49:09 +00:00
|
|
|
AssetFileDescriptor afd = null;
|
|
|
|
try {
|
2013-01-29 09:05:33 +00:00
|
|
|
final int resId = DictionaryInfoUtils.getMainDictionaryResourceIdIfAvailableForLocale(
|
|
|
|
context.getResources(), locale);
|
2012-04-11 12:29:53 +00:00
|
|
|
if (0 == resId) return null;
|
2012-04-11 05:58:02 +00:00
|
|
|
afd = context.getResources().openRawResourceFd(resId);
|
2011-04-26 12:49:09 +00:00
|
|
|
if (afd == null) {
|
|
|
|
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final String sourceDir = context.getApplicationInfo().sourceDir;
|
|
|
|
final File packagePath = new File(sourceDir);
|
|
|
|
// TODO: Come up with a way to handle a directory.
|
|
|
|
if (!packagePath.isFile()) {
|
|
|
|
Log.e(TAG, "sourceDir is not a file: " + sourceDir);
|
|
|
|
return null;
|
|
|
|
}
|
2013-01-11 09:59:01 +00:00
|
|
|
return new BinaryDictionary(sourceDir, afd.getStartOffset(), afd.getLength(),
|
2013-06-25 08:39:06 +00:00
|
|
|
false /* useFullEditDistance */, locale, Dictionary.TYPE_MAIN,
|
|
|
|
false /* isUpdatable */);
|
2011-04-26 12:49:09 +00:00
|
|
|
} catch (android.content.res.Resources.NotFoundException e) {
|
2012-04-11 12:02:26 +00:00
|
|
|
Log.e(TAG, "Could not find the resource");
|
2011-04-26 12:49:09 +00:00
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
if (null != afd) {
|
|
|
|
try {
|
|
|
|
afd.close();
|
|
|
|
} catch (java.io.IOException e) {
|
|
|
|
/* IOException on close ? What am I supposed to do ? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a dictionary from passed data. This is intended for unit tests only.
|
2013-05-31 04:51:22 +00:00
|
|
|
* @param dictionaryList the list of files to read, with their offsets and lengths
|
2012-04-06 10:12:05 +00:00
|
|
|
* @param useFullEditDistance whether to use the full edit distance in suggestions
|
2011-04-26 12:49:09 +00:00
|
|
|
* @return the created dictionary, or null.
|
|
|
|
*/
|
2013-05-31 04:51:22 +00:00
|
|
|
@UsedForTesting
|
|
|
|
public static Dictionary createDictionaryForTest(final AssetFileAddress[] dictionaryList,
|
2013-01-11 09:59:01 +00:00
|
|
|
final boolean useFullEditDistance, Locale locale) {
|
2013-05-31 04:51:22 +00:00
|
|
|
final DictionaryCollection dictionaryCollection =
|
|
|
|
new DictionaryCollection(Dictionary.TYPE_MAIN);
|
|
|
|
for (final AssetFileAddress address : dictionaryList) {
|
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(address.mFilename,
|
|
|
|
address.mOffset, address.mLength, useFullEditDistance, locale,
|
2013-06-25 08:39:06 +00:00
|
|
|
Dictionary.TYPE_MAIN, false /* isUpdatable */);
|
2013-05-31 04:51:22 +00:00
|
|
|
dictionaryCollection.addDictionary(binaryDictionary);
|
2011-04-26 12:49:09 +00:00
|
|
|
}
|
2013-05-31 04:51:22 +00:00
|
|
|
return dictionaryCollection;
|
2011-04-26 12:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find out whether a dictionary is available for this locale.
|
|
|
|
* @param context the context on which to check resources.
|
|
|
|
* @param locale the locale to check for.
|
|
|
|
* @return whether a (non-placeholder) dictionary is available or not.
|
|
|
|
*/
|
|
|
|
public static boolean isDictionaryAvailable(Context context, Locale locale) {
|
2012-04-11 05:58:02 +00:00
|
|
|
final Resources res = context.getResources();
|
2013-01-29 09:05:33 +00:00
|
|
|
return 0 != DictionaryInfoUtils.getMainDictionaryResourceIdIfAvailableForLocale(
|
|
|
|
res, locale);
|
2012-03-08 08:07:02 +00:00
|
|
|
}
|
2011-04-26 12:49:09 +00:00
|
|
|
}
|