• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Property;
22 import android.widget.Button;
23 
24 import com.android.launcher3.Utilities;
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 Property<ClearAllButton, Float> VISIBILITY_ALPHA =
31             new Property<ClearAllButton, Float>(Float.class, "visibilityAlpha") {
32                 @Override
33                 public Float get(ClearAllButton view) {
34                     return view.mVisibilityAlpha;
35                 }
36 
37                 @Override
38                 public void set(ClearAllButton view, Float visibilityAlpha) {
39                     view.setVisibilityAlpha(visibilityAlpha);
40                 }
41             };
42 
43     private float mScrollAlpha = 1;
44     private float mContentAlpha = 1;
45     private float mVisibilityAlpha = 1;
46 
47     private final 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 = Utilities.isRtl(context.getResources());
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 
60         RecentsView parent = (RecentsView) getParent();
61         mScrollOffset = mIsRtl ? parent.getPaddingRight() / 2 : - parent.getPaddingLeft() / 2;
62     }
63 
64     @Override
hasOverlappingRendering()65     public boolean hasOverlappingRendering() {
66         return false;
67     }
68 
setContentAlpha(float alpha)69     public void setContentAlpha(float alpha) {
70         if (mContentAlpha != alpha) {
71             mContentAlpha = alpha;
72             updateAlpha();
73         }
74     }
75 
setVisibilityAlpha(float alpha)76     public void setVisibilityAlpha(float alpha) {
77         if (mVisibilityAlpha != alpha) {
78             mVisibilityAlpha = alpha;
79             updateAlpha();
80         }
81     }
82 
83     @Override
onPageScroll(ScrollState scrollState)84     public void onPageScroll(ScrollState scrollState) {
85         float width = getWidth();
86         if (width == 0) {
87             return;
88         }
89 
90         float shift = Math.min(scrollState.scrollFromEdge, width);
91         setTranslationX(mIsRtl ? (mScrollOffset - shift) : (mScrollOffset + shift));
92         mScrollAlpha = 1 - shift / width;
93         updateAlpha();
94     }
95 
updateAlpha()96     private void updateAlpha() {
97         final float alpha = mScrollAlpha * mContentAlpha * mVisibilityAlpha;
98         setAlpha(alpha);
99         setClickable(alpha == 1);
100     }
101 }
102