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; |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame^] | 22 | import android.view.inputmethod.InputMethodManager; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 23 | import android.widget.EditText; |
| 24 | |
| 25 | |
| 26 | /** |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 27 | * 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] | 28 | */ |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 29 | public class ExtendedEditText extends EditText { |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 30 | |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame^] | 31 | private boolean mShowImeAfterFirstLayout; |
| 32 | |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Implemented by listeners of the back key. |
| 35 | */ |
| 36 | public interface OnBackKeyListener { |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 37 | public boolean onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | private OnBackKeyListener mBackKeyListener; |
| 41 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 42 | public ExtendedEditText(Context context) { |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame^] | 43 | this(context, null, 0); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 46 | public ExtendedEditText(Context context, AttributeSet attrs) { |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame^] | 47 | this(context, attrs, 0); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 50 | public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 51 | super(context, attrs, defStyleAttr); |
| 52 | } |
| 53 | |
| 54 | public void setOnBackKeyListener(OnBackKeyListener listener) { |
| 55 | mBackKeyListener = listener; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 60 | // If this is a back key, propagate the key back to the listener |
| 61 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { |
| 62 | if (mBackKeyListener != null) { |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 63 | return mBackKeyListener.onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | return super.onKeyPreIme(keyCode, event); |
| 68 | } |
Winson | 4e7a101 | 2015-08-13 16:47:55 -0700 | [diff] [blame] | 69 | |
| 70 | @Override |
| 71 | public boolean onDragEvent(DragEvent event) { |
| 72 | // We don't want this view to interfere with Launcher own drag and drop. |
| 73 | return false; |
| 74 | } |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame^] | 75 | |
| 76 | @Override |
| 77 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 78 | super.onLayout(changed, left, top, right, bottom); |
| 79 | if (mShowImeAfterFirstLayout) { |
| 80 | // soft input only shows one frame after the layout of the EditText happens, |
| 81 | post(new Runnable() { |
| 82 | @Override |
| 83 | public void run() { |
| 84 | showSoftInput(); |
| 85 | mShowImeAfterFirstLayout = false; |
| 86 | } |
| 87 | }); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public void showKeyboard() { |
| 92 | mShowImeAfterFirstLayout = !showSoftInput(); |
| 93 | } |
| 94 | |
| 95 | private boolean showSoftInput() { |
| 96 | return requestFocus() && |
| 97 | ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)) |
| 98 | .showSoftInput(this, InputMethodManager.SHOW_FORCED); |
| 99 | } |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 100 | } |