aboutsummaryrefslogtreecommitdiff
path: root/common/src/com/android/tv/common/ui/setup/animation/FadeAndShortSlide.java
blob: 60ffb70fa4cb314fa4562d695f61add055fa67a2 (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
/*
 * Copyright (C) 2015 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.tv.common.ui.setup.animation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.TimeInterpolator;
import android.transition.Fade;
import android.transition.Transition;
import android.transition.TransitionValues;
import android.transition.Visibility;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/** Execute horizontal slide of 1/4 width and fade (to workaround bug 23718734) */
public class FadeAndShortSlide extends Visibility {
    private static final TimeInterpolator APPEAR_INTERPOLATOR = new DecelerateInterpolator();
    private static final TimeInterpolator DISAPPEAR_INTERPOLATOR = new AccelerateInterpolator();

    private static final String PROPNAME_SCREEN_POSITION =
            "android_fadeAndShortSlideTransition_screenPosition";
    private static final String PROPNAME_DELAY = "propname_delay";

    private static final int DEFAULT_DISTANCE = 200;

    private abstract static class CalculateSlide {
        /** Returns the translation value for view when it goes out of the scene */
        public abstract float getGoneX(
                ViewGroup sceneRoot, View view, int[] position, int distance);
    }

    private static final CalculateSlide sCalculateStart =
            new CalculateSlide() {
                @Override
                public float getGoneX(
                        ViewGroup sceneRoot, View view, int[] position, int distance) {
                    final boolean isRtl =
                            sceneRoot.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
                    final float x;
                    if (isRtl) {
                        x = view.getTranslationX() + distance;
                    } else {
                        x = view.getTranslationX() - distance;
                    }
                    return x;
                }
            };

    private static final CalculateSlide sCalculateEnd =
            new CalculateSlide() {
                @Override
                public float getGoneX(
                        ViewGroup sceneRoot, View view, int[] position, int distance) {
                    final boolean isRtl =
                            sceneRoot.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
                    final float x;
                    if (isRtl) {
                        x = view.getTranslationX() - distance;
                    } else {
                        x = view.getTranslationX() + distance;
                    }
                    return x;
                }
            };

    private static final ViewPositionComparator sViewPositionComparator =
            new ViewPositionComparator();

    private int mSlideEdge;
    private CalculateSlide mSlideCalculator = sCalculateEnd;
    private Visibility mFade = new Fade();

    // TODO: Consider using TransitionPropagation.
    private final int[] mParentIdsForDelay;
    private int mDistance = DEFAULT_DISTANCE;

    public FadeAndShortSlide() {
        this(Gravity.START);
    }

    public FadeAndShortSlide(int slideEdge) {
        this(slideEdge, null);
    }

    public FadeAndShortSlide(int slideEdge, int[] parentIdsForDelay) {
        setSlideEdge(slideEdge);
        mParentIdsForDelay = parentIdsForDelay;
    }

    @Override
    public void setEpicenterCallback(EpicenterCallback epicenterCallback) {
        super.setEpicenterCallback(epicenterCallback);
        mFade.setEpicenterCallback(epicenterCallback);
    }

    private void captureValues(TransitionValues transitionValues) {
        View view = transitionValues.view;
        int[] position = new int[2];
        view.getLocationOnScreen(position);
        transitionValues.values.put(PROPNAME_SCREEN_POSITION, position);
    }

    private int getDelayOrder(View view, boolean appear) {
        if (mParentIdsForDelay == null) {
            return -1;
        }
        final View parentForDelay = findParentForDelay(view);
        if (parentForDelay == null || !(parentForDelay instanceof ViewGroup)) {
            return -1;
        }
        List<View> transitionTargets = new ArrayList<>();
        getTransitionTargets((ViewGroup) parentForDelay, transitionTargets);
        sViewPositionComparator.mParentForDelay = parentForDelay;
        sViewPositionComparator.mIsLtr = view.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
        sViewPositionComparator.mToLeft =
                sViewPositionComparator.mIsLtr
                        ? mSlideEdge == (appear ? Gravity.END : Gravity.START)
                        : mSlideEdge == (appear ? Gravity.START : Gravity.END);
        Collections.sort(transitionTargets, sViewPositionComparator);
        return transitionTargets.indexOf(view);
    }

    private View findParentForDelay(View view) {
        if (isParentForDelay(view.getId())) {
            return view;
        }
        View parent = view;
        while (parent.getParent() instanceof View) {
            parent = (View) parent.getParent();
            if (isParentForDelay(parent.getId())) {
                return parent;
            }
        }
        return null;
    }

    private boolean isParentForDelay(int viewId) {
        for (int id : mParentIdsForDelay) {
            if (id == viewId) {
                return true;
            }
        }
        return false;
    }

    private void getTransitionTargets(ViewGroup parent, List<View> transitionTargets) {
        int count = parent.getChildCount();
        for (int i = 0; i < count; ++i) {
            View child = parent.getChildAt(i);
            if (child instanceof ViewGroup && !((ViewGroup) child).isTransitionGroup()) {
                getTransitionTargets((ViewGroup) child, transitionTargets);
            } else {
                transitionTargets.add(child);
            }
        }
    }

    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        super.captureStartValues(transitionValues);
        mFade.captureStartValues(transitionValues);
        captureValues(transitionValues);
        int delayIndex = getDelayOrder(transitionValues.view, false);
        if (delayIndex > 0) {
            transitionValues.values.put(
                    PROPNAME_DELAY, delayIndex * SetupAnimationHelper.DELAY_BETWEEN_SIBLINGS_MS);
        }
    }

    @Override
    public void captureEndValues(TransitionValues transitionValues) {
        super.captureEndValues(transitionValues);
        mFade.captureEndValues(transitionValues);
        captureValues(transitionValues);
        int delayIndex = getDelayOrder(transitionValues.view, true);
        if (delayIndex > 0) {
            transitionValues.values.put(
                    PROPNAME_DELAY, delayIndex * SetupAnimationHelper.DELAY_BETWEEN_SIBLINGS_MS);
        }
    }

    public void setSlideEdge(int slideEdge) {
        mSlideEdge = slideEdge;
        switch (slideEdge) {
            case Gravity.START:
                mSlideCalculator = sCalculateStart;
                break;
            case Gravity.END:
                mSlideCalculator = sCalculateEnd;
                break;
            default:
                throw new IllegalArgumentException("Invalid slide direction");
        }
    }

    @Override
    public Animator onAppear(
            ViewGroup sceneRoot,
            View view,
            TransitionValues startValues,
            TransitionValues endValues) {
        if (endValues == null) {
            return null;
        }
        int[] position = (int[]) endValues.values.get(PROPNAME_SCREEN_POSITION);
        int left = position[0];
        float endX = view.getTranslationX();
        float startX = mSlideCalculator.getGoneX(sceneRoot, view, position, mDistance);
        final Animator slideAnimator =
                TranslationAnimationCreator.createAnimation(
                        view, endValues, left, startX, endX, APPEAR_INTERPOLATOR, this);
        if (slideAnimator == null) {
            return null;
        }
        mFade.setInterpolator(APPEAR_INTERPOLATOR);
        final AnimatorSet set = new AnimatorSet();
        set.play(slideAnimator).with(mFade.onAppear(sceneRoot, view, startValues, endValues));
        Long delay = (Long) endValues.values.get(PROPNAME_DELAY);
        if (delay != null) {
            set.setStartDelay(delay);
        }
        return set;
    }

    @Override
    public Animator onDisappear(
            ViewGroup sceneRoot,
            final View view,
            TransitionValues startValues,
            TransitionValues endValues) {
        if (startValues == null) {
            return null;
        }
        int[] position = (int[]) startValues.values.get(PROPNAME_SCREEN_POSITION);
        int left = position[0];
        float startX = view.getTranslationX();
        float endX = mSlideCalculator.getGoneX(sceneRoot, view, position, mDistance);
        final Animator slideAnimator =
                TranslationAnimationCreator.createAnimation(
                        view, startValues, left, startX, endX, DISAPPEAR_INTERPOLATOR, this);
        if (slideAnimator == null) { // slideAnimator is null if startX == endX
            return null;
        }

        mFade.setInterpolator(DISAPPEAR_INTERPOLATOR);
        final Animator fadeAnimator = mFade.onDisappear(sceneRoot, view, startValues, endValues);
        if (fadeAnimator == null) {
            return null;
        }
        fadeAnimator.addListener(
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animator) {
                        fadeAnimator.removeListener(this);
                        view.setAlpha(0.0f);
                    }
                });

        final AnimatorSet set = new AnimatorSet();
        set.play(slideAnimator).with(fadeAnimator);
        Long delay = (Long) startValues.values.get(PROPNAME_DELAY);
        if (delay != null) {
            set.setStartDelay(delay);
        }
        return set;
    }

    @Override
    public Transition addListener(TransitionListener listener) {
        mFade.addListener(listener);
        return super.addListener(listener);
    }

    @Override
    public Transition removeListener(TransitionListener listener) {
        mFade.removeListener(listener);
        return super.removeListener(listener);
    }

    @Override
    public Transition clone() {
        FadeAndShortSlide clone = (FadeAndShortSlide) super.clone();
        clone.mFade = (Visibility) mFade.clone();
        return clone;
    }

    @Override
    public Transition setDuration(long duration) {
        long scaledDuration = SetupAnimationHelper.applyAnimationTimeScale(duration);
        mFade.setDuration(scaledDuration);
        return super.setDuration(scaledDuration);
    }

    /** Sets the moving distance in pixel. */
    public void setDistance(int distance) {
        mDistance = distance;
    }

    private static class ViewPositionComparator implements Comparator<View> {
        View mParentForDelay;
        boolean mIsLtr;
        boolean mToLeft;

        @Override
        public int compare(View lhs, View rhs) {
            int start1;
            int start2;
            if (mIsLtr) {
                start1 = getRelativeLeft(lhs, mParentForDelay);
                start2 = getRelativeLeft(rhs, mParentForDelay);
            } else {
                start1 = getRelativeRight(lhs, mParentForDelay);
                start2 = getRelativeRight(rhs, mParentForDelay);
            }
            if (mToLeft) {
                if (start1 > start2) {
                    return 1;
                } else if (start1 < start2) {
                    return -1;
                }
            } else {
                if (start1 > start2) {
                    return -1;
                } else if (start1 < start2) {
                    return 1;
                }
            }
            int top1 = getRelativeTop(lhs, mParentForDelay);
            int top2 = getRelativeTop(rhs, mParentForDelay);
            return Integer.compare(top1, top2);
        }

        private int getRelativeLeft(View child, View ancestor) {
            ViewParent parent = child.getParent();
            int left = child.getLeft();
            while (parent instanceof View) {
                if (parent == ancestor) {
                    break;
                }
                left += ((View) parent).getLeft();
                parent = parent.getParent();
            }
            return left;
        }

        private int getRelativeRight(View child, View ancestor) {
            ViewParent parent = child.getParent();
            int right = child.getRight();
            while (parent instanceof View) {
                if (parent == ancestor) {
                    break;
                }
                right += ((View) parent).getLeft();
                parent = parent.getParent();
            }
            return right;
        }

        private int getRelativeTop(View child, View ancestor) {
            ViewParent parent = child.getParent();
            int top = child.getTop();
            while (parent instanceof View) {
                if (parent == ancestor) {
                    break;
                }
                top += ((View) parent).getTop();
                parent = parent.getParent();
            }
            return top;
        }
    }
}