blob: 61e6a424641115870ded57ef88b69f43df0c08cf [file] [log] [blame]
Fan Zhang2a9255b2017-03-23 16:42:13 -07001/*
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
17package com.android.settings.inputmethod;
18
19import android.app.admin.DevicePolicyManager;
20import android.content.Context;
21import android.content.pm.PackageManager;
jyhshiangwang4d015b12018-04-24 16:05:57 +080022import android.icu.text.ListFormatter;
Fan Zhangb1bdf902017-04-20 11:01:32 -070023import android.text.BidiFormatter;
Fan Zhang2a9255b2017-03-23 16:42:13 -070024import android.view.inputmethod.InputMethodInfo;
25import android.view.inputmethod.InputMethodManager;
26
Fan Zhang23f8d592018-08-28 15:11:40 -070027import androidx.preference.Preference;
28
Fan Zhang2a9255b2017-03-23 16:42:13 -070029import com.android.settings.R;
Tony Mantler1d583e12017-06-13 13:09:25 -070030import com.android.settings.core.PreferenceControllerMixin;
31import com.android.settingslib.core.AbstractPreferenceController;
Fan Zhang2a9255b2017-03-23 16:42:13 -070032
33import java.util.ArrayList;
34import java.util.List;
35
Tony Mantler1d583e12017-06-13 13:09:25 -070036public class VirtualKeyboardPreferenceController extends AbstractPreferenceController
37 implements PreferenceControllerMixin {
Fan Zhang2a9255b2017-03-23 16:42:13 -070038
39 private final InputMethodManager mImm;
40 private final DevicePolicyManager mDpm;
41 private final PackageManager mPm;
42
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
51 public boolean isAvailable() {
Ben Linc28b46f2017-12-21 11:09:39 -080052 return mContext.getResources().getBoolean(R.bool.config_show_virtual_keyboard_pref);
Fan Zhang2a9255b2017-03-23 16:42:13 -070053 }
54
55 @Override
56 public String getPreferenceKey() {
57 return "virtual_keyboard_pref";
58 }
59
60 @Override
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
Fan Zhangb1bdf902017-04-20 11:01:32 -070084 final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
85
jyhshiangwang4d015b12018-04-24 16:05:57 +080086 final List<String> summaries = new ArrayList<>();
Fan Zhang2a9255b2017-03-23 16:42:13 -070087 for (String label : labels) {
jyhshiangwang4d015b12018-04-24 16:05:57 +080088 summaries.add(bidiFormatter.unicodeWrap(label));
Fan Zhang2a9255b2017-03-23 16:42:13 -070089 }
jyhshiangwang4d015b12018-04-24 16:05:57 +080090 preference.setSummary(ListFormatter.getInstance().format(summaries));
Fan Zhang2a9255b2017-03-23 16:42:13 -070091 }
92}