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 | |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 19 | import android.animation.AnimatorSet; |
| 20 | import android.animation.ObjectAnimator; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.TypedArray; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.RectF; |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 26 | import android.hardware.input.InputManager; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 27 | import android.os.RemoteException; |
| 28 | import android.os.SystemClock; |
| 29 | import android.os.ServiceManager; |
| 30 | import android.util.AttributeSet; |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 31 | import android.view.accessibility.AccessibilityEvent; |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 32 | import android.view.HapticFeedbackConstants; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 33 | import android.view.IWindowManager; |
Jeff Brown | bbda99d | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 34 | import android.view.InputDevice; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 35 | import android.view.KeyCharacterMap; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 36 | import android.view.KeyEvent; |
| 37 | import android.view.MotionEvent; |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 38 | import android.view.SoundEffectConstants; |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 39 | import android.view.View; |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 40 | import android.view.ViewConfiguration; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 41 | import android.widget.ImageView; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 42 | |
| 43 | import com.android.systemui.R; |
| 44 | |
| 45 | public class KeyButtonView extends ImageView { |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 46 | private static final String TAG = "StatusBar.KeyButtonView"; |
| 47 | |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 48 | final float GLOW_MAX_SCALE_FACTOR = 1.8f; |
Justin Ho | bceff71 | 2011-10-26 21:01:13 -0400 | [diff] [blame] | 49 | final float BUTTON_QUIESCENT_ALPHA = 0.6f; |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 50 | |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 51 | long mDownTime; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 52 | int mCode; |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 53 | int mTouchSlop; |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 54 | Drawable mGlowBG; |
| 55 | float mGlowAlpha = 0f, mGlowScale = 1f, mDrawingAlpha = 1f; |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 56 | boolean mSupportsLongpress = true; |
Daniel Sandler | c638c1e | 2011-08-24 16:19:23 -0700 | [diff] [blame] | 57 | RectF mRect = new RectF(0f,0f,0f,0f); |
Michael Jurka | 60bc69e | 2012-01-16 05:05:56 -0800 | [diff] [blame] | 58 | AnimatorSet mPressedAnim; |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 59 | |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 60 | Runnable mCheckLongPress = new Runnable() { |
| 61 | public void run() { |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 62 | if (isPressed()) { |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 63 | // Slog.d("KeyButtonView", "longpressed: " + this); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 64 | if (mCode != 0) { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 65 | sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 66 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); |
| 67 | } else { |
| 68 | // Just an old-fashioned ImageView |
| 69 | performLongClick(); |
| 70 | } |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | }; |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 74 | |
| 75 | public KeyButtonView(Context context, AttributeSet attrs) { |
| 76 | this(context, attrs, 0); |
| 77 | } |
| 78 | |
| 79 | public KeyButtonView(Context context, AttributeSet attrs, int defStyle) { |
| 80 | super(context, attrs); |
| 81 | |
| 82 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KeyButtonView, |
| 83 | defStyle, 0); |
| 84 | |
| 85 | mCode = a.getInteger(R.styleable.KeyButtonView_keyCode, 0); |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 86 | |
| 87 | mSupportsLongpress = a.getBoolean(R.styleable.KeyButtonView_keyRepeat, true); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 88 | |
| 89 | mGlowBG = a.getDrawable(R.styleable.KeyButtonView_glowBackground); |
| 90 | if (mGlowBG != null) { |
Michael Jurka | 0b65866 | 2012-01-12 05:48:19 -0800 | [diff] [blame] | 91 | setDrawingAlpha(BUTTON_QUIESCENT_ALPHA); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | a.recycle(); |
| 95 | |
Joe Onorato | 641bad4 | 2011-01-06 15:54:23 -0800 | [diff] [blame] | 96 | setClickable(true); |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 97 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 100 | @Override |
| 101 | protected void onDraw(Canvas canvas) { |
| 102 | if (mGlowBG != null) { |
| 103 | canvas.save(); |
| 104 | final int w = getWidth(); |
| 105 | final int h = getHeight(); |
| 106 | canvas.scale(mGlowScale, mGlowScale, w*0.5f, h*0.5f); |
| 107 | mGlowBG.setBounds(0, 0, w, h); |
Michael Jurka | 0b65866 | 2012-01-12 05:48:19 -0800 | [diff] [blame] | 108 | mGlowBG.setAlpha((int)(mDrawingAlpha * mGlowAlpha * 255)); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 109 | mGlowBG.draw(canvas); |
| 110 | canvas.restore(); |
Daniel Sandler | c638c1e | 2011-08-24 16:19:23 -0700 | [diff] [blame] | 111 | mRect.right = w; |
| 112 | mRect.bottom = h; |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 113 | } |
| 114 | super.onDraw(canvas); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | public float getDrawingAlpha() { |
| 118 | if (mGlowBG == null) return 0; |
| 119 | return mDrawingAlpha; |
| 120 | } |
| 121 | |
| 122 | public void setDrawingAlpha(float x) { |
| 123 | if (mGlowBG == null) return; |
Michael Jurka | 0b65866 | 2012-01-12 05:48:19 -0800 | [diff] [blame] | 124 | // Calling setAlpha(int), which is an ImageView-specific |
| 125 | // method that's different from setAlpha(float). This sets |
| 126 | // the alpha on this ImageView's drawable directly |
| 127 | setAlpha((int) (x * 255)); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 128 | mDrawingAlpha = x; |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | public float getGlowAlpha() { |
| 132 | if (mGlowBG == null) return 0; |
| 133 | return mGlowAlpha; |
| 134 | } |
| 135 | |
| 136 | public void setGlowAlpha(float x) { |
| 137 | if (mGlowBG == null) return; |
| 138 | mGlowAlpha = x; |
| 139 | invalidate(); |
| 140 | } |
| 141 | |
| 142 | public float getGlowScale() { |
| 143 | if (mGlowBG == null) return 0; |
| 144 | return mGlowScale; |
| 145 | } |
| 146 | |
| 147 | public void setGlowScale(float x) { |
| 148 | if (mGlowBG == null) return; |
| 149 | mGlowScale = x; |
| 150 | final float w = getWidth(); |
| 151 | final float h = getHeight(); |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 152 | if (GLOW_MAX_SCALE_FACTOR <= 1.0f) { |
| 153 | // this only works if we know the glow will never leave our bounds |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 154 | invalidate(); |
| 155 | } else { |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 156 | final float rx = (w * (GLOW_MAX_SCALE_FACTOR - 1.0f)) / 2.0f + 1.0f; |
| 157 | final float ry = (h * (GLOW_MAX_SCALE_FACTOR - 1.0f)) / 2.0f + 1.0f; |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 158 | com.android.systemui.SwipeHelper.invalidateGlobalRegion( |
| 159 | this, |
Daniel Sandler | 5298582 | 2011-07-29 08:42:15 -0400 | [diff] [blame] | 160 | new RectF(getLeft() - rx, |
| 161 | getTop() - ry, |
| 162 | getRight() + rx, |
| 163 | getBottom() + ry)); |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 164 | |
| 165 | // also invalidate our immediate parent to help avoid situations where nearby glows |
| 166 | // interfere |
| 167 | ((View)getParent()).invalidate(); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | public void setPressed(boolean pressed) { |
| 172 | if (mGlowBG != null) { |
| 173 | if (pressed != isPressed()) { |
Michael Jurka | 60bc69e | 2012-01-16 05:05:56 -0800 | [diff] [blame] | 174 | if (mPressedAnim != null && mPressedAnim.isRunning()) { |
| 175 | mPressedAnim.cancel(); |
| 176 | } |
| 177 | final AnimatorSet as = mPressedAnim = new AnimatorSet(); |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 178 | if (pressed) { |
Justin Ho | bceff71 | 2011-10-26 21:01:13 -0400 | [diff] [blame] | 179 | if (mGlowScale < GLOW_MAX_SCALE_FACTOR) |
| 180 | mGlowScale = GLOW_MAX_SCALE_FACTOR; |
| 181 | if (mGlowAlpha < BUTTON_QUIESCENT_ALPHA) |
| 182 | mGlowAlpha = BUTTON_QUIESCENT_ALPHA; |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 183 | setDrawingAlpha(1f); |
| 184 | as.playTogether( |
| 185 | ObjectAnimator.ofFloat(this, "glowAlpha", 1f), |
Daniel Sandler | 96f4818 | 2011-08-17 09:50:35 -0400 | [diff] [blame] | 186 | ObjectAnimator.ofFloat(this, "glowScale", GLOW_MAX_SCALE_FACTOR) |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 187 | ); |
| 188 | as.setDuration(50); |
| 189 | } else { |
| 190 | as.playTogether( |
| 191 | ObjectAnimator.ofFloat(this, "glowAlpha", 0f), |
| 192 | ObjectAnimator.ofFloat(this, "glowScale", 1f), |
Justin Ho | bceff71 | 2011-10-26 21:01:13 -0400 | [diff] [blame] | 193 | ObjectAnimator.ofFloat(this, "drawingAlpha", BUTTON_QUIESCENT_ALPHA) |
Daniel Sandler | a375c94 | 2011-07-29 00:33:53 -0400 | [diff] [blame] | 194 | ); |
| 195 | as.setDuration(500); |
| 196 | } |
| 197 | as.start(); |
| 198 | } |
| 199 | } |
| 200 | super.setPressed(pressed); |
| 201 | } |
| 202 | |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 203 | public boolean onTouchEvent(MotionEvent ev) { |
| 204 | final int action = ev.getAction(); |
| 205 | int x, y; |
| 206 | |
| 207 | switch (action) { |
| 208 | case MotionEvent.ACTION_DOWN: |
Christopher Tate | 7a1a2f0 | 2010-09-09 12:22:40 -0700 | [diff] [blame] | 209 | //Slog.d("KeyButtonView", "press"); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 210 | mDownTime = SystemClock.uptimeMillis(); |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 211 | setPressed(true); |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 212 | if (mCode != 0) { |
| 213 | sendEvent(KeyEvent.ACTION_DOWN, 0, mDownTime); |
| 214 | } else { |
| 215 | // Provide the same haptic feedback that the system offers for virtual keys. |
| 216 | performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
| 217 | } |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 218 | if (mSupportsLongpress) { |
| 219 | removeCallbacks(mCheckLongPress); |
| 220 | postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout()); |
Daniel Sandler | 44a4616 | 2011-08-09 16:48:21 -0400 | [diff] [blame] | 221 | } |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 222 | break; |
| 223 | case MotionEvent.ACTION_MOVE: |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 224 | x = (int)ev.getX(); |
| 225 | y = (int)ev.getY(); |
| 226 | setPressed(x >= -mTouchSlop |
| 227 | && x < getWidth() + mTouchSlop |
| 228 | && y >= -mTouchSlop |
| 229 | && y < getHeight() + mTouchSlop); |
Joe Onorato | abb2777 | 2010-11-15 13:30:12 -0800 | [diff] [blame] | 230 | break; |
| 231 | case MotionEvent.ACTION_CANCEL: |
| 232 | setPressed(false); |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 233 | if (mCode != 0) { |
| 234 | sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED); |
| 235 | } |
| 236 | if (mSupportsLongpress) { |
| 237 | removeCallbacks(mCheckLongPress); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 238 | } |
| 239 | break; |
| 240 | case MotionEvent.ACTION_UP: |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 241 | final boolean doIt = isPressed(); |
Daniel Sandler | 804eb85 | 2010-08-31 15:43:50 -0400 | [diff] [blame] | 242 | setPressed(false); |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 243 | if (mCode != 0) { |
| 244 | if (doIt) { |
| 245 | sendEvent(KeyEvent.ACTION_UP, 0); |
| 246 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
| 247 | playSoundEffect(SoundEffectConstants.CLICK); |
Patrick Dubroy | 5ee1a3d | 2011-03-02 19:11:19 -0800 | [diff] [blame] | 248 | } else { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 249 | sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED); |
Daniel Sandler | aa051d6 | 2011-03-01 16:23:57 -0500 | [diff] [blame] | 250 | } |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 251 | } else { |
| 252 | // no key code, just a regular ImageView |
| 253 | if (doIt) { |
| 254 | performClick(); |
| 255 | } |
| 256 | } |
| 257 | if (mSupportsLongpress) { |
| 258 | removeCallbacks(mCheckLongPress); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 259 | } |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
Jeff Brown | bbda99d | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 266 | void sendEvent(int action, int flags) { |
| 267 | sendEvent(action, flags, SystemClock.uptimeMillis()); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Jeff Brown | bbda99d | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 270 | void sendEvent(int action, int flags, long when) { |
Jeff Brown | 98392ef | 2011-09-12 18:24:59 -0700 | [diff] [blame] | 271 | final int repeatCount = (flags & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0; |
| 272 | final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, repeatCount, |
| 273 | 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
| 274 | flags | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, |
| 275 | InputDevice.SOURCE_KEYBOARD); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame^] | 276 | InputManager.getInstance().injectInputEvent(ev, |
| 277 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
Joe Onorato | 86f9bd2 | 2010-06-30 17:03:42 -0400 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | |