FileDocCategorySizeDatePackage
Logic.javaAPI DocAndroid 1.5 API6616Wed May 06 22:42:42 BST 2009com.android.calculator2

Logic

public class Logic extends Object

Fields Summary
private CalculatorDisplay
mDisplay
private org.javia.arity.Symbols
mSymbols
private History
mHistory
private String
mResult
private android.widget.Button
mEqualButton
private final String
mEnterString
private boolean
mIsError
private final boolean
mOrientationPortrait
private final int
mLineLength
private static final int
LINE_LENGTH_PORTRAIT
private static final int
LINE_LENGTH_LANDSCAPE
private static final String
INFINITY_UNICODE
private static final String
INFINITY
private static final String
NAN
static final char
MINUS
private final String
mErrorString
private static final int
ROUND_DIGITS
Constructors Summary
Logic(android.content.Context context, History history, CalculatorDisplay display, android.widget.Button equalButton)


            
        mErrorString = context.getResources().getString(R.string.error);
        mOrientationPortrait = context.getResources().getConfiguration().orientation
            == Configuration.ORIENTATION_PORTRAIT;
        mLineLength = mOrientationPortrait ? LINE_LENGTH_PORTRAIT : LINE_LENGTH_LANDSCAPE;

        try {
            // in calculator we use log() for base-10,
            // unlike in arity-lib where log() is natural logarithm
            mSymbols.define(mSymbols.compileWithName("log(x)=log10(x)"));
        } catch (SyntaxException e) {
            throw new Error("" + e); //never
        }
        mHistory = history;
        mDisplay = display;
        mDisplay.setLogic(this);
        mEqualButton = equalButton;
        mEnterString = context.getText(R.string.enter).toString();

        clearWithHistory(false);
    
Methods Summary
booleanacceptInsert(java.lang.String delta)

        String text = getText();
        return !mIsError &&
            (!mResult.equals(text) || 
             isOperator(delta) ||
             mDisplay.getSelectionStart() != text.length());
    
private voidclear(boolean scroll)

        mDisplay.setText("", scroll ? CalculatorDisplay.Scroll.UP : CalculatorDisplay.Scroll.NONE);
        cleared();
    
private voidclearWithHistory(boolean scroll)

        mDisplay.setText(mHistory.getText(), 
                         scroll ? CalculatorDisplay.Scroll.UP : CalculatorDisplay.Scroll.NONE);
        mResult = "";
        mIsError = false;
    
voidcleared()

        mResult = "";
        mIsError = false;
        updateHistory();
    
booleaneatHorizontalMove(boolean toLeft)

        EditText editText = mDisplay.getEditText();
        int cursorPos = editText.getSelectionStart();
        return toLeft ? cursorPos == 0 : cursorPos >= editText.length(); 
    
java.lang.Stringevaluate(java.lang.String input)

         
        if (input.trim().equals("")) {
            return "";
        }

        // drop final infix operators (they can only result in error)
        int size = input.length();
        while (size > 0 && isOperator(input.charAt(size - 1))) {
            input = input.substring(0, size - 1);
            --size;
        }

        String result = Util.doubleToString(mSymbols.eval(input), mLineLength, ROUND_DIGITS);
        if (result.equals(NAN)) { // treat NaN as Error
            mIsError = true;
            return mErrorString;
        }
        return result.replace('-", MINUS).replace(INFINITY, INFINITY_UNICODE);
    
private java.lang.StringgetText()

        return mDisplay.getText().toString();
    
voidinsert(java.lang.String delta)

        mDisplay.insert(delta);
    
static booleanisOperator(java.lang.String text)

        return text.length() == 1 && isOperator(text.charAt(0));
    
static booleanisOperator(char c)

        //plus minus times div
        return "+\u2212\u00d7\u00f7/*".indexOf(c) != -1;
    
voidonClear()

        clear(false);
    
voidonDelete()

        if (getText().equals(mResult) || mIsError) {
            clear(false);
        } else {
            mDisplay.dispatchKeyEvent(new KeyEvent(0, KeyEvent.KEYCODE_DEL));
            mResult = "";
        }
    
voidonDown()

        String text = getText();
        if (!text.equals(mResult)) {
            mHistory.update(text);
        }
        if (mHistory.moveToNext()) {
            mDisplay.setText(mHistory.getText(), CalculatorDisplay.Scroll.UP);
        }
    
voidonEnter()

        String text = getText();
        if (text.equals(mResult)) {
            clearWithHistory(false); //clear after an Enter on result
        } else {
            mHistory.enter(text);
            try {
                mResult = evaluate(text);
            } catch (SyntaxException e) {
                mIsError = true;
                mResult = mErrorString;
            }
            if (text.equals(mResult)) {
                //no need to show result, it is exactly what the user entered
                clearWithHistory(true);
            } else {
                setText(mResult);
                //mEqualButton.setText(mEnterString);
            }
        }
    
voidonUp()

        String text = getText();
        if (!text.equals(mResult)) {
            mHistory.update(text);
        }
        if (mHistory.moveToPrevious()) {
            mDisplay.setText(mHistory.getText(), CalculatorDisplay.Scroll.DOWN);
        }
    
private voidsetText(java.lang.CharSequence text)

        mDisplay.setText(text, CalculatorDisplay.Scroll.UP);
    
voidupdateHistory()

        mHistory.update(getText());