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