Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 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 | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 17 | // FIXME: Menu handling, particularly for cut/paste, is very ugly |
| 18 | // and not the way it was intended. |
| 19 | // Other menus are not handled brilliantly either. |
| 20 | // TODO: Revisit handling of "Help" menu, so that it's more consistent |
| 21 | // with our conventions. |
| 22 | // TODO: See if we can make scrolling look better, especially on small |
| 23 | // displays. Fix evaluation interface so the evaluator returns entire |
| 24 | // result, and formatting of exponent etc. is done separately. |
| 25 | // TODO: Better indication of when the result is known to be exact. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 26 | // TODO: Fix placement of inverse trig buttons. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 27 | // TODO: Check and possibly fix accessability issues. |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 28 | // TODO: Copy & more general paste in formula? Note that this requires |
| 29 | // great care: Currently the text version of a displayed formula |
| 30 | // is not directly useful for re-evaluating the formula later, since |
| 31 | // it contains ellipses representing subexpressions evaluated with |
| 32 | // a different degree mode. Rather than supporting copy from the |
| 33 | // formula window, we may eventually want to support generation of a |
| 34 | // more useful text version in a separate window. It's not clear |
| 35 | // this is worth the added (code and user) complexity. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 36 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 37 | package com.android.calculator2; |
| 38 | |
| 39 | import android.animation.Animator; |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 40 | import android.animation.Animator.AnimatorListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 41 | import android.animation.AnimatorListenerAdapter; |
| 42 | import android.animation.AnimatorSet; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 43 | import android.animation.ObjectAnimator; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 44 | import android.app.Activity; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 45 | import android.app.AlertDialog; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 46 | import android.content.DialogInterface; |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 47 | import android.content.Intent; |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 48 | import android.content.res.Resources; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 49 | import android.graphics.Color; |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 50 | import android.graphics.Rect; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 51 | import android.net.Uri; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 52 | import android.os.Bundle; |
Justin Klaassen | f79d6f6 | 2014-08-26 12:27:08 -0700 | [diff] [blame] | 53 | import android.support.annotation.NonNull; |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 54 | import android.support.v4.view.ViewPager; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 55 | import android.text.SpannableString; |
| 56 | import android.text.Spanned; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 57 | import android.text.style.ForegroundColorSpan; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 58 | import android.view.KeyCharacterMap; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 59 | import android.view.KeyEvent; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 60 | import android.view.Menu; |
| 61 | import android.view.MenuItem; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 62 | import android.view.View; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 63 | import android.view.View.OnKeyListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 64 | import android.view.View.OnLongClickListener; |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 65 | import android.view.ViewAnimationUtils; |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 66 | import android.view.ViewGroupOverlay; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 67 | import android.view.animation.AccelerateDecelerateInterpolator; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 68 | import android.widget.TextView; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 69 | import android.widget.Toolbar; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 70 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 71 | import com.android.calculator2.CalculatorText.OnTextSizeChangeListener; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 72 | |
| 73 | import java.io.ByteArrayInputStream; |
| 74 | import java.io.ObjectInputStream; |
| 75 | import java.io.ByteArrayOutputStream; |
| 76 | import java.io.ObjectOutputStream; |
| 77 | import java.io.ObjectInput; |
| 78 | import java.io.ObjectOutput; |
| 79 | import java.io.IOException; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 80 | |
Justin Klaassen | 04f79c7 | 2014-06-27 17:25:35 -0700 | [diff] [blame] | 81 | public class Calculator extends Activity |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 82 | implements OnTextSizeChangeListener, OnLongClickListener, CalculatorText.PasteListener { |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Constant for an invalid resource id. |
| 86 | */ |
| 87 | public static final int INVALID_RES_ID = -1; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 88 | |
| 89 | private enum CalculatorState { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 90 | INPUT, // Result and formula both visible, no evaluation requested, |
| 91 | // Though result may be visible on bottom line. |
| 92 | EVALUATE, // Both visible, evaluation requested, evaluation/animation incomplete. |
| 93 | INIT, // Very temporary state used as alternative to EVALUATE |
| 94 | // during reinitialization. Do not animate on completion. |
| 95 | ANIMATE, // Result computed, animation to enlarge result window in progress. |
| 96 | RESULT, // Result displayed, formula invisible. |
| 97 | // If we are in RESULT state, the formula was evaluated without |
| 98 | // error to initial precision. |
| 99 | ERROR // Error displayed: Formula visible, result shows error message. |
| 100 | // Display similar to INPUT state. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 101 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 102 | // Normal transition sequence is |
| 103 | // INPUT -> EVALUATE -> ANIMATE -> RESULT (or ERROR) -> INPUT |
| 104 | // A RESULT -> ERROR transition is possible in rare corner cases, in which |
| 105 | // a higher precision evaluation exposes an error. This is possible, since we |
| 106 | // initially evaluate assuming we were given a well-defined problem. If we |
| 107 | // were actually asked to compute sqrt(<extremely tiny negative number>) we produce 0 |
| 108 | // unless we are asked for enough precision that we can distinguish the argument from zero. |
| 109 | // TODO: Consider further heuristics to reduce the chance of observing this? |
| 110 | // It already seems to be observable only in contrived cases. |
| 111 | // ANIMATE, ERROR, and RESULT are translated to an INIT state if the application |
| 112 | // is restarted in that state. This leads us to recompute and redisplay the result |
| 113 | // ASAP. |
| 114 | // TODO: Possibly save a bit more information, e.g. its initial display string |
| 115 | // or most significant digit position, to speed up restart. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 116 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 117 | // We currently assume that the formula does not change out from under us in |
| 118 | // any way. We explicitly handle all input to the formula here. |
| 119 | // TODO: Perhaps the formula should not be editable at all? |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 120 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 121 | private final OnKeyListener mFormulaOnKeyListener = new OnKeyListener() { |
| 122 | @Override |
| 123 | public boolean onKey(View view, int keyCode, KeyEvent keyEvent) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 124 | if (keyEvent.getAction() != KeyEvent.ACTION_UP) return true; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame^] | 125 | stopActionMode(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 126 | switch (keyCode) { |
| 127 | case KeyEvent.KEYCODE_NUMPAD_ENTER: |
| 128 | case KeyEvent.KEYCODE_ENTER: |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 129 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 130 | mCurrentButton = mEqualButton; |
| 131 | onEquals(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 132 | return true; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 133 | case KeyEvent.KEYCODE_DEL: |
| 134 | mCurrentButton = mDeleteButton; |
| 135 | onDelete(); |
| 136 | return true; |
| 137 | default: |
| 138 | final int raw = keyEvent.getKeyCharacterMap() |
| 139 | .get(keyCode, keyEvent.getMetaState()); |
| 140 | if ((raw & KeyCharacterMap.COMBINING_ACCENT) != 0) { |
| 141 | return true; // discard |
| 142 | } |
| 143 | // Try to discard non-printing characters and the like. |
| 144 | // The user will have to explicitly delete other junk that gets past us. |
| 145 | if (Character.isIdentifierIgnorable(raw) |
| 146 | || Character.isWhitespace(raw)) { |
| 147 | return true; |
| 148 | } |
| 149 | char c = (char)raw; |
| 150 | if (c == '=') { |
Hans Boehm | e57fb01 | 2015-05-07 19:52:32 -0700 | [diff] [blame] | 151 | mCurrentButton = mEqualButton; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 152 | onEquals(); |
| 153 | } else { |
| 154 | addChars(String.valueOf(c)); |
| 155 | redisplayAfterFormulaChange(); |
| 156 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | }; |
| 161 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 162 | private static final String NAME = Calculator.class.getName(); |
| 163 | private static final String KEY_DISPLAY_STATE = NAME + "_display_state"; |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 164 | private static final String KEY_UNPROCESSED_CHARS = NAME + "_unprocessed_chars"; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 165 | private static final String KEY_EVAL_STATE = NAME + "_eval_state"; |
| 166 | // Associated value is a byte array holding both mCalculatorState |
| 167 | // and the (much more complex) evaluator state. |
Justin Klaassen | 741471e | 2014-06-11 09:43:44 -0700 | [diff] [blame] | 168 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 169 | private CalculatorState mCurrentState; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 170 | private Evaluator mEvaluator; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 171 | |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 172 | private View mDisplayView; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 173 | private TextView mModeView; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 174 | private CalculatorText mFormulaText; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 175 | private CalculatorResult mResult; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 176 | |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 177 | private ViewPager mPadViewPager; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 178 | private View mDeleteButton; |
| 179 | private View mClearButton; |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 180 | private View mEqualButton; |
| 181 | private TextView mModeButton; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 182 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 183 | private View mCurrentButton; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 184 | private Animator mCurrentAnimator; |
| 185 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 186 | private String mUnprocessedChars = null; // Characters that were recently entered |
| 187 | // at the end of the display that have not yet |
| 188 | // been added to the underlying expression. |
| 189 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 190 | @Override |
| 191 | protected void onCreate(Bundle savedInstanceState) { |
| 192 | super.onCreate(savedInstanceState); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 193 | setContentView(R.layout.activity_calculator); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 194 | setActionBar((Toolbar) findViewById(R.id.toolbar)); |
| 195 | |
| 196 | // Hide all default options in the ActionBar. |
| 197 | getActionBar().setDisplayOptions(0); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 198 | |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 199 | mDisplayView = findViewById(R.id.display); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 200 | mModeView = (TextView) findViewById(R.id.deg_rad); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 201 | mFormulaText = (CalculatorText) findViewById(R.id.formula); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 202 | mResult = (CalculatorResult) findViewById(R.id.result); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 203 | |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 204 | mPadViewPager = (ViewPager) findViewById(R.id.pad_pager); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 205 | mDeleteButton = findViewById(R.id.del); |
| 206 | mClearButton = findViewById(R.id.clr); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 207 | mEqualButton = findViewById(R.id.pad_numeric).findViewById(R.id.eq); |
| 208 | if (mEqualButton == null || mEqualButton.getVisibility() != View.VISIBLE) { |
| 209 | mEqualButton = findViewById(R.id.pad_operator).findViewById(R.id.eq); |
| 210 | } |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 211 | mModeButton = (TextView) findViewById(R.id.mode_deg_rad); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 212 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 213 | mEvaluator = new Evaluator(this, mResult); |
| 214 | mResult.setEvaluator(mEvaluator); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 215 | KeyMaps.setActivity(this); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 216 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 217 | if (savedInstanceState != null) { |
| 218 | setState(CalculatorState.values()[ |
| 219 | savedInstanceState.getInt(KEY_DISPLAY_STATE, |
| 220 | CalculatorState.INPUT.ordinal())]); |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 221 | CharSequence unprocessed = savedInstanceState.getCharSequence(KEY_UNPROCESSED_CHARS); |
| 222 | if (unprocessed != null) { |
| 223 | mUnprocessedChars = unprocessed.toString(); |
| 224 | } |
| 225 | byte[] state = savedInstanceState.getByteArray(KEY_EVAL_STATE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 226 | if (state != null) { |
| 227 | try (ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(state))) { |
| 228 | mEvaluator.restoreInstanceState(in); |
| 229 | } catch (Throwable ignored) { |
| 230 | // When in doubt, revert to clean state |
| 231 | mCurrentState = CalculatorState.INPUT; |
| 232 | mEvaluator.clear(); |
| 233 | } |
| 234 | } |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 235 | } else { |
| 236 | mCurrentState = CalculatorState.INPUT; |
| 237 | mEvaluator.clear(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 238 | } |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 239 | mFormulaText.setOnKeyListener(mFormulaOnKeyListener); |
| 240 | mFormulaText.setOnTextSizeChangeListener(this); |
| 241 | mFormulaText.setPasteListener(this); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 242 | mDeleteButton.setOnLongClickListener(this); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 243 | updateDegreeMode(mEvaluator.getDegreeMode()); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 244 | if (mCurrentState != CalculatorState.INPUT) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 245 | // Just reevaluate. |
| 246 | redisplayFormula(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 247 | setState(CalculatorState.INIT); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 248 | mEvaluator.requireResult(); |
| 249 | } else { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 250 | redisplayAfterFormulaChange(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 251 | } |
| 252 | // TODO: We're currently not saving and restoring scroll position. |
| 253 | // We probably should. Details may require care to deal with: |
| 254 | // - new display size |
| 255 | // - slow recomputation if we've scrolled far. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
Justin Klaassen | f79d6f6 | 2014-08-26 12:27:08 -0700 | [diff] [blame] | 259 | protected void onSaveInstanceState(@NonNull Bundle outState) { |
| 260 | // If there's an animation in progress, cancel it first to ensure our state is up-to-date. |
| 261 | if (mCurrentAnimator != null) { |
| 262 | mCurrentAnimator.cancel(); |
| 263 | } |
| 264 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 265 | super.onSaveInstanceState(outState); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 266 | outState.putInt(KEY_DISPLAY_STATE, mCurrentState.ordinal()); |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 267 | outState.putCharSequence(KEY_UNPROCESSED_CHARS, mUnprocessedChars); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 268 | ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(); |
| 269 | try (ObjectOutput out = new ObjectOutputStream(byteArrayStream)) { |
| 270 | mEvaluator.saveInstanceState(out); |
| 271 | } catch (IOException e) { |
| 272 | // Impossible; No IO involved. |
| 273 | throw new AssertionError("Impossible IO exception", e); |
| 274 | } |
| 275 | outState.putByteArray(KEY_EVAL_STATE, byteArrayStream.toByteArray()); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 278 | // Set the state, updating delete label and display colors. |
| 279 | // This restores display positions on moving to INPUT. |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 280 | // But movement/animation for moving to RESULT has already been done. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 281 | private void setState(CalculatorState state) { |
| 282 | if (mCurrentState != state) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 283 | if (state == CalculatorState.INPUT) { |
| 284 | restoreDisplayPositions(); |
| 285 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 286 | mCurrentState = state; |
| 287 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 288 | if (mCurrentState == CalculatorState.RESULT) { |
| 289 | // No longer do this for ERROR; allow mistakes to be corrected. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 290 | mDeleteButton.setVisibility(View.GONE); |
| 291 | mClearButton.setVisibility(View.VISIBLE); |
| 292 | } else { |
| 293 | mDeleteButton.setVisibility(View.VISIBLE); |
| 294 | mClearButton.setVisibility(View.GONE); |
| 295 | } |
| 296 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 297 | if (mCurrentState == CalculatorState.ERROR) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 298 | final int errorColor = getResources().getColor(R.color.calculator_error_color); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 299 | mFormulaText.setTextColor(errorColor); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 300 | mResult.setTextColor(errorColor); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 301 | getWindow().setStatusBarColor(errorColor); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 302 | } else { |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 303 | mFormulaText.setTextColor( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 304 | getResources().getColor(R.color.display_formula_text_color)); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 305 | mResult.setTextColor( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 306 | getResources().getColor(R.color.display_result_text_color)); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 307 | getWindow().setStatusBarColor( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 308 | getResources().getColor(R.color.calculator_accent_color)); |
| 309 | } |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 310 | |
| 311 | invalidateOptionsMenu(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame^] | 315 | // Stop any active ActionMode. Return true if there was one. |
| 316 | private boolean stopActionMode() { |
| 317 | if (mResult.stopActionMode()) { |
| 318 | return true; |
| 319 | } |
| 320 | if (mFormulaText.stopActionMode()) { |
| 321 | return true; |
| 322 | } |
| 323 | return false; |
| 324 | } |
| 325 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 326 | @Override |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 327 | public void onBackPressed() { |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame^] | 328 | if (!stopActionMode()) { |
| 329 | if (mPadViewPager != null && mPadViewPager.getCurrentItem() != 0) { |
| 330 | // Select the previous pad. |
| 331 | mPadViewPager.setCurrentItem(mPadViewPager.getCurrentItem() - 1); |
| 332 | } else { |
| 333 | // If the user is currently looking at the first pad (or the pad is not paged), |
| 334 | // allow the system to handle the Back button. |
| 335 | super.onBackPressed(); |
| 336 | } |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 341 | public void onUserInteraction() { |
| 342 | super.onUserInteraction(); |
| 343 | |
| 344 | // If there's an animation in progress, cancel it so the user interaction can be handled |
| 345 | // immediately. |
| 346 | if (mCurrentAnimator != null) { |
| 347 | mCurrentAnimator.cancel(); |
| 348 | } |
| 349 | } |
| 350 | |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 351 | // Update the top corner degree/radian display and mode button |
| 352 | // to reflect the indicated current degree mode (true = degrees) |
| 353 | // TODO: Hide the top corner display until the advanced panel is exposed. |
| 354 | private void updateDegreeMode(boolean dm) { |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 355 | if (dm) { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 356 | mModeView.setText(R.string.mode_deg); |
| 357 | mModeButton.setText(R.string.mode_rad); |
| 358 | mModeButton.setContentDescription(getString(R.string.desc_mode_rad)); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 359 | } else { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 360 | mModeView.setText(R.string.mode_rad); |
| 361 | mModeButton.setText(R.string.mode_deg); |
| 362 | mModeButton.setContentDescription(getString(R.string.desc_mode_deg)); |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 365 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 366 | // Add the given button id to input expression. |
| 367 | // If appropriate, clear the expression before doing so. |
| 368 | private void addKeyToExpr(int id) { |
| 369 | if (mCurrentState == CalculatorState.ERROR) { |
| 370 | setState(CalculatorState.INPUT); |
| 371 | } else if (mCurrentState == CalculatorState.RESULT) { |
| 372 | if (KeyMaps.isBinary(id) || KeyMaps.isSuffix(id)) { |
| 373 | mEvaluator.collapse(); |
| 374 | } else { |
| 375 | mEvaluator.clear(); |
| 376 | } |
| 377 | setState(CalculatorState.INPUT); |
| 378 | } |
| 379 | if (!mEvaluator.append(id)) { |
| 380 | // TODO: Some user visible feedback? |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | private void redisplayAfterFormulaChange() { |
| 385 | // TODO: Could do this more incrementally. |
| 386 | redisplayFormula(); |
| 387 | setState(CalculatorState.INPUT); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 388 | if (mEvaluator.getExpr().hasInterestingOps()) { |
| 389 | mEvaluator.evaluateAndShowResult(); |
| 390 | } else { |
| 391 | mResult.clear(); |
| 392 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 395 | public void onButtonClick(View view) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 396 | mCurrentButton = view; |
Hans Boehm | 1176f23 | 2015-05-11 16:26:03 -0700 | [diff] [blame^] | 397 | stopActionMode(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 398 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 399 | // Always cancel in-progress evaluation. |
| 400 | // If we were waiting for the result, do nothing else. |
| 401 | mEvaluator.cancelAll(); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 402 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 403 | if (mCurrentState == CalculatorState.EVALUATE |
| 404 | || mCurrentState == CalculatorState.ANIMATE) { |
| 405 | onCancelled(); |
| 406 | return; |
| 407 | } |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 408 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 409 | final int id = view.getId(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 410 | switch (id) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 411 | case R.id.eq: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 412 | onEquals(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 413 | break; |
| 414 | case R.id.del: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 415 | onDelete(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 416 | break; |
| 417 | case R.id.clr: |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 418 | onClear(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 419 | break; |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 420 | case R.id.mode_deg_rad: |
| 421 | boolean mode = !mEvaluator.getDegreeMode(); |
| 422 | updateDegreeMode(mode); |
| 423 | if (mCurrentState == CalculatorState.RESULT) { |
| 424 | mEvaluator.collapse(); // Capture result evaluated in old mode |
| 425 | redisplayFormula(); |
| 426 | } |
| 427 | // In input mode, we reinterpret already entered trig functions. |
| 428 | mEvaluator.setDegreeMode(mode); |
| 429 | setState(CalculatorState.INPUT); |
| 430 | mResult.clear(); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 431 | if (mEvaluator.getExpr().hasInterestingOps()) { |
| 432 | mEvaluator.evaluateAndShowResult(); |
| 433 | } |
Hans Boehm | bfe8c22 | 2015-04-02 16:26:07 -0700 | [diff] [blame] | 434 | break; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 435 | default: |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 436 | addKeyToExpr(id); |
| 437 | redisplayAfterFormulaChange(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | } |
| 441 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 442 | void redisplayFormula() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 443 | String formula = mEvaluator.getExpr().toString(this); |
| 444 | if (mUnprocessedChars != null) { |
| 445 | // Add and highlight characters we couldn't process. |
| 446 | SpannableString formatted = new SpannableString(formula + mUnprocessedChars); |
| 447 | // TODO: should probably match this to the error color. |
| 448 | formatted.setSpan(new ForegroundColorSpan(Color.RED), |
| 449 | formula.length(), formatted.length(), |
| 450 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 451 | mFormulaText.setText(formatted); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 452 | } else { |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 453 | mFormulaText.setText(formula); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 454 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 457 | @Override |
| 458 | public boolean onLongClick(View view) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 459 | mCurrentButton = view; |
| 460 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 461 | if (view.getId() == R.id.del) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 462 | onClear(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 463 | return true; |
| 464 | } |
| 465 | return false; |
| 466 | } |
| 467 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 468 | // Initial evaluation completed successfully. Initiate display. |
| 469 | public void onEvaluate(int initDisplayPrec, String truncatedWholeNumber) { |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 470 | // Invalidate any options that may depend on the current result. |
| 471 | invalidateOptionsMenu(); |
| 472 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 473 | if (mCurrentState == CalculatorState.INPUT) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 474 | // Just update small result display. |
| 475 | mResult.displayResult(initDisplayPrec, truncatedWholeNumber); |
| 476 | } else { // in EVALUATE or INIT state |
| 477 | mResult.displayResult(initDisplayPrec, truncatedWholeNumber); |
| 478 | onResult(mCurrentState != CalculatorState.INIT); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 479 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 480 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 481 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 482 | public void onCancelled() { |
| 483 | // We should be in EVALUATE state. |
| 484 | // Display is still in input state. |
| 485 | setState(CalculatorState.INPUT); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 486 | mResult.clear(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // Reevaluation completed; ask result to redisplay current value. |
| 490 | public void onReevaluate() |
| 491 | { |
| 492 | mResult.redisplay(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 495 | @Override |
| 496 | public void onTextSizeChanged(final TextView textView, float oldSize) { |
| 497 | if (mCurrentState != CalculatorState.INPUT) { |
| 498 | // Only animate text changes that occur from user input. |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | // Calculate the values needed to perform the scale and translation animations, |
| 503 | // maintaining the same apparent baseline for the displayed text. |
| 504 | final float textScale = oldSize / textView.getTextSize(); |
| 505 | final float translationX = (1.0f - textScale) * |
| 506 | (textView.getWidth() / 2.0f - textView.getPaddingEnd()); |
| 507 | final float translationY = (1.0f - textScale) * |
| 508 | (textView.getHeight() / 2.0f - textView.getPaddingBottom()); |
| 509 | |
| 510 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 511 | animatorSet.playTogether( |
| 512 | ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f), |
| 513 | ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f), |
| 514 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f), |
| 515 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f)); |
Justin Klaassen | 94db720 | 2014-06-11 11:22:31 -0700 | [diff] [blame] | 516 | animatorSet.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 517 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 518 | animatorSet.start(); |
| 519 | } |
| 520 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 521 | private void onEquals() { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 522 | if (mCurrentState == CalculatorState.INPUT && !mEvaluator.getExpr().isEmpty()) { |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 523 | setState(CalculatorState.EVALUATE); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 524 | mEvaluator.requireResult(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | private void onDelete() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 529 | // Delete works like backspace; remove the last character or operator from the expression. |
| 530 | // Note that we handle keyboard delete exactly like the delete button. For |
| 531 | // example the delete button can be used to delete a character from an incomplete |
| 532 | // function name typed on a physical keyboard. |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 533 | mEvaluator.cancelAll(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 534 | // This should be impossible in RESULT state. |
| 535 | setState(CalculatorState.INPUT); |
| 536 | if (mUnprocessedChars != null) { |
| 537 | int len = mUnprocessedChars.length(); |
| 538 | if (len > 0) { |
| 539 | mUnprocessedChars = mUnprocessedChars.substring(0, len-1); |
| 540 | } else { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 541 | mEvaluator.delete(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 542 | } |
| 543 | } else { |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 544 | mEvaluator.delete(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 545 | } |
| 546 | redisplayAfterFormulaChange(); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 549 | private void reveal(View sourceView, int colorRes, AnimatorListener listener) { |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 550 | final ViewGroupOverlay groupOverlay = |
| 551 | (ViewGroupOverlay) getWindow().getDecorView().getOverlay(); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 552 | |
| 553 | final Rect displayRect = new Rect(); |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 554 | mDisplayView.getGlobalVisibleRect(displayRect); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 555 | |
| 556 | // Make reveal cover the display and status bar. |
| 557 | final View revealView = new View(this); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 558 | revealView.setBottom(displayRect.bottom); |
| 559 | revealView.setLeft(displayRect.left); |
| 560 | revealView.setRight(displayRect.right); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 561 | revealView.setBackgroundColor(getResources().getColor(colorRes)); |
Justin Klaassen | 06360f9 | 2014-08-28 11:08:44 -0700 | [diff] [blame] | 562 | groupOverlay.add(revealView); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 563 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 564 | final int[] clearLocation = new int[2]; |
| 565 | sourceView.getLocationInWindow(clearLocation); |
| 566 | clearLocation[0] += sourceView.getWidth() / 2; |
| 567 | clearLocation[1] += sourceView.getHeight() / 2; |
| 568 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 569 | final int revealCenterX = clearLocation[0] - revealView.getLeft(); |
| 570 | final int revealCenterY = clearLocation[1] - revealView.getTop(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 571 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 572 | final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2); |
| 573 | final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2); |
| 574 | final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 575 | final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2)); |
| 576 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 577 | final Animator revealAnimator = |
| 578 | ViewAnimationUtils.createCircularReveal(revealView, |
ztenghui | 3d6ecaf | 2014-06-05 09:56:00 -0700 | [diff] [blame] | 579 | revealCenterX, revealCenterY, 0.0f, revealRadius); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 580 | revealAnimator.setDuration( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 581 | getResources().getInteger(android.R.integer.config_longAnimTime)); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 582 | revealAnimator.addListener(listener); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 583 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 584 | final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 585 | alphaAnimator.setDuration( |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 586 | getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 587 | |
| 588 | final AnimatorSet animatorSet = new AnimatorSet(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 589 | animatorSet.play(revealAnimator).before(alphaAnimator); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 590 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 591 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 592 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 593 | public void onAnimationEnd(Animator animator) { |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 594 | groupOverlay.remove(revealView); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 595 | mCurrentAnimator = null; |
| 596 | } |
| 597 | }); |
| 598 | |
| 599 | mCurrentAnimator = animatorSet; |
| 600 | animatorSet.start(); |
| 601 | } |
| 602 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 603 | private void onClear() { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 604 | if (mEvaluator.getExpr().isEmpty()) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 605 | return; |
| 606 | } |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 607 | reveal(mCurrentButton, R.color.calculator_accent_color, new AnimatorListenerAdapter() { |
| 608 | @Override |
| 609 | public void onAnimationEnd(Animator animation) { |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 610 | mUnprocessedChars = null; |
| 611 | mResult.clear(); |
| 612 | mEvaluator.clear(); |
| 613 | setState(CalculatorState.INPUT); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 614 | redisplayFormula(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 615 | } |
| 616 | }); |
| 617 | } |
| 618 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 619 | // Evaluation encountered en error. Display the error. |
| 620 | void onError(final int errorResourceId) { |
Hans Boehm | fbcef70 | 2015-04-27 18:07:47 -0700 | [diff] [blame] | 621 | if (mCurrentState == CalculatorState.EVALUATE) { |
| 622 | setState(CalculatorState.ANIMATE); |
| 623 | reveal(mCurrentButton, R.color.calculator_error_color, |
| 624 | new AnimatorListenerAdapter() { |
| 625 | @Override |
| 626 | public void onAnimationEnd(Animator animation) { |
| 627 | setState(CalculatorState.ERROR); |
| 628 | mResult.displayError(errorResourceId); |
| 629 | } |
| 630 | }); |
| 631 | } else if (mCurrentState == CalculatorState.INIT) { |
| 632 | setState(CalculatorState.ERROR); |
| 633 | mResult.displayError(errorResourceId); |
Hans Boehm | c023b73 | 2015-04-29 11:30:47 -0700 | [diff] [blame] | 634 | } else { |
| 635 | mResult.clear(); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 636 | } |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 639 | |
| 640 | // Animate movement of result into the top formula slot. |
| 641 | // Result window now remains translated in the top slot while the result is displayed. |
| 642 | // (We convert it back to formula use only when the user provides new input.) |
| 643 | // Historical note: In the Lollipop version, this invisibly and instantaeously moved |
| 644 | // formula and result displays back at the end of the animation. We no longer do that, |
| 645 | // so that we can continue to properly support scrolling of the result. |
| 646 | // We assume the result already contains the text to be expanded. |
| 647 | private void onResult(boolean animate) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 648 | // Calculate the values needed to perform the scale and translation animations. |
| 649 | // We now fix the character size in the display to avoid weird effects |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 650 | // when we scroll. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 651 | // Display.xml is designed to ensure exactly a 3/2 ratio between the formula |
| 652 | // slot and small result slot. |
| 653 | final float resultScale = 1.5f; |
| 654 | final float resultTranslationX = -mResult.getWidth() * (resultScale - 1)/2; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 655 | // mFormulaText is aligned with mResult on the right. |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 656 | // When we enlarge it around its center, the right side |
| 657 | // moves to the right. This compensates. |
| 658 | float resultTranslationY = -mResult.getHeight(); |
| 659 | // This is how much we want to move the bottom. |
| 660 | // Now compensate for the fact that we're |
| 661 | // simultaenously expanding it around its center by half its height |
Hans Boehm | 760a9dc | 2015-04-20 10:27:12 -0700 | [diff] [blame] | 662 | resultTranslationY += mResult.getHeight() * (resultScale - 1)/2; |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 663 | final float formulaTranslationY = -mFormulaText.getBottom(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 664 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 665 | // TODO: Reintroduce textColorAnimator? |
| 666 | // The initial and final colors seemed to be the same in L. |
| 667 | // With the new model, the result logically changes back to a formula |
| 668 | // only when we switch back to INPUT state, so it's unclear that animating |
| 669 | // a color change here makes sense. |
| 670 | if (animate) { |
| 671 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 672 | animatorSet.playTogether( |
| 673 | ObjectAnimator.ofFloat(mResult, View.SCALE_X, resultScale), |
| 674 | ObjectAnimator.ofFloat(mResult, View.SCALE_Y, resultScale), |
| 675 | ObjectAnimator.ofFloat(mResult, View.TRANSLATION_X, resultTranslationX), |
| 676 | ObjectAnimator.ofFloat(mResult, View.TRANSLATION_Y, resultTranslationY), |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 677 | ObjectAnimator.ofFloat(mFormulaText, View.TRANSLATION_Y, |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 678 | formulaTranslationY)); |
| 679 | animatorSet.setDuration( |
| 680 | getResources().getInteger(android.R.integer.config_longAnimTime)); |
| 681 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 682 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 683 | @Override |
| 684 | public void onAnimationStart(Animator animation) { |
| 685 | // Result should already be displayed; no need to do anything. |
| 686 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 687 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 688 | @Override |
| 689 | public void onAnimationEnd(Animator animation) { |
| 690 | setState(CalculatorState.RESULT); |
| 691 | mCurrentAnimator = null; |
| 692 | } |
| 693 | }); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 694 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 695 | mCurrentAnimator = animatorSet; |
| 696 | animatorSet.start(); |
| 697 | } else /* No animation desired; get there fast, e.g. when restarting */ { |
| 698 | mResult.setScaleX(resultScale); |
| 699 | mResult.setScaleY(resultScale); |
| 700 | mResult.setTranslationX(resultTranslationX); |
| 701 | mResult.setTranslationY(resultTranslationY); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 702 | mFormulaText.setTranslationY(formulaTranslationY); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 703 | setState(CalculatorState.RESULT); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 704 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 705 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 706 | |
| 707 | // Restore positions of the formula and result displays back to their original, |
| 708 | // pre-animation state. |
| 709 | private void restoreDisplayPositions() { |
| 710 | // Clear result. |
| 711 | mResult.setText(""); |
| 712 | // Reset all of the values modified during the animation. |
| 713 | mResult.setScaleX(1.0f); |
| 714 | mResult.setScaleY(1.0f); |
| 715 | mResult.setTranslationX(0.0f); |
| 716 | mResult.setTranslationY(0.0f); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 717 | mFormulaText.setTranslationY(0.0f); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 718 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 719 | mFormulaText.requestFocus(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 722 | @Override |
| 723 | public boolean onCreateOptionsMenu(Menu menu) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 724 | super.onCreateOptionsMenu(menu); |
| 725 | |
| 726 | getMenuInflater().inflate(R.menu.activity_calculator, menu); |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 727 | return true; |
| 728 | } |
| 729 | |
| 730 | @Override |
| 731 | public boolean onPrepareOptionsMenu(Menu menu) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 732 | super.onPrepareOptionsMenu(menu); |
| 733 | |
| 734 | // Show the leading option when displaying a result. |
| 735 | menu.findItem(R.id.menu_leading).setVisible(mCurrentState == CalculatorState.RESULT); |
| 736 | |
| 737 | // Show the fraction option when displaying a rational result. |
| 738 | menu.findItem(R.id.menu_fraction).setVisible(mCurrentState == CalculatorState.RESULT |
| 739 | && mEvaluator.getRational() != null); |
| 740 | |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 741 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | @Override |
Justin Klaassen | d48b756 | 2015-04-16 16:51:38 -0700 | [diff] [blame] | 745 | public boolean onOptionsItemSelected(MenuItem item) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 746 | switch (item.getItemId()) { |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 747 | case R.id.menu_leading: |
| 748 | displayFull(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 749 | return true; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 750 | case R.id.menu_fraction: |
| 751 | displayFraction(); |
| 752 | return true; |
Justin Klaassen | d36d63e | 2015-05-05 12:59:36 -0700 | [diff] [blame] | 753 | case R.id.menu_licenses: |
| 754 | startActivity(new Intent(this, Licenses.class)); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 755 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 756 | default: |
| 757 | return super.onOptionsItemSelected(item); |
| 758 | } |
| 759 | } |
| 760 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 761 | private void displayMessage(String s) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 762 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 763 | builder.setMessage(s) |
| 764 | .setNegativeButton(R.string.dismiss, |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 765 | new DialogInterface.OnClickListener() { |
| 766 | public void onClick(DialogInterface d, int which) { } |
| 767 | }) |
| 768 | .show(); |
| 769 | } |
| 770 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 771 | private void displayFraction() { |
| 772 | BoundedRational result = mEvaluator.getRational(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 773 | displayMessage(KeyMaps.translateResult(result.toNiceString())); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | // Display full result to currently evaluated precision |
| 777 | private void displayFull() { |
| 778 | Resources res = getResources(); |
| 779 | String msg = mResult.getFullText() + " "; |
| 780 | if (mResult.fullTextIsExact()) { |
| 781 | msg += res.getString(R.string.exact); |
| 782 | } else { |
| 783 | msg += res.getString(R.string.approximate); |
| 784 | } |
| 785 | displayMessage(msg); |
| 786 | } |
| 787 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 788 | // Add input characters to the end of the expression by mapping them to |
| 789 | // the appropriate button pushes when possible. Leftover characters |
| 790 | // are added to mUnprocessedChars, which is presumed to immediately |
| 791 | // precede the newly added characters. |
| 792 | private void addChars(String moreChars) { |
| 793 | if (mUnprocessedChars != null) { |
| 794 | moreChars = mUnprocessedChars + moreChars; |
| 795 | } |
| 796 | int current = 0; |
| 797 | int len = moreChars.length(); |
| 798 | while (current < len) { |
| 799 | char c = moreChars.charAt(current); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 800 | int k = KeyMaps.keyForChar(c); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 801 | if (k != View.NO_ID) { |
| 802 | mCurrentButton = findViewById(k); |
| 803 | addKeyToExpr(k); |
| 804 | if (Character.isSurrogate(c)) { |
| 805 | current += 2; |
| 806 | } else { |
| 807 | ++current; |
| 808 | } |
| 809 | continue; |
| 810 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 811 | int f = KeyMaps.funForString(moreChars, current); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 812 | if (f != View.NO_ID) { |
| 813 | mCurrentButton = findViewById(f); |
| 814 | addKeyToExpr(f); |
| 815 | if (f == R.id.op_sqrt) { |
| 816 | // Square root entered as function; don't lose the parenthesis. |
| 817 | addKeyToExpr(R.id.lparen); |
| 818 | } |
| 819 | current = moreChars.indexOf('(', current) + 1; |
| 820 | continue; |
| 821 | } |
| 822 | // There are characters left, but we can't convert them to button presses. |
| 823 | mUnprocessedChars = moreChars.substring(current); |
| 824 | redisplayAfterFormulaChange(); |
| 825 | return; |
| 826 | } |
| 827 | mUnprocessedChars = null; |
| 828 | redisplayAfterFormulaChange(); |
| 829 | return; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 830 | } |
| 831 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 832 | @Override |
| 833 | public boolean paste(Uri uri) { |
| 834 | if (mEvaluator.isLastSaved(uri)) { |
| 835 | if (mCurrentState == CalculatorState.ERROR |
| 836 | || mCurrentState == CalculatorState.RESULT) { |
| 837 | setState(CalculatorState.INPUT); |
| 838 | mEvaluator.clear(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 839 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 840 | mEvaluator.addSaved(); |
| 841 | redisplayAfterFormulaChange(); |
| 842 | return true; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 843 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 844 | return false; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 845 | } |
| 846 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 847 | @Override |
| 848 | public void paste(String s) { |
| 849 | addChars(s); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 852 | } |