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.quickstep.views;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.util.FloatProperty;
22 import android.widget.Button;
23 
24 import com.android.launcher3.touch.PagedOrientationHandler;
25 import com.android.quickstep.views.RecentsView.PageCallbacks;
26 import com.android.quickstep.views.RecentsView.ScrollState;
27 
28 public class ClearAllButton extends Button implements PageCallbacks {
29 
30     public static final FloatProperty<ClearAllButton> VISIBILITY_ALPHA =
31             new FloatProperty<ClearAllButton>("visibilityAlpha") {
32                 @Override
33                 public Float get(ClearAllButton view) {
34                     return view.mVisibilityAlpha;
35                 }
36 
37                 @Override
38                 public void setValue(ClearAllButton view, float v) {
39                     view.setVisibilityAlpha(v);
40                 }
41             };
42 
43     private float mScrollAlpha = 1;
44     private float mContentAlpha = 1;
45     private float mVisibilityAlpha = 1;
46 
47     private boolean mIsRtl;
48 
49     private int mScrollOffset;
50 
ClearAllButton(Context context, AttributeSet attrs)51     public ClearAllButton(Context context, AttributeSet attrs) {
52         super(context, attrs);
53         mIsRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
54     }
55 
56     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)57     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
58         super.onLayout(changed, left, top, right, bottom);
59         PagedOrientationHandler orientationHandler = getRecentsView().getPagedOrientationHandler();
60         mScrollOffset = orientationHandler.getClearAllScrollOffset(getRecentsView(), mIsRtl);
61     }
62 
getRecentsView()63     private RecentsView getRecentsView() {
64         return (RecentsView) getParent();
65     }
66 
67     @Override
onRtlPropertiesChanged(int layoutDirection)68     public void onRtlPropertiesChanged(int layoutDirection) {
69         super.onRtlPropertiesChanged(layoutDirection);
70         mIsRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
71     }
72 
73     @Override
hasOverlappingRendering()74     public boolean hasOverlappingRendering() {
75         return false;
76     }
77 
setContentAlpha(float alpha)78     public void setContentAlpha(float alpha) {
79         if (mContentAlpha != alpha) {
80             mContentAlpha = alpha;
81             updateAlpha();
82         }
83     }
84 
setVisibilityAlpha(float alpha)85     public void setVisibilityAlpha(float alpha) {
86         if (mVisibilityAlpha != alpha) {
87             mVisibilityAlpha = alpha;
88             updateAlpha();
89         }
90     }
91 
92     @Override
onPageScroll(ScrollState scrollState)93     public void onPageScroll(ScrollState scrollState) {
94         PagedOrientationHandler orientationHandler = getRecentsView().getPagedOrientationHandler();
95         float orientationSize = orientationHandler.getPrimaryValue(getWidth(), getHeight());
96         if (orientationSize == 0) {
97             return;
98         }
99 
100         float shift = Math.min(scrollState.scrollFromEdge, orientationSize);
101         float translation = mIsRtl ? (mScrollOffset - shift) : (mScrollOffset + shift);
102         orientationHandler.setPrimaryAndResetSecondaryTranslate(this, translation);
103         mScrollAlpha = 1 - shift / orientationSize;
104         updateAlpha();
105     }
106 
updateAlpha()107     private void updateAlpha() {
108         final float alpha = mScrollAlpha * mContentAlpha * mVisibilityAlpha;
109         setAlpha(alpha);
110         setClickable(alpha == 1);
111     }
112 }
113