FeedbackLayout.java revision 3c233bf1a5003c478a27964758afe2ca581d3d8b
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.research;
18
19import android.app.Activity;
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.KeyEvent;
23import android.widget.LinearLayout;
24
25public class FeedbackLayout extends LinearLayout {
26    private Activity mActivity;
27
28    public FeedbackLayout(Context context) {
29        super(context);
30    }
31
32    public FeedbackLayout(Context context, AttributeSet attrs) {
33        super(context, attrs);
34    }
35
36    public FeedbackLayout(Context context, AttributeSet attrs, int defstyle) {
37        super(context, attrs, defstyle);
38    }
39
40    public void setActivity(Activity activity) {
41        mActivity = activity;
42    }
43
44    @Override
45    public boolean dispatchKeyEventPreIme(KeyEvent event) {
46        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
47            KeyEvent.DispatcherState state = getKeyDispatcherState();
48            if (state != null) {
49                if (event.getAction() == KeyEvent.ACTION_DOWN
50                        && event.getRepeatCount() == 0) {
51                    state.startTracking(event, this);
52                    return true;
53                } else if (event.getAction() == KeyEvent.ACTION_UP
54                        && !event.isCanceled() && state.isTracking(event)) {
55                    mActivity.onBackPressed();
56                    return true;
57                }
58            }
59        }
60        return super.dispatchKeyEventPreIme(event);
61    }
62}
63