aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/ui/TvTransitionManager.java
blob: 628bbb72d6b877a058a004426701a1b84cae33e4 (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
/*
 * 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.ui;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.support.annotation.IntDef;
import android.transition.Fade;
import android.transition.Scene;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
import android.transition.TransitionSet;
import android.transition.TransitionValues;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;

import com.android.tv.MainActivity;
import com.android.tv.R;
import com.android.tv.data.Channel;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class TvTransitionManager extends TransitionManager {
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({SCENE_TYPE_EMPTY, SCENE_TYPE_CHANNEL_BANNER, SCENE_TYPE_INPUT_BANNER,
        SCENE_TYPE_KEYPAD_CHANNEL_SWITCH, SCENE_TYPE_SELECT_INPUT})
    public @interface SceneType {}
    public static final int SCENE_TYPE_EMPTY = 0;
    public static final int SCENE_TYPE_CHANNEL_BANNER = 1;
    public static final int SCENE_TYPE_INPUT_BANNER = 2;
    public static final int SCENE_TYPE_KEYPAD_CHANNEL_SWITCH = 3;
    public static final int SCENE_TYPE_SELECT_INPUT = 4;

    private final MainActivity mMainActivity;
    private final ViewGroup mSceneContainer;
    private final ChannelBannerView mChannelBannerView;
    private final InputBannerView mInputBannerView;
    private final KeypadChannelSwitchView mKeypadChannelSwitchView;
    private final SelectInputView mSelectInputView;
    private final FrameLayout mEmptyView;
    private ViewGroup mCurrentSceneView;
    private Animator mEnterAnimator;
    private Animator mExitAnimator;

    private boolean mInitialized;
    private Scene mEmptyScene;
    private Scene mChannelBannerScene;
    private Scene mInputBannerScene;
    private Scene mKeypadChannelSwitchScene;
    private Scene mSelectInputScene;
    private Scene mCurrentScene;

    private Listener mListener;

    public TvTransitionManager(MainActivity mainActivity, ViewGroup sceneContainer,
            ChannelBannerView channelBannerView, InputBannerView inputBannerView,
            KeypadChannelSwitchView keypadChannelSwitchView, SelectInputView selectInputView) {
        mMainActivity = mainActivity;
        mSceneContainer = sceneContainer;
        mChannelBannerView = channelBannerView;
        mInputBannerView = inputBannerView;
        mKeypadChannelSwitchView = keypadChannelSwitchView;
        mSelectInputView = selectInputView;
        mEmptyView = (FrameLayout) mMainActivity.getLayoutInflater().inflate(
                R.layout.empty_info_banner, sceneContainer, false);
        mCurrentSceneView = mEmptyView;
    }

    public void goToEmptyScene(boolean withAnimation) {
        if (mCurrentScene == mEmptyScene) {
            return;
        }
        initIfNeeded();
        if (withAnimation) {
            mEmptyView.setAlpha(1.0f);
            transitionTo(mEmptyScene);
        } else {
            TransitionManager.go(mEmptyScene, null);
            // When transition is null, transition got stuck without calling endTransitions.
            TransitionManager.endTransitions(mEmptyScene.getSceneRoot());
            // Since Fade.OUT transition doesn't run, we need to set alpha manually.
            mEmptyView.setAlpha(0);
        }
    }

    public void goToChannelBannerScene() {
        initIfNeeded();
        Channel channel = mMainActivity.getCurrentChannel();
        if (channel != null && channel.isPassthrough()) {
            if (mCurrentScene != mInputBannerScene) {
                // Show the input banner instead.
                LayoutParams lp = (LayoutParams) mInputBannerView.getLayoutParams();
                lp.width = mCurrentScene == mSelectInputScene ? mSelectInputView.getWidth()
                        : FrameLayout.LayoutParams.WRAP_CONTENT;
                mInputBannerView.setLayoutParams(lp);
                mInputBannerView.updateLabel();
                transitionTo(mInputBannerScene);
            }
        } else if (mCurrentScene != mChannelBannerScene) {
            transitionTo(mChannelBannerScene);
        }
    }

    public void goToKeypadChannelSwitchScene() {
        initIfNeeded();
        if (mCurrentScene != mKeypadChannelSwitchScene) {
            transitionTo(mKeypadChannelSwitchScene);
        }
    }

    public void goToSelectInputScene() {
        initIfNeeded();
        if (mCurrentScene != mSelectInputScene) {
            mSelectInputView.setCurrentChannel(mMainActivity.getCurrentChannel());
            transitionTo(mSelectInputScene);
        }
    }

    public boolean isSceneActive() {
        return mCurrentScene != mEmptyScene;
    }

    public boolean isKeypadChannelSwitchActive() {
        return mCurrentScene != null && mCurrentScene == mKeypadChannelSwitchScene;
    }

    public boolean isSelectInputActive() {
        return mCurrentScene != null && mCurrentScene == mSelectInputScene;
    }

    public void setListener(Listener listener) {
        mListener = listener;
    }

    public void initIfNeeded() {
        if (mInitialized) {
            return;
        }
        mEnterAnimator = AnimatorInflater.loadAnimator(mMainActivity,
                R.animator.channel_banner_enter);
        mExitAnimator = AnimatorInflater.loadAnimator(mMainActivity,
                R.animator.channel_banner_exit);

        mEmptyScene = new Scene(mSceneContainer, (View) mEmptyView);
        mEmptyScene.setEnterAction(new Runnable() {
            @Override
            public void run() {
                FrameLayout.LayoutParams emptySceneLayoutParams =
                        (FrameLayout.LayoutParams) mEmptyView.getLayoutParams();
                ViewGroup.MarginLayoutParams lp =
                        (ViewGroup.MarginLayoutParams) mCurrentSceneView.getLayoutParams();
                emptySceneLayoutParams.topMargin = mCurrentSceneView.getTop();
                emptySceneLayoutParams.setMarginStart(lp.getMarginStart());
                emptySceneLayoutParams.height = mCurrentSceneView.getHeight();
                emptySceneLayoutParams.width = mCurrentSceneView.getWidth();
                mEmptyView.setLayoutParams(emptySceneLayoutParams);
                setCurrentScene(mEmptyScene, mEmptyView);
            }
        });
        mEmptyScene.setExitAction(new Runnable() {
            @Override
            public void run() {
                removeAllViewsFromOverlay();
            }
        });

        mChannelBannerScene = buildScene(mSceneContainer, mChannelBannerView);
        mInputBannerScene = buildScene(mSceneContainer, mInputBannerView);
        mKeypadChannelSwitchScene = buildScene(mSceneContainer, mKeypadChannelSwitchView);
        mSelectInputScene = buildScene(mSceneContainer, mSelectInputView);
        mCurrentScene = mEmptyScene;

        // Enter transitions
        TransitionSet enter = new TransitionSet()
                .addTransition(new SceneTransition(SceneTransition.ENTER))
                .addTransition(new Fade(Fade.IN));
        setTransition(mEmptyScene, mChannelBannerScene, enter);
        setTransition(mEmptyScene, mInputBannerScene, enter);
        setTransition(mEmptyScene, mKeypadChannelSwitchScene, enter);
        setTransition(mEmptyScene, mSelectInputScene, enter);

        // Exit transitions
        TransitionSet exit = new TransitionSet()
                .addTransition(new SceneTransition(SceneTransition.EXIT))
                .addTransition(new Fade(Fade.OUT));
        setTransition(mChannelBannerScene, mEmptyScene, exit);
        setTransition(mInputBannerScene, mEmptyScene, exit);
        setTransition(mKeypadChannelSwitchScene, mEmptyScene, exit);
        setTransition(mSelectInputScene, mEmptyScene, exit);

        // All other possible transitions between scenes
        TransitionInflater ti = TransitionInflater.from(mMainActivity);
        Transition transition = ti.inflateTransition(R.transition.transition_between_scenes);
        setTransition(mChannelBannerScene, mKeypadChannelSwitchScene, transition);
        setTransition(mChannelBannerScene, mSelectInputScene, transition);
        setTransition(mInputBannerScene, mSelectInputScene, transition);
        setTransition(mKeypadChannelSwitchScene, mChannelBannerScene, transition);
        setTransition(mKeypadChannelSwitchScene, mSelectInputScene, transition);
        setTransition(mSelectInputScene, mChannelBannerScene, transition);
        setTransition(mSelectInputScene, mInputBannerScene, transition);

        mInitialized = true;
    }

    /**
     * Returns the type of the given scene.
     */
    @SceneType public int getSceneType(Scene scene) {
        if (scene == mChannelBannerScene) {
            return SCENE_TYPE_CHANNEL_BANNER;
        } else if (scene == mInputBannerScene) {
            return SCENE_TYPE_INPUT_BANNER;
        } else if (scene == mKeypadChannelSwitchScene) {
            return SCENE_TYPE_KEYPAD_CHANNEL_SWITCH;
        } else if (scene == mSelectInputScene) {
            return SCENE_TYPE_SELECT_INPUT;
        }
        return SCENE_TYPE_EMPTY;
    }

    private void setCurrentScene(Scene scene, ViewGroup sceneView) {
        if (mListener != null) {
            mListener.onSceneChanged(getSceneType(mCurrentScene), getSceneType(scene));
        }
        mCurrentScene = scene;
        mCurrentSceneView = sceneView;
        // TODO: Is this a still valid call?
        mMainActivity.updateKeyInputFocus();
    }

    public interface TransitionLayout {
        // TODO: remove the parameter fromEmptyScene once a bug regarding transition alpha
        // is fixed. The bug is that the transition alpha is not reset after the transition is
        // canceled.
        void onEnterAction(boolean fromEmptyScene);

        void onExitAction();
    }

    private Scene buildScene(ViewGroup sceneRoot, final TransitionLayout layout) {
        final Scene scene = new Scene(sceneRoot, (View) layout);
        scene.setEnterAction(new Runnable() {
            @Override
            public void run() {
                boolean wasEmptyScene = (mCurrentScene == mEmptyScene);
                setCurrentScene(scene, (ViewGroup) layout);
                layout.onEnterAction(wasEmptyScene);
            }
        });
        scene.setExitAction(new Runnable() {
            @Override
            public void run() {
                removeAllViewsFromOverlay();
                layout.onExitAction();
            }
        });
        return scene;
    }

    private void removeAllViewsFromOverlay() {
        // Clean up all the animations which can be still running.
        mSceneContainer.getOverlay().remove(mChannelBannerView);
        mSceneContainer.getOverlay().remove(mInputBannerView);
        mSceneContainer.getOverlay().remove(mKeypadChannelSwitchView);
        mSceneContainer.getOverlay().remove(mSelectInputView);
    }

    private class SceneTransition extends Transition {
        static final int ENTER = 0;
        static final int EXIT = 1;

        private final Animator mAnimator;

        SceneTransition(int mode) {
            mAnimator = mode == ENTER ? mEnterAnimator : mExitAnimator;
        }

        @Override
        public void captureStartValues(TransitionValues transitionValues) {
        }

        @Override
        public void captureEndValues(TransitionValues transitionValues) {
        }

        @Override
        public Animator createAnimator(
                ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
            Animator animator = mAnimator.clone();
            animator.setTarget(sceneRoot);
            animator.addListener(new HardwareLayerAnimatorListenerAdapter(sceneRoot));
            return animator;
        }
    }

    /**
     * An interface for notification of the scene transition.
     */
    public interface Listener {
        /**
         * Called when the scene changes. This method is called just before the scene transition.
         */
        void onSceneChanged(@SceneType int fromSceneType, @SceneType int toSceneType);
    }
}