FileDocCategorySizeDatePackage
EventListener.javaAPI DocAndroid 1.5 API4310Wed May 06 22:42:42 BST 2009com.android.calculator2

EventListener

public class EventListener extends Object implements android.view.View.OnClickListener, android.view.View.OnKeyListener, android.view.View.OnLongClickListener

Fields Summary
Logic
mHandler
PanelSwitcher
mPanelSwitcher
private static final char[]
EQUAL
Constructors Summary
Methods Summary
public voidonClick(android.view.View view)

        int id = view.getId();
        switch (id) {
        case R.id.del:
            mHandler.onDelete();
            break;

        case R.id.equal:
            mHandler.onEnter();
            break;

            /*
        case R.id.clear:
            mHandler.onClear();
            break;
            */

        default:
            if (view instanceof Button) {
                String text = ((Button) view).getText().toString();
                if (text.length() >= 2) {
                    // add paren after sin, cos, ln, etc. from buttons
                    text += '(";
                }
                mHandler.insert(text);
                if (mPanelSwitcher != null && 
                    mPanelSwitcher.getCurrentIndex() == Calculator.ADVANCED_PANEL) {
                    mPanelSwitcher.moveRight();
                }                    
            }
        }
    
public booleanonKey(android.view.View view, int keyCode, android.view.KeyEvent keyEvent)


    //@Override
            
        int action = keyEvent.getAction();
        
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT ||
            keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            boolean eat = mHandler.eatHorizontalMove(keyCode == KeyEvent.KEYCODE_DPAD_LEFT);
            return eat;
        }

        //Work-around for spurious key event from IME, bug #1639445
        if (action == KeyEvent.ACTION_MULTIPLE && keyCode == KeyEvent.KEYCODE_UNKNOWN) {
            return true; // eat it
        }

        //Calculator.log("KEY " + keyCode + "; " + action);
        
        if (keyEvent.getMatch(EQUAL, keyEvent.getMetaState()) == '=") {
            if (action == KeyEvent.ACTION_UP) {
                mHandler.onEnter();
            }
            return true;
        }

        if (keyCode != KeyEvent.KEYCODE_DPAD_CENTER &&
            keyCode != KeyEvent.KEYCODE_DPAD_UP &&
            keyCode != KeyEvent.KEYCODE_DPAD_DOWN &&
            keyCode != KeyEvent.KEYCODE_ENTER) {
            return false;
        }

        /* 
           We should act on KeyEvent.ACTION_DOWN, but strangely
           sometimes the DOWN event isn't received, only the UP.
           So the workaround is to act on UP...
           http://b/issue?id=1022478
         */

        if (action == KeyEvent.ACTION_UP) {
            switch (keyCode) {                
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_DPAD_CENTER:
                mHandler.onEnter();
                break;
                
            case KeyEvent.KEYCODE_DPAD_UP:
                mHandler.onUp();
                break;

            case KeyEvent.KEYCODE_DPAD_DOWN:            
                mHandler.onDown();
                break;
            }
        }
        return true;
    
public booleanonLongClick(android.view.View view)

        int id = view.getId();
        if (id == R.id.del) {
            mHandler.onClear();
            return true;
        }
        return false;
    
voidsetHandler(Logic handler, PanelSwitcher panelSwitcher)

        mHandler = handler;
        mPanelSwitcher = panelSwitcher;