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