FileDocCategorySizeDatePackage
Calculator.javaAPI DocAndroid 1.5 API5104Wed May 06 22:42:42 BST 2009com.android.calculator2

Calculator

public class Calculator extends android.app.Activity

Fields Summary
EventListener
mListener
private CalculatorDisplay
mDisplay
private Persist
mPersist
private History
mHistory
private Logic
mLogic
private PanelSwitcher
mPanelSwitcher
private static final int
CMD_CLEAR_HISTORY
private static final int
CMD_BASIC_PANEL
private static final int
CMD_ADVANCED_PANEL
static final int
BASIC_PANEL
static final int
ADVANCED_PANEL
private static final String
LOG_TAG
private static final boolean
DEBUG
private static final boolean
LOG_ENABLED
Constructors Summary
Methods Summary
static voidlog(java.lang.String message)

        if (LOG_ENABLED) {
            Log.v(LOG_TAG, message);
        }
    
public voidonCreate(android.os.Bundle icicle)


    
        
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        mPersist = new Persist(this);
        mHistory = mPersist.history;

        mDisplay = (CalculatorDisplay) findViewById(R.id.display);

        mLogic = new Logic(this, mHistory, mDisplay, (Button) findViewById(R.id.equal));
        HistoryAdapter historyAdapter = new HistoryAdapter(this, mHistory, mLogic);
        mHistory.setObserver(historyAdapter);
        View view;
        mPanelSwitcher = (PanelSwitcher) findViewById(R.id.panelswitch);
                                       
        mListener.setHandler(mLogic, mPanelSwitcher);

        mDisplay.setOnKeyListener(mListener);


        if ((view = findViewById(R.id.del)) != null) {
            view.setOnClickListener(mListener);
            view.setOnLongClickListener(mListener);
        }
        /*
        if ((view = findViewById(R.id.clear)) != null) {
            view.setOnClickListener(mListener);
        }
        */

        /*
        ListView historyPad = (ListView) findViewById(R.id.historyPad);
        if (historyPad != null) {
            historyPad.setAdapter(historyAdapter);
        }
        */
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        super.onCreateOptionsMenu(menu);
        MenuItem item;
        
        item = menu.add(0, CMD_CLEAR_HISTORY, 0, R.string.clear_history);
        item.setIcon(R.drawable.clear_history);
        
        item = menu.add(0, CMD_ADVANCED_PANEL, 0, R.string.advanced);
        item.setIcon(R.drawable.advanced);
        
        item = menu.add(0, CMD_BASIC_PANEL, 0, R.string.basic);
        item.setIcon(R.drawable.simple);

        return true;
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        switch (item.getItemId()) {
        case CMD_CLEAR_HISTORY:
            mHistory.clear();
            break;

        case CMD_BASIC_PANEL:
            if (mPanelSwitcher != null && 
                mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL) {
                mPanelSwitcher.moveRight();
            }
            break;

        case CMD_ADVANCED_PANEL:
            if (mPanelSwitcher != null && 
                mPanelSwitcher.getCurrentIndex() == BASIC_PANEL) {
                mPanelSwitcher.moveLeft();
            }
            break;
        }
        return super.onOptionsItemSelected(item);
    
public voidonPause()

        super.onPause();
        mLogic.updateHistory();
        mPersist.save();
    
public booleanonPrepareOptionsMenu(android.view.Menu menu)

        super.onPrepareOptionsMenu(menu);
        menu.findItem(CMD_BASIC_PANEL).setVisible(mPanelSwitcher != null && 
                          mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL);
        
        menu.findItem(CMD_ADVANCED_PANEL).setVisible(mPanelSwitcher != null && 
                          mPanelSwitcher.getCurrentIndex() == BASIC_PANEL);
        
        return true;
    
protected voidonSaveInstanceState(android.os.Bundle icicle)

        // as work-around for ClassCastException in TextView on restart
        // avoid calling superclass, to keep icicle empty