• 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.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.graphics.drawable.Drawable;
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 androidx.preference.Preference;
30 
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.settingslib.inputmethod.InputMethodAndSubtypeUtilCompat;
37 import com.android.settingslib.inputmethod.InputMethodPreference;
38 import com.android.settingslib.search.SearchIndexable;
39 
40 import java.text.Collator;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.List;
44 
45 @SearchIndexable
46 public final class VirtualKeyboardFragment extends SettingsPreferenceFragment implements Indexable {
47 
48     private static final String ADD_VIRTUAL_KEYBOARD_SCREEN = "add_virtual_keyboard_screen";
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 SettingsEnums.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             final Drawable icon = imi.loadIcon(context.getPackageManager());
90             final InputMethodPreference pref = new InputMethodPreference(
91                     context,
92                     imi,
93                     false,  /* isImeEnabler */
94                     isAllowedByOrganization,
95                     null  /* this can be null since isImeEnabler is false */);
96             pref.setIcon(icon);
97             mInputMethodPreferenceList.add(pref);
98         }
99         final Collator collator = Collator.getInstance();
100         mInputMethodPreferenceList.sort((lhs, rhs) -> lhs.compareTo(rhs, collator));
101         getPreferenceScreen().removeAll();
102         for (int i = 0; i < N; ++i) {
103             final InputMethodPreference pref = mInputMethodPreferenceList.get(i);
104             pref.setOrder(i);
105             getPreferenceScreen().addPreference(pref);
106             InputMethodAndSubtypeUtilCompat.removeUnnecessaryNonPersistentPreference(pref);
107             pref.updatePreferenceViews();
108         }
109         mAddVirtualKeyboardScreen.setIcon(R.drawable.ic_add_24dp);
110         mAddVirtualKeyboardScreen.setOrder(N);
111         getPreferenceScreen().addPreference(mAddVirtualKeyboardScreen);
112     }
113 
114     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
115             new BaseSearchIndexProvider() {
116                 @Override
117                 public List<SearchIndexableResource> getXmlResourcesToIndex(
118                         Context context, boolean enabled) {
119                     final SearchIndexableResource sir = new SearchIndexableResource(context);
120                     sir.xmlResId = R.xml.virtual_keyboard_settings;
121                     return Arrays.asList(sir);
122                 }
123             };
124 }
125