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 | |
Anushree Ganjam | a461660 | 2022-07-25 22:20:18 +0000 | [diff] [blame] | 18 | import static com.android.launcher3.logging.KeyboardStateManager.KeyboardState.SHOW; |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame] | 19 | |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Luca Zuccarini | 0a3d67f | 2022-11-17 13:04:59 +0000 | [diff] [blame] | 21 | import android.graphics.Rect; |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Luca Zuccarini | 95bc2ce | 2023-08-04 13:18:16 +0000 | [diff] [blame] | 24 | import android.util.Log; |
Winson | 4e7a101 | 2015-08-13 16:47:55 -0700 | [diff] [blame] | 25 | import android.view.DragEvent; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 26 | import android.view.KeyEvent; |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 27 | import android.view.inputmethod.InputMethodManager; |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 28 | import android.widget.EditText; |
| 29 | |
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 | |
Luca Zuccarini | 0a3d67f | 2022-11-17 13:04:59 +0000 | [diff] [blame] | 32 | import java.util.HashSet; |
| 33 | import java.util.Set; |
| 34 | |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 37 | * 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] | 38 | * Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 39 | */ |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 40 | public class ExtendedEditText extends EditText { |
Luca Zuccarini | 95bc2ce | 2023-08-04 13:18:16 +0000 | [diff] [blame] | 41 | private static final String TAG = "ExtendedEditText"; |
| 42 | |
Luca Zuccarini | 0a3d67f | 2022-11-17 13:04:59 +0000 | [diff] [blame] | 43 | private final Set<OnFocusChangeListener> mOnFocusChangeListeners = new HashSet<>(); |
| 44 | |
Jon Miranda | 54d4e64 | 2017-02-24 10:00:14 -0800 | [diff] [blame] | 45 | private boolean mForceDisableSuggestions = false; |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 46 | |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Implemented by listeners of the back key. |
| 49 | */ |
| 50 | public interface OnBackKeyListener { |
Hyunyoung Song | 54d1f88 | 2020-01-10 23:37:16 -0800 | [diff] [blame] | 51 | boolean onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private OnBackKeyListener mBackKeyListener; |
| 55 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 56 | public ExtendedEditText(Context context) { |
Hyunyoung Song | 3f9d647 | 2016-09-20 12:37:41 -0700 | [diff] [blame] | 57 | // ctor chaining breaks the touch handling |
| 58 | super(context); |
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) { |
Hyunyoung Song | 3f9d647 | 2016-09-20 12:37:41 -0700 | [diff] [blame] | 62 | // ctor chaining breaks the touch handling |
| 63 | super(context, attrs); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Winson | 97b0d08 | 2015-08-13 15:18:25 -0700 | [diff] [blame] | 66 | public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 67 | super(context, attrs, defStyleAttr); |
| 68 | } |
| 69 | |
| 70 | public void setOnBackKeyListener(OnBackKeyListener listener) { |
| 71 | mBackKeyListener = listener; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 76 | // If this is a back key, propagate the key back to the listener |
Andy Wickham | 336c666 | 2023-08-25 16:56:30 -0700 | [diff] [blame] | 77 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP |
| 78 | && mBackKeyListener != null) { |
| 79 | return mBackKeyListener.onBackKey(); |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 80 | } |
| 81 | return super.onKeyPreIme(keyCode, event); |
| 82 | } |
Winson | 4e7a101 | 2015-08-13 16:47:55 -0700 | [diff] [blame] | 83 | |
| 84 | @Override |
| 85 | public boolean onDragEvent(DragEvent event) { |
| 86 | // We don't want this view to interfere with Launcher own drag and drop. |
| 87 | return false; |
| 88 | } |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 89 | |
Luca Zuccarini | 95bc2ce | 2023-08-04 13:18:16 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Synchronously shows the soft input method. |
| 92 | * |
Luca Zuccarini | 6ab6106 | 2023-11-13 17:58:19 +0000 | [diff] [blame] | 93 | * @return true if the keyboard is shown correctly and focus is given to this view. |
Luca Zuccarini | 95bc2ce | 2023-08-04 13:18:16 +0000 | [diff] [blame] | 94 | */ |
Luca Zuccarini | 6ab6106 | 2023-11-13 17:58:19 +0000 | [diff] [blame] | 95 | public boolean showKeyboard() { |
Anushree Ganjam | a461660 | 2022-07-25 22:20:18 +0000 | [diff] [blame] | 96 | onKeyboardShown(); |
Luca Zuccarini | 6ab6106 | 2023-11-13 17:58:19 +0000 | [diff] [blame] | 97 | return requestFocus() && showSoftInputInternal(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Requests the framework to show the keyboard in order to ensure that an already registered |
| 102 | * controlled keyboard animation is triggered correctly. |
| 103 | * Must NEVER be called in any other case than to trigger a pre-registered controlled animation. |
| 104 | */ |
| 105 | public void requestShowKeyboardForControlledAnimation() { |
| 106 | // We don't log the keyboard state, as that must happen only after the controlled animation |
| 107 | // has completed. |
| 108 | // We also must not request focus, as this triggers unwanted side effects. |
| 109 | showSoftInputInternal(); |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Mehdi Alizadeh | 25b4dfe | 2018-06-05 16:22:35 -0700 | [diff] [blame] | 112 | public void hideKeyboard() { |
Pat Manning | b53d8d6 | 2023-10-25 10:27:40 +0100 | [diff] [blame] | 113 | hideKeyboard(/* clearFocus= */ true); |
| 114 | } |
| 115 | |
| 116 | public void hideKeyboard(boolean clearFocus) { |
Sunny Goyal | 8958a70 | 2022-09-09 15:54:10 -0700 | [diff] [blame] | 117 | ActivityContext.lookupContext(getContext()).hideKeyboard(); |
Pat Manning | b53d8d6 | 2023-10-25 10:27:40 +0100 | [diff] [blame] | 118 | if (clearFocus) { |
| 119 | clearFocus(); |
| 120 | } |
Mehdi Alizadeh | 25b4dfe | 2018-06-05 16:22:35 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Anushree Ganjam | a461660 | 2022-07-25 22:20:18 +0000 | [diff] [blame] | 123 | protected void onKeyboardShown() { |
| 124 | ActivityContext.lookupContext(getContext()).getStatsLogManager() |
| 125 | .keyboardStateManager().setKeyboardState(SHOW); |
| 126 | } |
| 127 | |
Luca Zuccarini | 95bc2ce | 2023-08-04 13:18:16 +0000 | [diff] [blame] | 128 | private boolean showSoftInputInternal() { |
| 129 | boolean result = false; |
| 130 | InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); |
| 131 | if (imm != null) { |
| 132 | result = imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT); |
| 133 | } else { |
| 134 | Log.w(TAG, "Failed to retrieve InputMethodManager from the system."); |
| 135 | } |
| 136 | return result; |
Hyunyoung Song | c2fe114 | 2016-09-09 15:02:20 -0700 | [diff] [blame] | 137 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 138 | |
| 139 | public void dispatchBackKey() { |
Mehdi Alizadeh | 25b4dfe | 2018-06-05 16:22:35 -0700 | [diff] [blame] | 140 | hideKeyboard(); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 141 | if (mBackKeyListener != null) { |
| 142 | mBackKeyListener.onBackKey(); |
| 143 | } |
| 144 | } |
Jon Miranda | 54d4e64 | 2017-02-24 10:00:14 -0800 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Set to true when you want isSuggestionsEnabled to return false. |
| 148 | * Use this to disable the red underlines that appear under typos when suggestions is enabled. |
| 149 | */ |
| 150 | public void forceDisableSuggestions(boolean forceDisableSuggestions) { |
| 151 | mForceDisableSuggestions = forceDisableSuggestions; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public boolean isSuggestionsEnabled() { |
| 156 | return !mForceDisableSuggestions && super.isSuggestionsEnabled(); |
| 157 | } |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 158 | |
| 159 | public void reset() { |
| 160 | if (!TextUtils.isEmpty(getText())) { |
| 161 | setText(""); |
Hyunyoung Song | a907a99 | 2021-03-10 10:18:03 -0800 | [diff] [blame] | 162 | } |
Sunny Goyal | 326403e | 2017-10-02 12:45:10 -0700 | [diff] [blame] | 163 | } |
Luca Zuccarini | 0a3d67f | 2022-11-17 13:04:59 +0000 | [diff] [blame] | 164 | |
Anushree Ganjam | c3bf394 | 2023-11-15 13:31:26 -0800 | [diff] [blame] | 165 | @Override |
| 166 | public void setText(CharSequence text, BufferType type) { |
| 167 | super.setText(text, type); |
| 168 | // With hardware keyboard, there is a possibility that the user types before edit |
| 169 | // text is visible during the transition. |
| 170 | // So move the cursor to the end of the text. |
| 171 | setSelection(getText().length()); |
| 172 | } |
| 173 | |
Luca Zuccarini | 0a3d67f | 2022-11-17 13:04:59 +0000 | [diff] [blame] | 174 | /** |
| 175 | * This method should be preferred to {@link #setOnFocusChangeListener(OnFocusChangeListener)}, |
| 176 | * as it allows for multiple listeners from different sources. |
| 177 | */ |
| 178 | public void addOnFocusChangeListener(OnFocusChangeListener listener) { |
| 179 | mOnFocusChangeListeners.add(listener); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Removes the given listener from the set of registered focus listeners, or does nothing if it |
| 184 | * wasn't registered in the first place. |
| 185 | */ |
| 186 | public void removeOnFocusChangeListener(OnFocusChangeListener listener) { |
| 187 | mOnFocusChangeListeners.remove(listener); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 192 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 193 | for (OnFocusChangeListener listener : mOnFocusChangeListeners) { |
| 194 | listener.onFocusChange(this, focused); |
| 195 | } |
| 196 | } |
Holly Sun | 196d55a | 2022-12-08 14:05:37 -0800 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * Save the input, suggestion, hint states when it's on focus, and set to unfocused states. |
| 200 | */ |
| 201 | public void saveFocusedStateAndUpdateToUnfocusedState() {} |
| 202 | |
| 203 | /** |
| 204 | * Restore to the previous saved focused state. |
| 205 | */ |
| 206 | public void restoreToFocusedState() {} |
Winson Chung | de34aa4 | 2015-05-07 18:21:28 -0700 | [diff] [blame] | 207 | } |