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