1/*
2 * Copyright (C) 2013 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.compat;
18
19import android.annotation.TargetApi;
20import android.content.Context;
21import android.os.Build;
22import android.provider.UserDictionary;
23
24import java.util.Locale;
25
26public final class UserDictionaryCompatUtils {
27    @SuppressWarnings("deprecation")
28    public static void addWord(final Context context, final String word,
29            final int freq, final String shortcut, final Locale locale) {
30        if (BuildCompatUtils.EFFECTIVE_SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
31            addWordWithShortcut(context, word, freq, shortcut, locale);
32            return;
33        }
34        // Fall back to the pre-JellyBean method.
35        final Locale currentLocale = context.getResources().getConfiguration().locale;
36        final int localeType = currentLocale.equals(locale)
37                ? UserDictionary.Words.LOCALE_TYPE_CURRENT : UserDictionary.Words.LOCALE_TYPE_ALL;
38        UserDictionary.Words.addWord(context, word, freq, localeType);
39    }
40
41    // {@link UserDictionary.Words#addWord(Context,String,int,String,Locale)} was introduced
42    // in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
43    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
44    private static void addWordWithShortcut(final Context context, final String word,
45            final int freq, final String shortcut, final Locale locale) {
46        UserDictionary.Words.addWord(context, word, freq, shortcut, locale);
47    }
48}
49
50