blob: 4e0f2e7446c88c0ae824a9bab0f360698130b130 [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.
Winson Chungde34aa42015-05-07 18:21:28 -070032 */
Winson97b0d082015-08-13 15:18:25 -070033public class ExtendedEditText extends EditText {
Winson Chungde34aa42015-05-07 18:21:28 -070034
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070035 private boolean mShowImeAfterFirstLayout;
Jon Miranda54d4e642017-02-24 10:00:14 -080036 private boolean mForceDisableSuggestions = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070037
Winson Chungde34aa42015-05-07 18:21:28 -070038 /**
39 * Implemented by listeners of the back key.
40 */
41 public interface OnBackKeyListener {
Winson97b0d082015-08-13 15:18:25 -070042 public boolean onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070043 }
44
45 private OnBackKeyListener mBackKeyListener;
46
Winson97b0d082015-08-13 15:18:25 -070047 public ExtendedEditText(Context context) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070048 // ctor chaining breaks the touch handling
49 super(context);
Winson Chungde34aa42015-05-07 18:21:28 -070050 }
51
Winson97b0d082015-08-13 15:18:25 -070052 public ExtendedEditText(Context context, AttributeSet attrs) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070053 // ctor chaining breaks the touch handling
54 super(context, attrs);
Winson Chungde34aa42015-05-07 18:21:28 -070055 }
56
Winson97b0d082015-08-13 15:18:25 -070057 public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chungde34aa42015-05-07 18:21:28 -070058 super(context, attrs, defStyleAttr);
59 }
60
61 public void setOnBackKeyListener(OnBackKeyListener listener) {
62 mBackKeyListener = listener;
63 }
64
65 @Override
66 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
67 // If this is a back key, propagate the key back to the listener
68 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
69 if (mBackKeyListener != null) {
Winson97b0d082015-08-13 15:18:25 -070070 return mBackKeyListener.onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070071 }
72 return false;
73 }
74 return super.onKeyPreIme(keyCode, event);
75 }
Winson4e7a1012015-08-13 16:47:55 -070076
77 @Override
78 public boolean onDragEvent(DragEvent event) {
79 // We don't want this view to interfere with Launcher own drag and drop.
80 return false;
81 }
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070082
83 @Override
84 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
85 super.onLayout(changed, left, top, right, bottom);
86 if (mShowImeAfterFirstLayout) {
87 // soft input only shows one frame after the layout of the EditText happens,
88 post(new Runnable() {
89 @Override
90 public void run() {
91 showSoftInput();
92 mShowImeAfterFirstLayout = false;
93 }
94 });
95 }
96 }
97
98 public void showKeyboard() {
99 mShowImeAfterFirstLayout = !showSoftInput();
100 }
101
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700102 public void hideKeyboard() {
103 UiThreadHelper.hideKeyboardAsync(getContext(), getWindowToken());
104 }
105
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700106 private boolean showSoftInput() {
107 return requestFocus() &&
108 ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
Hyunyoung Song7e83ab92016-09-21 17:03:56 -0700109 .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700110 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700111
112 public void dispatchBackKey() {
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700113 hideKeyboard();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700114 if (mBackKeyListener != null) {
115 mBackKeyListener.onBackKey();
116 }
117 }
Jon Miranda54d4e642017-02-24 10:00:14 -0800118
119 /**
120 * Set to true when you want isSuggestionsEnabled to return false.
121 * Use this to disable the red underlines that appear under typos when suggestions is enabled.
122 */
123 public void forceDisableSuggestions(boolean forceDisableSuggestions) {
124 mForceDisableSuggestions = forceDisableSuggestions;
125 }
126
127 @Override
128 public boolean isSuggestionsEnabled() {
129 return !mForceDisableSuggestions && super.isSuggestionsEnabled();
130 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700131
132 public void reset() {
133 if (!TextUtils.isEmpty(getText())) {
134 setText("");
135 }
136 if (isFocused()) {
137 View nextFocus = focusSearch(View.FOCUS_DOWN);
138 if (nextFocus != null) {
139 nextFocus.requestFocus();
140 }
141 }
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700142 hideKeyboard();
Sunny Goyal326403e2017-10-02 12:45:10 -0700143 }
Winson Chungde34aa42015-05-07 18:21:28 -0700144}