From 23a3fd5bfacc54fd3841bb080460c91be4006051 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 25 Jul 2013 16:33:13 +0900 Subject: [PATCH] Fix a bug with multiple word insertion. This is a port of I5202bc52 Bug: 7903831 Change-Id: I3eb526de01a1f5b51212166e39b98de1d71e9eaf --- .../UserDictionaryAddWordContents.java | 14 ++++++++++++++ .../UserDictionaryAddWordFragment.java | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java index dad72963d..ba5a68460 100644 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java @@ -65,6 +65,8 @@ public class UserDictionaryAddWordContents { 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); @@ -96,6 +98,16 @@ public class UserDictionaryAddWordContents { updateLocale(args.getString(EXTRA_LOCALE)); } + /* 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); + } + // locale may be null, this means default locale // It may also be the empty string, which means "all locales" /* package */ void updateLocale(final String locale) { @@ -149,6 +161,8 @@ public class UserDictionaryAddWordContents { // If the word is somehow empty, don't insert it. return CODE_CANCEL; } + mSavedWord = newWord; + 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 diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java index 58c8f266c..8b8bd5e03 100644 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java @@ -57,23 +57,36 @@ public class UserDictionaryAddWordFragment extends Fragment private boolean mIsDeleting = false; @Override - public void onActivityCreated(Bundle savedInstanceState) { + public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setHasOptionsMenu(true); + // Keep the instance so that we remember mContents when configuration changes (eg rotation) + setRetainInstance(true); } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { + public View onCreateView(final LayoutInflater inflater, final ViewGroup container, + final Bundle savedState) { mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null); mIsDeleting = false; + // If we have a non-null mContents object, it's the old value before a configuration + // change (eg rotation) so we need to use its values. Otherwise, read from the arguments. if (null == mContents) { mContents = new UserDictionaryAddWordContents(mRootView, getArguments()); + } else { + // We create a new mContents object to account for the new situation : a word has + // been added to the user dictionary when we started rotating, and we are now editing + // it. That means in particular if the word undergoes any change, the old version should + // be updated, so the mContents object needs to switch to EDIT mode if it was in + // INSERT mode. + mContents = new UserDictionaryAddWordContents(mRootView, + mContents /* oldInstanceToBeEdited */); } return mRootView; } @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { final MenuItem actionItemAdd = menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title).setIcon(R.drawable.ic_menu_add); actionItemAdd.setShowAsAction(