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; |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 36 | import android.app.FragmentManager; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 37 | import android.app.FragmentTransaction; |
Justin Klaassen | fc5ac82 | 2015-06-18 13:15:17 -0700 | [diff] [blame] | 38 | import android.content.ClipData; |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 39 | import android.content.DialogInterface; |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 40 | import android.content.Intent; |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 41 | import android.content.res.Resources; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 42 | import android.graphics.Color; |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 43 | import android.graphics.Rect; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 44 | import android.net.Uri; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 45 | import android.os.Bundle; |
Justin Klaassen | f79d6f6 | 2014-08-26 12:27:08 -0700 | [diff] [blame] | 46 | import android.support.annotation.NonNull; |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 47 | import android.support.annotation.StringRes; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 48 | import android.support.v4.content.ContextCompat; |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 49 | import android.support.v4.view.ViewPager; |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 50 | import android.text.Editable; |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 51 | import android.text.SpannableStringBuilder; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 52 | import android.text.Spanned; |
Annie Chin | f360ef0 | 2016-03-10 13:45:39 -0800 | [diff] [blame] | 53 | import android.text.TextUtils; |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 54 | import android.text.TextWatcher; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 55 | import android.text.style.ForegroundColorSpan; |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 56 | import android.util.Log; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 57 | import android.util.Property; |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 58 | import android.view.ActionMode; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 59 | import android.view.KeyCharacterMap; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 60 | import android.view.KeyEvent; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 61 | import android.view.Menu; |
| 62 | import android.view.MenuItem; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 63 | import android.view.MotionEvent; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 64 | import android.view.View; |
| 65 | import android.view.View.OnLongClickListener; |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 66 | import android.view.ViewAnimationUtils; |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 67 | import android.view.ViewGroupOverlay; |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 68 | import android.view.ViewTreeObserver; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 69 | import android.view.animation.AccelerateDecelerateInterpolator; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 70 | import android.widget.FrameLayout; |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 71 | import android.widget.HorizontalScrollView; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 72 | import android.widget.TextView; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 73 | import android.widget.Toolbar; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 74 | |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 75 | import com.android.calculator2.CalculatorFormula.OnTextSizeChangeListener; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 76 | |
| 77 | import java.io.ByteArrayInputStream; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 78 | import java.io.ByteArrayOutputStream; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 79 | import java.io.IOException; |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 80 | import java.io.ObjectInput; |
| 81 | import java.io.ObjectInputStream; |
| 82 | import java.io.ObjectOutput; |
| 83 | import java.io.ObjectOutputStream; |
Christine Franks | bd90b79 | 2016-11-22 10:28:26 -0800 | [diff] [blame] | 84 | import java.text.DecimalFormatSymbols; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 85 | |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 86 | import static com.android.calculator2.CalculatorFormula.OnFormulaContextMenuClickListener; |
| 87 | |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 88 | public class Calculator extends Activity |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 89 | implements OnTextSizeChangeListener, OnLongClickListener, |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame^] | 90 | AlertDialogFragment.OnClickListener, Evaluator.EvaluationListener /* for main result */, |
| 91 | DragLayout.CloseCallback, DragLayout.DragCallback { |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 92 | |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 93 | private static final String TAG = "Calculator"; |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Constant for an invalid resource id. |
| 96 | */ |
| 97 | public static final int INVALID_RES_ID = -1; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 98 | |
| 99 | private enum CalculatorState { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 100 | INPUT, // Result and formula both visible, no evaluation requested, |
| 101 | // Though result may be visible on bottom line. |
| 102 | EVALUATE, // Both visible, evaluation requested, evaluation/animation incomplete. |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 103 | // Not used for instant result evaluation. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 104 | INIT, // Very temporary state used as alternative to EVALUATE |
| 105 | // during reinitialization. Do not animate on completion. |
Hans Boehm | a5ea8eb | 2016-12-01 12:33:38 -0800 | [diff] [blame] | 106 | INIT_FOR_RESULT, // Identical to INIT, but evaluation is known to terminate |
| 107 | // with result, and current expression has been copied to history. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 108 | ANIMATE, // Result computed, animation to enlarge result window in progress. |
| 109 | RESULT, // Result displayed, formula invisible. |
| 110 | // If we are in RESULT state, the formula was evaluated without |
| 111 | // error to initial precision. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 112 | // The current formula is now also the last history entry. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 113 | ERROR // Error displayed: Formula visible, result shows error message. |
| 114 | // Display similar to INPUT state. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 115 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 116 | // Normal transition sequence is |
| 117 | // INPUT -> EVALUATE -> ANIMATE -> RESULT (or ERROR) -> INPUT |
| 118 | // A RESULT -> ERROR transition is possible in rare corner cases, in which |
| 119 | // a higher precision evaluation exposes an error. This is possible, since we |
| 120 | // initially evaluate assuming we were given a well-defined problem. If we |
| 121 | // were actually asked to compute sqrt(<extremely tiny negative number>) we produce 0 |
| 122 | // unless we are asked for enough precision that we can distinguish the argument from zero. |
Hans Boehm | a5ea8eb | 2016-12-01 12:33:38 -0800 | [diff] [blame] | 123 | // ERROR and RESULT are translated to INIT or INIT_FOR_RESULT state if the application |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 124 | // is restarted in that state. This leads us to recompute and redisplay the result |
Hans Boehm | a5ea8eb | 2016-12-01 12:33:38 -0800 | [diff] [blame] | 125 | // ASAP. We avoid saving the ANIMATE state or activating history in that state. |
| 126 | // In INIT_FOR_RESULT, and RESULT state, a copy of the current |
| 127 | // expression has been saved in the history db; in the other non-ANIMATE states, |
| 128 | // it has not. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 129 | // TODO: Possibly save a bit more information, e.g. its initial display string |
| 130 | // or most significant digit position, to speed up restart. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 131 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 132 | private final Property<TextView, Integer> TEXT_COLOR = |
| 133 | new Property<TextView, Integer>(Integer.class, "textColor") { |
| 134 | @Override |
| 135 | public Integer get(TextView textView) { |
| 136 | return textView.getCurrentTextColor(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void set(TextView textView, Integer textColor) { |
| 141 | textView.setTextColor(textColor); |
| 142 | } |
| 143 | }; |
| 144 | |
Justin Klaassen | 3e223ea | 2016-02-05 14:18:06 -0800 | [diff] [blame] | 145 | private static final String NAME = "Calculator"; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 146 | private static final String KEY_DISPLAY_STATE = NAME + "_display_state"; |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 147 | private static final String KEY_UNPROCESSED_CHARS = NAME + "_unprocessed_chars"; |
Justin Klaassen | 3e223ea | 2016-02-05 14:18:06 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Associated value is a byte array holding the evaluator state. |
| 150 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 151 | private static final String KEY_EVAL_STATE = NAME + "_eval_state"; |
Justin Klaassen | 3e223ea | 2016-02-05 14:18:06 -0800 | [diff] [blame] | 152 | private static final String KEY_INVERSE_MODE = NAME + "_inverse_mode"; |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Associated value is an boolean holding the visibility state of the toolbar. |
| 155 | */ |
| 156 | private static final String KEY_SHOW_TOOLBAR = NAME + "_show_toolbar"; |
Justin Klaassen | 741471e | 2014-06-11 09:43:44 -0700 | [diff] [blame] | 157 | |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 158 | private final ViewTreeObserver.OnPreDrawListener mPreDrawListener = |
| 159 | new ViewTreeObserver.OnPreDrawListener() { |
| 160 | @Override |
| 161 | public boolean onPreDraw() { |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 162 | mFormulaContainer.scrollTo(mFormulaText.getRight(), 0); |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 163 | final ViewTreeObserver observer = mFormulaContainer.getViewTreeObserver(); |
| 164 | if (observer.isAlive()) { |
| 165 | observer.removeOnPreDrawListener(this); |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | }; |
| 170 | |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 171 | private final Evaluator.Callback mEvaluatorCallback = new Evaluator.Callback() { |
| 172 | @Override |
| 173 | public void onMemoryStateChanged() { |
| 174 | mFormulaText.onMemoryStateChanged(); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void showMessageDialog(@StringRes int title, @StringRes int message, |
| 179 | @StringRes int positiveButtonLabel, String tag) { |
| 180 | AlertDialogFragment.showMessageDialog(Calculator.this, title, message, |
| 181 | positiveButtonLabel, tag); |
| 182 | |
| 183 | } |
| 184 | }; |
| 185 | |
| 186 | private final OnDisplayMemoryOperationsListener mOnDisplayMemoryOperationsListener = |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 187 | new OnDisplayMemoryOperationsListener() { |
| 188 | @Override |
| 189 | public boolean shouldDisplayMemory() { |
| 190 | return mEvaluator.getMemoryIndex() != 0; |
| 191 | } |
| 192 | }; |
| 193 | |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 194 | private final OnFormulaContextMenuClickListener mOnFormulaContextMenuClickListener = |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 195 | new OnFormulaContextMenuClickListener() { |
| 196 | @Override |
| 197 | public boolean onPaste(ClipData clip) { |
| 198 | final ClipData.Item item = clip.getItemCount() == 0 ? null : clip.getItemAt(0); |
| 199 | if (item == null) { |
| 200 | // nothing to paste, bail early... |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | // Check if the item is a previously copied result, otherwise paste as raw text. |
| 205 | final Uri uri = item.getUri(); |
| 206 | if (uri != null && mEvaluator.isLastSaved(uri)) { |
| 207 | clearIfNotInputState(); |
| 208 | mEvaluator.appendExpr(mEvaluator.getSavedIndex()); |
| 209 | redisplayAfterFormulaChange(); |
| 210 | } else { |
| 211 | addChars(item.coerceToText(Calculator.this).toString(), false); |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void onMemoryRecall() { |
| 218 | clearIfNotInputState(); |
| 219 | long memoryIndex = mEvaluator.getMemoryIndex(); |
| 220 | if (memoryIndex != 0) { |
| 221 | mEvaluator.appendExpr(mEvaluator.getMemoryIndex()); |
| 222 | redisplayAfterFormulaChange(); |
Hans Boehm | cc36850 | 2016-12-09 10:44:46 -0800 | [diff] [blame] | 223 | } |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 224 | } |
| 225 | }; |
| 226 | |
| 227 | |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 228 | private final TextWatcher mFormulaTextWatcher = new TextWatcher() { |
| 229 | @Override |
| 230 | public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onTextChanged(CharSequence charSequence, int start, int count, int after) { |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void afterTextChanged(Editable editable) { |
| 239 | final ViewTreeObserver observer = mFormulaContainer.getViewTreeObserver(); |
| 240 | if (observer.isAlive()) { |
| 241 | observer.removeOnPreDrawListener(mPreDrawListener); |
| 242 | observer.addOnPreDrawListener(mPreDrawListener); |
| 243 | } |
| 244 | } |
| 245 | }; |
| 246 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 247 | private CalculatorState mCurrentState; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 248 | private Evaluator mEvaluator; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 249 | |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 250 | private CalculatorDisplay mDisplayView; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 251 | private TextView mModeView; |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 252 | private CalculatorFormula mFormulaText; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 253 | private CalculatorResult mResultText; |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 254 | private HorizontalScrollView mFormulaContainer; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 255 | private DragLayout mDragLayout; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 256 | private FrameLayout mHistoryFrame; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 257 | |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 258 | private ViewPager mPadViewPager; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 259 | private View mDeleteButton; |
| 260 | private View mClearButton; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 261 | private View mEqualButton; |
Annie Chin | eb36f95 | 2016-12-08 17:27:19 -0800 | [diff] [blame] | 262 | private View mMainCalculator; |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 263 | |
| 264 | private TextView mInverseToggle; |
| 265 | private TextView mModeToggle; |
| 266 | |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 267 | private View[] mInvertibleButtons; |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 268 | private View[] mInverseButtons; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 269 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 270 | private View mCurrentButton; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 271 | private Animator mCurrentAnimator; |
| 272 | |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 273 | // Characters that were recently entered at the end of the display that have not yet |
| 274 | // been added to the underlying expression. |
| 275 | private String mUnprocessedChars = null; |
| 276 | |
| 277 | // Color to highlight unprocessed characters from physical keyboard. |
| 278 | // TODO: should probably match this to the error color? |
| 279 | private ForegroundColorSpan mUnprocessedColorSpan = new ForegroundColorSpan(Color.RED); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 280 | |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 281 | // Whether the display is one line. |
| 282 | private boolean mOneLine; |
| 283 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 284 | private HistoryFragment mHistoryFragment = new HistoryFragment(); |
| 285 | |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 286 | /** |
Hans Boehm | a5ea8eb | 2016-12-01 12:33:38 -0800 | [diff] [blame] | 287 | * Map the old saved state to a new state reflecting requested result reevaluation. |
| 288 | */ |
| 289 | private CalculatorState mapFromSaved(CalculatorState savedState) { |
| 290 | switch (savedState) { |
| 291 | case RESULT: |
| 292 | case INIT_FOR_RESULT: |
| 293 | // Evaluation is expected to terminate normally. |
| 294 | return CalculatorState.INIT_FOR_RESULT; |
| 295 | case ERROR: |
| 296 | case INIT: |
| 297 | return CalculatorState.INIT; |
| 298 | case EVALUATE: |
| 299 | case INPUT: |
| 300 | return savedState; |
| 301 | default: // Includes ANIMATE state. |
| 302 | throw new AssertionError("Impossible saved state"); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 307 | * Restore Evaluator state and mCurrentState from savedInstanceState. |
| 308 | * Return true if the toolbar should be visible. |
| 309 | */ |
| 310 | private void restoreInstanceState(Bundle savedInstanceState) { |
| 311 | final CalculatorState savedState = CalculatorState.values()[ |
| 312 | savedInstanceState.getInt(KEY_DISPLAY_STATE, |
| 313 | CalculatorState.INPUT.ordinal())]; |
| 314 | setState(savedState); |
| 315 | CharSequence unprocessed = savedInstanceState.getCharSequence(KEY_UNPROCESSED_CHARS); |
| 316 | if (unprocessed != null) { |
| 317 | mUnprocessedChars = unprocessed.toString(); |
| 318 | } |
| 319 | byte[] state = savedInstanceState.getByteArray(KEY_EVAL_STATE); |
| 320 | if (state != null) { |
| 321 | try (ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(state))) { |
| 322 | mEvaluator.restoreInstanceState(in); |
| 323 | } catch (Throwable ignored) { |
| 324 | // When in doubt, revert to clean state |
| 325 | mCurrentState = CalculatorState.INPUT; |
| 326 | mEvaluator.clearMain(); |
| 327 | } |
| 328 | } |
| 329 | if (savedInstanceState.getBoolean(KEY_SHOW_TOOLBAR, true)) { |
| 330 | showAndMaybeHideToolbar(); |
| 331 | } else { |
| 332 | mDisplayView.hideToolbar(); |
| 333 | } |
| 334 | onInverseToggled(savedInstanceState.getBoolean(KEY_INVERSE_MODE)); |
| 335 | // TODO: We're currently not saving and restoring scroll position. |
| 336 | // We probably should. Details may require care to deal with: |
| 337 | // - new display size |
| 338 | // - slow recomputation if we've scrolled far. |
| 339 | } |
| 340 | |
| 341 | private void restoreDisplay() { |
| 342 | onModeChanged(mEvaluator.getDegreeMode(Evaluator.MAIN_INDEX)); |
| 343 | if (mCurrentState != CalculatorState.RESULT |
| 344 | && mCurrentState != CalculatorState.INIT_FOR_RESULT) { |
| 345 | redisplayFormula(); |
| 346 | } |
| 347 | if (mCurrentState == CalculatorState.INPUT) { |
| 348 | // This resultText will explicitly call evaluateAndNotify when ready. |
| 349 | mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_EVALUATE, this); |
| 350 | } else { |
| 351 | // Just reevaluate. |
Hans Boehm | a5ea8eb | 2016-12-01 12:33:38 -0800 | [diff] [blame] | 352 | setState(mapFromSaved(mCurrentState)); |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 353 | // Request evaluation when we know display width. |
| 354 | mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_REQUIRE, this); |
| 355 | } |
| 356 | } |
| 357 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 358 | @Override |
| 359 | protected void onCreate(Bundle savedInstanceState) { |
| 360 | super.onCreate(savedInstanceState); |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 361 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 362 | setContentView(R.layout.activity_calculator_main); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 363 | setActionBar((Toolbar) findViewById(R.id.toolbar)); |
| 364 | |
| 365 | // Hide all default options in the ActionBar. |
| 366 | getActionBar().setDisplayOptions(0); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 367 | |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 368 | // Ensure the toolbar stays visible while the options menu is displayed. |
| 369 | getActionBar().addOnMenuVisibilityListener(new ActionBar.OnMenuVisibilityListener() { |
| 370 | @Override |
| 371 | public void onMenuVisibilityChanged(boolean isVisible) { |
| 372 | mDisplayView.setForceToolbarVisible(isVisible); |
| 373 | } |
| 374 | }); |
| 375 | |
Annie Chin | eb36f95 | 2016-12-08 17:27:19 -0800 | [diff] [blame] | 376 | mMainCalculator = findViewById(R.id.main_calculator); |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 377 | mDisplayView = (CalculatorDisplay) findViewById(R.id.display); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 378 | mModeView = (TextView) findViewById(R.id.mode); |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 379 | mFormulaText = (CalculatorFormula) findViewById(R.id.formula); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 380 | mResultText = (CalculatorResult) findViewById(R.id.result); |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 381 | mFormulaContainer = (HorizontalScrollView) findViewById(R.id.formula_container); |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 382 | mEvaluator = Evaluator.getInstance(this); |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 383 | mEvaluator.setCallback(mEvaluatorCallback); |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 384 | mResultText.setEvaluator(mEvaluator, Evaluator.MAIN_INDEX); |
| 385 | KeyMaps.setActivity(this); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 386 | |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 387 | mPadViewPager = (ViewPager) findViewById(R.id.pad_pager); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 388 | mDeleteButton = findViewById(R.id.del); |
| 389 | mClearButton = findViewById(R.id.clr); |
Christine Franks | bd90b79 | 2016-11-22 10:28:26 -0800 | [diff] [blame] | 390 | final View numberPad = findViewById(R.id.pad_numeric); |
| 391 | mEqualButton = numberPad.findViewById(R.id.eq); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 392 | if (mEqualButton == null || mEqualButton.getVisibility() != View.VISIBLE) { |
| 393 | mEqualButton = findViewById(R.id.pad_operator).findViewById(R.id.eq); |
| 394 | } |
Christine Franks | bd90b79 | 2016-11-22 10:28:26 -0800 | [diff] [blame] | 395 | final TextView decimalPointButton = (TextView) numberPad.findViewById(R.id.dec_point); |
| 396 | decimalPointButton.setText(getDecimalSeparator()); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 397 | |
| 398 | mInverseToggle = (TextView) findViewById(R.id.toggle_inv); |
| 399 | mModeToggle = (TextView) findViewById(R.id.toggle_mode); |
| 400 | |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 401 | mOneLine = mResultText.getVisibility() == View.INVISIBLE; |
| 402 | |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 403 | mInvertibleButtons = new View[] { |
| 404 | findViewById(R.id.fun_sin), |
| 405 | findViewById(R.id.fun_cos), |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 406 | findViewById(R.id.fun_tan), |
| 407 | findViewById(R.id.fun_ln), |
| 408 | findViewById(R.id.fun_log), |
| 409 | findViewById(R.id.op_sqrt) |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 410 | }; |
| 411 | mInverseButtons = new View[] { |
| 412 | findViewById(R.id.fun_arcsin), |
| 413 | findViewById(R.id.fun_arccos), |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 414 | findViewById(R.id.fun_arctan), |
| 415 | findViewById(R.id.fun_exp), |
| 416 | findViewById(R.id.fun_10pow), |
| 417 | findViewById(R.id.op_sqr) |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 418 | }; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 419 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 420 | mDragLayout = (DragLayout) findViewById(R.id.drag_layout); |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame^] | 421 | mDragLayout.removeDragCallback(this); |
| 422 | mDragLayout.addDragCallback(this); |
| 423 | mDragLayout.setCloseCallback(this); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 424 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 425 | mHistoryFrame = (FrameLayout) findViewById(R.id.history_frame); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 426 | |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 427 | mFormulaText.setOnContextMenuClickListener(mOnFormulaContextMenuClickListener); |
| 428 | mFormulaText.setOnDisplayMemoryOperationsListener(mOnDisplayMemoryOperationsListener); |
| 429 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 430 | mFormulaText.setOnTextSizeChangeListener(this); |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 431 | mFormulaText.addTextChangedListener(mFormulaTextWatcher); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 432 | mDeleteButton.setOnLongClickListener(this); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 433 | |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 434 | if (savedInstanceState != null) { |
| 435 | restoreInstanceState(savedInstanceState); |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 436 | } else { |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 437 | mCurrentState = CalculatorState.INPUT; |
| 438 | mEvaluator.clearMain(); |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 439 | showAndMaybeHideToolbar(); |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 440 | onInverseToggled(false); |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 441 | } |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 442 | restoreDisplay(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | @Override |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 446 | protected void onResume() { |
| 447 | super.onResume(); |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 448 | if (mDisplayView.isToolbarVisible()) { |
| 449 | showAndMaybeHideToolbar(); |
| 450 | } |
Annie Chin | eb36f95 | 2016-12-08 17:27:19 -0800 | [diff] [blame] | 451 | // If HistoryFragment is showing, hide the main Calculator elements from accessibility. |
| 452 | // This is because Talkback does not use visibility as a cue for RelativeLayout elements, |
| 453 | // and RelativeLayout is the base class of DragLayout. |
| 454 | // If we did not do this, it would be possible to traverse to main Calculator elements from |
| 455 | // HistoryFragment. |
| 456 | mMainCalculator.setImportantForAccessibility( |
| 457 | mDragLayout.isOpen() ? View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS |
| 458 | : View.IMPORTANT_FOR_ACCESSIBILITY_AUTO); |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | @Override |
Justin Klaassen | f79d6f6 | 2014-08-26 12:27:08 -0700 | [diff] [blame] | 462 | protected void onSaveInstanceState(@NonNull Bundle outState) { |
Hans Boehm | 4012544 | 2016-01-22 10:35:35 -0800 | [diff] [blame] | 463 | mEvaluator.cancelAll(true); |
Justin Klaassen | f79d6f6 | 2014-08-26 12:27:08 -0700 | [diff] [blame] | 464 | // If there's an animation in progress, cancel it first to ensure our state is up-to-date. |
| 465 | if (mCurrentAnimator != null) { |
| 466 | mCurrentAnimator.cancel(); |
| 467 | } |
| 468 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 469 | super.onSaveInstanceState(outState); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 470 | outState.putInt(KEY_DISPLAY_STATE, mCurrentState.ordinal()); |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 471 | outState.putCharSequence(KEY_UNPROCESSED_CHARS, mUnprocessedChars); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 472 | ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(); |
| 473 | try (ObjectOutput out = new ObjectOutputStream(byteArrayStream)) { |
| 474 | mEvaluator.saveInstanceState(out); |
| 475 | } catch (IOException e) { |
| 476 | // Impossible; No IO involved. |
| 477 | throw new AssertionError("Impossible IO exception", e); |
| 478 | } |
| 479 | outState.putByteArray(KEY_EVAL_STATE, byteArrayStream.toByteArray()); |
Justin Klaassen | 3e223ea | 2016-02-05 14:18:06 -0800 | [diff] [blame] | 480 | outState.putBoolean(KEY_INVERSE_MODE, mInverseToggle.isSelected()); |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 481 | outState.putBoolean(KEY_SHOW_TOOLBAR, mDisplayView.isToolbarVisible()); |
Hans Boehm | e95203e | 2017-01-04 14:13:11 -0800 | [diff] [blame] | 482 | // We must wait for asynchronous writes to complete, since outState may contain |
| 483 | // references to expressions being written. |
| 484 | mEvaluator.waitForWrites(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 487 | // Set the state, updating delete label and display colors. |
| 488 | // This restores display positions on moving to INPUT. |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 489 | // But movement/animation for moving to RESULT has already been done. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 490 | private void setState(CalculatorState state) { |
| 491 | if (mCurrentState != state) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 492 | if (state == CalculatorState.INPUT) { |
Hans Boehm | d4959e8 | 2016-11-15 18:01:28 -0800 | [diff] [blame] | 493 | // We'll explicitly request evaluation from now on. |
Hans Boehm | bd01e4b | 2016-11-23 10:12:58 -0800 | [diff] [blame] | 494 | mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_NOT_EVALUATE, null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 495 | restoreDisplayPositions(); |
| 496 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 497 | mCurrentState = state; |
| 498 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 499 | if (mCurrentState == CalculatorState.RESULT) { |
| 500 | // No longer do this for ERROR; allow mistakes to be corrected. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 501 | mDeleteButton.setVisibility(View.GONE); |
| 502 | mClearButton.setVisibility(View.VISIBLE); |
| 503 | } else { |
| 504 | mDeleteButton.setVisibility(View.VISIBLE); |
| 505 | mClearButton.setVisibility(View.GONE); |
| 506 | } |
| 507 | |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 508 | if (mOneLine) { |
| 509 | if (mCurrentState == CalculatorState.RESULT |
| 510 | || mCurrentState == CalculatorState.EVALUATE |
| 511 | || mCurrentState == CalculatorState.ANIMATE) { |
| 512 | mFormulaText.setVisibility(View.VISIBLE); |
| 513 | mResultText.setVisibility(View.VISIBLE); |
Annie Chin | 947d93b | 2016-06-14 10:18:54 -0700 | [diff] [blame] | 514 | } else if (mCurrentState == CalculatorState.ERROR) { |
| 515 | mFormulaText.setVisibility(View.INVISIBLE); |
| 516 | mResultText.setVisibility(View.VISIBLE); |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 517 | } else { |
| 518 | mFormulaText.setVisibility(View.VISIBLE); |
| 519 | mResultText.setVisibility(View.INVISIBLE); |
| 520 | } |
| 521 | } |
| 522 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 523 | if (mCurrentState == CalculatorState.ERROR) { |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 524 | final int errorColor = |
| 525 | ContextCompat.getColor(this, R.color.calculator_error_color); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 526 | mFormulaText.setTextColor(errorColor); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 527 | mResultText.setTextColor(errorColor); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 528 | getWindow().setStatusBarColor(errorColor); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 529 | } else if (mCurrentState != CalculatorState.RESULT) { |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 530 | mFormulaText.setTextColor( |
| 531 | ContextCompat.getColor(this, R.color.display_formula_text_color)); |
| 532 | mResultText.setTextColor( |
| 533 | ContextCompat.getColor(this, R.color.display_result_text_color)); |
| 534 | getWindow().setStatusBarColor( |
Annie Chin | 96be246 | 2016-12-19 14:31:16 -0800 | [diff] [blame] | 535 | ContextCompat.getColor(this, R.color.calculator_statusbar_color)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 536 | } |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 537 | |
| 538 | invalidateOptionsMenu(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 542 | public boolean isResultLayout() { |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 543 | if (mCurrentState == CalculatorState.ANIMATE) { |
| 544 | throw new AssertionError("impossible state"); |
| 545 | } |
| 546 | // Note that ERROR has INPUT, not RESULT layout. |
| 547 | return mCurrentState == CalculatorState.INIT_FOR_RESULT |
| 548 | || mCurrentState == CalculatorState.RESULT; |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 551 | @Override |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 552 | protected void onDestroy() { |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame^] | 553 | mDragLayout.removeDragCallback(this); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 554 | super.onDestroy(); |
| 555 | } |
| 556 | |
Hans Boehm | a5ea8eb | 2016-12-01 12:33:38 -0800 | [diff] [blame] | 557 | /** |
| 558 | * Destroy the evaluator and close the underlying database. |
| 559 | */ |
| 560 | public void destroyEvaluator() { |
| 561 | mEvaluator.destroyEvaluator(); |
| 562 | } |
| 563 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 564 | @Override |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 565 | public void onActionModeStarted(ActionMode mode) { |
| 566 | super.onActionModeStarted(mode); |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 567 | if (mode.getTag() == CalculatorFormula.TAG_ACTION_MODE) { |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 568 | mFormulaContainer.scrollTo(mFormulaText.getRight(), 0); |
| 569 | } |
| 570 | } |
| 571 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 572 | /** |
| 573 | * Stop any active ActionMode or ContextMenu for copy/paste actions. |
| 574 | * Return true if there was one. |
| 575 | */ |
| 576 | private boolean stopActionModeOrContextMenu() { |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 577 | return mResultText.stopActionModeOrContextMenu() |
| 578 | || mFormulaText.stopActionModeOrContextMenu(); |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 581 | @Override |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 582 | public void onUserInteraction() { |
| 583 | super.onUserInteraction(); |
| 584 | |
| 585 | // If there's an animation in progress, end it immediately, so the user interaction can |
| 586 | // be handled. |
| 587 | if (mCurrentAnimator != null) { |
| 588 | mCurrentAnimator.end(); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | @Override |
Christine Franks | 1473ddd | 2016-12-01 15:02:23 -0800 | [diff] [blame] | 593 | public boolean dispatchTouchEvent(MotionEvent e) { |
| 594 | if (e.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 595 | stopActionModeOrContextMenu(); |
| 596 | if (mDragLayout.isOpen()) { |
| 597 | mHistoryFragment.stopActionModeOrContextMenu(); |
| 598 | } |
| 599 | } |
| 600 | return super.dispatchTouchEvent(e); |
| 601 | } |
| 602 | |
| 603 | @Override |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 604 | public void onBackPressed() { |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 605 | if (!stopActionModeOrContextMenu()) { |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 606 | if (mDragLayout.isOpen()) { |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 607 | if (!mHistoryFragment.stopActionModeOrContextMenu()) { |
| 608 | mDragLayout.setClosed(); |
Justin Klaassen | 12874e3 | 2016-12-12 07:57:47 -0800 | [diff] [blame] | 609 | removeHistoryFragment(); |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 610 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 611 | return; |
| 612 | } |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame] | 613 | if (mPadViewPager != null && mPadViewPager.getCurrentItem() != 0) { |
| 614 | // Select the previous pad. |
| 615 | mPadViewPager.setCurrentItem(mPadViewPager.getCurrentItem() - 1); |
| 616 | } else { |
| 617 | // If the user is currently looking at the first pad (or the pad is not paged), |
| 618 | // allow the system to handle the Back button. |
| 619 | super.onBackPressed(); |
| 620 | } |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | @Override |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 625 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Justin Klaassen | 83959da | 2016-04-06 11:55:24 -0700 | [diff] [blame] | 626 | // 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] | 627 | switch (keyCode) { |
Justin Klaassen | 83959da | 2016-04-06 11:55:24 -0700 | [diff] [blame] | 628 | case KeyEvent.KEYCODE_BACK: |
Christine Franks | f9ba220 | 2016-10-20 17:20:19 -0700 | [diff] [blame] | 629 | case KeyEvent.KEYCODE_ESCAPE: |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 630 | case KeyEvent.KEYCODE_DPAD_UP: |
| 631 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 632 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 633 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 634 | return super.onKeyUp(keyCode, event); |
| 635 | } |
| 636 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 637 | // Stop the action mode or context menu if it's showing. |
| 638 | stopActionModeOrContextMenu(); |
Justin Klaassen | d12e062 | 2016-04-27 16:26:47 -0700 | [diff] [blame] | 639 | |
Hans Boehm | ced295e | 2016-11-17 17:30:13 -0800 | [diff] [blame] | 640 | // Always cancel unrequested in-progress evaluation of the main expression, so that |
| 641 | // we don't have to worry about subsequent asynchronous completion. |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 642 | // Requested in-progress evaluations are handled below. |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 643 | cancelUnrequested(); |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 644 | |
| 645 | switch (keyCode) { |
| 646 | case KeyEvent.KEYCODE_NUMPAD_ENTER: |
| 647 | case KeyEvent.KEYCODE_ENTER: |
| 648 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 649 | mCurrentButton = mEqualButton; |
| 650 | onEquals(); |
| 651 | return true; |
| 652 | case KeyEvent.KEYCODE_DEL: |
| 653 | mCurrentButton = mDeleteButton; |
| 654 | onDelete(); |
| 655 | return true; |
Annie Chin | 56bcbf1 | 2016-09-23 17:04:22 -0700 | [diff] [blame] | 656 | case KeyEvent.KEYCODE_CLEAR: |
| 657 | mCurrentButton = mClearButton; |
| 658 | onClear(); |
| 659 | return true; |
Justin Klaassen | 12da1ad | 2016-04-04 14:20:37 -0700 | [diff] [blame] | 660 | default: |
| 661 | cancelIfEvaluating(false); |
| 662 | final int raw = event.getKeyCharacterMap().get(keyCode, event.getMetaState()); |
| 663 | if ((raw & KeyCharacterMap.COMBINING_ACCENT) != 0) { |
| 664 | return true; // discard |
| 665 | } |
| 666 | // Try to discard non-printing characters and the like. |
| 667 | // The user will have to explicitly delete other junk that gets past us. |
| 668 | if (Character.isIdentifierIgnorable(raw) || Character.isWhitespace(raw)) { |
| 669 | return true; |
| 670 | } |
| 671 | char c = (char) raw; |
| 672 | if (c == '=') { |
| 673 | mCurrentButton = mEqualButton; |
| 674 | onEquals(); |
| 675 | } else { |
| 676 | addChars(String.valueOf(c), true); |
| 677 | redisplayAfterFormulaChange(); |
| 678 | } |
| 679 | return true; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 683 | /** |
| 684 | * Invoked whenever the inverse button is toggled to update the UI. |
| 685 | * |
| 686 | * @param showInverse {@code true} if inverse functions should be shown |
| 687 | */ |
| 688 | private void onInverseToggled(boolean showInverse) { |
Justin Klaassen | 3e223ea | 2016-02-05 14:18:06 -0800 | [diff] [blame] | 689 | mInverseToggle.setSelected(showInverse); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 690 | if (showInverse) { |
| 691 | mInverseToggle.setContentDescription(getString(R.string.desc_inv_on)); |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 692 | for (View invertibleButton : mInvertibleButtons) { |
| 693 | invertibleButton.setVisibility(View.GONE); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 694 | } |
| 695 | for (View inverseButton : mInverseButtons) { |
| 696 | inverseButton.setVisibility(View.VISIBLE); |
| 697 | } |
| 698 | } else { |
| 699 | mInverseToggle.setContentDescription(getString(R.string.desc_inv_off)); |
Justin Klaassen | 721ec84 | 2015-05-28 14:30:08 -0700 | [diff] [blame] | 700 | for (View invertibleButton : mInvertibleButtons) { |
| 701 | invertibleButton.setVisibility(View.VISIBLE); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 702 | } |
| 703 | for (View inverseButton : mInverseButtons) { |
| 704 | inverseButton.setVisibility(View.GONE); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /** |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 710 | * Invoked whenever the deg/rad mode may have changed to update the UI. Note that the mode has |
| 711 | * not necessarily actually changed where this is invoked. |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 712 | * |
| 713 | * @param degreeMode {@code true} if in degree mode |
| 714 | */ |
| 715 | private void onModeChanged(boolean degreeMode) { |
| 716 | if (degreeMode) { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 717 | mModeView.setText(R.string.mode_deg); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 718 | mModeView.setContentDescription(getString(R.string.desc_mode_deg)); |
| 719 | |
| 720 | mModeToggle.setText(R.string.mode_rad); |
| 721 | mModeToggle.setContentDescription(getString(R.string.desc_switch_rad)); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 722 | } else { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 723 | mModeView.setText(R.string.mode_rad); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 724 | mModeView.setContentDescription(getString(R.string.desc_mode_rad)); |
| 725 | |
| 726 | mModeToggle.setText(R.string.mode_deg); |
| 727 | mModeToggle.setContentDescription(getString(R.string.desc_switch_deg)); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 728 | } |
| 729 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 730 | |
Justin Klaassen | 12874e3 | 2016-12-12 07:57:47 -0800 | [diff] [blame] | 731 | private void removeHistoryFragment() { |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 732 | final FragmentManager manager = getFragmentManager(); |
Annie Chin | eb36f95 | 2016-12-08 17:27:19 -0800 | [diff] [blame] | 733 | if (manager != null && !manager.isDestroyed()) { |
| 734 | manager.popBackStackImmediate(HistoryFragment.TAG, |
| 735 | FragmentManager.POP_BACK_STACK_INCLUSIVE); |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 736 | } |
Annie Chin | eb36f95 | 2016-12-08 17:27:19 -0800 | [diff] [blame] | 737 | |
| 738 | // When HistoryFragment is hidden, the main Calculator is important for accessibility again. |
| 739 | mMainCalculator.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO); |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 740 | } |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 741 | |
Hans Boehm | 5d79d10 | 2015-09-16 16:33:47 -0700 | [diff] [blame] | 742 | /** |
| 743 | * Switch to INPUT from RESULT state in response to input of the specified button_id. |
| 744 | * View.NO_ID is treated as an incomplete function id. |
| 745 | */ |
| 746 | private void switchToInput(int button_id) { |
| 747 | if (KeyMaps.isBinary(button_id) || KeyMaps.isSuffix(button_id)) { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 748 | mEvaluator.collapse(mEvaluator.getMaxIndex() /* Most recent history entry */); |
Hans Boehm | 5d79d10 | 2015-09-16 16:33:47 -0700 | [diff] [blame] | 749 | } else { |
| 750 | announceClearedForAccessibility(); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 751 | mEvaluator.clearMain(); |
Hans Boehm | 5d79d10 | 2015-09-16 16:33:47 -0700 | [diff] [blame] | 752 | } |
| 753 | setState(CalculatorState.INPUT); |
| 754 | } |
| 755 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 756 | // Add the given button id to input expression. |
| 757 | // If appropriate, clear the expression before doing so. |
| 758 | private void addKeyToExpr(int id) { |
| 759 | if (mCurrentState == CalculatorState.ERROR) { |
| 760 | setState(CalculatorState.INPUT); |
| 761 | } else if (mCurrentState == CalculatorState.RESULT) { |
Hans Boehm | 5d79d10 | 2015-09-16 16:33:47 -0700 | [diff] [blame] | 762 | switchToInput(id); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 763 | } |
| 764 | if (!mEvaluator.append(id)) { |
| 765 | // TODO: Some user visible feedback? |
| 766 | } |
| 767 | } |
| 768 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 769 | /** |
| 770 | * Add the given button id to input expression, assuming it was explicitly |
| 771 | * typed/touched. |
| 772 | * We perform slightly more aggressive correction than in pasted expressions. |
| 773 | */ |
| 774 | private void addExplicitKeyToExpr(int id) { |
| 775 | if (mCurrentState == CalculatorState.INPUT && id == R.id.op_sub) { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 776 | mEvaluator.getExpr(Evaluator.MAIN_INDEX).removeTrailingAdditiveOperators(); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 777 | } |
| 778 | addKeyToExpr(id); |
| 779 | } |
| 780 | |
Hans Boehm | bd01e4b | 2016-11-23 10:12:58 -0800 | [diff] [blame] | 781 | public void evaluateInstantIfNecessary() { |
| 782 | if (mCurrentState == CalculatorState.INPUT |
| 783 | && mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasInterestingOps()) { |
| 784 | mEvaluator.evaluateAndNotify(Evaluator.MAIN_INDEX, this, mResultText); |
| 785 | } |
| 786 | } |
| 787 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 788 | private void redisplayAfterFormulaChange() { |
| 789 | // TODO: Could do this more incrementally. |
| 790 | redisplayFormula(); |
| 791 | setState(CalculatorState.INPUT); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 792 | mResultText.clear(); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 793 | if (haveUnprocessed()) { |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 794 | // Force reevaluation when text is deleted, even if expression is unchanged. |
| 795 | mEvaluator.touch(); |
| 796 | } else { |
Hans Boehm | bd01e4b | 2016-11-23 10:12:58 -0800 | [diff] [blame] | 797 | evaluateInstantIfNecessary(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 798 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 801 | /** |
| 802 | * Show the toolbar. |
| 803 | * Automatically hide it again if it's not relevant to current formula. |
| 804 | */ |
| 805 | private void showAndMaybeHideToolbar() { |
| 806 | final boolean shouldBeVisible = |
| 807 | mCurrentState == CalculatorState.INPUT && mEvaluator.hasTrigFuncs(); |
| 808 | mDisplayView.showToolbar(!shouldBeVisible); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Display or hide the toolbar depending on calculator state. |
| 813 | */ |
| 814 | private void showOrHideToolbar() { |
| 815 | final boolean shouldBeVisible = |
| 816 | mCurrentState == CalculatorState.INPUT && mEvaluator.hasTrigFuncs(); |
| 817 | if (shouldBeVisible) { |
| 818 | mDisplayView.showToolbar(false); |
| 819 | } else { |
| 820 | mDisplayView.hideToolbar(); |
| 821 | } |
| 822 | } |
| 823 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 824 | public void onButtonClick(View view) { |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 825 | // Any animation is ended before we get here. |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 826 | mCurrentButton = view; |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 827 | stopActionModeOrContextMenu(); |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 828 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 829 | // See onKey above for the rationale behind some of the behavior below: |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 830 | cancelUnrequested(); |
Justin Klaassen | 9d33cdc | 2016-02-21 14:16:14 -0800 | [diff] [blame] | 831 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 832 | final int id = view.getId(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 833 | switch (id) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 834 | case R.id.eq: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 835 | onEquals(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 836 | break; |
| 837 | case R.id.del: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 838 | onDelete(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 839 | break; |
| 840 | case R.id.clr: |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 841 | onClear(); |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 842 | return; // Toolbar visibility adjusted at end of animation. |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 843 | case R.id.toggle_inv: |
| 844 | final boolean selected = !mInverseToggle.isSelected(); |
| 845 | mInverseToggle.setSelected(selected); |
| 846 | onInverseToggled(selected); |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 847 | if (mCurrentState == CalculatorState.RESULT) { |
| 848 | mResultText.redisplay(); // In case we cancelled reevaluation. |
| 849 | } |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 850 | break; |
| 851 | case R.id.toggle_mode: |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 852 | cancelIfEvaluating(false); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 853 | final boolean mode = !mEvaluator.getDegreeMode(Evaluator.MAIN_INDEX); |
| 854 | if (mCurrentState == CalculatorState.RESULT |
| 855 | && mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrigFuncs()) { |
| 856 | // Capture current result evaluated in old mode. |
| 857 | mEvaluator.collapse(mEvaluator.getMaxIndex()); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 858 | redisplayFormula(); |
| 859 | } |
| 860 | // In input mode, we reinterpret already entered trig functions. |
| 861 | mEvaluator.setDegreeMode(mode); |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 862 | onModeChanged(mode); |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 863 | // Show the toolbar to highlight the mode change. |
| 864 | showAndMaybeHideToolbar(); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 865 | setState(CalculatorState.INPUT); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 866 | mResultText.clear(); |
Hans Boehm | bd01e4b | 2016-11-23 10:12:58 -0800 | [diff] [blame] | 867 | if (!haveUnprocessed()) { |
| 868 | evaluateInstantIfNecessary(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 869 | } |
Christine Franks | eeff27f | 2016-07-29 12:05:29 -0700 | [diff] [blame] | 870 | return; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 871 | default: |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 872 | cancelIfEvaluating(false); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 873 | if (haveUnprocessed()) { |
| 874 | // For consistency, append as uninterpreted characters. |
| 875 | // This may actually be useful for a left parenthesis. |
| 876 | addChars(KeyMaps.toString(this, id), true); |
| 877 | } else { |
| 878 | addExplicitKeyToExpr(id); |
| 879 | redisplayAfterFormulaChange(); |
| 880 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 881 | break; |
| 882 | } |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 883 | showOrHideToolbar(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 886 | void redisplayFormula() { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 887 | SpannableStringBuilder formula |
| 888 | = mEvaluator.getExpr(Evaluator.MAIN_INDEX).toSpannableStringBuilder(this); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 889 | if (mUnprocessedChars != null) { |
| 890 | // Add and highlight characters we couldn't process. |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 891 | formula.append(mUnprocessedChars, mUnprocessedColorSpan, |
| 892 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 893 | } |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 894 | mFormulaText.changeTextTo(formula); |
Annie Chin | f360ef0 | 2016-03-10 13:45:39 -0800 | [diff] [blame] | 895 | mFormulaText.setContentDescription(TextUtils.isEmpty(formula) |
Justin Klaassen | d183141 | 2016-07-19 21:59:10 -0700 | [diff] [blame] | 896 | ? getString(R.string.desc_formula) : null); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 899 | @Override |
| 900 | public boolean onLongClick(View view) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 901 | mCurrentButton = view; |
| 902 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 903 | if (view.getId() == R.id.del) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 904 | onClear(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 905 | return true; |
| 906 | } |
| 907 | return false; |
| 908 | } |
| 909 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 910 | // Initial evaluation completed successfully. Initiate display. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 911 | public void onEvaluate(long index, int initDisplayPrec, int msd, int leastDigPos, |
Hans Boehm | a0e45f3 | 2015-05-30 13:20:35 -0700 | [diff] [blame] | 912 | String truncatedWholeNumber) { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 913 | if (index != Evaluator.MAIN_INDEX) { |
| 914 | throw new AssertionError("Unexpected evaluation result index\n"); |
| 915 | } |
Annie Chin | 37c33b6 | 2016-11-22 14:46:28 -0800 | [diff] [blame] | 916 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 917 | // Invalidate any options that may depend on the current result. |
| 918 | invalidateOptionsMenu(); |
| 919 | |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 920 | mResultText.onEvaluate(index, initDisplayPrec, msd, leastDigPos, truncatedWholeNumber); |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 921 | if (mCurrentState != CalculatorState.INPUT) { |
| 922 | // In EVALUATE, INIT, or INIT_FOR_RESULT state. |
Hans Boehm | 4522315 | 2016-12-21 10:35:35 -0800 | [diff] [blame] | 923 | onResult(mCurrentState == CalculatorState.EVALUATE /* animate */, |
| 924 | mCurrentState == CalculatorState.INIT_FOR_RESULT /* previously preserved */); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 925 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 926 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 927 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 928 | // Reset state to reflect evaluator cancellation. Invoked by evaluator. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 929 | public void onCancelled(long index) { |
| 930 | // Index is Evaluator.MAIN_INDEX. We should be in EVALUATE state. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 931 | setState(CalculatorState.INPUT); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 932 | mResultText.onCancelled(index); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // Reevaluation completed; ask result to redisplay current value. |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 936 | public void onReevaluate(long index) { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 937 | // Index is Evaluator.MAIN_INDEX. |
| 938 | mResultText.onReevaluate(index); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 941 | @Override |
| 942 | public void onTextSizeChanged(final TextView textView, float oldSize) { |
| 943 | if (mCurrentState != CalculatorState.INPUT) { |
| 944 | // Only animate text changes that occur from user input. |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | // Calculate the values needed to perform the scale and translation animations, |
| 949 | // maintaining the same apparent baseline for the displayed text. |
| 950 | final float textScale = oldSize / textView.getTextSize(); |
| 951 | final float translationX = (1.0f - textScale) * |
| 952 | (textView.getWidth() / 2.0f - textView.getPaddingEnd()); |
| 953 | final float translationY = (1.0f - textScale) * |
| 954 | (textView.getHeight() / 2.0f - textView.getPaddingBottom()); |
| 955 | |
| 956 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 957 | animatorSet.playTogether( |
| 958 | ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f), |
| 959 | ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f), |
| 960 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f), |
| 961 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f)); |
Justin Klaassen | 94db720 | 2014-06-11 11:22:31 -0700 | [diff] [blame] | 962 | animatorSet.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 963 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 964 | animatorSet.start(); |
| 965 | } |
| 966 | |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 967 | /** |
| 968 | * Cancel any in-progress explicitly requested evaluations. |
| 969 | * @param quiet suppress pop-up message. Explicit evaluation can change the expression |
| 970 | value, and certainly changes the display, so it seems reasonable to warn. |
| 971 | * @return true if there was such an evaluation |
| 972 | */ |
| 973 | private boolean cancelIfEvaluating(boolean quiet) { |
| 974 | if (mCurrentState == CalculatorState.EVALUATE) { |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 975 | mEvaluator.cancel(Evaluator.MAIN_INDEX, quiet); |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 976 | return true; |
| 977 | } else { |
| 978 | return false; |
| 979 | } |
| 980 | } |
| 981 | |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 982 | |
| 983 | private void cancelUnrequested() { |
| 984 | if (mCurrentState == CalculatorState.INPUT) { |
| 985 | mEvaluator.cancel(Evaluator.MAIN_INDEX, true); |
| 986 | } |
| 987 | } |
| 988 | |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 989 | private boolean haveUnprocessed() { |
| 990 | return mUnprocessedChars != null && !mUnprocessedChars.isEmpty(); |
| 991 | } |
| 992 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 993 | private void onEquals() { |
Hans Boehm | 56d6e76 | 2016-06-06 11:46:29 -0700 | [diff] [blame] | 994 | // Ignore if in non-INPUT state, or if there are no operators. |
Justin Klaassen | a8075af | 2016-07-27 15:24:45 -0700 | [diff] [blame] | 995 | if (mCurrentState == CalculatorState.INPUT) { |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 996 | if (haveUnprocessed()) { |
Justin Klaassen | a8075af | 2016-07-27 15:24:45 -0700 | [diff] [blame] | 997 | setState(CalculatorState.EVALUATE); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 998 | onError(Evaluator.MAIN_INDEX, R.string.error_syntax); |
| 999 | } else if (mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasInterestingOps()) { |
Justin Klaassen | a8075af | 2016-07-27 15:24:45 -0700 | [diff] [blame] | 1000 | setState(CalculatorState.EVALUATE); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1001 | mEvaluator.requireResult(Evaluator.MAIN_INDEX, this, mResultText); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 1002 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | private void onDelete() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1007 | // Delete works like backspace; remove the last character or operator from the expression. |
| 1008 | // Note that we handle keyboard delete exactly like the delete button. For |
| 1009 | // example the delete button can be used to delete a character from an incomplete |
| 1010 | // function name typed on a physical keyboard. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1011 | // This should be impossible in RESULT state. |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 1012 | // If there is an in-progress explicit evaluation, just cancel it and return. |
| 1013 | if (cancelIfEvaluating(false)) return; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1014 | setState(CalculatorState.INPUT); |
Hans Boehm | af04c3a | 2016-01-27 14:50:08 -0800 | [diff] [blame] | 1015 | if (haveUnprocessed()) { |
| 1016 | mUnprocessedChars = mUnprocessedChars.substring(0, mUnprocessedChars.length() - 1); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1017 | } else { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1018 | mEvaluator.delete(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1019 | } |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1020 | if (mEvaluator.getExpr(Evaluator.MAIN_INDEX).isEmpty() && !haveUnprocessed()) { |
Hans Boehm | db6f999 | 2015-08-19 12:32:56 -0700 | [diff] [blame] | 1021 | // Resulting formula won't be announced, since it's empty. |
| 1022 | announceClearedForAccessibility(); |
| 1023 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1024 | redisplayAfterFormulaChange(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1027 | private void reveal(View sourceView, int colorRes, AnimatorListener listener) { |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 1028 | final ViewGroupOverlay groupOverlay = |
| 1029 | (ViewGroupOverlay) getWindow().getDecorView().getOverlay(); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 1030 | |
| 1031 | final Rect displayRect = new Rect(); |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 1032 | mDisplayView.getGlobalVisibleRect(displayRect); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1033 | |
| 1034 | // Make reveal cover the display and status bar. |
| 1035 | final View revealView = new View(this); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 1036 | revealView.setBottom(displayRect.bottom); |
| 1037 | revealView.setLeft(displayRect.left); |
| 1038 | revealView.setRight(displayRect.right); |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 1039 | revealView.setBackgroundColor(ContextCompat.getColor(this, colorRes)); |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 1040 | groupOverlay.add(revealView); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1041 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1042 | final int[] clearLocation = new int[2]; |
| 1043 | sourceView.getLocationInWindow(clearLocation); |
| 1044 | clearLocation[0] += sourceView.getWidth() / 2; |
| 1045 | clearLocation[1] += sourceView.getHeight() / 2; |
| 1046 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1047 | final int revealCenterX = clearLocation[0] - revealView.getLeft(); |
| 1048 | final int revealCenterY = clearLocation[1] - revealView.getTop(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1049 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1050 | final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2); |
| 1051 | final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2); |
| 1052 | final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1053 | final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2)); |
| 1054 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1055 | final Animator revealAnimator = |
| 1056 | ViewAnimationUtils.createCircularReveal(revealView, |
ztenghui | 3d6ecaf | 2014-06-05 09:56:00 -0700 | [diff] [blame] | 1057 | revealCenterX, revealCenterY, 0.0f, revealRadius); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1058 | revealAnimator.setDuration( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1059 | getResources().getInteger(android.R.integer.config_longAnimTime)); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1060 | revealAnimator.addListener(listener); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1061 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1062 | final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1063 | alphaAnimator.setDuration( |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1064 | getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1065 | |
| 1066 | final AnimatorSet animatorSet = new AnimatorSet(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1067 | animatorSet.play(revealAnimator).before(alphaAnimator); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1068 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 1069 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 1070 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1071 | public void onAnimationEnd(Animator animator) { |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 1072 | groupOverlay.remove(revealView); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1073 | mCurrentAnimator = null; |
| 1074 | } |
| 1075 | }); |
| 1076 | |
| 1077 | mCurrentAnimator = animatorSet; |
| 1078 | animatorSet.start(); |
| 1079 | } |
| 1080 | |
Hans Boehm | db6f999 | 2015-08-19 12:32:56 -0700 | [diff] [blame] | 1081 | private void announceClearedForAccessibility() { |
| 1082 | mResultText.announceForAccessibility(getResources().getString(R.string.cleared)); |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Hans Boehm | 9db3ee2 | 2016-11-18 10:09:47 -0800 | [diff] [blame] | 1085 | public void onClearAnimationEnd() { |
| 1086 | mUnprocessedChars = null; |
| 1087 | mResultText.clear(); |
| 1088 | mEvaluator.clearMain(); |
| 1089 | setState(CalculatorState.INPUT); |
| 1090 | redisplayFormula(); |
| 1091 | } |
| 1092 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1093 | private void onClear() { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1094 | if (mEvaluator.getExpr(Evaluator.MAIN_INDEX).isEmpty() && !haveUnprocessed()) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1095 | return; |
| 1096 | } |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 1097 | cancelIfEvaluating(true); |
Hans Boehm | db6f999 | 2015-08-19 12:32:56 -0700 | [diff] [blame] | 1098 | announceClearedForAccessibility(); |
Annie Chin | 96be246 | 2016-12-19 14:31:16 -0800 | [diff] [blame] | 1099 | reveal(mCurrentButton, R.color.calculator_primary_color, new AnimatorListenerAdapter() { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1100 | @Override |
| 1101 | public void onAnimationEnd(Animator animation) { |
Hans Boehm | 9db3ee2 | 2016-11-18 10:09:47 -0800 | [diff] [blame] | 1102 | onClearAnimationEnd(); |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 1103 | showOrHideToolbar(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1104 | } |
| 1105 | }); |
| 1106 | } |
| 1107 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1108 | // Evaluation encountered en error. Display the error. |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1109 | @Override |
| 1110 | public void onError(final long index, final int errorResourceId) { |
| 1111 | if (index != Evaluator.MAIN_INDEX) { |
| 1112 | throw new AssertionError("Unexpected error source"); |
| 1113 | } |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 1114 | if (mCurrentState == CalculatorState.EVALUATE) { |
| 1115 | setState(CalculatorState.ANIMATE); |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 1116 | mResultText.announceForAccessibility(getResources().getString(errorResourceId)); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 1117 | reveal(mCurrentButton, R.color.calculator_error_color, |
| 1118 | new AnimatorListenerAdapter() { |
| 1119 | @Override |
| 1120 | public void onAnimationEnd(Animator animation) { |
| 1121 | setState(CalculatorState.ERROR); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1122 | mResultText.onError(index, errorResourceId); |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 1123 | } |
| 1124 | }); |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 1125 | } else if (mCurrentState == CalculatorState.INIT |
| 1126 | || mCurrentState == CalculatorState.INIT_FOR_RESULT /* very unlikely */) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 1127 | setState(CalculatorState.ERROR); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1128 | mResultText.onError(index, errorResourceId); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 1129 | } else { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1130 | mResultText.clear(); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 1131 | } |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1134 | // Animate movement of result into the top formula slot. |
| 1135 | // Result window now remains translated in the top slot while the result is displayed. |
| 1136 | // (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] | 1137 | // Historical note: In the Lollipop version, this invisibly and instantaneously moved |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1138 | // formula and result displays back at the end of the animation. We no longer do that, |
| 1139 | // so that we can continue to properly support scrolling of the result. |
| 1140 | // We assume the result already contains the text to be expanded. |
Hans Boehm | 4522315 | 2016-12-21 10:35:35 -0800 | [diff] [blame] | 1141 | private void onResult(boolean animate, boolean resultWasPreserved) { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1142 | // Calculate the textSize that would be used to display the result in the formula. |
| 1143 | // For scrollable results just use the minimum textSize to maximize the number of digits |
| 1144 | // that are visible on screen. |
| 1145 | float textSize = mFormulaText.getMinimumTextSize(); |
| 1146 | if (!mResultText.isScrollable()) { |
| 1147 | textSize = mFormulaText.getVariableTextSize(mResultText.getText().toString()); |
| 1148 | } |
| 1149 | |
| 1150 | // Scale the result to match the calculated textSize, minimizing the jump-cut transition |
| 1151 | // when a result is reused in a subsequent expression. |
| 1152 | final float resultScale = textSize / mResultText.getTextSize(); |
| 1153 | |
| 1154 | // Set the result's pivot to match its gravity. |
| 1155 | mResultText.setPivotX(mResultText.getWidth() - mResultText.getPaddingRight()); |
| 1156 | mResultText.setPivotY(mResultText.getHeight() - mResultText.getPaddingBottom()); |
| 1157 | |
| 1158 | // Calculate the necessary translations so the result takes the place of the formula and |
| 1159 | // the formula moves off the top of the screen. |
Annie Chin | 28589dc | 2016-06-09 17:50:51 -0700 | [diff] [blame] | 1160 | final float resultTranslationY = (mFormulaContainer.getBottom() - mResultText.getBottom()) |
| 1161 | - (mFormulaText.getPaddingBottom() - mResultText.getPaddingBottom()); |
| 1162 | float formulaTranslationY = -mFormulaContainer.getBottom(); |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 1163 | if (mOneLine) { |
| 1164 | // Position the result text. |
| 1165 | mResultText.setY(mResultText.getBottom()); |
Annie Chin | 28589dc | 2016-06-09 17:50:51 -0700 | [diff] [blame] | 1166 | formulaTranslationY = -(findViewById(R.id.toolbar).getBottom() |
| 1167 | + mFormulaContainer.getBottom()); |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 1168 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1169 | |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1170 | // Change the result's textColor to match the formula. |
| 1171 | final int formulaTextColor = mFormulaText.getCurrentTextColor(); |
| 1172 | |
Hans Boehm | 4522315 | 2016-12-21 10:35:35 -0800 | [diff] [blame] | 1173 | if (resultWasPreserved) { |
| 1174 | // Result was previously addded to history. |
| 1175 | mEvaluator.represerve(); |
| 1176 | } else { |
Hans Boehm | a5ea8eb | 2016-12-01 12:33:38 -0800 | [diff] [blame] | 1177 | // Add current result to history. |
| 1178 | mEvaluator.preserve(true); |
Hans Boehm | 4522315 | 2016-12-21 10:35:35 -0800 | [diff] [blame] | 1179 | } |
Hans Boehm | a5ea8eb | 2016-12-01 12:33:38 -0800 | [diff] [blame] | 1180 | |
Hans Boehm | 4522315 | 2016-12-21 10:35:35 -0800 | [diff] [blame] | 1181 | if (animate) { |
Hans Boehm | ccc5566 | 2015-07-07 14:16:59 -0700 | [diff] [blame] | 1182 | mResultText.announceForAccessibility(getResources().getString(R.string.desc_eq)); |
| 1183 | mResultText.announceForAccessibility(mResultText.getText()); |
Hans Boehm | c1ea091 | 2015-06-19 15:05:07 -0700 | [diff] [blame] | 1184 | setState(CalculatorState.ANIMATE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1185 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 1186 | animatorSet.playTogether( |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1187 | ObjectAnimator.ofPropertyValuesHolder(mResultText, |
| 1188 | PropertyValuesHolder.ofFloat(View.SCALE_X, resultScale), |
| 1189 | PropertyValuesHolder.ofFloat(View.SCALE_Y, resultScale), |
| 1190 | PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, resultTranslationY)), |
| 1191 | ObjectAnimator.ofArgb(mResultText, TEXT_COLOR, formulaTextColor), |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 1192 | ObjectAnimator.ofFloat(mFormulaContainer, View.TRANSLATION_Y, |
| 1193 | formulaTranslationY)); |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1194 | animatorSet.setDuration(getResources().getInteger( |
| 1195 | android.R.integer.config_longAnimTime)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1196 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 1197 | @Override |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1198 | public void onAnimationEnd(Animator animation) { |
| 1199 | setState(CalculatorState.RESULT); |
| 1200 | mCurrentAnimator = null; |
| 1201 | } |
| 1202 | }); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1203 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1204 | mCurrentAnimator = animatorSet; |
| 1205 | animatorSet.start(); |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1206 | } else /* No animation desired; get there fast when restarting */ { |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1207 | mResultText.setScaleX(resultScale); |
| 1208 | mResultText.setScaleY(resultScale); |
| 1209 | mResultText.setTranslationY(resultTranslationY); |
| 1210 | mResultText.setTextColor(formulaTextColor); |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 1211 | mFormulaContainer.setTranslationY(formulaTranslationY); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1212 | setState(CalculatorState.RESULT); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1213 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1214 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1215 | |
| 1216 | // Restore positions of the formula and result displays back to their original, |
| 1217 | // pre-animation state. |
| 1218 | private void restoreDisplayPositions() { |
| 1219 | // Clear result. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1220 | mResultText.setText(""); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1221 | // Reset all of the values modified during the animation. |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1222 | mResultText.setScaleX(1.0f); |
| 1223 | mResultText.setScaleY(1.0f); |
| 1224 | mResultText.setTranslationX(0.0f); |
| 1225 | mResultText.setTranslationY(0.0f); |
Annie Chin | e918fd2 | 2016-03-09 11:07:54 -0800 | [diff] [blame] | 1226 | mFormulaContainer.setTranslationY(0.0f); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1227 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 1228 | mFormulaText.requestFocus(); |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | @Override |
| 1232 | public void onClick(AlertDialogFragment fragment, int which) { |
| 1233 | if (which == DialogInterface.BUTTON_POSITIVE) { |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 1234 | if (HistoryFragment.CLEAR_DIALOG_TAG.equals(fragment.getTag())) { |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 1235 | // TODO: Try to preserve the current, saved, and memory expressions. How should we |
| 1236 | // handle expressions to which they refer? |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 1237 | mEvaluator.clearEverything(); |
| 1238 | // TODO: It's not clear what we should really do here. This is an initial hack. |
| 1239 | // May want to make onClearAnimationEnd() private if/when we fix this. |
| 1240 | onClearAnimationEnd(); |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 1241 | mEvaluatorCallback.onMemoryStateChanged(); |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 1242 | onBackPressed(); |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 1243 | } else if (Evaluator.TIMEOUT_DIALOG_TAG.equals(fragment.getTag())) { |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 1244 | // Timeout extension request. |
| 1245 | mEvaluator.setLongTimeout(); |
| 1246 | } else { |
| 1247 | Log.e(TAG, "Unknown AlertDialogFragment click:" + fragment.getTag()); |
| 1248 | } |
Hans Boehm | 5e6a0ca | 2015-09-22 17:09:01 -0700 | [diff] [blame] | 1249 | } |
| 1250 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1251 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 1252 | @Override |
| 1253 | public boolean onCreateOptionsMenu(Menu menu) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1254 | super.onCreateOptionsMenu(menu); |
| 1255 | |
| 1256 | getMenuInflater().inflate(R.menu.activity_calculator, menu); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 1257 | return true; |
| 1258 | } |
| 1259 | |
| 1260 | @Override |
| 1261 | public boolean onPrepareOptionsMenu(Menu menu) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1262 | super.onPrepareOptionsMenu(menu); |
| 1263 | |
| 1264 | // Show the leading option when displaying a result. |
| 1265 | menu.findItem(R.id.menu_leading).setVisible(mCurrentState == CalculatorState.RESULT); |
| 1266 | |
| 1267 | // Show the fraction option when displaying a rational result. |
| 1268 | menu.findItem(R.id.menu_fraction).setVisible(mCurrentState == CalculatorState.RESULT |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1269 | && mEvaluator.getResult(Evaluator.MAIN_INDEX).exactlyDisplayable()); |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1270 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 1271 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | @Override |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 1275 | public boolean onOptionsItemSelected(MenuItem item) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1276 | switch (item.getItemId()) { |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 1277 | case R.id.menu_history: |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 1278 | showHistoryFragment(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); |
| 1279 | mDragLayout.setOpen(); |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 1280 | return true; |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1281 | case R.id.menu_leading: |
| 1282 | displayFull(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1283 | return true; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1284 | case R.id.menu_fraction: |
| 1285 | displayFraction(); |
| 1286 | return true; |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 1287 | case R.id.menu_licenses: |
| 1288 | startActivity(new Intent(this, Licenses.class)); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1289 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1290 | default: |
| 1291 | return super.onOptionsItemSelected(item); |
| 1292 | } |
| 1293 | } |
| 1294 | |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame^] | 1295 | /* Begin override CloseCallback method. */ |
| 1296 | |
| 1297 | @Override |
| 1298 | public void onClose() { |
| 1299 | removeHistoryFragment(); |
| 1300 | } |
| 1301 | |
| 1302 | /* End override CloseCallback method. */ |
| 1303 | |
| 1304 | /* Begin override DragCallback methods */ |
| 1305 | |
| 1306 | @Override |
| 1307 | public void onStartDraggingOpen() { |
| 1308 | showHistoryFragment(FragmentTransaction.TRANSIT_NONE); |
| 1309 | } |
| 1310 | |
| 1311 | @Override |
| 1312 | public void whileDragging(float yFraction) { |
| 1313 | // no-op |
| 1314 | } |
| 1315 | |
| 1316 | @Override |
| 1317 | public boolean shouldCaptureView(View view, int x, int y) { |
| 1318 | return mDragLayout.isMoving() |
| 1319 | || mDragLayout.isOpen() |
| 1320 | || mDragLayout.isViewUnder(mDisplayView, x, y); |
| 1321 | } |
| 1322 | |
| 1323 | @Override |
| 1324 | public int getDisplayHeight() { |
| 1325 | return mDisplayView.getMeasuredHeight(); |
| 1326 | } |
| 1327 | |
| 1328 | @Override |
| 1329 | public void onLayout(int translation) { |
| 1330 | mHistoryFrame.setTranslationY(translation + mDisplayView.getBottom()); |
| 1331 | } |
| 1332 | |
| 1333 | /* End override DragCallback methods */ |
| 1334 | |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 1335 | /** |
| 1336 | * Change evaluation state to one that's friendly to the history fragment. |
| 1337 | * Return false if that was not easily possible. |
| 1338 | */ |
| 1339 | private boolean prepareForHistory() { |
| 1340 | if (mCurrentState == CalculatorState.ANIMATE) { |
| 1341 | throw new AssertionError("onUserInteraction should have ended animation"); |
Annie Chin | e5567fd | 2016-12-12 13:45:24 -0800 | [diff] [blame] | 1342 | } else if (mCurrentState == CalculatorState.EVALUATE) { |
| 1343 | // Cancel current evaluation |
| 1344 | cancelIfEvaluating(true /* quiet */ ); |
| 1345 | setState(CalculatorState.INPUT); |
| 1346 | return true; |
| 1347 | } else if (mCurrentState == CalculatorState.INIT) { |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 1348 | // Easiest to just refuse. Otherwise we can see a state change |
| 1349 | // while in history mode, which causes all sorts of problems. |
| 1350 | // TODO: Consider other alternatives. If we're just doing the decimal conversion |
| 1351 | // at the end of an evaluation, we could treat this as RESULT state. |
| 1352 | return false; |
| 1353 | } |
| 1354 | // We should be in INPUT, INIT_FOR_RESULT, RESULT, or ERROR state. |
| 1355 | return true; |
| 1356 | } |
| 1357 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 1358 | private void showHistoryFragment(int transit) { |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 1359 | final FragmentManager manager = getFragmentManager(); |
| 1360 | if (manager == null || manager.isDestroyed()) { |
| 1361 | return; |
| 1362 | } |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 1363 | if (!prepareForHistory()) { |
| 1364 | return; |
| 1365 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 1366 | if (!mDragLayout.isOpen()) { |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 1367 | stopActionModeOrContextMenu(); |
| 1368 | |
Annie Chin | 450de8a | 2016-11-23 10:03:56 -0800 | [diff] [blame] | 1369 | manager.beginTransaction() |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 1370 | .replace(R.id.history_frame, mHistoryFragment, HistoryFragment.TAG) |
| 1371 | .setTransition(transit) |
| 1372 | .addToBackStack(HistoryFragment.TAG) |
| 1373 | .commit(); |
Annie Chin | 450de8a | 2016-11-23 10:03:56 -0800 | [diff] [blame] | 1374 | manager.executePendingTransactions(); |
Annie Chin | eb36f95 | 2016-12-08 17:27:19 -0800 | [diff] [blame] | 1375 | |
| 1376 | // When HistoryFragment is visible, hide all descendants of the main Calculator view. |
| 1377 | mMainCalculator.setImportantForAccessibility( |
| 1378 | View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 1379 | } |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 1380 | // TODO: pass current scroll position of result |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 1383 | private void displayMessage(String title, String message) { |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 1384 | AlertDialogFragment.showMessageDialog(this, title, message, null, null /* tag */); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1387 | private void displayFraction() { |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1388 | UnifiedReal result = mEvaluator.getResult(Evaluator.MAIN_INDEX); |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 1389 | displayMessage(getString(R.string.menu_fraction), |
| 1390 | KeyMaps.translateResult(result.toNiceString())); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | // Display full result to currently evaluated precision |
| 1394 | private void displayFull() { |
| 1395 | Resources res = getResources(); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 1396 | String msg = mResultText.getFullText(true /* withSeparators */) + " "; |
Justin Klaassen | 4459516 | 2015-05-28 17:55:20 -0700 | [diff] [blame] | 1397 | if (mResultText.fullTextIsExact()) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1398 | msg += res.getString(R.string.exact); |
| 1399 | } else { |
| 1400 | msg += res.getString(R.string.approximate); |
| 1401 | } |
Christine Franks | 7452d3a | 2016-10-27 13:41:18 -0700 | [diff] [blame] | 1402 | displayMessage(getString(R.string.menu_leading), msg); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1405 | /** |
| 1406 | * Add input characters to the end of the expression. |
| 1407 | * Map them to the appropriate button pushes when possible. Leftover characters |
| 1408 | * are added to mUnprocessedChars, which is presumed to immediately precede the newly |
| 1409 | * added characters. |
Hans Boehm | 65a99a4 | 2016-02-03 18:16:07 -0800 | [diff] [blame] | 1410 | * @param moreChars characters to be added |
| 1411 | * @param explicit these characters were explicitly typed by the user, not pasted |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1412 | */ |
| 1413 | private void addChars(String moreChars, boolean explicit) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1414 | if (mUnprocessedChars != null) { |
| 1415 | moreChars = mUnprocessedChars + moreChars; |
| 1416 | } |
| 1417 | int current = 0; |
| 1418 | int len = moreChars.length(); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 1419 | boolean lastWasDigit = false; |
Hans Boehm | 5d79d10 | 2015-09-16 16:33:47 -0700 | [diff] [blame] | 1420 | if (mCurrentState == CalculatorState.RESULT && len != 0) { |
| 1421 | // Clear display immediately for incomplete function name. |
| 1422 | switchToInput(KeyMaps.keyForChar(moreChars.charAt(current))); |
| 1423 | } |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 1424 | char groupingSeparator = KeyMaps.translateResult(",").charAt(0); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1425 | while (current < len) { |
| 1426 | char c = moreChars.charAt(current); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame] | 1427 | if (Character.isSpaceChar(c) || c == groupingSeparator) { |
| 1428 | ++current; |
| 1429 | continue; |
| 1430 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 1431 | int k = KeyMaps.keyForChar(c); |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 1432 | if (!explicit) { |
| 1433 | int expEnd; |
| 1434 | if (lastWasDigit && current != |
| 1435 | (expEnd = Evaluator.exponentEnd(moreChars, current))) { |
| 1436 | // Process scientific notation with 'E' when pasting, in spite of ambiguity |
| 1437 | // with base of natural log. |
| 1438 | // Otherwise the 10^x key is the user's friend. |
| 1439 | mEvaluator.addExponent(moreChars, current, expEnd); |
| 1440 | current = expEnd; |
| 1441 | lastWasDigit = false; |
| 1442 | continue; |
| 1443 | } else { |
| 1444 | boolean isDigit = KeyMaps.digVal(k) != KeyMaps.NOT_DIGIT; |
| 1445 | if (current == 0 && (isDigit || k == R.id.dec_point) |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1446 | && mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasTrailingConstant()) { |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 1447 | // Refuse to concatenate pasted content to trailing constant. |
| 1448 | // This makes pasting of calculator results more consistent, whether or |
| 1449 | // not the old calculator instance is still around. |
| 1450 | addKeyToExpr(R.id.op_mul); |
| 1451 | } |
| 1452 | lastWasDigit = (isDigit || lastWasDigit && k == R.id.dec_point); |
| 1453 | } |
| 1454 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1455 | if (k != View.NO_ID) { |
| 1456 | mCurrentButton = findViewById(k); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1457 | if (explicit) { |
| 1458 | addExplicitKeyToExpr(k); |
| 1459 | } else { |
| 1460 | addKeyToExpr(k); |
| 1461 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1462 | if (Character.isSurrogate(c)) { |
| 1463 | current += 2; |
| 1464 | } else { |
| 1465 | ++current; |
| 1466 | } |
| 1467 | continue; |
| 1468 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 1469 | int f = KeyMaps.funForString(moreChars, current); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1470 | if (f != View.NO_ID) { |
| 1471 | mCurrentButton = findViewById(f); |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 1472 | if (explicit) { |
| 1473 | addExplicitKeyToExpr(f); |
| 1474 | } else { |
| 1475 | addKeyToExpr(f); |
| 1476 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1477 | if (f == R.id.op_sqrt) { |
| 1478 | // Square root entered as function; don't lose the parenthesis. |
| 1479 | addKeyToExpr(R.id.lparen); |
| 1480 | } |
| 1481 | current = moreChars.indexOf('(', current) + 1; |
| 1482 | continue; |
| 1483 | } |
| 1484 | // There are characters left, but we can't convert them to button presses. |
| 1485 | mUnprocessedChars = moreChars.substring(current); |
| 1486 | redisplayAfterFormulaChange(); |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 1487 | showOrHideToolbar(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 1488 | return; |
| 1489 | } |
| 1490 | mUnprocessedChars = null; |
| 1491 | redisplayAfterFormulaChange(); |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 1492 | showOrHideToolbar(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
Hans Boehm | 8f051c3 | 2016-10-03 16:53:58 -0700 | [diff] [blame] | 1495 | private void clearIfNotInputState() { |
| 1496 | if (mCurrentState == CalculatorState.ERROR |
| 1497 | || mCurrentState == CalculatorState.RESULT) { |
| 1498 | setState(CalculatorState.INPUT); |
| 1499 | mEvaluator.clearMain(); |
| 1500 | } |
| 1501 | } |
| 1502 | |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 1503 | /** |
Christine Franks | bd90b79 | 2016-11-22 10:28:26 -0800 | [diff] [blame] | 1504 | * Since we only support LTR format, using the RTL comma does not make sense. |
| 1505 | */ |
| 1506 | private String getDecimalSeparator() { |
| 1507 | final char defaultSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator(); |
| 1508 | final char rtlComma = '\u066b'; |
| 1509 | return defaultSeparator == rtlComma ? "," : String.valueOf(defaultSeparator); |
| 1510 | } |
| 1511 | |
| 1512 | /** |
Chenjie Yu | 3937b65 | 2016-06-01 23:14:26 -0700 | [diff] [blame] | 1513 | * Clean up animation for context menu. |
| 1514 | */ |
| 1515 | @Override |
| 1516 | public void onContextMenuClosed(Menu menu) { |
| 1517 | stopActionModeOrContextMenu(); |
| 1518 | } |
Christine Franks | 1d99be1 | 2016-11-14 14:00:36 -0800 | [diff] [blame] | 1519 | |
| 1520 | public interface OnDisplayMemoryOperationsListener { |
| 1521 | boolean shouldDisplayMemory(); |
| 1522 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1523 | } |