• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 import android.view.inputmethod.InputMethodInfo;
23 import android.view.inputmethod.InputMethodManager;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settingslib.core.lifecycle.LifecycleObserver;
30 import com.android.settingslib.core.lifecycle.events.OnStart;
31 import com.android.settingslib.inputmethod.InputMethodPreference;
32 
33 import com.google.common.annotations.VisibleForTesting;
34 
35 import java.text.Collator;
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 public class InputMethodPreferenceController extends BasePreferenceController implements
40         LifecycleObserver, OnStart {
41 
42     @VisibleForTesting
43     PreferenceScreen mScreen;
44     private Preference mPreference;
45     private InputMethodManager mImm;
46     private DevicePolicyManager mDpm;
47 
InputMethodPreferenceController(Context context, String key)48     public InputMethodPreferenceController(Context context, String key) {
49         super(context, key);
50         mImm = context.getSystemService(InputMethodManager.class);
51         mDpm = context.getSystemService(DevicePolicyManager.class);
52     }
53 
54     @Override
getAvailabilityStatus()55     public int getAvailabilityStatus() {
56         return AVAILABLE;
57     }
58 
59     @Override
displayPreference(PreferenceScreen screen)60     public void displayPreference(PreferenceScreen screen) {
61         super.displayPreference(screen);
62         mScreen = screen;
63         mPreference = mScreen.findPreference(getPreferenceKey());
64     }
65 
66     @Override
onStart()67     public void onStart() {
68         updateInputMethodPreferenceViews();
69     }
70 
updateInputMethodPreferenceViews()71     private void updateInputMethodPreferenceViews() {
72         final List<InputMethodPreference> preferenceList = new ArrayList<>();
73 
74         final List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
75         final List<InputMethodInfo> imis = mImm.getEnabledInputMethodList();
76         final int N = (imis == null ? 0 : imis.size());
77         for (int i = 0; i < N; ++i) {
78             final InputMethodInfo imi = imis.get(i);
79             final boolean isAllowedByOrganization = permittedList == null
80                     || permittedList.contains(imi.getPackageName());
81             final Drawable icon = imi.loadIcon(mContext.getPackageManager());
82             final InputMethodPreference pref = new InputMethodPreference(
83                     mScreen.getContext(),
84                     imi,
85                     false,  /* isImeEnabler */
86                     isAllowedByOrganization,
87                     null  /* this can be null since isImeEnabler is false */);
88             pref.setIcon(icon);
89             preferenceList.add(pref);
90         }
91         final Collator collator = Collator.getInstance();
92         preferenceList.sort((lhs, rhs) -> lhs.compareTo(rhs, collator));
93         mScreen.removeAll();
94         for (int i = 0; i < N; ++i) {
95             final InputMethodPreference pref = preferenceList.get(i);
96             pref.setOrder(i);
97             mScreen.addPreference(pref);
98             pref.updatePreferenceViews();
99         }
100         mScreen.addPreference(mPreference);
101     }
102 }
103