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 android.app.Activity;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.database.Cursor;
     23 import android.os.Bundle;
     24 import android.preference.Preference;
     25 import android.preference.PreferenceFragment;
     26 import android.preference.PreferenceGroup;
     27 import android.provider.UserDictionary;
     28 import android.text.TextUtils;
     29 import android.view.inputmethod.InputMethodInfo;
     30 import android.view.inputmethod.InputMethodManager;
     31 import android.view.inputmethod.InputMethodSubtype;
     32 
     33 import com.android.inputmethod.latin.R;
     34 import com.android.inputmethod.latin.utils.LocaleUtils;
     35 
     36 import java.util.List;
     37 import java.util.Locale;
     38 import java.util.TreeSet;
     39 
     40 // Caveat: This class is basically taken from
     41 // packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryList.java
     42 // in order to deal with some devices that have issues with the user dictionary handling
     43 
     44 public class UserDictionaryList extends PreferenceFragment {
     45 
     46     public static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION =
     47             "android.settings.USER_DICTIONARY_SETTINGS";
     48 
     49     @Override
     50     public void onCreate(Bundle icicle) {
     51         super.onCreate(icicle);
     52         setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity()));
     53     }
     54 
     55     public static TreeSet<String> getUserDictionaryLocalesSet(Activity activity) {
     56         final Cursor cursor = activity.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
     57                 new String[] { UserDictionary.Words.LOCALE },
     58                 null, null, null);
     59         final TreeSet<String> localeSet = new TreeSet<>();
     60         if (null == cursor) {
     61             // The user dictionary service is not present or disabled. Return null.
     62             return null;
     63         }
     64         try {
     65             if (cursor.moveToFirst()) {
     66                 final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE);
     67                 do {
     68                     final String locale = cursor.getString(columnIndex);
     69                     localeSet.add(null != locale ? locale : "");
     70                 } while (cursor.moveToNext());
     71             }
     72         } finally {
     73             cursor.close();
     74         }
     75         if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) {
     76             // For ICS, we need to show "For all languages" in case that the keyboard locale
     77             // is different from the system locale
     78             localeSet.add("");
     79         }
     80 
     81         final InputMethodManager imm =
     82                 (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
     83         final List<InputMethodInfo> imis = imm.getEnabledInputMethodList();
     84         for (final InputMethodInfo imi : imis) {
     85             final List<InputMethodSubtype> subtypes =
     86                     imm.getEnabledInputMethodSubtypeList(
     87                             imi, true /* allowsImplicitlySelectedSubtypes */);
     88             for (InputMethodSubtype subtype : subtypes) {
     89                 final String locale = subtype.getLocale();
     90                 if (!TextUtils.isEmpty(locale)) {
     91                     localeSet.add(locale);
     92                 }
     93             }
     94         }
     95 
     96         // We come here after we have collected locales from existing user dictionary entries and
     97         // enabled subtypes. If we already have the locale-without-country version of the system
     98         // locale, we don't add the system locale to avoid confusion even though it's technically
     99         // correct to add it.
    100         if (!localeSet.contains(Locale.getDefault().getLanguage().toString())) {
    101             localeSet.add(Locale.getDefault().toString());
    102         }
    103 
    104         return localeSet;
    105     }
    106 
    107     /**
    108      * Creates the entries that allow the user to go into the user dictionary for each locale.
    109      * @param userDictGroup The group to put the settings in.
    110      */
    111     protected void createUserDictSettings(PreferenceGroup userDictGroup) {
    112         final Activity activity = getActivity();
    113         userDictGroup.removeAll();
    114         final TreeSet<String> localeSet =
    115                 UserDictionaryList.getUserDictionaryLocalesSet(activity);
    116 
    117         if (localeSet.size() > 1) {
    118             // Have an "All languages" entry in the languages list if there are two or more active
    119             // languages
    120             localeSet.add("");
    121         }
    122 
    123         if (localeSet.isEmpty()) {
    124             userDictGroup.addPreference(createUserDictionaryPreference(null, activity));
    125         } else {
    126             for (String locale : localeSet) {
    127                 userDictGroup.addPreference(createUserDictionaryPreference(locale, activity));
    128             }
    129         }
    130     }
    131 
    132     /**
    133      * Create a single User Dictionary Preference object, with its parameters set.
    134      * @param locale The locale for which this user dictionary is for.
    135      * @return The corresponding preference.
    136      */
    137     protected Preference createUserDictionaryPreference(String locale, Activity activity) {
    138         final Preference newPref = new Preference(getActivity());
    139         final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION);
    140         if (null == locale) {
    141             newPref.setTitle(Locale.getDefault().getDisplayName());
    142         } else {
    143             if ("".equals(locale))
    144                 newPref.setTitle(getString(R.string.user_dict_settings_all_languages));
    145             else
    146                 newPref.setTitle(LocaleUtils.constructLocaleFromString(locale).getDisplayName());
    147             intent.putExtra("locale", locale);
    148             newPref.getExtras().putString("locale", locale);
    149         }
    150         newPref.setIntent(intent);
    151         newPref.setFragment(UserDictionarySettings.class.getName());
    152         return newPref;
    153     }
    154 
    155     @Override
    156     public void onResume() {
    157         super.onResume();
    158         createUserDictSettings(getPreferenceScreen());
    159     }
    160 }
    161