下面列出了怎么用android.provider.UserDictionary的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean hasWord(final String word, final Context context) {
final Cursor cursor;
// mLocale == "" indicates this is an entry for all languages. Here, mLocale can't
// be null at all (it's ensured by the updateLocale method).
if ("".equals(mLocale)) {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ALL_LOCALES,
new String[] { word }, null /* sort order */);
} else {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ONE_LOCALE,
new String[] { word, mLocale }, null /* sort order */);
}
try {
if (null == cursor) return false;
return cursor.getCount() > 0;
} finally {
if (null != cursor) cursor.close();
}
}
@SuppressWarnings("deprecation")
private Cursor createCursor(final String locale) {
// Locale can be any of:
// - The string representation of a locale, as returned by Locale#toString()
// - The empty string. This means we want a cursor returning words valid for all locales.
// - null. This means we want a cursor for the current locale, whatever this is.
// Note that this contrasts with the data inside the database, where NULL means "all
// locales" and there should never be an empty string. The confusion is called by the
// historical use of null for "all locales".
// TODO: it should be easy to make this more readable by making the special values
// human-readable, like "all_locales" and "current_locales" strings, provided they
// can be guaranteed not to match locales that may exist.
if ("".equals(locale)) {
// Case-insensitive sort
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION_ALL_LOCALES, null,
"UPPER(" + UserDictionary.Words.WORD + ")");
}
final String queryLocale = null != locale ? locale : Locale.getDefault().toString();
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION, new String[] { queryLocale },
"UPPER(" + UserDictionary.Words.WORD + ")");
}
public void open() {
Log.i(mTag, "open()");
// Schedule the initial load to run immediately. It's possible that the first call to
// isValidWord occurs before the dictionary has actually loaded, so it should not
// assume that the dictionary has been loaded.
loadPersonalDictionary();
// Register the observer to be notified on changes to the personal dictionary and all
// individual items.
//
// If the user is interacting with the Personal Dictionary settings UI, or with the
// "Add to dictionary" drop-down option, duplicate notifications will be sent for the same
// edit: if a new entry is added, there is a notification for the entry itself, and
// separately for the entire dictionary. However, when used programmatically,
// only notifications for the specific edits are sent. Thus, the observer is registered to
// receive every possible notification, and instead has throttling logic to avoid doing too
// many reloads.
mResolver.registerContentObserver(
UserDictionary.Words.CONTENT_URI,
true /* notifyForDescendents */,
mPersonalDictionaryContentObserver);
}
private boolean hasWord(final String word, final Context context) {
final Cursor cursor;
// mLocale == "" indicates this is an entry for all languages. Here, mLocale can't
// be null at all (it's ensured by the updateLocale method).
if ("".equals(mLocale)) {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ALL_LOCALES,
new String[] { word }, null /* sort order */);
} else {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ONE_LOCALE,
new String[] { word, mLocale }, null /* sort order */);
}
try {
if (null == cursor) return false;
return cursor.getCount() > 0;
} finally {
if (null != cursor) cursor.close();
}
}
@SuppressWarnings("deprecation")
private Cursor createCursor(final String locale) {
// Locale can be any of:
// - The string representation of a locale, as returned by Locale#toString()
// - The empty string. This means we want a cursor returning words valid for all locales.
// - null. This means we want a cursor for the current locale, whatever this is.
// Note that this contrasts with the data inside the database, where NULL means "all
// locales" and there should never be an empty string. The confusion is called by the
// historical use of null for "all locales".
// TODO: it should be easy to make this more readable by making the special values
// human-readable, like "all_locales" and "current_locales" strings, provided they
// can be guaranteed not to match locales that may exist.
if ("".equals(locale)) {
// Case-insensitive sort
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION_ALL_LOCALES, null,
"UPPER(" + UserDictionary.Words.WORD + ")");
}
final String queryLocale = null != locale ? locale : Locale.getDefault().toString();
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION, new String[] { queryLocale },
"UPPER(" + UserDictionary.Words.WORD + ")");
}
private boolean hasWord(final String word, final Context context) {
final Cursor cursor;
// mLocale == "" indicates this is an entry for all languages. Here, mLocale can't
// be null at all (it's ensured by the updateLocale method).
if ("".equals(mLocale)) {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ALL_LOCALES,
new String[] { word }, null /* sort order */);
} else {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ONE_LOCALE,
new String[] { word, mLocale }, null /* sort order */);
}
try {
if (null == cursor) return false;
return cursor.getCount() > 0;
} finally {
if (null != cursor) cursor.close();
}
}
@SuppressWarnings("deprecation")
private Cursor createCursor(final String locale) {
// Locale can be any of:
// - The string representation of a locale, as returned by Locale#toString()
// - The empty string. This means we want a cursor returning words valid for all locales.
// - null. This means we want a cursor for the current locale, whatever this is.
// Note that this contrasts with the data inside the database, where NULL means "all
// locales" and there should never be an empty string. The confusion is called by the
// historical use of null for "all locales".
// TODO: it should be easy to make this more readable by making the special values
// human-readable, like "all_locales" and "current_locales" strings, provided they
// can be guaranteed not to match locales that may exist.
if ("".equals(locale)) {
// Case-insensitive sort
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION_ALL_LOCALES, null,
"UPPER(" + UserDictionary.Words.WORD + ")");
}
final String queryLocale = null != locale ? locale : Locale.getDefault().toString();
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION, new String[] { queryLocale },
"UPPER(" + UserDictionary.Words.WORD + ")");
}
public void open() {
Log.i(mTag, "open()");
// Schedule the initial load to run immediately. It's possible that the first call to
// isValidWord occurs before the dictionary has actually loaded, so it should not
// assume that the dictionary has been loaded.
loadPersonalDictionary();
// Register the observer to be notified on changes to the personal dictionary and all
// individual items.
//
// If the user is interacting with the Personal Dictionary settings UI, or with the
// "Add to dictionary" drop-down option, duplicate notifications will be sent for the same
// edit: if a new entry is added, there is a notification for the entry itself, and
// separately for the entire dictionary. However, when used programmatically,
// only notifications for the specific edits are sent. Thus, the observer is registered to
// receive every possible notification, and instead has throttling logic to avoid doing too
// many reloads.
mResolver.registerContentObserver(
UserDictionary.Words.CONTENT_URI,
true /* notifyForDescendents */,
mPersonalDictionaryContentObserver);
}
private boolean hasWord(final String word, final Context context) {
final Cursor cursor;
// mLocale == "" indicates this is an entry for all languages. Here, mLocale can't
// be null at all (it's ensured by the updateLocale method).
if ("".equals(mLocale)) {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ALL_LOCALES,
new String[] { word }, null /* sort order */);
} else {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ONE_LOCALE,
new String[] { word, mLocale }, null /* sort order */);
}
try {
if (null == cursor) return false;
return cursor.getCount() > 0;
} finally {
if (null != cursor) cursor.close();
}
}
@SuppressWarnings("deprecation")
private Cursor createCursor(final String locale) {
// Locale can be any of:
// - The string representation of a locale, as returned by Locale#toString()
// - The empty string. This means we want a cursor returning words valid for all locales.
// - null. This means we want a cursor for the current locale, whatever this is.
// Note that this contrasts with the data inside the database, where NULL means "all
// locales" and there should never be an empty string. The confusion is called by the
// historical use of null for "all locales".
// TODO: it should be easy to make this more readable by making the special values
// human-readable, like "all_locales" and "current_locales" strings, provided they
// can be guaranteed not to match locales that may exist.
if ("".equals(locale)) {
// Case-insensitive sort
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION_ALL_LOCALES, null,
"UPPER(" + UserDictionary.Words.WORD + ")");
}
final String queryLocale = null != locale ? locale : Locale.getDefault().toString();
return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION, new String[] { queryLocale },
"UPPER(" + UserDictionary.Words.WORD + ")");
}
public void open() {
Log.i(mTag, "open()");
// Schedule the initial load to run immediately. It's possible that the first call to
// isValidWord occurs before the dictionary has actually loaded, so it should not
// assume that the dictionary has been loaded.
loadPersonalDictionary();
// Register the observer to be notified on changes to the personal dictionary and all
// individual items.
//
// If the user is interacting with the Personal Dictionary settings UI, or with the
// "Add to dictionary" drop-down option, duplicate notifications will be sent for the same
// edit: if a new entry is added, there is a notification for the entry itself, and
// separately for the entire dictionary. However, when used programmatically,
// only notifications for the specific edits are sent. Thus, the observer is registered to
// receive every possible notification, and instead has throttling logic to avoid doing too
// many reloads.
mResolver.registerContentObserver(
UserDictionary.Words.CONTENT_URI,
true /* notifyForDescendents */,
mPersonalDictionaryContentObserver);
}
private String getWord(final int position) {
if (null == mCursor) return null;
mCursor.moveToPosition(position);
// Handle a possible race-condition
if (mCursor.isAfterLast()) return null;
return mCursor.getString(
mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD));
}
private String getShortcut(final int position) {
if (!IS_SHORTCUT_API_SUPPORTED) return null;
if (null == mCursor) return null;
mCursor.moveToPosition(position);
// Handle a possible race-condition
if (mCursor.isAfterLast()) return null;
return mCursor.getString(
mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT));
}
public static void deleteWord(final String word, final String shortcut,
final ContentResolver resolver) {
if (!IS_SHORTCUT_API_SUPPORTED) {
resolver.delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_SHORTCUT_UNSUPPORTED,
new String[] { word });
} else if (TextUtils.isEmpty(shortcut)) {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT,
new String[] { word });
} else {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT,
new String[] { word, shortcut });
}
}
public MyAdapter(final Context context, final int layout, final Cursor c,
final String[] from, final int[] to) {
super(context, layout, c, from, to, 0 /* flags */);
if (null != c) {
final String alphabet = context.getString(R.string.user_dict_fast_scroll_alphabet);
final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD);
mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet);
}
setViewBinder(mViewBinder);
}
private String getWord(final int position) {
if (null == mCursor) return null;
mCursor.moveToPosition(position);
// Handle a possible race-condition
if (mCursor.isAfterLast()) return null;
return mCursor.getString(
mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD));
}
private String getShortcut(final int position) {
if (!IS_SHORTCUT_API_SUPPORTED) return null;
if (null == mCursor) return null;
mCursor.moveToPosition(position);
// Handle a possible race-condition
if (mCursor.isAfterLast()) return null;
return mCursor.getString(
mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT));
}
public static void deleteWord(final String word, final String shortcut,
final ContentResolver resolver) {
if (!IS_SHORTCUT_API_SUPPORTED) {
resolver.delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_SHORTCUT_UNSUPPORTED,
new String[] { word });
} else if (TextUtils.isEmpty(shortcut)) {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT,
new String[] { word });
} else {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT,
new String[] { word, shortcut });
}
}
public MyAdapter(final Context context, final int layout, final Cursor c,
final String[] from, final int[] to) {
super(context, layout, c, from, to, 0 /* flags */);
if (null != c) {
final String alphabet = context.getString(R.string.user_dict_fast_scroll_alphabet);
final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD);
mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet);
}
setViewBinder(mViewBinder);
}
private String getWord(final int position) {
if (null == mCursor) return null;
mCursor.moveToPosition(position);
// Handle a possible race-condition
if (mCursor.isAfterLast()) return null;
return mCursor.getString(
mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD));
}
private String getShortcut(final int position) {
if (!IS_SHORTCUT_API_SUPPORTED) return null;
if (null == mCursor) return null;
mCursor.moveToPosition(position);
// Handle a possible race-condition
if (mCursor.isAfterLast()) return null;
return mCursor.getString(
mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT));
}
public static void deleteWord(final String word, final String shortcut,
final ContentResolver resolver) {
if (!IS_SHORTCUT_API_SUPPORTED) {
resolver.delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_SHORTCUT_UNSUPPORTED,
new String[] { word });
} else if (TextUtils.isEmpty(shortcut)) {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT,
new String[] { word });
} else {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT,
new String[] { word, shortcut });
}
}
public MyAdapter(final Context context, final int layout, final Cursor c,
final String[] from, final int[] to) {
super(context, layout, c, from, to, 0 /* flags */);
if (null != c) {
final String alphabet = context.getString(R.string.user_dict_fast_scroll_alphabet);
final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD);
mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet);
}
setViewBinder(mViewBinder);
}
@SuppressWarnings("deprecation")
public static void addWord(final Context context, final String word,
final int freq, final String shortcut, final Locale locale) {
if (BuildCompatUtils.EFFECTIVE_SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
addWordWithShortcut(context, word, freq, shortcut, locale);
return;
}
// Fall back to the pre-JellyBean method.
final Locale currentLocale = context.getResources().getConfiguration().locale;
final int localeType = currentLocale.equals(locale)
? UserDictionary.Words.LOCALE_TYPE_CURRENT : UserDictionary.Words.LOCALE_TYPE_ALL;
UserDictionary.Words.addWord(context, word, freq, localeType);
}
private String getWord(final int position) {
if (null == mCursor) return null;
mCursor.moveToPosition(position);
// Handle a possible race-condition
if (mCursor.isAfterLast()) return null;
return mCursor.getString(
mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD));
}
private String getShortcut(final int position) {
if (!IS_SHORTCUT_API_SUPPORTED) return null;
if (null == mCursor) return null;
mCursor.moveToPosition(position);
// Handle a possible race-condition
if (mCursor.isAfterLast()) return null;
return mCursor.getString(
mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT));
}
public static void deleteWord(final String word, final String shortcut,
final ContentResolver resolver) {
if (!IS_SHORTCUT_API_SUPPORTED) {
resolver.delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_SHORTCUT_UNSUPPORTED,
new String[] { word });
} else if (TextUtils.isEmpty(shortcut)) {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT,
new String[] { word });
} else {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT,
new String[] { word, shortcut });
}
}
public MyAdapter(final Context context, final int layout, final Cursor c,
final String[] from, final int[] to) {
super(context, layout, c, from, to, 0 /* flags */);
if (null != c) {
final String alphabet = context.getString(R.string.user_dict_fast_scroll_alphabet);
final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD);
mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet);
}
setViewBinder(mViewBinder);
}
@SuppressWarnings("deprecation")
public static void addWord(final Context context, final String word,
final int freq, final String shortcut, final Locale locale) {
if (BuildCompatUtils.EFFECTIVE_SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
addWordWithShortcut(context, word, freq, shortcut, locale);
return;
}
// Fall back to the pre-JellyBean method.
final Locale currentLocale = context.getResources().getConfiguration().locale;
final int localeType = currentLocale.equals(locale)
? UserDictionary.Words.LOCALE_TYPE_CURRENT : UserDictionary.Words.LOCALE_TYPE_ALL;
UserDictionary.Words.addWord(context, word, freq, localeType);
}
/**
* Adds the given word to the personal dictionary.
*
* @param word the word to add
* @param locale the locale of the word to add
* @param frequency the frequency of the word to add
* @return the Uri for the given word
*/
@SuppressLint("NewApi")
private Uri addWord(final String word, final Locale locale, int frequency, String shortcut) {
// Add the given word for the given locale.
UserDictionary.Words.addWord(mContext, word, frequency, shortcut, locale);
// Obtain an Uri for the given word.
Cursor cursor = mContentResolver.query(UserDictionary.Words.CONTENT_URI, null,
UserDictionary.Words.WORD + "='" + word + "'", null, null);
assertTrue(cursor.moveToFirst());
Uri uri = Uri.withAppendedPath(UserDictionary.Words.CONTENT_URI,
cursor.getString(cursor.getColumnIndex(UserDictionary.Words._ID)));
// Add the row to the backup for later clearing.
mAddedBackup.add(uri);
return uri;
}