Merge "Call back to whom requested to add a word to the user dict" into jb-mr1.1-dev

This commit is contained in:
Mike Cleron
2012-12-14 10:57:17 -08:00
committed by Android (Google) Code Review
2 changed files with 37 additions and 4 deletions

View File

@@ -21,6 +21,9 @@ import com.android.settings.R;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View; import android.view.View;
public class UserDictionaryAddWordActivity extends Activity { public class UserDictionaryAddWordActivity extends Activity {
@@ -30,6 +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;
private static final int CODE_CANCEL = 1;
private UserDictionaryAddWordContents mContents; private UserDictionaryAddWordContents mContents;
@Override @Override
@@ -67,12 +73,29 @@ public class UserDictionaryAddWordActivity extends Activity {
mContents.saveStateIntoBundle(outState); mContents.saveStateIntoBundle(outState);
} }
private void reportBackToCaller(final Bundle result) {
final Intent senderIntent = getIntent();
final Object listener = senderIntent.getExtras().get("listener");
if (!(listener instanceof Messenger)) return; // This will work if listener is null too.
final Messenger messenger = (Messenger)listener;
final Message m = Message.obtain();
m.obj = result;
m.what = (null != result) ? CODE_WORD_ADDED : CODE_CANCEL;
try {
messenger.send(m);
} catch (RemoteException e) {
// Couldn't report back, but there is nothing we can do to fix it
}
}
public void onClickCancel(final View v) { public void onClickCancel(final View v) {
reportBackToCaller(null);
finish(); finish();
} }
public void onClickConfirm(final View v) { public void onClickConfirm(final View v) {
mContents.apply(this); reportBackToCaller(mContents.apply(this));
finish(); finish();
} }
} }

View File

@@ -43,6 +43,8 @@ public class UserDictionaryAddWordContents {
public static final String EXTRA_WORD = "word"; public static final String EXTRA_WORD = "word";
public static final String EXTRA_SHORTCUT = "shortcut"; public static final String EXTRA_SHORTCUT = "shortcut";
public static final String EXTRA_LOCALE = "locale"; 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_EDIT = 0;
public static final int MODE_INSERT = 1; public static final int MODE_INSERT = 1;
@@ -82,9 +84,13 @@ public class UserDictionaryAddWordContents {
/* package */ void saveStateIntoBundle(final Bundle outState) { /* package */ void saveStateIntoBundle(final Bundle outState) {
outState.putString(EXTRA_WORD, mWordEditText.getText().toString()); outState.putString(EXTRA_WORD, mWordEditText.getText().toString());
outState.putString(EXTRA_ORIGINAL_WORD, mOldWord);
if (null != mShortcutEditText) { if (null != mShortcutEditText) {
outState.putString(EXTRA_SHORTCUT, mShortcutEditText.getText().toString()); outState.putString(EXTRA_SHORTCUT, mShortcutEditText.getText().toString());
} }
if (null != mOldShortcut) {
outState.putString(EXTRA_ORIGINAL_SHORTCUT, mOldShortcut);
}
outState.putString(EXTRA_LOCALE, mLocale); outState.putString(EXTRA_LOCALE, mLocale);
} }
@@ -97,7 +103,7 @@ 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 */ void apply(final Context context) { /* package */ Bundle apply(final Context context) {
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.
@@ -117,13 +123,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; return null;
} }
// 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; if (hasWord(newWord, context)) return null;
// 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
@@ -139,6 +145,10 @@ public class UserDictionaryAddWordContents {
UserDictionary.Words.addWord(context, newWord.toString(), UserDictionary.Words.addWord(context, newWord.toString(),
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();
saveStateIntoBundle(returnValues);
return returnValues;
} }
private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD }; private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD };