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.settingslib.inputmethod;
18 
19 import android.annotation.AnyThread;
20 import android.annotation.UiThread;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.util.Log;
24 import android.util.SparseArray;
25 import android.view.inputmethod.InputMethodInfo;
26 import android.view.inputmethod.InputMethodManager;
27 
28 import androidx.annotation.NonNull;
29 
30 import com.android.internal.annotations.GuardedBy;
31 import com.android.internal.inputmethod.DirectBootAwareness;
32 
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
37 
38 /**
39  * This class is a wrapper for {@link InputMethodManager} and
40  * {@link android.provider.Settings.Secure#ENABLED_INPUT_METHODS}. You need to refresh internal
41  * states manually on some events when "InputMethodInfo"s and "InputMethodSubtype"s can be changed.
42  *
43  * <p>TODO: Consolidate this with {@link InputMethodAndSubtypeUtil}.</p>
44  */
45 @UiThread
46 public class InputMethodSettingValuesWrapper {
47     private static final String TAG = InputMethodSettingValuesWrapper.class.getSimpleName();
48 
49     private static final Object sInstanceMapLock = new Object();
50     /**
51      * Manages mapping between user ID and corresponding singleton
52      * {@link InputMethodSettingValuesWrapper} object.
53      */
54     @GuardedBy("sInstanceMapLock")
55     private static SparseArray<InputMethodSettingValuesWrapper> sInstanceMap = new SparseArray<>();
56     private final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
57     private final ContentResolver mContentResolver;
58     private final InputMethodManager mImm;
59 
60     @AnyThread
61     @NonNull
getInstance(@onNull Context context)62     public static InputMethodSettingValuesWrapper getInstance(@NonNull Context context) {
63         final int requestUserId = context.getUserId();
64         InputMethodSettingValuesWrapper valuesWrapper;
65         // First time to create the wrapper.
66         synchronized (sInstanceMapLock) {
67             if (sInstanceMap.size() == 0) {
68                 valuesWrapper = new InputMethodSettingValuesWrapper(context);
69                 sInstanceMap.put(requestUserId, valuesWrapper);
70                 return valuesWrapper;
71             }
72             // We have same user context as request.
73             if (sInstanceMap.indexOfKey(requestUserId) >= 0) {
74                 return sInstanceMap.get(requestUserId);
75             }
76             // Request by a new user context.
77             valuesWrapper = new InputMethodSettingValuesWrapper(context);
78             sInstanceMap.put(context.getUserId(), valuesWrapper);
79         }
80 
81         return valuesWrapper;
82     }
83 
84     // Ensure singleton
InputMethodSettingValuesWrapper(Context context)85     private InputMethodSettingValuesWrapper(Context context) {
86         mContentResolver = context.getContentResolver();
87         mImm = context.getSystemService(InputMethodManager.class);
88         refreshAllInputMethodAndSubtypes();
89     }
90 
refreshAllInputMethodAndSubtypes()91     public void refreshAllInputMethodAndSubtypes() {
92         mMethodList.clear();
93         List<InputMethodInfo> imis = mImm.getInputMethodListAsUser(
94                 mContentResolver.getUserId(), DirectBootAwareness.ANY);
95         for (int i = 0; i < imis.size(); ++i) {
96             InputMethodInfo imi = imis.get(i);
97             if (!imi.isVirtualDeviceOnly()) {
98                 mMethodList.add(imi);
99             }
100         }
101     }
102 
getInputMethodList()103     public List<InputMethodInfo> getInputMethodList() {
104         return new ArrayList<>(mMethodList);
105     }
106 
isAlwaysCheckedIme(InputMethodInfo imi)107     public boolean isAlwaysCheckedIme(InputMethodInfo imi) {
108         final boolean isEnabled = isEnabledImi(imi);
109         if (getEnabledInputMethodList().size() <= 1 && isEnabled) {
110             return true;
111         }
112 
113         final int enabledValidNonAuxAsciiCapableImeCount =
114                 getEnabledValidNonAuxAsciiCapableImeCount();
115 
116         return enabledValidNonAuxAsciiCapableImeCount <= 1
117                 && !(enabledValidNonAuxAsciiCapableImeCount == 1 && !isEnabled)
118                 && imi.isSystem()
119                 && InputMethodAndSubtypeUtil.isValidNonAuxAsciiCapableIme(imi);
120     }
121 
getEnabledValidNonAuxAsciiCapableImeCount()122     private int getEnabledValidNonAuxAsciiCapableImeCount() {
123         int count = 0;
124         final List<InputMethodInfo> enabledImis = getEnabledInputMethodList();
125         for (final InputMethodInfo imi : enabledImis) {
126             if (InputMethodAndSubtypeUtil.isValidNonAuxAsciiCapableIme(imi)) {
127                 ++count;
128             }
129         }
130         if (count == 0) {
131             Log.w(TAG, "No \"enabledValidNonAuxAsciiCapableIme\"s found.");
132         }
133         return count;
134     }
135 
isEnabledImi(InputMethodInfo imi)136     public boolean isEnabledImi(InputMethodInfo imi) {
137         final List<InputMethodInfo> enabledImis = getEnabledInputMethodList();
138         for (final InputMethodInfo tempImi : enabledImis) {
139             if (tempImi.getId().equals(imi.getId())) {
140                 return true;
141             }
142         }
143         return false;
144     }
145 
146     /**
147      * Returns the list of the enabled {@link InputMethodInfo} determined by
148      * {@link android.provider.Settings.Secure#ENABLED_INPUT_METHODS} rather than just returning
149      * {@link InputMethodManager#getEnabledInputMethodList()}.
150      *
151      * @return the list of the enabled {@link InputMethodInfo}
152      */
getEnabledInputMethodList()153     private ArrayList<InputMethodInfo> getEnabledInputMethodList() {
154         final HashMap<String, HashSet<String>> enabledInputMethodsAndSubtypes =
155                 InputMethodAndSubtypeUtil.getEnabledInputMethodsAndSubtypeList(mContentResolver);
156         final ArrayList<InputMethodInfo> result = new ArrayList<>();
157         for (InputMethodInfo imi : mMethodList) {
158             if (enabledInputMethodsAndSubtypes.keySet().contains(imi.getId())) {
159                 result.add(imi);
160             }
161         }
162         return result;
163     }
164 }
165