Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 17 | package com.android.systemui.statusbar.policy; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 18 | |
Charles Chen | f7150f6 | 2018-10-01 14:42:07 +0800 | [diff] [blame] | 19 | import static android.view.Display.INVALID_DISPLAY; |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 20 | import static android.view.KeyEvent.KEYCODE_UNKNOWN; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 21 | import static android.view.accessibility.AccessibilityNodeInfo.ACTION_CLICK; |
| 22 | import static android.view.accessibility.AccessibilityNodeInfo.ACTION_LONG_CLICK; |
| 23 | |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 24 | import android.app.ActivityManager; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 25 | import android.content.Context; |
Adrian Roos | 3bab515 | 2015-10-16 16:51:04 -0700 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.graphics.drawable.Icon; |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 30 | import android.hardware.input.InputManager; |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 31 | import android.media.AudioManager; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 32 | import android.metrics.LogMaker; |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 33 | import android.os.AsyncTask; |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 34 | import android.os.Bundle; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 35 | import android.os.SystemClock; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Matthew Ng | 0ceca18 | 2019-01-02 17:18:32 -0800 | [diff] [blame] | 37 | import android.util.Log; |
Adrian Roos | 3bab515 | 2015-10-16 16:51:04 -0700 | [diff] [blame] | 38 | import android.util.TypedValue; |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 39 | import android.view.HapticFeedbackConstants; |
Jeff Brown | bbda99d | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 40 | import android.view.InputDevice; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 41 | import android.view.KeyCharacterMap; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 42 | import android.view.KeyEvent; |
| 43 | import android.view.MotionEvent; |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 44 | import android.view.SoundEffectConstants; |
Christian Robertson | 4231096 | 2014-09-14 02:14:46 -0400 | [diff] [blame] | 45 | import android.view.View; |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 46 | import android.view.ViewConfiguration; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 47 | import android.view.accessibility.AccessibilityEvent; |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 48 | import android.view.accessibility.AccessibilityNodeInfo; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 49 | import android.widget.ImageView; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 50 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame^] | 51 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 52 | import com.android.internal.logging.MetricsLogger; |
| 53 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 54 | import com.android.systemui.Dependency; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 55 | import com.android.systemui.R; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame^] | 56 | import com.android.systemui.bubbles.BubbleController; |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 57 | import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider.ButtonInterface; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 58 | import com.android.systemui.recents.OverviewProxyService; |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 59 | import com.android.systemui.shared.system.NavigationBarCompat; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 60 | |
Jason Monk | 197f4db | 2016-08-26 13:28:20 -0400 | [diff] [blame] | 61 | public class KeyButtonView extends ImageView implements ButtonInterface { |
Winson Chung | 58bd439 | 2018-02-08 17:53:26 -0800 | [diff] [blame] | 62 | private static final String TAG = KeyButtonView.class.getSimpleName(); |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 63 | |
Jason Monk | 6c1e6ca | 2017-03-30 14:12:13 -0400 | [diff] [blame] | 64 | private final boolean mPlaySounds; |
Adrian Roos | 3bab515 | 2015-10-16 16:51:04 -0700 | [diff] [blame] | 65 | private int mContentDescriptionRes; |
Adrian Roos | cde52d7 | 2014-05-26 22:21:46 +0200 | [diff] [blame] | 66 | private long mDownTime; |
| 67 | private int mCode; |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 68 | private int mTouchDownX; |
| 69 | private int mTouchDownY; |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 70 | private boolean mIsVertical; |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 71 | private AudioManager mAudioManager; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 72 | private boolean mGestureAborted; |
Jorim Jaggi | 053f218 | 2015-11-24 16:07:55 -0800 | [diff] [blame] | 73 | private boolean mLongClicked; |
Jorim Jaggi | 05bce15 | 2016-08-25 18:14:14 -0700 | [diff] [blame] | 74 | private OnClickListener mOnClickListener; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 75 | private final KeyButtonRipple mRipple; |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 76 | private final OverviewProxyService mOverviewProxyService; |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 77 | private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class); |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame^] | 78 | private final InputManager mInputManager; |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 79 | |
Adrian Roos | cde52d7 | 2014-05-26 22:21:46 +0200 | [diff] [blame] | 80 | private final Runnable mCheckLongPress = new Runnable() { |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 81 | public void run() { |
Matthew Ng | 3aa0dde | 2018-03-13 14:20:54 -0700 | [diff] [blame] | 82 | if (isPressed()) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 83 | // Log.d("KeyButtonView", "longpressed: " + this); |
Jason Monk | 815e057 | 2014-08-12 17:26:36 -0400 | [diff] [blame] | 84 | if (isLongClickable()) { |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 85 | // Just an old-fashioned ImageView |
| 86 | performLongClick(); |
Jorim Jaggi | 053f218 | 2015-11-24 16:07:55 -0800 | [diff] [blame] | 87 | mLongClicked = true; |
Siarhei Vishniakou | d41b41f | 2019-03-19 13:52:50 -0700 | [diff] [blame] | 88 | } else { |
Jason Monk | 815e057 | 2014-08-12 17:26:36 -0400 | [diff] [blame] | 89 | sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS); |
| 90 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); |
Jorim Jaggi | 053f218 | 2015-11-24 16:07:55 -0800 | [diff] [blame] | 91 | mLongClicked = true; |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 92 | } |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | }; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 96 | |
| 97 | public KeyButtonView(Context context, AttributeSet attrs) { |
| 98 | this(context, attrs, 0); |
| 99 | } |
| 100 | |
| 101 | public KeyButtonView(Context context, AttributeSet attrs, int defStyle) { |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame^] | 102 | this(context, attrs, defStyle, InputManager.getInstance()); |
| 103 | } |
| 104 | |
| 105 | @VisibleForTesting |
| 106 | public KeyButtonView(Context context, AttributeSet attrs, int defStyle, InputManager manager) { |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 107 | super(context, attrs); |
| 108 | |
| 109 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KeyButtonView, |
| 110 | defStyle, 0); |
| 111 | |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 112 | mCode = a.getInteger(R.styleable.KeyButtonView_keyCode, KEYCODE_UNKNOWN); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 113 | |
Jason Monk | 6c1e6ca | 2017-03-30 14:12:13 -0400 | [diff] [blame] | 114 | mPlaySounds = a.getBoolean(R.styleable.KeyButtonView_playSound, true); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 115 | |
Adrian Roos | 3bab515 | 2015-10-16 16:51:04 -0700 | [diff] [blame] | 116 | TypedValue value = new TypedValue(); |
| 117 | if (a.getValue(R.styleable.KeyButtonView_android_contentDescription, value)) { |
| 118 | mContentDescriptionRes = value.resourceId; |
| 119 | } |
| 120 | |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 121 | a.recycle(); |
| 122 | |
Joe Onorato | 641bad4 | 2011-01-06 15:54:23 -0800 | [diff] [blame] | 123 | setClickable(true); |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 124 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 125 | |
| 126 | mRipple = new KeyButtonRipple(context, this); |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 127 | mOverviewProxyService = Dependency.get(OverviewProxyService.class); |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame^] | 128 | mInputManager = manager; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 129 | setBackground(mRipple); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 130 | forceHasOverlappingRendering(false); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 133 | @Override |
| 134 | public boolean isClickable() { |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 135 | return mCode != KEYCODE_UNKNOWN || super.isClickable(); |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 138 | public void setCode(int code) { |
| 139 | mCode = code; |
| 140 | } |
| 141 | |
Jorim Jaggi | 05bce15 | 2016-08-25 18:14:14 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public void setOnClickListener(OnClickListener onClickListener) { |
| 144 | super.setOnClickListener(onClickListener); |
| 145 | mOnClickListener = onClickListener; |
| 146 | } |
| 147 | |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 148 | public void loadAsync(Icon icon) { |
| 149 | new AsyncTask<Icon, Void, Drawable>() { |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 150 | @Override |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 151 | protected Drawable doInBackground(Icon... params) { |
| 152 | return params[0].loadDrawable(mContext); |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | protected void onPostExecute(Drawable drawable) { |
| 157 | setImageDrawable(drawable); |
| 158 | } |
Jason Monk | ea05f87 | 2017-01-24 19:43:36 -0500 | [diff] [blame] | 159 | }.execute(icon); |
Jason Monk | 8457ad8 | 2016-01-24 10:15:55 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 162 | @Override |
Adrian Roos | 3bab515 | 2015-10-16 16:51:04 -0700 | [diff] [blame] | 163 | protected void onConfigurationChanged(Configuration newConfig) { |
| 164 | super.onConfigurationChanged(newConfig); |
| 165 | |
| 166 | if (mContentDescriptionRes != 0) { |
| 167 | setContentDescription(mContext.getString(mContentDescriptionRes)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 172 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 173 | super.onInitializeAccessibilityNodeInfo(info); |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 174 | if (mCode != KEYCODE_UNKNOWN) { |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 175 | info.addAction(new AccessibilityNodeInfo.AccessibilityAction(ACTION_CLICK, null)); |
Siarhei Vishniakou | d41b41f | 2019-03-19 13:52:50 -0700 | [diff] [blame] | 176 | if (isLongClickable()) { |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 177 | info.addAction( |
| 178 | new AccessibilityNodeInfo.AccessibilityAction(ACTION_LONG_CLICK, null)); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | @Override |
Jorim Jaggi | b9e290c | 2014-10-28 19:04:20 +0100 | [diff] [blame] | 184 | protected void onWindowVisibilityChanged(int visibility) { |
| 185 | super.onWindowVisibilityChanged(visibility); |
| 186 | if (visibility != View.VISIBLE) { |
| 187 | jumpDrawablesToCurrentState(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 192 | public boolean performAccessibilityActionInternal(int action, Bundle arguments) { |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 193 | if (action == ACTION_CLICK && mCode != KEYCODE_UNKNOWN) { |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 194 | sendEvent(KeyEvent.ACTION_DOWN, 0, SystemClock.uptimeMillis()); |
| 195 | sendEvent(KeyEvent.ACTION_UP, 0); |
| 196 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
| 197 | playSoundEffect(SoundEffectConstants.CLICK); |
| 198 | return true; |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 199 | } else if (action == ACTION_LONG_CLICK && mCode != KEYCODE_UNKNOWN) { |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 200 | sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS); |
| 201 | sendEvent(KeyEvent.ACTION_UP, 0); |
| 202 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); |
| 203 | return true; |
| 204 | } |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 205 | return super.performAccessibilityActionInternal(action, arguments); |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Siarhei Vishniakou | 311748a | 2019-03-15 17:08:44 -0700 | [diff] [blame] | 208 | @Override |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 209 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 210 | final boolean showSwipeUI = mOverviewProxyService.shouldShowSwipeUpUI(); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 211 | final int action = ev.getAction(); |
| 212 | int x, y; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 213 | if (action == MotionEvent.ACTION_DOWN) { |
| 214 | mGestureAborted = false; |
| 215 | } |
| 216 | if (mGestureAborted) { |
Matthew Ng | 3aa0dde | 2018-03-13 14:20:54 -0700 | [diff] [blame] | 217 | setPressed(false); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 218 | return false; |
| 219 | } |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 220 | |
| 221 | switch (action) { |
| 222 | case MotionEvent.ACTION_DOWN: |
| 223 | mDownTime = SystemClock.uptimeMillis(); |
Jorim Jaggi | 053f218 | 2015-11-24 16:07:55 -0800 | [diff] [blame] | 224 | mLongClicked = false; |
Matthew Ng | 3aa0dde | 2018-03-13 14:20:54 -0700 | [diff] [blame] | 225 | setPressed(true); |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 226 | |
| 227 | // Use raw X and Y to detect gestures in case a parent changes the x and y values |
| 228 | mTouchDownX = (int) ev.getRawX(); |
| 229 | mTouchDownY = (int) ev.getRawY(); |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 230 | if (mCode != KEYCODE_UNKNOWN) { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 231 | sendEvent(KeyEvent.ACTION_DOWN, 0, mDownTime); |
| 232 | } else { |
| 233 | // Provide the same haptic feedback that the system offers for virtual keys. |
| 234 | performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
| 235 | } |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 236 | if (!showSwipeUI) { |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 237 | playSoundEffect(SoundEffectConstants.CLICK); |
| 238 | } |
Jorim Jaggi | 7e6571f | 2015-06-25 11:52:48 -0700 | [diff] [blame] | 239 | removeCallbacks(mCheckLongPress); |
| 240 | postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout()); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 241 | break; |
| 242 | case MotionEvent.ACTION_MOVE: |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 243 | x = (int)ev.getRawX(); |
| 244 | y = (int)ev.getRawY(); |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 245 | |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 246 | boolean exceededTouchSlopX = Math.abs(x - mTouchDownX) > (mIsVertical |
| 247 | ? NavigationBarCompat.getQuickScrubTouchSlopPx() |
| 248 | : NavigationBarCompat.getQuickStepTouchSlopPx()); |
| 249 | boolean exceededTouchSlopY = Math.abs(y - mTouchDownY) > (mIsVertical |
| 250 | ? NavigationBarCompat.getQuickStepTouchSlopPx() |
| 251 | : NavigationBarCompat.getQuickScrubTouchSlopPx()); |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 252 | if (exceededTouchSlopX || exceededTouchSlopY) { |
Matthew Ng | 2fb239c | 2018-02-28 11:55:52 -0800 | [diff] [blame] | 253 | // When quick step is enabled, prevent animating the ripple triggered by |
| 254 | // setPressed and decide to run it on touch up |
Matthew Ng | 3aa0dde | 2018-03-13 14:20:54 -0700 | [diff] [blame] | 255 | setPressed(false); |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 256 | removeCallbacks(mCheckLongPress); |
| 257 | } |
Joe Onorato | abb2777 | 2010-11-15 13:30:12 -0800 | [diff] [blame] | 258 | break; |
| 259 | case MotionEvent.ACTION_CANCEL: |
| 260 | setPressed(false); |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 261 | if (mCode != KEYCODE_UNKNOWN) { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 262 | sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED); |
| 263 | } |
Jorim Jaggi | 7e6571f | 2015-06-25 11:52:48 -0700 | [diff] [blame] | 264 | removeCallbacks(mCheckLongPress); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 265 | break; |
| 266 | case MotionEvent.ACTION_UP: |
Matthew Ng | 3aa0dde | 2018-03-13 14:20:54 -0700 | [diff] [blame] | 267 | final boolean doIt = isPressed() && !mLongClicked; |
| 268 | setPressed(false); |
Matthew Ng | 5c0592e | 2018-03-08 14:51:36 -0800 | [diff] [blame] | 269 | final boolean doHapticFeedback = (SystemClock.uptimeMillis() - mDownTime) > 150; |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 270 | if (showSwipeUI) { |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 271 | if (doIt) { |
Matthew Ng | 0e1e0f6 | 2018-03-30 12:09:31 -0700 | [diff] [blame] | 272 | // Apply haptic feedback on touch up since there is none on touch down |
| 273 | performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 274 | playSoundEffect(SoundEffectConstants.CLICK); |
| 275 | } |
Matthew Ng | 5c0592e | 2018-03-08 14:51:36 -0800 | [diff] [blame] | 276 | } else if (doHapticFeedback && !mLongClicked) { |
Matthew Ng | 83ef2f8 | 2018-01-29 16:11:40 -0800 | [diff] [blame] | 277 | // Always send a release ourselves because it doesn't seem to be sent elsewhere |
| 278 | // and it feels weird to sometimes get a release haptic and other times not. |
Jason Monk | 058cbcc | 2017-07-06 15:38:19 -0400 | [diff] [blame] | 279 | performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY_RELEASE); |
| 280 | } |
Siarhei Vishniakou | 9f70d02 | 2019-03-15 17:03:22 -0700 | [diff] [blame] | 281 | if (mCode != KEYCODE_UNKNOWN) { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 282 | if (doIt) { |
| 283 | sendEvent(KeyEvent.ACTION_UP, 0); |
| 284 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
Patrick Dubroy | 5ee1a3d | 2011-03-02 19:11:19 -0800 | [diff] [blame] | 285 | } else { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 286 | sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED); |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 287 | } |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 288 | } else { |
| 289 | // no key code, just a regular ImageView |
Jorim Jaggi | 05bce15 | 2016-08-25 18:14:14 -0700 | [diff] [blame] | 290 | if (doIt && mOnClickListener != null) { |
| 291 | mOnClickListener.onClick(this); |
| 292 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
Jorim Jaggi | 7e6571f | 2015-06-25 11:52:48 -0700 | [diff] [blame] | 295 | removeCallbacks(mCheckLongPress); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | |
| 299 | return true; |
| 300 | } |
| 301 | |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 302 | public void playSoundEffect(int soundConstant) { |
Jason Monk | 6c1e6ca | 2017-03-30 14:12:13 -0400 | [diff] [blame] | 303 | if (!mPlaySounds) return; |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 304 | mAudioManager.playSoundEffect(soundConstant, ActivityManager.getCurrentUser()); |
Jason Monk | 6c1e6ca | 2017-03-30 14:12:13 -0400 | [diff] [blame] | 305 | } |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 306 | |
Jason Monk | 815e057 | 2014-08-12 17:26:36 -0400 | [diff] [blame] | 307 | public void sendEvent(int action, int flags) { |
Jeff Brown | bbda99d | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 308 | sendEvent(action, flags, SystemClock.uptimeMillis()); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Siarhei Vishniakou | 311748a | 2019-03-15 17:08:44 -0700 | [diff] [blame] | 311 | private void sendEvent(int action, int flags, long when) { |
Jason Monk | cbf591b | 2017-03-31 15:42:27 -0400 | [diff] [blame] | 312 | mMetricsLogger.write(new LogMaker(MetricsEvent.ACTION_NAV_BUTTON_EVENT) |
| 313 | .setType(MetricsEvent.TYPE_ACTION) |
| 314 | .setSubtype(mCode) |
| 315 | .addTaggedData(MetricsEvent.FIELD_NAV_ACTION, action) |
| 316 | .addTaggedData(MetricsEvent.FIELD_FLAGS, flags)); |
Matthew Ng | 0ceca18 | 2019-01-02 17:18:32 -0800 | [diff] [blame] | 317 | // TODO(b/122195391): Added logs to make sure sysui is sending back button events |
| 318 | if (mCode == KeyEvent.KEYCODE_BACK && flags != KeyEvent.FLAG_LONG_PRESS) { |
| 319 | Log.i(TAG, "Back button event: " + KeyEvent.actionToString(action)); |
Winson Chung | 04ff8bda | 2019-04-02 15:08:59 -0700 | [diff] [blame] | 320 | if (action == MotionEvent.ACTION_UP) { |
| 321 | mOverviewProxyService.notifyBackAction((flags & KeyEvent.FLAG_CANCELED) == 0, |
| 322 | -1, -1, true /* isButton */, false /* gestureSwipeLeft */); |
| 323 | } |
Matthew Ng | 0ceca18 | 2019-01-02 17:18:32 -0800 | [diff] [blame] | 324 | } |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 325 | final int repeatCount = (flags & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0; |
| 326 | final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, repeatCount, |
| 327 | 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
| 328 | flags | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, |
| 329 | InputDevice.SOURCE_KEYBOARD); |
Charles Chen | f7150f6 | 2018-10-01 14:42:07 +0800 | [diff] [blame] | 330 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame^] | 331 | int displayId = INVALID_DISPLAY; |
| 332 | |
| 333 | // Make KeyEvent work on multi-display environment |
| 334 | if (getDisplay() != null) { |
| 335 | displayId = getDisplay().getDisplayId(); |
Charles Chen | f7150f6 | 2018-10-01 14:42:07 +0800 | [diff] [blame] | 336 | } |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame^] | 337 | // Bubble controller will give us a valid display id if it should get the back event |
| 338 | BubbleController bubbleController = Dependency.get(BubbleController.class); |
| 339 | int bubbleDisplayId = bubbleController.getExpandedDisplayId(mContext); |
| 340 | if (mCode == KeyEvent.KEYCODE_BACK && bubbleDisplayId != INVALID_DISPLAY) { |
| 341 | displayId = bubbleDisplayId; |
| 342 | } |
| 343 | if (displayId != INVALID_DISPLAY) { |
| 344 | ev.setDisplayId(displayId); |
| 345 | } |
| 346 | mInputManager.injectInputEvent(ev, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 347 | } |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 348 | |
Annie Chin | 1ea4935 | 2016-05-27 15:23:35 -0700 | [diff] [blame] | 349 | @Override |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 350 | public void abortCurrentGesture() { |
| 351 | setPressed(false); |
Matthew Ng | 3aa0dde | 2018-03-13 14:20:54 -0700 | [diff] [blame] | 352 | mRipple.abortDelayedRipple(); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 353 | mGestureAborted = true; |
| 354 | } |
Annie Chin | 1ea4935 | 2016-05-27 15:23:35 -0700 | [diff] [blame] | 355 | |
| 356 | @Override |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 357 | public void setDarkIntensity(float darkIntensity) { |
| 358 | Drawable drawable = getDrawable(); |
| 359 | if (drawable != null) { |
| 360 | ((KeyButtonDrawable) getDrawable()).setDarkIntensity(darkIntensity); |
Annie Chin | 1ea4935 | 2016-05-27 15:23:35 -0700 | [diff] [blame] | 361 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 362 | // Since we reuse the same drawable for multiple views, we need to invalidate the view |
| 363 | // manually. |
| 364 | invalidate(); |
| 365 | } |
| 366 | mRipple.setDarkIntensity(darkIntensity); |
Annie Chin | 1ea4935 | 2016-05-27 15:23:35 -0700 | [diff] [blame] | 367 | } |
Annie Chin | 6fc4600 | 2016-07-07 18:38:12 -0700 | [diff] [blame] | 368 | |
| 369 | @Override |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 370 | public void setDelayTouchFeedback(boolean shouldDelay) { |
Matthew Ng | 3aa0dde | 2018-03-13 14:20:54 -0700 | [diff] [blame] | 371 | mRipple.setDelayTouchFeedback(shouldDelay); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
Xiaohui Chen | 40e978e | 2016-11-29 15:10:04 -0800 | [diff] [blame] | 375 | public void setVertical(boolean vertical) { |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 376 | mIsVertical = vertical; |
Annie Chin | 6fc4600 | 2016-07-07 18:38:12 -0700 | [diff] [blame] | 377 | } |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | |