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