Rework the de-duplication scheme for shortcuts.
Bug: 4646172 Change-Id: I88966f3e910f51711ce49336fb9134d0953930de
This commit is contained in:
@@ -67,7 +67,11 @@ public class UserDictionarySettings extends ListFragment {
|
|||||||
private static final String QUERY_SELECTION_ALL_LOCALES =
|
private static final String QUERY_SELECTION_ALL_LOCALES =
|
||||||
UserDictionary.Words.LOCALE + " is null";
|
UserDictionary.Words.LOCALE + " is null";
|
||||||
|
|
||||||
private static final String DELETE_SELECTION = UserDictionary.Words.WORD + "=?";
|
private static final String DELETE_SELECTION_WITH_SHORTCUT = UserDictionary.Words.WORD
|
||||||
|
+ "=? AND " + UserDictionary.Words.SHORTCUT + "=?";
|
||||||
|
private static final String DELETE_SELECTION_WITHOUT_SHORTCUT = UserDictionary.Words.WORD
|
||||||
|
+ "=? AND " + UserDictionary.Words.SHORTCUT + " is null OR "
|
||||||
|
+ UserDictionary.Words.SHORTCUT + "=''";
|
||||||
|
|
||||||
private static final int OPTIONS_MENU_ADD = Menu.FIRST;
|
private static final int OPTIONS_MENU_ADD = Menu.FIRST;
|
||||||
|
|
||||||
@@ -220,9 +224,17 @@ public class UserDictionarySettings extends ListFragment {
|
|||||||
mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT));
|
mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void deleteWord(final String word, final ContentResolver resolver) {
|
public static void deleteWord(final String word, final String shortcut,
|
||||||
resolver.delete(
|
final ContentResolver resolver) {
|
||||||
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION, new String[] { word });
|
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 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class MyAdapter extends SimpleCursorAdapter implements SectionIndexer {
|
private static class MyAdapter extends SimpleCursorAdapter implements SectionIndexer {
|
||||||
|
@@ -90,22 +90,38 @@ public class UserDictionaryAddWordContents {
|
|||||||
/* package */ void apply(final Context context) {
|
/* package */ void apply(final Context context) {
|
||||||
final ContentResolver resolver = context.getContentResolver();
|
final ContentResolver resolver = context.getContentResolver();
|
||||||
if (UserDictionaryAddWordContents.MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) {
|
if (UserDictionaryAddWordContents.MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) {
|
||||||
UserDictionarySettings.deleteWord(mOldWord, resolver);
|
// Mode edit: remove the old entry.
|
||||||
|
UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver);
|
||||||
}
|
}
|
||||||
final String newWord = mWordEditText.getText().toString();
|
final String newWord = mWordEditText.getText().toString();
|
||||||
|
final String newShortcut;
|
||||||
|
if (null == mShortcutEditText) {
|
||||||
|
newShortcut = null;
|
||||||
|
} else {
|
||||||
|
final String tmpShortcut = mShortcutEditText.getText().toString();
|
||||||
|
if (TextUtils.isEmpty(tmpShortcut)) {
|
||||||
|
newShortcut = null;
|
||||||
|
} else {
|
||||||
|
newShortcut = tmpShortcut;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (TextUtils.isEmpty(newWord)) {
|
if (TextUtils.isEmpty(newWord)) {
|
||||||
// If the word is somehow empty, don't insert it.
|
// If the word is somehow empty, don't insert it.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Disallow duplicates.
|
// Disallow duplicates. If the same word with no shortcut is defined, remove it; if
|
||||||
// TODO: Redefine the logic when we support shortcuts.
|
// the same word with the same shortcut is defined, remove it; but we don't mind if
|
||||||
UserDictionarySettings.deleteWord(newWord, resolver);
|
// there is the same word with a different, non-empty shortcut.
|
||||||
|
UserDictionarySettings.deleteWord(newWord, null, resolver);
|
||||||
|
if (!TextUtils.isEmpty(newShortcut)) {
|
||||||
|
// If newShortcut is empty we just deleted this, no need to do it again
|
||||||
|
UserDictionarySettings.deleteWord(newWord, newShortcut, resolver);
|
||||||
|
}
|
||||||
|
|
||||||
// In this class we use the empty string to represent 'all locales' and mLocale cannot
|
// In this class we use the empty string to represent 'all locales' and mLocale cannot
|
||||||
// be null. However the addWord method takes null to mean 'all locales'.
|
// be null. However the addWord method takes null to mean 'all locales'.
|
||||||
UserDictionary.Words.addWord(context, newWord.toString(),
|
UserDictionary.Words.addWord(context, newWord.toString(),
|
||||||
FREQUENCY_FOR_USER_DICTIONARY_ADDS,
|
FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut,
|
||||||
null == mShortcutEditText ? null : mShortcutEditText.getText().toString(),
|
|
||||||
TextUtils.isEmpty(mLocale) ? null : Utils.createLocaleFromString(mLocale));
|
TextUtils.isEmpty(mLocale) ? null : Utils.createLocaleFromString(mLocale));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user