1 /*
2  * Copyright (C) 2015 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 package com.android.launcher3;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.KeyEvent;
21 import android.widget.EditText;
22 
23 
24 /**
25  * The edit text that reports back when the back key has been pressed.
26  */
27 public class ExtendedEditText extends EditText {
28 
29     /**
30      * Implemented by listeners of the back key.
31      */
32     public interface OnBackKeyListener {
onBackKey()33         public boolean onBackKey();
34     }
35 
36     private OnBackKeyListener mBackKeyListener;
37 
ExtendedEditText(Context context)38     public ExtendedEditText(Context context) {
39         super(context);
40     }
41 
ExtendedEditText(Context context, AttributeSet attrs)42     public ExtendedEditText(Context context, AttributeSet attrs) {
43         super(context, attrs);
44     }
45 
ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr)46     public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
47         super(context, attrs, defStyleAttr);
48     }
49 
setOnBackKeyListener(OnBackKeyListener listener)50     public void setOnBackKeyListener(OnBackKeyListener listener) {
51         mBackKeyListener = listener;
52     }
53 
54     @Override
onKeyPreIme(int keyCode, KeyEvent event)55     public boolean onKeyPreIme(int keyCode, KeyEvent event) {
56         // If this is a back key, propagate the key back to the listener
57         if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
58             if (mBackKeyListener != null) {
59                 return mBackKeyListener.onBackKey();
60             }
61             return false;
62         }
63         return super.onKeyPreIme(keyCode, event);
64     }
65 }
66