| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 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.content.Context; |
| Abodunrinwa Toki | 567ebd6 | 2016-01-23 16:37:37 +0000 | [diff] [blame] | 22 | import android.graphics.Color; |
| 23 | import android.graphics.drawable.ColorDrawable; |
| 24 | import android.graphics.drawable.Drawable; |
| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.support.v7.preference.Preference; |
| 27 | import android.view.inputmethod.InputMethodInfo; |
| 28 | import android.view.inputmethod.InputMethodManager; |
| Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame^] | 29 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 30 | import com.android.internal.util.Preconditions; |
| 31 | import com.android.settings.R; |
| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 32 | import com.android.settings.SettingsPreferenceFragment; |
| 33 | |
| 34 | import java.text.Collator; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.Collections; |
| 37 | import java.util.Comparator; |
| 38 | import java.util.List; |
| 39 | |
| 40 | public final class VirtualKeyboardFragment extends SettingsPreferenceFragment { |
| 41 | |
| 42 | private static final String ADD_VIRTUAL_KEYBOARD_SCREEN = "add_virtual_keyboard_screen"; |
| Abodunrinwa Toki | 567ebd6 | 2016-01-23 16:37:37 +0000 | [diff] [blame] | 43 | private static final Drawable NO_ICON = new ColorDrawable(Color.TRANSPARENT); |
| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 44 | |
| 45 | private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>(); |
| 46 | private InputMethodManager mImm; |
| 47 | private DevicePolicyManager mDpm; |
| 48 | private Preference mAddVirtualKeyboardScreen; |
| 49 | |
| 50 | @Override |
| 51 | public void onCreatePreferences(Bundle bundle, String s) { |
| 52 | Activity activity = Preconditions.checkNotNull(getActivity()); |
| 53 | addPreferencesFromResource(R.xml.virtual_keyboard_settings); |
| 54 | mImm = Preconditions.checkNotNull(activity.getSystemService(InputMethodManager.class)); |
| 55 | mDpm = Preconditions.checkNotNull(activity.getSystemService(DevicePolicyManager.class)); |
| 56 | mAddVirtualKeyboardScreen = Preconditions.checkNotNull( |
| 57 | findPreference(ADD_VIRTUAL_KEYBOARD_SCREEN)); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onResume() { |
| 62 | super.onResume(); |
| 63 | // Refresh internal states in mInputMethodSettingValues to keep the latest |
| 64 | // "InputMethodInfo"s and "InputMethodSubtype"s |
| 65 | updateInputMethodPreferenceViews(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 69 | public int getMetricsCategory() { |
| Jason Monk | 3e19fc5 | 2016-03-08 14:18:30 -0500 | [diff] [blame] | 70 | return MetricsEvent.VIRTUAL_KEYBOARDS; |
| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private void updateInputMethodPreferenceViews() { |
| 74 | // Clear existing "InputMethodPreference"s |
| 75 | mInputMethodPreferenceList.clear(); |
| 76 | List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser(); |
| 77 | final Context context = getPrefContext(); |
| 78 | final List<InputMethodInfo> imis = mImm.getEnabledInputMethodList(); |
| 79 | final int N = (imis == null ? 0 : imis.size()); |
| 80 | for (int i = 0; i < N; ++i) { |
| 81 | final InputMethodInfo imi = imis.get(i); |
| 82 | final boolean isAllowedByOrganization = permittedList == null |
| 83 | || permittedList.contains(imi.getPackageName()); |
| Abodunrinwa Toki | 567ebd6 | 2016-01-23 16:37:37 +0000 | [diff] [blame] | 84 | Drawable icon; |
| 85 | try { |
| 86 | // TODO: Consider other ways to retrieve an icon to show here. |
| 87 | icon = getActivity().getPackageManager().getApplicationIcon(imi.getPackageName()); |
| 88 | } catch (Exception e) { |
| 89 | // TODO: Consider handling the error differently perhaps by showing default icons. |
| 90 | icon = NO_ICON; |
| 91 | } |
| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 92 | final InputMethodPreference pref = new InputMethodPreference( |
| 93 | context, |
| 94 | imi, |
| 95 | false, /* isImeEnabler */ |
| 96 | isAllowedByOrganization, |
| 97 | null /* this can be null since isImeEnabler is false */); |
| Abodunrinwa Toki | 567ebd6 | 2016-01-23 16:37:37 +0000 | [diff] [blame] | 98 | pref.setIcon(icon); |
| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 99 | mInputMethodPreferenceList.add(pref); |
| 100 | } |
| 101 | final Collator collator = Collator.getInstance(); |
| 102 | Collections.sort(mInputMethodPreferenceList, new Comparator<InputMethodPreference>() { |
| 103 | @Override |
| 104 | public int compare(InputMethodPreference lhs, InputMethodPreference rhs) { |
| 105 | return lhs.compareTo(rhs, collator); |
| 106 | } |
| 107 | }); |
| 108 | getPreferenceScreen().removeAll(); |
| 109 | for (int i = 0; i < N; ++i) { |
| 110 | final InputMethodPreference pref = mInputMethodPreferenceList.get(i); |
| 111 | pref.setOrder(i); |
| 112 | getPreferenceScreen().addPreference(pref); |
| 113 | InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref); |
| 114 | pref.updatePreferenceViews(); |
| 115 | } |
| Abodunrinwa Toki | a1bda3a | 2016-04-15 12:36:20 +0100 | [diff] [blame] | 116 | mAddVirtualKeyboardScreen.setIcon(R.drawable.ic_add_24dp); |
| Abodunrinwa Toki | 976bb3f | 2016-01-20 18:43:20 +0000 | [diff] [blame] | 117 | mAddVirtualKeyboardScreen.setOrder(N); |
| 118 | getPreferenceScreen().addPreference(mAddVirtualKeyboardScreen); |
| 119 | } |
| 120 | } |