1 /* 2 * Copyright (C) 2016 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.Activity; 20 import android.app.admin.DevicePolicyManager; 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.content.res.Configuration; 24 import android.os.Bundle; 25 import android.provider.SearchIndexableResource; 26 import android.view.inputmethod.InputMethodInfo; 27 import android.view.inputmethod.InputMethodManager; 28 29 import com.android.settings.R; 30 import com.android.settings.SettingsPreferenceFragment; 31 import com.android.settings.search.BaseSearchIndexProvider; 32 import com.android.settingslib.inputmethod.InputMethodAndSubtypeUtilCompat; 33 import com.android.settingslib.inputmethod.InputMethodPreference; 34 import com.android.settingslib.inputmethod.InputMethodSettingValuesWrapper; 35 import com.android.settingslib.search.SearchIndexable; 36 37 import java.text.Collator; 38 import java.util.ArrayList; 39 import java.util.List; 40 41 @SearchIndexable 42 public final class AvailableVirtualKeyboardFragment extends SettingsPreferenceFragment 43 implements InputMethodPreference.OnSavePreferenceListener { 44 45 private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>(); 46 private InputMethodSettingValuesWrapper mInputMethodSettingValues; 47 private InputMethodManager mImm; 48 private DevicePolicyManager mDpm; 49 50 @Override onCreatePreferences(Bundle bundle, String s)51 public void onCreatePreferences(Bundle bundle, String s) { 52 addPreferencesFromResource(R.xml.available_virtual_keyboard); 53 Activity activity = getActivity(); 54 55 mInputMethodSettingValues = InputMethodSettingValuesWrapper.getInstance(activity); 56 mImm = activity.getSystemService(InputMethodManager.class); 57 mDpm = activity.getSystemService(DevicePolicyManager.class); 58 } 59 60 @Override onResume()61 public void onResume() { 62 super.onResume(); 63 // Refresh internal states in mInputMethodSettingValues to keep the latest 64 // "InputMethodInfo"s and "InputMethodSubtype"s 65 mInputMethodSettingValues.refreshAllInputMethodAndSubtypes(); 66 updateInputMethodPreferenceViews(); 67 } 68 69 @Override onSaveInputMethodPreference(final InputMethodPreference pref)70 public void onSaveInputMethodPreference(final InputMethodPreference pref) { 71 final boolean hasHardwareKeyboard = getResources().getConfiguration().keyboard 72 == Configuration.KEYBOARD_QWERTY; 73 InputMethodAndSubtypeUtilCompat.saveInputMethodSubtypeList(this, getContentResolver(), 74 mImm.getInputMethodList(), hasHardwareKeyboard); 75 // Update input method settings and preference list. 76 mInputMethodSettingValues.refreshAllInputMethodAndSubtypes(); 77 for (final InputMethodPreference p : mInputMethodPreferenceList) { 78 p.updatePreferenceViews(); 79 } 80 } 81 82 @Override getMetricsCategory()83 public int getMetricsCategory() { 84 return SettingsEnums.ENABLE_VIRTUAL_KEYBOARDS; 85 } 86 updateInputMethodPreferenceViews()87 private void updateInputMethodPreferenceViews() { 88 mInputMethodSettingValues.refreshAllInputMethodAndSubtypes(); 89 // Clear existing "InputMethodPreference"s 90 mInputMethodPreferenceList.clear(); 91 List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser(); 92 final Context context = getPrefContext(); 93 final List<InputMethodInfo> imis = mInputMethodSettingValues.getInputMethodList(); 94 final int numImis = (imis == null ? 0 : imis.size()); 95 for (int i = 0; i < numImis; ++i) { 96 final InputMethodInfo imi = imis.get(i); 97 final boolean isAllowedByOrganization = permittedList == null 98 || permittedList.contains(imi.getPackageName()); 99 final InputMethodPreference pref = new InputMethodPreference( 100 context, imi, true, isAllowedByOrganization, this); 101 pref.setIcon(imi.loadIcon(context.getPackageManager())); 102 mInputMethodPreferenceList.add(pref); 103 } 104 final Collator collator = Collator.getInstance(); 105 mInputMethodPreferenceList.sort((lhs, rhs) -> lhs.compareTo(rhs, collator)); 106 getPreferenceScreen().removeAll(); 107 for (int i = 0; i < numImis; ++i) { 108 final InputMethodPreference pref = mInputMethodPreferenceList.get(i); 109 pref.setOrder(i); 110 getPreferenceScreen().addPreference(pref); 111 InputMethodAndSubtypeUtilCompat.removeUnnecessaryNonPersistentPreference(pref); 112 pref.updatePreferenceViews(); 113 } 114 } 115 116 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 117 new BaseSearchIndexProvider() { 118 @Override 119 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, 120 boolean enabled) { 121 List<SearchIndexableResource> res = new ArrayList<>(); 122 SearchIndexableResource index = new SearchIndexableResource(context); 123 index.xmlResId = R.xml.available_virtual_keyboard; 124 res.add(index); 125 return res; 126 } 127 }; 128 } 129