Home | History | Annotate | Download | only in inputmethod
      1 /*
      2  * Copyright (C) 2011 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 com.android.settings.R;
     20 import com.android.settings.SettingsPreferenceFragment;
     21 import com.android.settings.Utils;
     22 
     23 import android.app.Activity;
     24 import android.content.Intent;
     25 import android.database.Cursor;
     26 import android.os.Bundle;
     27 import android.preference.Preference;
     28 import android.preference.PreferenceGroup;
     29 import android.provider.UserDictionary;
     30 
     31 import java.util.Locale;
     32 import java.util.Set;
     33 import java.util.TreeSet;
     34 
     35 public class UserDictionaryList extends SettingsPreferenceFragment {
     36 
     37     public static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION =
     38             "android.settings.USER_DICTIONARY_SETTINGS";
     39 
     40     @Override
     41     public void onCreate(Bundle icicle) {
     42         super.onCreate(icicle);
     43         setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity()));
     44     }
     45 
     46     static Set<String> getUserDictionaryLocalesList(Activity activity) {
     47         @SuppressWarnings("deprecation")
     48         final Cursor cursor = activity.managedQuery(UserDictionary.Words.CONTENT_URI,
     49                 new String[] { UserDictionary.Words.LOCALE },
     50                 null, null, null);
     51         final Set<String> localeList = new TreeSet<String>();
     52         if (null == cursor) {
     53             // The user dictionary service is not present or disabled. Return null.
     54             return null;
     55         } else if (cursor.moveToFirst()) {
     56             final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE);
     57             do {
     58                 String locale = cursor.getString(columnIndex);
     59                 localeList.add(null != locale ? locale : "");
     60             } while (cursor.moveToNext());
     61         }
     62         localeList.add(Locale.getDefault().toString());
     63         return localeList;
     64     }
     65 
     66     /**
     67      * Creates the entries that allow the user to go into the user dictionary for each locale.
     68      * @param userDictGroup The group to put the settings in.
     69      */
     70     protected void createUserDictSettings(PreferenceGroup userDictGroup) {
     71         final Activity activity = getActivity();
     72         userDictGroup.removeAll();
     73         final Set<String> localeList = UserDictionaryList.getUserDictionaryLocalesList(activity);
     74 
     75         if (localeList.isEmpty()) {
     76             userDictGroup.addPreference(createUserDictionaryPreference(null, activity));
     77         } else {
     78             for (String locale : localeList) {
     79                 userDictGroup.addPreference(createUserDictionaryPreference(locale, activity));
     80             }
     81         }
     82     }
     83 
     84     /**
     85      * Create a single User Dictionary Preference object, with its parameters set.
     86      * @param locale The locale for which this user dictionary is for.
     87      * @return The corresponding preference.
     88      */
     89     protected Preference createUserDictionaryPreference(String locale, Activity activity) {
     90         final Preference newPref = new Preference(getActivity());
     91         final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION);
     92         if (null == locale) {
     93             newPref.setTitle(Locale.getDefault().getDisplayName());
     94         } else {
     95             if ("".equals(locale))
     96                 newPref.setTitle(getString(R.string.user_dict_settings_all_languages));
     97             else
     98                 newPref.setTitle(Utils.createLocaleFromString(locale).getDisplayName());
     99             intent.putExtra("locale", locale);
    100             newPref.getExtras().putString("locale", locale);
    101         }
    102         newPref.setIntent(intent);
    103         return newPref;
    104     }
    105 
    106     @Override
    107     public void onResume() {
    108         super.onResume();
    109         createUserDictSettings(getPreferenceScreen());
    110     }
    111 }
    112