Merge "Determine the protocol version of the dictionary pack"
This commit is contained in:
commit
4beaab2a88
1 changed files with 45 additions and 15 deletions
|
@ -95,6 +95,35 @@ public final class BinaryDictionaryFileDumper {
|
||||||
path);
|
path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the content URI builder for a specified type.
|
||||||
|
*
|
||||||
|
* Supported types include QUERY_PATH_DICT_INFO, which takes the locale as
|
||||||
|
* the extraPath argument, and QUERY_PATH_DATAFILE, which needs a wordlist ID
|
||||||
|
* as the extraPath argument.
|
||||||
|
*
|
||||||
|
* @param clientId the clientId to use
|
||||||
|
* @param contentProviderClient the instance of content provider client
|
||||||
|
* @param queryPathType the path element encoding the type
|
||||||
|
* @param extraPath optional extra argument for this type (typically word list id)
|
||||||
|
* @return a builder that can build the URI for the best supported protocol version
|
||||||
|
* @throws RemoteException if the client can't be contacted
|
||||||
|
*/
|
||||||
|
private static Uri.Builder getContentUriBuilderForType(final String clientId,
|
||||||
|
final ContentProviderClient contentProviderClient, final String queryPathType,
|
||||||
|
final String extraPath) throws RemoteException {
|
||||||
|
// Check whether protocol v2 is supported by building a v2 URI and calling getType()
|
||||||
|
// on it. If this returns null, v2 is not supported.
|
||||||
|
final Uri.Builder uriV2Builder = getProviderUriBuilder(clientId);
|
||||||
|
uriV2Builder.appendPath(queryPathType);
|
||||||
|
uriV2Builder.appendPath(extraPath);
|
||||||
|
uriV2Builder.appendQueryParameter(QUERY_PARAMETER_PROTOCOL,
|
||||||
|
QUERY_PARAMETER_PROTOCOL_VALUE);
|
||||||
|
if (null != contentProviderClient.getType(uriV2Builder.build())) return uriV2Builder;
|
||||||
|
// Protocol v2 is not supported, so create and return the protocol v1 uri.
|
||||||
|
return getProviderUriBuilder(extraPath);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries a content provider for the list of word lists for a specific locale
|
* Queries a content provider for the list of word lists for a specific locale
|
||||||
* available to copy into Latin IME.
|
* available to copy into Latin IME.
|
||||||
|
@ -102,26 +131,27 @@ public final class BinaryDictionaryFileDumper {
|
||||||
private static List<WordListInfo> getWordListWordListInfos(final Locale locale,
|
private static List<WordListInfo> getWordListWordListInfos(final Locale locale,
|
||||||
final Context context, final boolean hasDefaultWordList) {
|
final Context context, final boolean hasDefaultWordList) {
|
||||||
final String clientId = context.getString(R.string.dictionary_pack_client_id);
|
final String clientId = context.getString(R.string.dictionary_pack_client_id);
|
||||||
final Uri.Builder builder = getProviderUriBuilder(clientId);
|
|
||||||
builder.appendPath(QUERY_PATH_DICT_INFO);
|
|
||||||
builder.appendPath(locale.toString());
|
|
||||||
builder.appendQueryParameter(QUERY_PARAMETER_PROTOCOL, QUERY_PARAMETER_PROTOCOL_VALUE);
|
|
||||||
if (!hasDefaultWordList) {
|
|
||||||
builder.appendQueryParameter(QUERY_PARAMETER_MAY_PROMPT_USER,
|
|
||||||
QUERY_PARAMETER_TRUE);
|
|
||||||
}
|
|
||||||
final Uri dictionaryPackUri = builder.build();
|
|
||||||
|
|
||||||
final ContentProviderClient client = context.getContentResolver().
|
final ContentProviderClient client = context.getContentResolver().
|
||||||
acquireContentProviderClient(getProviderUriBuilder("").build());
|
acquireContentProviderClient(getProviderUriBuilder("").build());
|
||||||
if (null == client) return Collections.<WordListInfo>emptyList();
|
if (null == client) return Collections.<WordListInfo>emptyList();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final Cursor c = client.query(dictionaryPackUri, DICTIONARY_PROJECTION, null, null,
|
final Uri.Builder builder = getContentUriBuilderForType(clientId, client,
|
||||||
null);
|
QUERY_PATH_DICT_INFO, locale.toString());
|
||||||
if (null == c) {
|
if (!hasDefaultWordList) {
|
||||||
reinitializeClientRecordInDictionaryContentProvider(context, client, clientId);
|
builder.appendQueryParameter(QUERY_PARAMETER_MAY_PROMPT_USER,
|
||||||
return Collections.<WordListInfo>emptyList();
|
QUERY_PARAMETER_TRUE);
|
||||||
}
|
}
|
||||||
|
final Uri queryUri = builder.build();
|
||||||
|
final boolean isProtocolV2 = (QUERY_PARAMETER_PROTOCOL_VALUE.equals(
|
||||||
|
queryUri.getQueryParameter(QUERY_PARAMETER_PROTOCOL)));
|
||||||
|
|
||||||
|
Cursor c = client.query(queryUri, DICTIONARY_PROJECTION, null, null, null);
|
||||||
|
if (isProtocolV2 && null == c) {
|
||||||
|
reinitializeClientRecordInDictionaryContentProvider(context, client, clientId);
|
||||||
|
c = client.query(queryUri, DICTIONARY_PROJECTION, null, null, null);
|
||||||
|
}
|
||||||
|
if (null == c) return Collections.<WordListInfo>emptyList();
|
||||||
if (c.getCount() <= 0 || !c.moveToFirst()) {
|
if (c.getCount() <= 0 || !c.moveToFirst()) {
|
||||||
c.close();
|
c.close();
|
||||||
return Collections.<WordListInfo>emptyList();
|
return Collections.<WordListInfo>emptyList();
|
||||||
|
|
Loading…
Reference in a new issue