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.util;
18 
19 import static com.android.launcher3.LauncherAnimUtils.VIEW_ALPHA;
20 
21 import android.view.View;
22 
23 import com.android.launcher3.anim.AlphaUpdateListener;
24 
25 /**
26  * Utility class to handle separating a single value as a factor of multiple values
27  */
28 public class MultiValueAlpha extends MultiPropertyFactory<View> {
29 
30     private static final FloatBiFunction ALPHA_AGGREGATOR = (a, b) -> a * b;
31 
32     // Whether we should change from INVISIBLE to VISIBLE and vice versa at low alpha values.
33     private boolean mUpdateVisibility;
34 
35     private final int mHiddenVisibility;
36 
MultiValueAlpha(View view, int size)37     public MultiValueAlpha(View view, int size) {
38         this(view, size, View.INVISIBLE);
39     }
40 
MultiValueAlpha(View view, int size, int hiddenVisibility)41     public MultiValueAlpha(View view, int size, int hiddenVisibility) {
42         super(view, VIEW_ALPHA, size, ALPHA_AGGREGATOR, 1f);
43         this.mHiddenVisibility = hiddenVisibility;
44     }
45 
46     /** Sets whether we should update between INVISIBLE and VISIBLE based on alpha. */
setUpdateVisibility(boolean updateVisibility)47     public void setUpdateVisibility(boolean updateVisibility) {
48         mUpdateVisibility = updateVisibility;
49     }
50 
51     @Override
apply(float value)52     protected void apply(float value) {
53         super.apply(value);
54         if (mUpdateVisibility) {
55             AlphaUpdateListener.updateVisibility(mTarget, mHiddenVisibility);
56         }
57     }
58 }
59