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