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