2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings.inputmethod;
19 import android.app.Fragment;
20 import android.os.Bundle;
21 import android.preference.PreferenceActivity;
22 import android.view.LayoutInflater;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.AdapterView;
29 import android.widget.ArrayAdapter;
31 import com.android.settings.R;
32 import com.android.settings.inputmethod.UserDictionaryAddWordContents.LocaleRenderer;
34 import java.util.ArrayList;
35 import java.util.Locale;
38 * Fragment to add a word/shortcut to the user dictionary.
40 * As opposed to the UserDictionaryActivity, this is only invoked within Settings
41 * from the UserDictionarySettings.
43 public class UserDictionaryAddWordFragment extends Fragment
44 implements AdapterView.OnItemSelectedListener,
45 com.android.internal.app.LocalePicker.LocaleSelectionListener {
47 private static final int OPTIONS_MENU_DELETE = Menu.FIRST;
49 private UserDictionaryAddWordContents mContents;
50 private View mRootView;
51 private boolean mIsDeleting = false;
54 public void onActivityCreated(final Bundle savedInstanceState) {
55 super.onActivityCreated(savedInstanceState);
56 setHasOptionsMenu(true);
57 getActivity().getActionBar().setTitle(R.string.user_dict_settings_title);
58 // Keep the instance so that we remember mContents when configuration changes (eg rotation)
59 setRetainInstance(true);
63 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
64 final Bundle savedState) {
65 mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null);
67 // If we have a non-null mContents object, it's the old value before a configuration
68 // change (eg rotation) so we need to use its values. Otherwise, read from the arguments.
69 if (null == mContents) {
70 mContents = new UserDictionaryAddWordContents(mRootView, getArguments());
72 // We create a new mContents object to account for the new situation : a word has
73 // been added to the user dictionary when we started rotating, and we are now editing
74 // it. That means in particular if the word undergoes any change, the old version should
75 // be updated, so the mContents object needs to switch to EDIT mode if it was in
77 mContents = new UserDictionaryAddWordContents(mRootView,
78 mContents /* oldInstanceToBeEdited */);
80 getActivity().getActionBar().setSubtitle(UserDictionarySettingsUtils.getLocaleDisplayName(
81 getActivity(), mContents.getCurrentUserDictionaryLocale()));
86 public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
87 MenuItem actionItem = menu.add(0, OPTIONS_MENU_DELETE, 0, R.string.delete)
88 .setIcon(android.R.drawable.ic_menu_delete);
89 actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
90 MenuItem.SHOW_AS_ACTION_WITH_TEXT);
94 * Callback for the framework when a menu option is pressed.
96 * This class only supports the delete menu item.
97 * @param MenuItem the item that was pressed
98 * @return false to allow normal menu processing to proceed, true to consume it here
101 public boolean onOptionsItemSelected(MenuItem item) {
102 if (item.getItemId() == OPTIONS_MENU_DELETE) {
103 mContents.delete(getActivity());
105 getActivity().onBackPressed();
112 public void onResume() {
114 // We are being shown: display the word
118 private void updateSpinner() {
119 final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity());
121 final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<LocaleRenderer>(getActivity(),
122 android.R.layout.simple_spinner_item, localesList);
123 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
127 public void onPause() {
129 // We are being hidden: commit changes to the user dictionary, unless we were deleting it
131 mContents.apply(getActivity(), null);
136 public void onItemSelected(final AdapterView<?> parent, final View view, final int pos,
138 final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos);
139 if (locale.isMoreLanguages()) {
140 PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
141 preferenceActivity.startPreferenceFragment(new UserDictionaryLocalePicker(this), true);
143 mContents.updateLocale(locale.getLocaleString());
148 public void onNothingSelected(final AdapterView<?> parent) {
149 // I'm not sure we can come here, but if we do, that's the right thing to do.
150 final Bundle args = getArguments();
151 mContents.updateLocale(args.getString(UserDictionaryAddWordContents.EXTRA_LOCALE));
154 // Called by the locale picker
156 public void onLocaleSelected(final Locale locale) {
157 mContents.updateLocale(locale.toString());
158 getActivity().onBackPressed();