1 /* 2 * Copyright (C) 2008-2009 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.example.android.softkeyboard; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.content.res.XmlResourceParser; 22 import android.graphics.drawable.Drawable; 23 import android.inputmethodservice.Keyboard; 24 import android.view.inputmethod.EditorInfo; 25 import android.view.inputmethod.InputMethodManager; 26 27 public class LatinKeyboard extends Keyboard { 28 29 private Key mEnterKey; 30 private Key mSpaceKey; 31 /** 32 * Stores the current state of the mode change key. Its width will be dynamically updated to 33 * match the region of {@link #mModeChangeKey} when {@link #mModeChangeKey} becomes invisible. 34 */ 35 private Key mModeChangeKey; 36 /** 37 * Stores the current state of the language switch key (a.k.a. globe key). This should be 38 * visible while {@link InputMethodManager#shouldOfferSwitchingToNextInputMethod(IBinder)} 39 * returns true. When this key becomes invisible, its width will be shrunk to zero. 40 */ 41 private Key mLanguageSwitchKey; 42 /** 43 * Stores the size and other information of {@link #mModeChangeKey} when 44 * {@link #mLanguageSwitchKey} is visible. This should be immutable and will be used only as a 45 * reference size when the visibility of {@link #mLanguageSwitchKey} is changed. 46 */ 47 private Key mSavedModeChangeKey; 48 /** 49 * Stores the size and other information of {@link #mLanguageSwitchKey} when it is visible. 50 * This should be immutable and will be used only as a reference size when the visibility of 51 * {@link #mLanguageSwitchKey} is changed. 52 */ 53 private Key mSavedLanguageSwitchKey; 54 LatinKeyboard(Context context, int xmlLayoutResId)55 public LatinKeyboard(Context context, int xmlLayoutResId) { 56 super(context, xmlLayoutResId); 57 } 58 LatinKeyboard(Context context, int layoutTemplateResId, CharSequence characters, int columns, int horizontalPadding)59 public LatinKeyboard(Context context, int layoutTemplateResId, 60 CharSequence characters, int columns, int horizontalPadding) { 61 super(context, layoutTemplateResId, characters, columns, horizontalPadding); 62 } 63 64 @Override createKeyFromXml(Resources res, Row parent, int x, int y, XmlResourceParser parser)65 protected Key createKeyFromXml(Resources res, Row parent, int x, int y, 66 XmlResourceParser parser) { 67 Key key = new LatinKey(res, parent, x, y, parser); 68 if (key.codes[0] == 10) { 69 mEnterKey = key; 70 } else if (key.codes[0] == ' ') { 71 mSpaceKey = key; 72 } else if (key.codes[0] == Keyboard.KEYCODE_MODE_CHANGE) { 73 mModeChangeKey = key; 74 mSavedModeChangeKey = new LatinKey(res, parent, x, y, parser); 75 } else if (key.codes[0] == LatinKeyboardView.KEYCODE_LANGUAGE_SWITCH) { 76 mLanguageSwitchKey = key; 77 mSavedLanguageSwitchKey = new LatinKey(res, parent, x, y, parser); 78 } 79 return key; 80 } 81 82 /** 83 * Dynamically change the visibility of the language switch key (a.k.a. globe key). 84 * @param visible True if the language switch key should be visible. 85 */ setLanguageSwitchKeyVisibility(boolean visible)86 void setLanguageSwitchKeyVisibility(boolean visible) { 87 if (visible) { 88 // The language switch key should be visible. Restore the size of the mode change key 89 // and language switch key using the saved layout. 90 mModeChangeKey.width = mSavedModeChangeKey.width; 91 mModeChangeKey.x = mSavedModeChangeKey.x; 92 mLanguageSwitchKey.width = mSavedLanguageSwitchKey.width; 93 mLanguageSwitchKey.icon = mSavedLanguageSwitchKey.icon; 94 mLanguageSwitchKey.iconPreview = mSavedLanguageSwitchKey.iconPreview; 95 } else { 96 // The language switch key should be hidden. Change the width of the mode change key 97 // to fill the space of the language key so that the user will not see any strange gap. 98 mModeChangeKey.width = mSavedModeChangeKey.width + mSavedLanguageSwitchKey.width; 99 mLanguageSwitchKey.width = 0; 100 mLanguageSwitchKey.icon = null; 101 mLanguageSwitchKey.iconPreview = null; 102 } 103 } 104 105 /** 106 * This looks at the ime options given by the current editor, to set the 107 * appropriate label on the keyboard's enter key (if it has one). 108 */ setImeOptions(Resources res, int options)109 void setImeOptions(Resources res, int options) { 110 if (mEnterKey == null) { 111 return; 112 } 113 114 switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) { 115 case EditorInfo.IME_ACTION_GO: 116 mEnterKey.iconPreview = null; 117 mEnterKey.icon = null; 118 mEnterKey.label = res.getText(R.string.label_go_key); 119 break; 120 case EditorInfo.IME_ACTION_NEXT: 121 mEnterKey.iconPreview = null; 122 mEnterKey.icon = null; 123 mEnterKey.label = res.getText(R.string.label_next_key); 124 break; 125 case EditorInfo.IME_ACTION_SEARCH: 126 mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search); 127 mEnterKey.label = null; 128 break; 129 case EditorInfo.IME_ACTION_SEND: 130 mEnterKey.iconPreview = null; 131 mEnterKey.icon = null; 132 mEnterKey.label = res.getText(R.string.label_send_key); 133 break; 134 default: 135 mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return); 136 mEnterKey.label = null; 137 break; 138 } 139 } 140 setSpaceIcon(final Drawable icon)141 void setSpaceIcon(final Drawable icon) { 142 if (mSpaceKey != null) { 143 mSpaceKey.icon = icon; 144 } 145 } 146 147 static class LatinKey extends Keyboard.Key { 148 LatinKey(Resources res, Keyboard.Row parent, int x, int y, XmlResourceParser parser)149 public LatinKey(Resources res, Keyboard.Row parent, int x, int y, 150 XmlResourceParser parser) { 151 super(res, parent, x, y, parser); 152 } 153 154 /** 155 * Overriding this method so that we can reduce the target area for the key that 156 * closes the keyboard. 157 */ 158 @Override isInside(int x, int y)159 public boolean isInside(int x, int y) { 160 return super.isInside(x, codes[0] == KEYCODE_CANCEL ? y - 10 : y); 161 } 162 } 163 164 } 165