1 /* 2 * Copyright (C) 2012 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 17 package com.android.inputmethod.research; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.util.AttributeSet; 22 import android.view.KeyEvent; 23 import android.widget.LinearLayout; 24 25 public 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