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