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