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