1 /*
2  * Copyright (C) 2019 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.ComponentName;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.provider.Settings;
23 import android.text.TextUtils;
24 import android.view.inputmethod.InputMethodInfo;
25 import android.view.inputmethod.InputMethodManager;
26 
27 import com.android.settings.core.BasePreferenceController;
28 
29 import java.util.List;
30 
31 public class LanguageAndInputPreferenceController extends BasePreferenceController {
32 
33     private PackageManager mPackageManager;
34     private InputMethodManager mInputMethodManager;
35 
LanguageAndInputPreferenceController(Context context, String key)36     public LanguageAndInputPreferenceController(Context context, String key) {
37         super(context, key);
38         mPackageManager = mContext.getPackageManager();
39         mInputMethodManager = mContext.getSystemService(InputMethodManager.class);
40     }
41 
42     @Override
getAvailabilityStatus()43     public int getAvailabilityStatus() {
44         return AVAILABLE;
45     }
46 
47     @Override
getSummary()48     public CharSequence getSummary() {
49         final String flattenComponent = Settings.Secure.getString(
50                 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
51         if (!TextUtils.isEmpty(flattenComponent)) {
52             final String pkg = ComponentName.unflattenFromString(flattenComponent)
53                     .getPackageName();
54             final List<InputMethodInfo> imis = mInputMethodManager.getInputMethodList();
55             for (InputMethodInfo imi : imis) {
56                 if (TextUtils.equals(imi.getPackageName(), pkg)) {
57                     return imi.loadLabel(mPackageManager);
58                 }
59             }
60         }
61         return "";
62     }
63 }
64