• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.language;
18 
19 import android.app.Fragment;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.support.v7.preference.Preference;
23 
24 import com.android.settings.core.PreferenceControllerMixin;
25 import com.android.settings.inputmethod.UserDictionaryList;
26 import com.android.settings.inputmethod.UserDictionarySettings;
27 import com.android.settingslib.core.AbstractPreferenceController;
28 
29 import java.util.TreeSet;
30 
31 public class UserDictionaryPreferenceController extends AbstractPreferenceController
32         implements PreferenceControllerMixin {
33 
34     private static final String KEY_USER_DICTIONARY_SETTINGS = "key_user_dictionary_settings";
35 
UserDictionaryPreferenceController(Context context)36     public UserDictionaryPreferenceController(Context context) {
37         super(context);
38     }
39 
40     @Override
isAvailable()41     public boolean isAvailable() {
42         return true;
43     }
44 
45     @Override
getPreferenceKey()46     public String getPreferenceKey() {
47         return KEY_USER_DICTIONARY_SETTINGS;
48     }
49 
50     @Override
updateState(Preference preference)51     public void updateState(Preference preference) {
52         if (!isAvailable() || preference == null) {
53             return;
54         }
55         final TreeSet<String> localeSet = getDictionaryLocales();
56         final Bundle extras = preference.getExtras();
57         final Class<? extends Fragment> targetFragment;
58         if (localeSet.size() <= 1) {
59             if (!localeSet.isEmpty()) {
60                 // If the size of localeList is 0, we don't set the locale
61                 // parameter in the extras. This will be interpreted by the
62                 // UserDictionarySettings class as meaning
63                 // "the current locale". Note that with the current code for
64                 // UserDictionaryList#getUserDictionaryLocalesSet()
65                 // the locale list always has at least one element, since it
66                 // always includes the current locale explicitly.
67                 // @see UserDictionaryList.getUserDictionaryLocalesSet().
68                 extras.putString("locale", localeSet.first());
69             }
70             targetFragment = UserDictionarySettings.class;
71         } else {
72             targetFragment = UserDictionaryList.class;
73         }
74         preference.setFragment(targetFragment.getCanonicalName());
75     }
76 
getDictionaryLocales()77     protected TreeSet<String> getDictionaryLocales() {
78         return UserDictionaryList.getUserDictionaryLocalesSet(mContext);
79     }
80 }
81