blob: 02c61620fe36c647caf6f1e52ac236c1ae6a458d [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
18import android.content.Context;
Sunny Goyal326403e2017-10-02 12:45:10 -070019import android.text.TextUtils;
Winson Chungde34aa42015-05-07 18:21:28 -070020import android.util.AttributeSet;
Winson4e7a1012015-08-13 16:47:55 -070021import android.view.DragEvent;
Winson Chungde34aa42015-05-07 18:21:28 -070022import android.view.KeyEvent;
Sunny Goyal326403e2017-10-02 12:45:10 -070023import android.view.View;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070024import android.view.inputmethod.InputMethodManager;
Winson Chungde34aa42015-05-07 18:21:28 -070025import android.widget.EditText;
26
Hyunyoung Songb9f9d692020-08-19 00:58:34 -070027import com.android.launcher3.config.FeatureFlags;
Sunny Goyal326403e2017-10-02 12:45:10 -070028import com.android.launcher3.util.UiThreadHelper;
29
Winson Chungde34aa42015-05-07 18:21:28 -070030
31/**
Winson97b0d082015-08-13 15:18:25 -070032 * The edit text that reports back when the back key has been pressed.
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080033 * Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion
Winson Chungde34aa42015-05-07 18:21:28 -070034 */
Winson97b0d082015-08-13 15:18:25 -070035public class ExtendedEditText extends EditText {
Winson Chungde34aa42015-05-07 18:21:28 -070036
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070037 private boolean mShowImeAfterFirstLayout;
Jon Miranda54d4e642017-02-24 10:00:14 -080038 private boolean mForceDisableSuggestions = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070039
Winson Chungde34aa42015-05-07 18:21:28 -070040 /**
41 * Implemented by listeners of the back key.
42 */
43 public interface OnBackKeyListener {
Hyunyoung Song54d1f882020-01-10 23:37:16 -080044 boolean onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070045 }
46
47 private OnBackKeyListener mBackKeyListener;
48
Winson97b0d082015-08-13 15:18:25 -070049 public ExtendedEditText(Context context) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070050 // ctor chaining breaks the touch handling
51 super(context);
Winson Chungde34aa42015-05-07 18:21:28 -070052 }
53
Winson97b0d082015-08-13 15:18:25 -070054 public ExtendedEditText(Context context, AttributeSet attrs) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070055 // ctor chaining breaks the touch handling
56 super(context, attrs);
Winson Chungde34aa42015-05-07 18:21:28 -070057 }
58
Winson97b0d082015-08-13 15:18:25 -070059 public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chungde34aa42015-05-07 18:21:28 -070060 super(context, attrs, defStyleAttr);
61 }
62
63 public void setOnBackKeyListener(OnBackKeyListener listener) {
64 mBackKeyListener = listener;
65 }
66
67 @Override
68 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
69 // If this is a back key, propagate the key back to the listener
70 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
71 if (mBackKeyListener != null) {
Winson97b0d082015-08-13 15:18:25 -070072 return mBackKeyListener.onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070073 }
74 return false;
75 }
76 return super.onKeyPreIme(keyCode, event);
77 }
Winson4e7a1012015-08-13 16:47:55 -070078
79 @Override
80 public boolean onDragEvent(DragEvent event) {
81 // We don't want this view to interfere with Launcher own drag and drop.
82 return false;
83 }
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070084
85 @Override
86 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
87 super.onLayout(changed, left, top, right, bottom);
88 if (mShowImeAfterFirstLayout) {
89 // soft input only shows one frame after the layout of the EditText happens,
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080090 post(() -> {
91 showSoftInput();
92 mShowImeAfterFirstLayout = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070093 });
94 }
95 }
96
97 public void showKeyboard() {
98 mShowImeAfterFirstLayout = !showSoftInput();
99 }
100
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700101 public void hideKeyboard() {
102 UiThreadHelper.hideKeyboardAsync(getContext(), getWindowToken());
103 }
104
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700105 private boolean showSoftInput() {
106 return requestFocus() &&
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -0800107 getContext().getSystemService(InputMethodManager.class)
Hyunyoung Song7e83ab92016-09-21 17:03:56 -0700108 .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700109 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700110
111 public void dispatchBackKey() {
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700112 hideKeyboard();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700113 if (mBackKeyListener != null) {
114 mBackKeyListener.onBackKey();
115 }
116 }
Jon Miranda54d4e642017-02-24 10:00:14 -0800117
118 /**
119 * Set to true when you want isSuggestionsEnabled to return false.
120 * Use this to disable the red underlines that appear under typos when suggestions is enabled.
121 */
122 public void forceDisableSuggestions(boolean forceDisableSuggestions) {
123 mForceDisableSuggestions = forceDisableSuggestions;
124 }
125
126 @Override
127 public boolean isSuggestionsEnabled() {
128 return !mForceDisableSuggestions && super.isSuggestionsEnabled();
129 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700130
131 public void reset() {
132 if (!TextUtils.isEmpty(getText())) {
133 setText("");
Hyunyoung Songb9f9d692020-08-19 00:58:34 -0700134 } else {
135 if (FeatureFlags.ENABLE_DEVICE_SEARCH.get()) {
136 return;
137 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700138 }
139 if (isFocused()) {
140 View nextFocus = focusSearch(View.FOCUS_DOWN);
141 if (nextFocus != null) {
142 nextFocus.requestFocus();
143 }
144 }
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700145 hideKeyboard();
Sunny Goyal326403e2017-10-02 12:45:10 -0700146 }
Winson Chungde34aa42015-05-07 18:21:28 -0700147}