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.UiThread;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.util.Log;
23 import android.view.inputmethod.InputMethodInfo;
24 import android.view.inputmethod.InputMethodManager;
25 
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 
31 /**
32  * This class is a wrapper for {@link InputMethodManager} and
33  * {@link android.provider.Settings.Secure#ENABLED_INPUT_METHODS}. You need to refresh internal
34  * states manually on some events when "InputMethodInfo"s and "InputMethodSubtype"s can be changed.
35  *
36  * <p>TODO: Consolidate this with {@link InputMethodAndSubtypeUtil}.</p>
37  */
38 @UiThread
39 public class InputMethodSettingValuesWrapper {
40     private static final String TAG = InputMethodSettingValuesWrapper.class.getSimpleName();
41 
42     private static volatile InputMethodSettingValuesWrapper sInstance;
43     private final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
44     private final ContentResolver mContentResolver;
45     private final InputMethodManager mImm;
46 
getInstance(Context context)47     public static InputMethodSettingValuesWrapper getInstance(Context context) {
48         if (sInstance == null) {
49             synchronized (TAG) {
50                 if (sInstance == null) {
51                     sInstance = new InputMethodSettingValuesWrapper(context);
52                 }
53             }
54         }
55         return sInstance;
56     }
57 
58     // Ensure singleton
InputMethodSettingValuesWrapper(Context context)59     private InputMethodSettingValuesWrapper(Context context) {
60         mContentResolver = context.getContentResolver();
61         mImm = context.getSystemService(InputMethodManager.class);
62         refreshAllInputMethodAndSubtypes();
63     }
64 
refreshAllInputMethodAndSubtypes()65     public void refreshAllInputMethodAndSubtypes() {
66         mMethodList.clear();
67         mMethodList.addAll(mImm.getInputMethodList());
68     }
69 
getInputMethodList()70     public List<InputMethodInfo> getInputMethodList() {
71         return new ArrayList<>(mMethodList);
72     }
73 
isAlwaysCheckedIme(InputMethodInfo imi)74     public boolean isAlwaysCheckedIme(InputMethodInfo imi) {
75         final boolean isEnabled = isEnabledImi(imi);
76         if (getEnabledInputMethodList().size() <= 1 && isEnabled) {
77             return true;
78         }
79 
80         final int enabledValidNonAuxAsciiCapableImeCount =
81                 getEnabledValidNonAuxAsciiCapableImeCount();
82 
83         return enabledValidNonAuxAsciiCapableImeCount <= 1
84                 && !(enabledValidNonAuxAsciiCapableImeCount == 1 && !isEnabled)
85                 && imi.isSystem()
86                 && InputMethodAndSubtypeUtil.isValidNonAuxAsciiCapableIme(imi);
87     }
88 
getEnabledValidNonAuxAsciiCapableImeCount()89     private int getEnabledValidNonAuxAsciiCapableImeCount() {
90         int count = 0;
91         final List<InputMethodInfo> enabledImis = getEnabledInputMethodList();
92         for (final InputMethodInfo imi : enabledImis) {
93             if (InputMethodAndSubtypeUtil.isValidNonAuxAsciiCapableIme(imi)) {
94                 ++count;
95             }
96         }
97         if (count == 0) {
98             Log.w(TAG, "No \"enabledValidNonAuxAsciiCapableIme\"s found.");
99         }
100         return count;
101     }
102 
isEnabledImi(InputMethodInfo imi)103     public boolean isEnabledImi(InputMethodInfo imi) {
104         final List<InputMethodInfo> enabledImis = getEnabledInputMethodList();
105         for (final InputMethodInfo tempImi : enabledImis) {
106             if (tempImi.getId().equals(imi.getId())) {
107                 return true;
108             }
109         }
110         return false;
111     }
112 
113     /**
114      * Returns the list of the enabled {@link InputMethodInfo} determined by
115      * {@link android.provider.Settings.Secure#ENABLED_INPUT_METHODS} rather than just returning
116      * {@link InputMethodManager#getEnabledInputMethodList()}.
117      *
118      * @return the list of the enabled {@link InputMethodInfo}
119      */
getEnabledInputMethodList()120     private ArrayList<InputMethodInfo> getEnabledInputMethodList() {
121         final HashMap<String, HashSet<String>> enabledInputMethodsAndSubtypes =
122                 InputMethodAndSubtypeUtil.getEnabledInputMethodsAndSubtypeList(mContentResolver);
123         final ArrayList<InputMethodInfo> result = new ArrayList<>();
124         for (InputMethodInfo imi : mMethodList) {
125             if (enabledInputMethodsAndSubtypes.keySet().contains(imi.getId())) {
126                 result.add(imi);
127             }
128         }
129         return result;
130     }
131 }
132