1 /**
2  * Copyright (c) 2011, Google Inc.
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 package com.android.mail.compose;
17 
18 import android.animation.Animator;
19 import android.animation.AnimatorSet;
20 import android.animation.ObjectAnimator;
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.util.AttributeSet;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.widget.RelativeLayout;
27 import com.android.mail.R;
28 
29 public class CcBccView extends RelativeLayout {
30 
31     private final View mCc;
32     private final View mBcc;
33 
CcBccView(Context context)34     public CcBccView(Context context) {
35         this(context, null);
36     }
37 
CcBccView(Context context, AttributeSet attrs)38     public CcBccView(Context context, AttributeSet attrs) {
39         this(context, attrs, -1);
40     }
41 
CcBccView(Context context, AttributeSet attrs, int defStyle)42     public CcBccView(Context context, AttributeSet attrs, int defStyle) {
43         super(context, attrs, defStyle);
44         LayoutInflater.from(context).inflate(R.layout.cc_bcc_view, this);
45         mCc = findViewById(R.id.cc_content);
46         mBcc = findViewById(R.id.bcc_content);
47     }
48 
show(boolean animate, boolean showCc, boolean showBcc)49     public void show(boolean animate, boolean showCc, boolean showBcc) {
50         boolean ccWasAlreadyShown = mCc.isShown();
51         mCc.setVisibility(showCc ? View.VISIBLE : View.GONE);
52         mBcc.setVisibility(showBcc ? View.VISIBLE : View.GONE);
53         if (animate) {
54             animate(ccWasAlreadyShown);
55         } else {
56             if (showCc) {
57                 mCc.setAlpha(1);
58             }
59             if (showBcc) {
60                 mBcc.setAlpha(1);
61             }
62             requestLayout();
63         }
64     }
65 
animate(boolean ccWasAlreadyShown)66     private void animate(boolean ccWasAlreadyShown) {
67         Resources res = getResources();
68         // Then, have cc/ bcc fade in
69         int fadeDuration = res.getInteger(R.integer.fadein_cc_bcc_dur);
70         ObjectAnimator bccAnimator = ObjectAnimator.ofFloat(mBcc, "alpha", 0, 1);
71         bccAnimator.setDuration(fadeDuration);
72 
73         Animator fadeAnimation;
74         if (!ccWasAlreadyShown) {
75             ObjectAnimator ccAnimator = ObjectAnimator.ofFloat(mCc, "alpha", 0, 1);
76             ccAnimator.setDuration(fadeDuration);
77             fadeAnimation = new AnimatorSet();
78             ((AnimatorSet) fadeAnimation).playTogether(ccAnimator, bccAnimator);
79         } else {
80             fadeAnimation = bccAnimator;
81         }
82         fadeAnimation.start();
83     }
84 
85     /**
86      * @return whether the CC field is visible
87      */
isCcVisible()88     public boolean isCcVisible() {
89         return mCc.getVisibility() == View.VISIBLE;
90     }
91 
92     /**
93      * @return whether the BCC field is visible
94      */
isBccVisible()95     public boolean isBccVisible() {
96         return mBcc.getVisibility() == View.VISIBLE;
97     }
98 }
99