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;
|
|
|
|
|
2013-02-11 19:52:34 +00:00
|
|
|
import android.content.ContentProviderClient;
|
2011-03-14 18:46:15 +00:00
|
|
|
import android.content.ContentResolver;
|
2013-02-02 07:30:11 +00:00
|
|
|
import android.content.ContentValues;
|
2011-03-14 18:46:15 +00:00
|
|
|
import android.content.Context;
|
2011-04-15 06:19:25 +00:00
|
|
|
import android.content.res.AssetFileDescriptor;
|
2011-08-02 10:13:44 +00:00
|
|
|
import android.database.Cursor;
|
2011-03-14 18:46:15 +00:00
|
|
|
import android.net.Uri;
|
2013-02-13 01:59:36 +00:00
|
|
|
import android.os.RemoteException;
|
2011-03-14 18:46:15 +00:00
|
|
|
import android.text.TextUtils;
|
2011-08-10 07:37:46 +00:00
|
|
|
import android.util.Log;
|
2011-03-14 18:46:15 +00:00
|
|
|
|
2013-03-19 07:45:25 +00:00
|
|
|
import com.android.inputmethod.dictionarypack.DictionaryPackConstants;
|
2014-05-21 05:02:54 +00:00
|
|
|
import com.android.inputmethod.dictionarypack.MD5Calculator;
|
2015-06-25 00:40:52 +00:00
|
|
|
import com.android.inputmethod.dictionarypack.UpdateHandler;
|
2015-05-04 18:57:31 +00:00
|
|
|
import com.android.inputmethod.latin.common.FileUtils;
|
2015-02-21 02:30:23 +00:00
|
|
|
import com.android.inputmethod.latin.define.DecoderSpecificConstants;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.DictionaryInfoUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.DictionaryInfoUtils.DictionaryInfo;
|
2013-07-22 03:43:37 +00:00
|
|
|
import com.android.inputmethod.latin.utils.FileTransforms;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.MetadataFileUriGetter;
|
2013-02-03 17:21:54 +00:00
|
|
|
|
2011-08-30 12:46:33 +00:00
|
|
|
import java.io.BufferedInputStream;
|
2013-01-18 10:21:14 +00:00
|
|
|
import java.io.BufferedOutputStream;
|
2013-05-30 09:53:56 +00:00
|
|
|
import java.io.Closeable;
|
2011-08-30 12:46:33 +00:00
|
|
|
import java.io.File;
|
2014-05-21 05:02:54 +00:00
|
|
|
import java.io.FileInputStream;
|
2011-03-14 18:46:15 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2013-02-03 17:21:54 +00:00
|
|
|
import java.util.ArrayList;
|
2014-05-23 11:18:17 +00:00
|
|
|
import java.util.Arrays;
|
2011-08-02 10:13:44 +00:00
|
|
|
import java.util.Collections;
|
2011-04-27 14:13:11 +00:00
|
|
|
import java.util.List;
|
2011-03-14 18:46:15 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Group class for static methods to help with creation and getting of the binary dictionary
|
|
|
|
* file from the dictionary provider
|
|
|
|
*/
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class BinaryDictionaryFileDumper {
|
2011-08-10 07:37:46 +00:00
|
|
|
private static final String TAG = BinaryDictionaryFileDumper.class.getSimpleName();
|
2011-08-24 12:36:18 +00:00
|
|
|
private static final boolean DEBUG = false;
|
2011-08-10 07:37:46 +00:00
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
/**
|
|
|
|
* The size of the temporary buffer to copy files.
|
|
|
|
*/
|
2012-10-11 07:08:25 +00:00
|
|
|
private static final int FILE_READ_BUFFER_SIZE = 8192;
|
2011-08-30 12:46:33 +00:00
|
|
|
// TODO: make the following data common with the native code
|
2012-04-20 09:56:03 +00:00
|
|
|
private static final byte[] MAGIC_NUMBER_VERSION_1 =
|
|
|
|
new byte[] { (byte)0x78, (byte)0xB1, (byte)0x00, (byte)0x00 };
|
|
|
|
private static final byte[] MAGIC_NUMBER_VERSION_2 =
|
|
|
|
new byte[] { (byte)0x9B, (byte)0xC1, (byte)0x3A, (byte)0xFE };
|
2011-03-14 18:46:15 +00:00
|
|
|
|
2015-02-21 02:30:23 +00:00
|
|
|
private static final boolean SHOULD_VERIFY_MAGIC_NUMBER =
|
|
|
|
DecoderSpecificConstants.SHOULD_VERIFY_MAGIC_NUMBER;
|
|
|
|
private static final boolean SHOULD_VERIFY_CHECKSUM =
|
|
|
|
DecoderSpecificConstants.SHOULD_VERIFY_CHECKSUM;
|
|
|
|
|
2011-08-02 10:13:44 +00:00
|
|
|
private static final String DICTIONARY_PROJECTION[] = { "id" };
|
|
|
|
|
2013-02-01 17:08:58 +00:00
|
|
|
private static final String QUERY_PARAMETER_MAY_PROMPT_USER = "mayPrompt";
|
|
|
|
private static final String QUERY_PARAMETER_TRUE = "true";
|
|
|
|
private static final String QUERY_PARAMETER_DELETE_RESULT = "result";
|
|
|
|
private static final String QUERY_PARAMETER_SUCCESS = "success";
|
|
|
|
private static final String QUERY_PARAMETER_FAILURE = "failure";
|
2012-04-11 12:29:53 +00:00
|
|
|
|
2013-02-02 06:16:43 +00:00
|
|
|
// Using protocol version 2 to communicate with the dictionary pack
|
|
|
|
private static final String QUERY_PARAMETER_PROTOCOL = "protocol";
|
|
|
|
private static final String QUERY_PARAMETER_PROTOCOL_VALUE = "2";
|
|
|
|
|
|
|
|
// The path fragment to append after the client ID for dictionary info requests.
|
|
|
|
private static final String QUERY_PATH_DICT_INFO = "dict";
|
2013-02-14 21:32:31 +00:00
|
|
|
// The path fragment to append after the client ID for dictionary datafile requests.
|
|
|
|
private static final String QUERY_PATH_DATAFILE = "datafile";
|
2013-02-02 07:30:11 +00:00
|
|
|
// The path fragment to append after the client ID for updating the metadata URI.
|
|
|
|
private static final String QUERY_PATH_METADATA = "metadata";
|
|
|
|
private static final String INSERT_METADATA_CLIENT_ID_COLUMN = "clientid";
|
|
|
|
private static final String INSERT_METADATA_METADATA_URI_COLUMN = "uri";
|
2013-04-19 10:49:53 +00:00
|
|
|
private static final String INSERT_METADATA_METADATA_ADDITIONAL_ID_COLUMN = "additionalid";
|
2013-02-02 06:16:43 +00:00
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
// Prevents this class to be accidentally instantiated.
|
|
|
|
private BinaryDictionaryFileDumper() {
|
|
|
|
}
|
|
|
|
|
2011-08-02 10:13:44 +00:00
|
|
|
/**
|
2012-04-11 12:29:53 +00:00
|
|
|
* Returns a URI builder pointing to the dictionary pack.
|
|
|
|
*
|
|
|
|
* This creates a URI builder able to build a URI pointing to the dictionary
|
|
|
|
* pack content provider for a specific dictionary id.
|
2011-08-02 10:13:44 +00:00
|
|
|
*/
|
2013-12-13 08:09:16 +00:00
|
|
|
public static Uri.Builder getProviderUriBuilder(final String path) {
|
2011-08-02 10:13:44 +00:00
|
|
|
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
|
2013-03-19 07:45:25 +00:00
|
|
|
.authority(DictionaryPackConstants.AUTHORITY).appendPath(path);
|
2011-08-02 10:13:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 03:51:39 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2011-08-02 10:13:44 +00:00
|
|
|
/**
|
2011-08-24 03:45:52 +00:00
|
|
|
* Queries a content provider for the list of word lists for a specific locale
|
2011-08-02 10:13:44 +00:00
|
|
|
* available to copy into Latin IME.
|
|
|
|
*/
|
2011-08-25 09:04:21 +00:00
|
|
|
private static List<WordListInfo> getWordListWordListInfos(final Locale locale,
|
2012-04-11 12:29:53 +00:00
|
|
|
final Context context, final boolean hasDefaultWordList) {
|
2013-02-11 23:00:49 +00:00
|
|
|
final String clientId = context.getString(R.string.dictionary_pack_client_id);
|
2013-02-13 01:59:36 +00:00
|
|
|
final ContentProviderClient client = context.getContentResolver().
|
|
|
|
acquireContentProviderClient(getProviderUriBuilder("").build());
|
|
|
|
if (null == client) return Collections.<WordListInfo>emptyList();
|
2014-02-18 07:02:51 +00:00
|
|
|
Cursor cursor = null;
|
2013-02-11 23:00:49 +00:00
|
|
|
try {
|
2013-02-13 03:51:39 +00:00
|
|
|
final Uri.Builder builder = getContentUriBuilderForType(clientId, client,
|
|
|
|
QUERY_PATH_DICT_INFO, locale.toString());
|
|
|
|
if (!hasDefaultWordList) {
|
|
|
|
builder.appendQueryParameter(QUERY_PARAMETER_MAY_PROMPT_USER,
|
|
|
|
QUERY_PARAMETER_TRUE);
|
|
|
|
}
|
|
|
|
final Uri queryUri = builder.build();
|
|
|
|
final boolean isProtocolV2 = (QUERY_PARAMETER_PROTOCOL_VALUE.equals(
|
|
|
|
queryUri.getQueryParameter(QUERY_PARAMETER_PROTOCOL)));
|
|
|
|
|
2014-02-18 07:02:51 +00:00
|
|
|
cursor = client.query(queryUri, DICTIONARY_PROJECTION, null, null, null);
|
|
|
|
if (isProtocolV2 && null == cursor) {
|
2013-02-13 01:59:36 +00:00
|
|
|
reinitializeClientRecordInDictionaryContentProvider(context, client, clientId);
|
2014-02-18 07:02:51 +00:00
|
|
|
cursor = client.query(queryUri, DICTIONARY_PROJECTION, null, null, null);
|
2013-02-11 19:52:34 +00:00
|
|
|
}
|
2014-02-18 07:02:51 +00:00
|
|
|
if (null == cursor) return Collections.<WordListInfo>emptyList();
|
|
|
|
if (cursor.getCount() <= 0 || !cursor.moveToFirst()) {
|
2013-02-11 19:52:34 +00:00
|
|
|
return Collections.<WordListInfo>emptyList();
|
|
|
|
}
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<WordListInfo> list = new ArrayList<>();
|
2011-08-25 09:04:21 +00:00
|
|
|
do {
|
2014-02-18 07:02:51 +00:00
|
|
|
final String wordListId = cursor.getString(0);
|
|
|
|
final String wordListLocale = cursor.getString(1);
|
2014-05-21 03:45:54 +00:00
|
|
|
final String wordListRawChecksum = cursor.getString(2);
|
2011-08-25 09:04:21 +00:00
|
|
|
if (TextUtils.isEmpty(wordListId)) continue;
|
2014-05-21 03:45:54 +00:00
|
|
|
list.add(new WordListInfo(wordListId, wordListLocale, wordListRawChecksum));
|
2014-02-18 07:02:51 +00:00
|
|
|
} while (cursor.moveToNext());
|
2011-08-25 09:04:21 +00:00
|
|
|
return list;
|
2013-02-13 01:59:36 +00:00
|
|
|
} catch (RemoteException e) {
|
|
|
|
// The documentation is unclear as to in which cases this may happen, but it probably
|
|
|
|
// happens when the content provider got suddenly killed because it crashed or because
|
|
|
|
// the user disabled it through Settings.
|
|
|
|
Log.e(TAG, "RemoteException: communication with the dictionary pack cut", e);
|
2013-02-11 19:52:34 +00:00
|
|
|
return Collections.<WordListInfo>emptyList();
|
2011-08-25 09:04:21 +00:00
|
|
|
} catch (Exception e) {
|
2013-02-13 01:59:36 +00:00
|
|
|
// A crash here is dangerous because crashing here would brick any encrypted device -
|
|
|
|
// we need the keyboard to be up and working to enter the password, so we don't want
|
|
|
|
// to die no matter what. So let's be as safe as possible.
|
|
|
|
Log.e(TAG, "Unexpected exception communicating with the dictionary pack", e);
|
2011-08-25 09:04:21 +00:00
|
|
|
return Collections.<WordListInfo>emptyList();
|
2013-02-13 01:59:36 +00:00
|
|
|
} finally {
|
2014-02-18 07:02:51 +00:00
|
|
|
if (null != cursor) {
|
|
|
|
cursor.close();
|
|
|
|
}
|
2013-02-13 01:59:36 +00:00
|
|
|
client.release();
|
2011-08-25 09:04:21 +00:00
|
|
|
}
|
2011-08-02 10:13:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-24 07:32:29 +00:00
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
/**
|
2011-08-24 07:32:29 +00:00
|
|
|
* Helper method to encapsulate exception handling.
|
|
|
|
*/
|
2013-02-14 21:32:31 +00:00
|
|
|
private static AssetFileDescriptor openAssetFileDescriptor(
|
|
|
|
final ContentProviderClient providerClient, final Uri uri) {
|
2011-08-24 07:32:29 +00:00
|
|
|
try {
|
2013-02-14 21:32:31 +00:00
|
|
|
return providerClient.openAssetFile(uri, "r");
|
2011-08-24 07:32:29 +00:00
|
|
|
} catch (FileNotFoundException e) {
|
2013-02-14 21:32:31 +00:00
|
|
|
// I don't want to log the word list URI here for security concerns. The exception
|
|
|
|
// contains the name of the file, so let's not pass it to Log.e here.
|
|
|
|
Log.e(TAG, "Could not find a word list from the dictionary provider."
|
|
|
|
/* intentionally don't pass the exception (see comment above) */);
|
|
|
|
return null;
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
Log.e(TAG, "Can't communicate with the dictionary pack", e);
|
2011-08-24 07:32:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-04 18:57:31 +00:00
|
|
|
* Stages a word list the id of which is passed as an argument. This will write the file
|
2011-08-24 07:32:29 +00:00
|
|
|
* to the cache file name designated by its id and locale, overwriting it if already present
|
|
|
|
* and creating it (and its containing directory) if necessary.
|
2011-08-24 03:45:52 +00:00
|
|
|
*/
|
2015-05-04 18:57:31 +00:00
|
|
|
private static void installWordListToStaging(final String wordlistId, final String locale,
|
2014-05-21 05:02:54 +00:00
|
|
|
final String rawChecksum, final ContentProviderClient providerClient,
|
|
|
|
final Context context) {
|
2011-08-24 07:32:29 +00:00
|
|
|
final int COMPRESSED_CRYPTED_COMPRESSED = 0;
|
|
|
|
final int CRYPTED_COMPRESSED = 1;
|
|
|
|
final int COMPRESSED_CRYPTED = 2;
|
|
|
|
final int COMPRESSED_ONLY = 3;
|
|
|
|
final int CRYPTED_ONLY = 4;
|
|
|
|
final int NONE = 5;
|
|
|
|
final int MODE_MIN = COMPRESSED_CRYPTED_COMPRESSED;
|
|
|
|
final int MODE_MAX = NONE;
|
|
|
|
|
2013-02-14 21:32:31 +00:00
|
|
|
final String clientId = context.getString(R.string.dictionary_pack_client_id);
|
|
|
|
final Uri.Builder wordListUriBuilder;
|
|
|
|
try {
|
|
|
|
wordListUriBuilder = getContentUriBuilderForType(clientId,
|
|
|
|
providerClient, QUERY_PATH_DATAFILE, wordlistId /* extraPath */);
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
Log.e(TAG, "Can't communicate with the dictionary pack", e);
|
2013-04-30 02:10:22 +00:00
|
|
|
return;
|
2013-02-14 21:32:31 +00:00
|
|
|
}
|
|
|
|
final String finalFileName =
|
2015-05-04 18:57:31 +00:00
|
|
|
DictionaryInfoUtils.getStagingFileName(wordlistId, locale, context);
|
2012-10-23 06:34:03 +00:00
|
|
|
String tempFileName;
|
|
|
|
try {
|
2013-02-14 21:32:31 +00:00
|
|
|
tempFileName = BinaryDictionaryGetter.getTempFileName(wordlistId, context);
|
2012-10-23 06:34:03 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(TAG, "Can't open the temporary file", e);
|
2013-04-30 02:10:22 +00:00
|
|
|
return;
|
2012-10-23 06:34:03 +00:00
|
|
|
}
|
2011-08-24 07:32:29 +00:00
|
|
|
|
|
|
|
for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) {
|
2013-04-30 02:10:22 +00:00
|
|
|
final InputStream originalSourceStream;
|
2011-08-24 07:32:29 +00:00
|
|
|
InputStream inputStream = null;
|
2012-11-13 07:28:57 +00:00
|
|
|
InputStream uncompressedStream = null;
|
|
|
|
InputStream decryptedStream = null;
|
2013-01-18 10:21:14 +00:00
|
|
|
BufferedInputStream bufferedInputStream = null;
|
2011-08-30 12:46:33 +00:00
|
|
|
File outputFile = null;
|
2013-01-18 10:21:14 +00:00
|
|
|
BufferedOutputStream bufferedOutputStream = null;
|
2011-08-24 07:32:29 +00:00
|
|
|
AssetFileDescriptor afd = null;
|
2012-05-23 09:46:51 +00:00
|
|
|
final Uri wordListUri = wordListUriBuilder.build();
|
2011-08-24 07:32:29 +00:00
|
|
|
try {
|
|
|
|
// Open input.
|
2013-02-14 21:32:31 +00:00
|
|
|
afd = openAssetFileDescriptor(providerClient, wordListUri);
|
2011-08-24 07:32:29 +00:00
|
|
|
// If we can't open it at all, don't even try a number of times.
|
2013-04-30 02:10:22 +00:00
|
|
|
if (null == afd) return;
|
2011-08-24 07:32:29 +00:00
|
|
|
originalSourceStream = afd.createInputStream();
|
|
|
|
// Open output.
|
2012-07-04 08:41:55 +00:00
|
|
|
outputFile = new File(tempFileName);
|
|
|
|
// Just to be sure, delete the file. This may fail silently, and return false: this
|
|
|
|
// is the right thing to do, as we just want to continue anyway.
|
|
|
|
outputFile.delete();
|
2011-08-24 07:32:29 +00:00
|
|
|
// Get the appropriate decryption method for this try
|
|
|
|
switch (mode) {
|
|
|
|
case COMPRESSED_CRYPTED_COMPRESSED:
|
2012-11-13 07:28:57 +00:00
|
|
|
uncompressedStream =
|
|
|
|
FileTransforms.getUncompressedStream(originalSourceStream);
|
|
|
|
decryptedStream = FileTransforms.getDecryptedStream(uncompressedStream);
|
|
|
|
inputStream = FileTransforms.getUncompressedStream(decryptedStream);
|
2011-08-24 07:32:29 +00:00
|
|
|
break;
|
|
|
|
case CRYPTED_COMPRESSED:
|
2012-11-13 07:28:57 +00:00
|
|
|
decryptedStream = FileTransforms.getDecryptedStream(originalSourceStream);
|
|
|
|
inputStream = FileTransforms.getUncompressedStream(decryptedStream);
|
2011-08-24 07:32:29 +00:00
|
|
|
break;
|
|
|
|
case COMPRESSED_CRYPTED:
|
2012-11-13 07:28:57 +00:00
|
|
|
uncompressedStream =
|
|
|
|
FileTransforms.getUncompressedStream(originalSourceStream);
|
|
|
|
inputStream = FileTransforms.getDecryptedStream(uncompressedStream);
|
2011-08-24 07:32:29 +00:00
|
|
|
break;
|
|
|
|
case COMPRESSED_ONLY:
|
|
|
|
inputStream = FileTransforms.getUncompressedStream(originalSourceStream);
|
|
|
|
break;
|
|
|
|
case CRYPTED_ONLY:
|
|
|
|
inputStream = FileTransforms.getDecryptedStream(originalSourceStream);
|
|
|
|
break;
|
|
|
|
case NONE:
|
|
|
|
inputStream = originalSourceStream;
|
|
|
|
break;
|
2012-11-13 07:28:57 +00:00
|
|
|
}
|
2013-01-18 10:21:14 +00:00
|
|
|
bufferedInputStream = new BufferedInputStream(inputStream);
|
|
|
|
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
|
|
|
|
checkMagicAndCopyFileTo(bufferedInputStream, bufferedOutputStream);
|
|
|
|
bufferedOutputStream.flush();
|
|
|
|
bufferedOutputStream.close();
|
2015-02-21 02:30:23 +00:00
|
|
|
|
|
|
|
if (SHOULD_VERIFY_CHECKSUM) {
|
|
|
|
final String actualRawChecksum = MD5Calculator.checksum(
|
|
|
|
new BufferedInputStream(new FileInputStream(outputFile)));
|
|
|
|
Log.i(TAG, "Computed checksum for downloaded dictionary. Expected = "
|
|
|
|
+ rawChecksum + " ; actual = " + actualRawChecksum);
|
|
|
|
if (!TextUtils.isEmpty(rawChecksum) && !rawChecksum.equals(actualRawChecksum)) {
|
|
|
|
throw new IOException(
|
|
|
|
"Could not decode the file correctly : checksum differs");
|
|
|
|
}
|
2014-05-21 05:02:54 +00:00
|
|
|
}
|
2015-02-21 02:30:23 +00:00
|
|
|
|
2015-05-04 18:57:31 +00:00
|
|
|
// move the output file to the final staging file.
|
2012-07-04 08:41:55 +00:00
|
|
|
final File finalFile = new File(finalFileName);
|
2015-05-04 18:57:31 +00:00
|
|
|
FileUtils.renameTo(outputFile, finalFile);
|
|
|
|
|
2012-05-23 09:46:51 +00:00
|
|
|
wordListUriBuilder.appendQueryParameter(QUERY_PARAMETER_DELETE_RESULT,
|
|
|
|
QUERY_PARAMETER_SUCCESS);
|
2013-02-14 21:32:31 +00:00
|
|
|
if (0 >= providerClient.delete(wordListUriBuilder.build(), null, null)) {
|
2011-08-24 07:32:29 +00:00
|
|
|
Log.e(TAG, "Could not have the dictionary pack delete a word list");
|
|
|
|
}
|
2015-05-04 18:57:31 +00:00
|
|
|
Log.d(TAG, "Successfully copied file for wordlist ID " + wordlistId);
|
2011-08-24 07:32:29 +00:00
|
|
|
// Success! Close files (through the finally{} clause) and return.
|
2013-04-30 02:10:22 +00:00
|
|
|
return;
|
2011-08-24 07:32:29 +00:00
|
|
|
} catch (Exception e) {
|
2011-08-24 11:27:04 +00:00
|
|
|
if (DEBUG) {
|
2015-05-04 18:57:31 +00:00
|
|
|
Log.e(TAG, "Can't open word list in mode " + mode, e);
|
2011-08-24 11:27:04 +00:00
|
|
|
}
|
2011-08-30 12:46:33 +00:00
|
|
|
if (null != outputFile) {
|
|
|
|
// This may or may not fail. The file may not have been created if the
|
|
|
|
// exception was thrown before it could be. Hence, both failure and
|
|
|
|
// success are expected outcomes, so we don't check the return value.
|
|
|
|
outputFile.delete();
|
|
|
|
}
|
2011-08-24 07:32:29 +00:00
|
|
|
// Try the next method.
|
|
|
|
} finally {
|
|
|
|
// Ignore exceptions while closing files.
|
2013-05-30 09:53:56 +00:00
|
|
|
closeAssetFileDescriptorAndReportAnyException(afd);
|
|
|
|
closeCloseableAndReportAnyException(inputStream);
|
|
|
|
closeCloseableAndReportAnyException(uncompressedStream);
|
|
|
|
closeCloseableAndReportAnyException(decryptedStream);
|
|
|
|
closeCloseableAndReportAnyException(bufferedInputStream);
|
|
|
|
closeCloseableAndReportAnyException(bufferedOutputStream);
|
2011-08-24 03:45:52 +00:00
|
|
|
}
|
2011-08-24 07:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We could not copy the file at all. This is very unexpected.
|
|
|
|
// I'd rather not print the word list ID to the log out of security concerns
|
|
|
|
Log.e(TAG, "Could not copy a word list. Will not be able to use it.");
|
2012-05-23 09:46:51 +00:00
|
|
|
// If we can't copy it we should warn the dictionary provider so that it can mark it
|
|
|
|
// as invalid.
|
2013-12-13 08:09:16 +00:00
|
|
|
reportBrokenFileToDictionaryProvider(providerClient, clientId, wordlistId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean reportBrokenFileToDictionaryProvider(
|
|
|
|
final ContentProviderClient providerClient, final String clientId,
|
|
|
|
final String wordlistId) {
|
2013-02-14 21:32:31 +00:00
|
|
|
try {
|
2013-12-13 08:09:16 +00:00
|
|
|
final Uri.Builder wordListUriBuilder = getContentUriBuilderForType(clientId,
|
|
|
|
providerClient, QUERY_PATH_DATAFILE, wordlistId /* extraPath */);
|
|
|
|
wordListUriBuilder.appendQueryParameter(QUERY_PARAMETER_DELETE_RESULT,
|
|
|
|
QUERY_PARAMETER_FAILURE);
|
2013-02-14 21:32:31 +00:00
|
|
|
if (0 >= providerClient.delete(wordListUriBuilder.build(), null, null)) {
|
2013-12-13 08:09:16 +00:00
|
|
|
Log.e(TAG, "Unable to delete a word list.");
|
2013-02-14 21:32:31 +00:00
|
|
|
}
|
|
|
|
} catch (RemoteException e) {
|
2013-12-13 08:09:16 +00:00
|
|
|
Log.e(TAG, "Communication with the dictionary provider was cut", e);
|
|
|
|
return false;
|
2011-08-24 03:45:52 +00:00
|
|
|
}
|
2013-12-13 08:09:16 +00:00
|
|
|
return true;
|
2011-08-24 03:45:52 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 09:53:56 +00:00
|
|
|
// Ideally the two following methods should be merged, but AssetFileDescriptor does not
|
|
|
|
// implement Closeable although it does implement #close(), and Java does not have
|
|
|
|
// structural typing.
|
|
|
|
private static void closeAssetFileDescriptorAndReportAnyException(
|
|
|
|
final AssetFileDescriptor file) {
|
|
|
|
try {
|
|
|
|
if (null != file) file.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, "Exception while closing a file", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void closeCloseableAndReportAnyException(final Closeable file) {
|
|
|
|
try {
|
|
|
|
if (null != file) file.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, "Exception while closing a file", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 03:45:52 +00:00
|
|
|
/**
|
2015-05-04 18:57:31 +00:00
|
|
|
* Queries a content provider for word list data for some locale and stage the returned files
|
2011-03-14 18:46:15 +00:00
|
|
|
*
|
2011-08-24 03:45:52 +00:00
|
|
|
* This will query a content provider for word list data for a given locale, and copy the
|
|
|
|
* files locally so that they can be mmap'ed. This may overwrite previously cached word lists
|
2011-08-11 07:44:36 +00:00
|
|
|
* with newer versions if a newer version is made available by the content provider.
|
2011-03-14 18:46:15 +00:00
|
|
|
* @throw FileNotFoundException if the provider returns non-existent data.
|
|
|
|
* @throw IOException if the provider-returned data could not be read.
|
|
|
|
*/
|
2015-05-04 18:57:31 +00:00
|
|
|
public static void installDictToStagingFromContentProvider(final Locale locale,
|
2012-04-11 12:29:53 +00:00
|
|
|
final Context context, final boolean hasDefaultWordList) {
|
2013-05-30 00:23:07 +00:00
|
|
|
final ContentProviderClient providerClient;
|
|
|
|
try {
|
|
|
|
providerClient = context.getContentResolver().
|
2013-02-14 21:32:31 +00:00
|
|
|
acquireContentProviderClient(getProviderUriBuilder("").build());
|
2013-05-30 00:23:07 +00:00
|
|
|
} catch (final SecurityException e) {
|
|
|
|
Log.e(TAG, "No permission to communicate with the dictionary provider", e);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-14 21:32:31 +00:00
|
|
|
if (null == providerClient) {
|
|
|
|
Log.e(TAG, "Can't establish communication with the dictionary provider");
|
2013-04-30 02:10:22 +00:00
|
|
|
return;
|
2013-02-14 21:32:31 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
final List<WordListInfo> idList = getWordListWordListInfos(locale, context,
|
|
|
|
hasDefaultWordList);
|
|
|
|
for (WordListInfo id : idList) {
|
2015-05-04 18:57:31 +00:00
|
|
|
installWordListToStaging(id.mId, id.mLocale, id.mRawChecksum, providerClient,
|
|
|
|
context);
|
2011-08-10 09:57:01 +00:00
|
|
|
}
|
2013-02-14 21:32:31 +00:00
|
|
|
} finally {
|
|
|
|
providerClient.release();
|
2011-08-02 10:13:44 +00:00
|
|
|
}
|
2011-03-14 18:46:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 18:57:31 +00:00
|
|
|
/**
|
|
|
|
* Downloads the dictionary if it was never requested/used.
|
|
|
|
*
|
|
|
|
* @param locale locale to download
|
|
|
|
* @param context the context for resources and providers.
|
|
|
|
* @param hasDefaultWordList whether the default wordlist exists in the resources.
|
|
|
|
*/
|
|
|
|
public static void downloadDictIfNeverRequested(final Locale locale,
|
|
|
|
final Context context, final boolean hasDefaultWordList) {
|
|
|
|
getWordListWordListInfos(locale, context, hasDefaultWordList);
|
|
|
|
}
|
|
|
|
|
2011-03-14 18:46:15 +00:00
|
|
|
/**
|
2011-08-30 12:46:33 +00:00
|
|
|
* Copies the data in an input stream to a target file if the magic number matches.
|
|
|
|
*
|
|
|
|
* If the magic number does not match the expected value, this method throws an
|
|
|
|
* IOException. Other usual conditions for IOException or FileNotFoundException
|
|
|
|
* also apply.
|
|
|
|
*
|
2011-04-15 06:19:25 +00:00
|
|
|
* @param input the stream to be copied.
|
2012-03-29 06:07:53 +00:00
|
|
|
* @param output an output stream to copy the data to.
|
2011-03-14 18:46:15 +00:00
|
|
|
*/
|
2013-01-18 10:21:14 +00:00
|
|
|
public static void checkMagicAndCopyFileTo(final BufferedInputStream input,
|
|
|
|
final BufferedOutputStream output) throws FileNotFoundException, IOException {
|
2011-08-30 12:46:33 +00:00
|
|
|
// Check the magic number
|
2012-04-20 09:56:03 +00:00
|
|
|
final int length = MAGIC_NUMBER_VERSION_2.length;
|
|
|
|
final byte[] magicNumberBuffer = new byte[length];
|
|
|
|
final int readMagicNumberSize = input.read(magicNumberBuffer, 0, length);
|
|
|
|
if (readMagicNumberSize < length) {
|
2011-08-30 12:46:33 +00:00
|
|
|
throw new IOException("Less bytes to read than the magic number length");
|
|
|
|
}
|
2015-02-21 02:30:23 +00:00
|
|
|
if (SHOULD_VERIFY_MAGIC_NUMBER) {
|
|
|
|
if (!Arrays.equals(MAGIC_NUMBER_VERSION_2, magicNumberBuffer)) {
|
|
|
|
if (!Arrays.equals(MAGIC_NUMBER_VERSION_1, magicNumberBuffer)) {
|
|
|
|
throw new IOException("Wrong magic number for downloaded file");
|
|
|
|
}
|
2012-04-20 09:56:03 +00:00
|
|
|
}
|
2011-08-30 12:46:33 +00:00
|
|
|
}
|
2012-04-20 09:56:03 +00:00
|
|
|
output.write(magicNumberBuffer);
|
2011-08-30 12:46:33 +00:00
|
|
|
|
|
|
|
// Actually copy the file
|
2011-03-14 18:46:15 +00:00
|
|
|
final byte[] buffer = new byte[FILE_READ_BUFFER_SIZE];
|
2013-12-13 08:09:16 +00:00
|
|
|
for (int readBytes = input.read(buffer); readBytes >= 0; readBytes = input.read(buffer)) {
|
2011-03-14 18:46:15 +00:00
|
|
|
output.write(buffer, 0, readBytes);
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
2011-03-14 18:46:15 +00:00
|
|
|
input.close();
|
|
|
|
}
|
2013-02-02 07:30:11 +00:00
|
|
|
|
|
|
|
private static void reinitializeClientRecordInDictionaryContentProvider(final Context context,
|
2013-02-13 01:59:36 +00:00
|
|
|
final ContentProviderClient client, final String clientId) throws RemoteException {
|
2013-04-11 08:52:16 +00:00
|
|
|
final String metadataFileUri = MetadataFileUriGetter.getMetadataUri(context);
|
2015-06-25 00:40:52 +00:00
|
|
|
Log.i(TAG, "reinitializeClientRecordInDictionaryContentProvider() : MetadataFileUri = "
|
|
|
|
+ metadataFileUri);
|
2013-04-19 10:49:53 +00:00
|
|
|
final String metadataAdditionalId = MetadataFileUriGetter.getMetadataAdditionalId(context);
|
2013-02-02 07:30:11 +00:00
|
|
|
// Tell the content provider to reset all information about this client id
|
|
|
|
final Uri metadataContentUri = getProviderUriBuilder(clientId)
|
|
|
|
.appendPath(QUERY_PATH_METADATA)
|
|
|
|
.appendQueryParameter(QUERY_PARAMETER_PROTOCOL, QUERY_PARAMETER_PROTOCOL_VALUE)
|
|
|
|
.build();
|
2013-02-13 01:59:36 +00:00
|
|
|
client.delete(metadataContentUri, null, null);
|
2013-02-02 07:30:11 +00:00
|
|
|
// Update the metadata URI
|
|
|
|
final ContentValues metadataValues = new ContentValues();
|
|
|
|
metadataValues.put(INSERT_METADATA_CLIENT_ID_COLUMN, clientId);
|
|
|
|
metadataValues.put(INSERT_METADATA_METADATA_URI_COLUMN, metadataFileUri);
|
2013-04-19 10:49:53 +00:00
|
|
|
metadataValues.put(INSERT_METADATA_METADATA_ADDITIONAL_ID_COLUMN, metadataAdditionalId);
|
2013-02-13 01:59:36 +00:00
|
|
|
client.insert(metadataContentUri, metadataValues);
|
2013-02-03 17:21:54 +00:00
|
|
|
|
|
|
|
// 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);
|
2015-06-25 00:40:52 +00:00
|
|
|
Log.i(TAG, "reinitializeClientRecordInDictionaryContentProvider() : Insert " + info);
|
2013-02-13 01:59:36 +00:00
|
|
|
client.insert(Uri.withAppendedPath(dictionaryContentUriBase, info.mId),
|
2013-02-03 17:21:54 +00:00
|
|
|
info.toContentValues());
|
|
|
|
}
|
2015-06-25 00:40:52 +00:00
|
|
|
|
|
|
|
// Read from metadata file in resources to get the baseline dictionary info.
|
|
|
|
// This ensures we start with a sane list of available dictionaries.
|
|
|
|
final int metadataResourceId = context.getResources().getIdentifier("metadata",
|
|
|
|
"raw", DictionaryInfoUtils.RESOURCE_PACKAGE_NAME);
|
|
|
|
if (metadataResourceId == 0) {
|
|
|
|
Log.w(TAG, "Missing metadata.json resource");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InputStream inputStream = null;
|
|
|
|
try {
|
|
|
|
inputStream = context.getResources().openRawResource(metadataResourceId);
|
|
|
|
UpdateHandler.handleMetadata(context, inputStream, clientId);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.w(TAG, "Failed to read metadata.json from resources", e);
|
|
|
|
} finally {
|
|
|
|
if (inputStream != null) {
|
|
|
|
try {
|
|
|
|
inputStream.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, "Failed to close metadata.json", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-02 07:30:11 +00:00
|
|
|
}
|
2013-03-28 09:59:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a client record with the dictionary content provider.
|
|
|
|
*
|
|
|
|
* This merely acquires the content provider and calls
|
|
|
|
* #reinitializeClientRecordInDictionaryContentProvider.
|
|
|
|
*
|
|
|
|
* @param context the context for resources and providers.
|
|
|
|
* @param clientId the client ID to use.
|
|
|
|
*/
|
2013-12-13 08:09:16 +00:00
|
|
|
public static void initializeClientRecordHelper(final Context context, final String clientId) {
|
2013-03-28 09:59:19 +00:00
|
|
|
try {
|
|
|
|
final ContentProviderClient client = context.getContentResolver().
|
|
|
|
acquireContentProviderClient(getProviderUriBuilder("").build());
|
|
|
|
if (null == client) return;
|
|
|
|
reinitializeClientRecordInDictionaryContentProvider(context, client, clientId);
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
Log.e(TAG, "Cannot contact the dictionary content provider", e);
|
|
|
|
}
|
|
|
|
}
|
2011-03-14 18:46:15 +00:00
|
|
|
}
|