Skip to content

Commit 96c804a

Browse files
author
Jean Chalard
committed
Add methods to support shortcuts in user dict.
Adds the new interface to the user dictionary, which includes a clean way of inserting words in any locale and support for shortcuts. Change Ib318c047 implements the provider part of this. Bug: 4646172 Change-Id: Id3ca792f2555fac46728f9d404ab0199971f6503
1 parent fb9ffe0 commit 96c804a

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

api/current.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17657,16 +17657,18 @@ package android.provider {
1765717657

1765817658
public static class UserDictionary.Words implements android.provider.BaseColumns {
1765917659
ctor public UserDictionary.Words();
17660-
method public static void addWord(android.content.Context, java.lang.String, int, int);
17660+
method public static deprecated void addWord(android.content.Context, java.lang.String, int, int);
17661+
method public static void addWord(android.content.Context, java.lang.String, int, java.lang.String, java.util.Locale);
1766117662
field public static final java.lang.String APP_ID = "appid";
1766217663
field public static final java.lang.String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.userword";
1766317664
field public static final java.lang.String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.userword";
1766417665
field public static final android.net.Uri CONTENT_URI;
1766517666
field public static final java.lang.String DEFAULT_SORT_ORDER = "frequency DESC";
1766617667
field public static final java.lang.String FREQUENCY = "frequency";
1766717668
field public static final java.lang.String LOCALE = "locale";
17668-
field public static final int LOCALE_TYPE_ALL = 0; // 0x0
17669-
field public static final int LOCALE_TYPE_CURRENT = 1; // 0x1
17669+
field public static final deprecated int LOCALE_TYPE_ALL = 0; // 0x0
17670+
field public static final deprecated int LOCALE_TYPE_CURRENT = 1; // 0x1
17671+
field public static final java.lang.String SHORTCUT = "shortcut";
1767017672
field public static final java.lang.String WORD = "word";
1767117673
field public static final java.lang.String _ID = "_id";
1767217674
}

core/java/android/provider/UserDictionary.java

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class UserDictionary {
4040
public static final Uri CONTENT_URI =
4141
Uri.parse("content://" + AUTHORITY);
4242

43+
private static final int FREQUENCY_MIN = 0;
44+
private static final int FREQUENCY_MAX = 255;
45+
4346
/**
4447
* Contains the user defined words.
4548
*/
@@ -87,48 +90,90 @@ public static class Words implements BaseColumns {
8790
*/
8891
public static final String APP_ID = "appid";
8992

90-
/** The locale type to specify that the word is common to all locales. */
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
91103
public static final int LOCALE_TYPE_ALL = 0;
92-
93-
/** The locale type to specify that the word is for the current locale. */
104+
105+
/**
106+
* @deprecated Use {@link #addWord(Context, String, int, String, Locale)}.
107+
*/
108+
@Deprecated
94109
public static final int LOCALE_TYPE_CURRENT = 1;
95-
110+
96111
/**
97112
* Sort by descending order of frequency.
98113
*/
99114
public static final String DEFAULT_SORT_ORDER = FREQUENCY + " DESC";
100115

101116
/** Adds a word to the dictionary, with the given frequency and the specified
102117
* specified locale type.
118+
*
119+
* @deprecated Please use
120+
* {@link #addWord(Context, String, int, String, Locale)} instead.
121+
*
103122
* @param context the current application context
104123
* @param word the word to add to the dictionary. This should not be null or
105124
* empty.
106125
* @param localeType the locale type for this word. It should be one of
107126
* {@link #LOCALE_TYPE_ALL} or {@link #LOCALE_TYPE_CURRENT}.
108127
*/
109-
public static void addWord(Context context, String word,
128+
@Deprecated
129+
public static void addWord(Context context, String word,
110130
int frequency, int localeType) {
111-
final ContentResolver resolver = context.getContentResolver();
112131

113-
if (TextUtils.isEmpty(word) || localeType < 0 || localeType > 1) {
132+
if (localeType != LOCALE_TYPE_ALL && localeType != LOCALE_TYPE_CURRENT) {
114133
return;
115134
}
116-
117-
if (frequency < 0) frequency = 0;
118-
if (frequency > 255) frequency = 255;
119135

120-
String locale = null;
136+
final Locale locale;
121137

122-
// TODO: Verify if this is the best way to get the current locale
123138
if (localeType == LOCALE_TYPE_CURRENT) {
124-
locale = Locale.getDefault().toString();
139+
locale = Locale.getDefault();
140+
} else {
141+
locale = null;
125142
}
126-
ContentValues values = new ContentValues(4);
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+
*/
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);
127171

128172
values.put(WORD, word);
129173
values.put(FREQUENCY, frequency);
130-
values.put(LOCALE, locale);
174+
values.put(LOCALE, null == locale ? null : locale.toString());
131175
values.put(APP_ID, 0); // TODO: Get App UID
176+
values.put(SHORTCUT, shortcut);
132177

133178
Uri result = resolver.insert(CONTENT_URI, values);
134179
// It's ok if the insert doesn't succeed because the word

0 commit comments

Comments
 (0)