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.tv.settings.inputmethod; 18 19 import android.annotation.DrawableRes; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.app.Activity; 23 import android.app.admin.DevicePolicyManager; 24 import android.content.Context; 25 import android.content.pm.ApplicationInfo; 26 import android.content.pm.PackageManager; 27 import android.content.pm.ServiceInfo; 28 import android.content.res.Configuration; 29 import android.graphics.Color; 30 import android.graphics.drawable.ColorDrawable; 31 import android.graphics.drawable.Drawable; 32 import android.os.Bundle; 33 import android.view.inputmethod.InputMethodInfo; 34 import android.view.inputmethod.InputMethodManager; 35 36 import androidx.annotation.Keep; 37 import androidx.preference.PreferenceScreen; 38 39 import com.android.internal.logging.nano.MetricsProto; 40 import com.android.settingslib.inputmethod.InputMethodAndSubtypeUtil; 41 import com.android.settingslib.inputmethod.InputMethodPreference; 42 import com.android.settingslib.inputmethod.InputMethodSettingValuesWrapper; 43 import com.android.tv.settings.R; 44 import com.android.tv.settings.SettingsPreferenceFragment; 45 46 import java.text.Collator; 47 import java.util.ArrayList; 48 import java.util.List; 49 50 /** 51 * Fragment for enabling/disabling virtual keyboard IMEs 52 */ 53 @Keep 54 public final class AvailableVirtualKeyboardFragment extends SettingsPreferenceFragment 55 implements InputMethodPreference.OnSavePreferenceListener { 56 57 private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>(); 58 private InputMethodSettingValuesWrapper mInputMethodSettingValues; 59 private InputMethodManager mImm; 60 private DevicePolicyManager mDpm; 61 62 @Override onCreatePreferences(Bundle bundle, String s)63 public void onCreatePreferences(Bundle bundle, String s) { 64 Activity activity = getActivity(); 65 PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(activity); 66 screen.setTitle(activity.getString(R.string.available_virtual_keyboard_category)); 67 setPreferenceScreen(screen); 68 mInputMethodSettingValues = InputMethodSettingValuesWrapper.getInstance(activity); 69 mImm = activity.getSystemService(InputMethodManager.class); 70 mDpm = activity.getSystemService(DevicePolicyManager.class); 71 } 72 73 @Override onResume()74 public void onResume() { 75 super.onResume(); 76 // Refresh internal states in mInputMethodSettingValues to keep the latest 77 // "InputMethodInfo"s and "InputMethodSubtype"s 78 mInputMethodSettingValues.refreshAllInputMethodAndSubtypes(); 79 updateInputMethodPreferenceViews(); 80 } 81 82 @Override onSaveInputMethodPreference(final InputMethodPreference pref)83 public void onSaveInputMethodPreference(final InputMethodPreference pref) { 84 final boolean hasHardwareKeyboard = getResources().getConfiguration().keyboard 85 == Configuration.KEYBOARD_QWERTY; 86 InputMethodAndSubtypeUtil.saveInputMethodSubtypeList(this, 87 getContext().getContentResolver(), mImm.getInputMethodList(), hasHardwareKeyboard); 88 // Update input method settings and preference list. 89 mInputMethodSettingValues.refreshAllInputMethodAndSubtypes(); 90 for (final InputMethodPreference p : mInputMethodPreferenceList) { 91 p.updatePreferenceViews(); 92 } 93 } 94 95 @Nullable loadDrawable(@onNull final PackageManager packageManager, @NonNull final String packageName, @DrawableRes final int resId, @NonNull final ApplicationInfo applicationInfo)96 private static Drawable loadDrawable(@NonNull final PackageManager packageManager, 97 @NonNull final String packageName, @DrawableRes final int resId, 98 @NonNull final ApplicationInfo applicationInfo) { 99 if (resId == 0) { 100 return null; 101 } 102 try { 103 return packageManager.getDrawable(packageName, resId, applicationInfo); 104 } catch (Exception e) { 105 return null; 106 } 107 } 108 109 @NonNull getInputMethodIcon(@onNull final PackageManager packageManager, @NonNull final InputMethodInfo imi)110 private static Drawable getInputMethodIcon(@NonNull final PackageManager packageManager, 111 @NonNull final InputMethodInfo imi) { 112 final ServiceInfo si = imi.getServiceInfo(); 113 final ApplicationInfo ai = si != null ? si.applicationInfo : null; 114 final String packageName = imi.getPackageName(); 115 if (si == null || ai == null || packageName == null) { 116 return new ColorDrawable(Color.TRANSPARENT); 117 } 118 // We do not use ServiceInfo#loadLogo() and ServiceInfo#loadIcon here since those methods 119 // internally have some fallback rules, which we want to do manually. 120 Drawable drawable = loadDrawable(packageManager, packageName, si.logo, ai); 121 if (drawable != null) { 122 return drawable; 123 } 124 drawable = loadDrawable(packageManager, packageName, si.icon, ai); 125 if (drawable != null) { 126 return drawable; 127 } 128 // We do not use ApplicationInfo#loadLogo() and ApplicationInfo#loadIcon here since those 129 // methods internally have some fallback rules, which we want to do manually. 130 drawable = loadDrawable(packageManager, packageName, ai.logo, ai); 131 if (drawable != null) { 132 return drawable; 133 } 134 drawable = loadDrawable(packageManager, packageName, ai.icon, ai); 135 if (drawable != null) { 136 return drawable; 137 } 138 return new ColorDrawable(Color.TRANSPARENT); 139 } 140 updateInputMethodPreferenceViews()141 private void updateInputMethodPreferenceViews() { 142 mInputMethodSettingValues.refreshAllInputMethodAndSubtypes(); 143 // Clear existing "InputMethodPreference"s 144 mInputMethodPreferenceList.clear(); 145 List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser(); 146 final Context context = getPreferenceManager().getContext(); 147 final PackageManager packageManager = getActivity().getPackageManager(); 148 final List<InputMethodInfo> imis = mInputMethodSettingValues.getInputMethodList(); 149 final int numImis = (imis == null ? 0 : imis.size()); 150 for (int i = 0; i < numImis; ++i) { 151 final InputMethodInfo imi = imis.get(i); 152 final boolean isAllowedByOrganization = permittedList == null 153 || permittedList.contains(imi.getPackageName()); 154 final InputMethodPreference pref = new InputMethodPreference( 155 context, imi, true, isAllowedByOrganization, this); 156 pref.setIcon(getInputMethodIcon(packageManager, imi)); 157 mInputMethodPreferenceList.add(pref); 158 } 159 final Collator collator = Collator.getInstance(); 160 mInputMethodPreferenceList.sort((lhs, rhs) -> lhs.compareTo(rhs, collator)); 161 getPreferenceScreen().removeAll(); 162 for (int i = 0; i < numImis; ++i) { 163 final InputMethodPreference pref = mInputMethodPreferenceList.get(i); 164 pref.setOrder(i); 165 getPreferenceScreen().addPreference(pref); 166 InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref); 167 pref.updatePreferenceViews(); 168 } 169 } 170 171 @Override getMetricsCategory()172 public int getMetricsCategory() { 173 return MetricsProto.MetricsEvent.ENABLE_VIRTUAL_KEYBOARDS; 174 } 175 } 176