UserDictionaryAddWordFragment.java revision a0868ffd5ddcb1fa23c5b6e4b1043e40611bbd34
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 */
16package com.android.settings.inputmethod;
17
18import android.app.Fragment;
19import android.os.Bundle;
20import android.view.LayoutInflater;
21import android.view.Menu;
22import android.view.MenuInflater;
23import android.view.MenuItem;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.AdapterView;
27import android.widget.ArrayAdapter;
28import android.widget.Spinner;
29
30import com.android.settings.R;
31import com.android.settings.inputmethod.UserDictionaryAddWordContents.LocaleRenderer;
32
33import 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 */
41public class UserDictionaryAddWordFragment extends Fragment
42        implements AdapterView.OnItemSelectedListener {
43
44    private static final int OPTIONS_MENU_DELETE = Menu.FIRST;
45
46    private UserDictionaryAddWordContents mContents;
47    private View mRootView;
48
49    @Override
50    public void onActivityCreated(Bundle savedInstanceState) {
51        super.onActivityCreated(savedInstanceState);
52        setHasOptionsMenu(true);
53    }
54
55    @Override
56    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
57        mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null);
58        return mRootView;
59    }
60
61    @Override
62    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
63        MenuItem actionItem = menu.add(0, OPTIONS_MENU_DELETE, 0, R.string.delete)
64                .setIcon(android.R.drawable.ic_menu_delete);
65        actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
66                MenuItem.SHOW_AS_ACTION_WITH_TEXT);
67    }
68
69    @Override
70    public void onResume() {
71        super.onResume();
72        // We are being shown: display the word
73        mContents = new UserDictionaryAddWordContents(mRootView, getArguments());
74        final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity());
75
76        final Spinner localeSpinner =
77                (Spinner)mRootView.findViewById(R.id.user_dictionary_settings_add_dialog_locale);
78        final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<LocaleRenderer>(getActivity(),
79                android.R.layout.simple_spinner_item, localesList);
80        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
81        localeSpinner.setAdapter(adapter);
82        localeSpinner.setOnItemSelectedListener(this);
83    }
84
85    @Override
86    public void onPause() {
87        super.onPause();
88        mContents.apply(getActivity());
89        // We are being hidden: commit changes to the user dictionary
90    }
91
92    @Override
93    public void onItemSelected(final AdapterView<?> parent, final View view, final int pos,
94            final long id) {
95        final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos);
96        mContents.updateLocale(locale.getLocaleString());
97    }
98
99    @Override
100    public void onNothingSelected(final AdapterView<?> parent) {
101        // I'm not sure we can come here, but if we do, that's the right thing to do.
102        final Bundle args = getArguments();
103        mContents.updateLocale(args.getString(UserDictionaryAddWordContents.EXTRA_LOCALE));
104    }
105}
106