1 /* 2 * Copyright (C) 2012 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.internal; 18 19 import android.content.res.Resources; 20 import android.content.res.TypedArray; 21 import android.util.Xml; 22 23 import com.android.inputmethod.keyboard.Key; 24 import com.android.inputmethod.keyboard.Keyboard; 25 import com.android.inputmethod.latin.R; 26 import com.android.inputmethod.latin.ResourceUtils; 27 28 import 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 */ 35 public 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 /** Default width of a key in this row. */ 42 private float mDefaultKeyWidth; 43 /** Default height of a key in this row. */ 44 public final int mRowHeight; 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 KeyboardRow(final Resources res, final KeyboardParams params, final XmlPullParser parser, final int y)54 public KeyboardRow(final Resources res, final KeyboardParams params, final XmlPullParser parser, 55 final int y) { 56 mParams = params; 57 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 TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser), 64 R.styleable.Keyboard_Key); 65 mDefaultKeyWidth = ResourceUtils.getDimensionOrFraction(keyAttr, 66 R.styleable.Keyboard_Key_keyWidth, 67 params.mBaseWidth, params.mDefaultKeyWidth); 68 mDefaultBackgroundType = keyAttr.getInt(R.styleable.Keyboard_Key_backgroundType, 69 Key.BACKGROUND_TYPE_NORMAL); 70 keyAttr.recycle(); 71 72 // TODO: Initialize this with <Row> attribute as backgroundType is done. 73 mDefaultKeyLabelFlags = 0; 74 mCurrentY = y; 75 mCurrentX = 0.0f; 76 } 77 getDefaultKeyWidth()78 public float getDefaultKeyWidth() { 79 return mDefaultKeyWidth; 80 } 81 setDefaultKeyWidth(final float defaultKeyWidth)82 public void setDefaultKeyWidth(final float defaultKeyWidth) { 83 mDefaultKeyWidth = defaultKeyWidth; 84 } 85 getDefaultKeyLabelFlags()86 public int getDefaultKeyLabelFlags() { 87 return mDefaultKeyLabelFlags; 88 } 89 setDefaultKeyLabelFlags(final int keyLabelFlags)90 public void setDefaultKeyLabelFlags(final int keyLabelFlags) { 91 mDefaultKeyLabelFlags = keyLabelFlags; 92 } 93 getDefaultBackgroundType()94 public int getDefaultBackgroundType() { 95 return mDefaultBackgroundType; 96 } 97 setDefaultBackgroundType(final int backgroundType)98 public void setDefaultBackgroundType(final int backgroundType) { 99 mDefaultBackgroundType = backgroundType; 100 } 101 setXPos(final float keyXPos)102 public void setXPos(final float keyXPos) { 103 mCurrentX = keyXPos; 104 } 105 advanceXPos(final float width)106 public void advanceXPos(final float width) { 107 mCurrentX += width; 108 } 109 getKeyY()110 public int getKeyY() { 111 return mCurrentY; 112 } 113 getKeyX(final TypedArray keyAttr)114 public float getKeyX(final TypedArray keyAttr) { 115 final int keyboardRightEdge = mParams.mOccupiedWidth 116 - mParams.mHorizontalEdgesPadding; 117 if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyXPos)) { 118 final float keyXPos = ResourceUtils.getDimensionOrFraction(keyAttr, 119 R.styleable.Keyboard_Key_keyXPos, mParams.mBaseWidth, 0); 120 if (keyXPos < 0) { 121 // If keyXPos is negative, the actual x-coordinate will be 122 // keyboardWidth + keyXPos. 123 // keyXPos shouldn't be less than mCurrentX because drawable area for this 124 // key starts at mCurrentX. Or, this key will overlaps the adjacent key on 125 // its left hand side. 126 return Math.max(keyXPos + keyboardRightEdge, mCurrentX); 127 } else { 128 return keyXPos + mParams.mHorizontalEdgesPadding; 129 } 130 } 131 return mCurrentX; 132 } 133 getKeyWidth(final TypedArray keyAttr)134 public float getKeyWidth(final TypedArray keyAttr) { 135 return getKeyWidth(keyAttr, mCurrentX); 136 } 137 getKeyWidth(final TypedArray keyAttr, final float keyXPos)138 public float getKeyWidth(final TypedArray keyAttr, final float keyXPos) { 139 final int widthType = ResourceUtils.getEnumValue(keyAttr, 140 R.styleable.Keyboard_Key_keyWidth, KEYWIDTH_NOT_ENUM); 141 switch (widthType) { 142 case KEYWIDTH_FILL_RIGHT: 143 final int keyboardRightEdge = 144 mParams.mOccupiedWidth - mParams.mHorizontalEdgesPadding; 145 // If keyWidth is fillRight, the actual key width will be determined to fill 146 // out the area up to the right edge of the keyboard. 147 return keyboardRightEdge - keyXPos; 148 default: // KEYWIDTH_NOT_ENUM 149 return ResourceUtils.getDimensionOrFraction(keyAttr, 150 R.styleable.Keyboard_Key_keyWidth, 151 mParams.mBaseWidth, mDefaultKeyWidth); 152 } 153 } 154 } 155