BinaryDictionaryUtils.java revision a785fa8edd7f7a1f91d45c5e66562d92cf5698af
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.latin.utils;
18
19import com.android.inputmethod.annotations.UsedForTesting;
20import com.android.inputmethod.latin.BinaryDictionary;
21import com.android.inputmethod.latin.makedict.DictionaryHeader;
22import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
23import com.android.inputmethod.latin.personalization.PersonalizationHelper;
24
25import java.io.File;
26import java.io.IOException;
27import java.util.Locale;
28import java.util.Map;
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32public final class BinaryDictionaryUtils {
33    private static final String TAG = BinaryDictionaryUtils.class.getSimpleName();
34
35    private BinaryDictionaryUtils() {
36        // This utility class is not publicly instantiable.
37    }
38
39    static {
40        JniUtils.loadNativeLibrary();
41    }
42
43    private static native boolean createEmptyDictFileNative(String filePath, long dictVersion,
44            String locale, String[] attributeKeyStringArray, String[] attributeValueStringArray);
45    private static native float calcNormalizedScoreNative(int[] before, int[] after, int score);
46    private static native int editDistanceNative(int[] before, int[] after);
47    private static native int setCurrentTimeForTestNative(int currentTime);
48
49    public static DictionaryHeader getHeader(final File dictFile)
50            throws IOException, UnsupportedFormatException {
51        return getHeaderWithOffsetAndLength(dictFile, 0 /* offset */, dictFile.length());
52    }
53
54    public static DictionaryHeader getHeaderWithOffsetAndLength(final File dictFile,
55            final long offset, final long length) throws IOException, UnsupportedFormatException {
56        // dictType is never used for reading the header. Passing an empty string.
57        final BinaryDictionary binaryDictionary = new BinaryDictionary(
58                dictFile.getAbsolutePath(), offset, length,
59                true /* useFullEditDistance */, null /* locale */, "" /* dictType */,
60                false /* isUpdatable */);
61        final DictionaryHeader header = binaryDictionary.getHeader();
62        binaryDictionary.close();
63        if (header == null) {
64            throw new IOException();
65        }
66        return header;
67    }
68
69    public static boolean renameDict(final File dictFile, final File newDictFile) {
70        if (dictFile.isFile()) {
71            return dictFile.renameTo(newDictFile);
72        } else if (dictFile.isDirectory()) {
73            final String dictName = dictFile.getName();
74            final String newDictName = newDictFile.getName();
75            if (newDictFile.exists()) {
76                return false;
77            }
78            for (final File file : dictFile.listFiles()) {
79                if (!file.isFile()) {
80                    continue;
81                }
82                final String fileName = file.getName();
83                final String newFileName = fileName.replaceFirst(
84                        Pattern.quote(dictName), Matcher.quoteReplacement(newDictName));
85                if (!file.renameTo(new File(dictFile, newFileName))) {
86                    return false;
87                }
88            }
89            return dictFile.renameTo(newDictFile);
90        }
91        return false;
92    }
93
94    public static boolean createEmptyDictFile(final String filePath, final long dictVersion,
95            final Locale locale, final Map<String, String> attributeMap) {
96        final String[] keyArray = new String[attributeMap.size()];
97        final String[] valueArray = new String[attributeMap.size()];
98        int index = 0;
99        for (final String key : attributeMap.keySet()) {
100            keyArray[index] = key;
101            valueArray[index] = attributeMap.get(key);
102            index++;
103        }
104        return createEmptyDictFileNative(filePath, dictVersion, locale.toString(), keyArray,
105                valueArray);
106    }
107
108    public static float calcNormalizedScore(final String before, final String after,
109            final int score) {
110        return calcNormalizedScoreNative(StringUtils.toCodePointArray(before),
111                StringUtils.toCodePointArray(after), score);
112    }
113
114    public static int editDistance(final String before, final String after) {
115        if (before == null || after == null) {
116            throw new IllegalArgumentException();
117        }
118        return editDistanceNative(StringUtils.toCodePointArray(before),
119                StringUtils.toCodePointArray(after));
120    }
121
122    /**
123     * Control the current time to be used in the native code. If currentTime >= 0, this method sets
124     * the current time and gets into test mode.
125     * In test mode, set timestamp is used as the current time in the native code.
126     * If currentTime < 0, quit the test mode and returns to using time() to get the current time.
127     *
128     * @param currentTime seconds since the unix epoch
129     * @return current time got in the native code.
130     */
131    @UsedForTesting
132    public static int setCurrentTimeForTest(final int currentTime) {
133        final int currentNativeTimestamp = setCurrentTimeForTestNative(currentTime);
134        PersonalizationHelper.currentTimeChangedForTesting(currentNativeTimestamp);
135        return currentNativeTimestamp;
136    }
137}
138