KeyboardRow.java revision 784416f73bc5053114ceb8274dba4bdabbbda700
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 17package com.android.inputmethod.keyboard.internal; 18 19import android.content.res.Resources; 20import android.content.res.TypedArray; 21import android.util.Xml; 22 23import com.android.inputmethod.keyboard.Key; 24import com.android.inputmethod.keyboard.Keyboard; 25import com.android.inputmethod.latin.R; 26import com.android.inputmethod.latin.utils.ResourceUtils; 27 28import org.xmlpull.v1.XmlPullParser; 29 30/** 31 * Container for keys in the keyboard. All keys in a row are at the same Y-coordinate. 32 * Some of the key size defaults can be overridden per row from what the {@link Keyboard} 33 * defines. 34 */ 35public final class KeyboardRow { 36 // keyWidth enum constants 37 private static final int KEYWIDTH_NOT_ENUM = 0; 38 private static final int KEYWIDTH_FILL_RIGHT = -1; 39 40 private final KeyboardParams mParams; 41 /** The height of this row. */ 42 private final int mRowHeight; 43 /** Default width of a key in this row. */ 44 private float mDefaultKeyWidth; 45 /** Default keyLabelFlags in this row. */ 46 private int mDefaultKeyLabelFlags; 47 /** Default backgroundType for this row */ 48 private int mDefaultBackgroundType; 49 50 private final int mCurrentY; 51 // Will be updated by {@link Key}'s constructor. 52 private float mCurrentX; 53 54 public KeyboardRow(final Resources res, final KeyboardParams params, final XmlPullParser parser, 55 final int y) { 56 mParams = params; 57 final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser), 58 R.styleable.Keyboard); 59 mRowHeight = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr, 60 R.styleable.Keyboard_rowHeight, 61 params.mBaseHeight, params.mDefaultRowHeight); 62 keyboardAttr.recycle(); 63 final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser), 64 R.styleable.Keyboard_Key); 65 mDefaultKeyWidth = keyAttr.getFraction(R.styleable.Keyboard_Key_keyWidth, 66 params.mBaseWidth, params.mBaseWidth, params.mDefaultKeyWidth); 67 mDefaultBackgroundType = keyAttr.getInt(R.styleable.Keyboard_Key_backgroundType, 68 Key.BACKGROUND_TYPE_NORMAL); 69 keyAttr.recycle(); 70 71 // TODO: Initialize this with <Row> attribute as backgroundType is done. 72 mDefaultKeyLabelFlags = 0; 73 mCurrentY = y; 74 mCurrentX = 0.0f; 75 } 76 77 public int getRowHeight() { 78 return mRowHeight; 79 } 80 81 public float getDefaultKeyWidth() { 82 return mDefaultKeyWidth; 83 } 84 85 public void setDefaultKeyWidth(final float defaultKeyWidth) { 86 mDefaultKeyWidth = defaultKeyWidth; 87 } 88 89 public int getDefaultKeyLabelFlags() { 90 return mDefaultKeyLabelFlags; 91 } 92 93 public void setDefaultKeyLabelFlags(final int keyLabelFlags) { 94 mDefaultKeyLabelFlags = keyLabelFlags; 95 } 96 97 public int getDefaultBackgroundType() { 98 return mDefaultBackgroundType; 99 } 100 101 public void setDefaultBackgroundType(final int backgroundType) { 102 mDefaultBackgroundType = backgroundType; 103 } 104 105 public void setXPos(final float keyXPos) { 106 mCurrentX = keyXPos; 107 } 108 109 public void advanceXPos(final float width) { 110 mCurrentX += width; 111 } 112 113 public int getKeyY() { 114 return mCurrentY; 115 } 116 117 public float getKeyX(final TypedArray keyAttr) { 118 if (keyAttr == null || !keyAttr.hasValue(R.styleable.Keyboard_Key_keyXPos)) { 119 return mCurrentX; 120 } 121 final float keyXPos = keyAttr.getFraction(R.styleable.Keyboard_Key_keyXPos, 122 mParams.mBaseWidth, mParams.mBaseWidth, 0); 123 if (keyXPos >= 0) { 124 return keyXPos + mParams.mLeftPadding; 125 } 126 // If keyXPos is negative, the actual x-coordinate will be 127 // keyboardWidth + keyXPos. 128 // keyXPos shouldn't be less than mCurrentX because drawable area for this 129 // key starts at mCurrentX. Or, this key will overlaps the adjacent key on 130 // its left hand side. 131 final int keyboardRightEdge = mParams.mOccupiedWidth - mParams.mRightPadding; 132 return Math.max(keyXPos + keyboardRightEdge, mCurrentX); 133 } 134 135 public float getKeyWidth(final TypedArray keyAttr) { 136 return getKeyWidth(keyAttr, mCurrentX); 137 } 138 139 public float getKeyWidth(final TypedArray keyAttr, final float keyXPos) { 140 if (keyAttr == null) { 141 return mDefaultKeyWidth; 142 } 143 final int widthType = ResourceUtils.getEnumValue(keyAttr, 144 R.styleable.Keyboard_Key_keyWidth, KEYWIDTH_NOT_ENUM); 145 switch (widthType) { 146 case KEYWIDTH_FILL_RIGHT: 147 // If keyWidth is fillRight, the actual key width will be determined to fill 148 // out the area up to the right edge of the keyboard. 149 final int keyboardRightEdge = mParams.mOccupiedWidth - mParams.mRightPadding; 150 return keyboardRightEdge - keyXPos; 151 default: // KEYWIDTH_NOT_ENUM 152 return keyAttr.getFraction(R.styleable.Keyboard_Key_keyWidth, 153 mParams.mBaseWidth, mParams.mBaseWidth, mDefaultKeyWidth); 154 } 155 } 156} 157