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 android.inputmethodservice.navigationbar;
18 
19 import static android.inputmethodservice.navigationbar.NavigationBarConstants.NAVIGATION_BAR_DEADZONE_DECAY;
20 import static android.inputmethodservice.navigationbar.NavigationBarConstants.NAVIGATION_BAR_DEADZONE_HOLD;
21 import static android.inputmethodservice.navigationbar.NavigationBarConstants.NAVIGATION_BAR_DEADZONE_SIZE;
22 import static android.inputmethodservice.navigationbar.NavigationBarConstants.NAVIGATION_BAR_DEADZONE_SIZE_MAX;
23 import static android.inputmethodservice.navigationbar.NavigationBarUtils.dpToPx;
24 
25 import android.animation.ObjectAnimator;
26 import android.content.res.Configuration;
27 import android.content.res.Resources;
28 import android.graphics.Canvas;
29 import android.os.SystemClock;
30 import android.util.FloatProperty;
31 import android.util.Log;
32 import android.view.MotionEvent;
33 import android.view.Surface;
34 
35 /**
36  * The "dead zone" consumes unintentional taps along the top edge of the navigation bar.
37  * When users are typing quickly on an IME they may attempt to hit the space bar, overshoot, and
38  * accidentally hit the home button. The DeadZone expands temporarily after each tap in the UI
39  * outside the navigation bar (since this is when accidental taps are more likely), then contracts
40  * back over time (since a later tap might be intended for the top of the bar).
41  */
42 final class DeadZone {
43     public static final String TAG = "DeadZone";
44 
45     public static final boolean DEBUG = false;
46     public static final int HORIZONTAL = 0;  // Consume taps along the top edge.
47     public static final int VERTICAL = 1;  // Consume taps along the left edge.
48 
49     private static final boolean CHATTY = true; // print to logcat when we eat a click
50 
51     private static final FloatProperty<DeadZone> FLASH_PROPERTY =
52             new FloatProperty<DeadZone>("DeadZoneFlash") {
53         @Override
54         public void setValue(DeadZone object, float value) {
55             object.setFlash(value);
56         }
57 
58         @Override
59         public Float get(DeadZone object) {
60             return object.getFlash();
61         }
62     };
63 
64     private final NavigationBarView mNavigationBarView;
65 
66     private boolean mShouldFlash;
67     private float mFlashFrac = 0f;
68 
69     private int mSizeMax;
70     private int mSizeMin;
71     // Upon activity elsewhere in the UI, the dead zone will hold steady for
72     // mHold ms, then move back over the course of mDecay ms
73     private int mHold, mDecay;
74     private boolean mVertical;
75     private long mLastPokeTime;
76     private int mDisplayRotation;
77 
78     private final Runnable mDebugFlash = new Runnable() {
79         @Override
80         public void run() {
81             ObjectAnimator.ofFloat(DeadZone.this, FLASH_PROPERTY, 1f, 0f).setDuration(150).start();
82         }
83     };
84 
DeadZone(NavigationBarView view)85     DeadZone(NavigationBarView view) {
86         mNavigationBarView = view;
87         onConfigurationChanged(Surface.ROTATION_0);
88     }
89 
lerp(float a, float b, float f)90     static float lerp(float a, float b, float f) {
91         return (b - a) * f + a;
92     }
93 
getSize(long now)94     private float getSize(long now) {
95         if (mSizeMax == 0) {
96             return 0;
97         }
98         long dt = (now - mLastPokeTime);
99         if (dt > mHold + mDecay) {
100             return mSizeMin;
101         } else if (dt < mHold) {
102             return mSizeMax;
103         }
104         return (int) lerp(mSizeMax, mSizeMin, (float) (dt - mHold) / mDecay);
105     }
106 
setFlashOnTouchCapture(boolean dbg)107     public void setFlashOnTouchCapture(boolean dbg) {
108         mShouldFlash = dbg;
109         mFlashFrac = 0f;
110         mNavigationBarView.postInvalidate();
111     }
112 
onConfigurationChanged(int rotation)113     public void onConfigurationChanged(int rotation) {
114         mDisplayRotation = rotation;
115 
116         final Resources res = mNavigationBarView.getResources();
117         mHold = NAVIGATION_BAR_DEADZONE_HOLD;
118         mDecay = NAVIGATION_BAR_DEADZONE_DECAY;
119 
120         mSizeMin = dpToPx(NAVIGATION_BAR_DEADZONE_SIZE, res);
121         mSizeMax = dpToPx(NAVIGATION_BAR_DEADZONE_SIZE_MAX, res);
122         mVertical = (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
123 
124         if (DEBUG) {
125             Log.v(TAG, this + " size=[" + mSizeMin + "-" + mSizeMax + "] hold=" + mHold
126                     + (mVertical ? " vertical" : " horizontal"));
127         }
128         setFlashOnTouchCapture(false);   // hard-coded from "bool/config_dead_zone_flash"
129     }
130 
131     // I made you a touch event...
onTouchEvent(MotionEvent event)132     public boolean onTouchEvent(MotionEvent event) {
133         if (DEBUG) {
134             Log.v(TAG, this + " onTouch: " + MotionEvent.actionToString(event.getAction()));
135         }
136 
137         // Don't consume events for high precision pointing devices. For this purpose a stylus is
138         // considered low precision (like a finger), so its events may be consumed.
139         if (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE) {
140             return false;
141         }
142 
143         final int action = event.getAction();
144         if (action == MotionEvent.ACTION_OUTSIDE) {
145             poke(event);
146             return true;
147         } else if (action == MotionEvent.ACTION_DOWN) {
148             if (DEBUG) {
149                 Log.v(TAG, this + " ACTION_DOWN: " + event.getX() + "," + event.getY());
150             }
151             //TODO(b/215443343): call mNavBarController.touchAutoDim(mDisplayId); here
152             int size = (int) getSize(event.getEventTime());
153             // In the vertical orientation consume taps along the left edge.
154             // In horizontal orientation consume taps along the top edge.
155             final boolean consumeEvent;
156             if (mVertical) {
157                 if (mDisplayRotation == Surface.ROTATION_270) {
158                     consumeEvent = event.getX() > mNavigationBarView.getWidth() - size;
159                 } else {
160                     consumeEvent = event.getX() < size;
161                 }
162             } else {
163                 consumeEvent = event.getY() < size;
164             }
165             if (consumeEvent) {
166                 if (CHATTY) {
167                     Log.v(TAG, "consuming errant click: (" + event.getX() + ","
168                             + event.getY() + ")");
169                 }
170                 if (mShouldFlash) {
171                     mNavigationBarView.post(mDebugFlash);
172                     mNavigationBarView.postInvalidate();
173                 }
174                 return true; // ...but I eated it
175             }
176         }
177         return false;
178     }
179 
180     private void poke(MotionEvent event) {
181         mLastPokeTime = event.getEventTime();
182         if (DEBUG) {
183             Log.v(TAG, "poked! size=" + getSize(mLastPokeTime));
184         }
185         if (mShouldFlash) mNavigationBarView.postInvalidate();
186     }
187 
188     public void setFlash(float f) {
189         mFlashFrac = f;
190         mNavigationBarView.postInvalidate();
191     }
192 
193     public float getFlash() {
194         return mFlashFrac;
195     }
196 
197     public void onDraw(Canvas can) {
198         if (!mShouldFlash || mFlashFrac <= 0f) {
199             return;
200         }
201 
202         final int size = (int) getSize(SystemClock.uptimeMillis());
203         if (mVertical) {
204             if (mDisplayRotation == Surface.ROTATION_270) {
205                 can.clipRect(can.getWidth() - size, 0, can.getWidth(), can.getHeight());
206             } else {
207                 can.clipRect(0, 0, size, can.getHeight());
208             }
209         } else {
210             can.clipRect(0, 0, can.getWidth(), size);
211         }
212 
213         final float frac = DEBUG ? (mFlashFrac - 0.5f) + 0.5f : mFlashFrac;
214         can.drawARGB((int) (frac * 0xFF), 0xDD, 0xEE, 0xAA);
215 
216         if (DEBUG && size > mSizeMin) {
217             // Very aggressive redrawing here, for debugging only
218             mNavigationBarView.postInvalidateDelayed(100);
219         }
220     }
221 }
222