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