blob: f51f3fbd841837f13cf2271b58b5aa59404f7045 [file] [log] [blame]
Justin Klaassen4b3af052014-05-27 17:53:10 -07001/*
Justin Klaassen12da1ad2016-04-04 14:20:37 -07002 * Copyright (C) 2016 The Android Open Source Project
Justin Klaassen4b3af052014-05-27 17:53:10 -07003 *
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 Boehm013969e2015-04-13 20:29:47 -070017// 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 Boehm84614952014-11-25 18:46:17 -080025
Justin Klaassen4b3af052014-05-27 17:53:10 -070026package com.android.calculator2;
27
28import android.animation.Animator;
Justin Klaassen5f2a3342014-06-11 17:40:22 -070029import android.animation.Animator.AnimatorListener;
Justin Klaassen4b3af052014-05-27 17:53:10 -070030import android.animation.AnimatorListenerAdapter;
31import android.animation.AnimatorSet;
Justin Klaassen4b3af052014-05-27 17:53:10 -070032import android.animation.ObjectAnimator;
Justin Klaassen44595162015-05-28 17:55:20 -070033import android.animation.PropertyValuesHolder;
Justin Klaassen9d33cdc2016-02-21 14:16:14 -080034import android.app.ActionBar;
Justin Klaassen4b3af052014-05-27 17:53:10 -070035import android.app.Activity;
Justin Klaassenfc5ac822015-06-18 13:15:17 -070036import android.content.ClipData;
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070037import android.content.DialogInterface;
Justin Klaassend36d63e2015-05-05 12:59:36 -070038import android.content.Intent;
Hans Boehmbfe8c222015-04-02 16:26:07 -070039import android.content.res.Resources;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070040import android.graphics.Color;
Justin Klaassen8fff1442014-06-19 10:43:29 -070041import android.graphics.Rect;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070042import android.net.Uri;
Justin Klaassen4b3af052014-05-27 17:53:10 -070043import android.os.Bundle;
Justin Klaassenf79d6f62014-08-26 12:27:08 -070044import android.support.annotation.NonNull;
Justin Klaassen3b4d13d2014-06-06 18:18:37 +010045import android.support.v4.view.ViewPager;
Annie Chine918fd22016-03-09 11:07:54 -080046import android.text.Editable;
Hans Boehm8a4f81c2015-07-09 10:41:25 -070047import android.text.SpannableStringBuilder;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070048import android.text.Spanned;
Annie Chinf360ef02016-03-10 13:45:39 -080049import android.text.TextUtils;
Annie Chine918fd22016-03-09 11:07:54 -080050import android.text.TextWatcher;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070051import android.text.style.ForegroundColorSpan;
Justin Klaassen44595162015-05-28 17:55:20 -070052import android.util.Property;
Annie Chine918fd22016-03-09 11:07:54 -080053import android.view.ActionMode;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -070054import android.view.KeyCharacterMap;
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -070055import android.view.KeyEvent;
Hans Boehm84614952014-11-25 18:46:17 -080056import android.view.Menu;
57import android.view.MenuItem;
Justin Klaassen4b3af052014-05-27 17:53:10 -070058import android.view.View;
59import android.view.View.OnLongClickListener;
Justin Klaassen5f2a3342014-06-11 17:40:22 -070060import android.view.ViewAnimationUtils;
Justin Klaassen8fff1442014-06-19 10:43:29 -070061import android.view.ViewGroupOverlay;
Annie Chine918fd22016-03-09 11:07:54 -080062import android.view.ViewTreeObserver;
Justin Klaassen4b3af052014-05-27 17:53:10 -070063import android.view.animation.AccelerateDecelerateInterpolator;
Annie Chine918fd22016-03-09 11:07:54 -080064import android.widget.HorizontalScrollView;
Justin Klaassenfed941a2014-06-09 18:42:40 +010065import android.widget.TextView;
Justin Klaassend48b7562015-04-16 16:51:38 -070066import android.widget.Toolbar;
Justin Klaassenfed941a2014-06-09 18:42:40 +010067
Hans Boehm08e8f322015-04-21 13:18:38 -070068import com.android.calculator2.CalculatorText.OnTextSizeChangeListener;
Hans Boehm84614952014-11-25 18:46:17 -080069
70import java.io.ByteArrayInputStream;
Hans Boehm84614952014-11-25 18:46:17 -080071import java.io.ByteArrayOutputStream;
Hans Boehm84614952014-11-25 18:46:17 -080072import java.io.IOException;
Justin Klaassen721ec842015-05-28 14:30:08 -070073import java.io.ObjectInput;
74import java.io.ObjectInputStream;
75import java.io.ObjectOutput;
76import java.io.ObjectOutputStream;
Justin Klaassen4b3af052014-05-27 17:53:10 -070077
Justin Klaassen04f79c72014-06-27 17:25:35 -070078public class Calculator extends Activity
Hans Boehm5e6a0ca2015-09-22 17:09:01 -070079 implements OnTextSizeChangeListener, OnLongClickListener, CalculatorText.OnPasteListener,
80 AlertDialogFragment.OnClickListener {
Justin Klaassen2be4fdb2014-08-06 19:54:09 -070081
82 /**
83 * Constant for an invalid resource id.
84 */
85 public static final int INVALID_RES_ID = -1;
Justin Klaassen4b3af052014-05-27 17:53:10 -070086
87 private enum CalculatorState {
Hans Boehm84614952014-11-25 18:46:17 -080088 INPUT, // Result and formula both visible, no evaluation requested,
89 // Though result may be visible on bottom line.
90 EVALUATE, // Both visible, evaluation requested, evaluation/animation incomplete.
Hans Boehmc1ea0912015-06-19 15:05:07 -070091 // Not used for instant result evaluation.
Hans Boehm84614952014-11-25 18:46:17 -080092 INIT, // Very temporary state used as alternative to EVALUATE
93 // during reinitialization. Do not animate on completion.
94 ANIMATE, // Result computed, animation to enlarge result window in progress.
95 RESULT, // Result displayed, formula invisible.
96 // If we are in RESULT state, the formula was evaluated without
97 // error to initial precision.
98 ERROR // Error displayed: Formula visible, result shows error message.
99 // Display similar to INPUT state.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700100 }
Hans Boehm84614952014-11-25 18:46:17 -0800101 // Normal transition sequence is
102 // INPUT -> EVALUATE -> ANIMATE -> RESULT (or ERROR) -> INPUT
103 // A RESULT -> ERROR transition is possible in rare corner cases, in which
104 // a higher precision evaluation exposes an error. This is possible, since we
105 // initially evaluate assuming we were given a well-defined problem. If we
106 // were actually asked to compute sqrt(<extremely tiny negative number>) we produce 0
107 // unless we are asked for enough precision that we can distinguish the argument from zero.
108 // TODO: Consider further heuristics to reduce the chance of observing this?
109 // It already seems to be observable only in contrived cases.
110 // ANIMATE, ERROR, and RESULT are translated to an INIT state if the application
111 // is restarted in that state. This leads us to recompute and redisplay the result
112 // ASAP.
113 // TODO: Possibly save a bit more information, e.g. its initial display string
114 // or most significant digit position, to speed up restart.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700115
Justin Klaassen44595162015-05-28 17:55:20 -0700116 private final Property<TextView, Integer> TEXT_COLOR =
117 new Property<TextView, Integer>(Integer.class, "textColor") {
118 @Override
119 public Integer get(TextView textView) {
120 return textView.getCurrentTextColor();
121 }
122
123 @Override
124 public void set(TextView textView, Integer textColor) {
125 textView.setTextColor(textColor);
126 }
127 };
128
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800129 private static final String NAME = "Calculator";
Hans Boehm84614952014-11-25 18:46:17 -0800130 private static final String KEY_DISPLAY_STATE = NAME + "_display_state";
Hans Boehm760a9dc2015-04-20 10:27:12 -0700131 private static final String KEY_UNPROCESSED_CHARS = NAME + "_unprocessed_chars";
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800132 /**
133 * Associated value is a byte array holding the evaluator state.
134 */
Hans Boehm84614952014-11-25 18:46:17 -0800135 private static final String KEY_EVAL_STATE = NAME + "_eval_state";
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800136 private static final String KEY_INVERSE_MODE = NAME + "_inverse_mode";
Justin Klaassen741471e2014-06-11 09:43:44 -0700137
Annie Chine918fd22016-03-09 11:07:54 -0800138 private final ViewTreeObserver.OnPreDrawListener mPreDrawListener =
139 new ViewTreeObserver.OnPreDrawListener() {
140 @Override
141 public boolean onPreDraw() {
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700142 mFormulaContainer.scrollTo(mFormulaText.getRight(), 0);
Annie Chine918fd22016-03-09 11:07:54 -0800143 final ViewTreeObserver observer = mFormulaContainer.getViewTreeObserver();
144 if (observer.isAlive()) {
145 observer.removeOnPreDrawListener(this);
146 }
147 return false;
148 }
149 };
150
151 private final TextWatcher mFormulaTextWatcher = new TextWatcher() {
152 @Override
153 public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
154 }
155
156 @Override
157 public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
158 }
159
160 @Override
161 public void afterTextChanged(Editable editable) {
162 final ViewTreeObserver observer = mFormulaContainer.getViewTreeObserver();
163 if (observer.isAlive()) {
164 observer.removeOnPreDrawListener(mPreDrawListener);
165 observer.addOnPreDrawListener(mPreDrawListener);
166 }
167 }
168 };
169
Justin Klaassen4b3af052014-05-27 17:53:10 -0700170 private CalculatorState mCurrentState;
Hans Boehm84614952014-11-25 18:46:17 -0800171 private Evaluator mEvaluator;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700172
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800173 private CalculatorDisplay mDisplayView;
Justin Klaassend48b7562015-04-16 16:51:38 -0700174 private TextView mModeView;
Hans Boehm08e8f322015-04-21 13:18:38 -0700175 private CalculatorText mFormulaText;
Justin Klaassen44595162015-05-28 17:55:20 -0700176 private CalculatorResult mResultText;
Annie Chine918fd22016-03-09 11:07:54 -0800177 private HorizontalScrollView mFormulaContainer;
Justin Klaassend48b7562015-04-16 16:51:38 -0700178
Justin Klaassen3b4d13d2014-06-06 18:18:37 +0100179 private ViewPager mPadViewPager;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700180 private View mDeleteButton;
181 private View mClearButton;
Justin Klaassend48b7562015-04-16 16:51:38 -0700182 private View mEqualButton;
Justin Klaassene2711cb2015-05-28 11:13:17 -0700183
184 private TextView mInverseToggle;
185 private TextView mModeToggle;
186
Justin Klaassen721ec842015-05-28 14:30:08 -0700187 private View[] mInvertibleButtons;
Justin Klaassene2711cb2015-05-28 11:13:17 -0700188 private View[] mInverseButtons;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700189
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700190 private View mCurrentButton;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700191 private Animator mCurrentAnimator;
192
Hans Boehm8a4f81c2015-07-09 10:41:25 -0700193 // Characters that were recently entered at the end of the display that have not yet
194 // been added to the underlying expression.
195 private String mUnprocessedChars = null;
196
197 // Color to highlight unprocessed characters from physical keyboard.
198 // TODO: should probably match this to the error color?
199 private ForegroundColorSpan mUnprocessedColorSpan = new ForegroundColorSpan(Color.RED);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700200
Annie Chin26e159e2016-05-18 15:17:14 -0700201 // Whether the display is one line.
202 private boolean mOneLine;
203
Justin Klaassen4b3af052014-05-27 17:53:10 -0700204 @Override
205 protected void onCreate(Bundle savedInstanceState) {
206 super.onCreate(savedInstanceState);
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700207 setContentView(R.layout.activity_calculator);
Justin Klaassend48b7562015-04-16 16:51:38 -0700208 setActionBar((Toolbar) findViewById(R.id.toolbar));
209
210 // Hide all default options in the ActionBar.
211 getActionBar().setDisplayOptions(0);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700212
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800213 // Ensure the toolbar stays visible while the options menu is displayed.
214 getActionBar().addOnMenuVisibilityListener(new ActionBar.OnMenuVisibilityListener() {
215 @Override
216 public void onMenuVisibilityChanged(boolean isVisible) {
217 mDisplayView.setForceToolbarVisible(isVisible);
218 }
219 });
220
221 mDisplayView = (CalculatorDisplay) findViewById(R.id.display);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700222 mModeView = (TextView) findViewById(R.id.mode);
Hans Boehm08e8f322015-04-21 13:18:38 -0700223 mFormulaText = (CalculatorText) findViewById(R.id.formula);
Justin Klaassen44595162015-05-28 17:55:20 -0700224 mResultText = (CalculatorResult) findViewById(R.id.result);
Annie Chine918fd22016-03-09 11:07:54 -0800225 mFormulaContainer = (HorizontalScrollView) findViewById(R.id.formula_container);
Justin Klaassend48b7562015-04-16 16:51:38 -0700226
Justin Klaassen3b4d13d2014-06-06 18:18:37 +0100227 mPadViewPager = (ViewPager) findViewById(R.id.pad_pager);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700228 mDeleteButton = findViewById(R.id.del);
229 mClearButton = findViewById(R.id.clr);
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700230 mEqualButton = findViewById(R.id.pad_numeric).findViewById(R.id.eq);
231 if (mEqualButton == null || mEqualButton.getVisibility() != View.VISIBLE) {
232 mEqualButton = findViewById(R.id.pad_operator).findViewById(R.id.eq);
233 }
Justin Klaassene2711cb2015-05-28 11:13:17 -0700234
235 mInverseToggle = (TextView) findViewById(R.id.toggle_inv);
236 mModeToggle = (TextView) findViewById(R.id.toggle_mode);
237
Annie Chin26e159e2016-05-18 15:17:14 -0700238 mOneLine = mResultText.getVisibility() == View.INVISIBLE;
239
Justin Klaassen721ec842015-05-28 14:30:08 -0700240 mInvertibleButtons = new View[] {
241 findViewById(R.id.fun_sin),
242 findViewById(R.id.fun_cos),
Hans Boehm4db31b42015-05-31 12:19:05 -0700243 findViewById(R.id.fun_tan),
244 findViewById(R.id.fun_ln),
245 findViewById(R.id.fun_log),
246 findViewById(R.id.op_sqrt)
Justin Klaassene2711cb2015-05-28 11:13:17 -0700247 };
248 mInverseButtons = new View[] {
249 findViewById(R.id.fun_arcsin),
250 findViewById(R.id.fun_arccos),
Hans Boehm4db31b42015-05-31 12:19:05 -0700251 findViewById(R.id.fun_arctan),
252 findViewById(R.id.fun_exp),
253 findViewById(R.id.fun_10pow),
254 findViewById(R.id.op_sqr)
Justin Klaassene2711cb2015-05-28 11:13:17 -0700255 };
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700256
Justin Klaassen44595162015-05-28 17:55:20 -0700257 mEvaluator = new Evaluator(this, mResultText);
258 mResultText.setEvaluator(mEvaluator);
Hans Boehm013969e2015-04-13 20:29:47 -0700259 KeyMaps.setActivity(this);
Justin Klaassen2be4fdb2014-08-06 19:54:09 -0700260
Hans Boehm84614952014-11-25 18:46:17 -0800261 if (savedInstanceState != null) {
262 setState(CalculatorState.values()[
263 savedInstanceState.getInt(KEY_DISPLAY_STATE,
264 CalculatorState.INPUT.ordinal())]);
Hans Boehm760a9dc2015-04-20 10:27:12 -0700265 CharSequence unprocessed = savedInstanceState.getCharSequence(KEY_UNPROCESSED_CHARS);
266 if (unprocessed != null) {
267 mUnprocessedChars = unprocessed.toString();
268 }
269 byte[] state = savedInstanceState.getByteArray(KEY_EVAL_STATE);
Hans Boehm84614952014-11-25 18:46:17 -0800270 if (state != null) {
271 try (ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(state))) {
272 mEvaluator.restoreInstanceState(in);
273 } catch (Throwable ignored) {
274 // When in doubt, revert to clean state
275 mCurrentState = CalculatorState.INPUT;
276 mEvaluator.clear();
277 }
278 }
Hans Boehmfbcef702015-04-27 18:07:47 -0700279 } else {
280 mCurrentState = CalculatorState.INPUT;
281 mEvaluator.clear();
Hans Boehm84614952014-11-25 18:46:17 -0800282 }
Justin Klaassene2711cb2015-05-28 11:13:17 -0700283
Hans Boehm08e8f322015-04-21 13:18:38 -0700284 mFormulaText.setOnTextSizeChangeListener(this);
Justin Klaassenfc5ac822015-06-18 13:15:17 -0700285 mFormulaText.setOnPasteListener(this);
Annie Chine918fd22016-03-09 11:07:54 -0800286 mFormulaText.addTextChangedListener(mFormulaTextWatcher);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700287 mDeleteButton.setOnLongClickListener(this);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700288
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800289 onInverseToggled(savedInstanceState != null
290 && savedInstanceState.getBoolean(KEY_INVERSE_MODE));
Justin Klaassene2711cb2015-05-28 11:13:17 -0700291 onModeChanged(mEvaluator.getDegreeMode());
292
Hans Boehm84614952014-11-25 18:46:17 -0800293 if (mCurrentState != CalculatorState.INPUT) {
Hans Boehmfbcef702015-04-27 18:07:47 -0700294 // Just reevaluate.
295 redisplayFormula();
Hans Boehm84614952014-11-25 18:46:17 -0800296 setState(CalculatorState.INIT);
Hans Boehm84614952014-11-25 18:46:17 -0800297 mEvaluator.requireResult();
298 } else {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700299 redisplayAfterFormulaChange();
Hans Boehm84614952014-11-25 18:46:17 -0800300 }
301 // TODO: We're currently not saving and restoring scroll position.
302 // We probably should. Details may require care to deal with:
303 // - new display size
304 // - slow recomputation if we've scrolled far.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700305 }
306
307 @Override
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800308 protected void onResume() {
309 super.onResume();
310
Hans Boehm52d477a2016-04-01 17:42:50 -0700311 // Always temporarily show the toolbar initially on launch.
312 showAndMaybeHideToolbar();
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800313 }
314
315 @Override
Justin Klaassenf79d6f62014-08-26 12:27:08 -0700316 protected void onSaveInstanceState(@NonNull Bundle outState) {
Hans Boehm40125442016-01-22 10:35:35 -0800317 mEvaluator.cancelAll(true);
Justin Klaassenf79d6f62014-08-26 12:27:08 -0700318 // If there's an animation in progress, cancel it first to ensure our state is up-to-date.
319 if (mCurrentAnimator != null) {
320 mCurrentAnimator.cancel();
321 }
322
Justin Klaassen4b3af052014-05-27 17:53:10 -0700323 super.onSaveInstanceState(outState);
Hans Boehm84614952014-11-25 18:46:17 -0800324 outState.putInt(KEY_DISPLAY_STATE, mCurrentState.ordinal());
Hans Boehm760a9dc2015-04-20 10:27:12 -0700325 outState.putCharSequence(KEY_UNPROCESSED_CHARS, mUnprocessedChars);
Hans Boehm84614952014-11-25 18:46:17 -0800326 ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
327 try (ObjectOutput out = new ObjectOutputStream(byteArrayStream)) {
328 mEvaluator.saveInstanceState(out);
329 } catch (IOException e) {
330 // Impossible; No IO involved.
331 throw new AssertionError("Impossible IO exception", e);
332 }
333 outState.putByteArray(KEY_EVAL_STATE, byteArrayStream.toByteArray());
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800334 outState.putBoolean(KEY_INVERSE_MODE, mInverseToggle.isSelected());
Justin Klaassen4b3af052014-05-27 17:53:10 -0700335 }
336
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700337 // Set the state, updating delete label and display colors.
338 // This restores display positions on moving to INPUT.
Justin Klaassend48b7562015-04-16 16:51:38 -0700339 // But movement/animation for moving to RESULT has already been done.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700340 private void setState(CalculatorState state) {
341 if (mCurrentState != state) {
Hans Boehm84614952014-11-25 18:46:17 -0800342 if (state == CalculatorState.INPUT) {
343 restoreDisplayPositions();
344 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700345 mCurrentState = state;
346
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700347 if (mCurrentState == CalculatorState.RESULT) {
348 // No longer do this for ERROR; allow mistakes to be corrected.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700349 mDeleteButton.setVisibility(View.GONE);
350 mClearButton.setVisibility(View.VISIBLE);
351 } else {
352 mDeleteButton.setVisibility(View.VISIBLE);
353 mClearButton.setVisibility(View.GONE);
354 }
355
Annie Chin26e159e2016-05-18 15:17:14 -0700356 if (mOneLine) {
357 if (mCurrentState == CalculatorState.RESULT
358 || mCurrentState == CalculatorState.EVALUATE
359 || mCurrentState == CalculatorState.ANIMATE) {
360 mFormulaText.setVisibility(View.VISIBLE);
361 mResultText.setVisibility(View.VISIBLE);
362 } else {
363 mFormulaText.setVisibility(View.VISIBLE);
364 mResultText.setVisibility(View.INVISIBLE);
365 }
366 }
367
Hans Boehm84614952014-11-25 18:46:17 -0800368 if (mCurrentState == CalculatorState.ERROR) {
Justin Klaassen44595162015-05-28 17:55:20 -0700369 final int errorColor = getColor(R.color.calculator_error_color);
Hans Boehm08e8f322015-04-21 13:18:38 -0700370 mFormulaText.setTextColor(errorColor);
Justin Klaassen44595162015-05-28 17:55:20 -0700371 mResultText.setTextColor(errorColor);
Justin Klaassen8fff1442014-06-19 10:43:29 -0700372 getWindow().setStatusBarColor(errorColor);
Justin Klaassen44595162015-05-28 17:55:20 -0700373 } else if (mCurrentState != CalculatorState.RESULT) {
374 mFormulaText.setTextColor(getColor(R.color.display_formula_text_color));
375 mResultText.setTextColor(getColor(R.color.display_result_text_color));
376 getWindow().setStatusBarColor(getColor(R.color.calculator_accent_color));
Justin Klaassen4b3af052014-05-27 17:53:10 -0700377 }
Justin Klaassend48b7562015-04-16 16:51:38 -0700378
379 invalidateOptionsMenu();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700380 }
381 }
382
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700383 @Override
384 public void onActionModeStarted(ActionMode mode) {
385 super.onActionModeStarted(mode);
386 if (mode.getTag() == CalculatorText.TAG_ACTION_MODE) {
387 mFormulaContainer.scrollTo(mFormulaText.getRight(), 0);
388 }
389 }
390
Hans Boehm1176f232015-05-11 16:26:03 -0700391 // Stop any active ActionMode. Return true if there was one.
392 private boolean stopActionMode() {
Justin Klaassen44595162015-05-28 17:55:20 -0700393 if (mResultText.stopActionMode()) {
Hans Boehm1176f232015-05-11 16:26:03 -0700394 return true;
395 }
396 if (mFormulaText.stopActionMode()) {
397 return true;
398 }
399 return false;
400 }
401
Justin Klaassen4b3af052014-05-27 17:53:10 -0700402 @Override
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700403 public void onUserInteraction() {
404 super.onUserInteraction();
405
406 // If there's an animation in progress, end it immediately, so the user interaction can
407 // be handled.
408 if (mCurrentAnimator != null) {
409 mCurrentAnimator.end();
410 }
411 }
412
413 @Override
Justin Klaassen3b4d13d2014-06-06 18:18:37 +0100414 public void onBackPressed() {
Hans Boehm1176f232015-05-11 16:26:03 -0700415 if (!stopActionMode()) {
416 if (mPadViewPager != null && mPadViewPager.getCurrentItem() != 0) {
417 // Select the previous pad.
418 mPadViewPager.setCurrentItem(mPadViewPager.getCurrentItem() - 1);
419 } else {
420 // If the user is currently looking at the first pad (or the pad is not paged),
421 // allow the system to handle the Back button.
422 super.onBackPressed();
423 }
Justin Klaassen3b4d13d2014-06-06 18:18:37 +0100424 }
425 }
426
427 @Override
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700428 public boolean onKeyUp(int keyCode, KeyEvent event) {
Justin Klaassen83959da2016-04-06 11:55:24 -0700429 // Allow the system to handle special key codes (e.g. "BACK" or "DPAD").
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700430 switch (keyCode) {
Justin Klaassen83959da2016-04-06 11:55:24 -0700431 case KeyEvent.KEYCODE_BACK:
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700432 case KeyEvent.KEYCODE_DPAD_UP:
433 case KeyEvent.KEYCODE_DPAD_DOWN:
434 case KeyEvent.KEYCODE_DPAD_LEFT:
435 case KeyEvent.KEYCODE_DPAD_RIGHT:
436 return super.onKeyUp(keyCode, event);
437 }
438
Justin Klaassend12e0622016-04-27 16:26:47 -0700439 // Stop the action mode if it's showing.
440 stopActionMode();
441
Justin Klaassen12da1ad2016-04-04 14:20:37 -0700442 // Always cancel unrequested in-progress evaluation, so that we don't have to worry about
443 // subsequent asynchronous completion.
444 // Requested in-progress evaluations are handled below.
445 if (mCurrentState != CalculatorState.EVALUATE) {
446 mEvaluator.cancelAll(true);
447 }
448
449 switch (keyCode) {
450 case KeyEvent.KEYCODE_NUMPAD_ENTER:
451 case KeyEvent.KEYCODE_ENTER:
452 case KeyEvent.KEYCODE_DPAD_CENTER:
453 mCurrentButton = mEqualButton;
454 onEquals();
455 return true;
456 case KeyEvent.KEYCODE_DEL:
457 mCurrentButton = mDeleteButton;
458 onDelete();
459 return true;
460 default:
461 cancelIfEvaluating(false);
462 final int raw = event.getKeyCharacterMap().get(keyCode, event.getMetaState());
463 if ((raw & KeyCharacterMap.COMBINING_ACCENT) != 0) {
464 return true; // discard
465 }
466 // Try to discard non-printing characters and the like.
467 // The user will have to explicitly delete other junk that gets past us.
468 if (Character.isIdentifierIgnorable(raw) || Character.isWhitespace(raw)) {
469 return true;
470 }
471 char c = (char) raw;
472 if (c == '=') {
473 mCurrentButton = mEqualButton;
474 onEquals();
475 } else {
476 addChars(String.valueOf(c), true);
477 redisplayAfterFormulaChange();
478 }
479 return true;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700480 }
481 }
482
Justin Klaassene2711cb2015-05-28 11:13:17 -0700483 /**
484 * Invoked whenever the inverse button is toggled to update the UI.
485 *
486 * @param showInverse {@code true} if inverse functions should be shown
487 */
488 private void onInverseToggled(boolean showInverse) {
Justin Klaassen3e223ea2016-02-05 14:18:06 -0800489 mInverseToggle.setSelected(showInverse);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700490 if (showInverse) {
491 mInverseToggle.setContentDescription(getString(R.string.desc_inv_on));
Justin Klaassen721ec842015-05-28 14:30:08 -0700492 for (View invertibleButton : mInvertibleButtons) {
493 invertibleButton.setVisibility(View.GONE);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700494 }
495 for (View inverseButton : mInverseButtons) {
496 inverseButton.setVisibility(View.VISIBLE);
497 }
498 } else {
499 mInverseToggle.setContentDescription(getString(R.string.desc_inv_off));
Justin Klaassen721ec842015-05-28 14:30:08 -0700500 for (View invertibleButton : mInvertibleButtons) {
501 invertibleButton.setVisibility(View.VISIBLE);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700502 }
503 for (View inverseButton : mInverseButtons) {
504 inverseButton.setVisibility(View.GONE);
505 }
506 }
507 }
508
509 /**
510 * Invoked whenever the deg/rad mode may have changed to update the UI.
511 *
512 * @param degreeMode {@code true} if in degree mode
513 */
514 private void onModeChanged(boolean degreeMode) {
515 if (degreeMode) {
Justin Klaassend48b7562015-04-16 16:51:38 -0700516 mModeView.setText(R.string.mode_deg);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700517 mModeView.setContentDescription(getString(R.string.desc_mode_deg));
518
519 mModeToggle.setText(R.string.mode_rad);
520 mModeToggle.setContentDescription(getString(R.string.desc_switch_rad));
Hans Boehmbfe8c222015-04-02 16:26:07 -0700521 } else {
Justin Klaassend48b7562015-04-16 16:51:38 -0700522 mModeView.setText(R.string.mode_rad);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700523 mModeView.setContentDescription(getString(R.string.desc_mode_rad));
524
525 mModeToggle.setText(R.string.mode_deg);
526 mModeToggle.setContentDescription(getString(R.string.desc_switch_deg));
Hans Boehmbfe8c222015-04-02 16:26:07 -0700527 }
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800528
529 // Show the toolbar to highlight the mode change.
Hans Boehm52d477a2016-04-01 17:42:50 -0700530 showAndMaybeHideToolbar();
Hans Boehmbfe8c222015-04-02 16:26:07 -0700531 }
Hans Boehm84614952014-11-25 18:46:17 -0800532
Hans Boehm5d79d102015-09-16 16:33:47 -0700533 /**
534 * Switch to INPUT from RESULT state in response to input of the specified button_id.
535 * View.NO_ID is treated as an incomplete function id.
536 */
537 private void switchToInput(int button_id) {
538 if (KeyMaps.isBinary(button_id) || KeyMaps.isSuffix(button_id)) {
539 mEvaluator.collapse();
540 } else {
541 announceClearedForAccessibility();
542 mEvaluator.clear();
543 }
544 setState(CalculatorState.INPUT);
545 }
546
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700547 // Add the given button id to input expression.
548 // If appropriate, clear the expression before doing so.
549 private void addKeyToExpr(int id) {
550 if (mCurrentState == CalculatorState.ERROR) {
551 setState(CalculatorState.INPUT);
552 } else if (mCurrentState == CalculatorState.RESULT) {
Hans Boehm5d79d102015-09-16 16:33:47 -0700553 switchToInput(id);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700554 }
555 if (!mEvaluator.append(id)) {
556 // TODO: Some user visible feedback?
557 }
558 }
559
Hans Boehm017de982015-06-10 17:46:03 -0700560 /**
561 * Add the given button id to input expression, assuming it was explicitly
562 * typed/touched.
563 * We perform slightly more aggressive correction than in pasted expressions.
564 */
565 private void addExplicitKeyToExpr(int id) {
566 if (mCurrentState == CalculatorState.INPUT && id == R.id.op_sub) {
567 mEvaluator.getExpr().removeTrailingAdditiveOperators();
568 }
569 addKeyToExpr(id);
570 }
571
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700572 private void redisplayAfterFormulaChange() {
573 // TODO: Could do this more incrementally.
574 redisplayFormula();
575 setState(CalculatorState.INPUT);
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800576 if (haveUnprocessed()) {
Justin Klaassen44595162015-05-28 17:55:20 -0700577 mResultText.clear();
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800578 // Force reevaluation when text is deleted, even if expression is unchanged.
579 mEvaluator.touch();
580 } else {
581 if (mEvaluator.getExpr().hasInterestingOps()) {
582 mEvaluator.evaluateAndShowResult();
583 } else {
584 mResultText.clear();
585 }
Hans Boehmc023b732015-04-29 11:30:47 -0700586 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700587 }
588
Hans Boehm52d477a2016-04-01 17:42:50 -0700589 /**
590 * Show the toolbar.
591 * Automatically hide it again if it's not relevant to current formula.
592 */
593 private void showAndMaybeHideToolbar() {
594 final boolean shouldBeVisible =
595 mCurrentState == CalculatorState.INPUT && mEvaluator.hasTrigFuncs();
596 mDisplayView.showToolbar(!shouldBeVisible);
597 }
598
599 /**
600 * Display or hide the toolbar depending on calculator state.
601 */
602 private void showOrHideToolbar() {
603 final boolean shouldBeVisible =
604 mCurrentState == CalculatorState.INPUT && mEvaluator.hasTrigFuncs();
605 if (shouldBeVisible) {
606 mDisplayView.showToolbar(false);
607 } else {
608 mDisplayView.hideToolbar();
609 }
610 }
611
Justin Klaassen4b3af052014-05-27 17:53:10 -0700612 public void onButtonClick(View view) {
Hans Boehmc1ea0912015-06-19 15:05:07 -0700613 // Any animation is ended before we get here.
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700614 mCurrentButton = view;
Hans Boehm1176f232015-05-11 16:26:03 -0700615 stopActionMode();
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800616
Hans Boehmc1ea0912015-06-19 15:05:07 -0700617 // See onKey above for the rationale behind some of the behavior below:
618 if (mCurrentState != CalculatorState.EVALUATE) {
619 // Cancel evaluations that were not specifically requested.
620 mEvaluator.cancelAll(true);
Hans Boehm84614952014-11-25 18:46:17 -0800621 }
Justin Klaassen9d33cdc2016-02-21 14:16:14 -0800622
Justin Klaassend48b7562015-04-16 16:51:38 -0700623 final int id = view.getId();
Hans Boehm84614952014-11-25 18:46:17 -0800624 switch (id) {
Justin Klaassen4b3af052014-05-27 17:53:10 -0700625 case R.id.eq:
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700626 onEquals();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700627 break;
628 case R.id.del:
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700629 onDelete();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700630 break;
631 case R.id.clr:
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700632 onClear();
Hans Boehm52d477a2016-04-01 17:42:50 -0700633 return; // Toolbar visibility adjusted at end of animation.
Justin Klaassene2711cb2015-05-28 11:13:17 -0700634 case R.id.toggle_inv:
635 final boolean selected = !mInverseToggle.isSelected();
636 mInverseToggle.setSelected(selected);
637 onInverseToggled(selected);
Hans Boehmc1ea0912015-06-19 15:05:07 -0700638 if (mCurrentState == CalculatorState.RESULT) {
639 mResultText.redisplay(); // In case we cancelled reevaluation.
640 }
Justin Klaassene2711cb2015-05-28 11:13:17 -0700641 break;
642 case R.id.toggle_mode:
Hans Boehmc1ea0912015-06-19 15:05:07 -0700643 cancelIfEvaluating(false);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700644 final boolean mode = !mEvaluator.getDegreeMode();
Hans Boehmbfe8c222015-04-02 16:26:07 -0700645 if (mCurrentState == CalculatorState.RESULT) {
646 mEvaluator.collapse(); // Capture result evaluated in old mode
647 redisplayFormula();
648 }
649 // In input mode, we reinterpret already entered trig functions.
650 mEvaluator.setDegreeMode(mode);
Justin Klaassene2711cb2015-05-28 11:13:17 -0700651 onModeChanged(mode);
Hans Boehmbfe8c222015-04-02 16:26:07 -0700652 setState(CalculatorState.INPUT);
Justin Klaassen44595162015-05-28 17:55:20 -0700653 mResultText.clear();
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800654 if (!haveUnprocessed() && mEvaluator.getExpr().hasInterestingOps()) {
Hans Boehmc023b732015-04-29 11:30:47 -0700655 mEvaluator.evaluateAndShowResult();
656 }
Hans Boehm52d477a2016-04-01 17:42:50 -0700657 return; // onModeChanged adjusted toolbar visibility.
Justin Klaassen4b3af052014-05-27 17:53:10 -0700658 default:
Hans Boehmc1ea0912015-06-19 15:05:07 -0700659 cancelIfEvaluating(false);
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800660 if (haveUnprocessed()) {
661 // For consistency, append as uninterpreted characters.
662 // This may actually be useful for a left parenthesis.
663 addChars(KeyMaps.toString(this, id), true);
664 } else {
665 addExplicitKeyToExpr(id);
666 redisplayAfterFormulaChange();
667 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700668 break;
669 }
Hans Boehm52d477a2016-04-01 17:42:50 -0700670 showOrHideToolbar();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700671 }
672
Hans Boehm84614952014-11-25 18:46:17 -0800673 void redisplayFormula() {
Hans Boehm8a4f81c2015-07-09 10:41:25 -0700674 SpannableStringBuilder formula = mEvaluator.getExpr().toSpannableStringBuilder(this);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700675 if (mUnprocessedChars != null) {
676 // Add and highlight characters we couldn't process.
Hans Boehm8a4f81c2015-07-09 10:41:25 -0700677 formula.append(mUnprocessedChars, mUnprocessedColorSpan,
678 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700679 }
Hans Boehm8a4f81c2015-07-09 10:41:25 -0700680 mFormulaText.changeTextTo(formula);
Annie Chinf360ef02016-03-10 13:45:39 -0800681 mFormulaText.setContentDescription(TextUtils.isEmpty(formula)
682 ? getString(R.string.desc_formula) : formula.toString());
Hans Boehm84614952014-11-25 18:46:17 -0800683 }
684
Justin Klaassen4b3af052014-05-27 17:53:10 -0700685 @Override
686 public boolean onLongClick(View view) {
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700687 mCurrentButton = view;
688
Justin Klaassen4b3af052014-05-27 17:53:10 -0700689 if (view.getId() == R.id.del) {
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700690 onClear();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700691 return true;
692 }
693 return false;
694 }
695
Hans Boehm84614952014-11-25 18:46:17 -0800696 // Initial evaluation completed successfully. Initiate display.
Hans Boehma0e45f32015-05-30 13:20:35 -0700697 public void onEvaluate(int initDisplayPrec, int msd, int leastDigPos,
698 String truncatedWholeNumber) {
Justin Klaassend48b7562015-04-16 16:51:38 -0700699 // Invalidate any options that may depend on the current result.
700 invalidateOptionsMenu();
701
Hans Boehma0e45f32015-05-30 13:20:35 -0700702 mResultText.displayResult(initDisplayPrec, msd, leastDigPos, truncatedWholeNumber);
Hans Boehm61568a12015-05-18 18:25:41 -0700703 if (mCurrentState != CalculatorState.INPUT) { // in EVALUATE or INIT state
Hans Boehm84614952014-11-25 18:46:17 -0800704 onResult(mCurrentState != CalculatorState.INIT);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700705 }
Hans Boehm84614952014-11-25 18:46:17 -0800706 }
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700707
Hans Boehmc1ea0912015-06-19 15:05:07 -0700708 // Reset state to reflect evaluator cancellation. Invoked by evaluator.
Hans Boehm84614952014-11-25 18:46:17 -0800709 public void onCancelled() {
710 // We should be in EVALUATE state.
Hans Boehm84614952014-11-25 18:46:17 -0800711 setState(CalculatorState.INPUT);
Justin Klaassen44595162015-05-28 17:55:20 -0700712 mResultText.clear();
Hans Boehm84614952014-11-25 18:46:17 -0800713 }
714
715 // Reevaluation completed; ask result to redisplay current value.
716 public void onReevaluate()
717 {
Justin Klaassen44595162015-05-28 17:55:20 -0700718 mResultText.redisplay();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700719 }
720
Justin Klaassenfed941a2014-06-09 18:42:40 +0100721 @Override
722 public void onTextSizeChanged(final TextView textView, float oldSize) {
723 if (mCurrentState != CalculatorState.INPUT) {
724 // Only animate text changes that occur from user input.
725 return;
726 }
727
728 // Calculate the values needed to perform the scale and translation animations,
729 // maintaining the same apparent baseline for the displayed text.
730 final float textScale = oldSize / textView.getTextSize();
731 final float translationX = (1.0f - textScale) *
732 (textView.getWidth() / 2.0f - textView.getPaddingEnd());
733 final float translationY = (1.0f - textScale) *
734 (textView.getHeight() / 2.0f - textView.getPaddingBottom());
735
736 final AnimatorSet animatorSet = new AnimatorSet();
737 animatorSet.playTogether(
738 ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f),
739 ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f),
740 ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f),
741 ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f));
Justin Klaassen94db7202014-06-11 11:22:31 -0700742 animatorSet.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
Justin Klaassenfed941a2014-06-09 18:42:40 +0100743 animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
744 animatorSet.start();
745 }
746
Hans Boehmc1ea0912015-06-19 15:05:07 -0700747 /**
748 * Cancel any in-progress explicitly requested evaluations.
749 * @param quiet suppress pop-up message. Explicit evaluation can change the expression
750 value, and certainly changes the display, so it seems reasonable to warn.
751 * @return true if there was such an evaluation
752 */
753 private boolean cancelIfEvaluating(boolean quiet) {
754 if (mCurrentState == CalculatorState.EVALUATE) {
755 mEvaluator.cancelAll(quiet);
756 return true;
757 } else {
758 return false;
759 }
760 }
761
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800762 private boolean haveUnprocessed() {
763 return mUnprocessedChars != null && !mUnprocessedChars.isEmpty();
764 }
765
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700766 private void onEquals() {
Hans Boehm56d6e762016-06-06 11:46:29 -0700767 // Ignore if in non-INPUT state, or if there are no operators.
768 if (mCurrentState == CalculatorState.INPUT && mEvaluator.getExpr().hasInterestingOps()) {
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700769 setState(CalculatorState.EVALUATE);
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800770 if (haveUnprocessed()) {
771 onError(R.string.error_syntax);
772 } else {
773 mEvaluator.requireResult();
774 }
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700775 }
776 }
777
778 private void onDelete() {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700779 // Delete works like backspace; remove the last character or operator from the expression.
780 // Note that we handle keyboard delete exactly like the delete button. For
781 // example the delete button can be used to delete a character from an incomplete
782 // function name typed on a physical keyboard.
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700783 // This should be impossible in RESULT state.
Hans Boehmc1ea0912015-06-19 15:05:07 -0700784 // If there is an in-progress explicit evaluation, just cancel it and return.
785 if (cancelIfEvaluating(false)) return;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700786 setState(CalculatorState.INPUT);
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800787 if (haveUnprocessed()) {
788 mUnprocessedChars = mUnprocessedChars.substring(0, mUnprocessedChars.length() - 1);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700789 } else {
Hans Boehmc023b732015-04-29 11:30:47 -0700790 mEvaluator.delete();
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700791 }
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800792 if (mEvaluator.getExpr().isEmpty() && !haveUnprocessed()) {
Hans Boehmdb6f9992015-08-19 12:32:56 -0700793 // Resulting formula won't be announced, since it's empty.
794 announceClearedForAccessibility();
795 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700796 redisplayAfterFormulaChange();
Budi Kusmiantoroad8e88a2014-08-11 17:21:09 -0700797 }
798
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700799 private void reveal(View sourceView, int colorRes, AnimatorListener listener) {
Justin Klaassen06360f92014-08-28 11:08:44 -0700800 final ViewGroupOverlay groupOverlay =
801 (ViewGroupOverlay) getWindow().getDecorView().getOverlay();
Justin Klaassen8fff1442014-06-19 10:43:29 -0700802
803 final Rect displayRect = new Rect();
Justin Klaassen06360f92014-08-28 11:08:44 -0700804 mDisplayView.getGlobalVisibleRect(displayRect);
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700805
806 // Make reveal cover the display and status bar.
807 final View revealView = new View(this);
Justin Klaassen8fff1442014-06-19 10:43:29 -0700808 revealView.setBottom(displayRect.bottom);
809 revealView.setLeft(displayRect.left);
810 revealView.setRight(displayRect.right);
Hans Boehmaf04c3a2016-01-27 14:50:08 -0800811 revealView.setBackgroundColor(getColor(colorRes));
Justin Klaassen06360f92014-08-28 11:08:44 -0700812 groupOverlay.add(revealView);
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700813
Justin Klaassen4b3af052014-05-27 17:53:10 -0700814 final int[] clearLocation = new int[2];
815 sourceView.getLocationInWindow(clearLocation);
816 clearLocation[0] += sourceView.getWidth() / 2;
817 clearLocation[1] += sourceView.getHeight() / 2;
818
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700819 final int revealCenterX = clearLocation[0] - revealView.getLeft();
820 final int revealCenterY = clearLocation[1] - revealView.getTop();
Justin Klaassen4b3af052014-05-27 17:53:10 -0700821
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700822 final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2);
823 final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2);
824 final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700825 final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2));
826
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700827 final Animator revealAnimator =
828 ViewAnimationUtils.createCircularReveal(revealView,
ztenghui3d6ecaf2014-06-05 09:56:00 -0700829 revealCenterX, revealCenterY, 0.0f, revealRadius);
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700830 revealAnimator.setDuration(
Justin Klaassen4b3af052014-05-27 17:53:10 -0700831 getResources().getInteger(android.R.integer.config_longAnimTime));
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700832 revealAnimator.addListener(listener);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700833
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700834 final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700835 alphaAnimator.setDuration(
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700836 getResources().getInteger(android.R.integer.config_mediumAnimTime));
Justin Klaassen4b3af052014-05-27 17:53:10 -0700837
838 final AnimatorSet animatorSet = new AnimatorSet();
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700839 animatorSet.play(revealAnimator).before(alphaAnimator);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700840 animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
841 animatorSet.addListener(new AnimatorListenerAdapter() {
842 @Override
Justin Klaassen4b3af052014-05-27 17:53:10 -0700843 public void onAnimationEnd(Animator animator) {
Justin Klaassen8fff1442014-06-19 10:43:29 -0700844 groupOverlay.remove(revealView);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700845 mCurrentAnimator = null;
846 }
847 });
848
849 mCurrentAnimator = animatorSet;
850 animatorSet.start();
851 }
852
Hans Boehmdb6f9992015-08-19 12:32:56 -0700853 private void announceClearedForAccessibility() {
854 mResultText.announceForAccessibility(getResources().getString(R.string.cleared));
Hans Boehmccc55662015-07-07 14:16:59 -0700855 }
856
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700857 private void onClear() {
Justin Klaassen1a428cf2016-02-24 15:58:18 -0800858 if (mEvaluator.getExpr().isEmpty() && !haveUnprocessed()) {
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700859 return;
860 }
Hans Boehmc1ea0912015-06-19 15:05:07 -0700861 cancelIfEvaluating(true);
Hans Boehmdb6f9992015-08-19 12:32:56 -0700862 announceClearedForAccessibility();
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700863 reveal(mCurrentButton, R.color.calculator_accent_color, new AnimatorListenerAdapter() {
864 @Override
865 public void onAnimationEnd(Animator animation) {
Hans Boehm760a9dc2015-04-20 10:27:12 -0700866 mUnprocessedChars = null;
Justin Klaassen44595162015-05-28 17:55:20 -0700867 mResultText.clear();
Hans Boehm760a9dc2015-04-20 10:27:12 -0700868 mEvaluator.clear();
869 setState(CalculatorState.INPUT);
Hans Boehm52d477a2016-04-01 17:42:50 -0700870 showOrHideToolbar();
Hans Boehm84614952014-11-25 18:46:17 -0800871 redisplayFormula();
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700872 }
873 });
874 }
875
Hans Boehm84614952014-11-25 18:46:17 -0800876 // Evaluation encountered en error. Display the error.
877 void onError(final int errorResourceId) {
Hans Boehmfbcef702015-04-27 18:07:47 -0700878 if (mCurrentState == CalculatorState.EVALUATE) {
879 setState(CalculatorState.ANIMATE);
Hans Boehmccc55662015-07-07 14:16:59 -0700880 mResultText.announceForAccessibility(getResources().getString(errorResourceId));
Hans Boehmfbcef702015-04-27 18:07:47 -0700881 reveal(mCurrentButton, R.color.calculator_error_color,
882 new AnimatorListenerAdapter() {
883 @Override
884 public void onAnimationEnd(Animator animation) {
885 setState(CalculatorState.ERROR);
Justin Klaassen44595162015-05-28 17:55:20 -0700886 mResultText.displayError(errorResourceId);
Hans Boehmfbcef702015-04-27 18:07:47 -0700887 }
888 });
889 } else if (mCurrentState == CalculatorState.INIT) {
890 setState(CalculatorState.ERROR);
Justin Klaassen44595162015-05-28 17:55:20 -0700891 mResultText.displayError(errorResourceId);
Hans Boehmc023b732015-04-29 11:30:47 -0700892 } else {
Justin Klaassen44595162015-05-28 17:55:20 -0700893 mResultText.clear();
Justin Klaassen2be4fdb2014-08-06 19:54:09 -0700894 }
Justin Klaassen5f2a3342014-06-11 17:40:22 -0700895 }
896
Hans Boehm84614952014-11-25 18:46:17 -0800897 // Animate movement of result into the top formula slot.
898 // Result window now remains translated in the top slot while the result is displayed.
899 // (We convert it back to formula use only when the user provides new input.)
Justin Klaassen44595162015-05-28 17:55:20 -0700900 // Historical note: In the Lollipop version, this invisibly and instantaneously moved
Hans Boehm84614952014-11-25 18:46:17 -0800901 // formula and result displays back at the end of the animation. We no longer do that,
902 // so that we can continue to properly support scrolling of the result.
903 // We assume the result already contains the text to be expanded.
904 private void onResult(boolean animate) {
Justin Klaassen44595162015-05-28 17:55:20 -0700905 // Calculate the textSize that would be used to display the result in the formula.
906 // For scrollable results just use the minimum textSize to maximize the number of digits
907 // that are visible on screen.
908 float textSize = mFormulaText.getMinimumTextSize();
909 if (!mResultText.isScrollable()) {
910 textSize = mFormulaText.getVariableTextSize(mResultText.getText().toString());
911 }
912
913 // Scale the result to match the calculated textSize, minimizing the jump-cut transition
914 // when a result is reused in a subsequent expression.
915 final float resultScale = textSize / mResultText.getTextSize();
916
917 // Set the result's pivot to match its gravity.
918 mResultText.setPivotX(mResultText.getWidth() - mResultText.getPaddingRight());
919 mResultText.setPivotY(mResultText.getHeight() - mResultText.getPaddingBottom());
920
921 // Calculate the necessary translations so the result takes the place of the formula and
922 // the formula moves off the top of the screen.
Annie Chin28589dc2016-06-09 17:50:51 -0700923 final float resultTranslationY = (mFormulaContainer.getBottom() - mResultText.getBottom())
924 - (mFormulaText.getPaddingBottom() - mResultText.getPaddingBottom());
925 float formulaTranslationY = -mFormulaContainer.getBottom();
Annie Chin26e159e2016-05-18 15:17:14 -0700926 if (mOneLine) {
927 // Position the result text.
928 mResultText.setY(mResultText.getBottom());
Annie Chin28589dc2016-06-09 17:50:51 -0700929 formulaTranslationY = -(findViewById(R.id.toolbar).getBottom()
930 + mFormulaContainer.getBottom());
Annie Chin26e159e2016-05-18 15:17:14 -0700931 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700932
Justin Klaassen44595162015-05-28 17:55:20 -0700933 // Change the result's textColor to match the formula.
934 final int formulaTextColor = mFormulaText.getCurrentTextColor();
935
Hans Boehm84614952014-11-25 18:46:17 -0800936 if (animate) {
Hans Boehmccc55662015-07-07 14:16:59 -0700937 mResultText.announceForAccessibility(getResources().getString(R.string.desc_eq));
938 mResultText.announceForAccessibility(mResultText.getText());
Hans Boehmc1ea0912015-06-19 15:05:07 -0700939 setState(CalculatorState.ANIMATE);
Hans Boehm84614952014-11-25 18:46:17 -0800940 final AnimatorSet animatorSet = new AnimatorSet();
941 animatorSet.playTogether(
Justin Klaassen44595162015-05-28 17:55:20 -0700942 ObjectAnimator.ofPropertyValuesHolder(mResultText,
943 PropertyValuesHolder.ofFloat(View.SCALE_X, resultScale),
944 PropertyValuesHolder.ofFloat(View.SCALE_Y, resultScale),
945 PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, resultTranslationY)),
946 ObjectAnimator.ofArgb(mResultText, TEXT_COLOR, formulaTextColor),
Annie Chine918fd22016-03-09 11:07:54 -0800947 ObjectAnimator.ofFloat(mFormulaContainer, View.TRANSLATION_Y,
948 formulaTranslationY));
Justin Klaassen44595162015-05-28 17:55:20 -0700949 animatorSet.setDuration(getResources().getInteger(
950 android.R.integer.config_longAnimTime));
Hans Boehm84614952014-11-25 18:46:17 -0800951 animatorSet.addListener(new AnimatorListenerAdapter() {
952 @Override
Hans Boehm84614952014-11-25 18:46:17 -0800953 public void onAnimationEnd(Animator animation) {
954 setState(CalculatorState.RESULT);
955 mCurrentAnimator = null;
956 }
957 });
Justin Klaassen4b3af052014-05-27 17:53:10 -0700958
Hans Boehm84614952014-11-25 18:46:17 -0800959 mCurrentAnimator = animatorSet;
960 animatorSet.start();
961 } else /* No animation desired; get there fast, e.g. when restarting */ {
Justin Klaassen44595162015-05-28 17:55:20 -0700962 mResultText.setScaleX(resultScale);
963 mResultText.setScaleY(resultScale);
964 mResultText.setTranslationY(resultTranslationY);
965 mResultText.setTextColor(formulaTextColor);
Annie Chine918fd22016-03-09 11:07:54 -0800966 mFormulaContainer.setTranslationY(formulaTranslationY);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -0700967 setState(CalculatorState.RESULT);
Hans Boehm84614952014-11-25 18:46:17 -0800968 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700969 }
Hans Boehm84614952014-11-25 18:46:17 -0800970
971 // Restore positions of the formula and result displays back to their original,
972 // pre-animation state.
973 private void restoreDisplayPositions() {
974 // Clear result.
Justin Klaassen44595162015-05-28 17:55:20 -0700975 mResultText.setText("");
Hans Boehm84614952014-11-25 18:46:17 -0800976 // Reset all of the values modified during the animation.
Justin Klaassen44595162015-05-28 17:55:20 -0700977 mResultText.setScaleX(1.0f);
978 mResultText.setScaleY(1.0f);
979 mResultText.setTranslationX(0.0f);
980 mResultText.setTranslationY(0.0f);
Annie Chine918fd22016-03-09 11:07:54 -0800981 mFormulaContainer.setTranslationY(0.0f);
Hans Boehm84614952014-11-25 18:46:17 -0800982
Hans Boehm08e8f322015-04-21 13:18:38 -0700983 mFormulaText.requestFocus();
Hans Boehm5e6a0ca2015-09-22 17:09:01 -0700984 }
985
986 @Override
987 public void onClick(AlertDialogFragment fragment, int which) {
988 if (which == DialogInterface.BUTTON_POSITIVE) {
989 // Timeout extension request.
990 mEvaluator.setLongTimeOut();
991 }
992 }
Hans Boehm84614952014-11-25 18:46:17 -0800993
Justin Klaassend48b7562015-04-16 16:51:38 -0700994 @Override
995 public boolean onCreateOptionsMenu(Menu menu) {
Justin Klaassend36d63e2015-05-05 12:59:36 -0700996 super.onCreateOptionsMenu(menu);
997
998 getMenuInflater().inflate(R.menu.activity_calculator, menu);
Justin Klaassend48b7562015-04-16 16:51:38 -0700999 return true;
1000 }
1001
1002 @Override
1003 public boolean onPrepareOptionsMenu(Menu menu) {
Justin Klaassend36d63e2015-05-05 12:59:36 -07001004 super.onPrepareOptionsMenu(menu);
1005
1006 // Show the leading option when displaying a result.
1007 menu.findItem(R.id.menu_leading).setVisible(mCurrentState == CalculatorState.RESULT);
1008
1009 // Show the fraction option when displaying a rational result.
1010 menu.findItem(R.id.menu_fraction).setVisible(mCurrentState == CalculatorState.RESULT
Hans Boehm995e5eb2016-02-08 11:03:01 -08001011 && mEvaluator.getResult().exactlyDisplayable());
Justin Klaassend36d63e2015-05-05 12:59:36 -07001012
Justin Klaassend48b7562015-04-16 16:51:38 -07001013 return true;
Hans Boehm84614952014-11-25 18:46:17 -08001014 }
1015
1016 @Override
Justin Klaassend48b7562015-04-16 16:51:38 -07001017 public boolean onOptionsItemSelected(MenuItem item) {
Hans Boehm84614952014-11-25 18:46:17 -08001018 switch (item.getItemId()) {
Justin Klaassend36d63e2015-05-05 12:59:36 -07001019 case R.id.menu_leading:
1020 displayFull();
Hans Boehm84614952014-11-25 18:46:17 -08001021 return true;
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001022 case R.id.menu_fraction:
1023 displayFraction();
1024 return true;
Justin Klaassend36d63e2015-05-05 12:59:36 -07001025 case R.id.menu_licenses:
1026 startActivity(new Intent(this, Licenses.class));
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001027 return true;
Hans Boehm84614952014-11-25 18:46:17 -08001028 default:
1029 return super.onOptionsItemSelected(item);
1030 }
1031 }
1032
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001033 private void displayMessage(String s) {
Hans Boehm5e6a0ca2015-09-22 17:09:01 -07001034 AlertDialogFragment.showMessageDialog(this, s, null);
Hans Boehm84614952014-11-25 18:46:17 -08001035 }
1036
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001037 private void displayFraction() {
Hans Boehm995e5eb2016-02-08 11:03:01 -08001038 UnifiedReal result = mEvaluator.getResult();
Hans Boehm013969e2015-04-13 20:29:47 -07001039 displayMessage(KeyMaps.translateResult(result.toNiceString()));
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001040 }
1041
1042 // Display full result to currently evaluated precision
1043 private void displayFull() {
1044 Resources res = getResources();
Justin Klaassen44595162015-05-28 17:55:20 -07001045 String msg = mResultText.getFullText() + " ";
1046 if (mResultText.fullTextIsExact()) {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001047 msg += res.getString(R.string.exact);
1048 } else {
1049 msg += res.getString(R.string.approximate);
1050 }
1051 displayMessage(msg);
1052 }
1053
Hans Boehm017de982015-06-10 17:46:03 -07001054 /**
1055 * Add input characters to the end of the expression.
1056 * Map them to the appropriate button pushes when possible. Leftover characters
1057 * are added to mUnprocessedChars, which is presumed to immediately precede the newly
1058 * added characters.
Hans Boehm65a99a42016-02-03 18:16:07 -08001059 * @param moreChars characters to be added
1060 * @param explicit these characters were explicitly typed by the user, not pasted
Hans Boehm017de982015-06-10 17:46:03 -07001061 */
1062 private void addChars(String moreChars, boolean explicit) {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001063 if (mUnprocessedChars != null) {
1064 moreChars = mUnprocessedChars + moreChars;
1065 }
1066 int current = 0;
1067 int len = moreChars.length();
Hans Boehm0b9806f2015-06-29 16:07:15 -07001068 boolean lastWasDigit = false;
Hans Boehm5d79d102015-09-16 16:33:47 -07001069 if (mCurrentState == CalculatorState.RESULT && len != 0) {
1070 // Clear display immediately for incomplete function name.
1071 switchToInput(KeyMaps.keyForChar(moreChars.charAt(current)));
1072 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001073 while (current < len) {
1074 char c = moreChars.charAt(current);
Hans Boehm013969e2015-04-13 20:29:47 -07001075 int k = KeyMaps.keyForChar(c);
Hans Boehm0b9806f2015-06-29 16:07:15 -07001076 if (!explicit) {
1077 int expEnd;
1078 if (lastWasDigit && current !=
1079 (expEnd = Evaluator.exponentEnd(moreChars, current))) {
1080 // Process scientific notation with 'E' when pasting, in spite of ambiguity
1081 // with base of natural log.
1082 // Otherwise the 10^x key is the user's friend.
1083 mEvaluator.addExponent(moreChars, current, expEnd);
1084 current = expEnd;
1085 lastWasDigit = false;
1086 continue;
1087 } else {
1088 boolean isDigit = KeyMaps.digVal(k) != KeyMaps.NOT_DIGIT;
1089 if (current == 0 && (isDigit || k == R.id.dec_point)
1090 && mEvaluator.getExpr().hasTrailingConstant()) {
1091 // Refuse to concatenate pasted content to trailing constant.
1092 // This makes pasting of calculator results more consistent, whether or
1093 // not the old calculator instance is still around.
1094 addKeyToExpr(R.id.op_mul);
1095 }
1096 lastWasDigit = (isDigit || lastWasDigit && k == R.id.dec_point);
1097 }
1098 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001099 if (k != View.NO_ID) {
1100 mCurrentButton = findViewById(k);
Hans Boehm017de982015-06-10 17:46:03 -07001101 if (explicit) {
1102 addExplicitKeyToExpr(k);
1103 } else {
1104 addKeyToExpr(k);
1105 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001106 if (Character.isSurrogate(c)) {
1107 current += 2;
1108 } else {
1109 ++current;
1110 }
1111 continue;
1112 }
Hans Boehm013969e2015-04-13 20:29:47 -07001113 int f = KeyMaps.funForString(moreChars, current);
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001114 if (f != View.NO_ID) {
1115 mCurrentButton = findViewById(f);
Hans Boehm017de982015-06-10 17:46:03 -07001116 if (explicit) {
1117 addExplicitKeyToExpr(f);
1118 } else {
1119 addKeyToExpr(f);
1120 }
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001121 if (f == R.id.op_sqrt) {
1122 // Square root entered as function; don't lose the parenthesis.
1123 addKeyToExpr(R.id.lparen);
1124 }
1125 current = moreChars.indexOf('(', current) + 1;
1126 continue;
1127 }
1128 // There are characters left, but we can't convert them to button presses.
1129 mUnprocessedChars = moreChars.substring(current);
1130 redisplayAfterFormulaChange();
Hans Boehm52d477a2016-04-01 17:42:50 -07001131 showOrHideToolbar();
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001132 return;
1133 }
1134 mUnprocessedChars = null;
1135 redisplayAfterFormulaChange();
Hans Boehm52d477a2016-04-01 17:42:50 -07001136 showOrHideToolbar();
Hans Boehm84614952014-11-25 18:46:17 -08001137 }
1138
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001139 @Override
Justin Klaassenfc5ac822015-06-18 13:15:17 -07001140 public boolean onPaste(ClipData clip) {
1141 final ClipData.Item item = clip.getItemCount() == 0 ? null : clip.getItemAt(0);
1142 if (item == null) {
1143 // nothing to paste, bail early...
1144 return false;
1145 }
1146
1147 // Check if the item is a previously copied result, otherwise paste as raw text.
1148 final Uri uri = item.getUri();
1149 if (uri != null && mEvaluator.isLastSaved(uri)) {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001150 if (mCurrentState == CalculatorState.ERROR
Justin Klaassenfc5ac822015-06-18 13:15:17 -07001151 || mCurrentState == CalculatorState.RESULT) {
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001152 setState(CalculatorState.INPUT);
1153 mEvaluator.clear();
Hans Boehm84614952014-11-25 18:46:17 -08001154 }
Hans Boehm3666e632015-07-27 18:33:12 -07001155 mEvaluator.appendSaved();
Hans Boehm4a6b7cb2015-04-03 18:41:52 -07001156 redisplayAfterFormulaChange();
Justin Klaassenfc5ac822015-06-18 13:15:17 -07001157 } else {
Hans Boehm017de982015-06-10 17:46:03 -07001158 addChars(item.coerceToText(this).toString(), false);
Hans Boehm84614952014-11-25 18:46:17 -08001159 }
Justin Klaassenfc5ac822015-06-18 13:15:17 -07001160 return true;
Hans Boehm84614952014-11-25 18:46:17 -08001161 }
Justin Klaassen4b3af052014-05-27 17:53:10 -07001162}