• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 package com.android.settings.inputmethod;
18 
19 import android.app.settings.SettingsEnums;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.Menu;
23 import android.view.MenuInflater;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ArrayAdapter;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.InstrumentedFragment;
31 import com.android.settings.inputmethod.UserDictionaryAddWordContents.LocaleRenderer;
32 
33 import java.util.ArrayList;
34 
35 /**
36  * Fragment to add a word/shortcut to the user dictionary.
37  *
38  * As opposed to the UserDictionaryActivity, this is only invoked within Settings
39  * from the UserDictionarySettings.
40  */
41 public class UserDictionaryAddWordFragment extends InstrumentedFragment {
42 
43     private static final int OPTIONS_MENU_DELETE = Menu.FIRST;
44 
45     private UserDictionaryAddWordContents mContents;
46     private View mRootView;
47     private boolean mIsDeleting = false;
48 
49     @Override
onActivityCreated(final Bundle savedInstanceState)50     public void onActivityCreated(final Bundle savedInstanceState) {
51         super.onActivityCreated(savedInstanceState);
52         setHasOptionsMenu(true);
53         // Keep the instance so that we remember mContents when configuration changes (eg rotation)
54         setRetainInstance(true);
55     }
56 
57     @Override
onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedState)58     public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
59             final Bundle savedState) {
60         mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null);
61         mIsDeleting = false;
62         // If we have a non-null mContents object, it's the old value before a configuration
63         // change (eg rotation) so we need to use its values. Otherwise, read from the arguments.
64         if (null == mContents) {
65             mContents = new UserDictionaryAddWordContents(mRootView, getArguments());
66         } else {
67             // We create a new mContents object to account for the new situation : a word has
68             // been added to the user dictionary when we started rotating, and we are now editing
69             // it. That means in particular if the word undergoes any change, the old version should
70             // be updated, so the mContents object needs to switch to EDIT mode if it was in
71             // INSERT mode.
72             mContents = new UserDictionaryAddWordContents(mRootView,
73                     mContents /* oldInstanceToBeEdited */);
74         }
75         getActivity().getActionBar().setSubtitle(UserDictionarySettingsUtils.getLocaleDisplayName(
76                 getActivity(), mContents.getCurrentUserDictionaryLocale()));
77         return mRootView;
78     }
79 
80     @Override
onCreateOptionsMenu(final Menu menu, final MenuInflater inflater)81     public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
82         MenuItem actionItem = menu.add(0, OPTIONS_MENU_DELETE, 0, R.string.delete)
83                 .setIcon(R.drawable.ic_delete);
84         actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
85                 MenuItem.SHOW_AS_ACTION_WITH_TEXT);
86     }
87 
88     /**
89      * Callback for the framework when a menu option is pressed.
90      *
91      * This class only supports the delete menu item.
92      * @param MenuItem the item that was pressed
93      * @return false to allow normal menu processing to proceed, true to consume it here
94      */
95     @Override
onOptionsItemSelected(MenuItem item)96     public boolean onOptionsItemSelected(MenuItem item) {
97         if (item.getItemId() == OPTIONS_MENU_DELETE) {
98             mContents.delete(getActivity());
99             mIsDeleting = true;
100             getActivity().onBackPressed();
101             return true;
102         }
103         return false;
104     }
105 
106     @Override
getMetricsCategory()107     public int getMetricsCategory() {
108         return SettingsEnums.INPUTMETHOD_USER_DICTIONARY_ADD_WORD;
109     }
110 
111     @Override
onResume()112     public void onResume() {
113         super.onResume();
114         // We are being shown: display the word
115         updateSpinner();
116     }
117 
updateSpinner()118     private void updateSpinner() {
119         final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity());
120 
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);
124     }
125 
126     @Override
onPause()127     public void onPause() {
128         super.onPause();
129         // We are being hidden: commit changes to the user dictionary, unless we were deleting it
130         if (!mIsDeleting) {
131             mContents.apply(getActivity(), null);
132         }
133     }
134 }
135