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; |
| 21 | import android.content.res.TypedArray; |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 22 | import android.hardware.input.InputManager; |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 23 | import android.media.AudioManager; |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 24 | import android.os.Bundle; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 25 | import android.os.SystemClock; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 27 | import android.view.HapticFeedbackConstants; |
Jeff Brown | bbda99d | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 28 | import android.view.InputDevice; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 29 | import android.view.KeyCharacterMap; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 30 | import android.view.KeyEvent; |
| 31 | import android.view.MotionEvent; |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 32 | import android.view.SoundEffectConstants; |
Christian Robertson | 4231096 | 2014-09-14 02:14:46 -0400 | [diff] [blame] | 33 | import android.view.View; |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 34 | import android.view.ViewConfiguration; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 35 | import android.view.accessibility.AccessibilityEvent; |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 36 | import android.view.accessibility.AccessibilityNodeInfo; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 37 | import android.widget.ImageView; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 38 | |
| 39 | import com.android.systemui.R; |
| 40 | |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 41 | import static android.view.accessibility.AccessibilityNodeInfo.ACTION_CLICK; |
| 42 | import static android.view.accessibility.AccessibilityNodeInfo.ACTION_LONG_CLICK; |
| 43 | |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 44 | public class KeyButtonView extends ImageView { |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 45 | |
Adrian Roos | cde52d7 | 2014-05-26 22:21:46 +0200 | [diff] [blame] | 46 | private long mDownTime; |
| 47 | private int mCode; |
| 48 | private int mTouchSlop; |
Adrian Roos | cde52d7 | 2014-05-26 22:21:46 +0200 | [diff] [blame] | 49 | private boolean mSupportsLongpress = true; |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 50 | private AudioManager mAudioManager; |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 51 | |
Adrian Roos | cde52d7 | 2014-05-26 22:21:46 +0200 | [diff] [blame] | 52 | private final Runnable mCheckLongPress = new Runnable() { |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 53 | public void run() { |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 54 | if (isPressed()) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 55 | // Log.d("KeyButtonView", "longpressed: " + this); |
Jason Monk | 815e057 | 2014-08-12 17:26:36 -0400 | [diff] [blame] | 56 | if (isLongClickable()) { |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 57 | // Just an old-fashioned ImageView |
| 58 | performLongClick(); |
Jason Monk | 815e057 | 2014-08-12 17:26:36 -0400 | [diff] [blame] | 59 | } else { |
| 60 | sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS); |
| 61 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 62 | } |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | }; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 66 | |
| 67 | public KeyButtonView(Context context, AttributeSet attrs) { |
| 68 | this(context, attrs, 0); |
| 69 | } |
| 70 | |
| 71 | public KeyButtonView(Context context, AttributeSet attrs, int defStyle) { |
| 72 | super(context, attrs); |
| 73 | |
| 74 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KeyButtonView, |
| 75 | defStyle, 0); |
| 76 | |
| 77 | mCode = a.getInteger(R.styleable.KeyButtonView_keyCode, 0); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 78 | |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 79 | mSupportsLongpress = a.getBoolean(R.styleable.KeyButtonView_keyRepeat, true); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 80 | |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 81 | a.recycle(); |
| 82 | |
Joe Onorato | 641bad4 | 2011-01-06 15:54:23 -0800 | [diff] [blame] | 83 | setClickable(true); |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 84 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 85 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 86 | setBackground(new KeyButtonRipple(context, this)); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 89 | @Override |
| 90 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 91 | super.onInitializeAccessibilityNodeInfo(info); |
| 92 | if (mCode != 0) { |
| 93 | info.addAction(new AccessibilityNodeInfo.AccessibilityAction(ACTION_CLICK, null)); |
| 94 | if (mSupportsLongpress) { |
| 95 | info.addAction( |
| 96 | new AccessibilityNodeInfo.AccessibilityAction(ACTION_LONG_CLICK, null)); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
Jorim Jaggi | b9e290c | 2014-10-28 19:04:20 +0100 | [diff] [blame] | 102 | protected void onWindowVisibilityChanged(int visibility) { |
| 103 | super.onWindowVisibilityChanged(visibility); |
| 104 | if (visibility != View.VISIBLE) { |
| 105 | jumpDrawablesToCurrentState(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 110 | public boolean performAccessibilityActionInternal(int action, Bundle arguments) { |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 111 | if (action == ACTION_CLICK && mCode != 0) { |
| 112 | sendEvent(KeyEvent.ACTION_DOWN, 0, SystemClock.uptimeMillis()); |
| 113 | sendEvent(KeyEvent.ACTION_UP, 0); |
| 114 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
| 115 | playSoundEffect(SoundEffectConstants.CLICK); |
| 116 | return true; |
| 117 | } else if (action == ACTION_LONG_CLICK && mCode != 0 && mSupportsLongpress) { |
| 118 | sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS); |
| 119 | sendEvent(KeyEvent.ACTION_UP, 0); |
| 120 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); |
| 121 | return true; |
| 122 | } |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 123 | return super.performAccessibilityActionInternal(action, arguments); |
Jorim Jaggi | 7beadfc | 2014-08-15 18:42:00 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 126 | public boolean onTouchEvent(MotionEvent ev) { |
| 127 | final int action = ev.getAction(); |
| 128 | int x, y; |
| 129 | |
| 130 | switch (action) { |
| 131 | case MotionEvent.ACTION_DOWN: |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 132 | //Log.d("KeyButtonView", "press"); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 133 | mDownTime = SystemClock.uptimeMillis(); |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 134 | setPressed(true); |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 135 | if (mCode != 0) { |
| 136 | sendEvent(KeyEvent.ACTION_DOWN, 0, mDownTime); |
| 137 | } else { |
| 138 | // Provide the same haptic feedback that the system offers for virtual keys. |
| 139 | performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
| 140 | } |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 141 | if (mSupportsLongpress) { |
| 142 | removeCallbacks(mCheckLongPress); |
| 143 | postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout()); |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 144 | } |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 145 | break; |
| 146 | case MotionEvent.ACTION_MOVE: |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 147 | x = (int)ev.getX(); |
| 148 | y = (int)ev.getY(); |
| 149 | setPressed(x >= -mTouchSlop |
| 150 | && x < getWidth() + mTouchSlop |
| 151 | && y >= -mTouchSlop |
| 152 | && y < getHeight() + mTouchSlop); |
Joe Onorato | abb2777 | 2010-11-15 13:30:12 -0800 | [diff] [blame] | 153 | break; |
| 154 | case MotionEvent.ACTION_CANCEL: |
| 155 | setPressed(false); |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 156 | if (mCode != 0) { |
| 157 | sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED); |
| 158 | } |
| 159 | if (mSupportsLongpress) { |
| 160 | removeCallbacks(mCheckLongPress); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 161 | } |
| 162 | break; |
| 163 | case MotionEvent.ACTION_UP: |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 164 | final boolean doIt = isPressed(); |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 165 | setPressed(false); |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 166 | if (mCode != 0) { |
| 167 | if (doIt) { |
| 168 | sendEvent(KeyEvent.ACTION_UP, 0); |
| 169 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
| 170 | playSoundEffect(SoundEffectConstants.CLICK); |
Patrick Dubroy | 5ee1a3d | 2011-03-02 19:11:19 -0800 | [diff] [blame] | 171 | } else { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 172 | sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED); |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 173 | } |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 174 | } else { |
| 175 | // no key code, just a regular ImageView |
| 176 | if (doIt) { |
| 177 | performClick(); |
| 178 | } |
| 179 | } |
| 180 | if (mSupportsLongpress) { |
| 181 | removeCallbacks(mCheckLongPress); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 182 | } |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 189 | public void playSoundEffect(int soundConstant) { |
| 190 | mAudioManager.playSoundEffect(soundConstant, ActivityManager.getCurrentUser()); |
| 191 | }; |
| 192 | |
Jason Monk | 815e057 | 2014-08-12 17:26:36 -0400 | [diff] [blame] | 193 | public void sendEvent(int action, int flags) { |
Jeff Brown | bbda99d | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 194 | sendEvent(action, flags, SystemClock.uptimeMillis()); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Jeff Brown | bbda99d | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 197 | void sendEvent(int action, int flags, long when) { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 198 | final int repeatCount = (flags & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0; |
| 199 | final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, repeatCount, |
| 200 | 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
| 201 | flags | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, |
| 202 | InputDevice.SOURCE_KEYBOARD); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 203 | InputManager.getInstance().injectInputEvent(ev, |
| 204 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 205 | } |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |