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