• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.tv.settings.inputmethod;
18 
19 import android.annotation.DrawableRes;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.app.Activity;
23 import android.app.admin.DevicePolicyManager;
24 import android.content.Context;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.content.pm.ServiceInfo;
28 import android.content.res.Configuration;
29 import android.graphics.Color;
30 import android.graphics.drawable.ColorDrawable;
31 import android.graphics.drawable.Drawable;
32 import android.os.Bundle;
33 import android.support.annotation.Keep;
34 import android.support.v7.preference.PreferenceScreen;
35 import android.view.inputmethod.InputMethodInfo;
36 import android.view.inputmethod.InputMethodManager;
37 
38 import com.android.internal.logging.nano.MetricsProto;
39 import com.android.settingslib.inputmethod.InputMethodAndSubtypeUtil;
40 import com.android.settingslib.inputmethod.InputMethodPreference;
41 import com.android.settingslib.inputmethod.InputMethodSettingValuesWrapper;
42 import com.android.tv.settings.R;
43 import com.android.tv.settings.SettingsPreferenceFragment;
44 
45 import java.text.Collator;
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 /**
50  * Fragment for enabling/disabling virtual keyboard IMEs
51  */
52 @Keep
53 public final class AvailableVirtualKeyboardFragment extends SettingsPreferenceFragment
54         implements InputMethodPreference.OnSavePreferenceListener {
55 
56     private final ArrayList<InputMethodPreference> mInputMethodPreferenceList = new ArrayList<>();
57     private InputMethodSettingValuesWrapper mInputMethodSettingValues;
58     private InputMethodManager mImm;
59     private DevicePolicyManager mDpm;
60 
61     @Override
onCreatePreferences(Bundle bundle, String s)62     public void onCreatePreferences(Bundle bundle, String s) {
63         Activity activity = getActivity();
64         PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(activity);
65         screen.setTitle(activity.getString(R.string.available_virtual_keyboard_category));
66         setPreferenceScreen(screen);
67         mInputMethodSettingValues = InputMethodSettingValuesWrapper.getInstance(activity);
68         mImm = activity.getSystemService(InputMethodManager.class);
69         mDpm = activity.getSystemService(DevicePolicyManager.class);
70     }
71 
72     @Override
onResume()73     public void onResume() {
74         super.onResume();
75         // Refresh internal states in mInputMethodSettingValues to keep the latest
76         // "InputMethodInfo"s and "InputMethodSubtype"s
77         mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
78         updateInputMethodPreferenceViews();
79     }
80 
81     @Override
onSaveInputMethodPreference(final InputMethodPreference pref)82     public void onSaveInputMethodPreference(final InputMethodPreference pref) {
83         final boolean hasHardwareKeyboard = getResources().getConfiguration().keyboard
84                 == Configuration.KEYBOARD_QWERTY;
85         InputMethodAndSubtypeUtil.saveInputMethodSubtypeList(this,
86                 getContext().getContentResolver(), mImm.getInputMethodList(), hasHardwareKeyboard);
87         // Update input method settings and preference list.
88         mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
89         for (final InputMethodPreference p : mInputMethodPreferenceList) {
90             p.updatePreferenceViews();
91         }
92     }
93 
94     @Nullable
loadDrawable(@onNull final PackageManager packageManager, @NonNull final String packageName, @DrawableRes final int resId, @NonNull final ApplicationInfo applicationInfo)95     private static Drawable loadDrawable(@NonNull final PackageManager packageManager,
96             @NonNull final String packageName, @DrawableRes final int resId,
97             @NonNull final ApplicationInfo applicationInfo) {
98         if (resId == 0) {
99             return null;
100         }
101         try {
102             return packageManager.getDrawable(packageName, resId, applicationInfo);
103         } catch (Exception e) {
104             return null;
105         }
106     }
107 
108     @NonNull
getInputMethodIcon(@onNull final PackageManager packageManager, @NonNull final InputMethodInfo imi)109     private static Drawable getInputMethodIcon(@NonNull final PackageManager packageManager,
110             @NonNull final InputMethodInfo imi) {
111         final ServiceInfo si = imi.getServiceInfo();
112         final ApplicationInfo ai = si != null ? si.applicationInfo : null;
113         final String packageName = imi.getPackageName();
114         if (si == null || ai == null || packageName == null) {
115             return new ColorDrawable(Color.TRANSPARENT);
116         }
117         // We do not use ServiceInfo#loadLogo() and ServiceInfo#loadIcon here since those methods
118         // internally have some fallback rules, which we want to do manually.
119         Drawable drawable = loadDrawable(packageManager, packageName, si.logo, ai);
120         if (drawable != null) {
121             return drawable;
122         }
123         drawable = loadDrawable(packageManager, packageName, si.icon, ai);
124         if (drawable != null) {
125             return drawable;
126         }
127         // We do not use ApplicationInfo#loadLogo() and ApplicationInfo#loadIcon here since those
128         // methods internally have some fallback rules, which we want to do manually.
129         drawable = loadDrawable(packageManager, packageName, ai.logo, ai);
130         if (drawable != null) {
131             return drawable;
132         }
133         drawable = loadDrawable(packageManager, packageName, ai.icon, ai);
134         if (drawable != null) {
135             return drawable;
136         }
137         return new ColorDrawable(Color.TRANSPARENT);
138     }
139 
updateInputMethodPreferenceViews()140     private void updateInputMethodPreferenceViews() {
141         mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
142         // Clear existing "InputMethodPreference"s
143         mInputMethodPreferenceList.clear();
144         List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
145         final Context context = getPreferenceManager().getContext();
146         final PackageManager packageManager = getActivity().getPackageManager();
147         final List<InputMethodInfo> imis = mInputMethodSettingValues.getInputMethodList();
148         final int numImis = (imis == null ? 0 : imis.size());
149         for (int i = 0; i < numImis; ++i) {
150             final InputMethodInfo imi = imis.get(i);
151             final boolean isAllowedByOrganization = permittedList == null
152                     || permittedList.contains(imi.getPackageName());
153             final InputMethodPreference pref = new InputMethodPreference(
154                     context, imi, true, isAllowedByOrganization, this);
155             pref.setIcon(getInputMethodIcon(packageManager, imi));
156             mInputMethodPreferenceList.add(pref);
157         }
158         final Collator collator = Collator.getInstance();
159         mInputMethodPreferenceList.sort((lhs, rhs) -> lhs.compareTo(rhs, collator));
160         getPreferenceScreen().removeAll();
161         for (int i = 0; i < numImis; ++i) {
162             final InputMethodPreference pref = mInputMethodPreferenceList.get(i);
163             pref.setOrder(i);
164             getPreferenceScreen().addPreference(pref);
165             InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref);
166             pref.updatePreferenceViews();
167         }
168     }
169 
170     @Override
getMetricsCategory()171     public int getMetricsCategory() {
172         return MetricsProto.MetricsEvent.ENABLE_VIRTUAL_KEYBOARDS;
173     }
174 }
175