1 /*
2  * Copyright (C) 2021 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 package com.android.launcher3.util;
17 
18 import android.content.Context;
19 import android.graphics.Canvas;
20 import android.widget.EdgeEffect;
21 
22 import com.android.launcher3.Utilities;
23 import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlay;
24 
25 /**
26  * Extension of {@link EdgeEffect} which shows the Launcher overlay
27  */
28 public class OverlayEdgeEffect extends EdgeEffectCompat {
29 
30     private final LauncherOverlay mOverlay;
31     private final boolean mIsRtl;
32 
33     private float mDistance;
34     private boolean mIsScrolling;
35 
OverlayEdgeEffect(Context context, LauncherOverlay overlay)36     public OverlayEdgeEffect(Context context, LauncherOverlay overlay) {
37         super(context);
38         mOverlay = overlay;
39         mIsRtl = Utilities.isRtl(context.getResources());
40 
41     }
42 
43     @Override
getDistance()44     public float getDistance() {
45         return mDistance;
46     }
47 
onPullDistance(float deltaDistance, float displacement)48     public float onPullDistance(float deltaDistance, float displacement) {
49         mDistance = Math.max(0f, deltaDistance + mDistance);
50         if (!mIsScrolling) {
51             mOverlay.onScrollInteractionBegin();
52             mIsScrolling = true;
53         }
54         mOverlay.onScrollChange(mDistance, mIsRtl);
55         return mDistance > 0 ? deltaDistance : 0;
56     }
57 
58     @Override
onAbsorb(int velocity)59     public void onAbsorb(int velocity) { }
60 
61     @Override
isFinished()62     public boolean isFinished() {
63         return mDistance <= 0;
64     }
65 
66     @Override
onRelease()67     public void onRelease() {
68         if (mIsScrolling) {
69             mDistance = 0;
70             mOverlay.onScrollInteractionEnd();
71             mIsScrolling = false;
72         }
73     }
74 
75     @Override
draw(Canvas canvas)76     public boolean draw(Canvas canvas) {
77         return false;
78     }
79 
finish()80     public void finish() {
81         mDistance = 0;
82     }
83 }
84