Home | History | Annotate | Download | only in keyboard
      1 /*
      2  * Copyright (C) 2010 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.keyboard;
     18 
     19 import android.view.inputmethod.EditorInfo;
     20 
     21 import com.android.inputmethod.compat.EditorInfoCompatUtils;
     22 import com.android.inputmethod.compat.InputTypeCompatUtils;
     23 import com.android.inputmethod.latin.R;
     24 
     25 import java.util.Arrays;
     26 import java.util.Locale;
     27 
     28 /**
     29  * Represents the parameters necessary to construct a new LatinKeyboard,
     30  * which also serve as a unique identifier for each keyboard type.
     31  */
     32 public class KeyboardId {
     33     public static final int MODE_TEXT = 0;
     34     public static final int MODE_URL = 1;
     35     public static final int MODE_EMAIL = 2;
     36     public static final int MODE_IM = 3;
     37     public static final int MODE_PHONE = 4;
     38     public static final int MODE_NUMBER = 5;
     39 
     40     public static final int F2KEY_MODE_NONE = 0;
     41     public static final int F2KEY_MODE_SETTINGS = 1;
     42     public static final int F2KEY_MODE_SHORTCUT_IME = 2;
     43     public static final int F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS = 3;
     44 
     45     public final Locale mLocale;
     46     public final int mOrientation;
     47     public final int mWidth;
     48     public final int mMode;
     49     public final int mXmlId;
     50     public final boolean mNavigateAction;
     51     public final boolean mPasswordInput;
     52     // TODO: Clean up these booleans and modes.
     53     public final boolean mHasSettingsKey;
     54     public final int mF2KeyMode;
     55     public final boolean mClobberSettingsKey;
     56     public final boolean mShortcutKeyEnabled;
     57     public final boolean mHasShortcutKey;
     58     public final int mImeAction;
     59 
     60     public final String mXmlName;
     61     public final EditorInfo mAttribute;
     62 
     63     private final int mHashCode;
     64 
     65     public KeyboardId(String xmlName, int xmlId, Locale locale, int orientation, int width,
     66             int mode, EditorInfo attribute, boolean hasSettingsKey, int f2KeyMode,
     67             boolean clobberSettingsKey, boolean shortcutKeyEnabled, boolean hasShortcutKey) {
     68         final int inputType = (attribute != null) ? attribute.inputType : 0;
     69         final int imeOptions = (attribute != null) ? attribute.imeOptions : 0;
     70         this.mLocale = locale;
     71         this.mOrientation = orientation;
     72         this.mWidth = width;
     73         this.mMode = mode;
     74         this.mXmlId = xmlId;
     75         // Note: Turn off checking navigation flag to show TAB key for now.
     76         this.mNavigateAction = InputTypeCompatUtils.isWebInputType(inputType);
     77 //                || EditorInfoCompatUtils.hasFlagNavigateNext(imeOptions)
     78 //                || EditorInfoCompatUtils.hasFlagNavigatePrevious(imeOptions);
     79         this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType)
     80                 || InputTypeCompatUtils.isVisiblePasswordInputType(inputType);
     81         this.mHasSettingsKey = hasSettingsKey;
     82         this.mF2KeyMode = f2KeyMode;
     83         this.mClobberSettingsKey = clobberSettingsKey;
     84         this.mShortcutKeyEnabled = shortcutKeyEnabled;
     85         this.mHasShortcutKey = hasShortcutKey;
     86         // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and
     87         // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
     88         this.mImeAction = imeOptions & (
     89                 EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
     90 
     91         this.mXmlName = xmlName;
     92         this.mAttribute = attribute;
     93 
     94         this.mHashCode = Arrays.hashCode(new Object[] {
     95                 locale,
     96                 orientation,
     97                 width,
     98                 mode,
     99                 xmlId,
    100                 mNavigateAction,
    101                 mPasswordInput,
    102                 hasSettingsKey,
    103                 f2KeyMode,
    104                 clobberSettingsKey,
    105                 shortcutKeyEnabled,
    106                 hasShortcutKey,
    107                 mImeAction,
    108         });
    109     }
    110 
    111     public KeyboardId cloneWithNewXml(String xmlName, int xmlId) {
    112         return new KeyboardId(xmlName, xmlId, mLocale, mOrientation, mWidth, mMode, mAttribute,
    113                 false, F2KEY_MODE_NONE, false, false, false);
    114     }
    115 
    116     public int getXmlId() {
    117         return mXmlId;
    118     }
    119 
    120     public boolean isAlphabetKeyboard() {
    121         return mXmlId == R.xml.kbd_qwerty;
    122     }
    123 
    124     public boolean isSymbolsKeyboard() {
    125         return mXmlId == R.xml.kbd_symbols || mXmlId == R.xml.kbd_symbols_shift;
    126     }
    127 
    128     public boolean isPhoneKeyboard() {
    129         return mMode == MODE_PHONE;
    130     }
    131 
    132     public boolean isPhoneShiftKeyboard() {
    133         return mXmlId == R.xml.kbd_phone_shift;
    134     }
    135 
    136     public boolean isNumberKeyboard() {
    137         return mMode == MODE_NUMBER;
    138     }
    139 
    140     @Override
    141     public boolean equals(Object other) {
    142         return other instanceof KeyboardId && equals((KeyboardId) other);
    143     }
    144 
    145     private boolean equals(KeyboardId other) {
    146         return other.mLocale.equals(this.mLocale)
    147             && other.mOrientation == this.mOrientation
    148             && other.mWidth == this.mWidth
    149             && other.mMode == this.mMode
    150             && other.mXmlId == this.mXmlId
    151             && other.mNavigateAction == this.mNavigateAction
    152             && other.mPasswordInput == this.mPasswordInput
    153             && other.mHasSettingsKey == this.mHasSettingsKey
    154             && other.mF2KeyMode == this.mF2KeyMode
    155             && other.mClobberSettingsKey == this.mClobberSettingsKey
    156             && other.mShortcutKeyEnabled == this.mShortcutKeyEnabled
    157             && other.mHasShortcutKey == this.mHasShortcutKey
    158             && other.mImeAction == this.mImeAction;
    159     }
    160 
    161     @Override
    162     public int hashCode() {
    163         return mHashCode;
    164     }
    165 
    166     @Override
    167     public String toString() {
    168         return String.format("[%s.xml %s %s%d %s %s %s%s%s%s%s%s%s]",
    169                 mXmlName,
    170                 mLocale,
    171                 (mOrientation == 1 ? "port" : "land"), mWidth,
    172                 modeName(mMode),
    173                 EditorInfoCompatUtils.imeOptionsName(mImeAction),
    174                 f2KeyModeName(mF2KeyMode),
    175                 (mClobberSettingsKey ? " clobberSettingsKey" : ""),
    176                 (mNavigateAction ? " navigateAction" : ""),
    177                 (mPasswordInput ? " passwordInput" : ""),
    178                 (mHasSettingsKey ? " hasSettingsKey" : ""),
    179                 (mShortcutKeyEnabled ? " shortcutKeyEnabled" : ""),
    180                 (mHasShortcutKey ? " hasShortcutKey" : "")
    181         );
    182     }
    183 
    184     public static String modeName(int mode) {
    185         switch (mode) {
    186         case MODE_TEXT: return "text";
    187         case MODE_URL: return "url";
    188         case MODE_EMAIL: return "email";
    189         case MODE_IM: return "im";
    190         case MODE_PHONE: return "phone";
    191         case MODE_NUMBER: return "number";
    192         default: return null;
    193         }
    194     }
    195 
    196     public static String f2KeyModeName(int f2KeyMode) {
    197         switch (f2KeyMode) {
    198         case F2KEY_MODE_NONE: return "none";
    199         case F2KEY_MODE_SETTINGS: return "settings";
    200         case F2KEY_MODE_SHORTCUT_IME: return "shortcutIme";
    201         case F2KEY_MODE_SHORTCUT_IME_OR_SETTINGS: return "shortcutImeOrSettings";
    202         default: return null;
    203         }
    204     }
    205 }
    206