Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.support.v7.widget.RecyclerView; |
| 20 | import android.view.View; |
| 21 | import android.widget.TextView; |
| 22 | |
| 23 | /** |
| 24 | * Contains the logic for animating the recyclerview elements on drag. |
| 25 | */ |
| 26 | public final class DragController { |
| 27 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 28 | private static final String TAG = "DragController"; |
| 29 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 30 | // References to views from the Calculator Display. |
| 31 | private CalculatorFormula mDisplayFormula; |
| 32 | private CalculatorResult mDisplayResult; |
| 33 | private View mToolbar; |
| 34 | |
| 35 | private int mFormulaTranslationY; |
| 36 | private int mFormulaTranslationX; |
| 37 | private float mFormulaScale; |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 38 | private float mResultScale; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 39 | |
| 40 | private int mResultTranslationY; |
| 41 | private int mResultTranslationX; |
| 42 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 43 | private int mDisplayHeight; |
| 44 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 45 | private boolean mAnimationInitialized; |
| 46 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 47 | private AnimationController mAnimationController; |
| 48 | |
| 49 | private Evaluator mEvaluator; |
| 50 | |
| 51 | public void setEvaluator(Evaluator evaluator) { |
| 52 | mEvaluator = evaluator; |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame^] | 55 | public void initializeController(boolean isResult) { |
| 56 | if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) { |
| 57 | // Empty display |
| 58 | mAnimationController = new EmptyAnimationController(); |
| 59 | } else if (isResult) { |
| 60 | // Result |
| 61 | mAnimationController = new ResultAnimationController(); |
| 62 | } else { |
| 63 | // There is something in the formula field. There may or may not be |
| 64 | // a quick result. |
| 65 | mAnimationController = new AnimationController(); |
| 66 | } |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 69 | public void setDisplayFormula(CalculatorFormula formula) { |
| 70 | mDisplayFormula = formula; |
| 71 | } |
| 72 | |
| 73 | public void setDisplayResult(CalculatorResult result) { |
| 74 | mDisplayResult = result; |
| 75 | } |
| 76 | |
| 77 | public void setToolbar(View toolbar) { |
| 78 | mToolbar = toolbar; |
| 79 | } |
| 80 | |
| 81 | public void animateViews(float yFraction, RecyclerView recyclerView, int itemCount) { |
| 82 | final HistoryAdapter.ViewHolder vh = (HistoryAdapter.ViewHolder) |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 83 | recyclerView.findViewHolderForAdapterPosition(0); |
Annie Chin | efa259a | 2016-11-23 14:15:15 -0800 | [diff] [blame] | 84 | if (yFraction > 0) { |
| 85 | recyclerView.setVisibility(View.VISIBLE); |
| 86 | } |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 87 | if (vh != null && !EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 88 | final CalculatorFormula formula = vh.getFormula(); |
| 89 | final CalculatorResult result = vh.getResult(); |
| 90 | final TextView date = vh.getDate(); |
| 91 | |
| 92 | if (!mAnimationInitialized) { |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 93 | mAnimationController.initializeScales(formula, result); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 94 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 95 | mAnimationController.initializeFormulaTranslationX(formula); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 96 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 97 | mAnimationController.initializeFormulaTranslationY(formula, result); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 98 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 99 | mAnimationController.initializeResultTranslationX(result); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 100 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 101 | mAnimationController.initializeResultTranslationY(result); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 102 | |
| 103 | mAnimationInitialized = true; |
| 104 | } |
| 105 | |
| 106 | if (mAnimationInitialized) { |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 107 | result.setScaleX(mAnimationController.getResultScale(yFraction)); |
| 108 | result.setScaleY(mAnimationController.getResultScale(yFraction)); |
| 109 | |
| 110 | formula.setScaleX(mAnimationController.getFormulaScale(yFraction)); |
| 111 | formula.setScaleY(mAnimationController.getFormulaScale(yFraction)); |
| 112 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 113 | formula.setPivotX(formula.getWidth() - formula.getPaddingEnd()); |
| 114 | formula.setPivotY(formula.getHeight() - formula.getPaddingBottom()); |
| 115 | |
| 116 | result.setPivotX(result.getWidth() - result.getPaddingEnd()); |
| 117 | result.setPivotY(result.getHeight() - result.getPaddingBottom()); |
| 118 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 119 | formula.setTranslationX(mAnimationController.getFormulaTranslationX(yFraction)); |
| 120 | formula.setTranslationY(mAnimationController.getFormulaTranslationY(yFraction)); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 121 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 122 | result.setTranslationX(mAnimationController.getResultTranslationX(yFraction)); |
| 123 | result.setTranslationY(mAnimationController.getResultTranslationY(yFraction)); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 124 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 125 | date.setTranslationY(mAnimationController.getDateTranslationY(yFraction)); |
| 126 | } |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 127 | } else if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) { |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 128 | // There is no current expression but we still need to collect information |
| 129 | // to translate the other viewholders. |
| 130 | if (!mAnimationInitialized) { |
| 131 | mAnimationController.initializeDisplayHeight(); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 132 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 133 | mAnimationInitialized = true; |
| 134 | } |
| 135 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 136 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 137 | // Move up all ViewHolders above the current expression; if there is no current expression, |
| 138 | // we're translating all the viewholders. |
| 139 | for (int i = recyclerView.getChildCount() - 1; |
| 140 | i >= mAnimationController.getFirstTranslatedViewHolderIndex(); |
| 141 | --i) { |
| 142 | final RecyclerView.ViewHolder vh2 = |
| 143 | recyclerView.getChildViewHolder(recyclerView.getChildAt(i)); |
| 144 | if (vh2 != null) { |
| 145 | final View view = vh2.itemView; |
| 146 | if (view != null) { |
| 147 | view.setTranslationY( |
| 148 | mAnimationController.getHistoryElementTranslationY(yFraction)); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
Annie Chin | efa259a | 2016-11-23 14:15:15 -0800 | [diff] [blame] | 155 | * Reset all initialized values and set recyclerview to INVISIBLE to avoid flickering. |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 156 | */ |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame^] | 157 | public void initializeAnimation(RecyclerView recyclerView, boolean isResult) { |
Annie Chin | efa259a | 2016-11-23 14:15:15 -0800 | [diff] [blame] | 158 | recyclerView.setVisibility(View.INVISIBLE); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 159 | mAnimationInitialized = false; |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame^] | 160 | initializeController(isResult); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | public interface AnimateTextInterface { |
| 164 | |
| 165 | void initializeDisplayHeight(); |
| 166 | |
| 167 | void initializeScales(CalculatorFormula formula, CalculatorResult result); |
| 168 | |
| 169 | void initializeFormulaTranslationX(CalculatorFormula formula); |
| 170 | |
| 171 | void initializeFormulaTranslationY(CalculatorFormula formula, CalculatorResult result); |
| 172 | |
| 173 | void initializeResultTranslationX(CalculatorResult result); |
| 174 | |
| 175 | void initializeResultTranslationY(CalculatorResult result); |
| 176 | |
| 177 | float getResultTranslationX(float yFraction); |
| 178 | |
| 179 | float getResultTranslationY(float yFraction); |
| 180 | |
| 181 | float getResultScale(float yFraction); |
| 182 | |
| 183 | float getFormulaScale(float yFraction); |
| 184 | |
| 185 | float getFormulaTranslationX(float yFraction); |
| 186 | |
| 187 | float getFormulaTranslationY(float yFraction); |
| 188 | |
| 189 | float getDateTranslationY(float yFraction); |
| 190 | |
| 191 | float getHistoryElementTranslationY(float yFraction); |
| 192 | |
| 193 | // Return the lowest index of the first Viewholder to be translated upwards. |
| 194 | // If there is no current expression, we translate all the viewholders; otherwise, |
| 195 | // we start at index 1. |
| 196 | int getFirstTranslatedViewHolderIndex(); |
| 197 | } |
| 198 | |
| 199 | // The default AnimationController when Display is in INPUT state and DisplayFormula is not |
| 200 | // empty. There may or may not be a quick result. |
| 201 | public class AnimationController implements DragController.AnimateTextInterface { |
| 202 | |
| 203 | public void initializeDisplayHeight() { |
| 204 | // no-op |
| 205 | } |
| 206 | |
| 207 | public void initializeScales(CalculatorFormula formula, CalculatorResult result) { |
| 208 | // Calculate the scale for the text |
| 209 | mFormulaScale = (mDisplayFormula.getTextSize() * 1.0f) / formula.getTextSize(); |
| 210 | } |
| 211 | |
| 212 | public void initializeFormulaTranslationY(CalculatorFormula formula, |
| 213 | CalculatorResult result) { |
| 214 | // Baseline of formula moves by the difference in formula bottom padding and the |
| 215 | // difference in result height. |
| 216 | mFormulaTranslationY = |
| 217 | mDisplayFormula.getPaddingBottom() - formula.getPaddingBottom() |
| 218 | + mDisplayResult.getHeight() - result.getHeight(); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | public void initializeFormulaTranslationX(CalculatorFormula formula) { |
| 223 | // Right border of formula moves by the difference in formula end padding. |
| 224 | mFormulaTranslationX = mDisplayFormula.getPaddingEnd() - formula.getPaddingEnd(); |
| 225 | } |
| 226 | |
| 227 | public void initializeResultTranslationY(CalculatorResult result) { |
| 228 | // Baseline of result moves by the difference in result bottom padding. |
| 229 | mResultTranslationY = mDisplayResult.getPaddingBottom() - result.getPaddingBottom(); |
| 230 | } |
| 231 | |
| 232 | public void initializeResultTranslationX(CalculatorResult result) { |
| 233 | mResultTranslationX = mDisplayResult.getPaddingEnd() - result.getPaddingEnd(); |
| 234 | } |
| 235 | |
| 236 | public float getResultTranslationX(float yFraction) { |
| 237 | return (mResultTranslationX * yFraction) - mResultTranslationX; |
| 238 | } |
| 239 | |
| 240 | public float getResultTranslationY(float yFraction) { |
| 241 | return (mResultTranslationY * yFraction) - mResultTranslationY; |
| 242 | } |
| 243 | |
| 244 | public float getResultScale(float yFraction) { |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | public float getFormulaScale(float yFraction) { |
| 249 | return mFormulaScale - (mFormulaScale * yFraction) + yFraction; |
| 250 | } |
| 251 | |
| 252 | public float getFormulaTranslationX(float yFraction) { |
| 253 | return (mFormulaTranslationX * yFraction) - |
| 254 | mFormulaTranslationX; |
| 255 | } |
| 256 | |
| 257 | public float getFormulaTranslationY(float yFraction) { |
| 258 | // Scale linearly between -FormulaTranslationY and 0. |
| 259 | return (mFormulaTranslationY * yFraction) - mFormulaTranslationY; |
| 260 | } |
| 261 | |
| 262 | public float getDateTranslationY(float yFraction) { |
| 263 | // We also want the date to start out above the visible screen with |
| 264 | // this distance decreasing as it's pulled down. |
| 265 | return -mToolbar.getHeight() * (1 - yFraction) |
| 266 | + getResultTranslationY(yFraction) |
| 267 | - mDisplayFormula.getPaddingTop() + |
| 268 | (mDisplayFormula.getPaddingTop() * yFraction); |
| 269 | } |
| 270 | |
| 271 | public float getHistoryElementTranslationY(float yFraction) { |
| 272 | return getDateTranslationY(yFraction); |
| 273 | } |
| 274 | |
| 275 | public int getFirstTranslatedViewHolderIndex() { |
| 276 | return 1; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // The default AnimationController when Display is in RESULT state. |
| 281 | public class ResultAnimationController extends AnimationController |
| 282 | implements DragController.AnimateTextInterface { |
| 283 | @Override |
| 284 | public void initializeScales(CalculatorFormula formula, CalculatorResult result) { |
| 285 | final float textSize = mDisplayResult.getTextSize() * mDisplayResult.getScaleX(); |
| 286 | mResultScale = textSize / result.getTextSize(); |
| 287 | |
| 288 | mFormulaScale = 1; |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void initializeFormulaTranslationY(CalculatorFormula formula, |
| 293 | CalculatorResult result) { |
| 294 | // Baseline of formula moves by the difference in formula bottom padding and the |
| 295 | // difference in the result height. |
| 296 | mFormulaTranslationY = mDisplayFormula.getPaddingBottom() - formula.getPaddingBottom() |
| 297 | + mDisplayResult.getHeight() - result.getHeight(); |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public void initializeFormulaTranslationX(CalculatorFormula formula) { |
| 302 | // Right border of formula moves by the difference in formula end padding. |
| 303 | mFormulaTranslationX = mDisplayFormula.getPaddingEnd() - formula.getPaddingEnd(); |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public void initializeResultTranslationY(CalculatorResult result) { |
| 308 | // Baseline of result moves by the difference in result bottom padding. |
| 309 | mResultTranslationY = mDisplayResult.getBottom() - result.getBottom() + |
| 310 | mDisplayResult.getPaddingBottom() - result.getPaddingBottom(); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void initializeResultTranslationX(CalculatorResult result) { |
| 315 | mResultTranslationX = mDisplayResult.getPaddingEnd() - result.getPaddingEnd(); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public float getResultTranslationX(float yFraction) { |
| 320 | return (mResultTranslationX * yFraction) - mResultTranslationX; |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public float getResultTranslationY(float yFraction) { |
| 325 | return (mResultTranslationY * yFraction) - mResultTranslationY; |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public float getFormulaTranslationX(float yFraction) { |
| 330 | return (mFormulaTranslationX * yFraction) - |
| 331 | mFormulaTranslationX; |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public float getFormulaTranslationY(float yFraction) { |
| 336 | return getDateTranslationY(yFraction); |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public float getResultScale(float yFraction) { |
| 341 | return mResultScale - (mResultScale * yFraction) + yFraction; |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public float getFormulaScale(float yFraction) { |
| 346 | return 1; |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public float getDateTranslationY(float yFraction) { |
| 351 | // We also want the date to start out above the visible screen with |
| 352 | // this distance decreasing as it's pulled down. |
| 353 | return -mToolbar.getHeight() * (1 - yFraction) |
| 354 | + (mResultTranslationY * yFraction) - mResultTranslationY |
| 355 | - mDisplayFormula.getPaddingTop() + |
| 356 | (mDisplayFormula.getPaddingTop() * yFraction); |
| 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public int getFirstTranslatedViewHolderIndex() { |
| 361 | return 1; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // The default AnimationController when Display is completely empty. |
| 366 | public class EmptyAnimationController extends AnimationController |
| 367 | implements DragController.AnimateTextInterface { |
| 368 | @Override |
| 369 | public void initializeDisplayHeight() { |
| 370 | mDisplayHeight = mToolbar.getHeight() + mDisplayResult.getHeight() |
| 371 | + mDisplayFormula.getHeight(); |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public void initializeScales(CalculatorFormula formula, CalculatorResult result) { |
| 376 | // no-op |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public void initializeFormulaTranslationY(CalculatorFormula formula, |
| 381 | CalculatorResult result) { |
| 382 | // no-op |
| 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public void initializeFormulaTranslationX(CalculatorFormula formula) { |
| 387 | // no-op |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public void initializeResultTranslationY(CalculatorResult result) { |
| 392 | // no-op |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public void initializeResultTranslationX(CalculatorResult result) { |
| 397 | // no-op |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public float getResultTranslationX(float yFraction) { |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public float getResultTranslationY(float yFraction) { |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public float getFormulaScale(float yFraction) { |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public float getDateTranslationY(float yFraction) { |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public float getHistoryElementTranslationY(float yFraction) { |
| 422 | return -mDisplayHeight * (1 - yFraction); |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public int getFirstTranslatedViewHolderIndex() { |
| 427 | return 0; |
| 428 | } |
| 429 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 430 | } |