1 /*
2  * Copyright (C) 2022 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.taskbar;
18 
19 import android.graphics.Rect;
20 import android.os.Handler;
21 import android.view.MotionEvent;
22 import android.view.TouchDelegate;
23 import android.view.View;
24 
25 import com.android.launcher3.DeviceProfile;
26 import com.android.launcher3.util.TouchController;
27 
28 import java.util.function.Supplier;
29 
30 /**
31  * Extends the Recents touch area during the taskbar to overview animation
32  * to give user some error room when trying to quickly double tap recents button since it moves.
33  *
34  * Listens for icon alignment as our indication for the animation.
35  */
36 public class RecentsHitboxExtender implements TouchController {
37 
38     private static final int RECENTS_HITBOX_TIMEOUT_MS = 500;
39 
40     private View mRecentsButton;
41     private View mRecentsParent;
42     private DeviceProfile mDeviceProfile;
43     private Supplier<float[]> mParentCoordSupplier;
44     private TouchDelegate mRecentsTouchDelegate;
45     /**
46      * Will be true while the animation from taskbar to overview is occurring.
47      * Lifecycle of this variable slightly extends past the animation by
48      * {@link #RECENTS_HITBOX_TIMEOUT_MS}, so can use this variable as a proxy for if
49      * the current hitbox is extended or not.
50      */
51     private boolean mAnimatingFromTaskbarToOverview;
52     private float mLastIconAlignment;
53     private final Rect mRecentsHitBox = new Rect();
54     private boolean mRecentsButtonClicked;
55     private Handler mHandler;
56     private final Runnable mRecentsHitboxResetRunnable = this::reset;
57 
init(View recentsButton, View recentsParent, DeviceProfile deviceProfile, Supplier<float[]> parentCoordSupplier, Handler handler)58     public void init(View recentsButton, View recentsParent, DeviceProfile deviceProfile,
59             Supplier<float[]> parentCoordSupplier, Handler handler) {
60         mRecentsButton = recentsButton;
61         mRecentsParent = recentsParent;
62         mDeviceProfile = deviceProfile;
63         mParentCoordSupplier = parentCoordSupplier;
64         mHandler = handler;
65     }
66 
onRecentsButtonClicked()67     public void onRecentsButtonClicked() {
68         mRecentsButtonClicked = true;
69     }
70 
71     /**
72      * @param progress 0 -> Taskbar, 1 -> Overview
73      */
onAnimationProgressToOverview(float progress)74     public void onAnimationProgressToOverview(float progress) {
75         if (progress == 1 || progress == 0) {
76             // Done w/ animation
77             mLastIconAlignment = progress;
78             if (mAnimatingFromTaskbarToOverview) {
79                 if (progress == 1) {
80                     // Finished animation to workspace, remove the touch delegate shortly
81                     mHandler.postDelayed(mRecentsHitboxResetRunnable, RECENTS_HITBOX_TIMEOUT_MS);
82                     return;
83                 } else {
84                     // Went back to taskbar, reset immediately
85                     mHandler.removeCallbacks(mRecentsHitboxResetRunnable);
86                     reset();
87                 }
88             }
89         }
90 
91         if (mAnimatingFromTaskbarToOverview) {
92             return;
93         }
94 
95         if (progress > 0 && mLastIconAlignment == 0 && mRecentsButtonClicked) {
96             // Starting animation, previously we were showing taskbar
97             mAnimatingFromTaskbarToOverview = true;
98             float[] recentsCoords = mParentCoordSupplier.get();
99             int x = (int) recentsCoords[0];
100             int y = (int) (recentsCoords[1]);
101             // Extend hitbox vertically by the offset amount from mDeviceProfile.getTaskbarOffsetY()
102             mRecentsHitBox.set(x, y,
103                     x + mRecentsButton.getWidth(),
104                     y + mRecentsButton.getHeight() + mDeviceProfile.getTaskbarOffsetY()
105             );
106             mRecentsTouchDelegate = new TouchDelegate(mRecentsHitBox, mRecentsButton);
107             mRecentsParent.setTouchDelegate(mRecentsTouchDelegate);
108         }
109     }
110 
reset()111     private void reset() {
112         mAnimatingFromTaskbarToOverview = false;
113         mRecentsButton.setTouchDelegate(null);
114         mRecentsHitBox.setEmpty();
115         mRecentsButtonClicked = false;
116     }
117 
118     /**
119      * @return {@code true} if the bounds for recents touches are currently extended
120      */
extendedHitboxEnabled()121     public boolean extendedHitboxEnabled() {
122         return mAnimatingFromTaskbarToOverview;
123     }
124 
125     @Override
onControllerTouchEvent(MotionEvent ev)126     public boolean onControllerTouchEvent(MotionEvent ev) {
127         return mRecentsTouchDelegate.onTouchEvent(ev);
128     }
129 
130     @Override
onControllerInterceptTouchEvent(MotionEvent ev)131     public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
132         return mRecentsHitBox.contains((int)ev.getX(), (int)ev.getY());
133     }
134 }
135