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