1616
1717package com .android .systemui .statusbar .policy ;
1818
19- import android .animation .Animator ;
2019import android .animation .AnimatorSet ;
2120import android .animation .ObjectAnimator ;
2221import android .content .Context ;
2322import android .content .res .TypedArray ;
24- import android .graphics .drawable .AnimationDrawable ;
2523import android .graphics .drawable .Drawable ;
2624import android .graphics .Canvas ;
2725import android .graphics .RectF ;
2826import android .os .RemoteException ;
2927import android .os .SystemClock ;
3028import android .os .ServiceManager ;
3129import android .util .AttributeSet ;
32- import android .util .Slog ;
3330import android .view .accessibility .AccessibilityEvent ;
3431import android .view .HapticFeedbackConstants ;
3532import android .view .IWindowManager ;
4037import android .view .SoundEffectConstants ;
4138import android .view .View ;
4239import android .view .ViewConfiguration ;
43- import android .view .ViewGroup ;
4440import android .widget .ImageView ;
45- import android .widget .RemoteViews .RemoteView ;
4641
4742import com .android .systemui .R ;
4843
@@ -53,9 +48,7 @@ public class KeyButtonView extends ImageView {
5348
5449 IWindowManager mWindowManager ;
5550 long mDownTime ;
56- boolean mSending ;
5751 int mCode ;
58- int mRepeat ;
5952 int mTouchSlop ;
6053 Drawable mGlowBG ;
6154 float mGlowAlpha = 0f , mGlowScale = 1f , mDrawingAlpha = 1f ;
@@ -67,12 +60,7 @@ public void run() {
6760 if (isPressed ()) {
6861 // Slog.d("KeyButtonView", "longpressed: " + this);
6962 if (mCode != 0 ) {
70- mRepeat ++;
71- sendEvent (KeyEvent .ACTION_DOWN ,
72- KeyEvent .FLAG_FROM_SYSTEM
73- | KeyEvent .FLAG_VIRTUAL_HARD_KEY
74- | KeyEvent .FLAG_LONG_PRESS );
75-
63+ sendEvent (KeyEvent .ACTION_DOWN , KeyEvent .FLAG_LONG_PRESS );
7664 sendAccessibilityEvent (AccessibilityEvent .TYPE_VIEW_LONG_CLICKED );
7765 } else {
7866 // Just an old-fashioned ImageView
@@ -217,64 +205,54 @@ public boolean onTouchEvent(MotionEvent ev) {
217205 case MotionEvent .ACTION_DOWN :
218206 //Slog.d("KeyButtonView", "press");
219207 mDownTime = SystemClock .uptimeMillis ();
220- mRepeat = 0 ;
221- mSending = true ;
222208 setPressed (true );
223- sendEvent (KeyEvent .ACTION_DOWN ,
224- KeyEvent .FLAG_FROM_SYSTEM | KeyEvent .FLAG_VIRTUAL_HARD_KEY , mDownTime );
209+ if (mCode != 0 ) {
210+ sendEvent (KeyEvent .ACTION_DOWN , 0 , mDownTime );
211+ } else {
212+ // Provide the same haptic feedback that the system offers for virtual keys.
213+ performHapticFeedback (HapticFeedbackConstants .VIRTUAL_KEY );
214+ }
225215 if (mSupportsLongpress ) {
226216 removeCallbacks (mCheckLongPress );
227217 postDelayed (mCheckLongPress , ViewConfiguration .getLongPressTimeout ());
228- } else {
229- mSending = false ;
230- sendEvent (KeyEvent .ACTION_UP ,
231- KeyEvent .FLAG_FROM_SYSTEM | KeyEvent .FLAG_VIRTUAL_HARD_KEY , mDownTime );
232- sendAccessibilityEvent (AccessibilityEvent .TYPE_VIEW_CLICKED );
233- playSoundEffect (SoundEffectConstants .CLICK );
234218 }
235219 break ;
236220 case MotionEvent .ACTION_MOVE :
237- if (mSending ) {
238- x = (int )ev .getX ();
239- y = (int )ev .getY ();
240- setPressed (x >= -mTouchSlop
241- && x < getWidth () + mTouchSlop
242- && y >= -mTouchSlop
243- && y < getHeight () + mTouchSlop );
244- }
221+ x = (int )ev .getX ();
222+ y = (int )ev .getY ();
223+ setPressed (x >= -mTouchSlop
224+ && x < getWidth () + mTouchSlop
225+ && y >= -mTouchSlop
226+ && y < getHeight () + mTouchSlop );
245227 break ;
246228 case MotionEvent .ACTION_CANCEL :
247229 setPressed (false );
248- if (mSending ) {
249- mSending = false ;
250- sendEvent (KeyEvent .ACTION_UP ,
251- KeyEvent .FLAG_FROM_SYSTEM | KeyEvent .FLAG_VIRTUAL_HARD_KEY
252- | KeyEvent .FLAG_CANCELED );
253- if (mSupportsLongpress ) {
254- removeCallbacks (mCheckLongPress );
255- }
230+ if (mCode != 0 ) {
231+ sendEvent (KeyEvent .ACTION_UP , KeyEvent .FLAG_CANCELED );
232+ }
233+ if (mSupportsLongpress ) {
234+ removeCallbacks (mCheckLongPress );
256235 }
257236 break ;
258237 case MotionEvent .ACTION_UP :
259238 final boolean doIt = isPressed ();
260239 setPressed (false );
261- if (mSending ) {
262- mSending = false ;
263- final int flags = KeyEvent .FLAG_FROM_SYSTEM | KeyEvent .FLAG_VIRTUAL_HARD_KEY ;
264- if (mSupportsLongpress ) {
265- removeCallbacks (mCheckLongPress );
266- }
267-
268- if (mCode != 0 ) {
269- if (doIt ) {
270- sendEvent (KeyEvent .ACTION_UP , flags );
271- sendAccessibilityEvent (AccessibilityEvent .TYPE_VIEW_CLICKED );
272- playSoundEffect (SoundEffectConstants .CLICK );
273- }
240+ if (mCode != 0 ) {
241+ if (doIt ) {
242+ sendEvent (KeyEvent .ACTION_UP , 0 );
243+ sendAccessibilityEvent (AccessibilityEvent .TYPE_VIEW_CLICKED );
244+ playSoundEffect (SoundEffectConstants .CLICK );
274245 } else {
275- // no key code, just a regular ImageView
276- if (doIt ) performClick ();
246+ sendEvent (KeyEvent .ACTION_UP , KeyEvent .FLAG_CANCELED );
277247 }
248+ } else {
249+ // no key code, just a regular ImageView
250+ if (doIt ) {
251+ performClick ();
252+ }
253+ }
254+ if (mSupportsLongpress ) {
255+ removeCallbacks (mCheckLongPress );
278256 }
279257 break ;
280258 }
@@ -287,8 +265,11 @@ void sendEvent(int action, int flags) {
287265 }
288266
289267 void sendEvent (int action , int flags , long when ) {
290- final KeyEvent ev = new KeyEvent (mDownTime , when , action , mCode , mRepeat ,
291- 0 , KeyCharacterMap .VIRTUAL_KEYBOARD , 0 , flags , InputDevice .SOURCE_KEYBOARD );
268+ final int repeatCount = (flags & KeyEvent .FLAG_LONG_PRESS ) != 0 ? 1 : 0 ;
269+ final KeyEvent ev = new KeyEvent (mDownTime , when , action , mCode , repeatCount ,
270+ 0 , KeyCharacterMap .VIRTUAL_KEYBOARD , 0 ,
271+ flags | KeyEvent .FLAG_FROM_SYSTEM | KeyEvent .FLAG_VIRTUAL_HARD_KEY ,
272+ InputDevice .SOURCE_KEYBOARD );
292273 try {
293274 //Slog.d(TAG, "injecting event " + ev);
294275 mWindowManager .injectInputEventNoWait (ev );
0 commit comments