blob: 8ff030eb485ccb2244ba677a57c338c96c9c4320 [file] [log] [blame]
Winson Chungde34aa42015-05-07 18:21:28 -07001/*
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 */
Winson97b0d082015-08-13 15:18:25 -070016package com.android.launcher3;
Winson Chungde34aa42015-05-07 18:21:28 -070017
Anushree Ganjama4616602022-07-25 22:20:18 +000018import static com.android.launcher3.logging.KeyboardStateManager.KeyboardState.SHOW;
Sunny Goyal59969372021-05-06 12:11:44 -070019
Winson Chungde34aa42015-05-07 18:21:28 -070020import android.content.Context;
Luca Zuccarini0a3d67f2022-11-17 13:04:59 +000021import android.graphics.Rect;
Sunny Goyal326403e2017-10-02 12:45:10 -070022import android.text.TextUtils;
Winson Chungde34aa42015-05-07 18:21:28 -070023import android.util.AttributeSet;
Luca Zuccarini95bc2ce2023-08-04 13:18:16 +000024import android.util.Log;
Winson4e7a1012015-08-13 16:47:55 -070025import android.view.DragEvent;
Winson Chungde34aa42015-05-07 18:21:28 -070026import android.view.KeyEvent;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070027import android.view.inputmethod.InputMethodManager;
Winson Chungde34aa42015-05-07 18:21:28 -070028import android.widget.EditText;
29
Sunny Goyal59969372021-05-06 12:11:44 -070030import com.android.launcher3.views.ActivityContext;
Sunny Goyal326403e2017-10-02 12:45:10 -070031
Luca Zuccarini0a3d67f2022-11-17 13:04:59 +000032import java.util.HashSet;
33import java.util.Set;
34
Winson Chungde34aa42015-05-07 18:21:28 -070035
36/**
Winson97b0d082015-08-13 15:18:25 -070037 * The edit text that reports back when the back key has been pressed.
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080038 * Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion
Winson Chungde34aa42015-05-07 18:21:28 -070039 */
Winson97b0d082015-08-13 15:18:25 -070040public class ExtendedEditText extends EditText {
Luca Zuccarini95bc2ce2023-08-04 13:18:16 +000041 private static final String TAG = "ExtendedEditText";
42
Luca Zuccarini0a3d67f2022-11-17 13:04:59 +000043 private final Set<OnFocusChangeListener> mOnFocusChangeListeners = new HashSet<>();
44
Jon Miranda54d4e642017-02-24 10:00:14 -080045 private boolean mForceDisableSuggestions = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070046
Winson Chungde34aa42015-05-07 18:21:28 -070047 /**
48 * Implemented by listeners of the back key.
49 */
50 public interface OnBackKeyListener {
Hyunyoung Song54d1f882020-01-10 23:37:16 -080051 boolean onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070052 }
53
54 private OnBackKeyListener mBackKeyListener;
55
Winson97b0d082015-08-13 15:18:25 -070056 public ExtendedEditText(Context context) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070057 // ctor chaining breaks the touch handling
58 super(context);
Winson Chungde34aa42015-05-07 18:21:28 -070059 }
60
Winson97b0d082015-08-13 15:18:25 -070061 public ExtendedEditText(Context context, AttributeSet attrs) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070062 // ctor chaining breaks the touch handling
63 super(context, attrs);
Winson Chungde34aa42015-05-07 18:21:28 -070064 }
65
Winson97b0d082015-08-13 15:18:25 -070066 public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chungde34aa42015-05-07 18:21:28 -070067 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 Wickham336c6662023-08-25 16:56:30 -070077 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP
78 && mBackKeyListener != null) {
79 return mBackKeyListener.onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070080 }
81 return super.onKeyPreIme(keyCode, event);
82 }
Winson4e7a1012015-08-13 16:47:55 -070083
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 Songc2fe1142016-09-09 15:02:20 -070089
Luca Zuccarini95bc2ce2023-08-04 13:18:16 +000090 /**
91 * Synchronously shows the soft input method.
92 *
93 * @param shouldFocus whether this EditText should also request focus.
94 * @return true if the keyboard is shown correctly and focus is given to this view (if
95 * applicable).
96 */
97 public boolean showKeyboard(boolean shouldFocus) {
Anushree Ganjama4616602022-07-25 22:20:18 +000098 onKeyboardShown();
Luca Zuccarini95bc2ce2023-08-04 13:18:16 +000099 boolean focusResult = !shouldFocus || requestFocus();
100 return focusResult && showSoftInputInternal();
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700101 }
102
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700103 public void hideKeyboard() {
Pat Manningb53d8d62023-10-25 10:27:40 +0100104 hideKeyboard(/* clearFocus= */ true);
105 }
106
107 public void hideKeyboard(boolean clearFocus) {
Sunny Goyal8958a702022-09-09 15:54:10 -0700108 ActivityContext.lookupContext(getContext()).hideKeyboard();
Pat Manningb53d8d62023-10-25 10:27:40 +0100109 if (clearFocus) {
110 clearFocus();
111 }
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700112 }
113
Anushree Ganjama4616602022-07-25 22:20:18 +0000114 protected void onKeyboardShown() {
115 ActivityContext.lookupContext(getContext()).getStatsLogManager()
116 .keyboardStateManager().setKeyboardState(SHOW);
117 }
118
Luca Zuccarini95bc2ce2023-08-04 13:18:16 +0000119 private boolean showSoftInputInternal() {
120 boolean result = false;
121 InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
122 if (imm != null) {
123 result = imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
124 } else {
125 Log.w(TAG, "Failed to retrieve InputMethodManager from the system.");
126 }
127 return result;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700128 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700129
130 public void dispatchBackKey() {
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700131 hideKeyboard();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700132 if (mBackKeyListener != null) {
133 mBackKeyListener.onBackKey();
134 }
135 }
Jon Miranda54d4e642017-02-24 10:00:14 -0800136
137 /**
138 * Set to true when you want isSuggestionsEnabled to return false.
139 * Use this to disable the red underlines that appear under typos when suggestions is enabled.
140 */
141 public void forceDisableSuggestions(boolean forceDisableSuggestions) {
142 mForceDisableSuggestions = forceDisableSuggestions;
143 }
144
145 @Override
146 public boolean isSuggestionsEnabled() {
147 return !mForceDisableSuggestions && super.isSuggestionsEnabled();
148 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700149
150 public void reset() {
151 if (!TextUtils.isEmpty(getText())) {
152 setText("");
Hyunyoung Songa907a992021-03-10 10:18:03 -0800153 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700154 }
Luca Zuccarini0a3d67f2022-11-17 13:04:59 +0000155
156 /**
157 * This method should be preferred to {@link #setOnFocusChangeListener(OnFocusChangeListener)},
158 * as it allows for multiple listeners from different sources.
159 */
160 public void addOnFocusChangeListener(OnFocusChangeListener listener) {
161 mOnFocusChangeListeners.add(listener);
162 }
163
164 /**
165 * Removes the given listener from the set of registered focus listeners, or does nothing if it
166 * wasn't registered in the first place.
167 */
168 public void removeOnFocusChangeListener(OnFocusChangeListener listener) {
169 mOnFocusChangeListeners.remove(listener);
170 }
171
172 @Override
173 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
174 super.onFocusChanged(focused, direction, previouslyFocusedRect);
175 for (OnFocusChangeListener listener : mOnFocusChangeListeners) {
176 listener.onFocusChange(this, focused);
177 }
178 }
Holly Sun196d55a2022-12-08 14:05:37 -0800179
180 /**
181 * Save the input, suggestion, hint states when it's on focus, and set to unfocused states.
182 */
183 public void saveFocusedStateAndUpdateToUnfocusedState() {}
184
185 /**
186 * Restore to the previous saved focused state.
187 */
188 public void restoreToFocusedState() {}
Winson Chungde34aa42015-05-07 18:21:28 -0700189}