am 3623ad23: [B14] Implement sending the dictionary list.

# Via Jean Chalard
* commit '3623ad238c3fa76f36cb54de63bd31cf320befb4':
  [B14] Implement sending the dictionary list.
main
Jean Chalard 2013-02-07 09:29:40 -08:00 committed by Android Git Automerger
commit a13f4942ad
2 changed files with 57 additions and 16 deletions

View File

@ -25,6 +25,8 @@ import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.DictionaryInfoUtils.DictionaryInfo;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
@ -33,6 +35,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -359,6 +362,19 @@ public final class BinaryDictionaryFileDumper {
metadataValues.put(INSERT_METADATA_CLIENT_ID_COLUMN, clientId); metadataValues.put(INSERT_METADATA_CLIENT_ID_COLUMN, clientId);
metadataValues.put(INSERT_METADATA_METADATA_URI_COLUMN, metadataFileUri); metadataValues.put(INSERT_METADATA_METADATA_URI_COLUMN, metadataFileUri);
resolver.insert(metadataContentUri, metadataValues); resolver.insert(metadataContentUri, metadataValues);
// TODO: Update the versions of the dictionaries
// Update the dictionary list.
final Uri dictionaryContentUriBase = getProviderUriBuilder(clientId)
.appendPath(QUERY_PATH_DICT_INFO)
.appendQueryParameter(QUERY_PARAMETER_PROTOCOL, QUERY_PARAMETER_PROTOCOL_VALUE)
.build();
final ArrayList<DictionaryInfo> dictionaryList =
DictionaryInfoUtils.getCurrentDictionaryFileNameAndVersionInfo(context);
final int length = dictionaryList.size();
for (int i = 0; i < length; ++i) {
final DictionaryInfo info = dictionaryList.get(i);
resolver.insert(Uri.withAppendedPath(dictionaryContentUriBase, info.mId),
info.toContentValues());
}
} }
} }

View File

@ -16,9 +16,11 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.makedict.BinaryDictIOUtils; import com.android.inputmethod.latin.makedict.BinaryDictIOUtils;
@ -39,19 +41,40 @@ public class DictionaryInfoUtils {
private static final String RESOURCE_PACKAGE_NAME = private static final String RESOURCE_PACKAGE_NAME =
DictionaryInfoUtils.class.getPackage().getName(); DictionaryInfoUtils.class.getPackage().getName();
private static final String DEFAULT_MAIN_DICT = "main"; private static final String DEFAULT_MAIN_DICT = "main";
private static final String ID_CATEGORY_SEPARATOR =
BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR;
private static final String MAIN_DICT_PREFIX = "main_"; private static final String MAIN_DICT_PREFIX = "main_";
// 6 digits - unicode is limited to 21 bits // 6 digits - unicode is limited to 21 bits
private static final int MAX_HEX_DIGITS_FOR_CODEPOINT = 6; private static final int MAX_HEX_DIGITS_FOR_CODEPOINT = 6;
public static class DictionaryInfo { public static class DictionaryInfo {
private static final String LOCALE_COLUMN = "locale";
private static final String WORDLISTID_COLUMN = "id";
private static final String LOCAL_FILENAME_COLUMN = "filename";
private static final String DATE_COLUMN = "date";
private static final String FILESIZE_COLUMN = "filesize";
private static final String VERSION_COLUMN = "version";
public final Locale mLocale; public final Locale mLocale;
public final AssetFileAddress mFileAddress; public final AssetFileAddress mFileAddress;
public final int mVersion; public final int mVersion;
public final String mId;
public DictionaryInfo(final Locale locale, final AssetFileAddress fileAddress, public DictionaryInfo(final Locale locale, final AssetFileAddress fileAddress,
final int version) { final int version) {
mLocale = locale; mLocale = locale;
mFileAddress = fileAddress; mFileAddress = fileAddress;
mVersion = version; mVersion = version;
mId = DEFAULT_MAIN_DICT + ID_CATEGORY_SEPARATOR + mLocale;
}
public ContentValues toContentValues() {
final ContentValues values = new ContentValues();
values.put(WORDLISTID_COLUMN, mId);
values.put(LOCALE_COLUMN, mLocale.toString());
values.put(LOCAL_FILENAME_COLUMN, mFileAddress.mFilename);
values.put(DATE_COLUMN,
new File(mFileAddress.mFilename).lastModified() / DateUtils.SECOND_IN_MILLIS);
values.put(FILESIZE_COLUMN, mFileAddress.mLength);
values.put(VERSION_COLUMN, mVersion);
return values;
} }
} }
@ -284,21 +307,23 @@ public class DictionaryInfoUtils {
// Retrieve downloaded dictionaries // Retrieve downloaded dictionaries
final File[] directoryList = getCachedDirectoryList(context); final File[] directoryList = getCachedDirectoryList(context);
for (final File directory : directoryList) { if (null != directoryList) {
final String localeString = getWordListIdFromFileName(directory.getName()); for (final File directory : directoryList) {
File[] dicts = BinaryDictionaryGetter.getCachedWordLists(localeString, context); final String localeString = getWordListIdFromFileName(directory.getName());
for (final File dict : dicts) { File[] dicts = BinaryDictionaryGetter.getCachedWordLists(localeString, context);
final String wordListId = getWordListIdFromFileName(dict.getName()); for (final File dict : dicts) {
if (!DictionaryInfoUtils.isMainWordListId(wordListId)) continue; final String wordListId = getWordListIdFromFileName(dict.getName());
final Locale locale = LocaleUtils.constructLocaleFromString(localeString); if (!DictionaryInfoUtils.isMainWordListId(wordListId)) continue;
final AssetFileAddress fileAddress = AssetFileAddress.makeFromFile(dict); final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
final DictionaryInfo dictionaryInfo = final AssetFileAddress fileAddress = AssetFileAddress.makeFromFile(dict);
createDictionaryInfoFromFileAddress(fileAddress); final DictionaryInfo dictionaryInfo =
// Protect against cases of a less-specific dictionary being found, like an createDictionaryInfoFromFileAddress(fileAddress);
// en dictionary being used for an en_US locale. In this case, the en dictionary // Protect against cases of a less-specific dictionary being found, like an
// should be used for en_US but discounted for listing purposes. // en dictionary being used for an en_US locale. In this case, the en dictionary
if (!dictionaryInfo.mLocale.equals(locale)) continue; // should be used for en_US but discounted for listing purposes.
addOrUpdateDictInfo(dictList, dictionaryInfo); if (!dictionaryInfo.mLocale.equals(locale)) continue;
addOrUpdateDictInfo(dictList, dictionaryInfo);
}
} }
} }