• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
22 import android.graphics.Color;
23 import android.graphics.drawable.ColorDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.os.Bundle;
26 import android.provider.SearchIndexableResource;
27 import android.support.v7.preference.Preference;
28 import android.view.inputmethod.InputMethodInfo;
29 import android.view.inputmethod.InputMethodManager;
30 
31 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
32 import com.android.internal.util.Preconditions;
33 import com.android.settings.R;
34 import com.android.settings.SettingsPreferenceFragment;
35 import com.android.settings.search.BaseSearchIndexProvider;
36 import com.android.settings.search.Indexable;
37 import com.android.settingslib.inputmethod.InputMethodAndSubtypeUtil;
38 import com.android.settingslib.inputmethod.InputMethodPreference;
39 
40 import java.text.Collator;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.List;
44 
45 public final class VirtualKeyboardFragment extends SettingsPreferenceFragment implements Indexable {
46 
47     private static final String ADD_VIRTUAL_KEYBOARD_SCREEN = "add_virtual_keyboard_screen";
48     private static final Drawable NO_ICON = new ColorDrawable(Color.TRANSPARENT);
49 
50     private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>();
51     private InputMethodManager mImm;
52     private DevicePolicyManager mDpm;
53     private Preference mAddVirtualKeyboardScreen;
54 
55     @Override
onCreatePreferences(Bundle bundle, String s)56     public void onCreatePreferences(Bundle bundle, String s) {
57         Activity activity = Preconditions.checkNotNull(getActivity());
58         addPreferencesFromResource(R.xml.virtual_keyboard_settings);
59         mImm = Preconditions.checkNotNull(activity.getSystemService(InputMethodManager.class));
60         mDpm = Preconditions.checkNotNull(activity.getSystemService(DevicePolicyManager.class));
61         mAddVirtualKeyboardScreen = Preconditions.checkNotNull(
62                 findPreference(ADD_VIRTUAL_KEYBOARD_SCREEN));
63     }
64 
65     @Override
onResume()66     public void onResume() {
67         super.onResume();
68         // Refresh internal states in mInputMethodSettingValues to keep the latest
69         // "InputMethodInfo"s and "InputMethodSubtype"s
70         updateInputMethodPreferenceViews();
71     }
72 
73     @Override
getMetricsCategory()74     public int getMetricsCategory() {
75         return MetricsEvent.VIRTUAL_KEYBOARDS;
76     }
77 
updateInputMethodPreferenceViews()78     private void updateInputMethodPreferenceViews() {
79         // Clear existing "InputMethodPreference"s
80         mInputMethodPreferenceList.clear();
81         List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
82         final Context context = getPrefContext();
83         final List<InputMethodInfo> imis = mImm.getEnabledInputMethodList();
84         final int N = (imis == null ? 0 : imis.size());
85         for (int i = 0; i < N; ++i) {
86             final InputMethodInfo imi = imis.get(i);
87             final boolean isAllowedByOrganization = permittedList == null
88                     || permittedList.contains(imi.getPackageName());
89             Drawable icon;
90             try {
91                 // TODO: Consider other ways to retrieve an icon to show here.
92                 icon = getActivity().getPackageManager().getApplicationIcon(imi.getPackageName());
93             } catch (Exception e) {
94                 // TODO: Consider handling the error differently perhaps by showing default icons.
95                 icon = NO_ICON;
96             }
97             final InputMethodPreference pref = new InputMethodPreference(
98                     context,
99                     imi,
100                     false,  /* isImeEnabler */
101                     isAllowedByOrganization,
102                     null  /* this can be null since isImeEnabler is false */);
103             pref.setIcon(icon);
104             mInputMethodPreferenceList.add(pref);
105         }
106         final Collator collator = Collator.getInstance();
107         mInputMethodPreferenceList.sort((lhs, rhs) -> lhs.compareTo(rhs, collator));
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         }
116         mAddVirtualKeyboardScreen.setIcon(R.drawable.ic_add_24dp);
117         mAddVirtualKeyboardScreen.setOrder(N);
118         getPreferenceScreen().addPreference(mAddVirtualKeyboardScreen);
119     }
120 
121     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
122             new BaseSearchIndexProvider() {
123                 @Override
124                 public List<SearchIndexableResource> getXmlResourcesToIndex(
125                         Context context, boolean enabled) {
126                     final SearchIndexableResource sir = new SearchIndexableResource(context);
127                     sir.xmlResId = R.xml.virtual_keyboard_settings;
128                     return Arrays.asList(sir);
129                 }
130 
131                 @Override
132                 public List<String> getNonIndexableKeys(Context context) {
133                     final List<String> keys = super.getNonIndexableKeys(context);
134                     keys.add("add_virtual_keyboard_screen");
135                     return keys;
136                 }
137             };
138 }
139