Merge "Implement the delete button for user words" into jb-dev

This commit is contained in:
Jean Chalard
2012-05-10 00:28:15 -07:00
committed by Android (Google) Code Review
2 changed files with 34 additions and 3 deletions

View File

@@ -87,9 +87,18 @@ public class UserDictionaryAddWordContents {
outState.putString(EXTRA_LOCALE, mLocale); outState.putString(EXTRA_LOCALE, mLocale);
} }
/* package */ void delete(final Context context) {
if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) {
// Mode edit: remove the old entry.
final ContentResolver resolver = context.getContentResolver();
UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver);
}
// If we are in add mode, nothing was added, so we don't need to do anything.
}
/* 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 (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) {
// Mode edit: remove the old entry. // Mode edit: remove the old entry.
UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver);
} }

View File

@@ -45,6 +45,7 @@ public class UserDictionaryAddWordFragment extends Fragment
private UserDictionaryAddWordContents mContents; private UserDictionaryAddWordContents mContents;
private View mRootView; private View mRootView;
private boolean mIsDeleting = false;
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
@@ -55,6 +56,7 @@ public class UserDictionaryAddWordFragment extends Fragment
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null); mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null);
mIsDeleting = false;
return mRootView; return mRootView;
} }
@@ -66,6 +68,24 @@ public class UserDictionaryAddWordFragment extends Fragment
MenuItem.SHOW_AS_ACTION_WITH_TEXT); MenuItem.SHOW_AS_ACTION_WITH_TEXT);
} }
/**
* Callback for the framework when a menu option is pressed.
*
* This class only supports the delete menu item.
* @param MenuItem the item that was pressed
* @return false to allow normal menu processing to proceed, true to consume it here
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == OPTIONS_MENU_DELETE) {
mContents.delete(getActivity());
mIsDeleting = true;
getFragmentManager().popBackStack();
return true;
}
return false;
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
@@ -85,8 +105,10 @@ public class UserDictionaryAddWordFragment extends Fragment
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
mContents.apply(getActivity()); // 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 if (!mIsDeleting) {
mContents.apply(getActivity());
}
} }
@Override @Override