am 6bfb234f: Refactor KeyDetector to share more methods

Merge commit '6bfb234f294b6ad95176f987256c85e8607d23f5' into gingerbread-plus-aosp

* commit '6bfb234f294b6ad95176f987256c85e8607d23f5':
  Refactor KeyDetector to share more methods
main
Tadashi G. Takaoka 2010-09-02 12:51:02 -07:00 committed by Android Git Automerger
commit 23eb471102
2 changed files with 19 additions and 10 deletions

View File

@ -19,11 +19,12 @@ package com.android.inputmethod.latin;
import android.inputmethodservice.Keyboard; import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key; import android.inputmethodservice.Keyboard.Key;
import java.util.Arrays;
import java.util.List; import java.util.List;
abstract class KeyDetector { abstract class KeyDetector {
protected Keyboard mKeyboard; protected Keyboard mKeyboard;
protected Key[] mKeys; private Key[] mKeys;
protected int mCorrectionX; protected int mCorrectionX;
protected int mCorrectionY; protected int mCorrectionY;
@ -50,6 +51,13 @@ abstract class KeyDetector {
return y + mCorrectionY; return y + mCorrectionY;
} }
protected Key[] getKeys() {
if (mKeys == null)
throw new IllegalStateException("keyboard isn't set");
// mKeyboard is guaranteed not null at setKeybaord() method
return mKeys;
}
public void setProximityCorrectionEnabled(boolean enabled) { public void setProximityCorrectionEnabled(boolean enabled) {
mProximityCorrectOn = enabled; mProximityCorrectOn = enabled;
} }
@ -62,7 +70,13 @@ abstract class KeyDetector {
mProximityThresholdSquare = threshold * threshold; mProximityThresholdSquare = threshold * threshold;
} }
abstract public int[] newCodeArray(); public int[] newCodeArray() {
int[] codes = new int[getMaxNearbyKeys()];
Arrays.fill(codes, LatinKeyboardBaseView.NOT_A_KEY);
return codes;
}
abstract protected int getMaxNearbyKeys();
abstract public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys); abstract public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys);
} }

View File

@ -27,20 +27,15 @@ class ProximityKeyDetector extends KeyDetector {
private int[] mDistances = new int[MAX_NEARBY_KEYS]; private int[] mDistances = new int[MAX_NEARBY_KEYS];
@Override @Override
public int[] newCodeArray() { protected int getMaxNearbyKeys() {
int[] codes = new int[MAX_NEARBY_KEYS]; return MAX_NEARBY_KEYS;
Arrays.fill(codes, LatinKeyboardBaseView.NOT_A_KEY);
return codes;
} }
@Override @Override
public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys) { public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys) {
final Key[] keys = getKeys();
final int touchX = getTouchX(x); final int touchX = getTouchX(x);
final int touchY = getTouchY(y); final int touchY = getTouchY(y);
final Key[] keys = mKeys;
if (keys == null)
throw new IllegalStateException("keyboard isn't set");
// mKeyboard is guaranteed not null at setKeybaord() method
int primaryIndex = LatinKeyboardBaseView.NOT_A_KEY; int primaryIndex = LatinKeyboardBaseView.NOT_A_KEY;
int closestKey = LatinKeyboardBaseView.NOT_A_KEY; int closestKey = LatinKeyboardBaseView.NOT_A_KEY;
int closestKeyDist = mProximityThresholdSquare + 1; int closestKeyDist = mProximityThresholdSquare + 1;