Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2012 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.inputmethod.latin;
     18 
     19 import android.text.InputType;
     20 import android.view.inputmethod.EditorInfo;
     21 
     22 public final class InputTypeUtils implements InputType {
     23     private static final int WEB_TEXT_PASSWORD_INPUT_TYPE =
     24             TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_PASSWORD;
     25     private static final int WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE =
     26             TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
     27     private static final int NUMBER_PASSWORD_INPUT_TYPE =
     28             TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD;
     29     private static final int TEXT_PASSWORD_INPUT_TYPE =
     30             TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD;
     31     private static final int TEXT_VISIBLE_PASSWORD_INPUT_TYPE =
     32             TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
     33     private static final int[] SUPPRESSING_AUTO_SPACES_FIELD_VARIATION = {
     34         InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
     35         InputType.TYPE_TEXT_VARIATION_PASSWORD,
     36         InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,
     37         InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD };
     38     public static final int IME_ACTION_CUSTOM_LABEL = EditorInfo.IME_MASK_ACTION + 1;
     39 
     40     private InputTypeUtils() {
     41         // This utility class is not publicly instantiable.
     42     }
     43 
     44     private static boolean isWebEditTextInputType(final int inputType) {
     45         return inputType == (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
     46     }
     47 
     48     private static boolean isWebPasswordInputType(final int inputType) {
     49         return WEB_TEXT_PASSWORD_INPUT_TYPE != 0
     50                 && inputType == WEB_TEXT_PASSWORD_INPUT_TYPE;
     51     }
     52 
     53     private static boolean isWebEmailAddressInputType(final int inputType) {
     54         return WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE != 0
     55                 && inputType == WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE;
     56     }
     57 
     58     private static boolean isNumberPasswordInputType(final int inputType) {
     59         return NUMBER_PASSWORD_INPUT_TYPE != 0
     60                 && inputType == NUMBER_PASSWORD_INPUT_TYPE;
     61     }
     62 
     63     private static boolean isTextPasswordInputType(final int inputType) {
     64         return inputType == TEXT_PASSWORD_INPUT_TYPE;
     65     }
     66 
     67     private static boolean isWebEmailAddressVariation(int variation) {
     68         return variation == TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
     69     }
     70 
     71     public static boolean isEmailVariation(final int variation) {
     72         return variation == TYPE_TEXT_VARIATION_EMAIL_ADDRESS
     73                 || isWebEmailAddressVariation(variation);
     74     }
     75 
     76     public static boolean isWebInputType(final int inputType) {
     77         final int maskedInputType =
     78                 inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION);
     79         return isWebEditTextInputType(maskedInputType) || isWebPasswordInputType(maskedInputType)
     80                 || isWebEmailAddressInputType(maskedInputType);
     81     }
     82 
     83     // Please refer to TextView.isPasswordInputType
     84     public static boolean isPasswordInputType(final int inputType) {
     85         final int maskedInputType =
     86                 inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION);
     87         return isTextPasswordInputType(maskedInputType) || isWebPasswordInputType(maskedInputType)
     88                 || isNumberPasswordInputType(maskedInputType);
     89     }
     90 
     91     // Please refer to TextView.isVisiblePasswordInputType
     92     public static boolean isVisiblePasswordInputType(final int inputType) {
     93         final int maskedInputType =
     94                 inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION);
     95         return maskedInputType == TEXT_VISIBLE_PASSWORD_INPUT_TYPE;
     96     }
     97 
     98     public static boolean isAutoSpaceFriendlyType(final int inputType) {
     99         if (TYPE_CLASS_TEXT != (TYPE_MASK_CLASS & inputType)) return false;
    100         final int variation = TYPE_MASK_VARIATION & inputType;
    101         for (final int fieldVariation : SUPPRESSING_AUTO_SPACES_FIELD_VARIATION) {
    102             if (variation == fieldVariation) return false;
    103         }
    104         return true;
    105     }
    106 
    107     public static int getImeOptionsActionIdFromEditorInfo(final EditorInfo editorInfo) {
    108         if ((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
    109             return EditorInfo.IME_ACTION_NONE;
    110         } else if (editorInfo.actionLabel != null) {
    111             return IME_ACTION_CUSTOM_LABEL;
    112         } else {
    113             // Note: this is different from editorInfo.actionId, hence "ImeOptionsActionId"
    114             return editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;
    115         }
    116     }
    117 }
    118