1/*
2 * Copyright (C) 2017 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 android.support.transition;
18
19import android.graphics.Rect;
20import android.support.v4.view.ViewCompat;
21import android.view.Gravity;
22import android.view.View;
23import android.view.ViewGroup;
24
25/**
26 * A <code>TransitionPropagation</code> that propagates based on the distance to the side
27 * and, orthogonally, the distance to epicenter. If the transitioning View is visible in
28 * the start of the transition, then it will transition sooner when closer to the side and
29 * later when farther. If the view is not visible in the start of the transition, then
30 * it will transition later when closer to the side and sooner when farther from the edge.
31 * This is the default TransitionPropagation used with {@link android.transition.Slide}.
32 */
33public class SidePropagation extends VisibilityPropagation {
34
35    private float mPropagationSpeed = 3.0f;
36    private int mSide = Gravity.BOTTOM;
37
38    /**
39     * Sets the side that is used to calculate the transition propagation. If the transitioning
40     * View is visible in the start of the transition, then it will transition sooner when
41     * closer to the side and later when farther. If the view is not visible in the start of
42     * the transition, then it will transition later when closer to the side and sooner when
43     * farther from the edge. The default is {@link Gravity#BOTTOM}.
44     *
45     * @param side The side that is used to calculate the transition propagation. Must be one of
46     *             {@link Gravity#LEFT}, {@link Gravity#TOP}, {@link Gravity#RIGHT},
47     *             {@link Gravity#BOTTOM}, {@link Gravity#START}, or {@link Gravity#END}.
48     */
49    public void setSide(@Slide.GravityFlag int side) {
50        mSide = side;
51    }
52
53    /**
54     * Sets the speed at which transition propagation happens, relative to the duration of the
55     * Transition. A <code>propagationSpeed</code> of 1 means that a View centered at the side
56     * set in {@link #setSide(int)} and View centered at the opposite edge will have a difference
57     * in start delay of approximately the duration of the Transition. A speed of 2 means the
58     * start delay difference will be approximately half of the duration of the transition. A
59     * value of 0 is illegal, but negative values will invert the propagation.
60     *
61     * @param propagationSpeed The speed at which propagation occurs, relative to the duration
62     *                         of the transition. A speed of 4 means it works 4 times as fast
63     *                         as the duration of the transition. May not be 0.
64     */
65    public void setPropagationSpeed(float propagationSpeed) {
66        if (propagationSpeed == 0) {
67            throw new IllegalArgumentException("propagationSpeed may not be 0");
68        }
69        mPropagationSpeed = propagationSpeed;
70    }
71
72    @Override
73    public long getStartDelay(ViewGroup sceneRoot, Transition transition,
74            TransitionValues startValues, TransitionValues endValues) {
75        if (startValues == null && endValues == null) {
76            return 0;
77        }
78        int directionMultiplier = 1;
79        Rect epicenter = transition.getEpicenter();
80        TransitionValues positionValues;
81        if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) {
82            positionValues = startValues;
83            directionMultiplier = -1;
84        } else {
85            positionValues = endValues;
86        }
87
88        int viewCenterX = getViewX(positionValues);
89        int viewCenterY = getViewY(positionValues);
90
91        int[] loc = new int[2];
92        sceneRoot.getLocationOnScreen(loc);
93        int left = loc[0] + Math.round(sceneRoot.getTranslationX());
94        int top = loc[1] + Math.round(sceneRoot.getTranslationY());
95        int right = left + sceneRoot.getWidth();
96        int bottom = top + sceneRoot.getHeight();
97
98        int epicenterX;
99        int epicenterY;
100        if (epicenter != null) {
101            epicenterX = epicenter.centerX();
102            epicenterY = epicenter.centerY();
103        } else {
104            epicenterX = (left + right) / 2;
105            epicenterY = (top + bottom) / 2;
106        }
107
108        float distance = distance(sceneRoot, viewCenterX, viewCenterY, epicenterX, epicenterY,
109                left, top, right, bottom);
110        float maxDistance = getMaxDistance(sceneRoot);
111        float distanceFraction = distance / maxDistance;
112
113        long duration = transition.getDuration();
114        if (duration < 0) {
115            duration = 300;
116        }
117
118        return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
119    }
120
121    private int distance(View sceneRoot, int viewX, int viewY, int epicenterX, int epicenterY,
122            int left, int top, int right, int bottom) {
123        final int side;
124        if (mSide == Gravity.START) {
125            final boolean isRtl = ViewCompat.getLayoutDirection(sceneRoot)
126                    == ViewCompat.LAYOUT_DIRECTION_RTL;
127            side = isRtl ? Gravity.RIGHT : Gravity.LEFT;
128        } else if (mSide == Gravity.END) {
129            final boolean isRtl = ViewCompat.getLayoutDirection(sceneRoot)
130                    == ViewCompat.LAYOUT_DIRECTION_RTL;
131            side = isRtl ? Gravity.LEFT : Gravity.RIGHT;
132        } else {
133            side = mSide;
134        }
135        int distance = 0;
136        switch (side) {
137            case Gravity.LEFT:
138                distance = right - viewX + Math.abs(epicenterY - viewY);
139                break;
140            case Gravity.TOP:
141                distance = bottom - viewY + Math.abs(epicenterX - viewX);
142                break;
143            case Gravity.RIGHT:
144                distance = viewX - left + Math.abs(epicenterY - viewY);
145                break;
146            case Gravity.BOTTOM:
147                distance = viewY - top + Math.abs(epicenterX - viewX);
148                break;
149        }
150        return distance;
151    }
152
153    private int getMaxDistance(ViewGroup sceneRoot) {
154        switch (mSide) {
155            case Gravity.LEFT:
156            case Gravity.RIGHT:
157            case Gravity.START:
158            case Gravity.END:
159                return sceneRoot.getWidth();
160            default:
161                return sceneRoot.getHeight();
162        }
163    }
164
165}
166