summaryrefslogtreecommitdiff
path: root/java/src/com/android/inputmethod/keyboard/MiniKeyboardBuilder.java
blob: 33d19602de55496dd87faf43aa5110c02bebb488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.android.inputmethod.keyboard;

import android.content.Context;
import android.content.res.Resources;

import java.util.List;

public class MiniKeyboardBuilder {
    private final Resources mRes;
    private final Keyboard mKeyboard;
    private final CharSequence[] mPopupCharacters;
    private final int mMaxColumns;
    private final int mNumRows;
    private int mColPos;
    private int mRowPos;
    private int mX;
    private int mY;

    public MiniKeyboardBuilder(Context context, int layoutTemplateResId, Key popupKey) {
        mRes = context.getResources();
        final Keyboard keyboard = new Keyboard(context, layoutTemplateResId, null);
        mKeyboard = keyboard;
        mPopupCharacters = popupKey.mPopupCharacters;
        final int numKeys = mPopupCharacters.length;
        final int maxColumns = popupKey.mMaxPopupColumn;
        int numRows = numKeys / maxColumns;
        if (numKeys % maxColumns != 0) numRows++;
        mMaxColumns = maxColumns;
        mNumRows = numRows;
        keyboard.setHeight((keyboard.getRowHeight() + keyboard.getVerticalGap()) * numRows
                - keyboard.getVerticalGap());
        // TODO: To determine key width we should pay attention to key label length.
        if (numRows > 1) {
            mColPos = numKeys % maxColumns;
            if (mColPos > 0) mColPos = maxColumns - mColPos;
            // Centering top-row keys.
            mX = mColPos * (keyboard.getKeyWidth() + keyboard.getHorizontalGap()) / 2;
        }
        mKeyboard.setMinWidth(0);
    }

    public Keyboard build() {
        final Keyboard keyboard = mKeyboard;
        final List<Key> keys = keyboard.getKeys();
        for (CharSequence label : mPopupCharacters) {
            refresh();
            final Key key = new Key(mRes, keyboard, label, mX, mY, getRowFlags());
            keys.add(key);
            advance();
        }
        return keyboard;
    }

    private int getRowFlags() {
        final int rowPos = mRowPos;
        int rowFlags = 0;
        if (rowPos == 0) rowFlags |= Keyboard.EDGE_TOP;
        if (rowPos == mNumRows - 1) rowFlags |= Keyboard.EDGE_BOTTOM;
        return rowFlags;
    }

    private void refresh() {
        if (mColPos >= mMaxColumns) {
            final Keyboard keyboard = mKeyboard;
            // TODO: Allocate key position depending the precedence of popup characters.
            mX = 0;
            mY += keyboard.getRowHeight() + keyboard.getVerticalGap();
            mColPos = 0;
            mRowPos++;
        }
    }

    private void advance() {
        final Keyboard keyboard = mKeyboard;
        // TODO: Allocate key position depending the precedence of popup characters.
        mX += keyboard.getKeyWidth() + keyboard.getHorizontalGap();
        if (mX > keyboard.getMinWidth())
            keyboard.setMinWidth(mX);
        mColPos++;
    }
}