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
17package com.android.settings.inputmethod;
18
19import android.app.Fragment;
20import android.os.Bundle;
21import android.preference.PreferenceActivity;
22import android.view.LayoutInflater;
23import android.view.Menu;
24import android.view.MenuInflater;
25import android.view.MenuItem;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.AdapterView;
29import android.widget.ArrayAdapter;
30
31import com.android.settings.R;
32import com.android.settings.inputmethod.UserDictionaryAddWordContents.LocaleRenderer;
33
34import java.util.ArrayList;
35import 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 */
43public 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
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);
60    }
61
62    @Override
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);
66        mIsDeleting = false;
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());
71        } else {
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
76            // INSERT mode.
77            mContents = new UserDictionaryAddWordContents(mRootView,
78                    mContents /* oldInstanceToBeEdited */);
79        }
80        getActivity().getActionBar().setSubtitle(UserDictionarySettingsUtils.getLocaleDisplayName(
81                getActivity(), mContents.getCurrentUserDictionaryLocale()));
82        return mRootView;
83    }
84
85    @Override
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);
91    }
92
93    /**
94     * Callback for the framework when a menu option is pressed.
95     *
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
99     */
100    @Override
101    public boolean onOptionsItemSelected(MenuItem item) {
102        if (item.getItemId() == OPTIONS_MENU_DELETE) {
103            mContents.delete(getActivity());
104            mIsDeleting = true;
105            getActivity().onBackPressed();
106            return true;
107        }
108        return false;
109    }
110
111    @Override
112    public void onResume() {
113        super.onResume();
114        // We are being shown: display the word
115        updateSpinner();
116    }
117
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
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    @Override
136    public void onItemSelected(final AdapterView<?> parent, final View view, final int pos,
137            final long id) {
138        final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos);
139        if (locale.isMoreLanguages()) {
140            PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
141            preferenceActivity.startPreferenceFragment(new UserDictionaryLocalePicker(this), true);
142        } else {
143            mContents.updateLocale(locale.getLocaleString());
144        }
145    }
146
147    @Override
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));
152    }
153
154    // Called by the locale picker
155    @Override
156    public void onLocaleSelected(final Locale locale) {
157        mContents.updateLocale(locale.toString());
158        getActivity().onBackPressed();
159    }
160}
161