1/*
2 * Copyright (C) 2009 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.pinyin;
18
19import android.graphics.drawable.Drawable;
20
21import java.util.Vector;
22
23/**
24 * Key icon definition. It is defined in soft keyboard template. A soft keyboard
25 * can refer to such an icon in its xml file directly to improve performance.
26 */
27class KeyIconRecord {
28    int keyCode;
29    Drawable icon;
30    Drawable iconPopup;
31}
32
33
34/**
35 * Default definition for a certain key. It is defined in soft keyboard
36 * template. A soft keyboard can refer to a default key in its xml file. Nothing
37 * of the key can be overwritten, including the size.
38 */
39class KeyRecord {
40    int keyId;
41    SoftKey softKey;
42}
43
44
45/**
46 * Soft keyboard template used by soft keyboards to share common resources. In
47 * this way, memory cost is reduced.
48 */
49public class SkbTemplate {
50    private int mSkbTemplateId;
51    private Drawable mSkbBg;
52    private Drawable mBalloonBg;
53    private Drawable mPopupBg;
54    private float mXMargin = 0;
55    private float mYMargin = 0;
56    /** Key type list. */
57    private Vector<SoftKeyType> mKeyTypeList = new Vector<SoftKeyType>();
58
59    /**
60     * Default key icon list. It is only for keys which do not have popup icons.
61     */
62    private Vector<KeyIconRecord> mKeyIconRecords = new Vector<KeyIconRecord>();
63
64    /**
65     * Default key list.
66     */
67    private Vector<KeyRecord> mKeyRecords = new Vector<KeyRecord>();
68
69    public SkbTemplate(int skbTemplateId) {
70        mSkbTemplateId = skbTemplateId;
71    }
72
73    public int getSkbTemplateId() {
74        return mSkbTemplateId;
75    }
76
77    public void setBackgrounds(Drawable skbBg, Drawable balloonBg,
78            Drawable popupBg) {
79        mSkbBg = skbBg;
80        mBalloonBg = balloonBg;
81        mPopupBg = popupBg;
82    }
83
84    public Drawable getSkbBackground() {
85        return mSkbBg;
86    }
87
88    public Drawable getBalloonBackground() {
89        return mBalloonBg;
90    }
91
92    public Drawable getPopupBackground() {
93        return mPopupBg;
94    }
95
96    public void setMargins(float xMargin, float yMargin) {
97        mXMargin = xMargin;
98        mYMargin = yMargin;
99    }
100
101    public float getXMargin() {
102        return mXMargin;
103    }
104
105    public float getYMargin() {
106        return mYMargin;
107    }
108
109    public SoftKeyType createKeyType(int id, Drawable bg, Drawable hlBg) {
110        return new SoftKeyType(id, bg, hlBg);
111    }
112
113    public boolean addKeyType(SoftKeyType keyType) {
114        // The newly added item should have the right id.
115        if (mKeyTypeList.size() != keyType.mKeyTypeId) return false;
116        mKeyTypeList.add(keyType);
117        return true;
118    }
119
120    public SoftKeyType getKeyType(int typeId) {
121        if (typeId < 0 || typeId > mKeyTypeList.size()) return null;
122        return mKeyTypeList.elementAt(typeId);
123    }
124
125    public void addDefaultKeyIcons(int keyCode, Drawable icon,
126            Drawable iconPopup) {
127        if (null == icon || null == iconPopup) return;
128
129        KeyIconRecord iconRecord = new KeyIconRecord();
130        iconRecord.icon = icon;
131        iconRecord.iconPopup = iconPopup;
132        iconRecord.keyCode = keyCode;
133
134        int size = mKeyIconRecords.size();
135        int pos = 0;
136        while (pos < size) {
137            if (mKeyIconRecords.get(pos).keyCode >= keyCode) break;
138            pos++;
139        }
140        mKeyIconRecords.add(pos, iconRecord);
141    }
142
143    public Drawable getDefaultKeyIcon(int keyCode) {
144        int size = mKeyIconRecords.size();
145        int pos = 0;
146        while (pos < size) {
147            KeyIconRecord iconRecord = mKeyIconRecords.get(pos);
148            if (iconRecord.keyCode < keyCode) {
149                pos++;
150                continue;
151            }
152            if (iconRecord.keyCode == keyCode) {
153                return iconRecord.icon;
154            }
155            return null;
156        }
157        return null;
158    }
159
160    public Drawable getDefaultKeyIconPopup(int keyCode) {
161        int size = mKeyIconRecords.size();
162        int pos = 0;
163        while (pos < size) {
164            KeyIconRecord iconRecord = mKeyIconRecords.get(pos);
165            if (iconRecord.keyCode < keyCode) {
166                pos++;
167                continue;
168            }
169            if (iconRecord.keyCode == keyCode) {
170                return iconRecord.iconPopup;
171            }
172            return null;
173        }
174        return null;
175    }
176
177    public void addDefaultKey(int keyId, SoftKey softKey) {
178        if (null == softKey) return;
179
180        KeyRecord keyRecord = new KeyRecord();
181        keyRecord.keyId = keyId;
182        keyRecord.softKey = softKey;
183
184        int size = mKeyRecords.size();
185        int pos = 0;
186        while (pos < size) {
187            if (mKeyRecords.get(pos).keyId >= keyId) break;
188            pos++;
189        }
190        mKeyRecords.add(pos, keyRecord);
191    }
192
193    public SoftKey getDefaultKey(int keyId) {
194        int size = mKeyRecords.size();
195        int pos = 0;
196        while (pos < size) {
197            KeyRecord keyRecord = mKeyRecords.get(pos);
198            if (keyRecord.keyId < keyId) {
199                pos++;
200                continue;
201            }
202            if (keyRecord.keyId == keyId) {
203                return keyRecord.softKey;
204            }
205            return null;
206        }
207        return null;
208    }
209}
210
211
212class SoftKeyType {
213    public static final int KEYTYPE_ID_NORMAL_KEY = 0;
214
215    public int mKeyTypeId;
216    public Drawable mKeyBg;
217    public Drawable mKeyHlBg;
218    public int mColor;
219    public int mColorHl;
220    public int mColorBalloon;
221
222    SoftKeyType(int id, Drawable bg, Drawable hlBg) {
223        mKeyTypeId = id;
224        mKeyBg = bg;
225        mKeyHlBg = hlBg;
226    }
227
228    public void setColors(int color, int colorHl, int colorBalloon) {
229        mColor = color;
230        mColorHl = colorHl;
231        mColorBalloon = colorBalloon;
232    }
233}
234