blob: 24c2ebcc7ce1d504671e2a856fb784b376e86475 [file] [log] [blame]
Abodunrinwa Toki976bb3f2016-01-20 18:43:20 +00001/*
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
17package com.android.settings.inputmethod;
18
19import android.app.Activity;
20import android.app.admin.DevicePolicyManager;
21import android.content.Context;
Abodunrinwa Toki567ebd62016-01-23 16:37:37 +000022import android.graphics.Color;
23import android.graphics.drawable.ColorDrawable;
24import android.graphics.drawable.Drawable;
Abodunrinwa Toki976bb3f2016-01-20 18:43:20 +000025import android.os.Bundle;
26import android.support.v7.preference.Preference;
27import android.view.inputmethod.InputMethodInfo;
28import android.view.inputmethod.InputMethodManager;
Tamas Berghammer265d3c22016-06-22 15:34:45 +010029import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Abodunrinwa Toki976bb3f2016-01-20 18:43:20 +000030import com.android.internal.util.Preconditions;
31import com.android.settings.R;
Abodunrinwa Toki976bb3f2016-01-20 18:43:20 +000032import com.android.settings.SettingsPreferenceFragment;
33
34import java.text.Collator;
35import java.util.ArrayList;
36import java.util.Collections;
37import java.util.Comparator;
38import java.util.List;
39
40public final class VirtualKeyboardFragment extends SettingsPreferenceFragment {
41
42 private static final String ADD_VIRTUAL_KEYBOARD_SCREEN = "add_virtual_keyboard_screen";
Abodunrinwa Toki567ebd62016-01-23 16:37:37 +000043 private static final Drawable NO_ICON = new ColorDrawable(Color.TRANSPARENT);
Abodunrinwa Toki976bb3f2016-01-20 18:43:20 +000044
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 Zhang65076132016-08-08 10:25:13 -070069 public int getMetricsCategory() {
Jason Monk3e19fc52016-03-08 14:18:30 -050070 return MetricsEvent.VIRTUAL_KEYBOARDS;
Abodunrinwa Toki976bb3f2016-01-20 18:43:20 +000071 }
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 Toki567ebd62016-01-23 16:37:37 +000084 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 Toki976bb3f2016-01-20 18:43:20 +000092 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 Toki567ebd62016-01-23 16:37:37 +000098 pref.setIcon(icon);
Abodunrinwa Toki976bb3f2016-01-20 18:43:20 +000099 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 Tokia1bda3a2016-04-15 12:36:20 +0100116 mAddVirtualKeyboardScreen.setIcon(R.drawable.ic_add_24dp);
Abodunrinwa Toki976bb3f2016-01-20 18:43:20 +0000117 mAddVirtualKeyboardScreen.setOrder(N);
118 getPreferenceScreen().addPreference(mAddVirtualKeyboardScreen);
119 }
120}