blob: 20c69380cc1f1e224b6ec79f608a2acc6addb447 [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
Sunny Goyal59969372021-05-06 12:11:44 -070018import static com.android.launcher3.util.UiThreadHelper.hideKeyboardAsync;
19
Winson Chungde34aa42015-05-07 18:21:28 -070020import android.content.Context;
Sunny Goyal326403e2017-10-02 12:45:10 -070021import android.text.TextUtils;
Winson Chungde34aa42015-05-07 18:21:28 -070022import android.util.AttributeSet;
Winson4e7a1012015-08-13 16:47:55 -070023import android.view.DragEvent;
Winson Chungde34aa42015-05-07 18:21:28 -070024import android.view.KeyEvent;
Sunny Goyal326403e2017-10-02 12:45:10 -070025import android.view.View;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070026import android.view.inputmethod.InputMethodManager;
Winson Chungde34aa42015-05-07 18:21:28 -070027import android.widget.EditText;
28
Hyunyoung Songb9f9d692020-08-19 00:58:34 -070029import com.android.launcher3.config.FeatureFlags;
Sunny Goyal59969372021-05-06 12:11:44 -070030import com.android.launcher3.views.ActivityContext;
Sunny Goyal326403e2017-10-02 12:45:10 -070031
Winson Chungde34aa42015-05-07 18:21:28 -070032
33/**
Winson97b0d082015-08-13 15:18:25 -070034 * The edit text that reports back when the back key has been pressed.
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080035 * Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion
Winson Chungde34aa42015-05-07 18:21:28 -070036 */
Winson97b0d082015-08-13 15:18:25 -070037public class ExtendedEditText extends EditText {
Winson Chungde34aa42015-05-07 18:21:28 -070038
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070039 private boolean mShowImeAfterFirstLayout;
Jon Miranda54d4e642017-02-24 10:00:14 -080040 private boolean mForceDisableSuggestions = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070041
Winson Chungde34aa42015-05-07 18:21:28 -070042 /**
43 * Implemented by listeners of the back key.
44 */
45 public interface OnBackKeyListener {
Hyunyoung Song54d1f882020-01-10 23:37:16 -080046 boolean onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070047 }
48
49 private OnBackKeyListener mBackKeyListener;
50
Winson97b0d082015-08-13 15:18:25 -070051 public ExtendedEditText(Context context) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070052 // ctor chaining breaks the touch handling
53 super(context);
Winson Chungde34aa42015-05-07 18:21:28 -070054 }
55
Winson97b0d082015-08-13 15:18:25 -070056 public ExtendedEditText(Context context, AttributeSet attrs) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070057 // ctor chaining breaks the touch handling
58 super(context, attrs);
Winson Chungde34aa42015-05-07 18:21:28 -070059 }
60
Winson97b0d082015-08-13 15:18:25 -070061 public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chungde34aa42015-05-07 18:21:28 -070062 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) {
Winson97b0d082015-08-13 15:18:25 -070074 return mBackKeyListener.onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070075 }
76 return false;
77 }
78 return super.onKeyPreIme(keyCode, event);
79 }
Winson4e7a1012015-08-13 16:47:55 -070080
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 Songc2fe1142016-09-09 15:02:20 -070086
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 Song5fcd8f92019-11-06 21:34:36 -080092 post(() -> {
93 showSoftInput();
94 mShowImeAfterFirstLayout = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070095 });
96 }
97 }
98
99 public void showKeyboard() {
100 mShowImeAfterFirstLayout = !showSoftInput();
101 }
102
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700103 public void hideKeyboard() {
Sunny Goyal59969372021-05-06 12:11:44 -0700104 hideKeyboardAsync(ActivityContext.lookupContext(getContext()), getWindowToken());
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700105 }
106
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700107 private boolean showSoftInput() {
108 return requestFocus() &&
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -0800109 getContext().getSystemService(InputMethodManager.class)
Hyunyoung Song7e83ab92016-09-21 17:03:56 -0700110 .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700111 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700112
113 public void dispatchBackKey() {
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700114 hideKeyboard();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700115 if (mBackKeyListener != null) {
116 mBackKeyListener.onBackKey();
117 }
118 }
Jon Miranda54d4e642017-02-24 10:00:14 -0800119
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 Goyal326403e2017-10-02 12:45:10 -0700132
133 public void reset() {
134 if (!TextUtils.isEmpty(getText())) {
135 setText("");
Hyunyoung Songa907a992021-03-10 10:18:03 -0800136 }
137 if (FeatureFlags.ENABLE_DEVICE_SEARCH.get()) {
138 return;
Sunny Goyal326403e2017-10-02 12:45:10 -0700139 }
140 if (isFocused()) {
141 View nextFocus = focusSearch(View.FOCUS_DOWN);
142 if (nextFocus != null) {
143 nextFocus.requestFocus();
144 }
145 }
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700146 hideKeyboard();
Sunny Goyal326403e2017-10-02 12:45:10 -0700147 }
Winson Chungde34aa42015-05-07 18:21:28 -0700148}