blob: 8b6d2091b65bdfb393226dadd2d6b923d4cde0a9 [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 Song5fcd8f92019-11-06 21:34:36 -080024import android.view.inputmethod.CompletionInfo;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070025import android.view.inputmethod.InputMethodManager;
Winson Chungde34aa42015-05-07 18:21:28 -070026import android.widget.EditText;
27
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080028import com.android.launcher3.folder.FolderNameProvider;
Sunny Goyal326403e2017-10-02 12:45:10 -070029import com.android.launcher3.util.UiThreadHelper;
30
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080031import java.util.List;
32
Winson Chungde34aa42015-05-07 18:21:28 -070033
34/**
Winson97b0d082015-08-13 15:18:25 -070035 * The edit text that reports back when the back key has been pressed.
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080036 * Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion
Winson Chungde34aa42015-05-07 18:21:28 -070037 */
Winson97b0d082015-08-13 15:18:25 -070038public class ExtendedEditText extends EditText {
Winson Chungde34aa42015-05-07 18:21:28 -070039
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070040 private boolean mShowImeAfterFirstLayout;
Jon Miranda54d4e642017-02-24 10:00:14 -080041 private boolean mForceDisableSuggestions = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070042
Winson Chungde34aa42015-05-07 18:21:28 -070043 /**
44 * Implemented by listeners of the back key.
45 */
46 public interface OnBackKeyListener {
Winson97b0d082015-08-13 15:18:25 -070047 public boolean onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070048 }
49
50 private OnBackKeyListener mBackKeyListener;
51
Winson97b0d082015-08-13 15:18:25 -070052 public ExtendedEditText(Context context) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070053 // ctor chaining breaks the touch handling
54 super(context);
Winson Chungde34aa42015-05-07 18:21:28 -070055 }
56
Winson97b0d082015-08-13 15:18:25 -070057 public ExtendedEditText(Context context, AttributeSet attrs) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070058 // ctor chaining breaks the touch handling
59 super(context, attrs);
Winson Chungde34aa42015-05-07 18:21:28 -070060 }
61
Winson97b0d082015-08-13 15:18:25 -070062 public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chungde34aa42015-05-07 18:21:28 -070063 super(context, attrs, defStyleAttr);
64 }
65
66 public void setOnBackKeyListener(OnBackKeyListener listener) {
67 mBackKeyListener = listener;
68 }
69
70 @Override
71 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
72 // If this is a back key, propagate the key back to the listener
73 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
74 if (mBackKeyListener != null) {
Winson97b0d082015-08-13 15:18:25 -070075 return mBackKeyListener.onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070076 }
77 return false;
78 }
79 return super.onKeyPreIme(keyCode, event);
80 }
Winson4e7a1012015-08-13 16:47:55 -070081
82 @Override
83 public boolean onDragEvent(DragEvent event) {
84 // We don't want this view to interfere with Launcher own drag and drop.
85 return false;
86 }
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070087
88 @Override
89 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
90 super.onLayout(changed, left, top, right, bottom);
91 if (mShowImeAfterFirstLayout) {
92 // soft input only shows one frame after the layout of the EditText happens,
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080093 post(() -> {
94 showSoftInput();
95 mShowImeAfterFirstLayout = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070096 });
97 }
98 }
99
100 public void showKeyboard() {
101 mShowImeAfterFirstLayout = !showSoftInput();
102 }
103
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700104 public void hideKeyboard() {
105 UiThreadHelper.hideKeyboardAsync(getContext(), getWindowToken());
106 }
107
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -0800108 @Override
109 public void onCommitCompletion(CompletionInfo text) {
110 setText(text.getText());
Hyunyoung Song9153ff72019-12-27 06:16:26 -0800111 setSelection(text.getText().length());
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -0800112 }
113
114 /**
115 * Currently only used for folder name suggestion.
116 */
117 public void displayCompletions(List<String> suggestList) {
118 int cnt = Math.min(suggestList.size(), FolderNameProvider.SUGGEST_MAX);
119 CompletionInfo[] cInfo = new CompletionInfo[cnt];
120 for (int i = 0; i < cnt; i++) {
121 cInfo[i] = new CompletionInfo(i, i, suggestList.get(i));
122 }
123 post(() -> getContext().getSystemService(InputMethodManager.class)
124 .displayCompletions(this, cInfo));
125 }
126
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700127 private boolean showSoftInput() {
128 return requestFocus() &&
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -0800129 getContext().getSystemService(InputMethodManager.class)
Hyunyoung Song7e83ab92016-09-21 17:03:56 -0700130 .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700131 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700132
133 public void dispatchBackKey() {
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700134 hideKeyboard();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700135 if (mBackKeyListener != null) {
136 mBackKeyListener.onBackKey();
137 }
138 }
Jon Miranda54d4e642017-02-24 10:00:14 -0800139
140 /**
141 * Set to true when you want isSuggestionsEnabled to return false.
142 * Use this to disable the red underlines that appear under typos when suggestions is enabled.
143 */
144 public void forceDisableSuggestions(boolean forceDisableSuggestions) {
145 mForceDisableSuggestions = forceDisableSuggestions;
146 }
147
148 @Override
149 public boolean isSuggestionsEnabled() {
150 return !mForceDisableSuggestions && super.isSuggestionsEnabled();
151 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700152
153 public void reset() {
154 if (!TextUtils.isEmpty(getText())) {
155 setText("");
156 }
157 if (isFocused()) {
158 View nextFocus = focusSearch(View.FOCUS_DOWN);
159 if (nextFocus != null) {
160 nextFocus.requestFocus();
161 }
162 }
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700163 hideKeyboard();
Sunny Goyal326403e2017-10-02 12:45:10 -0700164 }
Winson Chungde34aa42015-05-07 18:21:28 -0700165}