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); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView dictListView = (ListView) findViewById(R.id.dictionary_list_view); ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.listview_layout, cursor, COLUMNS_TO_BE_FOUND, LAYOUT_ITEMS_TO_FILL, 0); dictListView.setAdapter(adapter); registerForContextMenu(dictListView); }
@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 + ")"); } else { 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 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); }
public static TreeSet<String> getUserDictionaryLocalesSet(Activity activity) { @SuppressWarnings("deprecation") final Cursor cursor = activity.managedQuery(UserDictionary.Words.CONTENT_URI, new String[] { UserDictionary.Words.LOCALE }, null, null, null); final TreeSet<String> localeList = new TreeSet<String>(); boolean addedAllLocale = false; if (null == cursor) { // The user dictionary service is not present or disabled. Return null. return null; } else if (cursor.moveToFirst()) { final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE); do { final String locale = cursor.getString(columnIndex); final boolean allLocale = TextUtils.isEmpty(locale); localeList.add(allLocale ? "" : locale); if (allLocale) { addedAllLocale = true; } } while (cursor.moveToNext()); } if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED && !addedAllLocale) { // For ICS, we need to show "For all languages" in case that the keyboard locale // is different from the system locale localeList.add(""); } localeList.add(Locale.getDefault().toString()); return localeList; }
@SuppressWarnings("deprecation") public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, UserDictionarySettings settings) { super(context, layout, c, from, to); 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); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private static void addWordWithShortcut(final Context context, final String word, final int freq, final String shortcut, final Locale locale) { UserDictionary.Words.addWord(context, word, freq, shortcut, locale); }