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.content.Context;
20import android.content.res.Configuration;
21import android.view.Display;
22import android.view.WindowManager;
23
24/**
25 * Global environment configurations for showing soft keyboard and candidate
26 * view. All original dimension values are defined in float, and the real size
27 * is calculated from the float values of and screen size. In this way, this
28 * input method can work even when screen size is changed.
29 */
30public class Environment {
31    /**
32     * The key height for portrait mode. It is relative to the screen height.
33     */
34    private static final float KEY_HEIGHT_RATIO_PORTRAIT = 0.105f;
35
36    /**
37     * The key height for landscape mode. It is relative to the screen height.
38     */
39    private static final float KEY_HEIGHT_RATIO_LANDSCAPE = 0.147f;
40
41    /**
42     * The height of the candidates area for portrait mode. It is relative to
43     * screen height.
44     */
45    private static final float CANDIDATES_AREA_HEIGHT_RATIO_PORTRAIT = 0.084f;
46
47    /**
48     * The height of the candidates area for portrait mode. It is relative to
49     * screen height.
50     */
51    private static final float CANDIDATES_AREA_HEIGHT_RATIO_LANDSCAPE = 0.125f;
52
53    /**
54     * How much should the balloon width be larger than width of the real key.
55     * It is relative to the smaller one of screen width and height.
56     */
57    private static final float KEY_BALLOON_WIDTH_PLUS_RATIO = 0.08f;
58
59    /**
60     * How much should the balloon height be larger than that of the real key.
61     * It is relative to the smaller one of screen width and height.
62     */
63    private static final float KEY_BALLOON_HEIGHT_PLUS_RATIO = 0.07f;
64
65    /**
66     * The text size for normal keys. It is relative to the smaller one of
67     * screen width and height.
68     */
69    private static final float NORMAL_KEY_TEXT_SIZE_RATIO = 0.075f;
70
71    /**
72     * The text size for function keys. It is relative to the smaller one of
73     * screen width and height.
74     */
75    private static final float FUNCTION_KEY_TEXT_SIZE_RATIO = 0.055f;
76
77    /**
78     * The text size balloons of normal keys. It is relative to the smaller one
79     * of screen width and height.
80     */
81    private static final float NORMAL_BALLOON_TEXT_SIZE_RATIO = 0.14f;
82
83    /**
84     * The text size balloons of function keys. It is relative to the smaller
85     * one of screen width and height.
86     */
87    private static final float FUNCTION_BALLOON_TEXT_SIZE_RATIO = 0.085f;
88
89    /**
90     * The configurations are managed in a singleton.
91     */
92    private static Environment mInstance;
93
94    private int mScreenWidth;
95    private int mScreenHeight;
96    private int mKeyHeight;
97    private int mCandidatesAreaHeight;
98    private int mKeyBalloonWidthPlus;
99    private int mKeyBalloonHeightPlus;
100    private int mNormalKeyTextSize;
101    private int mFunctionKeyTextSize;
102    private int mNormalBalloonTextSize;
103    private int mFunctionBalloonTextSize;
104    private Configuration mConfig = new Configuration();
105    private boolean mDebug = false;
106
107    private Environment() {
108    }
109
110    public static Environment getInstance() {
111        if (null == mInstance) {
112            mInstance = new Environment();
113        }
114        return mInstance;
115    }
116
117    public void onConfigurationChanged(Configuration newConfig, Context context) {
118        if (mConfig.orientation != newConfig.orientation) {
119            WindowManager wm = (WindowManager) context
120                    .getSystemService(Context.WINDOW_SERVICE);
121            Display d = wm.getDefaultDisplay();
122            mScreenWidth = d.getWidth();
123            mScreenHeight = d.getHeight();
124
125            int scale;
126            if (mScreenHeight > mScreenWidth) {
127                mKeyHeight = (int) (mScreenHeight * KEY_HEIGHT_RATIO_PORTRAIT);
128                mCandidatesAreaHeight = (int) (mScreenHeight * CANDIDATES_AREA_HEIGHT_RATIO_PORTRAIT);
129                scale = mScreenWidth;
130            } else {
131                mKeyHeight = (int) (mScreenHeight * KEY_HEIGHT_RATIO_LANDSCAPE);
132                mCandidatesAreaHeight = (int) (mScreenHeight * CANDIDATES_AREA_HEIGHT_RATIO_LANDSCAPE);
133                scale = mScreenHeight;
134            }
135            mNormalKeyTextSize = (int) (scale * NORMAL_KEY_TEXT_SIZE_RATIO);
136            mFunctionKeyTextSize = (int) (scale * FUNCTION_KEY_TEXT_SIZE_RATIO);
137            mNormalBalloonTextSize = (int) (scale * NORMAL_BALLOON_TEXT_SIZE_RATIO);
138            mFunctionBalloonTextSize = (int) (scale * FUNCTION_BALLOON_TEXT_SIZE_RATIO);
139            mKeyBalloonWidthPlus = (int) (scale * KEY_BALLOON_WIDTH_PLUS_RATIO);
140            mKeyBalloonHeightPlus = (int) (scale * KEY_BALLOON_HEIGHT_PLUS_RATIO);
141        }
142
143        mConfig.updateFrom(newConfig);
144    }
145
146    public Configuration getConfiguration() {
147        return mConfig;
148    }
149
150    public int getScreenWidth() {
151        return mScreenWidth;
152    }
153
154    public int getScreenHeight() {
155        return mScreenHeight;
156    }
157
158    public int getHeightForCandidates() {
159        return mCandidatesAreaHeight;
160    }
161
162    public float getKeyXMarginFactor() {
163        return 1.0f;
164    }
165
166    public float getKeyYMarginFactor() {
167        if (Configuration.ORIENTATION_LANDSCAPE == mConfig.orientation) {
168            return 0.7f;
169        }
170        return 1.0f;
171    }
172
173    public int getKeyHeight() {
174        return mKeyHeight;
175    }
176
177    public int getKeyBalloonWidthPlus() {
178        return mKeyBalloonWidthPlus;
179    }
180
181    public int getKeyBalloonHeightPlus() {
182        return mKeyBalloonHeightPlus;
183    }
184
185    public int getSkbHeight() {
186        if (Configuration.ORIENTATION_PORTRAIT == mConfig.orientation) {
187            return mKeyHeight * 4;
188        } else if (Configuration.ORIENTATION_LANDSCAPE == mConfig.orientation) {
189            return mKeyHeight * 4;
190        }
191        return 0;
192    }
193
194    public int getKeyTextSize(boolean isFunctionKey) {
195        if (isFunctionKey) {
196            return mFunctionKeyTextSize;
197        } else {
198            return mNormalKeyTextSize;
199        }
200    }
201
202    public int getBalloonTextSize(boolean isFunctionKey) {
203        if (isFunctionKey) {
204            return mFunctionBalloonTextSize;
205        } else {
206            return mNormalBalloonTextSize;
207        }
208    }
209
210    public boolean hasHardKeyboard() {
211        if (mConfig.keyboard == Configuration.KEYBOARD_NOKEYS
212                || mConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
213            return false;
214        }
215        return true;
216    }
217
218    public boolean needDebug() {
219        return mDebug;
220    }
221}
222