• 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 package com.android.settings.inputmethod;
17 
18 import android.app.Fragment;
19 import android.os.Bundle;
20 import android.preference.PreferenceActivity;
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.AdapterView;
28 import android.widget.ArrayAdapter;
29 import android.widget.Spinner;
30 
31 import com.android.settings.R;
32 import com.android.settings.inputmethod.UserDictionaryAddWordContents.LocaleRenderer;
33 
34 import java.util.ArrayList;
35 import java.util.Locale;
36 
37 /**
38  * Fragment to add a word/shortcut to the user dictionary.
39  *
40  * As opposed to the UserDictionaryActivity, this is only invoked within Settings
41  * from the UserDictionarySettings.
42  */
43 public class UserDictionaryAddWordFragment extends Fragment
44         implements AdapterView.OnItemSelectedListener,
45         com.android.internal.app.LocalePicker.LocaleSelectionListener {
46 
47     private static final int OPTIONS_MENU_DELETE = Menu.FIRST;
48 
49     private UserDictionaryAddWordContents mContents;
50     private View mRootView;
51     private boolean mIsDeleting = false;
52 
53     @Override
onActivityCreated(Bundle savedInstanceState)54     public void onActivityCreated(Bundle savedInstanceState) {
55         super.onActivityCreated(savedInstanceState);
56         setHasOptionsMenu(true);
57     }
58 
59     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState)60     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
61         mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null);
62         mIsDeleting = false;
63         if (null == mContents) {
64             mContents = new UserDictionaryAddWordContents(mRootView, getArguments());
65         }
66         return mRootView;
67     }
68 
69     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)70     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
71         MenuItem actionItem = menu.add(0, OPTIONS_MENU_DELETE, 0, R.string.delete)
72                 .setIcon(android.R.drawable.ic_menu_delete);
73         actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
74                 MenuItem.SHOW_AS_ACTION_WITH_TEXT);
75     }
76 
77     /**
78      * Callback for the framework when a menu option is pressed.
79      *
80      * This class only supports the delete menu item.
81      * @param MenuItem the item that was pressed
82      * @return false to allow normal menu processing to proceed, true to consume it here
83      */
84     @Override
onOptionsItemSelected(MenuItem item)85     public boolean onOptionsItemSelected(MenuItem item) {
86         if (item.getItemId() == OPTIONS_MENU_DELETE) {
87             mContents.delete(getActivity());
88             mIsDeleting = true;
89             getActivity().onBackPressed();
90             return true;
91         }
92         return false;
93     }
94 
95     @Override
onResume()96     public void onResume() {
97         super.onResume();
98         // We are being shown: display the word
99         updateSpinner();
100     }
101 
updateSpinner()102     private void updateSpinner() {
103         final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity());
104 
105         final Spinner localeSpinner =
106                 (Spinner)mRootView.findViewById(R.id.user_dictionary_add_locale);
107         final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<LocaleRenderer>(getActivity(),
108                 android.R.layout.simple_spinner_item, localesList);
109         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
110         localeSpinner.setAdapter(adapter);
111         localeSpinner.setOnItemSelectedListener(this);
112     }
113 
114     @Override
onPause()115     public void onPause() {
116         super.onPause();
117         // We are being hidden: commit changes to the user dictionary, unless we were deleting it
118         if (!mIsDeleting) {
119             mContents.apply(getActivity());
120         }
121     }
122 
123     @Override
onItemSelected(final AdapterView<?> parent, final View view, final int pos, final long id)124     public void onItemSelected(final AdapterView<?> parent, final View view, final int pos,
125             final long id) {
126         final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos);
127         if (locale.isMoreLanguages()) {
128             PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
129             preferenceActivity.startPreferenceFragment(new UserDictionaryLocalePicker(this), true);
130         } else {
131             mContents.updateLocale(locale.getLocaleString());
132         }
133     }
134 
135     @Override
onNothingSelected(final AdapterView<?> parent)136     public void onNothingSelected(final AdapterView<?> parent) {
137         // I'm not sure we can come here, but if we do, that's the right thing to do.
138         final Bundle args = getArguments();
139         mContents.updateLocale(args.getString(UserDictionaryAddWordContents.EXTRA_LOCALE));
140     }
141 
142     // Called by the locale picker
143     @Override
onLocaleSelected(final Locale locale)144     public void onLocaleSelected(final Locale locale) {
145         mContents.updateLocale(locale.toString());
146         getActivity().onBackPressed();
147     }
148 }
149