Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 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; |
jyhshiangwang | 4d015b1 | 2018-04-24 16:05:57 +0800 | [diff] [blame] | 22 | import android.icu.text.ListFormatter; |
Fan Zhang | b1bdf90 | 2017-04-20 11:01:32 -0700 | [diff] [blame] | 23 | import android.text.BidiFormatter; |
Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 24 | import android.view.inputmethod.InputMethodInfo; |
| 25 | import android.view.inputmethod.InputMethodManager; |
| 26 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 27 | import androidx.preference.Preference; |
| 28 | |
Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 29 | import com.android.settings.R; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 30 | import com.android.settings.core.PreferenceControllerMixin; |
| 31 | import com.android.settingslib.core.AbstractPreferenceController; |
Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 36 | public class VirtualKeyboardPreferenceController extends AbstractPreferenceController |
| 37 | implements PreferenceControllerMixin { |
Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 38 | |
| 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 Lin | c28b46f | 2017-12-21 11:09:39 -0800 | [diff] [blame] | 52 | return mContext.getResources().getBoolean(R.bool.config_show_virtual_keyboard_pref); |
Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 53 | } |
| 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 Zhang | b1bdf90 | 2017-04-20 11:01:32 -0700 | [diff] [blame] | 84 | final BidiFormatter bidiFormatter = BidiFormatter.getInstance(); |
| 85 | |
jyhshiangwang | 4d015b1 | 2018-04-24 16:05:57 +0800 | [diff] [blame] | 86 | final List<String> summaries = new ArrayList<>(); |
Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 87 | for (String label : labels) { |
jyhshiangwang | 4d015b1 | 2018-04-24 16:05:57 +0800 | [diff] [blame] | 88 | summaries.add(bidiFormatter.unicodeWrap(label)); |
Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 89 | } |
jyhshiangwang | 4d015b1 | 2018-04-24 16:05:57 +0800 | [diff] [blame] | 90 | preference.setSummary(ListFormatter.getInstance().format(summaries)); |
Fan Zhang | 2a9255b | 2017-03-23 16:42:13 -0700 | [diff] [blame] | 91 | } |
| 92 | } |