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