android.provider.UserDictionary.Words Java Examples

The following examples show how to use android.provider.UserDictionary.Words. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserBinaryDictionary.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
private void addWordsLocked(final Cursor cursor) {
    if (cursor == null) return;
    if (cursor.moveToFirst()) {
        final int indexWord = cursor.getColumnIndex(Words.WORD);
        final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY);
        while (!cursor.isAfterLast()) {
            final String word = cursor.getString(indexWord);
            final int frequency = cursor.getInt(indexFrequency);
            final int adjustedFrequency = scaleFrequencyFromDefaultToLatinIme(frequency);
            // Safeguard against adding really long words.
            if (word.length() <= MAX_WORD_LENGTH) {
                runGCIfRequiredLocked(true /* mindsBlockByGC */);
                addUnigramLocked(word, adjustedFrequency, false /* isNotAWord */,
                        false /* isPossiblyOffensive */,
                        BinaryDictionary.NOT_A_VALID_TIMESTAMP);
            }
            cursor.moveToNext();
        }
    }
}
 
Example #2
Source File: UserDictionary.java    From hackerskeyboard with Apache License 2.0 6 votes vote down vote up
public UserDictionary(Context context, String locale) {
    super(context, Suggest.DIC_USER);
    mLocale = locale;
    // Perform a managed query. The Activity will handle closing and requerying the cursor
    // when needed.
    ContentResolver cres = context.getContentResolver();
    
    cres.registerContentObserver(Words.CONTENT_URI, true, mObserver = new ContentObserver(null) {
        @Override
        public void onChange(boolean self) {
            setRequiresReload(true);
        }
    });

    loadDictionary();
}
 
Example #3
Source File: UserBinaryDictionary.java    From Indic-Keyboard with Apache License 2.0 6 votes vote down vote up
private void addWordsLocked(final Cursor cursor) {
    if (cursor == null) return;
    if (cursor.moveToFirst()) {
        final int indexWord = cursor.getColumnIndex(Words.WORD);
        final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY);
        while (!cursor.isAfterLast()) {
            final String word = cursor.getString(indexWord);
            final int frequency = cursor.getInt(indexFrequency);
            final int adjustedFrequency = scaleFrequencyFromDefaultToLatinIme(frequency);
            // Safeguard against adding really long words.
            if (word.length() <= MAX_WORD_LENGTH) {
                runGCIfRequiredLocked(true /* mindsBlockByGC */);
                addUnigramLocked(word, adjustedFrequency, false /* isNotAWord */,
                        false /* isPossiblyOffensive */,
                        BinaryDictionary.NOT_A_VALID_TIMESTAMP);
            }
            cursor.moveToNext();
        }
    }
}
 
Example #4
Source File: AndroidWordLevelSpellCheckerSession.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
AndroidWordLevelSpellCheckerSession(final AndroidSpellCheckerService service) {
    mService = service;
    final ContentResolver cres = service.getContentResolver();

    mObserver = new ContentObserver(null) {
        @Override
        public void onChange(boolean self) {
            mSuggestionsCache.clearCache();
        }
    };
    cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
}
 
Example #5
Source File: UserBinaryDictionary.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
private void addWordsLocked(final Cursor cursor) {
    final boolean hasShortcutColumn = true;
    if (cursor == null) return;
    if (cursor.moveToFirst()) {
        final int indexWord = cursor.getColumnIndex(Words.WORD);
        final int indexShortcut = hasShortcutColumn ? cursor.getColumnIndex(Words.SHORTCUT) : 0;
        final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY);
        while (!cursor.isAfterLast()) {
            final String word = cursor.getString(indexWord);
            final String shortcut = hasShortcutColumn ? cursor.getString(indexShortcut) : null;
            final int frequency = cursor.getInt(indexFrequency);
            final int adjustedFrequency = scaleFrequencyFromDefaultToLatinIme(frequency);
            // Safeguard against adding really long words.
            if (word.length() <= MAX_WORD_LENGTH) {
                runGCIfRequiredLocked(true /* mindsBlockByGC */);
                addUnigramLocked(word, adjustedFrequency, null /* shortcutTarget */,
                        0 /* shortcutFreq */, false /* isNotAWord */,
                        false /* isPossiblyOffensive */,
                        BinaryDictionary.NOT_A_VALID_TIMESTAMP);
                if (null != shortcut && shortcut.length() <= MAX_WORD_LENGTH) {
                    runGCIfRequiredLocked(true /* mindsBlockByGC */);
                    addUnigramLocked(shortcut, adjustedFrequency, word,
                            USER_DICT_SHORTCUT_FREQUENCY, true /* isNotAWord */,
                            false /* isPossiblyOffensive */,
                            BinaryDictionary.NOT_A_VALID_TIMESTAMP);
                }
            }
            cursor.moveToNext();
        }
    }
}
 
Example #6
Source File: AndroidWordLevelSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
AndroidWordLevelSpellCheckerSession(final AndroidSpellCheckerService service) {
    mService = service;
    final ContentResolver cres = service.getContentResolver();

    mObserver = new ContentObserver(null) {
        @Override
        public void onChange(boolean self) {
            mSuggestionsCache.clearCache();
        }
    };
    cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
}
 
Example #7
Source File: AndroidWordLevelSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
AndroidWordLevelSpellCheckerSession(final AndroidSpellCheckerService service) {
    mService = service;
    final ContentResolver cres = service.getContentResolver();

    mObserver = new ContentObserver(null) {
        @Override
        public void onChange(boolean self) {
            mSuggestionsCache.clearCache();
        }
    };
    cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
}
 
Example #8
Source File: UserDictionary.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
@Override
public void loadDictionaryAsync() {
    Cursor cursor = getContext().getContentResolver()
            .query(Words.CONTENT_URI, PROJECTION, "(locale IS NULL) or (locale=?)", 
                    new String[] { mLocale }, null);
    addWords(cursor);
}
 
Example #9
Source File: UserDictionary.java    From hackerskeyboard with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a word to the dictionary and makes it persistent.
 * @param word the word to add. If the word is capitalized, then the dictionary will
 * recognize it as a capitalized word when searched.
 * @param frequency the frequency of occurrence of the word. A frequency of 255 is considered
 * the highest.
 * @TODO use a higher or float range for frequency
 */
@Override
public synchronized void addWord(String word, int frequency) {
    // Force load the dictionary here synchronously
    if (getRequiresReload()) loadDictionaryAsync();
    // Safeguard against adding long words. Can cause stack overflow.
    if (word.length() >= getMaxWordLength()) return;

    super.addWord(word, frequency);

    // Update the user dictionary provider
    final ContentValues values = new ContentValues(5);
    values.put(Words.WORD, word);
    values.put(Words.FREQUENCY, frequency);
    values.put(Words.LOCALE, mLocale);
    values.put(Words.APP_ID, 0);

    final ContentResolver contentResolver = getContext().getContentResolver();
    new Thread("addWord") {
        public void run() {
            contentResolver.insert(Words.CONTENT_URI, values);
        }
    }.start();

    // In case the above does a synchronous callback of the change observer
    setRequiresReload(false);
}
 
Example #10
Source File: AndroidWordLevelSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
AndroidWordLevelSpellCheckerSession(final AndroidSpellCheckerService service) {
    mService = service;
    final ContentResolver cres = service.getContentResolver();

    mObserver = new ContentObserver(null) {
        @Override
        public void onChange(boolean self) {
            mSuggestionsCache.clearCache();
        }
    };
    cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
}