Home | History | Annotate | Download | only in inputmethod
      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.os.Bundle;
     20 import android.view.LayoutInflater;
     21 import android.view.Menu;
     22 import android.view.MenuInflater;
     23 import android.view.MenuItem;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.AdapterView;
     27 import android.widget.ArrayAdapter;
     28 
     29 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     30 import com.android.settings.core.InstrumentedPreferenceFragment;
     31 import com.android.settings.R;
     32 import com.android.settings.SettingsActivity;
     33 import com.android.settings.inputmethod.UserDictionaryAddWordContents.LocaleRenderer;
     34 
     35 import java.util.ArrayList;
     36 import java.util.Locale;
     37 
     38 /**
     39  * Fragment to add a word/shortcut to the user dictionary.
     40  *
     41  * As opposed to the UserDictionaryActivity, this is only invoked within Settings
     42  * from the UserDictionarySettings.
     43  */
     44 public class UserDictionaryAddWordFragment extends InstrumentedPreferenceFragment
     45         implements AdapterView.OnItemSelectedListener,
     46         com.android.internal.app.LocalePicker.LocaleSelectionListener {
     47 
     48     private static final int OPTIONS_MENU_DELETE = Menu.FIRST;
     49 
     50     private UserDictionaryAddWordContents mContents;
     51     private View mRootView;
     52     private boolean mIsDeleting = false;
     53 
     54     @Override
     55     public void onActivityCreated(final Bundle savedInstanceState) {
     56         super.onActivityCreated(savedInstanceState);
     57         setHasOptionsMenu(true);
     58         getActivity().getActionBar().setTitle(R.string.user_dict_settings_title);
     59         // Keep the instance so that we remember mContents when configuration changes (eg rotation)
     60         setRetainInstance(true);
     61     }
     62 
     63     @Override
     64     public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
     65             final Bundle savedState) {
     66         mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null);
     67         mIsDeleting = false;
     68         // If we have a non-null mContents object, it's the old value before a configuration
     69         // change (eg rotation) so we need to use its values. Otherwise, read from the arguments.
     70         if (null == mContents) {
     71             mContents = new UserDictionaryAddWordContents(mRootView, getArguments());
     72         } else {
     73             // We create a new mContents object to account for the new situation : a word has
     74             // been added to the user dictionary when we started rotating, and we are now editing
     75             // it. That means in particular if the word undergoes any change, the old version should
     76             // be updated, so the mContents object needs to switch to EDIT mode if it was in
     77             // INSERT mode.
     78             mContents = new UserDictionaryAddWordContents(mRootView,
     79                     mContents /* oldInstanceToBeEdited */);
     80         }
     81         getActivity().getActionBar().setSubtitle(UserDictionarySettingsUtils.getLocaleDisplayName(
     82                 getActivity(), mContents.getCurrentUserDictionaryLocale()));
     83         return mRootView;
     84     }
     85 
     86     @Override
     87     public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
     88         MenuItem actionItem = menu.add(0, OPTIONS_MENU_DELETE, 0, R.string.delete)
     89                 .setIcon(R.drawable.ic_delete);
     90         actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
     91                 MenuItem.SHOW_AS_ACTION_WITH_TEXT);
     92     }
     93 
     94     /**
     95      * Callback for the framework when a menu option is pressed.
     96      *
     97      * This class only supports the delete menu item.
     98      * @param MenuItem the item that was pressed
     99      * @return false to allow normal menu processing to proceed, true to consume it here
    100      */
    101     @Override
    102     public boolean onOptionsItemSelected(MenuItem item) {
    103         if (item.getItemId() == OPTIONS_MENU_DELETE) {
    104             mContents.delete(getActivity());
    105             mIsDeleting = true;
    106             getActivity().onBackPressed();
    107             return true;
    108         }
    109         return false;
    110     }
    111 
    112     @Override
    113     public int getMetricsCategory() {
    114         return MetricsEvent.INPUTMETHOD_USER_DICTIONARY_ADD_WORD;
    115     }
    116 
    117     @Override
    118     public void onResume() {
    119         super.onResume();
    120         // We are being shown: display the word
    121         updateSpinner();
    122     }
    123 
    124     private void updateSpinner() {
    125         final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity());
    126 
    127         final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<LocaleRenderer>(getActivity(),
    128                 android.R.layout.simple_spinner_item, localesList);
    129         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    130     }
    131 
    132     @Override
    133     public void onPause() {
    134         super.onPause();
    135         // We are being hidden: commit changes to the user dictionary, unless we were deleting it
    136         if (!mIsDeleting) {
    137             mContents.apply(getActivity(), null);
    138         }
    139     }
    140 
    141     @Override
    142     public void onItemSelected(final AdapterView<?> parent, final View view, final int pos,
    143             final long id) {
    144         final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos);
    145         if (locale.isMoreLanguages()) {
    146             SettingsActivity sa = (SettingsActivity)getActivity();
    147             sa.startPreferenceFragment(new UserDictionaryLocalePicker(this), true);
    148         } else {
    149             mContents.updateLocale(locale.getLocaleString());
    150         }
    151     }
    152 
    153     @Override
    154     public void onNothingSelected(final AdapterView<?> parent) {
    155         // I'm not sure we can come here, but if we do, that's the right thing to do.
    156         final Bundle args = getArguments();
    157         mContents.updateLocale(args.getString(UserDictionaryAddWordContents.EXTRA_LOCALE));
    158     }
    159 
    160     // Called by the locale picker
    161     @Override
    162     public void onLocaleSelected(final Locale locale) {
    163         mContents.updateLocale(locale.toString());
    164         getActivity().onBackPressed();
    165     }
    166 }
    167