1 /*
2  * Copyright (C) 2017 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.inputmethod;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.icu.text.ListFormatter;
23 import android.text.BidiFormatter;
24 import android.view.inputmethod.InputMethodInfo;
25 import android.view.inputmethod.InputMethodManager;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settingslib.core.AbstractPreferenceController;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 public class VirtualKeyboardPreferenceController extends AbstractPreferenceController
37         implements PreferenceControllerMixin {
38 
39     private final InputMethodManager mImm;
40     private final DevicePolicyManager mDpm;
41     private final PackageManager mPm;
42 
VirtualKeyboardPreferenceController(Context context)43     public VirtualKeyboardPreferenceController(Context context) {
44         super(context);
45         mPm = mContext.getPackageManager();
46         mDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
47         mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
48     }
49 
50     @Override
isAvailable()51     public boolean isAvailable() {
52         return mContext.getResources().getBoolean(R.bool.config_show_virtual_keyboard_pref);
53     }
54 
55     @Override
getPreferenceKey()56     public String getPreferenceKey() {
57         return "virtual_keyboard_pref";
58     }
59 
60     @Override
updateState(Preference preference)61     public void updateState(Preference preference) {
62         final List<InputMethodInfo> imis = mImm.getEnabledInputMethodList();
63         if (imis == null) {
64             preference.setSummary(R.string.summary_empty);
65             return;
66         }
67 
68         final List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
69         final List<String> labels = new ArrayList<>();
70 
71         for (InputMethodInfo imi : imis) {
72             final boolean isAllowedByOrganization = permittedList == null
73                     || permittedList.contains(imi.getPackageName());
74             if (!isAllowedByOrganization) {
75                 continue;
76             }
77             labels.add(imi.loadLabel(mPm).toString());
78         }
79         if (labels.isEmpty()) {
80             preference.setSummary(R.string.summary_empty);
81             return;
82         }
83 
84         final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
85 
86         final List<String> summaries = new ArrayList<>();
87         for (String label : labels) {
88             summaries.add(bidiFormatter.unicodeWrap(label));
89         }
90         preference.setSummary(ListFormatter.getInstance().format(summaries));
91     }
92 }
93