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.TreeSet;
     33 
     34 public class UserDictionaryList extends SettingsPreferenceFragment {
     35 
     36     public static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION =
     37             "android.settings.USER_DICTIONARY_SETTINGS";
     38 
     39     @Override
     40     public void onCreate(Bundle icicle) {
     41         super.onCreate(icicle);
     42         setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity()));
     43     }
     44 
     45     static TreeSet<String> getUserDictionaryLocalesSet(Activity activity) {
     46         @SuppressWarnings("deprecation")
     47         final Cursor cursor = activity.managedQuery(UserDictionary.Words.CONTENT_URI,
     48                 new String[] { UserDictionary.Words.LOCALE },
     49                 null, null, null);
     50         final TreeSet<String> localeList = new TreeSet<String>();
     51         if (null == cursor) {
     52             // The user dictionary service is not present or disabled. Return null.
     53             return null;
     54         } else if (cursor.moveToFirst()) {
     55             final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE);
     56             do {
     57                 String locale = cursor.getString(columnIndex);
     58                 localeList.add(null != locale ? locale : "");
     59             } while (cursor.moveToNext());
     60         }
     61         localeList.add(Locale.getDefault().toString());
     62         return localeList;
     63     }
     64 
     65     /**
     66      * Creates the entries that allow the user to go into the user dictionary for each locale.
     67      * @param userDictGroup The group to put the settings in.
     68      */
     69     protected void createUserDictSettings(PreferenceGroup userDictGroup) {
     70         final Activity activity = getActivity();
     71         userDictGroup.removeAll();
     72         final TreeSet<String> localeList =
     73                 UserDictionaryList.getUserDictionaryLocalesSet(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         newPref.setFragment(com.android.settings.UserDictionarySettings.class.getName());
    104         return newPref;
    105     }
    106 
    107     @Override
    108     public void onResume() {
    109         super.onResume();
    110         createUserDictSettings(getPreferenceScreen());
    111     }
    112 }
    113