1 /* 2 * Copyright (C) 2008 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 17 package android.provider; 18 19 import java.util.Locale; 20 21 import android.content.ContentResolver; 22 import android.content.ContentValues; 23 import android.content.Context; 24 import android.net.Uri; 25 import android.text.TextUtils; 26 27 /** 28 * A provider of user defined words for input methods to use for predictive text input. 29 * Applications and input methods may add words into the dictionary. Words can have associated 30 * frequency information and locale information. 31 */ 32 public class UserDictionary { 33 34 /** Authority string for this provider. */ 35 public static final String AUTHORITY = "user_dictionary"; 36 37 /** 38 * The content:// style URL for this provider 39 */ 40 public static final Uri CONTENT_URI = 41 Uri.parse("content://" + AUTHORITY); 42 43 private static final int FREQUENCY_MIN = 0; 44 private static final int FREQUENCY_MAX = 255; 45 46 /** 47 * Contains the user defined words. 48 */ 49 public static class Words implements BaseColumns { 50 /** 51 * The content:// style URL for this table 52 */ 53 public static final Uri CONTENT_URI = 54 Uri.parse("content://" + AUTHORITY + "/words"); 55 56 /** 57 * The MIME type of {@link #CONTENT_URI} providing a directory of words. 58 */ 59 public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.userword"; 60 61 /** 62 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single word. 63 */ 64 public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.userword"; 65 66 public static final String _ID = BaseColumns._ID; 67 68 /** 69 * The word column. 70 * <p>TYPE: TEXT</p> 71 */ 72 public static final String WORD = "word"; 73 74 /** 75 * The frequency column. A value between 1 and 255. Higher values imply higher frequency. 76 * <p>TYPE: INTEGER</p> 77 */ 78 public static final String FREQUENCY = "frequency"; 79 80 /** 81 * The locale that this word belongs to. Null if it pertains to all 82 * locales. Locale is as defined by the string returned by Locale.toString(). 83 * <p>TYPE: TEXT</p> 84 */ 85 public static final String LOCALE = "locale"; 86 87 /** 88 * The uid of the application that inserted the word. 89 * <p>TYPE: INTEGER</p> 90 */ 91 public static final String APP_ID = "appid"; 92 93 /** 94 * An optional shortcut for this word. When the shortcut is typed, supporting IMEs should 95 * suggest the word in this row as an alternate spelling too. 96 */ 97 public static final String SHORTCUT = "shortcut"; 98 99 /** 100 * @deprecated Use {@link #addWord(Context, String, int, String, Locale)}. 101 */ 102 @Deprecated 103 public static final int LOCALE_TYPE_ALL = 0; 104 105 /** 106 * @deprecated Use {@link #addWord(Context, String, int, String, Locale)}. 107 */ 108 @Deprecated 109 public static final int LOCALE_TYPE_CURRENT = 1; 110 111 /** 112 * Sort by descending order of frequency. 113 */ 114 public static final String DEFAULT_SORT_ORDER = FREQUENCY + " DESC"; 115 116 /** Adds a word to the dictionary, with the given frequency and the specified 117 * specified locale type. 118 * 119 * @deprecated Please use 120 * {@link #addWord(Context, String, int, String, Locale)} instead. 121 * 122 * @param context the current application context 123 * @param word the word to add to the dictionary. This should not be null or 124 * empty. 125 * @param localeType the locale type for this word. It should be one of 126 * {@link #LOCALE_TYPE_ALL} or {@link #LOCALE_TYPE_CURRENT}. 127 */ 128 @Deprecated addWord(Context context, String word, int frequency, int localeType)129 public static void addWord(Context context, String word, 130 int frequency, int localeType) { 131 132 if (localeType != LOCALE_TYPE_ALL && localeType != LOCALE_TYPE_CURRENT) { 133 return; 134 } 135 136 final Locale locale; 137 138 if (localeType == LOCALE_TYPE_CURRENT) { 139 locale = Locale.getDefault(); 140 } else { 141 locale = null; 142 } 143 144 addWord(context, word, frequency, null, locale); 145 } 146 147 /** Adds a word to the dictionary, with the given frequency and the specified 148 * locale type. 149 * 150 * @param context the current application context 151 * @param word the word to add to the dictionary. This should not be null or 152 * empty. 153 * @param shortcut optional shortcut spelling for this word. When the shortcut 154 * is typed, the word may be suggested by applications that support it. May be null. 155 * @param locale the locale to insert the word for, or null to insert the word 156 * for all locales. 157 */ addWord(Context context, String word, int frequency, String shortcut, Locale locale)158 public static void addWord(Context context, String word, 159 int frequency, String shortcut, Locale locale) { 160 final ContentResolver resolver = context.getContentResolver(); 161 162 if (TextUtils.isEmpty(word)) { 163 return; 164 } 165 166 if (frequency < FREQUENCY_MIN) frequency = FREQUENCY_MIN; 167 if (frequency > FREQUENCY_MAX) frequency = FREQUENCY_MAX; 168 169 final int COLUMN_COUNT = 5; 170 ContentValues values = new ContentValues(COLUMN_COUNT); 171 172 values.put(WORD, word); 173 values.put(FREQUENCY, frequency); 174 values.put(LOCALE, null == locale ? null : locale.toString()); 175 values.put(APP_ID, 0); // TODO: Get App UID 176 values.put(SHORTCUT, shortcut); 177 178 Uri result = resolver.insert(CONTENT_URI, values); 179 // It's ok if the insert doesn't succeed because the word 180 // already exists. 181 } 182 } 183 } 184