KeyButtonView.java revision abb27777f4763d057d37a58936232f8e17f57de1
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 setPressed(x >= 0 && x < getWidth() && y >= 0 && y < getHeight()); 104 } 105 break; 106 case MotionEvent.ACTION_CANCEL: 107 setPressed(false); 108 if (mSending && !mLongPressed) { 109 mSending = false; 110 sendEvent(KeyEvent.ACTION_UP, 111 KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY 112 | KeyEvent.FLAG_CANCELED); 113 removeCallbacks(mCheckLongPress); 114 } 115 break; 116 case MotionEvent.ACTION_UP: 117 setPressed(false); 118 if (mSending && !mLongPressed) { 119 mSending = false; 120 sendEvent(KeyEvent.ACTION_UP, 121 KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY); 122 removeCallbacks(mCheckLongPress); 123 } 124 break; 125 } 126 127 return true; 128 } 129 130 void sendEvent(int action, int flags) { 131 sendEvent(action, flags, SystemClock.uptimeMillis()); 132 } 133 134 void sendEvent(int action, int flags, long when) { 135 final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, mRepeat, 136 0, 0, 0, flags, InputDevice.SOURCE_KEYBOARD); 137 try { 138 //Slog.d(TAG, "injecting event " + ev); 139 mWindowManager.injectInputEventNoWait(ev); 140 } catch (RemoteException ex) { 141 // System process is dead 142 } 143 } 144} 145 146 147