blob: 2f68f992527343af54474154afdda7f773f2f282 [file] [log] [blame]
Justin Klaassen4b3af052014-05-27 17:53:10 -07001/*
Justin Klaassen12da1ad2016-04-04 14:20:37 -07002 * Copyright (C) 2016 The Android Open Source Project
Justin Klaassen4b3af052014-05-27 17:53:10 -07003 *
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
Hans Boehm013969e2015-04-13 20:29:47 -070017// TODO: Copy & more general paste in formula? Note that this requires
18// great care: Currently the text version of a displayed formula
19// is not directly useful for re-evaluating the formula later, since
20// it contains ellipses representing subexpressions evaluated with
21// a different degree mode. Rather than supporting copy from the
22// formula window, we may eventually want to support generation of a
23// more useful text version in a separate window. It's not clear
24// this is worth the added (code and user) complexity.
Hans Boehm84614952014-11-25 18:46:17 -080025
Justin Klaassen4b3af052014-05-27 17:53:10 -070026package com.android.calculator2;
27
28import android.animation.Animator;
Justin Klaassen5f2a3342014-06-11 17:40:22 -070029import android.animation.Animator.AnimatorListener;
Justin Klaassen4b3af052014-05-27 17:53:10 -070030import android.animation.AnimatorListenerAdapter;
31import android.animation.AnimatorSet;
Justin Klaassen4b3af052014-05-27 17:53:10 -070032import android.animation.ObjectAnimator;
Justin Klaassen44595162015-05-28 17:55:20 -070033import android.animation.PropertyValuesHolder;
Justin Klaassen9d33cdc2016-02-21 14:16:14 -080034import android.app.ActionBar;
Justin Klaassen4b3af052014-05-27 17:53:10 -070035import android.app.Activity;
Annie Chin06fd3cf2016-11-07 16:04:33 -080036import android.app.FragmentManager;
Annie Chin09547532016-10-14 10:59:07 -070037import android.app.FragmentTransaction;
Justin Klaassenfc5ac822015-06-18 13:15:17 -070038import android.content.ClipData;
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070039import android.content.DialogInterface;
Justin Klaassend36d63e2015-05-05 12:59:36 -070040import android.content.Intent;
Hans Boehmbfe8c222015-04-02 16:26:07 -070041import android.content.res.Resources;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070042import android.graphics.Color;
Justin Klaassen8fff1442014-06-19 10:43:29 -070043import android.graphics.Rect;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070044import android.net.Uri;
Justin Klaassen4b3af052014-05-27 17:53:10 -070045import android.os.Bundle;
Justin Klaassenf79d6f62014-08-26 12:27:08 -070046import android.support.annotation.NonNull;
Chenjie Yu3937b652016-06-01 23:14:26 -070047import android.support.v4.content.ContextCompat;
Justin Klaassen3b4d13d2014-06-06 18:18:37 +010048import android.support.v4.view.ViewPager;
Annie Chine918fd22016-03-09 11:07:54 -080049import android.text.Editable;
Hans Boehm8a4f81c2015-07-09 10:41:25 -070050import android.text.SpannableStringBuilder;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070051import android.text.Spanned;
Annie Chinf360ef02016-03-10 13:45:39 -080052import android.text.TextUtils;
Annie Chine918fd22016-03-09 11:07:54 -080053import android.text.TextWatcher;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070054import android.text.style.ForegroundColorSpan;
Justin Klaassen44595162015-05-28 17:55:20 -070055import android.util.Property;
Annie Chine918fd22016-03-09 11:07:54 -080056import android.view.ActionMode;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070057import android.view.KeyCharacterMap;
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -070058import android.view.KeyEvent;
Hans Boehm84614952014-11-25 18:46:17 -080059import android.view.Menu;
60import android.view.MenuItem;
Annie Chind0f87d22016-10-24 09:04:12 -070061import android.view.MotionEvent;
Justin Klaassen4b3af052014-05-27 17:53:10 -070062import android.view.View;
63import android.view.View.OnLongClickListener;
Justin Klaassen5f2a3342014-06-11 17:40:22 -070064import android.view.ViewAnimationUtils;
Justin Klaassen8fff1442014-06-19 10:43:29 -070065import android.view.ViewGroupOverlay;
Annie Chine918fd22016-03-09 11:07:54 -080066import android.view.ViewTreeObserver;
Justin Klaassen4b3af052014-05-27 17:53:10 -070067import android.view.animation.AccelerateDecelerateInterpolator;
Annie Chind0f87d22016-10-24 09:04:12 -070068import android.widget.FrameLayout;
Annie Chine918fd22016-03-09 11:07:54 -080069import android.widget.HorizontalScrollView;
Justin Klaassenfed941a2014-06-09 18:42:40 +010070import android.widget.TextView;
Justin Klaassend48b7562015-04-16 16:51:38 -070071import android.widget.Toolbar;
Justin Klaassenfed941a2014-06-09 18:42:40 +010072
Christine Franks7452d3a2016-10-27 13:41:18 -070073import com.android.calculator2.CalculatorFormula.OnTextSizeChangeListener;
Hans Boehm84614952014-11-25 18:46:17 -080074
75import java.io.ByteArrayInputStream;
Hans Boehm84614952014-11-25 18:46:17 -080076import java.io.ByteArrayOutputStream;
Hans Boehm84614952014-11-25 18:46:17 -080077import java.io.IOException;
Justin Klaassen721ec842015-05-28 14:30:08 -070078import java.io.ObjectInput;
79import java.io.ObjectInputStream;
80import java.io.ObjectOutput;
81import java.io.ObjectOutputStream;
Christine Franksbd90b792016-11-22 10:28:26 -080082import java.text.DecimalFormatSymbols;
Justin Klaassen4b3af052014-05-27 17:53:10 -070083
Christine Franks1d99be12016-11-14 14:00:36 -080084import static com.android.calculator2.CalculatorFormula.OnFormulaContextMenuClickListener;
85
Hans Boehm8f051c32016-10-03 16:53:58 -070086public class Calculator extends Activity
Christine Franks1d99be12016-11-14 14:00:36 -080087 implements OnTextSizeChangeListener, OnLongClickListener,
Hans Boehm8f051c32016-10-03 16:53:58 -070088 AlertDialogFragment.OnClickListener, Evaluator.EvaluationListener /* for main result */ {
Justin Klaassen2be4fdb2014-08-06 19:54:09 -070089
Annie Chin9a211132016-11-30 12:52:06 -080090 private static final String TAG = "Calculator";
Justin Klaassen2be4fdb2014-08-06 19:54:09 -070091 /**
92 * Constant for an invalid resource id.
93 */
94 public static final int INVALID_RES_ID = -1;
Justin Klaassen4b3af052014-05-27 17:53:10 -070095
96 private enum CalculatorState {
Hans Boehm84614952014-11-25 18:46:17 -080097 INPUT, // Result and formula both visible, no evaluation requested,
98 // Though result may be visible on bottom line.
99 EVALUATE, // Both visible, evaluation requested, evaluation/animation incomplete.
Hans Boehmc1ea0912015-06-19 15:05:07 -0700100 // Not used for instant result evaluation.
Hans Boehm84614952014-11-25 18:46:17 -0800101 INIT, // Very temporary state used as alternative to EVALUATE
102 // during reinitialization. Do not animate on completion.
Hans Boehm31ea2522016-11-23 17:47:02 -0800103 INIT_FOR_RESULT, // Identical to INIT, but evaluation is known to terminate.
Hans Boehm84614952014-11-25 18:46:17 -0800104 ANIMATE, // Result computed, animation to enlarge result window in progress.
105 RESULT, // Result displayed, formula invisible.
106 // If we are in RESULT state, the formula was evaluated without
107 // error to initial precision.
Hans Boehm8f051c32016-10-03 16:53:58 -0700108 // The current formula is now also the last history entry.
Hans Boehm84614952014-11-25 18:46:17 -0800109 ERROR // Error displayed: Formula visible, result shows error message.
110 // Display similar to INPUT state.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700111 }
Hans Boehm84614952014-11-25 18:46:17 -0800112 // Normal transition sequence is
113 // INPUT -> EVALUATE -> ANIMATE -> RESULT (or ERROR) -> INPUT
114 // A RESULT -> ERROR transition is possible in rare corner cases, in which
115 // a higher precision evaluation exposes an error. This is possible, since we
116 // initially evaluate assuming we were given a well-defined problem. If we
117 // were actually asked to compute sqrt(<extremely tiny negative number>) we produce 0
118 // unless we are asked for enough precision that we can distinguish the argument from zero.
Hans Boehm84614952014-11-25 18:46:17 -0800119 // ANIMATE, ERROR, and RESULT are translated to an INIT state if the application
120 // is restarted in that state. This leads us to recompute and redisplay the result
121 // ASAP.
122 // TODO: Possibly save a bit more information, e.g. its initial display string
123 // or most significant digit position, to speed up restart.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700124
Justin Klaassen44595162015-05-28 17:55:20 -0700125 private final Property<TextView, Integer> TEXT_COLOR =
126 new Property<TextView, Integer>(Integer.class, "textColor") {
127 @Override
128 public Integer get(TextView textView) {
129 return textView.getCurrentTextColor();
130 }
131
132 @Override
133 public void set(TextView textView, Integer textColor) {
134 textView.setTextColor(textColor);
135 }
136 };
137
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800138 private static final String NAME = "Calculator";
Hans Boehm84614952014-11-25 18:46:17 -0800139 private static final String KEY_DISPLAY_STATE = NAME + "_display_state";
Hans Boehm760a9dc2015-04-20 10:27:12 -0700140 private static final String KEY_UNPROCESSED_CHARS = NAME + "_unprocessed_chars";
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800141 /**
142 * Associated value is a byte array holding the evaluator state.
143 */
Hans Boehm84614952014-11-25 18:46:17 -0800144 private static final String KEY_EVAL_STATE = NAME + "_eval_state";
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800145 private static final String KEY_INVERSE_MODE = NAME + "_inverse_mode";
Christine Frankseeff27f2016-07-29 12:05:29 -0700146 /**
147 * Associated value is an boolean holding the visibility state of the toolbar.
148 */
149 private static final String KEY_SHOW_TOOLBAR = NAME + "_show_toolbar";
Justin Klaassen741471e2014-06-11 09:43:44 -0700150
Annie Chine918fd22016-03-09 11:07:54 -0800151 private final ViewTreeObserver.OnPreDrawListener mPreDrawListener =
152 new ViewTreeObserver.OnPreDrawListener() {
153 @Override
154 public boolean onPreDraw() {
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700155 mFormulaContainer.scrollTo(mFormulaText.getRight(), 0);
Annie Chine918fd22016-03-09 11:07:54 -0800156 final ViewTreeObserver observer = mFormulaContainer.getViewTreeObserver();
157 if (observer.isAlive()) {
158 observer.removeOnPreDrawListener(this);
159 }
160 return false;
161 }
162 };
163
Christine Franks1d99be12016-11-14 14:00:36 -0800164 public final OnDisplayMemoryOperationsListener mOnDisplayMemoryOperationsListener =
165 new OnDisplayMemoryOperationsListener() {
166 @Override
167 public boolean shouldDisplayMemory() {
168 return mEvaluator.getMemoryIndex() != 0;
169 }
170 };
171
172 public final OnFormulaContextMenuClickListener mOnFormulaContextMenuClickListener =
173 new OnFormulaContextMenuClickListener() {
174 @Override
175 public boolean onPaste(ClipData clip) {
176 final ClipData.Item item = clip.getItemCount() == 0 ? null : clip.getItemAt(0);
177 if (item == null) {
178 // nothing to paste, bail early...
179 return false;
180 }
181
182 // Check if the item is a previously copied result, otherwise paste as raw text.
183 final Uri uri = item.getUri();
184 if (uri != null && mEvaluator.isLastSaved(uri)) {
185 clearIfNotInputState();
186 mEvaluator.appendExpr(mEvaluator.getSavedIndex());
187 redisplayAfterFormulaChange();
188 } else {
189 addChars(item.coerceToText(Calculator.this).toString(), false);
190 }
191 return true;
192 }
193
194 @Override
195 public void onMemoryRecall() {
196 clearIfNotInputState();
197 long memoryIndex = mEvaluator.getMemoryIndex();
198 if (memoryIndex != 0) {
199 mEvaluator.appendExpr(mEvaluator.getMemoryIndex());
200 redisplayAfterFormulaChange();
201 } // FIXME: Avoid the 0 case, e.g. by graying out button when memory is unavailable.
202 }
203 };
204
205
Annie Chine918fd22016-03-09 11:07:54 -0800206 private final TextWatcher mFormulaTextWatcher = new TextWatcher() {
207 @Override
208 public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
209 }
210
211 @Override
212 public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
213 }
214
215 @Override
216 public void afterTextChanged(Editable editable) {
217 final ViewTreeObserver observer = mFormulaContainer.getViewTreeObserver();
218 if (observer.isAlive()) {
219 observer.removeOnPreDrawListener(mPreDrawListener);
220 observer.addOnPreDrawListener(mPreDrawListener);
221 }
222 }
223 };
224
Annie Chin9a211132016-11-30 12:52:06 -0800225 private final DragLayout.CloseCallback mCloseCallback = new DragLayout.CloseCallback() {
226 @Override
227 public void onClose() {
228 popFragmentBackstack();
229 }
230 };
231
Annie Chind0f87d22016-10-24 09:04:12 -0700232 private final DragLayout.DragCallback mDragCallback = new DragLayout.DragCallback() {
233 @Override
Annie Chin9a211132016-11-30 12:52:06 -0800234 public void onStartDraggingOpen() {
Annie Chind0f87d22016-10-24 09:04:12 -0700235 showHistoryFragment(FragmentTransaction.TRANSIT_NONE);
236 }
237
238 @Override
239 public void whileDragging(float yFraction) {
240 // no-op
241 }
242
243 @Override
Annie Chind0f87d22016-10-24 09:04:12 -0700244 public boolean allowDrag(MotionEvent event) {
245 return isViewTarget(mHistoryFrame, event) || isViewTarget(mDisplayView, event);
246 }
247
248 @Override
249 public boolean shouldInterceptTouchEvent(MotionEvent event) {
250 return isViewTarget(mHistoryFrame, event) || isViewTarget(mDisplayView, event);
251 }
252
253 @Override
254 public int getDisplayHeight() {
255 return mDisplayView.getMeasuredHeight();
256 }
257
258 public void onLayout(int translation) {
259 mHistoryFrame.setTranslationY(translation + mDisplayView.getBottom());
260 }
261 };
262
263 private final Rect mHitRect = new Rect();
264
Justin Klaassen4b3af052014-05-27 17:53:10 -0700265 private CalculatorState mCurrentState;
Hans Boehm84614952014-11-25 18:46:17 -0800266 private Evaluator mEvaluator;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700267
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800268 private CalculatorDisplay mDisplayView;
Justin Klaassend48b7562015-04-16 16:51:38 -0700269 private TextView mModeView;
Christine Franks7452d3a2016-10-27 13:41:18 -0700270 private CalculatorFormula mFormulaText;
Justin Klaassen44595162015-05-28 17:55:20 -0700271 private CalculatorResult mResultText;
Annie Chine918fd22016-03-09 11:07:54 -0800272 private HorizontalScrollView mFormulaContainer;
Annie Chin09547532016-10-14 10:59:07 -0700273 private DragLayout mDragLayout;
Annie Chind0f87d22016-10-24 09:04:12 -0700274 private FrameLayout mHistoryFrame;
Justin Klaassend48b7562015-04-16 16:51:38 -0700275
Justin Klaassen3b4d13d2014-06-06 18:18:37 +0100276 private ViewPager mPadViewPager;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700277 private View mDeleteButton;
278 private View mClearButton;
Justin Klaassend48b7562015-04-16 16:51:38 -0700279 private View mEqualButton;
Justin Klaassene2711cb2015-05-28 11:13:17 -0700280
281 private TextView mInverseToggle;
282 private TextView mModeToggle;
283
Justin Klaassen721ec842015-05-28 14:30:08 -0700284 private View[] mInvertibleButtons;
Justin Klaassene2711cb2015-05-28 11:13:17 -0700285 private View[] mInverseButtons;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700286
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700287 private View mCurrentButton;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700288 private Animator mCurrentAnimator;
289
Hans Boehm8a4f81c2015-07-09 10:41:25 -0700290 // Characters that were recently entered at the end of the display that have not yet
291 // been added to the underlying expression.
292 private String mUnprocessedChars = null;
293
294 // Color to highlight unprocessed characters from physical keyboard.
295 // TODO: should probably match this to the error color?
296 private ForegroundColorSpan mUnprocessedColorSpan = new ForegroundColorSpan(Color.RED);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700297
Annie Chin26e159e2016-05-18 15:17:14 -0700298 // Whether the display is one line.
299 private boolean mOneLine;
300
Annie Chin09547532016-10-14 10:59:07 -0700301 private HistoryFragment mHistoryFragment = new HistoryFragment();
302
Hans Boehm31ea2522016-11-23 17:47:02 -0800303 /**
304 * Restore Evaluator state and mCurrentState from savedInstanceState.
305 * Return true if the toolbar should be visible.
306 */
307 private void restoreInstanceState(Bundle savedInstanceState) {
308 final CalculatorState savedState = CalculatorState.values()[
309 savedInstanceState.getInt(KEY_DISPLAY_STATE,
310 CalculatorState.INPUT.ordinal())];
311 setState(savedState);
312 CharSequence unprocessed = savedInstanceState.getCharSequence(KEY_UNPROCESSED_CHARS);
313 if (unprocessed != null) {
314 mUnprocessedChars = unprocessed.toString();
315 }
316 byte[] state = savedInstanceState.getByteArray(KEY_EVAL_STATE);
317 if (state != null) {
318 try (ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(state))) {
319 mEvaluator.restoreInstanceState(in);
320 } catch (Throwable ignored) {
321 // When in doubt, revert to clean state
322 mCurrentState = CalculatorState.INPUT;
323 mEvaluator.clearMain();
324 }
325 }
326 if (savedInstanceState.getBoolean(KEY_SHOW_TOOLBAR, true)) {
327 showAndMaybeHideToolbar();
328 } else {
329 mDisplayView.hideToolbar();
330 }
331 onInverseToggled(savedInstanceState.getBoolean(KEY_INVERSE_MODE));
332 // TODO: We're currently not saving and restoring scroll position.
333 // We probably should. Details may require care to deal with:
334 // - new display size
335 // - slow recomputation if we've scrolled far.
336 }
337
338 private void restoreDisplay() {
339 onModeChanged(mEvaluator.getDegreeMode(Evaluator.MAIN_INDEX));
340 if (mCurrentState != CalculatorState.RESULT
341 && mCurrentState != CalculatorState.INIT_FOR_RESULT) {
342 redisplayFormula();
343 }
344 if (mCurrentState == CalculatorState.INPUT) {
345 // This resultText will explicitly call evaluateAndNotify when ready.
346 mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_EVALUATE, this);
347 } else {
348 // Just reevaluate.
349 setState((mCurrentState == CalculatorState.RESULT
350 || mCurrentState == CalculatorState.INIT_FOR_RESULT) ?
351 CalculatorState.INIT_FOR_RESULT : CalculatorState.INIT);
352 // Request evaluation when we know display width.
353 mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_REQUIRE, this);
354 }
355 }
356
Justin Klaassen4b3af052014-05-27 17:53:10 -0700357 @Override
358 protected void onCreate(Bundle savedInstanceState) {
359 super.onCreate(savedInstanceState);
Hans Boehm31ea2522016-11-23 17:47:02 -0800360
Annie Chin09547532016-10-14 10:59:07 -0700361 setContentView(R.layout.activity_calculator_main);
Justin Klaassend48b7562015-04-16 16:51:38 -0700362 setActionBar((Toolbar) findViewById(R.id.toolbar));
363
364 // Hide all default options in the ActionBar.
365 getActionBar().setDisplayOptions(0);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700366
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800367 // Ensure the toolbar stays visible while the options menu is displayed.
368 getActionBar().addOnMenuVisibilityListener(new ActionBar.OnMenuVisibilityListener() {
369 @Override
370 public void onMenuVisibilityChanged(boolean isVisible) {
371 mDisplayView.setForceToolbarVisible(isVisible);
372 }
373 });
374
375 mDisplayView = (CalculatorDisplay) findViewById(R.id.display);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700376 mModeView = (TextView) findViewById(R.id.mode);
Christine Franks7452d3a2016-10-27 13:41:18 -0700377 mFormulaText = (CalculatorFormula) findViewById(R.id.formula);
Justin Klaassen44595162015-05-28 17:55:20 -0700378 mResultText = (CalculatorResult) findViewById(R.id.result);
Annie Chine918fd22016-03-09 11:07:54 -0800379 mFormulaContainer = (HorizontalScrollView) findViewById(R.id.formula_container);
Hans Boehm31ea2522016-11-23 17:47:02 -0800380 mEvaluator = Evaluator.getInstance(this);
381 mResultText.setEvaluator(mEvaluator, Evaluator.MAIN_INDEX);
382 KeyMaps.setActivity(this);
Justin Klaassend48b7562015-04-16 16:51:38 -0700383
Justin Klaassen3b4d13d2014-06-06 18:18:37 +0100384 mPadViewPager = (ViewPager) findViewById(R.id.pad_pager);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700385 mDeleteButton = findViewById(R.id.del);
386 mClearButton = findViewById(R.id.clr);
Christine Franksbd90b792016-11-22 10:28:26 -0800387 final View numberPad = findViewById(R.id.pad_numeric);
388 mEqualButton = numberPad.findViewById(R.id.eq);
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700389 if (mEqualButton == null || mEqualButton.getVisibility() != View.VISIBLE) {
390 mEqualButton = findViewById(R.id.pad_operator).findViewById(R.id.eq);
391 }
Christine Franksbd90b792016-11-22 10:28:26 -0800392 final TextView decimalPointButton = (TextView) numberPad.findViewById(R.id.dec_point);
393 decimalPointButton.setText(getDecimalSeparator());
Justin Klaassene2711cb2015-05-28 11:13:17 -0700394
395 mInverseToggle = (TextView) findViewById(R.id.toggle_inv);
396 mModeToggle = (TextView) findViewById(R.id.toggle_mode);
397
Annie Chin26e159e2016-05-18 15:17:14 -0700398 mOneLine = mResultText.getVisibility() == View.INVISIBLE;
399
Justin Klaassen721ec842015-05-28 14:30:08 -0700400 mInvertibleButtons = new View[] {
401 findViewById(R.id.fun_sin),
402 findViewById(R.id.fun_cos),
Hans Boehm4db31b42015-05-31 12:19:05 -0700403 findViewById(R.id.fun_tan),
404 findViewById(R.id.fun_ln),
405 findViewById(R.id.fun_log),
406 findViewById(R.id.op_sqrt)
Justin Klaassene2711cb2015-05-28 11:13:17 -0700407 };
408 mInverseButtons = new View[] {
409 findViewById(R.id.fun_arcsin),
410 findViewById(R.id.fun_arccos),
Hans Boehm4db31b42015-05-31 12:19:05 -0700411 findViewById(R.id.fun_arctan),
412 findViewById(R.id.fun_exp),
413 findViewById(R.id.fun_10pow),
414 findViewById(R.id.op_sqr)
Justin Klaassene2711cb2015-05-28 11:13:17 -0700415 };
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700416
Annie Chin09547532016-10-14 10:59:07 -0700417 mDragLayout = (DragLayout) findViewById(R.id.drag_layout);
Annie Chind0f87d22016-10-24 09:04:12 -0700418 mDragLayout.removeDragCallback(mDragCallback);
419 mDragLayout.addDragCallback(mDragCallback);
Annie Chin9a211132016-11-30 12:52:06 -0800420 mDragLayout.setCloseCallback(mCloseCallback);
Annie Chin09547532016-10-14 10:59:07 -0700421
Annie Chind0f87d22016-10-24 09:04:12 -0700422 mHistoryFrame = (FrameLayout) findViewById(R.id.history_frame);
Annie Chin09547532016-10-14 10:59:07 -0700423
Christine Franks1d99be12016-11-14 14:00:36 -0800424 mFormulaText.setOnContextMenuClickListener(mOnFormulaContextMenuClickListener);
425 mFormulaText.setOnDisplayMemoryOperationsListener(mOnDisplayMemoryOperationsListener);
426
Hans Boehm08e8f322015-04-21 13:18:38 -0700427 mFormulaText.setOnTextSizeChangeListener(this);
Annie Chine918fd22016-03-09 11:07:54 -0800428 mFormulaText.addTextChangedListener(mFormulaTextWatcher);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700429 mDeleteButton.setOnLongClickListener(this);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700430
Hans Boehm31ea2522016-11-23 17:47:02 -0800431 if (savedInstanceState != null) {
432 restoreInstanceState(savedInstanceState);
Christine Frankseeff27f2016-07-29 12:05:29 -0700433 } else {
Hans Boehm31ea2522016-11-23 17:47:02 -0800434 mCurrentState = CalculatorState.INPUT;
435 mEvaluator.clearMain();
Christine Frankseeff27f2016-07-29 12:05:29 -0700436 showAndMaybeHideToolbar();
Hans Boehm31ea2522016-11-23 17:47:02 -0800437 onInverseToggled(false);
Christine Frankseeff27f2016-07-29 12:05:29 -0700438 }
Hans Boehm31ea2522016-11-23 17:47:02 -0800439 restoreDisplay();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700440 }
441
442 @Override
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800443 protected void onResume() {
444 super.onResume();
Christine Frankseeff27f2016-07-29 12:05:29 -0700445 if (mDisplayView.isToolbarVisible()) {
446 showAndMaybeHideToolbar();
447 }
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800448 }
449
450 @Override
Justin Klaassenf79d6f62014-08-26 12:27:08 -0700451 protected void onSaveInstanceState(@NonNull Bundle outState) {
Hans Boehm40125442016-01-22 10:35:35 -0800452 mEvaluator.cancelAll(true);
Justin Klaassenf79d6f62014-08-26 12:27:08 -0700453 // If there's an animation in progress, cancel it first to ensure our state is up-to-date.
454 if (mCurrentAnimator != null) {
455 mCurrentAnimator.cancel();
456 }
457
Justin Klaassen4b3af052014-05-27 17:53:10 -0700458 super.onSaveInstanceState(outState);
Hans Boehm84614952014-11-25 18:46:17 -0800459 outState.putInt(KEY_DISPLAY_STATE, mCurrentState.ordinal());
Hans Boehm760a9dc2015-04-20 10:27:12 -0700460 outState.putCharSequence(KEY_UNPROCESSED_CHARS, mUnprocessedChars);
Hans Boehm84614952014-11-25 18:46:17 -0800461 ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
462 try (ObjectOutput out = new ObjectOutputStream(byteArrayStream)) {
463 mEvaluator.saveInstanceState(out);
464 } catch (IOException e) {
465 // Impossible; No IO involved.
466 throw new AssertionError("Impossible IO exception", e);
467 }
468 outState.putByteArray(KEY_EVAL_STATE, byteArrayStream.toByteArray());
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800469 outState.putBoolean(KEY_INVERSE_MODE, mInverseToggle.isSelected());
Christine Frankseeff27f2016-07-29 12:05:29 -0700470 outState.putBoolean(KEY_SHOW_TOOLBAR, mDisplayView.isToolbarVisible());
Justin Klaassen4b3af052014-05-27 17:53:10 -0700471 }
472
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700473 // Set the state, updating delete label and display colors.
474 // This restores display positions on moving to INPUT.
Justin Klaassend48b7562015-04-16 16:51:38 -0700475 // But movement/animation for moving to RESULT has already been done.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700476 private void setState(CalculatorState state) {
477 if (mCurrentState != state) {
Hans Boehm84614952014-11-25 18:46:17 -0800478 if (state == CalculatorState.INPUT) {
Hans Boehmd4959e82016-11-15 18:01:28 -0800479 // We'll explicitly request evaluation from now on.
Hans Boehmbd01e4b2016-11-23 10:12:58 -0800480 mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_NOT_EVALUATE, null);
Hans Boehm84614952014-11-25 18:46:17 -0800481 restoreDisplayPositions();
482 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700483 mCurrentState = state;
484
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700485 if (mCurrentState == CalculatorState.RESULT) {
486 // No longer do this for ERROR; allow mistakes to be corrected.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700487 mDeleteButton.setVisibility(View.GONE);
488 mClearButton.setVisibility(View.VISIBLE);
489 } else {
490 mDeleteButton.setVisibility(View.VISIBLE);
491 mClearButton.setVisibility(View.GONE);
492 }
493
Annie Chin26e159e2016-05-18 15:17:14 -0700494 if (mOneLine) {
495 if (mCurrentState == CalculatorState.RESULT
496 || mCurrentState == CalculatorState.EVALUATE
497 || mCurrentState == CalculatorState.ANIMATE) {
498 mFormulaText.setVisibility(View.VISIBLE);
499 mResultText.setVisibility(View.VISIBLE);
Annie Chin947d93b2016-06-14 10:18:54 -0700500 } else if (mCurrentState == CalculatorState.ERROR) {
501 mFormulaText.setVisibility(View.INVISIBLE);
502 mResultText.setVisibility(View.VISIBLE);
Annie Chin26e159e2016-05-18 15:17:14 -0700503 } else {
504 mFormulaText.setVisibility(View.VISIBLE);
505 mResultText.setVisibility(View.INVISIBLE);
506 }
507 }
508
Hans Boehm84614952014-11-25 18:46:17 -0800509 if (mCurrentState == CalculatorState.ERROR) {
Chenjie Yu3937b652016-06-01 23:14:26 -0700510 final int errorColor =
511 ContextCompat.getColor(this, R.color.calculator_error_color);
Hans Boehm08e8f322015-04-21 13:18:38 -0700512 mFormulaText.setTextColor(errorColor);
Justin Klaassen44595162015-05-28 17:55:20 -0700513 mResultText.setTextColor(errorColor);
Justin Klaassen8fff1442014-06-19 10:43:29 -0700514 getWindow().setStatusBarColor(errorColor);
Justin Klaassen44595162015-05-28 17:55:20 -0700515 } else if (mCurrentState != CalculatorState.RESULT) {
Chenjie Yu3937b652016-06-01 23:14:26 -0700516 mFormulaText.setTextColor(
517 ContextCompat.getColor(this, R.color.display_formula_text_color));
518 mResultText.setTextColor(
519 ContextCompat.getColor(this, R.color.display_result_text_color));
520 getWindow().setStatusBarColor(
521 ContextCompat.getColor(this, R.color.calculator_accent_color));
Justin Klaassen4b3af052014-05-27 17:53:10 -0700522 }
Justin Klaassend48b7562015-04-16 16:51:38 -0700523
524 invalidateOptionsMenu();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700525 }
526 }
527
Annie Chin94c1bd92016-11-23 13:39:56 -0800528 public boolean isResultLayout() {
Hans Boehm31ea2522016-11-23 17:47:02 -0800529 if (mCurrentState == CalculatorState.ANIMATE) {
530 throw new AssertionError("impossible state");
531 }
532 // Note that ERROR has INPUT, not RESULT layout.
533 return mCurrentState == CalculatorState.INIT_FOR_RESULT
534 || mCurrentState == CalculatorState.RESULT;
Annie Chin70ac8ea2016-11-18 14:43:56 -0800535 }
536
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700537 @Override
Annie Chind0f87d22016-10-24 09:04:12 -0700538 protected void onDestroy() {
539 mDragLayout.removeDragCallback(mDragCallback);
540 super.onDestroy();
541 }
542
543 @Override
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700544 public void onActionModeStarted(ActionMode mode) {
545 super.onActionModeStarted(mode);
Christine Franks7452d3a2016-10-27 13:41:18 -0700546 if (mode.getTag() == CalculatorFormula.TAG_ACTION_MODE) {
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700547 mFormulaContainer.scrollTo(mFormulaText.getRight(), 0);
548 }
549 }
550
Chenjie Yu3937b652016-06-01 23:14:26 -0700551 /**
552 * Stop any active ActionMode or ContextMenu for copy/paste actions.
553 * Return true if there was one.
554 */
555 private boolean stopActionModeOrContextMenu() {
Christine Franks7485df52016-12-01 13:18:45 -0800556 return mResultText.stopActionModeOrContextMenu()
557 || mFormulaText.stopActionModeOrContextMenu();
Hans Boehm1176f232015-05-11 16:26:03 -0700558 }
559
Justin Klaassen4b3af052014-05-27 17:53:10 -0700560 @Override
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700561 public void onUserInteraction() {
562 super.onUserInteraction();
563
564 // If there's an animation in progress, end it immediately, so the user interaction can
565 // be handled.
566 if (mCurrentAnimator != null) {
567 mCurrentAnimator.end();
568 }
569 }
570
571 @Override
Justin Klaassen3b4d13d2014-06-06 18:18:37 +0100572 public void onBackPressed() {
Chenjie Yu3937b652016-06-01 23:14:26 -0700573 if (!stopActionModeOrContextMenu()) {
Annie Chin09547532016-10-14 10:59:07 -0700574 if (mDragLayout.isOpen()) {
Christine Franks7485df52016-12-01 13:18:45 -0800575 if (!mHistoryFragment.stopActionModeOrContextMenu()) {
576 mDragLayout.setClosed();
577 popFragmentBackstack();
578 }
Annie Chin09547532016-10-14 10:59:07 -0700579 return;
580 }
Hans Boehm1176f232015-05-11 16:26:03 -0700581 if (mPadViewPager != null && mPadViewPager.getCurrentItem() != 0) {
582 // Select the previous pad.
583 mPadViewPager.setCurrentItem(mPadViewPager.getCurrentItem() - 1);
584 } else {
585 // If the user is currently looking at the first pad (or the pad is not paged),
586 // allow the system to handle the Back button.
587 super.onBackPressed();
588 }
Justin Klaassen3b4d13d2014-06-06 18:18:37 +0100589 }
590 }
591
592 @Override
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700593 public boolean onKeyUp(int keyCode, KeyEvent event) {
Justin Klaassen83959da2016-04-06 11:55:24 -0700594 // Allow the system to handle special key codes (e.g. "BACK" or "DPAD").
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700595 switch (keyCode) {
Justin Klaassen83959da2016-04-06 11:55:24 -0700596 case KeyEvent.KEYCODE_BACK:
Christine Franksf9ba2202016-10-20 17:20:19 -0700597 case KeyEvent.KEYCODE_ESCAPE:
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700598 case KeyEvent.KEYCODE_DPAD_UP:
599 case KeyEvent.KEYCODE_DPAD_DOWN:
600 case KeyEvent.KEYCODE_DPAD_LEFT:
601 case KeyEvent.KEYCODE_DPAD_RIGHT:
602 return super.onKeyUp(keyCode, event);
603 }
604
Chenjie Yu3937b652016-06-01 23:14:26 -0700605 // Stop the action mode or context menu if it's showing.
606 stopActionModeOrContextMenu();
Justin Klaassend12e0622016-04-27 16:26:47 -0700607
Hans Boehmced295e2016-11-17 17:30:13 -0800608 // Always cancel unrequested in-progress evaluation of the main expression, so that
609 // we don't have to worry about subsequent asynchronous completion.
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700610 // Requested in-progress evaluations are handled below.
Hans Boehm31ea2522016-11-23 17:47:02 -0800611 cancelUnrequested();
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700612
613 switch (keyCode) {
614 case KeyEvent.KEYCODE_NUMPAD_ENTER:
615 case KeyEvent.KEYCODE_ENTER:
616 case KeyEvent.KEYCODE_DPAD_CENTER:
617 mCurrentButton = mEqualButton;
618 onEquals();
619 return true;
620 case KeyEvent.KEYCODE_DEL:
621 mCurrentButton = mDeleteButton;
622 onDelete();
623 return true;
Annie Chin56bcbf12016-09-23 17:04:22 -0700624 case KeyEvent.KEYCODE_CLEAR:
625 mCurrentButton = mClearButton;
626 onClear();
627 return true;
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700628 default:
629 cancelIfEvaluating(false);
630 final int raw = event.getKeyCharacterMap().get(keyCode, event.getMetaState());
631 if ((raw & KeyCharacterMap.COMBINING_ACCENT) != 0) {
632 return true; // discard
633 }
634 // Try to discard non-printing characters and the like.
635 // The user will have to explicitly delete other junk that gets past us.
636 if (Character.isIdentifierIgnorable(raw) || Character.isWhitespace(raw)) {
637 return true;
638 }
639 char c = (char) raw;
640 if (c == '=') {
641 mCurrentButton = mEqualButton;
642 onEquals();
643 } else {
644 addChars(String.valueOf(c), true);
645 redisplayAfterFormulaChange();
646 }
647 return true;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700648 }
649 }
650
Justin Klaassene2711cb2015-05-28 11:13:17 -0700651 /**
652 * Invoked whenever the inverse button is toggled to update the UI.
653 *
654 * @param showInverse {@code true} if inverse functions should be shown
655 */
656 private void onInverseToggled(boolean showInverse) {
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800657 mInverseToggle.setSelected(showInverse);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700658 if (showInverse) {
659 mInverseToggle.setContentDescription(getString(R.string.desc_inv_on));
Justin Klaassen721ec842015-05-28 14:30:08 -0700660 for (View invertibleButton : mInvertibleButtons) {
661 invertibleButton.setVisibility(View.GONE);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700662 }
663 for (View inverseButton : mInverseButtons) {
664 inverseButton.setVisibility(View.VISIBLE);
665 }
666 } else {
667 mInverseToggle.setContentDescription(getString(R.string.desc_inv_off));
Justin Klaassen721ec842015-05-28 14:30:08 -0700668 for (View invertibleButton : mInvertibleButtons) {
669 invertibleButton.setVisibility(View.VISIBLE);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700670 }
671 for (View inverseButton : mInverseButtons) {
672 inverseButton.setVisibility(View.GONE);
673 }
674 }
675 }
676
677 /**
Christine Frankseeff27f2016-07-29 12:05:29 -0700678 * Invoked whenever the deg/rad mode may have changed to update the UI. Note that the mode has
679 * not necessarily actually changed where this is invoked.
Justin Klaassene2711cb2015-05-28 11:13:17 -0700680 *
681 * @param degreeMode {@code true} if in degree mode
682 */
683 private void onModeChanged(boolean degreeMode) {
684 if (degreeMode) {
Justin Klaassend48b7562015-04-16 16:51:38 -0700685 mModeView.setText(R.string.mode_deg);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700686 mModeView.setContentDescription(getString(R.string.desc_mode_deg));
687
688 mModeToggle.setText(R.string.mode_rad);
689 mModeToggle.setContentDescription(getString(R.string.desc_switch_rad));
Hans Boehmbfe8c222015-04-02 16:26:07 -0700690 } else {
Justin Klaassend48b7562015-04-16 16:51:38 -0700691 mModeView.setText(R.string.mode_rad);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700692 mModeView.setContentDescription(getString(R.string.desc_mode_rad));
693
694 mModeToggle.setText(R.string.mode_deg);
695 mModeToggle.setContentDescription(getString(R.string.desc_switch_deg));
Hans Boehmbfe8c222015-04-02 16:26:07 -0700696 }
697 }
Hans Boehm84614952014-11-25 18:46:17 -0800698
Annie Chin06fd3cf2016-11-07 16:04:33 -0800699 private void popFragmentBackstack() {
700 final FragmentManager manager = getFragmentManager();
701 if (manager == null || manager.isDestroyed()) {
702 return;
703 }
704 manager.popBackStack();
Annie Chin9a211132016-11-30 12:52:06 -0800705 manager.executePendingTransactions();
Annie Chin06fd3cf2016-11-07 16:04:33 -0800706 }
Annie Chin9a211132016-11-30 12:52:06 -0800707
Hans Boehm5d79d102015-09-16 16:33:47 -0700708 /**
709 * Switch to INPUT from RESULT state in response to input of the specified button_id.
710 * View.NO_ID is treated as an incomplete function id.
711 */
712 private void switchToInput(int button_id) {
713 if (KeyMaps.isBinary(button_id) || KeyMaps.isSuffix(button_id)) {
Hans Boehm8f051c32016-10-03 16:53:58 -0700714 mEvaluator.collapse(mEvaluator.getMaxIndex() /* Most recent history entry */);
Hans Boehm5d79d102015-09-16 16:33:47 -0700715 } else {
716 announceClearedForAccessibility();
Hans Boehm8f051c32016-10-03 16:53:58 -0700717 mEvaluator.clearMain();
Hans Boehm5d79d102015-09-16 16:33:47 -0700718 }
719 setState(CalculatorState.INPUT);
720 }
721
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700722 // Add the given button id to input expression.
723 // If appropriate, clear the expression before doing so.
724 private void addKeyToExpr(int id) {
725 if (mCurrentState == CalculatorState.ERROR) {
726 setState(CalculatorState.INPUT);
727 } else if (mCurrentState == CalculatorState.RESULT) {
Hans Boehm5d79d102015-09-16 16:33:47 -0700728 switchToInput(id);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700729 }
730 if (!mEvaluator.append(id)) {
731 // TODO: Some user visible feedback?
732 }
733 }
734
Hans Boehm017de982015-06-10 17:46:03 -0700735 /**
736 * Add the given button id to input expression, assuming it was explicitly
737 * typed/touched.
738 * We perform slightly more aggressive correction than in pasted expressions.
739 */
740 private void addExplicitKeyToExpr(int id) {
741 if (mCurrentState == CalculatorState.INPUT && id == R.id.op_sub) {
Hans Boehm8f051c32016-10-03 16:53:58 -0700742 mEvaluator.getExpr(Evaluator.MAIN_INDEX).removeTrailingAdditiveOperators();
Hans Boehm017de982015-06-10 17:46:03 -0700743 }
744 addKeyToExpr(id);
745 }
746
Hans Boehmbd01e4b2016-11-23 10:12:58 -0800747 public void evaluateInstantIfNecessary() {
748 if (mCurrentState == CalculatorState.INPUT
749 && mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasInterestingOps()) {
750 mEvaluator.evaluateAndNotify(Evaluator.MAIN_INDEX, this, mResultText);
751 }
752 }
753
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700754 private void redisplayAfterFormulaChange() {
755 // TODO: Could do this more incrementally.
756 redisplayFormula();
757 setState(CalculatorState.INPUT);
Hans Boehm8f051c32016-10-03 16:53:58 -0700758 mResultText.clear();
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800759 if (haveUnprocessed()) {
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800760 // Force reevaluation when text is deleted, even if expression is unchanged.
761 mEvaluator.touch();
762 } else {
Hans Boehmbd01e4b2016-11-23 10:12:58 -0800763 evaluateInstantIfNecessary();
Hans Boehmc023b732015-04-29 11:30:47 -0700764 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700765 }
766
Hans Boehm52d477a2016-04-01 17:42:50 -0700767 /**
768 * Show the toolbar.
769 * Automatically hide it again if it's not relevant to current formula.
770 */
771 private void showAndMaybeHideToolbar() {
772 final boolean shouldBeVisible =
773 mCurrentState == CalculatorState.INPUT && mEvaluator.hasTrigFuncs();
774 mDisplayView.showToolbar(!shouldBeVisible);
775 }
776
777 /**
778 * Display or hide the toolbar depending on calculator state.
779 */
780 private void showOrHideToolbar() {
781 final boolean shouldBeVisible =
782 mCurrentState == CalculatorState.INPUT && mEvaluator.hasTrigFuncs();
783 if (shouldBeVisible) {
784 mDisplayView.showToolbar(false);
785 } else {
786 mDisplayView.hideToolbar();
787 }
788 }
789
Justin Klaassen4b3af052014-05-27 17:53:10 -0700790 public void onButtonClick(View view) {
Hans Boehmc1ea0912015-06-19 15:05:07 -0700791 // Any animation is ended before we get here.
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700792 mCurrentButton = view;
Chenjie Yu3937b652016-06-01 23:14:26 -0700793 stopActionModeOrContextMenu();
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800794
Hans Boehmc1ea0912015-06-19 15:05:07 -0700795 // See onKey above for the rationale behind some of the behavior below:
Hans Boehm31ea2522016-11-23 17:47:02 -0800796 cancelUnrequested();
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800797
Justin Klaassend48b7562015-04-16 16:51:38 -0700798 final int id = view.getId();
Hans Boehm84614952014-11-25 18:46:17 -0800799 switch (id) {
Justin Klaassen4b3af052014-05-27 17:53:10 -0700800 case R.id.eq:
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700801 onEquals();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700802 break;
803 case R.id.del:
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700804 onDelete();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700805 break;
806 case R.id.clr:
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700807 onClear();
Hans Boehm52d477a2016-04-01 17:42:50 -0700808 return; // Toolbar visibility adjusted at end of animation.
Justin Klaassene2711cb2015-05-28 11:13:17 -0700809 case R.id.toggle_inv:
810 final boolean selected = !mInverseToggle.isSelected();
811 mInverseToggle.setSelected(selected);
812 onInverseToggled(selected);
Hans Boehmc1ea0912015-06-19 15:05:07 -0700813 if (mCurrentState == CalculatorState.RESULT) {
814 mResultText.redisplay(); // In case we cancelled reevaluation.
815 }
Justin Klaassene2711cb2015-05-28 11:13:17 -0700816 break;
817 case R.id.toggle_mode:
Hans Boehmc1ea0912015-06-19 15:05:07 -0700818 cancelIfEvaluating(false);
Hans Boehm8f051c32016-10-03 16:53:58 -0700819 final boolean mode = !mEvaluator.getDegreeMode(Evaluator.MAIN_INDEX);
820 if (mCurrentState == CalculatorState.RESULT
821 && mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrigFuncs()) {
822 // Capture current result evaluated in old mode.
823 mEvaluator.collapse(mEvaluator.getMaxIndex());
Hans Boehmbfe8c222015-04-02 16:26:07 -0700824 redisplayFormula();
825 }
826 // In input mode, we reinterpret already entered trig functions.
827 mEvaluator.setDegreeMode(mode);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700828 onModeChanged(mode);
Christine Frankseeff27f2016-07-29 12:05:29 -0700829 // Show the toolbar to highlight the mode change.
830 showAndMaybeHideToolbar();
Hans Boehmbfe8c222015-04-02 16:26:07 -0700831 setState(CalculatorState.INPUT);
Justin Klaassen44595162015-05-28 17:55:20 -0700832 mResultText.clear();
Hans Boehmbd01e4b2016-11-23 10:12:58 -0800833 if (!haveUnprocessed()) {
834 evaluateInstantIfNecessary();
Hans Boehmc023b732015-04-29 11:30:47 -0700835 }
Christine Frankseeff27f2016-07-29 12:05:29 -0700836 return;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700837 default:
Hans Boehmc1ea0912015-06-19 15:05:07 -0700838 cancelIfEvaluating(false);
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800839 if (haveUnprocessed()) {
840 // For consistency, append as uninterpreted characters.
841 // This may actually be useful for a left parenthesis.
842 addChars(KeyMaps.toString(this, id), true);
843 } else {
844 addExplicitKeyToExpr(id);
845 redisplayAfterFormulaChange();
846 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700847 break;
848 }
Hans Boehm52d477a2016-04-01 17:42:50 -0700849 showOrHideToolbar();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700850 }
851
Hans Boehm84614952014-11-25 18:46:17 -0800852 void redisplayFormula() {
Hans Boehm8f051c32016-10-03 16:53:58 -0700853 SpannableStringBuilder formula
854 = mEvaluator.getExpr(Evaluator.MAIN_INDEX).toSpannableStringBuilder(this);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700855 if (mUnprocessedChars != null) {
856 // Add and highlight characters we couldn't process.
Hans Boehm8a4f81c2015-07-09 10:41:25 -0700857 formula.append(mUnprocessedChars, mUnprocessedColorSpan,
858 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700859 }
Hans Boehm8a4f81c2015-07-09 10:41:25 -0700860 mFormulaText.changeTextTo(formula);
Annie Chinf360ef02016-03-10 13:45:39 -0800861 mFormulaText.setContentDescription(TextUtils.isEmpty(formula)
Justin Klaassend1831412016-07-19 21:59:10 -0700862 ? getString(R.string.desc_formula) : null);
Hans Boehm84614952014-11-25 18:46:17 -0800863 }
864
Justin Klaassen4b3af052014-05-27 17:53:10 -0700865 @Override
866 public boolean onLongClick(View view) {
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700867 mCurrentButton = view;
868
Justin Klaassen4b3af052014-05-27 17:53:10 -0700869 if (view.getId() == R.id.del) {
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700870 onClear();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700871 return true;
872 }
873 return false;
874 }
875
Hans Boehm84614952014-11-25 18:46:17 -0800876 // Initial evaluation completed successfully. Initiate display.
Hans Boehm8f051c32016-10-03 16:53:58 -0700877 public void onEvaluate(long index, int initDisplayPrec, int msd, int leastDigPos,
Hans Boehma0e45f32015-05-30 13:20:35 -0700878 String truncatedWholeNumber) {
Hans Boehm8f051c32016-10-03 16:53:58 -0700879 if (index != Evaluator.MAIN_INDEX) {
880 throw new AssertionError("Unexpected evaluation result index\n");
881 }
Annie Chin37c33b62016-11-22 14:46:28 -0800882
Justin Klaassend48b7562015-04-16 16:51:38 -0700883 // Invalidate any options that may depend on the current result.
884 invalidateOptionsMenu();
885
Hans Boehm8f051c32016-10-03 16:53:58 -0700886 mResultText.onEvaluate(index, initDisplayPrec, msd, leastDigPos, truncatedWholeNumber);
Hans Boehm31ea2522016-11-23 17:47:02 -0800887 if (mCurrentState != CalculatorState.INPUT) {
888 // In EVALUATE, INIT, or INIT_FOR_RESULT state.
889 onResult(mCurrentState == CalculatorState.EVALUATE);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700890 }
Hans Boehm84614952014-11-25 18:46:17 -0800891 }
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700892
Hans Boehmc1ea0912015-06-19 15:05:07 -0700893 // Reset state to reflect evaluator cancellation. Invoked by evaluator.
Hans Boehm8f051c32016-10-03 16:53:58 -0700894 public void onCancelled(long index) {
895 // Index is Evaluator.MAIN_INDEX. We should be in EVALUATE state.
Hans Boehm84614952014-11-25 18:46:17 -0800896 setState(CalculatorState.INPUT);
Hans Boehm8f051c32016-10-03 16:53:58 -0700897 mResultText.onCancelled(index);
Hans Boehm84614952014-11-25 18:46:17 -0800898 }
899
900 // Reevaluation completed; ask result to redisplay current value.
Hans Boehm8f051c32016-10-03 16:53:58 -0700901 public void onReevaluate(long index)
Hans Boehm84614952014-11-25 18:46:17 -0800902 {
Hans Boehm8f051c32016-10-03 16:53:58 -0700903 // Index is Evaluator.MAIN_INDEX.
904 mResultText.onReevaluate(index);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700905 }
906
Justin Klaassenfed941a2014-06-09 18:42:40 +0100907 @Override
908 public void onTextSizeChanged(final TextView textView, float oldSize) {
909 if (mCurrentState != CalculatorState.INPUT) {
910 // Only animate text changes that occur from user input.
911 return;
912 }
913
914 // Calculate the values needed to perform the scale and translation animations,
915 // maintaining the same apparent baseline for the displayed text.
916 final float textScale = oldSize / textView.getTextSize();
917 final float translationX = (1.0f - textScale) *
918 (textView.getWidth() / 2.0f - textView.getPaddingEnd());
919 final float translationY = (1.0f - textScale) *
920 (textView.getHeight() / 2.0f - textView.getPaddingBottom());
921
922 final AnimatorSet animatorSet = new AnimatorSet();
923 animatorSet.playTogether(
924 ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f),
925 ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f),
926 ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f),
927 ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f));
Justin Klaassen94db7202014-06-11 11:22:31 -0700928 animatorSet.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
Justin Klaassenfed941a2014-06-09 18:42:40 +0100929 animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
930 animatorSet.start();
931 }
932
Hans Boehmc1ea0912015-06-19 15:05:07 -0700933 /**
934 * Cancel any in-progress explicitly requested evaluations.
935 * @param quiet suppress pop-up message. Explicit evaluation can change the expression
936 value, and certainly changes the display, so it seems reasonable to warn.
937 * @return true if there was such an evaluation
938 */
939 private boolean cancelIfEvaluating(boolean quiet) {
940 if (mCurrentState == CalculatorState.EVALUATE) {
Hans Boehm31ea2522016-11-23 17:47:02 -0800941 mEvaluator.cancel(Evaluator.MAIN_INDEX, quiet);
Hans Boehmc1ea0912015-06-19 15:05:07 -0700942 return true;
943 } else {
944 return false;
945 }
946 }
947
Hans Boehm31ea2522016-11-23 17:47:02 -0800948
949 private void cancelUnrequested() {
950 if (mCurrentState == CalculatorState.INPUT) {
951 mEvaluator.cancel(Evaluator.MAIN_INDEX, true);
952 }
953 }
954
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800955 private boolean haveUnprocessed() {
956 return mUnprocessedChars != null && !mUnprocessedChars.isEmpty();
957 }
958
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700959 private void onEquals() {
Hans Boehm56d6e762016-06-06 11:46:29 -0700960 // Ignore if in non-INPUT state, or if there are no operators.
Justin Klaassena8075af2016-07-27 15:24:45 -0700961 if (mCurrentState == CalculatorState.INPUT) {
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800962 if (haveUnprocessed()) {
Justin Klaassena8075af2016-07-27 15:24:45 -0700963 setState(CalculatorState.EVALUATE);
Hans Boehm8f051c32016-10-03 16:53:58 -0700964 onError(Evaluator.MAIN_INDEX, R.string.error_syntax);
965 } else if (mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasInterestingOps()) {
Justin Klaassena8075af2016-07-27 15:24:45 -0700966 setState(CalculatorState.EVALUATE);
Hans Boehm8f051c32016-10-03 16:53:58 -0700967 mEvaluator.requireResult(Evaluator.MAIN_INDEX, this, mResultText);
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800968 }
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700969 }
970 }
971
972 private void onDelete() {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700973 // Delete works like backspace; remove the last character or operator from the expression.
974 // Note that we handle keyboard delete exactly like the delete button. For
975 // example the delete button can be used to delete a character from an incomplete
976 // function name typed on a physical keyboard.
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700977 // This should be impossible in RESULT state.
Hans Boehmc1ea0912015-06-19 15:05:07 -0700978 // If there is an in-progress explicit evaluation, just cancel it and return.
979 if (cancelIfEvaluating(false)) return;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700980 setState(CalculatorState.INPUT);
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800981 if (haveUnprocessed()) {
982 mUnprocessedChars = mUnprocessedChars.substring(0, mUnprocessedChars.length() - 1);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700983 } else {
Hans Boehmc023b732015-04-29 11:30:47 -0700984 mEvaluator.delete();
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700985 }
Hans Boehm8f051c32016-10-03 16:53:58 -0700986 if (mEvaluator.getExpr(Evaluator.MAIN_INDEX).isEmpty() && !haveUnprocessed()) {
Hans Boehmdb6f9992015-08-19 12:32:56 -0700987 // Resulting formula won't be announced, since it's empty.
988 announceClearedForAccessibility();
989 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700990 redisplayAfterFormulaChange();
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700991 }
992
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700993 private void reveal(View sourceView, int colorRes, AnimatorListener listener) {
Justin Klaassen06360f92014-08-28 11:08:44 -0700994 final ViewGroupOverlay groupOverlay =
995 (ViewGroupOverlay) getWindow().getDecorView().getOverlay();
Justin Klaassen8fff1442014-06-19 10:43:29 -0700996
997 final Rect displayRect = new Rect();
Justin Klaassen06360f92014-08-28 11:08:44 -0700998 mDisplayView.getGlobalVisibleRect(displayRect);
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700999
1000 // Make reveal cover the display and status bar.
1001 final View revealView = new View(this);
Justin Klaassen8fff1442014-06-19 10:43:29 -07001002 revealView.setBottom(displayRect.bottom);
1003 revealView.setLeft(displayRect.left);
1004 revealView.setRight(displayRect.right);
Chenjie Yu3937b652016-06-01 23:14:26 -07001005 revealView.setBackgroundColor(ContextCompat.getColor(this, colorRes));
Justin Klaassen06360f92014-08-28 11:08:44 -07001006 groupOverlay.add(revealView);
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001007
Justin Klaassen4b3af052014-05-27 17:53:10 -07001008 final int[] clearLocation = new int[2];
1009 sourceView.getLocationInWindow(clearLocation);
1010 clearLocation[0] += sourceView.getWidth() / 2;
1011 clearLocation[1] += sourceView.getHeight() / 2;
1012
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001013 final int revealCenterX = clearLocation[0] - revealView.getLeft();
1014 final int revealCenterY = clearLocation[1] - revealView.getTop();
Justin Klaassen4b3af052014-05-27 17:53:10 -07001015
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001016 final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2);
1017 final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2);
1018 final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2);
Justin Klaassen4b3af052014-05-27 17:53:10 -07001019 final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2));
1020
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001021 final Animator revealAnimator =
1022 ViewAnimationUtils.createCircularReveal(revealView,
ztenghui3d6ecaf2014-06-05 09:56:00 -07001023 revealCenterX, revealCenterY, 0.0f, revealRadius);
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001024 revealAnimator.setDuration(
Justin Klaassen4b3af052014-05-27 17:53:10 -07001025 getResources().getInteger(android.R.integer.config_longAnimTime));
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001026 revealAnimator.addListener(listener);
Justin Klaassen4b3af052014-05-27 17:53:10 -07001027
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001028 final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f);
Justin Klaassen4b3af052014-05-27 17:53:10 -07001029 alphaAnimator.setDuration(
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001030 getResources().getInteger(android.R.integer.config_mediumAnimTime));
Justin Klaassen4b3af052014-05-27 17:53:10 -07001031
1032 final AnimatorSet animatorSet = new AnimatorSet();
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001033 animatorSet.play(revealAnimator).before(alphaAnimator);
Justin Klaassen4b3af052014-05-27 17:53:10 -07001034 animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
1035 animatorSet.addListener(new AnimatorListenerAdapter() {
1036 @Override
Justin Klaassen4b3af052014-05-27 17:53:10 -07001037 public void onAnimationEnd(Animator animator) {
Justin Klaassen8fff1442014-06-19 10:43:29 -07001038 groupOverlay.remove(revealView);
Justin Klaassen4b3af052014-05-27 17:53:10 -07001039 mCurrentAnimator = null;
1040 }
1041 });
1042
1043 mCurrentAnimator = animatorSet;
1044 animatorSet.start();
1045 }
1046
Hans Boehmdb6f9992015-08-19 12:32:56 -07001047 private void announceClearedForAccessibility() {
1048 mResultText.announceForAccessibility(getResources().getString(R.string.cleared));
Hans Boehmccc55662015-07-07 14:16:59 -07001049 }
1050
Hans Boehm9db3ee22016-11-18 10:09:47 -08001051 public void onClearAnimationEnd() {
1052 mUnprocessedChars = null;
1053 mResultText.clear();
1054 mEvaluator.clearMain();
1055 setState(CalculatorState.INPUT);
1056 redisplayFormula();
1057 }
1058
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001059 private void onClear() {
Hans Boehm8f051c32016-10-03 16:53:58 -07001060 if (mEvaluator.getExpr(Evaluator.MAIN_INDEX).isEmpty() && !haveUnprocessed()) {
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001061 return;
1062 }
Hans Boehmc1ea0912015-06-19 15:05:07 -07001063 cancelIfEvaluating(true);
Hans Boehmdb6f9992015-08-19 12:32:56 -07001064 announceClearedForAccessibility();
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001065 reveal(mCurrentButton, R.color.calculator_accent_color, new AnimatorListenerAdapter() {
1066 @Override
1067 public void onAnimationEnd(Animator animation) {
Hans Boehm9db3ee22016-11-18 10:09:47 -08001068 onClearAnimationEnd();
Hans Boehm52d477a2016-04-01 17:42:50 -07001069 showOrHideToolbar();
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001070 }
1071 });
1072 }
1073
Hans Boehm84614952014-11-25 18:46:17 -08001074 // Evaluation encountered en error. Display the error.
Hans Boehm8f051c32016-10-03 16:53:58 -07001075 @Override
1076 public void onError(final long index, final int errorResourceId) {
1077 if (index != Evaluator.MAIN_INDEX) {
1078 throw new AssertionError("Unexpected error source");
1079 }
Hans Boehmfbcef702015-04-27 18:07:47 -07001080 if (mCurrentState == CalculatorState.EVALUATE) {
1081 setState(CalculatorState.ANIMATE);
Hans Boehmccc55662015-07-07 14:16:59 -07001082 mResultText.announceForAccessibility(getResources().getString(errorResourceId));
Hans Boehmfbcef702015-04-27 18:07:47 -07001083 reveal(mCurrentButton, R.color.calculator_error_color,
1084 new AnimatorListenerAdapter() {
1085 @Override
1086 public void onAnimationEnd(Animator animation) {
1087 setState(CalculatorState.ERROR);
Hans Boehm8f051c32016-10-03 16:53:58 -07001088 mResultText.onError(index, errorResourceId);
Hans Boehmfbcef702015-04-27 18:07:47 -07001089 }
1090 });
Hans Boehm31ea2522016-11-23 17:47:02 -08001091 } else if (mCurrentState == CalculatorState.INIT
1092 || mCurrentState == CalculatorState.INIT_FOR_RESULT /* very unlikely */) {
Hans Boehmfbcef702015-04-27 18:07:47 -07001093 setState(CalculatorState.ERROR);
Hans Boehm8f051c32016-10-03 16:53:58 -07001094 mResultText.onError(index, errorResourceId);
Hans Boehmc023b732015-04-29 11:30:47 -07001095 } else {
Justin Klaassen44595162015-05-28 17:55:20 -07001096 mResultText.clear();
Justin Klaassen2be4fdb2014-08-06 19:54:09 -07001097 }
Justin Klaassen5f2a3342014-06-11 17:40:22 -07001098 }
1099
Hans Boehm84614952014-11-25 18:46:17 -08001100 // Animate movement of result into the top formula slot.
1101 // Result window now remains translated in the top slot while the result is displayed.
1102 // (We convert it back to formula use only when the user provides new input.)
Justin Klaassen44595162015-05-28 17:55:20 -07001103 // Historical note: In the Lollipop version, this invisibly and instantaneously moved
Hans Boehm84614952014-11-25 18:46:17 -08001104 // formula and result displays back at the end of the animation. We no longer do that,
1105 // so that we can continue to properly support scrolling of the result.
1106 // We assume the result already contains the text to be expanded.
1107 private void onResult(boolean animate) {
Justin Klaassen44595162015-05-28 17:55:20 -07001108 // Calculate the textSize that would be used to display the result in the formula.
1109 // For scrollable results just use the minimum textSize to maximize the number of digits
1110 // that are visible on screen.
1111 float textSize = mFormulaText.getMinimumTextSize();
1112 if (!mResultText.isScrollable()) {
1113 textSize = mFormulaText.getVariableTextSize(mResultText.getText().toString());
1114 }
1115
1116 // Scale the result to match the calculated textSize, minimizing the jump-cut transition
1117 // when a result is reused in a subsequent expression.
1118 final float resultScale = textSize / mResultText.getTextSize();
1119
1120 // Set the result's pivot to match its gravity.
1121 mResultText.setPivotX(mResultText.getWidth() - mResultText.getPaddingRight());
1122 mResultText.setPivotY(mResultText.getHeight() - mResultText.getPaddingBottom());
1123
1124 // Calculate the necessary translations so the result takes the place of the formula and
1125 // the formula moves off the top of the screen.
Annie Chin28589dc2016-06-09 17:50:51 -07001126 final float resultTranslationY = (mFormulaContainer.getBottom() - mResultText.getBottom())
1127 - (mFormulaText.getPaddingBottom() - mResultText.getPaddingBottom());
1128 float formulaTranslationY = -mFormulaContainer.getBottom();
Annie Chin26e159e2016-05-18 15:17:14 -07001129 if (mOneLine) {
1130 // Position the result text.
1131 mResultText.setY(mResultText.getBottom());
Annie Chin28589dc2016-06-09 17:50:51 -07001132 formulaTranslationY = -(findViewById(R.id.toolbar).getBottom()
1133 + mFormulaContainer.getBottom());
Annie Chin26e159e2016-05-18 15:17:14 -07001134 }
Justin Klaassen4b3af052014-05-27 17:53:10 -07001135
Justin Klaassen44595162015-05-28 17:55:20 -07001136 // Change the result's textColor to match the formula.
1137 final int formulaTextColor = mFormulaText.getCurrentTextColor();
1138
Hans Boehm84614952014-11-25 18:46:17 -08001139 if (animate) {
Hans Boehmccc55662015-07-07 14:16:59 -07001140 mResultText.announceForAccessibility(getResources().getString(R.string.desc_eq));
1141 mResultText.announceForAccessibility(mResultText.getText());
Hans Boehmc1ea0912015-06-19 15:05:07 -07001142 setState(CalculatorState.ANIMATE);
Hans Boehm84614952014-11-25 18:46:17 -08001143 final AnimatorSet animatorSet = new AnimatorSet();
1144 animatorSet.playTogether(
Justin Klaassen44595162015-05-28 17:55:20 -07001145 ObjectAnimator.ofPropertyValuesHolder(mResultText,
1146 PropertyValuesHolder.ofFloat(View.SCALE_X, resultScale),
1147 PropertyValuesHolder.ofFloat(View.SCALE_Y, resultScale),
1148 PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, resultTranslationY)),
1149 ObjectAnimator.ofArgb(mResultText, TEXT_COLOR, formulaTextColor),
Annie Chine918fd22016-03-09 11:07:54 -08001150 ObjectAnimator.ofFloat(mFormulaContainer, View.TRANSLATION_Y,
1151 formulaTranslationY));
Justin Klaassen44595162015-05-28 17:55:20 -07001152 animatorSet.setDuration(getResources().getInteger(
1153 android.R.integer.config_longAnimTime));
Hans Boehm84614952014-11-25 18:46:17 -08001154 animatorSet.addListener(new AnimatorListenerAdapter() {
1155 @Override
Hans Boehm84614952014-11-25 18:46:17 -08001156 public void onAnimationEnd(Animator animation) {
Hans Boehm8f051c32016-10-03 16:53:58 -07001157 // Add current result to history.
1158 mEvaluator.preserve(true);
Hans Boehm84614952014-11-25 18:46:17 -08001159 setState(CalculatorState.RESULT);
1160 mCurrentAnimator = null;
1161 }
1162 });
Justin Klaassen4b3af052014-05-27 17:53:10 -07001163
Hans Boehm84614952014-11-25 18:46:17 -08001164 mCurrentAnimator = animatorSet;
1165 animatorSet.start();
Hans Boehm8f051c32016-10-03 16:53:58 -07001166 } else /* No animation desired; get there fast when restarting */ {
Justin Klaassen44595162015-05-28 17:55:20 -07001167 mResultText.setScaleX(resultScale);
1168 mResultText.setScaleY(resultScale);
1169 mResultText.setTranslationY(resultTranslationY);
1170 mResultText.setTextColor(formulaTextColor);
Annie Chine918fd22016-03-09 11:07:54 -08001171 mFormulaContainer.setTranslationY(formulaTranslationY);
Hans Boehm8f051c32016-10-03 16:53:58 -07001172 mEvaluator.represerve();
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001173 setState(CalculatorState.RESULT);
Hans Boehm84614952014-11-25 18:46:17 -08001174 }
Justin Klaassen4b3af052014-05-27 17:53:10 -07001175 }
Hans Boehm84614952014-11-25 18:46:17 -08001176
1177 // Restore positions of the formula and result displays back to their original,
1178 // pre-animation state.
1179 private void restoreDisplayPositions() {
1180 // Clear result.
Justin Klaassen44595162015-05-28 17:55:20 -07001181 mResultText.setText("");
Hans Boehm84614952014-11-25 18:46:17 -08001182 // Reset all of the values modified during the animation.
Justin Klaassen44595162015-05-28 17:55:20 -07001183 mResultText.setScaleX(1.0f);
1184 mResultText.setScaleY(1.0f);
1185 mResultText.setTranslationX(0.0f);
1186 mResultText.setTranslationY(0.0f);
Annie Chine918fd22016-03-09 11:07:54 -08001187 mFormulaContainer.setTranslationY(0.0f);
Hans Boehm84614952014-11-25 18:46:17 -08001188
Hans Boehm08e8f322015-04-21 13:18:38 -07001189 mFormulaText.requestFocus();
Hans Boehm5e6a0ca2015-09-22 17:09:01 -07001190 }
1191
1192 @Override
1193 public void onClick(AlertDialogFragment fragment, int which) {
1194 if (which == DialogInterface.BUTTON_POSITIVE) {
1195 // Timeout extension request.
Hans Boehm8f051c32016-10-03 16:53:58 -07001196 mEvaluator.setLongTimeout();
Hans Boehm5e6a0ca2015-09-22 17:09:01 -07001197 }
1198 }
Hans Boehm84614952014-11-25 18:46:17 -08001199
Justin Klaassend48b7562015-04-16 16:51:38 -07001200 @Override
1201 public boolean onCreateOptionsMenu(Menu menu) {
Justin Klaassend36d63e2015-05-05 12:59:36 -07001202 super.onCreateOptionsMenu(menu);
1203
1204 getMenuInflater().inflate(R.menu.activity_calculator, menu);
Justin Klaassend48b7562015-04-16 16:51:38 -07001205 return true;
1206 }
1207
1208 @Override
1209 public boolean onPrepareOptionsMenu(Menu menu) {
Justin Klaassend36d63e2015-05-05 12:59:36 -07001210 super.onPrepareOptionsMenu(menu);
1211
1212 // Show the leading option when displaying a result.
1213 menu.findItem(R.id.menu_leading).setVisible(mCurrentState == CalculatorState.RESULT);
1214
1215 // Show the fraction option when displaying a rational result.
1216 menu.findItem(R.id.menu_fraction).setVisible(mCurrentState == CalculatorState.RESULT
Hans Boehm8f051c32016-10-03 16:53:58 -07001217 && mEvaluator.getResult(Evaluator.MAIN_INDEX).exactlyDisplayable());
Justin Klaassend36d63e2015-05-05 12:59:36 -07001218
Justin Klaassend48b7562015-04-16 16:51:38 -07001219 return true;
Hans Boehm84614952014-11-25 18:46:17 -08001220 }
1221
1222 @Override
Justin Klaassend48b7562015-04-16 16:51:38 -07001223 public boolean onOptionsItemSelected(MenuItem item) {
Hans Boehm84614952014-11-25 18:46:17 -08001224 switch (item.getItemId()) {
Annie Chinabd202f2016-10-14 14:23:45 -07001225 case R.id.menu_history:
Annie Chin09547532016-10-14 10:59:07 -07001226 showHistoryFragment(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1227 mDragLayout.setOpen();
Annie Chinabd202f2016-10-14 14:23:45 -07001228 return true;
Justin Klaassend36d63e2015-05-05 12:59:36 -07001229 case R.id.menu_leading:
1230 displayFull();
Hans Boehm84614952014-11-25 18:46:17 -08001231 return true;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001232 case R.id.menu_fraction:
1233 displayFraction();
1234 return true;
Justin Klaassend36d63e2015-05-05 12:59:36 -07001235 case R.id.menu_licenses:
1236 startActivity(new Intent(this, Licenses.class));
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001237 return true;
Hans Boehm84614952014-11-25 18:46:17 -08001238 default:
1239 return super.onOptionsItemSelected(item);
1240 }
1241 }
1242
Hans Boehm31ea2522016-11-23 17:47:02 -08001243 /**
1244 * Change evaluation state to one that's friendly to the history fragment.
1245 * Return false if that was not easily possible.
1246 */
1247 private boolean prepareForHistory() {
1248 if (mCurrentState == CalculatorState.ANIMATE) {
1249 throw new AssertionError("onUserInteraction should have ended animation");
1250 } else if (mCurrentState == CalculatorState.EVALUATE
1251 || mCurrentState == CalculatorState.INIT) {
1252 // Easiest to just refuse. Otherwise we can see a state change
1253 // while in history mode, which causes all sorts of problems.
1254 // TODO: Consider other alternatives. If we're just doing the decimal conversion
1255 // at the end of an evaluation, we could treat this as RESULT state.
1256 return false;
1257 }
1258 // We should be in INPUT, INIT_FOR_RESULT, RESULT, or ERROR state.
1259 return true;
1260 }
1261
Annie Chin09547532016-10-14 10:59:07 -07001262 private void showHistoryFragment(int transit) {
Annie Chin06fd3cf2016-11-07 16:04:33 -08001263 final FragmentManager manager = getFragmentManager();
1264 if (manager == null || manager.isDestroyed()) {
1265 return;
1266 }
Hans Boehm31ea2522016-11-23 17:47:02 -08001267 if (!prepareForHistory()) {
1268 return;
1269 }
Annie Chind0f87d22016-10-24 09:04:12 -07001270 if (!mDragLayout.isOpen()) {
Christine Franks7485df52016-12-01 13:18:45 -08001271 stopActionModeOrContextMenu();
1272
Annie Chin450de8a2016-11-23 10:03:56 -08001273 manager.beginTransaction()
Annie Chind0f87d22016-10-24 09:04:12 -07001274 .replace(R.id.history_frame, mHistoryFragment, HistoryFragment.TAG)
1275 .setTransition(transit)
1276 .addToBackStack(HistoryFragment.TAG)
1277 .commit();
Annie Chin450de8a2016-11-23 10:03:56 -08001278 manager.executePendingTransactions();
Annie Chind0f87d22016-10-24 09:04:12 -07001279 }
Annie Chin06fd3cf2016-11-07 16:04:33 -08001280 // TODO: pass current scroll position of result
Annie Chin09547532016-10-14 10:59:07 -07001281 }
1282
Christine Franks7452d3a2016-10-27 13:41:18 -07001283 private void displayMessage(String title, String message) {
1284 AlertDialogFragment.showMessageDialog(this, title, message, null);
Hans Boehm84614952014-11-25 18:46:17 -08001285 }
1286
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001287 private void displayFraction() {
Hans Boehm8f051c32016-10-03 16:53:58 -07001288 UnifiedReal result = mEvaluator.getResult(Evaluator.MAIN_INDEX);
Christine Franks7452d3a2016-10-27 13:41:18 -07001289 displayMessage(getString(R.string.menu_fraction),
1290 KeyMaps.translateResult(result.toNiceString()));
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001291 }
1292
1293 // Display full result to currently evaluated precision
1294 private void displayFull() {
1295 Resources res = getResources();
Hans Boehm24c91ed2016-06-30 18:53:44 -07001296 String msg = mResultText.getFullText(true /* withSeparators */) + " ";
Justin Klaassen44595162015-05-28 17:55:20 -07001297 if (mResultText.fullTextIsExact()) {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001298 msg += res.getString(R.string.exact);
1299 } else {
1300 msg += res.getString(R.string.approximate);
1301 }
Christine Franks7452d3a2016-10-27 13:41:18 -07001302 displayMessage(getString(R.string.menu_leading), msg);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001303 }
1304
Hans Boehm017de982015-06-10 17:46:03 -07001305 /**
1306 * Add input characters to the end of the expression.
1307 * Map them to the appropriate button pushes when possible. Leftover characters
1308 * are added to mUnprocessedChars, which is presumed to immediately precede the newly
1309 * added characters.
Hans Boehm65a99a42016-02-03 18:16:07 -08001310 * @param moreChars characters to be added
1311 * @param explicit these characters were explicitly typed by the user, not pasted
Hans Boehm017de982015-06-10 17:46:03 -07001312 */
1313 private void addChars(String moreChars, boolean explicit) {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001314 if (mUnprocessedChars != null) {
1315 moreChars = mUnprocessedChars + moreChars;
1316 }
1317 int current = 0;
1318 int len = moreChars.length();
Hans Boehm0b9806f2015-06-29 16:07:15 -07001319 boolean lastWasDigit = false;
Hans Boehm5d79d102015-09-16 16:33:47 -07001320 if (mCurrentState == CalculatorState.RESULT && len != 0) {
1321 // Clear display immediately for incomplete function name.
1322 switchToInput(KeyMaps.keyForChar(moreChars.charAt(current)));
1323 }
Hans Boehm24c91ed2016-06-30 18:53:44 -07001324 char groupingSeparator = KeyMaps.translateResult(",").charAt(0);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001325 while (current < len) {
1326 char c = moreChars.charAt(current);
Hans Boehm24c91ed2016-06-30 18:53:44 -07001327 if (Character.isSpaceChar(c) || c == groupingSeparator) {
1328 ++current;
1329 continue;
1330 }
Hans Boehm013969e2015-04-13 20:29:47 -07001331 int k = KeyMaps.keyForChar(c);
Hans Boehm0b9806f2015-06-29 16:07:15 -07001332 if (!explicit) {
1333 int expEnd;
1334 if (lastWasDigit && current !=
1335 (expEnd = Evaluator.exponentEnd(moreChars, current))) {
1336 // Process scientific notation with 'E' when pasting, in spite of ambiguity
1337 // with base of natural log.
1338 // Otherwise the 10^x key is the user's friend.
1339 mEvaluator.addExponent(moreChars, current, expEnd);
1340 current = expEnd;
1341 lastWasDigit = false;
1342 continue;
1343 } else {
1344 boolean isDigit = KeyMaps.digVal(k) != KeyMaps.NOT_DIGIT;
1345 if (current == 0 && (isDigit || k == R.id.dec_point)
Hans Boehm8f051c32016-10-03 16:53:58 -07001346 && mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrailingConstant()) {
Hans Boehm0b9806f2015-06-29 16:07:15 -07001347 // Refuse to concatenate pasted content to trailing constant.
1348 // This makes pasting of calculator results more consistent, whether or
1349 // not the old calculator instance is still around.
1350 addKeyToExpr(R.id.op_mul);
1351 }
1352 lastWasDigit = (isDigit || lastWasDigit && k == R.id.dec_point);
1353 }
1354 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001355 if (k != View.NO_ID) {
1356 mCurrentButton = findViewById(k);
Hans Boehm017de982015-06-10 17:46:03 -07001357 if (explicit) {
1358 addExplicitKeyToExpr(k);
1359 } else {
1360 addKeyToExpr(k);
1361 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001362 if (Character.isSurrogate(c)) {
1363 current += 2;
1364 } else {
1365 ++current;
1366 }
1367 continue;
1368 }
Hans Boehm013969e2015-04-13 20:29:47 -07001369 int f = KeyMaps.funForString(moreChars, current);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001370 if (f != View.NO_ID) {
1371 mCurrentButton = findViewById(f);
Hans Boehm017de982015-06-10 17:46:03 -07001372 if (explicit) {
1373 addExplicitKeyToExpr(f);
1374 } else {
1375 addKeyToExpr(f);
1376 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001377 if (f == R.id.op_sqrt) {
1378 // Square root entered as function; don't lose the parenthesis.
1379 addKeyToExpr(R.id.lparen);
1380 }
1381 current = moreChars.indexOf('(', current) + 1;
1382 continue;
1383 }
1384 // There are characters left, but we can't convert them to button presses.
1385 mUnprocessedChars = moreChars.substring(current);
1386 redisplayAfterFormulaChange();
Hans Boehm52d477a2016-04-01 17:42:50 -07001387 showOrHideToolbar();
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001388 return;
1389 }
1390 mUnprocessedChars = null;
1391 redisplayAfterFormulaChange();
Hans Boehm52d477a2016-04-01 17:42:50 -07001392 showOrHideToolbar();
Hans Boehm84614952014-11-25 18:46:17 -08001393 }
1394
Hans Boehm8f051c32016-10-03 16:53:58 -07001395 private void clearIfNotInputState() {
1396 if (mCurrentState == CalculatorState.ERROR
1397 || mCurrentState == CalculatorState.RESULT) {
1398 setState(CalculatorState.INPUT);
1399 mEvaluator.clearMain();
1400 }
1401 }
1402
Annie Chind0f87d22016-10-24 09:04:12 -07001403 private boolean isViewTarget(View view, MotionEvent event) {
1404 mHitRect.set(0, 0, view.getWidth(), view.getHeight());
1405 mDragLayout.offsetDescendantRectToMyCoords(view, mHitRect);
1406 return mHitRect.contains((int) event.getX(), (int) event.getY());
1407 }
1408
Chenjie Yu3937b652016-06-01 23:14:26 -07001409 /**
Christine Franksbd90b792016-11-22 10:28:26 -08001410 * Since we only support LTR format, using the RTL comma does not make sense.
1411 */
1412 private String getDecimalSeparator() {
1413 final char defaultSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
1414 final char rtlComma = '\u066b';
1415 return defaultSeparator == rtlComma ? "," : String.valueOf(defaultSeparator);
1416 }
1417
1418 /**
Chenjie Yu3937b652016-06-01 23:14:26 -07001419 * Clean up animation for context menu.
1420 */
1421 @Override
1422 public void onContextMenuClosed(Menu menu) {
1423 stopActionModeOrContextMenu();
1424 }
Christine Franks1d99be12016-11-14 14:00:36 -08001425
1426 public interface OnDisplayMemoryOperationsListener {
1427 boolean shouldDisplayMemory();
1428 }
Justin Klaassen4b3af052014-05-27 17:53:10 -07001429}