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; |
Jon Miranda | 54d4e64 | 2017-02-24 10:00:14 -0800 | [diff] [blame] | 32 | private boolean mForceDisableSuggestions = false; |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 33 | |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Implemented by listeners of the back key. |
| 36 | */ |
| 37 | public interface OnBackKeyListener { |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 38 | public boolean onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | private OnBackKeyListener mBackKeyListener; |
| 42 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 43 | public ExtendedEditText(Context context) { |
Hyunyoung Song | 3f9d647 | 2016-09-20 12:37:41 -0700 | [diff] [blame] | 44 | // ctor chaining breaks the touch handling |
| 45 | super(context); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 48 | public ExtendedEditText(Context context, AttributeSet attrs) { |
Hyunyoung Song | 3f9d647 | 2016-09-20 12:37:41 -0700 | [diff] [blame] | 49 | // ctor chaining breaks the touch handling |
| 50 | super(context, attrs); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 53 | public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 54 | super(context, attrs, defStyleAttr); |
| 55 | } |
| 56 | |
| 57 | public void setOnBackKeyListener(OnBackKeyListener listener) { |
| 58 | mBackKeyListener = listener; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 63 | // If this is a back key, propagate the key back to the listener |
| 64 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { |
| 65 | if (mBackKeyListener != null) { |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 66 | return mBackKeyListener.onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | return super.onKeyPreIme(keyCode, event); |
| 71 | } |
Winson | 4e7a101 | 2015-08-13 16:47:55 -0700 | [diff] [blame] | 72 | |
| 73 | @Override |
| 74 | public boolean onDragEvent(DragEvent event) { |
| 75 | // We don't want this view to interfere with Launcher own drag and drop. |
| 76 | return false; |
| 77 | } |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 78 | |
| 79 | @Override |
| 80 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 81 | super.onLayout(changed, left, top, right, bottom); |
| 82 | if (mShowImeAfterFirstLayout) { |
| 83 | // soft input only shows one frame after the layout of the EditText happens, |
| 84 | post(new Runnable() { |
| 85 | @Override |
| 86 | public void run() { |
| 87 | showSoftInput(); |
| 88 | mShowImeAfterFirstLayout = false; |
| 89 | } |
| 90 | }); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public void showKeyboard() { |
| 95 | mShowImeAfterFirstLayout = !showSoftInput(); |
| 96 | } |
| 97 | |
| 98 | private boolean showSoftInput() { |
| 99 | return requestFocus() && |
| 100 | ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)) |
Hyunyoung Song | 7e83ab9 | 2016-09-21 17:03:56 -0700 | [diff] [blame] | 101 | .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT); |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 102 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 103 | |
| 104 | public void dispatchBackKey() { |
| 105 | ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)) |
| 106 | .hideSoftInputFromWindow(getWindowToken(), 0); |
| 107 | if (mBackKeyListener != null) { |
| 108 | mBackKeyListener.onBackKey(); |
| 109 | } |
| 110 | } |
Jon Miranda | 54d4e64 | 2017-02-24 10:00:14 -0800 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Set to true when you want isSuggestionsEnabled to return false. |
| 114 | * Use this to disable the red underlines that appear under typos when suggestions is enabled. |
| 115 | */ |
| 116 | public void forceDisableSuggestions(boolean forceDisableSuggestions) { |
| 117 | mForceDisableSuggestions = forceDisableSuggestions; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean isSuggestionsEnabled() { |
| 122 | return !mForceDisableSuggestions && super.isSuggestionsEnabled(); |
| 123 | } |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 124 | } |