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.UserDictionarySettings; 25 import com.android.settings.core.PreferenceController; 26 import com.android.settings.inputmethod.UserDictionaryList; 27 28 import java.util.TreeSet; 29 30 public class UserDictionaryPreferenceController extends PreferenceController { 31 32 private static final String KEY_USER_DICTIONARY_SETTINGS = "key_user_dictionary_settings"; 33 UserDictionaryPreferenceController(Context context)34 public UserDictionaryPreferenceController(Context context) { 35 super(context); 36 } 37 38 @Override isAvailable()39 public boolean isAvailable() { 40 final TreeSet<String> localeSet = getDictionaryLocales(); 41 // The locale list is null if and only if the user dictionary service is 42 // not present or disabled. In this case we need to remove the preference. 43 return localeSet != null; 44 } 45 46 @Override getPreferenceKey()47 public String getPreferenceKey() { 48 return KEY_USER_DICTIONARY_SETTINGS; 49 } 50 51 @Override updateState(Preference preference)52 public void updateState(Preference preference) { 53 if (!isAvailable() || preference == null) { 54 return; 55 } 56 final TreeSet<String> localeSet = getDictionaryLocales(); 57 final Bundle extras = preference.getExtras(); 58 final Class<? extends Fragment> targetFragment; 59 if (localeSet.size() <= 1) { 60 if (!localeSet.isEmpty()) { 61 // If the size of localeList is 0, we don't set the locale 62 // parameter in the extras. This will be interpreted by the 63 // UserDictionarySettings class as meaning 64 // "the current locale". Note that with the current code for 65 // UserDictionaryList#getUserDictionaryLocalesSet() 66 // the locale list always has at least one element, since it 67 // always includes the current locale explicitly. 68 // @see UserDictionaryList.getUserDictionaryLocalesSet(). 69 extras.putString("locale", localeSet.first()); 70 } 71 targetFragment = UserDictionarySettings.class; 72 } else { 73 targetFragment = UserDictionaryList.class; 74 } 75 preference.setFragment(targetFragment.getCanonicalName()); 76 } 77 getDictionaryLocales()78 protected TreeSet<String> getDictionaryLocales() { 79 return UserDictionaryList.getUserDictionaryLocalesSet(mContext); 80 } 81 } 82