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.os.SystemClock; 21 import android.view.MotionEvent; 22 import android.widget.EdgeEffect; 23 24 import com.android.launcher3.BuildConfig; 25 import com.android.launcher3.Utilities; 26 import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlayTouchProxy; 27 28 /** 29 * Extension of {@link EdgeEffect} which shows the Launcher overlay 30 */ 31 public class OverlayEdgeEffect extends EdgeEffectCompat { 32 33 protected float mDistance; 34 protected final LauncherOverlayTouchProxy mOverlay; 35 protected boolean mIsScrolling; 36 protected final boolean mIsRtl; 37 OverlayEdgeEffect(Context context, LauncherOverlayTouchProxy overlay)38 public OverlayEdgeEffect(Context context, LauncherOverlayTouchProxy overlay) { 39 super(context); 40 mOverlay = overlay; 41 mIsRtl = Utilities.isRtl(context.getResources()); 42 } 43 44 @Override getDistance()45 public float getDistance() { 46 return mDistance; 47 } 48 49 @Override onPullDistance(float deltaDistance, float displacement)50 public float onPullDistance(float deltaDistance, float displacement) { 51 // Fallback implementation, will never actually get called 52 if (BuildConfig.IS_DEBUG_DEVICE) { 53 throw new RuntimeException("Wrong method called"); 54 } 55 MotionEvent mv = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), 56 MotionEvent.ACTION_MOVE, displacement, 0, 0); 57 try { 58 return onPullDistance(deltaDistance, displacement, mv); 59 } finally { 60 mv.recycle(); 61 } 62 } 63 64 @Override onPullDistance(float deltaDistance, float displacement, MotionEvent ev)65 public float onPullDistance(float deltaDistance, float displacement, MotionEvent ev) { 66 mDistance = Math.max(0f, deltaDistance + mDistance); 67 if (!mIsScrolling) { 68 int originalAction = ev.getAction(); 69 ev.setAction(MotionEvent.ACTION_DOWN); 70 mOverlay.onOverlayMotionEvent(ev, 0); 71 ev.setAction(originalAction); 72 mIsScrolling = true; 73 } 74 mOverlay.onOverlayMotionEvent(ev, mDistance); 75 return mDistance > 0 ? deltaDistance : 0; 76 } 77 78 @Override onAbsorb(int velocity)79 public void onAbsorb(int velocity) { } 80 81 @Override isFinished()82 public boolean isFinished() { 83 return mDistance <= 0; 84 } 85 86 @Override onRelease()87 public void onRelease() { 88 // Fallback implementation, will never actually get called 89 if (BuildConfig.IS_DEBUG_DEVICE) { 90 throw new RuntimeException("Wrong method called"); 91 } 92 MotionEvent mv = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), 93 MotionEvent.ACTION_UP, mDistance, 0, 0); 94 onRelease(mv); 95 mv.recycle(); 96 } 97 98 @Override onFlingVelocity(int velocity)99 public void onFlingVelocity(int velocity) { 100 mOverlay.onFlingVelocity(velocity); 101 } 102 103 @Override onRelease(MotionEvent ev)104 public void onRelease(MotionEvent ev) { 105 if (mIsScrolling) { 106 int originalAction = ev.getAction(); 107 ev.setAction(MotionEvent.ACTION_UP); 108 mOverlay.onOverlayMotionEvent(ev, mDistance); 109 ev.setAction(originalAction); 110 111 mDistance = 0; 112 mIsScrolling = false; 113 } 114 } 115 116 @Override draw(Canvas canvas)117 public boolean draw(Canvas canvas) { 118 return false; 119 } 120 finish()121 public void finish() { 122 mDistance = 0; 123 } 124 } 125