blob: d64967b75e6418d0193a0a8546816c435b4a7986 [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
Sunny Goyal326403e2017-10-02 12:45:10 -070027import com.android.launcher3.util.UiThreadHelper;
28
Winson Chungde34aa42015-05-07 18:21:28 -070029
30/**
Winson97b0d082015-08-13 15:18:25 -070031 * The edit text that reports back when the back key has been pressed.
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080032 * Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion
Winson Chungde34aa42015-05-07 18:21:28 -070033 */
Winson97b0d082015-08-13 15:18:25 -070034public class ExtendedEditText extends EditText {
Winson Chungde34aa42015-05-07 18:21:28 -070035
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070036 private boolean mShowImeAfterFirstLayout;
Jon Miranda54d4e642017-02-24 10:00:14 -080037 private boolean mForceDisableSuggestions = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070038
Winson Chungde34aa42015-05-07 18:21:28 -070039 /**
40 * Implemented by listeners of the back key.
41 */
42 public interface OnBackKeyListener {
Hyunyoung Song54d1f882020-01-10 23:37:16 -080043 boolean onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070044 }
45
46 private OnBackKeyListener mBackKeyListener;
47
Winson97b0d082015-08-13 15:18:25 -070048 public ExtendedEditText(Context context) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070049 // ctor chaining breaks the touch handling
50 super(context);
Winson Chungde34aa42015-05-07 18:21:28 -070051 }
52
Winson97b0d082015-08-13 15:18:25 -070053 public ExtendedEditText(Context context, AttributeSet attrs) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070054 // ctor chaining breaks the touch handling
55 super(context, attrs);
Winson Chungde34aa42015-05-07 18:21:28 -070056 }
57
Winson97b0d082015-08-13 15:18:25 -070058 public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chungde34aa42015-05-07 18:21:28 -070059 super(context, attrs, defStyleAttr);
60 }
61
62 public void setOnBackKeyListener(OnBackKeyListener listener) {
63 mBackKeyListener = listener;
64 }
65
66 @Override
67 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
68 // If this is a back key, propagate the key back to the listener
69 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
70 if (mBackKeyListener != null) {
Winson97b0d082015-08-13 15:18:25 -070071 return mBackKeyListener.onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070072 }
73 return false;
74 }
75 return super.onKeyPreIme(keyCode, event);
76 }
Winson4e7a1012015-08-13 16:47:55 -070077
78 @Override
79 public boolean onDragEvent(DragEvent event) {
80 // We don't want this view to interfere with Launcher own drag and drop.
81 return false;
82 }
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070083
84 @Override
85 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
86 super.onLayout(changed, left, top, right, bottom);
87 if (mShowImeAfterFirstLayout) {
88 // soft input only shows one frame after the layout of the EditText happens,
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080089 post(() -> {
90 showSoftInput();
91 mShowImeAfterFirstLayout = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070092 });
93 }
94 }
95
96 public void showKeyboard() {
97 mShowImeAfterFirstLayout = !showSoftInput();
98 }
99
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700100 public void hideKeyboard() {
101 UiThreadHelper.hideKeyboardAsync(getContext(), getWindowToken());
102 }
103
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700104 private boolean showSoftInput() {
105 return requestFocus() &&
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -0800106 getContext().getSystemService(InputMethodManager.class)
Hyunyoung Song7e83ab92016-09-21 17:03:56 -0700107 .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700108 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700109
110 public void dispatchBackKey() {
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700111 hideKeyboard();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700112 if (mBackKeyListener != null) {
113 mBackKeyListener.onBackKey();
114 }
115 }
Jon Miranda54d4e642017-02-24 10:00:14 -0800116
117 /**
118 * Set to true when you want isSuggestionsEnabled to return false.
119 * Use this to disable the red underlines that appear under typos when suggestions is enabled.
120 */
121 public void forceDisableSuggestions(boolean forceDisableSuggestions) {
122 mForceDisableSuggestions = forceDisableSuggestions;
123 }
124
125 @Override
126 public boolean isSuggestionsEnabled() {
127 return !mForceDisableSuggestions && super.isSuggestionsEnabled();
128 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700129
130 public void reset() {
131 if (!TextUtils.isEmpty(getText())) {
132 setText("");
133 }
134 if (isFocused()) {
135 View nextFocus = focusSearch(View.FOCUS_DOWN);
136 if (nextFocus != null) {
137 nextFocus.requestFocus();
138 }
139 }
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700140 hideKeyboard();
Sunny Goyal326403e2017-10-02 12:45:10 -0700141 }
Winson Chungde34aa42015-05-07 18:21:28 -0700142}