• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 
17 package com.android.email.activity;
18 
19 import android.animation.Animator;
20 import android.animation.ObjectAnimator;
21 import android.animation.PropertyValuesHolder;
22 import android.animation.TimeInterpolator;
23 import android.content.Context;
24 import android.view.ViewGroup;
25 import android.view.animation.DecelerateInterpolator;
26 import android.widget.TextView;
27 
28 /**
29  * Class to hide/show a banner.
30  */
31 public class BannerController {
32     private static final int ANIMATION_DURATION = 100;
33     private static final TimeInterpolator INTERPOLATOR = new DecelerateInterpolator(1.5f);
34 
35     private final TextView mBannerView;
36     private final int mBannerHeight;
37 
38     private boolean mShown;
39 
40     /** Hold last animator to cancel. */
41     private Animator mLastAnimator;
42 
BannerController(Context context, TextView bannerView, int bannerHeight)43     public BannerController(Context context, TextView bannerView, int bannerHeight) {
44         mBannerView = bannerView;
45         mBannerHeight = bannerHeight;
46 
47         setBannerYAnim(-mBannerHeight); // hide by default.
48     }
49 
50     /**
51      * @return the current y position of the banner.
52      */
getBannerY()53     private int getBannerY() {
54         return ((ViewGroup.MarginLayoutParams) mBannerView.getLayoutParams()).topMargin;
55     }
56 
57     private static final String PROP_SET_BANNER_Y = "bannerYAnim";
58 
59     /**
60      * Set the Y position of the banner.  public, but should only be used by animators.
61      */
setBannerYAnim(int y)62     public void setBannerYAnim(int y) {
63         ((ViewGroup.MarginLayoutParams) mBannerView.getLayoutParams()).topMargin = y;
64         mBannerView.requestLayout();
65     }
66 
67     /**
68      * Show a banner with a message.
69      *
70      * @return false if a banner is already shown, in which case the message won't be updated.
71      */
show(String message)72     public boolean show(String message) {
73         if (mShown) {
74             return false; // If already shown, don't change the message, to avoid flicker.
75         }
76         mShown = true;
77         mBannerView.setText(message);
78         slideBanner(0);
79         return true;
80     }
81 
82     /**
83      * Dismiss a banner.
84      */
dismiss()85     public void dismiss() {
86         if (!mShown) {
87             return; // Always hidden, or hiding.
88         }
89         mShown = false;
90         slideBanner(-mBannerHeight); // Slide up to hide.
91     }
92 
slideBanner(int toY)93     private void slideBanner(int toY) {
94         if (mLastAnimator != null) {
95             mLastAnimator.cancel();
96         }
97 
98         final PropertyValuesHolder[] values = {
99                 PropertyValuesHolder.ofInt(PROP_SET_BANNER_Y, getBannerY(), toY) };
100         final ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(
101                 this, values).setDuration(ANIMATION_DURATION);
102         animator.setInterpolator(INTERPOLATOR);
103         mLastAnimator = animator;
104         animator.start();
105     }
106 }
107