Skip to content

Commit 520e991

Browse files
committed
refactor(keyboard): remove the Context parameter
1 parent 4c095a0 commit 520e991

File tree

4 files changed

+17
-45
lines changed

4 files changed

+17
-45
lines changed

app/src/main/java/com/osfans/trime/ime/keyboard/Key.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import static android.view.KeyEvent.isModifierKey;
2121

22-
import android.content.Context;
2322
import android.graphics.drawable.Drawable;
2423
import android.text.TextUtils;
2524
import android.view.KeyCharacterMap;
@@ -112,7 +111,7 @@ public Key(Keyboard parent) {
112111
* @param parent 按鍵所在的{@link Keyboard 鍵盤}
113112
* @param mk 從YAML中解析得到的Map
114113
*/
115-
public Key(Context context, Keyboard parent, Map<String, Object> mk) {
114+
public Key(Keyboard parent, Map<String, Object> mk) {
116115
this(parent);
117116
String s;
118117
Config config = Config.get();

app/src/main/java/com/osfans/trime/ime/keyboard/Keyboard.java

+11-28
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818

1919
package com.osfans.trime.ime.keyboard;
2020

21-
import android.content.Context;
22-
import android.content.res.Configuration;
2321
import android.graphics.drawable.Drawable;
24-
import android.util.DisplayMetrics;
2522
import android.view.KeyEvent;
26-
import androidx.annotation.NonNull;
23+
import com.blankj.utilcode.util.ScreenUtils;
2724
import com.osfans.trime.data.theme.Config;
2825
import com.osfans.trime.util.ConfigGetter;
2926
import com.osfans.trime.util.DimensionsKt;
@@ -96,24 +93,14 @@ public class Keyboard {
9693
// 特别的,当值为负数时,为倒序序号(-1即倒数第一个);当值大于按键行数时,为最后一行
9794
private int autoHeightIndex, keyboardHeight;
9895

99-
/**
100-
* Creates a keyboard from the given xml key layout file.
101-
*
102-
* @param context the application or service context
103-
*/
104-
public Keyboard(@NonNull Context context) {
96+
/** Creates a keyboard from the given xml key layout file. */
97+
public Keyboard() {
10598

10699
// 橫屏模式下,键盘左右两侧到屏幕边缘的距离
107-
final boolean land =
108-
(context.getResources().getConfiguration().orientation
109-
== Configuration.ORIENTATION_LANDSCAPE);
110100

111101
final Config config = Config.get();
112-
113-
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
114-
mDisplayWidth = dm.widthPixels;
115102
int[] keyboardPadding = config.getKeyboardPadding();
116-
mDisplayWidth = mDisplayWidth - keyboardPadding[0] - keyboardPadding[1];
103+
mDisplayWidth = ScreenUtils.getScreenWidth() - keyboardPadding[0] - keyboardPadding[1];
117104
/* Height of the screen */
118105
// final int mDisplayHeight = dm.heightPixels;
119106
// Log.v(TAG, "keyboard's display metrics:" + dm);
@@ -130,7 +117,7 @@ public Keyboard(@NonNull Context context) {
130117
mBackground = config.getColorDrawable("keyboard_back_color");
131118

132119
keyboardHeight = (int) DimensionsKt.dp2px(config.style.getFloat("keyboard_height"));
133-
if (land) {
120+
if (ScreenUtils.isLandscape()) {
134121
int keyBoardHeightLand =
135122
(int) DimensionsKt.dp2px(config.style.getFloat("keyboard_height_land"));
136123
if (keyBoardHeightLand > 0) keyboardHeight = keyBoardHeightLand;
@@ -150,16 +137,15 @@ public Keyboard(@NonNull Context context) {
150137
* <p>If the specified number of columns is -1, then the keyboard will fit as many keys as
151138
* possible in each row.
152139
*
153-
* @param context the application or service context
154140
* @param characters the list of characters to display on the keyboard. One key will be created
155141
* for each character.
156142
* @param columns the number of columns of keys to display. If this number is greater than the
157143
* number of keys that can fit in a row, it will be ignored. If this number is -1, the
158144
* keyboard will fit as many keys as possible in each row.
159145
* @param horizontalPadding 按鍵水平間距
160146
*/
161-
public Keyboard(Context context, CharSequence characters, int columns, int horizontalPadding) {
162-
this(context);
147+
public Keyboard(CharSequence characters, int columns, int horizontalPadding) {
148+
this();
163149
int x = 0;
164150
int y = 0;
165151
int column = 0;
@@ -190,11 +176,8 @@ public Keyboard(Context context, CharSequence characters, int columns, int horiz
190176
mTotalHeight = y + mDefaultHeight;
191177
}
192178

193-
public Keyboard(Context context, String name) {
194-
this(context);
195-
final boolean land =
196-
(context.getResources().getConfiguration().orientation
197-
== Configuration.ORIENTATION_LANDSCAPE);
179+
public Keyboard(String name) {
180+
this();
198181
Config config = Config.get();
199182
final Map<String, ?> keyboardConfig = config.getKeyboard(name);
200183
mLabelTransform = ConfigGetter.getString(keyboardConfig, "label_transform", "none");
@@ -241,7 +224,7 @@ public Keyboard(Context context, String name) {
241224

242225
if (keyboardHeight > 0) {
243226
int mkeyboardHeight = ConfigGetter.getPixel(keyboardConfig, "keyboard_height", 0);
244-
if (land) {
227+
if (ScreenUtils.isLandscape()) {
245228
int mkeyBoardHeightLand = ConfigGetter.getPixel(keyboardConfig, "keyboard_height_land", 0);
246229
if (mkeyBoardHeightLand > 0) mkeyboardHeight = mkeyBoardHeightLand;
247230
}
@@ -388,7 +371,7 @@ public Keyboard(Context context, String name) {
388371
ConfigGetter.getInt(
389372
keyboardConfig, "key_press_offset_y", config.style.getInt("key_press_offset_y"));
390373

391-
final Key key = new Key(context, this, mk);
374+
final Key key = new Key(this, mk);
392375
key.setKey_text_offset_x(
393376
ConfigGetter.getPixel(mk, "key_text_offset_x", defaultKeyTextOffsetX));
394377
key.setKey_text_offset_y(

app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardSwitcher.kt

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.osfans.trime.ime.keyboard
22

33
import android.content.res.Configuration
4+
import com.blankj.utilcode.util.ScreenUtils
45
import com.osfans.trime.data.AppPrefs
56
import com.osfans.trime.data.theme.Config
6-
import com.osfans.trime.ime.core.Trime
77
import com.osfans.trime.util.appContext
88
import timber.log.Timber
99

@@ -30,21 +30,15 @@ class KeyboardSwitcher {
3030
fun newOrReset() {
3131
val methodName = "\t<TrimeInit>\t" + Thread.currentThread().stackTrace[2].methodName + "\t"
3232
Timber.d(methodName)
33-
val ims = Trime.getService()
3433
Timber.d(methodName + "getConfig")
3534
keyboardNames = Config.get().keyboardNames
36-
Timber.d(methodName + "land")
37-
val land = (
38-
ims.resources.configuration.orientation
39-
== Configuration.ORIENTATION_LANDSCAPE
40-
)
4135
Timber.d(methodName + "getConfig")
42-
Config.get().getKeyboardPadding(land)
36+
Config.get().getKeyboardPadding(ScreenUtils.isLandscape())
4337
Timber.d("update KeyboardPadding: KeyboardSwitcher.init")
4438

4539
Timber.d(methodName + "getKeyboards")
4640
keyboards = Array(keyboardNames.size) { i ->
47-
Keyboard(ims, keyboardNames[i])
41+
Keyboard(keyboardNames[i])
4842
}
4943
Timber.d(methodName + "setKeyboard")
5044
setKeyboard(0)

app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardView.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1353,13 +1353,9 @@ public void onRelease(final int primaryCode) {
13531353
final Keyboard keyboard;
13541354
if (popupKey.getPopupCharacters() != null) {
13551355
keyboard =
1356-
new Keyboard(
1357-
getContext(),
1358-
popupKey.getPopupCharacters(),
1359-
-1,
1360-
getPaddingLeft() + getPaddingRight());
1356+
new Keyboard(popupKey.getPopupCharacters(), -1, getPaddingLeft() + getPaddingRight());
13611357
} else {
1362-
keyboard = new Keyboard(getContext());
1358+
keyboard = new Keyboard();
13631359
}
13641360
mMiniKeyboard.setKeyboard(keyboard);
13651361
mMiniKeyboard.setPopupParent(this);

0 commit comments

Comments
 (0)