1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.calculator2;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public class Calculator extends Activity implements PanelSwitcher.Listener, Logic.Listener,
OnClickListener, OnMenuItemClickListener {
EventListener mListener = new EventListener();
private CalculatorDisplay mDisplay;
private Persist mPersist;
private History mHistory;
private Logic mLogic;
private ViewPager mPager;
private View mClearButton;
private View mBackspaceButton;
private View mOverflowMenuButton;
static final int BASIC_PANEL = 0;
static final int ADVANCED_PANEL = 1;
private static final String LOG_TAG = "Calculator";
private static final boolean DEBUG = false;
private static final boolean LOG_ENABLED = false;
private static final String STATE_CURRENT_VIEW = "state-current-view";
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
// Disable IME for this application
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
setContentView(R.layout.main);
mPager = (ViewPager) findViewById(R.id.panelswitch);
if (mPager != null) {
mPager.setAdapter(new PageAdapter(mPager));
} else {
// Single page UI
final TypedArray buttons = getResources().obtainTypedArray(R.array.buttons);
for (int i = 0; i < buttons.length(); i++) {
setOnClickListener(null, buttons.getResourceId(i, 0));
}
buttons.recycle();
}
if (mClearButton == null) {
mClearButton = findViewById(R.id.clear);
mClearButton.setOnClickListener(mListener);
mClearButton.setOnLongClickListener(mListener);
}
if (mBackspaceButton == null) {
mBackspaceButton = findViewById(R.id.del);
mBackspaceButton.setOnClickListener(mListener);
mBackspaceButton.setOnLongClickListener(mListener);
}
mPersist = new Persist(this);
mPersist.load();
mHistory = mPersist.history;
mDisplay = (CalculatorDisplay) findViewById(R.id.display);
mLogic = new Logic(this, mHistory, mDisplay);
mLogic.setListener(this);
mLogic.setDeleteMode(mPersist.getDeleteMode());
mLogic.setLineLength(mDisplay.getMaxDigits());
HistoryAdapter historyAdapter = new HistoryAdapter(this, mHistory, mLogic);
mHistory.setObserver(historyAdapter);
if (mPager != null) {
mPager.setCurrentItem(state == null ? 0 : state.getInt(STATE_CURRENT_VIEW, 0));
}
mListener.setHandler(mLogic, mPager);
mDisplay.setOnKeyListener(mListener);
if (!ViewConfiguration.get(this).hasPermanentMenuKey()) {
createFakeMenu();
}
mLogic.resumeWithHistory();
updateDeleteMode();
}
private void updateDeleteMode() {
if (mLogic.getDeleteMode() == Logic.DELETE_MODE_BACKSPACE) {
mClearButton.setVisibility(View.GONE);
mBackspaceButton.setVisibility(View.VISIBLE);
} else {
mClearButton.setVisibility(View.VISIBLE);
mBackspaceButton.setVisibility(View.GONE);
}
}
void setOnClickListener(View root, int id) {
final View target = root != null ? root.findViewById(id) : findViewById(id);
target.setOnClickListener(mListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.basic).setVisible(getBasicVisibility());
menu.findItem(R.id.advanced).setVisible(getAdvancedVisibility());
return true;
}
private void createFakeMenu() {
mOverflowMenuButton = findViewById(R.id.overflow_menu);
if (mOverflowMenuButton != null) {
mOverflowMenuButton.setVisibility(View.VISIBLE);
mOverflowMenuButton.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.overflow_menu:
PopupMenu menu = constructPopupMenu();
if (menu != null) {
menu.show();
}
break;
}
}
private PopupMenu constructPopupMenu() {
final PopupMenu popupMenu = new PopupMenu(this, mOverflowMenuButton);
final Menu menu = popupMenu.getMenu();
popupMenu.inflate(R.menu.menu);
popupMenu.setOnMenuItemClickListener(this);
onPrepareOptionsMenu(menu);
return popupMenu;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
return onOptionsItemSelected(item);
}
private boolean getBasicVisibility() {
return mPager != null && mPager.getCurrentItem() == ADVANCED_PANEL;
}
private boolean getAdvancedVisibility() {
return mPager != null && mPager.getCurrentItem() == BASIC_PANEL;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.clear_history:
mHistory.clear();
mLogic.onClear();
break;
case R.id.basic:
if (getAdvancedVisibility()) {
mPager.setCurrentItem(BASIC_PANEL);
}
break;
case R.id.advanced:
if (getBasicVisibility()) {
mPager.setCurrentItem(ADVANCED_PANEL);
}
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
if (mPager != null) {
state.putInt(STATE_CURRENT_VIEW, mPager.getCurrentItem());
}
}
@Override
public void onPause() {
super.onPause();
mLogic.updateHistory();
mPersist.setDeleteMode(mLogic.getDeleteMode());
mPersist.save();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK && getAdvancedVisibility()) {
mPager.setCurrentItem(BASIC_PANEL);
return true;
} else {
return super.onKeyDown(keyCode, keyEvent);
}
}
static void log(String message) {
if (LOG_ENABLED) {
Log.v(LOG_TAG, message);
}
}
@Override
public void onChange() {
invalidateOptionsMenu();
}
@Override
public void onDeleteModeChange() {
updateDeleteMode();
}
class PageAdapter extends PagerAdapter {
private View mSimplePage;
private View mAdvancedPage;
public PageAdapter(ViewPager parent) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View simplePage = inflater.inflate(R.layout.simple_pad, parent, false);
final View advancedPage = inflater.inflate(R.layout.advanced_pad, parent, false);
mSimplePage = simplePage;
mAdvancedPage = advancedPage;
final Resources res = getResources();
final TypedArray simpleButtons = res.obtainTypedArray(R.array.simple_buttons);
for (int i = 0; i < simpleButtons.length(); i++) {
setOnClickListener(simplePage, simpleButtons.getResourceId(i, 0));
}
simpleButtons.recycle();
final TypedArray advancedButtons = res.obtainTypedArray(R.array.advanced_buttons);
for (int i = 0; i < advancedButtons.length(); i++) {
setOnClickListener(advancedPage, advancedButtons.getResourceId(i, 0));
}
advancedButtons.recycle();
final View clearButton = simplePage.findViewById(R.id.clear);
if (clearButton != null) {
mClearButton = clearButton;
}
final View backspaceButton = simplePage.findViewById(R.id.del);
if (backspaceButton != null) {
mBackspaceButton = backspaceButton;
}
}
@Override
public int getCount() {
return 2;
}
@Override
public void startUpdate(View container) {
}
@Override
public Object instantiateItem(View container, int position) {
final View page = position == 0 ? mSimplePage : mAdvancedPage;
((ViewGroup) container).addView(page);
return page;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewGroup) container).removeView((View) object);
}
@Override
public void finishUpdate(View container) {
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
}
}
|