blob: 9b40ecca4a0ec79a7332a3a595b48f0634b5bd4d [file] [log] [blame]
Annie Chind0f87d22016-10-24 09:04:12 -07001/*
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
17package com.android.calculator2;
18
19import android.support.v7.widget.RecyclerView;
20import android.view.View;
21import android.widget.TextView;
22
23/**
24 * Contains the logic for animating the recyclerview elements on drag.
25 */
26public final class DragController {
27
Annie Chinbc001882016-11-09 19:41:21 -080028 private static final String TAG = "DragController";
29
Annie Chind0f87d22016-10-24 09:04:12 -070030 // 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 Chinbc001882016-11-09 19:41:21 -080038 private float mResultScale;
Annie Chind0f87d22016-10-24 09:04:12 -070039
40 private int mResultTranslationY;
41 private int mResultTranslationX;
42
Annie Chinbc001882016-11-09 19:41:21 -080043 private int mDisplayHeight;
44
Annie Chind0f87d22016-10-24 09:04:12 -070045 private boolean mAnimationInitialized;
46
Annie Chinbc001882016-11-09 19:41:21 -080047 private AnimationController mAnimationController;
48
49 private Evaluator mEvaluator;
50
51 public void setEvaluator(Evaluator evaluator) {
52 mEvaluator = evaluator;
Annie Chinbc001882016-11-09 19:41:21 -080053 }
54
Annie Chin94c1bd92016-11-23 13:39:56 -080055 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 Chinbc001882016-11-09 19:41:21 -080067 }
68
Annie Chind0f87d22016-10-24 09:04:12 -070069 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 Chinab657d42016-11-03 16:43:59 -070083 recyclerView.findViewHolderForAdapterPosition(0);
Annie Chinefa259a2016-11-23 14:15:15 -080084 if (yFraction > 0) {
85 recyclerView.setVisibility(View.VISIBLE);
86 }
Annie Chin91796232016-11-16 17:27:36 -080087 if (vh != null && !EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
Annie Chind0f87d22016-10-24 09:04:12 -070088 final CalculatorFormula formula = vh.getFormula();
89 final CalculatorResult result = vh.getResult();
90 final TextView date = vh.getDate();
91
92 if (!mAnimationInitialized) {
Annie Chinbc001882016-11-09 19:41:21 -080093 mAnimationController.initializeScales(formula, result);
Annie Chind0f87d22016-10-24 09:04:12 -070094
Annie Chinbc001882016-11-09 19:41:21 -080095 mAnimationController.initializeFormulaTranslationX(formula);
Annie Chind0f87d22016-10-24 09:04:12 -070096
Annie Chinbc001882016-11-09 19:41:21 -080097 mAnimationController.initializeFormulaTranslationY(formula, result);
Annie Chind0f87d22016-10-24 09:04:12 -070098
Annie Chinbc001882016-11-09 19:41:21 -080099 mAnimationController.initializeResultTranslationX(result);
Annie Chind0f87d22016-10-24 09:04:12 -0700100
Annie Chinbc001882016-11-09 19:41:21 -0800101 mAnimationController.initializeResultTranslationY(result);
Annie Chind0f87d22016-10-24 09:04:12 -0700102
103 mAnimationInitialized = true;
104 }
105
106 if (mAnimationInitialized) {
Annie Chinbc001882016-11-09 19:41:21 -0800107 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 Chind0f87d22016-10-24 09:04:12 -0700113 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 Chinbc001882016-11-09 19:41:21 -0800119 formula.setTranslationX(mAnimationController.getFormulaTranslationX(yFraction));
120 formula.setTranslationY(mAnimationController.getFormulaTranslationY(yFraction));
Annie Chind0f87d22016-10-24 09:04:12 -0700121
Annie Chinbc001882016-11-09 19:41:21 -0800122 result.setTranslationX(mAnimationController.getResultTranslationX(yFraction));
123 result.setTranslationY(mAnimationController.getResultTranslationY(yFraction));
Annie Chind0f87d22016-10-24 09:04:12 -0700124
Annie Chinbc001882016-11-09 19:41:21 -0800125 date.setTranslationY(mAnimationController.getDateTranslationY(yFraction));
126 }
Annie Chin91796232016-11-16 17:27:36 -0800127 } else if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
Annie Chinbc001882016-11-09 19:41:21 -0800128 // 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 Chind0f87d22016-10-24 09:04:12 -0700132
Annie Chinbc001882016-11-09 19:41:21 -0800133 mAnimationInitialized = true;
134 }
135 }
Annie Chind0f87d22016-10-24 09:04:12 -0700136
Annie Chinbc001882016-11-09 19:41:21 -0800137 // 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 Chind0f87d22016-10-24 09:04:12 -0700149 }
150 }
151 }
152 }
Annie Chinbc001882016-11-09 19:41:21 -0800153
154 /**
Annie Chinefa259a2016-11-23 14:15:15 -0800155 * Reset all initialized values and set recyclerview to INVISIBLE to avoid flickering.
Annie Chinbc001882016-11-09 19:41:21 -0800156 */
Annie Chin94c1bd92016-11-23 13:39:56 -0800157 public void initializeAnimation(RecyclerView recyclerView, boolean isResult) {
Annie Chinefa259a2016-11-23 14:15:15 -0800158 recyclerView.setVisibility(View.INVISIBLE);
Annie Chinbc001882016-11-09 19:41:21 -0800159 mAnimationInitialized = false;
Annie Chin94c1bd92016-11-23 13:39:56 -0800160 initializeController(isResult);
Annie Chinbc001882016-11-09 19:41:21 -0800161 }
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 Chind0f87d22016-10-24 09:04:12 -0700430}