1 /*
2  * Copyright (C) 2018 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.launcher3.anim;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.ValueAnimator;
22 import android.animation.ValueAnimator.AnimatorUpdateListener;
23 import android.view.View;
24 import android.view.ViewGroup;
25 
26 /**
27  * A convenience class to update a view's visibility state after an alpha animation.
28  */
29 public class AlphaUpdateListener extends AnimatorListenerAdapter
30         implements AnimatorUpdateListener {
31     public static final float ALPHA_CUTOFF_THRESHOLD = 0.01f;
32 
33     private View mView;
34 
AlphaUpdateListener(View v)35     public AlphaUpdateListener(View v) {
36         mView = v;
37     }
38 
39     @Override
onAnimationUpdate(ValueAnimator arg0)40     public void onAnimationUpdate(ValueAnimator arg0) {
41         updateVisibility(mView);
42     }
43 
44     @Override
onAnimationEnd(Animator animator)45     public void onAnimationEnd(Animator animator) {
46         updateVisibility(mView);
47     }
48 
49     @Override
onAnimationStart(Animator arg0)50     public void onAnimationStart(Animator arg0) {
51         // We want the views to be visible for animation, so fade-in/out is visible
52         mView.setVisibility(View.VISIBLE);
53     }
54 
updateVisibility(View view)55     public static void updateVisibility(View view) {
56         updateVisibility(view, View.INVISIBLE);
57     }
58 
59     /**
60      * Update view's visibility.
61      *
62      * @param view View that needs to update visibility.
63      * @param hiddenVisibility {@link View#GONE} or {@link View#INVISIBLE}
64      */
updateVisibility(View view, int hiddenVisibility)65     public static void updateVisibility(View view, int hiddenVisibility) {
66         if (view.getAlpha() < ALPHA_CUTOFF_THRESHOLD && view.getVisibility() != hiddenVisibility) {
67             view.setVisibility(hiddenVisibility);
68         } else if (view.getAlpha() > ALPHA_CUTOFF_THRESHOLD
69                 && view.getVisibility() != View.VISIBLE) {
70             if (view instanceof ViewGroup) {
71                 ViewGroup viewGroup = ((ViewGroup) view);
72                 int oldFocusability = viewGroup.getDescendantFocusability();
73                 viewGroup.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
74                 viewGroup.setVisibility(View.VISIBLE);
75                 viewGroup.setDescendantFocusability(oldFocusability);
76             } else {
77                 view.setVisibility(View.VISIBLE);
78             }
79         }
80     }
81 }