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 | |
| 17 | package com.android.calculator2; |
| 18 | |
| 19 | import android.animation.Animator; |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 20 | import android.animation.Animator.AnimatorListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorListenerAdapter; |
| 22 | import android.animation.AnimatorSet; |
| 23 | import android.animation.ArgbEvaluator; |
| 24 | import android.animation.ObjectAnimator; |
| 25 | import android.animation.ValueAnimator; |
| 26 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
| 27 | import android.app.Activity; |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 28 | import android.graphics.Rect; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 30 | import android.support.v4.view.ViewPager; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 31 | import android.text.Editable; |
| 32 | import android.text.TextUtils; |
| 33 | import android.text.TextWatcher; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 34 | import android.view.KeyEvent; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 35 | import android.view.View; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 36 | import android.view.View.OnKeyListener; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 37 | import android.view.View.OnLongClickListener; |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 38 | import android.view.ViewAnimationUtils; |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 39 | import android.view.ViewGroupOverlay; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 40 | import android.view.animation.AccelerateDecelerateInterpolator; |
| 41 | import android.widget.Button; |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 42 | import android.widget.TextView; |
| 43 | |
| 44 | import com.android.calculator2.CalculatorEditText.OnTextSizeChangeListener; |
| 45 | import com.android.calculator2.CalculatorExpressionEvaluator.EvaluateCallback; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 46 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 47 | import java.lang.Override; |
| 48 | |
Justin Klaassen | 04f79c7 | 2014-06-27 17:25:35 -0700 | [diff] [blame] | 49 | public class Calculator extends Activity |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 50 | implements OnTextSizeChangeListener, EvaluateCallback, OnLongClickListener { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 51 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 52 | private static final String NAME = Calculator.class.getName(); |
| 53 | |
| 54 | // instance state keys |
| 55 | private static final String KEY_CURRENT_STATE = NAME + "_currentState"; |
| 56 | private static final String KEY_CURRENT_EXPRESSION = NAME + "_currentExpression"; |
| 57 | |
| 58 | /** |
| 59 | * Constant for an invalid resource id. |
| 60 | */ |
| 61 | public static final int INVALID_RES_ID = -1; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 62 | |
| 63 | private enum CalculatorState { |
| 64 | INPUT, EVALUATE, RESULT, ERROR |
| 65 | } |
| 66 | |
| 67 | private final TextWatcher mFormulaTextWatcher = new TextWatcher() { |
| 68 | @Override |
| 69 | public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onTextChanged(CharSequence charSequence, int start, int count, int after) { |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void afterTextChanged(Editable editable) { |
| 78 | setState(CalculatorState.INPUT); |
Justin Klaassen | 04f79c7 | 2014-06-27 17:25:35 -0700 | [diff] [blame] | 79 | mEvaluator.evaluate(editable, Calculator.this); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 83 | private final OnKeyListener mFormulaOnKeyListener = new OnKeyListener() { |
| 84 | @Override |
| 85 | public boolean onKey(View view, int keyCode, KeyEvent keyEvent) { |
| 86 | switch (keyCode) { |
| 87 | case KeyEvent.KEYCODE_NUMPAD_ENTER: |
| 88 | case KeyEvent.KEYCODE_ENTER: |
| 89 | if (keyEvent.getAction() == KeyEvent.ACTION_UP) { |
| 90 | mCurrentButton = mEqualButton; |
| 91 | onEquals(); |
| 92 | } |
| 93 | // ignore all other actions |
| 94 | return true; |
| 95 | case KeyEvent.KEYCODE_DEL: |
| 96 | onDelete(); |
| 97 | return true; |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | }; |
| 102 | |
Justin Klaassen | 741471e | 2014-06-11 09:43:44 -0700 | [diff] [blame] | 103 | private final Editable.Factory mFormulaEditableFactory = new Editable.Factory() { |
| 104 | @Override |
| 105 | public Editable newEditable(CharSequence source) { |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 106 | final boolean isEdited = mCurrentState == CalculatorState.INPUT |
| 107 | || mCurrentState == CalculatorState.ERROR; |
| 108 | return new CalculatorExpressionBuilder(source, mTokenizer, isEdited); |
Justin Klaassen | 741471e | 2014-06-11 09:43:44 -0700 | [diff] [blame] | 109 | } |
| 110 | }; |
| 111 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 112 | private CalculatorState mCurrentState; |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 113 | private CalculatorExpressionTokenizer mTokenizer; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 114 | private CalculatorExpressionEvaluator mEvaluator; |
| 115 | |
| 116 | private CalculatorEditText mFormulaEditText; |
| 117 | private CalculatorEditText mResultEditText; |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 118 | private ViewPager mPadViewPager; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 119 | private View mDeleteButton; |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 120 | private View mEqualButton; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 121 | private View mClearButton; |
| 122 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 123 | private View mCurrentButton; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 124 | private Animator mCurrentAnimator; |
| 125 | |
| 126 | @Override |
| 127 | protected void onCreate(Bundle savedInstanceState) { |
| 128 | super.onCreate(savedInstanceState); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 129 | setContentView(R.layout.activity_calculator); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 130 | |
| 131 | mFormulaEditText = (CalculatorEditText) findViewById(R.id.formula); |
| 132 | mResultEditText = (CalculatorEditText) findViewById(R.id.result); |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 133 | mPadViewPager = (ViewPager) findViewById(R.id.pad_pager); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 134 | mDeleteButton = findViewById(R.id.del); |
| 135 | mClearButton = findViewById(R.id.clr); |
| 136 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 137 | mEqualButton = findViewById(R.id.pad_numeric).findViewById(R.id.eq); |
| 138 | if (mEqualButton == null || mEqualButton.getVisibility() != View.VISIBLE) { |
| 139 | mEqualButton = findViewById(R.id.pad_operator).findViewById(R.id.eq); |
| 140 | } |
| 141 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 142 | mTokenizer = new CalculatorExpressionTokenizer(this); |
| 143 | mEvaluator = new CalculatorExpressionEvaluator(mTokenizer); |
| 144 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 145 | savedInstanceState = savedInstanceState == null ? Bundle.EMPTY : savedInstanceState; |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 146 | setState(CalculatorState.values()[ |
| 147 | savedInstanceState.getInt(KEY_CURRENT_STATE, CalculatorState.INPUT.ordinal())]); |
| 148 | mFormulaEditText.setText(mTokenizer.getLocalizedExpression( |
| 149 | savedInstanceState.getString(KEY_CURRENT_EXPRESSION, ""))); |
| 150 | mEvaluator.evaluate(mFormulaEditText.getText(), this); |
Justin Klaassen | 741471e | 2014-06-11 09:43:44 -0700 | [diff] [blame] | 151 | |
| 152 | mFormulaEditText.setEditableFactory(mFormulaEditableFactory); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 153 | mFormulaEditText.addTextChangedListener(mFormulaTextWatcher); |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 154 | mFormulaEditText.setOnKeyListener(mFormulaOnKeyListener); |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 155 | mFormulaEditText.setOnTextSizeChangeListener(this); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 156 | mDeleteButton.setOnLongClickListener(this); |
| 157 | } |
| 158 | |
| 159 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 160 | protected void onSaveInstanceState(Bundle outState) { |
| 161 | super.onSaveInstanceState(outState); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 162 | outState.putInt(KEY_CURRENT_STATE, mCurrentState.ordinal()); |
| 163 | outState.putString(KEY_CURRENT_EXPRESSION, |
| 164 | mTokenizer.getNormalizedExpression(mFormulaEditText.getText().toString())); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | private void setState(CalculatorState state) { |
| 168 | if (mCurrentState != state) { |
| 169 | mCurrentState = state; |
| 170 | |
| 171 | if (state == CalculatorState.RESULT || state == CalculatorState.ERROR) { |
| 172 | mDeleteButton.setVisibility(View.GONE); |
| 173 | mClearButton.setVisibility(View.VISIBLE); |
| 174 | } else { |
| 175 | mDeleteButton.setVisibility(View.VISIBLE); |
| 176 | mClearButton.setVisibility(View.GONE); |
| 177 | } |
| 178 | |
| 179 | if (state == CalculatorState.ERROR) { |
| 180 | final int errorColor = getResources().getColor(R.color.calculator_error_color); |
| 181 | mFormulaEditText.setTextColor(errorColor); |
| 182 | mResultEditText.setTextColor(errorColor); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 183 | getWindow().setStatusBarColor(errorColor); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 184 | } else { |
| 185 | mFormulaEditText.setTextColor( |
| 186 | getResources().getColor(R.color.display_formula_text_color)); |
| 187 | mResultEditText.setTextColor( |
| 188 | getResources().getColor(R.color.display_result_text_color)); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 189 | getWindow().setStatusBarColor( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 190 | getResources().getColor(R.color.calculator_accent_color)); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
Justin Klaassen | 3b4d13d | 2014-06-06 18:18:37 +0100 | [diff] [blame] | 196 | public void onBackPressed() { |
| 197 | if (mPadViewPager == null || mPadViewPager.getCurrentItem() == 0) { |
| 198 | // If the user is currently looking at the first pad (or the pad is not paged), |
| 199 | // allow the system to handle the Back button. |
| 200 | super.onBackPressed(); |
| 201 | } else { |
| 202 | // Otherwise, select the previous pad. |
| 203 | mPadViewPager.setCurrentItem(mPadViewPager.getCurrentItem() - 1); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 208 | public void onUserInteraction() { |
| 209 | super.onUserInteraction(); |
| 210 | |
| 211 | // If there's an animation in progress, cancel it so the user interaction can be handled |
| 212 | // immediately. |
| 213 | if (mCurrentAnimator != null) { |
| 214 | mCurrentAnimator.cancel(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | public void onButtonClick(View view) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 219 | mCurrentButton = view; |
| 220 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 221 | switch (view.getId()) { |
| 222 | case R.id.eq: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 223 | onEquals(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 224 | break; |
| 225 | case R.id.del: |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 226 | onDelete(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 227 | break; |
| 228 | case R.id.clr: |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 229 | onClear(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 230 | break; |
| 231 | case R.id.fun_cos: |
| 232 | case R.id.fun_ln: |
| 233 | case R.id.fun_log: |
| 234 | case R.id.fun_sin: |
| 235 | case R.id.fun_tan: |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 236 | // Add left parenthesis after functions. |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 237 | mFormulaEditText.append(((Button) view).getText() + "("); |
| 238 | break; |
| 239 | default: |
| 240 | mFormulaEditText.append(((Button) view).getText()); |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public boolean onLongClick(View view) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 247 | mCurrentButton = view; |
| 248 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 249 | if (view.getId() == R.id.del) { |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 250 | onClear(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | @Override |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 257 | public void onEvaluate(String expr, String result, int errorResourceId) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 258 | if (mCurrentState == CalculatorState.INPUT) { |
| 259 | mResultEditText.setText(result); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 260 | } else if (errorResourceId != INVALID_RES_ID) { |
| 261 | onError(errorResourceId); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 262 | } else if (!TextUtils.isEmpty(result)) { |
| 263 | onResult(result); |
| 264 | } else if (mCurrentState == CalculatorState.EVALUATE) { |
| 265 | // The current expression cannot be evaluated -> return to the input state. |
| 266 | setState(CalculatorState.INPUT); |
| 267 | } |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 268 | |
| 269 | mFormulaEditText.requestFocus(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 272 | @Override |
| 273 | public void onTextSizeChanged(final TextView textView, float oldSize) { |
| 274 | if (mCurrentState != CalculatorState.INPUT) { |
| 275 | // Only animate text changes that occur from user input. |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | // Calculate the values needed to perform the scale and translation animations, |
| 280 | // maintaining the same apparent baseline for the displayed text. |
| 281 | final float textScale = oldSize / textView.getTextSize(); |
| 282 | final float translationX = (1.0f - textScale) * |
| 283 | (textView.getWidth() / 2.0f - textView.getPaddingEnd()); |
| 284 | final float translationY = (1.0f - textScale) * |
| 285 | (textView.getHeight() / 2.0f - textView.getPaddingBottom()); |
| 286 | |
| 287 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 288 | animatorSet.playTogether( |
| 289 | ObjectAnimator.ofFloat(textView, View.SCALE_X, textScale, 1.0f), |
| 290 | ObjectAnimator.ofFloat(textView, View.SCALE_Y, textScale, 1.0f), |
| 291 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, translationX, 0.0f), |
| 292 | ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, 0.0f)); |
Justin Klaassen | 94db720 | 2014-06-11 11:22:31 -0700 | [diff] [blame] | 293 | animatorSet.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | fed941a | 2014-06-09 18:42:40 +0100 | [diff] [blame] | 294 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 295 | animatorSet.start(); |
| 296 | } |
| 297 | |
Budi Kusmiantoro | ad8e88a | 2014-08-11 17:21:09 -0700 | [diff] [blame^] | 298 | private void onEquals() { |
| 299 | if (mCurrentState == CalculatorState.INPUT) { |
| 300 | setState(CalculatorState.EVALUATE); |
| 301 | mEvaluator.evaluate(mFormulaEditText.getText(), this); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private void onDelete() { |
| 306 | // Delete works like backspace; remove the last character from the expression. |
| 307 | final Editable formulaText = mFormulaEditText.getEditableText(); |
| 308 | final int formulaLength = formulaText.length(); |
| 309 | if (formulaLength > 0) { |
| 310 | formulaText.delete(formulaLength - 1, formulaLength); |
| 311 | } |
| 312 | } |
| 313 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 314 | private void reveal(View sourceView, int colorRes, AnimatorListener listener) { |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 315 | final View displayView = findViewById(R.id.display); |
| 316 | final View decorView = getWindow().getDecorView(); |
| 317 | |
| 318 | final Rect displayRect = new Rect(); |
| 319 | displayView.getGlobalVisibleRect(displayRect); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 320 | |
| 321 | // Make reveal cover the display and status bar. |
| 322 | final View revealView = new View(this); |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 323 | revealView.setBottom(displayRect.bottom); |
| 324 | revealView.setLeft(displayRect.left); |
| 325 | revealView.setRight(displayRect.right); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 326 | revealView.setBackgroundColor(getResources().getColor(colorRes)); |
| 327 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 328 | final int[] clearLocation = new int[2]; |
| 329 | sourceView.getLocationInWindow(clearLocation); |
| 330 | clearLocation[0] += sourceView.getWidth() / 2; |
| 331 | clearLocation[1] += sourceView.getHeight() / 2; |
| 332 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 333 | final int revealCenterX = clearLocation[0] - revealView.getLeft(); |
| 334 | final int revealCenterY = clearLocation[1] - revealView.getTop(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 335 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 336 | final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2); |
| 337 | final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2); |
| 338 | final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 339 | final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2)); |
| 340 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 341 | final Animator revealAnimator = |
| 342 | ViewAnimationUtils.createCircularReveal(revealView, |
ztenghui | 3d6ecaf | 2014-06-05 09:56:00 -0700 | [diff] [blame] | 343 | revealCenterX, revealCenterY, 0.0f, revealRadius); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 344 | revealAnimator.setDuration( |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 345 | getResources().getInteger(android.R.integer.config_longAnimTime)); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 346 | revealAnimator.addListener(listener); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 347 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 348 | final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 349 | alphaAnimator.setDuration( |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 350 | getResources().getInteger(android.R.integer.config_mediumAnimTime)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 351 | |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 352 | final ViewGroupOverlay groupOverlay = (ViewGroupOverlay) decorView.getOverlay(); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 353 | final AnimatorSet animatorSet = new AnimatorSet(); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 354 | animatorSet.play(revealAnimator).before(alphaAnimator); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 355 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 356 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 357 | @Override |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 358 | public void onAnimationStart(Animator animation) { |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 359 | groupOverlay.add(revealView); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public void onAnimationEnd(Animator animator) { |
Justin Klaassen | 8fff144 | 2014-06-19 10:43:29 -0700 | [diff] [blame] | 364 | groupOverlay.remove(revealView); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 365 | mCurrentAnimator = null; |
| 366 | } |
| 367 | }); |
| 368 | |
| 369 | mCurrentAnimator = animatorSet; |
| 370 | animatorSet.start(); |
| 371 | } |
| 372 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 373 | private void onClear() { |
| 374 | if (TextUtils.isEmpty(mFormulaEditText.getText())) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | reveal(mCurrentButton, R.color.calculator_accent_color, new AnimatorListenerAdapter() { |
| 379 | @Override |
| 380 | public void onAnimationEnd(Animator animation) { |
| 381 | mFormulaEditText.getEditableText().clear(); |
| 382 | } |
| 383 | }); |
| 384 | } |
| 385 | |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 386 | private void onError(final int errorResourceId) { |
| 387 | if (mCurrentState != CalculatorState.EVALUATE) { |
| 388 | // Only animate error on evaluate. |
| 389 | mResultEditText.setText(errorResourceId); |
| 390 | return; |
| 391 | } |
| 392 | |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 393 | reveal(mCurrentButton, R.color.calculator_error_color, new AnimatorListenerAdapter() { |
| 394 | @Override |
| 395 | public void onAnimationEnd(Animator animation) { |
| 396 | setState(CalculatorState.ERROR); |
Justin Klaassen | 2be4fdb | 2014-08-06 19:54:09 -0700 | [diff] [blame] | 397 | mResultEditText.setText(errorResourceId); |
Justin Klaassen | 5f2a334 | 2014-06-11 17:40:22 -0700 | [diff] [blame] | 398 | } |
| 399 | }); |
| 400 | } |
| 401 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 402 | private void onResult(final String result) { |
| 403 | // Calculate the values needed to perform the scale and translation animations, |
| 404 | // accounting for how the scale will affect the final position of the text. |
| 405 | final float resultScale = |
| 406 | mFormulaEditText.getVariableTextSize(result) / mResultEditText.getTextSize(); |
| 407 | final float resultTranslationX = (1.0f - resultScale) * |
| 408 | (mResultEditText.getWidth() / 2.0f - mResultEditText.getPaddingEnd()); |
| 409 | final float resultTranslationY = (1.0f - resultScale) * |
| 410 | (mResultEditText.getHeight() / 2.0f - mResultEditText.getPaddingBottom()) + |
| 411 | (mFormulaEditText.getBottom() - mResultEditText.getBottom()) + |
| 412 | (mResultEditText.getPaddingBottom() - mFormulaEditText.getPaddingBottom()); |
| 413 | final float formulaTranslationY = -mFormulaEditText.getBottom(); |
| 414 | |
| 415 | // Use a value animator to fade to the final text color over the course of the animation. |
| 416 | final int resultTextColor = mResultEditText.getCurrentTextColor(); |
| 417 | final int formulaTextColor = mFormulaEditText.getCurrentTextColor(); |
| 418 | final ValueAnimator textColorAnimator = |
| 419 | ValueAnimator.ofObject(new ArgbEvaluator(), resultTextColor, formulaTextColor); |
| 420 | textColorAnimator.addUpdateListener(new AnimatorUpdateListener() { |
| 421 | @Override |
| 422 | public void onAnimationUpdate(ValueAnimator valueAnimator) { |
| 423 | mResultEditText.setTextColor((int) valueAnimator.getAnimatedValue()); |
| 424 | } |
| 425 | }); |
| 426 | |
| 427 | final AnimatorSet animatorSet = new AnimatorSet(); |
| 428 | animatorSet.playTogether( |
| 429 | textColorAnimator, |
| 430 | ObjectAnimator.ofFloat(mResultEditText, View.SCALE_X, resultScale), |
| 431 | ObjectAnimator.ofFloat(mResultEditText, View.SCALE_Y, resultScale), |
| 432 | ObjectAnimator.ofFloat(mResultEditText, View.TRANSLATION_X, resultTranslationX), |
| 433 | ObjectAnimator.ofFloat(mResultEditText, View.TRANSLATION_Y, resultTranslationY), |
| 434 | ObjectAnimator.ofFloat(mFormulaEditText, View.TRANSLATION_Y, formulaTranslationY)); |
| 435 | animatorSet.setDuration(getResources().getInteger(android.R.integer.config_longAnimTime)); |
| 436 | animatorSet.setInterpolator(new AccelerateDecelerateInterpolator()); |
| 437 | animatorSet.addListener(new AnimatorListenerAdapter() { |
| 438 | @Override |
| 439 | public void onAnimationStart(Animator animation) { |
| 440 | mResultEditText.setText(result); |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public void onAnimationEnd(Animator animation) { |
| 445 | // Reset all of the values modified during the animation. |
| 446 | mResultEditText.setTextColor(resultTextColor); |
| 447 | mResultEditText.setScaleX(1.0f); |
| 448 | mResultEditText.setScaleY(1.0f); |
| 449 | mResultEditText.setTranslationX(0.0f); |
| 450 | mResultEditText.setTranslationY(0.0f); |
| 451 | mFormulaEditText.setTranslationY(0.0f); |
| 452 | |
| 453 | // Finally update the formula to use the current result. |
| 454 | mFormulaEditText.setText(result); |
| 455 | setState(CalculatorState.RESULT); |
| 456 | |
| 457 | mCurrentAnimator = null; |
| 458 | } |
| 459 | }); |
| 460 | |
| 461 | mCurrentAnimator = animatorSet; |
| 462 | animatorSet.start(); |
| 463 | } |
| 464 | } |