Skip to content

Commit 58e7b83

Browse files
committed
refactor: try to meet spotless styling
- remove util/LocalUtils
1 parent 61f278a commit 58e7b83

File tree

11 files changed

+278
-261
lines changed

11 files changed

+278
-261
lines changed

app/src/main/java/com/osfans/trime/ime/core/Speech.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
/** {@link RecognitionListener 語音輸入} */
3939
public class Speech implements RecognitionListener {
40-
private final @NonNull Context context;
41-
private final @Nullable SpeechRecognizer speechRecognizer;
40+
private final Context context;
41+
private final SpeechRecognizer speechRecognizer;
4242
private final Intent recognizerIntent;
4343

4444
public Speech(@NonNull Context context) {

app/src/main/java/com/osfans/trime/ime/core/Trime.java

+156-132
Large diffs are not rendered by default.

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ private void computeProximityThreshold(Keyboard keyboard) {
666666
public void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
667667
super.onSizeChanged(w, h, oldw, oldh);
668668
// if (mKeyboard != null) {
669-
// mKeyboard.resize(w, h);
669+
// mKeyboard.resize(w, h);
670670
// }
671671
// Release the buffer, if any and it will be reallocated on the next draw
672672
mBuffer = null;
@@ -847,7 +847,7 @@ private int getKeyIndices(final int x, final int y, final int[] allKeys) {
847847
}
848848

849849
if ((mProximityCorrectOn && (dist = key.squaredDistanceFrom(x, y)) < mProximityThreshold)
850-
|| isInside) {
850+
|| isInside) {
851851
// Find insertion point
852852
final int nCodes = 1;
853853
if (dist < closestKeyDist) {
@@ -1125,8 +1125,7 @@ private boolean onLongPress(@NonNull final Key popupKey) {
11251125
final LayoutInflater inflater =
11261126
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
11271127
mMiniKeyboardContainer = inflater.inflate(mPopupLayout, null);
1128-
mMiniKeyboard =
1129-
mMiniKeyboardContainer.findViewById(android.R.id.keyboardView);
1128+
mMiniKeyboard = mMiniKeyboardContainer.findViewById(android.R.id.keyboardView);
11301129
final View closeButton = mMiniKeyboardContainer.findViewById(android.R.id.closeButton);
11311130
if (closeButton != null) closeButton.setOnClickListener(this);
11321131
mMiniKeyboard.setOnKeyboardActionListener(
@@ -1179,8 +1178,7 @@ public void onRelease(final int primaryCode) {
11791178

11801179
mMiniKeyboardCache.put(popupKey, mMiniKeyboardContainer);
11811180
} else {
1182-
mMiniKeyboard =
1183-
mMiniKeyboardContainer.findViewById(android.R.id.keyboardView);
1181+
mMiniKeyboard = mMiniKeyboardContainer.findViewById(android.R.id.keyboardView);
11841182
}
11851183
getLocationInWindow(mCoordinates);
11861184
int mPopupX = popupKey.getX() + getPaddingLeft();

app/src/main/java/com/osfans/trime/ime/text/Candidate.java

+93-89
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,28 @@
3737
public class Candidate extends View {
3838

3939
/** 處理候選條選字事件 */
40-
public interface CandidateListener {
41-
void onPickCandidate(int index);
40+
public interface EventListener {
41+
void onCandidatePressed(int index);
4242
}
4343

4444
private static final int MAX_CANDIDATE_COUNT = 30;
4545
private static final int CANDIDATE_TOUCH_OFFSET = -12;
4646

47-
private CandidateListener listener;
47+
private EventListener listener;
4848
private int highlightIndex;
4949
private Rime.RimeCandidate[] candidates;
5050
private int num_candidates;
5151
private int start_num = 0;
5252

5353
private Drawable candidateHighlight, candidateSeparator;
54-
private final Paint paintCandidate;
55-
private final Paint paintSymbol;
56-
private final Paint paintComment;
57-
private Typeface tfCandidate, tfSymbol, tfComment, tfHanB, tfLatin;
58-
private int candidate_text_color, hilited_candidate_text_color;
59-
private int comment_text_color, hilited_comment_text_color;
60-
private int candidate_view_height, comment_height, candidate_spacing, candidate_padding;
61-
private boolean show_comment = true, comment_on_top, candidate_use_cursor;
54+
private final Paint candidatePaint;
55+
private final Paint symbolPaint;
56+
private final Paint commentPaint;
57+
private Typeface candidateTypeface, symbolTypeface, commentTypeface, hanBTypeface, latinTypeface;
58+
private int candidateTextColor, hilitedCandidateTextColor;
59+
private int commentTextColor, hilitedCommentTextColor;
60+
private int candidateViewHeight, commentHeight, candidateSpacing, candidatePadding;
61+
private boolean shouldShowComment = true, isCommentOnTop, candidateUseCursor;
6262

6363
private final Rect[] candidateRect = new Rect[MAX_CANDIDATE_COUNT + 2];
6464

@@ -67,52 +67,52 @@ public void reset(Context context) {
6767
candidateHighlight = new PaintDrawable(config.getColor("hilited_candidate_back_color"));
6868
((PaintDrawable) candidateHighlight).setCornerRadius(config.getFloat("layout/round_corner"));
6969
candidateSeparator = new PaintDrawable(config.getColor("candidate_separator_color"));
70-
candidate_spacing = config.getPixel("candidate_spacing");
71-
candidate_padding = config.getPixel("candidate_padding");
70+
candidateSpacing = config.getPixel("candidate_spacing");
71+
candidatePadding = config.getPixel("candidate_padding");
7272

73-
candidate_text_color = config.getColor("candidate_text_color");
74-
comment_text_color = config.getColor("comment_text_color");
75-
hilited_candidate_text_color = config.getColor("hilited_candidate_text_color");
76-
hilited_comment_text_color = config.getColor("hilited_comment_text_color");
73+
candidateTextColor = config.getColor("candidate_text_color");
74+
commentTextColor = config.getColor("comment_text_color");
75+
hilitedCandidateTextColor = config.getColor("hilited_candidate_text_color");
76+
hilitedCommentTextColor = config.getColor("hilited_comment_text_color");
7777

7878
int candidate_text_size = config.getPixel("candidate_text_size");
7979
int comment_text_size = config.getPixel("comment_text_size");
80-
candidate_view_height = config.getPixel("candidate_view_height");
81-
comment_height = config.getPixel("comment_height");
82-
83-
tfCandidate = config.getFont("candidate_font");
84-
tfLatin = config.getFont("latin_font");
85-
tfHanB = config.getFont("hanb_font");
86-
tfComment = config.getFont("comment_font");
87-
tfSymbol = config.getFont("symbol_font");
88-
89-
paintCandidate.setTextSize(candidate_text_size);
90-
paintCandidate.setTypeface(tfCandidate);
91-
paintSymbol.setTextSize(candidate_text_size);
92-
paintSymbol.setTypeface(tfSymbol);
93-
paintComment.setTextSize(comment_text_size);
94-
paintComment.setTypeface(tfComment);
95-
96-
comment_on_top = config.getBoolean("comment_on_top");
97-
candidate_use_cursor = config.getBoolean("candidate_use_cursor");
80+
candidateViewHeight = config.getPixel("candidate_view_height");
81+
commentHeight = config.getPixel("comment_height");
82+
83+
candidateTypeface = config.getFont("candidate_font");
84+
latinTypeface = config.getFont("latin_font");
85+
hanBTypeface = config.getFont("hanb_font");
86+
commentTypeface = config.getFont("comment_font");
87+
symbolTypeface = config.getFont("symbol_font");
88+
89+
candidatePaint.setTextSize(candidate_text_size);
90+
candidatePaint.setTypeface(candidateTypeface);
91+
symbolPaint.setTextSize(candidate_text_size);
92+
symbolPaint.setTypeface(symbolTypeface);
93+
commentPaint.setTextSize(comment_text_size);
94+
commentPaint.setTypeface(commentTypeface);
95+
96+
isCommentOnTop = config.getBoolean("comment_on_top");
97+
candidateUseCursor = config.getBoolean("candidate_use_cursor");
9898
invalidate();
9999
}
100100

101101
public void setShowComment(boolean value) {
102-
show_comment = value;
102+
shouldShowComment = value;
103103
}
104104

105105
public Candidate(Context context, AttributeSet attrs) {
106106
super(context, attrs);
107-
paintCandidate = new Paint();
108-
paintCandidate.setAntiAlias(true);
109-
paintCandidate.setStrokeWidth(0);
110-
paintSymbol = new Paint();
111-
paintSymbol.setAntiAlias(true);
112-
paintSymbol.setStrokeWidth(0);
113-
paintComment = new Paint();
114-
paintComment.setAntiAlias(true);
115-
paintComment.setStrokeWidth(0);
107+
candidatePaint = new Paint();
108+
candidatePaint.setAntiAlias(true);
109+
candidatePaint.setStrokeWidth(0);
110+
symbolPaint = new Paint();
111+
symbolPaint.setAntiAlias(true);
112+
symbolPaint.setStrokeWidth(0);
113+
commentPaint = new Paint();
114+
commentPaint.setAntiAlias(true);
115+
commentPaint.setStrokeWidth(0);
116116

117117
reset(context);
118118

@@ -123,7 +123,7 @@ public static int getMaxCandidateCount() {
123123
return MAX_CANDIDATE_COUNT;
124124
}
125125

126-
public void setCandidateListener(CandidateListener listener) {
126+
public void setCandidateListener(EventListener listener) {
127127
this.listener = listener;
128128
}
129129

@@ -152,7 +152,7 @@ private boolean pickHighlighted(int index) {
152152
if ((highlightIndex != -1) && (listener != null)) {
153153
if (index == -1) index = highlightIndex;
154154
if (index >= 0) index += start_num;
155-
listener.onPickCandidate(index);
155+
listener.onCandidatePressed(index);
156156
return true;
157157
}
158158
return false;
@@ -175,7 +175,7 @@ private void removeHighlight() {
175175
}
176176

177177
private boolean isHighlighted(int i) {
178-
return candidate_use_cursor && i >= 0 && i == highlightIndex;
178+
return candidateUseCursor && i >= 0 && i == highlightIndex;
179179
}
180180

181181
private void drawHighlight(Canvas canvas) {
@@ -186,8 +186,9 @@ private void drawHighlight(Canvas canvas) {
186186
}
187187

188188
private Typeface getFont(int codepoint, Typeface font) {
189-
if (tfHanB != Typeface.DEFAULT && Character.isSupplementaryCodePoint(codepoint)) return tfHanB;
190-
if (tfLatin != Typeface.DEFAULT && codepoint < 0x2e80) return tfLatin;
189+
if (hanBTypeface != Typeface.DEFAULT && Character.isSupplementaryCodePoint(codepoint))
190+
return hanBTypeface;
191+
if (latinTypeface != Typeface.DEFAULT && codepoint < 0x2e80) return latinTypeface;
191192
return font;
192193
}
193194

@@ -198,7 +199,8 @@ private void drawText(
198199
if (length == 0) return;
199200
int points = s.codePointCount(0, length);
200201
float x = center - measureText(s, paint, font) / 2;
201-
if (tfLatin != Typeface.DEFAULT || (tfHanB != Typeface.DEFAULT && length > points)) {
202+
if (latinTypeface != Typeface.DEFAULT
203+
|| (hanBTypeface != Typeface.DEFAULT && length > points)) {
202204
int offset = 0;
203205
while (offset < length) {
204206
int codepoint = s.codePointAt(offset);
@@ -218,57 +220,58 @@ private void drawText(
218220
private void drawCandidates(Canvas canvas) {
219221
if (candidates == null) return;
220222

221-
float x;
222-
float y;
223-
int i = 0;
224-
float comment_x, comment_y;
225-
float comment_width;
226-
String candidate, comment;
223+
float candidateX;
224+
225+
float commentX, commentY;
226+
float commentWidth;
227+
String candidate;
227228

228-
y = candidateRect[0].centerY() - (paintCandidate.ascent() + paintCandidate.descent()) / 2;
229-
if (show_comment && comment_on_top) y += comment_height / 2f;
230-
comment_y = comment_height / 2f - (paintComment.ascent() + paintComment.descent()) / 2;
231-
if (show_comment && !comment_on_top) comment_y += candidateRect[0].bottom - comment_height;
229+
float candidateY =
230+
candidateRect[0].centerY() - (candidatePaint.ascent() + candidatePaint.descent()) / 2;
231+
if (shouldShowComment && isCommentOnTop) candidateY += commentHeight / 2f;
232+
commentY = commentHeight / 2f - (commentPaint.ascent() + commentPaint.descent()) / 2;
233+
if (shouldShowComment && !isCommentOnTop) commentY += candidateRect[0].bottom - commentHeight;
232234

235+
int i = 0;
233236
while (i < num_candidates) {
234237
// Calculate a position where the text could be centered in the rectangle.
235-
x = candidateRect[i].centerX();
236-
if (show_comment) {
237-
comment = getComment(i);
238+
candidateX = candidateRect[i].centerX();
239+
if (shouldShowComment) {
240+
final String comment = getComment(i);
238241
if (!TextUtils.isEmpty(comment)) {
239-
comment_width = measureText(comment, paintComment, tfComment);
240-
if (comment_on_top) {
241-
comment_x = candidateRect[i].centerX();
242+
commentWidth = measureText(comment, commentPaint, commentTypeface);
243+
if (isCommentOnTop) {
244+
commentX = candidateRect[i].centerX();
242245
} else {
243-
x -= comment_width / 2;
244-
comment_x = candidateRect[i].right - comment_width / 2;
246+
candidateX -= commentWidth / 2;
247+
commentX = candidateRect[i].right - commentWidth / 2;
245248
}
246-
paintComment.setColor(isHighlighted(i) ? hilited_comment_text_color : comment_text_color);
247-
drawText(comment, canvas, paintComment, tfComment, comment_x, comment_y);
249+
commentPaint.setColor(isHighlighted(i) ? hilitedCommentTextColor : commentTextColor);
250+
drawText(comment, canvas, commentPaint, commentTypeface, commentX, commentY);
248251
}
249252
}
250-
paintCandidate.setColor(
251-
isHighlighted(i) ? hilited_candidate_text_color : candidate_text_color);
252-
drawText(getCandidate(i), canvas, paintCandidate, tfCandidate, x, y);
253+
candidatePaint.setColor(isHighlighted(i) ? hilitedCandidateTextColor : candidateTextColor);
254+
drawText(getCandidate(i), canvas, candidatePaint, candidateTypeface, candidateX, candidateY);
253255
// Draw the separator at the right edge of each candidate.
254256
candidateSeparator.setBounds(
255257
candidateRect[i].right - candidateSeparator.getIntrinsicWidth(),
256258
candidateRect[i].top,
257-
candidateRect[i].right + candidate_spacing,
259+
candidateRect[i].right + candidateSpacing,
258260
candidateRect[i].bottom);
259261
candidateSeparator.draw(canvas);
260262
i++;
261263
}
262264
for (int j = -4; j >= -5; j--) { // -4: left, -5: right
263265
candidate = getCandidate(j);
264266
if (candidate == null) continue;
265-
paintSymbol.setColor(isHighlighted(i) ? hilited_comment_text_color : comment_text_color);
266-
x = candidateRect[i].centerX() - measureText(candidate, paintSymbol, tfSymbol) / 2;
267-
canvas.drawText(candidate, x, y, paintSymbol);
267+
symbolPaint.setColor(isHighlighted(i) ? hilitedCommentTextColor : commentTextColor);
268+
candidateX =
269+
candidateRect[i].centerX() - measureText(candidate, symbolPaint, symbolTypeface) / 2;
270+
canvas.drawText(candidate, candidateX, candidateY, symbolPaint);
268271
candidateSeparator.setBounds(
269272
candidateRect[i].right - candidateSeparator.getIntrinsicWidth(),
270273
candidateRect[i].top,
271-
candidateRect[i].right + candidate_spacing,
274+
candidateRect[i].right + candidateSpacing,
272275
candidateRect[i].bottom);
273276
candidateSeparator.draw(canvas);
274277
i++;
@@ -291,18 +294,18 @@ private void updateCandidateWidth() {
291294
final int bottom = getHeight();
292295
int i;
293296
int x = 0;
294-
if (Rime.hasLeft()) x += getCandidateWidth(-4) + candidate_spacing;
297+
if (Rime.hasLeft()) x += getCandidateWidth(-4) + candidateSpacing;
295298
getCandNum();
296299
for (i = 0; i < num_candidates; i++) {
297300
candidateRect[i] = new Rect(x, top, x += getCandidateWidth(i), bottom);
298-
x += candidate_spacing;
301+
x += candidateSpacing;
299302
}
300303
if (Rime.hasLeft()) candidateRect[i++] = new Rect(0, top, (int) getCandidateWidth(-4), bottom);
301304
if (Rime.hasRight()) candidateRect[i++] = new Rect(x, top, x += getCandidateWidth(-5), bottom);
302305
LayoutParams params = getLayoutParams();
303306
params.width = x;
304-
params.height = candidate_view_height;
305-
if (show_comment && comment_on_top) params.height += comment_height;
307+
params.height = candidateViewHeight;
308+
if (shouldShowComment && isCommentOnTop) params.height += commentHeight;
306309
setLayoutParams(params);
307310
}
308311

@@ -406,7 +409,8 @@ private float measureText(String s, Paint paint, Typeface font) {
406409
int length = s.length();
407410
if (length == 0) return x;
408411
int points = s.codePointCount(0, length);
409-
if (tfLatin != Typeface.DEFAULT || (tfHanB != Typeface.DEFAULT && length > points)) {
412+
if (latinTypeface != Typeface.DEFAULT
413+
|| (hanBTypeface != Typeface.DEFAULT && length > points)) {
410414
int offset = 0;
411415
while (offset < length) {
412416
int codepoint = s.codePointAt(offset);
@@ -427,13 +431,13 @@ private float measureText(String s, Paint paint, Typeface font) {
427431
private float getCandidateWidth(int i) {
428432
String s = getCandidate(i);
429433
// float n = (s == null ? 0 : s.codePointCount(0, s.length()));
430-
float x = 2 * candidate_padding;
431-
if (s != null) x += measureText(s, paintCandidate, tfCandidate);
432-
if (i >= 0 && show_comment) {
434+
float x = 2 * candidatePadding;
435+
if (s != null) x += measureText(s, candidatePaint, candidateTypeface);
436+
if (i >= 0 && shouldShowComment) {
433437
String comment = getComment(i);
434438
if (comment != null) {
435-
float x2 = measureText(comment, paintComment, tfComment);
436-
if (comment_on_top) {
439+
float x2 = measureText(comment, commentPaint, commentTypeface);
440+
if (isCommentOnTop) {
437441
if (x2 > x) x = x2;
438442
} // 提示在上方
439443
else x += x2; // 提示在右方

app/src/main/java/com/osfans/trime/ime/text/Composition.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public CandidateSpan(int i, Typeface _tf, int _hi_text, int _hi_back, int _text)
107107

108108
@Override
109109
public void onClick(View tv) {
110-
Trime.getService().onPickCandidate(index);
110+
Trime.getService().onCandidatePressed(index);
111111
}
112112

113113
@Override
@@ -186,7 +186,7 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
186186
if (action == MotionEvent.ACTION_DOWN) {
187187
if (first_move || movable.contentEquals("once")) {
188188
first_move = false;
189-
int[] location = ViewUtils.INSTANCE.getLocationOnScreen(this);
189+
int[] location = ViewUtils.getLocationOnScreen(this);
190190
mCurrentX = location[0];
191191
mCurrentY = location[1];
192192
}

0 commit comments

Comments
 (0)