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 | |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame^] | 18 | import static com.android.launcher3.util.UiThreadHelper.hideKeyboardAsync; |
| 19 | |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 21 | import android.text.TextUtils; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Winson | 4e7a101 | 2015-08-13 16:47:55 -0700 | [diff] [blame] | 23 | import android.view.DragEvent; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 24 | import android.view.KeyEvent; |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 25 | import android.view.View; |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 26 | import android.view.inputmethod.InputMethodManager; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 27 | import android.widget.EditText; |
| 28 | |
Hyunyoung Song | b9f9d69 | 2020-08-19 00:58:34 -0700 | [diff] [blame] | 29 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame^] | 30 | import com.android.launcher3.views.ActivityContext; |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 31 | |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 34 | * The edit text that reports back when the back key has been pressed. |
Hyunyoung Song | 5fcd8f9 | 2019-11-06 21:34:36 -0800 | [diff] [blame] | 35 | * Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 36 | */ |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 37 | public class ExtendedEditText extends EditText { |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 38 | |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 39 | private boolean mShowImeAfterFirstLayout; |
Jon Miranda | 54d4e64 | 2017-02-24 10:00:14 -0800 | [diff] [blame] | 40 | private boolean mForceDisableSuggestions = false; |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 41 | |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Implemented by listeners of the back key. |
| 44 | */ |
| 45 | public interface OnBackKeyListener { |
Hyunyoung Song | 54d1f88 | 2020-01-10 23:37:16 -0800 | [diff] [blame] | 46 | boolean onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | private OnBackKeyListener mBackKeyListener; |
| 50 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 51 | public ExtendedEditText(Context context) { |
Hyunyoung Song | 3f9d647 | 2016-09-20 12:37:41 -0700 | [diff] [blame] | 52 | // ctor chaining breaks the touch handling |
| 53 | super(context); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 56 | public ExtendedEditText(Context context, AttributeSet attrs) { |
Hyunyoung Song | 3f9d647 | 2016-09-20 12:37:41 -0700 | [diff] [blame] | 57 | // ctor chaining breaks the touch handling |
| 58 | super(context, attrs); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 61 | public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 62 | super(context, attrs, defStyleAttr); |
| 63 | } |
| 64 | |
| 65 | public void setOnBackKeyListener(OnBackKeyListener listener) { |
| 66 | mBackKeyListener = listener; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 71 | // If this is a back key, propagate the key back to the listener |
| 72 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { |
| 73 | if (mBackKeyListener != null) { |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 74 | return mBackKeyListener.onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | return super.onKeyPreIme(keyCode, event); |
| 79 | } |
Winson | 4e7a101 | 2015-08-13 16:47:55 -0700 | [diff] [blame] | 80 | |
| 81 | @Override |
| 82 | public boolean onDragEvent(DragEvent event) { |
| 83 | // We don't want this view to interfere with Launcher own drag and drop. |
| 84 | return false; |
| 85 | } |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 89 | super.onLayout(changed, left, top, right, bottom); |
| 90 | if (mShowImeAfterFirstLayout) { |
| 91 | // soft input only shows one frame after the layout of the EditText happens, |
Hyunyoung Song | 5fcd8f9 | 2019-11-06 21:34:36 -0800 | [diff] [blame] | 92 | post(() -> { |
| 93 | showSoftInput(); |
| 94 | mShowImeAfterFirstLayout = false; |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 95 | }); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public void showKeyboard() { |
| 100 | mShowImeAfterFirstLayout = !showSoftInput(); |
| 101 | } |
| 102 | |
Mehdi Alizadeh | 25b4dfe | 2018-06-05 16:22:35 -0700 | [diff] [blame] | 103 | public void hideKeyboard() { |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame^] | 104 | hideKeyboardAsync(ActivityContext.lookupContext(getContext()), getWindowToken()); |
Mehdi Alizadeh | 25b4dfe | 2018-06-05 16:22:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 107 | private boolean showSoftInput() { |
| 108 | return requestFocus() && |
Hyunyoung Song | 5fcd8f9 | 2019-11-06 21:34:36 -0800 | [diff] [blame] | 109 | getContext().getSystemService(InputMethodManager.class) |
Hyunyoung Song | 7e83ab9 | 2016-09-21 17:03:56 -0700 | [diff] [blame] | 110 | .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT); |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 111 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 112 | |
| 113 | public void dispatchBackKey() { |
Mehdi Alizadeh | 25b4dfe | 2018-06-05 16:22:35 -0700 | [diff] [blame] | 114 | hideKeyboard(); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 115 | if (mBackKeyListener != null) { |
| 116 | mBackKeyListener.onBackKey(); |
| 117 | } |
| 118 | } |
Jon Miranda | 54d4e64 | 2017-02-24 10:00:14 -0800 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Set to true when you want isSuggestionsEnabled to return false. |
| 122 | * Use this to disable the red underlines that appear under typos when suggestions is enabled. |
| 123 | */ |
| 124 | public void forceDisableSuggestions(boolean forceDisableSuggestions) { |
| 125 | mForceDisableSuggestions = forceDisableSuggestions; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean isSuggestionsEnabled() { |
| 130 | return !mForceDisableSuggestions && super.isSuggestionsEnabled(); |
| 131 | } |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 132 | |
| 133 | public void reset() { |
| 134 | if (!TextUtils.isEmpty(getText())) { |
| 135 | setText(""); |
Hyunyoung Song | a907a99 | 2021-03-10 10:18:03 -0800 | [diff] [blame] | 136 | } |
| 137 | if (FeatureFlags.ENABLE_DEVICE_SEARCH.get()) { |
| 138 | return; |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 139 | } |
| 140 | if (isFocused()) { |
| 141 | View nextFocus = focusSearch(View.FOCUS_DOWN); |
| 142 | if (nextFocus != null) { |
| 143 | nextFocus.requestFocus(); |
| 144 | } |
| 145 | } |
Mehdi Alizadeh | 25b4dfe | 2018-06-05 16:22:35 -0700 | [diff] [blame] | 146 | hideKeyboard(); |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 147 | } |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 148 | } |