Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 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 | */ |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 16 | package com.android.launcher3; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 17 | |
| 18 | import android.content.Context; |
| 19 | import android.util.AttributeSet; |
Winson | 4e7a101 | 2015-08-13 16:47:55 -0700 | [diff] [blame] | 20 | import android.view.DragEvent; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 21 | import android.view.KeyEvent; |
| 22 | import android.widget.EditText; |
| 23 | |
| 24 | |
| 25 | /** |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 26 | * The edit text that reports back when the back key has been pressed. |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 27 | */ |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 28 | public class ExtendedEditText extends EditText { |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Implemented by listeners of the back key. |
| 32 | */ |
| 33 | public interface OnBackKeyListener { |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 34 | public boolean onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | private OnBackKeyListener mBackKeyListener; |
| 38 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 39 | public ExtendedEditText(Context context) { |
| 40 | super(context); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 43 | public ExtendedEditText(Context context, AttributeSet attrs) { |
| 44 | super(context, attrs); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 47 | public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 48 | super(context, attrs, defStyleAttr); |
| 49 | } |
| 50 | |
| 51 | public void setOnBackKeyListener(OnBackKeyListener listener) { |
| 52 | mBackKeyListener = listener; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 57 | // If this is a back key, propagate the key back to the listener |
| 58 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { |
| 59 | if (mBackKeyListener != null) { |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 60 | return mBackKeyListener.onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | return super.onKeyPreIme(keyCode, event); |
| 65 | } |
Winson | 4e7a101 | 2015-08-13 16:47:55 -0700 | [diff] [blame] | 66 | |
| 67 | @Override |
| 68 | public boolean onDragEvent(DragEvent event) { |
| 69 | // We don't want this view to interfere with Launcher own drag and drop. |
| 70 | return false; |
| 71 | } |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 72 | } |