• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.settings;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.preference.ListPreference;
28 import android.preference.Preference;
29 
30 import com.android.inputmethod.dictionarypack.DictionarySettingsActivity;
31 import com.android.inputmethod.latin.R;
32 import com.android.inputmethod.latin.userdictionary.UserDictionaryList;
33 import com.android.inputmethod.latin.userdictionary.UserDictionarySettings;
34 
35 import java.util.TreeSet;
36 
37 /**
38  * "Text correction" settings sub screen.
39  *
40  * This settings sub screen handles the following text correction preferences.
41  * - Personal dictionary
42  * - Add-on dictionaries
43  * - Block offensive words
44  * - Auto-correction
45  * - Show correction suggestions
46  * - Personalized suggestions
47  * - Suggest Contact names
48  * - Next-word suggestions
49  */
50 public final class CorrectionSettingsFragment extends SubScreenFragment {
51     private static final boolean DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS = false;
52     private static final boolean USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS =
53             DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS
54             || Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2;
55 
56     @Override
onCreate(final Bundle icicle)57     public void onCreate(final Bundle icicle) {
58         super.onCreate(icicle);
59         addPreferencesFromResource(R.xml.prefs_screen_correction);
60 
61         final Context context = getActivity();
62         final PackageManager pm = context.getPackageManager();
63 
64         ensureConsistencyOfAutoCorrectionSettings();
65 
66         final Preference dictionaryLink = findPreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY);
67         final Intent intent = dictionaryLink.getIntent();
68         intent.setClassName(context.getPackageName(), DictionarySettingsActivity.class.getName());
69         final int number = pm.queryIntentActivities(intent, 0).size();
70         if (0 >= number) {
71             removePreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY);
72         }
73 
74         final Preference editPersonalDictionary =
75                 findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY);
76         final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent();
77         final ResolveInfo ri = USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS ? null
78                 : pm.resolveActivity(
79                         editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY);
80         if (ri == null) {
81             overwriteUserDictionaryPreference(editPersonalDictionary);
82         }
83     }
84 
85     @Override
onSharedPreferenceChanged(final SharedPreferences prefs, final String key)86     public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
87         ensureConsistencyOfAutoCorrectionSettings();
88     }
89 
ensureConsistencyOfAutoCorrectionSettings()90     private void ensureConsistencyOfAutoCorrectionSettings() {
91         final String autoCorrectionOff = getString(
92                 R.string.auto_correction_threshold_mode_index_off);
93         final ListPreference autoCorrectionThresholdPref = (ListPreference)findPreference(
94                 Settings.PREF_AUTO_CORRECTION_THRESHOLD);
95         final String currentSetting = autoCorrectionThresholdPref.getValue();
96         setPreferenceEnabled(
97                 Settings.PREF_BIGRAM_PREDICTIONS, !currentSetting.equals(autoCorrectionOff));
98     }
99 
overwriteUserDictionaryPreference(final Preference userDictionaryPreference)100     private void overwriteUserDictionaryPreference(final Preference userDictionaryPreference) {
101         final Activity activity = getActivity();
102         final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity);
103         if (null == localeList) {
104             // The locale list is null if and only if the user dictionary service is
105             // not present or disabled. In this case we need to remove the preference.
106             getPreferenceScreen().removePreference(userDictionaryPreference);
107         } else if (localeList.size() <= 1) {
108             userDictionaryPreference.setFragment(UserDictionarySettings.class.getName());
109             // If the size of localeList is 0, we don't set the locale parameter in the
110             // extras. This will be interpreted by the UserDictionarySettings class as
111             // meaning "the current locale".
112             // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet()
113             // the locale list always has at least one element, since it always includes the current
114             // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet().
115             if (localeList.size() == 1) {
116                 final String locale = (String)localeList.toArray()[0];
117                 userDictionaryPreference.getExtras().putString("locale", locale);
118             }
119         } else {
120             userDictionaryPreference.setFragment(UserDictionaryList.class.getName());
121         }
122     }
123 }
124