UserBinaryDictionary.java revision a410cb48eab0cd75aa27e20f60e47a29a59fb9ff
1/*
2 * Copyright (C) 2012 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;
18
19import android.content.ContentProviderClient;
20import android.content.ContentResolver;
21import android.content.ContentUris;
22import android.content.Context;
23import android.database.ContentObserver;
24import android.database.Cursor;
25import android.net.Uri;
26import android.os.Build;
27import android.provider.UserDictionary.Words;
28import android.text.TextUtils;
29
30import com.android.inputmethod.compat.UserDictionaryCompatUtils;
31import com.android.inputmethod.latin.utils.LocaleUtils;
32import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
33
34import java.util.Arrays;
35import java.util.Locale;
36
37/**
38 * An expandable dictionary that stores the words in the user dictionary provider into a binary
39 * dictionary file to use it from native code.
40 */
41public class UserBinaryDictionary extends ExpandableBinaryDictionary {
42
43    // The user dictionary provider uses an empty string to mean "all languages".
44    private static final String USER_DICTIONARY_ALL_LANGUAGES = "";
45    private static final int HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY = 250;
46    private static final int LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY = 160;
47
48    // TODO: use Words.SHORTCUT when we target JellyBean or above
49    final static String SHORTCUT = "shortcut";
50    private static final String[] PROJECTION_QUERY;
51    static {
52        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
53            PROJECTION_QUERY = new String[] {
54                Words.WORD,
55                SHORTCUT,
56                Words.FREQUENCY,
57            };
58        } else {
59            PROJECTION_QUERY = new String[] {
60                Words.WORD,
61                Words.FREQUENCY,
62            };
63        }
64    }
65
66    private static final String NAME = "userunigram";
67
68    private ContentObserver mObserver;
69    final private String mLocale;
70    final private boolean mAlsoUseMoreRestrictiveLocales;
71
72    public UserBinaryDictionary(final Context context, final String locale) {
73        this(context, locale, false);
74    }
75
76    public UserBinaryDictionary(final Context context, final String locale,
77            final boolean alsoUseMoreRestrictiveLocales) {
78        super(context, getFilenameWithLocale(NAME, locale), Dictionary.TYPE_USER);
79        if (null == locale) throw new NullPointerException(); // Catch the error earlier
80        if (SubtypeLocaleUtils.NO_LANGUAGE.equals(locale)) {
81            // If we don't have a locale, insert into the "all locales" user dictionary.
82            mLocale = USER_DICTIONARY_ALL_LANGUAGES;
83        } else {
84            mLocale = locale;
85        }
86        mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales;
87        // Perform a managed query. The Activity will handle closing and re-querying the cursor
88        // when needed.
89        ContentResolver cres = context.getContentResolver();
90
91        mObserver = new ContentObserver(null) {
92            @Override
93            public void onChange(final boolean self) {
94                // This hook is deprecated as of API level 16 (Build.VERSION_CODES.JELLY_BEAN),
95                // but should still be supported for cases where the IME is running on an older
96                // version of the platform.
97                onChange(self, null);
98            }
99            // The following hook is only available as of API level 16
100            // (Build.VERSION_CODES.JELLY_BEAN), and as such it will only work on JellyBean+
101            // devices. On older versions of the platform, the hook above will be called instead.
102            @Override
103            public void onChange(final boolean self, final Uri uri) {
104                setRequiresReload(true);
105                // We want to report back to Latin IME in case the user just entered the word.
106                // If the user changed the word in the dialog box, then we want to replace
107                // what was entered in the text field.
108                if (null == uri || !(context instanceof LatinIME)) return;
109                final long changedRowId = ContentUris.parseId(uri);
110                if (-1 == changedRowId) return; // Unknown content... Not sure why we're here
111                final String changedWord = getChangedWordForUri(uri);
112                ((LatinIME)context).onWordAddedToUserDictionary(changedWord);
113            }
114        };
115        cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
116
117        loadDictionary();
118    }
119
120    private String getChangedWordForUri(final Uri uri) {
121        final Cursor cursor = mContext.getContentResolver().query(uri,
122                PROJECTION_QUERY, null, null, null);
123        if (cursor == null) return null;
124        try {
125            if (!cursor.moveToFirst()) return null;
126            final int indexWord = cursor.getColumnIndex(Words.WORD);
127            return cursor.getString(indexWord);
128        } finally {
129            cursor.close();
130        }
131    }
132
133    @Override
134    public synchronized void close() {
135        if (mObserver != null) {
136            mContext.getContentResolver().unregisterContentObserver(mObserver);
137            mObserver = null;
138        }
139        super.close();
140    }
141
142    @Override
143    public void loadDictionaryAsync() {
144        // Split the locale. For example "en" => ["en"], "de_DE" => ["de", "DE"],
145        // "en_US_foo_bar_qux" => ["en", "US", "foo_bar_qux"] because of the limit of 3.
146        // This is correct for locale processing.
147        // For this example, we'll look at the "en_US_POSIX" case.
148        final String[] localeElements =
149                TextUtils.isEmpty(mLocale) ? new String[] {} : mLocale.split("_", 3);
150        final int length = localeElements.length;
151
152        final StringBuilder request = new StringBuilder("(locale is NULL)");
153        String localeSoFar = "";
154        // At start, localeElements = ["en", "US", "POSIX"] ; localeSoFar = "" ;
155        // and request = "(locale is NULL)"
156        for (int i = 0; i < length; ++i) {
157            // i | localeSoFar    | localeElements
158            // 0 | ""             | ["en", "US", "POSIX"]
159            // 1 | "en_"          | ["en", "US", "POSIX"]
160            // 2 | "en_US_"       | ["en", "en_US", "POSIX"]
161            localeElements[i] = localeSoFar + localeElements[i];
162            localeSoFar = localeElements[i] + "_";
163            // i | request
164            // 0 | "(locale is NULL)"
165            // 1 | "(locale is NULL) or (locale=?)"
166            // 2 | "(locale is NULL) or (locale=?) or (locale=?)"
167            request.append(" or (locale=?)");
168        }
169        // At the end, localeElements = ["en", "en_US", "en_US_POSIX"]; localeSoFar = en_US_POSIX_"
170        // and request = "(locale is NULL) or (locale=?) or (locale=?) or (locale=?)"
171
172        final String[] requestArguments;
173        // If length == 3, we already have all the arguments we need (common prefix is meaningless
174        // inside variants
175        if (mAlsoUseMoreRestrictiveLocales && length < 3) {
176            request.append(" or (locale like ?)");
177            // The following creates an array with one more (null) position
178            final String[] localeElementsWithMoreRestrictiveLocalesIncluded =
179                    Arrays.copyOf(localeElements, length + 1);
180            localeElementsWithMoreRestrictiveLocalesIncluded[length] =
181                    localeElements[length - 1] + "_%";
182            requestArguments = localeElementsWithMoreRestrictiveLocalesIncluded;
183            // If for example localeElements = ["en"]
184            // then requestArguments = ["en", "en_%"]
185            // and request = (locale is NULL) or (locale=?) or (locale like ?)
186            // If localeElements = ["en", "en_US"]
187            // then requestArguments = ["en", "en_US", "en_US_%"]
188        } else {
189            requestArguments = localeElements;
190        }
191        final Cursor cursor = mContext.getContentResolver().query(
192                Words.CONTENT_URI, PROJECTION_QUERY, request.toString(), requestArguments, null);
193        try {
194            addWords(cursor);
195        } finally {
196            if (null != cursor) cursor.close();
197        }
198    }
199
200    public boolean isEnabled() {
201        final ContentResolver cr = mContext.getContentResolver();
202        final ContentProviderClient client = cr.acquireContentProviderClient(Words.CONTENT_URI);
203        if (client != null) {
204            client.release();
205            return true;
206        } else {
207            return false;
208        }
209    }
210
211    /**
212     * Adds a word to the user dictionary and makes it persistent.
213     *
214     * @param word the word to add. If the word is capitalized, then the dictionary will
215     * recognize it as a capitalized word when searched.
216     */
217    public synchronized void addWordToUserDictionary(final String word) {
218        // Update the user dictionary provider
219        final Locale locale;
220        if (USER_DICTIONARY_ALL_LANGUAGES == mLocale) {
221            locale = null;
222        } else {
223            locale = LocaleUtils.constructLocaleFromString(mLocale);
224        }
225        UserDictionaryCompatUtils.addWord(mContext, word,
226                HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY, null, locale);
227    }
228
229    private int scaleFrequencyFromDefaultToLatinIme(final int defaultFrequency) {
230        // The default frequency for the user dictionary is 250 for historical reasons.
231        // Latin IME considers a good value for the default user dictionary frequency
232        // is about 160 considering the scale we use. So we are scaling down the values.
233        if (defaultFrequency > Integer.MAX_VALUE / LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY) {
234            return (defaultFrequency / HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY)
235                    * LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY;
236        } else {
237            return (defaultFrequency * LATINIME_DEFAULT_USER_DICTIONARY_FREQUENCY)
238                    / HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY;
239        }
240    }
241
242    private void addWords(final Cursor cursor) {
243        final boolean hasShortcutColumn = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
244        if (cursor == null) return;
245        if (cursor.moveToFirst()) {
246            final int indexWord = cursor.getColumnIndex(Words.WORD);
247            final int indexShortcut = hasShortcutColumn ? cursor.getColumnIndex(SHORTCUT) : 0;
248            final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY);
249            while (!cursor.isAfterLast()) {
250                final String word = cursor.getString(indexWord);
251                final String shortcut = hasShortcutColumn ? cursor.getString(indexShortcut) : null;
252                final int frequency = cursor.getInt(indexFrequency);
253                final int adjustedFrequency = scaleFrequencyFromDefaultToLatinIme(frequency);
254                // Safeguard against adding really long words.
255                if (word.length() < MAX_WORD_LENGTH) {
256                    super.addWord(word, null, adjustedFrequency, false /* isNotAWord */);
257                }
258                if (null != shortcut && shortcut.length() < MAX_WORD_LENGTH) {
259                    super.addWord(shortcut, word, adjustedFrequency, true /* isNotAWord */);
260                }
261                cursor.moveToNext();
262            }
263        }
264    }
265
266    @Override
267    protected boolean hasContentChanged() {
268        return true;
269    }
270
271    @Override
272    protected boolean needsToReloadBeforeWriting() {
273        return true;
274    }
275}
276