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