KeyButtonView.java revision fd52b18d9bf3cd62c7a07058536e9f97db65beea
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 17package com.android.systemui.statusbar.policy; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.drawable.AnimationDrawable; 22import android.graphics.drawable.Drawable; 23import android.os.RemoteException; 24import android.os.SystemClock; 25import android.os.ServiceManager; 26import android.util.AttributeSet; 27import android.util.Slog; 28import android.view.HapticFeedbackConstants; 29import android.view.IWindowManager; 30import android.view.InputDevice; 31import android.view.KeyEvent; 32import android.view.MotionEvent; 33import android.view.ViewConfiguration; 34import android.widget.ImageView; 35import android.widget.RemoteViews.RemoteView; 36 37import com.android.systemui.R; 38 39public class KeyButtonView extends ImageView { 40 private static final String TAG = "StatusBar.KeyButtonView"; 41 42 IWindowManager mWindowManager; 43 long mDownTime; 44 boolean mSending, mLongPressed; 45 int mCode; 46 int mRepeat; 47 Runnable mCheckLongPress = new Runnable() { 48 public void run() { 49 Slog.d("KeyButtonView", "longpress"); 50 if (isPressed()) { 51 mLongPressed = true; 52 mRepeat++; 53 sendEvent(KeyEvent.ACTION_DOWN, 54 KeyEvent.FLAG_FROM_SYSTEM 55 | KeyEvent.FLAG_VIRTUAL_HARD_KEY 56 | KeyEvent.FLAG_LONG_PRESS); 57 } 58 } 59 }; 60 61 public KeyButtonView(Context context, AttributeSet attrs) { 62 this(context, attrs, 0); 63 } 64 65 public KeyButtonView(Context context, AttributeSet attrs, int defStyle) { 66 super(context, attrs); 67 68 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KeyButtonView, 69 defStyle, 0); 70 71 mCode = a.getInteger(R.styleable.KeyButtonView_keyCode, 0); 72 if (mCode == 0) { 73 Slog.w(TAG, "KeyButtonView without key code id=0x" + Integer.toHexString(getId())); 74 } 75 76 a.recycle(); 77 78 mWindowManager = IWindowManager.Stub.asInterface( 79 ServiceManager.getService(Context.WINDOW_SERVICE)); 80 } 81 82 public boolean onTouchEvent(MotionEvent ev) { 83 final int action = ev.getAction(); 84 int x, y; 85 86 switch (action) { 87 case MotionEvent.ACTION_DOWN: 88 //Slog.d("KeyButtonView", "press"); 89 mDownTime = SystemClock.uptimeMillis(); 90 mRepeat = 0; 91 mSending = true; 92 mLongPressed = false; 93 sendEvent(KeyEvent.ACTION_DOWN, 94 KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, mDownTime); 95 setPressed(true); 96 removeCallbacks(mCheckLongPress); 97 postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout()); 98 break; 99 case MotionEvent.ACTION_MOVE: 100 if (mSending) { 101 x = (int)ev.getX(); 102 y = (int)ev.getY(); 103 if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) { 104 mSending = false; 105 sendEvent(KeyEvent.ACTION_UP, 106 KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY 107 | KeyEvent.FLAG_CANCELED); 108 setPressed(false); 109 removeCallbacks(mCheckLongPress); 110 } 111 } 112 break; 113 case MotionEvent.ACTION_UP: 114 case MotionEvent.ACTION_CANCEL: 115 setPressed(false); 116 if (mSending && !mLongPressed) { 117 mSending = false; 118 sendEvent(KeyEvent.ACTION_UP, 119 KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY); 120 removeCallbacks(mCheckLongPress); 121 } 122 break; 123 } 124 125 return true; 126 } 127 128 void sendEvent(int action, int flags) { 129 sendEvent(action, flags, SystemClock.uptimeMillis()); 130 } 131 132 void sendEvent(int action, int flags, long when) { 133 final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, mRepeat, 134 0, 0, 0, flags, InputDevice.SOURCE_KEYBOARD); 135 try { 136 //Slog.d(TAG, "injecting event " + ev); 137 mWindowManager.injectInputEventNoWait(ev); 138 } catch (RemoteException ex) { 139 // System process is dead 140 } 141 } 142} 143 144 145