UserDictionaryCompatUtils.java revision 03a1c442aeddbd511be92e11465b6206d2ca976c
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.content.Context; 20import android.provider.UserDictionary.Words; 21 22import java.lang.reflect.Method; 23import java.util.Locale; 24 25public final class UserDictionaryCompatUtils { 26 // UserDictionary.Words#addWord(Context, String, int, String, Locale) was introduced 27 // in API level 16 (Build.VERSION_CODES.JELLY_BEAN). 28 private static final Method METHOD_addWord = CompatUtils.getMethod(Words.class, "addWord", 29 Context.class, String.class, int.class, String.class, Locale.class); 30 31 @SuppressWarnings("deprecation") 32 public static void addWord(final Context context, final String word, 33 final int freq, final String shortcut, final Locale locale) { 34 if (hasNewerAddWord()) { 35 CompatUtils.invoke(Words.class, null, METHOD_addWord, context, word, freq, shortcut, 36 locale); 37 } else { 38 // Fall back to the pre-JellyBean method. 39 final int localeType; 40 if (null == locale) { 41 localeType = Words.LOCALE_TYPE_ALL; 42 } else { 43 final Locale currentLocale = context.getResources().getConfiguration().locale; 44 if (locale.equals(currentLocale)) { 45 localeType = Words.LOCALE_TYPE_CURRENT; 46 } else { 47 localeType = Words.LOCALE_TYPE_ALL; 48 } 49 } 50 Words.addWord(context, word, freq, localeType); 51 } 52 } 53 54 public static final boolean hasNewerAddWord() { 55 return null != METHOD_addWord; 56 } 57} 58