• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util.TypedValue;
24 import android.view.Display;
25 import android.view.Menu;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.view.KeyEvent;
29 import android.widget.Button;
30 import android.widget.TextView;
31 
32 public class Calculator extends Activity {
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 
44     private static final int HVGA_WIDTH_PIXELS  = 320;
45 
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;
52     private static final String STATE_CURRENT_VIEW = "state-current-view";
53 
54     @Override
onCreate(Bundle state)55     public void onCreate(Bundle state) {
56         super.onCreate(state);
57 
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);
68 
69         mPanelSwitcher = (PanelSwitcher) findViewById(R.id.panelswitch);
70         mPanelSwitcher.setCurrentIndex(state==null ? 0 : state.getInt(STATE_CURRENT_VIEW, 0));
71 
72         mListener.setHandler(mLogic, mPanelSwitcher);
73 
74         mDisplay.setOnKeyListener(mListener);
75 
76         View view;
77         if ((view = findViewById(R.id.del)) != null) {
78 //            view.setOnClickListener(mListener);
79             view.setOnLongClickListener(mListener);
80         }
81         /*
82         if ((view = findViewById(R.id.clear)) != null) {
83             view.setOnClickListener(mListener);
84         }
85         */
86     }
87 
88     @Override
onCreateOptionsMenu(Menu menu)89     public boolean onCreateOptionsMenu(Menu menu) {
90         super.onCreateOptionsMenu(menu);
91         MenuItem item;
92 
93         item = menu.add(0, CMD_CLEAR_HISTORY, 0, R.string.clear_history);
94         item.setIcon(R.drawable.clear_history);
95 
96         item = menu.add(0, CMD_ADVANCED_PANEL, 0, R.string.advanced);
97         item.setIcon(R.drawable.advanced);
98 
99         item = menu.add(0, CMD_BASIC_PANEL, 0, R.string.basic);
100         item.setIcon(R.drawable.simple);
101 
102         return true;
103     }
104 
105     @Override
onPrepareOptionsMenu(Menu menu)106     public boolean onPrepareOptionsMenu(Menu menu) {
107         super.onPrepareOptionsMenu(menu);
108         menu.findItem(CMD_BASIC_PANEL).setVisible(mPanelSwitcher != null &&
109                           mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL);
110 
111         menu.findItem(CMD_ADVANCED_PANEL).setVisible(mPanelSwitcher != null &&
112                           mPanelSwitcher.getCurrentIndex() == BASIC_PANEL);
113 
114         return true;
115     }
116 
117     @Override
onOptionsItemSelected(MenuItem item)118     public boolean onOptionsItemSelected(MenuItem item) {
119         switch (item.getItemId()) {
120         case CMD_CLEAR_HISTORY:
121             mHistory.clear();
122             break;
123 
124         case CMD_BASIC_PANEL:
125             if (mPanelSwitcher != null &&
126                 mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL) {
127                 mPanelSwitcher.moveRight();
128             }
129             break;
130 
131         case CMD_ADVANCED_PANEL:
132             if (mPanelSwitcher != null &&
133                 mPanelSwitcher.getCurrentIndex() == BASIC_PANEL) {
134                 mPanelSwitcher.moveLeft();
135             }
136             break;
137         }
138         return super.onOptionsItemSelected(item);
139     }
140 
141     @Override
onSaveInstanceState(Bundle state)142     protected void onSaveInstanceState(Bundle state) {
143         super.onSaveInstanceState(state);
144         state.putInt(STATE_CURRENT_VIEW, mPanelSwitcher.getCurrentIndex());
145     }
146 
147     @Override
onPause()148     public void onPause() {
149         super.onPause();
150         mLogic.updateHistory();
151         mPersist.save();
152     }
153 
154     @Override
onKeyDown(int keyCode, KeyEvent keyEvent)155     public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
156         if (keyCode == KeyEvent.KEYCODE_BACK
157             && mPanelSwitcher.getCurrentIndex() == ADVANCED_PANEL) {
158             mPanelSwitcher.moveRight();
159             return true;
160         } else {
161             return super.onKeyDown(keyCode, keyEvent);
162         }
163     }
164 
log(String message)165     static void log(String message) {
166         if (LOG_ENABLED) {
167             Log.v(LOG_TAG, message);
168         }
169     }
170 
171     /**
172      * The font sizes in the layout files are specified for a HVGA display.
173      * Adjust the font sizes accordingly if we are running on a different
174      * display.
175      */
adjustFontSize(TextView view)176     public void adjustFontSize(TextView view) {
177         float fontPixelSize = view.getTextSize();
178         Display display = getWindowManager().getDefaultDisplay();
179         int h = Math.min(display.getWidth(), display.getHeight());
180         float ratio = (float)h/HVGA_WIDTH_PIXELS;
181         view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize*ratio);
182     }
183 }
184