aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/anysoftkeyboard/keyboards/AnyPopupKeyboard.java
blob: 5c5c06ea589a8e8e75e2f2f61e8e75e711686b36 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (c) 2013 Menny Even-Danan
 *
 * 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.anysoftkeyboard.keyboards;

import android.content.Context;
import android.support.annotation.NonNull;

import com.anysoftkeyboard.addons.AddOn;
import com.menny.android.anysoftkeyboard.R;

import java.util.List;

public class AnyPopupKeyboard extends AnyKeyboard {

    private int mAdditionalWidth = 0;
    private static final char[] EMPTY_CHAR_ARRAY = new char[0];
    private final String mKeyboardName;

    public AnyPopupKeyboard(@NonNull AddOn keyboardAddOn, Context askContext, Context context,//note: the context can be from a different package!
                            int xmlLayoutResId,
                            final KeyboardDimens keyboardDimens,
                            String keyboardName) {
        super(keyboardAddOn, askContext, context, xmlLayoutResId, -1);
        mKeyboardName = keyboardName;
        loadKeyboard(keyboardDimens);
    }

    public AnyPopupKeyboard(@NonNull AddOn keyboardAddOn, @NonNull  Context askContext, CharSequence popupCharacters,
                            final KeyboardDimens keyboardDimens,
                            String keyboardName) {
        super(keyboardAddOn, askContext, askContext, getPopupLayout(popupCharacters));
        mKeyboardName = keyboardName;
        loadKeyboard(keyboardDimens);

        final int rowsCount = getPopupRowsCount(popupCharacters);
        final int keysPerRow = (int)Math.ceil((float)popupCharacters.length()/(float)rowsCount);

        List<Key> keys = getKeys();
        for(int rowIndex = rowsCount-1; rowIndex>=0; rowIndex--) {
            int baseKeyIndex = keys.size()-rowIndex-1;
            addPopupKeysToList(baseKeyIndex, keyboardDimens, keys, popupCharacters, rowIndex*keysPerRow, keysPerRow);
        }
    }

    private void addPopupKeysToList(int baseKeyIndex, KeyboardDimens keyboardDimens, List<Key> keys, CharSequence popupCharacters, int characterOffset, int keysPerRow) {
        int rowWidth = 0;
        AnyKey baseKey = (AnyKey)keys.get(baseKeyIndex);
        Row row = baseKey.row;
        //now adding the popups
        final float y = baseKey.y;
        final float keyHorizontalGap = row.defaultHorizontalGap;
        char popupCharacter = popupCharacters.charAt(characterOffset);
        baseKey.codes = new int[]{(int) popupCharacter};
        baseKey.label = Character.toString(popupCharacter);
        char upperCasePopupCharacter = Character.toUpperCase(popupCharacter);
        baseKey.shiftedCodes = new int[]{(int) upperCasePopupCharacter};
        baseKey.shiftedKeyLabel = Character.toString(upperCasePopupCharacter);
        float x = baseKey.width;
        AnyKey aKey = null;
        for (int popupCharIndex = characterOffset+1;
             popupCharIndex < characterOffset+keysPerRow && popupCharIndex < popupCharacters.length();
             popupCharIndex++) {
            x += (keyHorizontalGap / 2);

            aKey = new AnyKey(row, keyboardDimens);
            popupCharacter = popupCharacters.charAt(popupCharIndex);
            aKey.codes = new int[]{(int) popupCharacter};
            aKey.label = Character.toString(popupCharacter);
            upperCasePopupCharacter = Character.toUpperCase(popupCharacter);
            aKey.shiftedCodes = new int[]{(int) upperCasePopupCharacter};
            aKey.shiftedKeyLabel = Character.toString(upperCasePopupCharacter);
            aKey.x = (int) x;
            aKey.width -= keyHorizontalGap;//the gap is on both sides
            aKey.y = (int) y;
            final int xOffset = (int) (aKey.width + keyHorizontalGap + (keyHorizontalGap / 2));
            x += xOffset;
            rowWidth += xOffset;
            keys.add(baseKeyIndex, aKey);
        }
        //adding edge flag to the last key
        baseKey.edgeFlags = EDGE_LEFT;
        //this holds the last key
        if (aKey != null)
            aKey.edgeFlags = EDGE_RIGHT;
        else
            baseKey.edgeFlags |= EDGE_RIGHT;//adding another flag, since the baseKey is the only one in the row

        mAdditionalWidth = Math.max(rowWidth, mAdditionalWidth);
    }

    private static int getPopupLayout(CharSequence popupCharacters) {
        switch (getPopupRowsCount(popupCharacters)) {
            case 1:
                return R.xml.popup_one_row;
            case 2:
                return R.xml.popup_two_rows;
            case 3:
                return R.xml.popup_three_rows;
            default:
                throw new RuntimeException("AnyPopupKeyboard supports 1, 2, and 3 rows only!");
        }
    }

    private static int getPopupRowsCount(CharSequence popupCharacters) {
        final int count = popupCharacters.length();
        if (count <= 8)
            return 1;
        if (count <= 16)
            return 2;
        else
            return 3;
    }


    @Override
    public char[] getSentenceSeparators() {
        return EMPTY_CHAR_ARRAY;
    }

    @Override
    public int getMinWidth() {
        return super.getMinWidth() + mAdditionalWidth;
    }

    @Override
    public String getDefaultDictionaryLocale() {
        return null;
    }

    @Override
    public String getKeyboardName() {
        return mKeyboardName;
    }

    @Override
    public int getKeyboardIconResId() {
        return -1;
    }

    @Override
    public String getKeyboardPrefId() {
        return "keyboard_popup";
    }

    @Override
    protected void addGenericRows(int mode, KeyboardDimens keyboardDimens) {
        //no generic rows in popups, only in main keyboard
    }

    @Override
    protected boolean keyboardSupportShift() {
        return true;
    }

    public void mirrorKeys() {
        /* how to mirror?
        width = 55
        [0..15] [20..35] [40..55]
        phase 1: multiple by -1
        [0] [-20] [-40]
        phase 2: add keyboard width
        [55] [35] [15]
        phase 3: subtracting the key's width
        [40] [20] [0]
        cool?
         */
        final int keyboardWidth = getMinWidth();
        for(Key k : getKeys()) {
            k.x = k.x*(-1);//phase 1
            k.x += keyboardWidth;//phase 2
            k.x -= k.width;//phase 3
        }
    }
}