Home | History | Annotate | Download | only in latin
      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 final 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 private int mInputType;
     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         mInputType = inputType;
     38         if (inputClass != InputType.TYPE_CLASS_TEXT) {
     39             // If we are not looking at a TYPE_CLASS_TEXT field, the following strange
     40             // cases may arise, so we do a couple sanity checks for them. If it's a
     41             // TYPE_CLASS_TEXT field, these special cases cannot happen, by construction
     42             // of the flags.
     43             if (null == editorInfo) {
     44                 Log.w(TAG, "No editor info for this field. Bug?");
     45             } else if (InputType.TYPE_NULL == inputType) {
     46                 // TODO: We should honor TYPE_NULL specification.
     47                 Log.i(TAG, "InputType.TYPE_NULL is specified");
     48             } else if (inputClass == 0) {
     49                 // TODO: is this check still necessary?
     50                 Log.w(TAG, String.format("Unexpected input class: inputType=0x%08x"
     51                         + " imeOptions=0x%08x",
     52                         inputType, editorInfo.imeOptions));
     53             }
     54             mIsSettingsSuggestionStripOn = false;
     55             mInputTypeNoAutoCorrect = false;
     56             mApplicationSpecifiedCompletionOn = false;
     57         } else {
     58             final int variation = inputType & InputType.TYPE_MASK_VARIATION;
     59             final boolean flagNoSuggestions =
     60                     0 != (inputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
     61             final boolean flagMultiLine =
     62                     0 != (inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE);
     63             final boolean flagAutoCorrect =
     64                     0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
     65             final boolean flagAutoComplete =
     66                     0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
     67 
     68             // Make sure that passwords are not displayed in {@link SuggestionStripView}.
     69             if (InputTypeUtils.isPasswordInputType(inputType)
     70                     || InputTypeUtils.isVisiblePasswordInputType(inputType)
     71                     || InputTypeUtils.isEmailVariation(variation)
     72                     || InputType.TYPE_TEXT_VARIATION_URI == variation
     73                     || InputType.TYPE_TEXT_VARIATION_FILTER == variation
     74                     || flagNoSuggestions
     75                     || flagAutoComplete) {
     76                 mIsSettingsSuggestionStripOn = false;
     77             } else {
     78                 mIsSettingsSuggestionStripOn = true;
     79             }
     80 
     81             // If it's a browser edit field and auto correct is not ON explicitly, then
     82             // disable auto correction, but keep suggestions on.
     83             // If NO_SUGGESTIONS is set, don't do prediction.
     84             // If it's not multiline and the autoCorrect flag is not set, then don't correct
     85             if ((variation == InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT
     86                     && !flagAutoCorrect)
     87                     || flagNoSuggestions
     88                     || (!flagAutoCorrect && !flagMultiLine)) {
     89                 mInputTypeNoAutoCorrect = true;
     90             } else {
     91                 mInputTypeNoAutoCorrect = false;
     92             }
     93 
     94             mApplicationSpecifiedCompletionOn = flagAutoComplete && isFullscreenMode;
     95         }
     96     }
     97 
     98     public boolean isSameInputType(final EditorInfo editorInfo) {
     99         return editorInfo.inputType == mInputType;
    100     }
    101 
    102     @SuppressWarnings("unused")
    103     private void dumpFlags(final int inputType) {
    104         Log.i(TAG, "Input class:");
    105         final int inputClass = inputType & InputType.TYPE_MASK_CLASS;
    106         if (inputClass == InputType.TYPE_CLASS_TEXT)
    107             Log.i(TAG, "  TYPE_CLASS_TEXT");
    108         if (inputClass == InputType.TYPE_CLASS_PHONE)
    109             Log.i(TAG, "  TYPE_CLASS_PHONE");
    110         if (inputClass == InputType.TYPE_CLASS_NUMBER)
    111             Log.i(TAG, "  TYPE_CLASS_NUMBER");
    112         if (inputClass == InputType.TYPE_CLASS_DATETIME)
    113             Log.i(TAG, "  TYPE_CLASS_DATETIME");
    114         Log.i(TAG, "Variation:");
    115         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS))
    116             Log.i(TAG, "  TYPE_TEXT_VARIATION_EMAIL_ADDRESS");
    117         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT))
    118             Log.i(TAG, "  TYPE_TEXT_VARIATION_EMAIL_SUBJECT");
    119         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_FILTER))
    120             Log.i(TAG, "  TYPE_TEXT_VARIATION_FILTER");
    121         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE))
    122             Log.i(TAG, "  TYPE_TEXT_VARIATION_LONG_MESSAGE");
    123         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_NORMAL))
    124             Log.i(TAG, "  TYPE_TEXT_VARIATION_NORMAL");
    125         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_PASSWORD))
    126             Log.i(TAG, "  TYPE_TEXT_VARIATION_PASSWORD");
    127         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_PERSON_NAME))
    128             Log.i(TAG, "  TYPE_TEXT_VARIATION_PERSON_NAME");
    129         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_PHONETIC))
    130             Log.i(TAG, "  TYPE_TEXT_VARIATION_PHONETIC");
    131         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS))
    132             Log.i(TAG, "  TYPE_TEXT_VARIATION_POSTAL_ADDRESS");
    133         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE))
    134             Log.i(TAG, "  TYPE_TEXT_VARIATION_SHORT_MESSAGE");
    135         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_URI))
    136             Log.i(TAG, "  TYPE_TEXT_VARIATION_URI");
    137         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD))
    138             Log.i(TAG, "  TYPE_TEXT_VARIATION_VISIBLE_PASSWORD");
    139         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT))
    140             Log.i(TAG, "  TYPE_TEXT_VARIATION_WEB_EDIT_TEXT");
    141         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS))
    142             Log.i(TAG, "  TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS");
    143         if (0 != (inputType & InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD))
    144             Log.i(TAG, "  TYPE_TEXT_VARIATION_WEB_PASSWORD");
    145         Log.i(TAG, "Flags:");
    146         if (0 != (inputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS))
    147             Log.i(TAG, "  TYPE_TEXT_FLAG_NO_SUGGESTIONS");
    148         if (0 != (inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE))
    149             Log.i(TAG, "  TYPE_TEXT_FLAG_MULTI_LINE");
    150         if (0 != (inputType & InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE))
    151             Log.i(TAG, "  TYPE_TEXT_FLAG_IME_MULTI_LINE");
    152         if (0 != (inputType & InputType.TYPE_TEXT_FLAG_CAP_WORDS))
    153             Log.i(TAG, "  TYPE_TEXT_FLAG_CAP_WORDS");
    154         if (0 != (inputType & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES))
    155             Log.i(TAG, "  TYPE_TEXT_FLAG_CAP_SENTENCES");
    156         if (0 != (inputType & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS))
    157             Log.i(TAG, "  TYPE_TEXT_FLAG_CAP_CHARACTERS");
    158         if (0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_CORRECT))
    159             Log.i(TAG, "  TYPE_TEXT_FLAG_AUTO_CORRECT");
    160         if (0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE))
    161             Log.i(TAG, "  TYPE_TEXT_FLAG_AUTO_COMPLETE");
    162     }
    163 
    164     // Pretty print
    165     @Override
    166     public String toString() {
    167         return "\n mInputTypeNoAutoCorrect = " + mInputTypeNoAutoCorrect
    168                 + "\n mIsSettingsSuggestionStripOn = " + mIsSettingsSuggestionStripOn
    169                 + "\n mApplicationSpecifiedCompletionOn = " + mApplicationSpecifiedCompletionOn;
    170     }
    171 
    172     public static boolean inPrivateImeOptions(String packageName, String key,
    173             EditorInfo editorInfo) {
    174         if (editorInfo == null) return false;
    175         final String findingKey = (packageName != null) ? packageName + "." + key
    176                 : key;
    177         return StringUtils.containsInCsv(findingKey, editorInfo.privateImeOptions);
    178     }
    179 }
    180