• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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         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 
getDefaultKeyWidth()77     public float getDefaultKeyWidth() {
78         return mDefaultKeyWidth;
79     }
80 
setDefaultKeyWidth(final float defaultKeyWidth)81     public void setDefaultKeyWidth(final float defaultKeyWidth) {
82         mDefaultKeyWidth = defaultKeyWidth;
83     }
84 
getDefaultKeyLabelFlags()85     public int getDefaultKeyLabelFlags() {
86         return mDefaultKeyLabelFlags;
87     }
88 
setDefaultKeyLabelFlags(final int keyLabelFlags)89     public void setDefaultKeyLabelFlags(final int keyLabelFlags) {
90         mDefaultKeyLabelFlags = keyLabelFlags;
91     }
92 
getDefaultBackgroundType()93     public int getDefaultBackgroundType() {
94         return mDefaultBackgroundType;
95     }
96 
setDefaultBackgroundType(final int backgroundType)97     public void setDefaultBackgroundType(final int backgroundType) {
98         mDefaultBackgroundType = backgroundType;
99     }
100 
setXPos(final float keyXPos)101     public void setXPos(final float keyXPos) {
102         mCurrentX = keyXPos;
103     }
104 
advanceXPos(final float width)105     public void advanceXPos(final float width) {
106         mCurrentX += width;
107     }
108 
getKeyY()109     public int getKeyY() {
110         return mCurrentY;
111     }
112 
getKeyX(final TypedArray keyAttr)113     public float getKeyX(final TypedArray keyAttr) {
114         if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyXPos)) {
115             final float keyXPos = keyAttr.getFraction(R.styleable.Keyboard_Key_keyXPos,
116                     mParams.mBaseWidth, mParams.mBaseWidth, 0);
117             if (keyXPos < 0) {
118                 // If keyXPos is negative, the actual x-coordinate will be
119                 // keyboardWidth + keyXPos.
120                 // keyXPos shouldn't be less than mCurrentX because drawable area for this
121                 // key starts at mCurrentX. Or, this key will overlaps the adjacent key on
122                 // its left hand side.
123                 final int keyboardRightEdge = mParams.mOccupiedWidth - mParams.mRightPadding;
124                 return Math.max(keyXPos + keyboardRightEdge, mCurrentX);
125             } else {
126                 return keyXPos + mParams.mLeftPadding;
127             }
128         }
129         return mCurrentX;
130     }
131 
getKeyWidth(final TypedArray keyAttr)132     public float getKeyWidth(final TypedArray keyAttr) {
133         return getKeyWidth(keyAttr, mCurrentX);
134     }
135 
getKeyWidth(final TypedArray keyAttr, final float keyXPos)136     public float getKeyWidth(final TypedArray keyAttr, final float keyXPos) {
137         final int widthType = ResourceUtils.getEnumValue(keyAttr,
138                 R.styleable.Keyboard_Key_keyWidth, KEYWIDTH_NOT_ENUM);
139         switch (widthType) {
140         case KEYWIDTH_FILL_RIGHT:
141             // If keyWidth is fillRight, the actual key width will be determined to fill
142             // out the area up to the right edge of the keyboard.
143             final int keyboardRightEdge = mParams.mOccupiedWidth - mParams.mRightPadding;
144             return keyboardRightEdge - keyXPos;
145         default: // KEYWIDTH_NOT_ENUM
146             return keyAttr.getFraction(R.styleable.Keyboard_Key_keyWidth,
147                     mParams.mBaseWidth, mParams.mBaseWidth, mDefaultKeyWidth);
148         }
149     }
150 }
151