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; |
Jacek Surazski | 1d31175 | 2009-08-25 17:33:12 +0200 | [diff] [blame] | 23 | import android.util.TypedValue; |
| 24 | import android.view.Display; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 25 | import android.view.Menu; |
| 26 | import android.view.MenuItem; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 27 | import android.view.View; |
Mihai Preda | bcabcbb | 2009-08-05 17:07:31 +0200 | [diff] [blame] | 28 | import android.view.KeyEvent; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 29 | import android.widget.Button; |
Jacek Surazski | 1d31175 | 2009-08-25 17:33:12 +0200 | [diff] [blame] | 30 | import android.widget.TextView; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 31 | |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 32 | public class Calculator extends Activity implements PanelSwitcher.Listener { |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 33 | EventListener mListener = new EventListener(); |
| 34 | private CalculatorDisplay mDisplay; |
| 35 | private Persist mPersist; |
| 36 | private History mHistory; |
| 37 | private Logic mLogic; |
| 38 | private PanelSwitcher mPanelSwitcher; |
| 39 | |
| 40 | private static final int CMD_CLEAR_HISTORY = 1; |
| 41 | private static final int CMD_BASIC_PANEL = 2; |
| 42 | private static final int CMD_ADVANCED_PANEL = 3; |
| 43 | |
Mihai Preda | e08c830 | 2009-09-22 14:53:16 +0200 | [diff] [blame] | 44 | private static final int HVGA_WIDTH_PIXELS = 320; |
Jacek Surazski | 1d31175 | 2009-08-25 17:33:12 +0200 | [diff] [blame] | 45 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 46 | static final int BASIC_PANEL = 0; |
| 47 | static final int ADVANCED_PANEL = 1; |
| 48 | |
| 49 | private static final String LOG_TAG = "Calculator"; |
| 50 | private static final boolean DEBUG = false; |
| 51 | private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; |
Mihai Preda | cf3ca32 | 2010-03-09 12:27:45 +0100 | [diff] [blame] | 52 | private static final String STATE_CURRENT_VIEW = "state-current-view"; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 53 | |
| 54 | @Override |
Mihai Preda | cf3ca32 | 2010-03-09 12:27:45 +0100 | [diff] [blame] | 55 | public void onCreate(Bundle state) { |
| 56 | super.onCreate(state); |
Jeff Hamilton | 0c464d2 | 2009-04-11 01:54:05 -0700 | [diff] [blame] | 57 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 58 | setContentView(R.layout.main); |
| 59 | |
| 60 | mPersist = new Persist(this); |
| 61 | mHistory = mPersist.history; |
| 62 | |
| 63 | mDisplay = (CalculatorDisplay) findViewById(R.id.display); |
| 64 | |
| 65 | mLogic = new Logic(this, mHistory, mDisplay, (Button) findViewById(R.id.equal)); |
| 66 | HistoryAdapter historyAdapter = new HistoryAdapter(this, mHistory, mLogic); |
| 67 | mHistory.setObserver(historyAdapter); |
Mihai Preda | cf3ca32 | 2010-03-09 12:27:45 +0100 | [diff] [blame] | 68 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 69 | mPanelSwitcher = (PanelSwitcher) findViewById(R.id.panelswitch); |
Mihai Preda | cf3ca32 | 2010-03-09 12:27:45 +0100 | [diff] [blame] | 70 | mPanelSwitcher.setCurrentIndex(state==null ? 0 : state.getInt(STATE_CURRENT_VIEW, 0)); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 71 | mPanelSwitcher.setListener(this); |
Mihai Preda | cf3ca32 | 2010-03-09 12:27:45 +0100 | [diff] [blame] | 72 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 73 | mListener.setHandler(mLogic, mPanelSwitcher); |
| 74 | |
| 75 | mDisplay.setOnKeyListener(mListener); |
| 76 | |
Mihai Preda | cf3ca32 | 2010-03-09 12:27:45 +0100 | [diff] [blame] | 77 | View view; |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 78 | if ((view = findViewById(R.id.del)) != null) { |
Jacek Surazski | 1d31175 | 2009-08-25 17:33:12 +0200 | [diff] [blame] | 79 | // view.setOnClickListener(mListener); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 80 | view.setOnLongClickListener(mListener); |
| 81 | } |
| 82 | /* |
| 83 | if ((view = findViewById(R.id.clear)) != null) { |
| 84 | view.setOnClickListener(mListener); |
| 85 | } |
| 86 | */ |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean onCreateOptionsMenu(Menu menu) { |
| 91 | super.onCreateOptionsMenu(menu); |
| 92 | MenuItem item; |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 93 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 94 | item = menu.add(0, CMD_CLEAR_HISTORY, 0, R.string.clear_history); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 95 | item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 96 | item.setIcon(R.drawable.clear_history); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 97 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 98 | item = menu.add(0, CMD_ADVANCED_PANEL, 0, R.string.advanced); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 99 | item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 100 | item.setIcon(R.drawable.advanced); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 101 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 102 | item = menu.add(0, CMD_BASIC_PANEL, 0, R.string.basic); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 103 | item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 104 | item.setIcon(R.drawable.simple); |
| 105 | |
| 106 | return true; |
| 107 | } |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 108 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 109 | @Override |
| 110 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 111 | super.onPrepareOptionsMenu(menu); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 112 | menu.findItem(CMD_BASIC_PANEL).setVisible(mPanelSwitcher != null && |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 113 | mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 114 | |
| 115 | menu.findItem(CMD_ADVANCED_PANEL).setVisible(mPanelSwitcher != null && |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 116 | mPanelSwitcher.getCurrentIndex() == BASIC_PANEL); |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 117 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean onOptionsItemSelected(MenuItem item) { |
| 123 | switch (item.getItemId()) { |
| 124 | case CMD_CLEAR_HISTORY: |
| 125 | mHistory.clear(); |
| 126 | break; |
| 127 | |
| 128 | case CMD_BASIC_PANEL: |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 129 | if (mPanelSwitcher != null && |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 130 | mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL) { |
| 131 | mPanelSwitcher.moveRight(); |
| 132 | } |
| 133 | break; |
| 134 | |
| 135 | case CMD_ADVANCED_PANEL: |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 136 | if (mPanelSwitcher != null && |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 137 | mPanelSwitcher.getCurrentIndex() == BASIC_PANEL) { |
| 138 | mPanelSwitcher.moveLeft(); |
| 139 | } |
| 140 | break; |
| 141 | } |
| 142 | return super.onOptionsItemSelected(item); |
| 143 | } |
| 144 | |
| 145 | @Override |
Mihai Preda | cf3ca32 | 2010-03-09 12:27:45 +0100 | [diff] [blame] | 146 | protected void onSaveInstanceState(Bundle state) { |
| 147 | super.onSaveInstanceState(state); |
| 148 | state.putInt(STATE_CURRENT_VIEW, mPanelSwitcher.getCurrentIndex()); |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onPause() { |
| 153 | super.onPause(); |
| 154 | mLogic.updateHistory(); |
| 155 | mPersist.save(); |
| 156 | } |
| 157 | |
Mihai Preda | bcabcbb | 2009-08-05 17:07:31 +0200 | [diff] [blame] | 158 | @Override |
| 159 | public boolean onKeyDown(int keyCode, KeyEvent keyEvent) { |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 160 | if (keyCode == KeyEvent.KEYCODE_BACK |
Mihai Preda | bcabcbb | 2009-08-05 17:07:31 +0200 | [diff] [blame] | 161 | && mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL) { |
| 162 | mPanelSwitcher.moveRight(); |
| 163 | return true; |
| 164 | } else { |
| 165 | return super.onKeyDown(keyCode, keyEvent); |
| 166 | } |
| 167 | } |
| 168 | |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 169 | static void log(String message) { |
| 170 | if (LOG_ENABLED) { |
| 171 | Log.v(LOG_TAG, message); |
| 172 | } |
| 173 | } |
Jacek Surazski | 1d31175 | 2009-08-25 17:33:12 +0200 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * The font sizes in the layout files are specified for a HVGA display. |
| 177 | * Adjust the font sizes accordingly if we are running on a different |
| 178 | * display. |
| 179 | */ |
| 180 | public void adjustFontSize(TextView view) { |
| 181 | float fontPixelSize = view.getTextSize(); |
| 182 | Display display = getWindowManager().getDefaultDisplay(); |
Mihai Preda | e08c830 | 2009-09-22 14:53:16 +0200 | [diff] [blame] | 183 | int h = Math.min(display.getWidth(), display.getHeight()); |
| 184 | float ratio = (float)h/HVGA_WIDTH_PIXELS; |
Jacek Surazski | 1d31175 | 2009-08-25 17:33:12 +0200 | [diff] [blame] | 185 | view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize*ratio); |
| 186 | } |
Dmitri Plotnikov | 052c92a | 2011-01-10 18:30:42 -0800 | [diff] [blame^] | 187 | |
| 188 | @Override |
| 189 | public void onChange() { |
| 190 | invalidateOptionsMenu(); |
| 191 | } |
The Android Open Source Project | b301ed2 | 2009-03-03 19:32:17 -0800 | [diff] [blame] | 192 | } |