Skip to content

Commit

Permalink
refactor: apply spotless to unify style
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Oct 23, 2021
1 parent 5200a7e commit 0de8f23
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.view.inputmethod.InputConnection
import com.osfans.trime.Rime
import com.osfans.trime.ime.enums.InlineModeType
import com.osfans.trime.ime.text.TextInputManager
import timber.log.Timber

class EditorInstance(private val ims: InputMethodService) {

Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/osfans/trime/ime/core/Speech.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.blankj.utilcode.util.ToastUtils;
import com.osfans.trime.R;
Expand Down Expand Up @@ -106,20 +105,22 @@ public void onResults(Bundle results) {
speechRecognizer.destroy();
}
Timber.i("onResults");
@Nullable Trime trime = Trime.getService();
if (trime == null) return;
final ArrayList<String> matches =
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
final String openccConfig = Config.get(context).getString("speech_opencc_config");
for (String result : matches) trime.commitText(Rime.openccConvert(result, openccConfig));
final Trime trime = Trime.getServiceOrNull();
if (trime != null) {
final ArrayList<String> matches =
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
final String openccConfig = Config.get(context).getString("speech_opencc_config");
for (String result : matches) trime.commitText(Rime.openccConvert(result, openccConfig));
}
}

@Override
public void onRmsChanged(float rmsdB) {
Timber.i("onRmsChanged: %s", rmsdB);
}

private static @StringRes int getErrorText(int errorCode) {
@StringRes
private static int getErrorText(int errorCode) {
final int message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
Expand Down
71 changes: 29 additions & 42 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ public static Trime getServiceOrNull() {
}

private static final Handler syncBackgroundHandler =
new Handler(
msg -> {
if (!((Trime) msg.obj).isShowInputRequested()) { // 若当前没有输入面板,则后台同步。防止面板关闭后5秒内再次打开
ShortcutUtils.INSTANCE.syncInBackground((Trime) msg.obj);
((Trime) msg.obj).loadConfig();
}
return false;
});
new Handler(
msg -> {
if (!((Trime) msg.obj).isShowInputRequested()) { // 若当前没有输入面板,则后台同步。防止面板关闭后5秒内再次打开
ShortcutUtils.INSTANCE.syncInBackground((Trime) msg.obj);
((Trime) msg.obj).loadConfig();
}
return false;
});

public Trime() {
try {
Expand All @@ -258,7 +258,7 @@ public void onWindowShown() {

updateComposing();

for (EventListener listener : (List<EventListener>) eventListeners) {
for (EventListener listener : eventListeners) {
if (listener != null) listener.onWindowShown();
}
}
Expand All @@ -280,7 +280,7 @@ public void onWindowHidden() {
syncBackgroundHandler.sendMessageDelayed(msg, 5000); // 输入面板隐藏5秒后,开始后台同步
}

for (EventListener listener : (List<EventListener>) eventListeners) {
for (EventListener listener : eventListeners) {
if (listener != null) listener.onWindowHidden();
}
}
Expand Down Expand Up @@ -380,7 +380,7 @@ public void selectLiquidKeyboard(final int tabIndex) {
liquidKeyboard.calcPadding(mainInputView.getWidth());
liquidKeyboard.select(tabIndex);

tabView.updateCandidateWidth();
tabView.updateTabWidth();
if (inputRootBinding != null) {
mTabRoot.setBackground(mCandidateRoot.getBackground());
mTabRoot.move(tabView.getHightlightLeft(), tabView.getHightlightRight());
Expand Down Expand Up @@ -408,6 +408,7 @@ private void hideCompositionView() {
if (isPopupWindowMovable.equals("once")) {
popupWindowPos = getImeConfig().getWinPos();
}

if (mPopupWindow != null && mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
mPopupHandler.removeCallbacks(mPopupTimer);
Expand All @@ -420,13 +421,13 @@ private void showCompositionView() {
return;
}
compositionRootBinding.compositionRoot.measure(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setWidth(compositionRootBinding.compositionRoot.getMeasuredWidth());
mPopupWindow.setHeight(compositionRootBinding.compositionRoot.getMeasuredHeight());
mPopupHandler.post(mPopupTimer);
}

private void loadBackground() {
public void loadBackground() {
final Config mConfig = getImeConfig();
final int orientation = getResources().getConfiguration().orientation;

Expand Down Expand Up @@ -533,7 +534,7 @@ public void onDestroy() {
getImeConfig().destroy();
System.exit(0); // 清理內存
}
for (EventListener listener : (List<EventListener>) eventListeners) {
for (EventListener listener : eventListeners) {
if (listener != null) listener.onDestroy();
}
eventListeners.clear();
Expand Down Expand Up @@ -603,7 +604,7 @@ public void onUpdateSelection(
performEscape();
}
// Update the caps-lock status for the current cursor position.
updateCursorCapsToInputView();
dispatchCapsStateToInputView();
}

@Override
Expand Down Expand Up @@ -648,15 +649,15 @@ public View onCreateInputView() {
return inputRootBinding.inputRoot;
}

void setShowComment(boolean show_comment) {
public void setShowComment(boolean show_comment) {
// if (mCandidateRoot != null) mCandidate.setShowComment(show_comment);
mComposition.setShowComment(show_comment);
}

@Override
public void onStartInputView(EditorInfo attribute, boolean restarting) {
super.onStartInputView(attribute, restarting);
for (EventListener listener : (List<EventListener>) eventListeners) {
for (EventListener listener : eventListeners) {
if (listener != null) listener.onStartInputView(activeEditorInstance, restarting);
}
if (getPrefs().getOther().getShowStatusBarIcon()) {
Expand Down Expand Up @@ -685,24 +686,18 @@ public void bindKeyboardToInputView() {
// Bind the selected keyboard to the input view.
Keyboard sk = keyboardSwitcher.getCurrentKeyboard();
mainKeyboardView.setKeyboard(sk);
updateCursorCapsToInputView();
dispatchCapsStateToInputView();
}
}

// 句首自動大小寫
private void updateCursorCapsToInputView() {
if (autoCaps.contentEquals("false") || TextUtils.isEmpty(autoCaps)) return;
if ((autoCaps.contentEquals("true") || Rime.isAsciiMode())
/**
* Dispatches cursor caps info to input view in order to implement auto caps lock at the start of
* a sentence.
*/
private void dispatchCapsStateToInputView() {
if ((isAutoCaps || Rime.isAsciiMode())
&& (mainKeyboardView != null && !mainKeyboardView.isCapsOn())) {
final InputConnection ic = getCurrentInputConnection();
if (ic != null) {
int caps = 0;
final EditorInfo ei = getCurrentInputEditorInfo();
if ((ei != null) && (ei.inputType != EditorInfo.TYPE_NULL)) {
caps = ic.getCursorCapsMode(ei.inputType);
}
mainKeyboardView.setShifted(false, caps != 0);
}
mainKeyboardView.setShifted(false, activeEditorInstance.getCursorCapsMode() != 0);
}
}

Expand All @@ -714,14 +709,6 @@ public void commitText(String text) {
activeEditorInstance.commitText(text, true);
}

public void keyPressVibrate() {
if (inputFeedbackManager != null) inputFeedbackManager.keyPressVibrate();
}

public void keyPressSound() {
if (inputFeedbackManager != null) inputFeedbackManager.keyPressSound(0);
}

/**
* 如果爲{@link KeyEvent#KEYCODE_BACK Back鍵},則隱藏鍵盤
*
Expand All @@ -739,7 +726,7 @@ private boolean handleBack(int keyCode) {
public boolean onRimeKey(int[] event) {
updateRimeOption();
final boolean ret = Rime.onKey(event);
activeEditorInstance.commitTextFromRime();
activeEditorInstance.commitRimeText();
return ret;
}

Expand Down Expand Up @@ -904,10 +891,10 @@ public void updateComposing() {
activeEditorInstance.updateComposingText();
if (ic != null && !isWinFixed()) isCursorUpdated = ic.requestCursorUpdates(1);
if (mCandidateRoot != null) {
if (isPopupWindowShown) {
if (isPopupWindowEnabled) {
final int startNum = mComposition.setWindow(minPopupSize, minPopupCheckSize);
mCandidate.setText(startNum);
if (isWinFixed() || !isCursorUpdated) showCompositionView();
if (!isCursorUpdated) showCompositionView();
} else {
mCandidate.setText(0);
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/symbol/TabTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// 为了公用候选栏的皮肤参数以及外观,保持了和普通键盘布局相似的代码。此类相当于原键盘布局的Rime.RimeCandidate

import android.graphics.Rect;

import com.osfans.trime.ime.enums.KeyCommandType;
import com.osfans.trime.ime.enums.SymbolKeyboardType;

Expand Down
50 changes: 27 additions & 23 deletions app/src/main/java/com/osfans/trime/ime/symbol/TabView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
import com.osfans.trime.ime.enums.SymbolKeyboardType;
import com.osfans.trime.setup.Config;
import com.osfans.trime.util.GraphicUtils;

import java.util.ArrayList;

import timber.log.Timber;

// 这是滑动键盘顶部的view,展示了键盘布局的多个标签。
Expand Down Expand Up @@ -128,44 +126,48 @@ protected void onDraw(Canvas canvas) {
candidateHighlight.draw(canvas);
}
// Draw tab text
float tabY = (shouldShowComment && isCommentOnTop)
? tabTags.get(0).geometry.centerY() - (candidatePaint.ascent() + candidatePaint.descent()) / 2.0f
float tabY =
/* (shouldShowComment && isCommentOnTop)
? tabTags.get(0).geometry.centerY()
- (candidatePaint.ascent() + candidatePaint.descent()) / 2.0f
+ commentHeight / 2.0f
: tabTags.get(0).geometry.centerY() - (candidatePaint.ascent() + candidatePaint.descent()) / 2.0f;
: */ tabTags.get(0).geometry.centerY()
- (candidatePaint.ascent() + candidatePaint.descent()) / 2.0f;

for (TabTag computedTab: tabTags) {
for (TabTag computedTab : tabTags) {
int i = tabTags.indexOf(computedTab);
// Calculate a position where the text could be centered in the rectangle.
float tabX = computedTab.geometry.centerX();

candidatePaint.setColor(
isHighlighted(i) ? hilitedCandidateTextColor : candidateTextColor);
candidatePaint.setColor(isHighlighted(i) ? hilitedCandidateTextColor : candidateTextColor);
graphicUtils.drawText(canvas, computedTab.text, tabX, tabY, candidatePaint, candidateFont);
// Draw the separator at the right edge of each candidate.
canvas.drawRect(
computedTab.geometry.right - candidateSpacing,
computedTab.geometry.top,
computedTab.geometry.right + candidateSpacing,
computedTab.geometry.bottom,
separatorPaint
);
computedTab.geometry.right - candidateSpacing,
computedTab.geometry.top,
computedTab.geometry.right + candidateSpacing,
computedTab.geometry.bottom,
separatorPaint);
}
}

public void updateCandidateWidth() {
public void updateTabWidth() {
tabTags = TabManager.get().getTabCandidates();
highlightIndex = TabManager.get().getSelected();

int x = 0;
for (TabTag computedTab: tabTags) {
for (TabTag computedTab : tabTags) {
int i = tabTags.indexOf(computedTab);
computedTab.geometry = new Rect(x, 0, x += getTabWidth(i), getHeight());
x += candidateSpacing;
computedTab.geometry = new Rect(x, 0, (int) (x + getTabWidth(i)), getHeight());
x += +getTabWidth(i) + candidateSpacing;
}
LayoutParams params = getLayoutParams();
Timber.i("update, from Height=" + params.height + " width=" + params.width);
params.width = x;
params.height = (shouldShowComment && isCommentOnTop) ? candidateViewHeight + commentHeight : candidateViewHeight;
params.height =
(shouldShowComment && isCommentOnTop)
? candidateViewHeight + commentHeight
: candidateViewHeight;
Timber.i("update, to Height=" + candidateViewHeight + " width=" + x);
setLayoutParams(params);
params = getLayoutParams();
Expand All @@ -176,7 +178,7 @@ public void updateCandidateWidth() {
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
updateCandidateWidth();
updateTabWidth();
Timber.i("onSizeChanged() w=" + w + ", Height=" + oldh + "=>" + h);
}

Expand Down Expand Up @@ -240,9 +242,9 @@ public boolean onTouchEvent(@NonNull MotionEvent me) {
* @return {@code >=0}: 觸摸點 (x, y) 處候選項序號,從0開始編號; {@code -1}: 觸摸點 (x, y) 處無候選項;
*/
private int getTabIndex(int x, int y) {
//Rect r = new Rect();
// Rect r = new Rect();
int retIndex = -1; // Returns -1 if there is no tab in the hitting rectangle.
for (TabTag computedTab: tabTags) {
for (TabTag computedTab : tabTags) {
/* Enlarge the rectangle to be more responsive to user clicks.
// r.set(tabGeometries[j++]);
//r.inset(0, CANDIDATE_TOUCH_OFFSET); */
Expand All @@ -255,6 +257,8 @@ private int getTabIndex(int x, int y) {

private float getTabWidth(int i) {
String s = tabTags.get(i).text;
return s != null ? 2 * candidatePadding + graphicUtils.measureText(candidatePaint, s, candidateFont) : 2 * candidatePadding;
return s != null
? 2 * candidatePadding + graphicUtils.measureText(candidatePaint, s, candidateFont)
: 2 * candidatePadding;
}
}
1 change: 1 addition & 0 deletions app/src/main/java/com/osfans/trime/ime/text/Candidate.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Candidate extends View {
/** 處理候選條選字事件 */
public interface EventListener {
void onCandidatePressed(int index);

void onCandidateSymbolPressed(String arrow);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class TextInputManager private constructor() :
CoroutineScope by MainScope(),
Trime.EventListener,
KeyboardView.OnKeyboardActionListener,
Candidate.EventListener
{
Candidate.EventListener {
private val trime get() = Trime.getService()
private val prefs get() = Preferences.defaultInstance()
private val activeEditorInstance: EditorInstance
Expand Down Expand Up @@ -243,8 +242,8 @@ class TextInputManager private constructor() :
}
"_liquid_keyboard" -> trime.selectLiquidKeyboard(0)
"_hide_key_hint" -> if (mainKeyboardView != null) mainKeyboardView!!.setShowHint(!value)
else -> if (option.startsWith("_keyboard_")
&& option.length > 10 && value
else -> if (option.startsWith("_keyboard_") &&
option.length > 10 && value
) {
val keyboard = option.substring(10)
keyboardSwitcher.switchToKeyboard(keyboard)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import com.osfans.trime.ime.core.Preferences
import com.osfans.trime.settings.components.SchemaPickerDialog
import com.osfans.trime.util.RimeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber

internal const val FRAGMENT_TAG = "FRAGMENT_TAG"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.osfans.trime.settings.components

import androidx.appcompat.app.AlertDialog
import android.content.Context
import android.os.Build
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import com.osfans.trime.R
import com.osfans.trime.ime.core.Preferences
import com.osfans.trime.ime.core.Trime
Expand Down
Loading

0 comments on commit 0de8f23

Please sign in to comment.