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