Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.systemui.statusbar.policy; |
| 18 | |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 19 | import android.animation.ObjectAnimator; |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 20 | import android.content.Context; |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 21 | import android.content.res.TypedArray; |
| 22 | import android.graphics.Canvas; |
| 23 | import android.os.SystemClock; |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Daniel Sandler | 29787f6e | 2013-11-12 20:33:34 -0500 | [diff] [blame] | 25 | import android.util.Slog; |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Jorim Jaggi | 7aaa3d7 | 2016-11-28 14:03:11 +0100 | [diff] [blame] | 27 | import android.view.Surface; |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 28 | import android.view.View; |
| 29 | |
| 30 | import com.android.systemui.R; |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 31 | import com.android.systemui.SysUiServiceProvider; |
| 32 | import com.android.systemui.statusbar.phone.StatusBar; |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 33 | |
James Cook | f8da208 | 2015-04-09 10:38:05 -0700 | [diff] [blame] | 34 | /** |
| 35 | * The "dead zone" consumes unintentional taps along the top edge of the navigation bar. |
| 36 | * When users are typing quickly on an IME they may attempt to hit the space bar, overshoot, and |
| 37 | * accidentally hit the home button. The DeadZone expands temporarily after each tap in the UI |
| 38 | * outside the navigation bar (since this is when accidental taps are more likely), then contracts |
| 39 | * back over time (since a later tap might be intended for the top of the bar). |
| 40 | */ |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 41 | public class DeadZone extends View { |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 42 | public static final String TAG = "DeadZone"; |
| 43 | |
| 44 | public static final boolean DEBUG = false; |
James Cook | f8da208 | 2015-04-09 10:38:05 -0700 | [diff] [blame] | 45 | public static final int HORIZONTAL = 0; // Consume taps along the top edge. |
| 46 | public static final int VERTICAL = 1; // Consume taps along the left edge. |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 47 | |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 48 | private static final boolean CHATTY = true; // print to logcat when we eat a click |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 49 | private final StatusBar mStatusBar; |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 50 | |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 51 | private boolean mShouldFlash; |
| 52 | private float mFlashFrac = 0f; |
| 53 | |
| 54 | private int mSizeMax; |
| 55 | private int mSizeMin; |
| 56 | // Upon activity elsewhere in the UI, the dead zone will hold steady for |
| 57 | // mHold ms, then move back over the course of mDecay ms |
| 58 | private int mHold, mDecay; |
| 59 | private boolean mVertical; |
| 60 | private long mLastPokeTime; |
Jorim Jaggi | 7aaa3d7 | 2016-11-28 14:03:11 +0100 | [diff] [blame] | 61 | private int mDisplayRotation; |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 62 | |
| 63 | private final Runnable mDebugFlash = new Runnable() { |
| 64 | @Override |
| 65 | public void run() { |
| 66 | ObjectAnimator.ofFloat(DeadZone.this, "flash", 1f, 0f).setDuration(150).start(); |
| 67 | } |
| 68 | }; |
| 69 | |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 70 | public DeadZone(Context context, AttributeSet attrs) { |
| 71 | this(context, attrs, 0); |
| 72 | } |
| 73 | |
| 74 | public DeadZone(Context context, AttributeSet attrs, int defStyle) { |
| 75 | super(context, attrs); |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 76 | |
| 77 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeadZone, |
| 78 | defStyle, 0); |
| 79 | |
| 80 | mHold = a.getInteger(R.styleable.DeadZone_holdTime, 0); |
| 81 | mDecay = a.getInteger(R.styleable.DeadZone_decayTime, 0); |
| 82 | |
| 83 | mSizeMin = a.getDimensionPixelSize(R.styleable.DeadZone_minSize, 0); |
| 84 | mSizeMax = a.getDimensionPixelSize(R.styleable.DeadZone_maxSize, 0); |
| 85 | |
| 86 | int index = a.getInt(R.styleable.DeadZone_orientation, -1); |
| 87 | mVertical = (index == VERTICAL); |
| 88 | |
| 89 | if (DEBUG) |
Daniel Sandler | 29787f6e | 2013-11-12 20:33:34 -0500 | [diff] [blame] | 90 | Slog.v(TAG, this + " size=[" + mSizeMin + "-" + mSizeMax + "] hold=" + mHold |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 91 | + (mVertical ? " vertical" : " horizontal")); |
| 92 | |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 93 | setFlashOnTouchCapture(context.getResources().getBoolean(R.bool.config_dead_zone_flash)); |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 94 | mStatusBar = SysUiServiceProvider.getComponent(context, StatusBar.class); |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static float lerp(float a, float b, float f) { |
| 98 | return (b - a) * f + a; |
| 99 | } |
| 100 | |
| 101 | private float getSize(long now) { |
| 102 | if (mSizeMax == 0) |
| 103 | return 0; |
| 104 | long dt = (now - mLastPokeTime); |
| 105 | if (dt > mHold + mDecay) |
| 106 | return mSizeMin; |
| 107 | if (dt < mHold) |
| 108 | return mSizeMax; |
| 109 | return (int) lerp(mSizeMax, mSizeMin, (float) (dt - mHold) / mDecay); |
| 110 | } |
| 111 | |
| 112 | public void setFlashOnTouchCapture(boolean dbg) { |
| 113 | mShouldFlash = dbg; |
| 114 | mFlashFrac = 0f; |
| 115 | postInvalidate(); |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 118 | // I made you a touch event... |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 119 | @Override |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 120 | public boolean onTouchEvent(MotionEvent event) { |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 121 | if (DEBUG) { |
Daniel Sandler | 29787f6e | 2013-11-12 20:33:34 -0500 | [diff] [blame] | 122 | Slog.v(TAG, this + " onTouch: " + MotionEvent.actionToString(event.getAction())); |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 123 | } |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 124 | |
James Cook | f8da208 | 2015-04-09 10:38:05 -0700 | [diff] [blame] | 125 | // Don't consume events for high precision pointing devices. For this purpose a stylus is |
| 126 | // considered low precision (like a finger), so its events may be consumed. |
| 127 | if (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE) { |
| 128 | return false; |
| 129 | } |
| 130 | |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 131 | final int action = event.getAction(); |
| 132 | if (action == MotionEvent.ACTION_OUTSIDE) { |
| 133 | poke(event); |
Siarhei Vishniakou | 6ad0e39 | 2017-06-02 17:20:34 -0700 | [diff] [blame] | 134 | return true; |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 135 | } else if (action == MotionEvent.ACTION_DOWN) { |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 136 | if (DEBUG) { |
Daniel Sandler | 29787f6e | 2013-11-12 20:33:34 -0500 | [diff] [blame] | 137 | Slog.v(TAG, this + " ACTION_DOWN: " + event.getX() + "," + event.getY()); |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 138 | } |
Jason Monk | c2beef5 | 2017-08-29 18:58:00 -0400 | [diff] [blame] | 139 | if (mStatusBar != null) mStatusBar.touchAutoDim(); |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 140 | int size = (int) getSize(event.getEventTime()); |
James Cook | f8da208 | 2015-04-09 10:38:05 -0700 | [diff] [blame] | 141 | // In the vertical orientation consume taps along the left edge. |
| 142 | // In horizontal orientation consume taps along the top edge. |
Jorim Jaggi | 7aaa3d7 | 2016-11-28 14:03:11 +0100 | [diff] [blame] | 143 | final boolean consumeEvent; |
| 144 | if (mVertical) { |
| 145 | if (mDisplayRotation == Surface.ROTATION_270) { |
| 146 | consumeEvent = event.getX() > getWidth() - size; |
| 147 | } else { |
| 148 | consumeEvent = event.getX() < size; |
| 149 | } |
| 150 | } else { |
| 151 | consumeEvent = event.getY() < size; |
| 152 | } |
James Cook | f8da208 | 2015-04-09 10:38:05 -0700 | [diff] [blame] | 153 | if (consumeEvent) { |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 154 | if (CHATTY) { |
Daniel Sandler | 29787f6e | 2013-11-12 20:33:34 -0500 | [diff] [blame] | 155 | Slog.v(TAG, "consuming errant click: (" + event.getX() + "," + event.getY() + ")"); |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 156 | } |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 157 | if (mShouldFlash) { |
| 158 | post(mDebugFlash); |
| 159 | postInvalidate(); |
| 160 | } |
Daniel Sandler | 25bb8ee | 2012-08-30 17:10:50 -0400 | [diff] [blame] | 161 | return true; // ...but I eated it |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
Siarhei Vishniakou | 6ad0e39 | 2017-06-02 17:20:34 -0700 | [diff] [blame] | 167 | private void poke(MotionEvent event) { |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 168 | mLastPokeTime = event.getEventTime(); |
| 169 | if (DEBUG) |
Daniel Sandler | 29787f6e | 2013-11-12 20:33:34 -0500 | [diff] [blame] | 170 | Slog.v(TAG, "poked! size=" + getSize(mLastPokeTime)); |
Daniel Sandler | 4f6ec7f | 2013-01-17 16:02:36 -0500 | [diff] [blame] | 171 | if (mShouldFlash) postInvalidate(); |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | public void setFlash(float f) { |
| 175 | mFlashFrac = f; |
| 176 | postInvalidate(); |
| 177 | } |
| 178 | |
| 179 | public float getFlash() { |
| 180 | return mFlashFrac; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void onDraw(Canvas can) { |
| 185 | if (!mShouldFlash || mFlashFrac <= 0f) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | final int size = (int) getSize(SystemClock.uptimeMillis()); |
Jorim Jaggi | 7aaa3d7 | 2016-11-28 14:03:11 +0100 | [diff] [blame] | 190 | if (mVertical) { |
| 191 | if (mDisplayRotation == Surface.ROTATION_270) { |
| 192 | can.clipRect(can.getWidth() - size, 0, can.getWidth(), can.getHeight()); |
| 193 | } else { |
| 194 | can.clipRect(0, 0, size, can.getHeight()); |
| 195 | } |
| 196 | } else { |
| 197 | can.clipRect(0, 0, can.getWidth(), size); |
| 198 | } |
| 199 | |
Daniel Sandler | c26185b | 2012-08-29 15:49:53 -0400 | [diff] [blame] | 200 | final float frac = DEBUG ? (mFlashFrac - 0.5f) + 0.5f : mFlashFrac; |
| 201 | can.drawARGB((int) (frac * 0xFF), 0xDD, 0xEE, 0xAA); |
| 202 | |
| 203 | if (DEBUG && size > mSizeMin) |
| 204 | // crazy aggressive redrawing here, for debugging only |
| 205 | postInvalidateDelayed(100); |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 206 | } |
Jorim Jaggi | 7aaa3d7 | 2016-11-28 14:03:11 +0100 | [diff] [blame] | 207 | |
| 208 | public void setDisplayRotation(int rotation) { |
| 209 | mDisplayRotation = rotation; |
| 210 | } |
Daniel Sandler | e97a776 | 2012-03-19 22:56:42 -0400 | [diff] [blame] | 211 | } |