Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1 | /* |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 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 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 17 | // TODO: Better indication of when the result is known to be exact. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 18 | // TODO: Check and possibly fix accessability issues. |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 19 | // TODO: Copy & more general paste in formula? Note that this requires |
| 20 | // great care: Currently the text version of a displayed formula |
| 21 | // is not directly useful for re-evaluating the formula later, since |
| 22 | // it contains ellipses representing subexpressions evaluated with |
| 23 | // a different degree mode. Rather than supporting copy from the |
| 24 | // formula window, we may eventually want to support generation of a |
| 25 | // more useful text version in a separate window. It's not clear |
| 26 | // this is worth the added (code and user) complexity. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 27 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 28 | package com.android.calculator2; |
| 29 | |
| 30 | import android.animation.Animator; |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 31 | import android.animation.Animator.AnimatorListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 32 | import android.animation.AnimatorListenerAdapter; |
| 33 | import android.animation.AnimatorSet; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 34 | import android.animation.ObjectAnimator; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 35 | import android.animation.PropertyValuesHolder; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 36 | import android.app.Activity; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 37 | import android.app.AlertDialog; |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 38 | import android.content.ClipData; |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 39 | import android.content.Intent; |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 40 | import android.content.res.Resources; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 41 | import android.graphics.Color; |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 42 | import android.graphics.Rect; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 43 | import android.net.Uri; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 44 | import android.os.Bundle; |
Justin Klaassen | f79d6f6 | 2014-08-26 12:27:08 -0700 | [diff] [blame] | 45 | import android.support.annotation.NonNull; |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 46 | import android.support.v4.view.ViewPager; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 47 | import android.text.SpannableString; |
| 48 | import android.text.Spanned; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 49 | import android.text.style.ForegroundColorSpan; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 50 | import android.util.Property; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 51 | import android.view.KeyCharacterMap; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 52 | import android.view.KeyEvent; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 53 | import android.view.Menu; |
| 54 | import android.view.MenuItem; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 55 | import android.view.View; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 56 | import android.view.View.OnKeyListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 57 | import android.view.View.OnLongClickListener; |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 58 | import android.view.ViewAnimationUtils; |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 59 | import android.view.ViewGroupOverlay; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 60 | import android.view.animation.AccelerateDecelerateInterpolator; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 61 | import android.widget.TextView; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 62 | import android.widget.Toolbar; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 63 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 64 | import com.android.calculator2.CalculatorText.OnTextSizeChangeListener; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 65 | |
| 66 | import java.io.ByteArrayInputStream; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 67 | import java.io.ByteArrayOutputStream; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 68 | import java.io.IOException; |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 69 | import java.io.ObjectInput; |
| 70 | import java.io.ObjectInputStream; |
| 71 | import java.io.ObjectOutput; |
| 72 | import java.io.ObjectOutputStream; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 73 | |
Justin Klaassen | 04f79c7 | 2014-06-27 17:25:35 -0700 | [diff] [blame] | 74 | public class Calculator extends Activity |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 75 | implements OnTextSizeChangeListener, OnLongClickListener, CalculatorText.OnPasteListener { |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Constant for an invalid resource id. |
| 79 | */ |
| 80 | public static final int INVALID_RES_ID = -1; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 81 | |
| 82 | private enum CalculatorState { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 83 | INPUT, // Result and formula both visible, no evaluation requested, |
| 84 | // Though result may be visible on bottom line. |
| 85 | EVALUATE, // Both visible, evaluation requested, evaluation/animation incomplete. |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 86 | // Not used for instant result evaluation. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 87 | INIT, // Very temporary state used as alternative to EVALUATE |
| 88 | // during reinitialization. Do not animate on completion. |
| 89 | ANIMATE, // Result computed, animation to enlarge result window in progress. |
| 90 | RESULT, // Result displayed, formula invisible. |
| 91 | // If we are in RESULT state, the formula was evaluated without |
| 92 | // error to initial precision. |
| 93 | ERROR // Error displayed: Formula visible, result shows error message. |
| 94 | // Display similar to INPUT state. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 95 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 96 | // Normal transition sequence is |
| 97 | // INPUT -> EVALUATE -> ANIMATE -> RESULT (or ERROR) -> INPUT |
| 98 | // A RESULT -> ERROR transition is possible in rare corner cases, in which |
| 99 | // a higher precision evaluation exposes an error. This is possible, since we |
| 100 | // initially evaluate assuming we were given a well-defined problem. If we |
| 101 | // were actually asked to compute sqrt(<extremely tiny negative number>) we produce 0 |
| 102 | // unless we are asked for enough precision that we can distinguish the argument from zero. |
| 103 | // TODO: Consider further heuristics to reduce the chance of observing this? |
| 104 | // It already seems to be observable only in contrived cases. |
| 105 | // ANIMATE, ERROR, and RESULT are translated to an INIT state if the application |
| 106 | // is restarted in that state. This leads us to recompute and redisplay the result |
| 107 | // ASAP. |
| 108 | // TODO: Possibly save a bit more information, e.g. its initial display string |
| 109 | // or most significant digit position, to speed up restart. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 110 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 111 | private final Property<TextView, Integer> TEXT_COLOR = |
| 112 | new Property<TextView, Integer>(Integer.class, "textColor") { |
| 113 | @Override |
| 114 | public Integer get(TextView textView) { |
| 115 | return textView.getCurrentTextColor(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void set(TextView textView, Integer textColor) { |
| 120 | textView.setTextColor(textColor); |
| 121 | } |
| 122 | }; |
| 123 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 124 | // We currently assume that the formula does not change out from under us in |
| 125 | // any way. We explicitly handle all input to the formula here. |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 126 | private final OnKeyListener mFormulaOnKeyListener = new OnKeyListener() { |
| 127 | @Override |
| 128 | public boolean onKey(View view, int keyCode, KeyEvent keyEvent) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 129 | stopActionMode(); |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 130 | // Never consume DPAD key events. |
| 131 | switch (keyCode) { |
| 132 | case KeyEvent.KEYCODE_DPAD_UP: |
| 133 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 134 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 135 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 136 | return false; |
| 137 | } |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 138 | // Always cancel unrequested in-progress evaluation, so that we don't have |
| 139 | // to worry about subsequent asynchronous completion. |
| 140 | // Requested in-progress evaluations are handled below. |
| 141 | if (mCurrentState != CalculatorState.EVALUATE) { |
| 142 | mEvaluator.cancelAll(true); |
| 143 | } |
| 144 | // In other cases we go ahead and process the input normally after cancelling: |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 145 | if (keyEvent.getAction() != KeyEvent.ACTION_UP) { |
| 146 | return true; |
| 147 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 148 | switch (keyCode) { |
| 149 | case KeyEvent.KEYCODE_NUMPAD_ENTER: |
| 150 | case KeyEvent.KEYCODE_ENTER: |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 151 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 152 | mCurrentButton = mEqualButton; |
| 153 | onEquals(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 154 | return true; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 155 | case KeyEvent.KEYCODE_DEL: |
| 156 | mCurrentButton = mDeleteButton; |
| 157 | onDelete(); |
| 158 | return true; |
| 159 | default: |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 160 | cancelIfEvaluating(false); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 161 | final int raw = keyEvent.getKeyCharacterMap() |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 162 | .get(keyCode, keyEvent.getMetaState()); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 163 | if ((raw & KeyCharacterMap.COMBINING_ACCENT) != 0) { |
| 164 | return true; // discard |
| 165 | } |
| 166 | // Try to discard non-printing characters and the like. |
| 167 | // The user will have to explicitly delete other junk that gets past us. |
| 168 | if (Character.isIdentifierIgnorable(raw) |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 169 | || Character.isWhitespace(raw)) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 170 | return true; |
| 171 | } |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 172 | char c = (char) raw; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 173 | if (c == '=') { |
Hans Boehm | e57fb01 | 2015-05-07 19:52:32 -0700 | [diff] [blame] | 174 | mCurrentButton = mEqualButton; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 175 | onEquals(); |
| 176 | } else { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 177 | addChars(String.valueOf(c), true); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 178 | redisplayAfterFormulaChange(); |
| 179 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | }; |
| 184 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 185 | private static final String NAME = Calculator.class.getName(); |
| 186 | private static final String KEY_DISPLAY_STATE = NAME + "_display_state"; |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 187 | private static final String KEY_UNPROCESSED_CHARS = NAME + "_unprocessed_chars"; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 188 | private static final String KEY_EVAL_STATE = NAME + "_eval_state"; |
| 189 | // Associated value is a byte array holding both mCalculatorState |
| 190 | // and the (much more complex) evaluator state. |
Justin Klaassen | 741471e | 2014-06-11 09:43:44 -0700 | [diff] [blame] | 191 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 192 | private CalculatorState mCurrentState; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 193 | private Evaluator mEvaluator; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 194 | |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 195 | private View mDisplayView; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 196 | private TextView mModeView; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 197 | private CalculatorText mFormulaText; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 198 | private CalculatorResult mResultText; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 199 | |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 200 | private ViewPager mPadViewPager; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 201 | private View mDeleteButton; |
| 202 | private View mClearButton; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 203 | private View mEqualButton; |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 204 | |
| 205 | private TextView mInverseToggle; |
| 206 | private TextView mModeToggle; |
| 207 | |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 208 | private View[] mInvertibleButtons; |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 209 | private View[] mInverseButtons; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 210 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 211 | private View mCurrentButton; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 212 | private Animator mCurrentAnimator; |
| 213 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 214 | private String mUnprocessedChars = null; // Characters that were recently entered |
| 215 | // at the end of the display that have not yet |
| 216 | // been added to the underlying expression. |
| 217 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 218 | @Override |
| 219 | protected void onCreate(Bundle savedInstanceState) { |
| 220 | super.onCreate(savedInstanceState); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 221 | setContentView(R.layout.activity_calculator); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 222 | setActionBar((Toolbar) findViewById(R.id.toolbar)); |
| 223 | |
| 224 | // Hide all default options in the ActionBar. |
| 225 | getActionBar().setDisplayOptions(0); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 226 | |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 227 | mDisplayView = findViewById(R.id.display); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 228 | mModeView = (TextView) findViewById(R.id.mode); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 229 | mFormulaText = (CalculatorText) findViewById(R.id.formula); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 230 | mResultText = (CalculatorResult) findViewById(R.id.result); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 231 | |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 232 | mPadViewPager = (ViewPager) findViewById(R.id.pad_pager); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 233 | mDeleteButton = findViewById(R.id.del); |
| 234 | mClearButton = findViewById(R.id.clr); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 235 | mEqualButton = findViewById(R.id.pad_numeric).findViewById(R.id.eq); |
| 236 | if (mEqualButton == null || mEqualButton.getVisibility() != View.VISIBLE) { |
| 237 | mEqualButton = findViewById(R.id.pad_operator).findViewById(R.id.eq); |
| 238 | } |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 239 | |
| 240 | mInverseToggle = (TextView) findViewById(R.id.toggle_inv); |
| 241 | mModeToggle = (TextView) findViewById(R.id.toggle_mode); |
| 242 | |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 243 | mInvertibleButtons = new View[] { |
| 244 | findViewById(R.id.fun_sin), |
| 245 | findViewById(R.id.fun_cos), |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 246 | findViewById(R.id.fun_tan), |
| 247 | findViewById(R.id.fun_ln), |
| 248 | findViewById(R.id.fun_log), |
| 249 | findViewById(R.id.op_sqrt) |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 250 | }; |
| 251 | mInverseButtons = new View[] { |
| 252 | findViewById(R.id.fun_arcsin), |
| 253 | findViewById(R.id.fun_arccos), |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 254 | findViewById(R.id.fun_arctan), |
| 255 | findViewById(R.id.fun_exp), |
| 256 | findViewById(R.id.fun_10pow), |
| 257 | findViewById(R.id.op_sqr) |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 258 | }; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 259 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 260 | mEvaluator = new Evaluator(this, mResultText); |
| 261 | mResultText.setEvaluator(mEvaluator); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 262 | KeyMaps.setActivity(this); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 263 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 264 | if (savedInstanceState != null) { |
| 265 | setState(CalculatorState.values()[ |
| 266 | savedInstanceState.getInt(KEY_DISPLAY_STATE, |
| 267 | CalculatorState.INPUT.ordinal())]); |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 268 | CharSequence unprocessed = savedInstanceState.getCharSequence(KEY_UNPROCESSED_CHARS); |
| 269 | if (unprocessed != null) { |
| 270 | mUnprocessedChars = unprocessed.toString(); |
| 271 | } |
| 272 | byte[] state = savedInstanceState.getByteArray(KEY_EVAL_STATE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 273 | if (state != null) { |
| 274 | try (ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(state))) { |
| 275 | mEvaluator.restoreInstanceState(in); |
| 276 | } catch (Throwable ignored) { |
| 277 | // When in doubt, revert to clean state |
| 278 | mCurrentState = CalculatorState.INPUT; |
| 279 | mEvaluator.clear(); |
| 280 | } |
| 281 | } |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 282 | } else { |
| 283 | mCurrentState = CalculatorState.INPUT; |
| 284 | mEvaluator.clear(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 285 | } |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 286 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 287 | mFormulaText.setOnKeyListener(mFormulaOnKeyListener); |
| 288 | mFormulaText.setOnTextSizeChangeListener(this); |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 289 | mFormulaText.setOnPasteListener(this); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 290 | mDeleteButton.setOnLongClickListener(this); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 291 | |
| 292 | onInverseToggled(mInverseToggle.isSelected()); |
| 293 | onModeChanged(mEvaluator.getDegreeMode()); |
| 294 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 295 | if (mCurrentState != CalculatorState.INPUT) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 296 | // Just reevaluate. |
| 297 | redisplayFormula(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 298 | setState(CalculatorState.INIT); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 299 | mEvaluator.requireResult(); |
| 300 | } else { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 301 | redisplayAfterFormulaChange(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 302 | } |
| 303 | // TODO: We're currently not saving and restoring scroll position. |
| 304 | // We probably should. Details may require care to deal with: |
| 305 | // - new display size |
| 306 | // - slow recomputation if we've scrolled far. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
Justin Klaassen | f79d6f6 | 2014-08-26 12:27:08 -0700 | [diff] [blame] | 310 | protected void onSaveInstanceState(@NonNull Bundle outState) { |
| 311 | // If there's an animation in progress, cancel it first to ensure our state is up-to-date. |
| 312 | if (mCurrentAnimator != null) { |
| 313 | mCurrentAnimator.cancel(); |
| 314 | } |
| 315 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 316 | super.onSaveInstanceState(outState); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 317 | outState.putInt(KEY_DISPLAY_STATE, mCurrentState.ordinal()); |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 318 | outState.putCharSequence(KEY_UNPROCESSED_CHARS, mUnprocessedChars); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 319 | ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(); |
| 320 | try (ObjectOutput out = new ObjectOutputStream(byteArrayStream)) { |
| 321 | mEvaluator.saveInstanceState(out); |
| 322 | } catch (IOException e) { |
| 323 | // Impossible; No IO involved. |
| 324 | throw new AssertionError("Impossible IO exception", e); |
| 325 | } |
| 326 | outState.putByteArray(KEY_EVAL_STATE, byteArrayStream.toByteArray()); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 329 | // Set the state, updating delete label and display colors. |
| 330 | // This restores display positions on moving to INPUT. |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 331 | // But movement/animation for moving to RESULT has already been done. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 332 | private void setState(CalculatorState state) { |
| 333 | if (mCurrentState != state) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 334 | if (state == CalculatorState.INPUT) { |
| 335 | restoreDisplayPositions(); |
| 336 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 337 | mCurrentState = state; |
| 338 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 339 | if (mCurrentState == CalculatorState.RESULT) { |
| 340 | // No longer do this for ERROR; allow mistakes to be corrected. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 341 | mDeleteButton.setVisibility(View.GONE); |
| 342 | mClearButton.setVisibility(View.VISIBLE); |
| 343 | } else { |
| 344 | mDeleteButton.setVisibility(View.VISIBLE); |
| 345 | mClearButton.setVisibility(View.GONE); |
| 346 | } |
| 347 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 348 | if (mCurrentState == CalculatorState.ERROR) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 349 | final int errorColor = getColor(R.color.calculator_error_color); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 350 | mFormulaText.setTextColor(errorColor); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 351 | mResultText.setTextColor(errorColor); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 352 | getWindow().setStatusBarColor(errorColor); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 353 | } else if (mCurrentState != CalculatorState.RESULT) { |
| 354 | mFormulaText.setTextColor(getColor(R.color.display_formula_text_color)); |
| 355 | mResultText.setTextColor(getColor(R.color.display_result_text_color)); |
| 356 | getWindow().setStatusBarColor(getColor(R.color.calculator_accent_color)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 357 | } |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 358 | |
| 359 | invalidateOptionsMenu(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 363 | // Stop any active ActionMode. Return true if there was one. |
| 364 | private boolean stopActionMode() { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 365 | if (mResultText.stopActionMode()) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 366 | return true; |
| 367 | } |
| 368 | if (mFormulaText.stopActionMode()) { |
| 369 | return true; |
| 370 | } |
| 371 | return false; |
| 372 | } |
| 373 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 374 | @Override |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 375 | public void onBackPressed() { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 376 | if (!stopActionMode()) { |
| 377 | if (mPadViewPager != null && mPadViewPager.getCurrentItem() != 0) { |
| 378 | // Select the previous pad. |
| 379 | mPadViewPager.setCurrentItem(mPadViewPager.getCurrentItem() - 1); |
| 380 | } else { |
| 381 | // If the user is currently looking at the first pad (or the pad is not paged), |
| 382 | // allow the system to handle the Back button. |
| 383 | super.onBackPressed(); |
| 384 | } |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 389 | public void onUserInteraction() { |
| 390 | super.onUserInteraction(); |
| 391 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 392 | // If there's an animation in progress, end it immediately, so the user interaction can |
| 393 | // be handled. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 394 | if (mCurrentAnimator != null) { |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 395 | mCurrentAnimator.end(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 399 | /** |
| 400 | * Invoked whenever the inverse button is toggled to update the UI. |
| 401 | * |
| 402 | * @param showInverse {@code true} if inverse functions should be shown |
| 403 | */ |
| 404 | private void onInverseToggled(boolean showInverse) { |
| 405 | if (showInverse) { |
| 406 | mInverseToggle.setContentDescription(getString(R.string.desc_inv_on)); |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 407 | for (View invertibleButton : mInvertibleButtons) { |
| 408 | invertibleButton.setVisibility(View.GONE); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 409 | } |
| 410 | for (View inverseButton : mInverseButtons) { |
| 411 | inverseButton.setVisibility(View.VISIBLE); |
| 412 | } |
| 413 | } else { |
| 414 | mInverseToggle.setContentDescription(getString(R.string.desc_inv_off)); |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 415 | for (View invertibleButton : mInvertibleButtons) { |
| 416 | invertibleButton.setVisibility(View.VISIBLE); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 417 | } |
| 418 | for (View inverseButton : mInverseButtons) { |
| 419 | inverseButton.setVisibility(View.GONE); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Invoked whenever the deg/rad mode may have changed to update the UI. |
| 426 | * |
| 427 | * @param degreeMode {@code true} if in degree mode |
| 428 | */ |
| 429 | private void onModeChanged(boolean degreeMode) { |
| 430 | if (degreeMode) { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 431 | mModeView.setText(R.string.mode_deg); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 432 | mModeView.setContentDescription(getString(R.string.desc_mode_deg)); |
| 433 | |
| 434 | mModeToggle.setText(R.string.mode_rad); |
| 435 | mModeToggle.setContentDescription(getString(R.string.desc_switch_rad)); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 436 | } else { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 437 | mModeView.setText(R.string.mode_rad); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 438 | mModeView.setContentDescription(getString(R.string.desc_mode_rad)); |
| 439 | |
| 440 | mModeToggle.setText(R.string.mode_deg); |
| 441 | mModeToggle.setContentDescription(getString(R.string.desc_switch_deg)); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 444 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 445 | // Add the given button id to input expression. |
| 446 | // If appropriate, clear the expression before doing so. |
| 447 | private void addKeyToExpr(int id) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 448 | // FIXME: Other states? |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 449 | if (mCurrentState == CalculatorState.ERROR) { |
| 450 | setState(CalculatorState.INPUT); |
| 451 | } else if (mCurrentState == CalculatorState.RESULT) { |
| 452 | if (KeyMaps.isBinary(id) || KeyMaps.isSuffix(id)) { |
| 453 | mEvaluator.collapse(); |
| 454 | } else { |
| 455 | mEvaluator.clear(); |
| 456 | } |
| 457 | setState(CalculatorState.INPUT); |
| 458 | } |
| 459 | if (!mEvaluator.append(id)) { |
| 460 | // TODO: Some user visible feedback? |
| 461 | } |
| 462 | } |
| 463 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 464 | /** |
| 465 | * Add the given button id to input expression, assuming it was explicitly |
| 466 | * typed/touched. |
| 467 | * We perform slightly more aggressive correction than in pasted expressions. |
| 468 | */ |
| 469 | private void addExplicitKeyToExpr(int id) { |
| 470 | if (mCurrentState == CalculatorState.INPUT && id == R.id.op_sub) { |
| 471 | mEvaluator.getExpr().removeTrailingAdditiveOperators(); |
| 472 | } |
| 473 | addKeyToExpr(id); |
| 474 | } |
| 475 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 476 | private void redisplayAfterFormulaChange() { |
| 477 | // TODO: Could do this more incrementally. |
| 478 | redisplayFormula(); |
| 479 | setState(CalculatorState.INPUT); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 480 | if (mEvaluator.getExpr().hasInterestingOps()) { |
| 481 | mEvaluator.evaluateAndShowResult(); |
| 482 | } else { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 483 | mResultText.clear(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 484 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 487 | public void onButtonClick(View view) { |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 488 | // Any animation is ended before we get here. |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 489 | mCurrentButton = view; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 490 | stopActionMode(); |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 491 | // See onKey above for the rationale behind some of the behavior below: |
| 492 | if (mCurrentState != CalculatorState.EVALUATE) { |
| 493 | // Cancel evaluations that were not specifically requested. |
| 494 | mEvaluator.cancelAll(true); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 495 | } |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 496 | final int id = view.getId(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 497 | switch (id) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 498 | case R.id.eq: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 499 | onEquals(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 500 | break; |
| 501 | case R.id.del: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 502 | onDelete(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 503 | break; |
| 504 | case R.id.clr: |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 505 | onClear(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 506 | break; |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 507 | case R.id.toggle_inv: |
| 508 | final boolean selected = !mInverseToggle.isSelected(); |
| 509 | mInverseToggle.setSelected(selected); |
| 510 | onInverseToggled(selected); |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 511 | if (mCurrentState == CalculatorState.RESULT) { |
| 512 | mResultText.redisplay(); // In case we cancelled reevaluation. |
| 513 | } |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 514 | break; |
| 515 | case R.id.toggle_mode: |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 516 | cancelIfEvaluating(false); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 517 | final boolean mode = !mEvaluator.getDegreeMode(); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 518 | if (mCurrentState == CalculatorState.RESULT) { |
| 519 | mEvaluator.collapse(); // Capture result evaluated in old mode |
| 520 | redisplayFormula(); |
| 521 | } |
| 522 | // In input mode, we reinterpret already entered trig functions. |
| 523 | mEvaluator.setDegreeMode(mode); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 524 | onModeChanged(mode); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 525 | setState(CalculatorState.INPUT); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 526 | mResultText.clear(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 527 | if (mEvaluator.getExpr().hasInterestingOps()) { |
| 528 | mEvaluator.evaluateAndShowResult(); |
| 529 | } |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 530 | break; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 531 | default: |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 532 | cancelIfEvaluating(false); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 533 | addExplicitKeyToExpr(id); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 534 | redisplayAfterFormulaChange(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | } |
| 538 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 539 | void redisplayFormula() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 540 | String formula = mEvaluator.getExpr().toString(this); |
| 541 | if (mUnprocessedChars != null) { |
| 542 | // Add and highlight characters we couldn't process. |
| 543 | SpannableString formatted = new SpannableString(formula + mUnprocessedChars); |
| 544 | // TODO: should probably match this to the error color. |
| 545 | formatted.setSpan(new ForegroundColorSpan(Color.RED), |
| 546 | formula.length(), formatted.length(), |
| 547 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 548 | mFormulaText.setText(formatted); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 549 | } else { |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 550 | mFormulaText.setText(formula); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 551 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 554 | @Override |
| 555 | public boolean onLongClick(View view) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 556 | mCurrentButton = view; |
| 557 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 558 | if (view.getId() == R.id.del) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 559 | onClear(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 560 | return true; |
| 561 | } |
| 562 | return false; |
| 563 | } |
| 564 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 565 | // Initial evaluation completed successfully. Initiate display. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 566 | public void onEvaluate(int initDisplayPrec, int msd, int leastDigPos, |
| 567 | String truncatedWholeNumber) { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 568 | // Invalidate any options that may depend on the current result. |
| 569 | invalidateOptionsMenu(); |
| 570 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 571 | mResultText.displayResult(initDisplayPrec, msd, leastDigPos, truncatedWholeNumber); |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 572 | if (mCurrentState != CalculatorState.INPUT) { // in EVALUATE or INIT state |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 573 | onResult(mCurrentState != CalculatorState.INIT); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 574 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 575 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 576 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 577 | // Reset state to reflect evaluator cancellation. Invoked by evaluator. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 578 | public void onCancelled() { |
| 579 | // We should be in EVALUATE state. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 580 | setState(CalculatorState.INPUT); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 581 | mResultText.clear(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // Reevaluation completed; ask result to redisplay current value. |
| 585 | public void onReevaluate() |
| 586 | { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 587 | mResultText.redisplay(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 590 | @Override |
| 591 | public void onTextSizeChanged(final TextView textView, float oldSize) { |
| 592 | if (mCurrentState != CalculatorState.INPUT) { |
| 593 | // Only animate text changes that occur from user input. |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | // Calculate the values needed to perform the scale and translation animations, |
| 598 | // maintaining the same apparent baseline for the displayed text. |
| 599 | final float textScale = oldSize / textView.getTextSize(); |
| 600 | final float translationX = (1.0f - textScale) * |
| 601 | (textView.getWidth() / 2.0f - textView.getPaddingEnd()); |
| 602 | final float translationY = (1.0f - textScale) * |
| 603 | (textView.getHeight() / 2.0f - textView.getPaddingBottom()); |
| 604 | |
| 605 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 606 | animatorSet.playTogether( |
| 607 | ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f), |
| 608 | ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f), |
| 609 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f), |
| 610 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f)); |
Justin Klaassen | 94db720 | 2014-06-11 11:22:31 -0700 | [diff] [blame] | 611 | animatorSet.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 612 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 613 | animatorSet.start(); |
| 614 | } |
| 615 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 616 | /** |
| 617 | * Cancel any in-progress explicitly requested evaluations. |
| 618 | * @param quiet suppress pop-up message. Explicit evaluation can change the expression |
| 619 | value, and certainly changes the display, so it seems reasonable to warn. |
| 620 | * @return true if there was such an evaluation |
| 621 | */ |
| 622 | private boolean cancelIfEvaluating(boolean quiet) { |
| 623 | if (mCurrentState == CalculatorState.EVALUATE) { |
| 624 | mEvaluator.cancelAll(quiet); |
| 625 | return true; |
| 626 | } else { |
| 627 | return false; |
| 628 | } |
| 629 | } |
| 630 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 631 | private void onEquals() { |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 632 | // In non-INPUT state assume this was redundant and ignore it. |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 633 | if (mCurrentState == CalculatorState.INPUT && !mEvaluator.getExpr().isEmpty()) { |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 634 | setState(CalculatorState.EVALUATE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 635 | mEvaluator.requireResult(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
| 639 | private void onDelete() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 640 | // Delete works like backspace; remove the last character or operator from the expression. |
| 641 | // Note that we handle keyboard delete exactly like the delete button. For |
| 642 | // example the delete button can be used to delete a character from an incomplete |
| 643 | // function name typed on a physical keyboard. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 644 | // This should be impossible in RESULT state. |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 645 | // If there is an in-progress explicit evaluation, just cancel it and return. |
| 646 | if (cancelIfEvaluating(false)) return; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 647 | setState(CalculatorState.INPUT); |
| 648 | if (mUnprocessedChars != null) { |
| 649 | int len = mUnprocessedChars.length(); |
| 650 | if (len > 0) { |
| 651 | mUnprocessedChars = mUnprocessedChars.substring(0, len-1); |
| 652 | } else { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 653 | mEvaluator.delete(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 654 | } |
| 655 | } else { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 656 | mEvaluator.delete(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 657 | } |
| 658 | redisplayAfterFormulaChange(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 661 | private void reveal(View sourceView, int colorRes, AnimatorListener listener) { |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 662 | final ViewGroupOverlay groupOverlay = |
| 663 | (ViewGroupOverlay) getWindow().getDecorView().getOverlay(); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 664 | |
| 665 | final Rect displayRect = new Rect(); |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 666 | mDisplayView.getGlobalVisibleRect(displayRect); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 667 | |
| 668 | // Make reveal cover the display and status bar. |
| 669 | final View revealView = new View(this); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 670 | revealView.setBottom(displayRect.bottom); |
| 671 | revealView.setLeft(displayRect.left); |
| 672 | revealView.setRight(displayRect.right); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 673 | revealView.setBackgroundColor(getResources().getColor(colorRes)); |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 674 | groupOverlay.add(revealView); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 675 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 676 | final int[] clearLocation = new int[2]; |
| 677 | sourceView.getLocationInWindow(clearLocation); |
| 678 | clearLocation[0] += sourceView.getWidth() / 2; |
| 679 | clearLocation[1] += sourceView.getHeight() / 2; |
| 680 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 681 | final int revealCenterX = clearLocation[0] - revealView.getLeft(); |
| 682 | final int revealCenterY = clearLocation[1] - revealView.getTop(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 683 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 684 | final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2); |
| 685 | final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2); |
| 686 | final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 687 | final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2)); |
| 688 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 689 | final Animator revealAnimator = |
| 690 | ViewAnimationUtils.createCircularReveal(revealView, |
ztenghui | 3d6ecaf | 2014-06-05 09:56:00 -0700 | [diff] [blame] | 691 | revealCenterX, revealCenterY, 0.0f, revealRadius); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 692 | revealAnimator.setDuration( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 693 | getResources().getInteger(android.R.integer.config_longAnimTime)); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 694 | revealAnimator.addListener(listener); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 695 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 696 | final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 697 | alphaAnimator.setDuration( |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 698 | getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 699 | |
| 700 | final AnimatorSet animatorSet = new AnimatorSet(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 701 | animatorSet.play(revealAnimator).before(alphaAnimator); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 702 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 703 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 704 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 705 | public void onAnimationEnd(Animator animator) { |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 706 | groupOverlay.remove(revealView); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 707 | mCurrentAnimator = null; |
| 708 | } |
| 709 | }); |
| 710 | |
| 711 | mCurrentAnimator = animatorSet; |
| 712 | animatorSet.start(); |
| 713 | } |
| 714 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 715 | private void onClear() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 716 | if (mEvaluator.getExpr().isEmpty()) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 717 | return; |
| 718 | } |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 719 | cancelIfEvaluating(true); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 720 | reveal(mCurrentButton, R.color.calculator_accent_color, new AnimatorListenerAdapter() { |
| 721 | @Override |
| 722 | public void onAnimationEnd(Animator animation) { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 723 | mUnprocessedChars = null; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 724 | mResultText.clear(); |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 725 | mEvaluator.clear(); |
| 726 | setState(CalculatorState.INPUT); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 727 | redisplayFormula(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 728 | } |
| 729 | }); |
| 730 | } |
| 731 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 732 | // Evaluation encountered en error. Display the error. |
| 733 | void onError(final int errorResourceId) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 734 | if (mCurrentState == CalculatorState.EVALUATE) { |
| 735 | setState(CalculatorState.ANIMATE); |
| 736 | reveal(mCurrentButton, R.color.calculator_error_color, |
| 737 | new AnimatorListenerAdapter() { |
| 738 | @Override |
| 739 | public void onAnimationEnd(Animator animation) { |
| 740 | setState(CalculatorState.ERROR); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 741 | mResultText.displayError(errorResourceId); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 742 | } |
| 743 | }); |
| 744 | } else if (mCurrentState == CalculatorState.INIT) { |
| 745 | setState(CalculatorState.ERROR); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 746 | mResultText.displayError(errorResourceId); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 747 | } else { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 748 | mResultText.clear(); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 749 | } |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 752 | |
| 753 | // Animate movement of result into the top formula slot. |
| 754 | // Result window now remains translated in the top slot while the result is displayed. |
| 755 | // (We convert it back to formula use only when the user provides new input.) |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 756 | // Historical note: In the Lollipop version, this invisibly and instantaneously moved |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 757 | // formula and result displays back at the end of the animation. We no longer do that, |
| 758 | // so that we can continue to properly support scrolling of the result. |
| 759 | // We assume the result already contains the text to be expanded. |
| 760 | private void onResult(boolean animate) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 761 | // Calculate the textSize that would be used to display the result in the formula. |
| 762 | // For scrollable results just use the minimum textSize to maximize the number of digits |
| 763 | // that are visible on screen. |
| 764 | float textSize = mFormulaText.getMinimumTextSize(); |
| 765 | if (!mResultText.isScrollable()) { |
| 766 | textSize = mFormulaText.getVariableTextSize(mResultText.getText().toString()); |
| 767 | } |
| 768 | |
| 769 | // Scale the result to match the calculated textSize, minimizing the jump-cut transition |
| 770 | // when a result is reused in a subsequent expression. |
| 771 | final float resultScale = textSize / mResultText.getTextSize(); |
| 772 | |
| 773 | // Set the result's pivot to match its gravity. |
| 774 | mResultText.setPivotX(mResultText.getWidth() - mResultText.getPaddingRight()); |
| 775 | mResultText.setPivotY(mResultText.getHeight() - mResultText.getPaddingBottom()); |
| 776 | |
| 777 | // Calculate the necessary translations so the result takes the place of the formula and |
| 778 | // the formula moves off the top of the screen. |
| 779 | final float resultTranslationY = (mFormulaText.getBottom() - mResultText.getBottom()) |
| 780 | - (mFormulaText.getPaddingBottom() - mResultText.getPaddingBottom()); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 781 | final float formulaTranslationY = -mFormulaText.getBottom(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 782 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 783 | // Change the result's textColor to match the formula. |
| 784 | final int formulaTextColor = mFormulaText.getCurrentTextColor(); |
| 785 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 786 | if (animate) { |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame^] | 787 | setState(CalculatorState.ANIMATE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 788 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 789 | animatorSet.playTogether( |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 790 | ObjectAnimator.ofPropertyValuesHolder(mResultText, |
| 791 | PropertyValuesHolder.ofFloat(View.SCALE_X, resultScale), |
| 792 | PropertyValuesHolder.ofFloat(View.SCALE_Y, resultScale), |
| 793 | PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, resultTranslationY)), |
| 794 | ObjectAnimator.ofArgb(mResultText, TEXT_COLOR, formulaTextColor), |
| 795 | ObjectAnimator.ofFloat(mFormulaText, View.TRANSLATION_Y, formulaTranslationY)); |
| 796 | animatorSet.setDuration(getResources().getInteger( |
| 797 | android.R.integer.config_longAnimTime)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 798 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 799 | @Override |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 800 | public void onAnimationEnd(Animator animation) { |
| 801 | setState(CalculatorState.RESULT); |
| 802 | mCurrentAnimator = null; |
| 803 | } |
| 804 | }); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 805 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 806 | mCurrentAnimator = animatorSet; |
| 807 | animatorSet.start(); |
| 808 | } else /* No animation desired; get there fast, e.g. when restarting */ { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 809 | mResultText.setScaleX(resultScale); |
| 810 | mResultText.setScaleY(resultScale); |
| 811 | mResultText.setTranslationY(resultTranslationY); |
| 812 | mResultText.setTextColor(formulaTextColor); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 813 | mFormulaText.setTranslationY(formulaTranslationY); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 814 | setState(CalculatorState.RESULT); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 815 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 816 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 817 | |
| 818 | // Restore positions of the formula and result displays back to their original, |
| 819 | // pre-animation state. |
| 820 | private void restoreDisplayPositions() { |
| 821 | // Clear result. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 822 | mResultText.setText(""); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 823 | // Reset all of the values modified during the animation. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 824 | mResultText.setScaleX(1.0f); |
| 825 | mResultText.setScaleY(1.0f); |
| 826 | mResultText.setTranslationX(0.0f); |
| 827 | mResultText.setTranslationY(0.0f); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 828 | mFormulaText.setTranslationY(0.0f); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 829 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 830 | mFormulaText.requestFocus(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 833 | @Override |
| 834 | public boolean onCreateOptionsMenu(Menu menu) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 835 | super.onCreateOptionsMenu(menu); |
| 836 | |
| 837 | getMenuInflater().inflate(R.menu.activity_calculator, menu); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 838 | return true; |
| 839 | } |
| 840 | |
| 841 | @Override |
| 842 | public boolean onPrepareOptionsMenu(Menu menu) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 843 | super.onPrepareOptionsMenu(menu); |
| 844 | |
| 845 | // Show the leading option when displaying a result. |
| 846 | menu.findItem(R.id.menu_leading).setVisible(mCurrentState == CalculatorState.RESULT); |
| 847 | |
| 848 | // Show the fraction option when displaying a rational result. |
| 849 | menu.findItem(R.id.menu_fraction).setVisible(mCurrentState == CalculatorState.RESULT |
| 850 | && mEvaluator.getRational() != null); |
| 851 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 852 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | @Override |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 856 | public boolean onOptionsItemSelected(MenuItem item) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 857 | switch (item.getItemId()) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 858 | case R.id.menu_leading: |
| 859 | displayFull(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 860 | return true; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 861 | case R.id.menu_fraction: |
| 862 | displayFraction(); |
| 863 | return true; |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 864 | case R.id.menu_licenses: |
| 865 | startActivity(new Intent(this, Licenses.class)); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 866 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 867 | default: |
| 868 | return super.onOptionsItemSelected(item); |
| 869 | } |
| 870 | } |
| 871 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 872 | private void displayMessage(String s) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 873 | new AlertDialog.Builder(this) |
| 874 | .setMessage(s) |
| 875 | .setNegativeButton(R.string.dismiss, null /* listener */) |
| 876 | .show(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 879 | private void displayFraction() { |
| 880 | BoundedRational result = mEvaluator.getRational(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 881 | displayMessage(KeyMaps.translateResult(result.toNiceString())); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | // Display full result to currently evaluated precision |
| 885 | private void displayFull() { |
| 886 | Resources res = getResources(); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 887 | String msg = mResultText.getFullText() + " "; |
| 888 | if (mResultText.fullTextIsExact()) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 889 | msg += res.getString(R.string.exact); |
| 890 | } else { |
| 891 | msg += res.getString(R.string.approximate); |
| 892 | } |
| 893 | displayMessage(msg); |
| 894 | } |
| 895 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 896 | /** |
| 897 | * Add input characters to the end of the expression. |
| 898 | * Map them to the appropriate button pushes when possible. Leftover characters |
| 899 | * are added to mUnprocessedChars, which is presumed to immediately precede the newly |
| 900 | * added characters. |
| 901 | * @param moreChars Characters to be added. |
| 902 | * @param explicit These characters were explicitly typed by the user. |
| 903 | */ |
| 904 | private void addChars(String moreChars, boolean explicit) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 905 | if (mUnprocessedChars != null) { |
| 906 | moreChars = mUnprocessedChars + moreChars; |
| 907 | } |
| 908 | int current = 0; |
| 909 | int len = moreChars.length(); |
| 910 | while (current < len) { |
| 911 | char c = moreChars.charAt(current); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 912 | int k = KeyMaps.keyForChar(c); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 913 | if (k != View.NO_ID) { |
| 914 | mCurrentButton = findViewById(k); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 915 | if (explicit) { |
| 916 | addExplicitKeyToExpr(k); |
| 917 | } else { |
| 918 | addKeyToExpr(k); |
| 919 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 920 | if (Character.isSurrogate(c)) { |
| 921 | current += 2; |
| 922 | } else { |
| 923 | ++current; |
| 924 | } |
| 925 | continue; |
| 926 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 927 | int f = KeyMaps.funForString(moreChars, current); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 928 | if (f != View.NO_ID) { |
| 929 | mCurrentButton = findViewById(f); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 930 | if (explicit) { |
| 931 | addExplicitKeyToExpr(f); |
| 932 | } else { |
| 933 | addKeyToExpr(f); |
| 934 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 935 | if (f == R.id.op_sqrt) { |
| 936 | // Square root entered as function; don't lose the parenthesis. |
| 937 | addKeyToExpr(R.id.lparen); |
| 938 | } |
| 939 | current = moreChars.indexOf('(', current) + 1; |
| 940 | continue; |
| 941 | } |
| 942 | // There are characters left, but we can't convert them to button presses. |
| 943 | mUnprocessedChars = moreChars.substring(current); |
| 944 | redisplayAfterFormulaChange(); |
| 945 | return; |
| 946 | } |
| 947 | mUnprocessedChars = null; |
| 948 | redisplayAfterFormulaChange(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 951 | @Override |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 952 | public boolean onPaste(ClipData clip) { |
| 953 | final ClipData.Item item = clip.getItemCount() == 0 ? null : clip.getItemAt(0); |
| 954 | if (item == null) { |
| 955 | // nothing to paste, bail early... |
| 956 | return false; |
| 957 | } |
| 958 | |
| 959 | // Check if the item is a previously copied result, otherwise paste as raw text. |
| 960 | final Uri uri = item.getUri(); |
| 961 | if (uri != null && mEvaluator.isLastSaved(uri)) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 962 | if (mCurrentState == CalculatorState.ERROR |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 963 | || mCurrentState == CalculatorState.RESULT) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 964 | setState(CalculatorState.INPUT); |
| 965 | mEvaluator.clear(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 966 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 967 | mEvaluator.addSaved(); |
| 968 | redisplayAfterFormulaChange(); |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 969 | } else { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 970 | addChars(item.coerceToText(this).toString(), false); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 971 | } |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 972 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 973 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 974 | } |