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