BinaryDictionaryFileDumper.java revision 3abac7e28e9fbfc96b146abc0028fb5a2a5973da
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.res.AssetFileDescriptor;
22import android.database.Cursor;
23import android.net.Uri;
24import android.text.TextUtils;
25import android.util.Log;
26
27import java.io.BufferedInputStream;
28import java.io.File;
29import java.io.FileNotFoundException;
30import java.io.FileOutputStream;
31import java.io.IOException;
32import java.io.InputStream;
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.Collections;
36import java.util.List;
37import java.util.Locale;
38
39/**
40 * Group class for static methods to help with creation and getting of the binary dictionary
41 * file from the dictionary provider
42 */
43public class BinaryDictionaryFileDumper {
44    private static final String TAG = BinaryDictionaryFileDumper.class.getSimpleName();
45    private static final boolean DEBUG = false;
46
47    /**
48     * The size of the temporary buffer to copy files.
49     */
50    private static final int FILE_READ_BUFFER_SIZE = 1024;
51    // TODO: make the following data common with the native code
52    private static final byte[] MAGIC_NUMBER_VERSION_1 =
53            new byte[] { (byte)0x78, (byte)0xB1, (byte)0x00, (byte)0x00 };
54    private static final byte[] MAGIC_NUMBER_VERSION_2 =
55            new byte[] { (byte)0x9B, (byte)0xC1, (byte)0x3A, (byte)0xFE };
56
57    private static final String DICTIONARY_PROJECTION[] = { "id" };
58
59    public static final String QUERY_PARAMETER_MAY_PROMPT_USER = "mayPrompt";
60    public static final String QUERY_PARAMETER_TRUE = "true";
61    public static final String QUERY_PARAMETER_DELETE_RESULT = "result";
62    public static final String QUERY_PARAMETER_SUCCESS = "success";
63    public static final String QUERY_PARAMETER_FAILURE = "failure";
64
65    // Prevents this class to be accidentally instantiated.
66    private BinaryDictionaryFileDumper() {
67    }
68
69    /**
70     * Returns a URI builder pointing to the dictionary pack.
71     *
72     * This creates a URI builder able to build a URI pointing to the dictionary
73     * pack content provider for a specific dictionary id.
74     */
75    private static Uri.Builder getProviderUriBuilder(final String path) {
76        return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
77                .authority(BinaryDictionary.DICTIONARY_PACK_AUTHORITY).appendPath(
78                        path);
79    }
80
81    /**
82     * Queries a content provider for the list of word lists for a specific locale
83     * available to copy into Latin IME.
84     */
85    private static List<WordListInfo> getWordListWordListInfos(final Locale locale,
86            final Context context, final boolean hasDefaultWordList) {
87        final ContentResolver resolver = context.getContentResolver();
88        final Uri.Builder builder = getProviderUriBuilder(locale.toString());
89        if (!hasDefaultWordList) {
90            builder.appendQueryParameter(QUERY_PARAMETER_MAY_PROMPT_USER, QUERY_PARAMETER_TRUE);
91        }
92        final Uri dictionaryPackUri = builder.build();
93
94        final Cursor c = resolver.query(dictionaryPackUri, DICTIONARY_PROJECTION, null, null, null);
95        if (null == c) return Collections.<WordListInfo>emptyList();
96        if (c.getCount() <= 0 || !c.moveToFirst()) {
97            c.close();
98            return Collections.<WordListInfo>emptyList();
99        }
100
101        try {
102            final List<WordListInfo> list = new ArrayList<WordListInfo>();
103            do {
104                final String wordListId = c.getString(0);
105                final String wordListLocale = c.getString(1);
106                if (TextUtils.isEmpty(wordListId)) continue;
107                list.add(new WordListInfo(wordListId, wordListLocale));
108            } while (c.moveToNext());
109            c.close();
110            return list;
111        } catch (Exception e) {
112            // Just in case we hit a problem in communication with the dictionary pack.
113            // We don't want to die.
114            Log.e(TAG, "Exception communicating with the dictionary pack : " + e);
115            return Collections.<WordListInfo>emptyList();
116        }
117    }
118
119
120    /**
121     * Helper method to encapsulate exception handling.
122     */
123    private static AssetFileDescriptor openAssetFileDescriptor(final ContentResolver resolver,
124            final Uri uri) {
125        try {
126            return resolver.openAssetFileDescriptor(uri, "r");
127        } catch (FileNotFoundException e) {
128            // I don't want to log the word list URI here for security concerns
129            Log.e(TAG, "Could not find a word list from the dictionary provider.");
130            return null;
131        }
132    }
133
134    /**
135     * Caches a word list the id of which is passed as an argument. This will write the file
136     * to the cache file name designated by its id and locale, overwriting it if already present
137     * and creating it (and its containing directory) if necessary.
138     */
139    private static AssetFileAddress cacheWordList(final String id, final String locale,
140            final ContentResolver resolver, final Context context) {
141
142        final int COMPRESSED_CRYPTED_COMPRESSED = 0;
143        final int CRYPTED_COMPRESSED = 1;
144        final int COMPRESSED_CRYPTED = 2;
145        final int COMPRESSED_ONLY = 3;
146        final int CRYPTED_ONLY = 4;
147        final int NONE = 5;
148        final int MODE_MIN = COMPRESSED_CRYPTED_COMPRESSED;
149        final int MODE_MAX = NONE;
150
151        final Uri.Builder wordListUriBuilder = getProviderUriBuilder(id);
152        final String outputFileName = BinaryDictionaryGetter.getCacheFileName(id, locale, context);
153
154        for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) {
155            InputStream originalSourceStream = null;
156            InputStream inputStream = null;
157            File outputFile = null;
158            FileOutputStream outputStream = null;
159            AssetFileDescriptor afd = null;
160            final Uri wordListUri = wordListUriBuilder.build();
161            try {
162                // Open input.
163                afd = openAssetFileDescriptor(resolver, wordListUri);
164                // If we can't open it at all, don't even try a number of times.
165                if (null == afd) return null;
166                originalSourceStream = afd.createInputStream();
167                // Open output.
168                outputFile = new File(outputFileName);
169                outputStream = new FileOutputStream(outputFile);
170                // Get the appropriate decryption method for this try
171                switch (mode) {
172                    case COMPRESSED_CRYPTED_COMPRESSED:
173                        inputStream = FileTransforms.getUncompressedStream(
174                                FileTransforms.getDecryptedStream(
175                                        FileTransforms.getUncompressedStream(
176                                                originalSourceStream)));
177                        break;
178                    case CRYPTED_COMPRESSED:
179                        inputStream = FileTransforms.getUncompressedStream(
180                                FileTransforms.getDecryptedStream(originalSourceStream));
181                        break;
182                    case COMPRESSED_CRYPTED:
183                        inputStream = FileTransforms.getDecryptedStream(
184                                FileTransforms.getUncompressedStream(originalSourceStream));
185                        break;
186                    case COMPRESSED_ONLY:
187                        inputStream = FileTransforms.getUncompressedStream(originalSourceStream);
188                        break;
189                    case CRYPTED_ONLY:
190                        inputStream = FileTransforms.getDecryptedStream(originalSourceStream);
191                        break;
192                    case NONE:
193                        inputStream = originalSourceStream;
194                        break;
195                    }
196                checkMagicAndCopyFileTo(new BufferedInputStream(inputStream), outputStream);
197                wordListUriBuilder.appendQueryParameter(QUERY_PARAMETER_DELETE_RESULT,
198                        QUERY_PARAMETER_SUCCESS);
199                if (0 >= resolver.delete(wordListUriBuilder.build(), null, null)) {
200                    Log.e(TAG, "Could not have the dictionary pack delete a word list");
201                }
202                BinaryDictionaryGetter.removeFilesWithIdExcept(context, id, outputFile);
203                // Success! Close files (through the finally{} clause) and return.
204                return AssetFileAddress.makeFromFileName(outputFileName);
205            } catch (Exception e) {
206                if (DEBUG) {
207                    Log.i(TAG, "Can't open word list in mode " + mode + " : " + e);
208                }
209                if (null != outputFile) {
210                    // This may or may not fail. The file may not have been created if the
211                    // exception was thrown before it could be. Hence, both failure and
212                    // success are expected outcomes, so we don't check the return value.
213                    outputFile.delete();
214                }
215                // Try the next method.
216            } finally {
217                // Ignore exceptions while closing files.
218                try {
219                    // inputStream.close() will close afd, we should not call afd.close().
220                    if (null != inputStream) inputStream.close();
221                } catch (Exception e) {
222                    Log.e(TAG, "Exception while closing a cross-process file descriptor : " + e);
223                }
224                try {
225                    if (null != outputStream) outputStream.close();
226                } catch (Exception e) {
227                    Log.e(TAG, "Exception while closing a file : " + e);
228                }
229            }
230        }
231
232        // We could not copy the file at all. This is very unexpected.
233        // I'd rather not print the word list ID to the log out of security concerns
234        Log.e(TAG, "Could not copy a word list. Will not be able to use it.");
235        // If we can't copy it we should warn the dictionary provider so that it can mark it
236        // as invalid.
237        wordListUriBuilder.appendQueryParameter(QUERY_PARAMETER_DELETE_RESULT,
238                QUERY_PARAMETER_FAILURE);
239        if (0 >= resolver.delete(wordListUriBuilder.build(), null, null)) {
240            Log.e(TAG, "In addition, we were unable to delete it.");
241        }
242        return null;
243    }
244
245    /**
246     * Queries a content provider for word list data for some locale and cache the returned files
247     *
248     * This will query a content provider for word list data for a given locale, and copy the
249     * files locally so that they can be mmap'ed. This may overwrite previously cached word lists
250     * with newer versions if a newer version is made available by the content provider.
251     * @returns the addresses of the word list files, or null if no data could be obtained.
252     * @throw FileNotFoundException if the provider returns non-existent data.
253     * @throw IOException if the provider-returned data could not be read.
254     */
255    public static List<AssetFileAddress> cacheWordListsFromContentProvider(final Locale locale,
256            final Context context, final boolean hasDefaultWordList) {
257        final ContentResolver resolver = context.getContentResolver();
258        final List<WordListInfo> idList = getWordListWordListInfos(locale, context,
259                hasDefaultWordList);
260        final List<AssetFileAddress> fileAddressList = new ArrayList<AssetFileAddress>();
261        for (WordListInfo id : idList) {
262            final AssetFileAddress afd = cacheWordList(id.mId, id.mLocale, resolver, context);
263            if (null != afd) {
264                fileAddressList.add(afd);
265            }
266        }
267        return fileAddressList;
268    }
269
270    /**
271     * Copies the data in an input stream to a target file if the magic number matches.
272     *
273     * If the magic number does not match the expected value, this method throws an
274     * IOException. Other usual conditions for IOException or FileNotFoundException
275     * also apply.
276     *
277     * @param input the stream to be copied.
278     * @param output an output stream to copy the data to.
279     */
280    private static void checkMagicAndCopyFileTo(final BufferedInputStream input,
281            final FileOutputStream output) throws FileNotFoundException, IOException {
282        // Check the magic number
283        final int length = MAGIC_NUMBER_VERSION_2.length;
284        final byte[] magicNumberBuffer = new byte[length];
285        final int readMagicNumberSize = input.read(magicNumberBuffer, 0, length);
286        if (readMagicNumberSize < length) {
287            throw new IOException("Less bytes to read than the magic number length");
288        }
289        if (!Arrays.equals(MAGIC_NUMBER_VERSION_2, magicNumberBuffer)) {
290            if (!Arrays.equals(MAGIC_NUMBER_VERSION_1, magicNumberBuffer)) {
291                throw new IOException("Wrong magic number for downloaded file");
292            }
293        }
294        output.write(magicNumberBuffer);
295
296        // Actually copy the file
297        final byte[] buffer = new byte[FILE_READ_BUFFER_SIZE];
298        for (int readBytes = input.read(buffer); readBytes >= 0; readBytes = input.read(buffer))
299            output.write(buffer, 0, readBytes);
300        input.close();
301    }
302}
303