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.content.Context;
20 import android.os.Bundle;
21 
22 import androidx.fragment.app.Fragment;
23 import androidx.preference.Preference;
24 
25 import com.android.settings.core.BasePreferenceController;
26 import com.android.settings.inputmethod.UserDictionaryList;
27 import com.android.settings.inputmethod.UserDictionaryListPreferenceController;
28 import com.android.settings.inputmethod.UserDictionarySettings;
29 
30 import java.util.TreeSet;
31 
32 public class UserDictionaryPreferenceController extends BasePreferenceController {
33 
UserDictionaryPreferenceController(Context context, String key)34     public UserDictionaryPreferenceController(Context context, String key) {
35         super(context, key);
36     }
37 
38     @Override
getAvailabilityStatus()39     public int getAvailabilityStatus() {
40         return AVAILABLE;
41     }
42 
43     @Override
updateState(Preference preference)44     public void updateState(Preference preference) {
45         if (!isAvailable() || preference == null) {
46             return;
47         }
48         final TreeSet<String> localeSet = getDictionaryLocales();
49         final Bundle extras = preference.getExtras();
50         final Class<? extends Fragment> targetFragment;
51         if (localeSet.size() <= 1) {
52             if (!localeSet.isEmpty()) {
53                 // If the size of localeList is 0, we don't set the locale
54                 // parameter in the extras. This will be interpreted by the
55                 // UserDictionarySettings class as meaning
56                 // "the current locale". Note that with the current code for
57                 // UserDictionaryListPreferenceController#getUserDictionaryLocalesSet()
58                 // the locale list always has at least one element, since it
59                 // always includes the current locale explicitly.
60                 // @see UserDictionaryListPreferenceController.getUserDictionaryLocalesSet().
61                 extras.putString("locale", localeSet.first());
62             }
63             targetFragment = UserDictionarySettings.class;
64         } else {
65             targetFragment = UserDictionaryList.class;
66         }
67         preference.setFragment(targetFragment.getCanonicalName());
68     }
69 
getDictionaryLocales()70     protected TreeSet<String> getDictionaryLocales() {
71         return UserDictionaryListPreferenceController.getUserDictionaryLocalesSet(mContext);
72     }
73 }
74