• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /**
44      * Contains the user defined words.
45      */
46     public static class Words implements BaseColumns {
47         /**
48          * The content:// style URL for this table
49          */
50         public static final Uri CONTENT_URI =
51                 Uri.parse("content://" + AUTHORITY + "/words");
52 
53         /**
54          * The MIME type of {@link #CONTENT_URI} providing a directory of words.
55          */
56         public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.userword";
57 
58         /**
59          * The MIME type of a {@link #CONTENT_URI} sub-directory of a single word.
60          */
61         public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.userword";
62 
63         public static final String _ID = BaseColumns._ID;
64 
65         /**
66          * The word column.
67          * <p>TYPE: TEXT</p>
68          */
69         public static final String WORD = "word";
70 
71         /**
72          * The frequency column. A value between 1 and 255. Higher values imply higher frequency.
73          * <p>TYPE: INTEGER</p>
74          */
75         public static final String FREQUENCY = "frequency";
76 
77         /**
78          * The locale that this word belongs to. Null if it pertains to all
79          * locales. Locale is as defined by the string returned by Locale.toString().
80          * <p>TYPE: TEXT</p>
81          */
82         public static final String LOCALE = "locale";
83 
84         /**
85          * The uid of the application that inserted the word.
86          * <p>TYPE: INTEGER</p>
87          */
88         public static final String APP_ID = "appid";
89 
90         /** The locale type to specify that the word is common to all locales. */
91         public static final int LOCALE_TYPE_ALL = 0;
92 
93         /** The locale type to specify that the word is for the current locale. */
94         public static final int LOCALE_TYPE_CURRENT = 1;
95 
96         /**
97          * Sort by descending order of frequency.
98          */
99         public static final String DEFAULT_SORT_ORDER = FREQUENCY + " DESC";
100 
101         /** Adds a word to the dictionary, with the given frequency and the specified
102          *  specified locale type.
103          *  @param context the current application context
104          *  @param word the word to add to the dictionary. This should not be null or
105          *  empty.
106          *  @param localeType the locale type for this word. It should be one of
107          *  {@link #LOCALE_TYPE_ALL} or {@link #LOCALE_TYPE_CURRENT}.
108          */
addWord(Context context, String word, int frequency, int localeType)109         public static void addWord(Context context, String word,
110                 int frequency, int localeType) {
111             final ContentResolver resolver = context.getContentResolver();
112 
113             if (TextUtils.isEmpty(word) || localeType < 0 || localeType > 1) {
114                 return;
115             }
116 
117             if (frequency < 0) frequency = 0;
118             if (frequency > 255) frequency = 255;
119 
120             String locale = null;
121 
122             // TODO: Verify if this is the best way to get the current locale
123             if (localeType == LOCALE_TYPE_CURRENT) {
124                 locale = Locale.getDefault().toString();
125             }
126             ContentValues values = new ContentValues(4);
127 
128             values.put(WORD, word);
129             values.put(FREQUENCY, frequency);
130             values.put(LOCALE, locale);
131             values.put(APP_ID, 0); // TODO: Get App UID
132 
133             Uri result = resolver.insert(CONTENT_URI, values);
134             // It's ok if the insert doesn't succeed because the word
135             // already exists.
136         }
137     }
138 }
139