AnalogStickTranslator { public void handle(MotionEvent event) { if (!isAnalogStick(event) || mCallback == null) { return; } float oldX = mX; float oldY = mY; int key = KeyEvent.KEYCODE_UNKNOWN; calculate(event); if (mX > THRESHOLD && mX > oldX && oldX != 0) { key = KeyEvent.KEYCODE_DPAD_RIGHT; } else if (mX < THRESHOLD2 && mX < oldX && oldX != 0) { key = KeyEvent.KEYCODE_DPAD_LEFT; } else if (mY > THRESHOLD && mY > oldY && oldY != 0) { key = KeyEvent.KEYCODE_DPAD_UP; } else if (mY < THRESHOLD2 && mY < oldY && oldY != 0) { key = KeyEvent.KEYCODE_DPAD_DOWN; } if (key != KeyEvent.KEYCODE_UNKNOWN) { mCallback.onAnalogStickKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, key)); mCallback.onAnalogStickKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, key)); } } AnalogStickTranslator(Callback callback); void handle(MotionEvent event);  }
@Test public void moveStickLeftDescendant() { for (MotionEvent me : mLeftDesc) { mTranslator.handle(me); } assertNull("Event should NOT be produced when moving in descendant direction", mLastKeyEvent); }
@Test public void moveStickLeftAscendant() { for (MotionEvent me : mLeftAsc) { mTranslator.handle(me); } assertTrue("Event is not null", mLastKeyEvent != null); assertTrue("Event should be produced when moving in ascendant direction", mLastKeyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT); }
@Test public void moveStickRightDescendant() { for (MotionEvent me : mRightDesc) { mTranslator.handle(me); } assertNull("Event should NOT be produced when moving in descendant direction", mLastKeyEvent); }
@Test public void moveStickRightAscendant() { for (MotionEvent me : mRightAsc) { mTranslator.handle(me); } assertTrue("Event is not null", mLastKeyEvent != null); assertTrue("Event should be produced when moving in ascendant direction", mLastKeyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT); }
@Test public void moveStickUpDescendant() { for (MotionEvent me : mUpDesc) { mTranslator.handle(me); } assertNull("Event should NOT be produced when moving in descendant direction", mLastKeyEvent); }
@Test public void moveStickUpAscendant() { for (MotionEvent me : mUpAsc) { mTranslator.handle(me); } assertTrue("Event is not null", mLastKeyEvent != null); assertTrue("Event should be produced when moving in ascendant direction", mLastKeyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP); }
@Test public void moveStickDownDescendant() { for (MotionEvent me : mDownDesc) { mTranslator.handle(me); } assertNull("Event should NOT be produced when moving in descendant direction", mLastKeyEvent); }
@Test public void moveStickDownAscendant() { for (MotionEvent me : mDownAsc) { mTranslator.handle(me); } assertTrue("Event is not null", mLastKeyEvent != null); assertTrue("Event should be produced when moving in ascendant direction", mLastKeyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN); }