am a374d816: Distinguish adding an already present word / cancelling

* commit 'a374d81605f4ba760db9613dd8aa8d8a0370dab8':
  Distinguish adding an already present word / cancelling
This commit is contained in:
Jean Chalard
2012-12-17 10:44:36 -08:00
committed by Android Git Automerger
3 changed files with 15 additions and 13 deletions

View File

@@ -33,8 +33,9 @@ public class UserDictionaryAddWordActivity extends Activity {
public static final String MODE_EDIT_ACTION = "com.android.settings.USER_DICTIONARY_EDIT"; public static final String MODE_EDIT_ACTION = "com.android.settings.USER_DICTIONARY_EDIT";
public static final String MODE_INSERT_ACTION = "com.android.settings.USER_DICTIONARY_INSERT"; public static final String MODE_INSERT_ACTION = "com.android.settings.USER_DICTIONARY_INSERT";
private static final int CODE_WORD_ADDED = 0; /* package */ static final int CODE_WORD_ADDED = 0;
private static final int CODE_CANCEL = 1; /* package */ static final int CODE_CANCEL = 1;
/* package */ static final int CODE_ALREADY_PRESENT = 2;
private UserDictionaryAddWordContents mContents; private UserDictionaryAddWordContents mContents;
@@ -73,7 +74,7 @@ public class UserDictionaryAddWordActivity extends Activity {
mContents.saveStateIntoBundle(outState); mContents.saveStateIntoBundle(outState);
} }
private void reportBackToCaller(final Bundle result) { private void reportBackToCaller(final int resultCode, final Bundle result) {
final Intent senderIntent = getIntent(); final Intent senderIntent = getIntent();
final Object listener = senderIntent.getExtras().get("listener"); final Object listener = senderIntent.getExtras().get("listener");
if (!(listener instanceof Messenger)) return; // This will work if listener is null too. if (!(listener instanceof Messenger)) return; // This will work if listener is null too.
@@ -81,7 +82,7 @@ public class UserDictionaryAddWordActivity extends Activity {
final Message m = Message.obtain(); final Message m = Message.obtain();
m.obj = result; m.obj = result;
m.what = (null != result) ? CODE_WORD_ADDED : CODE_CANCEL; m.what = resultCode;
try { try {
messenger.send(m); messenger.send(m);
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -90,12 +91,14 @@ public class UserDictionaryAddWordActivity extends Activity {
} }
public void onClickCancel(final View v) { public void onClickCancel(final View v) {
reportBackToCaller(null); reportBackToCaller(CODE_CANCEL, null);
finish(); finish();
} }
public void onClickConfirm(final View v) { public void onClickConfirm(final View v) {
reportBackToCaller(mContents.apply(this)); final Bundle parameters = new Bundle();
final int resultCode = mContents.apply(this, parameters);
reportBackToCaller(resultCode, parameters);
finish(); finish();
} }
} }

View File

@@ -103,7 +103,8 @@ public class UserDictionaryAddWordContents {
// If we are in add mode, nothing was added, so we don't need to do anything. // If we are in add mode, nothing was added, so we don't need to do anything.
} }
/* package */ Bundle apply(final Context context) { /* package */ int apply(final Context context, final Bundle outParameters) {
if (null != outParameters) saveStateIntoBundle(outParameters);
final ContentResolver resolver = context.getContentResolver(); final ContentResolver resolver = context.getContentResolver();
if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) {
// Mode edit: remove the old entry. // Mode edit: remove the old entry.
@@ -123,13 +124,13 @@ public class UserDictionaryAddWordContents {
} }
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 null; return UserDictionaryAddWordActivity.CODE_CANCEL;
} }
// If there is no shortcut, and the word already exists in the database, then we // 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 // 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 // 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. // exists with at least one shortcut, in which case it has priority on our word.
if (hasWord(newWord, context)) return null; if (hasWord(newWord, context)) return UserDictionaryAddWordActivity.CODE_ALREADY_PRESENT;
// Disallow duplicates. If the same word with no shortcut is defined, remove it; if // 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 // the same word with the same shortcut is defined, remove it; but we don't mind if
@@ -146,9 +147,7 @@ public class UserDictionaryAddWordContents {
FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut, FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut,
TextUtils.isEmpty(mLocale) ? null : Utils.createLocaleFromString(mLocale)); TextUtils.isEmpty(mLocale) ? null : Utils.createLocaleFromString(mLocale));
final Bundle returnValues = new Bundle(); return UserDictionaryAddWordActivity.CODE_WORD_ADDED;
saveStateIntoBundle(returnValues);
return returnValues;
} }
private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD }; private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD };

View File

@@ -116,7 +116,7 @@ public class UserDictionaryAddWordFragment extends Fragment
super.onPause(); super.onPause();
// We are being hidden: commit changes to the user dictionary, unless we were deleting it // We are being hidden: commit changes to the user dictionary, unless we were deleting it
if (!mIsDeleting) { if (!mIsDeleting) {
mContents.apply(getActivity()); mContents.apply(getActivity(), null);
} }
} }