diff options
author | Jean Chalard <jchalard@google.com> | 2012-05-10 15:08:17 +0900 |
---|---|---|
committer | Jean Chalard <jchalard@google.com> | 2012-05-10 18:12:54 +0900 |
commit | eed02dd2d51a876894f9f10023b1418ea6a45d8e (patch) | |
tree | 166f823feb354dc526d64c47a4673041b3d4232d /src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java | |
parent | 271543876d3ecccdb5547554778e91c9e7dd87ba (diff) | |
download | packages_apps_Settings-eed02dd2d51a876894f9f10023b1418ea6a45d8e.zip packages_apps_Settings-eed02dd2d51a876894f9f10023b1418ea6a45d8e.tar.gz packages_apps_Settings-eed02dd2d51a876894f9f10023b1418ea6a45d8e.tar.bz2 |
Fix a bug with adding a word
It was possible to insert a word without a shortcut while the same
word used to be present with a shortcut. This change fixes that.
Bug: 6026080
Change-Id: I3be98bf450aad8e2eb38336e8f77aedab39d5797
Diffstat (limited to 'src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java')
-rw-r--r-- | src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java index 1522863..e46b19c 100644 --- a/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java +++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java @@ -19,6 +19,7 @@ package com.android.settings.inputmethod; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; +import android.database.Cursor; import android.os.Bundle; import android.provider.UserDictionary; import android.text.TextUtils; @@ -118,6 +119,12 @@ public class UserDictionaryAddWordContents { // If the word is somehow empty, don't insert it. return; } + // If there is no shortcut, and the word already exists in the database, then we + // should not insert, because either A. the word exists with no shortcut, in which + // case the exact same thing we want to insert is already there, or B. the word + // exists with at least one shortcut, in which case it has priority on our word. + if (hasWord(newWord, context)) return; + // Disallow duplicates. If the same word with no shortcut is defined, remove it; if // the same word with the same shortcut is defined, remove it; but we don't mind if // there is the same word with a different, non-empty shortcut. @@ -134,6 +141,32 @@ public class UserDictionaryAddWordContents { TextUtils.isEmpty(mLocale) ? null : Utils.createLocaleFromString(mLocale)); } + private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD }; + private static final String HAS_WORD_SELECTION_ONE_LOCALE = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.LOCALE + "=?"; + private static final String HAS_WORD_SELECTION_ALL_LOCALES = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.LOCALE + " is null"; + 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(); + } + } + public static class LocaleRenderer { private final String mLocaleString; private final String mDescription; |