refactoring KeyboardManager

This commit is contained in:
Yuriy Liskov
2018-01-23 12:00:53 +02:00
parent 558664f7fc
commit 3e02d5e4ef
4 changed files with 118 additions and 96 deletions

View File

@@ -26,7 +26,6 @@ import android.util.AttributeSet;
import com.anysoftkeyboard.addons.AddOn;
import com.anysoftkeyboard.addons.AddOnsFactory;
import com.anysoftkeyboard.utils.Logger;
//import com.menny.android.anysoftkeyboard.R;
import java.util.ArrayList;
import java.util.List;

View File

@@ -20,7 +20,6 @@ import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
@@ -312,7 +311,11 @@ public class LeanbackKeyboardContainer {
onShiftDoubleClick(isCapsLockOn());
}
// NOTE: suggestions settings applied here
/**
* All keyboard settings applied here
* @param resources resources (not used)
* @param info current ime attributes
*/
private void setImeOptions(Resources resources, EditorInfo info) {
// do not erase last keyboard
if (mInitialMainKeyboard == null) {
@@ -355,6 +358,8 @@ public class LeanbackKeyboardContainer {
mInitialMainKeyboard = this.mAbcKeyboard;
}
// TODO: many empty ifs
if (mSuggestionsEnabled) {
if ((info.inputType & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) == 0) {
;
@@ -478,18 +483,22 @@ public class LeanbackKeyboardContainer {
}
private void setShiftState(int state) {
this.mMainKeyboardView.setShiftState(state);
mMainKeyboardView.setShiftState(state);
}
private void setTouchStateInternal(int state) {
this.mTouchState = state;
mTouchState = state;
}
/**
* Speech recognizer routine
* @param context context
*/
private void startRecognition(Context context) {
this.mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
this.mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "free_form");
this.mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
this.mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "free_form");
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
float peakRmsLevel = 0.0F;
int rmsCounter = 0;
@@ -546,9 +555,9 @@ public class LeanbackKeyboardContainer {
@Override
public void onResults(Bundle bundle) {
ArrayList var2 = bundle.getStringArrayList("results_recognition");
if (var2 != null && LeanbackKeyboardContainer.this.mVoiceListener != null) {
LeanbackKeyboardContainer.this.mVoiceListener.onVoiceResult((String) var2.get(0));
ArrayList results = bundle.getStringArrayList("results_recognition");
if (results != null && LeanbackKeyboardContainer.this.mVoiceListener != null) {
LeanbackKeyboardContainer.this.mVoiceListener.onVoiceResult((String) results.get(0));
}
LeanbackKeyboardContainer.this.cancelVoiceRecording();
@@ -560,7 +569,7 @@ public class LeanbackKeyboardContainer {
throw new IllegalStateException("method not implemented");
}
});
this.mSpeechRecognizer.startListening(this.mRecognizerIntent);
mSpeechRecognizer.startListening(this.mRecognizerIntent);
}
public void alignSelector(final float x, final float y, final boolean playAnimation) {
@@ -648,21 +657,21 @@ public class LeanbackKeyboardContainer {
}
public LeanbackKeyboardContainer.KeyFocus getCurrFocus() {
return this.mCurrKeyInfo;
return mCurrKeyInfo;
}
public int getCurrKeyCode() {
int var1 = 0;
Key var2 = this.getKey(this.mCurrKeyInfo.type, this.mCurrKeyInfo.index);
if (var2 != null) {
var1 = var2.codes[0];
int keyCode = 0;
Key key = getKey(mCurrKeyInfo.type, mCurrKeyInfo.index);
if (key != null) {
keyCode = key.codes[0];
}
return var1;
return keyCode;
}
public Button getGoButton() {
return this.mActionButtonView;
return mActionButtonView;
}
public Key getKey(int type, int index) {
@@ -1036,6 +1045,10 @@ public class LeanbackKeyboardContainer {
}
}
/**
* Set touch state
* @param state {@link LeanbackKeyboardContainer LeanbackKeyboardContainer} constant
*/
public void setTouchState(final int state) {
switch (state) {
case TOUCH_STATE_NO_TOUCH:
@@ -1065,58 +1078,60 @@ public class LeanbackKeyboardContainer {
setKbFocus(mCurrKeyInfo, true, true);
}
public void setVoiceListener(LeanbackKeyboardContainer.VoiceListener var1) {
this.mVoiceListener = var1;
public void setVoiceListener(LeanbackKeyboardContainer.VoiceListener listener) {
mVoiceListener = listener;
}
public void startVoiceRecording() {
if (this.mVoiceEnabled) {
if (!this.mVoiceKeyDismissesEnabled) {
this.mVoiceAnimator.startEnterAnimation();
if (mVoiceEnabled) {
if (!mVoiceKeyDismissesEnabled) {
mVoiceAnimator.startEnterAnimation();
return;
}
this.mDismissListener.onDismiss(true);
mDismissListener.onDismiss(true);
}
}
/**
* Switch to next keyboard (looped).
* {@link KeyboardManager KeyboardManager} is the source behind all keyboard implementations
*/
public void switchToNextKeyboard() {
LeanbackKeyboardView var1 = this.mMainKeyboardView;
Keyboard var2 = this.mKeyboardManager.getNextKeyboard();
this.mInitialMainKeyboard = var2;
var1.setKeyboard(var2);
LeanbackKeyboardView keyboardView = mMainKeyboardView;
Keyboard keyboard = mKeyboardManager.getNextKeyboard();
mInitialMainKeyboard = keyboard;
keyboardView.setKeyboard(keyboard);
}
public void updateAddonKeyboard() {
KeyboardManager var1 = new KeyboardManager(this.mContext, this.mAbcKeyboard);
this.mKeyboardManager = var1;
this.mInitialMainKeyboard = var1.getNextKeyboard();
KeyboardManager manager = new KeyboardManager(mContext, mAbcKeyboard);
mKeyboardManager = manager;
mInitialMainKeyboard = manager.getNextKeyboard();
}
public void updateSuggestions(ArrayList<String> var1) {
int var2 = this.mSuggestions.getChildCount();
int var3 = var1.size();
if (var3 < var2) {
this.mSuggestions.removeViews(var3, var2 - var3);
} else if (var3 > var2) {
while (var2 < var3) {
View var4 = this.mContext.getLayoutInflater().inflate(R.layout.candidate, (ViewGroup) null);
this.mSuggestions.addView(var4);
++var2;
public void updateSuggestions(ArrayList<String> suggestions) {
int oldCount = mSuggestions.getChildCount();
int newCount = suggestions.size();
if (newCount < oldCount) {
mSuggestions.removeViews(newCount, oldCount - newCount);
} else if (newCount > oldCount) {
while (oldCount < newCount) {
View suggestion = mContext.getLayoutInflater().inflate(R.layout.candidate, null);
mSuggestions.addView(suggestion);
++oldCount;
}
}
for (var2 = 0; var2 < var3; ++var2) {
Button var5 = (Button) this.mSuggestions.getChildAt(var2).findViewById(R.id.text);
var5.setText((CharSequence) var1.get(var2));
var5.setContentDescription((CharSequence) var1.get(var2));
for (oldCount = 0; oldCount < newCount; ++oldCount) {
Button suggestion = mSuggestions.getChildAt(oldCount).findViewById(R.id.text);
suggestion.setText(suggestions.get(oldCount));
suggestion.setContentDescription(suggestions.get(oldCount));
}
if (this.getCurrFocus().type == 3) {
this.resetFocusCursor();
if (getCurrFocus().type == KeyFocus.TYPE_SUGGESTION) {
resetFocusCursor();
}
}
public interface DismissListener {

View File

@@ -185,6 +185,11 @@ public class LeanbackKeyboardController implements LeanbackKeyboardContainer.Voi
handleCommitKeyboardKey(keyCode, null);
}
/**
* Fake key index
* @param index key index
* @param type {@link KeyFocus KeyFocus} constant
*/
private void fakeKeyIndex(final int index, final int type) {
LeanbackKeyboardContainer.KeyFocus focus = mContainer.getCurrFocus();
focus.index = index;
@@ -669,29 +674,29 @@ public class LeanbackKeyboardController implements LeanbackKeyboardContainer.Voi
this.mDoubleClickDetector.reset();
}
public boolean onTouch(View var1, MotionEvent var2) {
Object var3 = var1.getTag();
if (var3 != null && "Go".equals(var3)) {
this.fakeKeyIndex(0, 2);
public boolean onTouch(View view, MotionEvent event) {
Object tag = view.getTag();
if (tag != null && "Go".equals(tag)) {
fakeKeyIndex(0, KeyFocus.TYPE_ACTION);
} else {
switch (var2.getAction()) {
case 0:
this.moveSelectorToPoint(var2.getX(), var2.getY());
this.fakeClickDown();
this.beginLongClickCountdown();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
moveSelectorToPoint(event.getX(), event.getY());
fakeClickDown();
beginLongClickCountdown();
break;
case 1:
if (!this.clickConsumed) {
this.clickConsumed = true;
if (this.isDoubleClick()) {
this.mContainer.onKeyLongPress();
case MotionEvent.ACTION_UP:
if (!clickConsumed) {
clickConsumed = true;
if (isDoubleClick()) {
mContainer.onKeyLongPress();
break;
}
this.fakeClickUp();
fakeClickUp();
}
this.fakeLongClickUp();
fakeLongClickUp();
break;
default:
return false;
@@ -701,35 +706,35 @@ public class LeanbackKeyboardController implements LeanbackKeyboardContainer.Voi
return true;
}
public void onVoiceResult(String var1) {
this.mInputListener.onEntry(6, 0, var1);
public void onVoiceResult(String result) {
mInputListener.onEntry(InputListener.ENTRY_TYPE_VOICE, 0, result);
}
public void run() {
if (!this.clickConsumed) {
this.clickConsumed = true;
this.fakeLongClickDown();
if (!clickConsumed) {
clickConsumed = true;
fakeLongClickDown();
}
}
public void setKeyboardContainer(LeanbackKeyboardContainer var1) {
this.mContainer = var1;
var1.getView().addOnLayoutChangeListener(this.mOnLayoutChangeListener);
public void setKeyboardContainer(LeanbackKeyboardContainer container) {
mContainer = container;
container.getView().addOnLayoutChangeListener(mOnLayoutChangeListener);
}
public void setSpaceTracker(TouchNavSpaceTracker var1) {
this.mSpaceTracker = var1;
var1.setLPFEnabled(true);
var1.setKeyEventListener(this.mTouchEventListener);
public void setSpaceTracker(TouchNavSpaceTracker tracker) {
mSpaceTracker = tracker;
tracker.setLPFEnabled(true);
tracker.setKeyEventListener(mTouchEventListener);
}
public void updateAddonKeyboard() {
this.mContainer.updateAddonKeyboard();
mContainer.updateAddonKeyboard();
}
public void updateSuggestions(ArrayList<String> var1) {
if (this.mContainer != null) {
this.mContainer.updateSuggestions(var1);
public void updateSuggestions(ArrayList<String> suggestions) {
if (mContainer != null) {
mContainer.updateSuggestions(suggestions);
}
}
@@ -740,23 +745,23 @@ public class LeanbackKeyboardController implements LeanbackKeyboardContainer.Voi
long mFirstClickTime;
private DoubleClickDetector() {
this.DOUBLE_CLICK_TIMEOUT_MS = 200L;
this.mFirstClickTime = 0L;
DOUBLE_CLICK_TIMEOUT_MS = 200L;
mFirstClickTime = 0L;
}
public void addEvent(long var1) {
if (var1 - this.mFirstClickTime > 200L) {
this.mFirstClickTime = var1;
this.mFirstClickShiftLocked = LeanbackKeyboardController.this.mContainer.isCapsLockOn();
public void addEvent(long currTime) {
if (currTime - mFirstClickTime > DOUBLE_CLICK_TIMEOUT_MS) {
mFirstClickTime = currTime;
mFirstClickShiftLocked = LeanbackKeyboardController.this.mContainer.isCapsLockOn();
LeanbackKeyboardController.this.commitKey();
} else {
LeanbackKeyboardController.this.mContainer.onShiftDoubleClick(this.mFirstClickShiftLocked);
this.reset();
LeanbackKeyboardController.this.mContainer.onShiftDoubleClick(mFirstClickShiftLocked);
reset();
}
}
public void reset() {
this.mFirstClickTime = 0L;
mFirstClickTime = 0L;
}
}

View File

@@ -16,8 +16,8 @@ public class KeyboardManager {
private final KeyboardFactory mKeyboardFactory;
private int mKeyboardIndex = 0;
public KeyboardManager(Context ctx, int defaultKeyboard1) {
this(ctx, new Keyboard(ctx, defaultKeyboard1));
public KeyboardManager(Context ctx, int keyboardResId) {
this(ctx, new Keyboard(ctx, keyboardResId));
}
public KeyboardManager(Context ctx, Keyboard englishKeyboard) {
@@ -40,16 +40,19 @@ public class KeyboardManager {
return keyboards;
}
/**
* Get next keyboard from internal source (looped)
* @return keyboard
*/
public Keyboard getNextKeyboard() {
++mKeyboardIndex;
mKeyboardIndex = mKeyboardIndex < mAllKeyboards.size() ? mKeyboardIndex : 0;
Keyboard kbd = mAllKeyboards.get(mKeyboardIndex);
if (kbd == null) {
throw new UnsupportedOperationException(String.format("Keyboard %s not initialized", mKeyboardIndex));
throw new IllegalStateException(String.format("Keyboard %s not initialized", mKeyboardIndex));
}
return kbd;
}
}