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