SystemBarScrimViews.java revision 59924fe0d9136cf349759bea1e06b661603f95fe
1/*
2 * Copyright (C) 2014 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.systemui.recents.views;
18
19import android.app.Activity;
20import android.content.Context;
21import android.view.View;
22
23import com.android.systemui.Interpolators;
24import com.android.systemui.R;
25import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
26import com.android.systemui.recents.events.activity.EnterRecentsWindowAnimationCompletedEvent;
27
28/** Manages the scrims for the various system bars. */
29public class SystemBarScrimViews {
30
31    Context mContext;
32
33    View mNavBarScrimView;
34
35    boolean mHasNavBarScrim;
36    boolean mShouldAnimateNavBarScrim;
37
38    int mNavBarScrimEnterDuration;
39
40    public SystemBarScrimViews(Activity activity) {
41        mContext = activity;
42        mNavBarScrimView = activity.findViewById(R.id.nav_bar_scrim);
43        mNavBarScrimEnterDuration = activity.getResources().getInteger(
44                R.integer.recents_nav_bar_scrim_enter_duration);
45    }
46
47    /**
48     * Prepares the scrim views for animating when entering Recents. This will be called before
49     * the first draw.
50     */
51    public void prepareEnterRecentsAnimation(boolean hasNavBarScrim, boolean animateNavBarScrim) {
52        mHasNavBarScrim = hasNavBarScrim;
53        mShouldAnimateNavBarScrim = animateNavBarScrim;
54
55        mNavBarScrimView.setVisibility(mHasNavBarScrim && !mShouldAnimateNavBarScrim ?
56                View.VISIBLE : View.INVISIBLE);
57    }
58
59    /**
60     * Animates the nav bar scrim visibility.
61     */
62    public void animateNavBarScrimVisibility(boolean visible, AnimationProps animation) {
63        int toY = 0;
64        if (visible) {
65            mNavBarScrimView.setVisibility(View.VISIBLE);
66            mNavBarScrimView.setTranslationY(mNavBarScrimView.getMeasuredHeight());
67        } else {
68            toY = mNavBarScrimView.getMeasuredHeight();
69        }
70        if (animation != AnimationProps.IMMEDIATE) {
71            mNavBarScrimView.animate()
72                    .translationY(toY)
73                    .setDuration(animation.getDuration(AnimationProps.BOUNDS))
74                    .setInterpolator(animation.getInterpolator(AnimationProps.BOUNDS))
75                    .start();
76        } else {
77            mNavBarScrimView.setTranslationY(toY);
78        }
79    }
80
81    /**** EventBus events ****/
82
83    /**
84     * Starts animating the scrim views when entering Recents.
85     */
86    public final void onBusEvent(EnterRecentsWindowAnimationCompletedEvent event) {
87        if (mHasNavBarScrim) {
88            AnimationProps animation = mShouldAnimateNavBarScrim
89                    ? new AnimationProps()
90                            .setDuration(AnimationProps.BOUNDS, mNavBarScrimEnterDuration)
91                            .setInterpolator(AnimationProps.BOUNDS, Interpolators.DECELERATE_QUINT)
92                    : AnimationProps.IMMEDIATE;
93            animateNavBarScrimVisibility(true, animation);
94        }
95    }
96
97    /**
98     * Starts animating the scrim views when leaving Recents (either via launching a task, or
99     * going home).
100     */
101    public final void onBusEvent(DismissRecentsToHomeAnimationStarted event) {
102        if (mHasNavBarScrim) {
103            AnimationProps animation = new AnimationProps()
104                    .setDuration(AnimationProps.BOUNDS,
105                            TaskStackAnimationHelper.EXIT_TO_HOME_TRANSLATION_DURATION)
106                    .setInterpolator(AnimationProps.BOUNDS, Interpolators.FAST_OUT_SLOW_IN);
107            animateNavBarScrimVisibility(false, animation);
108        }
109    }
110}
111