KeyButtonView.java revision f68b500bb0d4c24ccabb40284f97981d50f888a8
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            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        setClickable(true);
82    }
83
84    public boolean onTouchEvent(MotionEvent ev) {
85        final int action = ev.getAction();
86        int x, y;
87
88        switch (action) {
89            case MotionEvent.ACTION_DOWN:
90                //Slog.d("KeyButtonView", "press");
91                mDownTime = SystemClock.uptimeMillis();
92                mRepeat = 0;
93                mSending = true;
94                mLongPressed = false;
95                sendEvent(KeyEvent.ACTION_DOWN,
96                        KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, mDownTime);
97                setPressed(true);
98                removeCallbacks(mCheckLongPress);
99                postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout());
100                break;
101            case MotionEvent.ACTION_MOVE:
102                if (mSending) {
103                    x = (int)ev.getX();
104                    y = (int)ev.getY();
105                    setPressed(x >= 0 && x < getWidth() && y >= 0 &&  y < getHeight());
106                }
107                break;
108            case MotionEvent.ACTION_CANCEL:
109                setPressed(false);
110                if (mSending && !mLongPressed) {
111                    mSending = false;
112                    sendEvent(KeyEvent.ACTION_UP,
113                            KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY
114                                | KeyEvent.FLAG_CANCELED);
115                    removeCallbacks(mCheckLongPress);
116                }
117                break;
118            case MotionEvent.ACTION_UP:
119                setPressed(false);
120                if (mSending && !mLongPressed) {
121                    mSending = false;
122                    sendEvent(KeyEvent.ACTION_UP,
123                            KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY);
124                    removeCallbacks(mCheckLongPress);
125                }
126                break;
127        }
128
129        return true;
130    }
131
132    void sendEvent(int action, int flags) {
133        sendEvent(action, flags, SystemClock.uptimeMillis());
134    }
135
136    void sendEvent(int action, int flags, long when) {
137        final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, mRepeat,
138                0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags, InputDevice.SOURCE_KEYBOARD);
139        try {
140            //Slog.d(TAG, "injecting event " + ev);
141            mWindowManager.injectInputEventNoWait(ev);
142        } catch (RemoteException ex) {
143            // System process is dead
144        }
145    }
146}
147
148
149