summaryrefslogtreecommitdiff
path: root/src/com/android/calculator2/DragController.java
blob: 1716cc909eb3a5f73c6f3173d8940050690adac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.calculator2;

import android.animation.ArgbEvaluator;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

/**
 * Contains the logic for animating the recyclerview elements on drag.
 */
public final class DragController {

    private static final String TAG = "DragController";

    private static final ArgbEvaluator mColorEvaluator = new ArgbEvaluator();

    // References to views from the Calculator Display.
    private CalculatorFormula mDisplayFormula;
    private CalculatorResult mDisplayResult;
    private View mToolbar;

    private int mFormulaTranslationY;
    private int mFormulaTranslationX;
    private float mFormulaScale;
    private float mResultScale;

    private float mResultTranslationY;
    private int mResultTranslationX;

    private int mDisplayHeight;

    private int mFormulaStartColor;
    private int mFormulaEndColor;

    private int mResultStartColor;
    private int mResultEndColor;

    // The padding at the bottom of the RecyclerView itself.
    private int mBottomPaddingHeight;

    private boolean mAnimationInitialized;

    private boolean mOneLine;
    private boolean mIsDisplayEmpty;

    private AnimationController mAnimationController;

    private Evaluator mEvaluator;

    public void setEvaluator(Evaluator evaluator) {
        mEvaluator = evaluator;
    }

    public void initializeController(boolean isResult, boolean oneLine, boolean isDisplayEmpty) {
        mOneLine = oneLine;
        mIsDisplayEmpty = isDisplayEmpty;
        if (mIsDisplayEmpty) {
            // Empty display
            mAnimationController = new EmptyAnimationController();
        } else if (isResult) {
            // Result
            mAnimationController = new ResultAnimationController();
        } else {
            // There is something in the formula field. There may or may not be
            // a quick result.
            mAnimationController = new AnimationController();
        }
    }

    public void setDisplayFormula(CalculatorFormula formula) {
        mDisplayFormula = formula;
    }

    public void setDisplayResult(CalculatorResult result) {
        mDisplayResult = result;
    }

    public void setToolbar(View toolbar) {
        mToolbar = toolbar;
    }

    public void animateViews(float yFraction, RecyclerView recyclerView) {
        if (mDisplayFormula == null
                || mDisplayResult == null
                || mToolbar == null
                || mEvaluator == null) {
            // Bail if we aren't yet initialized.
            return;
        }

        final HistoryAdapter.ViewHolder vh =
                (HistoryAdapter.ViewHolder) recyclerView.findViewHolderForAdapterPosition(0);
        if (yFraction > 0 && vh != null) {
            recyclerView.setVisibility(View.VISIBLE);
        }
        if (vh != null && !mIsDisplayEmpty
                && vh.getItemViewType() == HistoryAdapter.HISTORY_VIEW_TYPE) {
            final AlignedTextView formula = vh.getFormula();
            final CalculatorResult result = vh.getResult();
            final TextView date = vh.getDate();
            final View divider = vh.getDivider();

            if (!mAnimationInitialized) {
                mBottomPaddingHeight = recyclerView.getPaddingBottom();

                mAnimationController.initializeScales(formula, result);

                mAnimationController.initializeColorAnimators(formula, result);

                mAnimationController.initializeFormulaTranslationX(formula);

                mAnimationController.initializeFormulaTranslationY(formula, result);

                mAnimationController.initializeResultTranslationX(result);

                mAnimationController.initializeResultTranslationY(result);

                mAnimationInitialized = true;
            }

            result.setScaleX(mAnimationController.getResultScale(yFraction));
            result.setScaleY(mAnimationController.getResultScale(yFraction));

            formula.setScaleX(mAnimationController.getFormulaScale(yFraction));
            formula.setScaleY(mAnimationController.getFormulaScale(yFraction));

            formula.setPivotX(formula.getWidth() - formula.getPaddingEnd());
            formula.setPivotY(formula.getHeight() - formula.getPaddingBottom());

            result.setPivotX(result.getWidth() - result.getPaddingEnd());
            result.setPivotY(result.getHeight() - result.getPaddingBottom());

            formula.setTranslationX(mAnimationController.getFormulaTranslationX(yFraction));
            formula.setTranslationY(mAnimationController.getFormulaTranslationY(yFraction));

            result.setTranslationX(mAnimationController.getResultTranslationX(yFraction));
            result.setTranslationY(mAnimationController.getResultTranslationY(yFraction));

            formula.setTextColor((int) mColorEvaluator.evaluate(yFraction, mFormulaStartColor,
                    mFormulaEndColor));

            result.setTextColor((int) mColorEvaluator.evaluate(yFraction, mResultStartColor,
                    mResultEndColor));

            date.setTranslationY(mAnimationController.getDateTranslationY(yFraction));
            divider.setTranslationY(mAnimationController.getDateTranslationY(yFraction));
        } else if (mIsDisplayEmpty) {
            // There is no current expression but we still need to collect information
            // to translate the other viewholders.
            if (!mAnimationInitialized) {
                mAnimationController.initializeDisplayHeight();
                mAnimationInitialized = true;
            }
        }

        // Move up all ViewHolders above the current expression; if there is no current expression,
        // we're translating all the viewholders.
        for (int i = recyclerView.getChildCount() - 1;
             i >= mAnimationController.getFirstTranslatedViewHolderIndex();
             --i) {
            final RecyclerView.ViewHolder vh2 =
                    recyclerView.getChildViewHolder(recyclerView.getChildAt(i));
            if (vh2 != null) {
                final View view = vh2.itemView;
                if (view != null) {
                    view.setTranslationY(
                        mAnimationController.getHistoryElementTranslationY(yFraction));
                }
            }
        }
    }

    /**
     * Reset all initialized values.
     */
    public void initializeAnimation(boolean isResult, boolean oneLine, boolean isDisplayEmpty) {
        mAnimationInitialized = false;
        initializeController(isResult, oneLine, isDisplayEmpty);
    }

    public interface AnimateTextInterface {

        void initializeDisplayHeight();

        void initializeColorAnimators(AlignedTextView formula, CalculatorResult result);

        void initializeScales(AlignedTextView formula, CalculatorResult result);

        void initializeFormulaTranslationX(AlignedTextView formula);

        void initializeFormulaTranslationY(AlignedTextView formula, CalculatorResult result);

        void initializeResultTranslationX(CalculatorResult result);

        void initializeResultTranslationY(CalculatorResult result);

        float getResultTranslationX(float yFraction);

        float getResultTranslationY(float yFraction);

        float getResultScale(float yFraction);

        float getFormulaScale(float yFraction);

        float getFormulaTranslationX(float yFraction);

        float getFormulaTranslationY(float yFraction);

        float getDateTranslationY(float yFraction);

        float getHistoryElementTranslationY(float yFraction);

        // Return the lowest index of the first Viewholder to be translated upwards.
        // If there is no current expression, we translate all the viewholders; otherwise,
        // we start at index 1.
        int getFirstTranslatedViewHolderIndex();
    }

    // The default AnimationController when Display is in INPUT state and DisplayFormula is not
    // empty. There may or may not be a quick result.
    public class AnimationController implements DragController.AnimateTextInterface {

        public void initializeDisplayHeight() {
            // no-op
        }

        public void initializeColorAnimators(AlignedTextView formula, CalculatorResult result) {
            mFormulaStartColor = mDisplayFormula.getCurrentTextColor();
            mFormulaEndColor = formula.getCurrentTextColor();

            mResultStartColor = mDisplayResult.getCurrentTextColor();
            mResultEndColor = result.getCurrentTextColor();
        }

        public void initializeScales(AlignedTextView formula, CalculatorResult result) {
            // Calculate the scale for the text
            mFormulaScale = mDisplayFormula.getTextSize() / formula.getTextSize();
        }

        public void initializeFormulaTranslationY(AlignedTextView formula,
                CalculatorResult result) {
            if (mOneLine) {
                // Disregard result since we set it to GONE in the one-line case.
                mFormulaTranslationY =
                        mDisplayFormula.getPaddingBottom() - formula.getPaddingBottom()
                        - mBottomPaddingHeight;
            } else {
                // Baseline of formula moves by the difference in formula bottom padding and the
                // difference in result height.
                mFormulaTranslationY =
                        mDisplayFormula.getPaddingBottom() - formula.getPaddingBottom()
                                + mDisplayResult.getHeight() - result.getHeight()
                                - mBottomPaddingHeight;
            }
        }

        public void initializeFormulaTranslationX(AlignedTextView formula) {
            // Right border of formula moves by the difference in formula end padding.
            mFormulaTranslationX = mDisplayFormula.getPaddingEnd() - formula.getPaddingEnd();
        }

        public void initializeResultTranslationY(CalculatorResult result) {
            // Baseline of result moves by the difference in result bottom padding.
            mResultTranslationY = mDisplayResult.getPaddingBottom() - result.getPaddingBottom()
            - mBottomPaddingHeight;
        }

        public void initializeResultTranslationX(CalculatorResult result) {
            mResultTranslationX = mDisplayResult.getPaddingEnd() - result.getPaddingEnd();
        }

        public float getResultTranslationX(float yFraction) {
            return mResultTranslationX * (yFraction - 1f);
        }

        public float getResultTranslationY(float yFraction) {
            return mResultTranslationY * (yFraction - 1f);
        }

        public float getResultScale(float yFraction) {
            return 1f;
        }

        public float getFormulaScale(float yFraction) {
            return mFormulaScale + (1f - mFormulaScale) * yFraction;
        }

        public float getFormulaTranslationX(float yFraction) {
            return mFormulaTranslationX * (yFraction - 1f);
        }

        public float getFormulaTranslationY(float yFraction) {
            // Scale linearly between -FormulaTranslationY and 0.
            return mFormulaTranslationY * (yFraction - 1f);
        }

        public float getDateTranslationY(float yFraction) {
            // We also want the date to start out above the visible screen with
            // this distance decreasing as it's pulled down.
            // Account for the scaled formula height.
            return -mToolbar.getHeight() * (1f - yFraction)
                    + getFormulaTranslationY(yFraction)
                    - mDisplayFormula.getHeight() /getFormulaScale(yFraction) * (1f - yFraction);
        }

        public float getHistoryElementTranslationY(float yFraction) {
            return getDateTranslationY(yFraction);
        }

        public int getFirstTranslatedViewHolderIndex() {
            return 1;
        }
    }

    // The default AnimationController when Display is in RESULT state.
    public class ResultAnimationController extends AnimationController
            implements DragController.AnimateTextInterface {
        @Override
        public void initializeScales(AlignedTextView formula, CalculatorResult result) {
            final float textSize = mDisplayResult.getTextSize() * mDisplayResult.getScaleX();
            mResultScale = textSize / result.getTextSize();
            mFormulaScale = 1f;
        }

        @Override
        public void initializeFormulaTranslationY(AlignedTextView formula,
                CalculatorResult result) {
            // Baseline of formula moves by the difference in formula bottom padding and the
            // difference in the result height.
            mFormulaTranslationY = mDisplayFormula.getPaddingBottom() - formula.getPaddingBottom()
                            + mDisplayResult.getHeight() - result.getHeight()
                            - mBottomPaddingHeight;
        }

        @Override
        public void initializeFormulaTranslationX(AlignedTextView formula) {
            // Right border of formula moves by the difference in formula end padding.
            mFormulaTranslationX = mDisplayFormula.getPaddingEnd() - formula.getPaddingEnd();
        }

        @Override
        public void initializeResultTranslationY(CalculatorResult result) {
            // Baseline of result moves by the difference in result bottom padding.
            mResultTranslationY =  mDisplayResult.getPaddingBottom() - result.getPaddingBottom()
                    - mDisplayResult.getTranslationY()
                    - mBottomPaddingHeight;
        }

        @Override
        public void initializeResultTranslationX(CalculatorResult result) {
            mResultTranslationX = mDisplayResult.getPaddingEnd() - result.getPaddingEnd();
        }

        @Override
        public float getResultTranslationX(float yFraction) {
            return (mResultTranslationX * yFraction) - mResultTranslationX;
        }

        @Override
        public float getResultTranslationY(float yFraction) {
            return (mResultTranslationY * yFraction) - mResultTranslationY;
        }

        @Override
        public float getFormulaTranslationX(float yFraction) {
            return (mFormulaTranslationX * yFraction) -
                    mFormulaTranslationX;
        }

        @Override
        public float getFormulaTranslationY(float yFraction) {
            return getDateTranslationY(yFraction);
        }

        @Override
        public float getResultScale(float yFraction) {
            return mResultScale - (mResultScale * yFraction) + yFraction;
        }

        @Override
        public float getFormulaScale(float yFraction) {
            return 1f;
        }

        @Override
        public float getDateTranslationY(float yFraction) {
            // We also want the date to start out above the visible screen with
            // this distance decreasing as it's pulled down.
            return -mToolbar.getHeight() * (1f - yFraction)
                    + (mResultTranslationY * yFraction) - mResultTranslationY
                    - mDisplayFormula.getPaddingTop() +
                    (mDisplayFormula.getPaddingTop() * yFraction);
        }

        @Override
        public int getFirstTranslatedViewHolderIndex() {
            return 1;
        }
    }

    // The default AnimationController when Display is completely empty.
    public class EmptyAnimationController extends AnimationController
            implements DragController.AnimateTextInterface {
        @Override
        public void initializeDisplayHeight() {
            mDisplayHeight = mToolbar.getHeight() + mDisplayResult.getHeight()
                    + mDisplayFormula.getHeight();
        }

        @Override
        public void initializeScales(AlignedTextView formula, CalculatorResult result) {
            // no-op
        }

        @Override
        public void initializeFormulaTranslationY(AlignedTextView formula,
                CalculatorResult result) {
            // no-op
        }

        @Override
        public void initializeFormulaTranslationX(AlignedTextView formula) {
            // no-op
        }

        @Override
        public void initializeResultTranslationY(CalculatorResult result) {
            // no-op
        }

        @Override
        public void initializeResultTranslationX(CalculatorResult result) {
            // no-op
        }

        @Override
        public float getResultTranslationX(float yFraction) {
            return 0f;
        }

        @Override
        public float getResultTranslationY(float yFraction) {
            return 0f;
        }

        @Override
        public float getFormulaScale(float yFraction) {
            return 1f;
        }

        @Override
        public float getDateTranslationY(float yFraction) {
            return 0f;
        }

        @Override
        public float getHistoryElementTranslationY(float yFraction) {
            return -mDisplayHeight * (1f - yFraction) - mBottomPaddingHeight;
        }

        @Override
        public int getFirstTranslatedViewHolderIndex() {
            return 0;
        }
    }
}