• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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.provider.UserDictionary;
25 import android.support.annotation.NonNull;
26 import android.support.v7.preference.Preference;
27 import android.support.v7.preference.PreferenceGroup;
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.internal.logging.nano.MetricsProto.MetricsEvent;
34 import com.android.settings.R;
35 import com.android.settings.SettingsPreferenceFragment;
36 import com.android.settings.Utils;
37 
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.TreeSet;
41 
42 public class UserDictionaryList extends SettingsPreferenceFragment {
43     public static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION =
44             "android.settings.USER_DICTIONARY_SETTINGS";
45     private String mLocale;
46 
47     @Override
getMetricsCategory()48     public int getMetricsCategory() {
49         return MetricsEvent.INPUTMETHOD_USER_DICTIONARY;
50     }
51 
52     @Override
onCreate(Bundle icicle)53     public void onCreate(Bundle icicle) {
54         super.onCreate(icicle);
55         setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity()));
56     }
57 
58     @Override
onActivityCreated(Bundle savedInstanceState)59     public void onActivityCreated(Bundle savedInstanceState) {
60         super.onActivityCreated(savedInstanceState);
61         getActivity().getActionBar().setTitle(R.string.user_dict_settings_title);
62 
63         final Intent intent = getActivity().getIntent();
64         final String localeFromIntent =
65                 null == intent ? null : intent.getStringExtra("locale");
66 
67         final Bundle arguments = getArguments();
68         final String localeFromArguments =
69                 null == arguments ? null : arguments.getString("locale");
70 
71         final String locale;
72         if (null != localeFromArguments) {
73             locale = localeFromArguments;
74         } else if (null != localeFromIntent) {
75             locale = localeFromIntent;
76         } else {
77             locale = null;
78         }
79         mLocale = locale;
80     }
81 
82     @NonNull
getUserDictionaryLocalesSet(Context context)83     public static TreeSet<String> getUserDictionaryLocalesSet(Context context) {
84         final Cursor cursor = context.getContentResolver().query(
85                 UserDictionary.Words.CONTENT_URI, new String[]{UserDictionary.Words.LOCALE},
86                 null, null, null);
87         final TreeSet<String> localeSet = new TreeSet<>();
88         if (cursor == null) {
89             // The user dictionary service is not present or disabled. Return empty set.
90             return localeSet;
91         }
92         try {
93             if (cursor.moveToFirst()) {
94                 final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE);
95                 do {
96                     final String locale = cursor.getString(columnIndex);
97                     localeSet.add(null != locale ? locale : "");
98                 } while (cursor.moveToNext());
99             }
100         } finally {
101             cursor.close();
102         }
103 
104         // CAVEAT: Keep this for consistency of the implementation between Keyboard and Settings
105         // if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) {
106         //     // For ICS, we need to show "For all languages" in case that the keyboard locale
107         //     // is different from the system locale
108         //     localeSet.add("");
109         // }
110 
111         final InputMethodManager imm =
112                 (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
113         final List<InputMethodInfo> imis = imm.getEnabledInputMethodList();
114         for (final InputMethodInfo imi : imis) {
115             final List<InputMethodSubtype> subtypes =
116                     imm.getEnabledInputMethodSubtypeList(
117                             imi, true /* allowsImplicitlySelectedSubtypes */);
118             for (InputMethodSubtype subtype : subtypes) {
119                 final String locale = subtype.getLocale();
120                 if (!TextUtils.isEmpty(locale)) {
121                     localeSet.add(locale);
122                 }
123             }
124         }
125 
126         // We come here after we have collected locales from existing user dictionary entries and
127         // enabled subtypes. If we already have the locale-without-country version of the system
128         // locale, we don't add the system locale to avoid confusion even though it's technically
129         // correct to add it.
130         if (!localeSet.contains(Locale.getDefault().getLanguage().toString())) {
131             localeSet.add(Locale.getDefault().toString());
132         }
133 
134         return localeSet;
135     }
136 
137     /**
138      * Creates the entries that allow the user to go into the user dictionary for each locale.
139      *
140      * @param userDictGroup The group to put the settings in.
141      */
createUserDictSettings(PreferenceGroup userDictGroup)142     protected void createUserDictSettings(PreferenceGroup userDictGroup) {
143         final Activity activity = getActivity();
144         userDictGroup.removeAll();
145         final TreeSet<String> localeSet =
146                 UserDictionaryList.getUserDictionaryLocalesSet(activity);
147         if (mLocale != null) {
148             // If the caller explicitly specify empty string as a locale, we'll show "all languages"
149             // in the list.
150             localeSet.add(mLocale);
151         }
152         if (localeSet.size() > 1) {
153             // Have an "All languages" entry in the languages list if there are two or more active
154             // languages
155             localeSet.add("");
156         }
157 
158         if (localeSet.isEmpty()) {
159             userDictGroup.addPreference(createUserDictionaryPreference(null, activity));
160         } else {
161             for (String locale : localeSet) {
162                 userDictGroup.addPreference(createUserDictionaryPreference(locale, activity));
163             }
164         }
165     }
166 
167     /**
168      * Create a single User Dictionary Preference object, with its parameters set.
169      *
170      * @param locale The locale for which this user dictionary is for.
171      * @return The corresponding preference.
172      */
createUserDictionaryPreference(String locale, Activity activity)173     protected Preference createUserDictionaryPreference(String locale, Activity activity) {
174         final Preference newPref = new Preference(getPrefContext());
175         final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION);
176         if (null == locale) {
177             newPref.setTitle(Locale.getDefault().getDisplayName());
178         } else {
179             if ("".equals(locale)) {
180                 newPref.setTitle(getString(R.string.user_dict_settings_all_languages));
181             } else {
182                 newPref.setTitle(Utils.createLocaleFromString(locale).getDisplayName());
183             }
184             intent.putExtra("locale", locale);
185             newPref.getExtras().putString("locale", locale);
186         }
187         newPref.setIntent(intent);
188         newPref.setFragment(UserDictionarySettings.class.getName());
189         return newPref;
190     }
191 
192     @Override
onResume()193     public void onResume() {
194         super.onResume();
195         createUserDictSettings(getPreferenceScreen());
196     }
197 }
198