blob: b01c5e72d22c9dacdb35f09c5d0524515e13c029 [file] [log] [blame]
Joe Onorato86f9bd22010-06-30 17:03:42 -04001/*
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;
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;
Daniel Sandler804eb852010-08-31 15:43:50 -040028import android.view.HapticFeedbackConstants;
Joe Onorato86f9bd22010-06-30 17:03:42 -040029import android.view.IWindowManager;
Jeff Brownbbda99d2010-07-28 15:48:59 -070030import android.view.InputDevice;
Joe Onorato86f9bd22010-06-30 17:03:42 -040031import android.view.KeyEvent;
32import android.view.MotionEvent;
Daniel Sandler804eb852010-08-31 15:43:50 -040033import android.view.ViewConfiguration;
Joe Onorato86f9bd22010-06-30 17:03:42 -040034import android.widget.ImageView;
35import android.widget.RemoteViews.RemoteView;
36
37import com.android.systemui.R;
38
39public class KeyButtonView extends ImageView {
40 IWindowManager mWindowManager;
41 long mDownTime;
Daniel Sandler804eb852010-08-31 15:43:50 -040042 boolean mSending, mLongPressed;
Joe Onorato86f9bd22010-06-30 17:03:42 -040043 int mCode;
44 int mRepeat;
Daniel Sandler804eb852010-08-31 15:43:50 -040045 Runnable mCheckLongPress = new Runnable() {
46 public void run() {
47 Slog.d("KeyButtonView", "longpress");
48 if (isPressed()) {
49 mLongPressed = true;
50 mRepeat++;
51 sendEvent(KeyEvent.ACTION_DOWN,
52 KeyEvent.FLAG_FROM_SYSTEM
53 | KeyEvent.FLAG_VIRTUAL_HARD_KEY
54 | KeyEvent.FLAG_LONG_PRESS);
55 }
56 }
57 };
Joe Onorato86f9bd22010-06-30 17:03:42 -040058
59 public KeyButtonView(Context context, AttributeSet attrs) {
60 this(context, attrs, 0);
61 }
62
63 public KeyButtonView(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs);
65
66 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KeyButtonView,
67 defStyle, 0);
68
69 mCode = a.getInteger(R.styleable.KeyButtonView_keyCode, 0);
70 if (mCode == 0) {
71 Slog.w(StatusBarService.TAG, "KeyButtonView without key code id=0x"
72 + Integer.toHexString(getId()));
73 }
74
75 a.recycle();
76
77 mWindowManager = IWindowManager.Stub.asInterface(
78 ServiceManager.getService(Context.WINDOW_SERVICE));
79 }
80
81 public boolean onTouchEvent(MotionEvent ev) {
82 final int action = ev.getAction();
83 int x, y;
84
85 switch (action) {
86 case MotionEvent.ACTION_DOWN:
Christopher Tate7a1a2f02010-09-09 12:22:40 -070087 //Slog.d("KeyButtonView", "press");
Joe Onorato86f9bd22010-06-30 17:03:42 -040088 mDownTime = SystemClock.uptimeMillis();
89 mRepeat = 0;
90 mSending = true;
Daniel Sandler804eb852010-08-31 15:43:50 -040091 mLongPressed = false;
Jeff Brownbbda99d2010-07-28 15:48:59 -070092 sendEvent(KeyEvent.ACTION_DOWN,
Daniel Sandler804eb852010-08-31 15:43:50 -040093 KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, mDownTime);
Daniel Sandlera101f8f2010-07-21 16:13:30 -040094 setPressed(true);
Daniel Sandler804eb852010-08-31 15:43:50 -040095 removeCallbacks(mCheckLongPress);
96 postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout());
Joe Onorato86f9bd22010-06-30 17:03:42 -040097 break;
98 case MotionEvent.ACTION_MOVE:
99 if (mSending) {
100 x = (int)ev.getX();
101 y = (int)ev.getY();
102 if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) {
103 mSending = false;
Jeff Brownbbda99d2010-07-28 15:48:59 -0700104 sendEvent(KeyEvent.ACTION_UP,
Daniel Sandler804eb852010-08-31 15:43:50 -0400105 KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY
Jeff Brownbbda99d2010-07-28 15:48:59 -0700106 | KeyEvent.FLAG_CANCELED);
Daniel Sandlera101f8f2010-07-21 16:13:30 -0400107 setPressed(false);
Daniel Sandler804eb852010-08-31 15:43:50 -0400108 removeCallbacks(mCheckLongPress);
Joe Onorato86f9bd22010-06-30 17:03:42 -0400109 }
110 }
111 break;
112 case MotionEvent.ACTION_UP:
113 case MotionEvent.ACTION_CANCEL:
Daniel Sandler804eb852010-08-31 15:43:50 -0400114 setPressed(false);
115 if (mSending && !mLongPressed) {
Joe Onorato86f9bd22010-06-30 17:03:42 -0400116 mSending = false;
Jeff Brownbbda99d2010-07-28 15:48:59 -0700117 sendEvent(KeyEvent.ACTION_UP,
Daniel Sandler804eb852010-08-31 15:43:50 -0400118 KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY);
119 removeCallbacks(mCheckLongPress);
Joe Onorato86f9bd22010-06-30 17:03:42 -0400120 }
121 break;
122 }
123
124 return true;
125 }
126
Jeff Brownbbda99d2010-07-28 15:48:59 -0700127 void sendEvent(int action, int flags) {
128 sendEvent(action, flags, SystemClock.uptimeMillis());
Joe Onorato86f9bd22010-06-30 17:03:42 -0400129 }
130
Jeff Brownbbda99d2010-07-28 15:48:59 -0700131 void sendEvent(int action, int flags, long when) {
132 final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, mRepeat,
133 0, 0, 0, flags, InputDevice.SOURCE_KEYBOARD);
Joe Onorato86f9bd22010-06-30 17:03:42 -0400134 try {
Christopher Tate7a1a2f02010-09-09 12:22:40 -0700135 //Slog.d(StatusBarService.TAG, "injecting event " + ev);
Jeff Brownbbda99d2010-07-28 15:48:59 -0700136 mWindowManager.injectInputEventNoWait(ev);
Joe Onorato86f9bd22010-06-30 17:03:42 -0400137 } catch (RemoteException ex) {
138 // System process is dead
139 }
140 }
141}
142
143