diff options
author | 2015-07-01 13:54:44 -0700 | |
---|---|---|
committer | 2015-07-06 17:32:13 -0700 | |
commit | 8c2591eec80dbe44158b1613cd96056057a41139 (patch) | |
tree | 431b6133b34e992d82cc44eb7701ca35221fc9e6 /java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java | |
parent | d23dd5978d60322613c0148556032cbce37e81b7 (diff) | |
download | latinime-8c2591eec80dbe44158b1613cd96056057a41139.tar.gz latinime-8c2591eec80dbe44158b1613cd96056057a41139.tar.xz latinime-8c2591eec80dbe44158b1613cd96056057a41139.zip |
Bring back shortcuts and add to dictionary UI
For JB and lower devices, the UI is surfaced by the IME.
Bug: 22200135
Change-Id: Icca08500ee0683e2ceb5357b0bc430cd1712220e
Diffstat (limited to 'java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java | 78 |
1 files changed, 65 insertions, 13 deletions
diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java index cb615f3af..58e6a25b8 100644 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java @@ -26,6 +26,7 @@ import android.text.TextUtils; import android.view.View; import android.widget.EditText; +import com.android.inputmethod.compat.UserDictionaryCompatUtils; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.common.LocaleUtils; @@ -46,7 +47,10 @@ import javax.annotation.Nullable; public class UserDictionaryAddWordContents { public static final String EXTRA_MODE = "mode"; public static final String EXTRA_WORD = "word"; + public static final String EXTRA_SHORTCUT = "shortcut"; public static final String EXTRA_LOCALE = "locale"; + public static final String EXTRA_ORIGINAL_WORD = "originalWord"; + public static final String EXTRA_ORIGINAL_SHORTCUT = "originalShortcut"; public static final int MODE_EDIT = 0; public static final int MODE_INSERT = 1; @@ -59,12 +63,20 @@ public class UserDictionaryAddWordContents { private final int mMode; // Either MODE_EDIT or MODE_INSERT private final EditText mWordEditText; + private final EditText mShortcutEditText; private String mLocale; private final String mOldWord; + private final String mOldShortcut; private String mSavedWord; + private String mSavedShortcut; /* package */ UserDictionaryAddWordContents(final View view, final Bundle args) { mWordEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text); + mShortcutEditText = (EditText)view.findViewById(R.id.user_dictionary_add_shortcut); + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + mShortcutEditText.setVisibility(View.GONE); + view.findViewById(R.id.user_dictionary_add_shortcut_label).setVisibility(View.GONE); + } final String word = args.getString(EXTRA_WORD); if (null != word) { mWordEditText.setText(word); @@ -72,6 +84,17 @@ public class UserDictionaryAddWordContents { // it's too long to be edited. mWordEditText.setSelection(mWordEditText.getText().length()); } + final String shortcut; + if (UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + shortcut = args.getString(EXTRA_SHORTCUT); + if (null != shortcut && null != mShortcutEditText) { + mShortcutEditText.setText(shortcut); + } + mOldShortcut = args.getString(EXTRA_SHORTCUT); + } else { + shortcut = null; + mOldShortcut = null; + } mMode = args.getInt(EXTRA_MODE); // default return value for #getInt() is 0 = MODE_EDIT mOldWord = args.getString(EXTRA_WORD); updateLocale(args.getString(EXTRA_LOCALE)); @@ -80,8 +103,10 @@ public class UserDictionaryAddWordContents { /* package */ UserDictionaryAddWordContents(final View view, final UserDictionaryAddWordContents oldInstanceToBeEdited) { mWordEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text); + mShortcutEditText = (EditText)view.findViewById(R.id.user_dictionary_add_shortcut); mMode = MODE_EDIT; mOldWord = oldInstanceToBeEdited.mSavedWord; + mOldShortcut = oldInstanceToBeEdited.mSavedShortcut; updateLocale(mLocale); } @@ -93,6 +118,13 @@ public class UserDictionaryAddWordContents { /* package */ void saveStateIntoBundle(final Bundle outState) { outState.putString(EXTRA_WORD, mWordEditText.getText().toString()); + outState.putString(EXTRA_ORIGINAL_WORD, mOldWord); + if (null != mShortcutEditText) { + outState.putString(EXTRA_SHORTCUT, mShortcutEditText.getText().toString()); + } + if (null != mOldShortcut) { + outState.putString(EXTRA_ORIGINAL_SHORTCUT, mOldShortcut); + } outState.putString(EXTRA_LOCALE, mLocale); } @@ -100,7 +132,7 @@ public class UserDictionaryAddWordContents { if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { // Mode edit: remove the old entry. final ContentResolver resolver = context.getContentResolver(); - UserDictionarySettings.deleteWord(mOldWord, resolver); + UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); } // If we are in add mode, nothing was added, so we don't need to do anything. } @@ -111,31 +143,50 @@ public class UserDictionaryAddWordContents { final ContentResolver resolver = context.getContentResolver(); if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { // Mode edit: remove the old entry. - UserDictionarySettings.deleteWord(mOldWord, resolver); + UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); } final String newWord = mWordEditText.getText().toString(); + final String newShortcut; + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + newShortcut = null; + } else 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 the word is somehow empty, don't insert it. return CODE_CANCEL; } mSavedWord = newWord; - // If the word already exists in the database, then we should not insert. - if (hasWord(newWord, context)) { + mSavedShortcut = newShortcut; + // 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 (TextUtils.isEmpty(newShortcut) && hasWord(newWord, context)) { return CODE_ALREADY_PRESENT; } - // Disallow duplicates. If the same word is defined, remove it. - UserDictionarySettings.deleteWord(newWord, resolver); + // 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. + 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 // be null. However the addWord method takes null to mean 'all locales'. - final Locale locale = TextUtils.isEmpty(mLocale) ? - null : LocaleUtils.constructLocaleFromString(mLocale); - final Locale currentLocale = context.getResources().getConfiguration().locale; - final boolean useCurrentLocale = currentLocale.equals(locale); - UserDictionary.Words.addWord(context, newWord.toString(), - FREQUENCY_FOR_USER_DICTIONARY_ADDS, null /* shortcut */, - useCurrentLocale ? Locale.getDefault() : null); + UserDictionaryCompatUtils.addWord(context, newWord.toString(), + FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut, TextUtils.isEmpty(mLocale) ? + null : LocaleUtils.constructLocaleFromString(mLocale)); return CODE_WORD_ADDED; } @@ -232,3 +283,4 @@ public class UserDictionaryAddWordContents { return mLocale; } } + |