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); |
Annie Chin | 947d93b | 2016-06-14 10:18:54 -0700 | [diff] [blame^] | 362 | } else if (mCurrentState == CalculatorState.ERROR) { |
| 363 | mFormulaText.setVisibility(View.INVISIBLE); |
| 364 | mResultText.setVisibility(View.VISIBLE); |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 365 | } else { |
| 366 | mFormulaText.setVisibility(View.VISIBLE); |
| 367 | mResultText.setVisibility(View.INVISIBLE); |
| 368 | } |
| 369 | } |
| 370 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 371 | if (mCurrentState == CalculatorState.ERROR) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 372 | final int errorColor = getColor(R.color.calculator_error_color); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 373 | mFormulaText.setTextColor(errorColor); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 374 | mResultText.setTextColor(errorColor); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 375 | getWindow().setStatusBarColor(errorColor); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 376 | } else if (mCurrentState != CalculatorState.RESULT) { |
| 377 | mFormulaText.setTextColor(getColor(R.color.display_formula_text_color)); |
| 378 | mResultText.setTextColor(getColor(R.color.display_result_text_color)); |
| 379 | getWindow().setStatusBarColor(getColor(R.color.calculator_accent_color)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 380 | } |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 381 | |
| 382 | invalidateOptionsMenu(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 386 | @Override |
| 387 | public void onActionModeStarted(ActionMode mode) { |
| 388 | super.onActionModeStarted(mode); |
| 389 | if (mode.getTag() == CalculatorText.TAG_ACTION_MODE) { |
| 390 | mFormulaContainer.scrollTo(mFormulaText.getRight(), 0); |
| 391 | } |
| 392 | } |
| 393 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 394 | // Stop any active ActionMode. Return true if there was one. |
| 395 | private boolean stopActionMode() { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 396 | if (mResultText.stopActionMode()) { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | if (mFormulaText.stopActionMode()) { |
| 400 | return true; |
| 401 | } |
| 402 | return false; |
| 403 | } |
| 404 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 405 | @Override |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 406 | public void onUserInteraction() { |
| 407 | super.onUserInteraction(); |
| 408 | |
| 409 | // If there's an animation in progress, end it immediately, so the user interaction can |
| 410 | // be handled. |
| 411 | if (mCurrentAnimator != null) { |
| 412 | mCurrentAnimator.end(); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | @Override |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 417 | public void onBackPressed() { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 418 | if (!stopActionMode()) { |
| 419 | if (mPadViewPager != null && mPadViewPager.getCurrentItem() != 0) { |
| 420 | // Select the previous pad. |
| 421 | mPadViewPager.setCurrentItem(mPadViewPager.getCurrentItem() - 1); |
| 422 | } else { |
| 423 | // If the user is currently looking at the first pad (or the pad is not paged), |
| 424 | // allow the system to handle the Back button. |
| 425 | super.onBackPressed(); |
| 426 | } |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | @Override |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 431 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Justin Klaassen | 83959da | 2016-04-06 11:55:24 -0700 | [diff] [blame] | 432 | // 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] | 433 | switch (keyCode) { |
Justin Klaassen | 83959da | 2016-04-06 11:55:24 -0700 | [diff] [blame] | 434 | case KeyEvent.KEYCODE_BACK: |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 435 | case KeyEvent.KEYCODE_DPAD_UP: |
| 436 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 437 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 438 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 439 | return super.onKeyUp(keyCode, event); |
| 440 | } |
| 441 | |
Justin Klaassen | d12e062 | 2016-04-27 16:26:47 -0700 | [diff] [blame] | 442 | // Stop the action mode if it's showing. |
| 443 | stopActionMode(); |
| 444 | |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 445 | // Always cancel unrequested in-progress evaluation, so that we don't have to worry about |
| 446 | // subsequent asynchronous completion. |
| 447 | // Requested in-progress evaluations are handled below. |
| 448 | if (mCurrentState != CalculatorState.EVALUATE) { |
| 449 | mEvaluator.cancelAll(true); |
| 450 | } |
| 451 | |
| 452 | switch (keyCode) { |
| 453 | case KeyEvent.KEYCODE_NUMPAD_ENTER: |
| 454 | case KeyEvent.KEYCODE_ENTER: |
| 455 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 456 | mCurrentButton = mEqualButton; |
| 457 | onEquals(); |
| 458 | return true; |
| 459 | case KeyEvent.KEYCODE_DEL: |
| 460 | mCurrentButton = mDeleteButton; |
| 461 | onDelete(); |
| 462 | return true; |
| 463 | default: |
| 464 | cancelIfEvaluating(false); |
| 465 | final int raw = event.getKeyCharacterMap().get(keyCode, event.getMetaState()); |
| 466 | if ((raw & KeyCharacterMap.COMBINING_ACCENT) != 0) { |
| 467 | return true; // discard |
| 468 | } |
| 469 | // Try to discard non-printing characters and the like. |
| 470 | // The user will have to explicitly delete other junk that gets past us. |
| 471 | if (Character.isIdentifierIgnorable(raw) || Character.isWhitespace(raw)) { |
| 472 | return true; |
| 473 | } |
| 474 | char c = (char) raw; |
| 475 | if (c == '=') { |
| 476 | mCurrentButton = mEqualButton; |
| 477 | onEquals(); |
| 478 | } else { |
| 479 | addChars(String.valueOf(c), true); |
| 480 | redisplayAfterFormulaChange(); |
| 481 | } |
| 482 | return true; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 486 | /** |
| 487 | * Invoked whenever the inverse button is toggled to update the UI. |
| 488 | * |
| 489 | * @param showInverse {@code true} if inverse functions should be shown |
| 490 | */ |
| 491 | private void onInverseToggled(boolean showInverse) { |
Justin Klaassen | 3e223ea | 2016-02-05 14:18:06 -0800 | [diff] [blame] | 492 | mInverseToggle.setSelected(showInverse); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 493 | if (showInverse) { |
| 494 | mInverseToggle.setContentDescription(getString(R.string.desc_inv_on)); |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 495 | for (View invertibleButton : mInvertibleButtons) { |
| 496 | invertibleButton.setVisibility(View.GONE); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 497 | } |
| 498 | for (View inverseButton : mInverseButtons) { |
| 499 | inverseButton.setVisibility(View.VISIBLE); |
| 500 | } |
| 501 | } else { |
| 502 | mInverseToggle.setContentDescription(getString(R.string.desc_inv_off)); |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 503 | for (View invertibleButton : mInvertibleButtons) { |
| 504 | invertibleButton.setVisibility(View.VISIBLE); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 505 | } |
| 506 | for (View inverseButton : mInverseButtons) { |
| 507 | inverseButton.setVisibility(View.GONE); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Invoked whenever the deg/rad mode may have changed to update the UI. |
| 514 | * |
| 515 | * @param degreeMode {@code true} if in degree mode |
| 516 | */ |
| 517 | private void onModeChanged(boolean degreeMode) { |
| 518 | if (degreeMode) { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 519 | mModeView.setText(R.string.mode_deg); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 520 | mModeView.setContentDescription(getString(R.string.desc_mode_deg)); |
| 521 | |
| 522 | mModeToggle.setText(R.string.mode_rad); |
| 523 | mModeToggle.setContentDescription(getString(R.string.desc_switch_rad)); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 524 | } else { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 525 | mModeView.setText(R.string.mode_rad); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 526 | mModeView.setContentDescription(getString(R.string.desc_mode_rad)); |
| 527 | |
| 528 | mModeToggle.setText(R.string.mode_deg); |
| 529 | mModeToggle.setContentDescription(getString(R.string.desc_switch_deg)); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 530 | } |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 531 | |
| 532 | // Show the toolbar to highlight the mode change. |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 533 | showAndMaybeHideToolbar(); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 534 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 535 | |
Hans Boehm | 5d79d10 | 2015-09-16 16:33:47 -0700 | [diff] [blame] | 536 | /** |
| 537 | * Switch to INPUT from RESULT state in response to input of the specified button_id. |
| 538 | * View.NO_ID is treated as an incomplete function id. |
| 539 | */ |
| 540 | private void switchToInput(int button_id) { |
| 541 | if (KeyMaps.isBinary(button_id) || KeyMaps.isSuffix(button_id)) { |
| 542 | mEvaluator.collapse(); |
| 543 | } else { |
| 544 | announceClearedForAccessibility(); |
| 545 | mEvaluator.clear(); |
| 546 | } |
| 547 | setState(CalculatorState.INPUT); |
| 548 | } |
| 549 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 550 | // Add the given button id to input expression. |
| 551 | // If appropriate, clear the expression before doing so. |
| 552 | private void addKeyToExpr(int id) { |
| 553 | if (mCurrentState == CalculatorState.ERROR) { |
| 554 | setState(CalculatorState.INPUT); |
| 555 | } else if (mCurrentState == CalculatorState.RESULT) { |
Hans Boehm | 5d79d10 | 2015-09-16 16:33:47 -0700 | [diff] [blame] | 556 | switchToInput(id); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 557 | } |
| 558 | if (!mEvaluator.append(id)) { |
| 559 | // TODO: Some user visible feedback? |
| 560 | } |
| 561 | } |
| 562 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 563 | /** |
| 564 | * Add the given button id to input expression, assuming it was explicitly |
| 565 | * typed/touched. |
| 566 | * We perform slightly more aggressive correction than in pasted expressions. |
| 567 | */ |
| 568 | private void addExplicitKeyToExpr(int id) { |
| 569 | if (mCurrentState == CalculatorState.INPUT && id == R.id.op_sub) { |
| 570 | mEvaluator.getExpr().removeTrailingAdditiveOperators(); |
| 571 | } |
| 572 | addKeyToExpr(id); |
| 573 | } |
| 574 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 575 | private void redisplayAfterFormulaChange() { |
| 576 | // TODO: Could do this more incrementally. |
| 577 | redisplayFormula(); |
| 578 | setState(CalculatorState.INPUT); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 579 | if (haveUnprocessed()) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 580 | mResultText.clear(); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 581 | // Force reevaluation when text is deleted, even if expression is unchanged. |
| 582 | mEvaluator.touch(); |
| 583 | } else { |
| 584 | if (mEvaluator.getExpr().hasInterestingOps()) { |
| 585 | mEvaluator.evaluateAndShowResult(); |
| 586 | } else { |
| 587 | mResultText.clear(); |
| 588 | } |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 589 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 592 | /** |
| 593 | * Show the toolbar. |
| 594 | * Automatically hide it again if it's not relevant to current formula. |
| 595 | */ |
| 596 | private void showAndMaybeHideToolbar() { |
| 597 | final boolean shouldBeVisible = |
| 598 | mCurrentState == CalculatorState.INPUT && mEvaluator.hasTrigFuncs(); |
| 599 | mDisplayView.showToolbar(!shouldBeVisible); |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Display or hide the toolbar depending on calculator state. |
| 604 | */ |
| 605 | private void showOrHideToolbar() { |
| 606 | final boolean shouldBeVisible = |
| 607 | mCurrentState == CalculatorState.INPUT && mEvaluator.hasTrigFuncs(); |
| 608 | if (shouldBeVisible) { |
| 609 | mDisplayView.showToolbar(false); |
| 610 | } else { |
| 611 | mDisplayView.hideToolbar(); |
| 612 | } |
| 613 | } |
| 614 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 615 | public void onButtonClick(View view) { |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 616 | // Any animation is ended before we get here. |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 617 | mCurrentButton = view; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 618 | stopActionMode(); |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 619 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 620 | // See onKey above for the rationale behind some of the behavior below: |
| 621 | if (mCurrentState != CalculatorState.EVALUATE) { |
| 622 | // Cancel evaluations that were not specifically requested. |
| 623 | mEvaluator.cancelAll(true); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 624 | } |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 625 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 626 | final int id = view.getId(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 627 | switch (id) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 628 | case R.id.eq: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 629 | onEquals(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 630 | break; |
| 631 | case R.id.del: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 632 | onDelete(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 633 | break; |
| 634 | case R.id.clr: |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 635 | onClear(); |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 636 | return; // Toolbar visibility adjusted at end of animation. |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 637 | case R.id.toggle_inv: |
| 638 | final boolean selected = !mInverseToggle.isSelected(); |
| 639 | mInverseToggle.setSelected(selected); |
| 640 | onInverseToggled(selected); |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 641 | if (mCurrentState == CalculatorState.RESULT) { |
| 642 | mResultText.redisplay(); // In case we cancelled reevaluation. |
| 643 | } |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 644 | break; |
| 645 | case R.id.toggle_mode: |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 646 | cancelIfEvaluating(false); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 647 | final boolean mode = !mEvaluator.getDegreeMode(); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 648 | if (mCurrentState == CalculatorState.RESULT) { |
| 649 | mEvaluator.collapse(); // Capture result evaluated in old mode |
| 650 | redisplayFormula(); |
| 651 | } |
| 652 | // In input mode, we reinterpret already entered trig functions. |
| 653 | mEvaluator.setDegreeMode(mode); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 654 | onModeChanged(mode); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 655 | setState(CalculatorState.INPUT); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 656 | mResultText.clear(); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 657 | if (!haveUnprocessed() && mEvaluator.getExpr().hasInterestingOps()) { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 658 | mEvaluator.evaluateAndShowResult(); |
| 659 | } |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 660 | return; // onModeChanged adjusted toolbar visibility. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 661 | default: |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 662 | cancelIfEvaluating(false); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 663 | if (haveUnprocessed()) { |
| 664 | // For consistency, append as uninterpreted characters. |
| 665 | // This may actually be useful for a left parenthesis. |
| 666 | addChars(KeyMaps.toString(this, id), true); |
| 667 | } else { |
| 668 | addExplicitKeyToExpr(id); |
| 669 | redisplayAfterFormulaChange(); |
| 670 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 671 | break; |
| 672 | } |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 673 | showOrHideToolbar(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 676 | void redisplayFormula() { |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 677 | SpannableStringBuilder formula = mEvaluator.getExpr().toSpannableStringBuilder(this); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 678 | if (mUnprocessedChars != null) { |
| 679 | // Add and highlight characters we couldn't process. |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 680 | formula.append(mUnprocessedChars, mUnprocessedColorSpan, |
| 681 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 682 | } |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 683 | mFormulaText.changeTextTo(formula); |
Annie Chin | f360ef0 | 2016-03-10 13:45:39 -0800 | [diff] [blame] | 684 | mFormulaText.setContentDescription(TextUtils.isEmpty(formula) |
| 685 | ? getString(R.string.desc_formula) : formula.toString()); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 688 | @Override |
| 689 | public boolean onLongClick(View view) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 690 | mCurrentButton = view; |
| 691 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 692 | if (view.getId() == R.id.del) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 693 | onClear(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 694 | return true; |
| 695 | } |
| 696 | return false; |
| 697 | } |
| 698 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 699 | // Initial evaluation completed successfully. Initiate display. |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 700 | public void onEvaluate(int initDisplayPrec, int msd, int leastDigPos, |
| 701 | String truncatedWholeNumber) { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 702 | // Invalidate any options that may depend on the current result. |
| 703 | invalidateOptionsMenu(); |
| 704 | |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 705 | mResultText.displayResult(initDisplayPrec, msd, leastDigPos, truncatedWholeNumber); |
Hans Boehm | 61568a1 | 2015-05-18 18:25:41 -0700 | [diff] [blame] | 706 | if (mCurrentState != CalculatorState.INPUT) { // in EVALUATE or INIT state |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 707 | onResult(mCurrentState != CalculatorState.INIT); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 708 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 709 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 710 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 711 | // Reset state to reflect evaluator cancellation. Invoked by evaluator. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 712 | public void onCancelled() { |
| 713 | // We should be in EVALUATE state. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 714 | setState(CalculatorState.INPUT); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 715 | mResultText.clear(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | // Reevaluation completed; ask result to redisplay current value. |
| 719 | public void onReevaluate() |
| 720 | { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 721 | mResultText.redisplay(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 724 | @Override |
| 725 | public void onTextSizeChanged(final TextView textView, float oldSize) { |
| 726 | if (mCurrentState != CalculatorState.INPUT) { |
| 727 | // Only animate text changes that occur from user input. |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | // Calculate the values needed to perform the scale and translation animations, |
| 732 | // maintaining the same apparent baseline for the displayed text. |
| 733 | final float textScale = oldSize / textView.getTextSize(); |
| 734 | final float translationX = (1.0f - textScale) * |
| 735 | (textView.getWidth() / 2.0f - textView.getPaddingEnd()); |
| 736 | final float translationY = (1.0f - textScale) * |
| 737 | (textView.getHeight() / 2.0f - textView.getPaddingBottom()); |
| 738 | |
| 739 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 740 | animatorSet.playTogether( |
| 741 | ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f), |
| 742 | ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f), |
| 743 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f), |
| 744 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f)); |
Justin Klaassen | 94db720 | 2014-06-11 11:22:31 -0700 | [diff] [blame] | 745 | animatorSet.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 746 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 747 | animatorSet.start(); |
| 748 | } |
| 749 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 750 | /** |
| 751 | * Cancel any in-progress explicitly requested evaluations. |
| 752 | * @param quiet suppress pop-up message. Explicit evaluation can change the expression |
| 753 | value, and certainly changes the display, so it seems reasonable to warn. |
| 754 | * @return true if there was such an evaluation |
| 755 | */ |
| 756 | private boolean cancelIfEvaluating(boolean quiet) { |
| 757 | if (mCurrentState == CalculatorState.EVALUATE) { |
| 758 | mEvaluator.cancelAll(quiet); |
| 759 | return true; |
| 760 | } else { |
| 761 | return false; |
| 762 | } |
| 763 | } |
| 764 | |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 765 | private boolean haveUnprocessed() { |
| 766 | return mUnprocessedChars != null && !mUnprocessedChars.isEmpty(); |
| 767 | } |
| 768 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 769 | private void onEquals() { |
Hans Boehm | 56d6e76 | 2016-06-06 11:46:29 -0700 | [diff] [blame] | 770 | // Ignore if in non-INPUT state, or if there are no operators. |
| 771 | if (mCurrentState == CalculatorState.INPUT && mEvaluator.getExpr().hasInterestingOps()) { |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 772 | setState(CalculatorState.EVALUATE); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 773 | if (haveUnprocessed()) { |
| 774 | onError(R.string.error_syntax); |
| 775 | } else { |
| 776 | mEvaluator.requireResult(); |
| 777 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
| 781 | private void onDelete() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 782 | // Delete works like backspace; remove the last character or operator from the expression. |
| 783 | // Note that we handle keyboard delete exactly like the delete button. For |
| 784 | // example the delete button can be used to delete a character from an incomplete |
| 785 | // function name typed on a physical keyboard. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 786 | // This should be impossible in RESULT state. |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 787 | // If there is an in-progress explicit evaluation, just cancel it and return. |
| 788 | if (cancelIfEvaluating(false)) return; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 789 | setState(CalculatorState.INPUT); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 790 | if (haveUnprocessed()) { |
| 791 | mUnprocessedChars = mUnprocessedChars.substring(0, mUnprocessedChars.length() - 1); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 792 | } else { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 793 | mEvaluator.delete(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 794 | } |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 795 | if (mEvaluator.getExpr().isEmpty() && !haveUnprocessed()) { |
Hans Boehm | db6f999 | 2015-08-19 12:32:56 -0700 | [diff] [blame] | 796 | // Resulting formula won't be announced, since it's empty. |
| 797 | announceClearedForAccessibility(); |
| 798 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 799 | redisplayAfterFormulaChange(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 802 | private void reveal(View sourceView, int colorRes, AnimatorListener listener) { |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 803 | final ViewGroupOverlay groupOverlay = |
| 804 | (ViewGroupOverlay) getWindow().getDecorView().getOverlay(); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 805 | |
| 806 | final Rect displayRect = new Rect(); |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 807 | mDisplayView.getGlobalVisibleRect(displayRect); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 808 | |
| 809 | // Make reveal cover the display and status bar. |
| 810 | final View revealView = new View(this); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 811 | revealView.setBottom(displayRect.bottom); |
| 812 | revealView.setLeft(displayRect.left); |
| 813 | revealView.setRight(displayRect.right); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 814 | revealView.setBackgroundColor(getColor(colorRes)); |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 815 | groupOverlay.add(revealView); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 816 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 817 | final int[] clearLocation = new int[2]; |
| 818 | sourceView.getLocationInWindow(clearLocation); |
| 819 | clearLocation[0] += sourceView.getWidth() / 2; |
| 820 | clearLocation[1] += sourceView.getHeight() / 2; |
| 821 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 822 | final int revealCenterX = clearLocation[0] - revealView.getLeft(); |
| 823 | final int revealCenterY = clearLocation[1] - revealView.getTop(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 824 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 825 | final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2); |
| 826 | final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2); |
| 827 | final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 828 | final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2)); |
| 829 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 830 | final Animator revealAnimator = |
| 831 | ViewAnimationUtils.createCircularReveal(revealView, |
ztenghui | 3d6ecaf | 2014-06-05 09:56:00 -0700 | [diff] [blame] | 832 | revealCenterX, revealCenterY, 0.0f, revealRadius); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 833 | revealAnimator.setDuration( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 834 | getResources().getInteger(android.R.integer.config_longAnimTime)); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 835 | revealAnimator.addListener(listener); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 836 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 837 | final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 838 | alphaAnimator.setDuration( |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 839 | getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 840 | |
| 841 | final AnimatorSet animatorSet = new AnimatorSet(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 842 | animatorSet.play(revealAnimator).before(alphaAnimator); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 843 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 844 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 845 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 846 | public void onAnimationEnd(Animator animator) { |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 847 | groupOverlay.remove(revealView); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 848 | mCurrentAnimator = null; |
| 849 | } |
| 850 | }); |
| 851 | |
| 852 | mCurrentAnimator = animatorSet; |
| 853 | animatorSet.start(); |
| 854 | } |
| 855 | |
Hans Boehm | db6f999 | 2015-08-19 12:32:56 -0700 | [diff] [blame] | 856 | private void announceClearedForAccessibility() { |
| 857 | mResultText.announceForAccessibility(getResources().getString(R.string.cleared)); |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 860 | private void onClear() { |
Justin Klaassen | 1a428cf | 2016-02-24 15:58:18 -0800 | [diff] [blame] | 861 | if (mEvaluator.getExpr().isEmpty() && !haveUnprocessed()) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 862 | return; |
| 863 | } |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 864 | cancelIfEvaluating(true); |
Hans Boehm | db6f999 | 2015-08-19 12:32:56 -0700 | [diff] [blame] | 865 | announceClearedForAccessibility(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 866 | reveal(mCurrentButton, R.color.calculator_accent_color, new AnimatorListenerAdapter() { |
| 867 | @Override |
| 868 | public void onAnimationEnd(Animator animation) { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 869 | mUnprocessedChars = null; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 870 | mResultText.clear(); |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 871 | mEvaluator.clear(); |
| 872 | setState(CalculatorState.INPUT); |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 873 | showOrHideToolbar(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 874 | redisplayFormula(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 875 | } |
| 876 | }); |
| 877 | } |
| 878 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 879 | // Evaluation encountered en error. Display the error. |
| 880 | void onError(final int errorResourceId) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 881 | if (mCurrentState == CalculatorState.EVALUATE) { |
| 882 | setState(CalculatorState.ANIMATE); |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 883 | mResultText.announceForAccessibility(getResources().getString(errorResourceId)); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 884 | reveal(mCurrentButton, R.color.calculator_error_color, |
| 885 | new AnimatorListenerAdapter() { |
| 886 | @Override |
| 887 | public void onAnimationEnd(Animator animation) { |
| 888 | setState(CalculatorState.ERROR); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 889 | mResultText.displayError(errorResourceId); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 890 | } |
| 891 | }); |
| 892 | } else if (mCurrentState == CalculatorState.INIT) { |
| 893 | setState(CalculatorState.ERROR); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 894 | mResultText.displayError(errorResourceId); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 895 | } else { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 896 | mResultText.clear(); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 897 | } |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 900 | // Animate movement of result into the top formula slot. |
| 901 | // Result window now remains translated in the top slot while the result is displayed. |
| 902 | // (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] | 903 | // Historical note: In the Lollipop version, this invisibly and instantaneously moved |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 904 | // formula and result displays back at the end of the animation. We no longer do that, |
| 905 | // so that we can continue to properly support scrolling of the result. |
| 906 | // We assume the result already contains the text to be expanded. |
| 907 | private void onResult(boolean animate) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 908 | // Calculate the textSize that would be used to display the result in the formula. |
| 909 | // For scrollable results just use the minimum textSize to maximize the number of digits |
| 910 | // that are visible on screen. |
| 911 | float textSize = mFormulaText.getMinimumTextSize(); |
| 912 | if (!mResultText.isScrollable()) { |
| 913 | textSize = mFormulaText.getVariableTextSize(mResultText.getText().toString()); |
| 914 | } |
| 915 | |
| 916 | // Scale the result to match the calculated textSize, minimizing the jump-cut transition |
| 917 | // when a result is reused in a subsequent expression. |
| 918 | final float resultScale = textSize / mResultText.getTextSize(); |
| 919 | |
| 920 | // Set the result's pivot to match its gravity. |
| 921 | mResultText.setPivotX(mResultText.getWidth() - mResultText.getPaddingRight()); |
| 922 | mResultText.setPivotY(mResultText.getHeight() - mResultText.getPaddingBottom()); |
| 923 | |
| 924 | // Calculate the necessary translations so the result takes the place of the formula and |
| 925 | // the formula moves off the top of the screen. |
Annie Chin | 28589dc | 2016-06-09 17:50:51 -0700 | [diff] [blame] | 926 | final float resultTranslationY = (mFormulaContainer.getBottom() - mResultText.getBottom()) |
| 927 | - (mFormulaText.getPaddingBottom() - mResultText.getPaddingBottom()); |
| 928 | float formulaTranslationY = -mFormulaContainer.getBottom(); |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 929 | if (mOneLine) { |
| 930 | // Position the result text. |
| 931 | mResultText.setY(mResultText.getBottom()); |
Annie Chin | 28589dc | 2016-06-09 17:50:51 -0700 | [diff] [blame] | 932 | formulaTranslationY = -(findViewById(R.id.toolbar).getBottom() |
| 933 | + mFormulaContainer.getBottom()); |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 934 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 935 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 936 | // Change the result's textColor to match the formula. |
| 937 | final int formulaTextColor = mFormulaText.getCurrentTextColor(); |
| 938 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 939 | if (animate) { |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 940 | mResultText.announceForAccessibility(getResources().getString(R.string.desc_eq)); |
| 941 | mResultText.announceForAccessibility(mResultText.getText()); |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 942 | setState(CalculatorState.ANIMATE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 943 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 944 | animatorSet.playTogether( |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 945 | ObjectAnimator.ofPropertyValuesHolder(mResultText, |
| 946 | PropertyValuesHolder.ofFloat(View.SCALE_X, resultScale), |
| 947 | PropertyValuesHolder.ofFloat(View.SCALE_Y, resultScale), |
| 948 | PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, resultTranslationY)), |
| 949 | ObjectAnimator.ofArgb(mResultText, TEXT_COLOR, formulaTextColor), |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 950 | ObjectAnimator.ofFloat(mFormulaContainer, View.TRANSLATION_Y, |
| 951 | formulaTranslationY)); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 952 | animatorSet.setDuration(getResources().getInteger( |
| 953 | android.R.integer.config_longAnimTime)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 954 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 955 | @Override |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 956 | public void onAnimationEnd(Animator animation) { |
| 957 | setState(CalculatorState.RESULT); |
| 958 | mCurrentAnimator = null; |
| 959 | } |
| 960 | }); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 961 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 962 | mCurrentAnimator = animatorSet; |
| 963 | animatorSet.start(); |
| 964 | } else /* No animation desired; get there fast, e.g. when restarting */ { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 965 | mResultText.setScaleX(resultScale); |
| 966 | mResultText.setScaleY(resultScale); |
| 967 | mResultText.setTranslationY(resultTranslationY); |
| 968 | mResultText.setTextColor(formulaTextColor); |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 969 | mFormulaContainer.setTranslationY(formulaTranslationY); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 970 | setState(CalculatorState.RESULT); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 971 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 972 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 973 | |
| 974 | // Restore positions of the formula and result displays back to their original, |
| 975 | // pre-animation state. |
| 976 | private void restoreDisplayPositions() { |
| 977 | // Clear result. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 978 | mResultText.setText(""); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 979 | // Reset all of the values modified during the animation. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 980 | mResultText.setScaleX(1.0f); |
| 981 | mResultText.setScaleY(1.0f); |
| 982 | mResultText.setTranslationX(0.0f); |
| 983 | mResultText.setTranslationY(0.0f); |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 984 | mFormulaContainer.setTranslationY(0.0f); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 985 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 986 | mFormulaText.requestFocus(); |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | @Override |
| 990 | public void onClick(AlertDialogFragment fragment, int which) { |
| 991 | if (which == DialogInterface.BUTTON_POSITIVE) { |
| 992 | // Timeout extension request. |
| 993 | mEvaluator.setLongTimeOut(); |
| 994 | } |
| 995 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 996 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 997 | @Override |
| 998 | public boolean onCreateOptionsMenu(Menu menu) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 999 | super.onCreateOptionsMenu(menu); |
| 1000 | |
| 1001 | getMenuInflater().inflate(R.menu.activity_calculator, menu); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | @Override |
| 1006 | public boolean onPrepareOptionsMenu(Menu menu) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1007 | super.onPrepareOptionsMenu(menu); |
| 1008 | |
| 1009 | // Show the leading option when displaying a result. |
| 1010 | menu.findItem(R.id.menu_leading).setVisible(mCurrentState == CalculatorState.RESULT); |
| 1011 | |
| 1012 | // Show the fraction option when displaying a rational result. |
| 1013 | menu.findItem(R.id.menu_fraction).setVisible(mCurrentState == CalculatorState.RESULT |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 1014 | && mEvaluator.getResult().exactlyDisplayable()); |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1015 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 1016 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | @Override |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 1020 | public boolean onOptionsItemSelected(MenuItem item) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1021 | switch (item.getItemId()) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1022 | case R.id.menu_leading: |
| 1023 | displayFull(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1024 | return true; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1025 | case R.id.menu_fraction: |
| 1026 | displayFraction(); |
| 1027 | return true; |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1028 | case R.id.menu_licenses: |
| 1029 | startActivity(new Intent(this, Licenses.class)); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1030 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1031 | default: |
| 1032 | return super.onOptionsItemSelected(item); |
| 1033 | } |
| 1034 | } |
| 1035 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1036 | private void displayMessage(String s) { |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 1037 | AlertDialogFragment.showMessageDialog(this, s, null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1040 | private void displayFraction() { |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 1041 | UnifiedReal result = mEvaluator.getResult(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 1042 | displayMessage(KeyMaps.translateResult(result.toNiceString())); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | // Display full result to currently evaluated precision |
| 1046 | private void displayFull() { |
| 1047 | Resources res = getResources(); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1048 | String msg = mResultText.getFullText() + " "; |
| 1049 | if (mResultText.fullTextIsExact()) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1050 | msg += res.getString(R.string.exact); |
| 1051 | } else { |
| 1052 | msg += res.getString(R.string.approximate); |
| 1053 | } |
| 1054 | displayMessage(msg); |
| 1055 | } |
| 1056 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1057 | /** |
| 1058 | * Add input characters to the end of the expression. |
| 1059 | * Map them to the appropriate button pushes when possible. Leftover characters |
| 1060 | * are added to mUnprocessedChars, which is presumed to immediately precede the newly |
| 1061 | * added characters. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 1062 | * @param moreChars characters to be added |
| 1063 | * @param explicit these characters were explicitly typed by the user, not pasted |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1064 | */ |
| 1065 | private void addChars(String moreChars, boolean explicit) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1066 | if (mUnprocessedChars != null) { |
| 1067 | moreChars = mUnprocessedChars + moreChars; |
| 1068 | } |
| 1069 | int current = 0; |
| 1070 | int len = moreChars.length(); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 1071 | boolean lastWasDigit = false; |
Hans Boehm | 5d79d10 | 2015-09-16 16:33:47 -0700 | [diff] [blame] | 1072 | if (mCurrentState == CalculatorState.RESULT && len != 0) { |
| 1073 | // Clear display immediately for incomplete function name. |
| 1074 | switchToInput(KeyMaps.keyForChar(moreChars.charAt(current))); |
| 1075 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1076 | while (current < len) { |
| 1077 | char c = moreChars.charAt(current); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 1078 | int k = KeyMaps.keyForChar(c); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 1079 | if (!explicit) { |
| 1080 | int expEnd; |
| 1081 | if (lastWasDigit && current != |
| 1082 | (expEnd = Evaluator.exponentEnd(moreChars, current))) { |
| 1083 | // Process scientific notation with 'E' when pasting, in spite of ambiguity |
| 1084 | // with base of natural log. |
| 1085 | // Otherwise the 10^x key is the user's friend. |
| 1086 | mEvaluator.addExponent(moreChars, current, expEnd); |
| 1087 | current = expEnd; |
| 1088 | lastWasDigit = false; |
| 1089 | continue; |
| 1090 | } else { |
| 1091 | boolean isDigit = KeyMaps.digVal(k) != KeyMaps.NOT_DIGIT; |
| 1092 | if (current == 0 && (isDigit || k == R.id.dec_point) |
| 1093 | && mEvaluator.getExpr().hasTrailingConstant()) { |
| 1094 | // Refuse to concatenate pasted content to trailing constant. |
| 1095 | // This makes pasting of calculator results more consistent, whether or |
| 1096 | // not the old calculator instance is still around. |
| 1097 | addKeyToExpr(R.id.op_mul); |
| 1098 | } |
| 1099 | lastWasDigit = (isDigit || lastWasDigit && k == R.id.dec_point); |
| 1100 | } |
| 1101 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1102 | if (k != View.NO_ID) { |
| 1103 | mCurrentButton = findViewById(k); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1104 | if (explicit) { |
| 1105 | addExplicitKeyToExpr(k); |
| 1106 | } else { |
| 1107 | addKeyToExpr(k); |
| 1108 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1109 | if (Character.isSurrogate(c)) { |
| 1110 | current += 2; |
| 1111 | } else { |
| 1112 | ++current; |
| 1113 | } |
| 1114 | continue; |
| 1115 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 1116 | int f = KeyMaps.funForString(moreChars, current); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1117 | if (f != View.NO_ID) { |
| 1118 | mCurrentButton = findViewById(f); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1119 | if (explicit) { |
| 1120 | addExplicitKeyToExpr(f); |
| 1121 | } else { |
| 1122 | addKeyToExpr(f); |
| 1123 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1124 | if (f == R.id.op_sqrt) { |
| 1125 | // Square root entered as function; don't lose the parenthesis. |
| 1126 | addKeyToExpr(R.id.lparen); |
| 1127 | } |
| 1128 | current = moreChars.indexOf('(', current) + 1; |
| 1129 | continue; |
| 1130 | } |
| 1131 | // There are characters left, but we can't convert them to button presses. |
| 1132 | mUnprocessedChars = moreChars.substring(current); |
| 1133 | redisplayAfterFormulaChange(); |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 1134 | showOrHideToolbar(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1135 | return; |
| 1136 | } |
| 1137 | mUnprocessedChars = null; |
| 1138 | redisplayAfterFormulaChange(); |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 1139 | showOrHideToolbar(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1142 | @Override |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 1143 | public boolean onPaste(ClipData clip) { |
| 1144 | final ClipData.Item item = clip.getItemCount() == 0 ? null : clip.getItemAt(0); |
| 1145 | if (item == null) { |
| 1146 | // nothing to paste, bail early... |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
| 1150 | // Check if the item is a previously copied result, otherwise paste as raw text. |
| 1151 | final Uri uri = item.getUri(); |
| 1152 | if (uri != null && mEvaluator.isLastSaved(uri)) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1153 | if (mCurrentState == CalculatorState.ERROR |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 1154 | || mCurrentState == CalculatorState.RESULT) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1155 | setState(CalculatorState.INPUT); |
| 1156 | mEvaluator.clear(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1157 | } |
Hans Boehm | 3666e63 | 2015-07-27 18:33:12 -0700 | [diff] [blame] | 1158 | mEvaluator.appendSaved(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1159 | redisplayAfterFormulaChange(); |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 1160 | } else { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1161 | addChars(item.coerceToText(this).toString(), false); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1162 | } |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 1163 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1164 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1165 | } |