The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.calculator2; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.os.Bundle; |
| 21 | import android.util.Log; |
| 22 | import android.util.Config; |
| 23 | import android.view.Menu; |
| 24 | import android.view.MenuItem; |
| 25 | import android.view.Window; |
| 26 | import android.view.View; |
| 27 | import android.widget.Button; |
| 28 | import android.widget.ListView; |
| 29 | import android.content.res.Configuration; |
| 30 | |
| 31 | public class Calculator extends Activity { |
| 32 | EventListener mListener = new EventListener(); |
| 33 | private CalculatorDisplay mDisplay; |
| 34 | private Persist mPersist; |
| 35 | private History mHistory; |
| 36 | private Logic mLogic; |
| 37 | private PanelSwitcher mPanelSwitcher; |
| 38 | |
| 39 | private static final int CMD_CLEAR_HISTORY = 1; |
| 40 | private static final int CMD_BASIC_PANEL = 2; |
| 41 | private static final int CMD_ADVANCED_PANEL = 3; |
| 42 | |
| 43 | static final int BASIC_PANEL = 0; |
| 44 | static final int ADVANCED_PANEL = 1; |
| 45 | |
| 46 | private static final String LOG_TAG = "Calculator"; |
| 47 | private static final boolean DEBUG = false; |
| 48 | private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; |
| 49 | |
| 50 | @Override |
| 51 | public void onCreate(Bundle icicle) { |
| 52 | super.onCreate(icicle); |
| 53 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 54 | setContentView(R.layout.main); |
| 55 | |
| 56 | mPersist = new Persist(this); |
| 57 | mHistory = mPersist.history; |
| 58 | |
| 59 | mDisplay = (CalculatorDisplay) findViewById(R.id.display); |
| 60 | |
| 61 | mLogic = new Logic(this, mHistory, mDisplay, (Button) findViewById(R.id.equal)); |
| 62 | HistoryAdapter historyAdapter = new HistoryAdapter(this, mHistory, mLogic); |
| 63 | mHistory.setObserver(historyAdapter); |
| 64 | View view; |
| 65 | mPanelSwitcher = (PanelSwitcher) findViewById(R.id.panelswitch); |
| 66 | |
| 67 | mListener.setHandler(mLogic, mPanelSwitcher); |
| 68 | |
| 69 | mDisplay.setOnKeyListener(mListener); |
| 70 | |
| 71 | |
| 72 | if ((view = findViewById(R.id.del)) != null) { |
| 73 | view.setOnClickListener(mListener); |
| 74 | view.setOnLongClickListener(mListener); |
| 75 | } |
| 76 | /* |
| 77 | if ((view = findViewById(R.id.clear)) != null) { |
| 78 | view.setOnClickListener(mListener); |
| 79 | } |
| 80 | */ |
| 81 | |
| 82 | /* |
| 83 | ListView historyPad = (ListView) findViewById(R.id.historyPad); |
| 84 | if (historyPad != null) { |
| 85 | historyPad.setAdapter(historyAdapter); |
| 86 | } |
| 87 | */ |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean onCreateOptionsMenu(Menu menu) { |
| 92 | super.onCreateOptionsMenu(menu); |
| 93 | MenuItem item; |
| 94 | |
| 95 | item = menu.add(0, CMD_CLEAR_HISTORY, 0, R.string.clear_history); |
| 96 | item.setIcon(R.drawable.clear_history); |
| 97 | |
| 98 | item = menu.add(0, CMD_ADVANCED_PANEL, 0, R.string.advanced); |
| 99 | item.setIcon(R.drawable.advanced); |
| 100 | |
| 101 | item = menu.add(0, CMD_BASIC_PANEL, 0, R.string.basic); |
| 102 | item.setIcon(R.drawable.simple); |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 109 | super.onPrepareOptionsMenu(menu); |
| 110 | menu.findItem(CMD_BASIC_PANEL).setVisible(mPanelSwitcher != null && |
| 111 | mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL); |
| 112 | |
| 113 | menu.findItem(CMD_ADVANCED_PANEL).setVisible(mPanelSwitcher != null && |
| 114 | mPanelSwitcher.getCurrentIndex() == BASIC_PANEL); |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean onOptionsItemSelected(MenuItem item) { |
| 121 | switch (item.getItemId()) { |
| 122 | case CMD_CLEAR_HISTORY: |
| 123 | mHistory.clear(); |
| 124 | break; |
| 125 | |
| 126 | case CMD_BASIC_PANEL: |
| 127 | if (mPanelSwitcher != null && |
| 128 | mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL) { |
| 129 | mPanelSwitcher.moveRight(); |
| 130 | } |
| 131 | break; |
| 132 | |
| 133 | case CMD_ADVANCED_PANEL: |
| 134 | if (mPanelSwitcher != null && |
| 135 | mPanelSwitcher.getCurrentIndex() == BASIC_PANEL) { |
| 136 | mPanelSwitcher.moveLeft(); |
| 137 | } |
| 138 | break; |
| 139 | } |
| 140 | return super.onOptionsItemSelected(item); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | protected void onSaveInstanceState(Bundle icicle) { |
| 145 | // as work-around for ClassCastException in TextView on restart |
| 146 | // avoid calling superclass, to keep icicle empty |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void onPause() { |
| 151 | super.onPause(); |
| 152 | mLogic.updateHistory(); |
| 153 | mPersist.save(); |
| 154 | } |
| 155 | |
| 156 | static void log(String message) { |
| 157 | if (LOG_ENABLED) { |
| 158 | Log.v(LOG_TAG, message); |
| 159 | } |
| 160 | } |
| 161 | } |