1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.inputmethod.latin; 18 19 import android.text.InputType; 20 import android.util.Log; 21 import android.view.inputmethod.EditorInfo; 22 23 /** 24 * Class to hold attributes of the input field. 25 */ 26 public class InputAttributes { 27 private final String TAG = InputAttributes.class.getSimpleName(); 28 29 final public boolean mInputTypeNoAutoCorrect; 30 final public boolean mIsSettingsSuggestionStripOn; 31 final public boolean mApplicationSpecifiedCompletionOn; 32 final public int mEditorAction; 33 34 public InputAttributes(final EditorInfo editorInfo, final boolean isFullscreenMode) { 35 final int inputType = null != editorInfo ? editorInfo.inputType : 0; 36 final int inputClass = inputType & InputType.TYPE_MASK_CLASS; 37 if (inputClass != InputType.TYPE_CLASS_TEXT) { 38 // If we are not looking at a TYPE_CLASS_TEXT field, the following strange 39 // cases may arise, so we do a couple sanity checks for them. If it's a 40 // TYPE_CLASS_TEXT field, these special cases cannot happen, by construction 41 // of the flags. 42 if (null == editorInfo) { 43 Log.w(TAG, "No editor info for this field. Bug?"); 44 } else if (InputType.TYPE_NULL == inputType) { 45 // TODO: We should honor TYPE_NULL specification. 46 Log.i(TAG, "InputType.TYPE_NULL is specified"); 47 } else if (inputClass == 0) { 48 // TODO: is this check still necessary? 49 Log.w(TAG, String.format("Unexpected input class: inputType=0x%08x" 50 + " imeOptions=0x%08x", 51 inputType, editorInfo.imeOptions)); 52 } 53 mIsSettingsSuggestionStripOn = false; 54 mInputTypeNoAutoCorrect = false; 55 mApplicationSpecifiedCompletionOn = false; 56 } else { 57 final int variation = inputType & InputType.TYPE_MASK_VARIATION; 58 final boolean flagNoSuggestions = 59 0 != (inputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); 60 final boolean flagMultiLine = 61 0 != (inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE); 62 final boolean flagAutoCorrect = 63 0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); 64 final boolean flagAutoComplete = 65 0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); 66 67 // Make sure that passwords are not displayed in {@link SuggestionsView}. 68 if (InputTypeUtils.isPasswordInputType(inputType) 69 || InputTypeUtils.isVisiblePasswordInputType(inputType) 70 || InputTypeUtils.isEmailVariation(variation) 71 || InputType.TYPE_TEXT_VARIATION_URI == variation 72 || InputType.TYPE_TEXT_VARIATION_FILTER == variation 73 || flagNoSuggestions 74 || flagAutoComplete) { 75 mIsSettingsSuggestionStripOn = false; 76 } else { 77 mIsSettingsSuggestionStripOn = true; 78 } 79 80 // If it's a browser edit field and auto correct is not ON explicitly, then 81 // disable auto correction, but keep suggestions on. 82 // If NO_SUGGESTIONS is set, don't do prediction. 83 // If it's not multiline and the autoCorrect flag is not set, then don't correct 84 if ((variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT 85 && !flagAutoCorrect) 86 || flagNoSuggestions 87 || (!flagAutoCorrect && !flagMultiLine)) { 88 mInputTypeNoAutoCorrect = true; 89 } else { 90 mInputTypeNoAutoCorrect = false; 91 } 92 93 mApplicationSpecifiedCompletionOn = flagAutoComplete && isFullscreenMode; 94 } 95 mEditorAction = (editorInfo == null) ? EditorInfo.IME_ACTION_UNSPECIFIED 96 : editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION; 97 } 98 99 @SuppressWarnings("unused") 100 private void dumpFlags(final int inputType) { 101 Log.i(TAG, "Input class:"); 102 final int inputClass = inputType & InputType.TYPE_MASK_CLASS; 103 if (inputClass == InputType.TYPE_CLASS_TEXT) 104 Log.i(TAG, " TYPE_CLASS_TEXT"); 105 if (inputClass == InputType.TYPE_CLASS_PHONE) 106 Log.i(TAG, " TYPE_CLASS_PHONE"); 107 if (inputClass == InputType.TYPE_CLASS_NUMBER) 108 Log.i(TAG, " TYPE_CLASS_NUMBER"); 109 if (inputClass == InputType.TYPE_CLASS_DATETIME) 110 Log.i(TAG, " TYPE_CLASS_DATETIME"); 111 Log.i(TAG, "Variation:"); 112 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS)) 113 Log.i(TAG, " TYPE_TEXT_VARIATION_EMAIL_ADDRESS"); 114 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT)) 115 Log.i(TAG, " TYPE_TEXT_VARIATION_EMAIL_SUBJECT"); 116 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_FILTER)) 117 Log.i(TAG, " TYPE_TEXT_VARIATION_FILTER"); 118 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE)) 119 Log.i(TAG, " TYPE_TEXT_VARIATION_LONG_MESSAGE"); 120 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_NORMAL)) 121 Log.i(TAG, " TYPE_TEXT_VARIATION_NORMAL"); 122 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_PASSWORD)) 123 Log.i(TAG, " TYPE_TEXT_VARIATION_PASSWORD"); 124 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_PERSON_NAME)) 125 Log.i(TAG, " TYPE_TEXT_VARIATION_PERSON_NAME"); 126 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_PHONETIC)) 127 Log.i(TAG, " TYPE_TEXT_VARIATION_PHONETIC"); 128 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS)) 129 Log.i(TAG, " TYPE_TEXT_VARIATION_POSTAL_ADDRESS"); 130 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE)) 131 Log.i(TAG, " TYPE_TEXT_VARIATION_SHORT_MESSAGE"); 132 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_URI)) 133 Log.i(TAG, " TYPE_TEXT_VARIATION_URI"); 134 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD)) 135 Log.i(TAG, " TYPE_TEXT_VARIATION_VISIBLE_PASSWORD"); 136 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT)) 137 Log.i(TAG, " TYPE_TEXT_VARIATION_WEB_EDIT_TEXT"); 138 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS)) 139 Log.i(TAG, " TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS"); 140 if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD)) 141 Log.i(TAG, " TYPE_TEXT_VARIATION_WEB_PASSWORD"); 142 Log.i(TAG, "Flags:"); 143 if (0 != (inputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)) 144 Log.i(TAG, " TYPE_TEXT_FLAG_NO_SUGGESTIONS"); 145 if (0 != (inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE)) 146 Log.i(TAG, " TYPE_TEXT_FLAG_MULTI_LINE"); 147 if (0 != (inputType & InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE)) 148 Log.i(TAG, " TYPE_TEXT_FLAG_IME_MULTI_LINE"); 149 if (0 != (inputType & InputType.TYPE_TEXT_FLAG_CAP_WORDS)) 150 Log.i(TAG, " TYPE_TEXT_FLAG_CAP_WORDS"); 151 if (0 != (inputType & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)) 152 Log.i(TAG, " TYPE_TEXT_FLAG_CAP_SENTENCES"); 153 if (0 != (inputType & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS)) 154 Log.i(TAG, " TYPE_TEXT_FLAG_CAP_CHARACTERS"); 155 if (0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT)) 156 Log.i(TAG, " TYPE_TEXT_FLAG_AUTO_CORRECT"); 157 if (0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE)) 158 Log.i(TAG, " TYPE_TEXT_FLAG_AUTO_COMPLETE"); 159 } 160 161 // Pretty print 162 @Override 163 public String toString() { 164 return "\n mInputTypeNoAutoCorrect = " + mInputTypeNoAutoCorrect 165 + "\n mIsSettingsSuggestionStripOn = " + mIsSettingsSuggestionStripOn 166 + "\n mApplicationSpecifiedCompletionOn = " + mApplicationSpecifiedCompletionOn; 167 } 168 169 public static boolean inPrivateImeOptions(String packageName, String key, 170 EditorInfo editorInfo) { 171 if (editorInfo == null) return false; 172 final String findingKey = (packageName != null) ? packageName + "." + key 173 : key; 174 return StringUtils.containsInCsv(findingKey, editorInfo.privateImeOptions); 175 } 176 } 177